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/nsproxy.h>
15 #include <linux/ethtool.h>
16 #include <net/net_namespace.h>
17 #include <lnet/lib-lnet.h>
19 /* tmp struct for parsing routes */
20 struct lnet_text_buf {
21 struct list_head ltb_list; /* stash on lists */
22 int ltb_size; /* allocated size */
23 char ltb_text[]; /* text buffer */
26 static int lnet_tbnob = 0; /* track text buf allocation */
27 #define LNET_MAX_TEXTBUF_NOB (64<<10) /* bound allocation */
28 #define LNET_SINGLE_TEXTBUF_NOB (4<<10)
30 #define SPACESTR " \t\v\r\n"
31 #define DELIMITERS ":()[]"
34 lnet_syntax(const char *name, const char *str, int offset, int width)
36 static char dots[LNET_SINGLE_TEXTBUF_NOB];
37 static char dashes[LNET_SINGLE_TEXTBUF_NOB];
39 memset(dots, '.', sizeof(dots));
40 dots[sizeof(dots)-1] = 0;
41 memset(dashes, '-', sizeof(dashes));
42 dashes[sizeof(dashes)-1] = 0;
44 LCONSOLE_ERROR_MSG(0x10f, "Error parsing '%s=\"%s\"'\n", name, str);
45 LCONSOLE_ERROR_MSG(0x110, "here...........%.*s..%.*s|%.*s|\n",
46 (int)strlen(name), dots, offset, dots,
47 (width < 1) ? 0 : width - 1, dashes);
64 lnet_net_unique(__u32 net_id, struct list_head *netlist,
65 struct lnet_net **net)
67 struct lnet_net *net_l;
72 list_for_each_entry(net_l, netlist, net_list) {
73 if (net_l->net_id == net_id) {
83 /* check that the NI is unique within the list of NIs already added to
86 lnet_ni_unique_net(struct list_head *nilist, char *iface)
88 struct list_head *tmp;
91 list_for_each(tmp, nilist) {
92 ni = list_entry(tmp, struct lnet_ni, ni_netlist);
94 if (ni->ni_interface != NULL &&
95 strncmp(ni->ni_interface,
97 LNET_MAX_STR_LEN) == 0)
104 in_array(__u32 *array, __u32 size, __u32 value)
108 for (i = 0; i < size; i++) {
109 if (array[i] == value)
117 lnet_net_append_cpts(__u32 *cpts, __u32 ncpts, struct lnet_net *net)
119 __u32 *added_cpts = NULL;
120 int i, j = 0, rc = 0;
123 * no need to go futher since a subset of the NIs already exist on
126 if (net->net_ncpts == LNET_CPT_NUMBER)
130 /* there is an NI which will exist on all CPTs */
131 if (net->net_cpts != NULL)
132 CFS_FREE_PTR_ARRAY(net->net_cpts, net->net_ncpts);
133 net->net_cpts = NULL;
134 net->net_ncpts = LNET_CPT_NUMBER;
138 if (net->net_cpts == NULL) {
139 CFS_ALLOC_PTR_ARRAY(net->net_cpts, ncpts);
140 if (net->net_cpts == NULL)
142 memcpy(net->net_cpts, cpts, ncpts * sizeof(*net->net_cpts));
143 net->net_ncpts = ncpts;
147 CFS_ALLOC_PTR_ARRAY(added_cpts, LNET_CPT_NUMBER);
148 if (added_cpts == NULL)
151 for (i = 0; i < ncpts; i++) {
152 if (!in_array(net->net_cpts, net->net_ncpts, cpts[i])) {
153 added_cpts[j] = cpts[i];
158 /* append the new cpts if any to the list of cpts in the net */
160 __u32 *array = NULL, *loc;
161 __u32 total_entries = j + net->net_ncpts;
163 CFS_ALLOC_PTR_ARRAY(array, total_entries);
169 memcpy(array, net->net_cpts,
170 net->net_ncpts * sizeof(*net->net_cpts));
171 loc = array + net->net_ncpts;
172 memcpy(loc, added_cpts, j * sizeof(*net->net_cpts));
174 CFS_FREE_PTR_ARRAY(net->net_cpts, net->net_ncpts);
175 net->net_ncpts = total_entries;
176 net->net_cpts = array;
180 CFS_FREE_PTR_ARRAY(added_cpts, LNET_CPT_NUMBER);
186 lnet_net_remove_cpts(__u32 *cpts, __u32 ncpts, struct lnet_net *net)
192 * Operation Assumption:
193 * This function is called after an NI has been removed from
196 * if we're removing an NI which exists on all CPTs then
197 * we have to check if any of the other NIs on this net also
198 * exists on all CPTs. If none, then we need to build our Net CPT
199 * list based on the remaining NIs.
201 * If the NI being removed exist on a subset of the CPTs then we
202 * alo rebuild the Net CPT list based on the remaining NIs, which
203 * should resutl in the expected Net CPT list.
207 * sometimes this function can be called due to some failure
208 * creating an NI, before any of the cpts are allocated, so check
209 * for that case and don't do anything
214 if (ncpts == LNET_CPT_NUMBER) {
216 * first iteration through the NI list in the net to see
217 * if any of the NIs exist on all the CPTs. If one is
218 * found then our job is done.
220 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
221 if (ni->ni_ncpts == LNET_CPT_NUMBER)
227 * Rebuild the Net CPT list again, thereby only including only the
228 * CPTs which the remaining NIs are associated with.
230 if (net->net_cpts != NULL) {
231 CFS_FREE_PTR_ARRAY(net->net_cpts, net->net_ncpts);
232 net->net_cpts = NULL;
235 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
236 rc = lnet_net_append_cpts(ni->ni_cpts, ni->ni_ncpts,
239 CERROR("Out of Memory\n");
241 * do our best to keep on going. Delete
242 * the net cpts and set it to NULL. This
243 * way we can keep on going but less
244 * efficiently, since memory accesses might be
247 if (net->net_cpts != NULL) {
248 CFS_FREE_PTR_ARRAY(net->net_cpts,
250 net->net_cpts = NULL;
251 net->net_ncpts = LNET_CPT_NUMBER;
259 lnet_ni_free(struct lnet_ni *ni)
261 lnet_net_remove_cpts(ni->ni_cpts, ni->ni_ncpts, ni->ni_net);
263 if (ni->ni_refs != NULL)
264 cfs_percpt_free(ni->ni_refs);
266 if (ni->ni_tx_queues != NULL)
267 cfs_percpt_free(ni->ni_tx_queues);
269 if (ni->ni_cpts != NULL)
270 cfs_expr_list_values_free(ni->ni_cpts, ni->ni_ncpts);
272 if (ni->ni_interface != NULL) {
273 LIBCFS_FREE(ni->ni_interface,
274 strlen(ni->ni_interface) + 1);
277 /* release reference to net namespace */
278 if (ni->ni_net_ns != NULL)
279 put_net(ni->ni_net_ns);
281 LIBCFS_FREE(ni, sizeof(*ni));
285 lnet_net_free(struct lnet_net *net)
287 struct list_head *tmp, *tmp2;
290 LASSERT(list_empty(&net->net_ni_zombie));
293 * delete any nis that haven't been added yet. This could happen
294 * if there is a failure on net startup
296 list_for_each_safe(tmp, tmp2, &net->net_ni_added) {
297 ni = list_entry(tmp, struct lnet_ni, ni_netlist);
298 list_del_init(&ni->ni_netlist);
302 /* delete any nis which have been started. */
303 list_for_each_safe(tmp, tmp2, &net->net_ni_list) {
304 ni = list_entry(tmp, struct lnet_ni, ni_netlist);
305 list_del_init(&ni->ni_netlist);
309 if (net->net_cpts != NULL) {
310 CFS_FREE_PTR_ARRAY(net->net_cpts, net->net_ncpts);
311 net->net_ncpts = LNET_CPT_NUMBER;
312 net->net_cpts = NULL;
315 LIBCFS_FREE(net, sizeof(*net));
319 lnet_net_alloc(__u32 net_id, struct list_head *net_list)
321 struct lnet_net *net;
323 if (!lnet_net_unique(net_id, net_list, &net)) {
324 CDEBUG(D_NET, "Returning duplicate net %p %s\n", net,
325 libcfs_net2str(net->net_id));
329 LIBCFS_ALLOC(net, sizeof(*net));
331 CERROR("Out of memory creating network %s\n",
332 libcfs_net2str(net_id));
336 INIT_LIST_HEAD(&net->net_list);
337 INIT_LIST_HEAD(&net->net_ni_list);
338 INIT_LIST_HEAD(&net->net_ni_added);
339 INIT_LIST_HEAD(&net->net_ni_zombie);
340 INIT_LIST_HEAD(&net->net_rtr_pref_nids);
341 spin_lock_init(&net->net_lock);
343 net->net_id = net_id;
344 net->net_last_alive = ktime_get_seconds();
346 net->net_sel_priority = LNET_MAX_SELECTION_PRIORITY;
348 /* initialize global paramters to undefiend */
349 net->net_tunables.lct_peer_timeout = -1;
350 net->net_tunables.lct_max_tx_credits = -1;
351 net->net_tunables.lct_peer_tx_credits = -1;
352 net->net_tunables.lct_peer_rtr_credits = -1;
353 net->net_tunables.lct_version = 0;
356 list_add_tail(&net->net_list, net_list);
361 int lnet_ni_add_interface(struct lnet_ni *ni, char *iface)
363 size_t iface_len = strlen(iface) + 1;
368 if (ni->ni_interface != NULL) {
369 LCONSOLE_ERROR_MSG(0x115, "%s: interface %s already set for net %s: rc = %d\n",
370 iface, ni->ni_interface,
371 libcfs_net2str(LNET_NID_NET(&ni->ni_nid)),
376 /* Allocate memory for the interface, so the code parsing input into
377 * tokens and adding interfaces can free the input safely.
378 * ni->ni_interface is freed in lnet_ni_free().
380 LIBCFS_ALLOC(ni->ni_interface, iface_len);
382 if (ni->ni_interface == NULL) {
383 CERROR("%s: cannot allocate net interface name: rc = %d\n",
388 strscpy(ni->ni_interface, iface, iface_len);
392 EXPORT_SYMBOL(lnet_ni_add_interface);
394 static struct lnet_ni *
395 lnet_ni_alloc_common(struct lnet_net *net, struct lnet_nid *nid, char *iface)
397 struct lnet_tx_queue *tq;
402 /* make sure that this NI is unique in the net it's
404 if (!lnet_ni_unique_net(&net->net_ni_added, iface))
407 LIBCFS_ALLOC(ni, sizeof(*ni));
409 CERROR("Out of memory creating network interface %s%s\n",
410 libcfs_net2str(net->net_id),
411 (iface != NULL) ? iface : "");
415 spin_lock_init(&ni->ni_lock);
416 INIT_LIST_HEAD(&ni->ni_netlist);
417 INIT_LIST_HEAD(&ni->ni_recovery);
418 LNetInvalidateMDHandle(&ni->ni_ping_mdh);
419 ni->ni_refs = cfs_percpt_alloc(lnet_cpt_table(),
420 sizeof(*ni->ni_refs[0]));
421 if (ni->ni_refs == NULL)
424 ni->ni_tx_queues = cfs_percpt_alloc(lnet_cpt_table(),
425 sizeof(*ni->ni_tx_queues[0]));
426 if (ni->ni_tx_queues == NULL)
429 cfs_percpt_for_each(tq, i, ni->ni_tx_queues)
430 INIT_LIST_HEAD(&tq->tq_delayed);
433 if (nid_same(nid, &LNET_ANY_NID)) {
434 /* LND will fill in the address part of the NID */
435 ni->ni_nid.nid_type = LNET_NETTYP(net->net_id);
436 ni->ni_nid.nid_num = cpu_to_be16(LNET_NETNUM(net->net_id));
441 /* Store net namespace in which current ni is being created */
442 if (current->nsproxy && current->nsproxy->net_ns)
443 ni->ni_net_ns = get_net(current->nsproxy->net_ns);
445 ni->ni_net_ns = get_net(&init_net);
447 ni->ni_state = LNET_NI_STATE_INIT;
448 ni->ni_sel_priority = LNET_MAX_SELECTION_PRIORITY;
449 list_add_tail(&ni->ni_netlist, &net->net_ni_added);
452 * if an interface name is provided then make sure to add in that
453 * interface name in NI
456 if (lnet_ni_add_interface(ni, iface) != 0)
465 /* allocate and add to the provided network */
467 lnet_ni_alloc(struct lnet_net *net, struct cfs_expr_list *el, char *iface)
472 ni = lnet_ni_alloc_common(net, &LNET_ANY_NID, iface);
478 ni->ni_ncpts = LNET_CPT_NUMBER;
480 rc = cfs_expr_list_values(el, LNET_CPT_NUMBER, &ni->ni_cpts);
482 CERROR("Failed to set CPTs for NI %s(%s): %d\n",
483 libcfs_net2str(net->net_id),
484 (iface != NULL) ? iface : "", rc);
488 LASSERT(rc <= LNET_CPT_NUMBER);
489 if (rc == LNET_CPT_NUMBER) {
490 CFS_FREE_PTR_ARRAY(ni->ni_cpts, rc);
497 rc = lnet_net_append_cpts(ni->ni_cpts, ni->ni_ncpts, net);
508 lnet_ni_alloc_w_cpt_array(struct lnet_net *net, struct lnet_nid *nid,
509 u32 *cpts, u32 ncpts, char *iface)
514 ni = lnet_ni_alloc_common(net, nid, iface);
520 ni->ni_ncpts = LNET_CPT_NUMBER;
522 size_t array_size = ncpts * sizeof(ni->ni_cpts[0]);
524 CFS_ALLOC_PTR_ARRAY(ni->ni_cpts, ncpts);
525 if (ni->ni_cpts == NULL)
527 memcpy(ni->ni_cpts, cpts, array_size);
528 ni->ni_ncpts = ncpts;
531 rc = lnet_net_append_cpts(ni->ni_cpts, ni->ni_ncpts, net);
542 * Parse the networks string and create the matching set of NIs on the
546 lnet_parse_networks(struct list_head *netlist, const char *networks)
548 struct cfs_expr_list *net_el = NULL;
549 struct cfs_expr_list *ni_el = NULL;
553 struct lnet_net *net;
554 struct lnet_ni *ni = NULL;
558 if (networks == NULL) {
559 CERROR("networks string is undefined\n");
563 if (strlen(networks) > LNET_SINGLE_TEXTBUF_NOB) {
564 /* _WAY_ conservative */
565 LCONSOLE_ERROR_MSG(0x112, "Can't parse networks: string too "
570 tokensize = strlen(networks) + 1;
572 LIBCFS_ALLOC(tokens, tokensize);
573 if (tokens == NULL) {
574 CERROR("Can't allocate net tokens\n");
578 memcpy(tokens, networks, tokensize);
584 * NB we don't check interface conflicts here; it's the LNDs
585 * responsibility (if it cares at all)
594 * Parse a network string into its components.
596 * <name>{"("...")"}{"["<el>"]"}
599 /* Network name (mandatory) */
600 while (isspace(*str))
605 str += strcspn(str, SPACESTR ":()[],");
606 while (isspace(*str))
609 /* Interface list (optional) */
613 str += strcspn(str, ")");
620 } while (isspace(*str));
625 /* CPT expression (optional) */
628 str += strcspn(str, "]");
633 rc = cfs_expr_list_parse(elstr, str - elstr + 1,
634 0, LNET_CPT_NUMBER - 1,
643 } while (isspace(*str));
647 if (*str && (strchr(DELIMITERS, *str) != NULL))
650 /* go to the next net if it exits */
651 str += strcspn(str, ",");
656 * At this point the name is properly terminated.
658 net_id = libcfs_str2net(name);
659 if (net_id == LNET_NET_ANY) {
660 LCONSOLE_ERROR_MSG(0x113,
661 "Unrecognised network type\n");
666 if (LNET_NETTYP(net_id) == LOLND) {
667 /* Loopback is implicit, and there can be only one. */
669 cfs_expr_list_free(net_el);
672 /* Should we error out instead? */
677 * All network paramaters are now known.
681 /* always allocate a net, since we will eventually add an
682 * interface to it, or we will fail, in which case we'll
684 net = lnet_net_alloc(net_id, netlist);
685 if (IS_ERR_OR_NULL(net))
688 if (the_lnet.ln_nis_use_large_nids)
689 net->net_tunables.lct_version = 1;
693 * No interface list was specified, allocate a
694 * ni using the defaults.
696 ni = lnet_ni_alloc(net, net_el, NULL);
697 if (IS_ERR_OR_NULL(ni))
702 cfs_expr_list_free(net_el);
712 /* Interface name (mandatory) */
713 while (isspace(*nistr))
716 nistr += strcspn(nistr, SPACESTR "[],");
717 while (isspace(*nistr))
720 /* CPT expression (optional) */
723 nistr += strcspn(nistr, "]");
728 rc = cfs_expr_list_parse(elstr,
730 0, LNET_CPT_NUMBER - 1,
739 } while (isspace(*nistr));
745 * End of single interface specificaton,
746 * advance to the start of the next one, if
752 } while (isspace(*nistr));
763 * At this point the name is properly terminated.
770 ni = lnet_ni_alloc(net, ni_el, name);
771 if (IS_ERR_OR_NULL(ni))
775 if (ni_el != net_el) {
776 cfs_expr_list_free(ni_el);
783 cfs_expr_list_free(net_el);
788 LIBCFS_FREE(tokens, tokensize);
792 lnet_syntax("networks", networks, (int)(str - tokens), strlen(str));
794 /* free the net list and all the nis on each net */
795 while ((net = list_first_entry_or_null(netlist,
797 net_list)) != NULL) {
798 list_del_init(&net->net_list);
802 if (ni_el && ni_el != net_el)
803 cfs_expr_list_free(ni_el);
805 cfs_expr_list_free(net_el);
807 LIBCFS_FREE(tokens, tokensize);
812 static struct lnet_text_buf *lnet_new_text_buf(int str_len)
814 struct lnet_text_buf *ltb;
817 /* NB allocate space for the terminating 0 */
818 nob = offsetof(struct lnet_text_buf, ltb_text[str_len + 1]);
819 if (nob > LNET_SINGLE_TEXTBUF_NOB) {
820 /* _way_ conservative for "route net gateway..." */
821 CERROR("text buffer too big\n");
825 if (lnet_tbnob + nob > LNET_MAX_TEXTBUF_NOB) {
826 CERROR("Too many text buffers\n");
830 LIBCFS_ALLOC(ltb, nob);
835 ltb->ltb_text[0] = 0;
841 lnet_free_text_buf(struct lnet_text_buf *ltb)
843 lnet_tbnob -= ltb->ltb_size;
844 LIBCFS_FREE(ltb, ltb->ltb_size);
848 lnet_free_text_bufs(struct list_head *tbs)
850 struct lnet_text_buf *ltb;
852 while ((ltb = list_first_entry_or_null(tbs, struct lnet_text_buf,
853 ltb_list)) != NULL) {
854 list_del(<b->ltb_list);
855 lnet_free_text_buf(ltb);
860 lnet_str2tbs_sep(struct list_head *tbs, const char *str)
866 struct lnet_text_buf *ltb;
868 /* Split 'str' into separate commands */
870 /* skip leading whitespace */
871 while (isspace(*str))
874 /* scan for separator or comment */
875 for (sep = str; *sep != 0; sep++)
876 if (lnet_issep(*sep) || *sep == '#')
879 nob = (int)(sep - str);
881 ltb = lnet_new_text_buf(nob);
883 lnet_free_text_bufs(&pending);
887 for (i = 0; i < nob; i++)
889 ltb->ltb_text[i] = ' ';
891 ltb->ltb_text[i] = str[i];
893 ltb->ltb_text[nob] = 0;
895 list_add_tail(<b->ltb_list, &pending);
899 /* scan for separator */
902 } while (*sep != 0 && !lnet_issep(*sep));
911 list_splice(&pending, tbs->prev);
916 lnet_expand1tb(struct list_head *list,
917 char *str, char *sep1, char *sep2,
918 char *item, int itemlen)
920 int len1 = (int)(sep1 - str);
921 int len2 = strlen(sep2 + 1);
922 struct lnet_text_buf *ltb;
924 LASSERT (*sep1 == '[');
925 LASSERT (*sep2 == ']');
927 ltb = lnet_new_text_buf(len1 + itemlen + len2);
931 memcpy(ltb->ltb_text, str, len1);
932 memcpy(<b->ltb_text[len1], item, itemlen);
933 memcpy(<b->ltb_text[len1+itemlen], sep2 + 1, len2);
934 ltb->ltb_text[len1 + itemlen + len2] = 0;
936 list_add_tail(<b->ltb_list, list);
941 lnet_str2tbs_expand(struct list_head *tbs, char *str)
956 sep = strchr(str, '[');
957 if (sep == NULL) /* nothing to expand */
960 sep2 = strchr(sep, ']');
964 for (parsed = sep; parsed < sep2; parsed = enditem) {
967 while (enditem < sep2 && *enditem != ',')
970 if (enditem == parsed) /* no empty items */
973 if (sscanf(parsed, "%d-%d/%d%n", &lo, &hi, &stride, &scanned) < 3) {
975 if (sscanf(parsed, "%d-%d%n", &lo, &hi, &scanned) < 2) {
977 /* simple string enumeration */
978 if (lnet_expand1tb(&pending, str, sep, sep2,
979 parsed, (int)(enditem - parsed)) != 0)
988 /* range expansion */
990 if (enditem != parsed + scanned) /* no trailing junk */
993 if (hi < 0 || lo < 0 || stride < 0 || hi < lo ||
994 (hi - lo) % stride != 0)
997 for (i = lo; i <= hi; i += stride) {
999 snprintf(num, sizeof(num), "%d", i);
1001 if (nob + 1 == sizeof(num))
1004 if (lnet_expand1tb(&pending, str, sep, sep2,
1010 list_splice(&pending, tbs->prev);
1014 lnet_free_text_bufs(&pending);
1019 lnet_parse_hops (char *str, unsigned int *hops)
1021 int len = strlen(str);
1024 return (sscanf(str, "%u%n", hops, &nob) >= 1 &&
1026 *hops > 0 && *hops < 256);
1029 #define LNET_PRIORITY_SEPARATOR (':')
1032 lnet_parse_priority(char *str, unsigned int *priority, char **token)
1038 sep = strchr(str, LNET_PRIORITY_SEPARATOR);
1043 len = strlen(sep + 1);
1045 if ((sscanf((sep+1), "%u%n", priority, &nob) < 1) || (len != nob)) {
1046 /* Update the caller's token pointer so it treats the found
1047 priority as the token to report in the error message. */
1048 *token += sep - str + 1;
1052 CDEBUG(D_NET, "gateway %s, priority %d, nob %d\n", str, *priority, nob);
1055 * Change priority separator to \0 to be able to parse NID
1062 lnet_parse_route(char *str, int *im_a_router)
1064 /* static scratch buffer OK (single threaded) */
1065 static char cmd[LNET_SINGLE_TEXTBUF_NOB];
1068 LIST_HEAD(gateways);
1069 struct list_head *tmp1;
1070 struct list_head *tmp2;
1072 struct lnet_nid nid;
1073 struct lnet_text_buf *ltb = NULL;
1074 struct lnet_text_buf *ltb1, *ltb2;
1082 unsigned int priority = 0;
1084 /* save a copy of the string for error messages */
1085 strncpy(cmd, str, sizeof(cmd));
1086 cmd[sizeof(cmd) - 1] = '\0';
1090 /* scan for token start */
1091 while (isspace(*sep))
1094 if (ntokens < (got_hops ? 3 : 2))
1102 /* scan for token end */
1103 while (*sep != 0 && !isspace(*sep))
1109 tmp2 = &nets; /* expanding nets */
1110 } else if (ntokens == 2 &&
1111 lnet_parse_hops(token, &hops)) {
1112 got_hops = 1; /* got a hop count */
1115 tmp2 = &gateways; /* expanding gateways */
1118 ltb = lnet_new_text_buf(strlen(token));
1122 strcpy(ltb->ltb_text, token);
1123 tmp1 = <b->ltb_list;
1124 list_add_tail(tmp1, tmp2);
1126 while (tmp1 != tmp2) {
1127 ltb = list_entry(tmp1, struct lnet_text_buf, ltb_list);
1129 rc = lnet_str2tbs_expand(tmp1->next, ltb->ltb_text);
1135 if (rc > 0) { /* expanded! */
1136 list_del(<b->ltb_list);
1137 lnet_free_text_buf(ltb);
1142 net = libcfs_str2net(ltb->ltb_text);
1143 if (net == LNET_NET_ANY ||
1144 LNET_NETTYP(net) == LOLND)
1147 rc = lnet_parse_priority(ltb->ltb_text,
1152 if (libcfs_strnid(&nid, ltb->ltb_text) != 0 ||
1159 /* if there are no hops set then we want to flag this value as
1160 * unset since hops is an optional parameter */
1162 hops = LNET_UNDEFINED_HOPS;
1164 LASSERT(!list_empty(&nets));
1165 LASSERT(!list_empty(&gateways));
1167 list_for_each_entry(ltb1, &nets, ltb_list) {
1168 net = libcfs_str2net(ltb1->ltb_text);
1169 LASSERT(net != LNET_NET_ANY);
1171 list_for_each_entry(ltb2, &gateways, ltb_list) {
1172 LASSERT(libcfs_strnid(&nid, ltb2->ltb_text) == 0);
1174 if (lnet_islocalnid(&nid)) {
1179 rc = lnet_add_route(net, hops, &nid, priority, 1);
1180 if (rc != 0 && rc != -EEXIST && rc != -EHOSTUNREACH) {
1181 CERROR("Can't create route "
1183 libcfs_net2str(net),
1184 libcfs_nidstr(&nid));
1194 lnet_syntax("routes", cmd, (int)(token - str), strlen(token));
1196 lnet_free_text_bufs(&nets);
1197 lnet_free_text_bufs(&gateways);
1202 lnet_parse_route_tbs(struct list_head *tbs, int *im_a_router)
1204 struct lnet_text_buf *ltb;
1206 while ((ltb = list_first_entry_or_null(tbs, struct lnet_text_buf,
1207 ltb_list)) != NULL) {
1208 if (lnet_parse_route(ltb->ltb_text, im_a_router) < 0) {
1209 lnet_free_text_bufs(tbs);
1213 list_del(<b->ltb_list);
1214 lnet_free_text_buf(ltb);
1221 lnet_parse_routes(const char *routes, int *im_a_router)
1228 if (lnet_str2tbs_sep(&tbs, routes) < 0) {
1229 CERROR("Error parsing routes\n");
1232 rc = lnet_parse_route_tbs(&tbs, im_a_router);
1235 LASSERT (lnet_tbnob == 0);
1240 lnet_match_network_token(char *token, __u32 *ipaddrs, int nip)
1246 rc = cfs_ip_addr_parse(token, 0, &list);
1250 for (rc = i = 0; !rc && i < nip; i++)
1251 rc = cfs_ip_addr_match(ipaddrs[i], &list);
1253 cfs_expr_list_free_list(&list);
1259 lnet_match_network_tokens(char *net_entry, __u32 *ipaddrs, int nip)
1261 static char tokens[LNET_SINGLE_TEXTBUF_NOB];
1270 LASSERT(strlen(net_entry) < sizeof(tokens));
1272 /* work on a copy of the string */
1273 strcpy(tokens, net_entry);
1276 /* scan for token start */
1277 sep = skip_spaces(sep);
1281 token = strsep(&sep, " \t\n\r\v\f");
1283 if (ntokens++ == 0) {
1288 rc = lnet_match_network_token(token, ipaddrs, nip);
1290 lnet_syntax("ip2nets", net_entry,
1291 (int)(token - tokens), strlen(token));
1295 matched |= (rc != 0);
1301 strcpy(net_entry, net); /* replace with matched net */
1306 lnet_netspec2net(char *netspec)
1308 char *bracket = strchr(netspec, '(');
1311 if (bracket != NULL)
1314 net = libcfs_str2net(netspec);
1316 if (bracket != NULL)
1323 lnet_splitnets(char *source, struct list_head *nets)
1328 struct lnet_text_buf *tb;
1329 struct lnet_text_buf *tb2;
1334 LASSERT(!list_empty(nets));
1335 LASSERT(nets->next == nets->prev); /* single entry */
1337 tb = list_first_entry(nets, struct lnet_text_buf, ltb_list);
1340 sep = strchr(tb->ltb_text, ',');
1341 bracket = strchr(tb->ltb_text, '(');
1346 /* netspec lists interfaces... */
1348 offset2 = offset + (int)(bracket - tb->ltb_text);
1349 len = strlen(bracket);
1351 bracket = strchr(bracket + 1, ')');
1353 if (bracket == NULL ||
1354 !(bracket[1] == ',' || bracket[1] == 0)) {
1355 lnet_syntax("ip2nets", source, offset2, len);
1359 sep = (bracket[1] == 0) ? NULL : bracket + 1;
1365 net = lnet_netspec2net(tb->ltb_text);
1366 if (net == LNET_NET_ANY) {
1367 lnet_syntax("ip2nets", source, offset,
1368 strlen(tb->ltb_text));
1372 list_for_each_entry(tb2, nets, ltb_list) {
1376 if (net == lnet_netspec2net(tb2->ltb_text)) {
1377 /* duplicate network */
1378 lnet_syntax("ip2nets", source, offset,
1379 strlen(tb->ltb_text));
1387 offset += (int)(sep - tb->ltb_text);
1389 tb2 = lnet_new_text_buf(len);
1393 strncpy(tb2->ltb_text, sep, len);
1394 tb2->ltb_text[len] = '\0';
1395 list_add_tail(&tb2->ltb_list, nets);
1402 lnet_match_networks(const char **networksp, const char *ip2nets,
1403 __u32 *ipaddrs, int nip)
1405 static char networks[LNET_SINGLE_TEXTBUF_NOB];
1406 static char source[LNET_SINGLE_TEXTBUF_NOB];
1408 LIST_HEAD(raw_entries);
1409 LIST_HEAD(matched_nets);
1410 LIST_HEAD(current_nets);
1411 struct list_head *t;
1412 struct list_head *t2;
1413 struct lnet_text_buf *tb;
1418 if (lnet_str2tbs_sep(&raw_entries, ip2nets) < 0) {
1419 CERROR("Error parsing ip2nets\n");
1420 LASSERT(lnet_tbnob == 0);
1429 while ((tb = list_first_entry_or_null(&raw_entries,
1430 struct lnet_text_buf,
1431 ltb_list)) != NULL) {
1432 strncpy(source, tb->ltb_text, sizeof(source));
1433 source[sizeof(source) - 1] = '\0';
1435 /* replace ltb_text with the network(s) add on match */
1436 rc = lnet_match_network_tokens(tb->ltb_text, ipaddrs, nip);
1440 list_del(&tb->ltb_list);
1442 if (rc == 0) { /* no match */
1443 lnet_free_text_buf(tb);
1447 /* split into separate networks */
1448 INIT_LIST_HEAD(¤t_nets);
1449 list_add(&tb->ltb_list, ¤t_nets);
1450 rc = lnet_splitnets(source, ¤t_nets);
1454 list_for_each_safe(t, t2, ¤t_nets) {
1455 tb = list_entry(t, struct lnet_text_buf, ltb_list);
1457 list_move_tail(&tb->ltb_list, &matched_nets);
1459 len += scnprintf(networks + len, sizeof(networks) - len,
1460 "%s%s", (len == 0) ? "" : ",",
1463 if (len >= sizeof(networks)) {
1464 CERROR("Too many matched networks\n");
1474 lnet_free_text_bufs(&raw_entries);
1475 lnet_free_text_bufs(&matched_nets);
1476 lnet_free_text_bufs(¤t_nets);
1477 LASSERT(lnet_tbnob == 0);
1482 *networksp = networks;
1486 __u32 lnet_set_link_fatal_state(struct lnet_ni *ni, unsigned int link_state)
1488 CDEBUG(D_NET, "%s: set link fatal state to %u\n",
1489 libcfs_nidstr(&ni->ni_nid), link_state);
1490 return atomic_xchg(&ni->ni_fatal_error_on, link_state);
1492 EXPORT_SYMBOL(lnet_set_link_fatal_state);
1494 int lnet_get_link_status(struct net_device *dev)
1501 if (!netif_running(dev)) {
1503 CDEBUG(D_NET, "device idx %d not running\n", dev->ifindex);
1505 /* Some devices may not be providing link settings */
1506 else if (dev->ethtool_ops->get_link) {
1507 ret = dev->ethtool_ops->get_link(dev);
1508 CDEBUG(D_NET, "device idx %d get_link %u\n",
1515 EXPORT_SYMBOL(lnet_get_link_status);
1517 int lnet_inet_select(struct lnet_ni *ni,
1518 struct lnet_inetdev *ifaces,
1521 bool addr_set = nid_addr_is_set(&ni->ni_nid);
1524 /* default to first interface if both interface and NID unspecified */
1525 if (!ni->ni_interface && !addr_set)
1528 for (if_idx = 0; if_idx < num_ifaces; if_idx++) {
1529 if (ni->ni_interface && strlen(ni->ni_interface) &&
1530 strcmp(ni->ni_interface, ifaces[if_idx].li_name) != 0)
1531 /* not the specified interface */
1535 /* IP unspecified, use IP of first matching interface */
1538 if (ifaces[if_idx].li_size == NID_ADDR_BYTES(&ni->ni_nid)) {
1539 char *addr = (char *)&ifaces[if_idx].li_ipaddr;
1541 if (ifaces[if_idx].li_size != 4)
1542 addr = (char *)ifaces[if_idx].li_ipv6addr;
1544 if (memcmp(ni->ni_nid.nid_addr, addr,
1545 ifaces[if_idx].li_size) == 0)
1550 if (if_idx < num_ifaces)
1554 CERROR("%s: failed to find IP address %s\n",
1555 libcfs_lnd2modname(ni->ni_nid.nid_type),
1556 libcfs_nidstr(&ni->ni_nid));
1557 else if (ni->ni_interface)
1558 CERROR("%s: failed to find interface %s%s%s\n",
1559 libcfs_lnd2modname(ni->ni_nid.nid_type),
1560 ni->ni_interface, addr_set ? "@" : "",
1561 addr_set ? libcfs_nidstr(&ni->ni_nid) : "");
1565 EXPORT_SYMBOL(lnet_inet_select);
1568 lnet_parse_ip2nets(const char **networksp, const char *ip2nets)
1570 struct lnet_inetdev *ifaces = NULL;
1571 u32 *ipaddrs = NULL;
1576 if (current->nsproxy && current->nsproxy->net_ns)
1577 nip = lnet_inet_enumerate(&ifaces, current->nsproxy->net_ns,
1578 the_lnet.ln_nis_use_large_nids);
1580 nip = lnet_inet_enumerate(&ifaces, &init_net,
1581 the_lnet.ln_nis_use_large_nids);
1583 if (nip != -ENOENT) {
1584 LCONSOLE_ERROR_MSG(0x117,
1585 "Error %d enumerating local IP interfaces for ip2nets to match\n",
1588 LCONSOLE_ERROR_MSG(0x118,
1589 "No local IP interfaces for ip2nets to match\n");
1594 CFS_ALLOC_PTR_ARRAY(ipaddrs, nip);
1597 CERROR("lnet: Can't allocate ipaddrs[%d], rc = %d\n",
1599 goto out_free_addrs;
1602 for (i = 0; i < nip; i++)
1603 ipaddrs[i] = ntohl(ifaces[i].li_ipaddr);
1605 rc = lnet_match_networks(networksp, ip2nets, ipaddrs, nip);
1607 LCONSOLE_ERROR_MSG(0x119, "Error %d parsing ip2nets\n", rc);
1608 } else if (rc == 0) {
1609 LCONSOLE_ERROR_MSG(0x11a, "ip2nets does not match "
1610 "any local IP interfaces\n");
1613 CFS_FREE_PTR_ARRAY(ipaddrs, nip);
1616 return rc > 0 ? 0 : rc;