1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
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:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
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
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.
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>
48 /* Max number of flow entries supported by the hardware */
49 #define DUMMY_MAX_FLOW 8192
52 /* xxx Explain need for this separate list because of RCU */
53 static spinlock_t pending_free_lock;
54 static struct list_head pending_free_list;
56 /* sw_flow private data for dummy table entries. */
57 struct sw_flow_dummy {
58 struct list_head node;
60 /* xxx If per-entry data is needed, define it here. */
63 struct sw_table_dummy {
67 unsigned int max_flows;
69 struct list_head flows;
70 struct list_head iter_flows;
71 unsigned long int next_serial;
75 static void table_dummy_sfw_destroy(struct sw_flow_dummy *sfw)
77 /* xxx Remove the entry from hardware. If you need to do any other
78 * xxx clean-up associated with the entry, do it here.
84 static void table_dummy_rcu_callback(struct rcu_head *rcu)
86 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
88 spin_lock(&pending_free_lock);
90 struct sw_flow_dummy *sfw = flow->private;
91 list_add(&sfw->node, &pending_free_list);
94 spin_unlock(&pending_free_lock);
98 static void table_dummy_flow_deferred_free(struct sw_flow *flow)
100 call_rcu(&flow->rcu, table_dummy_rcu_callback);
103 static struct sw_flow *table_dummy_lookup(struct sw_table *swt,
104 const struct sw_flow_key *key)
106 struct sw_table_dummy *td = (struct sw_table_dummy *) swt;
107 struct sw_flow *flow;
108 list_for_each_entry (flow, &td->flows, node) {
109 if (flow_matches(&flow->key, key)) {
116 static int table_dummy_insert(struct sw_table *swt, struct sw_flow *flow)
118 /* xxx Use a data cache? */
119 flow->private = kzalloc(sizeof(struct sw_flow_dummy), GFP_ATOMIC);
120 if (flow->private == NULL)
123 /* xxx Do whatever needs to be done to insert an entry in hardware.
124 * xxx If the entry can't be inserted, return 0. This stub code
125 * xxx doesn't do anything yet, so we're going to return 0...you
128 kfree(flow->private);
133 static int do_delete(struct sw_table *swt, struct sw_flow *flow)
135 if (flow_del(flow)) {
136 list_del_rcu(&flow->node);
137 list_del_rcu(&flow->iter_node);
138 table_dummy_flow_deferred_free(flow);
144 static int table_dummy_delete(struct sw_table *swt,
145 const struct sw_flow_key *key, uint16_t priority, int strict)
147 struct sw_table_dummy *td = (struct sw_table_dummy *) swt;
148 struct sw_flow *flow;
149 unsigned int count = 0;
151 list_for_each_entry_rcu (flow, &td->flows, node) {
152 if (flow_del_matches(&flow->key, key, strict)
153 && (!strict || (flow->priority == priority)))
154 count += do_delete(swt, flow);
157 atomic_sub(count, &td->n_flows);
162 static int table_dummy_timeout(struct datapath *dp, struct sw_table *swt)
164 struct sw_table_dummy *td = (struct sw_table_dummy *) swt;
165 struct sw_flow *flow;
166 struct sw_flow_dummy *sfw, *n;
168 uint64_t packet_count = 0;
171 list_for_each_entry_rcu (flow, &td->flows, node) {
172 /* xxx Retrieve the packet count associated with this entry
173 * xxx and store it in "packet_count".
176 if ((packet_count > flow->packet_count)
177 && (flow->max_idle != OFP_FLOW_PERMANENT)) {
178 flow->packet_count = packet_count;
179 flow->timeout = jiffies + HZ * flow->max_idle;
182 if (flow_timeout(flow)) {
183 if (dp->flags & OFPC_SEND_FLOW_EXP) {
184 /* xxx Get byte count */
185 flow->byte_count = 0;
186 dp_send_flow_expired(dp, flow);
188 del_count += do_delete(swt, flow);
191 msleep_interruptible(1);
196 /* Remove any entries queued for removal */
197 spin_lock_bh(&pending_free_lock);
198 list_for_each_entry_safe (sfw, n, &pending_free_list, node) {
199 list_del(&sfw->node);
200 table_dummy_sfw_destroy(sfw);
202 spin_unlock_bh(&pending_free_lock);
205 atomic_sub(del_count, &td->n_flows);
210 static void table_dummy_destroy(struct sw_table *swt)
212 struct sw_table_dummy *td = (struct sw_table_dummy *)swt;
215 /* xxx This table is being destroyed, so free any data that you
216 * xxx don't want to leak.
221 while (!list_empty(&td->flows)) {
222 struct sw_flow *flow = list_entry(td->flows.next,
223 struct sw_flow, node);
224 list_del(&flow->node);
231 static int table_dummy_iterate(struct sw_table *swt,
232 const struct sw_flow_key *key,
233 struct sw_table_position *position,
234 int (*callback)(struct sw_flow *, void *),
237 struct sw_table_dummy *tl = (struct sw_table_dummy *) swt;
238 struct sw_flow *flow;
241 start = ~position->private[0];
242 list_for_each_entry_rcu (flow, &tl->iter_flows, iter_node) {
243 if (flow->serial <= start && flow_matches(key, &flow->key)) {
244 int error = callback(flow, private);
246 position->private[0] = ~flow->serial;
254 static void table_dummy_stats(struct sw_table *swt,
255 struct sw_table_stats *stats)
257 struct sw_table_dummy *td = (struct sw_table_dummy *) swt;
258 stats->name = "dummy";
259 stats->n_flows = atomic_read(&td->n_flows);
260 stats->max_flows = td->max_flows;
264 static struct sw_table *table_dummy_create(void)
266 struct sw_table_dummy *td;
267 struct sw_table *swt;
269 td = kzalloc(sizeof *td, GFP_KERNEL);
274 swt->lookup = table_dummy_lookup;
275 swt->insert = table_dummy_insert;
276 swt->delete = table_dummy_delete;
277 swt->timeout = table_dummy_timeout;
278 swt->destroy = table_dummy_destroy;
279 swt->iterate = table_dummy_iterate;
280 swt->stats = table_dummy_stats;
282 td->max_flows = DUMMY_MAX_FLOW;
283 atomic_set(&td->n_flows, 0);
284 INIT_LIST_HEAD(&td->flows);
285 INIT_LIST_HEAD(&td->iter_flows);
286 spin_lock_init(&td->lock);
289 INIT_LIST_HEAD(&pending_free_list);
290 spin_lock_init(&pending_free_lock);
295 static int __init dummy_init(void)
297 return chain_set_hw_hook(table_dummy_create, THIS_MODULE);
299 module_init(dummy_init);
301 static void dummy_cleanup(void)
303 chain_clear_hw_hook();
305 module_exit(dummy_cleanup);
307 MODULE_DESCRIPTION("Dummy hardware table driver");
308 MODULE_AUTHOR("Copyright (c) 2008 The Board of Trustees of The Leland Stanford Junior University");
309 MODULE_LICENSE("Stanford License");