1 /*
2 * Copyright (c) 2013-2020 Intel Corporation. All rights reserved.
3 * Copyright (c) 2016 Cisco Systems, Inc. All rights reserved.
4 *
5 * This software is available to you under the BSD license below:
6 *
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * - Redistributions of source code must retain the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer.
14 *
15 * - Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials
18 * provided with the distribution.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30 #include "config.h"
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <getopt.h>
36 #include <ctype.h>
37
38 #include <ofi_osd.h>
39
40 #include <rdma/fabric.h>
41 #include <rdma/fi_errno.h>
42
43 static struct fi_info *hints;
44 static char *node, *port;
45 static int ver = 0;
46 static int list_providers = 0;
47 static int verbose = 0, env = 0;
48 static char *envstr;
49
50 /* options and matching help strings need to be kept in sync */
51
52 static const struct option longopts[] = {
53 {"help", no_argument, NULL, 'h'},
54 {"node", required_argument, NULL, 'n'},
55 {"port", required_argument, NULL, 'P'},
56 {"caps", required_argument, NULL, 'c'},
57 {"mode", required_argument, NULL, 'm'},
58 {"ep_type", required_argument, NULL, 't'},
59 {"domain", required_argument, NULL, 'd'},
60 {"fabric", required_argument, NULL, 'f'},
61 {"addr_format", required_argument, NULL, 'a'},
62 {"provider", required_argument, NULL, 'p'},
63 {"env", no_argument, NULL, 'e'},
64 {"getenv", required_argument, NULL, 'g'},
65 {"list", no_argument, NULL, 'l'},
66 {"verbose", no_argument, NULL, 'v'},
67 {"version", no_argument, &ver, 1},
68 {0,0,0,0}
69 };
70
71 static const char *help_strings[][2] = {
72 {"", "\t\tdisplay this help and exit"},
73 {"NAME", "\t\tnode name or address"},
74 {"PNUM", "\t\tport number"},
75 {"CAP1|CAP2..", "\tone or more capabilities: FI_MSG|FI_RMA..."},
76 {"MOD1|MOD2..", "\tone or more modes, default all modes"},
77 {"EPTYPE", "\t\tspecify single endpoint type: FI_EP_MSG, FI_EP_DGRAM..."},
78 {"DOMAIN", "\t\tspecify the domain name"},
79 {"FABRIC", "\t\tspecify the fabric name"},
80 {"FMT", "\t\tspecify accepted address format: FI_FORMAT_UNSPEC, FI_SOCKADDR..."},
81 {"PROV", "\t\tspecify provider explicitly"},
82 {"", "\t\tprint libfabric environment variables"},
83 {"", "\t\tprint libfabric environment variables with substr"},
84 {"", "\t\tlist available libfabric providers"},
85 {"", "\t\tverbose output"},
86 {"", "\t\tprint version info and exit"},
87 {"", ""}
88 };
89
usage(void)90 static void usage(void)
91 {
92 int i = 0;
93 const struct option *ptr = longopts;
94
95 for (; ptr->name != NULL; ++i, ptr = &longopts[i])
96 if (ptr->has_arg == required_argument)
97 printf(" -%c, --%s=%s%s\n", ptr->val, ptr->name,
98 help_strings[i][0], help_strings[i][1]);
99 else if (ptr->flag != NULL)
100 printf(" --%s\t%s\n", ptr->name,
101 help_strings[i][1]);
102 else
103 printf(" -%c, --%s\t%s\n", ptr->val, ptr->name,
104 help_strings[i][1]);
105 }
106
107 #define ORCASE(SYM) \
108 do { \
109 if (strcasecmp(#SYM, inputstr) == 0) { \
110 *value = SYM; \
111 return 0; \
112 } \
113 } while (0)
114
str2cap(char * inputstr,uint64_t * value)115 static int str2cap(char *inputstr, uint64_t *value)
116 {
117 ORCASE(FI_MSG);
118 ORCASE(FI_RMA);
119 ORCASE(FI_TAGGED);
120 ORCASE(FI_ATOMIC);
121 ORCASE(FI_MULTICAST);
122
123 ORCASE(FI_READ);
124 ORCASE(FI_WRITE);
125 ORCASE(FI_RECV);
126 ORCASE(FI_SEND);
127 ORCASE(FI_REMOTE_READ);
128 ORCASE(FI_REMOTE_WRITE);
129
130 ORCASE(FI_MULTI_RECV);
131 ORCASE(FI_REMOTE_CQ_DATA);
132 ORCASE(FI_MORE);
133 ORCASE(FI_PEEK);
134 ORCASE(FI_TRIGGER);
135 ORCASE(FI_FENCE);
136
137 ORCASE(FI_SOURCE_ERR);
138 ORCASE(FI_LOCAL_COMM);
139 ORCASE(FI_REMOTE_COMM);
140 ORCASE(FI_SHARED_AV);
141 ORCASE(FI_RMA_EVENT);
142 ORCASE(FI_SOURCE);
143 ORCASE(FI_NAMED_RX_CTX);
144 ORCASE(FI_DIRECTED_RECV);
145 ORCASE(FI_HMEM);
146
147 fprintf(stderr, "error: Unrecognized capability: %s\n", inputstr);
148
149 return -EINVAL;
150 }
151
str2mode(char * inputstr,uint64_t * value)152 static int str2mode(char *inputstr, uint64_t *value)
153 {
154 ORCASE(FI_CONTEXT);
155 ORCASE(FI_MSG_PREFIX);
156 ORCASE(FI_ASYNC_IOV);
157 ORCASE(FI_RX_CQ_DATA);
158 ORCASE(FI_LOCAL_MR);
159 ORCASE(FI_NOTIFY_FLAGS_ONLY);
160 ORCASE(FI_RESTRICTED_COMP);
161 ORCASE(FI_CONTEXT2);
162
163 fprintf(stderr, "error: Unrecognized mode: %s\n", inputstr);
164
165 return -EINVAL;
166 }
167
str2ep_type(char * inputstr,enum fi_ep_type * value)168 static int str2ep_type(char *inputstr, enum fi_ep_type *value)
169 {
170 ORCASE(FI_EP_UNSPEC);
171 ORCASE(FI_EP_MSG);
172 ORCASE(FI_EP_DGRAM);
173 ORCASE(FI_EP_RDM);
174 ORCASE(FI_EP_SOCK_STREAM);
175 ORCASE(FI_EP_SOCK_DGRAM);
176
177 fprintf(stderr, "error: Unrecognized endpoint type: %s\n", inputstr);
178
179 return -EINVAL;
180 }
181
str2addr_format(char * inputstr,uint32_t * value)182 static int str2addr_format(char *inputstr, uint32_t *value)
183 {
184 ORCASE(FI_FORMAT_UNSPEC);
185 ORCASE(FI_SOCKADDR);
186 ORCASE(FI_SOCKADDR_IN);
187 ORCASE(FI_SOCKADDR_IN6);
188 ORCASE(FI_SOCKADDR_IB);
189 ORCASE(FI_ADDR_PSMX);
190 ORCASE(FI_ADDR_GNI);
191 ORCASE(FI_ADDR_BGQ);
192 ORCASE(FI_ADDR_MLX);
193 ORCASE(FI_ADDR_STR);
194 ORCASE(FI_ADDR_PSMX2);
195 ORCASE(FI_ADDR_EFA);
196
197 fprintf(stderr, "error: Unrecognized address format: %s\n", inputstr);
198
199 return -EINVAL;
200 }
201
tokparse(char * caps,int (* str2flag)(char *,uint64_t *),uint64_t * flags)202 static int tokparse(char *caps,
203 int (*str2flag)(char *, uint64_t *),
204 uint64_t *flags)
205 {
206 uint64_t value;
207 char *tok;
208 int ret;
209
210 for (tok = strtok(caps, "|"); tok != NULL; tok = strtok(NULL, "|")) {
211 ret = str2flag(tok, &value);
212 if (ret)
213 return ret;
214
215 *flags |= value;
216 }
217
218 return 0;
219 }
220
param_type(enum fi_param_type type)221 static const char *param_type(enum fi_param_type type)
222 {
223 switch (type) {
224 case FI_PARAM_STRING:
225 return "String";
226 case FI_PARAM_INT:
227 return "Integer";
228 case FI_PARAM_SIZE_T:
229 return "size_t";
230 case FI_PARAM_BOOL:
231 return "Boolean (0/1, on/off, true/false, yes/no)";
232 default:
233 return "Unknown";
234 }
235 }
236
print_vars(void)237 static int print_vars(void)
238 {
239 int ret, count, i;
240 struct fi_param *params;
241 char delim;
242
243 ret = fi_getparams(¶ms, &count);
244 if (ret)
245 return ret;
246
247 for (i = 0; i < count; ++i) {
248 if (envstr && !strcasestr(params[i].name, envstr))
249 continue;
250
251 printf("# %s: %s\n", params[i].name, param_type(params[i].type));
252 printf("# %s\n", params[i].help_string);
253
254 if (params[i].value) {
255 delim = strchr(params[i].value, ' ') ? '"' : '\0';
256 printf("%s=%c%s%c\n", params[i].name, delim,
257 params[i].value, delim);
258 }
259
260 printf("\n");
261 }
262
263 fi_freeparams(params);
264 return ret;
265 }
266
print_providers(struct fi_info * info)267 static int print_providers(struct fi_info *info)
268 {
269 struct fi_info *cur;
270 for (cur = info; cur; cur = cur->next) {
271 printf("%s:\n", cur->fabric_attr->prov_name);
272 printf(" version: %d.%d\n",
273 FI_MAJOR(cur->fabric_attr->prov_version),
274 FI_MINOR(cur->fabric_attr->prov_version));
275 }
276 return EXIT_SUCCESS;
277 }
278
print_short_info(struct fi_info * info)279 static int print_short_info(struct fi_info *info)
280 {
281 struct fi_info *cur;
282 for (cur = info; cur; cur = cur->next) {
283 printf("provider: %s\n", cur->fabric_attr->prov_name);
284 printf(" fabric: %s\n", cur->fabric_attr->name),
285 printf(" domain: %s\n", cur->domain_attr->name),
286 printf(" version: %d.%d\n", FI_MAJOR(cur->fabric_attr->prov_version),
287 FI_MINOR(cur->fabric_attr->prov_version));
288 if (!list_providers) {
289 printf(" type: %s\n", fi_tostr(&cur->ep_attr->type, FI_TYPE_EP_TYPE));
290 printf(" protocol: %s\n", fi_tostr(&cur->ep_attr->protocol, FI_TYPE_PROTOCOL));
291 }
292 }
293 return EXIT_SUCCESS;
294 }
295
print_long_info(struct fi_info * info)296 static int print_long_info(struct fi_info *info)
297 {
298 struct fi_info *cur;
299 for (cur = info; cur; cur = cur->next) {
300 printf("---\n");
301 printf("%s", fi_tostr(cur, FI_TYPE_INFO));
302 }
303 return EXIT_SUCCESS;
304 }
305
run(struct fi_info * hints,char * node,char * port)306 static int run(struct fi_info *hints, char *node, char *port)
307 {
308 struct fi_info *info;
309 int ret;
310 uint64_t flags;
311
312 flags = list_providers ? FI_PROV_ATTR_ONLY : 0;
313 ret = fi_getinfo(FI_VERSION(FI_MAJOR_VERSION, FI_MINOR_VERSION),
314 node, port, flags, hints, &info);
315 if (ret) {
316 fprintf(stderr, "fi_getinfo: %d\n", ret);
317 return ret;
318 }
319
320 if (env)
321 ret = print_vars();
322 else if (verbose)
323 ret = print_long_info(info);
324 else if (list_providers)
325 ret = print_providers(info);
326 else
327 ret = print_short_info(info);
328
329 fi_freeinfo(info);
330 return ret;
331 }
332
main(int argc,char ** argv)333 int main(int argc, char **argv)
334 {
335 int op, ret, option_index;
336 int use_hints = 0;
337
338 hints = fi_allocinfo();
339 if (!hints)
340 return EXIT_FAILURE;
341
342 hints->mode = ~0;
343 hints->domain_attr->mode = ~0;
344 hints->domain_attr->mr_mode = ~(FI_MR_BASIC | FI_MR_SCALABLE);
345
346 while ((op = getopt_long(argc, argv, "n:P:c:m:t:a:p:d:f:eg:lhv", longopts,
347 &option_index)) != -1) {
348 switch (op) {
349 case 0:
350 /* there is no short variant only for --version */
351 if (ver) {
352 printf("%s: %s\n", argv[0], PACKAGE_VERSION);
353 printf("libfabric: %s\n", fi_tostr("1", FI_TYPE_VERSION));
354 printf("libfabric api: %d.%d\n",
355 FI_MAJOR_VERSION, FI_MINOR_VERSION);
356 return EXIT_SUCCESS;
357 }
358 goto print_help;
359 case 'n':
360 node = optarg;
361 break;
362 case 'P':
363 port = optarg;
364 break;
365 case 'c':
366 ret = tokparse(optarg, str2cap, &hints->caps);
367 if (ret)
368 goto out;
369
370 use_hints = 1;
371 break;
372 case 'm':
373 hints->mode = 0;
374 ret = tokparse(optarg, str2mode, &hints->mode);
375 if (ret)
376 goto out;
377
378 use_hints = 1;
379 break;
380 case 't':
381 ret = str2ep_type(optarg, &hints->ep_attr->type);
382 if (ret)
383 goto out;
384
385 use_hints = 1;
386 break;
387 case 'a':
388 ret = str2addr_format(optarg, &hints->addr_format);
389 if (ret)
390 goto out;
391
392 use_hints = 1;
393 break;
394 case 'p':
395 free(hints->fabric_attr->prov_name);
396 hints->fabric_attr->prov_name = strdup(optarg);
397 use_hints = 1;
398 break;
399 case 'd':
400 hints->domain_attr->name = strdup(optarg);
401 use_hints = 1;
402 break;
403 case 'f':
404 hints->fabric_attr->name = strdup(optarg);
405 use_hints = 1;
406 break;
407 case 'g':
408 envstr = optarg;
409 /* fall through */
410 case 'e':
411 env = 1;
412 break;
413 case 'l':
414 list_providers = 1;
415 break;
416 case 'v':
417 verbose = 1;
418 break;
419 case 'h':
420 default:
421 print_help:
422 printf("Usage: %s\n", argv[0]);
423 usage();
424 return EXIT_FAILURE;
425 }
426 }
427
428 ret = run(use_hints ? hints : NULL, node, port);
429
430 out:
431 fi_freeinfo(hints);
432 return -ret;
433 }
434