Get rid of now-excessive levels of indirection in hwtable_dummy.
[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 /* sw_flow private data for dummy table entries.  */
52 struct sw_flow_dummy {
53         struct list_head node;
54
55         /* xxx If per-entry data is needed, define it here. */
56 };
57
58 struct sw_table_dummy {
59         struct sw_table swt;
60
61         unsigned int max_flows;
62         unsigned int n_flows;
63         struct list_head flows;
64         struct list_head iter_flows;
65         unsigned long int next_serial;
66 };
67
68
69 static struct sw_flow *table_dummy_lookup(struct sw_table *swt,
70                                           const struct sw_flow_key *key)
71 {
72         struct sw_table_dummy *td = (struct sw_table_dummy *) swt;
73         struct sw_flow *flow;
74         list_for_each_entry (flow, &td->flows, node) {
75                 if (flow_matches(&flow->key, key)) {
76                         return flow; 
77                 }
78         }
79         return NULL;
80 }
81
82 static int table_dummy_insert(struct sw_table *swt, struct sw_flow *flow)
83 {
84         /* xxx Use a data cache? */
85         flow->private = kzalloc(sizeof(struct sw_flow_dummy), GFP_ATOMIC);
86         if (flow->private == NULL) 
87                 return 0;
88
89         /* xxx Do whatever needs to be done to insert an entry in hardware. 
90          * xxx If the entry can't be inserted, return 0.  This stub code
91          * xxx doesn't do anything yet, so we're going to return 0...you
92          * xxx shouldn't (and you should update n_flows in struct
93          * xxx sw_table_dummy, too).
94          */
95         kfree(flow->private);
96         return 0;
97 }
98
99
100 static int do_delete(struct sw_table *swt, struct sw_flow *flow)
101 {
102         /* xxx Remove the entry from hardware.  If you need to do any other
103          * xxx clean-up associated with the entry, do it here.
104          */
105         list_del_rcu(&flow->node);
106         list_del_rcu(&flow->iter_node);
107         return 1;
108 }
109
110 static int table_dummy_delete(struct sw_table *swt,
111                               const struct sw_flow_key *key, uint16_t priority, int strict)
112 {
113         struct sw_table_dummy *td = (struct sw_table_dummy *) swt;
114         struct sw_flow *flow;
115         unsigned int count = 0;
116
117         list_for_each_entry (flow, &td->flows, node) {
118                 if (flow_del_matches(&flow->key, key, strict)
119                     && (!strict || (flow->priority == priority)))
120                         count += do_delete(swt, flow);
121         }
122         td->n_flows -= count;
123         return count;
124 }
125
126
127 static int table_dummy_timeout(struct datapath *dp, struct sw_table *swt)
128 {
129         struct sw_table_dummy *td = (struct sw_table_dummy *) swt;
130         struct sw_flow *flow;
131         int del_count = 0;
132         uint64_t packet_count = 0;
133         int i = 0;
134
135         mutex_lock(&dp_mutex);
136         list_for_each_entry (flow, &td->flows, node) {
137                 /* xxx Retrieve the packet count associated with this entry
138                  * xxx and store it in "packet_count".
139                  */
140
141                 if ((packet_count > flow->packet_count)
142                     && (flow->max_idle != OFP_FLOW_PERMANENT)) {
143                         flow->packet_count = packet_count;
144                         flow->timeout = jiffies + HZ * flow->max_idle;
145                 }
146
147                 if (flow_timeout(flow)) {
148                         if (dp->flags & OFPC_SEND_FLOW_EXP) {
149                                 /* xxx Get byte count */
150                                 flow->byte_count = 0;
151                                 dp_send_flow_expired(dp, flow);
152                         }
153                         del_count += do_delete(swt, flow);
154                 }
155                 i++;
156         }
157         mutex_unlock(&dp_mutex);
158
159         td->n_flows -= del_count;
160         return del_count;
161 }
162
163
164 static void table_dummy_destroy(struct sw_table *swt)
165 {
166         struct sw_table_dummy *td = (struct sw_table_dummy *)swt;
167
168
169         /* xxx This table is being destroyed, so free any data that you
170          * xxx don't want to leak.
171          */
172
173
174         if (td) {
175                 while (!list_empty(&td->flows)) {
176                         struct sw_flow *flow = list_entry(td->flows.next,
177                                                           struct sw_flow, node);
178                         list_del(&flow->node);
179                         flow_free(flow);
180                 }
181                 kfree(td);
182         }
183 }
184
185 static int table_dummy_iterate(struct sw_table *swt,
186                                const struct sw_flow_key *key,
187                                struct sw_table_position *position,
188                                int (*callback)(struct sw_flow *, void *),
189                                void *private)
190 {
191         struct sw_table_dummy *tl = (struct sw_table_dummy *) swt;
192         struct sw_flow *flow;
193         unsigned long start;
194
195         start = ~position->private[0];
196         list_for_each_entry (flow, &tl->iter_flows, iter_node) {
197                 if (flow->serial <= start && flow_matches(key, &flow->key)) {
198                         int error = callback(flow, private);
199                         if (error) {
200                                 position->private[0] = ~flow->serial;
201                                 return error;
202                         }
203                 }
204         }
205         return 0;
206 }
207
208 static void table_dummy_stats(struct sw_table *swt,
209                               struct sw_table_stats *stats)
210 {
211         struct sw_table_dummy *td = (struct sw_table_dummy *) swt;
212         stats->name = "dummy";
213         stats->n_flows = td->n_flows;
214         stats->max_flows = td->max_flows;
215 }
216
217
218 static struct sw_table *table_dummy_create(void)
219 {
220         struct sw_table_dummy *td;
221         struct sw_table *swt;
222
223         td = kzalloc(sizeof *td, GFP_KERNEL);
224         if (td == NULL)
225                 return NULL;
226
227         swt = &td->swt;
228         swt->lookup = table_dummy_lookup;
229         swt->insert = table_dummy_insert;
230         swt->delete = table_dummy_delete;
231         swt->timeout = table_dummy_timeout;
232         swt->destroy = table_dummy_destroy;
233         swt->iterate = table_dummy_iterate;
234         swt->stats = table_dummy_stats;
235
236         td->max_flows = DUMMY_MAX_FLOW;
237         td->n_flows = 0;
238         INIT_LIST_HEAD(&td->flows);
239         INIT_LIST_HEAD(&td->iter_flows);
240         td->next_serial = 0;
241
242         return swt;
243 }
244
245 static int __init dummy_init(void)
246 {
247         return chain_set_hw_hook(table_dummy_create, THIS_MODULE);
248 }
249 module_init(dummy_init);
250
251 static void dummy_cleanup(void) 
252 {
253         chain_clear_hw_hook();
254 }
255 module_exit(dummy_cleanup);
256
257 MODULE_DESCRIPTION("Dummy hardware table driver");
258 MODULE_AUTHOR("Copyright (c) 2008 The Board of Trustees of The Leland Stanford Junior University");
259 MODULE_LICENSE("Stanford License");