]>
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" | |
da990ab4 | 18 | "where OBJECT := { dev | link | help }\n" |
ab6e2b7b | 19 | " OPTIONS := { -V[ersion] | -d[etails] | -j[son] | -p[retty]}\n", name); |
74bd75c2 LR |
20 | } |
21 | ||
22 | static int cmd_help(struct rd *rd) | |
23 | { | |
24 | help(rd->filename); | |
25 | return 0; | |
26 | } | |
27 | ||
28 | static int rd_cmd(struct rd *rd) | |
29 | { | |
30 | const struct rd_cmd cmds[] = { | |
31 | { NULL, cmd_help }, | |
32 | { "help", cmd_help }, | |
40df8263 | 33 | { "dev", cmd_dev }, |
da990ab4 | 34 | { "link", cmd_link }, |
74bd75c2 LR |
35 | { 0 } |
36 | }; | |
37 | ||
38 | return rd_exec_cmd(rd, cmds, "object"); | |
39 | } | |
40 | ||
41 | static int rd_init(struct rd *rd, int argc, char **argv, char *filename) | |
42 | { | |
43 | uint32_t seq; | |
44 | int ret; | |
45 | ||
46 | rd->filename = filename; | |
47 | rd->argc = argc; | |
48 | rd->argv = argv; | |
49 | INIT_LIST_HEAD(&rd->dev_map_list); | |
ab6e2b7b LR |
50 | |
51 | if (rd->json_output) { | |
52 | rd->jw = jsonw_new(stdout); | |
53 | if (!rd->jw) { | |
54 | pr_err("Failed to create JSON writer\n"); | |
55 | return -ENOMEM; | |
56 | } | |
57 | jsonw_pretty(rd->jw, rd->pretty_output); | |
58 | } | |
59 | ||
74bd75c2 LR |
60 | rd->buff = malloc(MNL_SOCKET_BUFFER_SIZE); |
61 | if (!rd->buff) | |
62 | return -ENOMEM; | |
63 | ||
64 | rd_prepare_msg(rd, RDMA_NLDEV_CMD_GET, | |
65 | &seq, (NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP)); | |
66 | ret = rd_send_msg(rd); | |
67 | if (ret) | |
68 | return ret; | |
69 | ||
70 | return rd_recv_msg(rd, rd_dev_init_cb, rd, seq); | |
71 | } | |
72 | ||
7109f4b2 | 73 | static void rd_cleanup(struct rd *rd) |
74bd75c2 | 74 | { |
ab6e2b7b LR |
75 | if (rd->json_output) |
76 | jsonw_destroy(&rd->jw); | |
5fc17280 | 77 | rd_free(rd); |
74bd75c2 LR |
78 | } |
79 | ||
80 | int main(int argc, char **argv) | |
81 | { | |
82 | static const struct option long_options[] = { | |
83 | { "version", no_argument, NULL, 'V' }, | |
84 | { "help", no_argument, NULL, 'h' }, | |
ab6e2b7b LR |
85 | { "json", no_argument, NULL, 'j' }, |
86 | { "pretty", no_argument, NULL, 'p' }, | |
74bd75c2 LR |
87 | { "details", no_argument, NULL, 'd' }, |
88 | { NULL, 0, NULL, 0 } | |
89 | }; | |
ab6e2b7b | 90 | bool pretty_output = false; |
74bd75c2 | 91 | bool show_details = false; |
ab6e2b7b | 92 | bool json_output = false; |
74bd75c2 LR |
93 | char *filename; |
94 | struct rd rd; | |
95 | int opt; | |
96 | int err; | |
97 | ||
98 | filename = basename(argv[0]); | |
99 | ||
ab6e2b7b | 100 | while ((opt = getopt_long(argc, argv, "Vhdpj", |
74bd75c2 LR |
101 | long_options, NULL)) >= 0) { |
102 | switch (opt) { | |
103 | case 'V': | |
104 | printf("%s utility, iproute2-ss%s\n", | |
105 | filename, SNAPSHOT); | |
106 | return EXIT_SUCCESS; | |
ab6e2b7b LR |
107 | case 'p': |
108 | pretty_output = true; | |
109 | break; | |
74bd75c2 LR |
110 | case 'd': |
111 | show_details = true; | |
112 | break; | |
ab6e2b7b LR |
113 | case 'j': |
114 | json_output = true; | |
115 | break; | |
74bd75c2 LR |
116 | case 'h': |
117 | help(filename); | |
118 | return EXIT_SUCCESS; | |
119 | default: | |
120 | pr_err("Unknown option.\n"); | |
121 | help(filename); | |
122 | return EXIT_FAILURE; | |
123 | } | |
124 | } | |
125 | ||
126 | argc -= optind; | |
127 | argv += optind; | |
128 | ||
ab6e2b7b LR |
129 | rd.show_details = show_details; |
130 | rd.json_output = json_output; | |
131 | rd.pretty_output = pretty_output; | |
132 | ||
74bd75c2 LR |
133 | err = rd_init(&rd, argc, argv, filename); |
134 | if (err) | |
135 | goto out; | |
136 | ||
74bd75c2 LR |
137 | err = rd_cmd(&rd); |
138 | out: | |
139 | /* Always cleanup */ | |
7109f4b2 | 140 | rd_cleanup(&rd); |
74bd75c2 LR |
141 | return err ? EXIT_FAILURE : EXIT_SUCCESS; |
142 | } |