libnl 2.0

/build/buildd/libnl2-2.0/lib/route/tc.c

00001 /*
00002  * lib/route/tc.c               Traffic Control
00003  *
00004  *      This library is free software; you can redistribute it and/or
00005  *      modify it under the terms of the GNU Lesser General Public
00006  *      License as published by the Free Software Foundation version 2.1
00007  *      of the License.
00008  *
00009  * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
00010  */
00011 
00012 /**
00013  * @ingroup rtnl
00014  * @defgroup tc Traffic Control
00015  * @brief
00016  * @{
00017  */
00018 
00019 #include <netlink-local.h>
00020 #include <netlink-tc.h>
00021 #include <netlink/netlink.h>
00022 #include <netlink/utils.h>
00023 #include <netlink/route/rtnl.h>
00024 #include <netlink/route/link.h>
00025 #include <netlink/route/tc.h>
00026 
00027 /** @cond SKIP */
00028 
00029 static struct nla_policy tc_policy[TCA_MAX+1] = {
00030         [TCA_KIND]      = { .type = NLA_STRING,
00031                             .maxlen = TCKINDSIZ },
00032         [TCA_STATS]     = { .minlen = sizeof(struct tc_stats) },
00033         [TCA_STATS2]    = { .type = NLA_NESTED },
00034 };
00035 
00036 int tca_parse(struct nlattr **tb, int maxattr, struct rtnl_tca *g,
00037               struct nla_policy *policy)
00038 {
00039         
00040         if (g->ce_mask & TCA_ATTR_OPTS)
00041                 return nla_parse(tb, maxattr,
00042                                  (struct nlattr *) g->tc_opts->d_data,
00043                                  g->tc_opts->d_size, policy);
00044         else {
00045                 /* Ugly but tb[] must be in a defined state even if no
00046                  * attributes can be found. */
00047                 memset(tb, 0, sizeof(struct nlattr *) * (maxattr + 1));
00048                 return 0;
00049         }
00050 }
00051 
00052 static struct nla_policy tc_stats2_policy[TCA_STATS_MAX+1] = {
00053         [TCA_STATS_BASIC]    = { .minlen = sizeof(struct gnet_stats_basic) },
00054         [TCA_STATS_RATE_EST] = { .minlen = sizeof(struct gnet_stats_rate_est) },
00055         [TCA_STATS_QUEUE]    = { .minlen = sizeof(struct gnet_stats_queue) },
00056 };
00057 
00058 int tca_msg_parser(struct nlmsghdr *n, struct rtnl_tca *g)
00059 {
00060         struct nlattr *tb[TCA_MAX + 1];
00061         struct tcmsg *tm;
00062         int err;
00063 
00064         err = nlmsg_parse(n, sizeof(*tm), tb, TCA_MAX, tc_policy);
00065         if (err < 0)
00066                 return err;
00067 
00068         if (tb[TCA_KIND] == NULL)
00069                 return -NLE_MISSING_ATTR;
00070 
00071         nla_strlcpy(g->tc_kind, tb[TCA_KIND], TCKINDSIZ);
00072 
00073         tm = nlmsg_data(n);
00074         g->tc_family  = tm->tcm_family;
00075         g->tc_ifindex = tm->tcm_ifindex;
00076         g->tc_handle  = tm->tcm_handle;
00077         g->tc_parent  = tm->tcm_parent;
00078         g->tc_info    = tm->tcm_info;
00079 
00080         g->ce_mask = (TCA_ATTR_FAMILY | TCA_ATTR_IFINDEX | TCA_ATTR_HANDLE |
00081                       TCA_ATTR_PARENT | TCA_ATTR_INFO | TCA_ATTR_KIND);
00082 
00083         if (tb[TCA_OPTIONS]) {
00084                 g->tc_opts = nl_data_alloc_attr(tb[TCA_OPTIONS]);
00085                 if (!g->tc_opts)
00086                         return -NLE_NOMEM;
00087                 g->ce_mask |= TCA_ATTR_OPTS;
00088         }
00089         
00090 
00091         if (tb[TCA_STATS2]) {
00092                 struct nlattr *tbs[TCA_STATS_MAX + 1];
00093 
00094                 err = nla_parse_nested(tbs, TCA_STATS_MAX, tb[TCA_STATS2],
00095                                        tc_stats2_policy);
00096                 if (err < 0)
00097                         return err;
00098 
00099                 if (tbs[TCA_STATS_BASIC]) {
00100                         struct gnet_stats_basic *bs;
00101                         
00102                         bs = nla_data(tbs[TCA_STATS_BASIC]);
00103                         g->tc_stats[RTNL_TC_BYTES]      = bs->bytes;
00104                         g->tc_stats[RTNL_TC_PACKETS]    = bs->packets;
00105                 }
00106 
00107                 if (tbs[TCA_STATS_RATE_EST]) {
00108                         struct gnet_stats_rate_est *re;
00109 
00110                         re = nla_data(tbs[TCA_STATS_RATE_EST]);
00111                         g->tc_stats[RTNL_TC_RATE_BPS]   = re->bps;
00112                         g->tc_stats[RTNL_TC_RATE_PPS]   = re->pps;
00113                 }
00114                 
00115                 if (tbs[TCA_STATS_QUEUE]) {
00116                         struct gnet_stats_queue *q;
00117 
00118                         q = nla_data(tbs[TCA_STATS_QUEUE]);
00119                         g->tc_stats[RTNL_TC_QLEN]       = q->qlen;
00120                         g->tc_stats[RTNL_TC_BACKLOG]    = q->backlog;
00121                         g->tc_stats[RTNL_TC_DROPS]      = q->drops;
00122                         g->tc_stats[RTNL_TC_REQUEUES]   = q->requeues;
00123                         g->tc_stats[RTNL_TC_OVERLIMITS] = q->overlimits;
00124                 }
00125 
00126                 g->ce_mask |= TCA_ATTR_STATS;
00127                 
00128                 if (tbs[TCA_STATS_APP]) {
00129                         g->tc_xstats = nl_data_alloc_attr(tbs[TCA_STATS_APP]);
00130                         if (g->tc_xstats == NULL)
00131                                 return -NLE_NOMEM;
00132                 } else
00133                         goto compat_xstats;
00134         } else {
00135                 if (tb[TCA_STATS]) {
00136                         struct tc_stats *st = nla_data(tb[TCA_STATS]);
00137 
00138                         g->tc_stats[RTNL_TC_BYTES]      = st->bytes;
00139                         g->tc_stats[RTNL_TC_PACKETS]    = st->packets;
00140                         g->tc_stats[RTNL_TC_RATE_BPS]   = st->bps;
00141                         g->tc_stats[RTNL_TC_RATE_PPS]   = st->pps;
00142                         g->tc_stats[RTNL_TC_QLEN]       = st->qlen;
00143                         g->tc_stats[RTNL_TC_BACKLOG]    = st->backlog;
00144                         g->tc_stats[RTNL_TC_DROPS]      = st->drops;
00145                         g->tc_stats[RTNL_TC_OVERLIMITS] = st->overlimits;
00146 
00147                         g->ce_mask |= TCA_ATTR_STATS;
00148                 }
00149 
00150 compat_xstats:
00151                 if (tb[TCA_XSTATS]) {
00152                         g->tc_xstats = nl_data_alloc_attr(tb[TCA_XSTATS]);
00153                         if (g->tc_xstats == NULL)
00154                                 return -NLE_NOMEM;
00155                         g->ce_mask |= TCA_ATTR_XSTATS;
00156                 }
00157         }
00158 
00159 
00160         return 0;
00161 }
00162 
00163 void tca_free_data(struct rtnl_tca *tca)
00164 {
00165         nl_data_free(tca->tc_opts);
00166         nl_data_free(tca->tc_xstats);
00167 }
00168 
00169 int tca_clone(struct rtnl_tca *dst, struct rtnl_tca *src)
00170 {
00171         if (src->tc_opts) {
00172                 dst->tc_opts = nl_data_clone(src->tc_opts);
00173                 if (!dst->tc_opts)
00174                         return -NLE_NOMEM;
00175         }
00176         
00177         if (src->tc_xstats) {
00178                 dst->tc_xstats = nl_data_clone(src->tc_xstats);
00179                 if (!dst->tc_xstats)
00180                         return -NLE_NOMEM;
00181         }
00182 
00183         return 0;
00184 }
00185 
00186 void tca_dump_line(struct rtnl_tca *g, const char *type,
00187                    struct nl_dump_params *p)
00188 {
00189         char handle[32], parent[32];
00190         struct nl_cache *link_cache;
00191         
00192         link_cache = nl_cache_mngt_require("route/link");
00193 
00194         nl_dump_line(p, "%s %s ", g->tc_kind, type);
00195 
00196         if (link_cache) {
00197                 char buf[32];
00198                 nl_dump(p, "dev %s ",
00199                         rtnl_link_i2name(link_cache, g->tc_ifindex,
00200                                          buf, sizeof(buf)));
00201         } else
00202                 nl_dump(p, "dev %u ", g->tc_ifindex);
00203         
00204         nl_dump(p, "handle %s parent %s",
00205                 rtnl_tc_handle2str(g->tc_handle, handle, sizeof(handle)),
00206                 rtnl_tc_handle2str(g->tc_parent, parent, sizeof(parent)));
00207 }
00208 
00209 void tca_dump_details(struct rtnl_tca *g, struct nl_dump_params *p)
00210 {
00211         nl_dump_line(p, "  ");
00212 }
00213 
00214 void tca_dump_stats(struct rtnl_tca *g, struct nl_dump_params *p)
00215 {
00216         char *unit, fmt[64];
00217         float res;
00218         strcpy(fmt, "        %7.2f %s %10u %10u %10u %10u %10u\n");
00219 
00220         nl_dump_line(p, 
00221                 "    Stats:    bytes    packets      drops overlimits" \
00222                 "       qlen    backlog\n");
00223 
00224         res = nl_cancel_down_bytes(g->tc_stats[RTNL_TC_BYTES], &unit);
00225         if (*unit == 'B')
00226                 fmt[11] = '9';
00227 
00228         nl_dump_line(p, fmt, res, unit,
00229                 g->tc_stats[RTNL_TC_PACKETS],
00230                 g->tc_stats[RTNL_TC_DROPS],
00231                 g->tc_stats[RTNL_TC_OVERLIMITS],
00232                 g->tc_stats[RTNL_TC_QLEN],
00233                 g->tc_stats[RTNL_TC_BACKLOG]);
00234 
00235         res = nl_cancel_down_bytes(g->tc_stats[RTNL_TC_RATE_BPS], &unit);
00236 
00237         strcpy(fmt, "        %7.2f %s/s%9u pps");
00238 
00239         if (*unit == 'B')
00240                 fmt[11] = '9';
00241 
00242         nl_dump_line(p, fmt, res, unit, g->tc_stats[RTNL_TC_RATE_PPS]);
00243 }
00244 
00245 int tca_compare(struct nl_object *_a, struct nl_object *_b,
00246                 uint32_t attrs, int flags)
00247 {
00248         struct rtnl_tca *a = (struct rtnl_tca *) _a;
00249         struct rtnl_tca *b = (struct rtnl_tca *) _b;
00250         int diff = 0;
00251 
00252 #define TC_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, TCA_ATTR_##ATTR, a, b, EXPR)
00253 
00254         diff |= TC_DIFF(HANDLE,         a->tc_handle != b->tc_handle);
00255         diff |= TC_DIFF(PARENT,         a->tc_parent != b->tc_parent);
00256         diff |= TC_DIFF(IFINDEX,        a->tc_ifindex != b->tc_ifindex);
00257         diff |= TC_DIFF(KIND,           strcmp(a->tc_kind, b->tc_kind));
00258 
00259 #undef TC_DIFF
00260 
00261         return diff;
00262 }
00263 
00264 void tca_set_ifindex(struct rtnl_tca *t, int ifindex)
00265 {
00266         t->tc_ifindex = ifindex;
00267         t->ce_mask |= TCA_ATTR_IFINDEX;
00268 }
00269 
00270 int tca_get_ifindex(struct rtnl_tca *t)
00271 {
00272         return t->tc_ifindex;
00273 }
00274 
00275 void tca_set_handle(struct rtnl_tca *t, uint32_t handle)
00276 {
00277         t->tc_handle = handle;
00278         t->ce_mask |= TCA_ATTR_HANDLE;
00279 }
00280 
00281 uint32_t tca_get_handle(struct rtnl_tca *t)
00282 {
00283         if (t->ce_mask & TCA_ATTR_HANDLE)
00284                 return t->tc_handle;
00285         else
00286                 return 0;
00287 }
00288 
00289 void tca_set_parent(struct rtnl_tca *t, uint32_t parent)
00290 {
00291         t->tc_parent = parent;
00292         t->ce_mask |= TCA_ATTR_PARENT;
00293 }
00294 
00295 uint32_t tca_get_parent(struct rtnl_tca *t)
00296 {
00297         if (t->ce_mask & TCA_ATTR_PARENT)
00298                 return t->tc_parent;
00299         else
00300                 return 0;
00301 }
00302 
00303 void tca_set_kind(struct rtnl_tca *t, const char *kind)
00304 {
00305         strncpy(t->tc_kind, kind, sizeof(t->tc_kind) - 1);
00306         t->ce_mask |= TCA_ATTR_KIND;
00307 }
00308 
00309 char *tca_get_kind(struct rtnl_tca *t)
00310 {
00311         if (t->ce_mask & TCA_ATTR_KIND)
00312                 return t->tc_kind;
00313         else
00314                 return NULL;
00315 }
00316 
00317 uint64_t tca_get_stat(struct rtnl_tca *t, int id)
00318 {
00319         if (id < 0 || id > RTNL_TC_STATS_MAX)
00320                 return 0;
00321 
00322         return t->tc_stats[id];
00323 }
00324 
00325 int tca_build_msg(struct rtnl_tca *tca, int type, int flags,
00326                   struct nl_msg **result)
00327 {
00328         struct nl_msg *msg;
00329         struct tcmsg tchdr = {
00330                 .tcm_family = AF_UNSPEC,
00331                 .tcm_ifindex = tca->tc_ifindex,
00332                 .tcm_handle = tca->tc_handle,
00333                 .tcm_parent = tca->tc_parent,
00334         };
00335 
00336         msg = nlmsg_alloc_simple(type, flags);
00337         if (!msg)
00338                 return -NLE_NOMEM;
00339 
00340         if (nlmsg_append(msg, &tchdr, sizeof(tchdr), NLMSG_ALIGNTO) < 0)
00341                 goto nla_put_failure;
00342 
00343         if (tca->ce_mask & TCA_ATTR_KIND)
00344             NLA_PUT_STRING(msg, TCA_KIND, tca->tc_kind);
00345 
00346         *result = msg;
00347         return 0;
00348 
00349 nla_put_failure:
00350         nlmsg_free(msg);
00351         return -NLE_MSGSIZE;
00352 }
00353 
00354 /** @endcond */
00355 
00356 /**
00357  * @name Utilities
00358  * @{
00359  */
00360 
00361 /**
00362  * Calculate time required to transmit buffer at a specific rate
00363  * @arg bufsize         Size of buffer to be transmited in bytes.
00364  * @arg rate            Transmit rate in bytes per second.
00365  *
00366  * Calculates the number of micro seconds required to transmit a
00367  * specific buffer at a specific transmit rate.
00368  *
00369  * @f[
00370  *   txtime=\frac{bufsize}{rate}10^6
00371  * @f]
00372  * 
00373  * @return Required transmit time in micro seconds.
00374  */
00375 int rtnl_tc_calc_txtime(int bufsize, int rate)
00376 {
00377         double tx_time_secs;
00378         
00379         tx_time_secs = (double) bufsize / (double) rate;
00380 
00381         return tx_time_secs * 1000000.;
00382 }
00383 
00384 /**
00385  * Calculate buffer size able to transmit in a specific time and rate.
00386  * @arg txtime          Available transmit time in micro seconds.
00387  * @arg rate            Transmit rate in bytes per second.
00388  *
00389  * Calculates the size of the buffer that can be transmitted in a
00390  * specific time period at a specific transmit rate.
00391  *
00392  * @f[
00393  *   bufsize=\frac{{txtime} \times {rate}}{10^6}
00394  * @f]
00395  *
00396  * @return Size of buffer in bytes.
00397  */
00398 int rtnl_tc_calc_bufsize(int txtime, int rate)
00399 {
00400         double bufsize;
00401 
00402         bufsize = (double) txtime * (double) rate;
00403 
00404         return bufsize / 1000000.;
00405 }
00406 
00407 /**
00408  * Calculate the binary logarithm for a specific cell size
00409  * @arg cell_size       Size of cell, must be a power of two.
00410  * @return Binary logirhtm of cell size or a negative error code.
00411  */
00412 int rtnl_tc_calc_cell_log(int cell_size)
00413 {
00414         int i;
00415 
00416         for (i = 0; i < 32; i++)
00417                 if ((1 << i) == cell_size)
00418                         return i;
00419 
00420         return -NLE_INVAL;
00421 }
00422 
00423 
00424 /** @} */
00425 
00426 /**
00427  * @name Rate Tables
00428  * @{
00429  */
00430 
00431 /**
00432  * Compute a transmission time lookup table
00433  * @arg dst      Destination buffer of RTNL_TC_RTABLE_SIZE uint32_t[].
00434  * @arg mpu      Minimal size of a packet at all times.
00435  * @arg overhead Overhead to be added to each packet.
00436  * @arg cell     Size of cell, i.e. size of step between entries in bytes.
00437  * @arg rate     Rate in bytes per second.
00438  *
00439  * Computes a table of RTNL_TC_RTABLE_SIZE entries specyfing the
00440  * transmission times for various packet sizes, e.g. the transmission
00441  * time for a packet of size \c pktsize could be looked up:
00442  * @code
00443  * txtime = table[pktsize >> log2(cell)];
00444  * @endcode
00445  */
00446 int rtnl_tc_build_rate_table(uint32_t *dst, uint8_t mpu, uint8_t overhead,
00447                              int cell, int rate)
00448 {
00449         int i, size, cell_log;
00450 
00451         cell_log = rtnl_tc_calc_cell_log(cell);
00452         if (cell_log < 0)
00453                 return cell_log;
00454 
00455         for (i = 0; i < RTNL_TC_RTABLE_SIZE; i++) {
00456                 size = (i << cell_log) + overhead;
00457                 if (size < mpu)
00458                         size = mpu;
00459 
00460                 dst[i] = rtnl_tc_calc_txtime(size, rate);
00461         }
00462 
00463         return 0;
00464 }
00465 
00466 /** @} */
00467 
00468 /**
00469  * @name Traffic Control Handle Translations
00470  * @{
00471  */
00472 
00473 /**
00474  * Convert a traffic control handle to a character string (Reentrant).
00475  * @arg handle          traffic control handle
00476  * @arg buf             destination buffer
00477  * @arg len             buffer length
00478  *
00479  * Converts a tarffic control handle to a character string in the
00480  * form of \c MAJ:MIN and stores it in the specified destination buffer.
00481  *
00482  * @return The destination buffer or the type encoded in hexidecimal
00483  *         form if no match was found.
00484  */
00485 char * rtnl_tc_handle2str(uint32_t handle, char *buf, size_t len)
00486 {
00487         if (TC_H_ROOT == handle)
00488                 snprintf(buf, len, "root");
00489         else if (TC_H_UNSPEC == handle)
00490                 snprintf(buf, len, "none");
00491         else if (0 == TC_H_MAJ(handle))
00492                 snprintf(buf, len, ":%02x", TC_H_MIN(handle));
00493         else if (0 == TC_H_MIN(handle))
00494                 snprintf(buf, len, "%02x:", TC_H_MAJ(handle) >> 16);
00495         else
00496                 snprintf(buf, len, "%02x:%02x",
00497                         TC_H_MAJ(handle) >> 16, TC_H_MIN(handle));
00498 
00499         return buf;
00500 }
00501 
00502 /**
00503  * Convert a charactering strint to a traffic control handle
00504  * @arg name            traffic control handle as character string
00505  * @arg res             destination buffer
00506  *
00507  * Converts the provided character string specifying a traffic
00508  * control handle to the corresponding numeric value.
00509  *
00510  * The handle must be provided in one of the following formats:
00511  *  - root
00512  *  - none
00513  *  - XXXX:
00514  *  - :YYYY
00515  *  - XXXX:YYYY
00516  *  - XXXXYYYY
00517  *
00518  * @return 0 on success or a negative error code
00519  */
00520 int rtnl_tc_str2handle(const char *name, uint32_t *res)
00521 {
00522         char *colon, *end;
00523         uint32_t h;
00524 
00525         if (!strcasecmp(name, "root")) {
00526                 *res = TC_H_ROOT;
00527                 return 0;
00528         }
00529 
00530         if (!strcasecmp(name, "none")) {
00531                 *res = TC_H_UNSPEC;
00532                 return 0;
00533         }
00534 
00535         h = strtoul(name, &colon, 16);
00536 
00537         if (colon == name) {
00538                 /* :YYYY */
00539                 h = 0;
00540                 if (':' != *colon)
00541                         return -NLE_INVAL;
00542         }
00543 
00544         if (':' == *colon) {
00545                 /* check if we would lose bits */
00546                 if (TC_H_MAJ(h))
00547                         return -NLE_RANGE;
00548                 h <<= 16;
00549 
00550                 if ('\0' == colon[1]) {
00551                         /* XXXX: */
00552                         *res = h;
00553                 } else {
00554                         /* XXXX:YYYY */
00555                         uint32_t l = strtoul(colon+1, &end, 16);
00556 
00557                         /* check if we overlap with major part */
00558                         if (TC_H_MAJ(l))
00559                                 return -NLE_RANGE;
00560 
00561                         if ('\0' != *end)
00562                                 return -NLE_INVAL;
00563 
00564                         *res = (h | l);
00565                 }
00566         } else if ('\0' == *colon) {
00567                 /* XXXXYYYY */
00568                 *res = h;
00569         } else
00570                 return -NLE_INVAL;
00571 
00572         return 0;
00573 }
00574 
00575 /** @} */
00576 
00577 /** @} */