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