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