]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/m_vlan.c
devlink: Add support for resource/dpipe relation
[mirror_iproute2.git] / tc / m_vlan.c
CommitLineData
8b1c0216
JP
1/*
2 * m_vlan.c vlan manipulation module
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: Jiri Pirko <jiri@resnulli.us>
10 */
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <unistd.h>
15#include <string.h>
16#include <linux/if_ether.h>
17#include "utils.h"
18#include "rt_names.h"
19#include "tc_util.h"
20#include <linux/tc_act/tc_vlan.h>
21
4654173e
SL
22static const char * const action_names[] = {
23 [TCA_VLAN_ACT_POP] = "pop",
24 [TCA_VLAN_ACT_PUSH] = "push",
25 [TCA_VLAN_ACT_MODIFY] = "modify",
26};
27
8b1c0216
JP
28static void explain(void)
29{
eb4bccf1
SH
30 fprintf(stderr,
31 "Usage: vlan pop\n"
32 " vlan push [ protocol VLANPROTO ] id VLANID [ priority VLANPRIO ] [CONTROL]\n"
33 " vlan modify [ protocol VLANPROTO ] id VLANID [ priority VLANPRIO ] [CONTROL]\n"
34 " VLANPROTO is one of 802.1Q or 802.1AD\n"
35 " with default: 802.1Q\n"
36 " CONTROL := reclassify | pipe | drop | continue | pass |\n"
37 " goto chain <CHAIN_INDEX>\n");
8b1c0216
JP
38}
39
40static void usage(void)
41{
42 explain();
43 exit(-1);
44}
45
4654173e
SL
46static bool has_push_attribs(int action)
47{
48 return action == TCA_VLAN_ACT_PUSH || action == TCA_VLAN_ACT_MODIFY;
49}
50
eb4bccf1
SH
51static void unexpected(const char *arg)
52{
53 fprintf(stderr,
54 "unexpected \"%s\" - action already specified\n",
55 arg);
56 explain();
57}
58
8b1c0216
JP
59static int parse_vlan(struct action_util *a, int *argc_p, char ***argv_p,
60 int tca_id, struct nlmsghdr *n)
61{
62 int argc = *argc_p;
63 char **argv = *argv_p;
64 struct rtattr *tail;
65 int action = 0;
66 __u16 id;
67 int id_set = 0;
68 __u16 proto;
69 int proto_set = 0;
0e43ed9d
HHZ
70 __u8 prio;
71 int prio_set = 0;
e67aba55 72 struct tc_vlan parm = {};
8b1c0216
JP
73
74 if (matches(*argv, "vlan") != 0)
75 return -1;
76
77 NEXT_ARG();
78
79 while (argc > 0) {
80 if (matches(*argv, "pop") == 0) {
81 if (action) {
eb4bccf1 82 unexpected(*argv);
8b1c0216
JP
83 return -1;
84 }
85 action = TCA_VLAN_ACT_POP;
86 } else if (matches(*argv, "push") == 0) {
87 if (action) {
eb4bccf1 88 unexpected(*argv);
8b1c0216
JP
89 return -1;
90 }
91 action = TCA_VLAN_ACT_PUSH;
4654173e
SL
92 } else if (matches(*argv, "modify") == 0) {
93 if (action) {
eb4bccf1 94 unexpected(*argv);
4654173e
SL
95 return -1;
96 }
97 action = TCA_VLAN_ACT_MODIFY;
8b1c0216 98 } else if (matches(*argv, "id") == 0) {
eb4bccf1
SH
99 if (!has_push_attribs(action))
100 invarg("only valid for push/modify", *argv);
101
8b1c0216
JP
102 NEXT_ARG();
103 if (get_u16(&id, *argv, 0))
104 invarg("id is invalid", *argv);
105 id_set = 1;
106 } else if (matches(*argv, "protocol") == 0) {
eb4bccf1
SH
107 if (!has_push_attribs(action))
108 invarg("only valid for push/modify", *argv);
109
8b1c0216
JP
110 NEXT_ARG();
111 if (ll_proto_a2n(&proto, *argv))
112 invarg("protocol is invalid", *argv);
113 proto_set = 1;
0e43ed9d 114 } else if (matches(*argv, "priority") == 0) {
eb4bccf1
SH
115 if (!has_push_attribs(action))
116 invarg("only valid for push/modify", *argv);
117
0e43ed9d
HHZ
118 NEXT_ARG();
119 if (get_u8(&prio, *argv, 0) || (prio & ~0x7))
120 invarg("prio is invalid", *argv);
121 prio_set = 1;
8b1c0216
JP
122 } else if (matches(*argv, "help") == 0) {
123 usage();
124 } else {
125 break;
126 }
127 argc--;
128 argv++;
129 }
130
e67aba55
JP
131 parse_action_control_dflt(&argc, &argv, &parm.action,
132 false, TC_ACT_PIPE);
8b1c0216 133
3572e01a 134 NEXT_ARG_FWD();
8b1c0216
JP
135 if (argc) {
136 if (matches(*argv, "index") == 0) {
137 NEXT_ARG();
138 if (get_u32(&parm.index, *argv, 10)) {
139 fprintf(stderr, "vlan: Illegal \"index\"\n");
140 return -1;
141 }
142 argc--;
143 argv++;
144 }
145 }
146
4654173e
SL
147 if (has_push_attribs(action) && !id_set) {
148 fprintf(stderr, "id needs to be set for %s\n",
149 action_names[action]);
8b1c0216
JP
150 explain();
151 return -1;
152 }
153
154 parm.v_action = action;
155 tail = NLMSG_TAIL(n);
156 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
157 addattr_l(n, MAX_MSG, TCA_VLAN_PARMS, &parm, sizeof(parm));
158 if (id_set)
159 addattr_l(n, MAX_MSG, TCA_VLAN_PUSH_VLAN_ID, &id, 2);
160 if (proto_set) {
161 if (proto != htons(ETH_P_8021Q) &&
162 proto != htons(ETH_P_8021AD)) {
163 fprintf(stderr, "protocol not supported\n");
164 explain();
165 return -1;
166 }
167
168 addattr_l(n, MAX_MSG, TCA_VLAN_PUSH_VLAN_PROTOCOL, &proto, 2);
169 }
0e43ed9d
HHZ
170 if (prio_set)
171 addattr8(n, MAX_MSG, TCA_VLAN_PUSH_VLAN_PRIORITY, prio);
172
8b1c0216
JP
173 tail->rta_len = (char *)NLMSG_TAIL(n) - (char *)tail;
174
175 *argc_p = argc;
176 *argv_p = argv;
177 return 0;
178}
179
180static int print_vlan(struct action_util *au, FILE *f, struct rtattr *arg)
181{
182 SPRINT_BUF(b1);
183 struct rtattr *tb[TCA_VLAN_MAX + 1];
184 __u16 val;
185 struct tc_vlan *parm;
186
187 if (arg == NULL)
188 return -1;
189
190 parse_rtattr_nested(tb, TCA_VLAN_MAX, arg);
191
192 if (!tb[TCA_VLAN_PARMS]) {
b021ee40 193 print_string(PRINT_FP, NULL, "%s", "[NULL vlan parameters]");
8b1c0216
JP
194 return -1;
195 }
196 parm = RTA_DATA(tb[TCA_VLAN_PARMS]);
197
b021ee40 198 print_string(PRINT_ANY, "kind", "%s ", "vlan");
eb4bccf1
SH
199 print_string(PRINT_ANY, "vlan_action", " %s",
200 action_names[parm->v_action]);
8b1c0216 201
32a121cb 202 switch (parm->v_action) {
8b1c0216 203 case TCA_VLAN_ACT_PUSH:
4654173e 204 case TCA_VLAN_ACT_MODIFY:
8b1c0216
JP
205 if (tb[TCA_VLAN_PUSH_VLAN_ID]) {
206 val = rta_getattr_u16(tb[TCA_VLAN_PUSH_VLAN_ID]);
b021ee40 207 print_uint(PRINT_ANY, "id", " id %u", val);
8b1c0216
JP
208 }
209 if (tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]) {
eb4bccf1
SH
210 __u16 proto;
211
212 proto = rta_getattr_u16(tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]);
b021ee40 213 print_string(PRINT_ANY, "protocol", " protocol %s",
eb4bccf1 214 ll_proto_n2a(proto, b1, sizeof(b1)));
8b1c0216 215 }
0e43ed9d
HHZ
216 if (tb[TCA_VLAN_PUSH_VLAN_PRIORITY]) {
217 val = rta_getattr_u8(tb[TCA_VLAN_PUSH_VLAN_PRIORITY]);
b021ee40 218 print_uint(PRINT_ANY, "priority", " priority %u", val);
0e43ed9d 219 }
8b1c0216
JP
220 break;
221 }
e67aba55 222 print_action_control(f, " ", parm->action, "");
8b1c0216 223
b021ee40
JP
224 print_uint(PRINT_ANY, "index", "\n\t index %u", parm->index);
225 print_int(PRINT_ANY, "ref", " ref %d", parm->refcnt);
226 print_int(PRINT_ANY, "bind", " bind %d", parm->bindcnt);
8b1c0216
JP
227
228 if (show_stats) {
229 if (tb[TCA_VLAN_TM]) {
230 struct tcf_t *tm = RTA_DATA(tb[TCA_VLAN_TM]);
32a121cb 231
8b1c0216
JP
232 print_tm(f, tm);
233 }
234 }
235
b021ee40 236 print_string(PRINT_FP, NULL, "%s", "\n");
8b1c0216
JP
237
238 return 0;
239}
240
241struct action_util vlan_action_util = {
242 .id = "vlan",
243 .parse_aopt = parse_vlan,
244 .print_aopt = print_vlan,
245};