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.
40 #include "switch-flow.h"
43 struct sw_table_linear {
46 unsigned int max_flows;
49 struct list iter_flows;
50 unsigned long int next_serial;
53 static struct sw_flow *table_linear_lookup(struct sw_table *swt,
54 const struct sw_flow_key *key)
56 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
58 LIST_FOR_EACH (flow, struct sw_flow, node, &tl->flows) {
59 if (flow_matches_1wild(key, &flow->key))
65 static int table_linear_insert(struct sw_table *swt, struct sw_flow *flow)
67 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
70 /* Loop through the existing list of entries. New entries will
71 * always be placed behind those with equal priority. Just replace
72 * any flows that match exactly.
74 LIST_FOR_EACH (f, struct sw_flow, node, &tl->flows) {
75 if (f->priority == flow->priority
76 && f->key.wildcards == flow->key.wildcards
77 && flow_matches_2wild(&f->key, &flow->key)) {
78 flow->serial = f->serial;
79 list_replace(&flow->node, &f->node);
80 list_replace(&flow->iter_node, &f->iter_node);
85 if (f->priority < flow->priority)
89 /* Make sure there's room in the table. */
90 if (tl->n_flows >= tl->max_flows) {
95 /* Insert the entry immediately in front of where we're pointing. */
96 flow->serial = tl->next_serial++;
97 list_insert(&f->node, &flow->node);
98 list_push_front(&tl->iter_flows, &flow->iter_node);
103 static int table_linear_modify(struct sw_table *swt,
104 const struct sw_flow_key *key, uint16_t priority, int strict,
105 const struct ofp_action_header *actions, size_t actions_len)
107 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
108 struct sw_flow *flow;
109 unsigned int count = 0;
111 LIST_FOR_EACH (flow, struct sw_flow, node, &tl->flows) {
112 if (flow_matches_desc(&flow->key, key, strict)
113 && (!strict || (flow->priority == priority))) {
114 flow_replace_acts(flow, actions, actions_len);
122 do_delete(struct sw_flow *flow)
124 list_remove(&flow->node);
125 list_remove(&flow->iter_node);
129 static int table_linear_delete(struct sw_table *swt,
130 const struct sw_flow_key *key,
131 uint16_t priority, int strict)
133 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
134 struct sw_flow *flow, *n;
135 unsigned int count = 0;
137 LIST_FOR_EACH_SAFE (flow, n, struct sw_flow, node, &tl->flows) {
138 if (flow_matches_desc(&flow->key, key, strict)
139 && (!strict || (flow->priority == priority))) {
144 tl->n_flows -= count;
148 static void table_linear_timeout(struct sw_table *swt, struct list *deleted)
150 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
151 struct sw_flow *flow, *n;
153 LIST_FOR_EACH_SAFE (flow, n, struct sw_flow, node, &tl->flows) {
154 if (flow_timeout(flow)) {
155 list_remove(&flow->node);
156 list_remove(&flow->iter_node);
157 list_push_back(deleted, &flow->node);
163 static void table_linear_destroy(struct sw_table *swt)
165 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
167 while (!list_is_empty(&tl->flows)) {
168 struct sw_flow *flow = CONTAINER_OF(list_front(&tl->flows),
169 struct sw_flow, node);
170 list_remove(&flow->node);
176 static int table_linear_iterate(struct sw_table *swt,
177 const struct sw_flow_key *key,
178 struct sw_table_position *position,
179 int (*callback)(struct sw_flow *, void *),
182 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
183 struct sw_flow *flow;
186 start = ~position->private[0];
187 LIST_FOR_EACH (flow, struct sw_flow, iter_node, &tl->iter_flows) {
188 if (flow->serial <= start && flow_matches_2wild(key, &flow->key)) {
189 int error = callback(flow, private);
191 position->private[0] = ~(flow->serial - 1);
199 static void table_linear_stats(struct sw_table *swt,
200 struct sw_table_stats *stats)
202 struct sw_table_linear *tl = (struct sw_table_linear *) swt;
203 stats->name = "linear";
204 stats->wildcards = OFPFW_ALL;
205 stats->n_flows = tl->n_flows;
206 stats->max_flows = tl->max_flows;
207 stats->n_lookup = swt->n_lookup;
208 stats->n_matched = swt->n_matched;
212 struct sw_table *table_linear_create(unsigned int max_flows)
214 struct sw_table_linear *tl;
215 struct sw_table *swt;
217 tl = calloc(1, sizeof *tl);
222 swt->lookup = table_linear_lookup;
223 swt->insert = table_linear_insert;
224 swt->modify = table_linear_modify;
225 swt->delete = table_linear_delete;
226 swt->timeout = table_linear_timeout;
227 swt->destroy = table_linear_destroy;
228 swt->iterate = table_linear_iterate;
229 swt->stats = table_linear_stats;
231 tl->max_flows = max_flows;
233 list_init(&tl->flows);
234 list_init(&tl->iter_flows);