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