Crossport lib/svec.[ch] from master branch.
[sliver-openvswitch.git] / udatapath / 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/openflow.h"
40 #include "openflow/nicira-ext.h"
41 #include "switch-flow.h"
42 #include "datapath.h"
43
44 struct sw_table_linear {
45     struct sw_table swt;
46
47     unsigned int max_flows;
48     unsigned int n_flows;
49     struct list flows;
50     struct list iter_flows;
51     unsigned long int next_serial;
52 };
53
54 static struct sw_flow *table_linear_lookup(struct sw_table *swt,
55                                            const struct sw_flow_key *key)
56 {
57     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
58     struct sw_flow *flow;
59     LIST_FOR_EACH (flow, struct sw_flow, node, &tl->flows) {
60         if (flow_matches_1wild(key, &flow->key))
61             return flow;
62     }
63     return NULL;
64 }
65
66 static int table_linear_insert(struct sw_table *swt, struct sw_flow *flow)
67 {
68     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
69     struct sw_flow *f;
70
71     /* Loop through the existing list of entries.  New entries will
72      * always be placed behind those with equal priority.  Just replace 
73      * any flows that match exactly.
74      */
75     LIST_FOR_EACH (f, struct sw_flow, node, &tl->flows) {
76         if (f->priority == flow->priority
77                 && f->key.wildcards == flow->key.wildcards
78                 && flow_matches_2wild(&f->key, &flow->key)) {
79             flow->serial = f->serial;
80             list_replace(&flow->node, &f->node);
81             list_replace(&flow->iter_node, &f->iter_node);
82             flow_free(f);
83             return 1;
84         }
85
86         if (f->priority < flow->priority)
87             break;
88     }
89
90     /* Make sure there's room in the table. */
91     if (tl->n_flows >= tl->max_flows) {
92         return 0;
93     }
94     tl->n_flows++;
95
96     /* Insert the entry immediately in front of where we're pointing. */
97     flow->serial = tl->next_serial++;
98     list_insert(&f->node, &flow->node);
99     list_push_front(&tl->iter_flows, &flow->iter_node);
100
101     return 1;
102 }
103
104 static int table_linear_modify(struct sw_table *swt,
105                 const struct sw_flow_key *key, uint16_t priority, int strict,
106                 const struct ofp_action_header *actions, size_t actions_len)
107 {
108     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
109     struct sw_flow *flow;
110     unsigned int count = 0;
111
112     LIST_FOR_EACH (flow, struct sw_flow, node, &tl->flows) {
113         if (flow_matches_desc(&flow->key, key, strict)
114                 && (!strict || (flow->priority == priority))) {
115             flow_replace_acts(flow, actions, actions_len);
116             count++;
117         }
118     }
119     return count;
120 }
121
122 static void
123 do_delete(struct sw_flow *flow) 
124 {
125     list_remove(&flow->node);
126     list_remove(&flow->iter_node);
127     flow_free(flow);
128 }
129
130 static int table_linear_delete(struct datapath *dp, struct sw_table *swt,
131                                const struct sw_flow_key *key, 
132                                uint16_t out_port, 
133                                uint16_t priority, int strict)
134 {
135     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
136     struct sw_flow *flow, *n;
137     unsigned int count = 0;
138
139     LIST_FOR_EACH_SAFE (flow, n, struct sw_flow, node, &tl->flows) {
140         if (flow_matches_desc(&flow->key, key, strict)
141                 && flow_has_out_port(flow, out_port)
142                 && (!strict || (flow->priority == priority))) {
143             dp_send_flow_end(dp, flow, NXFER_DELETE);
144             do_delete(flow);
145             count++;
146         }
147     }
148     tl->n_flows -= count;
149     return count;
150 }
151
152 static void table_linear_timeout(struct sw_table *swt, struct list *deleted)
153 {
154     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
155     struct sw_flow *flow, *n;
156
157     LIST_FOR_EACH_SAFE (flow, n, struct sw_flow, node, &tl->flows) {
158         if (flow_timeout(flow)) {
159             list_remove(&flow->node);
160             list_remove(&flow->iter_node);
161             list_push_back(deleted, &flow->node);
162             tl->n_flows--;
163         }
164     }
165 }
166
167 static void table_linear_destroy(struct sw_table *swt)
168 {
169     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
170
171     while (!list_is_empty(&tl->flows)) {
172         struct sw_flow *flow = CONTAINER_OF(list_front(&tl->flows),
173                                             struct sw_flow, node);
174         list_remove(&flow->node);
175         flow_free(flow);
176     }
177     free(tl);
178 }
179
180 static int table_linear_iterate(struct sw_table *swt,
181                                 const struct sw_flow_key *key,
182                                 uint16_t out_port,
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 
194                 && flow_matches_2wild(key, &flow->key)
195                 && flow_has_out_port(flow, out_port)) {
196             int error = callback(flow, private);
197             if (error) {
198                 position->private[0] = ~(flow->serial - 1);
199                 return error;
200             }
201         }
202     }
203     return 0;
204 }
205
206 static void table_linear_stats(struct sw_table *swt,
207                                struct sw_table_stats *stats)
208 {
209     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
210     stats->name = "linear";
211     stats->wildcards = OFPFW_ALL;
212     stats->n_flows   = tl->n_flows;
213     stats->max_flows = tl->max_flows;
214     stats->n_lookup  = swt->n_lookup;
215     stats->n_matched = swt->n_matched;
216 }
217
218
219 struct sw_table *table_linear_create(unsigned int max_flows)
220 {
221     struct sw_table_linear *tl;
222     struct sw_table *swt;
223
224     tl = calloc(1, sizeof *tl);
225     if (tl == NULL)
226         return NULL;
227
228     swt = &tl->swt;
229     swt->lookup = table_linear_lookup;
230     swt->insert = table_linear_insert;
231     swt->modify = table_linear_modify;
232     swt->delete = table_linear_delete;
233     swt->timeout = table_linear_timeout;
234     swt->destroy = table_linear_destroy;
235     swt->iterate = table_linear_iterate;
236     swt->stats = table_linear_stats;
237
238     tl->max_flows = max_flows;
239     tl->n_flows = 0;
240     list_init(&tl->flows);
241     list_init(&tl->iter_flows);
242     tl->next_serial = 0;
243
244     return swt;
245 }