libnl: /data/src/libnl3/lib/route/qdisc/fifo.c Source File

libnl 3.7.0

fifo.c
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * Copyright (c) 2003-2011 Thomas Graf <tgraf@suug.ch>
4 */
5
6 /**
7 * @ingroup qdisc
8 * @defgroup qdisc_fifo Packet/Bytes FIFO (pfifo/bfifo)
9 * @brief
10 *
11 * The FIFO qdisc comes in two flavours:
12 * @par bfifo (Byte FIFO)
13 * Allows enqueuing until the currently queued volume in bytes exceeds
14 * the configured limit.backlog contains currently enqueued volume in bytes.
15 *
16 * @par pfifo (Packet FIFO)
17 * Allows enquueing until the currently queued number of packets
18 * exceeds the configured limit.
19 *
20 * The configuration is exactly the same, the decision which of
21 * the two variations is going to be used is made based on the
22 * kind of the qdisc (rtnl_tc_set_kind()).
23 * @{
24 */
25
26 #include <netlink-private/netlink.h>
27 #include <netlink-private/tc.h>
28 #include <netlink/netlink.h>
29 #include <netlink-private/route/tc-api.h>
30 #include <netlink/route/qdisc.h>
31 #include <netlink/route/qdisc/fifo.h>
32 #include <netlink/utils.h>
33
34 /** @cond SKIP */
35 #define SCH_FIFO_ATTR_LIMIT 1
36 /** @endcond */
37
38 static int fifo_msg_parser(struct rtnl_tc *tc, void *data)
39 {
40 struct rtnl_fifo *fifo = data;
41 struct tc_fifo_qopt *opt;
42
43 if (tc->tc_opts->d_size < sizeof(struct tc_fifo_qopt))
44 return -NLE_INVAL;
45
46 opt = (struct tc_fifo_qopt *) tc->tc_opts->d_data;
47 fifo->qf_limit = opt->limit;
48 fifo->qf_mask = SCH_FIFO_ATTR_LIMIT;
49
50 return 0;
51 }
52
53 static void pfifo_dump_line(struct rtnl_tc *tc, void *data,
54 struct nl_dump_params *p)
55 {
56 struct rtnl_fifo *fifo = data;
57
58 if (fifo)
59 nl_dump(p, " limit %u packets", fifo->qf_limit);
60 }
61
62 static void bfifo_dump_line(struct rtnl_tc *tc, void *data,
63 struct nl_dump_params *p)
64 {
65 struct rtnl_fifo *fifo = data;
66 char *unit;
67 double r;
68
69 if (!fifo)
70 return;
71
72 r = nl_cancel_down_bytes(fifo->qf_limit, &unit);
73 nl_dump(p, " limit %.1f%s", r, unit);
74 }
75
76 static int fifo_msg_fill(struct rtnl_tc *tc, void *data, struct nl_msg *msg)
77 {
78 struct rtnl_fifo *fifo = data;
79 struct tc_fifo_qopt opts = {0};
80
81 if (!fifo || !(fifo->qf_mask & SCH_FIFO_ATTR_LIMIT))
82 return -NLE_INVAL;
83
84 opts.limit = fifo->qf_limit;
85
86 return nlmsg_append(msg, &opts, sizeof(opts), NL_DONTPAD);
87 }
88
89 /**
90 * @name Attribute Modification
91 * @{
92 */
93
94 /**
95 * Set limit of FIFO qdisc.
96 * @arg qdisc FIFO qdisc to be modified.
97 * @arg limit New limit.
98 * @return 0 on success or a negative error code.
99 */
100 int rtnl_qdisc_fifo_set_limit(struct rtnl_qdisc *qdisc, int limit)
101 {
102 struct rtnl_fifo *fifo;
103
104 if (!(fifo = rtnl_tc_data(TC_CAST(qdisc))))
105 return -NLE_NOMEM;
106
107 fifo->qf_limit = limit;
108 fifo->qf_mask |= SCH_FIFO_ATTR_LIMIT;
109
110 return 0;
111 }
112
113 /**
114 * Get limit of a FIFO qdisc.
115 * @arg qdisc FIFO qdisc.
116 * @return Numeric limit or a negative error code.
117 */
118 int rtnl_qdisc_fifo_get_limit(struct rtnl_qdisc *qdisc)
119 {
120 struct rtnl_fifo *fifo;
121
122 if (!(fifo = rtnl_tc_data(TC_CAST(qdisc))))
123 return -NLE_NOMEM;
124
125 if (fifo->qf_mask & SCH_FIFO_ATTR_LIMIT)
126 return fifo->qf_limit;
127 else
128 return -NLE_NOATTR;
129 }
130
131 /** @} */
132
133 static struct rtnl_tc_ops pfifo_ops = {
134 .to_kind = "pfifo",
135 .to_type = RTNL_TC_TYPE_QDISC,
136 .to_size = sizeof(struct rtnl_fifo),
137 .to_msg_parser = fifo_msg_parser,
138 .to_dump[NL_DUMP_LINE] = pfifo_dump_line,
139 .to_msg_fill = fifo_msg_fill,
140 };
141
142 static struct rtnl_tc_ops bfifo_ops = {
143 .to_kind = "bfifo",
144 .to_type = RTNL_TC_TYPE_QDISC,
145 .to_size = sizeof(struct rtnl_fifo),
146 .to_msg_parser = fifo_msg_parser,
147 .to_dump[NL_DUMP_LINE] = bfifo_dump_line,
148 .to_msg_fill = fifo_msg_fill,
149 };
150
151 static void __init fifo_init(void)
152 {
153 rtnl_tc_register(&pfifo_ops);
154 rtnl_tc_register(&bfifo_ops);
155 }
156
157 static void __exit fifo_exit(void)
158 {
159 rtnl_tc_unregister(&pfifo_ops);
160 rtnl_tc_unregister(&bfifo_ops);
161 }
162
163 /** @} */
nlmsg_append
int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
Append data to tail of a netlink message.
Definition: msg.c:442
rtnl_qdisc_fifo_get_limit
int rtnl_qdisc_fifo_get_limit(struct rtnl_qdisc *qdisc)
Get limit of a FIFO qdisc.
Definition: fifo.c:118
rtnl_qdisc_fifo_set_limit
int rtnl_qdisc_fifo_set_limit(struct rtnl_qdisc *qdisc, int limit)
Set limit of FIFO qdisc.
Definition: fifo.c:100
rtnl_tc_data
void * rtnl_tc_data(struct rtnl_tc *tc)
Return pointer to private data of traffic control object.
Definition: tc.c:1076
TC_CAST
#define TC_CAST(ptr)
Macro to cast qdisc/class/classifier to tc object.
Definition: tc.h:50
rtnl_tc_register
int rtnl_tc_register(struct rtnl_tc_ops *ops)
Register a traffic control module.
Definition: tc.c:1015
rtnl_tc_unregister
void rtnl_tc_unregister(struct rtnl_tc_ops *ops)
Unregister a traffic control module.
Definition: tc.c:1049
nl_dump
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
Definition: utils.c:955
nl_cancel_down_bytes
double nl_cancel_down_bytes(unsigned long long l, char **unit)
Cancel down a byte counter.
Definition: utils.c:163
NL_DUMP_LINE
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition: types.h:16
nl_dump_params
Dumping parameters.
Definition: types.h:28