Merge commit 'origin/master'
[sliver-openvswitch.git] / switch / table-linear.c
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  * 
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:
16  * 
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  * 
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
27  * SOFTWARE.
28  * 
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.
32  */
33
34 #include <config.h>
35 #include "table.h"
36 #include <stdlib.h>
37 #include "flow.h"
38 #include "list.h"
39 #include "openflow.h"
40 #include "switch-flow.h"
41 #include "datapath.h"
42
43 struct sw_table_linear {
44     struct sw_table swt;
45
46     unsigned int max_flows;
47     unsigned int n_flows;
48     struct list flows;
49     struct list iter_flows;
50     unsigned long int next_serial;
51 };
52
53 static struct sw_flow *table_linear_lookup(struct sw_table *swt,
54                                            const struct sw_flow_key *key)
55 {
56     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
57     struct sw_flow *flow;
58     LIST_FOR_EACH (flow, struct sw_flow, node, &tl->flows) {
59         if (flow_matches_1wild(key, &flow->key))
60             return flow;
61     }
62     return NULL;
63 }
64
65 static int table_linear_insert(struct sw_table *swt, struct sw_flow *flow)
66 {
67     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
68     struct sw_flow *f;
69
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.
73      */
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             /* Keep stats from the original flow */
79             flow->used = f->used;
80             flow->created = f->created;
81             flow->packet_count = f->packet_count;
82             flow->byte_count = f->byte_count;
83
84             flow->serial = f->serial;
85             list_replace(&flow->node, &f->node);
86             list_replace(&flow->iter_node, &f->iter_node);
87             flow_free(f);
88             return 1;
89         }
90
91         if (f->priority < flow->priority)
92             break;
93     }
94
95     /* Make sure there's room in the table. */
96     if (tl->n_flows >= tl->max_flows) {
97         return 0;
98     }
99     tl->n_flows++;
100
101     /* Insert the entry immediately in front of where we're pointing. */
102     flow->serial = tl->next_serial++;
103     list_insert(&f->node, &flow->node);
104     list_push_front(&tl->iter_flows, &flow->iter_node);
105
106     return 1;
107 }
108
109 static int table_linear_modify(struct sw_table *swt,
110                 const struct sw_flow_key *key,
111                 const struct ofp_action *actions, int n_actions)
112 {
113     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
114     struct sw_flow *flow;
115     unsigned int count = 0;
116
117     LIST_FOR_EACH (flow, struct sw_flow, node, &tl->flows) {
118         if (flow_matches_1wild(&flow->key, key)) {
119             flow_replace_acts(flow, actions, n_actions);
120             count++;
121         }
122     }
123     return count;
124 }
125
126 static void
127 do_delete(struct sw_flow *flow) 
128 {
129     list_remove(&flow->node);
130     list_remove(&flow->iter_node);
131     flow_free(flow);
132 }
133
134 static int table_linear_delete(struct sw_table *swt,
135                                const struct sw_flow_key *key, 
136                                uint16_t priority, int strict)
137 {
138     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
139     struct sw_flow *flow, *n;
140     unsigned int count = 0;
141
142     LIST_FOR_EACH_SAFE (flow, n, struct sw_flow, node, &tl->flows) {
143         if (flow_del_matches(&flow->key, key, strict)
144                 && (!strict || (flow->priority == priority))) {
145             do_delete(flow);
146             count++;
147         }
148     }
149     tl->n_flows -= count;
150     return count;
151 }
152
153 static void table_linear_timeout(struct sw_table *swt, struct list *deleted)
154 {
155     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
156     struct sw_flow *flow, *n;
157
158     LIST_FOR_EACH_SAFE (flow, n, struct sw_flow, node, &tl->flows) {
159         if (flow_timeout(flow)) {
160             list_remove(&flow->node);
161             list_remove(&flow->iter_node);
162             list_push_back(deleted, &flow->node);
163             tl->n_flows--;
164         }
165     }
166 }
167
168 static void table_linear_destroy(struct sw_table *swt)
169 {
170     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
171
172     while (!list_is_empty(&tl->flows)) {
173         struct sw_flow *flow = CONTAINER_OF(list_front(&tl->flows),
174                                             struct sw_flow, node);
175         list_remove(&flow->node);
176         flow_free(flow);
177     }
178     free(tl);
179 }
180
181 static int table_linear_iterate(struct sw_table *swt,
182                                 const struct sw_flow_key *key,
183                                 struct sw_table_position *position,
184                                 int (*callback)(struct sw_flow *, void *),
185                                 void *private)
186 {
187     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
188     struct sw_flow *flow;
189     unsigned long start;
190
191     start = ~position->private[0];
192     LIST_FOR_EACH (flow, struct sw_flow, iter_node, &tl->iter_flows) {
193         if (flow->serial <= start && flow_matches_2wild(key, &flow->key)) {
194             int error = callback(flow, private);
195             if (error) {
196                 position->private[0] = ~(flow->serial - 1);
197                 return error;
198             }
199         }
200     }
201     return 0;
202 }
203
204 static void table_linear_stats(struct sw_table *swt,
205                                struct sw_table_stats *stats)
206 {
207     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
208     stats->name = "linear";
209     stats->wildcards = OFPFW_ALL;
210     stats->n_flows   = tl->n_flows;
211     stats->max_flows = tl->max_flows;
212     stats->n_matched = swt->n_matched;
213 }
214
215
216 struct sw_table *table_linear_create(unsigned int max_flows)
217 {
218     struct sw_table_linear *tl;
219     struct sw_table *swt;
220
221     tl = calloc(1, sizeof *tl);
222     if (tl == NULL)
223         return NULL;
224
225     swt = &tl->swt;
226     swt->lookup = table_linear_lookup;
227     swt->insert = table_linear_insert;
228     swt->modify = table_linear_modify;
229     swt->delete = table_linear_delete;
230     swt->timeout = table_linear_timeout;
231     swt->destroy = table_linear_destroy;
232     swt->iterate = table_linear_iterate;
233     swt->stats = table_linear_stats;
234
235     tl->max_flows = max_flows;
236     tl->n_flows = 0;
237     list_init(&tl->flows);
238     list_init(&tl->iter_flows);
239     tl->next_serial = 0;
240
241     return swt;
242 }