/build/buildd/libnl-1.0~pre6/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-2006 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->tc_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 nl_error(EINVAL, "Missing tca kind TLV");
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->tc_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 = nla_get_data(tb[TCA_OPTIONS]);
00085                 if (!g->tc_opts)
00086                         return nl_errno(ENOMEM);
00087                 g->tc_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->tc_mask |= TCA_ATTR_STATS;
00127                 
00128                 if (tbs[TCA_STATS_APP]) {
00129                         g->tc_xstats = nla_get_data(tbs[TCA_STATS_APP]);
00130                         if (g->tc_xstats == NULL)
00131                                 return -ENOMEM;
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->tc_mask |= TCA_ATTR_STATS;
00148                 }
00149 
00150 compat_xstats:
00151                 if (tb[TCA_XSTATS]) {
00152                         g->tc_xstats = nla_get_data(tb[TCA_XSTATS]);
00153                         if (g->tc_xstats == NULL)
00154                                 return -ENOMEM;
00155                         g->tc_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_dump_brief(struct rtnl_tca *g, const char *type,
00170                    struct nl_dump_params *p, int line)
00171 {
00172         char handle[32], parent[32];
00173         struct nl_cache *link_cache;
00174         
00175         link_cache = nl_cache_mngt_require("route/link");
00176 
00177         dp_dump(p, "%s %s ", g->tc_kind, type);
00178 
00179         if (link_cache) {
00180                 char buf[32];
00181                 dp_dump(p, "dev %s ",
00182                         rtnl_link_i2name(link_cache, g->tc_ifindex,
00183                                          buf, sizeof(buf)));
00184         } else
00185                 dp_dump(p, "dev %u ", g->tc_ifindex);
00186         
00187         dp_dump(p, "handle %s parent %s",
00188                 rtnl_tc_handle2str(g->tc_handle, handle, sizeof(handle)),
00189                 rtnl_tc_handle2str(g->tc_parent, parent, sizeof(parent)));
00190 
00191         return 1;
00192 }
00193 
00194 int tca_dump_full(struct rtnl_tca *g, struct nl_dump_params *p, int line)
00195 {
00196         dp_dump_line(p, line++, "  ");
00197         return line;
00198 }
00199 
00200 int tca_dump_stats(struct rtnl_tca *g, struct nl_dump_params *p, int line)
00201 {
00202         char *unit, fmt[64];
00203         float res;
00204         strcpy(fmt, "        %7.2f %s %10u %10u %10u %10u %10u\n");
00205 
00206         dp_dump_line(p, line++,
00207                 "    Stats:    bytes    packets      drops overlimits" \
00208                 "       qlen    backlog\n");
00209 
00210         res = nl_cancel_down_bytes(g->tc_stats[RTNL_TC_BYTES], &unit);
00211         if (*unit == 'B')
00212                 fmt[11] = '9';
00213 
00214         dp_dump_line(p, line++, fmt, res, unit,
00215                 g->tc_stats[RTNL_TC_PACKETS],
00216                 g->tc_stats[RTNL_TC_DROPS],
00217                 g->tc_stats[RTNL_TC_OVERLIMITS],
00218                 g->tc_stats[RTNL_TC_QLEN],
00219                 g->tc_stats[RTNL_TC_BACKLOG]);
00220 
00221         res = nl_cancel_down_bytes(g->tc_stats[RTNL_TC_RATE_BPS], &unit);
00222 
00223         strcpy(fmt, "        %7.2f %s/s%9u pps");
00224 
00225         if (*unit == 'B')
00226                 fmt[11] = '9';
00227 
00228         dp_dump_line(p, line++, fmt, res, unit, g->tc_stats[RTNL_TC_RATE_PPS]);
00229 
00230         return line;
00231 }
00232 
00233 int tca_filter(struct rtnl_tca *o, struct rtnl_tca *f)
00234 {
00235 #define REQ(F) (f->tc_mask & TCA_ATTR_##F)
00236 #define AVAIL(F) (o->tc_mask & TCA_ATTR_##F)
00237 #define _O(F, EXPR) (REQ(F) && (!AVAIL(F) || (EXPR)))
00238 #define _C(F, N) (REQ(F) && (!AVAIL(F) || (o->N != f->N)))
00239         if (_C(HANDLE,  tc_handle)                      ||
00240             _C(PARENT,  tc_parent)                      ||
00241             _C(IFINDEX, tc_ifindex)                     ||
00242             _O(KIND,    strcmp(o->tc_kind, f->tc_kind)))
00243                 return 0;
00244 #undef REQ
00245 #undef AVAIL
00246 #undef _O
00247 #undef _C
00248 
00249         return 1;
00250 }
00251 
00252 void tca_set_ifindex(struct rtnl_tca *t, int ifindex)
00253 {
00254         t->tc_ifindex = ifindex;
00255         t->tc_mask |= TCA_ATTR_IFINDEX;
00256 }
00257 
00258 int tca_get_ifindex(struct rtnl_tca *t)
00259 {
00260         if (t->tc_mask & TCA_ATTR_IFINDEX)
00261                 return t->tc_ifindex;
00262         else
00263                 return RTNL_LINK_NOT_FOUND;
00264 }
00265 
00266 void tca_set_handle(struct rtnl_tca *t, uint32_t handle)
00267 {
00268         t->tc_handle = handle;
00269         t->tc_mask |= TCA_ATTR_HANDLE;
00270 }
00271 
00272 uint32_t tca_get_handle(struct rtnl_tca *t)
00273 {
00274         if (t->tc_mask & TCA_ATTR_HANDLE)
00275                 return t->tc_handle;
00276         else
00277                 return 0;
00278 }
00279 
00280 void tca_set_parent(struct rtnl_tca *t, uint32_t parent)
00281 {
00282         t->tc_parent = parent;
00283         t->tc_mask |= TCA_ATTR_PARENT;
00284 }
00285 
00286 uint32_t tca_get_parent(struct rtnl_tca *t)
00287 {
00288         if (t->tc_mask & TCA_ATTR_PARENT)
00289                 return t->tc_parent;
00290         else
00291                 return 0;
00292 }
00293 
00294 void tca_set_kind(struct rtnl_tca *t, const char *kind)
00295 {
00296         strncpy(t->tc_kind, kind, sizeof(t->tc_kind) - 1);
00297         t->tc_mask |= TCA_ATTR_KIND;
00298 }
00299 
00300 char *tca_get_kind(struct rtnl_tca *t)
00301 {
00302         if (t->tc_mask & TCA_ATTR_KIND)
00303                 return t->tc_kind;
00304         else
00305                 return NULL;
00306 }
00307 
00308 uint64_t tca_get_stat(struct rtnl_tca *t, int id)
00309 {
00310         if (id < 0 || id > RTNL_TC_STATS_MAX)
00311                 return 0;
00312 
00313         return t->tc_stats[id];
00314 }
00315 
00316 struct nl_msg *tca_build_msg(struct rtnl_tca *tca, int type, int flags)
00317 {
00318         struct nl_msg *msg;
00319         struct tcmsg tchdr = {
00320                 .tcm_family = AF_UNSPEC,
00321                 .tcm_ifindex = tca->tc_ifindex,
00322                 .tcm_handle = tca->tc_handle,
00323                 .tcm_parent = tca->tc_parent,
00324         };
00325 
00326         msg = nlmsg_build_simple(type, flags);
00327         if (!msg)
00328                 goto nla_put_failure;
00329 
00330         if (nlmsg_append(msg, &tchdr, sizeof(tchdr), 1) < 0)
00331                 goto nla_put_failure;
00332 
00333         if (tca->tc_mask & TCA_ATTR_KIND)
00334             NLA_PUT_STRING(msg, TCA_KIND, tca->tc_kind);
00335 
00336         return msg;
00337 
00338 nla_put_failure:
00339         nlmsg_free(msg);
00340         return NULL;
00341 }
00342 
00343 /** @endcond */
00344 
00345 /**
00346  * @name Utilities
00347  * @{
00348  */
00349 
00350 /**
00351  * Calculate time required to transmit buffer at a specific rate
00352  * @arg bufsize         Size of buffer to be transmited in bytes.
00353  * @arg rate            Transmit rate in bytes per second.
00354  *
00355  * Calculates the number of micro seconds required to transmit a
00356  * specific buffer at a specific transmit rate.
00357  *
00358  * @f[
00359  *   txtime=\frac{bufsize}{rate}10^6
00360  * @f]
00361  * 
00362  * @return Required transmit time in micro seconds.
00363  */
00364 int rtnl_tc_calc_txtime(int bufsize, int rate)
00365 {
00366         double tx_time_secs;
00367         
00368         tx_time_secs = (double) bufsize / (double) rate;
00369 
00370         return tx_time_secs * 1000000.;
00371 }
00372 
00373 /**
00374  * Calculate buffer size able to transmit in a specific time and rate.
00375  * @arg txtime          Available transmit time in micro seconds.
00376  * @arg rate            Transmit rate in bytes per second.
00377  *
00378  * Calculates the size of the buffer that can be transmitted in a
00379  * specific time period at a specific transmit rate.
00380  *
00381  * @f[
00382  *   bufsize=\frac{{txtime} \times {rate}}{10^6}
00383  * @f]
00384  *
00385  * @return Size of buffer in bytes.
00386  */
00387 int rtnl_tc_calc_bufsize(int txtime, int rate)
00388 {
00389         double bufsize;
00390 
00391         bufsize = (double) txtime * (double) rate;
00392 
00393         return bufsize / 1000000.;
00394 }
00395 
00396 /**
00397  * Calculate the binary logarithm for a specific cell size
00398  * @arg cell_size       Size of cell, must be a power of two.
00399  * @return Binary logirhtm of cell size or a negative error code.
00400  */
00401 int rtnl_tc_calc_cell_log(int cell_size)
00402 {
00403         int i;
00404 
00405         for (i = 0; i < 32; i++)
00406                 if ((1 << i) == cell_size)
00407                         return i;
00408 
00409         return nl_errno(EINVAL);
00410 }
00411 
00412 
00413 /** @} */
00414 
00415 /**
00416  * @name Rate Tables
00417  * @{
00418  */
00419 
00420 /**
00421  * Compute a transmission time lookup table
00422  * @arg dst      Destination buffer of RTNL_TC_RTABLE_SIZE uint32_t[].
00423  * @arg mpu      Minimal size of a packet at all times.
00424  * @arg overhead Overhead to be added to each packet.
00425  * @arg cell     Size of cell, i.e. size of step between entries in bytes.
00426  * @arg rate     Rate in bytes per second.
00427  *
00428  * Computes a table of RTNL_TC_RTABLE_SIZE entries specyfing the
00429  * transmission times for various packet sizes, e.g. the transmission
00430  * time for a packet of size \c pktsize could be looked up:
00431  * @code
00432  * txtime = table[pktsize >> log2(cell)];
00433  * @endcode
00434  */
00435 int rtnl_tc_build_rate_table(uint32_t *dst, uint8_t mpu, uint8_t overhead,
00436                              int cell, int rate)
00437 {
00438         int i, size, cell_log;
00439 
00440         cell_log = rtnl_tc_calc_cell_log(cell);
00441         if (cell_log < 0)
00442                 return cell_log;
00443 
00444         for (i = 0; i < RTNL_TC_RTABLE_SIZE; i++) {
00445                 size = (i << cell_log) + overhead;
00446                 if (size < mpu)
00447                         size = mpu;
00448 
00449                 dst[i] = rtnl_tc_calc_txtime(size, rate);
00450         }
00451 
00452         return 0;
00453 }
00454 
00455 /** @} */
00456 
00457 /**
00458  * @name Traffic Control Handle Translations
00459  * @{
00460  */
00461 
00462 /**
00463  * Convert a traffic control handle to a character string (Reentrant).
00464  * @arg handle          traffic control handle
00465  * @arg buf             destination buffer
00466  * @arg len             buffer length
00467  *
00468  * Converts a tarffic control handle to a character string in the
00469  * form of \c MAJ:MIN and stores it in the specified destination buffer.
00470  *
00471  * @return The destination buffer or the type encoded in hexidecimal
00472  *         form if no match was found.
00473  */
00474 char * rtnl_tc_handle2str(uint32_t handle, char *buf, size_t len)
00475 {
00476         if (TC_H_ROOT == handle)
00477                 snprintf(buf, len, "root");
00478         else if (TC_H_UNSPEC == handle)
00479                 snprintf(buf, len, "none");
00480         else if (0 == TC_H_MAJ(handle))
00481                 snprintf(buf, len, ":%02x", TC_H_MIN(handle));
00482         else if (0 == TC_H_MIN(handle))
00483                 snprintf(buf, len, "%02x:", TC_H_MAJ(handle) >> 16);
00484         else
00485                 snprintf(buf, len, "%02x:%02x",
00486                         TC_H_MAJ(handle) >> 16, TC_H_MIN(handle));
00487 
00488         return buf;
00489 }
00490 
00491 /**
00492  * Convert a charactering strint to a traffic control handle
00493  * @arg name            traffic control handle as character string
00494  * @arg res             destination buffer
00495  *
00496  * Converts the provided character string specifying a traffic
00497  * control handle to the corresponding numeric value.
00498  *
00499  * The handle must be provided in one of the following formats:
00500  *  - root
00501  *  - none
00502  *  - XXXX:
00503  *  - :YYYY
00504  *  - XXXX:YYYY
00505  *  - XXXXYYYY
00506  *
00507  * @return 0 on success or a negative error code
00508  */
00509 int rtnl_tc_str2handle(const char *name, uint32_t *res)
00510 {
00511         char *colon, *end;
00512         uint32_t h;
00513 
00514         if (!strcasecmp(name, "root")) {
00515                 *res = TC_H_ROOT;
00516                 return 0;
00517         }
00518 
00519         if (!strcasecmp(name, "none")) {
00520                 *res = TC_H_UNSPEC;
00521                 return 0;
00522         }
00523 
00524         h = strtoul(name, &colon, 16);
00525 
00526         if (colon == name) {
00527                 /* :YYYY */
00528                 h = 0;
00529                 if (':' != *colon)
00530                         return -EINVAL;
00531         }
00532 
00533         if (':' == *colon) {
00534                 /* check if we would lose bits */
00535                 if (TC_H_MAJ(h))
00536                         return -ERANGE;
00537                 h <<= 16;
00538 
00539                 if ('\0' == colon[1]) {
00540                         /* XXXX: */
00541                         *res = h;
00542                 } else {
00543                         /* XXXX:YYYY */
00544                         uint32_t l = strtoul(colon+1, &end, 16);
00545 
00546                         /* check if we overlap with major part */
00547                         if (TC_H_MAJ(l))
00548                                 return -ERANGE;
00549 
00550                         if ('\0' != *end)
00551                                 return -EINVAL;
00552 
00553                         *res = (h | l);
00554                 }
00555         } else if ('\0' == *colon) {
00556                 /* XXXXYYYY */
00557                 *res = h;
00558         } else
00559                 return -EINVAL;
00560 
00561         return 0;
00562 }
00563 
00564 /** @} */
00565 
00566 /** @} */

Generated on Fri Apr 27 14:14:07 2007 for libnl by  doxygen 1.5.1