]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/m_skbedit.c
devlink: Add support for resource/dpipe relation
[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 125
3572e01a 126 NEXT_ARG_FWD();
f72a7aab
AD
127 if (argc) {
128 if (matches(*argv, "index") == 0) {
129 NEXT_ARG();
130 if (get_u32(&sel.index, *argv, 10)) {
131 fprintf(stderr, "Pedit: Illegal \"index\"\n");
132 return -1;
133 }
134 argc--;
135 argv++;
136 ok++;
137 }
138 }
139
140 if (!ok) {
141 explain();
142 return -1;
143 }
144
145
146 tail = NLMSG_TAIL(n);
147 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
148 addattr_l(n, MAX_MSG, TCA_SKBEDIT_PARMS, &sel, sizeof(sel));
149 if (flags & SKBEDIT_F_QUEUE_MAPPING)
150 addattr_l(n, MAX_MSG, TCA_SKBEDIT_QUEUE_MAPPING,
151 &queue_mapping, sizeof(queue_mapping));
152 if (flags & SKBEDIT_F_PRIORITY)
153 addattr_l(n, MAX_MSG, TCA_SKBEDIT_PRIORITY,
154 &priority, sizeof(priority));
e04dd30a
JHS
155 if (flags & SKBEDIT_F_MARK)
156 addattr_l(n, MAX_MSG, TCA_SKBEDIT_MARK,
157 &mark, sizeof(mark));
1d1e0fd2
JHS
158 if (flags & SKBEDIT_F_PTYPE)
159 addattr_l(n, MAX_MSG, TCA_SKBEDIT_PTYPE,
160 &ptype, sizeof(ptype));
f72a7aab
AD
161 tail->rta_len = (char *)NLMSG_TAIL(n) - (char *)tail;
162
163 *argc_p = argc;
164 *argv_p = argv;
165 return 0;
166}
167
168static int print_skbedit(struct action_util *au, FILE *f, struct rtattr *arg)
169{
f72a7aab 170 struct rtattr *tb[TCA_SKBEDIT_MAX + 1];
32a121cb 171
f72a7aab
AD
172 SPRINT_BUF(b1);
173 __u32 *priority;
e04dd30a 174 __u32 *mark;
1d1e0fd2 175 __u16 *queue_mapping, *ptype;
02b1d345 176 struct tc_skbedit *p = NULL;
f72a7aab
AD
177
178 if (arg == NULL)
179 return -1;
180
181 parse_rtattr_nested(tb, TCA_SKBEDIT_MAX, arg);
182
183 if (tb[TCA_SKBEDIT_PARMS] == NULL) {
184 fprintf(f, "[NULL skbedit parameters]");
185 return -1;
186 }
02b1d345 187 p = RTA_DATA(tb[TCA_SKBEDIT_PARMS]);
f72a7aab 188
f72a7aab
AD
189 fprintf(f, " skbedit");
190
191 if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) {
192 queue_mapping = RTA_DATA(tb[TCA_SKBEDIT_QUEUE_MAPPING]);
193 fprintf(f, " queue_mapping %u", *queue_mapping);
194 }
195 if (tb[TCA_SKBEDIT_PRIORITY] != NULL) {
196 priority = RTA_DATA(tb[TCA_SKBEDIT_PRIORITY]);
197 fprintf(f, " priority %s", sprint_tc_classid(*priority, b1));
198 }
e04dd30a
JHS
199 if (tb[TCA_SKBEDIT_MARK] != NULL) {
200 mark = RTA_DATA(tb[TCA_SKBEDIT_MARK]);
201 fprintf(f, " mark %d", *mark);
202 }
1d1e0fd2
JHS
203 if (tb[TCA_SKBEDIT_PTYPE] != NULL) {
204 ptype = RTA_DATA(tb[TCA_SKBEDIT_PTYPE]);
205 if (*ptype == PACKET_HOST)
206 fprintf(f, " ptype host");
207 else if (*ptype == PACKET_BROADCAST)
208 fprintf(f, " ptype broadcast");
209 else if (*ptype == PACKET_MULTICAST)
210 fprintf(f, " ptype multicast");
211 else if (*ptype == PACKET_OTHERHOST)
212 fprintf(f, " ptype otherhost");
213 else
214 fprintf(f, " ptype %d", *ptype);
215 }
f72a7aab 216
e67aba55 217 print_action_control(f, " ", p->action, "");
878babff 218
53075318 219 fprintf(f, "\n\t index %u ref %d bind %d",
1d1e0fd2 220 p->index, p->refcnt, p->bindcnt);
02b1d345 221
f72a7aab
AD
222 if (show_stats) {
223 if (tb[TCA_SKBEDIT_TM]) {
224 struct tcf_t *tm = RTA_DATA(tb[TCA_SKBEDIT_TM]);
32a121cb 225
f72a7aab
AD
226 print_tm(f, tm);
227 }
228 }
229
02b1d345
JHS
230 fprintf(f, "\n ");
231
f72a7aab
AD
232 return 0;
233}
234
235struct action_util skbedit_action_util = {
236 .id = "skbedit",
237 .parse_aopt = parse_skbedit,
238 .print_aopt = print_skbedit,
239};