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.
38 #include "switch-flow.h"
41 struct sw_table_linear {
44 unsigned int max_flows;
49 static struct sw_flow *table_linear_lookup(struct sw_table *swt,
50 const struct sw_flow_key *key)
52 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
54 LIST_FOR_EACH (flow, struct sw_flow, node, &tl->flows) {
55 if (flow_matches(&flow->key, key))
61 static int table_linear_insert(struct sw_table *swt, struct sw_flow *flow)
63 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
66 /* Loop through the existing list of entries. New entries will
67 * always be placed behind those with equal priority. Just replace
68 * any flows that match exactly.
70 LIST_FOR_EACH (f, struct sw_flow, node, &tl->flows) {
71 if (f->priority == flow->priority
72 && f->key.wildcards == flow->key.wildcards
73 && flow_matches(&f->key, &flow->key)) {
74 list_replace(&flow->node, &f->node);
79 if (f->priority < flow->priority)
83 /* Make sure there's room in the table. */
84 if (tl->n_flows >= tl->max_flows) {
89 /* Insert the entry immediately in front of where we're pointing. */
90 list_push_back(&f->node, &flow->node);
96 do_delete(struct sw_flow *flow)
98 list_remove(&flow->node);
102 static int table_linear_delete(struct sw_table *swt,
103 const struct sw_flow_key *key,
104 uint16_t priority, int strict)
106 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
107 struct sw_flow *flow, *n;
108 unsigned int count = 0;
110 LIST_FOR_EACH_SAFE (flow, n, struct sw_flow, node, &tl->flows) {
111 if (flow_del_matches(&flow->key, key, strict)
112 && (strict && (flow->priority == priority))) {
117 tl->n_flows -= count;
121 static void table_linear_timeout(struct sw_table *swt, struct list *deleted)
123 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
124 struct sw_flow *flow, *n;
126 LIST_FOR_EACH_SAFE (flow, n, struct sw_flow, node, &tl->flows) {
127 if (flow_timeout(flow)) {
128 list_remove(&flow->node);
129 list_push_back(deleted, &flow->node);
135 static void table_linear_destroy(struct sw_table *swt)
137 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
139 while (!list_is_empty(&tl->flows)) {
140 struct sw_flow *flow = CONTAINER_OF(list_front(&tl->flows),
141 struct sw_flow, node);
142 list_remove(&flow->node);
148 /* Linear table's private data is just a pointer to the table */
150 static int table_linear_iterator(struct sw_table *swt,
151 struct swt_iterator *swt_iter)
153 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
155 swt_iter->private = tl;
158 swt_iter->flow = NULL;
160 swt_iter->flow = CONTAINER_OF(list_front(&tl->flows), struct sw_flow, node);
165 static void table_linear_next(struct swt_iterator *swt_iter)
167 struct sw_table_linear *tl;
170 if (swt_iter->flow == NULL)
173 tl = (struct sw_table_linear *) swt_iter->private;
175 next = swt_iter->flow->node.next;
176 if (next == &tl->flows)
177 swt_iter->flow = NULL;
179 swt_iter->flow = CONTAINER_OF(next, struct sw_flow, node);
182 static void table_linear_iterator_destroy(struct swt_iterator *swt_iter)
185 static void table_linear_stats(struct sw_table *swt,
186 struct sw_table_stats *stats)
188 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
189 stats->name = "linear";
190 stats->n_flows = tl->n_flows;
191 stats->max_flows = tl->max_flows;
195 struct sw_table *table_linear_create(unsigned int max_flows)
197 struct sw_table_linear *tl;
198 struct sw_table *swt;
200 tl = calloc(1, sizeof *tl);
205 swt->lookup = table_linear_lookup;
206 swt->insert = table_linear_insert;
207 swt->delete = table_linear_delete;
208 swt->timeout = table_linear_timeout;
209 swt->destroy = table_linear_destroy;
210 swt->stats = table_linear_stats;
212 swt->iterator = table_linear_iterator;
213 swt->iterator_next = table_linear_next;
214 swt->iterator_destroy = table_linear_iterator_destroy;
216 tl->max_flows = max_flows;
218 list_init(&tl->flows);