From patchwork Tue Jun 13 15:03:24 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roi Dayan X-Patchwork-Id: 775281 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3wnCjD3kBZz9s81 for ; Wed, 14 Jun 2017 01:04:32 +1000 (AEST) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id 6E097BA9; Tue, 13 Jun 2017 15:04:28 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id 206B0B88 for ; Tue, 13 Jun 2017 15:04:27 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mellanox.co.il (mail-il-dmz.mellanox.com [193.47.165.129]) by smtp1.linuxfoundation.org (Postfix) with ESMTP id D52341D9 for ; Tue, 13 Jun 2017 15:04:22 +0000 (UTC) Received: from Internal Mail-Server by MTLPINE1 (envelope-from roid@mellanox.com) with ESMTPS (AES256-SHA encrypted); 13 Jun 2017 18:04:20 +0300 Received: from dev-r-vrt-189.mtr.labs.mlnx (dev-r-vrt-189.mtr.labs.mlnx [10.212.189.1]) by labmailer.mlnx (8.13.8/8.13.8) with ESMTP id v5DF4KuW027434; Tue, 13 Jun 2017 18:04:20 +0300 From: Roi Dayan To: dev@openvswitch.org Date: Tue, 13 Jun 2017 18:03:24 +0300 Message-Id: <1497366235-20892-3-git-send-email-roid@mellanox.com> X-Mailer: git-send-email 1.8.4.3 In-Reply-To: <1497366235-20892-1-git-send-email-roid@mellanox.com> References: <1497366235-20892-1-git-send-email-roid@mellanox.com> X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Cc: Shahar Klein , Hadar Hen Zion , Rony Efraim , Flavio Leitner , Jiri Pirko , Marcelo Ricardo Leitner , Simon Horman , Or Gerlitz , Andy Gospodarek Subject: [ovs-dev] [PATCH V11 02/33] tc: Introduce tc module X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: ovs-dev-bounces@openvswitch.org Errors-To: ovs-dev-bounces@openvswitch.org From: Paul Blakey Add tc module to expose tc operations to be used by other modules. Move some tc related functions from netdev-linux.c to tc.c This patch doesn't change any functionality. Signed-off-by: Paul Blakey Co-authored-by: Roi Dayan Signed-off-by: Roi Dayan Acked-by: Joe Stringer Acked-by: Flavio Leitner --- lib/automake.mk | 4 +- lib/netdev-linux.c | 100 +--------------------------------------------- lib/tc.c | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/tc.h | 33 ++++++++++++++++ 4 files changed, 151 insertions(+), 100 deletions(-) create mode 100644 lib/tc.c create mode 100644 lib/tc.h diff --git a/lib/automake.mk b/lib/automake.mk index f5baba2..0ac4708 100644 --- a/lib/automake.mk +++ b/lib/automake.mk @@ -368,7 +368,9 @@ lib_libopenvswitch_la_SOURCES += \ lib/rtnetlink.c \ lib/rtnetlink.h \ lib/route-table.c \ - lib/route-table.h + lib/route-table.h \ + lib/tc.c \ + lib/tc.h endif if DPDK_NETDEV diff --git a/lib/netdev-linux.c b/lib/netdev-linux.c index d794453..c8145c6 100644 --- a/lib/netdev-linux.c +++ b/lib/netdev-linux.c @@ -29,9 +29,6 @@ #include #include #include -#include -#include -#include #include #include #include @@ -70,6 +67,7 @@ #include "openvswitch/shash.h" #include "socket-util.h" #include "sset.h" +#include "tc.h" #include "timer.h" #include "unaligned.h" #include "openvswitch/vlog.h" @@ -434,22 +432,14 @@ static const struct tc_ops *const tcs[] = { NULL }; -static unsigned int tc_make_handle(unsigned int major, unsigned int minor); -static unsigned int tc_get_major(unsigned int handle); -static unsigned int tc_get_minor(unsigned int handle); - static unsigned int tc_ticks_to_bytes(unsigned int rate, unsigned int ticks); static unsigned int tc_bytes_to_ticks(unsigned int rate, unsigned int size); static unsigned int tc_buffer_per_jiffy(unsigned int rate); -static struct tcmsg *tc_make_request(int ifindex, int type, - unsigned int flags, struct ofpbuf *); static struct tcmsg *netdev_linux_tc_make_request(const struct netdev *, int type, unsigned int flags, struct ofpbuf *); -static int tc_transact(struct ofpbuf *request, struct ofpbuf **replyp); -static int tc_add_del_ingress_qdisc(int ifindex, bool add); static int tc_add_policer(struct netdev *, uint32_t kbits_rate, uint32_t kbits_burst); @@ -4657,44 +4647,6 @@ static double ticks_per_s; */ static unsigned int buffer_hz; -/* Returns tc handle 'major':'minor'. */ -static unsigned int -tc_make_handle(unsigned int major, unsigned int minor) -{ - return TC_H_MAKE(major << 16, minor); -} - -/* Returns the major number from 'handle'. */ -static unsigned int -tc_get_major(unsigned int handle) -{ - return TC_H_MAJ(handle) >> 16; -} - -/* Returns the minor number from 'handle'. */ -static unsigned int -tc_get_minor(unsigned int handle) -{ - return TC_H_MIN(handle); -} - -static struct tcmsg * -tc_make_request(int ifindex, int type, unsigned int flags, - struct ofpbuf *request) -{ - struct tcmsg *tcmsg; - - ofpbuf_init(request, 512); - nl_msg_put_nlmsghdr(request, sizeof *tcmsg, type, NLM_F_REQUEST | flags); - tcmsg = ofpbuf_put_zeros(request, sizeof *tcmsg); - tcmsg->tcm_family = AF_UNSPEC; - tcmsg->tcm_ifindex = ifindex; - /* Caller should fill in tcmsg->tcm_handle. */ - /* Caller should fill in tcmsg->tcm_parent. */ - - return tcmsg; -} - static struct tcmsg * netdev_linux_tc_make_request(const struct netdev *netdev, int type, unsigned int flags, struct ofpbuf *request) @@ -4710,56 +4662,6 @@ netdev_linux_tc_make_request(const struct netdev *netdev, int type, return tc_make_request(ifindex, type, flags, request); } -static int -tc_transact(struct ofpbuf *request, struct ofpbuf **replyp) -{ - int error = nl_transact(NETLINK_ROUTE, request, replyp); - ofpbuf_uninit(request); - return error; -} - -/* Adds or deletes a root ingress qdisc on 'netdev'. We use this for - * policing configuration. - * - * This function is equivalent to running the following when 'add' is true: - * /sbin/tc qdisc add dev handle ffff: ingress - * - * This function is equivalent to running the following when 'add' is false: - * /sbin/tc qdisc del dev handle ffff: ingress - * - * The configuration and stats may be seen with the following command: - * /sbin/tc -s qdisc show dev - * - * Returns 0 if successful, otherwise a positive errno value. - */ -static int -tc_add_del_ingress_qdisc(int ifindex, bool add) -{ - struct ofpbuf request; - struct tcmsg *tcmsg; - int error; - int type = add ? RTM_NEWQDISC : RTM_DELQDISC; - int flags = add ? NLM_F_EXCL | NLM_F_CREATE : 0; - - tcmsg = tc_make_request(ifindex, type, flags, &request); - tcmsg->tcm_handle = tc_make_handle(0xffff, 0); - tcmsg->tcm_parent = TC_H_INGRESS; - nl_msg_put_string(&request, TCA_KIND, "ingress"); - nl_msg_put_unspec(&request, TCA_OPTIONS, NULL, 0); - - error = tc_transact(&request, NULL); - if (error) { - /* If we're deleting the qdisc, don't worry about some of the - * error conditions. */ - if (!add && (error == ENOENT || error == EINVAL)) { - return 0; - } - return error; - } - - return 0; -} - /* Adds a policer to 'netdev' with a rate of 'kbits_rate' and a burst size * of 'kbits_burst'. * diff --git a/lib/tc.c b/lib/tc.c new file mode 100644 index 0000000..d3263a2 --- /dev/null +++ b/lib/tc.c @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 Nicira, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "tc.h" +#include +#include "netlink-socket.h" +#include "netlink.h" +#include "openvswitch/ofpbuf.h" +#include "openvswitch/vlog.h" + +VLOG_DEFINE_THIS_MODULE(tc); + +/* Returns tc handle 'major':'minor'. */ +unsigned int +tc_make_handle(unsigned int major, unsigned int minor) +{ + return TC_H_MAKE(major << 16, minor); +} + +/* Returns the major number from 'handle'. */ +unsigned int +tc_get_major(unsigned int handle) +{ + return TC_H_MAJ(handle) >> 16; +} + +/* Returns the minor number from 'handle'. */ +unsigned int +tc_get_minor(unsigned int handle) +{ + return TC_H_MIN(handle); +} + +struct tcmsg * +tc_make_request(int ifindex, int type, unsigned int flags, + struct ofpbuf *request) +{ + struct tcmsg *tcmsg; + + ofpbuf_init(request, 512); + nl_msg_put_nlmsghdr(request, sizeof *tcmsg, type, NLM_F_REQUEST | flags); + tcmsg = ofpbuf_put_zeros(request, sizeof *tcmsg); + tcmsg->tcm_family = AF_UNSPEC; + tcmsg->tcm_ifindex = ifindex; + /* Caller should fill in tcmsg->tcm_handle. */ + /* Caller should fill in tcmsg->tcm_parent. */ + + return tcmsg; +} + +int +tc_transact(struct ofpbuf *request, struct ofpbuf **replyp) +{ + int error = nl_transact(NETLINK_ROUTE, request, replyp); + ofpbuf_uninit(request); + return error; +} + +/* Adds or deletes a root ingress qdisc on device with specified ifindex. + * + * This function is equivalent to running the following when 'add' is true: + * /sbin/tc qdisc add dev handle ffff: ingress + * + * This function is equivalent to running the following when 'add' is false: + * /sbin/tc qdisc del dev handle ffff: ingress + * + * Where dev is the device with specified ifindex name. + * + * The configuration and stats may be seen with the following command: + * /sbin/tc -s qdisc show dev + * + * Returns 0 if successful, otherwise a positive errno value. + */ +int +tc_add_del_ingress_qdisc(int ifindex, bool add) +{ + struct ofpbuf request; + struct tcmsg *tcmsg; + int error; + int type = add ? RTM_NEWQDISC : RTM_DELQDISC; + int flags = add ? NLM_F_EXCL | NLM_F_CREATE : 0; + + tcmsg = tc_make_request(ifindex, type, flags, &request); + tcmsg->tcm_handle = tc_make_handle(0xffff, 0); + tcmsg->tcm_parent = TC_H_INGRESS; + nl_msg_put_string(&request, TCA_KIND, "ingress"); + nl_msg_put_unspec(&request, TCA_OPTIONS, NULL, 0); + + error = tc_transact(&request, NULL); + if (error) { + /* If we're deleting the qdisc, don't worry about some of the + * error conditions. */ + if (!add && (error == ENOENT || error == EINVAL)) { + return 0; + } + return error; + } + + return 0; +} diff --git a/lib/tc.h b/lib/tc.h new file mode 100644 index 0000000..420cdf8 --- /dev/null +++ b/lib/tc.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 Nicira, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TC_H +#define TC_H 1 + +#include +#include +#include +#include "openvswitch/ofpbuf.h" + +unsigned int tc_make_handle(unsigned int major, unsigned int minor); +unsigned int tc_get_major(unsigned int handle); +unsigned int tc_get_minor(unsigned int handle); +struct tcmsg *tc_make_request(int ifindex, int type, + unsigned int flags, struct ofpbuf *); +int tc_transact(struct ofpbuf *request, struct ofpbuf **replyp); +int tc_add_del_ingress_qdisc(int ifindex, bool add); + +#endif /* tc.h */