Update copyright on all non-GPL files
[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 "table.h"
35 #include <stdlib.h>
36 #include "flow.h"
37 #include "list.h"
38 #include "switch-flow.h"
39 #include "datapath.h"
40
41 struct sw_table_linear {
42     struct sw_table swt;
43
44     unsigned int max_flows;
45     unsigned int n_flows;
46     struct list flows;
47 };
48
49 static struct sw_flow *table_linear_lookup(struct sw_table *swt,
50                                            const struct sw_flow_key *key)
51 {
52     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
53     struct sw_flow *flow;
54     LIST_FOR_EACH (flow, struct sw_flow, node, &tl->flows) {
55         if (flow_matches(&flow->key, key))
56             return flow;
57     }
58     return NULL;
59 }
60
61 static int table_linear_insert(struct sw_table *swt, struct sw_flow *flow)
62 {
63     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
64     struct sw_flow *f;
65
66     /* Replace flows that match exactly. */
67     LIST_FOR_EACH (f, struct sw_flow, node, &tl->flows) {
68         if (f->key.wildcards == flow->key.wildcards
69             && flow_matches(&f->key, &flow->key)) {
70             list_replace(&flow->node, &f->node);
71             flow_free(f);
72             return 1;
73         }
74     }
75
76     /* Table overflow? */
77     if (tl->n_flows >= tl->max_flows) {
78         return 0;
79     }
80     tl->n_flows++;
81
82     /* FIXME: need to order rules from most to least specific. */
83     list_push_back(&tl->flows, &flow->node);
84     return 1;
85 }
86
87 static void
88 do_delete(struct sw_flow *flow) 
89 {
90     list_remove(&flow->node);
91     flow_free(flow);
92 }
93
94 static int table_linear_delete(struct sw_table *swt,
95                                const struct sw_flow_key *key, int strict)
96 {
97     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
98     struct sw_flow *flow, *n;
99     unsigned int count = 0;
100
101     LIST_FOR_EACH_SAFE (flow, n, struct sw_flow, node, &tl->flows) {
102         if (flow_del_matches(&flow->key, key, strict)) {
103             do_delete(flow);
104             count++;
105         }
106     }
107     tl->n_flows -= count;
108     return count;
109 }
110
111 static int table_linear_timeout(struct datapath *dp, struct sw_table *swt)
112 {
113     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
114     struct sw_flow *flow, *n;
115     int count = 0;
116
117     LIST_FOR_EACH_SAFE (flow, n, struct sw_flow, node, &tl->flows) {
118         if (flow_timeout(flow)) {
119             dp_send_flow_expired(dp, flow);
120             do_delete(flow);
121             count++;
122         }
123     }
124     tl->n_flows -= count;
125     return count;
126 }
127
128 static void table_linear_destroy(struct sw_table *swt)
129 {
130     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
131
132     while (!list_is_empty(&tl->flows)) {
133         struct sw_flow *flow = CONTAINER_OF(list_front(&tl->flows),
134                                             struct sw_flow, node);
135         list_remove(&flow->node);
136         flow_free(flow);
137     }
138     free(tl);
139 }
140
141 /* Linear table's private data is just a pointer to the table */
142
143 static int table_linear_iterator(struct sw_table *swt,
144                                  struct swt_iterator *swt_iter) 
145 {
146     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
147
148     swt_iter->private = tl;
149
150     if (!tl->n_flows)
151         swt_iter->flow = NULL;
152     else
153         swt_iter->flow = CONTAINER_OF(list_front(&tl->flows), struct sw_flow, node);
154
155     return 1;
156 }
157
158 static void table_linear_next(struct swt_iterator *swt_iter)
159 {
160     struct sw_table_linear *tl;
161     struct list *next;
162
163     if (swt_iter->flow == NULL)
164         return;
165
166     tl = (struct sw_table_linear *) swt_iter->private;
167
168     next = swt_iter->flow->node.next;
169     if (next == &tl->flows)
170         swt_iter->flow = NULL;
171     else
172         swt_iter->flow = CONTAINER_OF(next, struct sw_flow, node);
173 }
174
175 static void table_linear_iterator_destroy(struct swt_iterator *swt_iter)
176 {}
177
178 static void table_linear_stats(struct sw_table *swt,
179                                struct sw_table_stats *stats)
180 {
181     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
182     stats->name = "linear";
183     stats->n_flows = tl->n_flows;
184     stats->max_flows = tl->max_flows;
185 }
186
187
188 struct sw_table *table_linear_create(unsigned int max_flows)
189 {
190     struct sw_table_linear *tl;
191     struct sw_table *swt;
192
193     tl = calloc(1, sizeof *tl);
194     if (tl == NULL)
195         return NULL;
196
197     swt = &tl->swt;
198     swt->lookup = table_linear_lookup;
199     swt->insert = table_linear_insert;
200     swt->delete = table_linear_delete;
201     swt->timeout = table_linear_timeout;
202     swt->destroy = table_linear_destroy;
203     swt->stats = table_linear_stats;
204
205     swt->iterator = table_linear_iterator;
206     swt->iterator_next = table_linear_next;
207     swt->iterator_destroy = table_linear_iterator_destroy;
208
209     tl->max_flows = max_flows;
210     tl->n_flows = 0;
211     list_init(&tl->flows);
212
213     return swt;
214 }