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

Cache object: 59d0e47d27e87a40a6031c5451853f9e


[ 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.