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-2006 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))
63 return nl_error(EINVAL, "FIFO options size mismatch");
65 fifo = fifo_alloc(qdisc);
67 return nl_errno(ENOMEM);
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 int pfifo_dump_brief(struct rtnl_qdisc *qdisc,
82 struct nl_dump_params *p, int line)
84 struct rtnl_fifo *fifo = fifo_qdisc(qdisc);
87 dp_dump(p, " limit %u packets", fifo->qf_limit);
92 static int bfifo_dump_brief(struct rtnl_qdisc *qdisc,
93 struct nl_dump_params *p, int line)
95 struct rtnl_fifo *fifo = fifo_qdisc(qdisc);
101 r = nl_cancel_down_bytes(fifo->qf_limit, &unit);
102 dp_dump(p, " limit %.1f%s", r, unit);
108 static struct nl_msg *fifo_get_opts(struct rtnl_qdisc *qdisc)
110 struct rtnl_fifo *fifo;
111 struct tc_fifo_qopt opts;
114 fifo = fifo_qdisc(qdisc);
115 if (!fifo || !(fifo->qf_mask & SCH_FIFO_ATTR_LIMIT))
118 msg = nlmsg_build_no_hdr();
122 memset(&opts, 0, sizeof(opts));
123 opts.limit = fifo->qf_limit;
125 if (nlmsg_append(msg, &opts, sizeof(opts), 0) < 0)
135 * @name Attribute Modification
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.
145 int rtnl_qdisc_fifo_set_limit(struct rtnl_qdisc *qdisc, int limit)
147 struct rtnl_fifo *fifo;
149 fifo = fifo_alloc(qdisc);
151 return nl_errno(ENOMEM);
153 fifo->qf_limit = limit;
154 fifo->qf_mask |= SCH_FIFO_ATTR_LIMIT;
160 * Get limit of a FIFO qdisc.
161 * @arg qdisc FIFO qdisc.
162 * @return Numeric limit or a negative error code.
164 int rtnl_qdisc_fifo_get_limit(struct rtnl_qdisc *qdisc)
166 struct rtnl_fifo *fifo;
168 fifo = fifo_qdisc(qdisc);
169 if (fifo && fifo->qf_mask & SCH_FIFO_ATTR_LIMIT)
170 return fifo->qf_limit;
172 return nl_errno(ENOMEM);
177 static struct rtnl_qdisc_ops pfifo_ops = {
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,
185 static struct rtnl_qdisc_ops bfifo_ops = {
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,
193 static void __init fifo_init(void)
195 rtnl_qdisc_register(&pfifo_ops);
196 rtnl_qdisc_register(&bfifo_ops);
199 static void __exit fifo_exit(void)
201 rtnl_qdisc_unregister(&pfifo_ops);
202 rtnl_qdisc_unregister(&bfifo_ops);