From e6fe511448b2b13fe8eddfdfafe11d4411faa580 Mon Sep 17 00:00:00 2001 From: glebius Date: Fri, 23 Dec 2005 19:14:38 +0000 Subject: Implement an upper limit for packets per second sent by node. --- sys/netgraph/ng_source.c | 38 ++++++++++++++++++++++++++++++++++++++ sys/netgraph/ng_source.h | 13 +++++++++---- 2 files changed, 47 insertions(+), 4 deletions(-) (limited to 'sys') diff --git a/sys/netgraph/ng_source.c b/sys/netgraph/ng_source.c index 16c10bb..2413a5d 100644 --- a/sys/netgraph/ng_source.c +++ b/sys/netgraph/ng_source.c @@ -181,6 +181,13 @@ static const struct ng_cmdlist ng_source_cmds[] = { &ng_parse_string_type, NULL }, + { + NGM_SOURCE_COOKIE, + NGM_SOURCE_SETPPS, + "setpps", + &ng_parse_uint32_type, + NULL + }, { 0 } }; @@ -352,6 +359,21 @@ ng_source_rcvmsg(node_p node, item_p item, hook_p lasthook) ng_source_store_output_ifp(sc, ifname); break; } + case NGM_SOURCE_SETPPS: + { + uint32_t pps; + + if (msg->header.arglen != sizeof(uint32_t)) { + error = EINVAL; + break; + } + + pps = *(uint32_t *)msg->data; + + sc->stats.maxPps = pps; + + break; + } default: error = EINVAL; break; @@ -548,6 +570,7 @@ ng_source_start(sc_p sc, uint64_t packets) timevalclear(&sc->stats.elapsedTime); timevalclear(&sc->stats.endTime); getmicrotime(&sc->stats.startTime); + getmicrotime(&sc->stats.lastTime); ng_callout(&sc->intr_ch, sc->node, NULL, 0, ng_source_intr, sc, 0); @@ -593,6 +616,21 @@ ng_source_intr(node_p node, hook_p hook, void *arg1, int arg2) } else packets = sc->snd_queue.ifq_len; + if (sc->stats.maxPps != 0) { + struct timeval now, elapsed; + uint64_t usec; + int maxpkt; + + getmicrotime(&now); + elapsed = now; + timevalsub(&elapsed, &sc->stats.lastTime); + usec = elapsed.tv_sec * 1000000 + elapsed.tv_usec; + maxpkt = (uint64_t)sc->stats.maxPps * usec / 1000000; + sc->stats.lastTime = now; + if (packets > maxpkt) + packets = maxpkt; + } + ng_source_send(sc, packets, NULL); if (sc->packets == 0) ng_source_stop(sc); diff --git a/sys/netgraph/ng_source.h b/sys/netgraph/ng_source.h index 0d84f06..fcfb151 100644 --- a/sys/netgraph/ng_source.h +++ b/sys/netgraph/ng_source.h @@ -52,13 +52,15 @@ /* Statistics structure returned by NGM_SOURCE_GET_STATS */ struct ng_source_stats { - u_int64_t outOctets; - u_int64_t outFrames; - u_int32_t queueOctets; - u_int32_t queueFrames; + uint64_t outOctets; + uint64_t outFrames; + uint32_t queueOctets; + uint32_t queueFrames; + uint32_t maxPps; struct timeval startTime; struct timeval endTime; struct timeval elapsedTime; + struct timeval lastTime; }; extern const struct ng_parse_type ng_source_timeval_type; @@ -68,9 +70,11 @@ extern const struct ng_parse_type ng_source_timeval_type; { "outFrames", &ng_parse_uint64_type }, \ { "queueOctets", &ng_parse_uint32_type }, \ { "queueFrames", &ng_parse_uint32_type }, \ + { "maxPps", &ng_parse_uint32_type }, \ { "startTime", &ng_source_timeval_type }, \ { "endTime", &ng_source_timeval_type }, \ { "elapsedTime", &ng_source_timeval_type }, \ + { "lastTime", &ng_source_timeval_type }, \ { NULL } \ } @@ -83,6 +87,7 @@ enum { NGM_SOURCE_STOP, /* stop sending queued data */ NGM_SOURCE_CLR_DATA, /* clear the queued data */ NGM_SOURCE_SETIFACE, /* configure downstream iface */ + NGM_SOURCE_SETPPS, /* rate-limiting packets per second */ }; #endif /* _NETGRAPH_NG_SOURCE_H_ */ -- cgit v1.1