1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * Copyright (c) 2003-2011 Thomas Graf <tgraf@suug.ch>
4 */
5
6 /**
7 * @ingroup rtnl
8 * @defgroup tc Traffic Control
9 * @{
10 */
11
12 #include <netlink-private/netlink.h>
13 #include <netlink-private/tc.h>
14 #include <netlink/netlink.h>
15 #include <netlink/utils.h>
16 #include <netlink/route/rtnl.h>
17 #include <netlink/route/link.h>
18 #include <netlink/route/tc.h>
19 #include <netlink-private/route/tc-api.h>
20
21 #include "netlink-private/utils.h"
22
23 /** @cond SKIP */
24
25 static struct nl_list_head tc_ops_list[__RTNL_TC_TYPE_MAX];
26 static struct rtnl_tc_type_ops *tc_type_ops[__RTNL_TC_TYPE_MAX];
27
28 static struct nla_policy tc_policy[TCA_MAX+1] = {
29 [TCA_KIND] = { .type = NLA_STRING,
30 .maxlen = TCKINDSIZ },
31 [TCA_CHAIN] = { .type = NLA_U32 },
32 [TCA_STATS] = { .minlen = sizeof(struct tc_stats) },
33 [TCA_STATS2] = { .type = NLA_NESTED },
34 };
35
tca_parse(struct nlattr ** tb,int maxattr,struct rtnl_tc * g,const struct nla_policy * policy)36 int tca_parse(struct nlattr **tb, int maxattr, struct rtnl_tc *g,
37 const struct nla_policy *policy)
38 {
39
40 if (g->ce_mask & TCA_ATTR_OPTS)
41 return nla_parse(tb, maxattr,
42 (struct nlattr *) g->tc_opts->d_data,
43 g->tc_opts->d_size, policy);
44 else {
45 /* Ugly but tb[] must be in a defined state even if no
46 * attributes can be found. */
47 memset(tb, 0, sizeof(struct nlattr *) * (maxattr + 1));
48 return 0;
49 }
50 }
51
52 static struct nla_policy tc_stats2_policy[TCA_STATS_MAX+1] = {
53 [TCA_STATS_BASIC] = { .minlen = sizeof(struct gnet_stats_basic) },
54 [TCA_STATS_RATE_EST] = { .minlen = sizeof(struct gnet_stats_rate_est) },
55 [TCA_STATS_QUEUE] = { .minlen = sizeof(struct gnet_stats_queue) },
56 };
57
rtnl_tc_msg_parse(struct nlmsghdr * n,struct rtnl_tc * tc)58 int rtnl_tc_msg_parse(struct nlmsghdr *n, struct rtnl_tc *tc)
59 {
60 struct nl_cache *link_cache;
61 struct rtnl_tc_ops *ops;
62 struct nlattr *tb[TCA_MAX + 1];
63 char kind[TCKINDSIZ];
64 struct tcmsg *tm;
65 int err;
66
67 tc->ce_msgtype = n->nlmsg_type;
68
69 err = nlmsg_parse(n, sizeof(*tm), tb, TCA_MAX, tc_policy);
70 if (err < 0)
71 return err;
72
73 if (tb[TCA_KIND] == NULL)
74 return -NLE_MISSING_ATTR;
75
76 nla_strlcpy(kind, tb[TCA_KIND], sizeof(kind));
77 rtnl_tc_set_kind(tc, kind);
78
79 if (tb[TCA_CHAIN])
80 rtnl_tc_set_chain(tc, nla_get_u32(tb[TCA_CHAIN]));
81
82 tm = nlmsg_data(n);
83 tc->tc_family = tm->tcm_family;
84 tc->tc_ifindex = tm->tcm_ifindex;
85 tc->tc_handle = tm->tcm_handle;
86 tc->tc_parent = tm->tcm_parent;
87 tc->tc_info = tm->tcm_info;
88
89 tc->ce_mask |= (TCA_ATTR_FAMILY | TCA_ATTR_IFINDEX | TCA_ATTR_HANDLE|
90 TCA_ATTR_PARENT | TCA_ATTR_INFO);
91
92 if (tb[TCA_OPTIONS]) {
93 tc->tc_opts = nl_data_alloc_attr(tb[TCA_OPTIONS]);
94 if (!tc->tc_opts)
95 return -NLE_NOMEM;
96 tc->ce_mask |= TCA_ATTR_OPTS;
97 }
98
99 if (tb[TCA_STATS2]) {
100 struct nlattr *tbs[TCA_STATS_MAX + 1];
101
102 err = nla_parse_nested(tbs, TCA_STATS_MAX, tb[TCA_STATS2],
103 tc_stats2_policy);
104 if (err < 0)
105 return err;
106
107 if (tbs[TCA_STATS_BASIC]) {
108 struct gnet_stats_basic *bs;
109
110 bs = nla_data(tbs[TCA_STATS_BASIC]);
111 tc->tc_stats[RTNL_TC_BYTES] = bs->bytes;
112 tc->tc_stats[RTNL_TC_PACKETS] = bs->packets;
113 }
114
115 if (tbs[TCA_STATS_RATE_EST]) {
116 struct gnet_stats_rate_est *re;
117
118 re = nla_data(tbs[TCA_STATS_RATE_EST]);
119 tc->tc_stats[RTNL_TC_RATE_BPS] = re->bps;
120 tc->tc_stats[RTNL_TC_RATE_PPS] = re->pps;
121 }
122
123 if (tbs[TCA_STATS_QUEUE]) {
124 struct gnet_stats_queue *q;
125
126 q = nla_data(tbs[TCA_STATS_QUEUE]);
127 tc->tc_stats[RTNL_TC_QLEN] = q->qlen;
128 tc->tc_stats[RTNL_TC_BACKLOG] = q->backlog;
129 tc->tc_stats[RTNL_TC_DROPS] = q->drops;
130 tc->tc_stats[RTNL_TC_REQUEUES] = q->requeues;
131 tc->tc_stats[RTNL_TC_OVERLIMITS] = q->overlimits;
132 }
133
134 tc->ce_mask |= TCA_ATTR_STATS;
135
136 if (tbs[TCA_STATS_APP]) {
137 tc->tc_xstats = nl_data_alloc_attr(tbs[TCA_STATS_APP]);
138 if (tc->tc_xstats == NULL)
139 return -NLE_NOMEM;
140 tc->ce_mask |= TCA_ATTR_XSTATS;
141 } else
142 goto compat_xstats;
143 } else {
144 if (tb[TCA_STATS]) {
145 struct tc_stats *st = nla_data(tb[TCA_STATS]);
146
147 tc->tc_stats[RTNL_TC_BYTES] = st->bytes;
148 tc->tc_stats[RTNL_TC_PACKETS] = st->packets;
149 tc->tc_stats[RTNL_TC_RATE_BPS] = st->bps;
150 tc->tc_stats[RTNL_TC_RATE_PPS] = st->pps;
151 tc->tc_stats[RTNL_TC_QLEN] = st->qlen;
152 tc->tc_stats[RTNL_TC_BACKLOG] = st->backlog;
153 tc->tc_stats[RTNL_TC_DROPS] = st->drops;
154 tc->tc_stats[RTNL_TC_OVERLIMITS]= st->overlimits;
155
156 tc->ce_mask |= TCA_ATTR_STATS;
157 }
158
159 compat_xstats:
160 if (tb[TCA_XSTATS]) {
161 tc->tc_xstats = nl_data_alloc_attr(tb[TCA_XSTATS]);
162 if (tc->tc_xstats == NULL)
163 return -NLE_NOMEM;
164 tc->ce_mask |= TCA_ATTR_XSTATS;
165 }
166 }
167
168 ops = rtnl_tc_get_ops(tc);
169 if (ops && ops->to_msg_parser) {
170 void *data = rtnl_tc_data(tc);
171
172 if (!data)
173 return -NLE_NOMEM;
174
175 err = ops->to_msg_parser(tc, data);
176 if (err < 0)
177 return err;
178 }
179
180 if ((link_cache = __nl_cache_mngt_require("route/link"))) {
181 struct rtnl_link *link;
182
183 if ((link = rtnl_link_get(link_cache, tc->tc_ifindex))) {
184 rtnl_tc_set_link(tc, link);
185
186 /* rtnl_tc_set_link incs refcnt */
187 rtnl_link_put(link);
188 }
189 }
190
191 return 0;
192 }
193
rtnl_tc_msg_build(struct rtnl_tc * tc,int type,int flags,struct nl_msg ** result)194 int rtnl_tc_msg_build(struct rtnl_tc *tc, int type, int flags,
195 struct nl_msg **result)
196 {
197 struct nl_msg *msg;
198 struct rtnl_tc_ops *ops;
199 struct tcmsg tchdr = {
200 .tcm_family = AF_UNSPEC,
201 .tcm_ifindex = tc->tc_ifindex,
202 .tcm_handle = tc->tc_handle,
203 .tcm_parent = tc->tc_parent,
204 };
205 int err;
206
207 msg = nlmsg_alloc_simple(type, flags);
208 if (!msg)
209 return -NLE_NOMEM;
210
211 if (nlmsg_append(msg, &tchdr, sizeof(tchdr), NLMSG_ALIGNTO) < 0) {
212 err = -NLE_MSGSIZE;
213 goto out_err;
214 }
215
216 if (tc->ce_mask & TCA_ATTR_KIND)
217 NLA_PUT_STRING(msg, TCA_KIND, tc->tc_kind);
218
219 if (tc->ce_mask & TCA_ATTR_CHAIN)
220 NLA_PUT_U32(msg, TCA_CHAIN, tc->tc_chain);
221
222 ops = rtnl_tc_get_ops(tc);
223 if (ops && (ops->to_msg_fill || ops->to_msg_fill_raw)) {
224 struct nlattr *opts;
225 void *data = rtnl_tc_data(tc);
226
227 if (ops->to_msg_fill) {
228 if (!(opts = nla_nest_start(msg, TCA_OPTIONS))) {
229 err = -NLE_NOMEM;
230 goto out_err;
231 }
232
233 if ((err = ops->to_msg_fill(tc, data, msg)) < 0)
234 goto out_err;
235
236 if (strcmp("cgroup", tc->tc_kind))
237 nla_nest_end(msg, opts);
238 else
239 nla_nest_end_keep_empty(msg, opts);
240 } else if ((err = ops->to_msg_fill_raw(tc, data, msg)) < 0)
241 goto out_err;
242 }
243
244 *result = msg;
245 return 0;
246
247 nla_put_failure:
248 err = -NLE_NOMEM;
249 out_err:
250 nlmsg_free(msg);
251 return err;
252 }
253
254
255 /** @endcond */
256
257 /**
258 * @name Attributes
259 * @{
260 */
261
262 /**
263 * Set interface index of traffic control object
264 * @arg tc traffic control object
265 * @arg ifindex interface index.
266 *
267 * Sets the interface index of a traffic control object. The interface
268 * index defines the network device which this tc object is attached to.
269 * This function will overwrite any network device assigned with previous
270 * calls to rtnl_tc_set_ifindex() or rtnl_tc_set_link().
271 */
rtnl_tc_set_ifindex(struct rtnl_tc * tc,int ifindex)272 void rtnl_tc_set_ifindex(struct rtnl_tc *tc, int ifindex)
273 {
274 /* Obsolete possible old link reference */
275 rtnl_link_put(tc->tc_link);
276 tc->tc_link = NULL;
277 tc->ce_mask &= ~TCA_ATTR_LINK;
278
279 tc->tc_ifindex = ifindex;
280 tc->ce_mask |= TCA_ATTR_IFINDEX;
281 }
282
283 /**
284 * Return interface index of traffic control object
285 * @arg tc traffic control object
286 */
rtnl_tc_get_ifindex(struct rtnl_tc * tc)287 int rtnl_tc_get_ifindex(struct rtnl_tc *tc)
288 {
289 return tc->tc_ifindex;
290 }
291
292 /**
293 * Set link of traffic control object
294 * @arg tc traffic control object
295 * @arg link link object
296 *
297 * Sets the link of a traffic control object. This function serves
298 * the same purpose as rtnl_tc_set_ifindex() but due to the continued
299 * allowed access to the link object it gives it the possibility to
300 * retrieve sane default values for the the MTU and the linktype.
301 * Always prefer this function over rtnl_tc_set_ifindex() if you can
302 * spare to have an additional link object around.
303 */
rtnl_tc_set_link(struct rtnl_tc * tc,struct rtnl_link * link)304 void rtnl_tc_set_link(struct rtnl_tc *tc, struct rtnl_link *link)
305 {
306 rtnl_link_put(tc->tc_link);
307
308 if (!link)
309 return;
310 if (!link->l_index)
311 BUG();
312
313 nl_object_get(OBJ_CAST(link));
314 tc->tc_link = link;
315 tc->tc_ifindex = link->l_index;
316 tc->ce_mask |= TCA_ATTR_LINK | TCA_ATTR_IFINDEX;
317 }
318
319 /**
320 * Get link of traffic control object
321 * @arg tc traffic control object
322 *
323 * Returns the link of a traffic control object. The link is only
324 * returned if it has been set before via rtnl_tc_set_link() or
325 * if a link cache was available while parsing the tc object. This
326 * function may still return NULL even if an ifindex is assigned to
327 * the tc object. It will _not_ look up the link by itself.
328 *
329 * @note The returned link will have its reference counter incremented.
330 * It is in the responsibility of the caller to return the
331 * reference.
332 *
333 * @return link object or NULL if not set.
334 */
rtnl_tc_get_link(struct rtnl_tc * tc)335 struct rtnl_link *rtnl_tc_get_link(struct rtnl_tc *tc)
336 {
337 if (tc->tc_link) {
338 nl_object_get(OBJ_CAST(tc->tc_link));
339 return tc->tc_link;
340 }
341
342 return NULL;
343 }
344
345 /**
346 * Set the Maximum Transmission Unit (MTU) of traffic control object
347 * @arg tc traffic control object
348 * @arg mtu largest packet size expected
349 *
350 * Sets the MTU of a traffic control object. Not all traffic control
351 * objects will make use of this but it helps while calculating rate
352 * tables. This value is typically derived directly from the link
353 * the tc object is attached to if the link has been assigned via
354 * rtnl_tc_set_link(). It is usually not necessary to set the MTU
355 * manually, this function is provided to allow overwriting the derived
356 * value.
357 */
rtnl_tc_set_mtu(struct rtnl_tc * tc,uint32_t mtu)358 void rtnl_tc_set_mtu(struct rtnl_tc *tc, uint32_t mtu)
359 {
360 tc->tc_mtu = mtu;
361 tc->ce_mask |= TCA_ATTR_MTU;
362 }
363
364 /**
365 * Return the MTU of traffic control object
366 * @arg tc traffic control object
367 *
368 * Returns the MTU of a traffic control object which has been set via:
369 * -# User specified value set via rtnl_tc_set_mtu()
370 * -# Dervied from link set via rtnl_tc_set_link()
371 * -# Fall back to default: ethernet = 1500
372 */
rtnl_tc_get_mtu(struct rtnl_tc * tc)373 uint32_t rtnl_tc_get_mtu(struct rtnl_tc *tc)
374 {
375 if (tc->ce_mask & TCA_ATTR_MTU)
376 return tc->tc_mtu;
377 else if (tc->ce_mask & TCA_ATTR_LINK)
378 return tc->tc_link->l_mtu;
379 else
380 return 1500; /* default to ethernet */
381 }
382
383 /**
384 * Set the Minimum Packet Unit (MPU) of a traffic control object
385 * @arg tc traffic control object
386 * @arg mpu minimum packet size expected
387 *
388 * Sets the MPU of a traffic contorl object. It specifies the minimum
389 * packet size to ever hit this traffic control object. Not all traffic
390 * control objects will make use of this but it helps while calculating
391 * rate tables.
392 */
rtnl_tc_set_mpu(struct rtnl_tc * tc,uint32_t mpu)393 void rtnl_tc_set_mpu(struct rtnl_tc *tc, uint32_t mpu)
394 {
395 tc->tc_mpu = mpu;
396 tc->ce_mask |= TCA_ATTR_MPU;
397 }
398
399 /**
400 * Return the Minimum Packet Unit (MPU) of a traffic control object
401 * @arg tc traffic control object
402 *
403 * @return The MPU previously set via rtnl_tc_set_mpu() or 0.
404 */
rtnl_tc_get_mpu(struct rtnl_tc * tc)405 uint32_t rtnl_tc_get_mpu(struct rtnl_tc *tc)
406 {
407 return tc->tc_mpu;
408 }
409
410 /**
411 * Set per packet overhead of a traffic control object
412 * @arg tc traffic control object
413 * @arg overhead overhead per packet in bytes
414 *
415 * Sets the per packet overhead in bytes occuring on the link not seen
416 * by the kernel. This value can be used to correct size calculations
417 * if the packet size on the wire does not match the packet sizes seen
418 * in the network stack. Not all traffic control objects will make use
419 * this but it helps while calculating accurate packet sizes in the
420 * kernel.
421 */
rtnl_tc_set_overhead(struct rtnl_tc * tc,uint32_t overhead)422 void rtnl_tc_set_overhead(struct rtnl_tc *tc, uint32_t overhead)
423 {
424 tc->tc_overhead = overhead;
425 tc->ce_mask |= TCA_ATTR_OVERHEAD;
426 }
427
428 /**
429 * Return per packet overhead of a traffic control object
430 * @arg tc traffic control object
431 *
432 * @return The overhead previously set by rtnl_tc_set_overhead() or 0.
433 */
rtnl_tc_get_overhead(struct rtnl_tc * tc)434 uint32_t rtnl_tc_get_overhead(struct rtnl_tc *tc)
435 {
436 return tc->tc_overhead;
437 }
438
439 /**
440 * Set the linktype of a traffic control object
441 * @arg tc traffic control object
442 * @arg type type of link (e.g. ARPHRD_ATM, ARPHRD_ETHER)
443 *
444 * Overwrites the type of link this traffic control object is attached to.
445 * This value is typically derived from the link this tc object is attached
446 * if the link has been assigned via rtnl_tc_set_link(). It is usually not
447 * necessary to set the linktype manually. This function is provided to
448 * allow overwriting the linktype.
449 */
rtnl_tc_set_linktype(struct rtnl_tc * tc,uint32_t type)450 void rtnl_tc_set_linktype(struct rtnl_tc *tc, uint32_t type)
451 {
452 tc->tc_linktype = type;
453 tc->ce_mask |= TCA_ATTR_LINKTYPE;
454 }
455
456 /**
457 * Return the linktype of a traffic control object
458 * @arg tc traffic control object
459 *
460 * Returns the linktype of the link the traffic control object is attached to:
461 * -# User specified value via rtnl_tc_set_linktype()
462 * -# Value derived from link set via rtnl_tc_set_link()
463 * -# Default fall-back: ARPHRD_ETHER
464 */
rtnl_tc_get_linktype(struct rtnl_tc * tc)465 uint32_t rtnl_tc_get_linktype(struct rtnl_tc *tc)
466 {
467 if (tc->ce_mask & TCA_ATTR_LINKTYPE)
468 return tc->tc_linktype;
469 else if (tc->ce_mask & TCA_ATTR_LINK)
470 return tc->tc_link->l_arptype;
471 else
472 return ARPHRD_ETHER; /* default to ethernet */
473 }
474
475 /**
476 * Set identifier of traffic control object
477 * @arg tc traffic control object
478 * @arg id unique identifier
479 */
rtnl_tc_set_handle(struct rtnl_tc * tc,uint32_t id)480 void rtnl_tc_set_handle(struct rtnl_tc *tc, uint32_t id)
481 {
482 tc->tc_handle = id;
483 tc->ce_mask |= TCA_ATTR_HANDLE;
484 }
485
486 /**
487 * Return identifier of a traffic control object
488 * @arg tc traffic control object
489 */
rtnl_tc_get_handle(struct rtnl_tc * tc)490 uint32_t rtnl_tc_get_handle(struct rtnl_tc *tc)
491 {
492 return tc->tc_handle;
493 }
494
495 /**
496 * Set the parent identifier of a traffic control object
497 * @arg tc traffic control object
498 * @arg parent identifier of parent traffif control object
499 *
500 */
rtnl_tc_set_parent(struct rtnl_tc * tc,uint32_t parent)501 void rtnl_tc_set_parent(struct rtnl_tc *tc, uint32_t parent)
502 {
503 tc->tc_parent = parent;
504 tc->ce_mask |= TCA_ATTR_PARENT;
505 }
506
507 /**
508 * Return parent identifier of a traffic control object
509 * @arg tc traffic control object
510 */
rtnl_tc_get_parent(struct rtnl_tc * tc)511 uint32_t rtnl_tc_get_parent(struct rtnl_tc *tc)
512 {
513 return tc->tc_parent;
514 }
515
516 /**
517 * Define the type of traffic control object
518 * @arg tc traffic control object
519 * @arg kind name of the tc object type
520 *
521 * @return 0 on success or a negative error code
522 */
rtnl_tc_set_kind(struct rtnl_tc * tc,const char * kind)523 int rtnl_tc_set_kind(struct rtnl_tc *tc, const char *kind)
524 {
525 if (tc->ce_mask & TCA_ATTR_KIND)
526 return -NLE_EXIST;
527
528 if ( !kind
529 || strlen (kind) >= sizeof (tc->tc_kind))
530 return -NLE_INVAL;
531
532 _nl_strncpy_assert(tc->tc_kind, kind, sizeof(tc->tc_kind));
533
534 tc->ce_mask |= TCA_ATTR_KIND;
535
536 /* Force allocation of data */
537 rtnl_tc_data(tc);
538
539 return 0;
540 }
541
542 /**
543 * Return kind of traffic control object
544 * @arg tc traffic control object
545 *
546 * @return Kind of traffic control object or NULL if not set.
547 */
rtnl_tc_get_kind(struct rtnl_tc * tc)548 char *rtnl_tc_get_kind(struct rtnl_tc *tc)
549 {
550 if (tc->ce_mask & TCA_ATTR_KIND)
551 return tc->tc_kind;
552 else
553 return NULL;
554 }
555
556 /**
557 * Return value of a statistical counter of a traffic control object
558 * @arg tc traffic control object
559 * @arg id identifier of statistical counter
560 *
561 * @return Value of requested statistic counter or 0.
562 */
rtnl_tc_get_stat(struct rtnl_tc * tc,enum rtnl_tc_stat id)563 uint64_t rtnl_tc_get_stat(struct rtnl_tc *tc, enum rtnl_tc_stat id)
564 {
565 if ((unsigned int) id > RTNL_TC_STATS_MAX)
566 return 0;
567
568 return tc->tc_stats[id];
569 }
570
571 /**
572 * Set the chain index of a traffic control object
573 * @arg tc traffic control object
574 * @arg chain chain index of traffic control object
575 *
576 */
rtnl_tc_set_chain(struct rtnl_tc * tc,uint32_t chain)577 void rtnl_tc_set_chain(struct rtnl_tc *tc, uint32_t chain)
578 {
579 tc->tc_chain = chain;
580 tc->ce_mask |= TCA_ATTR_CHAIN;
581 }
582
583 /**
584 * Return chain index of traffic control object
585 * @arg tc traffic control object
586 * @arg out_value output argument.
587 *
588 * @return 0 of the output value was successfully returned, or a negative
589 * error code on failure.
590 */
rtnl_tc_get_chain(struct rtnl_tc * tc,uint32_t * out_value)591 int rtnl_tc_get_chain(struct rtnl_tc *tc, uint32_t *out_value)
592 {
593 if (!(tc->ce_mask & TCA_ATTR_CHAIN))
594 return -NLE_MISSING_ATTR;
595 *out_value = tc->tc_chain;
596 return 0;
597 }
598
599 /** @} */
600
601 /**
602 * @name Utilities
603 * @{
604 */
605
606 static const struct trans_tbl tc_stats[] = {
607 __ADD(RTNL_TC_PACKETS, packets),
608 __ADD(RTNL_TC_BYTES, bytes),
609 __ADD(RTNL_TC_RATE_BPS, rate_bps),
610 __ADD(RTNL_TC_RATE_PPS, rate_pps),
611 __ADD(RTNL_TC_QLEN, qlen),
612 __ADD(RTNL_TC_BACKLOG, backlog),
613 __ADD(RTNL_TC_DROPS, drops),
614 __ADD(RTNL_TC_REQUEUES, requeues),
615 __ADD(RTNL_TC_OVERLIMITS, overlimits),
616 };
617
rtnl_tc_stat2str(enum rtnl_tc_stat st,char * buf,size_t len)618 char *rtnl_tc_stat2str(enum rtnl_tc_stat st, char *buf, size_t len)
619 {
620 return __type2str(st, buf, len, tc_stats, ARRAY_SIZE(tc_stats));
621 }
622
rtnl_tc_str2stat(const char * name)623 int rtnl_tc_str2stat(const char *name)
624 {
625 return __str2type(name, tc_stats, ARRAY_SIZE(tc_stats));
626 }
627
628 /**
629 * Calculate time required to transmit buffer at a specific rate
630 * @arg bufsize Size of buffer to be transmited in bytes.
631 * @arg rate Transmit rate in bytes per second.
632 *
633 * Calculates the number of micro seconds required to transmit a
634 * specific buffer at a specific transmit rate.
635 *
636 * @f[
637 * txtime=\frac{bufsize}{rate}10^6
638 * @f]
639 *
640 * @return Required transmit time in micro seconds.
641 */
rtnl_tc_calc_txtime(int bufsize,int rate)642 int rtnl_tc_calc_txtime(int bufsize, int rate)
643 {
644 return ((double) bufsize / (double) rate) * 1000000.0;
645 }
646
647 /**
648 * Calculate buffer size able to transmit in a specific time and rate.
649 * @arg txtime Available transmit time in micro seconds.
650 * @arg rate Transmit rate in bytes per second.
651 *
652 * Calculates the size of the buffer that can be transmitted in a
653 * specific time period at a specific transmit rate.
654 *
655 * @f[
656 * bufsize=\frac{{txtime} \times {rate}}{10^6}
657 * @f]
658 *
659 * @return Size of buffer in bytes.
660 */
rtnl_tc_calc_bufsize(int txtime,int rate)661 int rtnl_tc_calc_bufsize(int txtime, int rate)
662 {
663 return ((double) txtime * (double) rate) / 1000000.0;
664 }
665
666 /**
667 * Calculate the binary logarithm for a specific cell size
668 * @arg cell_size Size of cell, must be a power of two.
669 * @return Binary logirhtm of cell size or a negative error code.
670 */
rtnl_tc_calc_cell_log(int cell_size)671 int rtnl_tc_calc_cell_log(int cell_size)
672 {
673 int i;
674
675 for (i = 0; i < 32; i++)
676 if ((1 << i) == cell_size)
677 return i;
678
679 return -NLE_INVAL;
680 }
681
682
683 /** @} */
684
685 /**
686 * @name Rate Tables
687 * @{
688 */
689
690 /*
691 * COPYRIGHT NOTE:
692 * align_to_atm() and adjust_size() derived/coped from iproute2 source.
693 */
694
695 /*
696 * The align to ATM cells is used for determining the (ATM) SAR
697 * alignment overhead at the ATM layer. (SAR = Segmentation And
698 * Reassembly). This is for example needed when scheduling packet on
699 * an ADSL connection. Note that the extra ATM-AAL overhead is _not_
700 * included in this calculation. This overhead is added in the kernel
701 * before doing the rate table lookup, as this gives better precision
702 * (as the table will always be aligned for 48 bytes).
703 * --Hawk, d.7/11-2004. <hawk@diku.dk>
704 */
align_to_atm(unsigned int size)705 static unsigned int align_to_atm(unsigned int size)
706 {
707 int linksize, cells;
708 cells = size / ATM_CELL_PAYLOAD;
709 if ((size % ATM_CELL_PAYLOAD) > 0)
710 cells++;
711
712 linksize = cells * ATM_CELL_SIZE; /* Use full cell size to add ATM tax */
713 return linksize;
714 }
715
adjust_size(unsigned int size,unsigned int mpu,uint32_t linktype)716 static unsigned int adjust_size(unsigned int size, unsigned int mpu,
717 uint32_t linktype)
718 {
719 if (size < mpu)
720 size = mpu;
721
722 switch (linktype) {
723 case ARPHRD_ATM:
724 return align_to_atm(size);
725
726 case ARPHRD_ETHER:
727 default:
728 return size;
729 }
730 }
731
732 /**
733 * Compute a transmission time lookup table
734 * @arg tc traffic control object
735 * @arg spec Rate specification
736 * @arg dst Destination buffer of RTNL_TC_RTABLE_SIZE uint32_t[].
737 *
738 * Computes a table of RTNL_TC_RTABLE_SIZE entries specyfing the
739 * transmission times for various packet sizes, e.g. the transmission
740 * time for a packet of size \c pktsize could be looked up:
741 * @code
742 * txtime = table[pktsize >> log2(mtu)];
743 * @endcode
744 */
rtnl_tc_build_rate_table(struct rtnl_tc * tc,struct rtnl_ratespec * spec,uint32_t * dst)745 int rtnl_tc_build_rate_table(struct rtnl_tc *tc, struct rtnl_ratespec *spec,
746 uint32_t *dst)
747 {
748 uint32_t mtu = rtnl_tc_get_mtu(tc);
749 uint32_t linktype = rtnl_tc_get_linktype(tc);
750 uint8_t cell_log = spec->rs_cell_log;
751 unsigned int size, i;
752
753 spec->rs_mpu = rtnl_tc_get_mpu(tc);
754 spec->rs_overhead = rtnl_tc_get_overhead(tc);
755
756 if (mtu == 0)
757 mtu = 2047;
758
759 if (cell_log == UINT8_MAX) {
760 /*
761 * cell_log not specified, calculate it. It has to specify the
762 * minimum number of rshifts required to break the MTU to below
763 * RTNL_TC_RTABLE_SIZE.
764 */
765 cell_log = 0;
766 while ((mtu >> cell_log) >= RTNL_TC_RTABLE_SIZE)
767 cell_log++;
768 }
769
770 for (i = 0; i < RTNL_TC_RTABLE_SIZE; i++) {
771 size = adjust_size((i + 1) << cell_log, spec->rs_mpu, linktype);
772 dst[i] = nl_us2ticks(rtnl_tc_calc_txtime64(size, spec->rs_rate64));
773 }
774
775 spec->rs_cell_align = -1;
776 spec->rs_cell_log = cell_log;
777
778 return 0;
779 }
780
781 /** @} */
782
783 /**
784 * @name TC implementation of cache functions
785 */
786
rtnl_tc_free_data(struct nl_object * obj)787 void rtnl_tc_free_data(struct nl_object *obj)
788 {
789 struct rtnl_tc *tc = TC_CAST(obj);
790 struct rtnl_tc_ops *ops;
791
792 rtnl_link_put(tc->tc_link);
793 nl_data_free(tc->tc_opts);
794 nl_data_free(tc->tc_xstats);
795
796 if (tc->tc_subdata) {
797 ops = rtnl_tc_get_ops(tc);
798 if (ops && ops->to_free_data)
799 ops->to_free_data(tc, nl_data_get(tc->tc_subdata));
800
801 nl_data_free(tc->tc_subdata);
802 }
803 }
804
rtnl_tc_clone(struct nl_object * dstobj,struct nl_object * srcobj)805 int rtnl_tc_clone(struct nl_object *dstobj, struct nl_object *srcobj)
806 {
807 struct rtnl_tc *dst = TC_CAST(dstobj);
808 struct rtnl_tc *src = TC_CAST(srcobj);
809 struct rtnl_tc_ops *ops;
810
811 dst->tc_opts = NULL;
812 dst->tc_xstats = NULL;
813 dst->tc_subdata = NULL;
814 dst->tc_link = NULL;
815 dst->tc_ops = NULL;
816
817 if (src->tc_link) {
818 nl_object_get(OBJ_CAST(src->tc_link));
819 dst->tc_link = src->tc_link;
820 }
821
822 dst->ce_mask &= ~(TCA_ATTR_OPTS |
823 TCA_ATTR_XSTATS);
824
825 if (src->tc_opts) {
826 dst->tc_opts = nl_data_clone(src->tc_opts);
827 if (!dst->tc_opts)
828 return -NLE_NOMEM;
829 dst->ce_mask |= TCA_ATTR_OPTS;
830 }
831
832 if (src->tc_xstats) {
833 dst->tc_xstats = nl_data_clone(src->tc_xstats);
834 if (!dst->tc_xstats)
835 return -NLE_NOMEM;
836 dst->ce_mask |= TCA_ATTR_XSTATS;
837 }
838
839 if (src->tc_subdata) {
840 if (!(dst->tc_subdata = nl_data_clone(src->tc_subdata))) {
841 return -NLE_NOMEM;
842 }
843
844 /* Warning: if the data contains pointer, then at this point, dst->tc_subdata
845 * will alias those pointers.
846 *
847 * ops->to_clone() MUST fix that.
848 *
849 * If the type is actually "struct rtnl_act", then to_clone() must also
850 * fix dangling "a_next" pointer. */
851
852 ops = rtnl_tc_get_ops(src);
853 if (ops && ops->to_clone) {
854 return ops->to_clone(rtnl_tc_data(dst), rtnl_tc_data(src));
855 }
856 }
857
858 return 0;
859 }
860
tc_dump(struct rtnl_tc * tc,enum nl_dump_type type,struct nl_dump_params * p)861 static int tc_dump(struct rtnl_tc *tc, enum nl_dump_type type,
862 struct nl_dump_params *p)
863 {
864 struct rtnl_tc_type_ops *type_ops;
865 struct rtnl_tc_ops *ops;
866 void *data = rtnl_tc_data(tc);
867
868 type_ops = tc_type_ops[tc->tc_type];
869 if (type_ops && type_ops->tt_dump[type])
870 type_ops->tt_dump[type](tc, p);
871
872 ops = rtnl_tc_get_ops(tc);
873 if (ops && ops->to_dump[type]) {
874 ops->to_dump[type](tc, data, p);
875 return 1;
876 }
877
878 return 0;
879 }
880
rtnl_tc_dump_line(struct nl_object * obj,struct nl_dump_params * p)881 void rtnl_tc_dump_line(struct nl_object *obj, struct nl_dump_params *p)
882 {
883 struct rtnl_tc_type_ops *type_ops;
884 struct rtnl_tc *tc = TC_CAST(obj);
885 struct nl_cache *link_cache;
886 char buf[32];
887
888 nl_new_line(p);
889
890 type_ops = tc_type_ops[tc->tc_type];
891 if (type_ops && type_ops->tt_dump_prefix)
892 nl_dump(p, "%s ", type_ops->tt_dump_prefix);
893
894 nl_dump(p, "%s ", tc->tc_kind);
895
896 if ((link_cache = nl_cache_mngt_require_safe("route/link"))) {
897 nl_dump(p, "dev %s ",
898 rtnl_link_i2name(link_cache, tc->tc_ifindex,
899 buf, sizeof(buf)));
900 } else
901 nl_dump(p, "dev %u ", tc->tc_ifindex);
902
903 nl_dump(p, "id %s ",
904 rtnl_tc_handle2str(tc->tc_handle, buf, sizeof(buf)));
905
906 nl_dump(p, "parent %s",
907 rtnl_tc_handle2str(tc->tc_parent, buf, sizeof(buf)));
908
909 tc_dump(tc, NL_DUMP_LINE, p);
910 nl_dump(p, "\n");
911
912 if (link_cache)
913 nl_cache_put(link_cache);
914 }
915
rtnl_tc_dump_details(struct nl_object * obj,struct nl_dump_params * p)916 void rtnl_tc_dump_details(struct nl_object *obj, struct nl_dump_params *p)
917 {
918 struct rtnl_tc *tc = TC_CAST(obj);
919
920 rtnl_tc_dump_line(OBJ_CAST(tc), p);
921
922 nl_dump_line(p, " ");
923
924 if (tc->ce_mask & TCA_ATTR_MTU)
925 nl_dump(p, " mtu %u", tc->tc_mtu);
926
927 if (tc->ce_mask & TCA_ATTR_MPU)
928 nl_dump(p, " mpu %u", tc->tc_mpu);
929
930 if (tc->ce_mask & TCA_ATTR_OVERHEAD)
931 nl_dump(p, " overhead %u", tc->tc_overhead);
932
933 if (!tc_dump(tc, NL_DUMP_DETAILS, p))
934 nl_dump(p, "no options");
935 nl_dump(p, "\n");
936 }
937
rtnl_tc_dump_stats(struct nl_object * obj,struct nl_dump_params * p)938 void rtnl_tc_dump_stats(struct nl_object *obj, struct nl_dump_params *p)
939 {
940 struct rtnl_tc *tc = TC_CAST(obj);
941 char *unit;
942 float res;
943
944 rtnl_tc_dump_details(OBJ_CAST(tc), p);
945
946 nl_dump_line(p,
947 " stats: %-14s %-10s %-10s %-10s %-10s %-10s\n",
948 "bytes", "packets", "drops", "overlimits", "qlen", "backlog");
949
950 res = nl_cancel_down_bytes(tc->tc_stats[RTNL_TC_BYTES], &unit);
951
952 nl_dump_line(
953 p,
954 " %10.2f %3s %10llu %-10llu %-10llu %-10llu %-10llu\n",
955 res, unit, (long long unsigned)tc->tc_stats[RTNL_TC_PACKETS],
956 (long long unsigned)tc->tc_stats[RTNL_TC_DROPS],
957 (long long unsigned)tc->tc_stats[RTNL_TC_OVERLIMITS],
958 (long long unsigned)tc->tc_stats[RTNL_TC_QLEN],
959 (long long unsigned)tc->tc_stats[RTNL_TC_BACKLOG]);
960
961 res = nl_cancel_down_bytes(tc->tc_stats[RTNL_TC_RATE_BPS], &unit);
962
963 nl_dump_line(p, " %10.2f %3s/s %10llu/s\n", res, unit,
964 (long long unsigned)tc->tc_stats[RTNL_TC_RATE_PPS]);
965 }
966
rtnl_tc_compare(struct nl_object * aobj,struct nl_object * bobj,uint64_t attrs,int flags)967 uint64_t rtnl_tc_compare(struct nl_object *aobj, struct nl_object *bobj,
968 uint64_t attrs, int flags)
969 {
970 struct rtnl_tc *a = TC_CAST(aobj);
971 struct rtnl_tc *b = TC_CAST(bobj);
972 uint64_t diff = 0;
973
974 #define TC_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, TCA_ATTR_##ATTR, a, b, EXPR)
975
976 diff |= TC_DIFF(HANDLE, a->tc_handle != b->tc_handle);
977 diff |= TC_DIFF(PARENT, a->tc_parent != b->tc_parent);
978 diff |= TC_DIFF(IFINDEX, a->tc_ifindex != b->tc_ifindex);
979 diff |= TC_DIFF(KIND, strcmp(a->tc_kind, b->tc_kind));
980
981 #undef TC_DIFF
982
983 return diff;
984 }
985
986 /** @} */
987
988 /**
989 * @name Modules API
990 */
991
rtnl_tc_lookup_ops(enum rtnl_tc_type type,const char * kind)992 struct rtnl_tc_ops *rtnl_tc_lookup_ops(enum rtnl_tc_type type, const char *kind)
993 {
994 struct rtnl_tc_ops *ops;
995
996 nl_list_for_each_entry(ops, &tc_ops_list[type], to_list)
997 if (!strcmp(kind, ops->to_kind))
998 return ops;
999
1000 return NULL;
1001 }
1002
rtnl_tc_get_ops(struct rtnl_tc * tc)1003 struct rtnl_tc_ops *rtnl_tc_get_ops(struct rtnl_tc *tc)
1004 {
1005 if (!tc->tc_ops)
1006 tc->tc_ops = rtnl_tc_lookup_ops(tc->tc_type, tc->tc_kind);
1007
1008 return tc->tc_ops;
1009 }
1010
1011 /**
1012 * Register a traffic control module
1013 * @arg ops traffic control module operations
1014 */
rtnl_tc_register(struct rtnl_tc_ops * ops)1015 int rtnl_tc_register(struct rtnl_tc_ops *ops)
1016 {
1017 static int init = 0;
1018
1019 /*
1020 * Initialiation hack, make sure list is initialized when
1021 * the first tc module registers. Putting this in a
1022 * separate __init would required correct ordering of init
1023 * functions
1024 */
1025 if (!init) {
1026 int i;
1027
1028 for (i = 0; i < __RTNL_TC_TYPE_MAX; i++)
1029 nl_init_list_head(&tc_ops_list[i]);
1030
1031 init = 1;
1032 }
1033
1034 if (!ops->to_kind || ops->to_type > RTNL_TC_TYPE_MAX)
1035 BUG();
1036
1037 if (rtnl_tc_lookup_ops(ops->to_type, ops->to_kind))
1038 return -NLE_EXIST;
1039
1040 nl_list_add_tail(&ops->to_list, &tc_ops_list[ops->to_type]);
1041
1042 return 0;
1043 }
1044
1045 /**
1046 * Unregister a traffic control module
1047 * @arg ops traffic control module operations
1048 */
rtnl_tc_unregister(struct rtnl_tc_ops * ops)1049 void rtnl_tc_unregister(struct rtnl_tc_ops *ops)
1050 {
1051 nl_list_del(&ops->to_list);
1052 }
1053
1054 /**
1055 * Returns the private data of the traffic control object.
1056 * Contrary to rtnl_tc_data(), this returns NULL if the data is
1057 * not yet allocated
1058 * @arg tc traffic control object
1059 *
1060 * @return pointer to the private data or NULL if not allocated.
1061 */
rtnl_tc_data_peek(struct rtnl_tc * tc)1062 void *rtnl_tc_data_peek(struct rtnl_tc *tc)
1063 {
1064 return tc->tc_subdata ? nl_data_get(tc->tc_subdata) : NULL;
1065 }
1066
1067 /**
1068 * Return pointer to private data of traffic control object
1069 * @arg tc traffic control object
1070 *
1071 * Allocates the private traffic control object data section
1072 * as necessary and returns it.
1073 *
1074 * @return Pointer to private tc data or NULL if allocation failed.
1075 */
rtnl_tc_data(struct rtnl_tc * tc)1076 void *rtnl_tc_data(struct rtnl_tc *tc)
1077 {
1078 if (!tc->tc_subdata) {
1079 size_t size;
1080
1081 if (!tc->tc_ops) {
1082 if (!rtnl_tc_get_ops(tc))
1083 return NULL;
1084 }
1085
1086 if (!(size = tc->tc_ops->to_size))
1087 BUG();
1088
1089 if (!(tc->tc_subdata = nl_data_alloc(NULL, size)))
1090 return NULL;
1091 }
1092
1093 return nl_data_get(tc->tc_subdata);
1094 }
1095
1096 /**
1097 * Check traffic control object type and return private data section
1098 * @arg tc traffic control object
1099 * @arg ops expected traffic control object operations
1100 * @arg err the place where saves the error code if fails
1101 *
1102 * Checks whether the traffic control object matches the type
1103 * specified with the traffic control object operations. If the
1104 * type matches, the private tc object data is returned. If type
1105 * mismatches, APPBUG() will print a application bug warning.
1106 *
1107 * @see rtnl_tc_data()
1108 *
1109 * @return Pointer to private tc data or NULL if type mismatches.
1110 */
rtnl_tc_data_check(struct rtnl_tc * tc,struct rtnl_tc_ops * ops,int * err)1111 void *rtnl_tc_data_check(struct rtnl_tc *tc, struct rtnl_tc_ops *ops, int *err)
1112 {
1113 void *ret;
1114
1115 if (tc->tc_ops != ops) {
1116 char buf[64];
1117
1118 snprintf(buf, sizeof(buf),
1119 "tc object %p used in %s context but is of type %s",
1120 tc, ops->to_kind, tc->tc_ops->to_kind);
1121 APPBUG(buf);
1122
1123 if (err)
1124 *err = -NLE_OPNOTSUPP;
1125 return NULL;
1126 }
1127
1128 ret = rtnl_tc_data(tc);
1129 if (ret == NULL) {
1130 if (err)
1131 *err = -NLE_NOMEM;
1132 }
1133
1134 return ret;
1135 }
1136
1137 struct nl_af_group tc_groups[] = {
1138 { AF_UNSPEC, RTNLGRP_TC },
1139 { END_OF_GROUP_LIST },
1140 };
1141
1142
rtnl_tc_type_register(struct rtnl_tc_type_ops * ops)1143 void rtnl_tc_type_register(struct rtnl_tc_type_ops *ops)
1144 {
1145 if (ops->tt_type > RTNL_TC_TYPE_MAX)
1146 BUG();
1147
1148 tc_type_ops[ops->tt_type] = ops;
1149 }
1150
rtnl_tc_type_unregister(struct rtnl_tc_type_ops * ops)1151 void rtnl_tc_type_unregister(struct rtnl_tc_type_ops *ops)
1152 {
1153 if (ops->tt_type > RTNL_TC_TYPE_MAX)
1154 BUG();
1155
1156 tc_type_ops[ops->tt_type] = NULL;
1157 }
1158
1159 /** @} */
1160
1161 /** @} */
1162