From d3723022ac8eeb7f7755b3711aafa203f804dcdc Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Thu, 5 Jun 2014 09:35:40 +0000 Subject: [PATCH] added code to determine local interface and interface irqs (not yet finished and POC!) git-svn-id: https://zerberus.eonerc.rwth-aachen.de:8443/svn/s2ss/trunk@61 8ec27952-4edc-4aab-86aa-e87bb2611832 --- include/if.h | 32 ++++++++++++++++++++++ src/if.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/node.c | 1 - 3 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 include/if.h create mode 100644 src/if.c diff --git a/include/if.h b/include/if.h new file mode 100644 index 000000000..77af27f9b --- /dev/null +++ b/include/if.h @@ -0,0 +1,32 @@ +/** Interface related functions + * + * @author Steffen Vogel + * @copyright 2014, Institute for Automation of Complex Power Systems, EONERC + * @file if.h + */ + +#ifndef _IF_H_ +#define _IF_H_ + +#include + +/** Get the name of first interface which listens on addr. + * + * @param addr The address whose related interface is searched + * @return + * - A pointer to the interface name (has to be freed by caller) + * - NULL if the address is not used on the system + */ +char* if_addrtoname(struct sockaddr *addr); + +/** Change the SMP affinity of NIC interrupts. + * + * @param ifname The interface whose IRQs should be pinned + * @param affinity A mask specifying which cores should handle this interrupt. + * @return + * - 0 on success + * - otherwise an error occured + */ +int if_setaffinity(const char *ifname, int affinity); + +#endif /* _IF_H_ */ diff --git a/src/if.c b/src/if.c new file mode 100644 index 000000000..ff8c125f5 --- /dev/null +++ b/src/if.c @@ -0,0 +1,77 @@ +/** + * Interface related functions + * + * @author Steffen Vogel + * @copyright 2014, Institute for Automation of Complex Power Systems, EONERC + */ + +#include +#include +#include + +#include +#include + +#include +#include +#include + +#include "utils.h" + +char* if_addrtoname(struct sockaddr *addr) +{ + char *ifname = NULL; + struct ifaddrs *ifas, *ifa; + + if(getifaddrs(&ifas)) + return NULL; + + for(ifa = ifas; ifa; ifa = ifa->ifa_next) { + if (ifa->ifa_addr && !sockaddr_cmp(ifa->ifa_addr, addr)) { + ifname = malloc(strlen(ifa->ifa_name) + 1); + if (!ifname) + goto out; + + strcpy(ifname, ifa->ifa_name); + goto out; + } + } + +out: + freeifaddrs(ifas); + + return ifname; +} + +int if_setaffinity(const char *ifname, int affinity) +{ + char *dirname[NAME_MAX]; + char *filename[NAME_MAX]; + + DIR *dir; + FILE *file; + + /* Determine IRQs numbers */ + snprintf(dirname, sizeof(dirname), "/sys/class/net/%s/device/msi_irqs/", ifname); + dir = opendir(dirname); + if (!dir) + return -1; + + struct dirent *entry; + while (entry = readdir(dir)) { + /* Set SMP affinity */ + snprintf(filename, sizeof(filename), "/proc/irq/%s/smp_affinity"); + file = fopen(filename, "w"); + if (!file) + continue; + + debug(3, "Setting SMP affinity of IRQ %s (%s) to %8x\n", entry->d_name, ifname, affinity); + + fprintf("%8x", affinity); + fclose(file); + } + + closedir(dir); + + return 0; +} diff --git a/src/node.c b/src/node.c index 360f5a3f8..68c57e3c3 100644 --- a/src/node.c +++ b/src/node.c @@ -95,4 +95,3 @@ struct node* node_lookup_name(const char *str, struct node *nodes, int len) return NULL; } -