]>
git.proxmox.com Git - mirror_iproute2.git/blob - ip/iplink_vlan.c
2 * iplink_vlan.c VLAN device support
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.
9 * Authors: Patrick McHardy <kaber@trash.net>
15 #include <linux/if_vlan.h>
19 #include "ip_common.h"
21 static void print_explain(FILE *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 " [ ingress-qos-map QOS-MAP ]\n"
31 " [ egress-qos-map QOS-MAP ]\n"
34 "VLANPROTO: [ 802.1Q / 802.1ad ]\n"
35 "QOS-MAP := [ QOS-MAP ] QOS-MAPPING\n"
36 "QOS-MAPPING := FROM:TO\n"
40 static void explain(void)
42 print_explain(stderr
);
45 static int on_off(const char *msg
, const char *arg
)
47 fprintf(stderr
, "Error: argument of \"%s\" must be \"on\" or \"off\", not \"%s\"\n", msg
, arg
);
51 static int vlan_parse_qos_map(int *argcp
, char ***argvp
, struct nlmsghdr
*n
,
56 struct ifla_vlan_qos_mapping m
;
59 tail
= addattr_nest(n
, 1024, attrtype
);
62 char *colon
= strchr(*argv
, ':');
68 if (get_u32(&m
.from
, *argv
, 0))
70 if (get_u32(&m
.to
, colon
+ 1, 0))
74 addattr_l(n
, 1024, IFLA_VLAN_QOS_MAPPING
, &m
, sizeof(m
));
77 addattr_nest_end(n
, tail
);
84 static int vlan_parse_opt(struct link_util
*lu
, int argc
, char **argv
,
87 struct ifla_vlan_flags flags
= { 0 };
91 if (matches(*argv
, "protocol") == 0) {
93 if (ll_proto_a2n(&proto
, *argv
))
94 invarg("protocol is invalid", *argv
);
95 addattr_l(n
, 1024, IFLA_VLAN_PROTOCOL
, &proto
, 2);
96 } else if (matches(*argv
, "id") == 0) {
98 if (get_u16(&id
, *argv
, 0))
99 invarg("id is invalid", *argv
);
100 addattr_l(n
, 1024, IFLA_VLAN_ID
, &id
, 2);
101 } else if (matches(*argv
, "reorder_hdr") == 0) {
103 flags
.mask
|= VLAN_FLAG_REORDER_HDR
;
104 if (strcmp(*argv
, "on") == 0)
105 flags
.flags
|= VLAN_FLAG_REORDER_HDR
;
106 else if (strcmp(*argv
, "off") == 0)
107 flags
.flags
&= ~VLAN_FLAG_REORDER_HDR
;
109 return on_off("reorder_hdr", *argv
);
110 } else if (matches(*argv
, "gvrp") == 0) {
112 flags
.mask
|= VLAN_FLAG_GVRP
;
113 if (strcmp(*argv
, "on") == 0)
114 flags
.flags
|= VLAN_FLAG_GVRP
;
115 else if (strcmp(*argv
, "off") == 0)
116 flags
.flags
&= ~VLAN_FLAG_GVRP
;
118 return on_off("gvrp", *argv
);
119 } else if (matches(*argv
, "mvrp") == 0) {
121 flags
.mask
|= VLAN_FLAG_MVRP
;
122 if (strcmp(*argv
, "on") == 0)
123 flags
.flags
|= VLAN_FLAG_MVRP
;
124 else if (strcmp(*argv
, "off") == 0)
125 flags
.flags
&= ~VLAN_FLAG_MVRP
;
127 return on_off("mvrp", *argv
);
128 } else if (matches(*argv
, "loose_binding") == 0) {
130 flags
.mask
|= VLAN_FLAG_LOOSE_BINDING
;
131 if (strcmp(*argv
, "on") == 0)
132 flags
.flags
|= VLAN_FLAG_LOOSE_BINDING
;
133 else if (strcmp(*argv
, "off") == 0)
134 flags
.flags
&= ~VLAN_FLAG_LOOSE_BINDING
;
136 return on_off("loose_binding", *argv
);
137 } else if (matches(*argv
, "ingress-qos-map") == 0) {
139 if (vlan_parse_qos_map(&argc
, &argv
, n
,
140 IFLA_VLAN_INGRESS_QOS
))
141 invarg("invalid ingress-qos-map", *argv
);
143 } else if (matches(*argv
, "egress-qos-map") == 0) {
145 if (vlan_parse_qos_map(&argc
, &argv
, n
,
146 IFLA_VLAN_EGRESS_QOS
))
147 invarg("invalid egress-qos-map", *argv
);
149 } else if (matches(*argv
, "help") == 0) {
153 fprintf(stderr
, "vlan: unknown command \"%s\"?\n", *argv
);
161 addattr_l(n
, 1024, IFLA_VLAN_FLAGS
, &flags
, sizeof(flags
));
166 static void vlan_print_map(FILE *f
,
167 const char *name_json
,
171 struct ifla_vlan_qos_mapping
*m
;
175 open_json_array(PRINT_JSON
, name_json
);
176 print_string(PRINT_FP
, NULL
, "\n %s { ", name_fp
);
178 rem
= RTA_PAYLOAD(attr
);
179 for (i
= RTA_DATA(attr
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
182 if (is_json_context()) {
183 open_json_object(NULL
);
184 print_uint(PRINT_JSON
, "from", NULL
, m
->from
);
185 print_uint(PRINT_JSON
, "to", NULL
, m
->to
);
188 fprintf(f
, "%u:%u ", m
->from
, m
->to
);
192 close_json_array(PRINT_JSON
, NULL
);
193 print_string(PRINT_FP
, NULL
, "%s ", "}");
196 static void vlan_print_flags(FILE *fp
, __u32 flags
)
198 open_json_array(PRINT_ANY
, is_json_context() ? "flags" : "<");
199 #define _PF(f) if (flags & VLAN_FLAG_##f) { \
200 flags &= ~VLAN_FLAG_##f; \
201 print_string(PRINT_ANY, NULL, flags ? "%s," : "%s", #f); \
209 print_hex(PRINT_ANY
, NULL
, "%x", flags
);
210 close_json_array(PRINT_ANY
, "> ");
213 static void vlan_print_opt(struct link_util
*lu
, FILE *f
, struct rtattr
*tb
[])
215 struct ifla_vlan_flags
*flags
;
222 if (tb
[IFLA_VLAN_PROTOCOL
] &&
223 RTA_PAYLOAD(tb
[IFLA_VLAN_PROTOCOL
]) < sizeof(__u16
))
225 if (!tb
[IFLA_VLAN_ID
] ||
226 RTA_PAYLOAD(tb
[IFLA_VLAN_ID
]) < sizeof(__u16
))
229 if (tb
[IFLA_VLAN_PROTOCOL
])
230 print_string(PRINT_ANY
,
234 rta_getattr_u16(tb
[IFLA_VLAN_PROTOCOL
]),
237 print_string(PRINT_ANY
, "protocol", "protocol %s ", "802.1q");
239 print_uint(PRINT_ANY
,
242 rta_getattr_u16(tb
[IFLA_VLAN_ID
]));
244 if (tb
[IFLA_VLAN_FLAGS
]) {
245 if (RTA_PAYLOAD(tb
[IFLA_VLAN_FLAGS
]) < sizeof(*flags
))
247 flags
= RTA_DATA(tb
[IFLA_VLAN_FLAGS
]);
248 vlan_print_flags(f
, flags
->flags
);
250 if (tb
[IFLA_VLAN_INGRESS_QOS
])
254 tb
[IFLA_VLAN_INGRESS_QOS
]);
255 if (tb
[IFLA_VLAN_EGRESS_QOS
])
259 tb
[IFLA_VLAN_EGRESS_QOS
]);
262 static void vlan_print_help(struct link_util
*lu
, int argc
, char **argv
,
268 struct link_util vlan_link_util
= {
270 .maxattr
= IFLA_VLAN_MAX
,
271 .parse_opt
= vlan_parse_opt
,
272 .print_opt
= vlan_print_opt
,
273 .print_help
= vlan_print_help
,