2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
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>
27 #include "openflow/openflow.h"
28 #include "poll-loop.h"
33 #include "token-bucket.h"
37 struct hmap_node node; /* In struct pinsched's 'queues' hmap. */
38 uint16_t port_no; /* Port number. */
39 struct list packets; /* Contains "struct ofpbuf"s. */
40 int n; /* Number of packets in 'packets'. */
44 struct token_bucket token_bucket;
46 /* One queue per physical port. */
47 struct hmap queues; /* Contains "struct pinqueue"s. */
48 int n_queued; /* Sum over queues[*].n. */
49 struct pinqueue *next_txq; /* Next pinqueue check in round-robin. */
51 /* Transmission queue. */
52 int n_txq; /* No. of packets waiting in rconn for tx. */
54 /* Statistics reporting. */
55 unsigned long long n_normal; /* # txed w/o rate limit queuing. */
56 unsigned long long n_limited; /* # queued for rate limiting. */
57 unsigned long long n_queue_dropped; /* # dropped due to queue overflow. */
61 advance_txq(struct pinsched *ps)
63 struct hmap_node *next;
66 ? hmap_next(&ps->queues, &ps->next_txq->node)
67 : hmap_first(&ps->queues));
68 ps->next_txq = next ? CONTAINER_OF(next, struct pinqueue, node) : NULL;
71 static struct ofpbuf *
72 dequeue_packet(struct pinsched *ps, struct pinqueue *q)
74 struct ofpbuf *packet = ofpbuf_from_list(list_pop_front(&q->packets));
81 adjust_limits(int *rate_limit, int *burst_limit)
83 if (*rate_limit <= 0) {
86 if (*burst_limit <= 0) {
87 *burst_limit = *rate_limit / 4;
89 if (*burst_limit < 1) {
94 /* Destroys 'q' and removes it from 'ps''s set of queues.
95 * (The caller must ensure that 'q' is empty.) */
97 pinqueue_destroy(struct pinsched *ps, struct pinqueue *q)
99 hmap_remove(&ps->queues, &q->node);
103 static struct pinqueue *
104 pinqueue_get(struct pinsched *ps, uint16_t port_no)
106 uint32_t hash = hash_int(port_no, 0);
109 HMAP_FOR_EACH_IN_BUCKET (q, node, hash, &ps->queues) {
110 if (port_no == q->port_no) {
115 q = xmalloc(sizeof *q);
116 hmap_insert(&ps->queues, &q->node, hash);
117 q->port_no = port_no;
118 list_init(&q->packets);
123 /* Drop a packet from the longest queue in 'ps'. */
125 drop_packet(struct pinsched *ps)
127 struct pinqueue *longest; /* Queue currently selected as longest. */
128 int n_longest = 0; /* # of queues of same length as 'longest'. */
131 ps->n_queue_dropped++;
134 HMAP_FOR_EACH (q, node, &ps->queues) {
135 if (!longest || longest->n < q->n) {
138 } else if (longest->n == q->n) {
141 /* Randomly select one of the longest queues, with a uniform
142 * distribution (Knuth algorithm 3.4.2R). */
143 if (!random_range(n_longest)) {
149 /* FIXME: do we want to pop the tail instead? */
150 ofpbuf_delete(dequeue_packet(ps, longest));
151 if (longest->n == 0) {
152 pinqueue_destroy(ps, longest);
156 /* Remove and return the next packet to transmit (in round-robin order). */
157 static struct ofpbuf *
158 get_tx_packet(struct pinsched *ps)
160 struct ofpbuf *packet;
168 packet = dequeue_packet(ps, q);
171 pinqueue_destroy(ps, q);
177 /* Attempts to remove enough tokens from 'ps' to transmit a packet. Returns
178 * true if successful, false otherwise. (In the latter case no tokens are
181 get_token(struct pinsched *ps)
183 return token_bucket_withdraw(&ps->token_bucket, 1000);
187 pinsched_send(struct pinsched *ps, uint16_t port_no,
188 struct ofpbuf *packet, pinsched_tx_cb *cb, void *aux)
192 } else if (!ps->n_queued && get_token(ps)) {
193 /* In the common case where we are not constrained by the rate limit,
194 * let the packet take the normal path. */
198 /* Otherwise queue it up for the periodic callback to drain out. */
201 /* We might be called with a buffer obtained from dpif_recv() that has
202 * much more allocated space than actual content most of the time.
203 * Since we're going to store the packet for some time, free up that
204 * otherwise wasted space. */
207 if (ps->n_queued * 1000 >= ps->token_bucket.burst) {
210 q = pinqueue_get(ps, port_no);
211 list_push_back(&q->packets, &packet->list_node);
219 pinsched_run(struct pinsched *ps, pinsched_tx_cb *cb, void *aux)
224 /* Drain some packets out of the bucket if possible, but limit the
225 * number of iterations to allow other code to get work done too. */
226 for (i = 0; ps->n_queued && get_token(ps) && i < 50; i++) {
227 cb(get_tx_packet(ps), aux);
233 pinsched_wait(struct pinsched *ps)
235 if (ps && ps->n_queued) {
236 token_bucket_wait(&ps->token_bucket, 1000);
240 /* Creates and returns a scheduler for sending packet-in messages. */
242 pinsched_create(int rate_limit, int burst_limit)
246 ps = xzalloc(sizeof *ps);
248 adjust_limits(&rate_limit, &burst_limit);
249 token_bucket_init(&ps->token_bucket,
250 rate_limit, sat_mul(burst_limit, 1000));
252 hmap_init(&ps->queues);
258 ps->n_queue_dropped = 0;
264 pinsched_destroy(struct pinsched *ps)
267 struct pinqueue *q, *next;
269 HMAP_FOR_EACH_SAFE (q, next, node, &ps->queues) {
270 hmap_remove(&ps->queues, &q->node);
271 ofpbuf_list_delete(&q->packets);
274 hmap_destroy(&ps->queues);
280 pinsched_get_limits(const struct pinsched *ps,
281 int *rate_limit, int *burst_limit)
283 *rate_limit = ps->token_bucket.rate;
284 *burst_limit = ps->token_bucket.burst / 1000;
288 pinsched_set_limits(struct pinsched *ps, int rate_limit, int burst_limit)
290 adjust_limits(&rate_limit, &burst_limit);
291 token_bucket_set(&ps->token_bucket,
292 rate_limit, sat_mul(burst_limit, 1000));
293 while (ps->n_queued > burst_limit) {
298 /* Returns the number of packets scheduled to be sent eventually by 'ps'.
299 * Returns 0 if 'ps' is null. */
301 pinsched_count_txqlen(const struct pinsched *ps)
303 return ps ? ps->n_txq : 0;