]>
Commit | Line | Data |
---|---|---|
835d8321 | 1 | // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB |
74bd75c2 LR |
2 | /* |
3 | * rdma.c RDMA tool | |
74bd75c2 LR |
4 | * Authors: Leon Romanovsky <leonro@mellanox.com> |
5 | */ | |
6 | ||
7 | #include "rdma.h" | |
8 | #include "SNAPSHOT.h" | |
9 | ||
10 | static void help(char *name) | |
11 | { | |
12 | pr_out("Usage: %s [ OPTIONS ] OBJECT { COMMAND | help }\n" | |
486fe5f0 | 13 | " %s [ -f[orce] ] -b[atch] filename\n" |
923aa825 | 14 | "where OBJECT := { dev | link | resource | help }\n" |
486fe5f0 | 15 | " OPTIONS := { -V[ersion] | -d[etails] | -j[son] | -p[retty]}\n", name, name); |
74bd75c2 LR |
16 | } |
17 | ||
18 | static int cmd_help(struct rd *rd) | |
19 | { | |
20 | help(rd->filename); | |
21 | return 0; | |
22 | } | |
23 | ||
486fe5f0 | 24 | static int rd_cmd(struct rd *rd, int argc, char **argv) |
74bd75c2 LR |
25 | { |
26 | const struct rd_cmd cmds[] = { | |
27 | { NULL, cmd_help }, | |
28 | { "help", cmd_help }, | |
40df8263 | 29 | { "dev", cmd_dev }, |
da990ab4 | 30 | { "link", cmd_link }, |
923aa825 | 31 | { "resource", cmd_res }, |
74bd75c2 LR |
32 | { 0 } |
33 | }; | |
34 | ||
486fe5f0 LR |
35 | rd->argc = argc; |
36 | rd->argv = argv; | |
37 | ||
74bd75c2 LR |
38 | return rd_exec_cmd(rd, cmds, "object"); |
39 | } | |
40 | ||
486fe5f0 LR |
41 | static int rd_batch(struct rd *rd, const char *name, bool force) |
42 | { | |
43 | char *line = NULL; | |
44 | size_t len = 0; | |
45 | int ret = 0; | |
46 | ||
47 | if (name && strcmp(name, "-") != 0) { | |
48 | if (!freopen(name, "r", stdin)) { | |
49 | pr_err("Cannot open file \"%s\" for reading: %s\n", | |
50 | name, strerror(errno)); | |
51 | return errno; | |
52 | } | |
53 | } | |
54 | ||
55 | cmdlineno = 0; | |
56 | while (getcmdline(&line, &len, stdin) != -1) { | |
57 | char *largv[512]; | |
58 | int largc; | |
59 | ||
60 | largc = makeargs(line, largv, ARRAY_SIZE(largv)); | |
61 | if (!largc) | |
62 | continue; /* blank line */ | |
63 | ||
64 | ret = rd_cmd(rd, largc, largv); | |
65 | if (ret) { | |
66 | pr_err("Command failed %s:%d\n", name, cmdlineno); | |
67 | if (!force) | |
68 | break; | |
69 | } | |
70 | } | |
71 | ||
72 | free(line); | |
73 | ||
74 | return ret; | |
75 | } | |
76 | ||
77 | static int rd_init(struct rd *rd, char *filename) | |
74bd75c2 LR |
78 | { |
79 | uint32_t seq; | |
80 | int ret; | |
81 | ||
82 | rd->filename = filename; | |
74bd75c2 | 83 | INIT_LIST_HEAD(&rd->dev_map_list); |
1174be72 | 84 | INIT_LIST_HEAD(&rd->filter_list); |
ab6e2b7b LR |
85 | |
86 | if (rd->json_output) { | |
87 | rd->jw = jsonw_new(stdout); | |
88 | if (!rd->jw) { | |
89 | pr_err("Failed to create JSON writer\n"); | |
90 | return -ENOMEM; | |
91 | } | |
92 | jsonw_pretty(rd->jw, rd->pretty_output); | |
93 | } | |
94 | ||
74bd75c2 LR |
95 | rd->buff = malloc(MNL_SOCKET_BUFFER_SIZE); |
96 | if (!rd->buff) | |
97 | return -ENOMEM; | |
98 | ||
99 | rd_prepare_msg(rd, RDMA_NLDEV_CMD_GET, | |
100 | &seq, (NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP)); | |
101 | ret = rd_send_msg(rd); | |
102 | if (ret) | |
103 | return ret; | |
104 | ||
105 | return rd_recv_msg(rd, rd_dev_init_cb, rd, seq); | |
106 | } | |
107 | ||
7109f4b2 | 108 | static void rd_cleanup(struct rd *rd) |
74bd75c2 | 109 | { |
ab6e2b7b LR |
110 | if (rd->json_output) |
111 | jsonw_destroy(&rd->jw); | |
5fc17280 | 112 | rd_free(rd); |
74bd75c2 LR |
113 | } |
114 | ||
115 | int main(int argc, char **argv) | |
116 | { | |
117 | static const struct option long_options[] = { | |
118 | { "version", no_argument, NULL, 'V' }, | |
119 | { "help", no_argument, NULL, 'h' }, | |
ab6e2b7b LR |
120 | { "json", no_argument, NULL, 'j' }, |
121 | { "pretty", no_argument, NULL, 'p' }, | |
74bd75c2 | 122 | { "details", no_argument, NULL, 'd' }, |
486fe5f0 LR |
123 | { "force", no_argument, NULL, 'f' }, |
124 | { "batch", required_argument, NULL, 'b' }, | |
74bd75c2 LR |
125 | { NULL, 0, NULL, 0 } |
126 | }; | |
33115275 | 127 | bool show_driver_details = false; |
486fe5f0 | 128 | const char *batch_file = NULL; |
ab6e2b7b | 129 | bool pretty_output = false; |
74bd75c2 | 130 | bool show_details = false; |
ab6e2b7b | 131 | bool json_output = false; |
486fe5f0 | 132 | bool force = false; |
80c0478f | 133 | struct rd rd = {}; |
33115275 | 134 | char *filename; |
74bd75c2 LR |
135 | int opt; |
136 | int err; | |
137 | ||
138 | filename = basename(argv[0]); | |
139 | ||
486fe5f0 | 140 | while ((opt = getopt_long(argc, argv, ":Vhdpjfb:", |
74bd75c2 LR |
141 | long_options, NULL)) >= 0) { |
142 | switch (opt) { | |
143 | case 'V': | |
144 | printf("%s utility, iproute2-ss%s\n", | |
145 | filename, SNAPSHOT); | |
146 | return EXIT_SUCCESS; | |
ab6e2b7b LR |
147 | case 'p': |
148 | pretty_output = true; | |
149 | break; | |
74bd75c2 | 150 | case 'd': |
33115275 SW |
151 | if (show_details) |
152 | show_driver_details = true; | |
153 | else | |
154 | show_details = true; | |
74bd75c2 | 155 | break; |
ab6e2b7b LR |
156 | case 'j': |
157 | json_output = true; | |
158 | break; | |
486fe5f0 LR |
159 | case 'f': |
160 | force = true; | |
161 | break; | |
162 | case 'b': | |
163 | batch_file = optarg; | |
164 | break; | |
74bd75c2 LR |
165 | case 'h': |
166 | help(filename); | |
167 | return EXIT_SUCCESS; | |
486fe5f0 LR |
168 | case ':': |
169 | pr_err("-%c option requires an argument\n", optopt); | |
170 | return EXIT_FAILURE; | |
74bd75c2 LR |
171 | default: |
172 | pr_err("Unknown option.\n"); | |
173 | help(filename); | |
174 | return EXIT_FAILURE; | |
175 | } | |
176 | } | |
177 | ||
178 | argc -= optind; | |
179 | argv += optind; | |
180 | ||
ab6e2b7b | 181 | rd.show_details = show_details; |
33115275 | 182 | rd.show_driver_details = show_driver_details; |
ab6e2b7b LR |
183 | rd.json_output = json_output; |
184 | rd.pretty_output = pretty_output; | |
185 | ||
486fe5f0 | 186 | err = rd_init(&rd, filename); |
74bd75c2 LR |
187 | if (err) |
188 | goto out; | |
189 | ||
486fe5f0 LR |
190 | if (batch_file) |
191 | err = rd_batch(&rd, batch_file, force); | |
192 | else | |
193 | err = rd_cmd(&rd, argc, argv); | |
74bd75c2 LR |
194 | out: |
195 | /* Always cleanup */ | |
7109f4b2 | 196 | rd_cleanup(&rd); |
74bd75c2 LR |
197 | return err ? EXIT_FAILURE : EXIT_SUCCESS; |
198 | } |