libnl  3.2.14
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-local.h>
19 #include <netlink-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/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  } else
141  goto compat_xstats;
142  } else {
143  if (tb[TCA_STATS]) {
144  struct tc_stats *st = nla_data(tb[TCA_STATS]);
145 
146  tc->tc_stats[RTNL_TC_BYTES] = st->bytes;
147  tc->tc_stats[RTNL_TC_PACKETS] = st->packets;
148  tc->tc_stats[RTNL_TC_RATE_BPS] = st->bps;
149  tc->tc_stats[RTNL_TC_RATE_PPS] = st->pps;
150  tc->tc_stats[RTNL_TC_QLEN] = st->qlen;
151  tc->tc_stats[RTNL_TC_BACKLOG] = st->backlog;
152  tc->tc_stats[RTNL_TC_DROPS] = st->drops;
153  tc->tc_stats[RTNL_TC_OVERLIMITS]= st->overlimits;
154 
155  tc->ce_mask |= TCA_ATTR_STATS;
156  }
157 
158 compat_xstats:
159  if (tb[TCA_XSTATS]) {
160  tc->tc_xstats = nl_data_alloc_attr(tb[TCA_XSTATS]);
161  if (tc->tc_xstats == NULL)
162  return -NLE_NOMEM;
163  tc->ce_mask |= TCA_ATTR_XSTATS;
164  }
165  }
166 
167  ops = rtnl_tc_get_ops(tc);
168  if (ops && ops->to_msg_parser) {
169  void *data = rtnl_tc_data(tc);
170 
171  if (!data)
172  return -NLE_NOMEM;
173 
174  err = ops->to_msg_parser(tc, data);
175  if (err < 0)
176  return err;
177  }
178 
179  if ((link_cache = __nl_cache_mngt_require("route/link"))) {
180  struct rtnl_link *link;
181 
182  if ((link = rtnl_link_get(link_cache, tc->tc_ifindex))) {
183  rtnl_tc_set_link(tc, link);
184 
185  /* rtnl_tc_set_link incs refcnt */
186  rtnl_link_put(link);
187  }
188  }
189 
190  return 0;
191 }
192 
193 int rtnl_tc_msg_build(struct rtnl_tc *tc, int type, int flags,
194  struct nl_msg **result)
195 {
196  struct nl_msg *msg;
197  struct rtnl_tc_ops *ops;
198  struct tcmsg tchdr = {
199  .tcm_family = AF_UNSPEC,
200  .tcm_ifindex = tc->tc_ifindex,
201  .tcm_handle = tc->tc_handle,
202  .tcm_parent = tc->tc_parent,
203  };
204  int err = -NLE_MSGSIZE;
205 
206  msg = nlmsg_alloc_simple(type, flags);
207  if (!msg)
208  return -NLE_NOMEM;
209 
210  if (nlmsg_append(msg, &tchdr, sizeof(tchdr), NLMSG_ALIGNTO) < 0)
211  goto nla_put_failure;
212 
213  if (tc->ce_mask & TCA_ATTR_KIND)
214  NLA_PUT_STRING(msg, TCA_KIND, tc->tc_kind);
215 
216  ops = rtnl_tc_get_ops(tc);
217  if (ops && ops->to_msg_fill) {
218  struct nlattr *opts;
219  void *data = rtnl_tc_data(tc);
220 
221  if (!(opts = nla_nest_start(msg, TCA_OPTIONS)))
222  goto nla_put_failure;
223 
224  if ((err = ops->to_msg_fill(tc, data, msg)) < 0)
225  goto nla_put_failure;
226 
227  nla_nest_end(msg, opts);
228  }
229 
230  *result = msg;
231  return 0;
232 
233 nla_put_failure:
234  nlmsg_free(msg);
235  return err;
236 }
237 
238 void tca_set_kind(struct rtnl_tc *t, const char *kind)
239 {
240  strncpy(t->tc_kind, kind, sizeof(t->tc_kind) - 1);
241  t->ce_mask |= TCA_ATTR_KIND;
242 }
243 
244 
245 /** @endcond */
246 
247 /**
248  * @name Attributes
249  * @{
250  */
251 
252 /**
253  * Set interface index of traffic control object
254  * @arg tc traffic control object
255  * @arg ifindex interface index.
256  *
257  * Sets the interface index of a traffic control object. The interface
258  * index defines the network device which this tc object is attached to.
259  * This function will overwrite any network device assigned with previous
260  * calls to rtnl_tc_set_ifindex() or rtnl_tc_set_link().
261  */
262 void rtnl_tc_set_ifindex(struct rtnl_tc *tc, int ifindex)
263 {
264  /* Obsolete possible old link reference */
265  rtnl_link_put(tc->tc_link);
266  tc->tc_link = NULL;
267  tc->ce_mask &= ~TCA_ATTR_LINK;
268 
269  tc->tc_ifindex = ifindex;
270  tc->ce_mask |= TCA_ATTR_IFINDEX;
271 }
272 
273 /**
274  * Return interface index of traffic control object
275  * @arg tc traffic control object
276  */
277 int rtnl_tc_get_ifindex(struct rtnl_tc *tc)
278 {
279  return tc->tc_ifindex;
280 }
281 
282 /**
283  * Set link of traffic control object
284  * @arg tc traffic control object
285  * @arg link link object
286  *
287  * Sets the link of a traffic control object. This function serves
288  * the same purpose as rtnl_tc_set_ifindex() but due to the continued
289  * allowed access to the link object it gives it the possibility to
290  * retrieve sane default values for the the MTU and the linktype.
291  * Always prefer this function over rtnl_tc_set_ifindex() if you can
292  * spare to have an additional link object around.
293  */
294 void rtnl_tc_set_link(struct rtnl_tc *tc, struct rtnl_link *link)
295 {
296  rtnl_link_put(tc->tc_link);
297 
298  if (!link)
299  return;
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 (id < 0 || 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 /**
562  * Calculate time required to transmit buffer at a specific rate
563  * @arg bufsize Size of buffer to be transmited in bytes.
564  * @arg rate Transmit rate in bytes per second.
565  *
566  * Calculates the number of micro seconds required to transmit a
567  * specific buffer at a specific transmit rate.
568  *
569  * @f[
570  * txtime=\frac{bufsize}{rate}10^6
571  * @f]
572  *
573  * @return Required transmit time in micro seconds.
574  */
575 int rtnl_tc_calc_txtime(int bufsize, int rate)
576 {
577  double tx_time_secs;
578 
579  tx_time_secs = (double) bufsize / (double) rate;
580 
581  return tx_time_secs * 1000000.;
582 }
583 
584 /**
585  * Calculate buffer size able to transmit in a specific time and rate.
586  * @arg txtime Available transmit time in micro seconds.
587  * @arg rate Transmit rate in bytes per second.
588  *
589  * Calculates the size of the buffer that can be transmitted in a
590  * specific time period at a specific transmit rate.
591  *
592  * @f[
593  * bufsize=\frac{{txtime} \times {rate}}{10^6}
594  * @f]
595  *
596  * @return Size of buffer in bytes.
597  */
598 int rtnl_tc_calc_bufsize(int txtime, int rate)
599 {
600  double bufsize;
601 
602  bufsize = (double) txtime * (double) rate;
603 
604  return bufsize / 1000000.;
605 }
606 
607 /**
608  * Calculate the binary logarithm for a specific cell size
609  * @arg cell_size Size of cell, must be a power of two.
610  * @return Binary logirhtm of cell size or a negative error code.
611  */
612 int rtnl_tc_calc_cell_log(int cell_size)
613 {
614  int i;
615 
616  for (i = 0; i < 32; i++)
617  if ((1 << i) == cell_size)
618  return i;
619 
620  return -NLE_INVAL;
621 }
622 
623 
624 /** @} */
625 
626 /**
627  * @name Rate Tables
628  * @{
629  */
630 
631 /*
632  * COPYRIGHT NOTE:
633  * align_to_atm() and adjust_size() derived/coped from iproute2 source.
634  */
635 
636 /*
637  * The align to ATM cells is used for determining the (ATM) SAR
638  * alignment overhead at the ATM layer. (SAR = Segmentation And
639  * Reassembly). This is for example needed when scheduling packet on
640  * an ADSL connection. Note that the extra ATM-AAL overhead is _not_
641  * included in this calculation. This overhead is added in the kernel
642  * before doing the rate table lookup, as this gives better precision
643  * (as the table will always be aligned for 48 bytes).
644  * --Hawk, d.7/11-2004. <hawk@diku.dk>
645  */
646 static unsigned int align_to_atm(unsigned int size)
647 {
648  int linksize, cells;
649  cells = size / ATM_CELL_PAYLOAD;
650  if ((size % ATM_CELL_PAYLOAD) > 0)
651  cells++;
652 
653  linksize = cells * ATM_CELL_SIZE; /* Use full cell size to add ATM tax */
654  return linksize;
655 }
656 
657 static unsigned int adjust_size(unsigned int size, unsigned int mpu,
658  uint32_t linktype)
659 {
660  if (size < mpu)
661  size = mpu;
662 
663  switch (linktype) {
664  case ARPHRD_ATM:
665  return align_to_atm(size);
666 
667  case ARPHRD_ETHER:
668  default:
669  return size;
670  }
671 }
672 
673 /**
674  * Compute a transmission time lookup table
675  * @arg tc traffic control object
676  * @arg spec Rate specification
677  * @arg dst Destination buffer of RTNL_TC_RTABLE_SIZE uint32_t[].
678  *
679  * Computes a table of RTNL_TC_RTABLE_SIZE entries specyfing the
680  * transmission times for various packet sizes, e.g. the transmission
681  * time for a packet of size \c pktsize could be looked up:
682  * @code
683  * txtime = table[pktsize >> log2(mtu)];
684  * @endcode
685  */
686 int rtnl_tc_build_rate_table(struct rtnl_tc *tc, struct rtnl_ratespec *spec,
687  uint32_t *dst)
688 {
689  uint32_t mtu = rtnl_tc_get_mtu(tc);
690  uint32_t linktype = rtnl_tc_get_linktype(tc);
691  uint8_t cell_log = spec->rs_cell_log;
692  unsigned int size, i;
693 
694  spec->rs_mpu = rtnl_tc_get_mpu(tc);
695  spec->rs_overhead = rtnl_tc_get_overhead(tc);
696 
697  if (mtu == 0)
698  mtu = 2047;
699 
700  if (cell_log == UINT8_MAX) {
701  /*
702  * cell_log not specified, calculate it. It has to specify the
703  * minimum number of rshifts required to break the MTU to below
704  * RTNL_TC_RTABLE_SIZE.
705  */
706  cell_log = 0;
707  while ((mtu >> cell_log) >= RTNL_TC_RTABLE_SIZE)
708  cell_log++;
709  }
710 
711  for (i = 0; i < RTNL_TC_RTABLE_SIZE; i++) {
712  size = adjust_size((i + 1) << cell_log, spec->rs_mpu, linktype);
713  dst[i] = nl_us2ticks(rtnl_tc_calc_txtime(size, spec->rs_rate));
714  }
715 
716  spec->rs_cell_align = -1;
717  spec->rs_cell_log = cell_log;
718 
719  return 0;
720 }
721 
722 /** @} */
723 
724 /**
725  * @name TC implementation of cache functions
726  */
727 
728 void rtnl_tc_free_data(struct nl_object *obj)
729 {
730  struct rtnl_tc *tc = TC_CAST(obj);
731  struct rtnl_tc_ops *ops;
732 
733  rtnl_link_put(tc->tc_link);
734  nl_data_free(tc->tc_opts);
735  nl_data_free(tc->tc_xstats);
736 
737  if (tc->tc_subdata) {
738  ops = rtnl_tc_get_ops(tc);
739  if (ops && ops->to_free_data)
740  ops->to_free_data(tc, nl_data_get(tc->tc_subdata));
741 
742  nl_data_free(tc->tc_subdata);
743  }
744 }
745 
746 int rtnl_tc_clone(struct nl_object *dstobj, struct nl_object *srcobj)
747 {
748  struct rtnl_tc *dst = TC_CAST(dstobj);
749  struct rtnl_tc *src = TC_CAST(srcobj);
750  struct rtnl_tc_ops *ops;
751 
752  if (src->tc_link) {
753  nl_object_get(OBJ_CAST(src->tc_link));
754  dst->tc_link = src->tc_link;
755  }
756 
757  if (src->tc_opts) {
758  dst->tc_opts = nl_data_clone(src->tc_opts);
759  if (!dst->tc_opts)
760  return -NLE_NOMEM;
761  }
762 
763  if (src->tc_xstats) {
764  dst->tc_xstats = nl_data_clone(src->tc_xstats);
765  if (!dst->tc_xstats)
766  return -NLE_NOMEM;
767  }
768 
769  if (src->tc_subdata) {
770  if (!(dst->tc_subdata = nl_data_clone(src->tc_subdata))) {
771  return -NLE_NOMEM;
772  }
773  }
774 
775  ops = rtnl_tc_get_ops(src);
776  if (ops && ops->to_clone) {
777  void *a = rtnl_tc_data(dst), *b = rtnl_tc_data(src);
778 
779  if (!a)
780  return 0;
781  else if (!b)
782  return -NLE_NOMEM;
783 
784  return ops->to_clone(a, b);
785  }
786 
787  return 0;
788 }
789 
790 static int tc_dump(struct rtnl_tc *tc, enum nl_dump_type type,
791  struct nl_dump_params *p)
792 {
793  struct rtnl_tc_type_ops *type_ops;
794  struct rtnl_tc_ops *ops;
795  void *data = rtnl_tc_data(tc);
796 
797  type_ops = tc_type_ops[tc->tc_type];
798  if (type_ops && type_ops->tt_dump[type])
799  type_ops->tt_dump[type](tc, p);
800 
801  ops = rtnl_tc_get_ops(tc);
802  if (ops && ops->to_dump[type]) {
803  ops->to_dump[type](tc, data, p);
804  return 1;
805  }
806 
807  return 0;
808 }
809 
810 void rtnl_tc_dump_line(struct nl_object *obj, struct nl_dump_params *p)
811 {
812  struct rtnl_tc_type_ops *type_ops;
813  struct rtnl_tc *tc = TC_CAST(obj);
814  struct nl_cache *link_cache;
815  char buf[32];
816 
817  nl_new_line(p);
818 
819  type_ops = tc_type_ops[tc->tc_type];
820  if (type_ops && type_ops->tt_dump_prefix)
821  nl_dump(p, "%s ", type_ops->tt_dump_prefix);
822 
823  nl_dump(p, "%s ", tc->tc_kind);
824 
825  if ((link_cache = nl_cache_mngt_require("route/link"))) {
826  nl_dump(p, "dev %s ",
827  rtnl_link_i2name(link_cache, tc->tc_ifindex,
828  buf, sizeof(buf)));
829  } else
830  nl_dump(p, "dev %u ", tc->tc_ifindex);
831 
832  nl_dump(p, "id %s ",
833  rtnl_tc_handle2str(tc->tc_handle, buf, sizeof(buf)));
834 
835  nl_dump(p, "parent %s",
836  rtnl_tc_handle2str(tc->tc_parent, buf, sizeof(buf)));
837 
838  tc_dump(tc, NL_DUMP_LINE, p);
839  nl_dump(p, "\n");
840 }
841 
842 void rtnl_tc_dump_details(struct nl_object *obj, struct nl_dump_params *p)
843 {
844  struct rtnl_tc *tc = TC_CAST(obj);
845 
846  rtnl_tc_dump_line(OBJ_CAST(tc), p);
847 
848  nl_dump_line(p, " ");
849 
850  if (tc->ce_mask & TCA_ATTR_MTU)
851  nl_dump(p, " mtu %u", tc->tc_mtu);
852 
853  if (tc->ce_mask & TCA_ATTR_MPU)
854  nl_dump(p, " mpu %u", tc->tc_mpu);
855 
856  if (tc->ce_mask & TCA_ATTR_OVERHEAD)
857  nl_dump(p, " overhead %u", tc->tc_overhead);
858 
859  if (!tc_dump(tc, NL_DUMP_DETAILS, p))
860  nl_dump(p, "no options");
861  nl_dump(p, "\n");
862 }
863 
864 void rtnl_tc_dump_stats(struct nl_object *obj, struct nl_dump_params *p)
865 {
866  struct rtnl_tc *tc = TC_CAST(obj);
867  char *unit, fmt[64];
868  float res;
869 
870  rtnl_tc_dump_details(OBJ_CAST(tc), p);
871 
872  strcpy(fmt, " %7.2f %s %10u %10u %10u %10u %10u\n");
873 
874  nl_dump_line(p,
875  " Stats: bytes packets drops overlimits" \
876  " qlen backlog\n");
877 
878  res = nl_cancel_down_bytes(tc->tc_stats[RTNL_TC_BYTES], &unit);
879  if (*unit == 'B')
880  fmt[11] = '9';
881 
882  nl_dump_line(p, fmt, res, unit,
883  tc->tc_stats[RTNL_TC_PACKETS],
884  tc->tc_stats[RTNL_TC_DROPS],
885  tc->tc_stats[RTNL_TC_OVERLIMITS],
886  tc->tc_stats[RTNL_TC_QLEN],
887  tc->tc_stats[RTNL_TC_BACKLOG]);
888 
889  res = nl_cancel_down_bytes(tc->tc_stats[RTNL_TC_RATE_BPS], &unit);
890 
891  strcpy(fmt, " %7.2f %s/s%9u pps");
892 
893  if (*unit == 'B')
894  fmt[11] = '9';
895 
896  nl_dump_line(p, fmt, res, unit, tc->tc_stats[RTNL_TC_RATE_PPS]);
897 
898  tc_dump(tc, NL_DUMP_LINE, p);
899  nl_dump(p, "\n");
900 }
901 
902 int rtnl_tc_compare(struct nl_object *aobj, struct nl_object *bobj,
903  uint32_t attrs, int flags)
904 {
905  struct rtnl_tc *a = TC_CAST(aobj);
906  struct rtnl_tc *b = TC_CAST(bobj);
907  int diff = 0;
908 
909 #define TC_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, TCA_ATTR_##ATTR, a, b, EXPR)
910 
911  diff |= TC_DIFF(HANDLE, a->tc_handle != b->tc_handle);
912  diff |= TC_DIFF(PARENT, a->tc_parent != b->tc_parent);
913  diff |= TC_DIFF(IFINDEX, a->tc_ifindex != b->tc_ifindex);
914  diff |= TC_DIFF(KIND, strcmp(a->tc_kind, b->tc_kind));
915 
916 #undef TC_DIFF
917 
918  return diff;
919 }
920 
921 /** @} */
922 
923 /**
924  * @name Modules API
925  */
926 
927 struct rtnl_tc_ops *rtnl_tc_lookup_ops(enum rtnl_tc_type type, const char *kind)
928 {
929  struct rtnl_tc_ops *ops;
930 
931  nl_list_for_each_entry(ops, &tc_ops_list[type], to_list)
932  if (!strcmp(kind, ops->to_kind))
933  return ops;
934 
935  return NULL;
936 }
937 
938 struct rtnl_tc_ops *rtnl_tc_get_ops(struct rtnl_tc *tc)
939 {
940  if (!tc->tc_ops)
941  tc->tc_ops = rtnl_tc_lookup_ops(tc->tc_type, tc->tc_kind);
942 
943  return tc->tc_ops;
944 }
945 
946 /**
947  * Register a traffic control module
948  * @arg ops traffic control module operations
949  */
951 {
952  static int init = 0;
953 
954  /*
955  * Initialiation hack, make sure list is initialized when
956  * the first tc module registers. Putting this in a
957  * separate __init would required correct ordering of init
958  * functions
959  */
960  if (!init) {
961  int i;
962 
963  for (i = 0; i < __RTNL_TC_TYPE_MAX; i++)
964  nl_init_list_head(&tc_ops_list[i]);
965 
966  init = 1;
967  }
968 
969  if (!ops->to_kind || ops->to_type > RTNL_TC_TYPE_MAX)
970  BUG();
971 
972  if (rtnl_tc_lookup_ops(ops->to_type, ops->to_kind))
973  return -NLE_EXIST;
974 
975  nl_list_add_tail(&ops->to_list, &tc_ops_list[ops->to_type]);
976 
977  return 0;
978 }
979 
980 /**
981  * Unregister a traffic control module
982  * @arg ops traffic control module operations
983  */
985 {
986  nl_list_del(&ops->to_list);
987 }
988 
989 /**
990  * Return pointer to private data of traffic control object
991  * @arg tc traffic control object
992  *
993  * Allocates the private traffic control object data section
994  * as necessary and returns it.
995  *
996  * @return Pointer to private tc data or NULL if allocation failed.
997  */
998 void *rtnl_tc_data(struct rtnl_tc *tc)
999 {
1000  if (!tc->tc_subdata) {
1001  size_t size;
1002 
1003  if (!tc->tc_ops) {
1004  if (!tc->tc_kind)
1005  BUG();
1006 
1007  if (!rtnl_tc_get_ops(tc))
1008  return NULL;
1009  }
1010 
1011  if (!(size = tc->tc_ops->to_size))
1012  BUG();
1013 
1014  if (!(tc->tc_subdata = nl_data_alloc(NULL, size)))
1015  return NULL;
1016  }
1017 
1018  return nl_data_get(tc->tc_subdata);
1019 }
1020 
1021 /**
1022  * Check traffic control object type and return private data section
1023  * @arg tc traffic control object
1024  * @arg ops expected traffic control object operations
1025  *
1026  * Checks whether the traffic control object matches the type
1027  * specified with the traffic control object operations. If the
1028  * type matches, the private tc object data is returned. If type
1029  * mismatches, APPBUG() will print a application bug warning.
1030  *
1031  * @see rtnl_tc_data()
1032  *
1033  * @return Pointer to private tc data or NULL if type mismatches.
1034  */
1035 void *rtnl_tc_data_check(struct rtnl_tc *tc, struct rtnl_tc_ops *ops)
1036 {
1037  if (tc->tc_ops != ops) {
1038  char buf[64];
1039 
1040  snprintf(buf, sizeof(buf),
1041  "tc object %p used in %s context but is of type %s",
1042  tc, ops->to_kind, tc->tc_ops->to_kind);
1043  APPBUG(buf);
1044 
1045  return NULL;
1046  }
1047 
1048  return rtnl_tc_data(tc);
1049 }
1050 
1051 void rtnl_tc_type_register(struct rtnl_tc_type_ops *ops)
1052 {
1053  if (ops->tt_type > RTNL_TC_TYPE_MAX)
1054  BUG();
1055 
1056  tc_type_ops[ops->tt_type] = ops;
1057 }
1058 
1059 void rtnl_tc_type_unregister(struct rtnl_tc_type_ops *ops)
1060 {
1061  if (ops->tt_type > RTNL_TC_TYPE_MAX)
1062  BUG();
1063 
1064  tc_type_ops[ops->tt_type] = NULL;
1065 }
1066 
1067 /** @} */
1068 
1069 /** @} */