Don't special-case broadcast packets in in-band mode.
[sliver-openvswitch.git] / datapath / hwtable_dummy / hwtable_dummy.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 <linux/module.h>
35 #include <linux/rcupdate.h>
36 #include <linux/slab.h>
37 #include <linux/list.h>
38 #include <linux/delay.h>
39 #include <linux/if_arp.h>
40
41 #include "chain.h"
42 #include "table.h"
43 #include "flow.h"
44 #include "datapath.h"
45
46
47 /* Max number of flow entries supported by the hardware */
48 #define DUMMY_MAX_FLOW   8192
49
50
51 /* xxx Explain need for this separate list because of RCU */
52 static spinlock_t pending_free_lock;
53 static struct list_head pending_free_list;
54
55 /* sw_flow private data for dummy table entries.  */
56 struct sw_flow_dummy {
57         struct list_head node;
58
59         /* xxx If per-entry data is needed, define it here. */
60 };
61
62 struct sw_table_dummy {
63         struct sw_table swt;
64
65         spinlock_t lock;
66         unsigned int max_flows;
67         atomic_t n_flows;
68         struct list_head flows;
69         struct list_head iter_flows;
70         unsigned long int next_serial;
71 };
72
73
74 static void table_dummy_sfw_destroy(struct sw_flow_dummy *sfw)
75 {
76         /* xxx Remove the entry from hardware.  If you need to do any other
77          * xxx clean-up associated with the entry, do it here.
78          */
79
80         kfree(sfw);
81 }
82
83 static void table_dummy_rcu_callback(struct rcu_head *rcu)
84 {
85         struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
86
87         spin_lock(&pending_free_lock);
88         if (flow->private) {
89                 struct sw_flow_dummy *sfw = flow->private;
90                 list_add(&sfw->node, &pending_free_list);
91                 flow->private = NULL;
92         }
93         spin_unlock(&pending_free_lock);
94         flow_free(flow);
95 }
96
97 static void table_dummy_flow_deferred_free(struct sw_flow *flow)
98 {
99         call_rcu(&flow->rcu, table_dummy_rcu_callback);
100 }
101
102 static struct sw_flow *table_dummy_lookup(struct sw_table *swt,
103                                           const struct sw_flow_key *key)
104 {
105         struct sw_table_dummy *td = (struct sw_table_dummy *) swt;
106         struct sw_flow *flow;
107         list_for_each_entry (flow, &td->flows, node) {
108                 if (flow_matches(&flow->key, key)) {
109                         return flow; 
110                 }
111         }
112         return NULL;
113 }
114
115 static int table_dummy_insert(struct sw_table *swt, struct sw_flow *flow)
116 {
117         /* xxx Use a data cache? */
118         flow->private = kzalloc(sizeof(struct sw_flow_dummy), GFP_ATOMIC);
119         if (flow->private == NULL) 
120                 return 0;
121
122         /* xxx Do whatever needs to be done to insert an entry in hardware. 
123          * xxx If the entry can't be inserted, return 0.  This stub code
124          * xxx doesn't do anything yet, so we're going to return 0...you
125          * xxx shouldn't.
126          */
127         kfree(flow->private);
128         return 0;
129 }
130
131
132 static int do_delete(struct sw_table *swt, struct sw_flow *flow)
133 {
134         if (flow_del(flow)) {
135                 list_del_rcu(&flow->node);
136                 list_del_rcu(&flow->iter_node);
137                 table_dummy_flow_deferred_free(flow);
138                 return 1;
139         }
140         return 0;
141 }
142
143 static int table_dummy_delete(struct sw_table *swt,
144                               const struct sw_flow_key *key, uint16_t priority, int strict)
145 {
146         struct sw_table_dummy *td = (struct sw_table_dummy *) swt;
147         struct sw_flow *flow;
148         unsigned int count = 0;
149
150         list_for_each_entry_rcu (flow, &td->flows, node) {
151                 if (flow_del_matches(&flow->key, key, strict)
152                     && (!strict || (flow->priority == priority)))
153                         count += do_delete(swt, flow);
154         }
155         if (count)
156                 atomic_sub(count, &td->n_flows);
157         return count;
158 }
159
160
161 static int table_dummy_timeout(struct datapath *dp, struct sw_table *swt)
162 {
163         struct sw_table_dummy *td = (struct sw_table_dummy *) swt;
164         struct sw_flow *flow;
165         struct sw_flow_dummy *sfw, *n;
166         int del_count = 0;
167         uint64_t packet_count = 0;
168         int i = 0;
169
170         list_for_each_entry_rcu (flow, &td->flows, node) {
171                 /* xxx Retrieve the packet count associated with this entry
172                  * xxx and store it in "packet_count".
173                  */
174
175                 if ((packet_count > flow->packet_count)
176                     && (flow->max_idle != OFP_FLOW_PERMANENT)) {
177                         flow->packet_count = packet_count;
178                         flow->timeout = jiffies + HZ * flow->max_idle;
179                 }
180
181                 if (flow_timeout(flow)) {
182                         if (dp->flags & OFPC_SEND_FLOW_EXP) {
183                                 /* xxx Get byte count */
184                                 flow->byte_count = 0;
185                                 dp_send_flow_expired(dp, flow);
186                         }
187                         del_count += do_delete(swt, flow);
188                 }
189                 if ((i % 50) == 0) {
190                         msleep_interruptible(1);
191                 }
192                 i++;
193         }
194
195         /* Remove any entries queued for removal */
196         spin_lock_bh(&pending_free_lock);
197         list_for_each_entry_safe (sfw, n, &pending_free_list, node) {
198                 list_del(&sfw->node);
199                 table_dummy_sfw_destroy(sfw);
200         }
201         spin_unlock_bh(&pending_free_lock);
202
203         if (del_count)
204                 atomic_sub(del_count, &td->n_flows);
205         return del_count;
206 }
207
208
209 static void table_dummy_destroy(struct sw_table *swt)
210 {
211         struct sw_table_dummy *td = (struct sw_table_dummy *)swt;
212
213
214         /* xxx This table is being destroyed, so free any data that you
215          * xxx don't want to leak.
216          */
217
218
219         if (td) {
220                 while (!list_empty(&td->flows)) {
221                         struct sw_flow *flow = list_entry(td->flows.next,
222                                                           struct sw_flow, node);
223                         list_del(&flow->node);
224                         flow_free(flow);
225                 }
226                 kfree(td);
227         }
228 }
229
230 static int table_dummy_iterate(struct sw_table *swt,
231                                const struct sw_flow_key *key,
232                                struct sw_table_position *position,
233                                int (*callback)(struct sw_flow *, void *),
234                                void *private)
235 {
236         struct sw_table_dummy *tl = (struct sw_table_dummy *) swt;
237         struct sw_flow *flow;
238         unsigned long start;
239
240         start = ~position->private[0];
241         list_for_each_entry_rcu (flow, &tl->iter_flows, iter_node) {
242                 if (flow->serial <= start && flow_matches(key, &flow->key)) {
243                         int error = callback(flow, private);
244                         if (error) {
245                                 position->private[0] = ~flow->serial;
246                                 return error;
247                         }
248                 }
249         }
250         return 0;
251 }
252
253 static void table_dummy_stats(struct sw_table *swt,
254                               struct sw_table_stats *stats)
255 {
256         struct sw_table_dummy *td = (struct sw_table_dummy *) swt;
257         stats->name = "dummy";
258         stats->n_flows = atomic_read(&td->n_flows);
259         stats->max_flows = td->max_flows;
260 }
261
262
263 static struct sw_table *table_dummy_create(void)
264 {
265         struct sw_table_dummy *td;
266         struct sw_table *swt;
267
268         td = kzalloc(sizeof *td, GFP_KERNEL);
269         if (td == NULL)
270                 return NULL;
271
272         swt = &td->swt;
273         swt->lookup = table_dummy_lookup;
274         swt->insert = table_dummy_insert;
275         swt->delete = table_dummy_delete;
276         swt->timeout = table_dummy_timeout;
277         swt->destroy = table_dummy_destroy;
278         swt->iterate = table_dummy_iterate;
279         swt->stats = table_dummy_stats;
280
281         td->max_flows = DUMMY_MAX_FLOW;
282         atomic_set(&td->n_flows, 0);
283         INIT_LIST_HEAD(&td->flows);
284         INIT_LIST_HEAD(&td->iter_flows);
285         spin_lock_init(&td->lock);
286         td->next_serial = 0;
287
288         INIT_LIST_HEAD(&pending_free_list);
289         spin_lock_init(&pending_free_lock);
290
291         return swt;
292 }
293
294 static int __init dummy_init(void)
295 {
296         return chain_set_hw_hook(table_dummy_create, THIS_MODULE);
297 }
298 module_init(dummy_init);
299
300 static void dummy_cleanup(void) 
301 {
302         chain_clear_hw_hook();
303 }
304 module_exit(dummy_cleanup);
305
306 MODULE_DESCRIPTION("Dummy hardware table driver");
307 MODULE_AUTHOR("Copyright (c) 2008 The Board of Trustees of The Leland Stanford Junior University");
308 MODULE_LICENSE("Stanford License");