Whamcloud - gitweb
e7dece441f6a55cfbd94cbb1b4533a447a22fff4
[fs/lustre-release.git] / lnet / lnet / config.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  */
31
32 #define DEBUG_SUBSYSTEM S_LNET
33
34 #include <linux/ctype.h>
35 #include <linux/inetdevice.h>
36 #include <linux/nsproxy.h>
37 #include <linux/ethtool.h>
38 #include <net/net_namespace.h>
39 #include <lnet/lib-lnet.h>
40 #include <net/addrconf.h>
41
42 /* tmp struct for parsing routes */
43 struct lnet_text_buf {
44         struct list_head        ltb_list;       /* stash on lists */
45         int                     ltb_size;       /* allocated size */
46         char                    ltb_text[0];    /* text buffer */
47 };
48
49 static int lnet_tbnob = 0;                      /* track text buf allocation */
50 #define LNET_MAX_TEXTBUF_NOB     (64<<10)       /* bound allocation */
51 #define LNET_SINGLE_TEXTBUF_NOB  (4<<10)
52
53 #define SPACESTR " \t\v\r\n"
54 #define DELIMITERS ":()[]"
55
56 static void
57 lnet_syntax(const char *name, const char *str, int offset, int width)
58 {
59         static char dots[LNET_SINGLE_TEXTBUF_NOB];
60         static char dashes[LNET_SINGLE_TEXTBUF_NOB];
61
62         memset(dots, '.', sizeof(dots));
63         dots[sizeof(dots)-1] = 0;
64         memset(dashes, '-', sizeof(dashes));
65         dashes[sizeof(dashes)-1] = 0;
66
67         LCONSOLE_ERROR_MSG(0x10f, "Error parsing '%s=\"%s\"'\n", name, str);
68         LCONSOLE_ERROR_MSG(0x110, "here...........%.*s..%.*s|%.*s|\n",
69                            (int)strlen(name), dots, offset, dots,
70                             (width < 1) ? 0 : width - 1, dashes);
71 }
72
73 static int
74 lnet_issep (char c)
75 {
76         switch (c) {
77         case '\n':
78         case '\r':
79         case ';':
80                 return 1;
81         default:
82                 return 0;
83         }
84 }
85
86 bool
87 lnet_net_unique(__u32 net_id, struct list_head *netlist,
88                 struct lnet_net **net)
89 {
90         struct lnet_net  *net_l;
91
92         if (!netlist)
93                 return true;
94
95         list_for_each_entry(net_l, netlist, net_list) {
96                 if (net_l->net_id == net_id) {
97                         if (net != NULL)
98                                 *net = net_l;
99                         return false;
100                 }
101         }
102
103         return true;
104 }
105
106 /* check that the NI is unique within the list of NIs already added to
107  * a network */
108 bool
109 lnet_ni_unique_net(struct list_head *nilist, char *iface)
110 {
111         struct list_head *tmp;
112         struct lnet_ni *ni;
113
114         list_for_each(tmp, nilist) {
115                 ni = list_entry(tmp, struct lnet_ni, ni_netlist);
116
117                 if (ni->ni_interface != NULL &&
118                     strncmp(ni->ni_interface,
119                             iface,
120                             LNET_MAX_STR_LEN) == 0)
121                         return false;
122         }
123
124         return true;
125 }
126 static bool
127 in_array(__u32 *array, __u32 size, __u32 value)
128 {
129         int i;
130
131         for (i = 0; i < size; i++) {
132                 if (array[i] == value)
133                         return false;
134         }
135
136         return true;
137 }
138
139 static int
140 lnet_net_append_cpts(__u32 *cpts, __u32 ncpts, struct lnet_net *net)
141 {
142         __u32 *added_cpts = NULL;
143         int i, j = 0, rc = 0;
144
145         /*
146          * no need to go futher since a subset of the NIs already exist on
147          * all CPTs
148          */
149         if (net->net_ncpts == LNET_CPT_NUMBER)
150                 return 0;
151
152         if (cpts == NULL) {
153                 /* there is an NI which will exist on all CPTs */
154                 if (net->net_cpts != NULL)
155                         CFS_FREE_PTR_ARRAY(net->net_cpts, net->net_ncpts);
156                 net->net_cpts = NULL;
157                 net->net_ncpts = LNET_CPT_NUMBER;
158                 return 0;
159         }
160
161         if (net->net_cpts == NULL) {
162                 CFS_ALLOC_PTR_ARRAY(net->net_cpts, ncpts);
163                 if (net->net_cpts == NULL)
164                         return -ENOMEM;
165                 memcpy(net->net_cpts, cpts, ncpts * sizeof(*net->net_cpts));
166                 net->net_ncpts = ncpts;
167                 return 0;
168         }
169
170         CFS_ALLOC_PTR_ARRAY(added_cpts, LNET_CPT_NUMBER);
171         if (added_cpts == NULL)
172                 return -ENOMEM;
173
174         for (i = 0; i < ncpts; i++) {
175                 if (!in_array(net->net_cpts, net->net_ncpts, cpts[i])) {
176                         added_cpts[j] = cpts[i];
177                         j++;
178                 }
179         }
180
181         /* append the new cpts if any to the list of cpts in the net */
182         if (j > 0) {
183                 __u32 *array = NULL, *loc;
184                 __u32 total_entries = j + net->net_ncpts;
185
186                 CFS_ALLOC_PTR_ARRAY(array, total_entries);
187                 if (array == NULL) {
188                         rc = -ENOMEM;
189                         goto failed;
190                 }
191
192                 memcpy(array, net->net_cpts,
193                        net->net_ncpts * sizeof(*net->net_cpts));
194                 loc = array + net->net_ncpts;
195                 memcpy(loc, added_cpts, j * sizeof(*net->net_cpts));
196
197                 CFS_FREE_PTR_ARRAY(net->net_cpts, net->net_ncpts);
198                 net->net_ncpts = total_entries;
199                 net->net_cpts = array;
200         }
201
202 failed:
203         CFS_FREE_PTR_ARRAY(added_cpts, LNET_CPT_NUMBER);
204
205         return rc;
206 }
207
208 static void
209 lnet_net_remove_cpts(__u32 *cpts, __u32 ncpts, struct lnet_net *net)
210 {
211         struct lnet_ni *ni;
212         int rc;
213
214         /*
215          * Operation Assumption:
216          *      This function is called after an NI has been removed from
217          *      its parent net.
218          *
219          * if we're removing an NI which exists on all CPTs then
220          * we have to check if any of the other NIs on this net also
221          * exists on all CPTs. If none, then we need to build our Net CPT
222          * list based on the remaining NIs.
223          *
224          * If the NI being removed exist on a subset of the CPTs then we
225          * alo rebuild the Net CPT list based on the remaining NIs, which
226          * should resutl in the expected Net CPT list.
227          */
228
229         /*
230          * sometimes this function can be called due to some failure
231          * creating an NI, before any of the cpts are allocated, so check
232          * for that case and don't do anything
233          */
234         if (ncpts == 0)
235                 return;
236
237         if (ncpts == LNET_CPT_NUMBER) {
238                 /*
239                  * first iteration through the NI list in the net to see
240                  * if any of the NIs exist on all the CPTs. If one is
241                  * found then our job is done.
242                  */
243                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
244                         if (ni->ni_ncpts == LNET_CPT_NUMBER)
245                                 return;
246                 }
247         }
248
249         /*
250          * Rebuild the Net CPT list again, thereby only including only the
251          * CPTs which the remaining NIs are associated with.
252          */
253         if (net->net_cpts != NULL) {
254                 CFS_FREE_PTR_ARRAY(net->net_cpts, net->net_ncpts);
255                 net->net_cpts = NULL;
256         }
257
258         list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
259                 rc = lnet_net_append_cpts(ni->ni_cpts, ni->ni_ncpts,
260                                           net);
261                 if (rc != 0) {
262                         CERROR("Out of Memory\n");
263                         /*
264                          * do our best to keep on going. Delete
265                          * the net cpts and set it to NULL. This
266                          * way we can keep on going but less
267                          * efficiently, since memory accesses might be
268                          * accross CPT lines.
269                          */
270                         if (net->net_cpts != NULL) {
271                                 CFS_FREE_PTR_ARRAY(net->net_cpts,
272                                                    net->net_ncpts);
273                                 net->net_cpts = NULL;
274                                 net->net_ncpts = LNET_CPT_NUMBER;
275                         }
276                         return;
277                 }
278         }
279 }
280
281 void
282 lnet_ni_free(struct lnet_ni *ni)
283 {
284         lnet_net_remove_cpts(ni->ni_cpts, ni->ni_ncpts, ni->ni_net);
285
286         if (ni->ni_refs != NULL)
287                 cfs_percpt_free(ni->ni_refs);
288
289         if (ni->ni_tx_queues != NULL)
290                 cfs_percpt_free(ni->ni_tx_queues);
291
292         if (ni->ni_cpts != NULL)
293                 cfs_expr_list_values_free(ni->ni_cpts, ni->ni_ncpts);
294
295         if (ni->ni_interface != NULL) {
296                 LIBCFS_FREE(ni->ni_interface,
297                             strlen(ni->ni_interface) + 1);
298         }
299
300         /* release reference to net namespace */
301         if (ni->ni_net_ns != NULL)
302                 put_net(ni->ni_net_ns);
303
304         LIBCFS_FREE(ni, sizeof(*ni));
305 }
306
307 void
308 lnet_net_free(struct lnet_net *net)
309 {
310         struct list_head *tmp, *tmp2;
311         struct lnet_ni *ni;
312
313         LASSERT(list_empty(&net->net_ni_zombie));
314
315         /*
316          * delete any nis that haven't been added yet. This could happen
317          * if there is a failure on net startup
318          */
319         list_for_each_safe(tmp, tmp2, &net->net_ni_added) {
320                 ni = list_entry(tmp, struct lnet_ni, ni_netlist);
321                 list_del_init(&ni->ni_netlist);
322                 lnet_ni_free(ni);
323         }
324
325         /* delete any nis which have been started. */
326         list_for_each_safe(tmp, tmp2, &net->net_ni_list) {
327                 ni = list_entry(tmp, struct lnet_ni, ni_netlist);
328                 list_del_init(&ni->ni_netlist);
329                 lnet_ni_free(ni);
330         }
331
332         if (net->net_cpts != NULL) {
333                 CFS_FREE_PTR_ARRAY(net->net_cpts, net->net_ncpts);
334                 net->net_ncpts = LNET_CPT_NUMBER;
335                 net->net_cpts = NULL;
336         }
337
338         LIBCFS_FREE(net, sizeof(*net));
339 }
340
341 struct lnet_net *
342 lnet_net_alloc(__u32 net_id, struct list_head *net_list)
343 {
344         struct lnet_net         *net;
345
346         if (!lnet_net_unique(net_id, net_list, &net)) {
347                 CDEBUG(D_NET, "Returning duplicate net %p %s\n", net,
348                        libcfs_net2str(net->net_id));
349                 return net;
350         }
351
352         LIBCFS_ALLOC(net, sizeof(*net));
353         if (net == NULL) {
354                 CERROR("Out of memory creating network %s\n",
355                        libcfs_net2str(net_id));
356                 return NULL;
357         }
358
359         INIT_LIST_HEAD(&net->net_list);
360         INIT_LIST_HEAD(&net->net_ni_list);
361         INIT_LIST_HEAD(&net->net_ni_added);
362         INIT_LIST_HEAD(&net->net_ni_zombie);
363         INIT_LIST_HEAD(&net->net_rtr_pref_nids);
364         spin_lock_init(&net->net_lock);
365
366         net->net_id = net_id;
367         net->net_last_alive = ktime_get_seconds();
368
369         net->net_sel_priority = LNET_MAX_SELECTION_PRIORITY;
370
371         /* initialize global paramters to undefiend */
372         net->net_tunables.lct_peer_timeout = -1;
373         net->net_tunables.lct_max_tx_credits = -1;
374         net->net_tunables.lct_peer_tx_credits = -1;
375         net->net_tunables.lct_peer_rtr_credits = -1;
376
377         if (net_list)
378                 list_add_tail(&net->net_list, net_list);
379
380         return net;
381 }
382
383 int lnet_ni_add_interface(struct lnet_ni *ni, char *iface)
384 {
385         size_t iface_len = strlen(iface) + 1;
386
387         if (ni == NULL)
388                 return -ENOMEM;
389
390         if (ni->ni_interface != NULL) {
391                 LCONSOLE_ERROR_MSG(0x115, "%s: interface %s already set for net %s: rc = %d\n",
392                                    iface, ni->ni_interface,
393                                    libcfs_net2str(LNET_NID_NET(&ni->ni_nid)),
394                                    -EINVAL);
395                 return -EINVAL;
396         }
397
398         /* Allocate memory for the interface, so the code parsing input into
399          * tokens and adding interfaces can free the input safely.
400          * ni->ni_interface is freed in lnet_ni_free().
401          */
402         LIBCFS_ALLOC(ni->ni_interface, iface_len);
403
404         if (ni->ni_interface == NULL) {
405                 CERROR("%s: cannot allocate net interface name: rc = %d\n",
406                         iface, -ENOMEM);
407                 return -ENOMEM;
408         }
409
410         strscpy(ni->ni_interface, iface, iface_len);
411
412         return 0;
413 }
414 EXPORT_SYMBOL(lnet_ni_add_interface);
415
416 static struct lnet_ni *
417 lnet_ni_alloc_common(struct lnet_net *net, struct lnet_nid *nid, char *iface)
418 {
419         struct lnet_tx_queue    *tq;
420         struct lnet_ni          *ni;
421         int                     i;
422
423         if (iface != NULL)
424                 /* make sure that this NI is unique in the net it's
425                  * being added to */
426                 if (!lnet_ni_unique_net(&net->net_ni_added, iface))
427                         return NULL;
428
429         LIBCFS_ALLOC(ni, sizeof(*ni));
430         if (ni == NULL) {
431                 CERROR("Out of memory creating network interface %s%s\n",
432                        libcfs_net2str(net->net_id),
433                        (iface != NULL) ? iface : "");
434                 return NULL;
435         }
436
437         spin_lock_init(&ni->ni_lock);
438         INIT_LIST_HEAD(&ni->ni_netlist);
439         INIT_LIST_HEAD(&ni->ni_recovery);
440         LNetInvalidateMDHandle(&ni->ni_ping_mdh);
441         ni->ni_refs = cfs_percpt_alloc(lnet_cpt_table(),
442                                        sizeof(*ni->ni_refs[0]));
443         if (ni->ni_refs == NULL)
444                 goto failed;
445
446         ni->ni_tx_queues = cfs_percpt_alloc(lnet_cpt_table(),
447                                             sizeof(*ni->ni_tx_queues[0]));
448         if (ni->ni_tx_queues == NULL)
449                 goto failed;
450
451         cfs_percpt_for_each(tq, i, ni->ni_tx_queues)
452                 INIT_LIST_HEAD(&tq->tq_delayed);
453
454         ni->ni_net = net;
455         if (nid_same(nid, &LNET_ANY_NID)) {
456                 /* LND will fill in the address part of the NID */
457                 ni->ni_nid.nid_type = LNET_NETTYP(net->net_id);
458                 ni->ni_nid.nid_num = cpu_to_be16(LNET_NETNUM(net->net_id));
459         } else {
460                 ni->ni_nid = *nid;
461         }
462
463         /* Store net namespace in which current ni is being created */
464         if (current->nsproxy && current->nsproxy->net_ns)
465                 ni->ni_net_ns = get_net(current->nsproxy->net_ns);
466         else
467                 ni->ni_net_ns = get_net(&init_net);
468
469         ni->ni_state = LNET_NI_STATE_INIT;
470         ni->ni_sel_priority = LNET_MAX_SELECTION_PRIORITY;
471         list_add_tail(&ni->ni_netlist, &net->net_ni_added);
472
473         /*
474          * if an interface name is provided then make sure to add in that
475          * interface name in NI
476          */
477         if (iface)
478                 if (lnet_ni_add_interface(ni, iface) != 0)
479                         goto failed;
480
481         return ni;
482 failed:
483         lnet_ni_free(ni);
484         return NULL;
485 }
486
487 /* allocate and add to the provided network */
488 struct lnet_ni *
489 lnet_ni_alloc(struct lnet_net *net, struct cfs_expr_list *el, char *iface)
490 {
491         struct lnet_ni          *ni;
492         int                     rc;
493
494         ni = lnet_ni_alloc_common(net, &LNET_ANY_NID, iface);
495         if (!ni)
496                 return NULL;
497
498         if (!el) {
499                 ni->ni_cpts  = NULL;
500                 ni->ni_ncpts = LNET_CPT_NUMBER;
501         } else {
502                 rc = cfs_expr_list_values(el, LNET_CPT_NUMBER, &ni->ni_cpts);
503                 if (rc <= 0) {
504                         CERROR("Failed to set CPTs for NI %s(%s): %d\n",
505                                libcfs_net2str(net->net_id),
506                                (iface != NULL) ? iface : "", rc);
507                         goto failed;
508                 }
509
510                 LASSERT(rc <= LNET_CPT_NUMBER);
511                 if (rc == LNET_CPT_NUMBER) {
512                         CFS_FREE_PTR_ARRAY(ni->ni_cpts, rc);
513                         ni->ni_cpts = NULL;
514                 }
515
516                 ni->ni_ncpts = rc;
517         }
518
519         rc = lnet_net_append_cpts(ni->ni_cpts, ni->ni_ncpts, net);
520         if (rc != 0)
521                 goto failed;
522
523         return ni;
524 failed:
525         lnet_ni_free(ni);
526         return NULL;
527 }
528
529 struct lnet_ni *
530 lnet_ni_alloc_w_cpt_array(struct lnet_net *net, struct lnet_nid *nid,
531                           u32 *cpts, u32 ncpts, char *iface)
532 {
533         struct lnet_ni          *ni;
534         int                     rc;
535
536         ni = lnet_ni_alloc_common(net, nid, iface);
537         if (!ni)
538                 return NULL;
539
540         if (ncpts == 0) {
541                 ni->ni_cpts  = NULL;
542                 ni->ni_ncpts = LNET_CPT_NUMBER;
543         } else {
544                 size_t array_size = ncpts * sizeof(ni->ni_cpts[0]);
545
546                 CFS_ALLOC_PTR_ARRAY(ni->ni_cpts, ncpts);
547                 if (ni->ni_cpts == NULL)
548                         goto failed;
549                 memcpy(ni->ni_cpts, cpts, array_size);
550                 ni->ni_ncpts = ncpts;
551         }
552
553         rc = lnet_net_append_cpts(ni->ni_cpts, ni->ni_ncpts, net);
554         if (rc != 0)
555                 goto failed;
556
557         return ni;
558 failed:
559         lnet_ni_free(ni);
560         return NULL;
561 }
562
563 /*
564  * Parse the networks string and create the matching set of NIs on the
565  * nilist.
566  */
567 int
568 lnet_parse_networks(struct list_head *netlist, const char *networks)
569 {
570         struct cfs_expr_list *net_el = NULL;
571         struct cfs_expr_list *ni_el = NULL;
572         int             tokensize;
573         char            *tokens;
574         char            *str;
575         struct lnet_net *net;
576         struct lnet_ni  *ni = NULL;
577         __u32           net_id;
578         int             nnets = 0;
579
580         if (networks == NULL) {
581                 CERROR("networks string is undefined\n");
582                 return -EINVAL;
583         }
584
585         if (strlen(networks) > LNET_SINGLE_TEXTBUF_NOB) {
586                 /* _WAY_ conservative */
587                 LCONSOLE_ERROR_MSG(0x112, "Can't parse networks: string too "
588                                    "long\n");
589                 return -EINVAL;
590         }
591
592         tokensize = strlen(networks) + 1;
593
594         LIBCFS_ALLOC(tokens, tokensize);
595         if (tokens == NULL) {
596                 CERROR("Can't allocate net tokens\n");
597                 return -ENOMEM;
598         }
599
600         memcpy(tokens, networks, tokensize);
601         str = tokens;
602
603         /*
604          * Main parser loop.
605          *
606          * NB we don't check interface conflicts here; it's the LNDs
607          * responsibility (if it cares at all)
608          */
609         do {
610                 char *nistr;
611                 char *elstr;
612                 char *name;
613                 int rc;
614
615                 /*
616                  * Parse a network string into its components.
617                  *
618                  * <name>{"("...")"}{"["<el>"]"}
619                  */
620
621                 /* Network name (mandatory) */
622                 while (isspace(*str))
623                         *str++ = '\0';
624                 if (!*str)
625                         break;
626                 name = str;
627                 str += strcspn(str, SPACESTR ":()[],");
628                 while (isspace(*str))
629                         *str++ = '\0';
630
631                 /* Interface list (optional) */
632                 if (*str == '(') {
633                         *str++ = '\0';
634                         nistr = str;
635                         str += strcspn(str, ")");
636                         if (*str != ')') {
637                                 str = nistr;
638                                 goto failed_syntax;
639                         }
640                         do {
641                                 *str++ = '\0';
642                         } while (isspace(*str));
643                 } else {
644                         nistr = NULL;
645                 }
646
647                 /* CPT expression (optional) */
648                 if (*str == '[') {
649                         elstr = str;
650                         str += strcspn(str, "]");
651                         if (*str != ']') {
652                                 str = elstr;
653                                 goto failed_syntax;
654                         }
655                         rc = cfs_expr_list_parse(elstr, str - elstr + 1,
656                                                 0, LNET_CPT_NUMBER - 1,
657                                                 &net_el);
658                         if (rc != 0) {
659                                 str = elstr;
660                                 goto failed_syntax;
661                         }
662                         *elstr = '\0';
663                         do {
664                                 *str++ = '\0';
665                         } while (isspace(*str));
666                 }
667
668                 /* Bad delimiters */
669                 if (*str && (strchr(DELIMITERS, *str) != NULL))
670                         goto failed_syntax;
671
672                 /* go to the next net if it exits */
673                 str += strcspn(str, ",");
674                 if (*str == ',')
675                         *str++ = '\0';
676
677                 /*
678                  * At this point the name is properly terminated.
679                  */
680                 net_id = libcfs_str2net(name);
681                 if (net_id == LNET_NET_ANY) {
682                         LCONSOLE_ERROR_MSG(0x113,
683                                         "Unrecognised network type\n");
684                         str = name;
685                         goto failed_syntax;
686                 }
687
688                 if (LNET_NETTYP(net_id) == LOLND) {
689                         /* Loopback is implicit, and there can be only one. */
690                         if (net_el) {
691                                 cfs_expr_list_free(net_el);
692                                 net_el = NULL;
693                         }
694                         /* Should we error out instead? */
695                         continue;
696                 }
697
698                 /*
699                  * All network paramaters are now known.
700                  */
701                 nnets++;
702
703                 /* always allocate a net, since we will eventually add an
704                  * interface to it, or we will fail, in which case we'll
705                  * just delete it */
706                 net = lnet_net_alloc(net_id, netlist);
707                 if (IS_ERR_OR_NULL(net))
708                         goto failed;
709
710                 if (!nistr) {
711                         /*
712                          * No interface list was specified, allocate a
713                          * ni using the defaults.
714                          */
715                         ni = lnet_ni_alloc(net, net_el, NULL);
716                         if (IS_ERR_OR_NULL(ni))
717                                 goto failed;
718
719                         if (!nistr) {
720                                 if (net_el) {
721                                         cfs_expr_list_free(net_el);
722                                         net_el = NULL;
723                                 }
724                                 continue;
725                         }
726                 }
727
728                 do {
729                         elstr = NULL;
730
731                         /* Interface name (mandatory) */
732                         while (isspace(*nistr))
733                                 *nistr++ = '\0';
734                         name = nistr;
735                         nistr += strcspn(nistr, SPACESTR "[],");
736                         while (isspace(*nistr))
737                                 *nistr++ = '\0';
738
739                         /* CPT expression (optional) */
740                         if (*nistr == '[') {
741                                 elstr = nistr;
742                                 nistr += strcspn(nistr, "]");
743                                 if (*nistr != ']') {
744                                         str = elstr;
745                                         goto failed_syntax;
746                                 }
747                                 rc = cfs_expr_list_parse(elstr,
748                                                         nistr - elstr + 1,
749                                                         0, LNET_CPT_NUMBER - 1,
750                                                         &ni_el);
751                                 if (rc != 0) {
752                                         str = elstr;
753                                         goto failed_syntax;
754                                 }
755                                 *elstr = '\0';
756                                 do {
757                                         *nistr++ = '\0';
758                                 } while (isspace(*nistr));
759                         } else {
760                                 ni_el = net_el;
761                         }
762
763                         /*
764                          * End of single interface specificaton,
765                          * advance to the start of the next one, if
766                          * any.
767                          */
768                         if (*nistr == ',') {
769                                 do {
770                                         *nistr++ = '\0';
771                                 } while (isspace(*nistr));
772                                 if (!*nistr) {
773                                         str = nistr;
774                                         goto failed_syntax;
775                                 }
776                         } else if (*nistr) {
777                                 str = nistr;
778                                 goto failed_syntax;
779                         }
780
781                         /*
782                          * At this point the name is properly terminated.
783                          */
784                         if (!*name) {
785                                 str = name;
786                                 goto failed_syntax;
787                         }
788
789                         ni = lnet_ni_alloc(net, ni_el, name);
790                         if (IS_ERR_OR_NULL(ni))
791                                 goto failed;
792
793                         if (ni_el) {
794                                 if (ni_el != net_el) {
795                                         cfs_expr_list_free(ni_el);
796                                         ni_el = NULL;
797                                 }
798                         }
799                 } while (*nistr);
800
801                 if (net_el) {
802                         cfs_expr_list_free(net_el);
803                         net_el = NULL;
804                 }
805         } while (*str);
806
807         LIBCFS_FREE(tokens, tokensize);
808         return nnets;
809
810  failed_syntax:
811         lnet_syntax("networks", networks, (int)(str - tokens), strlen(str));
812  failed:
813         /* free the net list and all the nis on each net */
814         while ((net = list_first_entry_or_null(netlist,
815                                                struct lnet_net,
816                                                net_list)) != NULL) {
817                 list_del_init(&net->net_list);
818                 lnet_net_free(net);
819         }
820
821         if (ni_el && ni_el != net_el)
822                 cfs_expr_list_free(ni_el);
823         if (net_el)
824                 cfs_expr_list_free(net_el);
825
826         LIBCFS_FREE(tokens, tokensize);
827
828         return -EINVAL;
829 }
830
831 static struct lnet_text_buf *lnet_new_text_buf(int str_len)
832 {
833         struct lnet_text_buf *ltb;
834         int nob;
835
836         /* NB allocate space for the terminating 0 */
837         nob = offsetof(struct lnet_text_buf, ltb_text[str_len + 1]);
838         if (nob > LNET_SINGLE_TEXTBUF_NOB) {
839                 /* _way_ conservative for "route net gateway..." */
840                 CERROR("text buffer too big\n");
841                 return NULL;
842         }
843
844         if (lnet_tbnob + nob > LNET_MAX_TEXTBUF_NOB) {
845                 CERROR("Too many text buffers\n");
846                 return NULL;
847         }
848
849         LIBCFS_ALLOC(ltb, nob);
850         if (ltb == NULL)
851                 return NULL;
852
853         ltb->ltb_size = nob;
854         ltb->ltb_text[0] = 0;
855         lnet_tbnob += nob;
856         return ltb;
857 }
858
859 static void
860 lnet_free_text_buf(struct lnet_text_buf *ltb)
861 {
862         lnet_tbnob -= ltb->ltb_size;
863         LIBCFS_FREE(ltb, ltb->ltb_size);
864 }
865
866 static void
867 lnet_free_text_bufs(struct list_head *tbs)
868 {
869         struct lnet_text_buf  *ltb;
870
871         while ((ltb = list_first_entry_or_null(tbs, struct lnet_text_buf,
872                                                ltb_list)) != NULL) {
873                 list_del(&ltb->ltb_list);
874                 lnet_free_text_buf(ltb);
875         }
876 }
877
878 static int
879 lnet_str2tbs_sep(struct list_head *tbs, const char *str)
880 {
881         LIST_HEAD(pending);
882         const char *sep;
883         int nob;
884         int i;
885         struct lnet_text_buf *ltb;
886
887         /* Split 'str' into separate commands */
888         for (;;) {
889                 /* skip leading whitespace */
890                 while (isspace(*str))
891                         str++;
892
893                 /* scan for separator or comment */
894                 for (sep = str; *sep != 0; sep++)
895                         if (lnet_issep(*sep) || *sep == '#')
896                                 break;
897
898                 nob = (int)(sep - str);
899                 if (nob > 0) {
900                         ltb = lnet_new_text_buf(nob);
901                         if (ltb == NULL) {
902                                 lnet_free_text_bufs(&pending);
903                                 return -ENOMEM;
904                         }
905
906                         for (i = 0; i < nob; i++)
907                                 if (isspace(str[i]))
908                                         ltb->ltb_text[i] = ' ';
909                                 else
910                                         ltb->ltb_text[i] = str[i];
911
912                         ltb->ltb_text[nob] = 0;
913
914                         list_add_tail(&ltb->ltb_list, &pending);
915                 }
916
917                 if (*sep == '#') {
918                         /* scan for separator */
919                         do {
920                                 sep++;
921                         } while (*sep != 0 && !lnet_issep(*sep));
922                 }
923
924                 if (*sep == 0)
925                         break;
926
927                 str = sep + 1;
928         }
929
930         list_splice(&pending, tbs->prev);
931         return 0;
932 }
933
934 static int
935 lnet_expand1tb(struct list_head *list,
936                char *str, char *sep1, char *sep2,
937                char *item, int itemlen)
938 {
939         int              len1 = (int)(sep1 - str);
940         int              len2 = strlen(sep2 + 1);
941         struct lnet_text_buf *ltb;
942
943         LASSERT (*sep1 == '[');
944         LASSERT (*sep2 == ']');
945
946         ltb = lnet_new_text_buf(len1 + itemlen + len2);
947         if (ltb == NULL)
948                 return -ENOMEM;
949
950         memcpy(ltb->ltb_text, str, len1);
951         memcpy(&ltb->ltb_text[len1], item, itemlen);
952         memcpy(&ltb->ltb_text[len1+itemlen], sep2 + 1, len2);
953         ltb->ltb_text[len1 + itemlen + len2] = 0;
954
955         list_add_tail(&ltb->ltb_list, list);
956         return 0;
957 }
958
959 static int
960 lnet_str2tbs_expand(struct list_head *tbs, char *str)
961 {
962         char              num[16];
963         LIST_HEAD(pending);
964         char             *sep;
965         char             *sep2;
966         char             *parsed;
967         char             *enditem;
968         int               lo;
969         int               hi;
970         int               stride;
971         int               i;
972         int               nob;
973         int               scanned;
974
975         sep = strchr(str, '[');
976         if (sep == NULL)                        /* nothing to expand */
977                 return 0;
978
979         sep2 = strchr(sep, ']');
980         if (sep2 == NULL)
981                 goto failed;
982
983         for (parsed = sep; parsed < sep2; parsed = enditem) {
984
985                 enditem = ++parsed;
986                 while (enditem < sep2 && *enditem != ',')
987                         enditem++;
988
989                 if (enditem == parsed)          /* no empty items */
990                         goto failed;
991
992                 if (sscanf(parsed, "%d-%d/%d%n", &lo, &hi, &stride, &scanned) < 3) {
993
994                         if (sscanf(parsed, "%d-%d%n", &lo, &hi, &scanned) < 2) {
995
996                                 /* simple string enumeration */
997                                 if (lnet_expand1tb(&pending, str, sep, sep2,
998                                                    parsed, (int)(enditem - parsed)) != 0)
999                                         goto failed;
1000
1001                                 continue;
1002                         }
1003
1004                         stride = 1;
1005                 }
1006
1007                 /* range expansion */
1008
1009                 if (enditem != parsed + scanned) /* no trailing junk */
1010                         goto failed;
1011
1012                 if (hi < 0 || lo < 0 || stride < 0 || hi < lo ||
1013                     (hi - lo) % stride != 0)
1014                         goto failed;
1015
1016                 for (i = lo; i <= hi; i += stride) {
1017
1018                         snprintf(num, sizeof(num), "%d", i);
1019                         nob = strlen(num);
1020                         if (nob + 1 == sizeof(num))
1021                                 goto failed;
1022
1023                         if (lnet_expand1tb(&pending, str, sep, sep2,
1024                                            num, nob) != 0)
1025                                 goto failed;
1026                 }
1027         }
1028
1029         list_splice(&pending, tbs->prev);
1030         return 1;
1031
1032  failed:
1033         lnet_free_text_bufs(&pending);
1034         return -EINVAL;
1035 }
1036
1037 static int
1038 lnet_parse_hops (char *str, unsigned int *hops)
1039 {
1040         int     len = strlen(str);
1041         int     nob = len;
1042
1043         return (sscanf(str, "%u%n", hops, &nob) >= 1 &&
1044                 nob == len &&
1045                 *hops > 0 && *hops < 256);
1046 }
1047
1048 #define LNET_PRIORITY_SEPARATOR (':')
1049
1050 static int
1051 lnet_parse_priority(char *str, unsigned int *priority, char **token)
1052 {
1053         int   nob;
1054         char *sep;
1055         int   len;
1056
1057         sep = strchr(str, LNET_PRIORITY_SEPARATOR);
1058         if (sep == NULL) {
1059                 *priority = 0;
1060                 return 0;
1061         }
1062         len = strlen(sep + 1);
1063
1064         if ((sscanf((sep+1), "%u%n", priority, &nob) < 1) || (len != nob)) {
1065                 /* Update the caller's token pointer so it treats the found
1066                    priority as the token to report in the error message. */
1067                 *token += sep - str + 1;
1068                 return -EINVAL;
1069         }
1070
1071         CDEBUG(D_NET, "gateway %s, priority %d, nob %d\n", str, *priority, nob);
1072
1073         /*
1074          * Change priority separator to \0 to be able to parse NID
1075          */
1076         *sep = '\0';
1077         return 0;
1078 }
1079
1080 static int
1081 lnet_parse_route(char *str, int *im_a_router)
1082 {
1083         /* static scratch buffer OK (single threaded) */
1084         static char cmd[LNET_SINGLE_TEXTBUF_NOB];
1085
1086         LIST_HEAD(nets);
1087         LIST_HEAD(gateways);
1088         struct list_head *tmp1;
1089         struct list_head *tmp2;
1090         __u32 net;
1091         struct lnet_nid nid;
1092         struct lnet_text_buf *ltb = NULL;
1093         struct lnet_text_buf *ltb1, *ltb2;
1094         int rc;
1095         char *sep;
1096         char *token = str;
1097         int ntokens = 0;
1098         int myrc = -1;
1099         __u32 hops;
1100         int got_hops = 0;
1101         unsigned int priority = 0;
1102
1103         /* save a copy of the string for error messages */
1104         strncpy(cmd, str, sizeof(cmd));
1105         cmd[sizeof(cmd) - 1] = '\0';
1106
1107         sep = str;
1108         for (;;) {
1109                 /* scan for token start */
1110                 while (isspace(*sep))
1111                         sep++;
1112                 if (*sep == 0) {
1113                         if (ntokens < (got_hops ? 3 : 2))
1114                                 goto token_error;
1115                         break;
1116                 }
1117
1118                 ntokens++;
1119                 token = sep++;
1120
1121                 /* scan for token end */
1122                 while (*sep != 0 && !isspace(*sep))
1123                         sep++;
1124                 if (*sep != 0)
1125                         *sep++ = 0;
1126
1127                 if (ntokens == 1) {
1128                         tmp2 = &nets;           /* expanding nets */
1129                 } else if (ntokens == 2 &&
1130                            lnet_parse_hops(token, &hops)) {
1131                         got_hops = 1;           /* got a hop count */
1132                         continue;
1133                 } else {
1134                         tmp2 = &gateways;       /* expanding gateways */
1135                 }
1136
1137                 ltb = lnet_new_text_buf(strlen(token));
1138                 if (ltb == NULL)
1139                         goto out;
1140
1141                 strcpy(ltb->ltb_text, token);
1142                 tmp1 = &ltb->ltb_list;
1143                 list_add_tail(tmp1, tmp2);
1144
1145                 while (tmp1 != tmp2) {
1146                         ltb = list_entry(tmp1, struct lnet_text_buf, ltb_list);
1147
1148                         rc = lnet_str2tbs_expand(tmp1->next, ltb->ltb_text);
1149                         if (rc < 0)
1150                                 goto token_error;
1151
1152                         tmp1 = tmp1->next;
1153
1154                         if (rc > 0) {           /* expanded! */
1155                                 list_del(&ltb->ltb_list);
1156                                 lnet_free_text_buf(ltb);
1157                                 continue;
1158                         }
1159
1160                         if (ntokens == 1) {
1161                                 net = libcfs_str2net(ltb->ltb_text);
1162                                 if (net == LNET_NET_ANY ||
1163                                     LNET_NETTYP(net) == LOLND)
1164                                         goto token_error;
1165                         } else {
1166                                 rc = lnet_parse_priority(ltb->ltb_text,
1167                                                          &priority, &token);
1168                                 if (rc < 0)
1169                                         goto token_error;
1170
1171                                 if (libcfs_strnid(&nid, ltb->ltb_text) != 0 ||
1172                                     nid_is_lo0(&nid))
1173                                         goto token_error;
1174                         }
1175                 }
1176         }
1177
1178         /* if there are no hops set then we want to flag this value as
1179          * unset since hops is an optional parameter */
1180         if (!got_hops)
1181                 hops = LNET_UNDEFINED_HOPS;
1182
1183         LASSERT(!list_empty(&nets));
1184         LASSERT(!list_empty(&gateways));
1185
1186         list_for_each_entry(ltb1, &nets, ltb_list) {
1187                 net = libcfs_str2net(ltb1->ltb_text);
1188                 LASSERT(net != LNET_NET_ANY);
1189
1190                 list_for_each_entry(ltb2, &gateways, ltb_list) {
1191                         LASSERT(libcfs_strnid(&nid, ltb2->ltb_text) == 0);
1192
1193                         if (lnet_islocalnid(&nid)) {
1194                                 *im_a_router = 1;
1195                                 continue;
1196                         }
1197
1198                         rc = lnet_add_route(net, hops, &nid, priority, 1);
1199                         if (rc != 0 && rc != -EEXIST && rc != -EHOSTUNREACH) {
1200                                 CERROR("Can't create route "
1201                                        "to %s via %s\n",
1202                                        libcfs_net2str(net),
1203                                        libcfs_nidstr(&nid));
1204                                 goto out;
1205                         }
1206                 }
1207         }
1208
1209         myrc = 0;
1210         goto out;
1211
1212 token_error:
1213         lnet_syntax("routes", cmd, (int)(token - str), strlen(token));
1214 out:
1215         lnet_free_text_bufs(&nets);
1216         lnet_free_text_bufs(&gateways);
1217         return myrc;
1218 }
1219
1220 static int
1221 lnet_parse_route_tbs(struct list_head *tbs, int *im_a_router)
1222 {
1223         struct lnet_text_buf *ltb;
1224
1225         while ((ltb = list_first_entry_or_null(tbs, struct lnet_text_buf,
1226                                                ltb_list)) != NULL) {
1227                 if (lnet_parse_route(ltb->ltb_text, im_a_router) < 0) {
1228                         lnet_free_text_bufs(tbs);
1229                         return -EINVAL;
1230                 }
1231
1232                 list_del(&ltb->ltb_list);
1233                 lnet_free_text_buf(ltb);
1234         }
1235
1236         return 0;
1237 }
1238
1239 int
1240 lnet_parse_routes(const char *routes, int *im_a_router)
1241 {
1242         LIST_HEAD(tbs);
1243         int rc = 0;
1244
1245         *im_a_router = 0;
1246
1247         if (lnet_str2tbs_sep(&tbs, routes) < 0) {
1248                 CERROR("Error parsing routes\n");
1249                 rc = -EINVAL;
1250         } else {
1251                 rc = lnet_parse_route_tbs(&tbs, im_a_router);
1252         }
1253
1254         LASSERT (lnet_tbnob == 0);
1255         return rc;
1256 }
1257
1258 static int
1259 lnet_match_network_token(char *token, __u32 *ipaddrs, int nip)
1260 {
1261         LIST_HEAD(list);
1262         int             rc;
1263         int             i;
1264
1265         rc = cfs_ip_addr_parse(token, 0, &list);
1266         if (rc != 0)
1267                 return rc;
1268
1269         for (rc = i = 0; !rc && i < nip; i++)
1270                 rc = cfs_ip_addr_match(ipaddrs[i], &list);
1271
1272         cfs_expr_list_free_list(&list);
1273
1274         return rc;
1275 }
1276
1277 static int
1278 lnet_match_network_tokens(char *net_entry, __u32 *ipaddrs, int nip)
1279 {
1280         static char tokens[LNET_SINGLE_TEXTBUF_NOB];
1281
1282         int   matched = 0;
1283         int   ntokens = 0;
1284         char *net = NULL;
1285         char *sep;
1286         char *token;
1287         int   rc;
1288
1289         LASSERT(strlen(net_entry) < sizeof(tokens));
1290
1291         /* work on a copy of the string */
1292         strcpy(tokens, net_entry);
1293         sep = tokens;
1294         while (sep) {
1295                 /* scan for token start */
1296                 sep = skip_spaces(sep);
1297                 if (*sep == 0)
1298                         break;
1299
1300                 token = strsep(&sep, " \t\n\r\v\f");
1301
1302                 if (ntokens++ == 0) {
1303                         net = token;
1304                         continue;
1305                 }
1306
1307                 rc = lnet_match_network_token(token, ipaddrs, nip);
1308                 if (rc < 0) {
1309                         lnet_syntax("ip2nets", net_entry,
1310                                     (int)(token - tokens), strlen(token));
1311                         return rc;
1312                 }
1313
1314                 matched |= (rc != 0);
1315         }
1316
1317         if (!matched)
1318                 return 0;
1319
1320         strcpy(net_entry, net);                 /* replace with matched net */
1321         return 1;
1322 }
1323
1324 static __u32
1325 lnet_netspec2net(char *netspec)
1326 {
1327         char   *bracket = strchr(netspec, '(');
1328         __u32   net;
1329
1330         if (bracket != NULL)
1331                 *bracket = 0;
1332
1333         net = libcfs_str2net(netspec);
1334
1335         if (bracket != NULL)
1336                 *bracket = '(';
1337
1338         return net;
1339 }
1340
1341 static int
1342 lnet_splitnets(char *source, struct list_head *nets)
1343 {
1344         int               offset = 0;
1345         int               offset2;
1346         int               len;
1347         struct lnet_text_buf  *tb;
1348         struct lnet_text_buf  *tb2;
1349         char             *sep;
1350         char             *bracket;
1351         __u32             net;
1352
1353         LASSERT(!list_empty(nets));
1354         LASSERT(nets->next == nets->prev);      /* single entry */
1355
1356         tb = list_first_entry(nets, struct lnet_text_buf, ltb_list);
1357
1358         for (;;) {
1359                 sep = strchr(tb->ltb_text, ',');
1360                 bracket = strchr(tb->ltb_text, '(');
1361
1362                 if (sep != NULL &&
1363                     bracket != NULL &&
1364                     bracket < sep) {
1365                         /* netspec lists interfaces... */
1366
1367                         offset2 = offset + (int)(bracket - tb->ltb_text);
1368                         len = strlen(bracket);
1369
1370                         bracket = strchr(bracket + 1, ')');
1371
1372                         if (bracket == NULL ||
1373                             !(bracket[1] == ',' || bracket[1] == 0)) {
1374                                 lnet_syntax("ip2nets", source, offset2, len);
1375                                 return -EINVAL;
1376                         }
1377
1378                         sep = (bracket[1] == 0) ? NULL : bracket + 1;
1379                 }
1380
1381                 if (sep != NULL)
1382                         *sep++ = 0;
1383
1384                 net = lnet_netspec2net(tb->ltb_text);
1385                 if (net == LNET_NET_ANY) {
1386                         lnet_syntax("ip2nets", source, offset,
1387                                     strlen(tb->ltb_text));
1388                         return -EINVAL;
1389                 }
1390
1391                 list_for_each_entry(tb2, nets, ltb_list) {
1392                         if (tb2 == tb)
1393                                 continue;
1394
1395                         if (net == lnet_netspec2net(tb2->ltb_text)) {
1396                                 /* duplicate network */
1397                                 lnet_syntax("ip2nets", source, offset,
1398                                             strlen(tb->ltb_text));
1399                                 return -EINVAL;
1400                         }
1401                 }
1402
1403                 if (sep == NULL)
1404                         return 0;
1405
1406                 offset += (int)(sep - tb->ltb_text);
1407                 len = strlen(sep);
1408                 tb2 = lnet_new_text_buf(len);
1409                 if (tb2 == NULL)
1410                         return -ENOMEM;
1411
1412                 strncpy(tb2->ltb_text, sep, len);
1413                 tb2->ltb_text[len] = '\0';
1414                 list_add_tail(&tb2->ltb_list, nets);
1415
1416                 tb = tb2;
1417         }
1418 }
1419
1420 static int
1421 lnet_match_networks(const char **networksp, const char *ip2nets,
1422                     __u32 *ipaddrs, int nip)
1423 {
1424         static char       networks[LNET_SINGLE_TEXTBUF_NOB];
1425         static char       source[LNET_SINGLE_TEXTBUF_NOB];
1426
1427         LIST_HEAD(raw_entries);
1428         LIST_HEAD(matched_nets);
1429         LIST_HEAD(current_nets);
1430         struct list_head *t;
1431         struct list_head *t2;
1432         struct lnet_text_buf  *tb;
1433         int               len;
1434         int               count;
1435         int               rc;
1436
1437         if (lnet_str2tbs_sep(&raw_entries, ip2nets) < 0) {
1438                 CERROR("Error parsing ip2nets\n");
1439                 LASSERT(lnet_tbnob == 0);
1440                 return -EINVAL;
1441         }
1442
1443         networks[0] = 0;
1444         count = 0;
1445         len = 0;
1446         rc = 0;
1447
1448         while ((tb = list_first_entry_or_null(&raw_entries,
1449                                               struct lnet_text_buf,
1450                                               ltb_list)) != NULL) {
1451                 strncpy(source, tb->ltb_text, sizeof(source));
1452                 source[sizeof(source) - 1] = '\0';
1453
1454                 /* replace ltb_text with the network(s) add on match */
1455                 rc = lnet_match_network_tokens(tb->ltb_text, ipaddrs, nip);
1456                 if (rc < 0)
1457                         break;
1458
1459                 list_del(&tb->ltb_list);
1460
1461                 if (rc == 0) {                  /* no match */
1462                         lnet_free_text_buf(tb);
1463                         continue;
1464                 }
1465
1466                 /* split into separate networks */
1467                 INIT_LIST_HEAD(&current_nets);
1468                 list_add(&tb->ltb_list, &current_nets);
1469                 rc = lnet_splitnets(source, &current_nets);
1470                 if (rc < 0)
1471                         break;
1472
1473                 list_for_each_safe(t, t2, &current_nets) {
1474                         tb = list_entry(t, struct lnet_text_buf, ltb_list);
1475
1476                         list_move_tail(&tb->ltb_list, &matched_nets);
1477
1478                         len += scnprintf(networks + len, sizeof(networks) - len,
1479                                          "%s%s", (len == 0) ? "" : ",",
1480                                          tb->ltb_text);
1481
1482                         if (len >= sizeof(networks)) {
1483                                 CERROR("Too many matched networks\n");
1484                                 rc = -E2BIG;
1485                                 goto out;
1486                         }
1487                 }
1488
1489                 count++;
1490         }
1491
1492  out:
1493         lnet_free_text_bufs(&raw_entries);
1494         lnet_free_text_bufs(&matched_nets);
1495         lnet_free_text_bufs(&current_nets);
1496         LASSERT(lnet_tbnob == 0);
1497
1498         if (rc < 0)
1499                 return rc;
1500
1501         *networksp = networks;
1502         return count;
1503 }
1504
1505 __u32 lnet_set_link_fatal_state(struct lnet_ni *ni, unsigned int link_state)
1506 {
1507         CDEBUG(D_NET, "%s: set link fatal state to %u\n",
1508                libcfs_nidstr(&ni->ni_nid), link_state);
1509         return atomic_xchg(&ni->ni_fatal_error_on, link_state);
1510 }
1511 EXPORT_SYMBOL(lnet_set_link_fatal_state);
1512
1513 int lnet_get_link_status(struct net_device *dev)
1514 {
1515         int ret = -1;
1516
1517         if (!dev)
1518                 return -1;
1519
1520         if (!netif_running(dev)) {
1521                 ret = 0;
1522                 CDEBUG(D_NET, "device idx %d not running\n", dev->ifindex);
1523         }
1524         /* Some devices may not be providing link settings */
1525         else if (dev->ethtool_ops->get_link) {
1526                 ret = dev->ethtool_ops->get_link(dev);
1527                 CDEBUG(D_NET, "device idx %d get_link %u\n",
1528                        ret,
1529                        dev->ifindex);
1530         }
1531
1532         return ret;
1533 }
1534 EXPORT_SYMBOL(lnet_get_link_status);
1535
1536 int lnet_inet_enumerate(struct lnet_inetdev **dev_list, struct net *ns, bool v6)
1537 {
1538         struct lnet_inetdev *ifaces = NULL;
1539         struct net_device *dev;
1540         int nalloc = 0;
1541         int nip = 0;
1542         DECLARE_CONST_IN_IFADDR(ifa);
1543
1544         rtnl_lock();
1545         for_each_netdev(ns, dev) {
1546                 int flags = dev_get_flags(dev);
1547                 struct in_device *in_dev;
1548 #if IS_ENABLED(CONFIG_IPV6)
1549                 struct inet6_dev *in6_dev;
1550                 const struct inet6_ifaddr *ifa6;
1551 #endif
1552                 int node_id;
1553                 int cpt;
1554
1555                 if (flags & IFF_LOOPBACK) /* skip the loopback IF */
1556                         continue;
1557
1558                 if (!(flags & IFF_UP)) {
1559                         CWARN("lnet: Ignoring interface %s: it's down\n",
1560                               dev->name);
1561                         continue;
1562                 }
1563
1564                 node_id = dev_to_node(&dev->dev);
1565                 cpt = cfs_cpt_of_node(lnet_cpt_table(), node_id);
1566
1567                 in_dev = __in_dev_get_rtnl(dev);
1568                 if (!in_dev) {
1569                         if (!v6)
1570                                 CWARN("lnet: Interface %s has no IPv4 status.\n",
1571                                       dev->name);
1572                         goto try_v6;
1573                 }
1574
1575                 in_dev_for_each_ifa_rtnl(ifa, in_dev) {
1576                         if (nip >= nalloc) {
1577                                 struct lnet_inetdev *tmp;
1578
1579                                 nalloc += LNET_INTERFACES_NUM;
1580                                 tmp = krealloc(ifaces, nalloc * sizeof(*tmp),
1581                                                GFP_KERNEL);
1582                                 if (!tmp) {
1583                                         kfree(ifaces);
1584                                         ifaces = NULL;
1585                                         nip = -ENOMEM;
1586                                         goto unlock_rtnl;
1587                                 }
1588                                 ifaces = tmp;
1589                         }
1590
1591                         ifaces[nip].li_cpt = cpt;
1592                         ifaces[nip].li_iff_master = !!(flags & IFF_MASTER);
1593                         ifaces[nip].li_size = sizeof(ifa->ifa_local);
1594                         ifaces[nip].li_index = dev->ifindex;
1595                         ifaces[nip].li_ipaddr = ifa->ifa_local;
1596                         ifaces[nip].li_netmask = ntohl(ifa->ifa_mask);
1597                         strlcpy(ifaces[nip].li_name, ifa->ifa_label,
1598                                 sizeof(ifaces[nip].li_name));
1599                         nip++;
1600                 }
1601                 endfor_ifa(in_dev);
1602
1603         try_v6:
1604                 if (!v6)
1605                         continue;
1606 #if IS_ENABLED(CONFIG_IPV6)
1607                 in6_dev = __in6_dev_get(dev);
1608                 if (!in6_dev) {
1609                         if (!in_dev)
1610                                 CWARN("lnet: Interface %s has no IP status.\n",
1611                                       dev->name);
1612                         continue;
1613                 }
1614
1615                 list_for_each_entry_rcu(ifa6, &in6_dev->addr_list, if_list) {
1616                         if (ifa6->flags & IFA_F_TEMPORARY)
1617                                 continue;
1618                         if (nip >= nalloc) {
1619                                 struct lnet_inetdev *tmp;
1620
1621                                 nalloc += LNET_INTERFACES_NUM;
1622                                 tmp = krealloc(ifaces, nalloc * sizeof(*tmp),
1623                                                GFP_KERNEL);
1624                                 if (!tmp) {
1625                                         kfree(ifaces);
1626                                         ifaces = NULL;
1627                                         nip = -ENOMEM;
1628                                         goto unlock_rtnl;
1629                                 }
1630                                 ifaces = tmp;
1631                         }
1632
1633                         ifaces[nip].li_cpt = cpt;
1634                         ifaces[nip].li_iff_master = !!(flags & IFF_MASTER);
1635                         ifaces[nip].li_size = sizeof(struct in6_addr);
1636                         ifaces[nip].li_index = dev->ifindex;
1637                         memcpy(ifaces[nip].li_ipv6addr,
1638                                &ifa6->addr, sizeof(struct in6_addr));
1639                         strlcpy(ifaces[nip].li_name, dev->name,
1640                                 sizeof(ifaces[nip].li_name));
1641                         nip++;
1642                         /* As different IPv6 addresses don't have unique
1643                          * labels, it is safest just to use the first
1644                          * and ignore the rest.
1645                          */
1646                         break;
1647                 }
1648 #endif /* IS_ENABLED(CONFIG_IPV6) */
1649
1650         }
1651 unlock_rtnl:
1652         rtnl_unlock();
1653
1654         if (nip == 0) {
1655                 CERROR("lnet: Can't find any usable interfaces, rc = -ENOENT\n");
1656                 nip = -ENOENT;
1657         }
1658
1659         *dev_list = ifaces;
1660         return nip;
1661 }
1662 EXPORT_SYMBOL(lnet_inet_enumerate);
1663
1664 int lnet_inet_select(struct lnet_ni *ni,
1665                      struct lnet_inetdev *ifaces,
1666                      int num_ifaces)
1667 {
1668         bool addr_set = nid_addr_is_set(&ni->ni_nid);
1669         int if_idx;
1670
1671         /* default to first interface if both interface and NID unspecified */
1672         if (!ni->ni_interface && !addr_set)
1673                 return 0;
1674
1675         for (if_idx = 0; if_idx < num_ifaces; if_idx++) {
1676                 if (ni->ni_interface && strlen(ni->ni_interface) &&
1677                         strcmp(ni->ni_interface, ifaces[if_idx].li_name) != 0)
1678                         /* not the specified interface */
1679                         continue;
1680
1681                 if (!addr_set)
1682                         /* IP unspecified, use IP of first matching interface */
1683                         break;
1684
1685                 if (ifaces[if_idx].li_size == NID_ADDR_BYTES(&ni->ni_nid)) {
1686                         char *addr = (char *)&ifaces[if_idx].li_ipaddr;
1687
1688                         if (ifaces[if_idx].li_size != 4)
1689                                 addr = (char *)ifaces[if_idx].li_ipv6addr;
1690
1691                         if (memcmp(ni->ni_nid.nid_addr, addr,
1692                                    ifaces[if_idx].li_size) == 0)
1693                                 break;
1694                 }
1695         }
1696
1697         if (if_idx < num_ifaces)
1698                 return if_idx;
1699
1700         if (addr_set)
1701                 CERROR("%s: failed to find IP address %s\n",
1702                        libcfs_lnd2modname(ni->ni_nid.nid_type),
1703                        libcfs_nidstr(&ni->ni_nid));
1704         else if (ni->ni_interface)
1705                 CERROR("%s: failed to find interface %s%s%s\n",
1706                        libcfs_lnd2modname(ni->ni_nid.nid_type),
1707                        ni->ni_interface, addr_set ? "@" : "",
1708                        addr_set ? libcfs_nidstr(&ni->ni_nid) : "");
1709
1710         return -EINVAL;
1711 }
1712 EXPORT_SYMBOL(lnet_inet_select);
1713
1714 int
1715 lnet_parse_ip2nets(const char **networksp, const char *ip2nets)
1716 {
1717         struct lnet_inetdev *ifaces = NULL;
1718         __u32     *ipaddrs = NULL;
1719         int nip;
1720         int        rc;
1721         int i;
1722
1723         if (current->nsproxy && current->nsproxy->net_ns)
1724                 nip = lnet_inet_enumerate(&ifaces, current->nsproxy->net_ns,
1725                                           false);
1726         else
1727                 nip = lnet_inet_enumerate(&ifaces, &init_net, false);
1728         if (nip < 0) {
1729                 if (nip != -ENOENT) {
1730                         LCONSOLE_ERROR_MSG(0x117,
1731                                            "Error %d enumerating local IP interfaces for ip2nets to match\n",
1732                                            nip);
1733                 } else {
1734                         LCONSOLE_ERROR_MSG(0x118,
1735                                            "No local IP interfaces for ip2nets to match\n");
1736                 }
1737                 return nip;
1738         }
1739
1740         CFS_ALLOC_PTR_ARRAY(ipaddrs, nip);
1741         if (!ipaddrs) {
1742                 rc = -ENOMEM;
1743                 CERROR("lnet: Can't allocate ipaddrs[%d], rc = %d\n",
1744                        nip, rc);
1745                 goto out_free_addrs;
1746         }
1747
1748         for (i = 0; i < nip; i++)
1749                 ipaddrs[i] = ntohl(ifaces[i].li_ipaddr);
1750
1751         rc = lnet_match_networks(networksp, ip2nets, ipaddrs, nip);
1752         if (rc < 0) {
1753                 LCONSOLE_ERROR_MSG(0x119, "Error %d parsing ip2nets\n", rc);
1754         } else if (rc == 0) {
1755                 LCONSOLE_ERROR_MSG(0x11a, "ip2nets does not match "
1756                                    "any local IP interfaces\n");
1757                 rc = -ENOENT;
1758         }
1759         CFS_FREE_PTR_ARRAY(ipaddrs, nip);
1760 out_free_addrs:
1761         kfree(ifaces);
1762         return rc > 0 ? 0 : rc;
1763 }