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