2 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <sys/types.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
25 #include "openflow/openflow.h"
26 #include "poll-loop.h"
27 #include "port-array.h"
36 /* Client-supplied parameters. */
37 int rate_limit; /* Packets added to bucket per second. */
38 int burst_limit; /* Maximum token bucket size, in packets. */
40 /* One queue per physical port. */
41 struct port_array queues; /* Array of "struct ovs_queue *". */
42 int n_queued; /* Sum over queues[*].n. */
43 unsigned int last_tx_port; /* Last port checked in round-robin. */
47 * It costs 1000 tokens to send a single packet_in message. A single token
48 * per message would be more straightforward, but this choice lets us avoid
49 * round-off error in refill_bucket()'s calculation of how many tokens to
50 * add to the bucket, since no division step is needed. */
51 long long int last_fill; /* Time at which we last added tokens. */
52 int tokens; /* Current number of tokens. */
54 /* Transmission queue. */
55 int n_txq; /* No. of packets waiting in rconn for tx. */
57 /* Statistics reporting. */
58 unsigned long long n_normal; /* # txed w/o rate limit queuing. */
59 unsigned long long n_limited; /* # queued for rate limiting. */
60 unsigned long long n_queue_dropped; /* # dropped due to queue overflow. */
63 struct status_category *ss_cat;
66 static struct ofpbuf *
67 dequeue_packet(struct pinsched *ps, struct ovs_queue *q,
70 struct ofpbuf *packet = queue_pop_head(q);
73 port_array_set(&ps->queues, port_no, NULL);
79 /* Drop a packet from the longest queue in 'ps'. */
81 drop_packet(struct pinsched *ps)
83 struct ovs_queue *longest; /* Queue currently selected as longest. */
84 int n_longest; /* # of queues of same length as 'longest'. */
85 unsigned int longest_port_no;
89 ps->n_queue_dropped++;
91 longest = port_array_first(&ps->queues, &port_no);
92 longest_port_no = port_no;
94 while ((q = port_array_next(&ps->queues, &port_no)) != NULL) {
95 if (longest->n < q->n) {
98 } else if (longest->n == q->n) {
101 /* Randomly select one of the longest queues, with a uniform
102 * distribution (Knuth algorithm 3.4.2R). */
103 if (!random_range(n_longest)) {
105 longest_port_no = port_no;
110 /* FIXME: do we want to pop the tail instead? */
111 ofpbuf_delete(dequeue_packet(ps, longest, longest_port_no));
114 /* Remove and return the next packet to transmit (in round-robin order). */
115 static struct ofpbuf *
116 get_tx_packet(struct pinsched *ps)
118 struct ovs_queue *q = port_array_next(&ps->queues, &ps->last_tx_port);
120 q = port_array_first(&ps->queues, &ps->last_tx_port);
122 return dequeue_packet(ps, q, ps->last_tx_port);
125 /* Add tokens to the bucket based on elapsed time. */
127 refill_bucket(struct pinsched *ps)
129 long long int now = time_msec();
130 long long int tokens = (now - ps->last_fill) * ps->rate_limit + ps->tokens;
131 if (tokens >= 1000) {
133 ps->tokens = MIN(tokens, ps->burst_limit * 1000);
137 /* Attempts to remove enough tokens from 'ps' to transmit a packet. Returns
138 * true if successful, false otherwise. (In the latter case no tokens are
141 get_token(struct pinsched *ps)
143 if (ps->tokens >= 1000) {
152 pinsched_send(struct pinsched *ps, uint16_t port_no,
153 struct ofpbuf *packet, pinsched_tx_cb *cb, void *aux)
157 } else if (!ps->n_queued && get_token(ps)) {
158 /* In the common case where we are not constrained by the rate limit,
159 * let the packet take the normal path. */
163 /* Otherwise queue it up for the periodic callback to drain out. */
166 /* We are called with a buffer obtained from dpif_recv() that has much
167 * more allocated space than actual content most of the time. Since
168 * we're going to store the packet for some time, free up that
169 * otherwise wasted space. */
172 if (ps->n_queued >= ps->burst_limit) {
175 q = port_array_get(&ps->queues, port_no);
177 q = xmalloc(sizeof *q);
179 port_array_set(&ps->queues, port_no, q);
181 queue_push_tail(q, packet);
188 pinsched_status_cb(struct status_reply *sr, void *ps_)
190 struct pinsched *ps = ps_;
192 status_reply_put(sr, "normal=%llu", ps->n_normal);
193 status_reply_put(sr, "limited=%llu", ps->n_limited);
194 status_reply_put(sr, "queue-dropped=%llu", ps->n_queue_dropped);
198 pinsched_run(struct pinsched *ps, pinsched_tx_cb *cb, void *aux)
203 /* Drain some packets out of the bucket if possible, but limit the
204 * number of iterations to allow other code to get work done too. */
206 for (i = 0; ps->n_queued && get_token(ps) && i < 50; i++) {
207 cb(get_tx_packet(ps), aux);
213 pinsched_wait(struct pinsched *ps)
215 if (ps && ps->n_queued) {
216 if (ps->tokens >= 1000) {
217 /* We can transmit more packets as soon as we're called again. */
218 poll_immediate_wake();
220 /* We have to wait for the bucket to re-fill. We could calculate
221 * the exact amount of time here for increased smoothness. */
222 poll_timer_wait(TIME_UPDATE_INTERVAL / 2);
227 /* Creates and returns a scheduler for sending packet-in messages. */
229 pinsched_create(int rate_limit, int burst_limit, struct switch_status *ss)
233 ps = xzalloc(sizeof *ps);
234 port_array_init(&ps->queues);
236 ps->last_tx_port = PORT_ARRAY_SIZE;
237 ps->last_fill = time_msec();
238 ps->tokens = rate_limit * 100;
242 ps->n_queue_dropped = 0;
243 pinsched_set_limits(ps, rate_limit, burst_limit);
246 ps->ss_cat = switch_status_register(ss, "rate-limit",
247 pinsched_status_cb, ps);
254 pinsched_destroy(struct pinsched *ps)
257 struct ovs_queue *queue;
258 unsigned int port_no;
260 PORT_ARRAY_FOR_EACH (queue, &ps->queues, port_no) {
261 queue_destroy(queue);
264 port_array_destroy(&ps->queues);
265 switch_status_unregister(ps->ss_cat);
271 pinsched_get_limits(const struct pinsched *ps,
272 int *rate_limit, int *burst_limit)
274 *rate_limit = ps->rate_limit;
275 *burst_limit = ps->burst_limit;
279 pinsched_set_limits(struct pinsched *ps, int rate_limit, int burst_limit)
281 if (rate_limit <= 0) {
284 if (burst_limit <= 0) {
285 burst_limit = rate_limit / 4;
287 burst_limit = MAX(burst_limit, 1);
288 burst_limit = MIN(burst_limit, INT_MAX / 1000);
290 ps->rate_limit = rate_limit;
291 ps->burst_limit = burst_limit;
292 while (ps->n_queued > burst_limit) {