The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netgraph/ng_car.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2005 Nuno Antunes <nuno.antunes@gmail.com>
    3  * Copyright (c) 2007 Alexander Motin <mav@freebsd.org>
    4  * All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  *
   15  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   18  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
   19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   25  * SUCH DAMAGE.
   26  *
   27  * $FreeBSD$
   28  */
   29 
   30 /*
   31  * ng_car - An implementation of commited access rate for netgraph
   32  *
   33  * TODO:
   34  *      - Sanitize input config values (impose some limits)
   35  *      - Implement internal packet painting (possibly using mbuf tags)
   36  *      - Implement color-aware mode
   37  *      - Implement DSCP marking for IPv4
   38  */
   39 
   40 #include <sys/param.h>
   41 #include <sys/errno.h>
   42 #include <sys/kernel.h>
   43 #include <sys/malloc.h>
   44 #include <sys/mbuf.h>
   45 
   46 #include <netgraph/ng_message.h>
   47 #include <netgraph/ng_parse.h>
   48 #include <netgraph/netgraph.h>
   49 #include <netgraph/ng_car.h>
   50 
   51 #define NG_CAR_QUEUE_SIZE       100     /* Maximum queue size for SHAPE mode */
   52 #define NG_CAR_QUEUE_MIN_TH     8       /* Minimum RED threshhold for SHAPE mode */
   53 
   54 /* Hook private info */
   55 struct hookinfo {
   56         hook_p          hook;           /* this (source) hook */
   57         hook_p          dest;           /* destination hook */
   58 
   59         int64_t         tc;             /* commited token bucket counter */
   60         int64_t         te;             /* exceeded/peak token bucket counter */
   61         struct timeval  lastRefill;     /* last token refill time */
   62 
   63         struct ng_car_hookconf conf;    /* hook configuration */
   64         struct ng_car_hookstats stats;  /* hook stats */
   65 
   66         struct mbuf     *q[NG_CAR_QUEUE_SIZE];  /* circular packet queue */
   67         int             q_first;        /* first queue element */
   68         int             q_last;         /* last queue element */
   69         struct callout  q_callout;      /* periodic queue processing routine */
   70         struct mtx      q_mtx;          /* queue mutex */
   71 };
   72 
   73 /* Private information for each node instance */
   74 struct privdata {
   75         node_p node;                            /* the node itself */
   76         struct hookinfo upper;                  /* hook to upper layers */
   77         struct hookinfo lower;                  /* hook to lower layers */
   78 };
   79 typedef struct privdata *priv_p;
   80 
   81 static ng_constructor_t ng_car_constructor;
   82 static ng_rcvmsg_t      ng_car_rcvmsg;
   83 static ng_shutdown_t    ng_car_shutdown;
   84 static ng_newhook_t     ng_car_newhook;
   85 static ng_rcvdata_t     ng_car_rcvdata;
   86 static ng_disconnect_t  ng_car_disconnect;
   87 
   88 static void     ng_car_refillhook(struct hookinfo *h);
   89 static void     ng_car_schedule(struct hookinfo *h);
   90 void            ng_car_q_event(node_p node, hook_p hook, void *arg, int arg2);
   91 static void     ng_car_enqueue(struct hookinfo *h, item_p item);
   92 
   93 /* Parse type for struct ng_car_hookstats */
   94 static const struct ng_parse_struct_field ng_car_hookstats_type_fields[]
   95         = NG_CAR_HOOKSTATS;
   96 static const struct ng_parse_type ng_car_hookstats_type = {
   97         &ng_parse_struct_type,
   98         &ng_car_hookstats_type_fields
   99 };
  100 
  101 /* Parse type for struct ng_car_bulkstats */
  102 static const struct ng_parse_struct_field ng_car_bulkstats_type_fields[]
  103         = NG_CAR_BULKSTATS(&ng_car_hookstats_type);
  104 static const struct ng_parse_type ng_car_bulkstats_type = {
  105         &ng_parse_struct_type,
  106         &ng_car_bulkstats_type_fields
  107 };
  108 
  109 /* Parse type for struct ng_car_hookconf */
  110 static const struct ng_parse_struct_field ng_car_hookconf_type_fields[]
  111         = NG_CAR_HOOKCONF;
  112 static const struct ng_parse_type ng_car_hookconf_type = {
  113         &ng_parse_struct_type,
  114         &ng_car_hookconf_type_fields
  115 };
  116 
  117 /* Parse type for struct ng_car_bulkconf */
  118 static const struct ng_parse_struct_field ng_car_bulkconf_type_fields[]
  119         = NG_CAR_BULKCONF(&ng_car_hookconf_type);
  120 static const struct ng_parse_type ng_car_bulkconf_type = {
  121         &ng_parse_struct_type,
  122         &ng_car_bulkconf_type_fields
  123 };
  124 
  125 /* Command list */
  126 static struct ng_cmdlist ng_car_cmdlist[] = {
  127         {
  128           NGM_CAR_COOKIE,
  129           NGM_CAR_GET_STATS,
  130           "getstats",
  131           NULL,
  132           &ng_car_bulkstats_type,
  133         },
  134         {
  135           NGM_CAR_COOKIE,
  136           NGM_CAR_CLR_STATS,
  137           "clrstats",
  138           NULL,
  139           NULL,
  140         },
  141         {
  142           NGM_CAR_COOKIE,
  143           NGM_CAR_GETCLR_STATS,
  144           "getclrstats",
  145           NULL,
  146           &ng_car_bulkstats_type,
  147         },
  148 
  149         {
  150           NGM_CAR_COOKIE,
  151           NGM_CAR_GET_CONF,
  152           "getconf",
  153           NULL,
  154           &ng_car_bulkconf_type,
  155         },
  156         {
  157           NGM_CAR_COOKIE,
  158           NGM_CAR_SET_CONF,
  159           "setconf",
  160           &ng_car_bulkconf_type,
  161           NULL,
  162         },
  163         { 0 }
  164 };
  165 
  166 /* Netgraph node type descriptor */
  167 static struct ng_type ng_car_typestruct = {
  168         .version =      NG_ABI_VERSION,
  169         .name =         NG_CAR_NODE_TYPE,
  170         .constructor =  ng_car_constructor,
  171         .rcvmsg =       ng_car_rcvmsg,
  172         .shutdown =     ng_car_shutdown,
  173         .newhook =      ng_car_newhook,
  174         .rcvdata =      ng_car_rcvdata,
  175         .disconnect =   ng_car_disconnect,
  176         .cmdlist =      ng_car_cmdlist,
  177 };
  178 NETGRAPH_INIT(car, &ng_car_typestruct);
  179 
  180 /*
  181  * Node constructor
  182  */
  183 static int
  184 ng_car_constructor(node_p node)
  185 {
  186         priv_p priv;
  187 
  188         /* Initialize private descriptor. */
  189         priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
  190         if (priv == NULL)
  191                 return (ENOMEM);
  192 
  193         NG_NODE_SET_PRIVATE(node, priv);
  194         priv->node = node;
  195 
  196         /*
  197          * Arbitrary default values
  198          */
  199 
  200         priv->upper.hook = NULL;
  201         priv->upper.dest = NULL;
  202         priv->upper.tc = priv->upper.conf.cbs = NG_CAR_CBS_MIN;
  203         priv->upper.te = priv->upper.conf.ebs = NG_CAR_EBS_MIN;
  204         priv->upper.conf.cir = NG_CAR_CIR_DFLT;
  205         priv->upper.conf.green_action = NG_CAR_ACTION_FORWARD;
  206         priv->upper.conf.yellow_action = NG_CAR_ACTION_FORWARD;
  207         priv->upper.conf.red_action = NG_CAR_ACTION_DROP;
  208         priv->upper.conf.mode = 0;
  209         getmicrotime(&priv->upper.lastRefill);
  210         priv->upper.q_first = 0;
  211         priv->upper.q_last = 0;
  212         ng_callout_init(&priv->upper.q_callout);
  213         mtx_init(&priv->upper.q_mtx, "ng_car_u", NULL, MTX_DEF);
  214 
  215         priv->lower.hook = NULL;
  216         priv->lower.dest = NULL;
  217         priv->lower.tc = priv->lower.conf.cbs = NG_CAR_CBS_MIN;
  218         priv->lower.te = priv->lower.conf.ebs = NG_CAR_EBS_MIN;
  219         priv->lower.conf.cir = NG_CAR_CIR_DFLT;
  220         priv->lower.conf.green_action = NG_CAR_ACTION_FORWARD;
  221         priv->lower.conf.yellow_action = NG_CAR_ACTION_FORWARD;
  222         priv->lower.conf.red_action = NG_CAR_ACTION_DROP;
  223         priv->lower.conf.mode = 0;
  224         priv->lower.lastRefill = priv->upper.lastRefill;
  225         priv->lower.q_first = 0;
  226         priv->lower.q_last = 0;
  227         ng_callout_init(&priv->lower.q_callout);
  228         mtx_init(&priv->lower.q_mtx, "ng_car_l", NULL, MTX_DEF);
  229 
  230         return (0);
  231 }
  232 
  233 /*
  234  * Add a hook.
  235  */
  236 static int
  237 ng_car_newhook(node_p node, hook_p hook, const char *name)
  238 {
  239         const priv_p priv = NG_NODE_PRIVATE(node);
  240 
  241         if (strcmp(name, NG_CAR_HOOK_LOWER) == 0) {
  242                 priv->lower.hook = hook;
  243                 priv->upper.dest = hook;
  244                 bzero(&priv->lower.stats, sizeof(priv->lower.stats));
  245                 NG_HOOK_SET_PRIVATE(hook, &priv->lower);
  246         } else if (strcmp(name, NG_CAR_HOOK_UPPER) == 0) {
  247                 priv->upper.hook = hook;
  248                 priv->lower.dest = hook;
  249                 bzero(&priv->upper.stats, sizeof(priv->upper.stats));
  250                 NG_HOOK_SET_PRIVATE(hook, &priv->upper);
  251         } else
  252                 return (EINVAL);
  253         return(0);
  254 }
  255 
  256 /*
  257  * Data has arrived.
  258  */
  259 static int
  260 ng_car_rcvdata(hook_p hook, item_p item )
  261 {
  262         struct hookinfo *const hinfo = NG_HOOK_PRIVATE(hook);
  263         hook_p dest = hinfo->dest;
  264         struct mbuf *m = NULL;
  265         int error = 0;
  266 
  267         /* Node is useless without destination hook. */
  268         if (dest == NULL) {
  269                 NG_FREE_ITEM(item);
  270                 ++hinfo->stats.errors;
  271                 return(EINVAL);
  272         }
  273 
  274         /* If queue is not empty now then enqueue packet. */
  275         if (hinfo->q_first != hinfo->q_last) {
  276                 ng_car_enqueue(hinfo, item);
  277                 return (0);
  278         }
  279 
  280         m = NGI_M(item);
  281 
  282 #define NG_CAR_PERFORM_MATCH_ACTION(a)                  \
  283         do {                                            \
  284                 switch (a) {                            \
  285                 case NG_CAR_ACTION_FORWARD:             \
  286                         /* Do nothing. */               \
  287                         break;                          \
  288                 case NG_CAR_ACTION_MARK:                \
  289                         /* XXX find a way to mark packets (mbuf tag?) */ \
  290                         ++hinfo->stats.errors;          \
  291                         break;                          \
  292                 case NG_CAR_ACTION_DROP:                \
  293                 default:                                \
  294                         /* Drop packet and return. */   \
  295                         NG_FREE_ITEM(item);             \
  296                         ++hinfo->stats.droped_pkts;     \
  297                         return (0);                     \
  298                 }                                       \
  299         } while (0)
  300 
  301         /* Check commited token bucket. */
  302         if (hinfo->tc - m->m_pkthdr.len >= 0) {
  303                 /* This packet is green. */
  304                 ++hinfo->stats.green_pkts;
  305                 hinfo->tc -= m->m_pkthdr.len;
  306                 NG_CAR_PERFORM_MATCH_ACTION(hinfo->conf.green_action);
  307         } else {
  308 
  309                 /* Refill only if not green without it. */
  310                 ng_car_refillhook(hinfo);
  311 
  312                  /* Check commited token bucket again after refill. */
  313                 if (hinfo->tc - m->m_pkthdr.len >= 0) {
  314                         /* This packet is green */
  315                         ++hinfo->stats.green_pkts;
  316                         hinfo->tc -= m->m_pkthdr.len;
  317                         NG_CAR_PERFORM_MATCH_ACTION(hinfo->conf.green_action);
  318 
  319                 /* If not green and mode is SHAPE, enqueue packet. */
  320                 } else if (hinfo->conf.mode == NG_CAR_SHAPE) {
  321                         ng_car_enqueue(hinfo, item);
  322                         return (0);
  323 
  324                 /* If not green and mode is RED, calculate probability. */
  325                 } else if (hinfo->conf.mode == NG_CAR_RED) {
  326                         /* Is packet is bigger then extended burst? */
  327                         if (m->m_pkthdr.len - (hinfo->tc - m->m_pkthdr.len) >
  328                             hinfo->conf.ebs) {
  329                                 /* This packet is definitely red. */
  330                                 ++hinfo->stats.red_pkts;
  331                                 hinfo->te = 0;
  332                                 NG_CAR_PERFORM_MATCH_ACTION(hinfo->conf.red_action);
  333 
  334                         /* Use token bucket to simulate RED-like drop
  335                            probability. */
  336                         } else if (hinfo->te + (m->m_pkthdr.len - hinfo->tc) <
  337                             hinfo->conf.ebs) {
  338                                 /* This packet is yellow */
  339                                 ++hinfo->stats.yellow_pkts;
  340                                 hinfo->te += m->m_pkthdr.len - hinfo->tc;
  341                                 /* Go to negative tokens. */
  342                                 hinfo->tc -= m->m_pkthdr.len;
  343                                 NG_CAR_PERFORM_MATCH_ACTION(hinfo->conf.yellow_action);
  344                         } else {
  345                                 /* This packet is probaly red. */
  346                                 ++hinfo->stats.red_pkts;
  347                                 hinfo->te = 0;
  348                                 NG_CAR_PERFORM_MATCH_ACTION(hinfo->conf.red_action);
  349                         }
  350                 /* If not green and mode is SINGLE/DOUBLE RATE. */
  351                 } else {
  352                         /* Check extended token bucket. */
  353                         if (hinfo->te - m->m_pkthdr.len >= 0) {
  354                                 /* This packet is yellow */
  355                                 ++hinfo->stats.yellow_pkts;
  356                                 hinfo->te -= m->m_pkthdr.len;
  357                                 NG_CAR_PERFORM_MATCH_ACTION(hinfo->conf.yellow_action);
  358                         } else {
  359                                 /* This packet is red */
  360                                 ++hinfo->stats.red_pkts;
  361                                 NG_CAR_PERFORM_MATCH_ACTION(hinfo->conf.red_action);
  362                         }
  363                 }
  364         }
  365 
  366 #undef NG_CAR_PERFORM_MATCH_ACTION
  367 
  368         NG_FWD_ITEM_HOOK(error, item, dest);
  369         if (error != 0)
  370                 ++hinfo->stats.errors;
  371         ++hinfo->stats.passed_pkts;
  372 
  373         return (error);
  374 }
  375 
  376 /*
  377  * Receive a control message.
  378  */
  379 static int
  380 ng_car_rcvmsg(node_p node, item_p item, hook_p lasthook)
  381 {
  382         const priv_p priv = NG_NODE_PRIVATE(node);
  383         struct ng_mesg *resp = NULL;
  384         int error = 0;
  385         struct ng_mesg *msg;
  386 
  387         NGI_GET_MSG(item, msg);
  388         switch (msg->header.typecookie) {
  389         case NGM_CAR_COOKIE:
  390                 switch (msg->header.cmd) {
  391                 case NGM_CAR_GET_STATS:
  392                 case NGM_CAR_GETCLR_STATS:
  393                         {
  394                                 struct ng_car_bulkstats *bstats;
  395 
  396                                 NG_MKRESPONSE(resp, msg,
  397                                         sizeof(*bstats), M_NOWAIT);
  398                                 if (resp == NULL) {
  399                                         error = ENOMEM;
  400                                         break;
  401                                 }
  402                                 bstats = (struct ng_car_bulkstats *)resp->data;
  403 
  404                                 bcopy(&priv->upper.stats, &bstats->downstream,
  405                                     sizeof(bstats->downstream));
  406                                 bcopy(&priv->lower.stats, &bstats->upstream,
  407                                     sizeof(bstats->upstream));
  408                         }
  409                         if (msg->header.cmd == NGM_CAR_GET_STATS)
  410                                 break;
  411                 case NGM_CAR_CLR_STATS:
  412                         bzero(&priv->upper.stats,
  413                                 sizeof(priv->upper.stats));
  414                         bzero(&priv->lower.stats,
  415                                 sizeof(priv->lower.stats));
  416                         break;
  417                 case NGM_CAR_GET_CONF:
  418                         {
  419                                 struct ng_car_bulkconf *bconf;
  420 
  421                                 NG_MKRESPONSE(resp, msg,
  422                                         sizeof(*bconf), M_NOWAIT);
  423                                 if (resp == NULL) {
  424                                         error = ENOMEM;
  425                                         break;
  426                                 }
  427                                 bconf = (struct ng_car_bulkconf *)resp->data;
  428 
  429                                 bcopy(&priv->upper.conf, &bconf->downstream,
  430                                     sizeof(bconf->downstream));
  431                                 bcopy(&priv->lower.conf, &bconf->upstream,
  432                                     sizeof(bconf->upstream));
  433                         }
  434                         break;
  435                 case NGM_CAR_SET_CONF:
  436                         {
  437                                 struct ng_car_bulkconf *const bconf =
  438                                 (struct ng_car_bulkconf *)msg->data;
  439 
  440                                 /* Check for invalid or illegal config. */
  441                                 if ((msg->header.arglen != sizeof(*bconf))
  442                                     || (bconf->downstream.cir > 1000000000)
  443                                     || (bconf->downstream.pir > 1000000000)
  444                                     || (bconf->upstream.cir > 1000000000)
  445                                     || (bconf->upstream.pir > 1000000000)
  446                                     || (bconf->downstream.cbs == 0
  447                                         && bconf->downstream.ebs == 0)
  448                                     || (bconf->upstream.cbs == 0
  449                                         && bconf->upstream.ebs == 0))
  450                                 {
  451                                         error = EINVAL;
  452                                         break;
  453                                 }
  454 
  455                                 /* Copy downstream config. */
  456                                 bcopy(&bconf->downstream, &priv->upper.conf,
  457                                     sizeof(priv->upper.conf));
  458                                 priv->upper.tc = priv->upper.conf.cbs;
  459                                 if (priv->upper.conf.mode == NG_CAR_RED ||
  460                                     priv->upper.conf.mode == NG_CAR_SHAPE) {
  461                                         priv->upper.te = 0;
  462                                 } else {
  463                                         priv->upper.te = priv->upper.conf.ebs;
  464                                 }
  465 
  466                                 /* Copy upstream config. */
  467                                 bcopy(&bconf->upstream, &priv->lower.conf,
  468                                     sizeof(priv->lower.conf));
  469                                 priv->lower.tc = priv->lower.conf.cbs;
  470                                 if (priv->lower.conf.mode == NG_CAR_RED ||
  471                                     priv->lower.conf.mode == NG_CAR_SHAPE) {
  472                                         priv->lower.te = 0;
  473                                 } else {
  474                                         priv->lower.te = priv->lower.conf.ebs;
  475                                 }
  476                         }
  477                         break;
  478                 default:
  479                         error = EINVAL;
  480                         break;
  481                 }
  482                 break;
  483         default:
  484                 error = EINVAL;
  485                 break;
  486         }
  487         NG_RESPOND_MSG(error, node, item, resp);
  488         NG_FREE_MSG(msg);
  489         return (error);
  490 }
  491 
  492 /*
  493  * Do local shutdown processing.
  494  */
  495 static int
  496 ng_car_shutdown(node_p node)
  497 {
  498         const priv_p priv = NG_NODE_PRIVATE(node);
  499 
  500         ng_uncallout(&priv->upper.q_callout, node);
  501         ng_uncallout(&priv->lower.q_callout, node);
  502         mtx_destroy(&priv->upper.q_mtx);
  503         mtx_destroy(&priv->lower.q_mtx);
  504         NG_NODE_UNREF(priv->node);
  505         free(priv, M_NETGRAPH);
  506         return (0);
  507 }
  508 
  509 /*
  510  * Hook disconnection.
  511  *
  512  * For this type, removal of the last link destroys the node.
  513  */
  514 static int
  515 ng_car_disconnect(hook_p hook)
  516 {
  517         struct hookinfo *const hinfo = NG_HOOK_PRIVATE(hook);
  518         const node_p node = NG_HOOK_NODE(hook);
  519         const priv_p priv = NG_NODE_PRIVATE(node);
  520 
  521         if (hinfo) {
  522                 /* Purge queue if not empty. */
  523                 while (hinfo->q_first != hinfo->q_last) {
  524                         NG_FREE_M(hinfo->q[hinfo->q_first]);
  525                         hinfo->q_first++;
  526                         if (hinfo->q_first >= NG_CAR_QUEUE_SIZE)
  527                                 hinfo->q_first = 0;
  528                 }
  529                 /* Remove hook refs. */
  530                 if (hinfo->hook == priv->upper.hook)
  531                         priv->lower.dest = NULL;
  532                 else
  533                         priv->upper.dest = NULL;
  534                 hinfo->hook = NULL;
  535         }
  536         /* Already shutting down? */
  537         if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
  538             && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
  539                 ng_rmnode_self(NG_HOOK_NODE(hook));
  540         return (0);
  541 }
  542 
  543 /*
  544  * Hook's token buckets refillment.
  545  */
  546 static void
  547 ng_car_refillhook(struct hookinfo *h)
  548 {
  549         struct timeval newt, deltat;
  550         int64_t deltat_us;
  551         int64_t delta;
  552 
  553         /* Get current time. */
  554         getmicrotime(&newt);
  555 
  556         /* Time must go forward. */
  557         if (timevalcmp(&newt, &h->lastRefill, <= )) {
  558             h->lastRefill = newt;
  559             return;
  560         }
  561 
  562         /* Get time delta since last refill. */
  563         deltat = newt;
  564         timevalsub(&deltat, &h->lastRefill);
  565 
  566         /* Sanity check */
  567         if (deltat.tv_sec > 1000) {
  568             deltat_us = 1000000000;
  569         } else {
  570             deltat_us = ((int64_t)deltat.tv_sec) * 1000000 + deltat.tv_usec;
  571         }
  572 
  573         if (h->conf.mode == NG_CAR_SINGLE_RATE) {
  574                 /* Refill commited token bucket. */
  575                 h->tc += h->conf.cir * deltat_us / 8000000;
  576                 delta = h->tc - h->conf.cbs;
  577                 if (delta > 0) {
  578                         h->tc = h->conf.cbs;
  579 
  580                         /* Refill exceeded token bucket. */
  581                         h->te += delta;
  582                         if (h->te > h->conf.ebs)
  583                                 h->te = h->conf.ebs;
  584                 }
  585 
  586         } else if (h->conf.mode == NG_CAR_DOUBLE_RATE) {
  587                 /* Refill commited token bucket. */
  588                 h->tc += h->conf.cir * deltat_us / 8000000;
  589                 if (h->tc > h->conf.cbs)
  590                         h->tc = h->conf.cbs;
  591 
  592                 /* Refill peak token bucket. */
  593                 h->te += h->conf.pir * deltat_us / 8000000;
  594                 if (h->te > h->conf.ebs)
  595                         h->te = h->conf.ebs;
  596 
  597         } else { /* RED or SHAPE mode. */
  598                 /* Refill commited token bucket. */
  599                 h->tc += h->conf.cir * deltat_us / 8000000;
  600                 if (h->tc > ((int64_t)h->conf.cbs))
  601                         h->tc = h->conf.cbs;
  602         }
  603 
  604         /* Remember this moment. */
  605         h->lastRefill = newt;
  606 }
  607 
  608 /*
  609  * Schedule callout when we will have required tokens.
  610  */
  611 static void
  612 ng_car_schedule(struct hookinfo *hinfo)
  613 {
  614         int     delay;
  615 
  616         delay = (-(hinfo->tc)) * hz * 8 / hinfo->conf.cir + 1;
  617 
  618         ng_callout(&hinfo->q_callout, NG_HOOK_NODE(hinfo->hook), hinfo->hook,
  619             delay, &ng_car_q_event, NULL, 0);
  620 }
  621 
  622 /*
  623  * Queue processing callout handler.
  624  */
  625 void
  626 ng_car_q_event(node_p node, hook_p hook, void *arg, int arg2)
  627 {
  628         struct hookinfo *hinfo = NG_HOOK_PRIVATE(hook);
  629         item_p          item;
  630         struct mbuf     *m;
  631         int             error;
  632 
  633         /* Refill tokens for time we have slept. */
  634         ng_car_refillhook(hinfo);
  635 
  636         if (hinfo->dest != NULL) {
  637                 /* If we have some tokens */
  638                 while (hinfo->tc >= 0) {
  639 
  640                         /* Send packet. */
  641                         m = hinfo->q[hinfo->q_first];
  642                         if ((item = ng_package_data(m, NG_NOFLAGS)) != NULL)
  643                                 NG_FWD_ITEM_HOOK(error, item, hinfo->dest);
  644 
  645                         /* Get next one. */
  646                         hinfo->q_first++;
  647                         if (hinfo->q_first >= NG_CAR_QUEUE_SIZE)
  648                                 hinfo->q_first = 0;
  649 
  650                         /* Stop if none left. */
  651                         if (hinfo->q_first == hinfo->q_last)
  652                                 break;
  653 
  654                         /* If we have more packet, try it. */
  655                         m = hinfo->q[hinfo->q_first];
  656                         hinfo->tc -= m->m_pkthdr.len;
  657                 }
  658         }
  659 
  660         /* If something left */
  661         if (hinfo->q_first != hinfo->q_last)
  662                 /* Schedule queue processing. */
  663                 ng_car_schedule(hinfo);
  664 }
  665 
  666 /*
  667  * Enqueue packet.
  668  */
  669 static void
  670 ng_car_enqueue(struct hookinfo *hinfo, item_p item)
  671 {
  672         struct mbuf     *m;
  673         int             len;
  674 
  675         NGI_GET_M(item, m);
  676         NG_FREE_ITEM(item);
  677 
  678         /* Lock queue mutex. */
  679         mtx_lock(&hinfo->q_mtx);
  680 
  681         /* Calculate used queue length. */
  682         len = hinfo->q_last - hinfo->q_first;
  683         if (len < 0)
  684                 len += NG_CAR_QUEUE_SIZE;
  685 
  686         /* If queue is overflowed or we have no RED tokens. */
  687         if ((len >= (NG_CAR_QUEUE_SIZE - 1)) ||
  688             (hinfo->te + len >= NG_CAR_QUEUE_SIZE)) {
  689                 /* Drop packet. */
  690                 ++hinfo->stats.red_pkts;
  691                 NG_FREE_M(m);
  692 
  693                 hinfo->te = 0;
  694         } else {
  695                 /* This packet is yellow. */
  696                 ++hinfo->stats.yellow_pkts;
  697 
  698                 /* Enqueue packet. */
  699                 hinfo->q[hinfo->q_last] = m;
  700                 hinfo->q_last++;
  701                 if (hinfo->q_last >= NG_CAR_QUEUE_SIZE)
  702                         hinfo->q_last = 0;
  703 
  704                 /* Use RED tokens. */
  705                 if (len > NG_CAR_QUEUE_MIN_TH)
  706                         hinfo->te += len - NG_CAR_QUEUE_MIN_TH;
  707 
  708                 /* If this is a first packet in the queue. */
  709                 if (len == 0) {
  710                         hinfo->tc -= m->m_pkthdr.len;
  711 
  712                         /* Schedule queue processing. */
  713                         ng_car_schedule(hinfo);
  714                 }
  715         }
  716 
  717         /* Unlock queue mutex. */
  718         mtx_unlock(&hinfo->q_mtx);
  719 }

Cache object: c2a2731af48d01eba08084ba4d939052


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.