oops
[libnl.git] / lib / route / sch / fifo.c
1 /*
2  * lib/route/sch/fifo.c         (p|b)fifo
3  *
4  *      This library is free software; you can redistribute it and/or
5  *      modify it under the terms of the GNU Lesser General Public
6  *      License as published by the Free Software Foundation version 2.1
7  *      of the License.
8  *
9  * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
10  */
11
12 /**
13  * @ingroup qdisc
14  * @defgroup fifo Packet/Bytes FIFO (pfifo/bfifo)
15  * @brief
16  *
17  * The FIFO qdisc comes in two flavours:
18  * @par bfifo (Byte FIFO)
19  * Allows enqueuing until the currently queued volume in bytes exceeds
20  * the configured limit.backlog contains currently enqueued volume in bytes.
21  *
22  * @par pfifo (Packet FIFO)
23  * Allows enquueing until the currently queued number of packets
24  * exceeds the configured limit.
25  *
26  * The configuration is exactly the same, the decision which of
27  * the two variations is going to be used is made based on the
28  * kind of the qdisc (rtnl_qdisc_set_kind()).
29  * @{
30  */
31
32 #include <netlink-local.h>
33 #include <netlink-tc.h>
34 #include <netlink/netlink.h>
35 #include <netlink/route/qdisc.h>
36 #include <netlink/route/qdisc-modules.h>
37 #include <netlink/route/sch/fifo.h>
38 #include <netlink/utils.h>
39
40 /** @cond SKIP */
41 #define SCH_FIFO_ATTR_LIMIT 1
42 /** @endcond */
43
44 static inline struct rtnl_fifo *fifo_qdisc(struct rtnl_qdisc *qdisc)
45 {
46         return (struct rtnl_fifo *) qdisc->q_subdata;
47 }
48
49 static inline struct rtnl_fifo *fifo_alloc(struct rtnl_qdisc *qdisc)
50 {
51         if (!qdisc->q_subdata)
52                 qdisc->q_subdata = calloc(1, sizeof(struct rtnl_fifo));
53
54         return fifo_qdisc(qdisc);
55 }
56
57 static int fifo_msg_parser(struct rtnl_qdisc *qdisc)
58 {
59         struct rtnl_fifo *fifo;
60         struct tc_fifo_qopt *opt;
61
62         if (qdisc->q_opts->d_size < sizeof(struct tc_fifo_qopt))
63                 return nl_error(EINVAL, "FIFO options size mismatch");
64
65         fifo = fifo_alloc(qdisc);
66         if (!fifo)
67                 return nl_errno(ENOMEM);
68
69         opt = (struct tc_fifo_qopt *) qdisc->q_opts->d_data;
70         fifo->qf_limit = opt->limit;
71         fifo->qf_mask = SCH_FIFO_ATTR_LIMIT;
72
73         return 0;
74 }
75
76 static void fifo_free_data(struct rtnl_qdisc *qdisc)
77 {
78         free(qdisc->q_subdata);
79 }
80
81 static int pfifo_dump_brief(struct rtnl_qdisc *qdisc,
82                             struct nl_dump_params *p, int line)
83 {
84         struct rtnl_fifo *fifo = fifo_qdisc(qdisc);
85
86         if (fifo)
87                 dp_dump(p, " limit %u packets", fifo->qf_limit);
88
89         return line;
90 }
91
92 static int bfifo_dump_brief(struct rtnl_qdisc *qdisc,
93                             struct nl_dump_params *p, int line)
94 {
95         struct rtnl_fifo *fifo = fifo_qdisc(qdisc);
96
97         if (fifo) {
98                 char *unit;
99                 double r;
100
101                 r = nl_cancel_down_bytes(fifo->qf_limit, &unit);
102                 dp_dump(p, " limit %.1f%s", r, unit);
103         }
104
105         return line;
106 }
107
108 static struct nl_msg *fifo_get_opts(struct rtnl_qdisc *qdisc)
109 {
110         struct rtnl_fifo *fifo;
111         struct tc_fifo_qopt opts;
112         struct nl_msg *msg;
113
114         fifo = fifo_qdisc(qdisc);
115         if (!fifo || !(fifo->qf_mask & SCH_FIFO_ATTR_LIMIT))
116                 return NULL;
117
118         msg = nlmsg_build_no_hdr();
119         if (!msg)
120                 goto errout;
121
122         memset(&opts, 0, sizeof(opts));
123         opts.limit = fifo->qf_limit;
124
125         if (nlmsg_append(msg, &opts, sizeof(opts), 0) < 0)
126                 goto errout;
127
128         return msg;
129 errout:
130         nlmsg_free(msg);
131         return NULL;
132 }
133
134 /**
135  * @name Attribute Modification
136  * @{
137  */
138
139 /**
140  * Set limit of FIFO qdisc.
141  * @arg qdisc           FIFO qdisc to be modified.
142  * @arg limit           New limit.
143  * @return 0 on success or a negative error code.
144  */
145 int rtnl_qdisc_fifo_set_limit(struct rtnl_qdisc *qdisc, int limit)
146 {
147         struct rtnl_fifo *fifo;
148         
149         fifo = fifo_alloc(qdisc);
150         if (!fifo)
151                 return nl_errno(ENOMEM);
152                 
153         fifo->qf_limit = limit;
154         fifo->qf_mask |= SCH_FIFO_ATTR_LIMIT;
155
156         return 0;
157 }
158
159 /**
160  * Get limit of a FIFO qdisc.
161  * @arg qdisc           FIFO qdisc.
162  * @return Numeric limit or a negative error code.
163  */
164 int rtnl_qdisc_fifo_get_limit(struct rtnl_qdisc *qdisc)
165 {
166         struct rtnl_fifo *fifo;
167         
168         fifo = fifo_qdisc(qdisc);
169         if (fifo && fifo->qf_mask & SCH_FIFO_ATTR_LIMIT)
170                 return fifo->qf_limit;
171         else
172                 return nl_errno(ENOMEM);
173 }
174
175 /** @} */
176
177 static struct rtnl_qdisc_ops pfifo_ops = {
178         .qo_kind                = "pfifo",
179         .qo_msg_parser          = fifo_msg_parser,
180         .qo_free_data           = fifo_free_data,
181         .qo_dump[NL_DUMP_BRIEF] = pfifo_dump_brief,
182         .qo_get_opts            = fifo_get_opts,
183 };
184
185 static struct rtnl_qdisc_ops bfifo_ops = {
186         .qo_kind                = "bfifo",
187         .qo_msg_parser          = fifo_msg_parser,
188         .qo_free_data           = fifo_free_data,
189         .qo_dump[NL_DUMP_BRIEF] = bfifo_dump_brief,
190         .qo_get_opts            = fifo_get_opts,
191 };
192
193 static void __init fifo_init(void)
194 {
195         rtnl_qdisc_register(&pfifo_ops);
196         rtnl_qdisc_register(&bfifo_ops);
197 }
198
199 static void __exit fifo_exit(void)
200 {
201         rtnl_qdisc_unregister(&pfifo_ops);
202         rtnl_qdisc_unregister(&bfifo_ops);
203 }
204
205 /** @} */