libnl 1.1.4
red.c
1/*
2 * lib/route/sch/red.c RED Qdisc
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_api
14 * @defgroup red Random Early Detection (RED)
15 * @brief
16 * @{
17 */
18
19#include <netlink-local.h>
20#include <netlink-tc.h>
21#include <netlink/netlink.h>
22#include <netlink/utils.h>
23#include <netlink/route/qdisc.h>
24#include <netlink/route/qdisc-modules.h>
25#include <netlink/route/sch/red.h>
26
27/** @cond SKIP */
28#define RED_ATTR_LIMIT 0x01
29#define RED_ATTR_QTH_MIN 0x02
30#define RED_ATTR_QTH_MAX 0x04
31#define RED_ATTR_FLAGS 0x08
32#define RED_ATTR_WLOG 0x10
33#define RED_ATTR_PLOG 0x20
34#define RED_ATTR_SCELL_LOG 0x40
35/** @endcond */
36
37static inline struct rtnl_red *red_qdisc(struct rtnl_qdisc *qdisc)
38{
39 return (struct rtnl_red *) qdisc->q_subdata;
40}
41
42static inline struct rtnl_red *red_alloc(struct rtnl_qdisc *qdisc)
43{
44 if (!qdisc->q_subdata)
45 qdisc->q_subdata = calloc(1, sizeof(struct rtnl_red));
46
47 return red_qdisc(qdisc);
48}
49
50static struct nla_policy red_policy[TCA_RED_MAX+1] = {
51 [TCA_RED_PARMS] = { .minlen = sizeof(struct tc_red_qopt) },
52};
53
54static int red_msg_parser(struct rtnl_qdisc *qdisc)
55{
56 struct nlattr *tb[TCA_RED_MAX+1];
57 struct rtnl_red *red;
58 struct tc_red_qopt *opts;
59 int err;
60
61 if (!(qdisc->ce_mask & TCA_ATTR_OPTS))
62 return 0;
63
64 err = tca_parse(tb, TCA_RED_MAX, (struct rtnl_tca *) qdisc, red_policy);
65 if (err < 0)
66 return err;
67
68 if (!tb[TCA_RED_PARMS])
69 return nl_error(EINVAL, "Missing TCA_RED_PARMS");
70
71 red = red_alloc(qdisc);
72 if (!red)
73 return nl_errno(ENOMEM);
74
75 opts = nla_data(tb[TCA_RED_PARMS]);
76
77 red->qr_limit = opts->limit;
78 red->qr_qth_min = opts->qth_min;
79 red->qr_qth_max = opts->qth_max;
80 red->qr_flags = opts->flags;
81 red->qr_wlog = opts->Wlog;
82 red->qr_plog = opts->Plog;
83 red->qr_scell_log = opts->Scell_log;
84
85 red->qr_mask = (RED_ATTR_LIMIT | RED_ATTR_QTH_MIN | RED_ATTR_QTH_MAX |
86 RED_ATTR_FLAGS | RED_ATTR_WLOG | RED_ATTR_PLOG |
87 RED_ATTR_SCELL_LOG);
88
89 return 0;
90}
91
92static int red_dump_brief(struct rtnl_qdisc *qdisc, struct nl_dump_params *p,
93 int line)
94{
95 struct rtnl_red *red = red_qdisc(qdisc);
96
97 if (red) {
98 /* XXX: limit, min, max, flags */
99 }
100
101 return line;
102}
103
104static int red_dump_full(struct rtnl_qdisc *qdisc, struct nl_dump_params *p,
105 int line)
106{
107 struct rtnl_red *red = red_qdisc(qdisc);
108
109 if (red) {
110 /* XXX: wlog, plog, scell_log */
111 }
112
113 return line;
114}
115
116static int red_dump_stats(struct rtnl_qdisc *qdisc, struct nl_dump_params *p,
117 int line)
118{
119 struct rtnl_red *red = red_qdisc(qdisc);
120
121 if (red) {
122 /* XXX: xstats */
123 }
124
125 return line;
126}
127
128static struct nl_msg *red_get_opts(struct rtnl_qdisc *qdisc)
129{
130 struct rtnl_red *red;
131 struct nl_msg *msg;
132
133 red = red_qdisc(qdisc);
134 if (!red)
135 return NULL;
136
137 msg = nlmsg_alloc();
138 if (!msg)
139 goto errout;
140
141#if 0
142 memset(&opts, 0, sizeof(opts));
143 opts.quantum = sfq->qs_quantum;
144 opts.perturb_period = sfq->qs_perturb;
145 opts.limit = sfq->qs_limit;
146
147 if (nlmsg_append(msg, &opts, sizeof(opts), NL_DONTPAD) < 0)
148 goto errout;
149#endif
150
151 return msg;
152errout:
153 nlmsg_free(msg);
154 return NULL;
155}
156
157/**
158 * @name Attribute Access
159 * @{
160 */
161
162/**
163 * Set limit of RED qdisc.
164 * @arg qdisc RED qdisc to be modified.
165 * @arg limit New limit in number of packets.
166 * @return 0 on success or a negative error code.
167 */
168int rtnl_red_set_limit(struct rtnl_qdisc *qdisc, int limit)
169{
170 struct rtnl_red *red;
171
172 red = red_alloc(qdisc);
173 if (!red)
174 return nl_errno(ENOMEM);
175
176 red->qr_limit = limit;
177 red->qr_mask |= RED_ATTR_LIMIT;
178
179 return 0;
180}
181
182/**
183 * Get limit of RED qdisc.
184 * @arg qdisc RED qdisc.
185 * @return Limit or a negative error code.
186 */
187int rtnl_red_get_limit(struct rtnl_qdisc *qdisc)
188{
189 struct rtnl_red *red;
190
191 red = red_qdisc(qdisc);
192 if (red && (red->qr_mask & RED_ATTR_LIMIT))
193 return red->qr_limit;
194 else
195 return nl_errno(ENOENT);
196}
197
198/** @} */
199
200static struct rtnl_qdisc_ops red_ops = {
201 .qo_kind = "red",
202 .qo_msg_parser = red_msg_parser,
203 .qo_dump[NL_DUMP_BRIEF] = red_dump_brief,
204 .qo_dump[NL_DUMP_FULL] = red_dump_full,
205 .qo_dump[NL_DUMP_STATS] = red_dump_stats,
206 .qo_get_opts = red_get_opts,
207};
208
209static void __init red_init(void)
210{
211 rtnl_qdisc_register(&red_ops);
212}
213
214static void __exit red_exit(void)
215{
216 rtnl_qdisc_unregister(&red_ops);
217}
218
219/** @} */
void * nla_data(const struct nlattr *nla)
head of payload
Definition: attr.c:151
void nlmsg_free(struct nl_msg *n)
Free a netlink message.
Definition: msg.c:656
struct nl_msg * nlmsg_alloc(void)
Allocate a new netlink message with the default maximum payload size.
Definition: msg.c:401
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:549
int rtnl_qdisc_unregister(struct rtnl_qdisc_ops *qops)
Unregister a qdisc module.
Definition: qdisc_api.c:61
int rtnl_qdisc_register(struct rtnl_qdisc_ops *qops)
Register a qdisc module.
Definition: qdisc_api.c:40
int rtnl_red_set_limit(struct rtnl_qdisc *qdisc, int limit)
Set limit of RED qdisc.
Definition: red.c:168
int rtnl_red_get_limit(struct rtnl_qdisc *qdisc)
Get limit of RED qdisc.
Definition: red.c:187
@ NL_DUMP_FULL
Dump all attributes but no statistics.
Definition: types.h:23
@ NL_DUMP_BRIEF
Dump object in a brief one-liner.
Definition: types.h:22
@ NL_DUMP_STATS
Dump all attributes including statistics.
Definition: types.h:24
Dumping parameters.
Definition: types.h:37
attribute validation policy
Definition: attr.h:73
uint16_t minlen
Minimal length of payload required to be available.
Definition: attr.h:78
Qdisc Operations.
Definition: qdisc-modules.h:26
char qo_kind[32]
Kind/Name of Qdisc.
Definition: qdisc-modules.h:30