]>
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" | |
fbef6555 | 8 | #include "version.h" |
b0a688a5 | 9 | #include "color.h" |
74bd75c2 LR |
10 | |
11 | static void help(char *name) | |
12 | { | |
13 | pr_out("Usage: %s [ OPTIONS ] OBJECT { COMMAND | help }\n" | |
486fe5f0 | 14 | " %s [ -f[orce] ] -b[atch] filename\n" |
5937552b | 15 | "where OBJECT := { dev | link | resource | system | statistic | help }\n" |
7c01e0fc | 16 | " OPTIONS := { -V[ersion] | -d[etails] | -j[son] | -p[retty] -r[aw]}\n", name, name); |
74bd75c2 LR |
17 | } |
18 | ||
19 | static int cmd_help(struct rd *rd) | |
20 | { | |
21 | help(rd->filename); | |
22 | return 0; | |
23 | } | |
24 | ||
486fe5f0 | 25 | static int rd_cmd(struct rd *rd, int argc, char **argv) |
74bd75c2 LR |
26 | { |
27 | const struct rd_cmd cmds[] = { | |
28 | { NULL, cmd_help }, | |
29 | { "help", cmd_help }, | |
40df8263 | 30 | { "dev", cmd_dev }, |
da990ab4 | 31 | { "link", cmd_link }, |
923aa825 | 32 | { "resource", cmd_res }, |
c4572a46 | 33 | { "system", cmd_sys }, |
5937552b | 34 | { "statistic", cmd_stat }, |
74bd75c2 LR |
35 | { 0 } |
36 | }; | |
37 | ||
486fe5f0 LR |
38 | rd->argc = argc; |
39 | rd->argv = argv; | |
40 | ||
74bd75c2 LR |
41 | return rd_exec_cmd(rd, cmds, "object"); |
42 | } | |
43 | ||
1d9a81b8 | 44 | static int rd_batch_cmd(int argc, char *argv[], void *data) |
486fe5f0 | 45 | { |
1d9a81b8 | 46 | struct rd *rd = data; |
486fe5f0 | 47 | |
1d9a81b8 PM |
48 | return rd_cmd(rd, argc, argv); |
49 | } | |
486fe5f0 | 50 | |
1d9a81b8 PM |
51 | static int rd_batch(struct rd *rd, const char *name, bool force) |
52 | { | |
53 | return do_batch(name, force, rd_batch_cmd, rd); | |
486fe5f0 LR |
54 | } |
55 | ||
56 | static int rd_init(struct rd *rd, char *filename) | |
74bd75c2 LR |
57 | { |
58 | uint32_t seq; | |
59 | int ret; | |
60 | ||
61 | rd->filename = filename; | |
74bd75c2 | 62 | INIT_LIST_HEAD(&rd->dev_map_list); |
1174be72 | 63 | INIT_LIST_HEAD(&rd->filter_list); |
ab6e2b7b | 64 | |
74bd75c2 LR |
65 | rd->buff = malloc(MNL_SOCKET_BUFFER_SIZE); |
66 | if (!rd->buff) | |
67 | return -ENOMEM; | |
68 | ||
69 | rd_prepare_msg(rd, RDMA_NLDEV_CMD_GET, | |
70 | &seq, (NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP)); | |
71 | ret = rd_send_msg(rd); | |
72 | if (ret) | |
73 | return ret; | |
74 | ||
75 | return rd_recv_msg(rd, rd_dev_init_cb, rd, seq); | |
76 | } | |
77 | ||
7109f4b2 | 78 | static void rd_cleanup(struct rd *rd) |
74bd75c2 | 79 | { |
5fc17280 | 80 | rd_free(rd); |
74bd75c2 LR |
81 | } |
82 | ||
83 | int main(int argc, char **argv) | |
84 | { | |
85 | static const struct option long_options[] = { | |
86 | { "version", no_argument, NULL, 'V' }, | |
87 | { "help", no_argument, NULL, 'h' }, | |
ab6e2b7b LR |
88 | { "json", no_argument, NULL, 'j' }, |
89 | { "pretty", no_argument, NULL, 'p' }, | |
74bd75c2 | 90 | { "details", no_argument, NULL, 'd' }, |
7c01e0fc | 91 | { "raw", no_argument, NULL, 'r' }, |
486fe5f0 LR |
92 | { "force", no_argument, NULL, 'f' }, |
93 | { "batch", required_argument, NULL, 'b' }, | |
74bd75c2 LR |
94 | { NULL, 0, NULL, 0 } |
95 | }; | |
33115275 | 96 | bool show_driver_details = false; |
486fe5f0 | 97 | const char *batch_file = NULL; |
74bd75c2 | 98 | bool show_details = false; |
ab6e2b7b | 99 | bool json_output = false; |
7c01e0fc | 100 | bool show_raw = false; |
486fe5f0 | 101 | bool force = false; |
80c0478f | 102 | struct rd rd = {}; |
33115275 | 103 | char *filename; |
74bd75c2 LR |
104 | int opt; |
105 | int err; | |
74bd75c2 LR |
106 | filename = basename(argv[0]); |
107 | ||
7c01e0fc | 108 | while ((opt = getopt_long(argc, argv, ":Vhdrpjfb:", |
74bd75c2 LR |
109 | long_options, NULL)) >= 0) { |
110 | switch (opt) { | |
111 | case 'V': | |
fbef6555 SH |
112 | printf("%s utility, iproute2-%s\n", |
113 | filename, version); | |
74bd75c2 | 114 | return EXIT_SUCCESS; |
ab6e2b7b | 115 | case 'p': |
b0a688a5 | 116 | pretty = 1; |
ab6e2b7b | 117 | break; |
74bd75c2 | 118 | case 'd': |
33115275 SW |
119 | if (show_details) |
120 | show_driver_details = true; | |
121 | else | |
122 | show_details = true; | |
74bd75c2 | 123 | break; |
7c01e0fc MG |
124 | case 'r': |
125 | show_raw = true; | |
126 | break; | |
ab6e2b7b | 127 | case 'j': |
b0a688a5 | 128 | json_output = 1; |
ab6e2b7b | 129 | break; |
486fe5f0 LR |
130 | case 'f': |
131 | force = true; | |
132 | break; | |
133 | case 'b': | |
134 | batch_file = optarg; | |
135 | break; | |
74bd75c2 LR |
136 | case 'h': |
137 | help(filename); | |
138 | return EXIT_SUCCESS; | |
486fe5f0 LR |
139 | case ':': |
140 | pr_err("-%c option requires an argument\n", optopt); | |
141 | return EXIT_FAILURE; | |
74bd75c2 LR |
142 | default: |
143 | pr_err("Unknown option.\n"); | |
144 | help(filename); | |
145 | return EXIT_FAILURE; | |
146 | } | |
147 | } | |
148 | ||
149 | argc -= optind; | |
150 | argv += optind; | |
151 | ||
ab6e2b7b | 152 | rd.show_details = show_details; |
33115275 | 153 | rd.show_driver_details = show_driver_details; |
ab6e2b7b | 154 | rd.json_output = json_output; |
b0a688a5 | 155 | rd.pretty_output = pretty; |
7c01e0fc | 156 | rd.show_raw = show_raw; |
ab6e2b7b | 157 | |
486fe5f0 | 158 | err = rd_init(&rd, filename); |
74bd75c2 LR |
159 | if (err) |
160 | goto out; | |
161 | ||
486fe5f0 LR |
162 | if (batch_file) |
163 | err = rd_batch(&rd, batch_file, force); | |
164 | else | |
165 | err = rd_cmd(&rd, argc, argv); | |
74bd75c2 LR |
166 | out: |
167 | /* Always cleanup */ | |
7109f4b2 | 168 | rd_cleanup(&rd); |
74bd75c2 LR |
169 | return err ? EXIT_FAILURE : EXIT_SUCCESS; |
170 | } |