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