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 [sun.com URL with a
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 2008 Sun Microsystems, Inc. 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 struct list_head 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 struct list_head 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);
104 lnet_trimwhite(char *str)
108 while (lnet_iswhite(*str))
111 end = str + strlen(str);
113 if (!lnet_iswhite(end[-1]))
123 lnet_net_unique(__u32 net, struct list_head *nilist)
125 struct list_head *tmp;
128 list_for_each (tmp, nilist) {
129 ni = list_entry(tmp, lnet_ni_t, ni_list);
131 if (LNET_NIDNET(ni->ni_nid) == net)
139 lnet_new_ni(__u32 net, struct list_head *nilist)
143 if (!lnet_net_unique(net, nilist)) {
144 LCONSOLE_ERROR_MSG(0x111, "Duplicate network specified: %s\n",
145 libcfs_net2str(net));
149 LIBCFS_ALLOC(ni, sizeof(*ni));
151 CERROR("Out of memory creating network %s\n",
152 libcfs_net2str(net));
156 /* zero counters/flags, NULL pointers... */
157 memset(ni, 0, sizeof(*ni));
159 /* LND will fill in the address part of the NID */
160 ni->ni_nid = LNET_MKNID(net, 0);
161 CFS_INIT_LIST_HEAD(&ni->ni_txq);
163 list_add_tail(&ni->ni_list, nilist);
168 lnet_parse_networks(struct list_head *nilist, char *networks)
170 int tokensize = strlen(networks) + 1;
177 if (strlen(networks) > LNET_SINGLE_TEXTBUF_NOB) {
178 /* _WAY_ conservative */
179 LCONSOLE_ERROR_MSG(0x112, "Can't parse networks: string too "
184 LIBCFS_ALLOC(tokens, tokensize);
185 if (tokens == NULL) {
186 CERROR("Can't allocate net tokens\n");
190 the_lnet.ln_network_tokens = tokens;
191 the_lnet.ln_network_tokens_nob = tokensize;
192 memcpy (tokens, networks, tokensize);
195 /* Add in the loopback network */
196 ni = lnet_new_ni(LNET_MKNET(LOLND, 0), nilist);
200 while (str != NULL && *str != 0) {
201 char *comma = strchr(str, ',');
202 char *bracket = strchr(str, '(');
206 /* NB we don't check interface conflicts here; it's the LNDs
207 * responsibility (if it cares at all) */
209 if (bracket == NULL ||
210 (comma != NULL && comma < bracket)) {
212 /* no interface list specified */
216 net = libcfs_str2net(lnet_trimwhite(str));
218 if (net == LNET_NIDNET(LNET_NID_ANY)) {
219 lnet_syntax("networks", networks,
220 str - tokens, strlen(str));
221 LCONSOLE_ERROR_MSG(0x113, "Unrecognised network"
226 if (LNET_NETTYP(net) != LOLND && /* loopback is implicit */
227 lnet_new_ni(net, nilist) == NULL)
235 net = libcfs_str2net(lnet_trimwhite(str));
236 if (net == LNET_NIDNET(LNET_NID_ANY)) {
237 lnet_syntax("networks", networks,
238 str - tokens, strlen(str));
243 ni = lnet_new_ni(net, nilist);
250 bracket = strchr(iface, ')');
251 if (bracket == NULL) {
252 lnet_syntax("networks", networks,
253 iface - tokens, strlen(iface));
259 comma = strchr(iface, ',');
263 iface = lnet_trimwhite(iface);
265 lnet_syntax("networks", networks,
266 iface - tokens, strlen(iface));
270 if (niface == LNET_MAX_INTERFACES) {
271 LCONSOLE_ERROR_MSG(0x115, "Too many interfaces "
273 libcfs_net2str(net));
277 ni->ni_interfaces[niface++] = iface;
279 } while (iface != NULL);
282 comma = strchr(bracket + 1, ',');
285 str = lnet_trimwhite(str);
287 lnet_syntax("networks", networks,
288 str - tokens, strlen(str));
295 str = lnet_trimwhite(str);
297 lnet_syntax("networks", networks,
298 str - tokens, strlen(str));
303 LASSERT (!list_empty(nilist));
307 while (!list_empty(nilist)) {
308 ni = list_entry(nilist->next, lnet_ni_t, ni_list);
310 list_del(&ni->ni_list);
311 LIBCFS_FREE(ni, sizeof(*ni));
313 LIBCFS_FREE(tokens, tokensize);
314 the_lnet.ln_network_tokens = NULL;
320 lnet_new_text_buf (int str_len)
322 lnet_text_buf_t *ltb;
325 /* NB allocate space for the terminating 0 */
326 nob = offsetof(lnet_text_buf_t, ltb_text[str_len + 1]);
327 if (nob > LNET_SINGLE_TEXTBUF_NOB) {
328 /* _way_ conservative for "route net gateway..." */
329 CERROR("text buffer too big\n");
333 if (lnet_tbnob + nob > LNET_MAX_TEXTBUF_NOB) {
334 CERROR("Too many text buffers\n");
338 LIBCFS_ALLOC(ltb, nob);
343 ltb->ltb_text[0] = 0;
349 lnet_free_text_buf (lnet_text_buf_t *ltb)
351 lnet_tbnob -= ltb->ltb_size;
352 LIBCFS_FREE(ltb, ltb->ltb_size);
356 lnet_free_text_bufs(struct list_head *tbs)
358 lnet_text_buf_t *ltb;
360 while (!list_empty(tbs)) {
361 ltb = list_entry(tbs->next, lnet_text_buf_t, ltb_list);
363 list_del(<b->ltb_list);
364 lnet_free_text_buf(ltb);
369 lnet_print_text_bufs(struct list_head *tbs)
371 struct list_head *tmp;
372 lnet_text_buf_t *ltb;
374 list_for_each (tmp, tbs) {
375 ltb = list_entry(tmp, lnet_text_buf_t, ltb_list);
377 CDEBUG(D_WARNING, "%s\n", ltb->ltb_text);
380 CDEBUG(D_WARNING, "%d allocated\n", lnet_tbnob);
384 lnet_str2tbs_sep (struct list_head *tbs, char *str)
386 struct list_head pending;
390 lnet_text_buf_t *ltb;
392 CFS_INIT_LIST_HEAD(&pending);
394 /* Split 'str' into separate commands */
396 /* skip leading whitespace */
397 while (lnet_iswhite(*str))
400 /* scan for separator or comment */
401 for (sep = str; *sep != 0; sep++)
402 if (lnet_issep(*sep) || *sep == '#')
407 ltb = lnet_new_text_buf(nob);
409 lnet_free_text_bufs(&pending);
413 for (i = 0; i < nob; i++)
414 if (lnet_iswhite(str[i]))
415 ltb->ltb_text[i] = ' ';
417 ltb->ltb_text[i] = str[i];
419 ltb->ltb_text[nob] = 0;
421 list_add_tail(<b->ltb_list, &pending);
425 /* scan for separator */
428 } while (*sep != 0 && !lnet_issep(*sep));
437 list_splice(&pending, tbs->prev);
442 lnet_expand1tb (struct list_head *list,
443 char *str, char *sep1, char *sep2,
444 char *item, int itemlen)
446 int len1 = sep1 - str;
447 int len2 = strlen(sep2 + 1);
448 lnet_text_buf_t *ltb;
450 LASSERT (*sep1 == '[');
451 LASSERT (*sep2 == ']');
453 ltb = lnet_new_text_buf(len1 + itemlen + len2);
457 memcpy(ltb->ltb_text, str, len1);
458 memcpy(<b->ltb_text[len1], item, itemlen);
459 memcpy(<b->ltb_text[len1+itemlen], sep2 + 1, len2);
460 ltb->ltb_text[len1 + itemlen + len2] = 0;
462 list_add_tail(<b->ltb_list, list);
467 lnet_str2tbs_expand (struct list_head *tbs, char *str)
470 struct list_head pending;
482 CFS_INIT_LIST_HEAD(&pending);
484 sep = strchr(str, '[');
485 if (sep == NULL) /* nothing to expand */
488 sep2 = strchr(sep, ']');
492 for (parsed = sep; parsed < sep2; parsed = enditem) {
495 while (enditem < sep2 && *enditem != ',')
498 if (enditem == parsed) /* no empty items */
501 if (sscanf(parsed, "%d-%d/%d%n", &lo, &hi, &stride, &scanned) < 3) {
503 if (sscanf(parsed, "%d-%d%n", &lo, &hi, &scanned) < 2) {
505 /* simple string enumeration */
506 if (lnet_expand1tb(&pending, str, sep, sep2,
507 parsed, enditem - parsed) != 0)
516 /* range expansion */
518 if (enditem != parsed + scanned) /* no trailing junk */
521 if (hi < 0 || lo < 0 || stride < 0 || hi < lo ||
522 (hi - lo) % stride != 0)
525 for (i = lo; i <= hi; i += stride) {
527 snprintf(num, sizeof(num), "%d", i);
529 if (nob + 1 == sizeof(num))
532 if (lnet_expand1tb(&pending, str, sep, sep2,
538 list_splice(&pending, tbs->prev);
542 lnet_free_text_bufs(&pending);
547 lnet_parse_hops (char *str, unsigned int *hops)
549 int len = strlen(str);
552 return (sscanf(str, "%u%n", hops, &nob) >= 1 &&
554 *hops > 0 && *hops < 256);
559 lnet_parse_route (char *str, int *im_a_router)
561 /* static scratch buffer OK (single threaded) */
562 static char cmd[LNET_SINGLE_TEXTBUF_NOB];
564 struct list_head nets;
565 struct list_head gateways;
566 struct list_head *tmp1;
567 struct list_head *tmp2;
570 lnet_text_buf_t *ltb;
579 CFS_INIT_LIST_HEAD(&gateways);
580 CFS_INIT_LIST_HEAD(&nets);
582 /* save a copy of the string for error messages */
583 strncpy(cmd, str, sizeof(cmd) - 1);
584 cmd[sizeof(cmd) - 1] = 0;
588 /* scan for token start */
589 while (lnet_iswhite(*sep))
592 if (ntokens < (got_hops ? 3 : 2))
600 /* scan for token end */
601 while (*sep != 0 && !lnet_iswhite(*sep))
607 tmp2 = &nets; /* expanding nets */
608 } else if (ntokens == 2 &&
609 lnet_parse_hops(token, &hops)) {
610 got_hops = 1; /* got a hop count */
613 tmp2 = &gateways; /* expanding gateways */
616 ltb = lnet_new_text_buf(strlen(token));
620 strcpy(ltb->ltb_text, token);
621 tmp1 = <b->ltb_list;
622 list_add_tail(tmp1, tmp2);
624 while (tmp1 != tmp2) {
625 ltb = list_entry(tmp1, lnet_text_buf_t, ltb_list);
627 rc = lnet_str2tbs_expand(tmp1->next, ltb->ltb_text);
633 if (rc > 0) { /* expanded! */
634 list_del(<b->ltb_list);
635 lnet_free_text_buf(ltb);
640 net = libcfs_str2net(ltb->ltb_text);
641 if (net == LNET_NIDNET(LNET_NID_ANY) ||
642 LNET_NETTYP(net) == LOLND)
645 nid = libcfs_str2nid(ltb->ltb_text);
646 if (nid == LNET_NID_ANY ||
647 LNET_NETTYP(LNET_NIDNET(nid)) == LOLND)
656 LASSERT (!list_empty(&nets));
657 LASSERT (!list_empty(&gateways));
659 list_for_each (tmp1, &nets) {
660 ltb = list_entry(tmp1, lnet_text_buf_t, ltb_list);
661 net = libcfs_str2net(ltb->ltb_text);
662 LASSERT (net != LNET_NIDNET(LNET_NID_ANY));
664 list_for_each (tmp2, &gateways) {
665 ltb = list_entry(tmp2, lnet_text_buf_t, ltb_list);
666 nid = libcfs_str2nid(ltb->ltb_text);
667 LASSERT (nid != LNET_NID_ANY);
669 if (lnet_islocalnid(nid)) {
674 rc = lnet_add_route (net, hops, nid);
676 CERROR("Can't create route "
679 libcfs_nid2str(nid));
689 lnet_syntax("routes", cmd, token - str, strlen(token));
691 lnet_free_text_bufs(&nets);
692 lnet_free_text_bufs(&gateways);
697 lnet_parse_route_tbs(struct list_head *tbs, int *im_a_router)
699 lnet_text_buf_t *ltb;
701 while (!list_empty(tbs)) {
702 ltb = list_entry(tbs->next, lnet_text_buf_t, ltb_list);
704 if (lnet_parse_route(ltb->ltb_text, im_a_router) < 0) {
705 lnet_free_text_bufs(tbs);
709 list_del(<b->ltb_list);
710 lnet_free_text_buf(ltb);
717 lnet_parse_routes (char *routes, int *im_a_router)
719 struct list_head tbs;
724 CFS_INIT_LIST_HEAD(&tbs);
726 if (lnet_str2tbs_sep(&tbs, routes) < 0) {
727 CERROR("Error parsing routes\n");
730 rc = lnet_parse_route_tbs(&tbs, im_a_router);
733 LASSERT (lnet_tbnob == 0);
738 lnet_print_range_exprs(struct list_head *exprs)
741 lnet_range_expr_t *lre;
743 list_for_each(e, exprs) {
744 lre = list_entry(exprs->next, lnet_range_expr_t, lre_list);
746 CDEBUG(D_WARNING, "%d-%d/%d\n",
747 lre->lre_min, lre->lre_max, lre->lre_stride);
750 CDEBUG(D_WARNING, "%d allocated\n", lnet_re_alloc);
754 lnet_new_range_expr(struct list_head *exprs, int min, int max, int stride)
756 lnet_range_expr_t *lre;
758 CDEBUG(D_NET, "%d-%d/%d\n", min, max, stride);
760 if (min < 0 || min > 255 || min > max || stride < 0)
763 LIBCFS_ALLOC(lre, sizeof(*lre));
771 lre->lre_stride = stride;
773 list_add(&lre->lre_list, exprs);
778 lnet_destroy_range_exprs(struct list_head *exprs)
780 lnet_range_expr_t *lre;
782 while (!list_empty(exprs)) {
783 lre = list_entry(exprs->next, lnet_range_expr_t, lre_list);
785 list_del(&lre->lre_list);
786 LIBCFS_FREE(lre, sizeof(*lre));
792 lnet_parse_range_expr(struct list_head *exprs, char *str)
794 int nob = strlen(str);
805 if (!strcmp(str, "*")) /* match all */
806 return lnet_new_range_expr(exprs, 0, 255, 1);
809 if (sscanf(str, "%u%n", &x, &n) >= 1 && n == nob) {
811 return lnet_new_range_expr(exprs, x, x, 1);
814 /* Has to be an expansion */
815 if (!(str[0] == '[' && nob > 2 && str[nob-1] == ']'))
823 /* Comma separated list of expressions... */
824 sep = strchr(str, ',');
830 if (sscanf(str, "%u%n", &x, &n) >= 1 && n == nob) {
832 rc = lnet_new_range_expr(exprs, x, x, 1);
840 if (sscanf(str, "%u-%u%n", &x, &y, &n) >= 2 && n == nob) {
842 rc = lnet_new_range_expr(exprs, x, y, 1);
849 if (sscanf(str, "%u-%u/%u%n", &x, &y, &z, &n) >= 3 && n == nob) {
851 rc = lnet_new_range_expr(exprs, x, y, z);
859 } while ((str = sep) != NULL);
865 lnet_match_network_token(char *token, __u32 *ipaddrs, int nip)
867 struct list_head exprs[4];
869 lnet_range_expr_t *re;
878 for (i = 0; i < 4; i++)
879 CFS_INIT_LIST_HEAD(&exprs[i]);
881 for (i = 0; i < 4; i++) {
884 token = strchr(token, '.');
892 rc = lnet_parse_range_expr(&exprs[i], str);
899 for (match = i = 0; !match && i < nip; i++) {
902 for (match = 1, j = 0; match && j < 4; j++) {
903 n = (ip >> (8 * (3 - j))) & 0xff;
906 list_for_each(e, &exprs[j]) {
907 re = list_entry(e, lnet_range_expr_t, lre_list);
909 if (re->lre_min <= n &&
911 (n - re->lre_min) % re->lre_stride == 0) {
922 for (i = 0; i < 4; i++)
923 lnet_destroy_range_exprs(&exprs[i]);
924 LASSERT (lnet_re_alloc == 0);
930 lnet_match_network_tokens(char *net_entry, __u32 *ipaddrs, int nip)
932 static char tokens[LNET_SINGLE_TEXTBUF_NOB];
942 LASSERT (strlen(net_entry) < sizeof(tokens));
944 /* work on a copy of the string */
945 strcpy(tokens, net_entry);
948 /* scan for token start */
949 while (lnet_iswhite(*sep))
956 /* scan for token end */
957 while (*sep != 0 && !lnet_iswhite(*sep))
962 if (ntokens++ == 0) {
969 rc = lnet_match_network_token(token, ipaddrs, nip);
971 lnet_syntax("ip2nets", net_entry,
972 token - tokens, len);
976 matched |= (rc != 0);
982 strcpy(net_entry, net); /* replace with matched net */
987 lnet_netspec2net(char *netspec)
989 char *bracket = strchr(netspec, '(');
995 net = libcfs_str2net(netspec);
1004 lnet_splitnets(char *source, struct list_head *nets)
1009 lnet_text_buf_t *tb;
1010 lnet_text_buf_t *tb2;
1011 struct list_head *t;
1016 LASSERT (!list_empty(nets));
1017 LASSERT (nets->next == nets->prev); /* single entry */
1019 tb = list_entry(nets->next, lnet_text_buf_t, ltb_list);
1022 sep = strchr(tb->ltb_text, ',');
1023 bracket = strchr(tb->ltb_text, '(');
1028 /* netspec lists interfaces... */
1030 offset2 = offset + (bracket - tb->ltb_text);
1031 len = strlen(bracket);
1033 bracket = strchr(bracket + 1, ')');
1035 if (bracket == NULL ||
1036 !(bracket[1] == ',' || bracket[1] == 0)) {
1037 lnet_syntax("ip2nets", source, offset2, len);
1041 sep = (bracket[1] == 0) ? NULL : bracket + 1;
1047 net = lnet_netspec2net(tb->ltb_text);
1048 if (net == LNET_NIDNET(LNET_NID_ANY)) {
1049 lnet_syntax("ip2nets", source, offset,
1050 strlen(tb->ltb_text));
1054 list_for_each(t, nets) {
1055 tb2 = list_entry(t, lnet_text_buf_t, ltb_list);
1060 if (net == lnet_netspec2net(tb2->ltb_text)) {
1061 /* duplicate network */
1062 lnet_syntax("ip2nets", source, offset,
1063 strlen(tb->ltb_text));
1071 offset += sep - tb->ltb_text;
1072 tb2 = lnet_new_text_buf(strlen(sep));
1076 strcpy(tb2->ltb_text, sep);
1077 list_add_tail(&tb2->ltb_list, nets);
1084 lnet_match_networks (char **networksp, char *ip2nets, __u32 *ipaddrs, int nip)
1086 static char networks[LNET_SINGLE_TEXTBUF_NOB];
1087 static char source[LNET_SINGLE_TEXTBUF_NOB];
1089 struct list_head raw_entries;
1090 struct list_head matched_nets;
1091 struct list_head current_nets;
1092 struct list_head *t;
1093 struct list_head *t2;
1094 lnet_text_buf_t *tb;
1095 lnet_text_buf_t *tb2;
1103 CFS_INIT_LIST_HEAD(&raw_entries);
1104 if (lnet_str2tbs_sep(&raw_entries, ip2nets) < 0) {
1105 CERROR("Error parsing ip2nets\n");
1106 LASSERT (lnet_tbnob == 0);
1110 CFS_INIT_LIST_HEAD(&matched_nets);
1111 CFS_INIT_LIST_HEAD(¤t_nets);
1117 while (!list_empty(&raw_entries)) {
1118 tb = list_entry(raw_entries.next, lnet_text_buf_t, ltb_list);
1120 strncpy(source, tb->ltb_text, sizeof(source)-1);
1121 source[sizeof(source)-1] = 0;
1123 /* replace ltb_text with the network(s) add on match */
1124 rc = lnet_match_network_tokens(tb->ltb_text, ipaddrs, nip);
1128 list_del(&tb->ltb_list);
1130 if (rc == 0) { /* no match */
1131 lnet_free_text_buf(tb);
1135 /* split into separate networks */
1136 CFS_INIT_LIST_HEAD(¤t_nets);
1137 list_add(&tb->ltb_list, ¤t_nets);
1138 rc = lnet_splitnets(source, ¤t_nets);
1143 list_for_each (t, ¤t_nets) {
1144 tb = list_entry(t, lnet_text_buf_t, ltb_list);
1145 net1 = lnet_netspec2net(tb->ltb_text);
1146 LASSERT (net1 != LNET_NIDNET(LNET_NID_ANY));
1148 list_for_each(t2, &matched_nets) {
1149 tb2 = list_entry(t2, lnet_text_buf_t, ltb_list);
1150 net2 = lnet_netspec2net(tb2->ltb_text);
1151 LASSERT (net2 != LNET_NIDNET(LNET_NID_ANY));
1164 lnet_free_text_bufs(¤t_nets);
1168 list_for_each_safe(t, t2, ¤t_nets) {
1169 tb = list_entry(t, lnet_text_buf_t, ltb_list);
1171 list_del(&tb->ltb_list);
1172 list_add_tail(&tb->ltb_list, &matched_nets);
1174 len += snprintf(networks + len, sizeof(networks) - len,
1175 "%s%s", (len == 0) ? "" : ",",
1178 if (len >= sizeof(networks)) {
1179 CERROR("Too many matched networks\n");
1189 lnet_free_text_bufs(&raw_entries);
1190 lnet_free_text_bufs(&matched_nets);
1191 lnet_free_text_bufs(¤t_nets);
1192 LASSERT (lnet_tbnob == 0);
1197 *networksp = networks;
1203 lnet_ipaddr_free_enumeration(__u32 *ipaddrs, int nip)
1205 LIBCFS_FREE(ipaddrs, nip * sizeof(*ipaddrs));
1209 lnet_ipaddr_enumerate (__u32 **ipaddrsp)
1217 int nif = libcfs_ipif_enumerate(&ifnames);
1224 LIBCFS_ALLOC(ipaddrs, nif * sizeof(*ipaddrs));
1225 if (ipaddrs == NULL) {
1226 CERROR("Can't allocate ipaddrs[%d]\n", nif);
1227 libcfs_ipif_free_enumeration(ifnames, nif);
1231 for (i = nip = 0; i < nif; i++) {
1232 if (!strcmp(ifnames[i], "lo"))
1235 rc = libcfs_ipif_query(ifnames[i], &up,
1236 &ipaddrs[nip], &netmask);
1238 CWARN("Can't query interface %s: %d\n",
1244 CWARN("Ignoring interface %s: it's down\n",
1252 libcfs_ipif_free_enumeration(ifnames, nif);
1255 *ipaddrsp = ipaddrs;
1258 LIBCFS_ALLOC(ipaddrs2, nip * sizeof(*ipaddrs2));
1259 if (ipaddrs2 == NULL) {
1260 CERROR("Can't allocate ipaddrs[%d]\n", nip);
1263 memcpy(ipaddrs2, ipaddrs,
1264 nip * sizeof(*ipaddrs));
1265 *ipaddrsp = ipaddrs2;
1269 lnet_ipaddr_free_enumeration(ipaddrs, nif);
1275 lnet_parse_ip2nets (char **networksp, char *ip2nets)
1278 int nip = lnet_ipaddr_enumerate(&ipaddrs);
1282 LCONSOLE_ERROR_MSG(0x117, "Error %d enumerating local IP "
1283 "interfaces for ip2nets to match\n", nip);
1288 LCONSOLE_ERROR_MSG(0x118, "No local IP interfaces "
1289 "for ip2nets to match\n");
1293 rc = lnet_match_networks(networksp, ip2nets, ipaddrs, nip);
1294 lnet_ipaddr_free_enumeration(ipaddrs, nip);
1297 LCONSOLE_ERROR_MSG(0x119, "Error %d parsing ip2nets\n", rc);
1302 LCONSOLE_ERROR_MSG(0x11a, "ip2nets does not match "
1303 "any local IP interfaces\n");
1311 lnet_set_ip_niaddr (lnet_ni_t *ni)
1313 __u32 net = LNET_NIDNET(ni->ni_nid);
1322 /* Convenience for LNDs that use the IP address of a local interface as
1323 * the local address part of their NID */
1325 if (ni->ni_interfaces[0] != NULL) {
1327 CLASSERT (LNET_MAX_INTERFACES > 1);
1329 if (ni->ni_interfaces[1] != NULL) {
1330 CERROR("Net %s doesn't support multiple interfaces\n",
1331 libcfs_net2str(net));
1335 rc = libcfs_ipif_query(ni->ni_interfaces[0],
1336 &up, &ip, &netmask);
1338 CERROR("Net %s can't query interface %s: %d\n",
1339 libcfs_net2str(net), ni->ni_interfaces[0], rc);
1344 CERROR("Net %s can't use interface %s: it's down\n",
1345 libcfs_net2str(net), ni->ni_interfaces[0]);
1349 ni->ni_nid = LNET_MKNID(net, ip);
1353 n = libcfs_ipif_enumerate(&names);
1355 CERROR("Net %s can't enumerate interfaces: %d\n",
1356 libcfs_net2str(net), n);
1360 for (i = 0; i < n; i++) {
1361 if (!strcmp(names[i], "lo")) /* skip the loopback IF */
1364 rc = libcfs_ipif_query(names[i], &up, &ip, &netmask);
1367 CWARN("Net %s can't query interface %s: %d\n",
1368 libcfs_net2str(net), names[i], rc);
1373 CWARN("Net %s ignoring interface %s (down)\n",
1374 libcfs_net2str(net), names[i]);
1378 libcfs_ipif_free_enumeration(names, n);
1379 ni->ni_nid = LNET_MKNID(net, ip);
1383 CERROR("Net %s can't find any interfaces\n", libcfs_net2str(net));
1384 libcfs_ipif_free_enumeration(names, n);
1387 EXPORT_SYMBOL(lnet_set_ip_niaddr);