2 * Copyright (c) 2005 Nuno Antunes <nuno.antunes@gmail.com>
3 * Copyright (c) 2007 Alexander Motin <mav@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
27 * $FreeBSD: src/sys/netgraph/ng_car.c,v 1.7 2008/03/30 07:53:51 mav Exp $
28 * $DragonFly: src/sys/netgraph7/ng_car.c,v 1.2 2008/06/26 23:05:35 dillon Exp $
32 * ng_car - An implementation of commited access rate for netgraph
35 * - Sanitize input config values (impose some limits)
36 * - Implement internal packet painting (possibly using mbuf tags)
37 * - Implement color-aware mode
38 * - Implement DSCP marking for IPv4
41 #include <sys/param.h>
42 #include <sys/errno.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
47 #include "ng_message.h"
52 #define NG_CAR_QUEUE_SIZE 100 /* Maximum queue size for SHAPE mode */
53 #define NG_CAR_QUEUE_MIN_TH 8 /* Minimum RED threshhold for SHAPE mode */
55 /* Hook private info */
57 hook_p hook; /* this (source) hook */
58 hook_p dest; /* destination hook */
60 int64_t tc; /* commited token bucket counter */
61 int64_t te; /* exceeded/peak token bucket counter */
62 struct bintime lastRefill; /* last token refill time */
64 struct ng_car_hookconf conf; /* hook configuration */
65 struct ng_car_hookstats stats; /* hook stats */
67 struct mbuf *q[NG_CAR_QUEUE_SIZE]; /* circular packet queue */
68 u_int q_first; /* first queue element */
69 u_int q_last; /* last queue element */
70 struct callout q_callout; /* periodic queue processing routine */
71 struct mtx q_mtx; /* queue mutex */
74 /* Private information for each node instance */
76 node_p node; /* the node itself */
77 struct hookinfo upper; /* hook to upper layers */
78 struct hookinfo lower; /* hook to lower layers */
80 typedef struct privdata *priv_p;
82 static ng_constructor_t ng_car_constructor;
83 static ng_rcvmsg_t ng_car_rcvmsg;
84 static ng_shutdown_t ng_car_shutdown;
85 static ng_newhook_t ng_car_newhook;
86 static ng_rcvdata_t ng_car_rcvdata;
87 static ng_disconnect_t ng_car_disconnect;
89 static void ng_car_refillhook(struct hookinfo *h);
90 static void ng_car_schedule(struct hookinfo *h);
91 void ng_car_q_event(node_p node, hook_p hook, void *arg, int arg2);
92 static void ng_car_enqueue(struct hookinfo *h, item_p item);
94 /* Parse type for struct ng_car_hookstats */
95 static const struct ng_parse_struct_field ng_car_hookstats_type_fields[]
97 static const struct ng_parse_type ng_car_hookstats_type = {
98 &ng_parse_struct_type,
99 &ng_car_hookstats_type_fields
102 /* Parse type for struct ng_car_bulkstats */
103 static const struct ng_parse_struct_field ng_car_bulkstats_type_fields[]
104 = NG_CAR_BULKSTATS(&ng_car_hookstats_type);
105 static const struct ng_parse_type ng_car_bulkstats_type = {
106 &ng_parse_struct_type,
107 &ng_car_bulkstats_type_fields
110 /* Parse type for struct ng_car_hookconf */
111 static const struct ng_parse_struct_field ng_car_hookconf_type_fields[]
113 static const struct ng_parse_type ng_car_hookconf_type = {
114 &ng_parse_struct_type,
115 &ng_car_hookconf_type_fields
118 /* Parse type for struct ng_car_bulkconf */
119 static const struct ng_parse_struct_field ng_car_bulkconf_type_fields[]
120 = NG_CAR_BULKCONF(&ng_car_hookconf_type);
121 static const struct ng_parse_type ng_car_bulkconf_type = {
122 &ng_parse_struct_type,
123 &ng_car_bulkconf_type_fields
127 static struct ng_cmdlist ng_car_cmdlist[] = {
133 &ng_car_bulkstats_type,
144 NGM_CAR_GETCLR_STATS,
147 &ng_car_bulkstats_type,
155 &ng_car_bulkconf_type,
161 &ng_car_bulkconf_type,
167 /* Netgraph node type descriptor */
168 static struct ng_type ng_car_typestruct = {
169 .version = NG_ABI_VERSION,
170 .name = NG_CAR_NODE_TYPE,
171 .constructor = ng_car_constructor,
172 .rcvmsg = ng_car_rcvmsg,
173 .shutdown = ng_car_shutdown,
174 .newhook = ng_car_newhook,
175 .rcvdata = ng_car_rcvdata,
176 .disconnect = ng_car_disconnect,
177 .cmdlist = ng_car_cmdlist,
179 NETGRAPH_INIT(car, &ng_car_typestruct);
185 ng_car_constructor(node_p node)
189 /* Initialize private descriptor. */
190 priv = kmalloc(sizeof(*priv), M_NETGRAPH, M_WAITOK | M_NULLOK | M_ZERO);
194 NG_NODE_SET_PRIVATE(node, priv);
198 * Arbitrary default values
201 priv->upper.hook = NULL;
202 priv->upper.dest = NULL;
203 priv->upper.tc = priv->upper.conf.cbs = NG_CAR_CBS_MIN;
204 priv->upper.te = priv->upper.conf.ebs = NG_CAR_EBS_MIN;
205 priv->upper.conf.cir = NG_CAR_CIR_DFLT;
206 priv->upper.conf.green_action = NG_CAR_ACTION_FORWARD;
207 priv->upper.conf.yellow_action = NG_CAR_ACTION_FORWARD;
208 priv->upper.conf.red_action = NG_CAR_ACTION_DROP;
209 priv->upper.conf.mode = 0;
210 getbinuptime(&priv->upper.lastRefill);
211 priv->upper.q_first = 0;
212 priv->upper.q_last = 0;
213 ng_callout_init(&priv->upper.q_callout);
214 mtx_init(&priv->upper.q_mtx, "ng_car_u", NULL, MTX_DEF);
216 priv->lower.hook = NULL;
217 priv->lower.dest = NULL;
218 priv->lower.tc = priv->lower.conf.cbs = NG_CAR_CBS_MIN;
219 priv->lower.te = priv->lower.conf.ebs = NG_CAR_EBS_MIN;
220 priv->lower.conf.cir = NG_CAR_CIR_DFLT;
221 priv->lower.conf.green_action = NG_CAR_ACTION_FORWARD;
222 priv->lower.conf.yellow_action = NG_CAR_ACTION_FORWARD;
223 priv->lower.conf.red_action = NG_CAR_ACTION_DROP;
224 priv->lower.conf.mode = 0;
225 priv->lower.lastRefill = priv->upper.lastRefill;
226 priv->lower.q_first = 0;
227 priv->lower.q_last = 0;
228 ng_callout_init(&priv->lower.q_callout);
229 mtx_init(&priv->lower.q_mtx, "ng_car_l", NULL, MTX_DEF);
238 ng_car_newhook(node_p node, hook_p hook, const char *name)
240 const priv_p priv = NG_NODE_PRIVATE(node);
242 if (strcmp(name, NG_CAR_HOOK_LOWER) == 0) {
243 priv->lower.hook = hook;
244 priv->upper.dest = hook;
245 bzero(&priv->lower.stats, sizeof(priv->lower.stats));
246 NG_HOOK_SET_PRIVATE(hook, &priv->lower);
247 } else if (strcmp(name, NG_CAR_HOOK_UPPER) == 0) {
248 priv->upper.hook = hook;
249 priv->lower.dest = hook;
250 bzero(&priv->upper.stats, sizeof(priv->upper.stats));
251 NG_HOOK_SET_PRIVATE(hook, &priv->upper);
261 ng_car_rcvdata(hook_p hook, item_p item )
263 struct hookinfo *const hinfo = NG_HOOK_PRIVATE(hook);
268 /* If queue is not empty now then enqueue packet. */
269 if (hinfo->q_first != hinfo->q_last) {
270 ng_car_enqueue(hinfo, item);
276 #define NG_CAR_PERFORM_MATCH_ACTION(a) \
279 case NG_CAR_ACTION_FORWARD: \
282 case NG_CAR_ACTION_MARK: \
283 /* XXX find a way to mark packets (mbuf tag?) */ \
284 ++hinfo->stats.errors; \
286 case NG_CAR_ACTION_DROP: \
288 /* Drop packet and return. */ \
289 NG_FREE_ITEM(item); \
290 ++hinfo->stats.droped_pkts; \
295 /* Packet is counted as 128 tokens for better resolution */
296 if (hinfo->conf.opt & NG_CAR_COUNT_PACKETS) {
299 len = m->m_pkthdr.len;
302 /* Check commited token bucket. */
303 if (hinfo->tc - len >= 0) {
304 /* This packet is green. */
305 ++hinfo->stats.green_pkts;
307 NG_CAR_PERFORM_MATCH_ACTION(hinfo->conf.green_action);
310 /* Refill only if not green without it. */
311 ng_car_refillhook(hinfo);
313 /* Check commited token bucket again after refill. */
314 if (hinfo->tc - len >= 0) {
315 /* This packet is green */
316 ++hinfo->stats.green_pkts;
318 NG_CAR_PERFORM_MATCH_ACTION(hinfo->conf.green_action);
320 /* If not green and mode is SHAPE, enqueue packet. */
321 } else if (hinfo->conf.mode == NG_CAR_SHAPE) {
322 ng_car_enqueue(hinfo, item);
325 /* If not green and mode is RED, calculate probability. */
326 } else if (hinfo->conf.mode == NG_CAR_RED) {
327 /* Is packet is bigger then extended burst? */
328 if (len - (hinfo->tc - len) > hinfo->conf.ebs) {
329 /* This packet is definitely red. */
330 ++hinfo->stats.red_pkts;
332 NG_CAR_PERFORM_MATCH_ACTION(hinfo->conf.red_action);
334 /* Use token bucket to simulate RED-like drop
336 } else if (hinfo->te + (len - hinfo->tc) <
338 /* This packet is yellow */
339 ++hinfo->stats.yellow_pkts;
340 hinfo->te += len - hinfo->tc;
341 /* Go to negative tokens. */
343 NG_CAR_PERFORM_MATCH_ACTION(hinfo->conf.yellow_action);
345 /* This packet is probaly red. */
346 ++hinfo->stats.red_pkts;
348 NG_CAR_PERFORM_MATCH_ACTION(hinfo->conf.red_action);
350 /* If not green and mode is SINGLE/DOUBLE RATE. */
352 /* Check extended token bucket. */
353 if (hinfo->te - len >= 0) {
354 /* This packet is yellow */
355 ++hinfo->stats.yellow_pkts;
357 NG_CAR_PERFORM_MATCH_ACTION(hinfo->conf.yellow_action);
359 /* This packet is red */
360 ++hinfo->stats.red_pkts;
361 NG_CAR_PERFORM_MATCH_ACTION(hinfo->conf.red_action);
366 #undef NG_CAR_PERFORM_MATCH_ACTION
368 NG_FWD_ITEM_HOOK(error, item, hinfo->dest);
370 ++hinfo->stats.errors;
371 ++hinfo->stats.passed_pkts;
377 * Receive a control message.
380 ng_car_rcvmsg(node_p node, item_p item, hook_p lasthook)
382 const priv_p priv = NG_NODE_PRIVATE(node);
383 struct ng_mesg *resp = NULL;
387 NGI_GET_MSG(item, msg);
388 switch (msg->header.typecookie) {
390 switch (msg->header.cmd) {
391 case NGM_CAR_GET_STATS:
392 case NGM_CAR_GETCLR_STATS:
394 struct ng_car_bulkstats *bstats;
396 NG_MKRESPONSE(resp, msg,
397 sizeof(*bstats), M_WAITOK | M_NULLOK);
402 bstats = (struct ng_car_bulkstats *)resp->data;
404 bcopy(&priv->upper.stats, &bstats->downstream,
405 sizeof(bstats->downstream));
406 bcopy(&priv->lower.stats, &bstats->upstream,
407 sizeof(bstats->upstream));
409 if (msg->header.cmd == NGM_CAR_GET_STATS)
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));
417 case NGM_CAR_GET_CONF:
419 struct ng_car_bulkconf *bconf;
421 NG_MKRESPONSE(resp, msg,
422 sizeof(*bconf), M_WAITOK | M_NULLOK);
427 bconf = (struct ng_car_bulkconf *)resp->data;
429 bcopy(&priv->upper.conf, &bconf->downstream,
430 sizeof(bconf->downstream));
431 bcopy(&priv->lower.conf, &bconf->upstream,
432 sizeof(bconf->upstream));
433 /* Convert internal 1/(8*128) of pps into pps */
434 if (bconf->downstream.opt & NG_CAR_COUNT_PACKETS) {
435 bconf->downstream.cir /= 1024;
436 bconf->downstream.pir /= 1024;
437 bconf->downstream.cbs /= 128;
438 bconf->downstream.ebs /= 128;
440 if (bconf->upstream.opt & NG_CAR_COUNT_PACKETS) {
441 bconf->upstream.cir /= 1024;
442 bconf->upstream.pir /= 1024;
443 bconf->upstream.cbs /= 128;
444 bconf->upstream.ebs /= 128;
448 case NGM_CAR_SET_CONF:
450 struct ng_car_bulkconf *const bconf =
451 (struct ng_car_bulkconf *)msg->data;
453 /* Check for invalid or illegal config. */
454 if (msg->header.arglen != sizeof(*bconf)) {
458 /* Convert pps into internal 1/(8*128) of pps */
459 if (bconf->downstream.opt & NG_CAR_COUNT_PACKETS) {
460 bconf->downstream.cir *= 1024;
461 bconf->downstream.pir *= 1024;
462 bconf->downstream.cbs *= 125;
463 bconf->downstream.ebs *= 125;
465 if (bconf->upstream.opt & NG_CAR_COUNT_PACKETS) {
466 bconf->upstream.cir *= 1024;
467 bconf->upstream.pir *= 1024;
468 bconf->upstream.cbs *= 125;
469 bconf->upstream.ebs *= 125;
471 if ((bconf->downstream.cir > 1000000000) ||
472 (bconf->downstream.pir > 1000000000) ||
473 (bconf->upstream.cir > 1000000000) ||
474 (bconf->upstream.pir > 1000000000) ||
475 (bconf->downstream.cbs == 0 &&
476 bconf->downstream.ebs == 0) ||
477 (bconf->upstream.cbs == 0 &&
478 bconf->upstream.ebs == 0))
483 if ((bconf->upstream.mode == NG_CAR_SHAPE) &&
484 (bconf->upstream.cir == 0)) {
488 if ((bconf->downstream.mode == NG_CAR_SHAPE) &&
489 (bconf->downstream.cir == 0)) {
494 /* Copy downstream config. */
495 bcopy(&bconf->downstream, &priv->upper.conf,
496 sizeof(priv->upper.conf));
497 priv->upper.tc = priv->upper.conf.cbs;
498 if (priv->upper.conf.mode == NG_CAR_RED ||
499 priv->upper.conf.mode == NG_CAR_SHAPE) {
502 priv->upper.te = priv->upper.conf.ebs;
505 /* Copy upstream config. */
506 bcopy(&bconf->upstream, &priv->lower.conf,
507 sizeof(priv->lower.conf));
508 priv->lower.tc = priv->lower.conf.cbs;
509 if (priv->lower.conf.mode == NG_CAR_RED ||
510 priv->lower.conf.mode == NG_CAR_SHAPE) {
513 priv->lower.te = priv->lower.conf.ebs;
526 NG_RESPOND_MSG(error, node, item, resp);
532 * Do local shutdown processing.
535 ng_car_shutdown(node_p node)
537 const priv_p priv = NG_NODE_PRIVATE(node);
539 ng_uncallout(&priv->upper.q_callout, node);
540 ng_uncallout(&priv->lower.q_callout, node);
541 mtx_destroy(&priv->upper.q_mtx);
542 mtx_destroy(&priv->lower.q_mtx);
543 NG_NODE_UNREF(priv->node);
544 kfree(priv, M_NETGRAPH);
549 * Hook disconnection.
551 * For this type, removal of the last link destroys the node.
554 ng_car_disconnect(hook_p hook)
556 struct hookinfo *const hinfo = NG_HOOK_PRIVATE(hook);
557 const node_p node = NG_HOOK_NODE(hook);
558 const priv_p priv = NG_NODE_PRIVATE(node);
561 /* Purge queue if not empty. */
562 while (hinfo->q_first != hinfo->q_last) {
563 NG_FREE_M(hinfo->q[hinfo->q_first]);
565 if (hinfo->q_first >= NG_CAR_QUEUE_SIZE)
568 /* Remove hook refs. */
569 if (hinfo->hook == priv->upper.hook)
570 priv->lower.dest = NULL;
572 priv->upper.dest = NULL;
575 /* Already shutting down? */
576 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
577 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
578 ng_rmnode_self(NG_HOOK_NODE(hook));
583 * Hook's token buckets refillment.
586 ng_car_refillhook(struct hookinfo *h)
588 struct bintime newt, deltat;
589 unsigned int deltat_us;
591 /* Get current time. */
594 /* Get time delta since last refill. */
596 bintime_sub(&deltat, &h->lastRefill);
598 /* Time must go forward. */
599 if (deltat.sec < 0) {
600 h->lastRefill = newt;
604 /* But not too far forward. */
605 if (deltat.sec >= 1000) {
606 deltat_us = (1000 << 20);
608 /* convert bintime to the 1/(2^20) of sec */
609 deltat_us = (deltat.sec << 20) + (deltat.frac >> 44);
612 if (h->conf.mode == NG_CAR_SINGLE_RATE) {
614 /* Refill commited token bucket. */
615 h->tc += (h->conf.cir * deltat_us) >> 23;
616 delta = h->tc - h->conf.cbs;
620 /* Refill exceeded token bucket. */
622 if (h->te > ((int64_t)h->conf.ebs))
626 } else if (h->conf.mode == NG_CAR_DOUBLE_RATE) {
627 /* Refill commited token bucket. */
628 h->tc += (h->conf.cir * deltat_us) >> 23;
629 if (h->tc > ((int64_t)h->conf.cbs))
632 /* Refill peak token bucket. */
633 h->te += (h->conf.pir * deltat_us) >> 23;
634 if (h->te > ((int64_t)h->conf.ebs))
637 } else { /* RED or SHAPE mode. */
638 /* Refill commited token bucket. */
639 h->tc += (h->conf.cir * deltat_us) >> 23;
640 if (h->tc > ((int64_t)h->conf.cbs))
644 /* Remember this moment. */
645 h->lastRefill = newt;
649 * Schedule callout when we will have required tokens.
652 ng_car_schedule(struct hookinfo *hinfo)
656 delay = (-(hinfo->tc)) * hz * 8 / hinfo->conf.cir + 1;
658 ng_callout(&hinfo->q_callout, NG_HOOK_NODE(hinfo->hook), hinfo->hook,
659 delay, &ng_car_q_event, NULL, 0);
663 * Queue processing callout handler.
666 ng_car_q_event(node_p node, hook_p hook, void *arg, int arg2)
668 struct hookinfo *hinfo = NG_HOOK_PRIVATE(hook);
672 /* Refill tokens for time we have slept. */
673 ng_car_refillhook(hinfo);
675 /* If we have some tokens */
676 while (hinfo->tc >= 0) {
679 m = hinfo->q[hinfo->q_first];
680 NG_SEND_DATA_ONLY(error, hinfo->dest, m);
682 ++hinfo->stats.errors;
683 ++hinfo->stats.passed_pkts;
687 if (hinfo->q_first >= NG_CAR_QUEUE_SIZE)
690 /* Stop if none left. */
691 if (hinfo->q_first == hinfo->q_last)
694 /* If we have more packet, try it. */
695 m = hinfo->q[hinfo->q_first];
696 if (hinfo->conf.opt & NG_CAR_COUNT_PACKETS) {
699 hinfo->tc -= m->m_pkthdr.len;
703 /* If something left */
704 if (hinfo->q_first != hinfo->q_last)
705 /* Schedule queue processing. */
706 ng_car_schedule(hinfo);
713 ng_car_enqueue(struct hookinfo *hinfo, item_p item)
721 /* Lock queue mutex. */
722 mtx_lock(&hinfo->q_mtx);
724 /* Calculate used queue length. */
725 len = hinfo->q_last - hinfo->q_first;
727 len += NG_CAR_QUEUE_SIZE;
729 /* If queue is overflowed or we have no RED tokens. */
730 if ((len >= (NG_CAR_QUEUE_SIZE - 1)) ||
731 (hinfo->te + len >= NG_CAR_QUEUE_SIZE)) {
733 ++hinfo->stats.red_pkts;
734 ++hinfo->stats.droped_pkts;
739 /* This packet is yellow. */
740 ++hinfo->stats.yellow_pkts;
742 /* Enqueue packet. */
743 hinfo->q[hinfo->q_last] = m;
745 if (hinfo->q_last >= NG_CAR_QUEUE_SIZE)
748 /* Use RED tokens. */
749 if (len > NG_CAR_QUEUE_MIN_TH)
750 hinfo->te += len - NG_CAR_QUEUE_MIN_TH;
752 /* If this is a first packet in the queue. */
754 if (hinfo->conf.opt & NG_CAR_COUNT_PACKETS) {
757 hinfo->tc -= m->m_pkthdr.len;
760 /* Schedule queue processing. */
761 ng_car_schedule(hinfo);
765 /* Unlock queue mutex. */
766 mtx_unlock(&hinfo->q_mtx);