blob: 9e2183ff847e66a405997e7fa8b7d1b69e7f672d [file] [log] [blame]
Peng Taod7e09d02013-05-02 16:46:55 +08001/*
2 * GPL HEADER START
3 *
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
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.
9 *
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).
15 *
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
Oleg Drokin6a5b99a2016-06-14 23:33:40 -040018 * http://www.gnu.org/licenses/gpl-2.0.html
Peng Taod7e09d02013-05-02 16:46:55 +080019 *
Peng Taod7e09d02013-05-02 16:46:55 +080020 * GPL HEADER END
21 */
22/*
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
25 *
Andreas Dilger1dc563a2015-11-08 18:09:37 -050026 * Copyright (c) 2012, 2015, Intel Corporation.
Peng Taod7e09d02013-05-02 16:46:55 +080027 */
28/*
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
31 */
32
33#define DEBUG_SUBSYSTEM S_LNET
Sebastien Buisson4c6722f2016-09-18 16:38:59 -040034#include <linux/nsproxy.h>
35#include <net/net_namespace.h>
Greg Kroah-Hartman9fdaf8c2014-07-11 20:51:16 -070036#include "../../include/linux/lnet/lib-lnet.h"
Peng Taod7e09d02013-05-02 16:46:55 +080037
James Simmonsddbc66a2016-02-12 12:05:59 -050038struct lnet_text_buf { /* tmp struct for parsing routes */
Mike Shuey7e7ab092015-05-19 10:14:32 -040039 struct list_head ltb_list; /* stash on lists */
40 int ltb_size; /* allocated size */
41 char ltb_text[0]; /* text buffer */
Rafaƫl Bocquete416a892015-04-02 17:12:34 +020042};
Peng Taod7e09d02013-05-02 16:46:55 +080043
Rashika Kheriaad0a75b2013-10-26 00:24:03 +053044static int lnet_tbnob; /* track text buf allocation */
James Simmons51078e22016-02-12 12:06:04 -050045#define LNET_MAX_TEXTBUF_NOB (64 << 10) /* bound allocation */
46#define LNET_SINGLE_TEXTBUF_NOB (4 << 10)
Peng Taod7e09d02013-05-02 16:46:55 +080047
Benedict Boerger714340d2014-08-08 18:26:22 +020048static void
Peng Taod7e09d02013-05-02 16:46:55 +080049lnet_syntax(char *name, char *str, int offset, int width)
50{
51 static char dots[LNET_SINGLE_TEXTBUF_NOB];
52 static char dashes[LNET_SINGLE_TEXTBUF_NOB];
53
54 memset(dots, '.', sizeof(dots));
James Simmons51078e22016-02-12 12:06:04 -050055 dots[sizeof(dots) - 1] = 0;
Peng Taod7e09d02013-05-02 16:46:55 +080056 memset(dashes, '-', sizeof(dashes));
James Simmons51078e22016-02-12 12:06:04 -050057 dashes[sizeof(dashes) - 1] = 0;
Peng Taod7e09d02013-05-02 16:46:55 +080058
59 LCONSOLE_ERROR_MSG(0x10f, "Error parsing '%s=\"%s\"'\n", name, str);
60 LCONSOLE_ERROR_MSG(0x110, "here...........%.*s..%.*s|%.*s|\n",
61 (int)strlen(name), dots, offset, dots,
62 (width < 1) ? 0 : width - 1, dashes);
63}
64
Benedict Boerger714340d2014-08-08 18:26:22 +020065static int
Rashika Kheria24edbe42013-10-26 00:21:24 +053066lnet_issep(char c)
Peng Taod7e09d02013-05-02 16:46:55 +080067{
68 switch (c) {
69 case '\n':
70 case '\r':
71 case ';':
72 return 1;
73 default:
74 return 0;
75 }
76}
77
Amir Shehata6c9e5a52016-02-15 10:25:54 -050078int
Peng Taod7e09d02013-05-02 16:46:55 +080079lnet_net_unique(__u32 net, struct list_head *nilist)
80{
Mike Shuey7e7ab092015-05-19 10:14:32 -040081 struct list_head *tmp;
82 lnet_ni_t *ni;
Peng Taod7e09d02013-05-02 16:46:55 +080083
Rashika Kheria24edbe42013-10-26 00:21:24 +053084 list_for_each(tmp, nilist) {
Peng Taod7e09d02013-05-02 16:46:55 +080085 ni = list_entry(tmp, lnet_ni_t, ni_list);
86
87 if (LNET_NIDNET(ni->ni_nid) == net)
88 return 0;
89 }
90
91 return 1;
92}
93
94void
95lnet_ni_free(struct lnet_ni *ni)
96{
Amir Shehata21602c72016-02-15 10:25:52 -050097 int i;
98
James Simmons06ace262016-02-12 12:06:08 -050099 if (ni->ni_refs)
Peng Taod7e09d02013-05-02 16:46:55 +0800100 cfs_percpt_free(ni->ni_refs);
101
James Simmons06ace262016-02-12 12:06:08 -0500102 if (ni->ni_tx_queues)
Peng Taod7e09d02013-05-02 16:46:55 +0800103 cfs_percpt_free(ni->ni_tx_queues);
104
James Simmons06ace262016-02-12 12:06:08 -0500105 if (ni->ni_cpts)
Peng Taod7e09d02013-05-02 16:46:55 +0800106 cfs_expr_list_values_free(ni->ni_cpts, ni->ni_ncpts);
107
Amir Shehata243a9412016-05-06 21:30:24 -0400108 if (ni->ni_lnd_tunables)
109 LIBCFS_FREE(ni->ni_lnd_tunables, sizeof(*ni->ni_lnd_tunables));
110
Amir Shehata21602c72016-02-15 10:25:52 -0500111 for (i = 0; i < LNET_MAX_INTERFACES && ni->ni_interfaces[i]; i++) {
112 LIBCFS_FREE(ni->ni_interfaces[i],
113 strlen(ni->ni_interfaces[i]) + 1);
114 }
Sebastien Buisson4c6722f2016-09-18 16:38:59 -0400115
116 /* release reference to net namespace */
117 if (ni->ni_net_ns)
118 put_net(ni->ni_net_ns);
119
Peng Taod7e09d02013-05-02 16:46:55 +0800120 LIBCFS_FREE(ni, sizeof(*ni));
121}
122
Amir Shehata9c26b892016-02-22 17:29:08 -0500123lnet_ni_t *
Peng Taod7e09d02013-05-02 16:46:55 +0800124lnet_ni_alloc(__u32 net, struct cfs_expr_list *el, struct list_head *nilist)
125{
Mike Shuey7e7ab092015-05-19 10:14:32 -0400126 struct lnet_tx_queue *tq;
127 struct lnet_ni *ni;
128 int rc;
129 int i;
Peng Taod7e09d02013-05-02 16:46:55 +0800130
131 if (!lnet_net_unique(net, nilist)) {
132 LCONSOLE_ERROR_MSG(0x111, "Duplicate network specified: %s\n",
133 libcfs_net2str(net));
134 return NULL;
135 }
136
137 LIBCFS_ALLOC(ni, sizeof(*ni));
James Simmons06ace262016-02-12 12:06:08 -0500138 if (!ni) {
Peng Taod7e09d02013-05-02 16:46:55 +0800139 CERROR("Out of memory creating network %s\n",
140 libcfs_net2str(net));
141 return NULL;
142 }
143
144 spin_lock_init(&ni->ni_lock);
145 INIT_LIST_HEAD(&ni->ni_cptlist);
146 ni->ni_refs = cfs_percpt_alloc(lnet_cpt_table(),
147 sizeof(*ni->ni_refs[0]));
James Simmons06ace262016-02-12 12:06:08 -0500148 if (!ni->ni_refs)
Peng Taod7e09d02013-05-02 16:46:55 +0800149 goto failed;
150
151 ni->ni_tx_queues = cfs_percpt_alloc(lnet_cpt_table(),
152 sizeof(*ni->ni_tx_queues[0]));
James Simmons06ace262016-02-12 12:06:08 -0500153 if (!ni->ni_tx_queues)
Peng Taod7e09d02013-05-02 16:46:55 +0800154 goto failed;
155
156 cfs_percpt_for_each(tq, i, ni->ni_tx_queues)
157 INIT_LIST_HEAD(&tq->tq_delayed);
158
James Simmons06ace262016-02-12 12:06:08 -0500159 if (!el) {
Peng Taod7e09d02013-05-02 16:46:55 +0800160 ni->ni_cpts = NULL;
161 ni->ni_ncpts = LNET_CPT_NUMBER;
162 } else {
163 rc = cfs_expr_list_values(el, LNET_CPT_NUMBER, &ni->ni_cpts);
164 if (rc <= 0) {
165 CERROR("Failed to set CPTs for NI %s: %d\n",
166 libcfs_net2str(net), rc);
167 goto failed;
168 }
169
170 LASSERT(rc <= LNET_CPT_NUMBER);
171 if (rc == LNET_CPT_NUMBER) {
172 LIBCFS_FREE(ni->ni_cpts, rc * sizeof(ni->ni_cpts[0]));
173 ni->ni_cpts = NULL;
174 }
175
176 ni->ni_ncpts = rc;
177 }
178
179 /* LND will fill in the address part of the NID */
180 ni->ni_nid = LNET_MKNID(net, 0);
Sebastien Buisson4c6722f2016-09-18 16:38:59 -0400181
182 /* Store net namespace in which current ni is being created */
183 if (current->nsproxy->net_ns)
184 ni->ni_net_ns = get_net(current->nsproxy->net_ns);
185 else
186 ni->ni_net_ns = NULL;
187
Arnd Bergmannec0067d2015-09-27 16:45:22 -0400188 ni->ni_last_alive = ktime_get_real_seconds();
Peng Taod7e09d02013-05-02 16:46:55 +0800189 list_add_tail(&ni->ni_list, nilist);
190 return ni;
191 failed:
192 lnet_ni_free(ni);
193 return NULL;
194}
195
196int
197lnet_parse_networks(struct list_head *nilist, char *networks)
198{
199 struct cfs_expr_list *el = NULL;
Amir Shehata8766cd122016-02-22 17:29:04 -0500200 int tokensize;
Mike Shuey7e7ab092015-05-19 10:14:32 -0400201 char *tokens;
202 char *str;
203 char *tmp;
204 struct lnet_ni *ni;
205 __u32 net;
206 int nnets = 0;
Amir Shehata9c26b892016-02-22 17:29:08 -0500207 struct list_head *temp_node;
Peng Taod7e09d02013-05-02 16:46:55 +0800208
Amir Shehata8766cd122016-02-22 17:29:04 -0500209 if (!networks) {
210 CERROR("networks string is undefined\n");
211 return -EINVAL;
212 }
213
Peng Taod7e09d02013-05-02 16:46:55 +0800214 if (strlen(networks) > LNET_SINGLE_TEXTBUF_NOB) {
215 /* _WAY_ conservative */
Rashika Kheriae46b3a02013-10-26 00:22:54 +0530216 LCONSOLE_ERROR_MSG(0x112,
217 "Can't parse networks: string too long\n");
Peng Taod7e09d02013-05-02 16:46:55 +0800218 return -EINVAL;
219 }
220
Amir Shehata8766cd122016-02-22 17:29:04 -0500221 tokensize = strlen(networks) + 1;
222
Peng Taod7e09d02013-05-02 16:46:55 +0800223 LIBCFS_ALLOC(tokens, tokensize);
James Simmons06ace262016-02-12 12:06:08 -0500224 if (!tokens) {
Peng Taod7e09d02013-05-02 16:46:55 +0800225 CERROR("Can't allocate net tokens\n");
226 return -ENOMEM;
227 }
228
Rashika Kheria24edbe42013-10-26 00:21:24 +0530229 memcpy(tokens, networks, tokensize);
James Simmonsd3d3d372016-02-12 12:06:05 -0500230 tmp = tokens;
231 str = tokens;
Peng Taod7e09d02013-05-02 16:46:55 +0800232
James Simmons5fd88332016-02-12 12:06:09 -0500233 while (str && *str) {
Mike Shuey7e7ab092015-05-19 10:14:32 -0400234 char *comma = strchr(str, ',');
235 char *bracket = strchr(str, '(');
236 char *square = strchr(str, '[');
237 char *iface;
238 int niface;
239 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800240
Sushuruth Sadagopan587cb022016-01-18 02:09:23 -0500241 /*
242 * NB we don't check interface conflicts here; it's the LNDs
243 * responsibility (if it cares at all)
244 */
James Simmons06ace262016-02-12 12:06:08 -0500245 if (square && (!comma || square < comma)) {
Sushuruth Sadagopan587cb022016-01-18 02:09:23 -0500246 /*
247 * i.e: o2ib0(ib0)[1,2], number between square
248 * brackets are CPTs this NI needs to be bond
249 */
James Simmons06ace262016-02-12 12:06:08 -0500250 if (bracket && bracket > square) {
Peng Taod7e09d02013-05-02 16:46:55 +0800251 tmp = square;
252 goto failed_syntax;
253 }
254
255 tmp = strchr(square, ']');
James Simmons06ace262016-02-12 12:06:08 -0500256 if (!tmp) {
Peng Taod7e09d02013-05-02 16:46:55 +0800257 tmp = square;
258 goto failed_syntax;
259 }
260
261 rc = cfs_expr_list_parse(square, tmp - square + 1,
262 0, LNET_CPT_NUMBER - 1, &el);
James Simmons5fd88332016-02-12 12:06:09 -0500263 if (rc) {
Peng Taod7e09d02013-05-02 16:46:55 +0800264 tmp = square;
265 goto failed_syntax;
266 }
267
268 while (square <= tmp)
269 *square++ = ' ';
270 }
271
James Simmons06ace262016-02-12 12:06:08 -0500272 if (!bracket || (comma && comma < bracket)) {
Peng Taod7e09d02013-05-02 16:46:55 +0800273 /* no interface list specified */
274
James Simmons06ace262016-02-12 12:06:08 -0500275 if (comma)
Peng Taod7e09d02013-05-02 16:46:55 +0800276 *comma++ = 0;
277 net = libcfs_str2net(cfs_trimwhite(str));
278
279 if (net == LNET_NIDNET(LNET_NID_ANY)) {
Rashika Kheriae46b3a02013-10-26 00:22:54 +0530280 LCONSOLE_ERROR_MSG(0x113,
281 "Unrecognised network type\n");
Peng Taod7e09d02013-05-02 16:46:55 +0800282 tmp = str;
283 goto failed_syntax;
284 }
285
286 if (LNET_NETTYP(net) != LOLND && /* LO is implicit */
James Simmons06ace262016-02-12 12:06:08 -0500287 !lnet_ni_alloc(net, el, nilist))
Peng Taod7e09d02013-05-02 16:46:55 +0800288 goto failed;
289
James Simmons06ace262016-02-12 12:06:08 -0500290 if (el) {
Peng Taod7e09d02013-05-02 16:46:55 +0800291 cfs_expr_list_free(el);
292 el = NULL;
293 }
294
295 str = comma;
296 continue;
297 }
298
299 *bracket = 0;
300 net = libcfs_str2net(cfs_trimwhite(str));
301 if (net == LNET_NIDNET(LNET_NID_ANY)) {
302 tmp = str;
303 goto failed_syntax;
304 }
305
Peng Taod7e09d02013-05-02 16:46:55 +0800306 ni = lnet_ni_alloc(net, el, nilist);
James Simmons06ace262016-02-12 12:06:08 -0500307 if (!ni)
Peng Taod7e09d02013-05-02 16:46:55 +0800308 goto failed;
309
James Simmons06ace262016-02-12 12:06:08 -0500310 if (el) {
Peng Taod7e09d02013-05-02 16:46:55 +0800311 cfs_expr_list_free(el);
312 el = NULL;
313 }
314
315 niface = 0;
316 iface = bracket + 1;
317
318 bracket = strchr(iface, ')');
James Simmons06ace262016-02-12 12:06:08 -0500319 if (!bracket) {
Peng Taod7e09d02013-05-02 16:46:55 +0800320 tmp = iface;
321 goto failed_syntax;
322 }
323
324 *bracket = 0;
325 do {
326 comma = strchr(iface, ',');
James Simmons06ace262016-02-12 12:06:08 -0500327 if (comma)
Peng Taod7e09d02013-05-02 16:46:55 +0800328 *comma++ = 0;
329
330 iface = cfs_trimwhite(iface);
James Simmons5fd88332016-02-12 12:06:09 -0500331 if (!*iface) {
Peng Taod7e09d02013-05-02 16:46:55 +0800332 tmp = iface;
333 goto failed_syntax;
334 }
335
336 if (niface == LNET_MAX_INTERFACES) {
Rashika Kheriae46b3a02013-10-26 00:22:54 +0530337 LCONSOLE_ERROR_MSG(0x115,
338 "Too many interfaces for net %s\n",
Peng Taod7e09d02013-05-02 16:46:55 +0800339 libcfs_net2str(net));
340 goto failed;
341 }
342
Amir Shehata21602c72016-02-15 10:25:52 -0500343 /*
344 * Allocate a separate piece of memory and copy
345 * into it the string, so we don't have
346 * a depencency on the tokens string. This way we
347 * can free the tokens at the end of the function.
348 * The newly allocated ni_interfaces[] can be
349 * freed when freeing the NI
350 */
351 LIBCFS_ALLOC(ni->ni_interfaces[niface],
352 strlen(iface) + 1);
353 if (!ni->ni_interfaces[niface]) {
354 CERROR("Can't allocate net interface name\n");
355 goto failed;
356 }
357 strncpy(ni->ni_interfaces[niface], iface,
358 strlen(iface));
359 niface++;
Peng Taod7e09d02013-05-02 16:46:55 +0800360 iface = comma;
James Simmons06ace262016-02-12 12:06:08 -0500361 } while (iface);
Peng Taod7e09d02013-05-02 16:46:55 +0800362
363 str = bracket + 1;
364 comma = strchr(bracket + 1, ',');
James Simmons06ace262016-02-12 12:06:08 -0500365 if (comma) {
Peng Taod7e09d02013-05-02 16:46:55 +0800366 *comma = 0;
367 str = cfs_trimwhite(str);
James Simmons5fd88332016-02-12 12:06:09 -0500368 if (*str) {
Peng Taod7e09d02013-05-02 16:46:55 +0800369 tmp = str;
370 goto failed_syntax;
371 }
372 str = comma + 1;
373 continue;
374 }
375
376 str = cfs_trimwhite(str);
James Simmons5fd88332016-02-12 12:06:09 -0500377 if (*str) {
Peng Taod7e09d02013-05-02 16:46:55 +0800378 tmp = str;
379 goto failed_syntax;
380 }
381 }
382
Amir Shehata9c26b892016-02-22 17:29:08 -0500383 list_for_each(temp_node, nilist)
384 nnets++;
Amir Shehata21602c72016-02-15 10:25:52 -0500385
386 LIBCFS_FREE(tokens, tokensize);
Amir Shehata9c26b892016-02-22 17:29:08 -0500387 return nnets;
Peng Taod7e09d02013-05-02 16:46:55 +0800388
389 failed_syntax:
390 lnet_syntax("networks", networks, (int)(tmp - tokens), strlen(tmp));
391 failed:
392 while (!list_empty(nilist)) {
393 ni = list_entry(nilist->next, lnet_ni_t, ni_list);
394
395 list_del(&ni->ni_list);
396 lnet_ni_free(ni);
397 }
398
James Simmons06ace262016-02-12 12:06:08 -0500399 if (el)
Peng Taod7e09d02013-05-02 16:46:55 +0800400 cfs_expr_list_free(el);
401
402 LIBCFS_FREE(tokens, tokensize);
Peng Taod7e09d02013-05-02 16:46:55 +0800403
404 return -EINVAL;
405}
406
James Simmonsddbc66a2016-02-12 12:05:59 -0500407static struct lnet_text_buf *
Rashika Kheria24edbe42013-10-26 00:21:24 +0530408lnet_new_text_buf(int str_len)
Peng Taod7e09d02013-05-02 16:46:55 +0800409{
James Simmonsddbc66a2016-02-12 12:05:59 -0500410 struct lnet_text_buf *ltb;
Mike Shuey7e7ab092015-05-19 10:14:32 -0400411 int nob;
Peng Taod7e09d02013-05-02 16:46:55 +0800412
413 /* NB allocate space for the terminating 0 */
James Simmonsddbc66a2016-02-12 12:05:59 -0500414 nob = offsetof(struct lnet_text_buf, ltb_text[str_len + 1]);
Peng Taod7e09d02013-05-02 16:46:55 +0800415 if (nob > LNET_SINGLE_TEXTBUF_NOB) {
416 /* _way_ conservative for "route net gateway..." */
417 CERROR("text buffer too big\n");
418 return NULL;
419 }
420
421 if (lnet_tbnob + nob > LNET_MAX_TEXTBUF_NOB) {
422 CERROR("Too many text buffers\n");
423 return NULL;
424 }
425
426 LIBCFS_ALLOC(ltb, nob);
James Simmons06ace262016-02-12 12:06:08 -0500427 if (!ltb)
Peng Taod7e09d02013-05-02 16:46:55 +0800428 return NULL;
429
430 ltb->ltb_size = nob;
431 ltb->ltb_text[0] = 0;
432 lnet_tbnob += nob;
433 return ltb;
434}
435
Benedict Boerger714340d2014-08-08 18:26:22 +0200436static void
James Simmonsddbc66a2016-02-12 12:05:59 -0500437lnet_free_text_buf(struct lnet_text_buf *ltb)
Peng Taod7e09d02013-05-02 16:46:55 +0800438{
439 lnet_tbnob -= ltb->ltb_size;
440 LIBCFS_FREE(ltb, ltb->ltb_size);
441}
442
Benedict Boerger714340d2014-08-08 18:26:22 +0200443static void
Peng Taod7e09d02013-05-02 16:46:55 +0800444lnet_free_text_bufs(struct list_head *tbs)
445{
James Simmonsddbc66a2016-02-12 12:05:59 -0500446 struct lnet_text_buf *ltb;
Peng Taod7e09d02013-05-02 16:46:55 +0800447
448 while (!list_empty(tbs)) {
James Simmonsddbc66a2016-02-12 12:05:59 -0500449 ltb = list_entry(tbs->next, struct lnet_text_buf, ltb_list);
Peng Taod7e09d02013-05-02 16:46:55 +0800450
451 list_del(&ltb->ltb_list);
452 lnet_free_text_buf(ltb);
453 }
454}
455
Benedict Boerger714340d2014-08-08 18:26:22 +0200456static int
Rashika Kheria24edbe42013-10-26 00:21:24 +0530457lnet_str2tbs_sep(struct list_head *tbs, char *str)
Peng Taod7e09d02013-05-02 16:46:55 +0800458{
Mike Shuey7e7ab092015-05-19 10:14:32 -0400459 struct list_head pending;
460 char *sep;
461 int nob;
462 int i;
James Simmonsddbc66a2016-02-12 12:05:59 -0500463 struct lnet_text_buf *ltb;
Peng Taod7e09d02013-05-02 16:46:55 +0800464
465 INIT_LIST_HEAD(&pending);
466
467 /* Split 'str' into separate commands */
468 for (;;) {
469 /* skip leading whitespace */
Peng Taoe525a682014-03-05 21:27:18 +0800470 while (isspace(*str))
Peng Taod7e09d02013-05-02 16:46:55 +0800471 str++;
472
473 /* scan for separator or comment */
James Simmons5fd88332016-02-12 12:06:09 -0500474 for (sep = str; *sep; sep++)
Peng Taod7e09d02013-05-02 16:46:55 +0800475 if (lnet_issep(*sep) || *sep == '#')
476 break;
477
478 nob = (int)(sep - str);
479 if (nob > 0) {
480 ltb = lnet_new_text_buf(nob);
James Simmons06ace262016-02-12 12:06:08 -0500481 if (!ltb) {
Peng Taod7e09d02013-05-02 16:46:55 +0800482 lnet_free_text_bufs(&pending);
James Simmons58cb2ad2016-03-02 17:01:47 -0500483 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +0800484 }
485
486 for (i = 0; i < nob; i++)
Peng Taoe525a682014-03-05 21:27:18 +0800487 if (isspace(str[i]))
Peng Taod7e09d02013-05-02 16:46:55 +0800488 ltb->ltb_text[i] = ' ';
489 else
490 ltb->ltb_text[i] = str[i];
491
492 ltb->ltb_text[nob] = 0;
493
494 list_add_tail(&ltb->ltb_list, &pending);
495 }
496
497 if (*sep == '#') {
498 /* scan for separator */
499 do {
500 sep++;
James Simmons5fd88332016-02-12 12:06:09 -0500501 } while (*sep && !lnet_issep(*sep));
Peng Taod7e09d02013-05-02 16:46:55 +0800502 }
503
James Simmons5fd88332016-02-12 12:06:09 -0500504 if (!*sep)
Peng Taod7e09d02013-05-02 16:46:55 +0800505 break;
506
507 str = sep + 1;
508 }
509
510 list_splice(&pending, tbs->prev);
511 return 0;
512}
513
Benedict Boerger714340d2014-08-08 18:26:22 +0200514static int
Rashika Kheria24edbe42013-10-26 00:21:24 +0530515lnet_expand1tb(struct list_head *list,
Peng Taod7e09d02013-05-02 16:46:55 +0800516 char *str, char *sep1, char *sep2,
517 char *item, int itemlen)
518{
Mike Shuey7e7ab092015-05-19 10:14:32 -0400519 int len1 = (int)(sep1 - str);
520 int len2 = strlen(sep2 + 1);
James Simmonsddbc66a2016-02-12 12:05:59 -0500521 struct lnet_text_buf *ltb;
Peng Taod7e09d02013-05-02 16:46:55 +0800522
Rashika Kheria24edbe42013-10-26 00:21:24 +0530523 LASSERT(*sep1 == '[');
524 LASSERT(*sep2 == ']');
Peng Taod7e09d02013-05-02 16:46:55 +0800525
526 ltb = lnet_new_text_buf(len1 + itemlen + len2);
James Simmons06ace262016-02-12 12:06:08 -0500527 if (!ltb)
Peng Taod7e09d02013-05-02 16:46:55 +0800528 return -ENOMEM;
529
530 memcpy(ltb->ltb_text, str, len1);
531 memcpy(&ltb->ltb_text[len1], item, itemlen);
James Simmons51078e22016-02-12 12:06:04 -0500532 memcpy(&ltb->ltb_text[len1 + itemlen], sep2 + 1, len2);
Peng Taod7e09d02013-05-02 16:46:55 +0800533 ltb->ltb_text[len1 + itemlen + len2] = 0;
534
535 list_add_tail(&ltb->ltb_list, list);
536 return 0;
537}
538
Benedict Boerger714340d2014-08-08 18:26:22 +0200539static int
Rashika Kheria24edbe42013-10-26 00:21:24 +0530540lnet_str2tbs_expand(struct list_head *tbs, char *str)
Peng Taod7e09d02013-05-02 16:46:55 +0800541{
Mike Shuey7e7ab092015-05-19 10:14:32 -0400542 char num[16];
543 struct list_head pending;
544 char *sep;
545 char *sep2;
546 char *parsed;
547 char *enditem;
548 int lo;
549 int hi;
550 int stride;
551 int i;
552 int nob;
553 int scanned;
Peng Taod7e09d02013-05-02 16:46:55 +0800554
555 INIT_LIST_HEAD(&pending);
556
557 sep = strchr(str, '[');
James Simmons06ace262016-02-12 12:06:08 -0500558 if (!sep) /* nothing to expand */
Peng Taod7e09d02013-05-02 16:46:55 +0800559 return 0;
560
561 sep2 = strchr(sep, ']');
James Simmons06ace262016-02-12 12:06:08 -0500562 if (!sep2)
Peng Taod7e09d02013-05-02 16:46:55 +0800563 goto failed;
564
565 for (parsed = sep; parsed < sep2; parsed = enditem) {
Peng Taod7e09d02013-05-02 16:46:55 +0800566 enditem = ++parsed;
567 while (enditem < sep2 && *enditem != ',')
568 enditem++;
569
570 if (enditem == parsed) /* no empty items */
571 goto failed;
572
Rafaƫl Bocquet3a338e22015-04-02 17:12:33 +0200573 if (sscanf(parsed, "%d-%d/%d%n", &lo, &hi,
574 &stride, &scanned) < 3) {
Peng Taod7e09d02013-05-02 16:46:55 +0800575 if (sscanf(parsed, "%d-%d%n", &lo, &hi, &scanned) < 2) {
Peng Taod7e09d02013-05-02 16:46:55 +0800576 /* simple string enumeration */
James Simmonsc314c312016-02-12 12:06:01 -0500577 if (lnet_expand1tb(&pending, str, sep, sep2,
578 parsed,
James Simmons5fd88332016-02-12 12:06:09 -0500579 (int)(enditem - parsed))) {
Peng Taod7e09d02013-05-02 16:46:55 +0800580 goto failed;
Rafaƫl Bocquet3a338e22015-04-02 17:12:33 +0200581 }
Peng Taod7e09d02013-05-02 16:46:55 +0800582 continue;
583 }
584
585 stride = 1;
586 }
587
588 /* range expansion */
589
590 if (enditem != parsed + scanned) /* no trailing junk */
591 goto failed;
592
593 if (hi < 0 || lo < 0 || stride < 0 || hi < lo ||
James Simmons5fd88332016-02-12 12:06:09 -0500594 (hi - lo) % stride)
Peng Taod7e09d02013-05-02 16:46:55 +0800595 goto failed;
596
597 for (i = lo; i <= hi; i += stride) {
Peng Taod7e09d02013-05-02 16:46:55 +0800598 snprintf(num, sizeof(num), "%d", i);
599 nob = strlen(num);
600 if (nob + 1 == sizeof(num))
601 goto failed;
602
603 if (lnet_expand1tb(&pending, str, sep, sep2,
James Simmons5fd88332016-02-12 12:06:09 -0500604 num, nob))
Peng Taod7e09d02013-05-02 16:46:55 +0800605 goto failed;
606 }
607 }
608
609 list_splice(&pending, tbs->prev);
610 return 1;
611
612 failed:
613 lnet_free_text_bufs(&pending);
James Simmons58cb2ad2016-03-02 17:01:47 -0500614 return -EINVAL;
Peng Taod7e09d02013-05-02 16:46:55 +0800615}
616
Benedict Boerger714340d2014-08-08 18:26:22 +0200617static int
Rashika Kheria24edbe42013-10-26 00:21:24 +0530618lnet_parse_hops(char *str, unsigned int *hops)
Peng Taod7e09d02013-05-02 16:46:55 +0800619{
Mike Shuey7e7ab092015-05-19 10:14:32 -0400620 int len = strlen(str);
621 int nob = len;
Peng Taod7e09d02013-05-02 16:46:55 +0800622
623 return (sscanf(str, "%u%n", hops, &nob) >= 1 &&
624 nob == len &&
625 *hops > 0 && *hops < 256);
626}
627
Doug Ouchareke75fb872013-12-09 22:56:54 +0800628#define LNET_PRIORITY_SEPARATOR (':')
629
Benedict Boerger714340d2014-08-08 18:26:22 +0200630static int
Doug Ouchareke75fb872013-12-09 22:56:54 +0800631lnet_parse_priority(char *str, unsigned int *priority, char **token)
632{
Mike Shuey7e7ab092015-05-19 10:14:32 -0400633 int nob;
Doug Ouchareke75fb872013-12-09 22:56:54 +0800634 char *sep;
Mike Shuey7e7ab092015-05-19 10:14:32 -0400635 int len;
Doug Ouchareke75fb872013-12-09 22:56:54 +0800636
637 sep = strchr(str, LNET_PRIORITY_SEPARATOR);
James Simmons06ace262016-02-12 12:06:08 -0500638 if (!sep) {
Doug Ouchareke75fb872013-12-09 22:56:54 +0800639 *priority = 0;
640 return 0;
641 }
642 len = strlen(sep + 1);
643
James Simmons51078e22016-02-12 12:06:04 -0500644 if ((sscanf((sep + 1), "%u%n", priority, &nob) < 1) || (len != nob)) {
Sushuruth Sadagopan587cb022016-01-18 02:09:23 -0500645 /*
646 * Update the caller's token pointer so it treats the found
647 * priority as the token to report in the error message.
648 */
Doug Ouchareke75fb872013-12-09 22:56:54 +0800649 *token += sep - str + 1;
James Simmons58cb2ad2016-03-02 17:01:47 -0500650 return -EINVAL;
Doug Ouchareke75fb872013-12-09 22:56:54 +0800651 }
652
653 CDEBUG(D_NET, "gateway %s, priority %d, nob %d\n", str, *priority, nob);
654
655 /*
656 * Change priority separator to \0 to be able to parse NID
657 */
658 *sep = '\0';
659 return 0;
660}
Peng Taod7e09d02013-05-02 16:46:55 +0800661
Benedict Boerger714340d2014-08-08 18:26:22 +0200662static int
Rashika Kheria24edbe42013-10-26 00:21:24 +0530663lnet_parse_route(char *str, int *im_a_router)
Peng Taod7e09d02013-05-02 16:46:55 +0800664{
665 /* static scratch buffer OK (single threaded) */
Mike Shuey7e7ab092015-05-19 10:14:32 -0400666 static char cmd[LNET_SINGLE_TEXTBUF_NOB];
Peng Taod7e09d02013-05-02 16:46:55 +0800667
Mike Shuey7e7ab092015-05-19 10:14:32 -0400668 struct list_head nets;
669 struct list_head gateways;
670 struct list_head *tmp1;
671 struct list_head *tmp2;
672 __u32 net;
673 lnet_nid_t nid;
James Simmonsddbc66a2016-02-12 12:05:59 -0500674 struct lnet_text_buf *ltb;
Mike Shuey7e7ab092015-05-19 10:14:32 -0400675 int rc;
676 char *sep;
677 char *token = str;
678 int ntokens = 0;
679 int myrc = -1;
Amir Shehatab9bbb612016-03-02 17:01:46 -0500680 __u32 hops;
Mike Shuey7e7ab092015-05-19 10:14:32 -0400681 int got_hops = 0;
682 unsigned int priority = 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800683
684 INIT_LIST_HEAD(&gateways);
685 INIT_LIST_HEAD(&nets);
686
687 /* save a copy of the string for error messages */
Dmitry Eremin9563fe82015-11-04 13:40:00 -0500688 strncpy(cmd, str, sizeof(cmd));
689 cmd[sizeof(cmd) - 1] = '\0';
Peng Taod7e09d02013-05-02 16:46:55 +0800690
691 sep = str;
692 for (;;) {
693 /* scan for token start */
Peng Taoe525a682014-03-05 21:27:18 +0800694 while (isspace(*sep))
Peng Taod7e09d02013-05-02 16:46:55 +0800695 sep++;
James Simmons5fd88332016-02-12 12:06:09 -0500696 if (!*sep) {
Peng Taod7e09d02013-05-02 16:46:55 +0800697 if (ntokens < (got_hops ? 3 : 2))
698 goto token_error;
699 break;
700 }
701
702 ntokens++;
703 token = sep++;
704
705 /* scan for token end */
James Simmons5fd88332016-02-12 12:06:09 -0500706 while (*sep && !isspace(*sep))
Peng Taod7e09d02013-05-02 16:46:55 +0800707 sep++;
James Simmons5fd88332016-02-12 12:06:09 -0500708 if (*sep)
Peng Taod7e09d02013-05-02 16:46:55 +0800709 *sep++ = 0;
710
711 if (ntokens == 1) {
712 tmp2 = &nets; /* expanding nets */
713 } else if (ntokens == 2 &&
714 lnet_parse_hops(token, &hops)) {
715 got_hops = 1; /* got a hop count */
716 continue;
717 } else {
718 tmp2 = &gateways; /* expanding gateways */
719 }
720
721 ltb = lnet_new_text_buf(strlen(token));
James Simmons06ace262016-02-12 12:06:08 -0500722 if (!ltb)
Peng Taod7e09d02013-05-02 16:46:55 +0800723 goto out;
724
725 strcpy(ltb->ltb_text, token);
726 tmp1 = &ltb->ltb_list;
727 list_add_tail(tmp1, tmp2);
728
729 while (tmp1 != tmp2) {
James Simmonsddbc66a2016-02-12 12:05:59 -0500730 ltb = list_entry(tmp1, struct lnet_text_buf, ltb_list);
Peng Taod7e09d02013-05-02 16:46:55 +0800731
732 rc = lnet_str2tbs_expand(tmp1->next, ltb->ltb_text);
733 if (rc < 0)
734 goto token_error;
735
736 tmp1 = tmp1->next;
737
738 if (rc > 0) { /* expanded! */
739 list_del(&ltb->ltb_list);
740 lnet_free_text_buf(ltb);
741 continue;
742 }
743
744 if (ntokens == 1) {
745 net = libcfs_str2net(ltb->ltb_text);
746 if (net == LNET_NIDNET(LNET_NID_ANY) ||
747 LNET_NETTYP(net) == LOLND)
748 goto token_error;
749 } else {
Doug Ouchareke75fb872013-12-09 22:56:54 +0800750 rc = lnet_parse_priority(ltb->ltb_text,
751 &priority, &token);
752 if (rc < 0)
753 goto token_error;
754
Peng Taod7e09d02013-05-02 16:46:55 +0800755 nid = libcfs_str2nid(ltb->ltb_text);
756 if (nid == LNET_NID_ANY ||
757 LNET_NETTYP(LNET_NIDNET(nid)) == LOLND)
758 goto token_error;
759 }
760 }
761 }
762
Amir Shehatab9bbb612016-03-02 17:01:46 -0500763 /**
764 * if there are no hops set then we want to flag this value as
765 * unset since hops is an optional parameter
766 */
Peng Taod7e09d02013-05-02 16:46:55 +0800767 if (!got_hops)
Amir Shehatab9bbb612016-03-02 17:01:46 -0500768 hops = LNET_UNDEFINED_HOPS;
Peng Taod7e09d02013-05-02 16:46:55 +0800769
Rashika Kheria24edbe42013-10-26 00:21:24 +0530770 LASSERT(!list_empty(&nets));
771 LASSERT(!list_empty(&gateways));
Peng Taod7e09d02013-05-02 16:46:55 +0800772
Rashika Kheria24edbe42013-10-26 00:21:24 +0530773 list_for_each(tmp1, &nets) {
James Simmonsddbc66a2016-02-12 12:05:59 -0500774 ltb = list_entry(tmp1, struct lnet_text_buf, ltb_list);
Peng Taod7e09d02013-05-02 16:46:55 +0800775 net = libcfs_str2net(ltb->ltb_text);
Rashika Kheria24edbe42013-10-26 00:21:24 +0530776 LASSERT(net != LNET_NIDNET(LNET_NID_ANY));
Peng Taod7e09d02013-05-02 16:46:55 +0800777
Rashika Kheria24edbe42013-10-26 00:21:24 +0530778 list_for_each(tmp2, &gateways) {
James Simmonsddbc66a2016-02-12 12:05:59 -0500779 ltb = list_entry(tmp2, struct lnet_text_buf, ltb_list);
Peng Taod7e09d02013-05-02 16:46:55 +0800780 nid = libcfs_str2nid(ltb->ltb_text);
Rashika Kheria24edbe42013-10-26 00:21:24 +0530781 LASSERT(nid != LNET_NID_ANY);
Peng Taod7e09d02013-05-02 16:46:55 +0800782
783 if (lnet_islocalnid(nid)) {
784 *im_a_router = 1;
785 continue;
786 }
787
Doug Ouchareke75fb872013-12-09 22:56:54 +0800788 rc = lnet_add_route(net, hops, nid, priority);
Amir Shehatabe8240a2016-02-22 17:29:09 -0500789 if (rc && rc != -EEXIST && rc != -EHOSTUNREACH) {
Rashika Kheriae46b3a02013-10-26 00:22:54 +0530790 CERROR("Can't create route to %s via %s\n",
Peng Taod7e09d02013-05-02 16:46:55 +0800791 libcfs_net2str(net),
792 libcfs_nid2str(nid));
793 goto out;
794 }
795 }
796 }
797
798 myrc = 0;
799 goto out;
800
801 token_error:
802 lnet_syntax("routes", cmd, (int)(token - str), strlen(token));
803 out:
804 lnet_free_text_bufs(&nets);
805 lnet_free_text_bufs(&gateways);
806 return myrc;
807}
808
Benedict Boerger714340d2014-08-08 18:26:22 +0200809static int
Peng Taod7e09d02013-05-02 16:46:55 +0800810lnet_parse_route_tbs(struct list_head *tbs, int *im_a_router)
811{
James Simmonsddbc66a2016-02-12 12:05:59 -0500812 struct lnet_text_buf *ltb;
Peng Taod7e09d02013-05-02 16:46:55 +0800813
814 while (!list_empty(tbs)) {
James Simmonsddbc66a2016-02-12 12:05:59 -0500815 ltb = list_entry(tbs->next, struct lnet_text_buf, ltb_list);
Peng Taod7e09d02013-05-02 16:46:55 +0800816
817 if (lnet_parse_route(ltb->ltb_text, im_a_router) < 0) {
818 lnet_free_text_bufs(tbs);
819 return -EINVAL;
820 }
821
822 list_del(&ltb->ltb_list);
823 lnet_free_text_buf(ltb);
824 }
825
826 return 0;
827}
828
829int
Rashika Kheria24edbe42013-10-26 00:21:24 +0530830lnet_parse_routes(char *routes, int *im_a_router)
Peng Taod7e09d02013-05-02 16:46:55 +0800831{
Mike Shuey7e7ab092015-05-19 10:14:32 -0400832 struct list_head tbs;
833 int rc = 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800834
835 *im_a_router = 0;
836
837 INIT_LIST_HEAD(&tbs);
838
839 if (lnet_str2tbs_sep(&tbs, routes) < 0) {
840 CERROR("Error parsing routes\n");
841 rc = -EINVAL;
842 } else {
843 rc = lnet_parse_route_tbs(&tbs, im_a_router);
844 }
845
James Simmons5fd88332016-02-12 12:06:09 -0500846 LASSERT(!lnet_tbnob);
Peng Taod7e09d02013-05-02 16:46:55 +0800847 return rc;
848}
849
Benedict Boerger714340d2014-08-08 18:26:22 +0200850static int
Peng Taod7e09d02013-05-02 16:46:55 +0800851lnet_match_network_token(char *token, int len, __u32 *ipaddrs, int nip)
852{
Rashika Kheria24edbe42013-10-26 00:21:24 +0530853 LIST_HEAD(list);
Mike Shuey7e7ab092015-05-19 10:14:32 -0400854 int rc;
855 int i;
Peng Taod7e09d02013-05-02 16:46:55 +0800856
857 rc = cfs_ip_addr_parse(token, len, &list);
James Simmons5fd88332016-02-12 12:06:09 -0500858 if (rc)
Peng Taod7e09d02013-05-02 16:46:55 +0800859 return rc;
860
861 for (rc = i = 0; !rc && i < nip; i++)
862 rc = cfs_ip_addr_match(ipaddrs[i], &list);
863
James Simmonsa620ec62015-10-21 21:52:42 -0400864 cfs_expr_list_free_list(&list);
Peng Taod7e09d02013-05-02 16:46:55 +0800865
866 return rc;
867}
868
Benedict Boerger714340d2014-08-08 18:26:22 +0200869static int
Peng Taod7e09d02013-05-02 16:46:55 +0800870lnet_match_network_tokens(char *net_entry, __u32 *ipaddrs, int nip)
871{
872 static char tokens[LNET_SINGLE_TEXTBUF_NOB];
873
Mike Shuey7e7ab092015-05-19 10:14:32 -0400874 int matched = 0;
875 int ntokens = 0;
876 int len;
Peng Taod7e09d02013-05-02 16:46:55 +0800877 char *net = NULL;
878 char *sep;
879 char *token;
Mike Shuey7e7ab092015-05-19 10:14:32 -0400880 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800881
Rashika Kheria24edbe42013-10-26 00:21:24 +0530882 LASSERT(strlen(net_entry) < sizeof(tokens));
Peng Taod7e09d02013-05-02 16:46:55 +0800883
884 /* work on a copy of the string */
885 strcpy(tokens, net_entry);
886 sep = tokens;
887 for (;;) {
888 /* scan for token start */
Peng Taoe525a682014-03-05 21:27:18 +0800889 while (isspace(*sep))
Peng Taod7e09d02013-05-02 16:46:55 +0800890 sep++;
James Simmons5fd88332016-02-12 12:06:09 -0500891 if (!*sep)
Peng Taod7e09d02013-05-02 16:46:55 +0800892 break;
893
894 token = sep++;
895
896 /* scan for token end */
James Simmons5fd88332016-02-12 12:06:09 -0500897 while (*sep && !isspace(*sep))
Peng Taod7e09d02013-05-02 16:46:55 +0800898 sep++;
James Simmons5fd88332016-02-12 12:06:09 -0500899 if (*sep)
Peng Taod7e09d02013-05-02 16:46:55 +0800900 *sep++ = 0;
901
James Simmons5fd88332016-02-12 12:06:09 -0500902 if (!ntokens++) {
Peng Taod7e09d02013-05-02 16:46:55 +0800903 net = token;
904 continue;
905 }
906
907 len = strlen(token);
908
909 rc = lnet_match_network_token(token, len, ipaddrs, nip);
910 if (rc < 0) {
911 lnet_syntax("ip2nets", net_entry,
912 (int)(token - tokens), len);
913 return rc;
914 }
915
James Simmons5fd88332016-02-12 12:06:09 -0500916 if (rc)
917 matched |= 1;
Peng Taod7e09d02013-05-02 16:46:55 +0800918 }
919
920 if (!matched)
921 return 0;
922
923 strcpy(net_entry, net); /* replace with matched net */
924 return 1;
925}
926
Benedict Boerger714340d2014-08-08 18:26:22 +0200927static __u32
Peng Taod7e09d02013-05-02 16:46:55 +0800928lnet_netspec2net(char *netspec)
929{
Mike Shuey7e7ab092015-05-19 10:14:32 -0400930 char *bracket = strchr(netspec, '(');
931 __u32 net;
Peng Taod7e09d02013-05-02 16:46:55 +0800932
James Simmons06ace262016-02-12 12:06:08 -0500933 if (bracket)
Peng Taod7e09d02013-05-02 16:46:55 +0800934 *bracket = 0;
935
936 net = libcfs_str2net(netspec);
937
James Simmons06ace262016-02-12 12:06:08 -0500938 if (bracket)
Peng Taod7e09d02013-05-02 16:46:55 +0800939 *bracket = '(';
940
941 return net;
942}
943
Benedict Boerger714340d2014-08-08 18:26:22 +0200944static int
Peng Taod7e09d02013-05-02 16:46:55 +0800945lnet_splitnets(char *source, struct list_head *nets)
946{
Mike Shuey7e7ab092015-05-19 10:14:32 -0400947 int offset = 0;
948 int offset2;
949 int len;
James Simmonsddbc66a2016-02-12 12:05:59 -0500950 struct lnet_text_buf *tb;
951 struct lnet_text_buf *tb2;
Mike Shuey7e7ab092015-05-19 10:14:32 -0400952 struct list_head *t;
953 char *sep;
954 char *bracket;
955 __u32 net;
Peng Taod7e09d02013-05-02 16:46:55 +0800956
Rashika Kheria24edbe42013-10-26 00:21:24 +0530957 LASSERT(!list_empty(nets));
958 LASSERT(nets->next == nets->prev); /* single entry */
Peng Taod7e09d02013-05-02 16:46:55 +0800959
James Simmonsddbc66a2016-02-12 12:05:59 -0500960 tb = list_entry(nets->next, struct lnet_text_buf, ltb_list);
Peng Taod7e09d02013-05-02 16:46:55 +0800961
962 for (;;) {
963 sep = strchr(tb->ltb_text, ',');
964 bracket = strchr(tb->ltb_text, '(');
965
James Simmons06ace262016-02-12 12:06:08 -0500966 if (sep && bracket && bracket < sep) {
Peng Taod7e09d02013-05-02 16:46:55 +0800967 /* netspec lists interfaces... */
968
969 offset2 = offset + (int)(bracket - tb->ltb_text);
970 len = strlen(bracket);
971
972 bracket = strchr(bracket + 1, ')');
973
James Simmons06ace262016-02-12 12:06:08 -0500974 if (!bracket ||
James Simmons5fd88332016-02-12 12:06:09 -0500975 !(bracket[1] == ',' || !bracket[1])) {
Peng Taod7e09d02013-05-02 16:46:55 +0800976 lnet_syntax("ip2nets", source, offset2, len);
977 return -EINVAL;
978 }
979
James Simmons5fd88332016-02-12 12:06:09 -0500980 sep = !bracket[1] ? NULL : bracket + 1;
Peng Taod7e09d02013-05-02 16:46:55 +0800981 }
982
James Simmons06ace262016-02-12 12:06:08 -0500983 if (sep)
Peng Taod7e09d02013-05-02 16:46:55 +0800984 *sep++ = 0;
985
986 net = lnet_netspec2net(tb->ltb_text);
987 if (net == LNET_NIDNET(LNET_NID_ANY)) {
988 lnet_syntax("ip2nets", source, offset,
989 strlen(tb->ltb_text));
990 return -EINVAL;
991 }
992
993 list_for_each(t, nets) {
James Simmonsddbc66a2016-02-12 12:05:59 -0500994 tb2 = list_entry(t, struct lnet_text_buf, ltb_list);
Peng Taod7e09d02013-05-02 16:46:55 +0800995
996 if (tb2 == tb)
997 continue;
998
999 if (net == lnet_netspec2net(tb2->ltb_text)) {
1000 /* duplicate network */
1001 lnet_syntax("ip2nets", source, offset,
1002 strlen(tb->ltb_text));
1003 return -EINVAL;
1004 }
1005 }
1006
James Simmons06ace262016-02-12 12:06:08 -05001007 if (!sep)
Peng Taod7e09d02013-05-02 16:46:55 +08001008 return 0;
1009
1010 offset += (int)(sep - tb->ltb_text);
Dmitry Eremin9563fe82015-11-04 13:40:00 -05001011 len = strlen(sep);
1012 tb2 = lnet_new_text_buf(len);
James Simmons06ace262016-02-12 12:06:08 -05001013 if (!tb2)
Peng Taod7e09d02013-05-02 16:46:55 +08001014 return -ENOMEM;
1015
Dmitry Eremin9563fe82015-11-04 13:40:00 -05001016 strncpy(tb2->ltb_text, sep, len);
1017 tb2->ltb_text[len] = '\0';
Peng Taod7e09d02013-05-02 16:46:55 +08001018 list_add_tail(&tb2->ltb_list, nets);
1019
1020 tb = tb2;
1021 }
1022}
1023
Benedict Boerger714340d2014-08-08 18:26:22 +02001024static int
Rashika Kheria24edbe42013-10-26 00:21:24 +05301025lnet_match_networks(char **networksp, char *ip2nets, __u32 *ipaddrs, int nip)
Peng Taod7e09d02013-05-02 16:46:55 +08001026{
Mike Shuey7e7ab092015-05-19 10:14:32 -04001027 static char networks[LNET_SINGLE_TEXTBUF_NOB];
1028 static char source[LNET_SINGLE_TEXTBUF_NOB];
Peng Taod7e09d02013-05-02 16:46:55 +08001029
Mike Shuey7e7ab092015-05-19 10:14:32 -04001030 struct list_head raw_entries;
1031 struct list_head matched_nets;
1032 struct list_head current_nets;
1033 struct list_head *t;
1034 struct list_head *t2;
James Simmonsddbc66a2016-02-12 12:05:59 -05001035 struct lnet_text_buf *tb;
Bhaktipriya Shridharcb734cf2016-03-12 01:33:03 +05301036 struct lnet_text_buf *temp;
James Simmonsddbc66a2016-02-12 12:05:59 -05001037 struct lnet_text_buf *tb2;
Mike Shuey7e7ab092015-05-19 10:14:32 -04001038 __u32 net1;
1039 __u32 net2;
1040 int len;
1041 int count;
1042 int dup;
1043 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001044
1045 INIT_LIST_HEAD(&raw_entries);
1046 if (lnet_str2tbs_sep(&raw_entries, ip2nets) < 0) {
1047 CERROR("Error parsing ip2nets\n");
James Simmons5fd88332016-02-12 12:06:09 -05001048 LASSERT(!lnet_tbnob);
Peng Taod7e09d02013-05-02 16:46:55 +08001049 return -EINVAL;
1050 }
1051
1052 INIT_LIST_HEAD(&matched_nets);
1053 INIT_LIST_HEAD(&current_nets);
1054 networks[0] = 0;
1055 count = 0;
1056 len = 0;
1057 rc = 0;
1058
Bhaktipriya Shridharcb734cf2016-03-12 01:33:03 +05301059 list_for_each_entry_safe(tb, temp, &raw_entries, ltb_list) {
Dmitry Eremin9563fe82015-11-04 13:40:00 -05001060 strncpy(source, tb->ltb_text, sizeof(source));
James Simmons51078e22016-02-12 12:06:04 -05001061 source[sizeof(source) - 1] = '\0';
Peng Taod7e09d02013-05-02 16:46:55 +08001062
1063 /* replace ltb_text with the network(s) add on match */
1064 rc = lnet_match_network_tokens(tb->ltb_text, ipaddrs, nip);
1065 if (rc < 0)
1066 break;
1067
1068 list_del(&tb->ltb_list);
1069
James Simmons5fd88332016-02-12 12:06:09 -05001070 if (!rc) { /* no match */
Peng Taod7e09d02013-05-02 16:46:55 +08001071 lnet_free_text_buf(tb);
1072 continue;
1073 }
1074
1075 /* split into separate networks */
1076 INIT_LIST_HEAD(&current_nets);
1077 list_add(&tb->ltb_list, &current_nets);
1078 rc = lnet_splitnets(source, &current_nets);
1079 if (rc < 0)
1080 break;
1081
1082 dup = 0;
Rashika Kheria24edbe42013-10-26 00:21:24 +05301083 list_for_each(t, &current_nets) {
James Simmonsddbc66a2016-02-12 12:05:59 -05001084 tb = list_entry(t, struct lnet_text_buf, ltb_list);
Peng Taod7e09d02013-05-02 16:46:55 +08001085 net1 = lnet_netspec2net(tb->ltb_text);
Rashika Kheria24edbe42013-10-26 00:21:24 +05301086 LASSERT(net1 != LNET_NIDNET(LNET_NID_ANY));
Peng Taod7e09d02013-05-02 16:46:55 +08001087
1088 list_for_each(t2, &matched_nets) {
James Simmonsddbc66a2016-02-12 12:05:59 -05001089 tb2 = list_entry(t2, struct lnet_text_buf,
1090 ltb_list);
Peng Taod7e09d02013-05-02 16:46:55 +08001091 net2 = lnet_netspec2net(tb2->ltb_text);
Rashika Kheria24edbe42013-10-26 00:21:24 +05301092 LASSERT(net2 != LNET_NIDNET(LNET_NID_ANY));
Peng Taod7e09d02013-05-02 16:46:55 +08001093
1094 if (net1 == net2) {
1095 dup = 1;
1096 break;
1097 }
1098 }
1099
1100 if (dup)
1101 break;
1102 }
1103
1104 if (dup) {
1105 lnet_free_text_bufs(&current_nets);
1106 continue;
1107 }
1108
1109 list_for_each_safe(t, t2, &current_nets) {
James Simmonsddbc66a2016-02-12 12:05:59 -05001110 tb = list_entry(t, struct lnet_text_buf, ltb_list);
Peng Taod7e09d02013-05-02 16:46:55 +08001111
1112 list_del(&tb->ltb_list);
1113 list_add_tail(&tb->ltb_list, &matched_nets);
1114
1115 len += snprintf(networks + len, sizeof(networks) - len,
James Simmons5fd88332016-02-12 12:06:09 -05001116 "%s%s", !len ? "" : ",",
Peng Taod7e09d02013-05-02 16:46:55 +08001117 tb->ltb_text);
1118
1119 if (len >= sizeof(networks)) {
1120 CERROR("Too many matched networks\n");
1121 rc = -E2BIG;
1122 goto out;
1123 }
1124 }
1125
1126 count++;
1127 }
1128
1129 out:
1130 lnet_free_text_bufs(&raw_entries);
1131 lnet_free_text_bufs(&matched_nets);
1132 lnet_free_text_bufs(&current_nets);
James Simmons5fd88332016-02-12 12:06:09 -05001133 LASSERT(!lnet_tbnob);
Peng Taod7e09d02013-05-02 16:46:55 +08001134
1135 if (rc < 0)
1136 return rc;
1137
1138 *networksp = networks;
1139 return count;
1140}
1141
Benedict Boerger714340d2014-08-08 18:26:22 +02001142static int
Rashika Kheria24edbe42013-10-26 00:21:24 +05301143lnet_ipaddr_enumerate(__u32 **ipaddrsp)
Peng Taod7e09d02013-05-02 16:46:55 +08001144{
Mike Shuey7e7ab092015-05-19 10:14:32 -04001145 int up;
1146 __u32 netmask;
1147 __u32 *ipaddrs;
1148 __u32 *ipaddrs2;
1149 int nip;
1150 char **ifnames;
James Simmons1ad6a732015-06-08 22:27:10 -04001151 int nif = lnet_ipif_enumerate(&ifnames);
Mike Shuey7e7ab092015-05-19 10:14:32 -04001152 int i;
1153 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001154
1155 if (nif <= 0)
1156 return nif;
1157
1158 LIBCFS_ALLOC(ipaddrs, nif * sizeof(*ipaddrs));
James Simmons06ace262016-02-12 12:06:08 -05001159 if (!ipaddrs) {
Peng Taod7e09d02013-05-02 16:46:55 +08001160 CERROR("Can't allocate ipaddrs[%d]\n", nif);
James Simmons1ad6a732015-06-08 22:27:10 -04001161 lnet_ipif_free_enumeration(ifnames, nif);
Peng Taod7e09d02013-05-02 16:46:55 +08001162 return -ENOMEM;
1163 }
1164
1165 for (i = nip = 0; i < nif; i++) {
1166 if (!strcmp(ifnames[i], "lo"))
1167 continue;
1168
James Simmons1ad6a732015-06-08 22:27:10 -04001169 rc = lnet_ipif_query(ifnames[i], &up, &ipaddrs[nip], &netmask);
James Simmons5fd88332016-02-12 12:06:09 -05001170 if (rc) {
Peng Taod7e09d02013-05-02 16:46:55 +08001171 CWARN("Can't query interface %s: %d\n",
1172 ifnames[i], rc);
1173 continue;
1174 }
1175
1176 if (!up) {
1177 CWARN("Ignoring interface %s: it's down\n",
1178 ifnames[i]);
1179 continue;
1180 }
1181
1182 nip++;
1183 }
1184
James Simmons1ad6a732015-06-08 22:27:10 -04001185 lnet_ipif_free_enumeration(ifnames, nif);
Peng Taod7e09d02013-05-02 16:46:55 +08001186
1187 if (nip == nif) {
1188 *ipaddrsp = ipaddrs;
1189 } else {
1190 if (nip > 0) {
1191 LIBCFS_ALLOC(ipaddrs2, nip * sizeof(*ipaddrs2));
James Simmons06ace262016-02-12 12:06:08 -05001192 if (!ipaddrs2) {
Peng Taod7e09d02013-05-02 16:46:55 +08001193 CERROR("Can't allocate ipaddrs[%d]\n", nip);
1194 nip = -ENOMEM;
1195 } else {
1196 memcpy(ipaddrs2, ipaddrs,
1197 nip * sizeof(*ipaddrs));
1198 *ipaddrsp = ipaddrs2;
1199 rc = nip;
1200 }
1201 }
Shivani Bhardwajcfa38112015-10-29 12:28:31 +05301202 LIBCFS_FREE(ipaddrs, nip * sizeof(*ipaddrs));
Peng Taod7e09d02013-05-02 16:46:55 +08001203 }
1204 return nip;
1205}
1206
1207int
Rashika Kheria24edbe42013-10-26 00:21:24 +05301208lnet_parse_ip2nets(char **networksp, char *ip2nets)
Peng Taod7e09d02013-05-02 16:46:55 +08001209{
Mike Shuey7e7ab092015-05-19 10:14:32 -04001210 __u32 *ipaddrs = NULL;
1211 int nip = lnet_ipaddr_enumerate(&ipaddrs);
1212 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001213
1214 if (nip < 0) {
Rashika Kheriae46b3a02013-10-26 00:22:54 +05301215 LCONSOLE_ERROR_MSG(0x117,
1216 "Error %d enumerating local IP interfaces for ip2nets to match\n",
1217 nip);
Peng Taod7e09d02013-05-02 16:46:55 +08001218 return nip;
1219 }
1220
James Simmons5fd88332016-02-12 12:06:09 -05001221 if (!nip) {
Rashika Kheriae46b3a02013-10-26 00:22:54 +05301222 LCONSOLE_ERROR_MSG(0x118,
1223 "No local IP interfaces for ip2nets to match\n");
Peng Taod7e09d02013-05-02 16:46:55 +08001224 return -ENOENT;
1225 }
1226
1227 rc = lnet_match_networks(networksp, ip2nets, ipaddrs, nip);
Shivani Bhardwajcfa38112015-10-29 12:28:31 +05301228 LIBCFS_FREE(ipaddrs, nip * sizeof(*ipaddrs));
Peng Taod7e09d02013-05-02 16:46:55 +08001229
1230 if (rc < 0) {
1231 LCONSOLE_ERROR_MSG(0x119, "Error %d parsing ip2nets\n", rc);
1232 return rc;
1233 }
1234
James Simmons5fd88332016-02-12 12:06:09 -05001235 if (!rc) {
Rashika Kheriae46b3a02013-10-26 00:22:54 +05301236 LCONSOLE_ERROR_MSG(0x11a,
1237 "ip2nets does not match any local IP interfaces\n");
Peng Taod7e09d02013-05-02 16:46:55 +08001238 return -ENOENT;
1239 }
1240
1241 return 0;
1242}