Add-on hardware tables initial implementation.
[openvswitch] / datapath / chain.c
1 /*
2  * Distributed under the terms of the GNU GPL version 2.
3  * Copyright (c) 2007, 2008 The Board of Trustees of The Leland 
4  * Stanford Junior University
5  */
6
7 #include "chain.h"
8 #include "flow.h"
9 #include "table.h"
10 #include <linux/module.h>
11 #include <linux/rcupdate.h>
12 #include <linux/slab.h>
13 #include <linux/spinlock.h>
14
15 static struct sw_table *(*create_hw_table_hook)(void);
16 static struct module *hw_table_owner;
17 static DEFINE_SPINLOCK(hook_lock);
18
19 /* Attempts to append 'table' to the set of tables in 'chain'.  Returns 0 or
20  * negative error.  If 'table' is null it is assumed that table creation failed
21  * due to out-of-memory. */
22 static int add_table(struct sw_chain *chain, struct sw_table *table)
23 {
24         if (table == NULL)
25                 return -ENOMEM;
26         if (chain->n_tables >= CHAIN_MAX_TABLES) {
27                 printk("too many tables in chain\n");
28                 table->destroy(table);
29                 return -ENOBUFS;
30         }
31         chain->tables[chain->n_tables++] = table;
32         return 0;
33 }
34
35 /* Creates and returns a new chain associated with 'dp'.  Returns NULL if the
36  * chain cannot be created. */
37 struct sw_chain *chain_create(struct datapath *dp)
38 {
39         struct sw_chain *chain = kzalloc(sizeof *chain, GFP_KERNEL);
40         if (chain == NULL)
41                 goto error;
42         chain->dp = dp;
43         chain->owner = try_module_get(hw_table_owner) ? hw_table_owner : NULL;
44         if (chain->owner && create_hw_table_hook) {
45                 struct sw_table *hwtable = create_hw_table_hook();
46                 if (!hwtable || add_table(chain, hwtable))
47                         goto error;
48         }
49
50         if (add_table(chain, table_hash2_create(0x1EDC6F41, TABLE_HASH_MAX_FLOWS,
51                                                 0x741B8CD7, TABLE_HASH_MAX_FLOWS))
52             || add_table(chain, table_linear_create(TABLE_LINEAR_MAX_FLOWS)))
53                 goto error;
54         return chain;
55
56 error:
57         if (chain)
58                 chain_destroy(chain);
59         return NULL;
60 }
61
62 /* Searches 'chain' for a flow matching 'key', which must not have any wildcard
63  * fields.  Returns the flow if successful, otherwise a null pointer.
64  *
65  * Caller must hold rcu_read_lock, and not release it until it is done with the
66  * returned flow. */
67 struct sw_flow *chain_lookup(struct sw_chain *chain,
68                          const struct sw_flow_key *key)
69 {
70         int i;
71
72         BUG_ON(key->wildcards);
73         for (i = 0; i < chain->n_tables; i++) {
74                 struct sw_table *t = chain->tables[i];
75                 struct sw_flow *flow = t->lookup(t, key);
76                 if (flow)
77                         return flow;
78         }
79         return NULL;
80 }
81
82 /* Inserts 'flow' into 'chain', replacing any duplicate flow.  Returns 0 if
83  * successful or a negative error.
84  *
85  * If successful, 'flow' becomes owned by the chain, otherwise it is retained
86  * by the caller.
87  *
88  * Caller must hold rcu_read_lock.  If insertion is successful, it must not
89  * release rcu_read_lock until it is done with the inserted flow. */
90 int chain_insert(struct sw_chain *chain, struct sw_flow *flow)
91 {
92         int i;
93
94         for (i = 0; i < chain->n_tables; i++) {
95                 struct sw_table *t = chain->tables[i];
96                 if (t->insert(t, flow))
97                         return 0;
98         }
99
100         return -ENOBUFS;
101 }
102
103 /* Deletes from 'chain' any and all flows that match 'key'.  Returns the number
104  * of flows that were deleted.
105  *
106  * Expensive in the general case as currently implemented, since it requires
107  * iterating through the entire contents of each table for keys that contain
108  * wildcards.  Relatively cheap for fully specified keys.
109  *
110  * The caller need not hold any locks. */
111 int chain_delete(struct sw_chain *chain, const struct sw_flow_key *key, 
112                 uint16_t priority, int strict)
113 {
114         int count = 0;
115         int i;
116
117         for (i = 0; i < chain->n_tables; i++) {
118                 struct sw_table *t = chain->tables[i];
119                 rcu_read_lock();
120                 count += t->delete(t, key, priority, strict);
121                 rcu_read_unlock();
122         }
123
124         return count;
125 }
126
127 /* Performs timeout processing on all the tables in 'chain'.  Returns the
128  * number of flow entries deleted through expiration.
129  *
130  * Expensive as currently implemented, since it iterates through the entire
131  * contents of each table.
132  *
133  * The caller need not hold any locks. */
134 int chain_timeout(struct sw_chain *chain)
135 {
136         int count = 0;
137         int i;
138
139         for (i = 0; i < chain->n_tables; i++) {
140                 struct sw_table *t = chain->tables[i];
141                 rcu_read_lock();
142                 count += t->timeout(chain->dp, t);
143                 rcu_read_unlock();
144         }
145         return count;
146 }
147
148 /* Destroys 'chain', which must not have any users. */
149 void chain_destroy(struct sw_chain *chain)
150 {
151         int i;
152
153         synchronize_rcu();
154         for (i = 0; i < chain->n_tables; i++) {
155                 struct sw_table *t = chain->tables[i];
156                 t->destroy(t);
157         }
158         module_put(chain->owner);
159         kfree(chain);
160 }
161
162 /* Prints statistics for each of the tables in 'chain'. */
163 void chain_print_stats(struct sw_chain *chain)
164 {
165         int i;
166
167         printk("\n");
168         for (i = 0; i < chain->n_tables; i++) {
169                 struct sw_table *t = chain->tables[i];
170                 struct sw_table_stats stats;
171                 t->stats(t, &stats);
172                 printk("%s: %lu/%lu flows\n",
173                                         stats.name, stats.n_flows, stats.max_flows);
174         }
175 }
176
177
178 int chain_set_hw_hook(struct sw_table *(*create_hw_table)(void),
179                       struct module *owner)
180 {
181         int retval = -EBUSY;
182
183         spin_lock(&hook_lock);
184         if (!create_hw_table_hook) {
185                 create_hw_table_hook = create_hw_table;
186                 hw_table_owner = owner;
187                 retval = 0;
188         }
189         spin_unlock(&hook_lock);
190
191         return retval;
192 }
193 EXPORT_SYMBOL(chain_set_hw_hook);
194
195 void chain_clear_hw_hook(void)
196 {
197         create_hw_table_hook = NULL;
198         hw_table_owner = NULL;
199 }
200 EXPORT_SYMBOL(chain_clear_hw_hook);