]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/iplink_vlan.c
treewide: refactor help messages
[mirror_iproute2.git] / ip / iplink_vlan.c
1 /*
2 * iplink_vlan.c VLAN device support
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: Patrick McHardy <kaber@trash.net>
10 */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <linux/if_vlan.h>
16
17 #include "rt_names.h"
18 #include "utils.h"
19 #include "ip_common.h"
20
21 static void print_explain(FILE *f)
22 {
23 fprintf(f,
24 "Usage: ... vlan id VLANID\n"
25 " [ protocol VLANPROTO ]\n"
26 " [ reorder_hdr { on | off } ]\n"
27 " [ gvrp { on | off } ]\n"
28 " [ mvrp { on | off } ]\n"
29 " [ loose_binding { on | off } ]\n"
30 " [ bridge_binding { on | off } ]\n"
31 " [ ingress-qos-map QOS-MAP ]\n"
32 " [ egress-qos-map QOS-MAP ]\n"
33 "\n"
34 "VLANID := 0-4095\n"
35 "VLANPROTO: [ 802.1Q | 802.1ad ]\n"
36 "QOS-MAP := [ QOS-MAP ] QOS-MAPPING\n"
37 "QOS-MAPPING := FROM:TO\n"
38 );
39 }
40
41 static void explain(void)
42 {
43 print_explain(stderr);
44 }
45
46 static int on_off(const char *msg, const char *arg)
47 {
48 fprintf(stderr, "Error: argument of \"%s\" must be \"on\" or \"off\", not \"%s\"\n", msg, arg);
49 return -1;
50 }
51
52 static int vlan_parse_qos_map(int *argcp, char ***argvp, struct nlmsghdr *n,
53 int attrtype)
54 {
55 int argc = *argcp;
56 char **argv = *argvp;
57 struct ifla_vlan_qos_mapping m;
58 struct rtattr *tail;
59
60 tail = addattr_nest(n, 1024, attrtype);
61
62 while (argc > 0) {
63 char *colon = strchr(*argv, ':');
64
65 if (!colon)
66 break;
67 *colon = '\0';
68
69 if (get_u32(&m.from, *argv, 0))
70 return 1;
71 if (get_u32(&m.to, colon + 1, 0))
72 return 1;
73 argc--, argv++;
74
75 addattr_l(n, 1024, IFLA_VLAN_QOS_MAPPING, &m, sizeof(m));
76 }
77
78 addattr_nest_end(n, tail);
79
80 *argcp = argc;
81 *argvp = argv;
82 return 0;
83 }
84
85 static int vlan_parse_opt(struct link_util *lu, int argc, char **argv,
86 struct nlmsghdr *n)
87 {
88 struct ifla_vlan_flags flags = { 0 };
89 __u16 id, proto;
90
91 while (argc > 0) {
92 if (matches(*argv, "protocol") == 0) {
93 NEXT_ARG();
94 if (ll_proto_a2n(&proto, *argv))
95 invarg("protocol is invalid", *argv);
96 addattr_l(n, 1024, IFLA_VLAN_PROTOCOL, &proto, 2);
97 } else if (matches(*argv, "id") == 0) {
98 NEXT_ARG();
99 if (get_u16(&id, *argv, 0))
100 invarg("id is invalid", *argv);
101 addattr_l(n, 1024, IFLA_VLAN_ID, &id, 2);
102 } else if (matches(*argv, "reorder_hdr") == 0) {
103 NEXT_ARG();
104 flags.mask |= VLAN_FLAG_REORDER_HDR;
105 if (strcmp(*argv, "on") == 0)
106 flags.flags |= VLAN_FLAG_REORDER_HDR;
107 else if (strcmp(*argv, "off") == 0)
108 flags.flags &= ~VLAN_FLAG_REORDER_HDR;
109 else
110 return on_off("reorder_hdr", *argv);
111 } else if (matches(*argv, "gvrp") == 0) {
112 NEXT_ARG();
113 flags.mask |= VLAN_FLAG_GVRP;
114 if (strcmp(*argv, "on") == 0)
115 flags.flags |= VLAN_FLAG_GVRP;
116 else if (strcmp(*argv, "off") == 0)
117 flags.flags &= ~VLAN_FLAG_GVRP;
118 else
119 return on_off("gvrp", *argv);
120 } else if (matches(*argv, "mvrp") == 0) {
121 NEXT_ARG();
122 flags.mask |= VLAN_FLAG_MVRP;
123 if (strcmp(*argv, "on") == 0)
124 flags.flags |= VLAN_FLAG_MVRP;
125 else if (strcmp(*argv, "off") == 0)
126 flags.flags &= ~VLAN_FLAG_MVRP;
127 else
128 return on_off("mvrp", *argv);
129 } else if (matches(*argv, "loose_binding") == 0) {
130 NEXT_ARG();
131 flags.mask |= VLAN_FLAG_LOOSE_BINDING;
132 if (strcmp(*argv, "on") == 0)
133 flags.flags |= VLAN_FLAG_LOOSE_BINDING;
134 else if (strcmp(*argv, "off") == 0)
135 flags.flags &= ~VLAN_FLAG_LOOSE_BINDING;
136 else
137 return on_off("loose_binding", *argv);
138 } else if (matches(*argv, "bridge_binding") == 0) {
139 NEXT_ARG();
140 flags.mask |= VLAN_FLAG_BRIDGE_BINDING;
141 if (strcmp(*argv, "on") == 0)
142 flags.flags |= VLAN_FLAG_BRIDGE_BINDING;
143 else if (strcmp(*argv, "off") == 0)
144 flags.flags &= ~VLAN_FLAG_BRIDGE_BINDING;
145 else
146 return on_off("bridge_binding", *argv);
147 } else if (matches(*argv, "ingress-qos-map") == 0) {
148 NEXT_ARG();
149 if (vlan_parse_qos_map(&argc, &argv, n,
150 IFLA_VLAN_INGRESS_QOS))
151 invarg("invalid ingress-qos-map", *argv);
152 continue;
153 } else if (matches(*argv, "egress-qos-map") == 0) {
154 NEXT_ARG();
155 if (vlan_parse_qos_map(&argc, &argv, n,
156 IFLA_VLAN_EGRESS_QOS))
157 invarg("invalid egress-qos-map", *argv);
158 continue;
159 } else if (matches(*argv, "help") == 0) {
160 explain();
161 return -1;
162 } else {
163 fprintf(stderr, "vlan: unknown command \"%s\"?\n", *argv);
164 explain();
165 return -1;
166 }
167 argc--, argv++;
168 }
169
170 if (flags.mask)
171 addattr_l(n, 1024, IFLA_VLAN_FLAGS, &flags, sizeof(flags));
172
173 return 0;
174 }
175
176 static void vlan_print_map(FILE *f,
177 const char *name_json,
178 const char *name_fp,
179 struct rtattr *attr)
180 {
181 struct ifla_vlan_qos_mapping *m;
182 struct rtattr *i;
183 int rem;
184
185 open_json_array(PRINT_JSON, name_json);
186 print_string(PRINT_FP, NULL, "\n %s { ", name_fp);
187
188 rem = RTA_PAYLOAD(attr);
189 for (i = RTA_DATA(attr); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
190 m = RTA_DATA(i);
191
192 if (is_json_context()) {
193 open_json_object(NULL);
194 print_uint(PRINT_JSON, "from", NULL, m->from);
195 print_uint(PRINT_JSON, "to", NULL, m->to);
196 close_json_object();
197 } else {
198 fprintf(f, "%u:%u ", m->from, m->to);
199 }
200 }
201
202 close_json_array(PRINT_JSON, NULL);
203 print_string(PRINT_FP, NULL, "%s ", "}");
204 }
205
206 static void vlan_print_flags(FILE *fp, __u32 flags)
207 {
208 open_json_array(PRINT_ANY, is_json_context() ? "flags" : "<");
209 #define _PF(f) if (flags & VLAN_FLAG_##f) { \
210 flags &= ~VLAN_FLAG_##f; \
211 print_string(PRINT_ANY, NULL, flags ? "%s," : "%s", #f); \
212 }
213 _PF(REORDER_HDR);
214 _PF(GVRP);
215 _PF(MVRP);
216 _PF(LOOSE_BINDING);
217 _PF(BRIDGE_BINDING);
218 #undef _PF
219 if (flags)
220 print_hex(PRINT_ANY, NULL, "%x", flags);
221 close_json_array(PRINT_ANY, "> ");
222 }
223
224 static void vlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
225 {
226 struct ifla_vlan_flags *flags;
227
228 SPRINT_BUF(b1);
229
230 if (!tb)
231 return;
232
233 if (tb[IFLA_VLAN_PROTOCOL] &&
234 RTA_PAYLOAD(tb[IFLA_VLAN_PROTOCOL]) < sizeof(__u16))
235 return;
236 if (!tb[IFLA_VLAN_ID] ||
237 RTA_PAYLOAD(tb[IFLA_VLAN_ID]) < sizeof(__u16))
238 return;
239
240 if (tb[IFLA_VLAN_PROTOCOL])
241 print_string(PRINT_ANY,
242 "protocol",
243 "protocol %s ",
244 ll_proto_n2a(
245 rta_getattr_u16(tb[IFLA_VLAN_PROTOCOL]),
246 b1, sizeof(b1)));
247 else
248 print_string(PRINT_ANY, "protocol", "protocol %s ", "802.1q");
249
250 print_uint(PRINT_ANY,
251 "id",
252 "id %u ",
253 rta_getattr_u16(tb[IFLA_VLAN_ID]));
254
255 if (tb[IFLA_VLAN_FLAGS]) {
256 if (RTA_PAYLOAD(tb[IFLA_VLAN_FLAGS]) < sizeof(*flags))
257 return;
258 flags = RTA_DATA(tb[IFLA_VLAN_FLAGS]);
259 vlan_print_flags(f, flags->flags);
260 }
261 if (tb[IFLA_VLAN_INGRESS_QOS])
262 vlan_print_map(f,
263 "ingress_qos",
264 "ingress-qos-map",
265 tb[IFLA_VLAN_INGRESS_QOS]);
266 if (tb[IFLA_VLAN_EGRESS_QOS])
267 vlan_print_map(f,
268 "egress_qos",
269 "egress-qos-map",
270 tb[IFLA_VLAN_EGRESS_QOS]);
271 }
272
273 static void vlan_print_help(struct link_util *lu, int argc, char **argv,
274 FILE *f)
275 {
276 print_explain(f);
277 }
278
279 struct link_util vlan_link_util = {
280 .id = "vlan",
281 .maxattr = IFLA_VLAN_MAX,
282 .parse_opt = vlan_parse_opt,
283 .print_opt = vlan_print_opt,
284 .print_help = vlan_print_help,
285 };