Bring manpages and usage messages up-to-date.
[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     struct list iter_flows;
48     unsigned long int next_serial;
49 };
50
51 static struct sw_flow *table_linear_lookup(struct sw_table *swt,
52                                            const struct sw_flow_key *key)
53 {
54     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
55     struct sw_flow *flow;
56     LIST_FOR_EACH (flow, struct sw_flow, node, &tl->flows) {
57         if (flow_matches(&flow->key, key))
58             return flow;
59     }
60     return NULL;
61 }
62
63 static int table_linear_insert(struct sw_table *swt, struct sw_flow *flow)
64 {
65     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
66     struct sw_flow *f;
67
68     /* Loop through the existing list of entries.  New entries will
69      * always be placed behind those with equal priority.  Just replace 
70      * any flows that match exactly.
71      */
72     LIST_FOR_EACH (f, struct sw_flow, node, &tl->flows) {
73         if (f->priority == flow->priority
74                 && f->key.wildcards == flow->key.wildcards
75                 && flow_matches(&f->key, &flow->key)) {
76             flow->serial = f->serial;
77             list_replace(&flow->node, &f->node);
78             list_replace(&flow->iter_node, &f->iter_node);
79             flow_free(f);
80             return 1;
81         }
82
83         if (f->priority < flow->priority)
84             break;
85     }
86
87     /* Make sure there's room in the table. */
88     if (tl->n_flows >= tl->max_flows) {
89         return 0;
90     }
91     tl->n_flows++;
92
93     /* Insert the entry immediately in front of where we're pointing. */
94     list_insert(&f->node, &flow->node);
95     list_push_front(&tl->iter_flows, &flow->iter_node);
96
97     return 1;
98 }
99
100 static void
101 do_delete(struct sw_flow *flow) 
102 {
103     list_remove(&flow->node);
104     list_remove(&flow->iter_node);
105     flow_free(flow);
106 }
107
108 static int table_linear_delete(struct sw_table *swt,
109                                const struct sw_flow_key *key, 
110                                uint16_t priority, int strict)
111 {
112     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
113     struct sw_flow *flow, *n;
114     unsigned int count = 0;
115
116     LIST_FOR_EACH_SAFE (flow, n, struct sw_flow, node, &tl->flows) {
117         if (flow_del_matches(&flow->key, key, strict)
118                 && (!strict || (flow->priority == priority))) {
119             do_delete(flow);
120             count++;
121         }
122     }
123     tl->n_flows -= count;
124     return count;
125 }
126
127 static void table_linear_timeout(struct sw_table *swt, struct list *deleted)
128 {
129     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
130     struct sw_flow *flow, *n;
131
132     LIST_FOR_EACH_SAFE (flow, n, struct sw_flow, node, &tl->flows) {
133         if (flow_timeout(flow)) {
134             list_remove(&flow->node);
135             list_remove(&flow->iter_node);
136             list_push_back(deleted, &flow->node);
137             tl->n_flows--;
138         }
139     }
140 }
141
142 static void table_linear_destroy(struct sw_table *swt)
143 {
144     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
145
146     while (!list_is_empty(&tl->flows)) {
147         struct sw_flow *flow = CONTAINER_OF(list_front(&tl->flows),
148                                             struct sw_flow, node);
149         list_remove(&flow->node);
150         flow_free(flow);
151     }
152     free(tl);
153 }
154
155 static int table_linear_iterate(struct sw_table *swt,
156                                 const struct sw_flow_key *key,
157                                 struct sw_table_position *position,
158                                 int (*callback)(struct sw_flow *, void *),
159                                 void *private)
160 {
161     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
162     struct sw_flow *flow;
163     unsigned long start;
164
165     start = ~position->private[0];
166     LIST_FOR_EACH (flow, struct sw_flow, iter_node, &tl->iter_flows) {
167         if (flow->serial <= start && flow_matches(key, &flow->key)) {
168             int error = callback(flow, private);
169             if (error) {
170                 position->private[0] = ~(flow->serial - 1);
171                 return error;
172             }
173         }
174     }
175     return 0;
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->iterate = table_linear_iterate;
204     swt->stats = table_linear_stats;
205
206     tl->max_flows = max_flows;
207     tl->n_flows = 0;
208     list_init(&tl->flows);
209     list_init(&tl->iter_flows);
210     tl->next_serial = 0;
211
212     return swt;
213 }