4 * Copyright 2017 6WIND S.A.
5 * Copyright 2017 Mellanox.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of 6WIND S.A. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <linux/netlink.h>
40 #include <tap_tcmsgs.h>
53 struct qdisc_custom_arg {
60 * Initialize a netlink message with a TC header.
63 * The netlink message to fill.
65 * The netdevice ifindex where the rule will be applied.
67 * The type of TC message to create (RTM_NEWTFILTER, RTM_NEWQDISC, etc.).
69 * Overrides the default netlink flags for this msg with those specified.
72 tc_init_msg(struct nlmsg *msg, uint16_t ifindex, uint16_t type, uint16_t flags)
74 struct nlmsghdr *n = &msg->nh;
76 n->nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg));
79 n->nlmsg_flags = flags;
81 n->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
82 msg->t.tcm_family = AF_UNSPEC;
83 msg->t.tcm_ifindex = ifindex;
87 * Delete a specific QDISC identified by its iface, and it's handle and parent.
90 * The netlink socket file descriptor used for communication.
92 * The netdevice ifindex on whom the deletion will happen.
94 * Additional info to identify the QDISC (handle and parent).
97 * 0 on success, -1 otherwise with errno set.
100 qdisc_del(int nlsk_fd, uint16_t ifindex, struct qdisc *qinfo)
105 tc_init_msg(&msg, ifindex, RTM_DELQDISC, 0);
106 msg.t.tcm_handle = qinfo->handle;
107 msg.t.tcm_parent = qinfo->parent;
108 /* if no netlink socket is provided, create one */
113 "Could not delete QDISC: null netlink socket\n");
119 if (nl_send(fd, &msg.nh) < 0)
121 if (nl_recv_ack(fd) < 0)
133 * Add the multiqueue QDISC with MULTIQ_MAJOR_HANDLE handle.
136 * The netlink socket file descriptor used for communication.
138 * The netdevice ifindex where to add the multiqueue QDISC.
141 * 0 on success, -1 otherwise with errno set.
144 qdisc_add_multiq(int nlsk_fd, uint16_t ifindex)
146 struct tc_multiq_qopt opt;
149 tc_init_msg(&msg, ifindex, RTM_NEWQDISC,
150 NLM_F_REQUEST | NLM_F_ACK | NLM_F_EXCL | NLM_F_CREATE);
151 msg.t.tcm_handle = TC_H_MAKE(MULTIQ_MAJOR_HANDLE, 0);
152 msg.t.tcm_parent = TC_H_ROOT;
153 nlattr_add(&msg.nh, TCA_KIND, sizeof("multiq"), "multiq");
154 nlattr_add(&msg.nh, TCA_OPTIONS, sizeof(opt), &opt);
155 if (nl_send(nlsk_fd, &msg.nh) < 0)
157 if (nl_recv_ack(nlsk_fd) < 0)
163 * Add the ingress QDISC with default ffff: handle.
166 * The netlink socket file descriptor used for communication.
168 * The netdevice ifindex where the QDISC will be added.
171 * 0 on success, -1 otherwise with errno set.
174 qdisc_add_ingress(int nlsk_fd, uint16_t ifindex)
178 tc_init_msg(&msg, ifindex, RTM_NEWQDISC,
179 NLM_F_REQUEST | NLM_F_ACK | NLM_F_EXCL | NLM_F_CREATE);
180 msg.t.tcm_handle = TC_H_MAKE(TC_H_INGRESS, 0);
181 msg.t.tcm_parent = TC_H_INGRESS;
182 nlattr_add(&msg.nh, TCA_KIND, sizeof("ingress"), "ingress");
183 if (nl_send(nlsk_fd, &msg.nh) < 0)
185 if (nl_recv_ack(nlsk_fd) < 0)
191 * Callback function to delete a QDISC.
194 * The netlink message to parse, received from the kernel.
196 * Custom arguments for the callback.
199 * 0 on success, -1 otherwise with errno set.
202 qdisc_del_cb(struct nlmsghdr *nh, void *arg)
204 struct tcmsg *t = NLMSG_DATA(nh);
205 struct list_args *args = arg;
207 struct qdisc qinfo = {
208 .handle = t->tcm_handle,
209 .parent = t->tcm_parent,
212 /* filter out other ifaces' qdiscs */
213 if (args->ifindex != (unsigned int)t->tcm_ifindex)
216 * Use another nlsk_fd (0) to avoid tampering with the current list
219 return qdisc_del(0, args->ifindex, &qinfo);
223 * Iterate over all QDISC, and call the callback() function for each.
226 * The netlink socket file descriptor used for communication.
228 * The netdevice ifindex where to find QDISCs.
229 * @param[in] callback
230 * The function to call for each QDISC.
231 * @param[in, out] arg
232 * The arguments to provide the callback function with.
235 * 0 on success, -1 otherwise with errno set.
238 qdisc_iterate(int nlsk_fd, uint16_t ifindex,
239 int (*callback)(struct nlmsghdr *, void *), void *arg)
242 struct list_args args = {
248 tc_init_msg(&msg, ifindex, RTM_GETQDISC, NLM_F_REQUEST | NLM_F_DUMP);
249 if (nl_send(nlsk_fd, &msg.nh) < 0)
251 if (nl_recv(nlsk_fd, callback, &args) < 0)
257 * Delete all QDISCs for a given netdevice.
260 * The netlink socket file descriptor used for communication.
262 * The netdevice ifindex where to find QDISCs.
265 * 0 on success, -1 otherwise with errno set.
268 qdisc_flush(int nlsk_fd, uint16_t ifindex)
270 return qdisc_iterate(nlsk_fd, ifindex, qdisc_del_cb, NULL);
274 * Create the multiqueue QDISC, only if it does not exist already.
277 * The netlink socket file descriptor used for communication.
279 * The netdevice ifindex where to add the multiqueue QDISC.
282 * 0 if the qdisc exists or if has been successfully added.
283 * Return -1 otherwise.
286 qdisc_create_multiq(int nlsk_fd, uint16_t ifindex)
290 err = qdisc_add_multiq(nlsk_fd, ifindex);
291 if (err < 0 && errno != -EEXIST) {
292 RTE_LOG(ERR, PMD, "Could not add multiq qdisc (%d): %s\n",
293 errno, strerror(errno));
300 * Create the ingress QDISC, only if it does not exist already.
303 * The netlink socket file descriptor used for communication.
305 * The netdevice ifindex where to add the ingress QDISC.
308 * 0 if the qdisc exists or if has been successfully added.
309 * Return -1 otherwise.
312 qdisc_create_ingress(int nlsk_fd, uint16_t ifindex)
316 err = qdisc_add_ingress(nlsk_fd, ifindex);
317 if (err < 0 && errno != -EEXIST) {
318 RTE_LOG(ERR, PMD, "Could not add ingress qdisc (%d): %s\n",
319 errno, strerror(errno));