Move Autoconf's macro definitions into config.h.
[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 "switch-flow.h"
40 #include "datapath.h"
41
42 struct sw_table_linear {
43     struct sw_table swt;
44
45     unsigned int max_flows;
46     unsigned int n_flows;
47     struct list flows;
48     struct list iter_flows;
49     unsigned long int next_serial;
50 };
51
52 static struct sw_flow *table_linear_lookup(struct sw_table *swt,
53                                            const struct sw_flow_key *key)
54 {
55     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
56     struct sw_flow *flow;
57     LIST_FOR_EACH (flow, struct sw_flow, node, &tl->flows) {
58         if (flow_matches(&flow->key, key))
59             return flow;
60     }
61     return NULL;
62 }
63
64 static int table_linear_insert(struct sw_table *swt, struct sw_flow *flow)
65 {
66     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
67     struct sw_flow *f;
68
69     /* Loop through the existing list of entries.  New entries will
70      * always be placed behind those with equal priority.  Just replace 
71      * any flows that match exactly.
72      */
73     LIST_FOR_EACH (f, struct sw_flow, node, &tl->flows) {
74         if (f->priority == flow->priority
75                 && f->key.wildcards == flow->key.wildcards
76                 && flow_matches(&f->key, &flow->key)) {
77             flow->serial = f->serial;
78             list_replace(&flow->node, &f->node);
79             list_replace(&flow->iter_node, &f->iter_node);
80             flow_free(f);
81             return 1;
82         }
83
84         if (f->priority < flow->priority)
85             break;
86     }
87
88     /* Make sure there's room in the table. */
89     if (tl->n_flows >= tl->max_flows) {
90         return 0;
91     }
92     tl->n_flows++;
93
94     /* Insert the entry immediately in front of where we're pointing. */
95     list_insert(&f->node, &flow->node);
96     list_push_front(&tl->iter_flows, &flow->iter_node);
97
98     return 1;
99 }
100
101 static void
102 do_delete(struct sw_flow *flow) 
103 {
104     list_remove(&flow->node);
105     list_remove(&flow->iter_node);
106     flow_free(flow);
107 }
108
109 static int table_linear_delete(struct sw_table *swt,
110                                const struct sw_flow_key *key, 
111                                uint16_t priority, int strict)
112 {
113     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
114     struct sw_flow *flow, *n;
115     unsigned int count = 0;
116
117     LIST_FOR_EACH_SAFE (flow, n, struct sw_flow, node, &tl->flows) {
118         if (flow_del_matches(&flow->key, key, strict)
119                 && (!strict || (flow->priority == priority))) {
120             do_delete(flow);
121             count++;
122         }
123     }
124     tl->n_flows -= count;
125     return count;
126 }
127
128 static void table_linear_timeout(struct sw_table *swt, struct list *deleted)
129 {
130     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
131     struct sw_flow *flow, *n;
132
133     LIST_FOR_EACH_SAFE (flow, n, struct sw_flow, node, &tl->flows) {
134         if (flow_timeout(flow)) {
135             list_remove(&flow->node);
136             list_remove(&flow->iter_node);
137             list_push_back(deleted, &flow->node);
138             tl->n_flows--;
139         }
140     }
141 }
142
143 static void table_linear_destroy(struct sw_table *swt)
144 {
145     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
146
147     while (!list_is_empty(&tl->flows)) {
148         struct sw_flow *flow = CONTAINER_OF(list_front(&tl->flows),
149                                             struct sw_flow, node);
150         list_remove(&flow->node);
151         flow_free(flow);
152     }
153     free(tl);
154 }
155
156 static int table_linear_iterate(struct sw_table *swt,
157                                 const struct sw_flow_key *key,
158                                 struct sw_table_position *position,
159                                 int (*callback)(struct sw_flow *, void *),
160                                 void *private)
161 {
162     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
163     struct sw_flow *flow;
164     unsigned long start;
165
166     start = ~position->private[0];
167     LIST_FOR_EACH (flow, struct sw_flow, iter_node, &tl->iter_flows) {
168         if (flow->serial <= start && flow_matches(key, &flow->key)) {
169             int error = callback(flow, private);
170             if (error) {
171                 position->private[0] = ~(flow->serial - 1);
172                 return error;
173             }
174         }
175     }
176     return 0;
177 }
178
179 static void table_linear_stats(struct sw_table *swt,
180                                struct sw_table_stats *stats)
181 {
182     struct sw_table_linear *tl = (struct sw_table_linear *) swt;
183     stats->name = "linear";
184     stats->n_flows = tl->n_flows;
185     stats->max_flows = tl->max_flows;
186 }
187
188
189 struct sw_table *table_linear_create(unsigned int max_flows)
190 {
191     struct sw_table_linear *tl;
192     struct sw_table *swt;
193
194     tl = calloc(1, sizeof *tl);
195     if (tl == NULL)
196         return NULL;
197
198     swt = &tl->swt;
199     swt->lookup = table_linear_lookup;
200     swt->insert = table_linear_insert;
201     swt->delete = table_linear_delete;
202     swt->timeout = table_linear_timeout;
203     swt->destroy = table_linear_destroy;
204     swt->iterate = table_linear_iterate;
205     swt->stats = table_linear_stats;
206
207     tl->max_flows = max_flows;
208     tl->n_flows = 0;
209     list_init(&tl->flows);
210     list_init(&tl->iter_flows);
211     tl->next_serial = 0;
212
213     return swt;
214 }