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
10 #include <linux/module.h>
11 #include <linux/rcupdate.h>
12 #include <linux/slab.h>
13 #include <linux/spinlock.h>
15 static struct sw_table *(*create_hw_table_hook)(void);
16 static struct module *hw_table_owner;
17 static DEFINE_SPINLOCK(hook_lock);
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)
26 if (chain->n_tables >= CHAIN_MAX_TABLES) {
27 printk("too many tables in chain\n");
28 table->destroy(table);
31 chain->tables[chain->n_tables++] = table;
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)
39 struct sw_chain *chain = kzalloc(sizeof *chain, GFP_KERNEL);
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))
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)))
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.
65 * Caller must hold rcu_read_lock or dp_mutex. */
66 struct sw_flow *chain_lookup(struct sw_chain *chain,
67 const struct sw_flow_key *key)
71 BUG_ON(key->wildcards);
72 for (i = 0; i < chain->n_tables; i++) {
73 struct sw_table *t = chain->tables[i];
74 struct sw_flow *flow = t->lookup(t, key);
83 /* Inserts 'flow' into 'chain', replacing any duplicate flow. Returns 0 if
84 * successful or a negative error.
86 * If successful, 'flow' becomes owned by the chain, otherwise it is retained
89 * Caller must hold dp_mutex. */
90 int chain_insert(struct sw_chain *chain, struct sw_flow *flow)
95 for (i = 0; i < chain->n_tables; i++) {
96 struct sw_table *t = chain->tables[i];
97 if (t->insert(t, flow))
104 /* Modifies actions in 'chain' that match 'key'. Returns the number of
105 * flows that were modified.
107 * Expensive in the general case as currently implemented, since it requires
108 * iterating through the entire contents of each table for keys that contain
109 * wildcards. Relatively cheap for fully specified keys. */
111 chain_modify(struct sw_chain *chain, const struct sw_flow_key *key,
112 const struct ofp_action *actions, int n_actions)
117 for (i = 0; i < chain->n_tables; i++) {
118 struct sw_table *t = chain->tables[i];
119 count += t->modify(t, key, actions, n_actions);
125 /* Deletes from 'chain' any and all flows that match 'key'. Returns the number
126 * of flows that were deleted.
128 * Expensive in the general case as currently implemented, since it requires
129 * iterating through the entire contents of each table for keys that contain
130 * wildcards. Relatively cheap for fully specified keys.
132 * Caller must hold dp_mutex. */
133 int chain_delete(struct sw_chain *chain, const struct sw_flow_key *key,
134 uint16_t priority, int strict)
140 for (i = 0; i < chain->n_tables; i++) {
141 struct sw_table *t = chain->tables[i];
142 count += t->delete(t, key, priority, strict);
148 /* Performs timeout processing on all the tables in 'chain'. Returns the
149 * number of flow entries deleted through expiration.
151 * Expensive as currently implemented, since it iterates through the entire
152 * contents of each table.
154 * Caller must not hold dp_mutex, because individual tables take and release it
156 int chain_timeout(struct sw_chain *chain)
162 for (i = 0; i < chain->n_tables; i++) {
163 struct sw_table *t = chain->tables[i];
164 count += t->timeout(chain->dp, t);
169 /* Destroys 'chain', which must not have any users. */
170 void chain_destroy(struct sw_chain *chain)
175 for (i = 0; i < chain->n_tables; i++) {
176 struct sw_table *t = chain->tables[i];
180 module_put(chain->owner);
184 int chain_set_hw_hook(struct sw_table *(*create_hw_table)(void),
185 struct module *owner)
189 spin_lock(&hook_lock);
190 if (!create_hw_table_hook) {
191 create_hw_table_hook = create_hw_table;
192 hw_table_owner = owner;
195 spin_unlock(&hook_lock);
199 EXPORT_SYMBOL(chain_set_hw_hook);
201 void chain_clear_hw_hook(void)
203 create_hw_table_hook = NULL;
204 hw_table_owner = NULL;
206 EXPORT_SYMBOL(chain_clear_hw_hook);