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 <arpa/inet.h>
23 #include "openflow/openflow.h"
24 #include "poll-loop.h"
25 #include "port-array.h"
34 /* Client-supplied parameters. */
35 int rate_limit; /* Packets added to bucket per second. */
36 int burst_limit; /* Maximum token bucket size, in packets. */
38 /* One queue per physical port. */
39 struct port_array queues; /* Array of "struct ovs_queue *". */
40 int n_queued; /* Sum over queues[*].n. */
41 unsigned int last_tx_port; /* Last port checked in round-robin. */
45 * It costs 1000 tokens to send a single packet_in message. A single token
46 * per message would be more straightforward, but this choice lets us avoid
47 * round-off error in refill_bucket()'s calculation of how many tokens to
48 * add to the bucket, since no division step is needed. */
49 long long int last_fill; /* Time at which we last added tokens. */
50 int tokens; /* Current number of tokens. */
52 /* Transmission queue. */
53 int n_txq; /* No. of packets waiting in rconn for tx. */
55 /* Statistics reporting. */
56 unsigned long long n_normal; /* # txed w/o rate limit queuing. */
57 unsigned long long n_limited; /* # queued for rate limiting. */
58 unsigned long long n_queue_dropped; /* # dropped due to queue overflow. */
61 struct status_category *ss_cat;
64 static struct ofpbuf *
65 dequeue_packet(struct pinsched *ps, struct ovs_queue *q,
68 struct ofpbuf *packet = queue_pop_head(q);
71 port_array_set(&ps->queues, port_no, NULL);
77 /* Drop a packet from the longest queue in 'ps'. */
79 drop_packet(struct pinsched *ps)
81 struct ovs_queue *longest; /* Queue currently selected as longest. */
82 int n_longest; /* # of queues of same length as 'longest'. */
83 unsigned int longest_port_no;
87 ps->n_queue_dropped++;
89 longest = port_array_first(&ps->queues, &port_no);
90 longest_port_no = port_no;
92 while ((q = port_array_next(&ps->queues, &port_no)) != NULL) {
93 if (longest->n < q->n) {
96 } else if (longest->n == q->n) {
99 /* Randomly select one of the longest queues, with a uniform
100 * distribution (Knuth algorithm 3.4.2R). */
101 if (!random_range(n_longest)) {
103 longest_port_no = port_no;
108 /* FIXME: do we want to pop the tail instead? */
109 ofpbuf_delete(dequeue_packet(ps, longest, longest_port_no));
112 /* Remove and return the next packet to transmit (in round-robin order). */
113 static struct ofpbuf *
114 get_tx_packet(struct pinsched *ps)
116 struct ovs_queue *q = port_array_next(&ps->queues, &ps->last_tx_port);
118 q = port_array_first(&ps->queues, &ps->last_tx_port);
120 return dequeue_packet(ps, q, ps->last_tx_port);
123 /* Add tokens to the bucket based on elapsed time. */
125 refill_bucket(struct pinsched *ps)
127 long long int now = time_msec();
128 long long int tokens = (now - ps->last_fill) * ps->rate_limit + ps->tokens;
129 if (tokens >= 1000) {
131 ps->tokens = MIN(tokens, ps->burst_limit * 1000);
135 /* Attempts to remove enough tokens from 'ps' to transmit a packet. Returns
136 * true if successful, false otherwise. (In the latter case no tokens are
139 get_token(struct pinsched *ps)
141 if (ps->tokens >= 1000) {
150 pinsched_send(struct pinsched *ps, uint16_t port_no,
151 struct ofpbuf *packet, pinsched_tx_cb *cb, void *aux)
155 } else if (!ps->n_queued && get_token(ps)) {
156 /* In the common case where we are not constrained by the rate limit,
157 * let the packet take the normal path. */
161 /* Otherwise queue it up for the periodic callback to drain out. */
164 /* We are called with a buffer obtained from dpif_recv() that has much
165 * more allocated space than actual content most of the time. Since
166 * we're going to store the packet for some time, free up that
167 * otherwise wasted space. */
170 if (ps->n_queued >= ps->burst_limit) {
173 q = port_array_get(&ps->queues, port_no);
175 q = xmalloc(sizeof *q);
177 port_array_set(&ps->queues, port_no, q);
179 queue_push_tail(q, packet);
186 pinsched_status_cb(struct status_reply *sr, void *ps_)
188 struct pinsched *ps = ps_;
190 status_reply_put(sr, "normal=%llu", ps->n_normal);
191 status_reply_put(sr, "limited=%llu", ps->n_limited);
192 status_reply_put(sr, "queue-dropped=%llu", ps->n_queue_dropped);
196 pinsched_run(struct pinsched *ps, pinsched_tx_cb *cb, void *aux)
201 /* Drain some packets out of the bucket if possible, but limit the
202 * number of iterations to allow other code to get work done too. */
204 for (i = 0; ps->n_queued && get_token(ps) && i < 50; i++) {
205 cb(get_tx_packet(ps), aux);
211 pinsched_wait(struct pinsched *ps)
213 if (ps && ps->n_queued) {
214 if (ps->tokens >= 1000) {
215 /* We can transmit more packets as soon as we're called again. */
216 poll_immediate_wake();
218 /* We have to wait for the bucket to re-fill. We could calculate
219 * the exact amount of time here for increased smoothness. */
220 poll_timer_wait(TIME_UPDATE_INTERVAL / 2);
225 /* Creates and returns a scheduler for sending packet-in messages. */
227 pinsched_create(int rate_limit, int burst_limit, struct switch_status *ss)
231 ps = xcalloc(1, sizeof *ps);
232 port_array_init(&ps->queues);
234 ps->last_tx_port = PORT_ARRAY_SIZE;
235 ps->last_fill = time_msec();
236 ps->tokens = rate_limit * 100;
240 ps->n_queue_dropped = 0;
241 pinsched_set_limits(ps, rate_limit, burst_limit);
244 ps->ss_cat = switch_status_register(ss, "rate-limit",
245 pinsched_status_cb, ps);
252 pinsched_destroy(struct pinsched *ps)
255 struct ovs_queue *queue;
256 unsigned int port_no;
258 PORT_ARRAY_FOR_EACH (queue, &ps->queues, port_no) {
259 queue_destroy(queue);
262 port_array_destroy(&ps->queues);
263 switch_status_unregister(ps->ss_cat);
269 pinsched_set_limits(struct pinsched *ps, int rate_limit, int burst_limit)
271 if (rate_limit <= 0) {
274 if (burst_limit <= 0) {
275 burst_limit = rate_limit / 4;
277 burst_limit = MAX(burst_limit, 1);
278 burst_limit = MIN(burst_limit, INT_MAX / 1000);
280 ps->rate_limit = rate_limit;
281 ps->burst_limit = burst_limit;
282 while (ps->n_queued > burst_limit) {