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