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/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 #define DEBUG_SUBSYSTEM S_LNET
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>
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 lnet_syntax(const char *name, const char *str, int offset, int width)
58 static char dots[LNET_SINGLE_TEXTBUF_NOB];
59 static char dashes[LNET_SINGLE_TEXTBUF_NOB];
61 memset(dots, '.', sizeof(dots));
62 dots[sizeof(dots)-1] = 0;
63 memset(dashes, '-', sizeof(dashes));
64 dashes[sizeof(dashes)-1] = 0;
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);
86 lnet_net_unique(__u32 net_id, struct list_head *netlist,
87 struct lnet_net **net)
89 struct lnet_net *net_l;
94 list_for_each_entry(net_l, netlist, net_list) {
95 if (net_l->net_id == net_id) {
105 /* check that the NI is unique within the list of NIs already added to
108 lnet_ni_unique_net(struct list_head *nilist, char *iface)
110 struct list_head *tmp;
113 list_for_each(tmp, nilist) {
114 ni = list_entry(tmp, struct lnet_ni, ni_netlist);
116 if (ni->ni_interfaces[0] != NULL &&
117 strncmp(ni->ni_interfaces[0], iface, strlen(iface)) == 0)
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 */
127 lnet_ni_unique_ni(char *iface_list[LNET_INTERFACES_NUM], char *iface)
130 for (i = 0; i < LNET_INTERFACES_NUM; i++) {
131 if (iface_list[i] != NULL &&
132 strncmp(iface_list[i], iface, strlen(iface)) == 0)
140 in_array(__u32 *array, __u32 size, __u32 value)
144 for (i = 0; i < size; i++) {
145 if (array[i] == value)
153 lnet_net_append_cpts(__u32 *cpts, __u32 ncpts, struct lnet_net *net)
155 __u32 *added_cpts = NULL;
156 int i, j = 0, rc = 0;
159 * no need to go futher since a subset of the NIs already exist on
162 if (net->net_ncpts == LNET_CPT_NUMBER)
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) *
170 net->net_cpts = NULL;
171 net->net_ncpts = LNET_CPT_NUMBER;
175 if (net->net_cpts == NULL) {
176 LIBCFS_ALLOC(net->net_cpts, sizeof(*net->net_cpts) * ncpts);
177 if (net->net_cpts == NULL)
179 memcpy(net->net_cpts, cpts, ncpts * sizeof(*net->net_cpts));
180 net->net_ncpts = ncpts;
184 LIBCFS_ALLOC(added_cpts, sizeof(*added_cpts) * LNET_CPT_NUMBER);
185 if (added_cpts == NULL)
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];
195 /* append the new cpts if any to the list of cpts in the net */
197 __u32 *array = NULL, *loc;
198 __u32 total_entries = j + net->net_ncpts;
200 LIBCFS_ALLOC(array, sizeof(*net->net_cpts) * total_entries);
206 memcpy(array, net->net_cpts, net->net_ncpts);
207 loc = array + net->net_ncpts;
208 memcpy(loc, added_cpts, j);
210 LIBCFS_FREE(net->net_cpts, sizeof(*net->net_cpts) *
212 net->net_ncpts = total_entries;
213 net->net_cpts = array;
217 LIBCFS_FREE(added_cpts, sizeof(*added_cpts) * LNET_CPT_NUMBER);
223 lnet_net_remove_cpts(__u32 *cpts, __u32 ncpts, struct lnet_net *net)
229 * Operation Assumption:
230 * This function is called after an NI has been removed from
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.
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.
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
251 if (ncpts == LNET_CPT_NUMBER) {
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.
257 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
258 if (ni->ni_ncpts == LNET_CPT_NUMBER)
264 * Rebuild the Net CPT list again, thereby only including only the
265 * CPTs which the remaining NIs are associated with.
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;
273 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
274 rc = lnet_net_append_cpts(ni->ni_cpts, ni->ni_ncpts,
277 CERROR("Out of Memory\n");
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
285 if (net->net_cpts != NULL) {
286 LIBCFS_FREE(net->net_cpts,
287 sizeof(*net->net_cpts) *
289 net->net_cpts = NULL;
290 net->net_ncpts = LNET_CPT_NUMBER;
298 lnet_ni_free(struct lnet_ni *ni)
302 lnet_net_remove_cpts(ni->ni_cpts, ni->ni_ncpts, ni->ni_net);
304 if (ni->ni_refs != NULL)
305 cfs_percpt_free(ni->ni_refs);
307 if (ni->ni_tx_queues != NULL)
308 cfs_percpt_free(ni->ni_tx_queues);
310 if (ni->ni_cpts != NULL)
311 cfs_expr_list_values_free(ni->ni_cpts, ni->ni_ncpts);
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);
319 /* release reference to net namespace */
320 if (ni->ni_net_ns != NULL)
321 put_net(ni->ni_net_ns);
323 LIBCFS_FREE(ni, sizeof(*ni));
327 lnet_net_free(struct lnet_net *net)
329 struct list_head *tmp, *tmp2;
332 LASSERT(list_empty(&net->net_ni_zombie));
335 * delete any nis that haven't been added yet. This could happen
336 * if there is a failure on net startup
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);
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);
351 if (net->net_cpts != NULL)
352 LIBCFS_FREE(net->net_cpts,
353 sizeof(*net->net_cpts) * net->net_ncpts);
355 LIBCFS_FREE(net, sizeof(*net));
359 lnet_net_alloc(__u32 net_id, struct list_head *net_list)
361 struct lnet_net *net;
363 if (!lnet_net_unique(net_id, net_list, NULL)) {
364 CERROR("Duplicate net %s. Ignore\n",
365 libcfs_net2str(net_id));
369 LIBCFS_ALLOC(net, sizeof(*net));
371 CERROR("Out of memory creating network %s\n",
372 libcfs_net2str(net_id));
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);
382 net->net_id = net_id;
383 net->net_last_alive = ktime_get_real_seconds();
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;
392 list_add_tail(&net->net_list, net_list);
398 lnet_ni_add_interface(struct lnet_ni *ni, char *iface)
405 if (!lnet_ni_unique_ni(ni->ni_interfaces, iface))
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)
418 if (niface >= LNET_INTERFACES_NUM) {
419 LCONSOLE_ERROR_MSG(0x115, "Too many interfaces "
421 libcfs_net2str(LNET_NIDNET(ni->ni_nid)));
425 LIBCFS_ALLOC(ni->ni_interfaces[niface],
428 if (ni->ni_interfaces[niface] == NULL) {
429 CERROR("Can't allocate net interface name\n");
433 strncpy(ni->ni_interfaces[niface], iface,
439 static struct lnet_ni *
440 lnet_ni_alloc_common(struct lnet_net *net, char *iface)
442 struct lnet_tx_queue *tq;
447 /* make sure that this NI is unique in the net it's
449 if (!lnet_ni_unique_net(&net->net_ni_added, iface))
452 LIBCFS_ALLOC(ni, sizeof(*ni));
454 CERROR("Out of memory creating network interface %s%s\n",
455 libcfs_net2str(net->net_id),
456 (iface != NULL) ? iface : "");
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)
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)
474 cfs_percpt_for_each(tq, i, ni->ni_tx_queues)
475 INIT_LIST_HEAD(&tq->tq_delayed);
478 /* LND will fill in the address part of the NID */
479 ni->ni_nid = LNET_MKNID(net->net_id, 0);
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);
485 ni->ni_net_ns = get_net(&init_net);
487 ni->ni_state = LNET_NI_STATE_INIT;
488 list_add_tail(&ni->ni_netlist, &net->net_ni_added);
491 * if an interface name is provided then make sure to add in that
492 * interface name in NI
495 if (lnet_ni_add_interface(ni, iface) != 0)
504 /* allocate and add to the provided network */
506 lnet_ni_alloc(struct lnet_net *net, struct cfs_expr_list *el, char *iface)
511 ni = lnet_ni_alloc_common(net, iface);
517 ni->ni_ncpts = LNET_CPT_NUMBER;
519 rc = cfs_expr_list_values(el, LNET_CPT_NUMBER, &ni->ni_cpts);
521 CERROR("Failed to set CPTs for NI %s(%s): %d\n",
522 libcfs_net2str(net->net_id),
523 (iface != NULL) ? iface : "", rc);
527 LASSERT(rc <= LNET_CPT_NUMBER);
528 if (rc == LNET_CPT_NUMBER) {
529 LIBCFS_FREE(ni->ni_cpts, rc * sizeof(ni->ni_cpts[0]));
536 rc = lnet_net_append_cpts(ni->ni_cpts, ni->ni_ncpts, net);
547 lnet_ni_alloc_w_cpt_array(struct lnet_net *net, __u32 *cpts, __u32 ncpts,
553 ni = lnet_ni_alloc_common(net, iface);
559 ni->ni_ncpts = LNET_CPT_NUMBER;
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)
565 memcpy(ni->ni_cpts, cpts, array_size);
566 ni->ni_ncpts = ncpts;
569 rc = lnet_net_append_cpts(ni->ni_cpts, ni->ni_ncpts, net);
580 * Parse the networks string and create the matching set of NIs on the
584 lnet_parse_networks(struct list_head *netlist, char *networks,
585 bool use_tcp_bonding)
587 struct cfs_expr_list *net_el = NULL;
588 struct cfs_expr_list *ni_el = NULL;
592 struct lnet_net *net;
593 struct lnet_ni *ni = NULL;
597 if (networks == NULL) {
598 CERROR("networks string is undefined\n");
602 if (strlen(networks) > LNET_SINGLE_TEXTBUF_NOB) {
603 /* _WAY_ conservative */
604 LCONSOLE_ERROR_MSG(0x112, "Can't parse networks: string too "
609 tokensize = strlen(networks) + 1;
611 LIBCFS_ALLOC(tokens, tokensize);
612 if (tokens == NULL) {
613 CERROR("Can't allocate net tokens\n");
617 memcpy(tokens, networks, tokensize);
623 * NB we don't check interface conflicts here; it's the LNDs
624 * responsibility (if it cares at all)
633 * Parse a network string into its components.
635 * <name>{"("...")"}{"["<el>"]"}
638 /* Network name (mandatory) */
639 while (isspace(*str))
644 str += strcspn(str, SPACESTR ":()[],");
645 while (isspace(*str))
648 /* Interface list (optional) */
652 str += strcspn(str, ")");
659 } while (isspace(*str));
664 /* CPT expression (optional) */
667 str += strcspn(str, "]");
672 rc = cfs_expr_list_parse(elstr, str - elstr + 1,
673 0, LNET_CPT_NUMBER - 1,
682 } while (isspace(*str));
686 if (*str && (strchr(DELIMITERS, *str) != NULL))
689 /* go to the next net if it exits */
690 str += strcspn(str, ",");
695 * At this point the name is properly terminated.
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");
705 if (LNET_NETTYP(net_id) == LOLND) {
706 /* Loopback is implicit, and there can be only one. */
708 cfs_expr_list_free(net_el);
711 /* Should we error out instead? */
716 * All network paramaters are now known.
720 /* always allocate a net, since we will eventually add an
721 * interface to it, or we will fail, in which case we'll
723 net = lnet_net_alloc(net_id, netlist);
724 if (IS_ERR_OR_NULL(net))
728 (use_tcp_bonding && LNET_NETTYP(net_id) == SOCKLND)) {
730 * No interface list was specified, allocate a
731 * ni using the defaults.
733 ni = lnet_ni_alloc(net, net_el, NULL);
734 if (IS_ERR_OR_NULL(ni))
739 cfs_expr_list_free(net_el);
749 /* Interface name (mandatory) */
750 while (isspace(*nistr))
753 nistr += strcspn(nistr, SPACESTR "[],");
754 while (isspace(*nistr))
757 /* CPT expression (optional) */
760 nistr += strcspn(nistr, "]");
765 rc = cfs_expr_list_parse(elstr,
767 0, LNET_CPT_NUMBER - 1,
776 } while (isspace(*nistr));
782 * End of single interface specificaton,
783 * advance to the start of the next one, if
789 } while (isspace(*nistr));
800 * At this point the name is properly terminated.
807 if (use_tcp_bonding &&
808 LNET_NETTYP(net->net_id) == SOCKLND) {
809 rc = lnet_ni_add_interface(ni, name);
813 ni = lnet_ni_alloc(net, ni_el, name);
814 if (IS_ERR_OR_NULL(ni))
819 if (ni_el != net_el) {
820 cfs_expr_list_free(ni_el);
827 cfs_expr_list_free(net_el);
832 LIBCFS_FREE(tokens, tokensize);
836 lnet_syntax("networks", networks, (int)(str - tokens), strlen(str));
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);
842 list_del_init(&net->net_list);
846 if (ni_el && ni_el != net_el)
847 cfs_expr_list_free(ni_el);
849 cfs_expr_list_free(net_el);
851 LIBCFS_FREE(tokens, tokensize);
856 static struct lnet_text_buf *lnet_new_text_buf(int str_len)
858 struct lnet_text_buf *ltb;
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");
869 if (lnet_tbnob + nob > LNET_MAX_TEXTBUF_NOB) {
870 CERROR("Too many text buffers\n");
874 LIBCFS_ALLOC(ltb, nob);
879 ltb->ltb_text[0] = 0;
885 lnet_free_text_buf(struct lnet_text_buf *ltb)
887 lnet_tbnob -= ltb->ltb_size;
888 LIBCFS_FREE(ltb, ltb->ltb_size);
892 lnet_free_text_bufs(struct list_head *tbs)
894 struct lnet_text_buf *ltb;
896 while (!list_empty(tbs)) {
897 ltb = list_entry(tbs->next, struct lnet_text_buf, ltb_list);
899 list_del(<b->ltb_list);
900 lnet_free_text_buf(ltb);
905 lnet_print_text_bufs(struct list_head *tbs)
907 struct list_head *tmp;
908 struct lnet_text_buf *ltb;
910 list_for_each(tmp, tbs) {
911 ltb = list_entry(tmp, struct lnet_text_buf, ltb_list);
913 CDEBUG(D_WARNING, "%s\n", ltb->ltb_text);
916 CDEBUG(D_WARNING, "%d allocated\n", lnet_tbnob);
920 lnet_str2tbs_sep(struct list_head *tbs, char *str)
922 struct list_head pending;
926 struct lnet_text_buf *ltb;
928 INIT_LIST_HEAD(&pending);
930 /* Split 'str' into separate commands */
932 /* skip leading whitespace */
933 while (isspace(*str))
936 /* scan for separator or comment */
937 for (sep = str; *sep != 0; sep++)
938 if (lnet_issep(*sep) || *sep == '#')
941 nob = (int)(sep - str);
943 ltb = lnet_new_text_buf(nob);
945 lnet_free_text_bufs(&pending);
949 for (i = 0; i < nob; i++)
951 ltb->ltb_text[i] = ' ';
953 ltb->ltb_text[i] = str[i];
955 ltb->ltb_text[nob] = 0;
957 list_add_tail(<b->ltb_list, &pending);
961 /* scan for separator */
964 } while (*sep != 0 && !lnet_issep(*sep));
973 list_splice(&pending, tbs->prev);
978 lnet_expand1tb(struct list_head *list,
979 char *str, char *sep1, char *sep2,
980 char *item, int itemlen)
982 int len1 = (int)(sep1 - str);
983 int len2 = strlen(sep2 + 1);
984 struct lnet_text_buf *ltb;
986 LASSERT (*sep1 == '[');
987 LASSERT (*sep2 == ']');
989 ltb = lnet_new_text_buf(len1 + itemlen + len2);
993 memcpy(ltb->ltb_text, str, len1);
994 memcpy(<b->ltb_text[len1], item, itemlen);
995 memcpy(<b->ltb_text[len1+itemlen], sep2 + 1, len2);
996 ltb->ltb_text[len1 + itemlen + len2] = 0;
998 list_add_tail(<b->ltb_list, list);
1003 lnet_str2tbs_expand(struct list_head *tbs, char *str)
1006 struct list_head pending;
1018 INIT_LIST_HEAD(&pending);
1020 sep = strchr(str, '[');
1021 if (sep == NULL) /* nothing to expand */
1024 sep2 = strchr(sep, ']');
1028 for (parsed = sep; parsed < sep2; parsed = enditem) {
1031 while (enditem < sep2 && *enditem != ',')
1034 if (enditem == parsed) /* no empty items */
1037 if (sscanf(parsed, "%d-%d/%d%n", &lo, &hi, &stride, &scanned) < 3) {
1039 if (sscanf(parsed, "%d-%d%n", &lo, &hi, &scanned) < 2) {
1041 /* simple string enumeration */
1042 if (lnet_expand1tb(&pending, str, sep, sep2,
1043 parsed, (int)(enditem - parsed)) != 0)
1052 /* range expansion */
1054 if (enditem != parsed + scanned) /* no trailing junk */
1057 if (hi < 0 || lo < 0 || stride < 0 || hi < lo ||
1058 (hi - lo) % stride != 0)
1061 for (i = lo; i <= hi; i += stride) {
1063 snprintf(num, sizeof(num), "%d", i);
1065 if (nob + 1 == sizeof(num))
1068 if (lnet_expand1tb(&pending, str, sep, sep2,
1074 list_splice(&pending, tbs->prev);
1078 lnet_free_text_bufs(&pending);
1083 lnet_parse_hops (char *str, unsigned int *hops)
1085 int len = strlen(str);
1088 return (sscanf(str, "%u%n", hops, &nob) >= 1 &&
1090 *hops > 0 && *hops < 256);
1093 #define LNET_PRIORITY_SEPARATOR (':')
1096 lnet_parse_priority(char *str, unsigned int *priority, char **token)
1102 sep = strchr(str, LNET_PRIORITY_SEPARATOR);
1107 len = strlen(sep + 1);
1109 if ((sscanf((sep+1), "%u%n", priority, &nob) < 1) || (len != nob)) {
1110 /* Update the caller's token pointer so it treats the found
1111 priority as the token to report in the error message. */
1112 *token += sep - str + 1;
1116 CDEBUG(D_NET, "gateway %s, priority %d, nob %d\n", str, *priority, nob);
1119 * Change priority separator to \0 to be able to parse NID
1126 lnet_parse_route (char *str, int *im_a_router)
1128 /* static scratch buffer OK (single threaded) */
1129 static char cmd[LNET_SINGLE_TEXTBUF_NOB];
1131 struct list_head nets;
1132 struct list_head gateways;
1133 struct list_head *tmp1;
1134 struct list_head *tmp2;
1137 struct lnet_text_buf *ltb;
1145 unsigned int priority = 0;
1147 INIT_LIST_HEAD(&gateways);
1148 INIT_LIST_HEAD(&nets);
1150 /* save a copy of the string for error messages */
1151 strncpy(cmd, str, sizeof(cmd));
1152 cmd[sizeof(cmd) - 1] = '\0';
1156 /* scan for token start */
1157 while (isspace(*sep))
1160 if (ntokens < (got_hops ? 3 : 2))
1168 /* scan for token end */
1169 while (*sep != 0 && !isspace(*sep))
1175 tmp2 = &nets; /* expanding nets */
1176 } else if (ntokens == 2 &&
1177 lnet_parse_hops(token, &hops)) {
1178 got_hops = 1; /* got a hop count */
1181 tmp2 = &gateways; /* expanding gateways */
1184 ltb = lnet_new_text_buf(strlen(token));
1188 strcpy(ltb->ltb_text, token);
1189 tmp1 = <b->ltb_list;
1190 list_add_tail(tmp1, tmp2);
1192 while (tmp1 != tmp2) {
1193 ltb = list_entry(tmp1, struct lnet_text_buf, ltb_list);
1195 rc = lnet_str2tbs_expand(tmp1->next, ltb->ltb_text);
1201 if (rc > 0) { /* expanded! */
1202 list_del(<b->ltb_list);
1203 lnet_free_text_buf(ltb);
1208 net = libcfs_str2net(ltb->ltb_text);
1209 if (net == LNET_NIDNET(LNET_NID_ANY) ||
1210 LNET_NETTYP(net) == LOLND)
1213 rc = lnet_parse_priority(ltb->ltb_text,
1218 nid = libcfs_str2nid(ltb->ltb_text);
1219 if (nid == LNET_NID_ANY ||
1220 LNET_NETTYP(LNET_NIDNET(nid)) == LOLND)
1226 /* if there are no hops set then we want to flag this value as
1227 * unset since hops is an optional parameter */
1229 hops = LNET_UNDEFINED_HOPS;
1231 LASSERT(!list_empty(&nets));
1232 LASSERT(!list_empty(&gateways));
1234 list_for_each(tmp1, &nets) {
1235 ltb = list_entry(tmp1, struct lnet_text_buf, ltb_list);
1236 net = libcfs_str2net(ltb->ltb_text);
1237 LASSERT (net != LNET_NIDNET(LNET_NID_ANY));
1239 list_for_each(tmp2, &gateways) {
1240 ltb = list_entry(tmp2, struct lnet_text_buf, ltb_list);
1241 nid = libcfs_str2nid(ltb->ltb_text);
1242 LASSERT(nid != LNET_NID_ANY);
1244 if (lnet_islocalnid(nid)) {
1249 rc = lnet_add_route(net, hops, nid, priority, 1);
1250 if (rc != 0 && rc != -EEXIST && rc != -EHOSTUNREACH) {
1251 CERROR("Can't create route "
1253 libcfs_net2str(net),
1254 libcfs_nid2str(nid));
1264 lnet_syntax("routes", cmd, (int)(token - str), strlen(token));
1266 lnet_free_text_bufs(&nets);
1267 lnet_free_text_bufs(&gateways);
1272 lnet_parse_route_tbs(struct list_head *tbs, int *im_a_router)
1274 struct lnet_text_buf *ltb;
1276 while (!list_empty(tbs)) {
1277 ltb = list_entry(tbs->next, struct lnet_text_buf, ltb_list);
1279 if (lnet_parse_route(ltb->ltb_text, im_a_router) < 0) {
1280 lnet_free_text_bufs(tbs);
1284 list_del(<b->ltb_list);
1285 lnet_free_text_buf(ltb);
1292 lnet_parse_routes (char *routes, int *im_a_router)
1294 struct list_head tbs;
1299 INIT_LIST_HEAD(&tbs);
1301 if (lnet_str2tbs_sep(&tbs, routes) < 0) {
1302 CERROR("Error parsing routes\n");
1305 rc = lnet_parse_route_tbs(&tbs, im_a_router);
1308 LASSERT (lnet_tbnob == 0);
1313 lnet_match_network_token(char *token, int len, __u32 *ipaddrs, int nip)
1319 rc = cfs_ip_addr_parse(token, len, &list);
1323 for (rc = i = 0; !rc && i < nip; i++)
1324 rc = cfs_ip_addr_match(ipaddrs[i], &list);
1326 cfs_expr_list_free_list(&list);
1332 lnet_match_network_tokens(char *net_entry, __u32 *ipaddrs, int nip)
1334 static char tokens[LNET_SINGLE_TEXTBUF_NOB];
1344 LASSERT(strlen(net_entry) < sizeof(tokens));
1346 /* work on a copy of the string */
1347 strcpy(tokens, net_entry);
1350 /* scan for token start */
1351 while (isspace(*sep))
1358 /* scan for token end */
1359 while (*sep != 0 && !isspace(*sep))
1364 if (ntokens++ == 0) {
1369 len = strlen(token);
1371 rc = lnet_match_network_token(token, len, ipaddrs, nip);
1373 lnet_syntax("ip2nets", net_entry,
1374 (int)(token - tokens), len);
1378 matched |= (rc != 0);
1384 strcpy(net_entry, net); /* replace with matched net */
1389 lnet_netspec2net(char *netspec)
1391 char *bracket = strchr(netspec, '(');
1394 if (bracket != NULL)
1397 net = libcfs_str2net(netspec);
1399 if (bracket != NULL)
1406 lnet_splitnets(char *source, struct list_head *nets)
1411 struct lnet_text_buf *tb;
1412 struct lnet_text_buf *tb2;
1413 struct list_head *t;
1418 LASSERT(!list_empty(nets));
1419 LASSERT(nets->next == nets->prev); /* single entry */
1421 tb = list_entry(nets->next, struct lnet_text_buf, ltb_list);
1424 sep = strchr(tb->ltb_text, ',');
1425 bracket = strchr(tb->ltb_text, '(');
1430 /* netspec lists interfaces... */
1432 offset2 = offset + (int)(bracket - tb->ltb_text);
1433 len = strlen(bracket);
1435 bracket = strchr(bracket + 1, ')');
1437 if (bracket == NULL ||
1438 !(bracket[1] == ',' || bracket[1] == 0)) {
1439 lnet_syntax("ip2nets", source, offset2, len);
1443 sep = (bracket[1] == 0) ? NULL : bracket + 1;
1449 net = lnet_netspec2net(tb->ltb_text);
1450 if (net == LNET_NIDNET(LNET_NID_ANY)) {
1451 lnet_syntax("ip2nets", source, offset,
1452 strlen(tb->ltb_text));
1456 list_for_each(t, nets) {
1457 tb2 = list_entry(t, struct lnet_text_buf, ltb_list);
1462 if (net == lnet_netspec2net(tb2->ltb_text)) {
1463 /* duplicate network */
1464 lnet_syntax("ip2nets", source, offset,
1465 strlen(tb->ltb_text));
1473 offset += (int)(sep - tb->ltb_text);
1475 tb2 = lnet_new_text_buf(len);
1479 strncpy(tb2->ltb_text, sep, len);
1480 tb2->ltb_text[len] = '\0';
1481 list_add_tail(&tb2->ltb_list, nets);
1488 lnet_match_networks (char **networksp, char *ip2nets, __u32 *ipaddrs, int nip)
1490 static char networks[LNET_SINGLE_TEXTBUF_NOB];
1491 static char source[LNET_SINGLE_TEXTBUF_NOB];
1493 struct list_head raw_entries;
1494 struct list_head matched_nets;
1495 struct list_head current_nets;
1496 struct list_head *t;
1497 struct list_head *t2;
1498 struct lnet_text_buf *tb;
1499 struct lnet_text_buf *tb2;
1507 INIT_LIST_HEAD(&raw_entries);
1508 if (lnet_str2tbs_sep(&raw_entries, ip2nets) < 0) {
1509 CERROR("Error parsing ip2nets\n");
1510 LASSERT(lnet_tbnob == 0);
1514 INIT_LIST_HEAD(&matched_nets);
1515 INIT_LIST_HEAD(¤t_nets);
1521 while (!list_empty(&raw_entries)) {
1522 tb = list_entry(raw_entries.next, struct lnet_text_buf,
1525 strncpy(source, tb->ltb_text, sizeof(source));
1526 source[sizeof(source) - 1] = '\0';
1528 /* replace ltb_text with the network(s) add on match */
1529 rc = lnet_match_network_tokens(tb->ltb_text, ipaddrs, nip);
1533 list_del(&tb->ltb_list);
1535 if (rc == 0) { /* no match */
1536 lnet_free_text_buf(tb);
1540 /* split into separate networks */
1541 INIT_LIST_HEAD(¤t_nets);
1542 list_add(&tb->ltb_list, ¤t_nets);
1543 rc = lnet_splitnets(source, ¤t_nets);
1548 list_for_each(t, ¤t_nets) {
1549 tb = list_entry(t, struct lnet_text_buf, ltb_list);
1550 net1 = lnet_netspec2net(tb->ltb_text);
1551 LASSERT(net1 != LNET_NIDNET(LNET_NID_ANY));
1553 list_for_each(t2, &matched_nets) {
1554 tb2 = list_entry(t2, struct lnet_text_buf,
1556 net2 = lnet_netspec2net(tb2->ltb_text);
1557 LASSERT(net2 != LNET_NIDNET(LNET_NID_ANY));
1570 lnet_free_text_bufs(¤t_nets);
1574 list_for_each_safe(t, t2, ¤t_nets) {
1575 tb = list_entry(t, struct lnet_text_buf, ltb_list);
1577 list_del(&tb->ltb_list);
1578 list_add_tail(&tb->ltb_list, &matched_nets);
1580 len += snprintf(networks + len, sizeof(networks) - len,
1581 "%s%s", (len == 0) ? "" : ",",
1584 if (len >= sizeof(networks)) {
1585 CERROR("Too many matched networks\n");
1595 lnet_free_text_bufs(&raw_entries);
1596 lnet_free_text_bufs(&matched_nets);
1597 lnet_free_text_bufs(¤t_nets);
1598 LASSERT(lnet_tbnob == 0);
1603 *networksp = networks;
1607 * kernel 5.3: commit ef11db3310e272d3d8dbe8739e0770820dd20e52
1608 * added in_dev_for_each_ifa_rtnl and in_dev_for_each_ifa_rcu
1609 * and removed for_ifa and endfor_ifa.
1610 * Use the _rntl variant as the current locking is rtnl.
1612 #ifdef in_dev_for_each_ifa_rtnl
1613 #define DECLARE_CONST_IN_IFADDR(ifa) const struct in_ifaddr *ifa
1614 #define endfor_ifa(in_dev)
1616 #define DECLARE_CONST_IN_IFADDR(ifa)
1617 #define in_dev_for_each_ifa_rtnl(ifa, in_dev) for_ifa((in_dev))
1620 int lnet_inet_enumerate(struct lnet_inetdev **dev_list, struct net *ns)
1622 struct lnet_inetdev *ifaces = NULL;
1623 struct net_device *dev;
1626 DECLARE_CONST_IN_IFADDR(ifa);
1629 for_each_netdev(ns, dev) {
1630 int flags = dev_get_flags(dev);
1631 struct in_device *in_dev;
1635 if (flags & IFF_LOOPBACK) /* skip the loopback IF */
1638 if (!(flags & IFF_UP)) {
1639 CWARN("lnet: Ignoring interface %s: it's down\n",
1644 in_dev = __in_dev_get_rtnl(dev);
1646 CWARN("lnet: Interface %s has no IPv4 status.\n",
1651 node_id = dev_to_node(&dev->dev);
1652 cpt = cfs_cpt_of_node(lnet_cpt_table(), node_id);
1654 in_dev_for_each_ifa_rtnl(ifa, in_dev) {
1655 if (nip >= nalloc) {
1656 struct lnet_inetdev *tmp;
1658 nalloc += LNET_INTERFACES_NUM;
1659 tmp = krealloc(ifaces, nalloc * sizeof(*tmp),
1670 ifaces[nip].li_cpt = cpt;
1671 ifaces[nip].li_flags = flags;
1672 ifaces[nip].li_ipaddr = ntohl(ifa->ifa_local);
1673 ifaces[nip].li_netmask = ntohl(ifa->ifa_mask);
1674 strlcpy(ifaces[nip].li_name, ifa->ifa_label,
1675 sizeof(ifaces[nip].li_name));
1684 CERROR("lnet: Can't find any usable interfaces, rc = -ENOENT\n");
1691 EXPORT_SYMBOL(lnet_inet_enumerate);
1694 lnet_parse_ip2nets (char **networksp, char *ip2nets)
1696 struct lnet_inetdev *ifaces = NULL;
1697 __u32 *ipaddrs = NULL;
1702 if (current->nsproxy && current->nsproxy->net_ns)
1703 nip = lnet_inet_enumerate(&ifaces, current->nsproxy->net_ns);
1705 nip = lnet_inet_enumerate(&ifaces, &init_net);
1707 if (nip != -ENOENT) {
1708 LCONSOLE_ERROR_MSG(0x117,
1709 "Error %d enumerating local IP interfaces for ip2nets to match\n",
1712 LCONSOLE_ERROR_MSG(0x118,
1713 "No local IP interfaces for ip2nets to match\n");
1718 LIBCFS_ALLOC(ipaddrs, nip * sizeof(*ipaddrs));
1721 CERROR("lnet: Can't allocate ipaddrs[%d], rc = %d\n",
1723 goto out_free_addrs;
1726 for (i = 0; i < nip; i++)
1727 ipaddrs[i] = ifaces[i].li_ipaddr;
1729 rc = lnet_match_networks(networksp, ip2nets, ipaddrs, nip);
1731 LCONSOLE_ERROR_MSG(0x119, "Error %d parsing ip2nets\n", rc);
1732 } else if (rc == 0) {
1733 LCONSOLE_ERROR_MSG(0x11a, "ip2nets does not match "
1734 "any local IP interfaces\n");
1737 LIBCFS_FREE(ipaddrs, nip * sizeof(*ipaddrs));
1740 return rc > 0 ? 0 : rc;