lnet: reduce code in lib-socket
cover lnet: reduce code in lib-socket
lib-socket contains code to iterate over network interfaces and determine the IPv4 address of each. It does this but using socket-ioctls.
There are more direct interfaces within the kernel for accessing this information, so change to use those directly.
---
NeilBrown (5):
lustre: socklnd: use for_each_netdev() instead of lnet_ipif_enumerate()
lustre: socklnd: use ksocknal_enumerate_interfaces for individual interfaces.
lustre: change lnet_ipaddr_enumerate() to use for_each_netdev()
lustre: o2iblnd: get IP address more directly.
lustre: lnet: remove lnet_ipif_enumerate()
.../staging/lustre/include/linux/lnet/lib-lnet.h | 3 .../staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c | 48 +++-- .../staging/lustre/lnet/klnds/socklnd/socklnd.c | 96 ++++----- drivers/staging/lustre/lnet/lnet/config.c | 78 +++---- drivers/staging/lustre/lnet/lnet/lib-socket.c | 211 -------------------- 5 files changed, 103 insertions(+), 333 deletions(-)
--
Signature
PATCH 1/5 lustre: socklnd: use for_each_netdev() instead of lnet_ipif_enumerate()
for_each_netdev() is a more direct interface and doesn't require library support.
Also get the ip address directly from the net_device, rather than using lnet_ipif_query().
--- .../staging/lustre/lnet/klnds/socklnd/socklnd.c | 63 +++++++++----------- 1 file changed, 28 insertions(+), 35 deletions(-)
drivers/staging/lustre/lnet/klnds/socklnd/socklnd.c +28 -35
@@ -39,6 +39,7 @@*/ #include "socklnd.h" +#include <linux/inetdevice.h> static struct lnet_lnd the_ksocklnd;struct ksock_nal_data ksocknal_data;
@@ -2614,53 +2615,45 @@ ksocknal_shutdown(struct lnet_ni *ni)static int ksocknal_enumerate_interfaces (struct ksock_net *net) { -char **names; -int i; -int j; -int rc; -int n; - - n =lnet_ipif_enumerate (&names); -if (n <=0 ) { -CERROR ("Can't enumerate interfaces: %d\n" , n); -return n; - } +int j =0 ; +struct net_device *dev; -for (i = j =0 ; i < n; i++) { -int up; -__u32 ip; -__u32 mask; +rtnl_lock (); +for_each_netdev (&init_net, dev) { +const char *name = dev->name; +struct in_device *in_dev; +struct ksock_interface *ksi = &net->ksnn_interfaces[j]; -if (!strcmp (names[i],"lo" ))/* skip the loopback IF */ +if (strcmp (name,"lo" ) ==0 )/* skip the loopback IF */ continue ; - rc =lnet_ipif_query (names[i], &up, &ip, &mask); -if (rc) { -CWARN ("Can't get interface %s info: %d\n" , - names[i], rc); -continue ; - } - -if (!up) { -CWARN ("Ignoring interface %s (down)\n" , - names[i]); +if (!(dev_get_flags (dev) &IFF_UP )) { +CWARN ("Ignoring interface %s (down)\n" , name);continue ; }if (j ==LNET_MAX_INTERFACES ) {CWARN ("Ignoring interface %s (too many interfaces)\n" , - names[i]); + name);continue ; } - - net->ksnn_interfaces[j].ksni_ipaddr = ip; - net->ksnn_interfaces[j].ksni_netmask = mask; -strlcpy (net->ksnn_interfaces[j].ksni_name, - names[i],sizeof (net->ksnn_interfaces[j].ksni_name)); - j++; + in_dev =__in_dev_get_rtnl (dev); +if (!in_dev) { +CWARN ("Interface %s has no IPv4 status.\n" , name); +continue ; + } +for_primary_ifa (in_dev) + if (strcmp(ifa->ifa_label, name) == 0) { + ksi->ksni_ipaddr = ifa->ifa_local; + ksi->ksni_netmask = ifa->ifa_mask; +strlcpy (ksi->ksni_name, + name,sizeof (ksi->ksni_name)); + j++; +break ; + } +endfor_ifa (in_dev); } - -lnet_ipif_free_enumeration (names, n); +rtnl_unlock ();if (!j)CERROR ("Can't find any usable interfaces\n" );
PATCH 1/5 lustre: socklnd: use for_each_netdev() instead of lnet_ipif_enumerate()
Reviewed-by: Doug Oucharek <
dougso at me.com<mailto:[email protected]>
Quoted 1 line
On Jul 24, 2018, at 4:07 PM, NeilBrown <neilb at suse.com<mailto:[email protected]>> wrote:
for_each_netdev() is a more direct interface and doesn't require library support.
Also get the ip address directly from the net_device, rather than using lnet_ipif_query().
---.../staging/lustre/lnet/klnds/socklnd/socklnd.c | 63 +++++++++-----------
1 file changed, 28 insertions(+), 35 deletions(-)
drivers/staging/lustre/lnet/klnds/socklnd/socklnd.c +0 -0
@@ -39,6 +39,7 @@*/
#include "socklnd.h"
+#include <linux/inetdevice.h>
static struct lnet_lnd the_ksocklnd;
struct ksock_nal_data ksocknal_data;
@@ -2614,53 +2615,45 @@ ksocknal_shutdown(struct lnet_ni *ni) static intksocknal_enumerate_interfaces(struct ksock_net *net) {
- char **names;
- int i;
- int j;
- int rc;
- int n;
-
- n = lnet_ipif_enumerate(&names);
- if (n <= 0) {
- CERROR("Can't enumerate interfaces: %d\n", n);
- return n;
- }
+ int j = 0;
+ struct net_device *dev;
- for (i = j = 0; i < n; i++) {
- int up;
- __u32 ip;
- __u32 mask;
+ rtnl_lock();
+ for_each_netdev(&init_net, dev) {
+ const char *name = dev->name;
+ struct in_device *in_dev;
+ struct ksock_interface *ksi = &net->ksnn_interfaces[j];
- if (!strcmp(names[i], "lo")) /* skip the loopback IF */
+ if (strcmp(name, "lo") == 0) /* skip the loopback IF */ continue;
- rc = lnet_ipif_query(names[i], &up, &ip, &mask);
- if (rc) {
- CWARN("Can't get interface %s info: %d\n",
- names[i], rc);
- continue;
- }
-
- if (!up) {
- CWARN("Ignoring interface %s (down)\n",
- names[i]);
+ if (!(dev_get_flags(dev) & IFF_UP)) {
+ CWARN("Ignoring interface %s (down)\n", name); continue;
}
if (j == LNET_MAX_INTERFACES) { CWARN("Ignoring interface %s (too many interfaces)\n",
- names[i]);
+ name);
continue;
}
-
- net->ksnn_interfaces[j].ksni_ipaddr = ip;
- net->ksnn_interfaces[j].ksni_netmask = mask;
- strlcpy(net->ksnn_interfaces[j].ksni_name,
- names[i], sizeof(net->ksnn_interfaces[j].ksni_name));
- j++;
+ in_dev = __in_dev_get_rtnl(dev);
+ if (!in_dev) {
+ CWARN("Interface %s has no IPv4 status.\n", name);
+ continue;
+ }
+ for_primary_ifa(in_dev)
+ if (strcmp(ifa->ifa_label, name) == 0) {
+ ksi->ksni_ipaddr = ifa->ifa_local;
+ ksi->ksni_netmask = ifa->ifa_mask;
+ strlcpy(ksi->ksni_name,
+ name, sizeof(ksi->ksni_name));
+ j++;
+ break;
+ }
+ endfor_ifa(in_dev);
}
-
- lnet_ipif_free_enumeration(names, n);
+ rtnl_unlock();
if (!j)CERROR("Can't find any usable interfaces\n");
_______________________________________________
lustre-devel mailing list
lustre-devel at lists.lustre.org<mailto:[email protected]>
http://lists.lustre.org/listinfo.cgi/lustre-devel-lustre.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lustre.org/pipermail/lustre-devel-lustre.org/attachments/20180725/59bb2f7c/attachment.html>
PATCH 2/5 lustre: socklnd: use ksocknal_enumerate_interfaces for individual interfaces.
In reply to NeilBrownWhen individual interfaces are listed, such a via a module parameter, it is easy to use ksocknal_enumerate_interfaces() to find them, and save duplicating code.
--- .../staging/lustre/lnet/klnds/socklnd/socklnd.c | 37 ++++++-------------- 1 file changed, 12 insertions(+), 25 deletions(-)
drivers/staging/lustre/lnet/klnds/socklnd/socklnd.c +12 -25
@@ -2613,7 +2613,7 @@ ksocknal_shutdown(struct lnet_ni *ni) }static int -ksocknal_enumerate_interfaces (struct ksock_net *net) +ksocknal_enumerate_interfaces (struct ksock_net *net,char *iname) {int j =0 ;struct net_device *dev;
@@ -2622,10 +2622,13 @@ ksocknal_enumerate_interfaces(struct ksock_net *net)for_each_netdev (&init_net, dev) {const char *name = dev->name;struct in_device *in_dev; -struct ksock_interface *ksi = &net->ksnn_interfaces[j]; +struct ksock_interface *ksi = + &net->ksnn_interfaces[net->ksnn_ninterfaces + j];if (strcmp (name,"lo" ) ==0 )/* skip the loopback IF */ continue ; +if (iname &&strcmp (name, iname) !=0 ) +continue ;if (!(dev_get_flags (dev) &IFF_UP )) {CWARN ("Ignoring interface %s (down)\n" , name);
@@ -2655,7 +2658,7 @@ ksocknal_enumerate_interfaces(struct ksock_net *net) }rtnl_unlock (); -if (!j) +if (!iname && !j)CERROR ("Can't find any usable interfaces\n" );return j;
@@ -2802,40 +2805,24 @@ ksocknal_startup(struct lnet_ni *ni) ni->ni_peertxcredits = *ksocknal_tunables.ksnd_peertxcredits; ni->ni_peerrtrcredits = *ksocknal_tunables.ksnd_peerrtrcredits; + net->ksnn_ninterfaces =0 ;if (!ni->ni_interfaces[0 ]) { - rc =ksocknal_enumerate_interfaces (net); + rc =ksocknal_enumerate_interfaces (net,NULL );if (rc <=0 ) goto fail_1; +/* FIXME why is this 1, not rc ?? */ net->ksnn_ninterfaces =1 ; }else {for (i =0 ; i <LNET_MAX_INTERFACES ; i++) { -int up; -if (!ni->ni_interfaces[i])break ; + rc =ksocknal_enumerate_interfaces (net, ni->ni_interfaces[i]); - rc =lnet_ipif_query (ni->ni_interfaces[i], &up, - &net->ksnn_interfaces[i].ksni_ipaddr, - &net->ksnn_interfaces[i].ksni_netmask); - -if (rc) { -CERROR ("Can't get interface %s info: %d\n" , - ni->ni_interfaces[i], rc); - goto fail_1; - } - -if (!up) { -CERROR ("Interface %s is down\n" , - ni->ni_interfaces[i]); +if (rc <=0 ) goto fail_1; - } - -strlcpy (net->ksnn_interfaces[i].ksni_name, - ni->ni_interfaces[i], -sizeof (net->ksnn_interfaces[i].ksni_name)); + net->ksnn_ninterfaces += rc; } - net->ksnn_ninterfaces = i; }/* call it before add it to ksocknal_data.ksnd_nets */
PATCH 2/5 lustre: socklnd: use ksocknal_enumerate_interfaces for individual interfaces.
With regards to the FIXME comment:
That does seem to be a bug. There is an assumption that we will only get one address back from the enumerate call. That has probably been true in the field thus far. Should be safe to change it to rc as the comment indicates.
Doug
On Jul 24, 2018, at 4:07 PM, NeilBrown <neilb at suse.com<mailto:[email protected]>> wrote:
When individual interfaces are listed, such a via a module parameter, it is easy to use ksocknal_enumerate_interfaces() to find them, and save duplicating code.
---.../staging/lustre/lnet/klnds/socklnd/socklnd.c | 37 ++++++--------------
1 file changed, 12 insertions(+), 25 deletions(-)
drivers/staging/lustre/lnet/klnds/socklnd/socklnd.c +0 -0
@@ -2613,7 +2613,7 @@ ksocknal_shutdown(struct lnet_ni *ni)
}
static int
-ksocknal_enumerate_interfaces(struct ksock_net *net) +ksocknal_enumerate_interfaces(struct ksock_net *net, char *iname) {
int j = 0;
struct net_device *dev;
@@ -2622,10 +2622,13 @@ ksocknal_enumerate_interfaces(struct ksock_net *net) for_each_netdev(&init_net, dev) {
const char *name = dev->name;
struct in_device *in_dev;
- struct ksock_interface *ksi = &net->ksnn_interfaces[j];
+ struct ksock_interface *ksi =
+ &net->ksnn_interfaces[net->ksnn_ninterfaces + j];
if (strcmp(name, "lo") == 0) /* skip the loopback IF */ continue;
+ if (iname && strcmp(name, iname) != 0)
+ continue;
if (!(dev_get_flags(dev) & IFF_UP)) {CWARN("Ignoring interface %s (down)\n", name);
@@ -2655,7 +2658,7 @@ ksocknal_enumerate_interfaces(struct ksock_net *net) }rtnl_unlock();
- if (!j)
+ if (!iname && !j)CERROR("Can't find any usable interfaces\n");
return j;
@@ -2802,40 +2805,24 @@ ksocknal_startup(struct lnet_ni *ni) ni->ni_peertxcredits = *ksocknal_tunables.ksnd_peertxcredits; ni->ni_peerrtrcredits = *ksocknal_tunables.ksnd_peerrtrcredits;
+ net->ksnn_ninterfaces = 0;
if (!ni->ni_interfaces[0]) {
- rc = ksocknal_enumerate_interfaces(net);
+ rc = ksocknal_enumerate_interfaces(net, NULL);
if (rc <= 0)
goto fail_1;
+ /* FIXME why is this 1, not rc ?? */ net->ksnn_ninterfaces = 1;
} else {
for (i = 0; i < LNET_MAX_INTERFACES; i++) {
- int up;
-
if (!ni->ni_interfaces[i])
break;
+ rc = ksocknal_enumerate_interfaces(net, ni->ni_interfaces[i]);
- rc = lnet_ipif_query(ni->ni_interfaces[i], &up,
- &net->ksnn_interfaces[i].ksni_ipaddr,
- &net->ksnn_interfaces[i].ksni_netmask);
-
- if (rc) {
- CERROR("Can't get interface %s info: %d\n",
- ni->ni_interfaces[i], rc);
- goto fail_1;
- }
-
- if (!up) {
- CERROR("Interface %s is down\n",
- ni->ni_interfaces[i]);
+ if (rc <= 0)
goto fail_1;
- }
-
- strlcpy(net->ksnn_interfaces[i].ksni_name,
- ni->ni_interfaces[i],
- sizeof(net->ksnn_interfaces[i].ksni_name));
+ net->ksnn_ninterfaces += rc;
}
- net->ksnn_ninterfaces = i;
}
/* call it before add it to ksocknal_data.ksnd_nets */
_______________________________________________
lustre-devel mailing list
lustre-devel at lists.lustre.org<mailto:[email protected]>
http://lists.lustre.org/listinfo.cgi/lustre-devel-lustre.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lustre.org/pipermail/lustre-devel-lustre.org/attachments/20180725/65b89749/attachment-0001.html>
PATCH 3/5 lustre: change lnet_ipaddr_enumerate() to use for_each_netdev()
In reply to NeilBrownfor_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.
--- drivers/staging/lustre/lnet/lnet/config.c | 78 +++++++++++++---------------- 1 file changed, 35 insertions(+), 43 deletions(-)
drivers/staging/lustre/lnet/lnet/config.c +35 -43
@@ -36,6 +36,7 @@#include <net/net_namespace.h> #include <linux/ctype.h> #include <linux/lnet/lib-lnet.h> +#include <linux/inetdevice.h> struct lnet_text_buf {/* tmp struct for parsing routes */ struct list_head ltb_list;/* stash on lists */
@@ -1134,66 +1135,57 @@ lnet_match_networks(char **networksp, char *ip2nets, __u32 *ipaddrs, int nip)static int lnet_ipaddr_enumerate (__u32 **ipaddrsp) { -int up; -__u32 netmask; +struct net_device *dev;__u32 *ipaddrs; -__u32 *ipaddrs2; +int nalloc =64 ;int nip; -char **ifnames; -int nif =lnet_ipif_enumerate (&ifnames); -int i; -int rc; -if (nif <=0 ) -return nif; - - ipaddrs =kcalloc (nif,sizeof (*ipaddrs),GFP_KERNEL ); + ipaddrs =kcalloc (nalloc,sizeof (*ipaddrs),GFP_KERNEL );if (!ipaddrs) { -CERROR ("Can't allocate ipaddrs[%d]\n" , nif); -lnet_ipif_free_enumeration (ifnames, nif); +CERROR ("Can't allocate ipaddrs[%d]\n" , nalloc);return -ENOMEM ; } -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 )continue ; - rc =lnet_ipif_query (ifnames[i], &up, &ipaddrs[nip], &netmask); -if (rc) { -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);continue ; } - -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);continue ; } - nip++; - } - -lnet_ipif_free_enumeration (ifnames, nif); - -if (nip == nif) { - *ipaddrsp = ipaddrs; - }else { -if (nip >0 ) { - ipaddrs2 =kcalloc (nip,sizeof (*ipaddrs2), -GFP_KERNEL ); -if (!ipaddrs2) { -CERROR ("Can't allocate ipaddrs[%d]\n" , nip); - nip = -ENOMEM ; - }else { -memcpy (ipaddrs2, ipaddrs, - nip *sizeof (*ipaddrs)); - *ipaddrsp = ipaddrs2; - rc = nip; +if (nip >= nalloc) { +__u32 *ipaddrs2; + nalloc += nalloc; + ipaddrs2 =krealloc (ipaddrs, nalloc *sizeof (*ipaddrs2), +GFP_KERNEL ); +if (ipaddrs2 ==NULL ) { +kfree (ipaddrs); +CERROR ("Can't allocate ipaddrs[%d]\n" , nalloc); +return -ENOMEM ; } + ipaddrs = ipaddrs2; } -kfree (ipaddrs); + +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;return nip; }
PATCH 3/5 lustre: change lnet_ipaddr_enumerate() to use for_each_netdev()
Note:
I did not think your changes would collide with Multi-Rail. Unfortunately, it looks like this one does. This config code was changed quite a bit by the Multi-Rail feature. However, it is ok as landing this change now will prompt the Multi-Rail changes to utilize the better way of getting interface information. Just a heads up to Multi-Rail porters.
Doug
On Jul 24, 2018, at 4:07 PM, NeilBrown <neilb at suse.com<mailto:[email protected]>> wrote:
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.
---drivers/staging/lustre/lnet/lnet/config.c | 78 +++++++++++++----------------
1 file changed, 35 insertions(+), 43 deletions(-)
drivers/staging/lustre/lnet/lnet/config.c +0 -0
@@ -36,6 +36,7 @@
#include <net/net_namespace.h>
#include <linux/ctype.h>
#include <linux/lnet/lib-lnet.h>
+#include <linux/inetdevice.h>
struct lnet_text_buf { /* tmp struct for parsing routes */
struct list_head ltb_list; /* stash on lists */
@@ -1134,66 +1135,57 @@ lnet_match_networks(char **networksp, char *ip2nets, __u32 *ipaddrs, int nip) static intlnet_ipaddr_enumerate(__u32 **ipaddrsp)
{
- int up;
- __u32 netmask;
+ struct net_device *dev;__u32 *ipaddrs;
- __u32 *ipaddrs2;
+ int nalloc = 64;
int nip;
- char **ifnames;
- int nif = lnet_ipif_enumerate(&ifnames);
- int i;
- int rc;
- if (nif <= 0)
- return nif;
-
- ipaddrs = kcalloc(nif, sizeof(*ipaddrs), GFP_KERNEL);
+ ipaddrs = kcalloc(nalloc, sizeof(*ipaddrs), GFP_KERNEL); if (!ipaddrs) {
- CERROR("Can't allocate ipaddrs[%d]\n", nif);
- lnet_ipif_free_enumeration(ifnames, nif);
+ CERROR("Can't allocate ipaddrs[%d]\n", nalloc); return -ENOMEM;
}
- 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)
continue;
- rc = lnet_ipif_query(ifnames[i], &up, &ipaddrs[nip], &netmask);
- if (rc) {
- 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); continue;
}
-
- 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); continue;
}
- nip++;
- }
-
- lnet_ipif_free_enumeration(ifnames, nif);
-
- if (nip == nif) {
- *ipaddrsp = ipaddrs;
- } else {
- if (nip > 0) {
- ipaddrs2 = kcalloc(nip, sizeof(*ipaddrs2),
- GFP_KERNEL);
- if (!ipaddrs2) {
- CERROR("Can't allocate ipaddrs[%d]\n", nip);
- nip = -ENOMEM;
- } else {
- memcpy(ipaddrs2, ipaddrs,
- nip * sizeof(*ipaddrs));
- *ipaddrsp = ipaddrs2;
- rc = nip;
+ if (nip >= nalloc) {
+ __u32 *ipaddrs2;
+ nalloc += nalloc;
+ ipaddrs2 = krealloc(ipaddrs, nalloc * sizeof(*ipaddrs2),
+ GFP_KERNEL);
+ if (ipaddrs2 == NULL) {
+ kfree(ipaddrs);
+ CERROR("Can't allocate ipaddrs[%d]\n", nalloc);
+ return -ENOMEM;
}
+ ipaddrs = ipaddrs2;
}
- kfree(ipaddrs);
+
+ 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;
return nip;
}
_______________________________________________
lustre-devel mailing list
lustre-devel at lists.lustre.org<mailto:[email protected]>
http://lists.lustre.org/listinfo.cgi/lustre-devel-lustre.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lustre.org/pipermail/lustre-devel-lustre.org/attachments/20180725/e7ceb368/attachment-0001.html>
PATCH 3/5 lustre: change lnet_ipaddr_enumerate() to use for_each_netdev()
Hi Doug,
thanks for your review to this patch and the others.
Thanks particularly for noticing the FIXME :-) I'll make that change.
Thanks,
NeilBrown
Quoted 140 lines · Doug Oucharek
Reviewed-by Doug Oucharek <dougso at me.com<mailto:[email protected]>>Note:
I did not think your changes would collide with Multi-Rail. Unfortunately, it looks like this one does. This config code was changed quite a bit by the Multi-Rail feature. However, it is ok as landing this change now will prompt the Multi-Rail changes to utilize the better way of getting interface information. Just a heads up to Multi-Rail porters.Doug
On Jul 24, 2018, at 4:07 PM, NeilBrown <neilb at suse.com<mailto:[email protected]>> wrote:
for_each_netdev()is a more direct interface thanlnet_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.
Signed-off-by NeilBrown <neilb at suse.com<mailto:[email protected]>>---
drivers/staging/lustre/lnet/lnet/config.c| 78 +++++++++++++----------------1 file changed, 35 insertions(+), 43 deletions(-)drivers/staging/lustre/lnet/lnet/config.c +1
-0@@ -36,6 +36,7 @@#include <net/net_namespace.h> #include <linux/ctype.h> #include <linux/lnet/lib-lnet.h> +#include <linux/inetdevice.h> struct
lnet_text_buf{ /* tmp struct for parsing routes */
structlist_headltb_list; /* stash on lists */drivers/staging/lustre/lnet/lnet/config.c +0
-0@@ -1134,66 +1135,57 @@ lnet_match_networks(char **networksp, char *ip2nets, __u32 *ipaddrs, int nip)static int
lnet_ipaddr_enumerate(__u32 **ipaddrsp)
{
- int up;
-__u32netmask;
+ structnet_device*dev;__u32*ipaddrs;
-__u32*ipaddrs2;
+ int nalloc = 64;
int nip;
- char **ifnames;
- int nif =lnet_ipif_enumerate(&ifnames);
- int i;
- int rc;- if (nif <= 0)
- return nif;
-
- ipaddrs =kcalloc(nif, sizeof(*ipaddrs), GFP_KERNEL);
+ ipaddrs =kcalloc(nalloc, sizeof(*ipaddrs), GFP_KERNEL); if (!ipaddrs) {
-CERROR("Can't allocate ipaddrs[%d]\n", nif);
-lnet_ipif_free_enumeration(ifnames, nif);
+CERROR("Can't allocate ipaddrs[%d]\n", nalloc); return-ENOMEM;
}- for (i = nip = 0; i < nif; i++) {
- if (!strcmp(ifnames[i], "lo"))
+rtnl_lock();
+for_each_netdev(&init_net, dev){
+ structin_device*in_dev;
+
+ if (strcmp(dev->name, "lo")== 0)
continue;- rc =
lnet_ipif_query(ifnames[i], &up, &ipaddrs[nip], &netmask);
- if (rc) {
- 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); continue;
}drivers/staging/lustre/lnet/lnet/config.c +3
-4- -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);continue;
}- nip++;
- }drivers/staging/lustre/lnet/lnet/config.c +9
-17- -lnet_ipif_free_enumeration (ifnames, nif); - -if (nip == nif) { - *ipaddrsp = ipaddrs; - }else { -if (nip >0 ) { - ipaddrs2 =kcalloc (nip,sizeof (*ipaddrs2), -GFP_KERNEL ); -if (!ipaddrs2) { -CERROR ("Can't allocate ipaddrs[%d]\n" , nip); - nip = -ENOMEM ; - }else { -memcpy (ipaddrs2, ipaddrs, - nip *sizeof (*ipaddrs)); - *ipaddrsp = ipaddrs2; - rc = nip; +if (nip >= nalloc) { +__u32 *ipaddrs2; + nalloc += nalloc; + ipaddrs2 =krealloc (ipaddrs, nalloc *sizeof (*ipaddrs2), +GFP_KERNEL ); +if (ipaddrs2 ==NULL ) { +kfree (ipaddrs); +CERROR ("Can't allocate ipaddrs[%d]\n" , nalloc); +return -ENOMEM ;}
+ ipaddrs = ipaddrs2;
}
- kfree(ipaddrs);
+
+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;
return nip;
}
Quoted 4 lines
_______________________________________________
lustre-devel mailing list
lustre-devel at lists.lustre.org<mailto:[email protected]>
http://lists.lustre.org/listinfo.cgi/lustre-devel-lustre.org
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 832 bytes
Desc: not available
URL: <http://lists.lustre.org/pipermail/lustre-devel-lustre.org/attachments/20180726/b4ffb366/attachment.sig>
PATCH 4/5 lustre: o2iblnd: get IP address more directly.
In reply to NeilBrownUse dev_get_by_name() and for_primary_ifa() to
get IP address for a named device. This is more
direct.
--- .../staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c | 48 +++++++++++++------- 1 file changed, 30 insertions(+), 18 deletions(-)
drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c +30 -18
@@ -38,6 +38,7 @@#include <asm/div64.h> #include <asm/page.h> #include "o2iblnd.h" +#include <linux/inetdevice.h> static struct lnet_lnd the_o2iblnd;
@@ -2450,40 +2451,48 @@ void kiblnd_destroy_dev(struct kib_dev *dev)static struct kib_dev *kiblnd_create_dev (char *ifname) {struct net_device *netdev; +struct in_device *in_dev;struct kib_dev *dev; -__u32 netmask; -__u32 ip; -int up; +int flags;int rc; - rc =lnet_ipif_query (ifname, &up, &ip, &netmask); -if (rc) { -CERROR ("Can't query IPoIB interface %s: %d\n" , - ifname, rc); -return NULL ; +rtnl_lock (); + netdev =dev_get_by_name (&init_net, ifname); +if (!netdev) { +CERROR ("Can't find IPoIB interface %s\n" , + ifname); + goto unlock; } -if (!up) { + flags =dev_get_flags (netdev); +if (!(flags &IFF_UP )) {CERROR ("Can't query IPoIB interface %s: it's down\n" , ifname); +rtnl_unlock ();return NULL ; } dev =kzalloc (sizeof (*dev),GFP_NOFS );if (!dev) -return NULL ; + goto unlock; - netdev =dev_get_by_name (&init_net, ifname); -if (!netdev) { - dev->ibd_can_failover =0 ; - }else { - dev->ibd_can_failover = !!(netdev->flags &IFF_MASTER ); -dev_put (netdev); - } + dev->ibd_can_failover = !!(flags &IFF_MASTER );INIT_LIST_HEAD (&dev->ibd_nets);INIT_LIST_HEAD (&dev->ibd_list);/* not yet in kib_devs */ INIT_LIST_HEAD (&dev->ibd_fail_list); - dev->ibd_ifip = ip; +for_primary_ifa (in_dev) + if (strcmp(ifa->ifa_label, ifname) == 0) { + dev->ibd_ifip = ifa->ifa_local; +break ; + } +endfor_ifa (in_dev); +rtnl_unlock (); + +if (dev->ibd_ifip ==0 ) { +CERROR ("Can't initialize device: no IP address\n" ); +kfree (dev); +return NULL ; + }strcpy (&dev->ibd_ifname[0 ], ifname);/* initialize the device */
@@ -2496,6 +2505,9 @@ static struct kib_dev *kiblnd_create_dev(char *ifname)list_add_tail (&dev->ibd_list, &kiblnd_data.kib_devs);return dev; +unlock: +rtnl_unlock (); +return NULL ; }static void kiblnd_base_shutdown (void )
PATCH 4/5 lustre: o2iblnd: get IP address more directly.
Doug
On Jul 24, 2018, at 4:07 PM, NeilBrown <neilb at suse.com<mailto:[email protected]>> wrote:
Use dev_get_by_name() and for_primary_ifa() to
get IP address for a named device. This is more
direct.
---.../staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c | 48 +++++++++++++-------
1 file changed, 30 insertions(+), 18 deletions(-)
drivers/staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c +0 -0
@@ -38,6 +38,7 @@
#include <asm/div64.h>
#include <asm/page.h>
#include "o2iblnd.h"
+#include <linux/inetdevice.h>
static struct lnet_lnd the_o2iblnd;
@@ -2450,40 +2451,48 @@ void kiblnd_destroy_dev(struct kib_dev *dev) static struct kib_dev *kiblnd_create_dev(char *ifname)
{
struct net_device *netdev;
+ struct in_device *in_dev;
struct kib_dev *dev;
- __u32 netmask;
- __u32 ip;
- int up;
+ int flags;
int rc;
- rc = lnet_ipif_query(ifname, &up, &ip, &netmask);
- if (rc) {
- CERROR("Can't query IPoIB interface %s: %d\n",
- ifname, rc);
- return NULL;
+ rtnl_lock();
+ netdev = dev_get_by_name(&init_net, ifname);
+ if (!netdev) {
+ CERROR("Can't find IPoIB interface %s\n",
+ ifname);
+ goto unlock;
}
- if (!up) {
+ flags = dev_get_flags(netdev);
+ if (!(flags & IFF_UP)) {CERROR("Can't query IPoIB interface %s: it's down\n", ifname);
+ rtnl_unlock();
return NULL;
}
dev = kzalloc(sizeof(*dev), GFP_NOFS);
if (!dev)
- return NULL;
+ goto unlock;
- netdev = dev_get_by_name(&init_net, ifname);
- if (!netdev) {
- dev->ibd_can_failover = 0;
- } else {
- dev->ibd_can_failover = !!(netdev->flags & IFF_MASTER);
- dev_put(netdev);
- }
+ dev->ibd_can_failover = !!(flags & IFF_MASTER);
INIT_LIST_HEAD(&dev->ibd_nets); INIT_LIST_HEAD(&dev->ibd_list); /* not yet in kib_devs */ INIT_LIST_HEAD(&dev->ibd_fail_list);
- dev->ibd_ifip = ip;
+ for_primary_ifa(in_dev)
+ if (strcmp(ifa->ifa_label, ifname) == 0) {
+ dev->ibd_ifip = ifa->ifa_local;
+ break;
+ }
+ endfor_ifa(in_dev);
+ rtnl_unlock();
+
+ if (dev->ibd_ifip == 0) {
+ CERROR("Can't initialize device: no IP address\n");
+ kfree(dev);
+ return NULL;
+ }strcpy(&dev->ibd_ifname[0], ifname);
/* initialize the device */
@@ -2496,6 +2505,9 @@ static struct kib_dev *kiblnd_create_dev(char *ifname)
list_add_tail(&dev->ibd_list, &kiblnd_data.kib_devs); return dev;
+unlock:
+ rtnl_unlock();
+ return NULL;
}
static void kiblnd_base_shutdown(void)
_______________________________________________
lustre-devel mailing list
lustre-devel at lists.lustre.org<mailto:[email protected]>
http://lists.lustre.org/listinfo.cgi/lustre-devel-lustre.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lustre.org/pipermail/lustre-devel-lustre.org/attachments/20180725/618c236d/attachment.html>
PATCH 5/5 lustre: lnet: remove lnet_ipif_enumerate()
In reply to NeilBrownAlso remove lnet_ipif_query() and related functions.
There are no longer any users of these functions, so remove them.
--- .../staging/lustre/include/linux/lnet/lib-lnet.h | 3 drivers/staging/lustre/lnet/lnet/lib-socket.c | 211 -------------------- 2 files changed, 214 deletions(-)
drivers/staging/lustre/include/linux/lnet/lib-lnet.h +0 -3
@@ -594,9 +594,6 @@ int lnet_acceptor_port(void);int lnet_acceptor_start (void );void lnet_acceptor_stop (void ); -int lnet_ipif_query (char *name,int *up,__u32 *ip,__u32 *mask); -int lnet_ipif_enumerate (char ***names); -void lnet_ipif_free_enumeration (char **names,int n);int lnet_sock_setbuf (struct socket *socket,int txbufsize,int rxbufsize);int lnet_sock_getbuf (struct socket *socket,int *txbufsize,int *rxbufsize);int lnet_sock_getaddr (struct socket *socket,bool remote,__u32 *ip,int *port);
drivers/staging/lustre/lnet/lnet/lib-socket.c +0 -211
217 lines not on this page · raw message
PATCH 5/5 lustre: lnet: remove lnet_ipif_enumerate()
Doug
On Jul 24, 2018, at 4:07 PM, NeilBrown <neilb at suse.com<mailto:[email protected]>> wrote:
Also remove lnet_ipif_query() and related functions.
There are no longer any users of these functions, so remove them.
---.../staging/lustre/include/linux/lnet/lib-lnet.h | 3drivers/staging/lustre/lnet/lnet/lib-socket.c | 211 --------------------
2 files changed, 214 deletions(-)
drivers/staging/lustre/include/linux/lnet/lib-lnet.h +0 -0
@@ -594,9 +594,6 @@ int lnet_acceptor_port(void);
int lnet_acceptor_start(void);
void lnet_acceptor_stop(void);
-int lnet_ipif_query(char *name, int *up, __u32 *ip, __u32 *mask); -int lnet_ipif_enumerate(char ***names);
-void lnet_ipif_free_enumeration(char **names, int n);
int lnet_sock_setbuf(struct socket *socket, int txbufsize, int rxbufsize); int lnet_sock_getbuf(struct socket *socket, int *txbufsize, int *rxbufsize); int lnet_sock_getaddr(struct socket *socket, bool remote, __u32 *ip, int *port);
drivers/staging/lustre/lnet/lnet/lib-socket.c +0 -0
@@ -43,217 +43,6 @@
#include <linux/lnet/lib-lnet.h>
-static int
-kernel_sock_unlocked_ioctl(struct file *filp, int cmd, unsigned long arg) -{
- mm_segment_t oldfs = get_fs();
- int err;
-
- set_fs(KERNEL_DS);
- err = filp->f_op->unlocked_ioctl(filp, cmd, arg);
- set_fs(oldfs);
-
- return err;
-}
-
-static int
-lnet_sock_ioctl(int cmd, unsigned long arg)
-{
- struct file *sock_filp;
- struct socket *sock;
- int rc;
-
- rc = sock_create(PF_INET, SOCK_STREAM, 0, &sock);
- if (rc) {
- CERROR("Can't create socket: %d\n", rc);
- return rc;
- }
-
- sock_filp = sock_alloc_file(sock, 0, NULL);
- if (IS_ERR(sock_filp))
- return PTR_ERR(sock_filp);
-
- rc = kernel_sock_unlocked_ioctl(sock_filp, cmd, arg);
-
- fput(sock_filp);
- return rc;
-}
-
-int
-lnet_ipif_query(char *name, int *up, __u32 *ip, __u32 *mask) -{
- struct ifreq ifr;
- int nob;
- int rc;
- __be32 val;
-
- nob = strnlen(name, IFNAMSIZ);
- if (nob == IFNAMSIZ) {
- CERROR("Interface name %s too long\n", name);
- return -EINVAL;
- }
-
- BUILD_BUG_ON(sizeof(ifr.ifr_name) < IFNAMSIZ);
-
- if (strlen(name) > sizeof(ifr.ifr_name) - 1)
- return -E2BIG;
- strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
-
- rc = lnet_sock_ioctl(SIOCGIFFLAGS, (unsigned long)&ifr);
- if (rc) {
- CERROR("Can't get flags for interface %s\n", name);
- return rc;
- }
-
- if (!(ifr.ifr_flags & IFF_UP)) {
- CDEBUG(D_NET, "Interface %s down\n", name);
- *up = 0;
- *ip = *mask = 0;
- return 0;
- }
- *up = 1;
-
- if (strlen(name) > sizeof(ifr.ifr_name) - 1)
- return -E2BIG;
- strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
-
- ifr.ifr_addr.sa_family = AF_INET;
- rc = lnet_sock_ioctl(SIOCGIFADDR, (unsigned long)&ifr);
- if (rc) {
- CERROR("Can't get IP address for interface %s\n", name);
- return rc;
- }
-
- val = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
- *ip = ntohl(val);
-
- if (strlen(name) > sizeof(ifr.ifr_name) - 1)
- return -E2BIG;
- strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
-
- ifr.ifr_addr.sa_family = AF_INET;
- rc = lnet_sock_ioctl(SIOCGIFNETMASK, (unsigned long)&ifr);
- if (rc) {
- CERROR("Can't get netmask for interface %s\n", name);
- return rc;
- }
-
- val = ((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr.s_addr;
- *mask = ntohl(val);
-
- return 0;
-}
-EXPORT_SYMBOL(lnet_ipif_query);
-
-int
-lnet_ipif_enumerate(char ***namesp)
-{
- /* Allocate and fill in 'names', returning # interfaces/error */
- char **names;
- int toobig;
- int nalloc;
- int nfound;
- struct ifreq *ifr;
- struct ifconf ifc;
- int rc;
- int nob;
- int i;
-
- nalloc = 16; /* first guess at max interfaces */
- toobig = 0;
- for (;;) {
- if (nalloc * sizeof(*ifr) > PAGE_SIZE) {
- toobig = 1;
- nalloc = PAGE_SIZE / sizeof(*ifr);
- CWARN("Too many interfaces: only enumerating first %d\n",
- nalloc);
- }
-
- ifr = kzalloc(nalloc * sizeof(*ifr), GFP_KERNEL);
- if (!ifr) {
- CERROR("ENOMEM enumerating up to %d interfaces\n",
- nalloc);
- rc = -ENOMEM;
- goto out0;
- }
-
- ifc.ifc_buf = (char *)ifr;
- ifc.ifc_len = nalloc * sizeof(*ifr);
-
- rc = lnet_sock_ioctl(SIOCGIFCONF, (unsigned long)&ifc);
- if (rc < 0) {
- CERROR("Error %d enumerating interfaces\n", rc);
- goto out1;
- }
-
- LASSERT(!rc);
-
- nfound = ifc.ifc_len / sizeof(*ifr);
- LASSERT(nfound <= nalloc);
-
- if (nfound < nalloc || toobig)
- break;
-
- kfree(ifr);
- nalloc *= 2;
- }
-
- if (!nfound)
- goto out1;
-
- names = kzalloc(nfound * sizeof(*names), GFP_KERNEL);
- if (!names) {
- rc = -ENOMEM;
- goto out1;
- }
-
- for (i = 0; i < nfound; i++) {
- nob = strnlen(ifr[i].ifr_name, IFNAMSIZ);
- if (nob == IFNAMSIZ) {
- /* no space for terminating NULL */
- CERROR("interface name %.*s too long (%d max)\n",
- nob, ifr[i].ifr_name, IFNAMSIZ);
- rc = -ENAMETOOLONG;
- goto out2;
- }
-
- names[i] = kmalloc(IFNAMSIZ, GFP_KERNEL);
- if (!names[i]) {
- rc = -ENOMEM;
- goto out2;
- }
-
- memcpy(names[i], ifr[i].ifr_name, nob);
- names[i][nob] = 0;
- }
-
- *namesp = names;
- rc = nfound;
-
-out2:
- if (rc < 0)
- lnet_ipif_free_enumeration(names, nfound);
-out1:
- kfree(ifr);
-out0:
- return rc;
-}
-EXPORT_SYMBOL(lnet_ipif_enumerate);
-
-void
-lnet_ipif_free_enumeration(char **names, int n)
-{
- int i;
-
- LASSERT(n > 0);
-
- for (i = 0; i < n && names[i]; i++)
- kfree(names[i]);
-
- kfree(names);
-}
-EXPORT_SYMBOL(lnet_ipif_free_enumeration);
-
intlnet_sock_write(struct socket *sock, void *buffer, int nob, int timeout) {
_______________________________________________
lustre-devel mailing list
lustre-devel at lists.lustre.org<mailto:[email protected]>
http://lists.lustre.org/listinfo.cgi/lustre-devel-lustre.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lustre.org/pipermail/lustre-devel-lustre.org/attachments/20180725/d8be92f2/attachment-0001.html>
cover lnet: reduce code in lib-socket
In reply to NeilBrownQuoted 6 lines
lib-socket contains code to iterate over network interfaces and determine the IPv4 address of each. It does this but using socket-ioctls.
There are more direct interfaces within the kernel for accessing this information, so change to use those directly.
With an updated based on Doug's reviewer to check the return value I give this a positive review for the patch series.
Quoted 21 lines
---
NeilBrown (5):
lustre: socklnd: usefor_each_netdev()instead oflnet_ipif_enumerate()
lustre: socklnd: useksocknal_enumerate_interfacesfor individual interfaces.
lustre: changelnet_ipaddr_enumerate()to usefor_each_netdev()
lustre: o2iblnd: get IP address more directly.
lustre: lnet: removelnet_ipif_enumerate().../staging/lustre/include/linux/lnet/lib-lnet.h | 3 .../staging/lustre/lnet/klnds/o2iblnd/o2iblnd.c | 48 +++-- .../staging/lustre/lnet/klnds/socklnd/socklnd.c | 96 ++++----- drivers/staging/lustre/lnet/lnet/config.c | 78 +++---- drivers/staging/lustre/lnet/lnet/lib-socket.c | 211 -------------------- 5 files changed, 103 insertions(+), 333 deletions(-)--
Signature