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