for_each_netdev() is a more direct interface than
lnet_ipif_enumerate(), so use it instead. Also get
address and 'up' status directly from the device.
This means we need to possible re-allocate the storage
space if there are lots of IP addresses.
However there is no need to resize the allocation down if we
over-allocated. This is only used once, and is freed soon
after it is allocated, so that is a false optimization.
Linux-commit:
0400cf406c32ac3968241cd528747d922b6c55c3
Change-Id: I1c1e7722c7b2b267dcb8134ae295a54f976d96ad
Signed-off-by: NeilBrown <neilb@suse.com>
Reviewed-on: https://review.whamcloud.com/33969
Tested-by: Jenkins
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: Doug Oucharek <dougso@me.com>
Reviewed-by: Sonia Sharma <sharmaso@whamcloud.com>
Reviewed-by: Oleg Drokin <green@whamcloud.com>
#define DEBUG_SUBSYSTEM S_LNET
#include <linux/ctype.h>
#define DEBUG_SUBSYSTEM S_LNET
#include <linux/ctype.h>
+#include <linux/inetdevice.h>
#include <linux/nsproxy.h>
#include <net/net_namespace.h>
#include <lnet/lib-lnet.h>
#include <linux/nsproxy.h>
#include <net/net_namespace.h>
#include <lnet/lib-lnet.h>
-lnet_ipaddr_enumerate (__u32 **ipaddrsp)
+lnet_ipaddr_enumerate(u32 **ipaddrsp)
- int up;
- __u32 netmask;
- __u32 *ipaddrs;
- __u32 *ipaddrs2;
- int nip;
- char **ifnames;
- int nif = lnet_ipif_enumerate(&ifnames);
- int i;
- int rc;
-
- if (nif <= 0)
- return nif;
-
- LIBCFS_ALLOC(ipaddrs, nif * sizeof(*ipaddrs));
- if (ipaddrs == NULL) {
- CERROR("Can't allocate ipaddrs[%d]\n", nif);
- lnet_ipif_free_enumeration(ifnames, nif);
+ struct net_device *dev;
+ u32 *ipaddrs;
+ int nalloc = 64;
+ int nip = 0;
+
+ LIBCFS_ALLOC(ipaddrs, nalloc * sizeof(*ipaddrs));
+ if (!ipaddrs) {
+ CERROR("Can't allocate ipaddrs[%d]\n", nalloc);
- for (i = nip = 0; i < nif; i++) {
- if (!strcmp(ifnames[i], "lo"))
+ rtnl_lock();
+ for_each_netdev(&init_net, dev) {
+ struct in_device *in_dev;
+
+ if (strcmp(dev->name, "lo") == 0)
- rc = lnet_ipif_query(ifnames[i], &up,
- &ipaddrs[nip], &netmask);
- if (rc != 0) {
- CWARN("Can't query interface %s: %d\n",
- ifnames[i], rc);
+ if (!(dev_get_flags(dev) & IFF_UP)) {
+ CWARN("Ignoring interface %s: it's down\n", dev->name);
- if (!up) {
- CWARN("Ignoring interface %s: it's down\n",
- ifnames[i]);
+ in_dev = __in_dev_get_rtnl(dev);
+ if (!in_dev) {
+ CWARN("Interface %s has no IPv4 status.\n", dev->name);
- nip++;
- }
-
- lnet_ipif_free_enumeration(ifnames, nif);
+ if (nip >= nalloc) {
+ u32 *ipaddrs2;
- if (nip == nif) {
- *ipaddrsp = ipaddrs;
- } else {
- if (nip > 0) {
- LIBCFS_ALLOC(ipaddrs2, nip * sizeof(*ipaddrs2));
- if (ipaddrs2 == NULL) {
+ nalloc += nalloc;
+ ipaddrs2 = krealloc(ipaddrs, nalloc * sizeof(*ipaddrs2),
+ GFP_KERNEL);
+ if (!ipaddrs2) {
+ kfree(ipaddrs);
CERROR("Can't allocate ipaddrs[%d]\n", nip);
CERROR("Can't allocate ipaddrs[%d]\n", nip);
- nip = -ENOMEM;
- } else {
- memcpy(ipaddrs2, ipaddrs,
- nip * sizeof(*ipaddrs));
- *ipaddrsp = ipaddrs2;
- rc = nip;
- lnet_ipaddr_free_enumeration(ipaddrs, nif);
+
+ for_primary_ifa(in_dev)
+ if (strcmp(ifa->ifa_label, dev->name) == 0) {
+ ipaddrs[nip++] = ifa->ifa_local;
+ break;
+ }
+ endfor_ifa(in_dev);
+ rtnl_unlock();
+
+ *ipaddrsp = ipaddrs;