4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
32 #define DEBUG_SUBSYSTEM S_LNET
34 #include <linux/ctype.h>
35 #include <linux/inetdevice.h>
36 #include <linux/nsproxy.h>
37 #include <net/net_namespace.h>
38 #include <lnet/lib-lnet.h>
39 #include <net/addrconf.h>
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 */
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)
52 #define SPACESTR " \t\v\r\n"
53 #define DELIMITERS ":()[]"
56 #define strscpy(s1, s2, sz) strlcpy((s1), (s2), (sz))
60 lnet_syntax(const char *name, const char *str, int offset, int width)
62 static char dots[LNET_SINGLE_TEXTBUF_NOB];
63 static char dashes[LNET_SINGLE_TEXTBUF_NOB];
65 memset(dots, '.', sizeof(dots));
66 dots[sizeof(dots)-1] = 0;
67 memset(dashes, '-', sizeof(dashes));
68 dashes[sizeof(dashes)-1] = 0;
70 LCONSOLE_ERROR_MSG(0x10f, "Error parsing '%s=\"%s\"'\n", name, str);
71 LCONSOLE_ERROR_MSG(0x110, "here...........%.*s..%.*s|%.*s|\n",
72 (int)strlen(name), dots, offset, dots,
73 (width < 1) ? 0 : width - 1, dashes);
90 lnet_net_unique(__u32 net_id, struct list_head *netlist,
91 struct lnet_net **net)
93 struct lnet_net *net_l;
98 list_for_each_entry(net_l, netlist, net_list) {
99 if (net_l->net_id == net_id) {
109 /* check that the NI is unique within the list of NIs already added to
112 lnet_ni_unique_net(struct list_head *nilist, char *iface)
114 struct list_head *tmp;
117 list_for_each(tmp, nilist) {
118 ni = list_entry(tmp, struct lnet_ni, ni_netlist);
120 if (ni->ni_interface != NULL &&
121 strncmp(ni->ni_interface, iface, strlen(iface)) == 0)
128 in_array(__u32 *array, __u32 size, __u32 value)
132 for (i = 0; i < size; i++) {
133 if (array[i] == value)
141 lnet_net_append_cpts(__u32 *cpts, __u32 ncpts, struct lnet_net *net)
143 __u32 *added_cpts = NULL;
144 int i, j = 0, rc = 0;
147 * no need to go futher since a subset of the NIs already exist on
150 if (net->net_ncpts == LNET_CPT_NUMBER)
154 /* there is an NI which will exist on all CPTs */
155 if (net->net_cpts != NULL)
156 CFS_FREE_PTR_ARRAY(net->net_cpts, net->net_ncpts);
157 net->net_cpts = NULL;
158 net->net_ncpts = LNET_CPT_NUMBER;
162 if (net->net_cpts == NULL) {
163 CFS_ALLOC_PTR_ARRAY(net->net_cpts, ncpts);
164 if (net->net_cpts == NULL)
166 memcpy(net->net_cpts, cpts, ncpts * sizeof(*net->net_cpts));
167 net->net_ncpts = ncpts;
171 CFS_ALLOC_PTR_ARRAY(added_cpts, LNET_CPT_NUMBER);
172 if (added_cpts == NULL)
175 for (i = 0; i < ncpts; i++) {
176 if (!in_array(net->net_cpts, net->net_ncpts, cpts[i])) {
177 added_cpts[j] = cpts[i];
182 /* append the new cpts if any to the list of cpts in the net */
184 __u32 *array = NULL, *loc;
185 __u32 total_entries = j + net->net_ncpts;
187 CFS_ALLOC_PTR_ARRAY(array, total_entries);
193 memcpy(array, net->net_cpts,
194 net->net_ncpts * sizeof(*net->net_cpts));
195 loc = array + net->net_ncpts;
196 memcpy(loc, added_cpts, j * sizeof(*net->net_cpts));
198 CFS_FREE_PTR_ARRAY(net->net_cpts, net->net_ncpts);
199 net->net_ncpts = total_entries;
200 net->net_cpts = array;
204 CFS_FREE_PTR_ARRAY(added_cpts, LNET_CPT_NUMBER);
210 lnet_net_remove_cpts(__u32 *cpts, __u32 ncpts, struct lnet_net *net)
216 * Operation Assumption:
217 * This function is called after an NI has been removed from
220 * if we're removing an NI which exists on all CPTs then
221 * we have to check if any of the other NIs on this net also
222 * exists on all CPTs. If none, then we need to build our Net CPT
223 * list based on the remaining NIs.
225 * If the NI being removed exist on a subset of the CPTs then we
226 * alo rebuild the Net CPT list based on the remaining NIs, which
227 * should resutl in the expected Net CPT list.
231 * sometimes this function can be called due to some failure
232 * creating an NI, before any of the cpts are allocated, so check
233 * for that case and don't do anything
238 if (ncpts == LNET_CPT_NUMBER) {
240 * first iteration through the NI list in the net to see
241 * if any of the NIs exist on all the CPTs. If one is
242 * found then our job is done.
244 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
245 if (ni->ni_ncpts == LNET_CPT_NUMBER)
251 * Rebuild the Net CPT list again, thereby only including only the
252 * CPTs which the remaining NIs are associated with.
254 if (net->net_cpts != NULL) {
255 CFS_FREE_PTR_ARRAY(net->net_cpts, net->net_ncpts);
256 net->net_cpts = NULL;
259 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
260 rc = lnet_net_append_cpts(ni->ni_cpts, ni->ni_ncpts,
263 CERROR("Out of Memory\n");
265 * do our best to keep on going. Delete
266 * the net cpts and set it to NULL. This
267 * way we can keep on going but less
268 * efficiently, since memory accesses might be
271 if (net->net_cpts != NULL) {
272 CFS_FREE_PTR_ARRAY(net->net_cpts,
274 net->net_cpts = NULL;
275 net->net_ncpts = LNET_CPT_NUMBER;
283 lnet_ni_free(struct lnet_ni *ni)
285 lnet_net_remove_cpts(ni->ni_cpts, ni->ni_ncpts, ni->ni_net);
287 if (ni->ni_refs != NULL)
288 cfs_percpt_free(ni->ni_refs);
290 if (ni->ni_tx_queues != NULL)
291 cfs_percpt_free(ni->ni_tx_queues);
293 if (ni->ni_cpts != NULL)
294 cfs_expr_list_values_free(ni->ni_cpts, ni->ni_ncpts);
296 if (ni->ni_interface != NULL) {
297 LIBCFS_FREE(ni->ni_interface,
298 strlen(ni->ni_interface) + 1);
301 /* release reference to net namespace */
302 if (ni->ni_net_ns != NULL)
303 put_net(ni->ni_net_ns);
305 LIBCFS_FREE(ni, sizeof(*ni));
309 lnet_net_free(struct lnet_net *net)
311 struct list_head *tmp, *tmp2;
314 LASSERT(list_empty(&net->net_ni_zombie));
317 * delete any nis that haven't been added yet. This could happen
318 * if there is a failure on net startup
320 list_for_each_safe(tmp, tmp2, &net->net_ni_added) {
321 ni = list_entry(tmp, struct lnet_ni, ni_netlist);
322 list_del_init(&ni->ni_netlist);
326 /* delete any nis which have been started. */
327 list_for_each_safe(tmp, tmp2, &net->net_ni_list) {
328 ni = list_entry(tmp, struct lnet_ni, ni_netlist);
329 list_del_init(&ni->ni_netlist);
333 if (net->net_cpts != NULL)
334 CFS_FREE_PTR_ARRAY(net->net_cpts, net->net_ncpts);
336 LIBCFS_FREE(net, sizeof(*net));
340 lnet_net_alloc(__u32 net_id, struct list_head *net_list)
342 struct lnet_net *net;
344 if (!lnet_net_unique(net_id, net_list, &net)) {
345 CDEBUG(D_NET, "Returning duplicate net %p %s\n", net,
346 libcfs_net2str(net->net_id));
350 LIBCFS_ALLOC(net, sizeof(*net));
352 CERROR("Out of memory creating network %s\n",
353 libcfs_net2str(net_id));
357 INIT_LIST_HEAD(&net->net_list);
358 INIT_LIST_HEAD(&net->net_ni_list);
359 INIT_LIST_HEAD(&net->net_ni_added);
360 INIT_LIST_HEAD(&net->net_ni_zombie);
361 INIT_LIST_HEAD(&net->net_rtr_pref_nids);
362 spin_lock_init(&net->net_lock);
364 net->net_id = net_id;
365 net->net_last_alive = ktime_get_seconds();
367 net->net_sel_priority = LNET_MAX_SELECTION_PRIORITY;
369 /* initialize global paramters to undefiend */
370 net->net_tunables.lct_peer_timeout = -1;
371 net->net_tunables.lct_max_tx_credits = -1;
372 net->net_tunables.lct_peer_tx_credits = -1;
373 net->net_tunables.lct_peer_rtr_credits = -1;
376 list_add_tail(&net->net_list, net_list);
382 lnet_ni_add_interface(struct lnet_ni *ni, char *iface)
384 size_t iface_len = strlen(iface) + 1;
389 if (ni->ni_interface != NULL) {
390 LCONSOLE_ERROR_MSG(0x115, "%s: interface %s already set for net %s: rc = %d\n",
391 iface, ni->ni_interface,
392 libcfs_net2str(LNET_NID_NET(&ni->ni_nid)),
397 /* Allocate memory for the interface, so the code parsing input into
398 * tokens and adding interfaces can free the input safely.
399 * ni->ni_interface is freed in lnet_ni_free().
401 LIBCFS_ALLOC(ni->ni_interface, iface_len);
403 if (ni->ni_interface == NULL) {
404 CERROR("%s: cannot allocate net interface name: rc = %d\n",
409 strscpy(ni->ni_interface, iface, iface_len);
414 static struct lnet_ni *
415 lnet_ni_alloc_common(struct lnet_net *net, char *iface)
417 struct lnet_tx_queue *tq;
422 /* make sure that this NI is unique in the net it's
424 if (!lnet_ni_unique_net(&net->net_ni_added, iface))
427 LIBCFS_ALLOC(ni, sizeof(*ni));
429 CERROR("Out of memory creating network interface %s%s\n",
430 libcfs_net2str(net->net_id),
431 (iface != NULL) ? iface : "");
435 spin_lock_init(&ni->ni_lock);
436 INIT_LIST_HEAD(&ni->ni_netlist);
437 INIT_LIST_HEAD(&ni->ni_recovery);
438 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
439 ni->ni_refs = cfs_percpt_alloc(lnet_cpt_table(),
440 sizeof(*ni->ni_refs[0]));
441 if (ni->ni_refs == NULL)
444 ni->ni_tx_queues = cfs_percpt_alloc(lnet_cpt_table(),
445 sizeof(*ni->ni_tx_queues[0]));
446 if (ni->ni_tx_queues == NULL)
449 cfs_percpt_for_each(tq, i, ni->ni_tx_queues)
450 INIT_LIST_HEAD(&tq->tq_delayed);
453 /* LND will fill in the address part of the NID */
454 ni->ni_nid.nid_type = LNET_NETTYP(net->net_id);
455 ni->ni_nid.nid_num = cpu_to_be16(LNET_NETNUM(net->net_id));
457 /* Store net namespace in which current ni is being created */
458 if (current->nsproxy && current->nsproxy->net_ns)
459 ni->ni_net_ns = get_net(current->nsproxy->net_ns);
461 ni->ni_net_ns = get_net(&init_net);
463 ni->ni_state = LNET_NI_STATE_INIT;
464 ni->ni_sel_priority = LNET_MAX_SELECTION_PRIORITY;
465 list_add_tail(&ni->ni_netlist, &net->net_ni_added);
468 * if an interface name is provided then make sure to add in that
469 * interface name in NI
472 if (lnet_ni_add_interface(ni, iface) != 0)
481 /* allocate and add to the provided network */
483 lnet_ni_alloc(struct lnet_net *net, struct cfs_expr_list *el, char *iface)
488 ni = lnet_ni_alloc_common(net, iface);
494 ni->ni_ncpts = LNET_CPT_NUMBER;
496 rc = cfs_expr_list_values(el, LNET_CPT_NUMBER, &ni->ni_cpts);
498 CERROR("Failed to set CPTs for NI %s(%s): %d\n",
499 libcfs_net2str(net->net_id),
500 (iface != NULL) ? iface : "", rc);
504 LASSERT(rc <= LNET_CPT_NUMBER);
505 if (rc == LNET_CPT_NUMBER) {
506 CFS_FREE_PTR_ARRAY(ni->ni_cpts, rc);
513 rc = lnet_net_append_cpts(ni->ni_cpts, ni->ni_ncpts, net);
524 lnet_ni_alloc_w_cpt_array(struct lnet_net *net, __u32 *cpts, __u32 ncpts,
530 ni = lnet_ni_alloc_common(net, iface);
536 ni->ni_ncpts = LNET_CPT_NUMBER;
538 size_t array_size = ncpts * sizeof(ni->ni_cpts[0]);
540 CFS_ALLOC_PTR_ARRAY(ni->ni_cpts, ncpts);
541 if (ni->ni_cpts == NULL)
543 memcpy(ni->ni_cpts, cpts, array_size);
544 ni->ni_ncpts = ncpts;
547 rc = lnet_net_append_cpts(ni->ni_cpts, ni->ni_ncpts, net);
558 * Parse the networks string and create the matching set of NIs on the
562 lnet_parse_networks(struct list_head *netlist, const char *networks)
564 struct cfs_expr_list *net_el = NULL;
565 struct cfs_expr_list *ni_el = NULL;
569 struct lnet_net *net;
570 struct lnet_ni *ni = NULL;
574 if (networks == NULL) {
575 CERROR("networks string is undefined\n");
579 if (strlen(networks) > LNET_SINGLE_TEXTBUF_NOB) {
580 /* _WAY_ conservative */
581 LCONSOLE_ERROR_MSG(0x112, "Can't parse networks: string too "
586 tokensize = strlen(networks) + 1;
588 LIBCFS_ALLOC(tokens, tokensize);
589 if (tokens == NULL) {
590 CERROR("Can't allocate net tokens\n");
594 memcpy(tokens, networks, tokensize);
600 * NB we don't check interface conflicts here; it's the LNDs
601 * responsibility (if it cares at all)
610 * Parse a network string into its components.
612 * <name>{"("...")"}{"["<el>"]"}
615 /* Network name (mandatory) */
616 while (isspace(*str))
621 str += strcspn(str, SPACESTR ":()[],");
622 while (isspace(*str))
625 /* Interface list (optional) */
629 str += strcspn(str, ")");
636 } while (isspace(*str));
641 /* CPT expression (optional) */
644 str += strcspn(str, "]");
649 rc = cfs_expr_list_parse(elstr, str - elstr + 1,
650 0, LNET_CPT_NUMBER - 1,
659 } while (isspace(*str));
663 if (*str && (strchr(DELIMITERS, *str) != NULL))
666 /* go to the next net if it exits */
667 str += strcspn(str, ",");
672 * At this point the name is properly terminated.
674 net_id = libcfs_str2net(name);
675 if (net_id == LNET_NET_ANY) {
676 LCONSOLE_ERROR_MSG(0x113,
677 "Unrecognised network type\n");
682 if (LNET_NETTYP(net_id) == LOLND) {
683 /* Loopback is implicit, and there can be only one. */
685 cfs_expr_list_free(net_el);
688 /* Should we error out instead? */
693 * All network paramaters are now known.
697 /* always allocate a net, since we will eventually add an
698 * interface to it, or we will fail, in which case we'll
700 net = lnet_net_alloc(net_id, netlist);
701 if (IS_ERR_OR_NULL(net))
706 * No interface list was specified, allocate a
707 * ni using the defaults.
709 ni = lnet_ni_alloc(net, net_el, NULL);
710 if (IS_ERR_OR_NULL(ni))
715 cfs_expr_list_free(net_el);
725 /* Interface name (mandatory) */
726 while (isspace(*nistr))
729 nistr += strcspn(nistr, SPACESTR "[],");
730 while (isspace(*nistr))
733 /* CPT expression (optional) */
736 nistr += strcspn(nistr, "]");
741 rc = cfs_expr_list_parse(elstr,
743 0, LNET_CPT_NUMBER - 1,
752 } while (isspace(*nistr));
758 * End of single interface specificaton,
759 * advance to the start of the next one, if
765 } while (isspace(*nistr));
776 * At this point the name is properly terminated.
783 ni = lnet_ni_alloc(net, ni_el, name);
784 if (IS_ERR_OR_NULL(ni))
788 if (ni_el != net_el) {
789 cfs_expr_list_free(ni_el);
796 cfs_expr_list_free(net_el);
801 LIBCFS_FREE(tokens, tokensize);
805 lnet_syntax("networks", networks, (int)(str - tokens), strlen(str));
807 /* free the net list and all the nis on each net */
808 while ((net = list_first_entry_or_null(netlist,
810 net_list)) != NULL) {
811 list_del_init(&net->net_list);
815 if (ni_el && ni_el != net_el)
816 cfs_expr_list_free(ni_el);
818 cfs_expr_list_free(net_el);
820 LIBCFS_FREE(tokens, tokensize);
825 static struct lnet_text_buf *lnet_new_text_buf(int str_len)
827 struct lnet_text_buf *ltb;
830 /* NB allocate space for the terminating 0 */
831 nob = offsetof(struct lnet_text_buf, ltb_text[str_len + 1]);
832 if (nob > LNET_SINGLE_TEXTBUF_NOB) {
833 /* _way_ conservative for "route net gateway..." */
834 CERROR("text buffer too big\n");
838 if (lnet_tbnob + nob > LNET_MAX_TEXTBUF_NOB) {
839 CERROR("Too many text buffers\n");
843 LIBCFS_ALLOC(ltb, nob);
848 ltb->ltb_text[0] = 0;
854 lnet_free_text_buf(struct lnet_text_buf *ltb)
856 lnet_tbnob -= ltb->ltb_size;
857 LIBCFS_FREE(ltb, ltb->ltb_size);
861 lnet_free_text_bufs(struct list_head *tbs)
863 struct lnet_text_buf *ltb;
865 while ((ltb = list_first_entry_or_null(tbs, struct lnet_text_buf,
866 ltb_list)) != NULL) {
867 list_del(<b->ltb_list);
868 lnet_free_text_buf(ltb);
873 lnet_str2tbs_sep(struct list_head *tbs, const char *str)
879 struct lnet_text_buf *ltb;
881 /* Split 'str' into separate commands */
883 /* skip leading whitespace */
884 while (isspace(*str))
887 /* scan for separator or comment */
888 for (sep = str; *sep != 0; sep++)
889 if (lnet_issep(*sep) || *sep == '#')
892 nob = (int)(sep - str);
894 ltb = lnet_new_text_buf(nob);
896 lnet_free_text_bufs(&pending);
900 for (i = 0; i < nob; i++)
902 ltb->ltb_text[i] = ' ';
904 ltb->ltb_text[i] = str[i];
906 ltb->ltb_text[nob] = 0;
908 list_add_tail(<b->ltb_list, &pending);
912 /* scan for separator */
915 } while (*sep != 0 && !lnet_issep(*sep));
924 list_splice(&pending, tbs->prev);
929 lnet_expand1tb(struct list_head *list,
930 char *str, char *sep1, char *sep2,
931 char *item, int itemlen)
933 int len1 = (int)(sep1 - str);
934 int len2 = strlen(sep2 + 1);
935 struct lnet_text_buf *ltb;
937 LASSERT (*sep1 == '[');
938 LASSERT (*sep2 == ']');
940 ltb = lnet_new_text_buf(len1 + itemlen + len2);
944 memcpy(ltb->ltb_text, str, len1);
945 memcpy(<b->ltb_text[len1], item, itemlen);
946 memcpy(<b->ltb_text[len1+itemlen], sep2 + 1, len2);
947 ltb->ltb_text[len1 + itemlen + len2] = 0;
949 list_add_tail(<b->ltb_list, list);
954 lnet_str2tbs_expand(struct list_head *tbs, char *str)
969 sep = strchr(str, '[');
970 if (sep == NULL) /* nothing to expand */
973 sep2 = strchr(sep, ']');
977 for (parsed = sep; parsed < sep2; parsed = enditem) {
980 while (enditem < sep2 && *enditem != ',')
983 if (enditem == parsed) /* no empty items */
986 if (sscanf(parsed, "%d-%d/%d%n", &lo, &hi, &stride, &scanned) < 3) {
988 if (sscanf(parsed, "%d-%d%n", &lo, &hi, &scanned) < 2) {
990 /* simple string enumeration */
991 if (lnet_expand1tb(&pending, str, sep, sep2,
992 parsed, (int)(enditem - parsed)) != 0)
1001 /* range expansion */
1003 if (enditem != parsed + scanned) /* no trailing junk */
1006 if (hi < 0 || lo < 0 || stride < 0 || hi < lo ||
1007 (hi - lo) % stride != 0)
1010 for (i = lo; i <= hi; i += stride) {
1012 snprintf(num, sizeof(num), "%d", i);
1014 if (nob + 1 == sizeof(num))
1017 if (lnet_expand1tb(&pending, str, sep, sep2,
1023 list_splice(&pending, tbs->prev);
1027 lnet_free_text_bufs(&pending);
1032 lnet_parse_hops (char *str, unsigned int *hops)
1034 int len = strlen(str);
1037 return (sscanf(str, "%u%n", hops, &nob) >= 1 &&
1039 *hops > 0 && *hops < 256);
1042 #define LNET_PRIORITY_SEPARATOR (':')
1045 lnet_parse_priority(char *str, unsigned int *priority, char **token)
1051 sep = strchr(str, LNET_PRIORITY_SEPARATOR);
1056 len = strlen(sep + 1);
1058 if ((sscanf((sep+1), "%u%n", priority, &nob) < 1) || (len != nob)) {
1059 /* Update the caller's token pointer so it treats the found
1060 priority as the token to report in the error message. */
1061 *token += sep - str + 1;
1065 CDEBUG(D_NET, "gateway %s, priority %d, nob %d\n", str, *priority, nob);
1068 * Change priority separator to \0 to be able to parse NID
1075 lnet_parse_route(char *str, int *im_a_router)
1077 /* static scratch buffer OK (single threaded) */
1078 static char cmd[LNET_SINGLE_TEXTBUF_NOB];
1081 LIST_HEAD(gateways);
1082 struct list_head *tmp1;
1083 struct list_head *tmp2;
1085 struct lnet_nid nid;
1086 struct lnet_text_buf *ltb = NULL;
1087 struct lnet_text_buf *ltb1, *ltb2;
1095 unsigned int priority = 0;
1097 /* save a copy of the string for error messages */
1098 strncpy(cmd, str, sizeof(cmd));
1099 cmd[sizeof(cmd) - 1] = '\0';
1103 /* scan for token start */
1104 while (isspace(*sep))
1107 if (ntokens < (got_hops ? 3 : 2))
1115 /* scan for token end */
1116 while (*sep != 0 && !isspace(*sep))
1122 tmp2 = &nets; /* expanding nets */
1123 } else if (ntokens == 2 &&
1124 lnet_parse_hops(token, &hops)) {
1125 got_hops = 1; /* got a hop count */
1128 tmp2 = &gateways; /* expanding gateways */
1131 ltb = lnet_new_text_buf(strlen(token));
1135 strcpy(ltb->ltb_text, token);
1136 tmp1 = <b->ltb_list;
1137 list_add_tail(tmp1, tmp2);
1139 while (tmp1 != tmp2) {
1140 ltb = list_entry(tmp1, struct lnet_text_buf, ltb_list);
1142 rc = lnet_str2tbs_expand(tmp1->next, ltb->ltb_text);
1148 if (rc > 0) { /* expanded! */
1149 list_del(<b->ltb_list);
1150 lnet_free_text_buf(ltb);
1155 net = libcfs_str2net(ltb->ltb_text);
1156 if (net == LNET_NET_ANY ||
1157 LNET_NETTYP(net) == LOLND)
1160 rc = lnet_parse_priority(ltb->ltb_text,
1165 if (libcfs_strnid(&nid, ltb->ltb_text) != 0 ||
1172 /* if there are no hops set then we want to flag this value as
1173 * unset since hops is an optional parameter */
1175 hops = LNET_UNDEFINED_HOPS;
1177 LASSERT(!list_empty(&nets));
1178 LASSERT(!list_empty(&gateways));
1180 list_for_each_entry(ltb1, &nets, ltb_list) {
1181 net = libcfs_str2net(ltb1->ltb_text);
1182 LASSERT(net != LNET_NET_ANY);
1184 list_for_each_entry(ltb2, &gateways, ltb_list) {
1185 LASSERT(libcfs_strnid(&nid, ltb->ltb_text) == 0);
1187 if (lnet_islocalnid(&nid)) {
1192 rc = lnet_add_route(net, hops, &nid, priority, 1);
1193 if (rc != 0 && rc != -EEXIST && rc != -EHOSTUNREACH) {
1194 CERROR("Can't create route "
1196 libcfs_net2str(net),
1197 libcfs_nidstr(&nid));
1207 lnet_syntax("routes", cmd, (int)(token - str), strlen(token));
1209 lnet_free_text_bufs(&nets);
1210 lnet_free_text_bufs(&gateways);
1215 lnet_parse_route_tbs(struct list_head *tbs, int *im_a_router)
1217 struct lnet_text_buf *ltb;
1219 while ((ltb = list_first_entry_or_null(tbs, struct lnet_text_buf,
1220 ltb_list)) != NULL) {
1221 if (lnet_parse_route(ltb->ltb_text, im_a_router) < 0) {
1222 lnet_free_text_bufs(tbs);
1226 list_del(<b->ltb_list);
1227 lnet_free_text_buf(ltb);
1234 lnet_parse_routes(const char *routes, int *im_a_router)
1241 if (lnet_str2tbs_sep(&tbs, routes) < 0) {
1242 CERROR("Error parsing routes\n");
1245 rc = lnet_parse_route_tbs(&tbs, im_a_router);
1248 LASSERT (lnet_tbnob == 0);
1253 lnet_match_network_token(char *token, int len, __u32 *ipaddrs, int nip)
1259 rc = cfs_ip_addr_parse(token, len, &list);
1263 for (rc = i = 0; !rc && i < nip; i++)
1264 rc = cfs_ip_addr_match(ipaddrs[i], &list);
1266 cfs_expr_list_free_list(&list);
1272 lnet_match_network_tokens(char *net_entry, __u32 *ipaddrs, int nip)
1274 static char tokens[LNET_SINGLE_TEXTBUF_NOB];
1284 LASSERT(strlen(net_entry) < sizeof(tokens));
1286 /* work on a copy of the string */
1287 strcpy(tokens, net_entry);
1290 /* scan for token start */
1291 while (isspace(*sep))
1298 /* scan for token end */
1299 while (*sep != 0 && !isspace(*sep))
1304 if (ntokens++ == 0) {
1309 len = strlen(token);
1311 rc = lnet_match_network_token(token, len, ipaddrs, nip);
1313 lnet_syntax("ip2nets", net_entry,
1314 (int)(token - tokens), len);
1318 matched |= (rc != 0);
1324 strcpy(net_entry, net); /* replace with matched net */
1329 lnet_netspec2net(char *netspec)
1331 char *bracket = strchr(netspec, '(');
1334 if (bracket != NULL)
1337 net = libcfs_str2net(netspec);
1339 if (bracket != NULL)
1346 lnet_splitnets(char *source, struct list_head *nets)
1351 struct lnet_text_buf *tb;
1352 struct lnet_text_buf *tb2;
1357 LASSERT(!list_empty(nets));
1358 LASSERT(nets->next == nets->prev); /* single entry */
1360 tb = list_first_entry(nets, struct lnet_text_buf, ltb_list);
1363 sep = strchr(tb->ltb_text, ',');
1364 bracket = strchr(tb->ltb_text, '(');
1369 /* netspec lists interfaces... */
1371 offset2 = offset + (int)(bracket - tb->ltb_text);
1372 len = strlen(bracket);
1374 bracket = strchr(bracket + 1, ')');
1376 if (bracket == NULL ||
1377 !(bracket[1] == ',' || bracket[1] == 0)) {
1378 lnet_syntax("ip2nets", source, offset2, len);
1382 sep = (bracket[1] == 0) ? NULL : bracket + 1;
1388 net = lnet_netspec2net(tb->ltb_text);
1389 if (net == LNET_NET_ANY) {
1390 lnet_syntax("ip2nets", source, offset,
1391 strlen(tb->ltb_text));
1395 list_for_each_entry(tb2, nets, ltb_list) {
1399 if (net == lnet_netspec2net(tb2->ltb_text)) {
1400 /* duplicate network */
1401 lnet_syntax("ip2nets", source, offset,
1402 strlen(tb->ltb_text));
1410 offset += (int)(sep - tb->ltb_text);
1412 tb2 = lnet_new_text_buf(len);
1416 strncpy(tb2->ltb_text, sep, len);
1417 tb2->ltb_text[len] = '\0';
1418 list_add_tail(&tb2->ltb_list, nets);
1425 lnet_match_networks(const char **networksp, const char *ip2nets,
1426 __u32 *ipaddrs, int nip)
1428 static char networks[LNET_SINGLE_TEXTBUF_NOB];
1429 static char source[LNET_SINGLE_TEXTBUF_NOB];
1431 LIST_HEAD(raw_entries);
1432 LIST_HEAD(matched_nets);
1433 LIST_HEAD(current_nets);
1434 struct list_head *t;
1435 struct list_head *t2;
1436 struct lnet_text_buf *tb;
1441 if (lnet_str2tbs_sep(&raw_entries, ip2nets) < 0) {
1442 CERROR("Error parsing ip2nets\n");
1443 LASSERT(lnet_tbnob == 0);
1452 while ((tb = list_first_entry_or_null(&raw_entries,
1453 struct lnet_text_buf,
1454 ltb_list)) != NULL) {
1455 strncpy(source, tb->ltb_text, sizeof(source));
1456 source[sizeof(source) - 1] = '\0';
1458 /* replace ltb_text with the network(s) add on match */
1459 rc = lnet_match_network_tokens(tb->ltb_text, ipaddrs, nip);
1463 list_del(&tb->ltb_list);
1465 if (rc == 0) { /* no match */
1466 lnet_free_text_buf(tb);
1470 /* split into separate networks */
1471 INIT_LIST_HEAD(¤t_nets);
1472 list_add(&tb->ltb_list, ¤t_nets);
1473 rc = lnet_splitnets(source, ¤t_nets);
1477 list_for_each_safe(t, t2, ¤t_nets) {
1478 tb = list_entry(t, struct lnet_text_buf, ltb_list);
1480 list_move_tail(&tb->ltb_list, &matched_nets);
1482 len += scnprintf(networks + len, sizeof(networks) - len,
1483 "%s%s", (len == 0) ? "" : ",",
1486 if (len >= sizeof(networks)) {
1487 CERROR("Too many matched networks\n");
1497 lnet_free_text_bufs(&raw_entries);
1498 lnet_free_text_bufs(&matched_nets);
1499 lnet_free_text_bufs(¤t_nets);
1500 LASSERT(lnet_tbnob == 0);
1505 *networksp = networks;
1509 int lnet_inet_enumerate(struct lnet_inetdev **dev_list, struct net *ns, bool v6)
1511 struct lnet_inetdev *ifaces = NULL;
1512 struct net_device *dev;
1515 DECLARE_CONST_IN_IFADDR(ifa);
1518 for_each_netdev(ns, dev) {
1519 int flags = dev_get_flags(dev);
1520 struct in_device *in_dev;
1521 struct inet6_dev *in6_dev;
1522 const struct inet6_ifaddr *ifa6;
1526 if (flags & IFF_LOOPBACK) /* skip the loopback IF */
1529 if (!(flags & IFF_UP)) {
1530 CWARN("lnet: Ignoring interface %s: it's down\n",
1535 node_id = dev_to_node(&dev->dev);
1536 cpt = cfs_cpt_of_node(lnet_cpt_table(), node_id);
1538 in_dev = __in_dev_get_rtnl(dev);
1541 CWARN("lnet: Interface %s has no IPv4 status.\n",
1546 in_dev_for_each_ifa_rtnl(ifa, in_dev) {
1547 if (nip >= nalloc) {
1548 struct lnet_inetdev *tmp;
1550 nalloc += LNET_INTERFACES_NUM;
1551 tmp = krealloc(ifaces, nalloc * sizeof(*tmp),
1562 ifaces[nip].li_cpt = cpt;
1563 ifaces[nip].li_iff_master = !!(flags & IFF_MASTER);
1564 ifaces[nip].li_ipv6 = false;
1565 ifaces[nip].li_index = dev->ifindex;
1566 ifaces[nip].li_ipaddr = ntohl(ifa->ifa_local);
1567 ifaces[nip].li_netmask = ntohl(ifa->ifa_mask);
1568 strlcpy(ifaces[nip].li_name, ifa->ifa_label,
1569 sizeof(ifaces[nip].li_name));
1577 #if IS_ENABLED(CONFIG_IPV6)
1578 in6_dev = __in6_dev_get(dev);
1581 CWARN("lnet: Interface %s has no IP status.\n",
1586 list_for_each_entry_rcu(ifa6, &in6_dev->addr_list, if_list) {
1587 if (ifa6->flags & IFA_F_TEMPORARY)
1589 if (nip >= nalloc) {
1590 struct lnet_inetdev *tmp;
1592 nalloc += LNET_INTERFACES_NUM;
1593 tmp = krealloc(ifaces, nalloc * sizeof(*tmp),
1604 ifaces[nip].li_cpt = cpt;
1605 ifaces[nip].li_iff_master = !!(flags & IFF_MASTER);
1606 ifaces[nip].li_ipv6 = true;
1607 ifaces[nip].li_index = dev->ifindex;
1608 memcpy(ifaces[nip].li_ipv6addr,
1609 &ifa6->addr, sizeof(struct in6_addr));
1610 strlcpy(ifaces[nip].li_name, dev->name,
1611 sizeof(ifaces[nip].li_name));
1613 /* As different IPv6 addresses don't have unique
1614 * labels, it is safest just to use the first
1615 * and ignore the rest.
1619 #endif /* IS_ENABLED(CONFIG_IPV6) */
1626 CERROR("lnet: Can't find any usable interfaces, rc = -ENOENT\n");
1633 EXPORT_SYMBOL(lnet_inet_enumerate);
1636 lnet_parse_ip2nets(const char **networksp, const char *ip2nets)
1638 struct lnet_inetdev *ifaces = NULL;
1639 __u32 *ipaddrs = NULL;
1644 if (current->nsproxy && current->nsproxy->net_ns)
1645 nip = lnet_inet_enumerate(&ifaces, current->nsproxy->net_ns,
1648 nip = lnet_inet_enumerate(&ifaces, &init_net, false);
1650 if (nip != -ENOENT) {
1651 LCONSOLE_ERROR_MSG(0x117,
1652 "Error %d enumerating local IP interfaces for ip2nets to match\n",
1655 LCONSOLE_ERROR_MSG(0x118,
1656 "No local IP interfaces for ip2nets to match\n");
1661 CFS_ALLOC_PTR_ARRAY(ipaddrs, nip);
1664 CERROR("lnet: Can't allocate ipaddrs[%d], rc = %d\n",
1666 goto out_free_addrs;
1669 for (i = 0; i < nip; i++)
1670 ipaddrs[i] = ifaces[i].li_ipaddr;
1672 rc = lnet_match_networks(networksp, ip2nets, ipaddrs, nip);
1674 LCONSOLE_ERROR_MSG(0x119, "Error %d parsing ip2nets\n", rc);
1675 } else if (rc == 0) {
1676 LCONSOLE_ERROR_MSG(0x11a, "ip2nets does not match "
1677 "any local IP interfaces\n");
1680 CFS_FREE_PTR_ARRAY(ipaddrs, nip);
1683 return rc > 0 ? 0 : rc;