]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/m_skbedit.c
Merge branch 'bpf-btf' into iproute2-next
[mirror_iproute2.git] / tc / m_skbedit.c
CommitLineData
f72a7aab
AD
1/*
2 * m_skbedit.c SKB Editing module
3 *
4 * Copyright (c) 2008, Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
4d98ab00
SH
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses>.
f72a7aab
AD
17 *
18 * Authors: Alexander Duyck <alexander.h.duyck@intel.com>
19 *
20 */
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <unistd.h>
25#include <string.h>
26#include "utils.h"
27#include "tc_util.h"
28#include <linux/tc_act/tc_skbedit.h>
1d1e0fd2 29#include <linux/if_packet.h>
f72a7aab 30
1d1e0fd2 31static void explain(void)
f72a7aab 32{
1d1e0fd2 33 fprintf(stderr, "Usage: ... skbedit <[QM] [PM] [MM] [PT]>\n"
e04dd30a 34 "QM = queue_mapping QUEUE_MAPPING\n"
32a121cb
SH
35 "PM = priority PRIORITY\n"
36 "MM = mark MARK\n"
1d1e0fd2
JHS
37 "PT = ptype PACKETYPE\n"
38 "PACKETYPE = is one of:\n"
39 " host, otherhost, broadcast, multicast\n"
e04dd30a
JHS
40 "QUEUE_MAPPING = device transmit queue to use\n"
41 "PRIORITY = classID to assign to priority field\n"
42 "MARK = firewall mark to set\n");
f72a7aab
AD
43}
44
45static void
46usage(void)
47{
48 explain();
49 exit(-1);
50}
51
52static int
53parse_skbedit(struct action_util *a, int *argc_p, char ***argv_p, int tca_id,
54 struct nlmsghdr *n)
55{
f72a7aab
AD
56 int argc = *argc_p;
57 char **argv = *argv_p;
58 int ok = 0;
59 struct rtattr *tail;
60 unsigned int tmp;
1d1e0fd2 61 __u16 queue_mapping, ptype;
e04dd30a 62 __u32 flags = 0, priority, mark;
46a65732 63 struct tc_skbedit sel = { 0 };
f72a7aab
AD
64
65 if (matches(*argv, "skbedit") != 0)
66 return -1;
67
68 NEXT_ARG();
69
70 while (argc > 0) {
71 if (matches(*argv, "queue_mapping") == 0) {
72 flags |= SKBEDIT_F_QUEUE_MAPPING;
73 NEXT_ARG();
74 if (get_unsigned(&tmp, *argv, 10) || tmp > 65535) {
75 fprintf(stderr, "Illegal queue_mapping\n");
76 return -1;
77 }
78 queue_mapping = tmp;
79 ok++;
80 } else if (matches(*argv, "priority") == 0) {
81 flags |= SKBEDIT_F_PRIORITY;
82 NEXT_ARG();
83 if (get_tc_classid(&priority, *argv)) {
84 fprintf(stderr, "Illegal priority\n");
85 return -1;
86 }
87 ok++;
e04dd30a
JHS
88 } else if (matches(*argv, "mark") == 0) {
89 flags |= SKBEDIT_F_MARK;
90 NEXT_ARG();
e906975a 91 if (get_u32(&mark, *argv, 0)) {
e04dd30a
JHS
92 fprintf(stderr, "Illegal mark\n");
93 return -1;
94 }
95 ok++;
1d1e0fd2
JHS
96 } else if (matches(*argv, "ptype") == 0) {
97
98 NEXT_ARG();
99 if (matches(*argv, "host") == 0) {
100 ptype = PACKET_HOST;
101 } else if (matches(*argv, "broadcast") == 0) {
102 ptype = PACKET_BROADCAST;
103 } else if (matches(*argv, "multicast") == 0) {
104 ptype = PACKET_MULTICAST;
105 } else if (matches(*argv, "otherhost") == 0) {
106 ptype = PACKET_OTHERHOST;
107 } else {
108 fprintf(stderr, "Illegal ptype (%s)\n",
109 *argv);
110 return -1;
111 }
112 flags |= SKBEDIT_F_PTYPE;
113 ok++;
f72a7aab
AD
114 } else if (matches(*argv, "help") == 0) {
115 usage();
116 } else {
117 break;
118 }
119 argc--;
120 argv++;
121 }
122
e67aba55
JP
123 parse_action_control_dflt(&argc, &argv, &sel.action,
124 false, TC_ACT_PIPE);
f72a7aab
AD
125
126 if (argc) {
127 if (matches(*argv, "index") == 0) {
128 NEXT_ARG();
129 if (get_u32(&sel.index, *argv, 10)) {
130 fprintf(stderr, "Pedit: Illegal \"index\"\n");
131 return -1;
132 }
133 argc--;
134 argv++;
135 ok++;
136 }
137 }
138
139 if (!ok) {
140 explain();
141 return -1;
142 }
143
144
c14f9d92 145 tail = addattr_nest(n, MAX_MSG, tca_id);
f72a7aab
AD
146 addattr_l(n, MAX_MSG, TCA_SKBEDIT_PARMS, &sel, sizeof(sel));
147 if (flags & SKBEDIT_F_QUEUE_MAPPING)
148 addattr_l(n, MAX_MSG, TCA_SKBEDIT_QUEUE_MAPPING,
149 &queue_mapping, sizeof(queue_mapping));
150 if (flags & SKBEDIT_F_PRIORITY)
151 addattr_l(n, MAX_MSG, TCA_SKBEDIT_PRIORITY,
152 &priority, sizeof(priority));
e04dd30a
JHS
153 if (flags & SKBEDIT_F_MARK)
154 addattr_l(n, MAX_MSG, TCA_SKBEDIT_MARK,
155 &mark, sizeof(mark));
1d1e0fd2
JHS
156 if (flags & SKBEDIT_F_PTYPE)
157 addattr_l(n, MAX_MSG, TCA_SKBEDIT_PTYPE,
158 &ptype, sizeof(ptype));
c14f9d92 159 addattr_nest_end(n, tail);
f72a7aab
AD
160
161 *argc_p = argc;
162 *argv_p = argv;
163 return 0;
164}
165
166static int print_skbedit(struct action_util *au, FILE *f, struct rtattr *arg)
167{
f72a7aab 168 struct rtattr *tb[TCA_SKBEDIT_MAX + 1];
32a121cb 169
f72a7aab 170 SPRINT_BUF(b1);
7b177017
RM
171 __u32 priority;
172 __u16 ptype;
02b1d345 173 struct tc_skbedit *p = NULL;
f72a7aab
AD
174
175 if (arg == NULL)
176 return -1;
177
178 parse_rtattr_nested(tb, TCA_SKBEDIT_MAX, arg);
179
180 if (tb[TCA_SKBEDIT_PARMS] == NULL) {
7b177017 181 print_string(PRINT_FP, NULL, "%s", "[NULL skbedit parameters]");
f72a7aab
AD
182 return -1;
183 }
02b1d345 184 p = RTA_DATA(tb[TCA_SKBEDIT_PARMS]);
f72a7aab 185
7b177017 186 print_string(PRINT_ANY, "kind", "%s ", "skbedit");
f72a7aab
AD
187
188 if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) {
7b177017
RM
189 print_uint(PRINT_ANY, "queue_mapping", "queue_mapping %u",
190 rta_getattr_u16(tb[TCA_SKBEDIT_QUEUE_MAPPING]));
f72a7aab
AD
191 }
192 if (tb[TCA_SKBEDIT_PRIORITY] != NULL) {
7b177017
RM
193 priority = rta_getattr_u32(tb[TCA_SKBEDIT_PRIORITY]);
194 print_string(PRINT_ANY, "priority", " priority %s",
195 sprint_tc_classid(priority, b1));
f72a7aab 196 }
e04dd30a 197 if (tb[TCA_SKBEDIT_MARK] != NULL) {
7b177017
RM
198 print_uint(PRINT_ANY, "mark", " mark %u",
199 rta_getattr_u32(tb[TCA_SKBEDIT_MARK]));
e04dd30a 200 }
1d1e0fd2 201 if (tb[TCA_SKBEDIT_PTYPE] != NULL) {
7b177017
RM
202 ptype = rta_getattr_u16(tb[TCA_SKBEDIT_PTYPE]);
203 if (ptype == PACKET_HOST)
204 print_string(PRINT_ANY, "ptype", " ptype %s", "host");
205 else if (ptype == PACKET_BROADCAST)
206 print_string(PRINT_ANY, "ptype", " ptype %s",
207 "broadcast");
208 else if (ptype == PACKET_MULTICAST)
209 print_string(PRINT_ANY, "ptype", " ptype %s",
210 "multicast");
211 else if (ptype == PACKET_OTHERHOST)
212 print_string(PRINT_ANY, "ptype", " ptype %s",
213 "otherhost");
1d1e0fd2 214 else
7b177017 215 print_uint(PRINT_ANY, "ptype", " ptype %u", ptype);
1d1e0fd2 216 }
f72a7aab 217
e67aba55 218 print_action_control(f, " ", p->action, "");
878babff 219
7b177017
RM
220 print_string(PRINT_FP, NULL, "%s", _SL_);
221 print_uint(PRINT_ANY, "index", "\t index %u", p->index);
222 print_int(PRINT_ANY, "ref", " ref %d", p->refcnt);
223 print_int(PRINT_ANY, "bind", " bind %d", p->bindcnt);
02b1d345 224
f72a7aab
AD
225 if (show_stats) {
226 if (tb[TCA_SKBEDIT_TM]) {
227 struct tcf_t *tm = RTA_DATA(tb[TCA_SKBEDIT_TM]);
32a121cb 228
f72a7aab
AD
229 print_tm(f, tm);
230 }
231 }
232
7b177017 233 print_string(PRINT_FP, NULL, "%s", _SL_);
02b1d345 234
f72a7aab
AD
235 return 0;
236}
237
238struct action_util skbedit_action_util = {
239 .id = "skbedit",
240 .parse_aopt = parse_skbedit,
241 .print_aopt = print_skbedit,
242};