Get rid of unused parameter to rate_limit_start().
[sliver-openvswitch.git] / secchan / ratelimit.c
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  *
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:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
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
27  * SOFTWARE.
28  *
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.
32  */
33
34 #include <config.h>
35 #include "ratelimit.h"
36 #include <arpa/inet.h>
37 #include <stdlib.h>
38 #include "ofpbuf.h"
39 #include "openflow/openflow.h"
40 #include "poll-loop.h"
41 #include "queue.h"
42 #include "rconn.h"
43 #include "secchan.h"
44 #include "status.h"
45 #include "timeval.h"
46 #include "vconn.h"
47
48 struct rate_limiter {
49     const struct settings *s;
50     struct rconn *remote_rconn;
51
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. */
56
57     /* Token bucket.
58      *
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. */
65
66     /* Transmission queue. */
67     int n_txq;                  /* No. of packets waiting in rconn for tx. */
68
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. */
74 };
75
76 /* Drop a packet from the longest queue in 'rl'. */
77 static void
78 drop_packet(struct rate_limiter *rl)
79 {
80     struct ofp_queue *longest;  /* Queue currently selected as longest. */
81     int n_longest;              /* # of queues of same length as 'longest'. */
82     struct ofp_queue *q;
83
84     longest = &rl->queues[0];
85     n_longest = 1;
86     for (q = &rl->queues[0]; q < &rl->queues[OFPP_MAX]; q++) {
87         if (longest->n < q->n) {
88             longest = q;
89             n_longest = 1;
90         } else if (longest->n == q->n) {
91             n_longest++;
92
93             /* Randomly select one of the longest queues, with a uniform
94              * distribution (Knuth algorithm 3.4.2R). */
95             if (!random_range(n_longest)) {
96                 longest = q;
97             }
98         }
99     }
100
101     /* FIXME: do we want to pop the tail instead? */
102     ofpbuf_delete(queue_pop_head(longest));
103     rl->n_queued--;
104 }
105
106 /* Remove and return the next packet to transmit (in round-robin order). */
107 static struct ofpbuf *
108 dequeue_packet(struct rate_limiter *rl)
109 {
110     unsigned int i;
111
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];
115         if (q->n) {
116             rl->next_tx_port = (port + 1) % OFPP_MAX;
117             rl->n_queued--;
118             return queue_pop_head(q);
119         }
120     }
121     NOT_REACHED();
122 }
123
124 /* Add tokens to the bucket based on elapsed time. */
125 static void
126 refill_bucket(struct rate_limiter *rl)
127 {
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) {
132         rl->last_fill = now;
133         rl->tokens = MIN(tokens, s->burst_limit * 1000);
134     }
135 }
136
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
139  * removed.) */
140 static bool
141 get_token(struct rate_limiter *rl)
142 {
143     if (rl->tokens >= 1000) {
144         rl->tokens -= 1000;
145         return true;
146     } else {
147         return false;
148     }
149 }
150
151 static bool
152 rate_limit_local_packet_cb(struct relay *r, void *rl_)
153 {
154     struct rate_limiter *rl = rl_;
155     const struct settings *s = rl->s;
156     struct ofp_packet_in *opi;
157
158     opi = get_ofp_packet_in(r);
159     if (!opi) {
160         return false;
161     }
162
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. */
167         return false;
168     }
169
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. */
173         rl->n_normal++;
174         return false;
175     } else {
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) {
180             drop_packet(rl);
181         }
182         queue_push_tail(&rl->queues[port], ofpbuf_clone(msg));
183         rl->n_queued++;
184         rl->n_limited++;
185         return true;
186     }
187 }
188
189 static void
190 rate_limit_status_cb(struct status_reply *sr, void *rl_)
191 {
192     struct rate_limiter *rl = rl_;
193
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);
198 }
199
200 static void
201 rate_limit_periodic_cb(void *rl_)
202 {
203     struct rate_limiter *rl = rl_;
204     int i;
205
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. */
208     refill_bucket(rl);
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
213          * handle. */
214         struct ofpbuf *b = dequeue_packet(rl);
215         if (rconn_send_with_limit(rl->remote_rconn, b, &rl->n_txq, 10)) {
216             rl->n_tx_dropped++;
217         }
218     }
219 }
220
221 static void
222 rate_limit_wait_cb(void *rl_)
223 {
224     struct rate_limiter *rl = rl_;
225     if (rl->n_queued) {
226         if (rl->tokens >= 1000) {
227             /* We can transmit more packets as soon as we're called again. */
228             poll_immediate_wake();
229         } else {
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);
233         }
234     }
235 }
236
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 */
243 };
244
245 void
246 rate_limit_start(struct secchan *secchan, const struct settings *s,
247                  struct switch_status *ss, struct rconn *remote)
248 {
249     struct rate_limiter *rl;
250     size_t i;
251
252     rl = xcalloc(1, sizeof *rl);
253     rl->s = s;
254     rl->remote_rconn = remote;
255     for (i = 0; i < ARRAY_SIZE(rl->queues); i++) {
256         queue_init(&rl->queues[i]);
257     }
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);
263 }