2 * lib/route/sch/fifo.c (p|b)fifo
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
9 * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
14 * @defgroup fifo Packet/Bytes FIFO (pfifo/bfifo)
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.
22 * @par pfifo (Packet FIFO)
23 * Allows enquueing until the currently queued number of packets
24 * exceeds the configured limit.
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()).
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>
41 #define SCH_FIFO_ATTR_LIMIT 1
44 static inline struct rtnl_fifo *fifo_qdisc(struct rtnl_qdisc *qdisc)
46 return (struct rtnl_fifo *) qdisc->q_subdata;
49 static inline struct rtnl_fifo *fifo_alloc(struct rtnl_qdisc *qdisc)
51 if (!qdisc->q_subdata)
52 qdisc->q_subdata = calloc(1, sizeof(struct rtnl_fifo));
54 return fifo_qdisc(qdisc);
57 static int fifo_msg_parser(struct rtnl_qdisc *qdisc)
59 struct rtnl_fifo *fifo;
60 struct tc_fifo_qopt *opt;
62 if (qdisc->q_opts->d_size < sizeof(struct tc_fifo_qopt))
65 fifo = fifo_alloc(qdisc);
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;
76 static void fifo_free_data(struct rtnl_qdisc *qdisc)
78 free(qdisc->q_subdata);
81 static void pfifo_dump_line(struct rtnl_qdisc *qdisc, struct nl_dump_params *p)
83 struct rtnl_fifo *fifo = fifo_qdisc(qdisc);
86 nl_dump(p, " limit %u packets", fifo->qf_limit);
89 static void bfifo_dump_line(struct rtnl_qdisc *qdisc, struct nl_dump_params *p)
91 struct rtnl_fifo *fifo = fifo_qdisc(qdisc);
97 r = nl_cancel_down_bytes(fifo->qf_limit, &unit);
98 nl_dump(p, " limit %.1f%s", r, unit);
102 static struct nl_msg *fifo_get_opts(struct rtnl_qdisc *qdisc)
104 struct rtnl_fifo *fifo;
105 struct tc_fifo_qopt opts;
108 fifo = fifo_qdisc(qdisc);
109 if (!fifo || !(fifo->qf_mask & SCH_FIFO_ATTR_LIMIT))
116 memset(&opts, 0, sizeof(opts));
117 opts.limit = fifo->qf_limit;
119 if (nlmsg_append(msg, &opts, sizeof(opts), NL_DONTPAD) < 0)
129 * @name Attribute Modification
134 * Set limit of FIFO qdisc.
135 * @arg qdisc FIFO qdisc to be modified.
136 * @arg limit New limit.
137 * @return 0 on success or a negative error code.
139 int rtnl_qdisc_fifo_set_limit(struct rtnl_qdisc *qdisc, int limit)
141 struct rtnl_fifo *fifo;
143 fifo = fifo_alloc(qdisc);
147 fifo->qf_limit = limit;
148 fifo->qf_mask |= SCH_FIFO_ATTR_LIMIT;
154 * Get limit of a FIFO qdisc.
155 * @arg qdisc FIFO qdisc.
156 * @return Numeric limit or a negative error code.
158 int rtnl_qdisc_fifo_get_limit(struct rtnl_qdisc *qdisc)
160 struct rtnl_fifo *fifo;
162 fifo = fifo_qdisc(qdisc);
163 if (fifo && fifo->qf_mask & SCH_FIFO_ATTR_LIMIT)
164 return fifo->qf_limit;
171 static struct rtnl_qdisc_ops pfifo_ops = {
173 .qo_msg_parser = fifo_msg_parser,
174 .qo_free_data = fifo_free_data,
175 .qo_dump[NL_DUMP_LINE] = pfifo_dump_line,
176 .qo_get_opts = fifo_get_opts,
179 static struct rtnl_qdisc_ops bfifo_ops = {
181 .qo_msg_parser = fifo_msg_parser,
182 .qo_free_data = fifo_free_data,
183 .qo_dump[NL_DUMP_LINE] = bfifo_dump_line,
184 .qo_get_opts = fifo_get_opts,
187 static void __init fifo_init(void)
189 rtnl_qdisc_register(&pfifo_ops);
190 rtnl_qdisc_register(&bfifo_ops);
193 static void __exit fifo_exit(void)
195 rtnl_qdisc_unregister(&pfifo_ops);
196 rtnl_qdisc_unregister(&bfifo_ops);