summaryrefslogtreecommitdiff
path: root/lib/route/qdisc/fifo.c
blob: dc6d18959e9be32eb190a9562b277a0c2adf0c5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* SPDX-License-Identifier: LGPL-2.1-only */
/*
 * Copyright (c) 2003-2011 Thomas Graf <tgraf@suug.ch>
 */

/**
 * @ingroup qdisc
 * @defgroup qdisc_fifo Packet/Bytes FIFO (pfifo/bfifo)
 * @brief
 *
 * The FIFO qdisc comes in two flavours:
 * @par bfifo (Byte FIFO)
 * Allows enqueuing until the currently queued volume in bytes exceeds
 * the configured limit.backlog contains currently enqueued volume in bytes.
 *
 * @par pfifo (Packet FIFO)
 * Allows enquueing until the currently queued number of packets
 * exceeds the configured limit.
 *
 * The configuration is exactly the same, the decision which of
 * the two variations is going to be used is made based on the
 * kind of the qdisc (rtnl_tc_set_kind()).
 * @{
 */

#include <netlink-private/netlink.h>
#include <netlink-private/tc.h>
#include <netlink/netlink.h>
#include <netlink-private/route/tc-api.h>
#include <netlink/route/qdisc.h>
#include <netlink/route/qdisc/fifo.h>
#include <netlink/utils.h>

/** @cond SKIP */
#define SCH_FIFO_ATTR_LIMIT 1
/** @endcond */

static int fifo_msg_parser(struct rtnl_tc *tc, void *data)
{
	struct rtnl_fifo *fifo = data;
	struct tc_fifo_qopt *opt;

	if (tc->tc_opts->d_size < sizeof(struct tc_fifo_qopt))
		return -NLE_INVAL;

	opt = (struct tc_fifo_qopt *) tc->tc_opts->d_data;
	fifo->qf_limit = opt->limit;
	fifo->qf_mask = SCH_FIFO_ATTR_LIMIT;

	return 0;
}

static void pfifo_dump_line(struct rtnl_tc *tc, void *data,
			    struct nl_dump_params *p)
{
	struct rtnl_fifo *fifo = data;

	if (fifo)
		nl_dump(p, " limit %u packets", fifo->qf_limit);
}

static void bfifo_dump_line(struct rtnl_tc *tc, void *data,
			    struct nl_dump_params *p)
{
	struct rtnl_fifo *fifo = data;
	char *unit;
	double r;

	if (!fifo)
		return;

	r = nl_cancel_down_bytes(fifo->qf_limit, &unit);
	nl_dump(p, " limit %.1f%s", r, unit);
}

static int fifo_msg_fill(struct rtnl_tc *tc, void *data, struct nl_msg *msg)
{
	struct rtnl_fifo *fifo = data;
	struct tc_fifo_qopt opts = {0};

	if (!fifo || !(fifo->qf_mask & SCH_FIFO_ATTR_LIMIT))
		return -NLE_INVAL;

	opts.limit = fifo->qf_limit;

	return nlmsg_append(msg, &opts, sizeof(opts), NL_DONTPAD);
}

/**
 * @name Attribute Modification
 * @{
 */

/**
 * Set limit of FIFO qdisc.
 * @arg qdisc		FIFO qdisc to be modified.
 * @arg limit		New limit.
 * @return 0 on success or a negative error code.
 */
int rtnl_qdisc_fifo_set_limit(struct rtnl_qdisc *qdisc, int limit)
{
	struct rtnl_fifo *fifo;
	
	if (!(fifo = rtnl_tc_data(TC_CAST(qdisc))))
		return -NLE_NOMEM;
		
	fifo->qf_limit = limit;
	fifo->qf_mask |= SCH_FIFO_ATTR_LIMIT;

	return 0;
}

/**
 * Get limit of a FIFO qdisc.
 * @arg qdisc		FIFO qdisc.
 * @return Numeric limit or a negative error code.
 */
int rtnl_qdisc_fifo_get_limit(struct rtnl_qdisc *qdisc)
{
	struct rtnl_fifo *fifo;
	
	if (!(fifo = rtnl_tc_data(TC_CAST(qdisc))))
		return -NLE_NOMEM;
	
	if (fifo->qf_mask & SCH_FIFO_ATTR_LIMIT)
		return fifo->qf_limit;
	else
		return -NLE_NOATTR;
}

/** @} */

static struct rtnl_tc_ops pfifo_ops = {
	.to_kind		= "pfifo",
	.to_type		= RTNL_TC_TYPE_QDISC,
	.to_size		= sizeof(struct rtnl_fifo),
	.to_msg_parser		= fifo_msg_parser,
	.to_dump[NL_DUMP_LINE]	= pfifo_dump_line,
	.to_msg_fill		= fifo_msg_fill,
};

static struct rtnl_tc_ops bfifo_ops = {
	.to_kind		= "bfifo",
	.to_type		= RTNL_TC_TYPE_QDISC,
	.to_size		= sizeof(struct rtnl_fifo),
	.to_msg_parser		= fifo_msg_parser,
	.to_dump[NL_DUMP_LINE]	= bfifo_dump_line,
	.to_msg_fill		= fifo_msg_fill,
};

static void __init fifo_init(void)
{
	rtnl_tc_register(&pfifo_ops);
	rtnl_tc_register(&bfifo_ops);
}

static void __exit fifo_exit(void)
{
	rtnl_tc_unregister(&pfifo_ops);
	rtnl_tc_unregister(&bfifo_ops);
}

/** @} */