1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
4 * We are making the OpenFlow specification and associated documentation
5 * (Software) available for public use and benefit with the expectation
6 * that others will use, modify and enhance the Software and contribute
7 * those enhancements back to the community. However, since we would
8 * like to make the Software available for broadest use, with as few
9 * restrictions as possible permission is hereby granted, free of
10 * charge, to any person obtaining a copy of this Software to deal in
11 * the Software under the copyrights without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * The name and trademarks of copyright holder(s) may NOT be used in
30 * advertising or publicity pertaining to the Software or any
31 * derivatives without specific, written prior permission.
35 #include "ratelimit.h"
36 #include <arpa/inet.h>
39 #include "openflow/openflow.h"
40 #include "poll-loop.h"
49 const struct settings *s;
50 struct rconn *remote_rconn;
52 /* One queue per physical port. */
53 struct ofp_queue queues[OFPP_MAX];
54 int n_queued; /* Sum over queues[*].n. */
55 int next_tx_port; /* Next port to check in round-robin. */
59 * It costs 1000 tokens to send a single packet_in message. A single token
60 * per message would be more straightforward, but this choice lets us avoid
61 * round-off error in refill_bucket()'s calculation of how many tokens to
62 * add to the bucket, since no division step is needed. */
63 long long int last_fill; /* Time at which we last added tokens. */
64 int tokens; /* Current number of tokens. */
66 /* Transmission queue. */
67 int n_txq; /* No. of packets waiting in rconn for tx. */
69 /* Statistics reporting. */
70 unsigned long long n_normal; /* # txed w/o rate limit queuing. */
71 unsigned long long n_limited; /* # queued for rate limiting. */
72 unsigned long long n_queue_dropped; /* # dropped due to queue overflow. */
73 unsigned long long n_tx_dropped; /* # dropped due to tx overflow. */
76 /* Drop a packet from the longest queue in 'rl'. */
78 drop_packet(struct rate_limiter *rl)
80 struct ofp_queue *longest; /* Queue currently selected as longest. */
81 int n_longest; /* # of queues of same length as 'longest'. */
84 longest = &rl->queues[0];
86 for (q = &rl->queues[0]; q < &rl->queues[OFPP_MAX]; q++) {
87 if (longest->n < q->n) {
90 } else if (longest->n == q->n) {
93 /* Randomly select one of the longest queues, with a uniform
94 * distribution (Knuth algorithm 3.4.2R). */
95 if (!random_range(n_longest)) {
101 /* FIXME: do we want to pop the tail instead? */
102 ofpbuf_delete(queue_pop_head(longest));
106 /* Remove and return the next packet to transmit (in round-robin order). */
107 static struct ofpbuf *
108 dequeue_packet(struct rate_limiter *rl)
112 for (i = 0; i < OFPP_MAX; i++) {
113 unsigned int port = (rl->next_tx_port + i) % OFPP_MAX;
114 struct ofp_queue *q = &rl->queues[port];
116 rl->next_tx_port = (port + 1) % OFPP_MAX;
118 return queue_pop_head(q);
124 /* Add tokens to the bucket based on elapsed time. */
126 refill_bucket(struct rate_limiter *rl)
128 const struct settings *s = rl->s;
129 long long int now = time_msec();
130 long long int tokens = (now - rl->last_fill) * s->rate_limit + rl->tokens;
131 if (tokens >= 1000) {
133 rl->tokens = MIN(tokens, s->burst_limit * 1000);
137 /* Attempts to remove enough tokens from 'rl' to transmit a packet. Returns
138 * true if successful, false otherwise. (In the latter case no tokens are
141 get_token(struct rate_limiter *rl)
143 if (rl->tokens >= 1000) {
152 rate_limit_local_packet_cb(struct relay *r, void *rl_)
154 struct rate_limiter *rl = rl_;
155 const struct settings *s = rl->s;
156 struct ofp_packet_in *opi;
158 opi = get_ofp_packet_in(r);
163 if (opi->reason == OFPR_ACTION) {
164 /* Don't rate-limit 'ofp-packet_in's generated by flows that the
165 * controller set up. XXX we should really just rate-limit them
166 * *separately* so that no one can flood the controller this way. */
170 if (!rl->n_queued && get_token(rl)) {
171 /* In the common case where we are not constrained by the rate limit,
172 * let the packet take the normal path. */
176 /* Otherwise queue it up for the periodic callback to drain out. */
177 struct ofpbuf *msg = r->halves[HALF_LOCAL].rxbuf;
178 int port = ntohs(opi->in_port) % OFPP_MAX;
179 if (rl->n_queued >= s->burst_limit) {
182 queue_push_tail(&rl->queues[port], ofpbuf_clone(msg));
190 rate_limit_status_cb(struct status_reply *sr, void *rl_)
192 struct rate_limiter *rl = rl_;
194 status_reply_put(sr, "normal=%llu", rl->n_normal);
195 status_reply_put(sr, "limited=%llu", rl->n_limited);
196 status_reply_put(sr, "queue-dropped=%llu", rl->n_queue_dropped);
197 status_reply_put(sr, "tx-dropped=%llu", rl->n_tx_dropped);
201 rate_limit_periodic_cb(void *rl_)
203 struct rate_limiter *rl = rl_;
206 /* Drain some packets out of the bucket if possible, but limit the number
207 * of iterations to allow other code to get work done too. */
209 for (i = 0; rl->n_queued && get_token(rl) && i < 50; i++) {
210 /* Use a small, arbitrary limit for the amount of queuing to do here,
211 * because the TCP connection is responsible for buffering and there is
212 * no point in trying to transmit faster than the TCP connection can
214 struct ofpbuf *b = dequeue_packet(rl);
215 if (rconn_send_with_limit(rl->remote_rconn, b, &rl->n_txq, 10)) {
222 rate_limit_wait_cb(void *rl_)
224 struct rate_limiter *rl = rl_;
226 if (rl->tokens >= 1000) {
227 /* We can transmit more packets as soon as we're called again. */
228 poll_immediate_wake();
230 /* We have to wait for the bucket to re-fill. We could calculate
231 * the exact amount of time here for increased smoothness. */
232 poll_timer_wait(TIME_UPDATE_INTERVAL / 2);
237 static struct hook_class rate_limit_hook_class = {
238 rate_limit_local_packet_cb, /* local_packet_cb */
239 NULL, /* remote_packet_cb */
240 rate_limit_periodic_cb, /* periodic_cb */
241 rate_limit_wait_cb, /* wait_cb */
242 NULL, /* closing_cb */
246 rate_limit_start(struct secchan *secchan, const struct settings *s,
247 struct switch_status *ss, struct rconn *remote)
249 struct rate_limiter *rl;
252 rl = xcalloc(1, sizeof *rl);
254 rl->remote_rconn = remote;
255 for (i = 0; i < ARRAY_SIZE(rl->queues); i++) {
256 queue_init(&rl->queues[i]);
258 rl->last_fill = time_msec();
259 rl->tokens = s->rate_limit * 100;
260 switch_status_register_category(ss, "rate-limit",
261 rate_limit_status_cb, rl);
262 add_hook(secchan, &rate_limit_hook_class, rl);