Skip to content

Commit

Permalink
netdev-linux: Refactor two tc functions
Browse files Browse the repository at this point in the history
Refactor tc_make_request and tc_add_del_ingress_qdisc to accept
ifindex instead of netdev struct.
We later want to move those outside netdev-linux module to be
used by other modules.
This patch doesn't change any functionality.

Signed-off-by: Roi Dayan <[email protected]>
Co-authored-by: Paul Blakey <[email protected]>
Signed-off-by: Paul Blakey <[email protected]>
Acked-by: Joe Stringer <[email protected]>
Acked-by: Flavio Leitner <[email protected]>
Signed-off-by: Simon Horman <[email protected]>
  • Loading branch information
2 people authored and shorman-netronome committed Jun 14, 2017
1 parent 81d3921 commit 7874bdf
Showing 1 changed file with 55 additions and 36 deletions.
91 changes: 55 additions & 36 deletions lib/netdev-linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -442,10 +442,14 @@ 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(const struct netdev *, int type,
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(struct netdev *netdev, bool add);
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);

Expand Down Expand Up @@ -2089,6 +2093,7 @@ netdev_linux_set_policing(struct netdev *netdev_,
{
struct netdev_linux *netdev = netdev_linux_cast(netdev_);
const char *netdev_name = netdev_get_name(netdev_);
int ifindex;
int error;

kbits_burst = (!kbits_rate ? 0 /* Force to 0 if no rate specified. */
Expand All @@ -2106,17 +2111,22 @@ netdev_linux_set_policing(struct netdev *netdev_,
netdev->cache_valid &= ~VALID_POLICING;
}

error = get_ifindex(netdev_, &ifindex);
if (error) {
goto out;
}

COVERAGE_INC(netdev_set_policing);
/* Remove any existing ingress qdisc. */
error = tc_add_del_ingress_qdisc(netdev_, false);
error = tc_add_del_ingress_qdisc(ifindex, false);
if (error) {
VLOG_WARN_RL(&rl, "%s: removing policing failed: %s",
netdev_name, ovs_strerror(error));
goto out;
}

if (kbits_rate) {
error = tc_add_del_ingress_qdisc(netdev_, true);
error = tc_add_del_ingress_qdisc(ifindex, true);
if (error) {
VLOG_WARN_RL(&rl, "%s: adding policing qdisc failed: %s",
netdev_name, ovs_strerror(error));
Expand Down Expand Up @@ -2385,7 +2395,7 @@ start_queue_dump(const struct netdev *netdev, struct queue_dump_state *state)
struct ofpbuf request;
struct tcmsg *tcmsg;

tcmsg = tc_make_request(netdev, RTM_GETTCLASS, 0, &request);
tcmsg = netdev_linux_tc_make_request(netdev, RTM_GETTCLASS, 0, &request);
if (!tcmsg) {
return false;
}
Expand Down Expand Up @@ -2944,8 +2954,8 @@ codel_setup_qdisc__(struct netdev *netdev, uint32_t target, uint32_t limit,

tc_del_qdisc(netdev);

tcmsg = tc_make_request(netdev, RTM_NEWQDISC,
NLM_F_EXCL | NLM_F_CREATE, &request);
tcmsg = netdev_linux_tc_make_request(netdev, RTM_NEWQDISC,
NLM_F_EXCL | NLM_F_CREATE, &request);
if (!tcmsg) {
return ENODEV;
}
Expand Down Expand Up @@ -3162,8 +3172,8 @@ fqcodel_setup_qdisc__(struct netdev *netdev, uint32_t target, uint32_t limit,

tc_del_qdisc(netdev);

tcmsg = tc_make_request(netdev, RTM_NEWQDISC,
NLM_F_EXCL | NLM_F_CREATE, &request);
tcmsg = netdev_linux_tc_make_request(netdev, RTM_NEWQDISC,
NLM_F_EXCL | NLM_F_CREATE, &request);
if (!tcmsg) {
return ENODEV;
}
Expand Down Expand Up @@ -3386,8 +3396,8 @@ sfq_setup_qdisc__(struct netdev *netdev, uint32_t quantum, uint32_t perturb)

tc_del_qdisc(netdev);

tcmsg = tc_make_request(netdev, RTM_NEWQDISC,
NLM_F_EXCL | NLM_F_CREATE, &request);
tcmsg = netdev_linux_tc_make_request(netdev, RTM_NEWQDISC,
NLM_F_EXCL | NLM_F_CREATE, &request);
if (!tcmsg) {
return ENODEV;
}
Expand Down Expand Up @@ -3573,8 +3583,8 @@ htb_setup_qdisc__(struct netdev *netdev)

tc_del_qdisc(netdev);

tcmsg = tc_make_request(netdev, RTM_NEWQDISC,
NLM_F_EXCL | NLM_F_CREATE, &request);
tcmsg = netdev_linux_tc_make_request(netdev, RTM_NEWQDISC,
NLM_F_EXCL | NLM_F_CREATE, &request);
if (!tcmsg) {
return ENODEV;
}
Expand Down Expand Up @@ -3627,7 +3637,8 @@ htb_setup_class__(struct netdev *netdev, unsigned int handle,
opt.cbuffer = tc_calc_buffer(opt.ceil.rate, mtu, class->burst);
opt.prio = class->priority;

tcmsg = tc_make_request(netdev, RTM_NEWTCLASS, NLM_F_CREATE, &request);
tcmsg = netdev_linux_tc_make_request(netdev, RTM_NEWTCLASS, NLM_F_CREATE,
&request);
if (!tcmsg) {
return ENODEV;
}
Expand Down Expand Up @@ -4236,8 +4247,8 @@ hfsc_setup_qdisc__(struct netdev * netdev)

tc_del_qdisc(netdev);

tcmsg = tc_make_request(netdev, RTM_NEWQDISC,
NLM_F_EXCL | NLM_F_CREATE, &request);
tcmsg = netdev_linux_tc_make_request(netdev, RTM_NEWQDISC,
NLM_F_EXCL | NLM_F_CREATE, &request);

if (!tcmsg) {
return ENODEV;
Expand Down Expand Up @@ -4269,7 +4280,8 @@ hfsc_setup_class__(struct netdev *netdev, unsigned int handle,
struct ofpbuf request;
struct tc_service_curve min, max;

tcmsg = tc_make_request(netdev, RTM_NEWTCLASS, NLM_F_CREATE, &request);
tcmsg = netdev_linux_tc_make_request(netdev, RTM_NEWTCLASS, NLM_F_CREATE,
&request);

if (!tcmsg) {
return ENODEV;
Expand Down Expand Up @@ -4667,17 +4679,10 @@ tc_get_minor(unsigned int handle)
}

static struct tcmsg *
tc_make_request(const struct netdev *netdev, int type, unsigned int flags,
tc_make_request(int ifindex, int type, unsigned int flags,
struct ofpbuf *request)
{
struct tcmsg *tcmsg;
int ifindex;
int error;

error = get_ifindex(netdev, &ifindex);
if (error) {
return NULL;
}

ofpbuf_init(request, 512);
nl_msg_put_nlmsghdr(request, sizeof *tcmsg, type, NLM_F_REQUEST | flags);
Expand All @@ -4690,6 +4695,21 @@ tc_make_request(const struct netdev *netdev, int type, unsigned int flags,
return tcmsg;
}

static struct tcmsg *
netdev_linux_tc_make_request(const struct netdev *netdev, int type,
unsigned int flags, struct ofpbuf *request)
{
int ifindex;
int error;

error = get_ifindex(netdev, &ifindex);
if (error) {
return NULL;
}

return tc_make_request(ifindex, type, flags, request);
}

static int
tc_transact(struct ofpbuf *request, struct ofpbuf **replyp)
{
Expand All @@ -4713,18 +4733,15 @@ tc_transact(struct ofpbuf *request, struct ofpbuf **replyp)
* Returns 0 if successful, otherwise a positive errno value.
*/
static int
tc_add_del_ingress_qdisc(struct netdev *netdev, bool add)
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(netdev, type, flags, &request);
if (!tcmsg) {
return ENODEV;
}
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");
Expand Down Expand Up @@ -4783,8 +4800,8 @@ tc_add_policer(struct netdev *netdev,
tc_police.burst = tc_bytes_to_ticks(
tc_police.rate.rate, MIN(UINT32_MAX / 1024, kbits_burst) * 1024 / 8);

tcmsg = tc_make_request(netdev, RTM_NEWTFILTER,
NLM_F_EXCL | NLM_F_CREATE, &request);
tcmsg = netdev_linux_tc_make_request(netdev, RTM_NEWTFILTER,
NLM_F_EXCL | NLM_F_CREATE, &request);
if (!tcmsg) {
return ENODEV;
}
Expand Down Expand Up @@ -5049,7 +5066,8 @@ tc_query_class(const struct netdev *netdev,
struct tcmsg *tcmsg;
int error;

tcmsg = tc_make_request(netdev, RTM_GETTCLASS, NLM_F_ECHO, &request);
tcmsg = netdev_linux_tc_make_request(netdev, RTM_GETTCLASS, NLM_F_ECHO,
&request);
if (!tcmsg) {
return ENODEV;
}
Expand All @@ -5075,7 +5093,7 @@ tc_delete_class(const struct netdev *netdev, unsigned int handle)
struct tcmsg *tcmsg;
int error;

tcmsg = tc_make_request(netdev, RTM_DELTCLASS, 0, &request);
tcmsg = netdev_linux_tc_make_request(netdev, RTM_DELTCLASS, 0, &request);
if (!tcmsg) {
return ENODEV;
}
Expand All @@ -5101,7 +5119,7 @@ tc_del_qdisc(struct netdev *netdev_)
struct tcmsg *tcmsg;
int error;

tcmsg = tc_make_request(netdev_, RTM_DELQDISC, 0, &request);
tcmsg = netdev_linux_tc_make_request(netdev_, RTM_DELQDISC, 0, &request);
if (!tcmsg) {
return ENODEV;
}
Expand Down Expand Up @@ -5182,7 +5200,8 @@ tc_query_qdisc(const struct netdev *netdev_)
* in such a case we get no response at all from the kernel (!) if a
* builtin qdisc is in use (which is later caught by "!error &&
* !qdisc->size"). */
tcmsg = tc_make_request(netdev_, RTM_GETQDISC, NLM_F_ECHO, &request);
tcmsg = netdev_linux_tc_make_request(netdev_, RTM_GETQDISC, NLM_F_ECHO,
&request);
if (!tcmsg) {
return ENODEV;
}
Expand Down

0 comments on commit 7874bdf

Please sign in to comment.