1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_LNET
38 #include <lnet/lib-lnet.h>
40 typedef struct { /* tmp struct for parsing routes */
41 cfs_list_t ltb_list; /* stash on lists */
42 int ltb_size; /* allocated size */
43 char ltb_text[0]; /* text buffer */
46 static int lnet_tbnob = 0; /* track text buf allocation */
47 #define LNET_MAX_TEXTBUF_NOB (64<<10) /* bound allocation */
48 #define LNET_SINGLE_TEXTBUF_NOB (4<<10)
51 cfs_list_t lre_list; /* stash in a list */
52 int lre_min; /* min value */
53 int lre_max; /* max value */
54 int lre_stride; /* stride */
57 static int lnet_re_alloc = 0; /* track expr allocation */
60 lnet_syntax(char *name, char *str, int offset, int width)
62 static char dots[LNET_SINGLE_TEXTBUF_NOB];
63 static char dashes[LNET_SINGLE_TEXTBUF_NOB];
65 memset(dots, '.', sizeof(dots));
66 dots[sizeof(dots)-1] = 0;
67 memset(dashes, '-', sizeof(dashes));
68 dashes[sizeof(dashes)-1] = 0;
70 LCONSOLE_ERROR_MSG(0x10f, "Error parsing '%s=\"%s\"'\n", name, str);
71 LCONSOLE_ERROR_MSG(0x110, "here...........%.*s..%.*s|%.*s|\n",
72 (int)strlen(name), dots, offset, dots,
73 (width < 1) ? 0 : width - 1, dashes);
90 lnet_trimwhite(char *str)
94 while (cfs_iswhite(*str))
97 end = str + strlen(str);
99 if (!cfs_iswhite(end[-1]))
109 lnet_net_unique(__u32 net, cfs_list_t *nilist)
114 cfs_list_for_each (tmp, nilist) {
115 ni = cfs_list_entry(tmp, lnet_ni_t, ni_list);
117 if (LNET_NIDNET(ni->ni_nid) == net)
125 lnet_new_ni(__u32 net, cfs_list_t *nilist)
129 if (!lnet_net_unique(net, nilist)) {
130 LCONSOLE_ERROR_MSG(0x111, "Duplicate network specified: %s\n",
131 libcfs_net2str(net));
135 LIBCFS_ALLOC(ni, sizeof(*ni));
137 CERROR("Out of memory creating network %s\n",
138 libcfs_net2str(net));
142 /* zero counters/flags, NULL pointers... */
143 memset(ni, 0, sizeof(*ni));
145 /* LND will fill in the address part of the NID */
146 ni->ni_nid = LNET_MKNID(net, 0);
147 CFS_INIT_LIST_HEAD(&ni->ni_txq);
148 ni->ni_last_alive = cfs_time_current();
150 cfs_list_add_tail(&ni->ni_list, nilist);
155 lnet_parse_networks(cfs_list_t *nilist, char *networks)
157 int tokensize = strlen(networks) + 1;
164 if (strlen(networks) > LNET_SINGLE_TEXTBUF_NOB) {
165 /* _WAY_ conservative */
166 LCONSOLE_ERROR_MSG(0x112, "Can't parse networks: string too "
171 LIBCFS_ALLOC(tokens, tokensize);
172 if (tokens == NULL) {
173 CERROR("Can't allocate net tokens\n");
177 the_lnet.ln_network_tokens = tokens;
178 the_lnet.ln_network_tokens_nob = tokensize;
179 memcpy (tokens, networks, tokensize);
182 /* Add in the loopback network */
183 ni = lnet_new_ni(LNET_MKNET(LOLND, 0), nilist);
187 while (str != NULL && *str != 0) {
188 char *comma = strchr(str, ',');
189 char *bracket = strchr(str, '(');
193 /* NB we don't check interface conflicts here; it's the LNDs
194 * responsibility (if it cares at all) */
196 if (bracket == NULL ||
197 (comma != NULL && comma < bracket)) {
199 /* no interface list specified */
203 net = libcfs_str2net(lnet_trimwhite(str));
205 if (net == LNET_NIDNET(LNET_NID_ANY)) {
206 lnet_syntax("networks", networks,
207 (int)(str - tokens), strlen(str));
208 LCONSOLE_ERROR_MSG(0x113, "Unrecognised network"
213 if (LNET_NETTYP(net) != LOLND && /* loopback is implicit */
214 lnet_new_ni(net, nilist) == NULL)
222 net = libcfs_str2net(lnet_trimwhite(str));
223 if (net == LNET_NIDNET(LNET_NID_ANY)) {
224 lnet_syntax("networks", networks,
225 (int)(str - tokens), strlen(str));
230 ni = lnet_new_ni(net, nilist);
237 bracket = strchr(iface, ')');
238 if (bracket == NULL) {
239 lnet_syntax("networks", networks,
240 (int)(iface - tokens), strlen(iface));
246 comma = strchr(iface, ',');
250 iface = lnet_trimwhite(iface);
252 lnet_syntax("networks", networks,
253 (int)(iface - tokens), strlen(iface));
257 if (niface == LNET_MAX_INTERFACES) {
258 LCONSOLE_ERROR_MSG(0x115, "Too many interfaces "
260 libcfs_net2str(net));
264 ni->ni_interfaces[niface++] = iface;
266 } while (iface != NULL);
269 comma = strchr(bracket + 1, ',');
272 str = lnet_trimwhite(str);
274 lnet_syntax("networks", networks,
275 (int)(str - tokens), strlen(str));
282 str = lnet_trimwhite(str);
284 lnet_syntax("networks", networks,
285 (int)(str - tokens), strlen(str));
290 LASSERT (!cfs_list_empty(nilist));
294 while (!cfs_list_empty(nilist)) {
295 ni = cfs_list_entry(nilist->next, lnet_ni_t, ni_list);
297 cfs_list_del(&ni->ni_list);
298 LIBCFS_FREE(ni, sizeof(*ni));
300 LIBCFS_FREE(tokens, tokensize);
301 the_lnet.ln_network_tokens = NULL;
307 lnet_new_text_buf (int str_len)
309 lnet_text_buf_t *ltb;
312 /* NB allocate space for the terminating 0 */
313 nob = offsetof(lnet_text_buf_t, ltb_text[str_len + 1]);
314 if (nob > LNET_SINGLE_TEXTBUF_NOB) {
315 /* _way_ conservative for "route net gateway..." */
316 CERROR("text buffer too big\n");
320 if (lnet_tbnob + nob > LNET_MAX_TEXTBUF_NOB) {
321 CERROR("Too many text buffers\n");
325 LIBCFS_ALLOC(ltb, nob);
330 ltb->ltb_text[0] = 0;
336 lnet_free_text_buf (lnet_text_buf_t *ltb)
338 lnet_tbnob -= ltb->ltb_size;
339 LIBCFS_FREE(ltb, ltb->ltb_size);
343 lnet_free_text_bufs(cfs_list_t *tbs)
345 lnet_text_buf_t *ltb;
347 while (!cfs_list_empty(tbs)) {
348 ltb = cfs_list_entry(tbs->next, lnet_text_buf_t, ltb_list);
350 cfs_list_del(<b->ltb_list);
351 lnet_free_text_buf(ltb);
356 lnet_print_text_bufs(cfs_list_t *tbs)
359 lnet_text_buf_t *ltb;
361 cfs_list_for_each (tmp, tbs) {
362 ltb = cfs_list_entry(tmp, lnet_text_buf_t, ltb_list);
364 CDEBUG(D_WARNING, "%s\n", ltb->ltb_text);
367 CDEBUG(D_WARNING, "%d allocated\n", lnet_tbnob);
371 lnet_str2tbs_sep (cfs_list_t *tbs, char *str)
377 lnet_text_buf_t *ltb;
379 CFS_INIT_LIST_HEAD(&pending);
381 /* Split 'str' into separate commands */
383 /* skip leading whitespace */
384 while (cfs_iswhite(*str))
387 /* scan for separator or comment */
388 for (sep = str; *sep != 0; sep++)
389 if (lnet_issep(*sep) || *sep == '#')
392 nob = (int)(sep - str);
394 ltb = lnet_new_text_buf(nob);
396 lnet_free_text_bufs(&pending);
400 for (i = 0; i < nob; i++)
401 if (cfs_iswhite(str[i]))
402 ltb->ltb_text[i] = ' ';
404 ltb->ltb_text[i] = str[i];
406 ltb->ltb_text[nob] = 0;
408 cfs_list_add_tail(<b->ltb_list, &pending);
412 /* scan for separator */
415 } while (*sep != 0 && !lnet_issep(*sep));
424 cfs_list_splice(&pending, tbs->prev);
429 lnet_expand1tb (cfs_list_t *list,
430 char *str, char *sep1, char *sep2,
431 char *item, int itemlen)
433 int len1 = (int)(sep1 - str);
434 int len2 = strlen(sep2 + 1);
435 lnet_text_buf_t *ltb;
437 LASSERT (*sep1 == '[');
438 LASSERT (*sep2 == ']');
440 ltb = lnet_new_text_buf(len1 + itemlen + len2);
444 memcpy(ltb->ltb_text, str, len1);
445 memcpy(<b->ltb_text[len1], item, itemlen);
446 memcpy(<b->ltb_text[len1+itemlen], sep2 + 1, len2);
447 ltb->ltb_text[len1 + itemlen + len2] = 0;
449 cfs_list_add_tail(<b->ltb_list, list);
454 lnet_str2tbs_expand (cfs_list_t *tbs, char *str)
469 CFS_INIT_LIST_HEAD(&pending);
471 sep = strchr(str, '[');
472 if (sep == NULL) /* nothing to expand */
475 sep2 = strchr(sep, ']');
479 for (parsed = sep; parsed < sep2; parsed = enditem) {
482 while (enditem < sep2 && *enditem != ',')
485 if (enditem == parsed) /* no empty items */
488 if (sscanf(parsed, "%d-%d/%d%n", &lo, &hi, &stride, &scanned) < 3) {
490 if (sscanf(parsed, "%d-%d%n", &lo, &hi, &scanned) < 2) {
492 /* simple string enumeration */
493 if (lnet_expand1tb(&pending, str, sep, sep2,
494 parsed, (int)(enditem - parsed)) != 0)
503 /* range expansion */
505 if (enditem != parsed + scanned) /* no trailing junk */
508 if (hi < 0 || lo < 0 || stride < 0 || hi < lo ||
509 (hi - lo) % stride != 0)
512 for (i = lo; i <= hi; i += stride) {
514 snprintf(num, sizeof(num), "%d", i);
516 if (nob + 1 == sizeof(num))
519 if (lnet_expand1tb(&pending, str, sep, sep2,
525 cfs_list_splice(&pending, tbs->prev);
529 lnet_free_text_bufs(&pending);
534 lnet_parse_hops (char *str, unsigned int *hops)
536 int len = strlen(str);
539 return (sscanf(str, "%u%n", hops, &nob) >= 1 &&
541 *hops > 0 && *hops < 256);
546 lnet_parse_route (char *str, int *im_a_router)
548 /* static scratch buffer OK (single threaded) */
549 static char cmd[LNET_SINGLE_TEXTBUF_NOB];
557 lnet_text_buf_t *ltb;
566 CFS_INIT_LIST_HEAD(&gateways);
567 CFS_INIT_LIST_HEAD(&nets);
569 /* save a copy of the string for error messages */
570 strncpy(cmd, str, sizeof(cmd) - 1);
571 cmd[sizeof(cmd) - 1] = 0;
575 /* scan for token start */
576 while (cfs_iswhite(*sep))
579 if (ntokens < (got_hops ? 3 : 2))
587 /* scan for token end */
588 while (*sep != 0 && !cfs_iswhite(*sep))
594 tmp2 = &nets; /* expanding nets */
595 } else if (ntokens == 2 &&
596 lnet_parse_hops(token, &hops)) {
597 got_hops = 1; /* got a hop count */
600 tmp2 = &gateways; /* expanding gateways */
603 ltb = lnet_new_text_buf(strlen(token));
607 strcpy(ltb->ltb_text, token);
608 tmp1 = <b->ltb_list;
609 cfs_list_add_tail(tmp1, tmp2);
611 while (tmp1 != tmp2) {
612 ltb = cfs_list_entry(tmp1, lnet_text_buf_t, ltb_list);
614 rc = lnet_str2tbs_expand(tmp1->next, ltb->ltb_text);
620 if (rc > 0) { /* expanded! */
621 cfs_list_del(<b->ltb_list);
622 lnet_free_text_buf(ltb);
627 net = libcfs_str2net(ltb->ltb_text);
628 if (net == LNET_NIDNET(LNET_NID_ANY) ||
629 LNET_NETTYP(net) == LOLND)
632 nid = libcfs_str2nid(ltb->ltb_text);
633 if (nid == LNET_NID_ANY ||
634 LNET_NETTYP(LNET_NIDNET(nid)) == LOLND)
643 LASSERT (!cfs_list_empty(&nets));
644 LASSERT (!cfs_list_empty(&gateways));
646 cfs_list_for_each (tmp1, &nets) {
647 ltb = cfs_list_entry(tmp1, lnet_text_buf_t, ltb_list);
648 net = libcfs_str2net(ltb->ltb_text);
649 LASSERT (net != LNET_NIDNET(LNET_NID_ANY));
651 cfs_list_for_each (tmp2, &gateways) {
652 ltb = cfs_list_entry(tmp2, lnet_text_buf_t, ltb_list);
653 nid = libcfs_str2nid(ltb->ltb_text);
654 LASSERT (nid != LNET_NID_ANY);
656 if (lnet_islocalnid(nid)) {
661 rc = lnet_add_route (net, hops, nid);
663 CERROR("Can't create route "
666 libcfs_nid2str(nid));
676 lnet_syntax("routes", cmd, (int)(token - str), strlen(token));
678 lnet_free_text_bufs(&nets);
679 lnet_free_text_bufs(&gateways);
684 lnet_parse_route_tbs(cfs_list_t *tbs, int *im_a_router)
686 lnet_text_buf_t *ltb;
688 while (!cfs_list_empty(tbs)) {
689 ltb = cfs_list_entry(tbs->next, lnet_text_buf_t, ltb_list);
691 if (lnet_parse_route(ltb->ltb_text, im_a_router) < 0) {
692 lnet_free_text_bufs(tbs);
696 cfs_list_del(<b->ltb_list);
697 lnet_free_text_buf(ltb);
704 lnet_parse_routes (char *routes, int *im_a_router)
711 CFS_INIT_LIST_HEAD(&tbs);
713 if (lnet_str2tbs_sep(&tbs, routes) < 0) {
714 CERROR("Error parsing routes\n");
717 rc = lnet_parse_route_tbs(&tbs, im_a_router);
720 LASSERT (lnet_tbnob == 0);
725 lnet_print_range_exprs(cfs_list_t *exprs)
728 lnet_range_expr_t *lre;
730 cfs_list_for_each(e, exprs) {
731 lre = cfs_list_entry(exprs->next, lnet_range_expr_t, lre_list);
733 CDEBUG(D_WARNING, "%d-%d/%d\n",
734 lre->lre_min, lre->lre_max, lre->lre_stride);
737 CDEBUG(D_WARNING, "%d allocated\n", lnet_re_alloc);
741 lnet_new_range_expr(cfs_list_t *exprs, int min, int max, int stride)
743 lnet_range_expr_t *lre;
745 CDEBUG(D_NET, "%d-%d/%d\n", min, max, stride);
747 if (min < 0 || min > 255 || min > max || stride < 0)
750 LIBCFS_ALLOC(lre, sizeof(*lre));
758 lre->lre_stride = stride;
760 cfs_list_add(&lre->lre_list, exprs);
765 lnet_destroy_range_exprs(cfs_list_t *exprs)
767 lnet_range_expr_t *lre;
769 while (!cfs_list_empty(exprs)) {
770 lre = cfs_list_entry(exprs->next, lnet_range_expr_t, lre_list);
772 cfs_list_del(&lre->lre_list);
773 LIBCFS_FREE(lre, sizeof(*lre));
779 lnet_parse_range_expr(cfs_list_t *exprs, char *str)
781 int nob = strlen(str);
792 if (!strcmp(str, "*")) /* match all */
793 return lnet_new_range_expr(exprs, 0, 255, 1);
796 if (sscanf(str, "%u%n", &x, &n) >= 1 && n == nob) {
798 return lnet_new_range_expr(exprs, x, x, 1);
801 /* Has to be an expansion */
802 if (!(str[0] == '[' && nob > 2 && str[nob-1] == ']'))
810 /* Comma separated list of expressions... */
811 sep = strchr(str, ',');
817 if (sscanf(str, "%u%n", &x, &n) >= 1 && n == nob) {
819 rc = lnet_new_range_expr(exprs, x, x, 1);
827 if (sscanf(str, "%u-%u%n", &x, &y, &n) >= 2 && n == nob) {
829 rc = lnet_new_range_expr(exprs, x, y, 1);
836 if (sscanf(str, "%u-%u/%u%n", &x, &y, &z, &n) >= 3 && n == nob) {
838 rc = lnet_new_range_expr(exprs, x, y, z);
846 } while ((str = sep) != NULL);
852 lnet_match_network_token(char *token, __u32 *ipaddrs, int nip)
856 lnet_range_expr_t *re;
865 for (i = 0; i < 4; i++)
866 CFS_INIT_LIST_HEAD(&exprs[i]);
868 for (i = 0; i < 4; i++) {
871 token = strchr(token, '.');
879 rc = lnet_parse_range_expr(&exprs[i], str);
886 for (match = i = 0; !match && i < nip; i++) {
889 for (match = 1, j = 0; match && j < 4; j++) {
890 n = (ip >> (8 * (3 - j))) & 0xff;
893 cfs_list_for_each(e, &exprs[j]) {
894 re = cfs_list_entry(e, lnet_range_expr_t,
897 if (re->lre_min <= n &&
899 (n - re->lre_min) % re->lre_stride == 0) {
910 for (i = 0; i < 4; i++)
911 lnet_destroy_range_exprs(&exprs[i]);
912 LASSERT (lnet_re_alloc == 0);
918 lnet_match_network_tokens(char *net_entry, __u32 *ipaddrs, int nip)
920 static char tokens[LNET_SINGLE_TEXTBUF_NOB];
930 LASSERT (strlen(net_entry) < sizeof(tokens));
932 /* work on a copy of the string */
933 strcpy(tokens, net_entry);
936 /* scan for token start */
937 while (cfs_iswhite(*sep))
944 /* scan for token end */
945 while (*sep != 0 && !cfs_iswhite(*sep))
950 if (ntokens++ == 0) {
957 rc = lnet_match_network_token(token, ipaddrs, nip);
959 lnet_syntax("ip2nets", net_entry,
960 (int)(token - tokens), len);
964 matched |= (rc != 0);
970 strcpy(net_entry, net); /* replace with matched net */
975 lnet_netspec2net(char *netspec)
977 char *bracket = strchr(netspec, '(');
983 net = libcfs_str2net(netspec);
992 lnet_splitnets(char *source, cfs_list_t *nets)
998 lnet_text_buf_t *tb2;
1004 LASSERT (!cfs_list_empty(nets));
1005 LASSERT (nets->next == nets->prev); /* single entry */
1007 tb = cfs_list_entry(nets->next, lnet_text_buf_t, ltb_list);
1010 sep = strchr(tb->ltb_text, ',');
1011 bracket = strchr(tb->ltb_text, '(');
1016 /* netspec lists interfaces... */
1018 offset2 = offset + (int)(bracket - tb->ltb_text);
1019 len = strlen(bracket);
1021 bracket = strchr(bracket + 1, ')');
1023 if (bracket == NULL ||
1024 !(bracket[1] == ',' || bracket[1] == 0)) {
1025 lnet_syntax("ip2nets", source, offset2, len);
1029 sep = (bracket[1] == 0) ? NULL : bracket + 1;
1035 net = lnet_netspec2net(tb->ltb_text);
1036 if (net == LNET_NIDNET(LNET_NID_ANY)) {
1037 lnet_syntax("ip2nets", source, offset,
1038 strlen(tb->ltb_text));
1042 cfs_list_for_each(t, nets) {
1043 tb2 = cfs_list_entry(t, lnet_text_buf_t, ltb_list);
1048 if (net == lnet_netspec2net(tb2->ltb_text)) {
1049 /* duplicate network */
1050 lnet_syntax("ip2nets", source, offset,
1051 strlen(tb->ltb_text));
1059 offset += (int)(sep - tb->ltb_text);
1060 tb2 = lnet_new_text_buf(strlen(sep));
1064 strcpy(tb2->ltb_text, sep);
1065 cfs_list_add_tail(&tb2->ltb_list, nets);
1072 lnet_match_networks (char **networksp, char *ip2nets, __u32 *ipaddrs, int nip)
1074 static char networks[LNET_SINGLE_TEXTBUF_NOB];
1075 static char source[LNET_SINGLE_TEXTBUF_NOB];
1077 cfs_list_t raw_entries;
1078 cfs_list_t matched_nets;
1079 cfs_list_t current_nets;
1082 lnet_text_buf_t *tb;
1083 lnet_text_buf_t *tb2;
1091 CFS_INIT_LIST_HEAD(&raw_entries);
1092 if (lnet_str2tbs_sep(&raw_entries, ip2nets) < 0) {
1093 CERROR("Error parsing ip2nets\n");
1094 LASSERT (lnet_tbnob == 0);
1098 CFS_INIT_LIST_HEAD(&matched_nets);
1099 CFS_INIT_LIST_HEAD(¤t_nets);
1105 while (!cfs_list_empty(&raw_entries)) {
1106 tb = cfs_list_entry(raw_entries.next, lnet_text_buf_t,
1109 strncpy(source, tb->ltb_text, sizeof(source)-1);
1110 source[sizeof(source)-1] = 0;
1112 /* replace ltb_text with the network(s) add on match */
1113 rc = lnet_match_network_tokens(tb->ltb_text, ipaddrs, nip);
1117 cfs_list_del(&tb->ltb_list);
1119 if (rc == 0) { /* no match */
1120 lnet_free_text_buf(tb);
1124 /* split into separate networks */
1125 CFS_INIT_LIST_HEAD(¤t_nets);
1126 cfs_list_add(&tb->ltb_list, ¤t_nets);
1127 rc = lnet_splitnets(source, ¤t_nets);
1132 cfs_list_for_each (t, ¤t_nets) {
1133 tb = cfs_list_entry(t, lnet_text_buf_t, ltb_list);
1134 net1 = lnet_netspec2net(tb->ltb_text);
1135 LASSERT (net1 != LNET_NIDNET(LNET_NID_ANY));
1137 cfs_list_for_each(t2, &matched_nets) {
1138 tb2 = cfs_list_entry(t2, lnet_text_buf_t,
1140 net2 = lnet_netspec2net(tb2->ltb_text);
1141 LASSERT (net2 != LNET_NIDNET(LNET_NID_ANY));
1154 lnet_free_text_bufs(¤t_nets);
1158 cfs_list_for_each_safe(t, t2, ¤t_nets) {
1159 tb = cfs_list_entry(t, lnet_text_buf_t, ltb_list);
1161 cfs_list_del(&tb->ltb_list);
1162 cfs_list_add_tail(&tb->ltb_list, &matched_nets);
1164 len += snprintf(networks + len, sizeof(networks) - len,
1165 "%s%s", (len == 0) ? "" : ",",
1168 if (len >= sizeof(networks)) {
1169 CERROR("Too many matched networks\n");
1179 lnet_free_text_bufs(&raw_entries);
1180 lnet_free_text_bufs(&matched_nets);
1181 lnet_free_text_bufs(¤t_nets);
1182 LASSERT (lnet_tbnob == 0);
1187 *networksp = networks;
1193 lnet_ipaddr_free_enumeration(__u32 *ipaddrs, int nip)
1195 LIBCFS_FREE(ipaddrs, nip * sizeof(*ipaddrs));
1199 lnet_ipaddr_enumerate (__u32 **ipaddrsp)
1207 int nif = libcfs_ipif_enumerate(&ifnames);
1214 LIBCFS_ALLOC(ipaddrs, nif * sizeof(*ipaddrs));
1215 if (ipaddrs == NULL) {
1216 CERROR("Can't allocate ipaddrs[%d]\n", nif);
1217 libcfs_ipif_free_enumeration(ifnames, nif);
1221 for (i = nip = 0; i < nif; i++) {
1222 if (!strcmp(ifnames[i], "lo"))
1225 rc = libcfs_ipif_query(ifnames[i], &up,
1226 &ipaddrs[nip], &netmask);
1228 CWARN("Can't query interface %s: %d\n",
1234 CWARN("Ignoring interface %s: it's down\n",
1242 libcfs_ipif_free_enumeration(ifnames, nif);
1245 *ipaddrsp = ipaddrs;
1248 LIBCFS_ALLOC(ipaddrs2, nip * sizeof(*ipaddrs2));
1249 if (ipaddrs2 == NULL) {
1250 CERROR("Can't allocate ipaddrs[%d]\n", nip);
1253 memcpy(ipaddrs2, ipaddrs,
1254 nip * sizeof(*ipaddrs));
1255 *ipaddrsp = ipaddrs2;
1259 lnet_ipaddr_free_enumeration(ipaddrs, nif);
1265 lnet_parse_ip2nets (char **networksp, char *ip2nets)
1268 int nip = lnet_ipaddr_enumerate(&ipaddrs);
1272 LCONSOLE_ERROR_MSG(0x117, "Error %d enumerating local IP "
1273 "interfaces for ip2nets to match\n", nip);
1278 LCONSOLE_ERROR_MSG(0x118, "No local IP interfaces "
1279 "for ip2nets to match\n");
1283 rc = lnet_match_networks(networksp, ip2nets, ipaddrs, nip);
1284 lnet_ipaddr_free_enumeration(ipaddrs, nip);
1287 LCONSOLE_ERROR_MSG(0x119, "Error %d parsing ip2nets\n", rc);
1292 LCONSOLE_ERROR_MSG(0x11a, "ip2nets does not match "
1293 "any local IP interfaces\n");
1301 lnet_set_ip_niaddr (lnet_ni_t *ni)
1303 __u32 net = LNET_NIDNET(ni->ni_nid);
1312 /* Convenience for LNDs that use the IP address of a local interface as
1313 * the local address part of their NID */
1315 if (ni->ni_interfaces[0] != NULL) {
1317 CLASSERT (LNET_MAX_INTERFACES > 1);
1319 if (ni->ni_interfaces[1] != NULL) {
1320 CERROR("Net %s doesn't support multiple interfaces\n",
1321 libcfs_net2str(net));
1325 rc = libcfs_ipif_query(ni->ni_interfaces[0],
1326 &up, &ip, &netmask);
1328 CERROR("Net %s can't query interface %s: %d\n",
1329 libcfs_net2str(net), ni->ni_interfaces[0], rc);
1334 CERROR("Net %s can't use interface %s: it's down\n",
1335 libcfs_net2str(net), ni->ni_interfaces[0]);
1339 ni->ni_nid = LNET_MKNID(net, ip);
1343 n = libcfs_ipif_enumerate(&names);
1345 CERROR("Net %s can't enumerate interfaces: %d\n",
1346 libcfs_net2str(net), n);
1350 for (i = 0; i < n; i++) {
1351 if (!strcmp(names[i], "lo")) /* skip the loopback IF */
1354 rc = libcfs_ipif_query(names[i], &up, &ip, &netmask);
1357 CWARN("Net %s can't query interface %s: %d\n",
1358 libcfs_net2str(net), names[i], rc);
1363 CWARN("Net %s ignoring interface %s (down)\n",
1364 libcfs_net2str(net), names[i]);
1368 libcfs_ipif_free_enumeration(names, n);
1369 ni->ni_nid = LNET_MKNID(net, ip);
1373 CERROR("Net %s can't find any interfaces\n", libcfs_net2str(net));
1374 libcfs_ipif_free_enumeration(names, n);
1377 EXPORT_SYMBOL(lnet_set_ip_niaddr);