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("Error parsing '%s=\"%s\"'\n", name, str);
45 LCONSOLE_ERROR("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("%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("Can't parse networks: string too long\n");
569 tokensize = strlen(networks) + 1;
571 LIBCFS_ALLOC(tokens, tokensize);
572 if (tokens == NULL) {
573 CERROR("Can't allocate net tokens\n");
577 memcpy(tokens, networks, tokensize);
583 * NB we don't check interface conflicts here; it's the LNDs
584 * responsibility (if it cares at all)
593 * Parse a network string into its components.
595 * <name>{"("...")"}{"["<el>"]"}
598 /* Network name (mandatory) */
599 while (isspace(*str))
604 str += strcspn(str, SPACESTR ":()[],");
605 while (isspace(*str))
608 /* Interface list (optional) */
612 str += strcspn(str, ")");
619 } while (isspace(*str));
624 /* CPT expression (optional) */
627 str += strcspn(str, "]");
632 rc = cfs_expr_list_parse(elstr, str - elstr + 1,
633 0, LNET_CPT_NUMBER - 1,
642 } while (isspace(*str));
646 if (*str && (strchr(DELIMITERS, *str) != NULL))
649 /* go to the next net if it exits */
650 str += strcspn(str, ",");
655 * At this point the name is properly terminated.
657 net_id = libcfs_str2net(name);
658 if (net_id == LNET_NET_ANY) {
659 LCONSOLE_ERROR("Unrecognised network type\n");
664 if (LNET_NETTYP(net_id) == LOLND) {
665 /* Loopback is implicit, and there can be only one. */
667 cfs_expr_list_free(net_el);
670 /* Should we error out instead? */
675 * All network paramaters are now known.
679 /* always allocate a net, since we will eventually add an
680 * interface to it, or we will fail, in which case we'll
682 net = lnet_net_alloc(net_id, netlist);
683 if (IS_ERR_OR_NULL(net))
688 * No interface list was specified, allocate a
689 * ni using the defaults.
691 ni = lnet_ni_alloc(net, net_el, NULL);
692 if (IS_ERR_OR_NULL(ni))
697 cfs_expr_list_free(net_el);
707 /* Interface name (mandatory) */
708 while (isspace(*nistr))
711 nistr += strcspn(nistr, SPACESTR "[],");
712 while (isspace(*nistr))
715 /* CPT expression (optional) */
718 nistr += strcspn(nistr, "]");
723 rc = cfs_expr_list_parse(elstr,
725 0, LNET_CPT_NUMBER - 1,
734 } while (isspace(*nistr));
740 * End of single interface specificaton,
741 * advance to the start of the next one, if
747 } while (isspace(*nistr));
758 * At this point the name is properly terminated.
765 ni = lnet_ni_alloc(net, ni_el, name);
766 if (IS_ERR_OR_NULL(ni))
770 if (ni_el != net_el) {
771 cfs_expr_list_free(ni_el);
778 cfs_expr_list_free(net_el);
783 LIBCFS_FREE(tokens, tokensize);
787 lnet_syntax("networks", networks, (int)(str - tokens), strlen(str));
789 /* free the net list and all the nis on each net */
790 while ((net = list_first_entry_or_null(netlist,
792 net_list)) != NULL) {
793 list_del_init(&net->net_list);
797 if (ni_el && ni_el != net_el)
798 cfs_expr_list_free(ni_el);
800 cfs_expr_list_free(net_el);
802 LIBCFS_FREE(tokens, tokensize);
807 static struct lnet_text_buf *lnet_new_text_buf(int str_len)
809 struct lnet_text_buf *ltb;
812 /* NB allocate space for the terminating 0 */
813 nob = offsetof(struct lnet_text_buf, ltb_text[str_len + 1]);
814 if (nob > LNET_SINGLE_TEXTBUF_NOB) {
815 /* _way_ conservative for "route net gateway..." */
816 CERROR("text buffer too big\n");
820 if (lnet_tbnob + nob > LNET_MAX_TEXTBUF_NOB) {
821 CERROR("Too many text buffers\n");
825 LIBCFS_ALLOC(ltb, nob);
830 ltb->ltb_text[0] = 0;
836 lnet_free_text_buf(struct lnet_text_buf *ltb)
838 lnet_tbnob -= ltb->ltb_size;
839 LIBCFS_FREE(ltb, ltb->ltb_size);
843 lnet_free_text_bufs(struct list_head *tbs)
845 struct lnet_text_buf *ltb;
847 while ((ltb = list_first_entry_or_null(tbs, struct lnet_text_buf,
848 ltb_list)) != NULL) {
849 list_del(<b->ltb_list);
850 lnet_free_text_buf(ltb);
855 lnet_str2tbs_sep(struct list_head *tbs, const char *str)
861 struct lnet_text_buf *ltb;
863 /* Split 'str' into separate commands */
865 /* skip leading whitespace */
866 while (isspace(*str))
869 /* scan for separator or comment */
870 for (sep = str; *sep != 0; sep++)
871 if (lnet_issep(*sep) || *sep == '#')
874 nob = (int)(sep - str);
876 ltb = lnet_new_text_buf(nob);
878 lnet_free_text_bufs(&pending);
882 for (i = 0; i < nob; i++)
884 ltb->ltb_text[i] = ' ';
886 ltb->ltb_text[i] = str[i];
888 ltb->ltb_text[nob] = 0;
890 list_add_tail(<b->ltb_list, &pending);
894 /* scan for separator */
897 } while (*sep != 0 && !lnet_issep(*sep));
906 list_splice(&pending, tbs->prev);
911 lnet_expand1tb(struct list_head *list,
912 char *str, char *sep1, char *sep2,
913 char *item, int itemlen)
915 int len1 = (int)(sep1 - str);
916 int len2 = strlen(sep2 + 1);
917 struct lnet_text_buf *ltb;
919 LASSERT (*sep1 == '[');
920 LASSERT (*sep2 == ']');
922 ltb = lnet_new_text_buf(len1 + itemlen + len2);
926 memcpy(ltb->ltb_text, str, len1);
927 memcpy(<b->ltb_text[len1], item, itemlen);
928 memcpy(<b->ltb_text[len1+itemlen], sep2 + 1, len2);
929 ltb->ltb_text[len1 + itemlen + len2] = 0;
931 list_add_tail(<b->ltb_list, list);
936 lnet_str2tbs_expand(struct list_head *tbs, char *str)
951 sep = strchr(str, '[');
952 if (sep == NULL) /* nothing to expand */
955 sep2 = strchr(sep, ']');
959 for (parsed = sep; parsed < sep2; parsed = enditem) {
962 while (enditem < sep2 && *enditem != ',')
965 if (enditem == parsed) /* no empty items */
968 if (sscanf(parsed, "%d-%d/%d%n", &lo, &hi, &stride, &scanned) < 3) {
970 if (sscanf(parsed, "%d-%d%n", &lo, &hi, &scanned) < 2) {
972 /* simple string enumeration */
973 if (lnet_expand1tb(&pending, str, sep, sep2,
974 parsed, (int)(enditem - parsed)) != 0)
983 /* range expansion */
985 if (enditem != parsed + scanned) /* no trailing junk */
988 if (hi < 0 || lo < 0 || stride < 0 || hi < lo ||
989 (hi - lo) % stride != 0)
992 for (i = lo; i <= hi; i += stride) {
994 snprintf(num, sizeof(num), "%d", i);
996 if (nob + 1 == sizeof(num))
999 if (lnet_expand1tb(&pending, str, sep, sep2,
1005 list_splice(&pending, tbs->prev);
1009 lnet_free_text_bufs(&pending);
1014 lnet_parse_hops (char *str, unsigned int *hops)
1016 int len = strlen(str);
1019 return (sscanf(str, "%u%n", hops, &nob) >= 1 &&
1021 *hops > 0 && *hops < 256);
1024 #define LNET_PRIORITY_SEPARATOR (':')
1027 lnet_parse_priority(char *str, unsigned int *priority, char **token)
1033 sep = strchr(str, LNET_PRIORITY_SEPARATOR);
1038 len = strlen(sep + 1);
1040 if ((sscanf((sep+1), "%u%n", priority, &nob) < 1) || (len != nob)) {
1041 /* Update the caller's token pointer so it treats the found
1042 priority as the token to report in the error message. */
1043 *token += sep - str + 1;
1047 CDEBUG(D_NET, "gateway %s, priority %d, nob %d\n", str, *priority, nob);
1050 * Change priority separator to \0 to be able to parse NID
1057 lnet_parse_route(char *str, int *im_a_router)
1059 /* static scratch buffer OK (single threaded) */
1060 static char cmd[LNET_SINGLE_TEXTBUF_NOB];
1063 LIST_HEAD(gateways);
1064 struct list_head *tmp1;
1065 struct list_head *tmp2;
1067 struct lnet_nid nid;
1068 struct lnet_text_buf *ltb = NULL;
1069 struct lnet_text_buf *ltb1, *ltb2;
1077 unsigned int priority = 0;
1079 /* save a copy of the string for error messages */
1080 strncpy(cmd, str, sizeof(cmd));
1081 cmd[sizeof(cmd) - 1] = '\0';
1085 /* scan for token start */
1086 while (isspace(*sep))
1089 if (ntokens < (got_hops ? 3 : 2))
1097 /* scan for token end */
1098 while (*sep != 0 && !isspace(*sep))
1104 tmp2 = &nets; /* expanding nets */
1105 } else if (ntokens == 2 &&
1106 lnet_parse_hops(token, &hops)) {
1107 got_hops = 1; /* got a hop count */
1110 tmp2 = &gateways; /* expanding gateways */
1113 ltb = lnet_new_text_buf(strlen(token));
1117 strcpy(ltb->ltb_text, token);
1118 tmp1 = <b->ltb_list;
1119 list_add_tail(tmp1, tmp2);
1121 while (tmp1 != tmp2) {
1122 ltb = list_entry(tmp1, struct lnet_text_buf, ltb_list);
1124 rc = lnet_str2tbs_expand(tmp1->next, ltb->ltb_text);
1130 if (rc > 0) { /* expanded! */
1131 list_del(<b->ltb_list);
1132 lnet_free_text_buf(ltb);
1137 net = libcfs_str2net(ltb->ltb_text);
1138 if (net == LNET_NET_ANY ||
1139 LNET_NETTYP(net) == LOLND)
1142 rc = lnet_parse_priority(ltb->ltb_text,
1147 if (libcfs_strnid(&nid, ltb->ltb_text) != 0 ||
1154 /* if there are no hops set then we want to flag this value as
1155 * unset since hops is an optional parameter */
1157 hops = LNET_UNDEFINED_HOPS;
1159 LASSERT(!list_empty(&nets));
1160 LASSERT(!list_empty(&gateways));
1162 list_for_each_entry(ltb1, &nets, ltb_list) {
1163 net = libcfs_str2net(ltb1->ltb_text);
1164 LASSERT(net != LNET_NET_ANY);
1166 list_for_each_entry(ltb2, &gateways, ltb_list) {
1167 LASSERT(libcfs_strnid(&nid, ltb2->ltb_text) == 0);
1169 if (lnet_islocalnid(&nid)) {
1174 rc = lnet_add_route(net, hops, &nid, priority, 1);
1175 if (rc != 0 && rc != -EEXIST && rc != -EHOSTUNREACH) {
1176 CERROR("Can't create route "
1178 libcfs_net2str(net),
1179 libcfs_nidstr(&nid));
1189 lnet_syntax("routes", cmd, (int)(token - str), strlen(token));
1191 lnet_free_text_bufs(&nets);
1192 lnet_free_text_bufs(&gateways);
1197 lnet_parse_route_tbs(struct list_head *tbs, int *im_a_router)
1199 struct lnet_text_buf *ltb;
1201 while ((ltb = list_first_entry_or_null(tbs, struct lnet_text_buf,
1202 ltb_list)) != NULL) {
1203 if (lnet_parse_route(ltb->ltb_text, im_a_router) < 0) {
1204 lnet_free_text_bufs(tbs);
1208 list_del(<b->ltb_list);
1209 lnet_free_text_buf(ltb);
1216 lnet_parse_routes(const char *routes, int *im_a_router)
1223 if (lnet_str2tbs_sep(&tbs, routes) < 0) {
1224 CERROR("Error parsing routes\n");
1227 rc = lnet_parse_route_tbs(&tbs, im_a_router);
1230 LASSERT (lnet_tbnob == 0);
1235 lnet_match_network_token(char *token, __u32 *ipaddrs, int nip)
1241 rc = cfs_ip_addr_parse(token, 0, &list);
1245 for (rc = i = 0; !rc && i < nip; i++)
1246 rc = cfs_ip_addr_match(ipaddrs[i], &list);
1248 cfs_expr_list_free_list(&list);
1254 lnet_match_network_tokens(char *net_entry, __u32 *ipaddrs, int nip)
1256 static char tokens[LNET_SINGLE_TEXTBUF_NOB];
1265 LASSERT(strlen(net_entry) < sizeof(tokens));
1267 /* work on a copy of the string */
1268 strcpy(tokens, net_entry);
1271 /* scan for token start */
1272 sep = skip_spaces(sep);
1276 token = strsep(&sep, " \t\n\r\v\f");
1278 if (ntokens++ == 0) {
1283 rc = lnet_match_network_token(token, ipaddrs, nip);
1285 lnet_syntax("ip2nets", net_entry,
1286 (int)(token - tokens), strlen(token));
1290 matched |= (rc != 0);
1296 strcpy(net_entry, net); /* replace with matched net */
1301 lnet_netspec2net(char *netspec)
1303 char *bracket = strchr(netspec, '(');
1306 if (bracket != NULL)
1309 net = libcfs_str2net(netspec);
1311 if (bracket != NULL)
1318 lnet_splitnets(char *source, struct list_head *nets)
1323 struct lnet_text_buf *tb;
1324 struct lnet_text_buf *tb2;
1329 LASSERT(!list_empty(nets));
1330 LASSERT(nets->next == nets->prev); /* single entry */
1332 tb = list_first_entry(nets, struct lnet_text_buf, ltb_list);
1335 sep = strchr(tb->ltb_text, ',');
1336 bracket = strchr(tb->ltb_text, '(');
1341 /* netspec lists interfaces... */
1343 offset2 = offset + (int)(bracket - tb->ltb_text);
1344 len = strlen(bracket);
1346 bracket = strchr(bracket + 1, ')');
1348 if (bracket == NULL ||
1349 !(bracket[1] == ',' || bracket[1] == 0)) {
1350 lnet_syntax("ip2nets", source, offset2, len);
1354 sep = (bracket[1] == 0) ? NULL : bracket + 1;
1360 net = lnet_netspec2net(tb->ltb_text);
1361 if (net == LNET_NET_ANY) {
1362 lnet_syntax("ip2nets", source, offset,
1363 strlen(tb->ltb_text));
1367 list_for_each_entry(tb2, nets, ltb_list) {
1371 if (net == lnet_netspec2net(tb2->ltb_text)) {
1372 /* duplicate network */
1373 lnet_syntax("ip2nets", source, offset,
1374 strlen(tb->ltb_text));
1382 offset += (int)(sep - tb->ltb_text);
1384 tb2 = lnet_new_text_buf(len);
1388 strncpy(tb2->ltb_text, sep, len);
1389 tb2->ltb_text[len] = '\0';
1390 list_add_tail(&tb2->ltb_list, nets);
1397 lnet_match_networks(const char **networksp, const char *ip2nets,
1398 __u32 *ipaddrs, int nip)
1400 static char networks[LNET_SINGLE_TEXTBUF_NOB];
1401 static char source[LNET_SINGLE_TEXTBUF_NOB];
1403 LIST_HEAD(raw_entries);
1404 LIST_HEAD(matched_nets);
1405 LIST_HEAD(current_nets);
1406 struct list_head *t;
1407 struct list_head *t2;
1408 struct lnet_text_buf *tb;
1413 if (lnet_str2tbs_sep(&raw_entries, ip2nets) < 0) {
1414 CERROR("Error parsing ip2nets\n");
1415 LASSERT(lnet_tbnob == 0);
1424 while ((tb = list_first_entry_or_null(&raw_entries,
1425 struct lnet_text_buf,
1426 ltb_list)) != NULL) {
1427 strncpy(source, tb->ltb_text, sizeof(source));
1428 source[sizeof(source) - 1] = '\0';
1430 /* replace ltb_text with the network(s) add on match */
1431 rc = lnet_match_network_tokens(tb->ltb_text, ipaddrs, nip);
1435 list_del(&tb->ltb_list);
1437 if (rc == 0) { /* no match */
1438 lnet_free_text_buf(tb);
1442 /* split into separate networks */
1443 INIT_LIST_HEAD(¤t_nets);
1444 list_add(&tb->ltb_list, ¤t_nets);
1445 rc = lnet_splitnets(source, ¤t_nets);
1449 list_for_each_safe(t, t2, ¤t_nets) {
1450 tb = list_entry(t, struct lnet_text_buf, ltb_list);
1452 list_move_tail(&tb->ltb_list, &matched_nets);
1454 len += scnprintf(networks + len, sizeof(networks) - len,
1455 "%s%s", (len == 0) ? "" : ",",
1458 if (len >= sizeof(networks)) {
1459 CERROR("Too many matched networks\n");
1469 lnet_free_text_bufs(&raw_entries);
1470 lnet_free_text_bufs(&matched_nets);
1471 lnet_free_text_bufs(¤t_nets);
1472 LASSERT(lnet_tbnob == 0);
1477 *networksp = networks;
1481 __u32 lnet_set_link_fatal_state(struct lnet_ni *ni, unsigned int link_state)
1483 CDEBUG(D_NET, "%s: set link fatal state to %u\n",
1484 libcfs_nidstr(&ni->ni_nid), link_state);
1485 return atomic_xchg(&ni->ni_fatal_error_on, link_state);
1487 EXPORT_SYMBOL(lnet_set_link_fatal_state);
1489 int lnet_get_link_status(struct net_device *dev)
1496 if (!netif_running(dev)) {
1498 CDEBUG(D_NET, "device idx %d not running\n", dev->ifindex);
1500 /* Some devices may not be providing link settings */
1501 else if (dev->ethtool_ops->get_link) {
1502 ret = dev->ethtool_ops->get_link(dev);
1503 CDEBUG(D_NET, "device idx %d get_link %u\n",
1510 EXPORT_SYMBOL(lnet_get_link_status);
1512 int lnet_inet_select(struct lnet_ni *ni,
1513 struct lnet_inetdev *ifaces,
1516 bool addr_set = nid_addr_is_set(&ni->ni_nid);
1519 /* default to first interface if both interface and NID unspecified */
1520 if (!ni->ni_interface && !addr_set)
1523 for (if_idx = 0; if_idx < num_ifaces; if_idx++) {
1524 if (ni->ni_interface && strlen(ni->ni_interface) &&
1525 strcmp(ni->ni_interface, ifaces[if_idx].li_name) != 0)
1526 /* not the specified interface */
1530 /* IP unspecified, use IP of first matching interface */
1533 if (ifaces[if_idx].li_size == NID_ADDR_BYTES(&ni->ni_nid)) {
1534 char *addr = (char *)&ifaces[if_idx].li_ipaddr;
1536 if (ifaces[if_idx].li_size != 4)
1537 addr = (char *)ifaces[if_idx].li_ipv6addr;
1539 if (memcmp(ni->ni_nid.nid_addr, addr,
1540 ifaces[if_idx].li_size) == 0)
1545 if (if_idx < num_ifaces)
1549 CERROR("%s: failed to find IP address %s\n",
1550 libcfs_lnd2modname(ni->ni_nid.nid_type),
1551 libcfs_nidstr(&ni->ni_nid));
1552 else if (ni->ni_interface)
1553 CERROR("%s: failed to find interface %s%s%s\n",
1554 libcfs_lnd2modname(ni->ni_nid.nid_type),
1555 ni->ni_interface, addr_set ? "@" : "",
1556 addr_set ? libcfs_nidstr(&ni->ni_nid) : "");
1560 EXPORT_SYMBOL(lnet_inet_select);
1563 lnet_parse_ip2nets(const char **networksp, const char *ip2nets)
1565 struct lnet_inetdev *ifaces = NULL;
1566 u32 *ipaddrs = NULL;
1571 if (current->nsproxy && current->nsproxy->net_ns)
1572 nip = lnet_inet_enumerate(&ifaces, current->nsproxy->net_ns,
1573 the_lnet.ln_nis_use_large_nids);
1575 nip = lnet_inet_enumerate(&ifaces, &init_net,
1576 the_lnet.ln_nis_use_large_nids);
1578 if (nip != -ENOENT) {
1579 LCONSOLE_ERROR("Error %d enumerating local IP interfaces for ip2nets to match\n",
1582 LCONSOLE_ERROR("No local IP interfaces for ip2nets to match\n");
1587 CFS_ALLOC_PTR_ARRAY(ipaddrs, nip);
1590 CERROR("lnet: Can't allocate ipaddrs[%d], rc = %d\n",
1592 goto out_free_addrs;
1595 for (i = 0; i < nip; i++)
1596 ipaddrs[i] = ntohl(ifaces[i].li_ipaddr);
1598 rc = lnet_match_networks(networksp, ip2nets, ipaddrs, nip);
1600 LCONSOLE_ERROR("Error %d parsing ip2nets\n", rc);
1601 } else if (rc == 0) {
1602 LCONSOLE_ERROR("ip2nets does not match any local IP interfaces\n");
1605 CFS_FREE_PTR_ARRAY(ipaddrs, nip);
1608 return rc > 0 ? 0 : rc;