4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 #define DEBUG_SUBSYSTEM S_LNET
35 #include <linux/ctype.h>
36 #include <linux/inetdevice.h>
37 #include <linux/nsproxy.h>
38 #include <net/net_namespace.h>
39 #include <lnet/lib-lnet.h>
41 /* tmp struct for parsing routes */
42 struct lnet_text_buf {
43 struct list_head ltb_list; /* stash on lists */
44 int ltb_size; /* allocated size */
45 char ltb_text[0]; /* text buffer */
48 static int lnet_tbnob = 0; /* track text buf allocation */
49 #define LNET_MAX_TEXTBUF_NOB (64<<10) /* bound allocation */
50 #define LNET_SINGLE_TEXTBUF_NOB (4<<10)
52 #define SPACESTR " \t\v\r\n"
53 #define DELIMITERS ":()[]"
56 lnet_syntax(const char *name, const char *str, int offset, int width)
58 static char dots[LNET_SINGLE_TEXTBUF_NOB];
59 static char dashes[LNET_SINGLE_TEXTBUF_NOB];
61 memset(dots, '.', sizeof(dots));
62 dots[sizeof(dots)-1] = 0;
63 memset(dashes, '-', sizeof(dashes));
64 dashes[sizeof(dashes)-1] = 0;
66 LCONSOLE_ERROR_MSG(0x10f, "Error parsing '%s=\"%s\"'\n", name, str);
67 LCONSOLE_ERROR_MSG(0x110, "here...........%.*s..%.*s|%.*s|\n",
68 (int)strlen(name), dots, offset, dots,
69 (width < 1) ? 0 : width - 1, dashes);
86 lnet_net_unique(__u32 net_id, struct list_head *netlist,
87 struct lnet_net **net)
89 struct lnet_net *net_l;
94 list_for_each_entry(net_l, netlist, net_list) {
95 if (net_l->net_id == net_id) {
105 /* check that the NI is unique within the list of NIs already added to
108 lnet_ni_unique_net(struct list_head *nilist, char *iface)
110 struct list_head *tmp;
113 list_for_each(tmp, nilist) {
114 ni = list_entry(tmp, struct lnet_ni, ni_netlist);
116 if (ni->ni_interfaces[0] != NULL &&
117 strncmp(ni->ni_interfaces[0], iface, strlen(iface)) == 0)
124 /* check that the NI is unique to the interfaces with in the same NI.
125 * This is only a consideration if use_tcp_bonding is set */
127 lnet_ni_unique_ni(char *iface_list[LNET_INTERFACES_NUM], char *iface)
130 for (i = 0; i < LNET_INTERFACES_NUM; i++) {
131 if (iface_list[i] != NULL &&
132 strncmp(iface_list[i], iface, strlen(iface)) == 0)
140 in_array(__u32 *array, __u32 size, __u32 value)
144 for (i = 0; i < size; i++) {
145 if (array[i] == value)
153 lnet_net_append_cpts(__u32 *cpts, __u32 ncpts, struct lnet_net *net)
155 __u32 *added_cpts = NULL;
156 int i, j = 0, rc = 0;
159 * no need to go futher since a subset of the NIs already exist on
162 if (net->net_ncpts == LNET_CPT_NUMBER)
166 /* there is an NI which will exist on all CPTs */
167 if (net->net_cpts != NULL)
168 LIBCFS_FREE(net->net_cpts, sizeof(*net->net_cpts) *
170 net->net_cpts = NULL;
171 net->net_ncpts = LNET_CPT_NUMBER;
175 if (net->net_cpts == NULL) {
176 LIBCFS_ALLOC(net->net_cpts, sizeof(*net->net_cpts) * ncpts);
177 if (net->net_cpts == NULL)
179 memcpy(net->net_cpts, cpts, ncpts * sizeof(*net->net_cpts));
180 net->net_ncpts = ncpts;
184 LIBCFS_ALLOC(added_cpts, sizeof(*added_cpts) * LNET_CPT_NUMBER);
185 if (added_cpts == NULL)
188 for (i = 0; i < ncpts; i++) {
189 if (!in_array(net->net_cpts, net->net_ncpts, cpts[i])) {
190 added_cpts[j] = cpts[i];
195 /* append the new cpts if any to the list of cpts in the net */
197 __u32 *array = NULL, *loc;
198 __u32 total_entries = j + net->net_ncpts;
200 LIBCFS_ALLOC(array, sizeof(*net->net_cpts) * total_entries);
206 memcpy(array, net->net_cpts, net->net_ncpts);
207 loc = array + net->net_ncpts;
208 memcpy(loc, added_cpts, j);
210 LIBCFS_FREE(net->net_cpts, sizeof(*net->net_cpts) *
212 net->net_ncpts = total_entries;
213 net->net_cpts = array;
217 LIBCFS_FREE(added_cpts, sizeof(*added_cpts) * LNET_CPT_NUMBER);
223 lnet_net_remove_cpts(__u32 *cpts, __u32 ncpts, struct lnet_net *net)
229 * Operation Assumption:
230 * This function is called after an NI has been removed from
233 * if we're removing an NI which exists on all CPTs then
234 * we have to check if any of the other NIs on this net also
235 * exists on all CPTs. If none, then we need to build our Net CPT
236 * list based on the remaining NIs.
238 * If the NI being removed exist on a subset of the CPTs then we
239 * alo rebuild the Net CPT list based on the remaining NIs, which
240 * should resutl in the expected Net CPT list.
244 * sometimes this function can be called due to some failure
245 * creating an NI, before any of the cpts are allocated, so check
246 * for that case and don't do anything
251 if (ncpts == LNET_CPT_NUMBER) {
253 * first iteration through the NI list in the net to see
254 * if any of the NIs exist on all the CPTs. If one is
255 * found then our job is done.
257 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
258 if (ni->ni_ncpts == LNET_CPT_NUMBER)
264 * Rebuild the Net CPT list again, thereby only including only the
265 * CPTs which the remaining NIs are associated with.
267 if (net->net_cpts != NULL) {
268 LIBCFS_FREE(net->net_cpts,
269 sizeof(*net->net_cpts) * net->net_ncpts);
270 net->net_cpts = NULL;
273 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
274 rc = lnet_net_append_cpts(ni->ni_cpts, ni->ni_ncpts,
277 CERROR("Out of Memory\n");
279 * do our best to keep on going. Delete
280 * the net cpts and set it to NULL. This
281 * way we can keep on going but less
282 * efficiently, since memory accesses might be
285 if (net->net_cpts != NULL) {
286 LIBCFS_FREE(net->net_cpts,
287 sizeof(*net->net_cpts) *
289 net->net_cpts = NULL;
290 net->net_ncpts = LNET_CPT_NUMBER;
298 lnet_ni_free(struct lnet_ni *ni)
302 lnet_net_remove_cpts(ni->ni_cpts, ni->ni_ncpts, ni->ni_net);
304 if (ni->ni_refs != NULL)
305 cfs_percpt_free(ni->ni_refs);
307 if (ni->ni_tx_queues != NULL)
308 cfs_percpt_free(ni->ni_tx_queues);
310 if (ni->ni_cpts != NULL)
311 cfs_expr_list_values_free(ni->ni_cpts, ni->ni_ncpts);
313 for (i = 0; i < LNET_INTERFACES_NUM &&
314 ni->ni_interfaces[i] != NULL; i++) {
315 LIBCFS_FREE(ni->ni_interfaces[i],
316 strlen(ni->ni_interfaces[i]) + 1);
319 /* release reference to net namespace */
320 if (ni->ni_net_ns != NULL)
321 put_net(ni->ni_net_ns);
323 LIBCFS_FREE(ni, sizeof(*ni));
327 lnet_net_free(struct lnet_net *net)
329 struct list_head *tmp, *tmp2;
332 LASSERT(list_empty(&net->net_ni_zombie));
335 * delete any nis that haven't been added yet. This could happen
336 * if there is a failure on net startup
338 list_for_each_safe(tmp, tmp2, &net->net_ni_added) {
339 ni = list_entry(tmp, struct lnet_ni, ni_netlist);
340 list_del_init(&ni->ni_netlist);
344 /* delete any nis which have been started. */
345 list_for_each_safe(tmp, tmp2, &net->net_ni_list) {
346 ni = list_entry(tmp, struct lnet_ni, ni_netlist);
347 list_del_init(&ni->ni_netlist);
351 if (net->net_cpts != NULL)
352 LIBCFS_FREE(net->net_cpts,
353 sizeof(*net->net_cpts) * net->net_ncpts);
355 LIBCFS_FREE(net, sizeof(*net));
359 lnet_net_alloc(__u32 net_id, struct list_head *net_list)
361 struct lnet_net *net;
363 if (!lnet_net_unique(net_id, net_list, NULL)) {
364 CERROR("Duplicate net %s. Ignore\n",
365 libcfs_net2str(net_id));
369 LIBCFS_ALLOC(net, sizeof(*net));
371 CERROR("Out of memory creating network %s\n",
372 libcfs_net2str(net_id));
376 INIT_LIST_HEAD(&net->net_list);
377 INIT_LIST_HEAD(&net->net_ni_list);
378 INIT_LIST_HEAD(&net->net_ni_added);
379 INIT_LIST_HEAD(&net->net_ni_zombie);
381 net->net_id = net_id;
382 net->net_state = LNET_NET_STATE_INIT;
384 /* initialize global paramters to undefiend */
385 net->net_tunables.lct_peer_timeout = -1;
386 net->net_tunables.lct_max_tx_credits = -1;
387 net->net_tunables.lct_peer_tx_credits = -1;
388 net->net_tunables.lct_peer_rtr_credits = -1;
391 list_add_tail(&net->net_list, net_list);
397 lnet_ni_add_interface(struct lnet_ni *ni, char *iface)
404 if (!lnet_ni_unique_ni(ni->ni_interfaces, iface))
407 /* Allocate a separate piece of memory and copy
408 * into it the string, so we don't have
409 * a depencency on the tokens string. This way we
410 * can free the tokens at the end of the function.
411 * The newly allocated ni_interfaces[] can be
412 * freed when freeing the NI */
413 while (niface < LNET_INTERFACES_NUM &&
414 ni->ni_interfaces[niface] != NULL)
417 if (niface >= LNET_INTERFACES_NUM) {
418 LCONSOLE_ERROR_MSG(0x115, "Too many interfaces "
420 libcfs_net2str(LNET_NIDNET(ni->ni_nid)));
424 LIBCFS_ALLOC(ni->ni_interfaces[niface],
427 if (ni->ni_interfaces[niface] == NULL) {
428 CERROR("Can't allocate net interface name\n");
432 strncpy(ni->ni_interfaces[niface], iface,
438 static struct lnet_ni *
439 lnet_ni_alloc_common(struct lnet_net *net, char *iface)
441 struct lnet_tx_queue *tq;
446 /* make sure that this NI is unique in the net it's
448 if (!lnet_ni_unique_net(&net->net_ni_added, iface))
451 LIBCFS_ALLOC(ni, sizeof(*ni));
453 CERROR("Out of memory creating network interface %s%s\n",
454 libcfs_net2str(net->net_id),
455 (iface != NULL) ? iface : "");
459 spin_lock_init(&ni->ni_lock);
460 INIT_LIST_HEAD(&ni->ni_netlist);
461 INIT_LIST_HEAD(&ni->ni_recovery);
462 ni->ni_refs = cfs_percpt_alloc(lnet_cpt_table(),
463 sizeof(*ni->ni_refs[0]));
464 if (ni->ni_refs == NULL)
467 ni->ni_tx_queues = cfs_percpt_alloc(lnet_cpt_table(),
468 sizeof(*ni->ni_tx_queues[0]));
469 if (ni->ni_tx_queues == NULL)
472 cfs_percpt_for_each(tq, i, ni->ni_tx_queues)
473 INIT_LIST_HEAD(&tq->tq_delayed);
476 /* LND will fill in the address part of the NID */
477 ni->ni_nid = LNET_MKNID(net->net_id, 0);
479 /* Store net namespace in which current ni is being created */
480 if (current->nsproxy->net_ns != NULL)
481 ni->ni_net_ns = get_net(current->nsproxy->net_ns);
483 ni->ni_net_ns = NULL;
485 ni->ni_last_alive = ktime_get_real_seconds();
486 ni->ni_state = LNET_NI_STATE_INIT;
487 list_add_tail(&ni->ni_netlist, &net->net_ni_added);
490 * if an interface name is provided then make sure to add in that
491 * interface name in NI
494 if (lnet_ni_add_interface(ni, iface) != 0)
503 /* allocate and add to the provided network */
505 lnet_ni_alloc(struct lnet_net *net, struct cfs_expr_list *el, char *iface)
510 ni = lnet_ni_alloc_common(net, iface);
516 ni->ni_ncpts = LNET_CPT_NUMBER;
518 rc = cfs_expr_list_values(el, LNET_CPT_NUMBER, &ni->ni_cpts);
520 CERROR("Failed to set CPTs for NI %s(%s): %d\n",
521 libcfs_net2str(net->net_id),
522 (iface != NULL) ? iface : "", rc);
526 LASSERT(rc <= LNET_CPT_NUMBER);
527 if (rc == LNET_CPT_NUMBER) {
528 LIBCFS_FREE(ni->ni_cpts, rc * sizeof(ni->ni_cpts[0]));
535 rc = lnet_net_append_cpts(ni->ni_cpts, ni->ni_ncpts, net);
546 lnet_ni_alloc_w_cpt_array(struct lnet_net *net, __u32 *cpts, __u32 ncpts,
552 ni = lnet_ni_alloc_common(net, iface);
558 ni->ni_ncpts = LNET_CPT_NUMBER;
560 size_t array_size = ncpts * sizeof(ni->ni_cpts[0]);
561 LIBCFS_ALLOC(ni->ni_cpts, array_size);
562 if (ni->ni_cpts == NULL)
564 memcpy(ni->ni_cpts, cpts, array_size);
565 ni->ni_ncpts = ncpts;
568 rc = lnet_net_append_cpts(ni->ni_cpts, ni->ni_ncpts, net);
579 * Parse the networks string and create the matching set of NIs on the
583 lnet_parse_networks(struct list_head *netlist, char *networks,
584 bool use_tcp_bonding)
586 struct cfs_expr_list *net_el = NULL;
587 struct cfs_expr_list *ni_el = NULL;
591 struct lnet_net *net;
592 struct lnet_ni *ni = NULL;
596 if (networks == NULL) {
597 CERROR("networks string is undefined\n");
601 if (strlen(networks) > LNET_SINGLE_TEXTBUF_NOB) {
602 /* _WAY_ conservative */
603 LCONSOLE_ERROR_MSG(0x112, "Can't parse networks: string too "
608 tokensize = strlen(networks) + 1;
610 LIBCFS_ALLOC(tokens, tokensize);
611 if (tokens == NULL) {
612 CERROR("Can't allocate net tokens\n");
616 memcpy(tokens, networks, tokensize);
622 * NB we don't check interface conflicts here; it's the LNDs
623 * responsibility (if it cares at all)
632 * Parse a network string into its components.
634 * <name>{"("...")"}{"["<el>"]"}
637 /* Network name (mandatory) */
638 while (isspace(*str))
643 str += strcspn(str, SPACESTR ":()[],");
644 while (isspace(*str))
647 /* Interface list (optional) */
651 str += strcspn(str, ")");
658 } while (isspace(*str));
663 /* CPT expression (optional) */
666 str += strcspn(str, "]");
671 rc = cfs_expr_list_parse(elstr, str - elstr + 1,
672 0, LNET_CPT_NUMBER - 1,
681 } while (isspace(*str));
685 if (*str && (strchr(DELIMITERS, *str) != NULL))
688 /* go to the next net if it exits */
689 str += strcspn(str, ",");
694 * At this point the name is properly terminated.
696 net_id = libcfs_str2net(name);
697 if (net_id == LNET_NIDNET(LNET_NID_ANY)) {
698 LCONSOLE_ERROR_MSG(0x113,
699 "Unrecognised network type\n");
704 if (LNET_NETTYP(net_id) == LOLND) {
705 /* Loopback is implicit, and there can be only one. */
707 cfs_expr_list_free(net_el);
710 /* Should we error out instead? */
715 * All network paramaters are now known.
719 /* always allocate a net, since we will eventually add an
720 * interface to it, or we will fail, in which case we'll
722 net = lnet_net_alloc(net_id, netlist);
723 if (IS_ERR_OR_NULL(net))
727 (use_tcp_bonding && LNET_NETTYP(net_id) == SOCKLND)) {
729 * No interface list was specified, allocate a
730 * ni using the defaults.
732 ni = lnet_ni_alloc(net, net_el, NULL);
733 if (IS_ERR_OR_NULL(ni))
738 cfs_expr_list_free(net_el);
748 /* Interface name (mandatory) */
749 while (isspace(*nistr))
752 nistr += strcspn(nistr, SPACESTR "[],");
753 while (isspace(*nistr))
756 /* CPT expression (optional) */
759 nistr += strcspn(nistr, "]");
764 rc = cfs_expr_list_parse(elstr,
766 0, LNET_CPT_NUMBER - 1,
775 } while (isspace(*nistr));
781 * End of single interface specificaton,
782 * advance to the start of the next one, if
788 } while (isspace(*nistr));
799 * At this point the name is properly terminated.
806 if (use_tcp_bonding &&
807 LNET_NETTYP(net->net_id) == SOCKLND) {
808 rc = lnet_ni_add_interface(ni, name);
812 ni = lnet_ni_alloc(net, ni_el, name);
813 if (IS_ERR_OR_NULL(ni))
818 if (ni_el != net_el) {
819 cfs_expr_list_free(ni_el);
826 cfs_expr_list_free(net_el);
831 LIBCFS_FREE(tokens, tokensize);
835 lnet_syntax("networks", networks, (int)(str - tokens), strlen(str));
837 /* free the net list and all the nis on each net */
838 while (!list_empty(netlist)) {
839 net = list_entry(netlist->next, struct lnet_net, net_list);
841 list_del_init(&net->net_list);
845 if (ni_el && ni_el != net_el)
846 cfs_expr_list_free(ni_el);
848 cfs_expr_list_free(net_el);
850 LIBCFS_FREE(tokens, tokensize);
855 static struct lnet_text_buf *lnet_new_text_buf(int str_len)
857 struct lnet_text_buf *ltb;
860 /* NB allocate space for the terminating 0 */
861 nob = offsetof(struct lnet_text_buf, ltb_text[str_len + 1]);
862 if (nob > LNET_SINGLE_TEXTBUF_NOB) {
863 /* _way_ conservative for "route net gateway..." */
864 CERROR("text buffer too big\n");
868 if (lnet_tbnob + nob > LNET_MAX_TEXTBUF_NOB) {
869 CERROR("Too many text buffers\n");
873 LIBCFS_ALLOC(ltb, nob);
878 ltb->ltb_text[0] = 0;
884 lnet_free_text_buf(struct lnet_text_buf *ltb)
886 lnet_tbnob -= ltb->ltb_size;
887 LIBCFS_FREE(ltb, ltb->ltb_size);
891 lnet_free_text_bufs(struct list_head *tbs)
893 struct lnet_text_buf *ltb;
895 while (!list_empty(tbs)) {
896 ltb = list_entry(tbs->next, struct lnet_text_buf, ltb_list);
898 list_del(<b->ltb_list);
899 lnet_free_text_buf(ltb);
904 lnet_print_text_bufs(struct list_head *tbs)
906 struct list_head *tmp;
907 struct lnet_text_buf *ltb;
909 list_for_each(tmp, tbs) {
910 ltb = list_entry(tmp, struct lnet_text_buf, ltb_list);
912 CDEBUG(D_WARNING, "%s\n", ltb->ltb_text);
915 CDEBUG(D_WARNING, "%d allocated\n", lnet_tbnob);
919 lnet_str2tbs_sep(struct list_head *tbs, char *str)
921 struct list_head pending;
925 struct lnet_text_buf *ltb;
927 INIT_LIST_HEAD(&pending);
929 /* Split 'str' into separate commands */
931 /* skip leading whitespace */
932 while (isspace(*str))
935 /* scan for separator or comment */
936 for (sep = str; *sep != 0; sep++)
937 if (lnet_issep(*sep) || *sep == '#')
940 nob = (int)(sep - str);
942 ltb = lnet_new_text_buf(nob);
944 lnet_free_text_bufs(&pending);
948 for (i = 0; i < nob; i++)
950 ltb->ltb_text[i] = ' ';
952 ltb->ltb_text[i] = str[i];
954 ltb->ltb_text[nob] = 0;
956 list_add_tail(<b->ltb_list, &pending);
960 /* scan for separator */
963 } while (*sep != 0 && !lnet_issep(*sep));
972 list_splice(&pending, tbs->prev);
977 lnet_expand1tb(struct list_head *list,
978 char *str, char *sep1, char *sep2,
979 char *item, int itemlen)
981 int len1 = (int)(sep1 - str);
982 int len2 = strlen(sep2 + 1);
983 struct lnet_text_buf *ltb;
985 LASSERT (*sep1 == '[');
986 LASSERT (*sep2 == ']');
988 ltb = lnet_new_text_buf(len1 + itemlen + len2);
992 memcpy(ltb->ltb_text, str, len1);
993 memcpy(<b->ltb_text[len1], item, itemlen);
994 memcpy(<b->ltb_text[len1+itemlen], sep2 + 1, len2);
995 ltb->ltb_text[len1 + itemlen + len2] = 0;
997 list_add_tail(<b->ltb_list, list);
1002 lnet_str2tbs_expand(struct list_head *tbs, char *str)
1005 struct list_head pending;
1017 INIT_LIST_HEAD(&pending);
1019 sep = strchr(str, '[');
1020 if (sep == NULL) /* nothing to expand */
1023 sep2 = strchr(sep, ']');
1027 for (parsed = sep; parsed < sep2; parsed = enditem) {
1030 while (enditem < sep2 && *enditem != ',')
1033 if (enditem == parsed) /* no empty items */
1036 if (sscanf(parsed, "%d-%d/%d%n", &lo, &hi, &stride, &scanned) < 3) {
1038 if (sscanf(parsed, "%d-%d%n", &lo, &hi, &scanned) < 2) {
1040 /* simple string enumeration */
1041 if (lnet_expand1tb(&pending, str, sep, sep2,
1042 parsed, (int)(enditem - parsed)) != 0)
1051 /* range expansion */
1053 if (enditem != parsed + scanned) /* no trailing junk */
1056 if (hi < 0 || lo < 0 || stride < 0 || hi < lo ||
1057 (hi - lo) % stride != 0)
1060 for (i = lo; i <= hi; i += stride) {
1062 snprintf(num, sizeof(num), "%d", i);
1064 if (nob + 1 == sizeof(num))
1067 if (lnet_expand1tb(&pending, str, sep, sep2,
1073 list_splice(&pending, tbs->prev);
1077 lnet_free_text_bufs(&pending);
1082 lnet_parse_hops (char *str, unsigned int *hops)
1084 int len = strlen(str);
1087 return (sscanf(str, "%u%n", hops, &nob) >= 1 &&
1089 *hops > 0 && *hops < 256);
1092 #define LNET_PRIORITY_SEPARATOR (':')
1095 lnet_parse_priority(char *str, unsigned int *priority, char **token)
1101 sep = strchr(str, LNET_PRIORITY_SEPARATOR);
1106 len = strlen(sep + 1);
1108 if ((sscanf((sep+1), "%u%n", priority, &nob) < 1) || (len != nob)) {
1109 /* Update the caller's token pointer so it treats the found
1110 priority as the token to report in the error message. */
1111 *token += sep - str + 1;
1115 CDEBUG(D_NET, "gateway %s, priority %d, nob %d\n", str, *priority, nob);
1118 * Change priority separator to \0 to be able to parse NID
1125 lnet_parse_route (char *str, int *im_a_router)
1127 /* static scratch buffer OK (single threaded) */
1128 static char cmd[LNET_SINGLE_TEXTBUF_NOB];
1130 struct list_head nets;
1131 struct list_head gateways;
1132 struct list_head *tmp1;
1133 struct list_head *tmp2;
1136 struct lnet_text_buf *ltb;
1144 unsigned int priority = 0;
1146 INIT_LIST_HEAD(&gateways);
1147 INIT_LIST_HEAD(&nets);
1149 /* save a copy of the string for error messages */
1150 strncpy(cmd, str, sizeof(cmd));
1151 cmd[sizeof(cmd) - 1] = '\0';
1155 /* scan for token start */
1156 while (isspace(*sep))
1159 if (ntokens < (got_hops ? 3 : 2))
1167 /* scan for token end */
1168 while (*sep != 0 && !isspace(*sep))
1174 tmp2 = &nets; /* expanding nets */
1175 } else if (ntokens == 2 &&
1176 lnet_parse_hops(token, &hops)) {
1177 got_hops = 1; /* got a hop count */
1180 tmp2 = &gateways; /* expanding gateways */
1183 ltb = lnet_new_text_buf(strlen(token));
1187 strcpy(ltb->ltb_text, token);
1188 tmp1 = <b->ltb_list;
1189 list_add_tail(tmp1, tmp2);
1191 while (tmp1 != tmp2) {
1192 ltb = list_entry(tmp1, struct lnet_text_buf, ltb_list);
1194 rc = lnet_str2tbs_expand(tmp1->next, ltb->ltb_text);
1200 if (rc > 0) { /* expanded! */
1201 list_del(<b->ltb_list);
1202 lnet_free_text_buf(ltb);
1207 net = libcfs_str2net(ltb->ltb_text);
1208 if (net == LNET_NIDNET(LNET_NID_ANY) ||
1209 LNET_NETTYP(net) == LOLND)
1212 rc = lnet_parse_priority(ltb->ltb_text,
1217 nid = libcfs_str2nid(ltb->ltb_text);
1218 if (nid == LNET_NID_ANY ||
1219 LNET_NETTYP(LNET_NIDNET(nid)) == LOLND)
1225 /* if there are no hops set then we want to flag this value as
1226 * unset since hops is an optional parameter */
1228 hops = LNET_UNDEFINED_HOPS;
1230 LASSERT(!list_empty(&nets));
1231 LASSERT(!list_empty(&gateways));
1233 list_for_each(tmp1, &nets) {
1234 ltb = list_entry(tmp1, struct lnet_text_buf, ltb_list);
1235 net = libcfs_str2net(ltb->ltb_text);
1236 LASSERT (net != LNET_NIDNET(LNET_NID_ANY));
1238 list_for_each(tmp2, &gateways) {
1239 ltb = list_entry(tmp2, struct lnet_text_buf, ltb_list);
1240 nid = libcfs_str2nid(ltb->ltb_text);
1241 LASSERT(nid != LNET_NID_ANY);
1243 if (lnet_islocalnid(nid)) {
1248 rc = lnet_add_route(net, hops, nid, priority);
1249 if (rc != 0 && rc != -EEXIST && rc != -EHOSTUNREACH) {
1250 CERROR("Can't create route "
1252 libcfs_net2str(net),
1253 libcfs_nid2str(nid));
1263 lnet_syntax("routes", cmd, (int)(token - str), strlen(token));
1265 lnet_free_text_bufs(&nets);
1266 lnet_free_text_bufs(&gateways);
1271 lnet_parse_route_tbs(struct list_head *tbs, int *im_a_router)
1273 struct lnet_text_buf *ltb;
1275 while (!list_empty(tbs)) {
1276 ltb = list_entry(tbs->next, struct lnet_text_buf, ltb_list);
1278 if (lnet_parse_route(ltb->ltb_text, im_a_router) < 0) {
1279 lnet_free_text_bufs(tbs);
1283 list_del(<b->ltb_list);
1284 lnet_free_text_buf(ltb);
1291 lnet_parse_routes (char *routes, int *im_a_router)
1293 struct list_head tbs;
1298 INIT_LIST_HEAD(&tbs);
1300 if (lnet_str2tbs_sep(&tbs, routes) < 0) {
1301 CERROR("Error parsing routes\n");
1304 rc = lnet_parse_route_tbs(&tbs, im_a_router);
1307 LASSERT (lnet_tbnob == 0);
1312 lnet_match_network_token(char *token, int len, __u32 *ipaddrs, int nip)
1314 struct list_head list = LIST_HEAD_INIT(list);
1318 rc = cfs_ip_addr_parse(token, len, &list);
1322 for (rc = i = 0; !rc && i < nip; i++)
1323 rc = cfs_ip_addr_match(ipaddrs[i], &list);
1325 cfs_expr_list_free_list(&list);
1331 lnet_match_network_tokens(char *net_entry, __u32 *ipaddrs, int nip)
1333 static char tokens[LNET_SINGLE_TEXTBUF_NOB];
1343 LASSERT(strlen(net_entry) < sizeof(tokens));
1345 /* work on a copy of the string */
1346 strcpy(tokens, net_entry);
1349 /* scan for token start */
1350 while (isspace(*sep))
1357 /* scan for token end */
1358 while (*sep != 0 && !isspace(*sep))
1363 if (ntokens++ == 0) {
1368 len = strlen(token);
1370 rc = lnet_match_network_token(token, len, ipaddrs, nip);
1372 lnet_syntax("ip2nets", net_entry,
1373 (int)(token - tokens), len);
1377 matched |= (rc != 0);
1383 strcpy(net_entry, net); /* replace with matched net */
1388 lnet_netspec2net(char *netspec)
1390 char *bracket = strchr(netspec, '(');
1393 if (bracket != NULL)
1396 net = libcfs_str2net(netspec);
1398 if (bracket != NULL)
1405 lnet_splitnets(char *source, struct list_head *nets)
1410 struct lnet_text_buf *tb;
1411 struct lnet_text_buf *tb2;
1412 struct list_head *t;
1417 LASSERT(!list_empty(nets));
1418 LASSERT(nets->next == nets->prev); /* single entry */
1420 tb = list_entry(nets->next, struct lnet_text_buf, ltb_list);
1423 sep = strchr(tb->ltb_text, ',');
1424 bracket = strchr(tb->ltb_text, '(');
1429 /* netspec lists interfaces... */
1431 offset2 = offset + (int)(bracket - tb->ltb_text);
1432 len = strlen(bracket);
1434 bracket = strchr(bracket + 1, ')');
1436 if (bracket == NULL ||
1437 !(bracket[1] == ',' || bracket[1] == 0)) {
1438 lnet_syntax("ip2nets", source, offset2, len);
1442 sep = (bracket[1] == 0) ? NULL : bracket + 1;
1448 net = lnet_netspec2net(tb->ltb_text);
1449 if (net == LNET_NIDNET(LNET_NID_ANY)) {
1450 lnet_syntax("ip2nets", source, offset,
1451 strlen(tb->ltb_text));
1455 list_for_each(t, nets) {
1456 tb2 = list_entry(t, struct lnet_text_buf, ltb_list);
1461 if (net == lnet_netspec2net(tb2->ltb_text)) {
1462 /* duplicate network */
1463 lnet_syntax("ip2nets", source, offset,
1464 strlen(tb->ltb_text));
1472 offset += (int)(sep - tb->ltb_text);
1474 tb2 = lnet_new_text_buf(len);
1478 strncpy(tb2->ltb_text, sep, len);
1479 tb2->ltb_text[len] = '\0';
1480 list_add_tail(&tb2->ltb_list, nets);
1487 lnet_match_networks (char **networksp, char *ip2nets, __u32 *ipaddrs, int nip)
1489 static char networks[LNET_SINGLE_TEXTBUF_NOB];
1490 static char source[LNET_SINGLE_TEXTBUF_NOB];
1492 struct list_head raw_entries;
1493 struct list_head matched_nets;
1494 struct list_head current_nets;
1495 struct list_head *t;
1496 struct list_head *t2;
1497 struct lnet_text_buf *tb;
1498 struct lnet_text_buf *tb2;
1506 INIT_LIST_HEAD(&raw_entries);
1507 if (lnet_str2tbs_sep(&raw_entries, ip2nets) < 0) {
1508 CERROR("Error parsing ip2nets\n");
1509 LASSERT(lnet_tbnob == 0);
1513 INIT_LIST_HEAD(&matched_nets);
1514 INIT_LIST_HEAD(¤t_nets);
1520 while (!list_empty(&raw_entries)) {
1521 tb = list_entry(raw_entries.next, struct lnet_text_buf,
1524 strncpy(source, tb->ltb_text, sizeof(source));
1525 source[sizeof(source) - 1] = '\0';
1527 /* replace ltb_text with the network(s) add on match */
1528 rc = lnet_match_network_tokens(tb->ltb_text, ipaddrs, nip);
1532 list_del(&tb->ltb_list);
1534 if (rc == 0) { /* no match */
1535 lnet_free_text_buf(tb);
1539 /* split into separate networks */
1540 INIT_LIST_HEAD(¤t_nets);
1541 list_add(&tb->ltb_list, ¤t_nets);
1542 rc = lnet_splitnets(source, ¤t_nets);
1547 list_for_each(t, ¤t_nets) {
1548 tb = list_entry(t, struct lnet_text_buf, ltb_list);
1549 net1 = lnet_netspec2net(tb->ltb_text);
1550 LASSERT(net1 != LNET_NIDNET(LNET_NID_ANY));
1552 list_for_each(t2, &matched_nets) {
1553 tb2 = list_entry(t2, struct lnet_text_buf,
1555 net2 = lnet_netspec2net(tb2->ltb_text);
1556 LASSERT(net2 != LNET_NIDNET(LNET_NID_ANY));
1569 lnet_free_text_bufs(¤t_nets);
1573 list_for_each_safe(t, t2, ¤t_nets) {
1574 tb = list_entry(t, struct lnet_text_buf, ltb_list);
1576 list_del(&tb->ltb_list);
1577 list_add_tail(&tb->ltb_list, &matched_nets);
1579 len += snprintf(networks + len, sizeof(networks) - len,
1580 "%s%s", (len == 0) ? "" : ",",
1583 if (len >= sizeof(networks)) {
1584 CERROR("Too many matched networks\n");
1594 lnet_free_text_bufs(&raw_entries);
1595 lnet_free_text_bufs(&matched_nets);
1596 lnet_free_text_bufs(¤t_nets);
1597 LASSERT(lnet_tbnob == 0);
1602 *networksp = networks;
1607 lnet_ipaddr_free_enumeration(__u32 *ipaddrs, int nip)
1609 LIBCFS_FREE(ipaddrs, nip * sizeof(*ipaddrs));
1613 lnet_ipaddr_enumerate(u32 **ipaddrsp)
1615 struct net_device *dev;
1620 LIBCFS_ALLOC(ipaddrs, nalloc * sizeof(*ipaddrs));
1622 CERROR("Can't allocate ipaddrs[%d]\n", nalloc);
1627 for_each_netdev(&init_net, dev) {
1628 struct in_device *in_dev;
1630 if (strcmp(dev->name, "lo") == 0)
1633 if (!(dev_get_flags(dev) & IFF_UP)) {
1634 CWARN("Ignoring interface %s: it's down\n", dev->name);
1638 in_dev = __in_dev_get_rtnl(dev);
1640 CWARN("Interface %s has no IPv4 status.\n", dev->name);
1644 if (nip >= nalloc) {
1648 ipaddrs2 = krealloc(ipaddrs, nalloc * sizeof(*ipaddrs2),
1652 CERROR("Can't allocate ipaddrs[%d]\n", nip);
1658 for_primary_ifa(in_dev)
1659 if (strcmp(ifa->ifa_label, dev->name) == 0) {
1660 ipaddrs[nip++] = ifa->ifa_local;
1667 *ipaddrsp = ipaddrs;
1672 lnet_parse_ip2nets (char **networksp, char *ip2nets)
1674 __u32 *ipaddrs = NULL;
1675 int nip = lnet_ipaddr_enumerate(&ipaddrs);
1679 LCONSOLE_ERROR_MSG(0x117, "Error %d enumerating local IP "
1680 "interfaces for ip2nets to match\n", nip);
1685 LCONSOLE_ERROR_MSG(0x118, "No local IP interfaces "
1686 "for ip2nets to match\n");
1690 rc = lnet_match_networks(networksp, ip2nets, ipaddrs, nip);
1691 lnet_ipaddr_free_enumeration(ipaddrs, nip);
1694 LCONSOLE_ERROR_MSG(0x119, "Error %d parsing ip2nets\n", rc);
1699 LCONSOLE_ERROR_MSG(0x11a, "ip2nets does not match "
1700 "any local IP interfaces\n");