2 * Copyright (c) 2009 Nicira Networks.
3 * Distributed under the terms of the GNU GPL version 2.
5 * Significant portions of this file may be copied from parts of the Linux
6 * kernel, by Linus Torvalds and others.
12 #include <linux/gfp.h>
13 #include <linux/jhash.h>
14 #include <linux/random.h>
15 #include <linux/slab.h>
16 #include <linux/vmalloc.h>
18 #include <linux/highmem.h>
19 #include <asm/pgtable.h>
21 static inline int bucket_size(int n_flows)
23 return sizeof(struct dp_bucket) + sizeof(struct sw_flow*) * n_flows;
26 static struct dp_bucket *dp_bucket_alloc(int n_flows)
28 return kmalloc(bucket_size(n_flows), GFP_KERNEL);
31 static void free_buckets(struct dp_bucket ***l1, unsigned int n_buckets,
36 for (i = 0; i < n_buckets >> DP_L1_BITS; i++) {
37 struct dp_bucket **l2 = l1[i];
40 for (j = 0; j < DP_L1_SIZE; j++) {
41 struct dp_bucket *bucket = l2[j];
47 for (k = 0; k < bucket->n_flows; k++)
48 flow_free(bucket->flows[k]);
52 free_page((unsigned long)l2);
57 static struct dp_bucket ***alloc_buckets(unsigned int n_buckets)
59 struct dp_bucket ***l1;
62 l1 = kmalloc((n_buckets >> DP_L1_BITS) * sizeof(struct dp_bucket**),
66 for (i = 0; i < n_buckets >> DP_L1_BITS; i++) {
67 l1[i] = (struct dp_bucket **)get_zeroed_page(GFP_KERNEL);
69 free_buckets(l1, i << DP_L1_BITS, 0);
77 * dp_table_create - create and return a new flow table
78 * @n_buckets: number of buckets in the new table
80 * Creates and returns a new flow table, or %NULL if memory cannot be
81 * allocated. @n_buckets must be a power of 2 in the range %DP_L1_SIZE to
84 struct dp_table *dp_table_create(unsigned int n_buckets)
86 struct dp_table *table;
88 table = kzalloc(sizeof *table, GFP_KERNEL);
92 table->n_buckets = n_buckets;
93 table->buckets = alloc_buckets(n_buckets);
96 get_random_bytes(&table->hash_seed, sizeof table->hash_seed);
107 * dp_table_destroy - destroy flow table and optionally the flows it contains
108 * @table: table to destroy (must not be %NULL)
109 * @free_flows: whether to destroy the flows
111 * If @free_flows is zero, then the buckets in @table are destroyed but not the
112 * flows within those buckets. This behavior is useful when a table is being
113 * replaced by a larger or smaller one without destroying the flows.
115 * If @free_flows is nonzero, then the flows in @table are destroyed as well as
118 void dp_table_destroy(struct dp_table *table, int free_flows)
120 free_buckets(table->buckets, table->n_buckets, free_flows);
124 static struct dp_bucket **find_bucket(struct dp_table *table, u32 hash)
126 unsigned int l1 = (hash & (table->n_buckets - 1)) >> DP_L1_SHIFT;
127 unsigned int l2 = hash & ((1 << DP_L2_BITS) - 1);
128 return &table->buckets[l1][l2];
131 static int search_bucket(const struct dp_bucket *bucket, const struct odp_flow_key *key)
135 for (i = 0; i < bucket->n_flows; i++) {
136 struct sw_flow *flow = rcu_dereference(bucket->flows[i]);
137 if (!memcmp(&flow->key, key, sizeof(struct odp_flow_key)))
144 static struct sw_flow *lookup_flow(struct dp_table *table, u32 hash,
145 const struct odp_flow_key *key)
147 struct dp_bucket **bucketp = find_bucket(table, hash);
148 struct dp_bucket *bucket = rcu_dereference(*bucketp);
154 index = search_bucket(bucket, key);
158 return bucket->flows[index];
161 static u32 flow_hash(const struct dp_table *table,
162 const struct odp_flow_key *key)
164 return jhash2((u32*)key, sizeof *key / sizeof(u32), table->hash_seed);
168 * dp_table_lookup - searches flow table for a matching flow
169 * @table: flow table to search
170 * @key: flow key for which to search
172 * Searches @table for a flow whose key is equal to @key. Returns the flow if
173 * successful, otherwise %NULL.
175 struct sw_flow *dp_table_lookup(struct dp_table *table,
176 const struct odp_flow_key *key)
178 return lookup_flow(table, flow_hash(table, key), key);
182 * dp_table_foreach - iterate through flow table
183 * @table: table to iterate
184 * @callback: function to call for each flow entry
185 * @aux: Extra data to pass to @callback
187 * Iterates through all of the flows in @table in hash order, passing each of
188 * them in turn to @callback. If @callback returns nonzero, this terminates
189 * the iteration and dp_table_foreach() returns the same value. Returns 0 if
190 * @callback never returns nonzero.
192 * This function does not try to intelligently handle the case where @callback
193 * adds or removes flows in @table.
195 int dp_table_foreach(struct dp_table *table,
196 int (*callback)(struct sw_flow *flow, void *aux),
199 unsigned int i, j, k;
200 for (i = 0; i < table->n_buckets >> DP_L1_BITS; i++) {
201 struct dp_bucket **l2 = table->buckets[i];
202 for (j = 0; j < DP_L1_SIZE; j++) {
203 struct dp_bucket *bucket = rcu_dereference(l2[j]);
207 for (k = 0; k < bucket->n_flows; k++) {
208 int error = (*callback)(bucket->flows[k], aux);
217 static int insert_flow(struct sw_flow *flow, void *new_table_)
219 struct dp_table *new_table = new_table_;
220 return dp_table_insert(new_table, flow);
223 static void dp_free_table_rcu(struct rcu_head *rcu)
225 struct dp_table *table = container_of(rcu, struct dp_table, rcu);
226 dp_table_destroy(table, 0);
230 * dp_table_expand - replace datapath's flow table by one with more buckets
231 * @dp: datapath to expand
233 * Replaces @dp's flow table by one that has twice as many buckets. All of the
234 * flows in @dp's flow table are moved to the new flow table. Returns 0 if
235 * successful, otherwise a negative error.
237 int dp_table_expand(struct datapath *dp)
239 struct dp_table *old_table = rcu_dereference(dp->table);
240 struct dp_table *new_table;
242 new_table = dp_table_create(old_table->n_buckets * 2);
246 if (dp_table_foreach(old_table, insert_flow, new_table))
247 goto error_free_new_table;
249 rcu_assign_pointer(dp->table, new_table);
250 call_rcu(&old_table->rcu, dp_free_table_rcu);
253 error_free_new_table:
254 dp_table_destroy(new_table, 0);
259 static void dp_free_table_and_flows_rcu(struct rcu_head *rcu)
261 struct dp_table *table = container_of(rcu, struct dp_table, rcu);
262 dp_table_destroy(table, 1);
266 * dp_table_flush - clear datapath's flow table
267 * @dp: datapath to clear
269 * Replaces @dp's flow table by an empty flow table, destroying all the flows
270 * in the old table (after a suitable RCU grace period).
272 int dp_table_flush(struct datapath *dp)
274 struct dp_table *old_table = rcu_dereference(dp->table);
275 struct dp_table *new_table = dp_table_create(DP_L1_SIZE);
278 rcu_assign_pointer(dp->table, new_table);
279 call_rcu(&old_table->rcu, dp_free_table_and_flows_rcu);
283 static void dp_free_bucket_rcu(struct rcu_head *rcu)
285 struct dp_bucket *bucket = container_of(rcu, struct dp_bucket, rcu);
290 * dp_table_insert - insert flow into table
291 * @table: table in which to insert flow
292 * @target: flow to insert
294 * The caller must ensure that no flow with key identical to @target->key
295 * already exists in @table. Returns 0 or a negative error (currently just
298 * The caller is responsible for updating &struct datapath's n_flows member.
300 int dp_table_insert(struct dp_table *table, struct sw_flow *target)
302 u32 hash = flow_hash(table, &target->key);
303 struct dp_bucket **oldp = find_bucket(table, hash);
304 struct dp_bucket *old = *rcu_dereference(oldp);
305 unsigned int n = old ? old->n_flows : 0;
306 struct dp_bucket *new = dp_bucket_alloc(n + 1);
311 new->n_flows = n + 1;
313 memcpy(new->flows, old->flows, n * sizeof(struct sw_flow*));
314 new->flows[n] = target;
316 rcu_assign_pointer(*oldp, new);
318 call_rcu(&old->rcu, dp_free_bucket_rcu);
324 * dp_table_delete - remove flow from table
325 * @table: table from which to remove flow
326 * @target: flow to remove
328 * The caller must ensure that @target itself is in @table. (It is not
329 * good enough for @table to contain a different flow with a key equal to
332 * Returns 0 or a negative error (currently just -ENOMEM). Yes, it *is*
333 * possible for a flow deletion to fail due to lack of memory.
335 * The caller is responsible for updating &struct datapath's n_flows member.
337 int dp_table_delete(struct dp_table *table, struct sw_flow *target)
339 u32 hash = flow_hash(table, &target->key);
340 struct dp_bucket **oldp = find_bucket(table, hash);
341 struct dp_bucket *old = *rcu_dereference(oldp);
342 unsigned int n = old->n_flows;
343 struct dp_bucket *new;
348 new = dp_bucket_alloc(n - 1);
353 for (i = 0; i < n; i++) {
354 struct sw_flow *flow = old->flows[i];
356 new->flows[new->n_flows++] = flow;
358 WARN_ON_ONCE(new->n_flows != n - 1);
363 rcu_assign_pointer(*oldp, new);
364 call_rcu(&old->rcu, dp_free_bucket_rcu);