Clean-up related to supporting priorities...mostly suggestions from Ben.
[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     /* 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.
69      */
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);
75             flow_free(f);
76             return 1;
77         }
78
79         if (f->priority < flow->priority)
80             break;
81     }
82
83     /* Make sure there's room in the table. */
84     if (tl->n_flows >= tl->max_flows) {
85         return 0;
86     }
87     tl->n_flows++;
88
89     /* Insert the entry immediately in front of where we're pointing. */
90     list_push_back(&f->node, &flow->node);
91
92     return 1;
93 }
94
95 static void
96 do_delete(struct sw_flow *flow) 
97 {
98     list_remove(&flow->node);
99     flow_free(flow);
100 }
101
102 static int table_linear_delete(struct sw_table *swt,
103                                const struct sw_flow_key *key, int strict)
104 {
105     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
106     struct sw_flow *flow, *n;
107     unsigned int count = 0;
108
109     LIST_FOR_EACH_SAFE (flow, n, struct sw_flow, node, &tl->flows) {
110         if (flow_del_matches(&flow->key, key, strict)) {
111             do_delete(flow);
112             count++;
113         }
114     }
115     tl->n_flows -= count;
116     return count;
117 }
118
119 static void table_linear_timeout(struct sw_table *swt, struct list *deleted)
120 {
121     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
122     struct sw_flow *flow, *n;
123
124     LIST_FOR_EACH_SAFE (flow, n, struct sw_flow, node, &tl->flows) {
125         if (flow_timeout(flow)) {
126             list_remove(&flow->node);
127             list_push_back(deleted, &flow->node);
128             tl->n_flows--;
129         }
130     }
131 }
132
133 static void table_linear_destroy(struct sw_table *swt)
134 {
135     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
136
137     while (!list_is_empty(&tl->flows)) {
138         struct sw_flow *flow = CONTAINER_OF(list_front(&tl->flows),
139                                             struct sw_flow, node);
140         list_remove(&flow->node);
141         flow_free(flow);
142     }
143     free(tl);
144 }
145
146 /* Linear table's private data is just a pointer to the table */
147
148 static int table_linear_iterator(struct sw_table *swt,
149                                  struct swt_iterator *swt_iter) 
150 {
151     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
152
153     swt_iter->private = tl;
154
155     if (!tl->n_flows)
156         swt_iter->flow = NULL;
157     else
158         swt_iter->flow = CONTAINER_OF(list_front(&tl->flows), struct sw_flow, node);
159
160     return 1;
161 }
162
163 static void table_linear_next(struct swt_iterator *swt_iter)
164 {
165     struct sw_table_linear *tl;
166     struct list *next;
167
168     if (swt_iter->flow == NULL)
169         return;
170
171     tl = (struct sw_table_linear *) swt_iter->private;
172
173     next = swt_iter->flow->node.next;
174     if (next == &tl->flows)
175         swt_iter->flow = NULL;
176     else
177         swt_iter->flow = CONTAINER_OF(next, struct sw_flow, node);
178 }
179
180 static void table_linear_iterator_destroy(struct swt_iterator *swt_iter)
181 {}
182
183 static void table_linear_stats(struct sw_table *swt,
184                                struct sw_table_stats *stats)
185 {
186     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
187     stats->name = "linear";
188     stats->n_flows = tl->n_flows;
189     stats->max_flows = tl->max_flows;
190 }
191
192
193 struct sw_table *table_linear_create(unsigned int max_flows)
194 {
195     struct sw_table_linear *tl;
196     struct sw_table *swt;
197
198     tl = calloc(1, sizeof *tl);
199     if (tl == NULL)
200         return NULL;
201
202     swt = &tl->swt;
203     swt->lookup = table_linear_lookup;
204     swt->insert = table_linear_insert;
205     swt->delete = table_linear_delete;
206     swt->timeout = table_linear_timeout;
207     swt->destroy = table_linear_destroy;
208     swt->stats = table_linear_stats;
209
210     swt->iterator = table_linear_iterator;
211     swt->iterator_next = table_linear_next;
212     swt->iterator_destroy = table_linear_iterator_destroy;
213
214     tl->max_flows = max_flows;
215     tl->n_flows = 0;
216     list_init(&tl->flows);
217
218     return swt;
219 }