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