1 // SPDX-License-Identifier: GPL-2.0
3 /* Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
4 * Use is subject to license terms.
6 * Copyright (c) 2012, 2017, Intel Corporation.
9 /* This file is part of Lustre, http://www.lustre.org/ */
11 #define DEBUG_SUBSYSTEM S_LNET
13 #include <linux/ctype.h>
14 #include <linux/inetdevice.h>
15 #include <linux/nsproxy.h>
16 #include <linux/ethtool.h>
17 #include <net/net_namespace.h>
18 #include <lnet/lib-lnet.h>
19 #include <net/addrconf.h>
21 /* tmp struct for parsing routes */
22 struct lnet_text_buf {
23 struct list_head ltb_list; /* stash on lists */
24 int ltb_size; /* allocated size */
25 char ltb_text[0]; /* text buffer */
28 static int lnet_tbnob = 0; /* track text buf allocation */
29 #define LNET_MAX_TEXTBUF_NOB (64<<10) /* bound allocation */
30 #define LNET_SINGLE_TEXTBUF_NOB (4<<10)
32 #define SPACESTR " \t\v\r\n"
33 #define DELIMITERS ":()[]"
36 lnet_syntax(const char *name, const char *str, int offset, int width)
38 static char dots[LNET_SINGLE_TEXTBUF_NOB];
39 static char dashes[LNET_SINGLE_TEXTBUF_NOB];
41 memset(dots, '.', sizeof(dots));
42 dots[sizeof(dots)-1] = 0;
43 memset(dashes, '-', sizeof(dashes));
44 dashes[sizeof(dashes)-1] = 0;
46 LCONSOLE_ERROR_MSG(0x10f, "Error parsing '%s=\"%s\"'\n", name, str);
47 LCONSOLE_ERROR_MSG(0x110, "here...........%.*s..%.*s|%.*s|\n",
48 (int)strlen(name), dots, offset, dots,
49 (width < 1) ? 0 : width - 1, dashes);
66 lnet_net_unique(__u32 net_id, struct list_head *netlist,
67 struct lnet_net **net)
69 struct lnet_net *net_l;
74 list_for_each_entry(net_l, netlist, net_list) {
75 if (net_l->net_id == net_id) {
85 /* check that the NI is unique within the list of NIs already added to
88 lnet_ni_unique_net(struct list_head *nilist, char *iface)
90 struct list_head *tmp;
93 list_for_each(tmp, nilist) {
94 ni = list_entry(tmp, struct lnet_ni, ni_netlist);
96 if (ni->ni_interface != NULL &&
97 strncmp(ni->ni_interface,
99 LNET_MAX_STR_LEN) == 0)
106 in_array(__u32 *array, __u32 size, __u32 value)
110 for (i = 0; i < size; i++) {
111 if (array[i] == value)
119 lnet_net_append_cpts(__u32 *cpts, __u32 ncpts, struct lnet_net *net)
121 __u32 *added_cpts = NULL;
122 int i, j = 0, rc = 0;
125 * no need to go futher since a subset of the NIs already exist on
128 if (net->net_ncpts == LNET_CPT_NUMBER)
132 /* there is an NI which will exist on all CPTs */
133 if (net->net_cpts != NULL)
134 CFS_FREE_PTR_ARRAY(net->net_cpts, net->net_ncpts);
135 net->net_cpts = NULL;
136 net->net_ncpts = LNET_CPT_NUMBER;
140 if (net->net_cpts == NULL) {
141 CFS_ALLOC_PTR_ARRAY(net->net_cpts, ncpts);
142 if (net->net_cpts == NULL)
144 memcpy(net->net_cpts, cpts, ncpts * sizeof(*net->net_cpts));
145 net->net_ncpts = ncpts;
149 CFS_ALLOC_PTR_ARRAY(added_cpts, LNET_CPT_NUMBER);
150 if (added_cpts == NULL)
153 for (i = 0; i < ncpts; i++) {
154 if (!in_array(net->net_cpts, net->net_ncpts, cpts[i])) {
155 added_cpts[j] = cpts[i];
160 /* append the new cpts if any to the list of cpts in the net */
162 __u32 *array = NULL, *loc;
163 __u32 total_entries = j + net->net_ncpts;
165 CFS_ALLOC_PTR_ARRAY(array, total_entries);
171 memcpy(array, net->net_cpts,
172 net->net_ncpts * sizeof(*net->net_cpts));
173 loc = array + net->net_ncpts;
174 memcpy(loc, added_cpts, j * sizeof(*net->net_cpts));
176 CFS_FREE_PTR_ARRAY(net->net_cpts, net->net_ncpts);
177 net->net_ncpts = total_entries;
178 net->net_cpts = array;
182 CFS_FREE_PTR_ARRAY(added_cpts, LNET_CPT_NUMBER);
188 lnet_net_remove_cpts(__u32 *cpts, __u32 ncpts, struct lnet_net *net)
194 * Operation Assumption:
195 * This function is called after an NI has been removed from
198 * if we're removing an NI which exists on all CPTs then
199 * we have to check if any of the other NIs on this net also
200 * exists on all CPTs. If none, then we need to build our Net CPT
201 * list based on the remaining NIs.
203 * If the NI being removed exist on a subset of the CPTs then we
204 * alo rebuild the Net CPT list based on the remaining NIs, which
205 * should resutl in the expected Net CPT list.
209 * sometimes this function can be called due to some failure
210 * creating an NI, before any of the cpts are allocated, so check
211 * for that case and don't do anything
216 if (ncpts == LNET_CPT_NUMBER) {
218 * first iteration through the NI list in the net to see
219 * if any of the NIs exist on all the CPTs. If one is
220 * found then our job is done.
222 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
223 if (ni->ni_ncpts == LNET_CPT_NUMBER)
229 * Rebuild the Net CPT list again, thereby only including only the
230 * CPTs which the remaining NIs are associated with.
232 if (net->net_cpts != NULL) {
233 CFS_FREE_PTR_ARRAY(net->net_cpts, net->net_ncpts);
234 net->net_cpts = NULL;
237 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
238 rc = lnet_net_append_cpts(ni->ni_cpts, ni->ni_ncpts,
241 CERROR("Out of Memory\n");
243 * do our best to keep on going. Delete
244 * the net cpts and set it to NULL. This
245 * way we can keep on going but less
246 * efficiently, since memory accesses might be
249 if (net->net_cpts != NULL) {
250 CFS_FREE_PTR_ARRAY(net->net_cpts,
252 net->net_cpts = NULL;
253 net->net_ncpts = LNET_CPT_NUMBER;
261 lnet_ni_free(struct lnet_ni *ni)
263 lnet_net_remove_cpts(ni->ni_cpts, ni->ni_ncpts, ni->ni_net);
265 if (ni->ni_refs != NULL)
266 cfs_percpt_free(ni->ni_refs);
268 if (ni->ni_tx_queues != NULL)
269 cfs_percpt_free(ni->ni_tx_queues);
271 if (ni->ni_cpts != NULL)
272 cfs_expr_list_values_free(ni->ni_cpts, ni->ni_ncpts);
274 if (ni->ni_interface != NULL) {
275 LIBCFS_FREE(ni->ni_interface,
276 strlen(ni->ni_interface) + 1);
279 /* release reference to net namespace */
280 if (ni->ni_net_ns != NULL)
281 put_net(ni->ni_net_ns);
283 LIBCFS_FREE(ni, sizeof(*ni));
287 lnet_net_free(struct lnet_net *net)
289 struct list_head *tmp, *tmp2;
292 LASSERT(list_empty(&net->net_ni_zombie));
295 * delete any nis that haven't been added yet. This could happen
296 * if there is a failure on net startup
298 list_for_each_safe(tmp, tmp2, &net->net_ni_added) {
299 ni = list_entry(tmp, struct lnet_ni, ni_netlist);
300 list_del_init(&ni->ni_netlist);
304 /* delete any nis which have been started. */
305 list_for_each_safe(tmp, tmp2, &net->net_ni_list) {
306 ni = list_entry(tmp, struct lnet_ni, ni_netlist);
307 list_del_init(&ni->ni_netlist);
311 if (net->net_cpts != NULL) {
312 CFS_FREE_PTR_ARRAY(net->net_cpts, net->net_ncpts);
313 net->net_ncpts = LNET_CPT_NUMBER;
314 net->net_cpts = NULL;
317 LIBCFS_FREE(net, sizeof(*net));
321 lnet_net_alloc(__u32 net_id, struct list_head *net_list)
323 struct lnet_net *net;
325 if (!lnet_net_unique(net_id, net_list, &net)) {
326 CDEBUG(D_NET, "Returning duplicate net %p %s\n", net,
327 libcfs_net2str(net->net_id));
331 LIBCFS_ALLOC(net, sizeof(*net));
333 CERROR("Out of memory creating network %s\n",
334 libcfs_net2str(net_id));
338 INIT_LIST_HEAD(&net->net_list);
339 INIT_LIST_HEAD(&net->net_ni_list);
340 INIT_LIST_HEAD(&net->net_ni_added);
341 INIT_LIST_HEAD(&net->net_ni_zombie);
342 INIT_LIST_HEAD(&net->net_rtr_pref_nids);
343 spin_lock_init(&net->net_lock);
345 net->net_id = net_id;
346 net->net_last_alive = ktime_get_seconds();
348 net->net_sel_priority = LNET_MAX_SELECTION_PRIORITY;
350 /* initialize global paramters to undefiend */
351 net->net_tunables.lct_peer_timeout = -1;
352 net->net_tunables.lct_max_tx_credits = -1;
353 net->net_tunables.lct_peer_tx_credits = -1;
354 net->net_tunables.lct_peer_rtr_credits = -1;
357 list_add_tail(&net->net_list, net_list);
362 int lnet_ni_add_interface(struct lnet_ni *ni, char *iface)
364 size_t iface_len = strlen(iface) + 1;
369 if (ni->ni_interface != NULL) {
370 LCONSOLE_ERROR_MSG(0x115, "%s: interface %s already set for net %s: rc = %d\n",
371 iface, ni->ni_interface,
372 libcfs_net2str(LNET_NID_NET(&ni->ni_nid)),
377 /* Allocate memory for the interface, so the code parsing input into
378 * tokens and adding interfaces can free the input safely.
379 * ni->ni_interface is freed in lnet_ni_free().
381 LIBCFS_ALLOC(ni->ni_interface, iface_len);
383 if (ni->ni_interface == NULL) {
384 CERROR("%s: cannot allocate net interface name: rc = %d\n",
389 strscpy(ni->ni_interface, iface, iface_len);
393 EXPORT_SYMBOL(lnet_ni_add_interface);
395 static struct lnet_ni *
396 lnet_ni_alloc_common(struct lnet_net *net, struct lnet_nid *nid, char *iface)
398 struct lnet_tx_queue *tq;
403 /* make sure that this NI is unique in the net it's
405 if (!lnet_ni_unique_net(&net->net_ni_added, iface))
408 LIBCFS_ALLOC(ni, sizeof(*ni));
410 CERROR("Out of memory creating network interface %s%s\n",
411 libcfs_net2str(net->net_id),
412 (iface != NULL) ? iface : "");
416 spin_lock_init(&ni->ni_lock);
417 INIT_LIST_HEAD(&ni->ni_netlist);
418 INIT_LIST_HEAD(&ni->ni_recovery);
419 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
420 ni->ni_refs = cfs_percpt_alloc(lnet_cpt_table(),
421 sizeof(*ni->ni_refs[0]));
422 if (ni->ni_refs == NULL)
425 ni->ni_tx_queues = cfs_percpt_alloc(lnet_cpt_table(),
426 sizeof(*ni->ni_tx_queues[0]));
427 if (ni->ni_tx_queues == NULL)
430 cfs_percpt_for_each(tq, i, ni->ni_tx_queues)
431 INIT_LIST_HEAD(&tq->tq_delayed);
434 if (nid_same(nid, &LNET_ANY_NID)) {
435 /* LND will fill in the address part of the NID */
436 ni->ni_nid.nid_type = LNET_NETTYP(net->net_id);
437 ni->ni_nid.nid_num = cpu_to_be16(LNET_NETNUM(net->net_id));
442 /* Store net namespace in which current ni is being created */
443 if (current->nsproxy && current->nsproxy->net_ns)
444 ni->ni_net_ns = get_net(current->nsproxy->net_ns);
446 ni->ni_net_ns = get_net(&init_net);
448 ni->ni_state = LNET_NI_STATE_INIT;
449 ni->ni_sel_priority = LNET_MAX_SELECTION_PRIORITY;
450 list_add_tail(&ni->ni_netlist, &net->net_ni_added);
453 * if an interface name is provided then make sure to add in that
454 * interface name in NI
457 if (lnet_ni_add_interface(ni, iface) != 0)
466 /* allocate and add to the provided network */
468 lnet_ni_alloc(struct lnet_net *net, struct cfs_expr_list *el, char *iface)
473 ni = lnet_ni_alloc_common(net, &LNET_ANY_NID, iface);
479 ni->ni_ncpts = LNET_CPT_NUMBER;
481 rc = cfs_expr_list_values(el, LNET_CPT_NUMBER, &ni->ni_cpts);
483 CERROR("Failed to set CPTs for NI %s(%s): %d\n",
484 libcfs_net2str(net->net_id),
485 (iface != NULL) ? iface : "", rc);
489 LASSERT(rc <= LNET_CPT_NUMBER);
490 if (rc == LNET_CPT_NUMBER) {
491 CFS_FREE_PTR_ARRAY(ni->ni_cpts, rc);
498 rc = lnet_net_append_cpts(ni->ni_cpts, ni->ni_ncpts, net);
509 lnet_ni_alloc_w_cpt_array(struct lnet_net *net, struct lnet_nid *nid,
510 u32 *cpts, u32 ncpts, char *iface)
515 ni = lnet_ni_alloc_common(net, nid, iface);
521 ni->ni_ncpts = LNET_CPT_NUMBER;
523 size_t array_size = ncpts * sizeof(ni->ni_cpts[0]);
525 CFS_ALLOC_PTR_ARRAY(ni->ni_cpts, ncpts);
526 if (ni->ni_cpts == NULL)
528 memcpy(ni->ni_cpts, cpts, array_size);
529 ni->ni_ncpts = ncpts;
532 rc = lnet_net_append_cpts(ni->ni_cpts, ni->ni_ncpts, net);
543 * Parse the networks string and create the matching set of NIs on the
547 lnet_parse_networks(struct list_head *netlist, const char *networks)
549 struct cfs_expr_list *net_el = NULL;
550 struct cfs_expr_list *ni_el = NULL;
554 struct lnet_net *net;
555 struct lnet_ni *ni = NULL;
559 if (networks == NULL) {
560 CERROR("networks string is undefined\n");
564 if (strlen(networks) > LNET_SINGLE_TEXTBUF_NOB) {
565 /* _WAY_ conservative */
566 LCONSOLE_ERROR_MSG(0x112, "Can't parse networks: string too "
571 tokensize = strlen(networks) + 1;
573 LIBCFS_ALLOC(tokens, tokensize);
574 if (tokens == NULL) {
575 CERROR("Can't allocate net tokens\n");
579 memcpy(tokens, networks, tokensize);
585 * NB we don't check interface conflicts here; it's the LNDs
586 * responsibility (if it cares at all)
595 * Parse a network string into its components.
597 * <name>{"("...")"}{"["<el>"]"}
600 /* Network name (mandatory) */
601 while (isspace(*str))
606 str += strcspn(str, SPACESTR ":()[],");
607 while (isspace(*str))
610 /* Interface list (optional) */
614 str += strcspn(str, ")");
621 } while (isspace(*str));
626 /* CPT expression (optional) */
629 str += strcspn(str, "]");
634 rc = cfs_expr_list_parse(elstr, str - elstr + 1,
635 0, LNET_CPT_NUMBER - 1,
644 } while (isspace(*str));
648 if (*str && (strchr(DELIMITERS, *str) != NULL))
651 /* go to the next net if it exits */
652 str += strcspn(str, ",");
657 * At this point the name is properly terminated.
659 net_id = libcfs_str2net(name);
660 if (net_id == LNET_NET_ANY) {
661 LCONSOLE_ERROR_MSG(0x113,
662 "Unrecognised network type\n");
667 if (LNET_NETTYP(net_id) == LOLND) {
668 /* Loopback is implicit, and there can be only one. */
670 cfs_expr_list_free(net_el);
673 /* Should we error out instead? */
678 * All network paramaters are now known.
682 /* always allocate a net, since we will eventually add an
683 * interface to it, or we will fail, in which case we'll
685 net = lnet_net_alloc(net_id, netlist);
686 if (IS_ERR_OR_NULL(net))
691 * No interface list was specified, allocate a
692 * ni using the defaults.
694 ni = lnet_ni_alloc(net, net_el, NULL);
695 if (IS_ERR_OR_NULL(ni))
700 cfs_expr_list_free(net_el);
710 /* Interface name (mandatory) */
711 while (isspace(*nistr))
714 nistr += strcspn(nistr, SPACESTR "[],");
715 while (isspace(*nistr))
718 /* CPT expression (optional) */
721 nistr += strcspn(nistr, "]");
726 rc = cfs_expr_list_parse(elstr,
728 0, LNET_CPT_NUMBER - 1,
737 } while (isspace(*nistr));
743 * End of single interface specificaton,
744 * advance to the start of the next one, if
750 } while (isspace(*nistr));
761 * At this point the name is properly terminated.
768 ni = lnet_ni_alloc(net, ni_el, name);
769 if (IS_ERR_OR_NULL(ni))
773 if (ni_el != net_el) {
774 cfs_expr_list_free(ni_el);
781 cfs_expr_list_free(net_el);
786 LIBCFS_FREE(tokens, tokensize);
790 lnet_syntax("networks", networks, (int)(str - tokens), strlen(str));
792 /* free the net list and all the nis on each net */
793 while ((net = list_first_entry_or_null(netlist,
795 net_list)) != NULL) {
796 list_del_init(&net->net_list);
800 if (ni_el && ni_el != net_el)
801 cfs_expr_list_free(ni_el);
803 cfs_expr_list_free(net_el);
805 LIBCFS_FREE(tokens, tokensize);
810 static struct lnet_text_buf *lnet_new_text_buf(int str_len)
812 struct lnet_text_buf *ltb;
815 /* NB allocate space for the terminating 0 */
816 nob = offsetof(struct lnet_text_buf, ltb_text[str_len + 1]);
817 if (nob > LNET_SINGLE_TEXTBUF_NOB) {
818 /* _way_ conservative for "route net gateway..." */
819 CERROR("text buffer too big\n");
823 if (lnet_tbnob + nob > LNET_MAX_TEXTBUF_NOB) {
824 CERROR("Too many text buffers\n");
828 LIBCFS_ALLOC(ltb, nob);
833 ltb->ltb_text[0] = 0;
839 lnet_free_text_buf(struct lnet_text_buf *ltb)
841 lnet_tbnob -= ltb->ltb_size;
842 LIBCFS_FREE(ltb, ltb->ltb_size);
846 lnet_free_text_bufs(struct list_head *tbs)
848 struct lnet_text_buf *ltb;
850 while ((ltb = list_first_entry_or_null(tbs, struct lnet_text_buf,
851 ltb_list)) != NULL) {
852 list_del(<b->ltb_list);
853 lnet_free_text_buf(ltb);
858 lnet_str2tbs_sep(struct list_head *tbs, const char *str)
864 struct lnet_text_buf *ltb;
866 /* Split 'str' into separate commands */
868 /* skip leading whitespace */
869 while (isspace(*str))
872 /* scan for separator or comment */
873 for (sep = str; *sep != 0; sep++)
874 if (lnet_issep(*sep) || *sep == '#')
877 nob = (int)(sep - str);
879 ltb = lnet_new_text_buf(nob);
881 lnet_free_text_bufs(&pending);
885 for (i = 0; i < nob; i++)
887 ltb->ltb_text[i] = ' ';
889 ltb->ltb_text[i] = str[i];
891 ltb->ltb_text[nob] = 0;
893 list_add_tail(<b->ltb_list, &pending);
897 /* scan for separator */
900 } while (*sep != 0 && !lnet_issep(*sep));
909 list_splice(&pending, tbs->prev);
914 lnet_expand1tb(struct list_head *list,
915 char *str, char *sep1, char *sep2,
916 char *item, int itemlen)
918 int len1 = (int)(sep1 - str);
919 int len2 = strlen(sep2 + 1);
920 struct lnet_text_buf *ltb;
922 LASSERT (*sep1 == '[');
923 LASSERT (*sep2 == ']');
925 ltb = lnet_new_text_buf(len1 + itemlen + len2);
929 memcpy(ltb->ltb_text, str, len1);
930 memcpy(<b->ltb_text[len1], item, itemlen);
931 memcpy(<b->ltb_text[len1+itemlen], sep2 + 1, len2);
932 ltb->ltb_text[len1 + itemlen + len2] = 0;
934 list_add_tail(<b->ltb_list, list);
939 lnet_str2tbs_expand(struct list_head *tbs, char *str)
954 sep = strchr(str, '[');
955 if (sep == NULL) /* nothing to expand */
958 sep2 = strchr(sep, ']');
962 for (parsed = sep; parsed < sep2; parsed = enditem) {
965 while (enditem < sep2 && *enditem != ',')
968 if (enditem == parsed) /* no empty items */
971 if (sscanf(parsed, "%d-%d/%d%n", &lo, &hi, &stride, &scanned) < 3) {
973 if (sscanf(parsed, "%d-%d%n", &lo, &hi, &scanned) < 2) {
975 /* simple string enumeration */
976 if (lnet_expand1tb(&pending, str, sep, sep2,
977 parsed, (int)(enditem - parsed)) != 0)
986 /* range expansion */
988 if (enditem != parsed + scanned) /* no trailing junk */
991 if (hi < 0 || lo < 0 || stride < 0 || hi < lo ||
992 (hi - lo) % stride != 0)
995 for (i = lo; i <= hi; i += stride) {
997 snprintf(num, sizeof(num), "%d", i);
999 if (nob + 1 == sizeof(num))
1002 if (lnet_expand1tb(&pending, str, sep, sep2,
1008 list_splice(&pending, tbs->prev);
1012 lnet_free_text_bufs(&pending);
1017 lnet_parse_hops (char *str, unsigned int *hops)
1019 int len = strlen(str);
1022 return (sscanf(str, "%u%n", hops, &nob) >= 1 &&
1024 *hops > 0 && *hops < 256);
1027 #define LNET_PRIORITY_SEPARATOR (':')
1030 lnet_parse_priority(char *str, unsigned int *priority, char **token)
1036 sep = strchr(str, LNET_PRIORITY_SEPARATOR);
1041 len = strlen(sep + 1);
1043 if ((sscanf((sep+1), "%u%n", priority, &nob) < 1) || (len != nob)) {
1044 /* Update the caller's token pointer so it treats the found
1045 priority as the token to report in the error message. */
1046 *token += sep - str + 1;
1050 CDEBUG(D_NET, "gateway %s, priority %d, nob %d\n", str, *priority, nob);
1053 * Change priority separator to \0 to be able to parse NID
1060 lnet_parse_route(char *str, int *im_a_router)
1062 /* static scratch buffer OK (single threaded) */
1063 static char cmd[LNET_SINGLE_TEXTBUF_NOB];
1066 LIST_HEAD(gateways);
1067 struct list_head *tmp1;
1068 struct list_head *tmp2;
1070 struct lnet_nid nid;
1071 struct lnet_text_buf *ltb = NULL;
1072 struct lnet_text_buf *ltb1, *ltb2;
1080 unsigned int priority = 0;
1082 /* save a copy of the string for error messages */
1083 strncpy(cmd, str, sizeof(cmd));
1084 cmd[sizeof(cmd) - 1] = '\0';
1088 /* scan for token start */
1089 while (isspace(*sep))
1092 if (ntokens < (got_hops ? 3 : 2))
1100 /* scan for token end */
1101 while (*sep != 0 && !isspace(*sep))
1107 tmp2 = &nets; /* expanding nets */
1108 } else if (ntokens == 2 &&
1109 lnet_parse_hops(token, &hops)) {
1110 got_hops = 1; /* got a hop count */
1113 tmp2 = &gateways; /* expanding gateways */
1116 ltb = lnet_new_text_buf(strlen(token));
1120 strcpy(ltb->ltb_text, token);
1121 tmp1 = <b->ltb_list;
1122 list_add_tail(tmp1, tmp2);
1124 while (tmp1 != tmp2) {
1125 ltb = list_entry(tmp1, struct lnet_text_buf, ltb_list);
1127 rc = lnet_str2tbs_expand(tmp1->next, ltb->ltb_text);
1133 if (rc > 0) { /* expanded! */
1134 list_del(<b->ltb_list);
1135 lnet_free_text_buf(ltb);
1140 net = libcfs_str2net(ltb->ltb_text);
1141 if (net == LNET_NET_ANY ||
1142 LNET_NETTYP(net) == LOLND)
1145 rc = lnet_parse_priority(ltb->ltb_text,
1150 if (libcfs_strnid(&nid, ltb->ltb_text) != 0 ||
1157 /* if there are no hops set then we want to flag this value as
1158 * unset since hops is an optional parameter */
1160 hops = LNET_UNDEFINED_HOPS;
1162 LASSERT(!list_empty(&nets));
1163 LASSERT(!list_empty(&gateways));
1165 list_for_each_entry(ltb1, &nets, ltb_list) {
1166 net = libcfs_str2net(ltb1->ltb_text);
1167 LASSERT(net != LNET_NET_ANY);
1169 list_for_each_entry(ltb2, &gateways, ltb_list) {
1170 LASSERT(libcfs_strnid(&nid, ltb2->ltb_text) == 0);
1172 if (lnet_islocalnid(&nid)) {
1177 rc = lnet_add_route(net, hops, &nid, priority, 1);
1178 if (rc != 0 && rc != -EEXIST && rc != -EHOSTUNREACH) {
1179 CERROR("Can't create route "
1181 libcfs_net2str(net),
1182 libcfs_nidstr(&nid));
1192 lnet_syntax("routes", cmd, (int)(token - str), strlen(token));
1194 lnet_free_text_bufs(&nets);
1195 lnet_free_text_bufs(&gateways);
1200 lnet_parse_route_tbs(struct list_head *tbs, int *im_a_router)
1202 struct lnet_text_buf *ltb;
1204 while ((ltb = list_first_entry_or_null(tbs, struct lnet_text_buf,
1205 ltb_list)) != NULL) {
1206 if (lnet_parse_route(ltb->ltb_text, im_a_router) < 0) {
1207 lnet_free_text_bufs(tbs);
1211 list_del(<b->ltb_list);
1212 lnet_free_text_buf(ltb);
1219 lnet_parse_routes(const char *routes, int *im_a_router)
1226 if (lnet_str2tbs_sep(&tbs, routes) < 0) {
1227 CERROR("Error parsing routes\n");
1230 rc = lnet_parse_route_tbs(&tbs, im_a_router);
1233 LASSERT (lnet_tbnob == 0);
1238 lnet_match_network_token(char *token, __u32 *ipaddrs, int nip)
1244 rc = cfs_ip_addr_parse(token, 0, &list);
1248 for (rc = i = 0; !rc && i < nip; i++)
1249 rc = cfs_ip_addr_match(ipaddrs[i], &list);
1251 cfs_expr_list_free_list(&list);
1257 lnet_match_network_tokens(char *net_entry, __u32 *ipaddrs, int nip)
1259 static char tokens[LNET_SINGLE_TEXTBUF_NOB];
1268 LASSERT(strlen(net_entry) < sizeof(tokens));
1270 /* work on a copy of the string */
1271 strcpy(tokens, net_entry);
1274 /* scan for token start */
1275 sep = skip_spaces(sep);
1279 token = strsep(&sep, " \t\n\r\v\f");
1281 if (ntokens++ == 0) {
1286 rc = lnet_match_network_token(token, ipaddrs, nip);
1288 lnet_syntax("ip2nets", net_entry,
1289 (int)(token - tokens), strlen(token));
1293 matched |= (rc != 0);
1299 strcpy(net_entry, net); /* replace with matched net */
1304 lnet_netspec2net(char *netspec)
1306 char *bracket = strchr(netspec, '(');
1309 if (bracket != NULL)
1312 net = libcfs_str2net(netspec);
1314 if (bracket != NULL)
1321 lnet_splitnets(char *source, struct list_head *nets)
1326 struct lnet_text_buf *tb;
1327 struct lnet_text_buf *tb2;
1332 LASSERT(!list_empty(nets));
1333 LASSERT(nets->next == nets->prev); /* single entry */
1335 tb = list_first_entry(nets, struct lnet_text_buf, ltb_list);
1338 sep = strchr(tb->ltb_text, ',');
1339 bracket = strchr(tb->ltb_text, '(');
1344 /* netspec lists interfaces... */
1346 offset2 = offset + (int)(bracket - tb->ltb_text);
1347 len = strlen(bracket);
1349 bracket = strchr(bracket + 1, ')');
1351 if (bracket == NULL ||
1352 !(bracket[1] == ',' || bracket[1] == 0)) {
1353 lnet_syntax("ip2nets", source, offset2, len);
1357 sep = (bracket[1] == 0) ? NULL : bracket + 1;
1363 net = lnet_netspec2net(tb->ltb_text);
1364 if (net == LNET_NET_ANY) {
1365 lnet_syntax("ip2nets", source, offset,
1366 strlen(tb->ltb_text));
1370 list_for_each_entry(tb2, nets, ltb_list) {
1374 if (net == lnet_netspec2net(tb2->ltb_text)) {
1375 /* duplicate network */
1376 lnet_syntax("ip2nets", source, offset,
1377 strlen(tb->ltb_text));
1385 offset += (int)(sep - tb->ltb_text);
1387 tb2 = lnet_new_text_buf(len);
1391 strncpy(tb2->ltb_text, sep, len);
1392 tb2->ltb_text[len] = '\0';
1393 list_add_tail(&tb2->ltb_list, nets);
1400 lnet_match_networks(const char **networksp, const char *ip2nets,
1401 __u32 *ipaddrs, int nip)
1403 static char networks[LNET_SINGLE_TEXTBUF_NOB];
1404 static char source[LNET_SINGLE_TEXTBUF_NOB];
1406 LIST_HEAD(raw_entries);
1407 LIST_HEAD(matched_nets);
1408 LIST_HEAD(current_nets);
1409 struct list_head *t;
1410 struct list_head *t2;
1411 struct lnet_text_buf *tb;
1416 if (lnet_str2tbs_sep(&raw_entries, ip2nets) < 0) {
1417 CERROR("Error parsing ip2nets\n");
1418 LASSERT(lnet_tbnob == 0);
1427 while ((tb = list_first_entry_or_null(&raw_entries,
1428 struct lnet_text_buf,
1429 ltb_list)) != NULL) {
1430 strncpy(source, tb->ltb_text, sizeof(source));
1431 source[sizeof(source) - 1] = '\0';
1433 /* replace ltb_text with the network(s) add on match */
1434 rc = lnet_match_network_tokens(tb->ltb_text, ipaddrs, nip);
1438 list_del(&tb->ltb_list);
1440 if (rc == 0) { /* no match */
1441 lnet_free_text_buf(tb);
1445 /* split into separate networks */
1446 INIT_LIST_HEAD(¤t_nets);
1447 list_add(&tb->ltb_list, ¤t_nets);
1448 rc = lnet_splitnets(source, ¤t_nets);
1452 list_for_each_safe(t, t2, ¤t_nets) {
1453 tb = list_entry(t, struct lnet_text_buf, ltb_list);
1455 list_move_tail(&tb->ltb_list, &matched_nets);
1457 len += scnprintf(networks + len, sizeof(networks) - len,
1458 "%s%s", (len == 0) ? "" : ",",
1461 if (len >= sizeof(networks)) {
1462 CERROR("Too many matched networks\n");
1472 lnet_free_text_bufs(&raw_entries);
1473 lnet_free_text_bufs(&matched_nets);
1474 lnet_free_text_bufs(¤t_nets);
1475 LASSERT(lnet_tbnob == 0);
1480 *networksp = networks;
1484 __u32 lnet_set_link_fatal_state(struct lnet_ni *ni, unsigned int link_state)
1486 CDEBUG(D_NET, "%s: set link fatal state to %u\n",
1487 libcfs_nidstr(&ni->ni_nid), link_state);
1488 return atomic_xchg(&ni->ni_fatal_error_on, link_state);
1490 EXPORT_SYMBOL(lnet_set_link_fatal_state);
1492 int lnet_get_link_status(struct net_device *dev)
1499 if (!netif_running(dev)) {
1501 CDEBUG(D_NET, "device idx %d not running\n", dev->ifindex);
1503 /* Some devices may not be providing link settings */
1504 else if (dev->ethtool_ops->get_link) {
1505 ret = dev->ethtool_ops->get_link(dev);
1506 CDEBUG(D_NET, "device idx %d get_link %u\n",
1513 EXPORT_SYMBOL(lnet_get_link_status);
1515 int lnet_inet_enumerate(struct lnet_inetdev **dev_list, struct net *ns, bool v6)
1517 struct lnet_inetdev *ifaces = NULL;
1518 struct net_device *dev;
1521 DECLARE_CONST_IN_IFADDR(ifa);
1524 for_each_netdev(ns, dev) {
1525 int flags = dev_get_flags(dev);
1526 struct in_device *in_dev;
1527 #if IS_ENABLED(CONFIG_IPV6)
1528 struct inet6_dev *in6_dev;
1529 const struct inet6_ifaddr *ifa6;
1534 if (flags & IFF_LOOPBACK) /* skip the loopback IF */
1537 if (!(flags & IFF_UP)) {
1538 CWARN("lnet: Ignoring interface %s: it's down\n",
1543 node_id = dev_to_node(&dev->dev);
1544 cpt = cfs_cpt_of_node(lnet_cpt_table(), node_id);
1546 in_dev = __in_dev_get_rtnl(dev);
1549 CWARN("lnet: Interface %s has no IPv4 status.\n",
1554 in_dev_for_each_ifa_rtnl(ifa, in_dev) {
1555 if (nip >= nalloc) {
1556 struct lnet_inetdev *tmp;
1558 nalloc += LNET_INTERFACES_NUM;
1559 tmp = krealloc(ifaces, nalloc * sizeof(*tmp),
1570 ifaces[nip].li_cpt = cpt;
1571 ifaces[nip].li_iff_master = !!(flags & IFF_MASTER);
1572 ifaces[nip].li_size = sizeof(ifa->ifa_local);
1573 ifaces[nip].li_index = dev->ifindex;
1574 ifaces[nip].li_ipaddr = ifa->ifa_local;
1575 ifaces[nip].li_netmask = ntohl(ifa->ifa_mask);
1576 strscpy(ifaces[nip].li_name, ifa->ifa_label,
1577 sizeof(ifaces[nip].li_name));
1585 #if IS_ENABLED(CONFIG_IPV6)
1586 in6_dev = __in6_dev_get(dev);
1589 CWARN("lnet: Interface %s has no IP status.\n",
1594 list_for_each_entry_rcu(ifa6, &in6_dev->addr_list, if_list) {
1595 if (ifa6->flags & IFA_F_TEMPORARY)
1597 if (nip >= nalloc) {
1598 struct lnet_inetdev *tmp;
1600 nalloc += LNET_INTERFACES_NUM;
1601 tmp = krealloc(ifaces, nalloc * sizeof(*tmp),
1612 ifaces[nip].li_cpt = cpt;
1613 ifaces[nip].li_iff_master = !!(flags & IFF_MASTER);
1614 ifaces[nip].li_size = sizeof(struct in6_addr);
1615 ifaces[nip].li_index = dev->ifindex;
1616 memcpy(ifaces[nip].li_ipv6addr,
1617 &ifa6->addr, sizeof(struct in6_addr));
1618 strscpy(ifaces[nip].li_name, dev->name,
1619 sizeof(ifaces[nip].li_name));
1621 /* As different IPv6 addresses don't have unique
1622 * labels, it is safest just to use the first
1623 * and ignore the rest.
1627 #endif /* IS_ENABLED(CONFIG_IPV6) */
1634 CERROR("lnet: Can't find any usable interfaces, rc = -ENOENT\n");
1641 EXPORT_SYMBOL(lnet_inet_enumerate);
1643 int lnet_inet_select(struct lnet_ni *ni,
1644 struct lnet_inetdev *ifaces,
1647 bool addr_set = nid_addr_is_set(&ni->ni_nid);
1650 /* default to first interface if both interface and NID unspecified */
1651 if (!ni->ni_interface && !addr_set)
1654 for (if_idx = 0; if_idx < num_ifaces; if_idx++) {
1655 if (ni->ni_interface && strlen(ni->ni_interface) &&
1656 strcmp(ni->ni_interface, ifaces[if_idx].li_name) != 0)
1657 /* not the specified interface */
1661 /* IP unspecified, use IP of first matching interface */
1664 if (ifaces[if_idx].li_size == NID_ADDR_BYTES(&ni->ni_nid)) {
1665 char *addr = (char *)&ifaces[if_idx].li_ipaddr;
1667 if (ifaces[if_idx].li_size != 4)
1668 addr = (char *)ifaces[if_idx].li_ipv6addr;
1670 if (memcmp(ni->ni_nid.nid_addr, addr,
1671 ifaces[if_idx].li_size) == 0)
1676 if (if_idx < num_ifaces)
1680 CERROR("%s: failed to find IP address %s\n",
1681 libcfs_lnd2modname(ni->ni_nid.nid_type),
1682 libcfs_nidstr(&ni->ni_nid));
1683 else if (ni->ni_interface)
1684 CERROR("%s: failed to find interface %s%s%s\n",
1685 libcfs_lnd2modname(ni->ni_nid.nid_type),
1686 ni->ni_interface, addr_set ? "@" : "",
1687 addr_set ? libcfs_nidstr(&ni->ni_nid) : "");
1691 EXPORT_SYMBOL(lnet_inet_select);
1694 lnet_parse_ip2nets(const char **networksp, const 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,
1706 nip = lnet_inet_enumerate(&ifaces, &init_net, false);
1708 if (nip != -ENOENT) {
1709 LCONSOLE_ERROR_MSG(0x117,
1710 "Error %d enumerating local IP interfaces for ip2nets to match\n",
1713 LCONSOLE_ERROR_MSG(0x118,
1714 "No local IP interfaces for ip2nets to match\n");
1719 CFS_ALLOC_PTR_ARRAY(ipaddrs, nip);
1722 CERROR("lnet: Can't allocate ipaddrs[%d], rc = %d\n",
1724 goto out_free_addrs;
1727 for (i = 0; i < nip; i++)
1728 ipaddrs[i] = ntohl(ifaces[i].li_ipaddr);
1730 rc = lnet_match_networks(networksp, ip2nets, ipaddrs, nip);
1732 LCONSOLE_ERROR_MSG(0x119, "Error %d parsing ip2nets\n", rc);
1733 } else if (rc == 0) {
1734 LCONSOLE_ERROR_MSG(0x11a, "ip2nets does not match "
1735 "any local IP interfaces\n");
1738 CFS_FREE_PTR_ARRAY(ipaddrs, nip);
1741 return rc > 0 ? 0 : rc;