1
2
3
4
5
6#include <inttypes.h>
7#include <linux/netlink.h>
8#include <net/if.h>
9#include <string.h>
10
11#include <rte_log.h>
12#include <tap_tcmsgs.h>
13#include "tap_log.h"
14
15struct qdisc {
16 uint32_t handle;
17 uint32_t parent;
18};
19
20struct list_args {
21 int nlsk_fd;
22 uint16_t ifindex;
23 void *custom_arg;
24};
25
26struct qdisc_custom_arg {
27 uint32_t handle;
28 uint32_t parent;
29 uint8_t exists;
30};
31
32
33
34
35
36
37
38
39
40
41
42
43
44void
45tc_init_msg(struct nlmsg *msg, uint16_t ifindex, uint16_t type, uint16_t flags)
46{
47 struct nlmsghdr *n = &msg->nh;
48
49 n->nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg));
50 n->nlmsg_type = type;
51 if (flags)
52 n->nlmsg_flags = flags;
53 else
54 n->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
55 msg->t.tcm_family = AF_UNSPEC;
56 msg->t.tcm_ifindex = ifindex;
57}
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72static int
73qdisc_del(int nlsk_fd, uint16_t ifindex, struct qdisc *qinfo)
74{
75 struct nlmsg msg;
76 int fd = 0;
77
78 tc_init_msg(&msg, ifindex, RTM_DELQDISC, 0);
79 msg.t.tcm_handle = qinfo->handle;
80 msg.t.tcm_parent = qinfo->parent;
81
82 if (!nlsk_fd) {
83 fd = tap_nl_init(0);
84 if (fd < 0) {
85 TAP_LOG(ERR,
86 "Could not delete QDISC: null netlink socket");
87 return -1;
88 }
89 } else {
90 fd = nlsk_fd;
91 }
92 if (tap_nl_send(fd, &msg.nh) < 0)
93 goto error;
94 if (tap_nl_recv_ack(fd) < 0)
95 goto error;
96 if (!nlsk_fd)
97 return tap_nl_final(fd);
98 return 0;
99error:
100 if (!nlsk_fd)
101 tap_nl_final(fd);
102 return -1;
103}
104
105
106
107
108
109
110
111
112
113
114
115
116int
117qdisc_add_multiq(int nlsk_fd, uint16_t ifindex)
118{
119 struct tc_multiq_qopt opt = {0};
120 struct nlmsg msg;
121
122 tc_init_msg(&msg, ifindex, RTM_NEWQDISC,
123 NLM_F_REQUEST | NLM_F_ACK | NLM_F_EXCL | NLM_F_CREATE);
124 msg.t.tcm_handle = TC_H_MAKE(MULTIQ_MAJOR_HANDLE, 0);
125 msg.t.tcm_parent = TC_H_ROOT;
126 tap_nlattr_add(&msg.nh, TCA_KIND, sizeof("multiq"), "multiq");
127 tap_nlattr_add(&msg.nh, TCA_OPTIONS, sizeof(opt), &opt);
128 if (tap_nl_send(nlsk_fd, &msg.nh) < 0)
129 return -1;
130 if (tap_nl_recv_ack(nlsk_fd) < 0)
131 return -1;
132 return 0;
133}
134
135
136
137
138
139
140
141
142
143
144
145
146int
147qdisc_add_ingress(int nlsk_fd, uint16_t ifindex)
148{
149 struct nlmsg msg;
150
151 tc_init_msg(&msg, ifindex, RTM_NEWQDISC,
152 NLM_F_REQUEST | NLM_F_ACK | NLM_F_EXCL | NLM_F_CREATE);
153 msg.t.tcm_handle = TC_H_MAKE(TC_H_INGRESS, 0);
154 msg.t.tcm_parent = TC_H_INGRESS;
155 tap_nlattr_add(&msg.nh, TCA_KIND, sizeof("ingress"), "ingress");
156 if (tap_nl_send(nlsk_fd, &msg.nh) < 0)
157 return -1;
158 if (tap_nl_recv_ack(nlsk_fd) < 0)
159 return -1;
160 return 0;
161}
162
163
164
165
166
167
168
169
170
171
172
173
174static int
175qdisc_del_cb(struct nlmsghdr *nh, void *arg)
176{
177 struct tcmsg *t = NLMSG_DATA(nh);
178 struct list_args *args = arg;
179
180 struct qdisc qinfo = {
181 .handle = t->tcm_handle,
182 .parent = t->tcm_parent,
183 };
184
185
186 if (args->ifindex != (unsigned int)t->tcm_ifindex)
187 return 0;
188
189
190
191
192 return qdisc_del(0, args->ifindex, &qinfo);
193}
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210static int
211qdisc_iterate(int nlsk_fd, uint16_t ifindex,
212 int (*callback)(struct nlmsghdr *, void *), void *arg)
213{
214 struct nlmsg msg;
215 struct list_args args = {
216 .nlsk_fd = nlsk_fd,
217 .ifindex = ifindex,
218 .custom_arg = arg,
219 };
220
221 tc_init_msg(&msg, ifindex, RTM_GETQDISC, NLM_F_REQUEST | NLM_F_DUMP);
222 if (tap_nl_send(nlsk_fd, &msg.nh) < 0)
223 return -1;
224 if (tap_nl_recv(nlsk_fd, callback, &args) < 0)
225 return -1;
226 return 0;
227}
228
229
230
231
232
233
234
235
236
237
238
239
240int
241qdisc_flush(int nlsk_fd, uint16_t ifindex)
242{
243 return qdisc_iterate(nlsk_fd, ifindex, qdisc_del_cb, NULL);
244}
245
246
247
248
249
250
251
252
253
254
255
256
257
258int
259qdisc_create_multiq(int nlsk_fd, uint16_t ifindex)
260{
261 int err = 0;
262
263 err = qdisc_add_multiq(nlsk_fd, ifindex);
264 if (err < 0 && errno != -EEXIST) {
265 TAP_LOG(ERR, "Could not add multiq qdisc (%d): %s",
266 errno, strerror(errno));
267 return -1;
268 }
269 return 0;
270}
271
272
273
274
275
276
277
278
279
280
281
282
283
284int
285qdisc_create_ingress(int nlsk_fd, uint16_t ifindex)
286{
287 int err = 0;
288
289 err = qdisc_add_ingress(nlsk_fd, ifindex);
290 if (err < 0 && errno != -EEXIST) {
291 TAP_LOG(ERR, "Could not add ingress qdisc (%d): %s",
292 errno, strerror(errno));
293 return -1;
294 }
295 return 0;
296}
297