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