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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, Intel Corporation.
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 /* tmp struct for parsing routes */
41 struct lnet_text_buf {
42 struct list_head ltb_list; /* stash on lists */
43 int ltb_size; /* allocated size */
44 char ltb_text[0]; /* text buffer */
47 static int lnet_tbnob = 0; /* track text buf allocation */
48 #define LNET_MAX_TEXTBUF_NOB (64<<10) /* bound allocation */
49 #define LNET_SINGLE_TEXTBUF_NOB (4<<10)
52 lnet_syntax(char *name, char *str, int offset, int width)
54 static char dots[LNET_SINGLE_TEXTBUF_NOB];
55 static char dashes[LNET_SINGLE_TEXTBUF_NOB];
57 memset(dots, '.', sizeof(dots));
58 dots[sizeof(dots)-1] = 0;
59 memset(dashes, '-', sizeof(dashes));
60 dashes[sizeof(dashes)-1] = 0;
62 LCONSOLE_ERROR_MSG(0x10f, "Error parsing '%s=\"%s\"'\n", name, str);
63 LCONSOLE_ERROR_MSG(0x110, "here...........%.*s..%.*s|%.*s|\n",
64 (int)strlen(name), dots, offset, dots,
65 (width < 1) ? 0 : width - 1, dashes);
82 lnet_net_unique(__u32 net, struct list_head *nilist)
84 struct list_head *tmp;
87 list_for_each(tmp, nilist) {
88 ni = list_entry(tmp, lnet_ni_t, ni_list);
90 if (LNET_NIDNET(ni->ni_nid) == net)
98 lnet_ni_free(struct lnet_ni *ni)
102 if (ni->ni_refs != NULL)
103 cfs_percpt_free(ni->ni_refs);
105 if (ni->ni_tx_queues != NULL)
106 cfs_percpt_free(ni->ni_tx_queues);
108 if (ni->ni_cpts != NULL)
109 cfs_expr_list_values_free(ni->ni_cpts, ni->ni_ncpts);
112 # ifdef HAVE_LIBPTHREAD
113 pthread_mutex_destroy(&ni->ni_lock);
116 for (i = 0; i < LNET_MAX_INTERFACES &&
117 ni->ni_interfaces[i] != NULL; i++) {
118 LIBCFS_FREE(ni->ni_interfaces[i],
119 strlen(ni->ni_interfaces[i]) + 1);
121 LIBCFS_FREE(ni, sizeof(*ni));
125 lnet_ni_alloc(__u32 net, struct cfs_expr_list *el, struct list_head *nilist)
127 struct lnet_tx_queue *tq;
132 if (!lnet_net_unique(net, nilist)) {
133 LCONSOLE_ERROR_MSG(0x111, "Duplicate network specified: %s\n",
134 libcfs_net2str(net));
138 LIBCFS_ALLOC(ni, sizeof(*ni));
140 CERROR("Out of memory creating network %s\n",
141 libcfs_net2str(net));
146 spin_lock_init(&ni->ni_lock);
148 # ifdef HAVE_LIBPTHREAD
149 pthread_mutex_init(&ni->ni_lock, NULL);
152 INIT_LIST_HEAD(&ni->ni_cptlist);
153 ni->ni_refs = cfs_percpt_alloc(lnet_cpt_table(),
154 sizeof(*ni->ni_refs[0]));
155 if (ni->ni_refs == NULL)
158 ni->ni_tx_queues = cfs_percpt_alloc(lnet_cpt_table(),
159 sizeof(*ni->ni_tx_queues[0]));
160 if (ni->ni_tx_queues == NULL)
163 cfs_percpt_for_each(tq, i, ni->ni_tx_queues)
164 INIT_LIST_HEAD(&tq->tq_delayed);
168 ni->ni_ncpts = LNET_CPT_NUMBER;
170 rc = cfs_expr_list_values(el, LNET_CPT_NUMBER, &ni->ni_cpts);
172 CERROR("Failed to set CPTs for NI %s: %d\n",
173 libcfs_net2str(net), rc);
177 LASSERT(rc <= LNET_CPT_NUMBER);
178 if (rc == LNET_CPT_NUMBER) {
179 LIBCFS_FREE(ni->ni_cpts, rc * sizeof(ni->ni_cpts[0]));
186 /* LND will fill in the address part of the NID */
187 ni->ni_nid = LNET_MKNID(net, 0);
188 ni->ni_last_alive = cfs_time_current_sec();
189 list_add_tail(&ni->ni_list, nilist);
197 lnet_parse_networks(struct list_head *nilist, char *networks)
199 struct cfs_expr_list *el = NULL;
208 if (networks == NULL) {
209 CERROR("networks string is undefined\n");
213 if (strlen(networks) > LNET_SINGLE_TEXTBUF_NOB) {
214 /* _WAY_ conservative */
215 LCONSOLE_ERROR_MSG(0x112, "Can't parse networks: string too "
220 tokensize = strlen(networks) + 1;
222 LIBCFS_ALLOC(tokens, tokensize);
223 if (tokens == NULL) {
224 CERROR("Can't allocate net tokens\n");
228 memcpy(tokens, networks, tokensize);
231 /* Add in the loopback network */
232 ni = lnet_ni_alloc(LNET_MKNET(LOLND, 0), NULL, nilist);
236 while (str != NULL && *str != 0) {
237 char *comma = strchr(str, ',');
238 char *bracket = strchr(str, '(');
239 char *square = strchr(str, '[');
244 /* NB we don't check interface conflicts here; it's the LNDs
245 * responsibility (if it cares at all) */
247 if (square != NULL && (comma == NULL || square < comma)) {
248 /* i.e: o2ib0(ib0)[1,2], number between square
249 * brackets are CPTs this NI needs to be bond */
250 if (bracket != NULL && bracket > square) {
255 tmp = strchr(square, ']');
261 rc = cfs_expr_list_parse(square, tmp - square + 1,
262 0, LNET_CPT_NUMBER - 1, &el);
268 while (square <= tmp)
272 if (bracket == NULL ||
273 (comma != NULL && comma < bracket)) {
275 /* no interface list specified */
279 net = libcfs_str2net(cfs_trimwhite(str));
281 if (net == LNET_NIDNET(LNET_NID_ANY)) {
282 LCONSOLE_ERROR_MSG(0x113, "Unrecognised network"
288 if (LNET_NETTYP(net) != LOLND && /* LO is implicit */
289 lnet_ni_alloc(net, el, nilist) == NULL)
293 cfs_expr_list_free(el);
302 net = libcfs_str2net(cfs_trimwhite(str));
303 if (net == LNET_NIDNET(LNET_NID_ANY)) {
309 ni = lnet_ni_alloc(net, el, nilist);
314 cfs_expr_list_free(el);
321 bracket = strchr(iface, ')');
322 if (bracket == NULL) {
329 comma = strchr(iface, ',');
333 iface = cfs_trimwhite(iface);
339 if (niface == LNET_MAX_INTERFACES) {
340 LCONSOLE_ERROR_MSG(0x115, "Too many interfaces "
342 libcfs_net2str(net));
346 /* Allocate a seperate piece of memory and copy
347 * into it the string, so we don't have
348 * a depencency on the tokens string. This way we
349 * can free the tokens at the end of the function.
350 * The newly allocated ni_interfaces[] can be
351 * freed when freeing the NI */
352 LIBCFS_ALLOC(ni->ni_interfaces[niface],
354 if (ni->ni_interfaces[niface] == NULL) {
355 CERROR("Can't allocate net interface name\n");
358 strncpy(ni->ni_interfaces[niface], iface,
362 } while (iface != NULL);
365 comma = strchr(bracket + 1, ',');
368 str = cfs_trimwhite(str);
377 str = cfs_trimwhite(str);
384 LASSERT(!list_empty(nilist));
386 LIBCFS_FREE(tokens, tokensize);
390 lnet_syntax("networks", networks, (int)(tmp - tokens), strlen(tmp));
392 while (!list_empty(nilist)) {
393 ni = list_entry(nilist->next, lnet_ni_t, ni_list);
395 list_del(&ni->ni_list);
400 cfs_expr_list_free(el);
402 LIBCFS_FREE(tokens, tokensize);
407 struct lnet_text_buf *lnet_new_text_buf(int str_len)
409 struct lnet_text_buf *ltb;
412 /* NB allocate space for the terminating 0 */
413 nob = offsetof(struct lnet_text_buf, ltb_text[str_len + 1]);
414 if (nob > LNET_SINGLE_TEXTBUF_NOB) {
415 /* _way_ conservative for "route net gateway..." */
416 CERROR("text buffer too big\n");
420 if (lnet_tbnob + nob > LNET_MAX_TEXTBUF_NOB) {
421 CERROR("Too many text buffers\n");
425 LIBCFS_ALLOC(ltb, nob);
430 ltb->ltb_text[0] = 0;
436 lnet_free_text_buf(struct lnet_text_buf *ltb)
438 lnet_tbnob -= ltb->ltb_size;
439 LIBCFS_FREE(ltb, ltb->ltb_size);
443 lnet_free_text_bufs(struct list_head *tbs)
445 struct lnet_text_buf *ltb;
447 while (!list_empty(tbs)) {
448 ltb = list_entry(tbs->next, struct lnet_text_buf, ltb_list);
450 list_del(<b->ltb_list);
451 lnet_free_text_buf(ltb);
456 lnet_print_text_bufs(struct list_head *tbs)
458 struct list_head *tmp;
459 struct lnet_text_buf *ltb;
461 list_for_each(tmp, tbs) {
462 ltb = list_entry(tmp, struct lnet_text_buf, ltb_list);
464 CDEBUG(D_WARNING, "%s\n", ltb->ltb_text);
467 CDEBUG(D_WARNING, "%d allocated\n", lnet_tbnob);
471 lnet_str2tbs_sep(struct list_head *tbs, char *str)
473 struct list_head pending;
477 struct lnet_text_buf *ltb;
479 INIT_LIST_HEAD(&pending);
481 /* Split 'str' into separate commands */
483 /* skip leading whitespace */
484 while (cfs_iswhite(*str))
487 /* scan for separator or comment */
488 for (sep = str; *sep != 0; sep++)
489 if (lnet_issep(*sep) || *sep == '#')
492 nob = (int)(sep - str);
494 ltb = lnet_new_text_buf(nob);
496 lnet_free_text_bufs(&pending);
500 for (i = 0; i < nob; i++)
501 if (cfs_iswhite(str[i]))
502 ltb->ltb_text[i] = ' ';
504 ltb->ltb_text[i] = str[i];
506 ltb->ltb_text[nob] = 0;
508 list_add_tail(<b->ltb_list, &pending);
512 /* scan for separator */
515 } while (*sep != 0 && !lnet_issep(*sep));
524 list_splice(&pending, tbs->prev);
529 lnet_expand1tb(struct list_head *list,
530 char *str, char *sep1, char *sep2,
531 char *item, int itemlen)
533 int len1 = (int)(sep1 - str);
534 int len2 = strlen(sep2 + 1);
535 struct lnet_text_buf *ltb;
537 LASSERT (*sep1 == '[');
538 LASSERT (*sep2 == ']');
540 ltb = lnet_new_text_buf(len1 + itemlen + len2);
544 memcpy(ltb->ltb_text, str, len1);
545 memcpy(<b->ltb_text[len1], item, itemlen);
546 memcpy(<b->ltb_text[len1+itemlen], sep2 + 1, len2);
547 ltb->ltb_text[len1 + itemlen + len2] = 0;
549 list_add_tail(<b->ltb_list, list);
554 lnet_str2tbs_expand(struct list_head *tbs, char *str)
557 struct list_head pending;
569 INIT_LIST_HEAD(&pending);
571 sep = strchr(str, '[');
572 if (sep == NULL) /* nothing to expand */
575 sep2 = strchr(sep, ']');
579 for (parsed = sep; parsed < sep2; parsed = enditem) {
582 while (enditem < sep2 && *enditem != ',')
585 if (enditem == parsed) /* no empty items */
588 if (sscanf(parsed, "%d-%d/%d%n", &lo, &hi, &stride, &scanned) < 3) {
590 if (sscanf(parsed, "%d-%d%n", &lo, &hi, &scanned) < 2) {
592 /* simple string enumeration */
593 if (lnet_expand1tb(&pending, str, sep, sep2,
594 parsed, (int)(enditem - parsed)) != 0)
603 /* range expansion */
605 if (enditem != parsed + scanned) /* no trailing junk */
608 if (hi < 0 || lo < 0 || stride < 0 || hi < lo ||
609 (hi - lo) % stride != 0)
612 for (i = lo; i <= hi; i += stride) {
614 snprintf(num, sizeof(num), "%d", i);
616 if (nob + 1 == sizeof(num))
619 if (lnet_expand1tb(&pending, str, sep, sep2,
625 list_splice(&pending, tbs->prev);
629 lnet_free_text_bufs(&pending);
634 lnet_parse_hops (char *str, unsigned int *hops)
636 int len = strlen(str);
639 return (sscanf(str, "%u%n", hops, &nob) >= 1 &&
641 *hops > 0 && *hops < 256);
644 #define LNET_PRIORITY_SEPARATOR (':')
647 lnet_parse_priority(char *str, unsigned int *priority, char **token)
653 sep = strchr(str, LNET_PRIORITY_SEPARATOR);
658 len = strlen(sep + 1);
660 if ((sscanf((sep+1), "%u%n", priority, &nob) < 1) || (len != nob)) {
661 /* Update the caller's token pointer so it treats the found
662 priority as the token to report in the error message. */
663 *token += sep - str + 1;
667 CDEBUG(D_NET, "gateway %s, priority %d, nob %d\n", str, *priority, nob);
670 * Change priority separator to \0 to be able to parse NID
677 lnet_parse_route (char *str, int *im_a_router)
679 /* static scratch buffer OK (single threaded) */
680 static char cmd[LNET_SINGLE_TEXTBUF_NOB];
682 struct list_head nets;
683 struct list_head gateways;
684 struct list_head *tmp1;
685 struct list_head *tmp2;
688 struct lnet_text_buf *ltb;
696 unsigned int priority = 0;
698 INIT_LIST_HEAD(&gateways);
699 INIT_LIST_HEAD(&nets);
701 /* save a copy of the string for error messages */
702 strncpy(cmd, str, sizeof(cmd));
703 cmd[sizeof(cmd) - 1] = '\0';
707 /* scan for token start */
708 while (cfs_iswhite(*sep))
711 if (ntokens < (got_hops ? 3 : 2))
719 /* scan for token end */
720 while (*sep != 0 && !cfs_iswhite(*sep))
726 tmp2 = &nets; /* expanding nets */
727 } else if (ntokens == 2 &&
728 lnet_parse_hops(token, &hops)) {
729 got_hops = 1; /* got a hop count */
732 tmp2 = &gateways; /* expanding gateways */
735 ltb = lnet_new_text_buf(strlen(token));
739 strcpy(ltb->ltb_text, token);
740 tmp1 = <b->ltb_list;
741 list_add_tail(tmp1, tmp2);
743 while (tmp1 != tmp2) {
744 ltb = list_entry(tmp1, struct lnet_text_buf, ltb_list);
746 rc = lnet_str2tbs_expand(tmp1->next, ltb->ltb_text);
752 if (rc > 0) { /* expanded! */
753 list_del(<b->ltb_list);
754 lnet_free_text_buf(ltb);
759 net = libcfs_str2net(ltb->ltb_text);
760 if (net == LNET_NIDNET(LNET_NID_ANY) ||
761 LNET_NETTYP(net) == LOLND)
764 rc = lnet_parse_priority(ltb->ltb_text,
769 nid = libcfs_str2nid(ltb->ltb_text);
770 if (nid == LNET_NID_ANY ||
771 LNET_NETTYP(LNET_NIDNET(nid)) == LOLND)
780 LASSERT(!list_empty(&nets));
781 LASSERT(!list_empty(&gateways));
783 list_for_each(tmp1, &nets) {
784 ltb = list_entry(tmp1, struct lnet_text_buf, ltb_list);
785 net = libcfs_str2net(ltb->ltb_text);
786 LASSERT (net != LNET_NIDNET(LNET_NID_ANY));
788 list_for_each(tmp2, &gateways) {
789 ltb = list_entry(tmp2, struct lnet_text_buf, ltb_list);
790 nid = libcfs_str2nid(ltb->ltb_text);
791 LASSERT(nid != LNET_NID_ANY);
793 if (lnet_islocalnid(nid)) {
798 rc = lnet_add_route(net, hops, nid, priority);
800 CERROR("Can't create route "
803 libcfs_nid2str(nid));
813 lnet_syntax("routes", cmd, (int)(token - str), strlen(token));
815 lnet_free_text_bufs(&nets);
816 lnet_free_text_bufs(&gateways);
821 lnet_parse_route_tbs(struct list_head *tbs, int *im_a_router)
823 struct lnet_text_buf *ltb;
825 while (!list_empty(tbs)) {
826 ltb = list_entry(tbs->next, struct lnet_text_buf, ltb_list);
828 if (lnet_parse_route(ltb->ltb_text, im_a_router) < 0) {
829 lnet_free_text_bufs(tbs);
833 list_del(<b->ltb_list);
834 lnet_free_text_buf(ltb);
841 lnet_parse_routes (char *routes, int *im_a_router)
843 struct list_head tbs;
848 INIT_LIST_HEAD(&tbs);
850 if (lnet_str2tbs_sep(&tbs, routes) < 0) {
851 CERROR("Error parsing routes\n");
854 rc = lnet_parse_route_tbs(&tbs, im_a_router);
857 LASSERT (lnet_tbnob == 0);
862 lnet_match_network_token(char *token, int len, __u32 *ipaddrs, int nip)
864 struct list_head list = LIST_HEAD_INIT(list);
868 rc = cfs_ip_addr_parse(token, len, &list);
872 for (rc = i = 0; !rc && i < nip; i++)
873 rc = cfs_ip_addr_match(ipaddrs[i], &list);
875 cfs_ip_addr_free(&list);
881 lnet_match_network_tokens(char *net_entry, __u32 *ipaddrs, int nip)
883 static char tokens[LNET_SINGLE_TEXTBUF_NOB];
893 LASSERT (strlen(net_entry) < sizeof(tokens));
895 /* work on a copy of the string */
896 strcpy(tokens, net_entry);
899 /* scan for token start */
900 while (cfs_iswhite(*sep))
907 /* scan for token end */
908 while (*sep != 0 && !cfs_iswhite(*sep))
913 if (ntokens++ == 0) {
920 rc = lnet_match_network_token(token, len, ipaddrs, nip);
922 lnet_syntax("ip2nets", net_entry,
923 (int)(token - tokens), len);
927 matched |= (rc != 0);
933 strcpy(net_entry, net); /* replace with matched net */
938 lnet_netspec2net(char *netspec)
940 char *bracket = strchr(netspec, '(');
946 net = libcfs_str2net(netspec);
955 lnet_splitnets(char *source, struct list_head *nets)
960 struct lnet_text_buf *tb;
961 struct lnet_text_buf *tb2;
967 LASSERT(!list_empty(nets));
968 LASSERT(nets->next == nets->prev); /* single entry */
970 tb = list_entry(nets->next, struct lnet_text_buf, ltb_list);
973 sep = strchr(tb->ltb_text, ',');
974 bracket = strchr(tb->ltb_text, '(');
979 /* netspec lists interfaces... */
981 offset2 = offset + (int)(bracket - tb->ltb_text);
982 len = strlen(bracket);
984 bracket = strchr(bracket + 1, ')');
986 if (bracket == NULL ||
987 !(bracket[1] == ',' || bracket[1] == 0)) {
988 lnet_syntax("ip2nets", source, offset2, len);
992 sep = (bracket[1] == 0) ? NULL : bracket + 1;
998 net = lnet_netspec2net(tb->ltb_text);
999 if (net == LNET_NIDNET(LNET_NID_ANY)) {
1000 lnet_syntax("ip2nets", source, offset,
1001 strlen(tb->ltb_text));
1005 list_for_each(t, nets) {
1006 tb2 = list_entry(t, struct lnet_text_buf, ltb_list);
1011 if (net == lnet_netspec2net(tb2->ltb_text)) {
1012 /* duplicate network */
1013 lnet_syntax("ip2nets", source, offset,
1014 strlen(tb->ltb_text));
1022 offset += (int)(sep - tb->ltb_text);
1024 tb2 = lnet_new_text_buf(len);
1028 strncpy(tb2->ltb_text, sep, len);
1029 tb2->ltb_text[len] = '\0';
1030 list_add_tail(&tb2->ltb_list, nets);
1037 lnet_match_networks (char **networksp, char *ip2nets, __u32 *ipaddrs, int nip)
1039 static char networks[LNET_SINGLE_TEXTBUF_NOB];
1040 static char source[LNET_SINGLE_TEXTBUF_NOB];
1042 struct list_head raw_entries;
1043 struct list_head matched_nets;
1044 struct list_head current_nets;
1045 struct list_head *t;
1046 struct list_head *t2;
1047 struct lnet_text_buf *tb;
1048 struct lnet_text_buf *tb2;
1056 INIT_LIST_HEAD(&raw_entries);
1057 if (lnet_str2tbs_sep(&raw_entries, ip2nets) < 0) {
1058 CERROR("Error parsing ip2nets\n");
1059 LASSERT (lnet_tbnob == 0);
1063 INIT_LIST_HEAD(&matched_nets);
1064 INIT_LIST_HEAD(¤t_nets);
1070 while (!list_empty(&raw_entries)) {
1071 tb = list_entry(raw_entries.next, struct lnet_text_buf,
1074 strncpy(source, tb->ltb_text, sizeof(source));
1075 source[sizeof(source) - 1] = '\0';
1077 /* replace ltb_text with the network(s) add on match */
1078 rc = lnet_match_network_tokens(tb->ltb_text, ipaddrs, nip);
1082 list_del(&tb->ltb_list);
1084 if (rc == 0) { /* no match */
1085 lnet_free_text_buf(tb);
1089 /* split into separate networks */
1090 INIT_LIST_HEAD(¤t_nets);
1091 list_add(&tb->ltb_list, ¤t_nets);
1092 rc = lnet_splitnets(source, ¤t_nets);
1097 list_for_each(t, ¤t_nets) {
1098 tb = list_entry(t, struct lnet_text_buf, ltb_list);
1099 net1 = lnet_netspec2net(tb->ltb_text);
1100 LASSERT(net1 != LNET_NIDNET(LNET_NID_ANY));
1102 list_for_each(t2, &matched_nets) {
1103 tb2 = list_entry(t2, struct lnet_text_buf,
1105 net2 = lnet_netspec2net(tb2->ltb_text);
1106 LASSERT(net2 != LNET_NIDNET(LNET_NID_ANY));
1119 lnet_free_text_bufs(¤t_nets);
1123 list_for_each_safe(t, t2, ¤t_nets) {
1124 tb = list_entry(t, struct lnet_text_buf, ltb_list);
1126 list_del(&tb->ltb_list);
1127 list_add_tail(&tb->ltb_list, &matched_nets);
1129 len += snprintf(networks + len, sizeof(networks) - len,
1130 "%s%s", (len == 0) ? "" : ",",
1133 if (len >= sizeof(networks)) {
1134 CERROR("Too many matched networks\n");
1144 lnet_free_text_bufs(&raw_entries);
1145 lnet_free_text_bufs(&matched_nets);
1146 lnet_free_text_bufs(¤t_nets);
1147 LASSERT (lnet_tbnob == 0);
1152 *networksp = networks;
1158 lnet_ipaddr_free_enumeration(__u32 *ipaddrs, int nip)
1160 LIBCFS_FREE(ipaddrs, nip * sizeof(*ipaddrs));
1164 lnet_ipaddr_enumerate (__u32 **ipaddrsp)
1172 int nif = libcfs_ipif_enumerate(&ifnames);
1179 LIBCFS_ALLOC(ipaddrs, nif * sizeof(*ipaddrs));
1180 if (ipaddrs == NULL) {
1181 CERROR("Can't allocate ipaddrs[%d]\n", nif);
1182 libcfs_ipif_free_enumeration(ifnames, nif);
1186 for (i = nip = 0; i < nif; i++) {
1187 if (!strcmp(ifnames[i], "lo"))
1190 rc = libcfs_ipif_query(ifnames[i], &up,
1191 &ipaddrs[nip], &netmask);
1193 CWARN("Can't query interface %s: %d\n",
1199 CWARN("Ignoring interface %s: it's down\n",
1207 libcfs_ipif_free_enumeration(ifnames, nif);
1210 *ipaddrsp = ipaddrs;
1213 LIBCFS_ALLOC(ipaddrs2, nip * sizeof(*ipaddrs2));
1214 if (ipaddrs2 == NULL) {
1215 CERROR("Can't allocate ipaddrs[%d]\n", nip);
1218 memcpy(ipaddrs2, ipaddrs,
1219 nip * sizeof(*ipaddrs));
1220 *ipaddrsp = ipaddrs2;
1224 lnet_ipaddr_free_enumeration(ipaddrs, nif);
1230 lnet_parse_ip2nets (char **networksp, char *ip2nets)
1233 int nip = lnet_ipaddr_enumerate(&ipaddrs);
1237 LCONSOLE_ERROR_MSG(0x117, "Error %d enumerating local IP "
1238 "interfaces for ip2nets to match\n", nip);
1243 LCONSOLE_ERROR_MSG(0x118, "No local IP interfaces "
1244 "for ip2nets to match\n");
1248 rc = lnet_match_networks(networksp, ip2nets, ipaddrs, nip);
1249 lnet_ipaddr_free_enumeration(ipaddrs, nip);
1252 LCONSOLE_ERROR_MSG(0x119, "Error %d parsing ip2nets\n", rc);
1257 LCONSOLE_ERROR_MSG(0x11a, "ip2nets does not match "
1258 "any local IP interfaces\n");
1266 lnet_set_ip_niaddr (lnet_ni_t *ni)
1268 __u32 net = LNET_NIDNET(ni->ni_nid);
1277 /* Convenience for LNDs that use the IP address of a local interface as
1278 * the local address part of their NID */
1280 if (ni->ni_interfaces[0] != NULL) {
1282 CLASSERT (LNET_MAX_INTERFACES > 1);
1284 if (ni->ni_interfaces[1] != NULL) {
1285 CERROR("Net %s doesn't support multiple interfaces\n",
1286 libcfs_net2str(net));
1290 rc = libcfs_ipif_query(ni->ni_interfaces[0],
1291 &up, &ip, &netmask);
1293 CERROR("Net %s can't query interface %s: %d\n",
1294 libcfs_net2str(net), ni->ni_interfaces[0], rc);
1299 CERROR("Net %s can't use interface %s: it's down\n",
1300 libcfs_net2str(net), ni->ni_interfaces[0]);
1304 ni->ni_nid = LNET_MKNID(net, ip);
1308 n = libcfs_ipif_enumerate(&names);
1310 CERROR("Net %s can't enumerate interfaces: %d\n",
1311 libcfs_net2str(net), n);
1315 for (i = 0; i < n; i++) {
1316 if (!strcmp(names[i], "lo")) /* skip the loopback IF */
1319 rc = libcfs_ipif_query(names[i], &up, &ip, &netmask);
1322 CWARN("Net %s can't query interface %s: %d\n",
1323 libcfs_net2str(net), names[i], rc);
1328 CWARN("Net %s ignoring interface %s (down)\n",
1329 libcfs_net2str(net), names[i]);
1333 libcfs_ipif_free_enumeration(names, n);
1334 ni->ni_nid = LNET_MKNID(net, ip);
1338 CERROR("Net %s can't find any interfaces\n", libcfs_net2str(net));
1339 libcfs_ipif_free_enumeration(names, n);
1342 EXPORT_SYMBOL(lnet_set_ip_niaddr);