diff mbox

[03/16] ibacm: use sysfs (in acm_if_is_ib) rather than ioctl(... SIOCGIFHWADDR ...) to read interface type

Message ID 1395985810-23822-4-git-send-email-sean.hefty@intel.com (mailing list archive)
State Not Applicable, archived
Headers show

Commit Message

Hefty, Sean March 28, 2014, 5:49 a.m. UTC
From: Ira Weiny <ira.weiny@intel.com>

Getting an interface type from sysfs is easier than using an ioctl when an
interface name is readily available (as is the case with netlink.)

In preparation for netlink support create a function which uses sysfs and use
it instead of ioctls.

Signed-off-by: Ira Weiny <ira.weiny@intel.com>
---
 linux/acme_linux.c |   34 +++++++++++++++++++++++++++-------
 1 files changed, 27 insertions(+), 7 deletions(-)
diff mbox

Patch

diff --git a/linux/acme_linux.c b/linux/acme_linux.c
index 201ff19..0bd1f75 100644
--- a/linux/acme_linux.c
+++ b/linux/acme_linux.c
@@ -38,6 +38,7 @@ 
 #include <netinet/in.h>
 #include <sys/socket.h>
 #include <sys/types.h>
+#include <errno.h>
 
 #include <infiniband/verbs.h>
 
@@ -101,6 +102,31 @@  get_sgid(char *ifname, union ibv_gid *sgid)
 	return ret;
 }
 
+static int acm_if_is_ib(char *ifname)
+{
+	unsigned type;
+	char buf[128];
+	FILE *f;
+	int ret;
+
+	snprintf(buf, sizeof buf, "//sys//class//net//%s//type", ifname);
+	f = fopen(buf, "r");
+	if (!f) {
+		printf("failed to open %s\n", buf);
+		return 0;
+	}
+
+	if (fgets(buf, sizeof buf, f)) {
+		type = strtol(buf, NULL, 0);
+		ret = (type == ARPHRD_INFINIBAND);
+	} else {
+		ret = 0;
+	}
+
+	fclose(f);
+	return ret;
+}
+
 static int
 get_devaddr(char *ifname, int *dev_index, uint8_t *port, uint16_t *pkey)
 {
@@ -189,13 +215,7 @@  int gen_addr_ip(FILE *f)
 			continue;
 		}
 
-		ret = ioctl(s, SIOCGIFHWADDR, &ifr[i]);
-		if (ret) {
-			printf("failed to get hw address %d\n", ret);
-			continue;
-		}
-
-		if (ifr[i].ifr_hwaddr.sa_family != ARPHRD_INFINIBAND)
+		if (!acm_if_is_ib(ifr[i].ifr_name))
 			continue;
 
 		ret = get_devaddr(ifr[i].ifr_name, &dev_index, &port, &pkey);