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, 2016, 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
34 #include <linux/nsproxy.h>
35 #include <net/net_namespace.h>
36 #include <lnet/lib-lnet.h>
38 /* tmp struct for parsing routes */
39 struct lnet_text_buf {
40 struct list_head ltb_list; /* stash on lists */
41 int ltb_size; /* allocated size */
42 char ltb_text[0]; /* text buffer */
45 static int lnet_tbnob = 0; /* track text buf allocation */
46 #define LNET_MAX_TEXTBUF_NOB (64<<10) /* bound allocation */
47 #define LNET_SINGLE_TEXTBUF_NOB (4<<10)
49 #define SPACESTR " \t\v\r\n"
50 #define DELIMITERS ":()[]"
53 lnet_syntax(const char *name, const char *str, int offset, int width)
55 static char dots[LNET_SINGLE_TEXTBUF_NOB];
56 static char dashes[LNET_SINGLE_TEXTBUF_NOB];
58 memset(dots, '.', sizeof(dots));
59 dots[sizeof(dots)-1] = 0;
60 memset(dashes, '-', sizeof(dashes));
61 dashes[sizeof(dashes)-1] = 0;
63 LCONSOLE_ERROR_MSG(0x10f, "Error parsing '%s=\"%s\"'\n", name, str);
64 LCONSOLE_ERROR_MSG(0x110, "here...........%.*s..%.*s|%.*s|\n",
65 (int)strlen(name), dots, offset, dots,
66 (width < 1) ? 0 : width - 1, dashes);
83 lnet_net_unique(__u32 net_id, struct list_head *netlist,
84 struct lnet_net **net)
86 struct lnet_net *net_l;
88 list_for_each_entry(net_l, netlist, net_list) {
89 if (net_l->net_id == net_id) {
99 /* check that the NI is unique within the list of NIs already added to
102 lnet_ni_unique_net(struct list_head *nilist, char *iface)
104 struct list_head *tmp;
107 list_for_each(tmp, nilist) {
108 ni = list_entry(tmp, struct lnet_ni, ni_netlist);
110 if (ni->ni_interfaces[0] != NULL &&
111 strncmp(ni->ni_interfaces[0], iface, strlen(iface)) == 0)
118 /* check that the NI is unique to the interfaces with in the same NI.
119 * This is only a consideration if use_tcp_bonding is set */
121 lnet_ni_unique_ni(char *iface_list[LNET_MAX_INTERFACES], char *iface)
124 for (i = 0; i < LNET_MAX_INTERFACES; i++) {
125 if (iface_list[i] != NULL &&
126 strncmp(iface_list[i], iface, strlen(iface)) == 0)
134 in_array(__u32 *array, __u32 size, __u32 value)
138 for (i = 0; i < size; i++) {
139 if (array[i] == value)
147 lnet_net_append_cpts(__u32 *cpts, __u32 ncpts, struct lnet_net *net)
149 __u32 *added_cpts = NULL;
150 int i, j = 0, rc = 0;
153 * no need to go futher since a subset of the NIs already exist on
156 if (net->net_ncpts == LNET_CPT_NUMBER)
160 /* there is an NI which will exist on all CPTs */
161 if (net->net_cpts != NULL)
162 LIBCFS_FREE(net->net_cpts, sizeof(*net->net_cpts) *
164 net->net_cpts = NULL;
165 net->net_ncpts = LNET_CPT_NUMBER;
169 if (net->net_cpts == NULL) {
170 LIBCFS_ALLOC(net->net_cpts, sizeof(*net->net_cpts) * ncpts);
171 if (net->net_cpts == NULL)
173 memcpy(net->net_cpts, cpts, ncpts);
177 LIBCFS_ALLOC(added_cpts, sizeof(*added_cpts) * LNET_CPT_NUMBER);
178 if (added_cpts == NULL)
181 for (i = 0; i < ncpts; i++) {
182 if (!in_array(net->net_cpts, net->net_ncpts, cpts[i])) {
183 added_cpts[j] = cpts[i];
188 /* append the new cpts if any to the list of cpts in the net */
190 __u32 *array = NULL, *loc;
191 __u32 total_entries = j + net->net_ncpts;
193 LIBCFS_ALLOC(array, sizeof(*net->net_cpts) * total_entries);
199 memcpy(array, net->net_cpts, net->net_ncpts);
200 loc = array + net->net_ncpts;
201 memcpy(loc, added_cpts, j);
203 LIBCFS_FREE(net->net_cpts, sizeof(*net->net_cpts) *
205 net->net_ncpts = total_entries;
206 net->net_cpts = array;
210 LIBCFS_FREE(added_cpts, sizeof(*added_cpts) * LNET_CPT_NUMBER);
216 lnet_net_remove_cpts(__u32 *cpts, __u32 ncpts, struct lnet_net *net)
222 * Operation Assumption:
223 * This function is called after an NI has been removed from
226 * if we're removing an NI which exists on all CPTs then
227 * we have to check if any of the other NIs on this net also
228 * exists on all CPTs. If none, then we need to build our Net CPT
229 * list based on the remaining NIs.
231 * If the NI being removed exist on a subset of the CPTs then we
232 * alo rebuild the Net CPT list based on the remaining NIs, which
233 * should resutl in the expected Net CPT list.
237 * sometimes this function can be called due to some failure
238 * creating an NI, before any of the cpts are allocated, so check
239 * for that case and don't do anything
244 if (ncpts == LNET_CPT_NUMBER) {
246 * first iteration through the NI list in the net to see
247 * if any of the NIs exist on all the CPTs. If one is
248 * found then our job is done.
250 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
251 if (ni->ni_ncpts == LNET_CPT_NUMBER)
257 * Rebuild the Net CPT list again, thereby only including only the
258 * CPTs which the remaining NIs are associated with.
260 if (net->net_cpts != NULL) {
261 LIBCFS_FREE(net->net_cpts,
262 sizeof(*net->net_cpts) * net->net_ncpts);
263 net->net_cpts = NULL;
266 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
267 rc = lnet_net_append_cpts(ni->ni_cpts, ni->ni_ncpts,
270 CERROR("Out of Memory\n");
272 * do our best to keep on going. Delete
273 * the net cpts and set it to NULL. This
274 * way we can keep on going but less
275 * efficiently, since memory accesses might be
278 if (net->net_cpts != NULL) {
279 LIBCFS_FREE(net->net_cpts,
280 sizeof(*net->net_cpts) *
282 net->net_cpts = NULL;
283 net->net_ncpts = LNET_CPT_NUMBER;
291 lnet_ni_free(struct lnet_ni *ni)
295 lnet_net_remove_cpts(ni->ni_cpts, ni->ni_ncpts, ni->ni_net);
297 if (ni->ni_refs != NULL)
298 cfs_percpt_free(ni->ni_refs);
300 if (ni->ni_tx_queues != NULL)
301 cfs_percpt_free(ni->ni_tx_queues);
303 if (ni->ni_cpts != NULL)
304 cfs_expr_list_values_free(ni->ni_cpts, ni->ni_ncpts);
306 for (i = 0; i < LNET_MAX_INTERFACES &&
307 ni->ni_interfaces[i] != NULL; i++) {
308 LIBCFS_FREE(ni->ni_interfaces[i],
309 strlen(ni->ni_interfaces[i]) + 1);
312 /* release reference to net namespace */
313 if (ni->ni_net_ns != NULL)
314 put_net(ni->ni_net_ns);
316 LIBCFS_FREE(ni, sizeof(*ni));
320 lnet_net_free(struct lnet_net *net)
322 struct list_head *tmp, *tmp2;
325 LASSERT(list_empty(&net->net_ni_zombie));
328 * delete any nis that haven't been added yet. This could happen
329 * if there is a failure on net startup
331 list_for_each_safe(tmp, tmp2, &net->net_ni_added) {
332 ni = list_entry(tmp, struct lnet_ni, ni_netlist);
333 list_del_init(&ni->ni_netlist);
337 /* delete any nis which have been started. */
338 list_for_each_safe(tmp, tmp2, &net->net_ni_list) {
339 ni = list_entry(tmp, struct lnet_ni, ni_netlist);
340 list_del_init(&ni->ni_netlist);
344 if (net->net_cpts != NULL)
345 LIBCFS_FREE(net->net_cpts,
346 sizeof(*net->net_cpts) * net->net_ncpts);
348 LIBCFS_FREE(net, sizeof(*net));
352 lnet_net_alloc(__u32 net_id, struct list_head *net_list)
354 struct lnet_net *net;
356 if (!lnet_net_unique(net_id, net_list, NULL)) {
357 CERROR("Duplicate net %s. Ignore\n",
358 libcfs_net2str(net_id));
362 LIBCFS_ALLOC(net, sizeof(*net));
364 CERROR("Out of memory creating network %s\n",
365 libcfs_net2str(net_id));
369 INIT_LIST_HEAD(&net->net_list);
370 INIT_LIST_HEAD(&net->net_ni_list);
371 INIT_LIST_HEAD(&net->net_ni_added);
372 INIT_LIST_HEAD(&net->net_ni_zombie);
374 net->net_id = net_id;
375 net->net_state = LNET_NET_STATE_INIT;
377 /* initialize global paramters to undefiend */
378 net->net_tunables.lct_peer_timeout = -1;
379 net->net_tunables.lct_max_tx_credits = -1;
380 net->net_tunables.lct_peer_tx_credits = -1;
381 net->net_tunables.lct_peer_rtr_credits = -1;
383 list_add_tail(&net->net_list, net_list);
389 lnet_ni_add_interface(struct lnet_ni *ni, char *iface)
396 if (!lnet_ni_unique_ni(ni->ni_interfaces, iface))
399 /* Allocate a separate piece of memory and copy
400 * into it the string, so we don't have
401 * a depencency on the tokens string. This way we
402 * can free the tokens at the end of the function.
403 * The newly allocated ni_interfaces[] can be
404 * freed when freeing the NI */
405 while (niface < LNET_MAX_INTERFACES &&
406 ni->ni_interfaces[niface] != NULL)
409 if (niface >= LNET_MAX_INTERFACES) {
410 LCONSOLE_ERROR_MSG(0x115, "Too many interfaces "
412 libcfs_net2str(LNET_NIDNET(ni->ni_nid)));
416 LIBCFS_ALLOC(ni->ni_interfaces[niface],
419 if (ni->ni_interfaces[niface] == NULL) {
420 CERROR("Can't allocate net interface name\n");
424 strncpy(ni->ni_interfaces[niface], iface,
430 /* allocate and add to the provided network */
432 lnet_ni_alloc(struct lnet_net *net, struct cfs_expr_list *el, char *iface)
434 struct lnet_tx_queue *tq;
440 /* make sure that this NI is unique in the net it's
442 if (!lnet_ni_unique_net(&net->net_ni_added, iface))
445 LIBCFS_ALLOC(ni, sizeof(*ni));
447 CERROR("Out of memory creating network interface %s%s\n",
448 libcfs_net2str(net->net_id),
449 (iface != NULL) ? iface : "");
453 spin_lock_init(&ni->ni_lock);
454 INIT_LIST_HEAD(&ni->ni_cptlist);
455 INIT_LIST_HEAD(&ni->ni_netlist);
456 ni->ni_refs = cfs_percpt_alloc(lnet_cpt_table(),
457 sizeof(*ni->ni_refs[0]));
458 if (ni->ni_refs == NULL)
461 ni->ni_tx_queues = cfs_percpt_alloc(lnet_cpt_table(),
462 sizeof(*ni->ni_tx_queues[0]));
463 if (ni->ni_tx_queues == NULL)
466 cfs_percpt_for_each(tq, i, ni->ni_tx_queues)
467 INIT_LIST_HEAD(&tq->tq_delayed);
471 ni->ni_ncpts = LNET_CPT_NUMBER;
473 rc = cfs_expr_list_values(el, LNET_CPT_NUMBER, &ni->ni_cpts);
475 CERROR("Failed to set CPTs for NI %s(%s): %d\n",
476 libcfs_net2str(net->net_id),
477 (iface != NULL) ? iface : "", rc);
481 LASSERT(rc <= LNET_CPT_NUMBER);
482 if (rc == LNET_CPT_NUMBER) {
483 LIBCFS_FREE(ni->ni_cpts, rc * sizeof(ni->ni_cpts[0]));
491 /* LND will fill in the address part of the NID */
492 ni->ni_nid = LNET_MKNID(net->net_id, 0);
494 /* Store net namespace in which current ni is being created */
495 if (current->nsproxy->net_ns != NULL)
496 ni->ni_net_ns = get_net(current->nsproxy->net_ns);
498 ni->ni_net_ns = NULL;
500 ni->ni_last_alive = ktime_get_real_seconds();
501 ni->ni_state = LNET_NI_STATE_INIT;
502 rc = lnet_net_append_cpts(ni->ni_cpts, ni->ni_ncpts, net);
505 list_add_tail(&ni->ni_netlist, &net->net_ni_added);
507 /* if an interface name is provided then make sure to add in that
508 * interface name in NI */
510 if (lnet_ni_add_interface(ni, iface) != 0)
520 * Parse the networks string and create the matching set of NIs on the
524 lnet_parse_networks(struct list_head *netlist, char *networks,
525 bool use_tcp_bonding)
527 struct cfs_expr_list *net_el = NULL;
528 struct cfs_expr_list *ni_el = NULL;
532 struct lnet_net *net;
533 struct lnet_ni *ni = NULL;
537 if (networks == NULL) {
538 CERROR("networks string is undefined\n");
542 if (strlen(networks) > LNET_SINGLE_TEXTBUF_NOB) {
543 /* _WAY_ conservative */
544 LCONSOLE_ERROR_MSG(0x112, "Can't parse networks: string too "
549 tokensize = strlen(networks) + 1;
551 LIBCFS_ALLOC(tokens, tokensize);
552 if (tokens == NULL) {
553 CERROR("Can't allocate net tokens\n");
557 memcpy(tokens, networks, tokensize);
563 * NB we don't check interface conflicts here; it's the LNDs
564 * responsibility (if it cares at all)
573 * Parse a network string into its components.
575 * <name>{"("...")"}{"["<el>"]"}
578 /* Network name (mandatory) */
579 while (isspace(*str))
584 str += strcspn(str, SPACESTR ":()[],");
585 while (isspace(*str))
588 /* Interface list (optional) */
592 str += strcspn(str, ")");
599 } while (isspace(*str));
604 /* CPT expression (optional) */
607 str += strcspn(str, "]");
612 rc = cfs_expr_list_parse(elstr, str - elstr + 1,
613 0, LNET_CPT_NUMBER - 1,
622 } while (isspace(*str));
626 if (*str && (strchr(DELIMITERS, *str) != NULL))
629 /* go to the next net if it exits */
630 str += strcspn(str, ",");
635 * At this point the name is properly terminated.
637 net_id = libcfs_str2net(name);
638 if (net_id == LNET_NIDNET(LNET_NID_ANY)) {
639 LCONSOLE_ERROR_MSG(0x113,
640 "Unrecognised network type\n");
645 if (LNET_NETTYP(net_id) == LOLND) {
646 /* Loopback is implicit, and there can be only one. */
648 cfs_expr_list_free(net_el);
651 /* Should we error out instead? */
656 * All network paramaters are now known.
660 /* always allocate a net, since we will eventually add an
661 * interface to it, or we will fail, in which case we'll
663 net = lnet_net_alloc(net_id, netlist);
664 if (IS_ERR_OR_NULL(net))
668 (use_tcp_bonding && LNET_NETTYP(net_id) == SOCKLND)) {
670 * No interface list was specified, allocate a
671 * ni using the defaults.
673 ni = lnet_ni_alloc(net, net_el, NULL);
674 if (IS_ERR_OR_NULL(ni))
679 cfs_expr_list_free(net_el);
689 /* Interface name (mandatory) */
690 while (isspace(*nistr))
693 nistr += strcspn(nistr, SPACESTR "[],");
694 while (isspace(*nistr))
697 /* CPT expression (optional) */
700 nistr += strcspn(nistr, "]");
705 rc = cfs_expr_list_parse(elstr,
707 0, LNET_CPT_NUMBER - 1,
716 } while (isspace(*nistr));
722 * End of single interface specificaton,
723 * advance to the start of the next one, if
729 } while (isspace(*nistr));
740 * At this point the name is properly terminated.
747 if (use_tcp_bonding &&
748 LNET_NETTYP(net->net_id) == SOCKLND) {
749 rc = lnet_ni_add_interface(ni, name);
753 ni = lnet_ni_alloc(net, ni_el, name);
754 if (IS_ERR_OR_NULL(ni))
759 if (ni_el != net_el) {
760 cfs_expr_list_free(ni_el);
767 cfs_expr_list_free(net_el);
772 LIBCFS_FREE(tokens, tokensize);
776 lnet_syntax("networks", networks, (int)(str - tokens), strlen(str));
778 /* free the net list and all the nis on each net */
779 while (!list_empty(netlist)) {
780 net = list_entry(netlist->next, struct lnet_net, net_list);
782 list_del_init(&net->net_list);
786 if (ni_el && ni_el != net_el)
787 cfs_expr_list_free(ni_el);
789 cfs_expr_list_free(net_el);
791 LIBCFS_FREE(tokens, tokensize);
796 static struct lnet_text_buf *lnet_new_text_buf(int str_len)
798 struct lnet_text_buf *ltb;
801 /* NB allocate space for the terminating 0 */
802 nob = offsetof(struct lnet_text_buf, ltb_text[str_len + 1]);
803 if (nob > LNET_SINGLE_TEXTBUF_NOB) {
804 /* _way_ conservative for "route net gateway..." */
805 CERROR("text buffer too big\n");
809 if (lnet_tbnob + nob > LNET_MAX_TEXTBUF_NOB) {
810 CERROR("Too many text buffers\n");
814 LIBCFS_ALLOC(ltb, nob);
819 ltb->ltb_text[0] = 0;
825 lnet_free_text_buf(struct lnet_text_buf *ltb)
827 lnet_tbnob -= ltb->ltb_size;
828 LIBCFS_FREE(ltb, ltb->ltb_size);
832 lnet_free_text_bufs(struct list_head *tbs)
834 struct lnet_text_buf *ltb;
836 while (!list_empty(tbs)) {
837 ltb = list_entry(tbs->next, struct lnet_text_buf, ltb_list);
839 list_del(<b->ltb_list);
840 lnet_free_text_buf(ltb);
845 lnet_print_text_bufs(struct list_head *tbs)
847 struct list_head *tmp;
848 struct lnet_text_buf *ltb;
850 list_for_each(tmp, tbs) {
851 ltb = list_entry(tmp, struct lnet_text_buf, ltb_list);
853 CDEBUG(D_WARNING, "%s\n", ltb->ltb_text);
856 CDEBUG(D_WARNING, "%d allocated\n", lnet_tbnob);
860 lnet_str2tbs_sep(struct list_head *tbs, char *str)
862 struct list_head pending;
866 struct lnet_text_buf *ltb;
868 INIT_LIST_HEAD(&pending);
870 /* Split 'str' into separate commands */
872 /* skip leading whitespace */
873 while (isspace(*str))
876 /* scan for separator or comment */
877 for (sep = str; *sep != 0; sep++)
878 if (lnet_issep(*sep) || *sep == '#')
881 nob = (int)(sep - str);
883 ltb = lnet_new_text_buf(nob);
885 lnet_free_text_bufs(&pending);
889 for (i = 0; i < nob; i++)
891 ltb->ltb_text[i] = ' ';
893 ltb->ltb_text[i] = str[i];
895 ltb->ltb_text[nob] = 0;
897 list_add_tail(<b->ltb_list, &pending);
901 /* scan for separator */
904 } while (*sep != 0 && !lnet_issep(*sep));
913 list_splice(&pending, tbs->prev);
918 lnet_expand1tb(struct list_head *list,
919 char *str, char *sep1, char *sep2,
920 char *item, int itemlen)
922 int len1 = (int)(sep1 - str);
923 int len2 = strlen(sep2 + 1);
924 struct lnet_text_buf *ltb;
926 LASSERT (*sep1 == '[');
927 LASSERT (*sep2 == ']');
929 ltb = lnet_new_text_buf(len1 + itemlen + len2);
933 memcpy(ltb->ltb_text, str, len1);
934 memcpy(<b->ltb_text[len1], item, itemlen);
935 memcpy(<b->ltb_text[len1+itemlen], sep2 + 1, len2);
936 ltb->ltb_text[len1 + itemlen + len2] = 0;
938 list_add_tail(<b->ltb_list, list);
943 lnet_str2tbs_expand(struct list_head *tbs, char *str)
946 struct list_head pending;
958 INIT_LIST_HEAD(&pending);
960 sep = strchr(str, '[');
961 if (sep == NULL) /* nothing to expand */
964 sep2 = strchr(sep, ']');
968 for (parsed = sep; parsed < sep2; parsed = enditem) {
971 while (enditem < sep2 && *enditem != ',')
974 if (enditem == parsed) /* no empty items */
977 if (sscanf(parsed, "%d-%d/%d%n", &lo, &hi, &stride, &scanned) < 3) {
979 if (sscanf(parsed, "%d-%d%n", &lo, &hi, &scanned) < 2) {
981 /* simple string enumeration */
982 if (lnet_expand1tb(&pending, str, sep, sep2,
983 parsed, (int)(enditem - parsed)) != 0)
992 /* range expansion */
994 if (enditem != parsed + scanned) /* no trailing junk */
997 if (hi < 0 || lo < 0 || stride < 0 || hi < lo ||
998 (hi - lo) % stride != 0)
1001 for (i = lo; i <= hi; i += stride) {
1003 snprintf(num, sizeof(num), "%d", i);
1005 if (nob + 1 == sizeof(num))
1008 if (lnet_expand1tb(&pending, str, sep, sep2,
1014 list_splice(&pending, tbs->prev);
1018 lnet_free_text_bufs(&pending);
1023 lnet_parse_hops (char *str, unsigned int *hops)
1025 int len = strlen(str);
1028 return (sscanf(str, "%u%n", hops, &nob) >= 1 &&
1030 *hops > 0 && *hops < 256);
1033 #define LNET_PRIORITY_SEPARATOR (':')
1036 lnet_parse_priority(char *str, unsigned int *priority, char **token)
1042 sep = strchr(str, LNET_PRIORITY_SEPARATOR);
1047 len = strlen(sep + 1);
1049 if ((sscanf((sep+1), "%u%n", priority, &nob) < 1) || (len != nob)) {
1050 /* Update the caller's token pointer so it treats the found
1051 priority as the token to report in the error message. */
1052 *token += sep - str + 1;
1056 CDEBUG(D_NET, "gateway %s, priority %d, nob %d\n", str, *priority, nob);
1059 * Change priority separator to \0 to be able to parse NID
1066 lnet_parse_route (char *str, int *im_a_router)
1068 /* static scratch buffer OK (single threaded) */
1069 static char cmd[LNET_SINGLE_TEXTBUF_NOB];
1071 struct list_head nets;
1072 struct list_head gateways;
1073 struct list_head *tmp1;
1074 struct list_head *tmp2;
1077 struct lnet_text_buf *ltb;
1085 unsigned int priority = 0;
1087 INIT_LIST_HEAD(&gateways);
1088 INIT_LIST_HEAD(&nets);
1090 /* save a copy of the string for error messages */
1091 strncpy(cmd, str, sizeof(cmd));
1092 cmd[sizeof(cmd) - 1] = '\0';
1096 /* scan for token start */
1097 while (isspace(*sep))
1100 if (ntokens < (got_hops ? 3 : 2))
1108 /* scan for token end */
1109 while (*sep != 0 && !isspace(*sep))
1115 tmp2 = &nets; /* expanding nets */
1116 } else if (ntokens == 2 &&
1117 lnet_parse_hops(token, &hops)) {
1118 got_hops = 1; /* got a hop count */
1121 tmp2 = &gateways; /* expanding gateways */
1124 ltb = lnet_new_text_buf(strlen(token));
1128 strcpy(ltb->ltb_text, token);
1129 tmp1 = <b->ltb_list;
1130 list_add_tail(tmp1, tmp2);
1132 while (tmp1 != tmp2) {
1133 ltb = list_entry(tmp1, struct lnet_text_buf, ltb_list);
1135 rc = lnet_str2tbs_expand(tmp1->next, ltb->ltb_text);
1141 if (rc > 0) { /* expanded! */
1142 list_del(<b->ltb_list);
1143 lnet_free_text_buf(ltb);
1148 net = libcfs_str2net(ltb->ltb_text);
1149 if (net == LNET_NIDNET(LNET_NID_ANY) ||
1150 LNET_NETTYP(net) == LOLND)
1153 rc = lnet_parse_priority(ltb->ltb_text,
1158 nid = libcfs_str2nid(ltb->ltb_text);
1159 if (nid == LNET_NID_ANY ||
1160 LNET_NETTYP(LNET_NIDNET(nid)) == LOLND)
1166 /* if there are no hops set then we want to flag this value as
1167 * unset since hops is an optional parameter */
1169 hops = LNET_UNDEFINED_HOPS;
1171 LASSERT(!list_empty(&nets));
1172 LASSERT(!list_empty(&gateways));
1174 list_for_each(tmp1, &nets) {
1175 ltb = list_entry(tmp1, struct lnet_text_buf, ltb_list);
1176 net = libcfs_str2net(ltb->ltb_text);
1177 LASSERT (net != LNET_NIDNET(LNET_NID_ANY));
1179 list_for_each(tmp2, &gateways) {
1180 ltb = list_entry(tmp2, struct lnet_text_buf, ltb_list);
1181 nid = libcfs_str2nid(ltb->ltb_text);
1182 LASSERT(nid != LNET_NID_ANY);
1184 if (lnet_islocalnid(nid)) {
1189 rc = lnet_add_route(net, hops, nid, priority);
1190 if (rc != 0 && rc != -EEXIST && rc != -EHOSTUNREACH) {
1191 CERROR("Can't create route "
1193 libcfs_net2str(net),
1194 libcfs_nid2str(nid));
1204 lnet_syntax("routes", cmd, (int)(token - str), strlen(token));
1206 lnet_free_text_bufs(&nets);
1207 lnet_free_text_bufs(&gateways);
1212 lnet_parse_route_tbs(struct list_head *tbs, int *im_a_router)
1214 struct lnet_text_buf *ltb;
1216 while (!list_empty(tbs)) {
1217 ltb = list_entry(tbs->next, struct lnet_text_buf, ltb_list);
1219 if (lnet_parse_route(ltb->ltb_text, im_a_router) < 0) {
1220 lnet_free_text_bufs(tbs);
1224 list_del(<b->ltb_list);
1225 lnet_free_text_buf(ltb);
1232 lnet_parse_routes (char *routes, int *im_a_router)
1234 struct list_head tbs;
1239 INIT_LIST_HEAD(&tbs);
1241 if (lnet_str2tbs_sep(&tbs, routes) < 0) {
1242 CERROR("Error parsing routes\n");
1245 rc = lnet_parse_route_tbs(&tbs, im_a_router);
1248 LASSERT (lnet_tbnob == 0);
1253 lnet_match_network_token(char *token, int len, __u32 *ipaddrs, int nip)
1255 struct list_head list = LIST_HEAD_INIT(list);
1259 rc = cfs_ip_addr_parse(token, len, &list);
1263 for (rc = i = 0; !rc && i < nip; i++)
1264 rc = cfs_ip_addr_match(ipaddrs[i], &list);
1266 cfs_expr_list_free_list(&list);
1272 lnet_match_network_tokens(char *net_entry, __u32 *ipaddrs, int nip)
1274 static char tokens[LNET_SINGLE_TEXTBUF_NOB];
1284 LASSERT(strlen(net_entry) < sizeof(tokens));
1286 /* work on a copy of the string */
1287 strcpy(tokens, net_entry);
1290 /* scan for token start */
1291 while (isspace(*sep))
1298 /* scan for token end */
1299 while (*sep != 0 && !isspace(*sep))
1304 if (ntokens++ == 0) {
1309 len = strlen(token);
1311 rc = lnet_match_network_token(token, len, ipaddrs, nip);
1313 lnet_syntax("ip2nets", net_entry,
1314 (int)(token - tokens), len);
1318 matched |= (rc != 0);
1324 strcpy(net_entry, net); /* replace with matched net */
1329 lnet_netspec2net(char *netspec)
1331 char *bracket = strchr(netspec, '(');
1334 if (bracket != NULL)
1337 net = libcfs_str2net(netspec);
1339 if (bracket != NULL)
1346 lnet_splitnets(char *source, struct list_head *nets)
1351 struct lnet_text_buf *tb;
1352 struct lnet_text_buf *tb2;
1353 struct list_head *t;
1358 LASSERT(!list_empty(nets));
1359 LASSERT(nets->next == nets->prev); /* single entry */
1361 tb = list_entry(nets->next, struct lnet_text_buf, ltb_list);
1364 sep = strchr(tb->ltb_text, ',');
1365 bracket = strchr(tb->ltb_text, '(');
1370 /* netspec lists interfaces... */
1372 offset2 = offset + (int)(bracket - tb->ltb_text);
1373 len = strlen(bracket);
1375 bracket = strchr(bracket + 1, ')');
1377 if (bracket == NULL ||
1378 !(bracket[1] == ',' || bracket[1] == 0)) {
1379 lnet_syntax("ip2nets", source, offset2, len);
1383 sep = (bracket[1] == 0) ? NULL : bracket + 1;
1389 net = lnet_netspec2net(tb->ltb_text);
1390 if (net == LNET_NIDNET(LNET_NID_ANY)) {
1391 lnet_syntax("ip2nets", source, offset,
1392 strlen(tb->ltb_text));
1396 list_for_each(t, nets) {
1397 tb2 = list_entry(t, struct lnet_text_buf, ltb_list);
1402 if (net == lnet_netspec2net(tb2->ltb_text)) {
1403 /* duplicate network */
1404 lnet_syntax("ip2nets", source, offset,
1405 strlen(tb->ltb_text));
1413 offset += (int)(sep - tb->ltb_text);
1415 tb2 = lnet_new_text_buf(len);
1419 strncpy(tb2->ltb_text, sep, len);
1420 tb2->ltb_text[len] = '\0';
1421 list_add_tail(&tb2->ltb_list, nets);
1428 lnet_match_networks (char **networksp, char *ip2nets, __u32 *ipaddrs, int nip)
1430 static char networks[LNET_SINGLE_TEXTBUF_NOB];
1431 static char source[LNET_SINGLE_TEXTBUF_NOB];
1433 struct list_head raw_entries;
1434 struct list_head matched_nets;
1435 struct list_head current_nets;
1436 struct list_head *t;
1437 struct list_head *t2;
1438 struct lnet_text_buf *tb;
1439 struct lnet_text_buf *tb2;
1447 INIT_LIST_HEAD(&raw_entries);
1448 if (lnet_str2tbs_sep(&raw_entries, ip2nets) < 0) {
1449 CERROR("Error parsing ip2nets\n");
1450 LASSERT(lnet_tbnob == 0);
1454 INIT_LIST_HEAD(&matched_nets);
1455 INIT_LIST_HEAD(¤t_nets);
1461 while (!list_empty(&raw_entries)) {
1462 tb = list_entry(raw_entries.next, struct lnet_text_buf,
1465 strncpy(source, tb->ltb_text, sizeof(source));
1466 source[sizeof(source) - 1] = '\0';
1468 /* replace ltb_text with the network(s) add on match */
1469 rc = lnet_match_network_tokens(tb->ltb_text, ipaddrs, nip);
1473 list_del(&tb->ltb_list);
1475 if (rc == 0) { /* no match */
1476 lnet_free_text_buf(tb);
1480 /* split into separate networks */
1481 INIT_LIST_HEAD(¤t_nets);
1482 list_add(&tb->ltb_list, ¤t_nets);
1483 rc = lnet_splitnets(source, ¤t_nets);
1488 list_for_each(t, ¤t_nets) {
1489 tb = list_entry(t, struct lnet_text_buf, ltb_list);
1490 net1 = lnet_netspec2net(tb->ltb_text);
1491 LASSERT(net1 != LNET_NIDNET(LNET_NID_ANY));
1493 list_for_each(t2, &matched_nets) {
1494 tb2 = list_entry(t2, struct lnet_text_buf,
1496 net2 = lnet_netspec2net(tb2->ltb_text);
1497 LASSERT(net2 != LNET_NIDNET(LNET_NID_ANY));
1510 lnet_free_text_bufs(¤t_nets);
1514 list_for_each_safe(t, t2, ¤t_nets) {
1515 tb = list_entry(t, struct lnet_text_buf, ltb_list);
1517 list_del(&tb->ltb_list);
1518 list_add_tail(&tb->ltb_list, &matched_nets);
1520 len += snprintf(networks + len, sizeof(networks) - len,
1521 "%s%s", (len == 0) ? "" : ",",
1524 if (len >= sizeof(networks)) {
1525 CERROR("Too many matched networks\n");
1535 lnet_free_text_bufs(&raw_entries);
1536 lnet_free_text_bufs(&matched_nets);
1537 lnet_free_text_bufs(¤t_nets);
1538 LASSERT(lnet_tbnob == 0);
1543 *networksp = networks;
1548 lnet_ipaddr_free_enumeration(__u32 *ipaddrs, int nip)
1550 LIBCFS_FREE(ipaddrs, nip * sizeof(*ipaddrs));
1554 lnet_ipaddr_enumerate (__u32 **ipaddrsp)
1562 int nif = lnet_ipif_enumerate(&ifnames);
1569 LIBCFS_ALLOC(ipaddrs, nif * sizeof(*ipaddrs));
1570 if (ipaddrs == NULL) {
1571 CERROR("Can't allocate ipaddrs[%d]\n", nif);
1572 lnet_ipif_free_enumeration(ifnames, nif);
1576 for (i = nip = 0; i < nif; i++) {
1577 if (!strcmp(ifnames[i], "lo"))
1580 rc = lnet_ipif_query(ifnames[i], &up,
1581 &ipaddrs[nip], &netmask);
1583 CWARN("Can't query interface %s: %d\n",
1589 CWARN("Ignoring interface %s: it's down\n",
1597 lnet_ipif_free_enumeration(ifnames, nif);
1600 *ipaddrsp = ipaddrs;
1603 LIBCFS_ALLOC(ipaddrs2, nip * sizeof(*ipaddrs2));
1604 if (ipaddrs2 == NULL) {
1605 CERROR("Can't allocate ipaddrs[%d]\n", nip);
1608 memcpy(ipaddrs2, ipaddrs,
1609 nip * sizeof(*ipaddrs));
1610 *ipaddrsp = ipaddrs2;
1614 lnet_ipaddr_free_enumeration(ipaddrs, nif);
1620 lnet_parse_ip2nets (char **networksp, char *ip2nets)
1622 __u32 *ipaddrs = NULL;
1623 int nip = lnet_ipaddr_enumerate(&ipaddrs);
1627 LCONSOLE_ERROR_MSG(0x117, "Error %d enumerating local IP "
1628 "interfaces for ip2nets to match\n", nip);
1633 LCONSOLE_ERROR_MSG(0x118, "No local IP interfaces "
1634 "for ip2nets to match\n");
1638 rc = lnet_match_networks(networksp, ip2nets, ipaddrs, nip);
1639 lnet_ipaddr_free_enumeration(ipaddrs, nip);
1642 LCONSOLE_ERROR_MSG(0x119, "Error %d parsing ip2nets\n", rc);
1647 LCONSOLE_ERROR_MSG(0x11a, "ip2nets does not match "
1648 "any local IP interfaces\n");