1e6445719SPravin B Shelar /* 2e6445719SPravin B Shelar * Copyright (c) 2007-2013 Nicira, Inc. 3e6445719SPravin B Shelar * 4e6445719SPravin B Shelar * This program is free software; you can redistribute it and/or 5e6445719SPravin B Shelar * modify it under the terms of version 2 of the GNU General Public 6e6445719SPravin B Shelar * License as published by the Free Software Foundation. 7e6445719SPravin B Shelar * 8e6445719SPravin B Shelar * This program is distributed in the hope that it will be useful, but 9e6445719SPravin B Shelar * WITHOUT ANY WARRANTY; without even the implied warranty of 10e6445719SPravin B Shelar * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11e6445719SPravin B Shelar * General Public License for more details. 12e6445719SPravin B Shelar * 13e6445719SPravin B Shelar * You should have received a copy of the GNU General Public License 14e6445719SPravin B Shelar * along with this program; if not, write to the Free Software 15e6445719SPravin B Shelar * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 16e6445719SPravin B Shelar * 02110-1301, USA 17e6445719SPravin B Shelar */ 18e6445719SPravin B Shelar 19e6445719SPravin B Shelar #include "flow.h" 20e6445719SPravin B Shelar #include "datapath.h" 21e6445719SPravin B Shelar #include <linux/uaccess.h> 22e6445719SPravin B Shelar #include <linux/netdevice.h> 23e6445719SPravin B Shelar #include <linux/etherdevice.h> 24e6445719SPravin B Shelar #include <linux/if_ether.h> 25e6445719SPravin B Shelar #include <linux/if_vlan.h> 26e6445719SPravin B Shelar #include <net/llc_pdu.h> 27e6445719SPravin B Shelar #include <linux/kernel.h> 28e6445719SPravin B Shelar #include <linux/jhash.h> 29e6445719SPravin B Shelar #include <linux/jiffies.h> 30e6445719SPravin B Shelar #include <linux/llc.h> 31e6445719SPravin B Shelar #include <linux/module.h> 32e6445719SPravin B Shelar #include <linux/in.h> 33e6445719SPravin B Shelar #include <linux/rcupdate.h> 34e6445719SPravin B Shelar #include <linux/if_arp.h> 35e6445719SPravin B Shelar #include <linux/ip.h> 36e6445719SPravin B Shelar #include <linux/ipv6.h> 37e6445719SPravin B Shelar #include <linux/sctp.h> 38e6445719SPravin B Shelar #include <linux/tcp.h> 39e6445719SPravin B Shelar #include <linux/udp.h> 40e6445719SPravin B Shelar #include <linux/icmp.h> 41e6445719SPravin B Shelar #include <linux/icmpv6.h> 42e6445719SPravin B Shelar #include <linux/rculist.h> 43e6445719SPravin B Shelar #include <net/ip.h> 44e6445719SPravin B Shelar #include <net/ipv6.h> 45e6445719SPravin B Shelar #include <net/ndisc.h> 46e6445719SPravin B Shelar 47b637e498SPravin B Shelar #include "datapath.h" 48b637e498SPravin B Shelar 49b637e498SPravin B Shelar #define TBL_MIN_BUCKETS 1024 50b637e498SPravin B Shelar #define REHASH_INTERVAL (10 * 60 * HZ) 51b637e498SPravin B Shelar 52e6445719SPravin B Shelar static struct kmem_cache *flow_cache; 53e6445719SPravin B Shelar 54e6445719SPravin B Shelar static u16 range_n_bytes(const struct sw_flow_key_range *range) 55e6445719SPravin B Shelar { 56e6445719SPravin B Shelar return range->end - range->start; 57e6445719SPravin B Shelar } 58e6445719SPravin B Shelar 59e6445719SPravin B Shelar void ovs_flow_mask_key(struct sw_flow_key *dst, const struct sw_flow_key *src, 60e6445719SPravin B Shelar const struct sw_flow_mask *mask) 61e6445719SPravin B Shelar { 62e6445719SPravin B Shelar const long *m = (long *)((u8 *)&mask->key + mask->range.start); 63e6445719SPravin B Shelar const long *s = (long *)((u8 *)src + mask->range.start); 64e6445719SPravin B Shelar long *d = (long *)((u8 *)dst + mask->range.start); 65e6445719SPravin B Shelar int i; 66e6445719SPravin B Shelar 67e6445719SPravin B Shelar /* The memory outside of the 'mask->range' are not set since 68e6445719SPravin B Shelar * further operations on 'dst' only uses contents within 69e6445719SPravin B Shelar * 'mask->range'. 70e6445719SPravin B Shelar */ 71e6445719SPravin B Shelar for (i = 0; i < range_n_bytes(&mask->range); i += sizeof(long)) 72e6445719SPravin B Shelar *d++ = *s++ & *m++; 73e6445719SPravin B Shelar } 74e6445719SPravin B Shelar 75e6445719SPravin B Shelar struct sw_flow *ovs_flow_alloc(void) 76e6445719SPravin B Shelar { 77e6445719SPravin B Shelar struct sw_flow *flow; 78e6445719SPravin B Shelar 79e6445719SPravin B Shelar flow = kmem_cache_alloc(flow_cache, GFP_KERNEL); 80e6445719SPravin B Shelar if (!flow) 81e6445719SPravin B Shelar return ERR_PTR(-ENOMEM); 82e6445719SPravin B Shelar 83e6445719SPravin B Shelar spin_lock_init(&flow->lock); 84e6445719SPravin B Shelar flow->sf_acts = NULL; 85e6445719SPravin B Shelar flow->mask = NULL; 86e6445719SPravin B Shelar 87e6445719SPravin B Shelar return flow; 88e6445719SPravin B Shelar } 89e6445719SPravin B Shelar 90b637e498SPravin B Shelar int ovs_flow_tbl_count(struct flow_table *table) 91b637e498SPravin B Shelar { 92b637e498SPravin B Shelar return table->count; 93b637e498SPravin B Shelar } 94b637e498SPravin B Shelar 95e6445719SPravin B Shelar static struct flex_array *alloc_buckets(unsigned int n_buckets) 96e6445719SPravin B Shelar { 97e6445719SPravin B Shelar struct flex_array *buckets; 98e6445719SPravin B Shelar int i, err; 99e6445719SPravin B Shelar 100e6445719SPravin B Shelar buckets = flex_array_alloc(sizeof(struct hlist_head), 101e6445719SPravin B Shelar n_buckets, GFP_KERNEL); 102e6445719SPravin B Shelar if (!buckets) 103e6445719SPravin B Shelar return NULL; 104e6445719SPravin B Shelar 105e6445719SPravin B Shelar err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL); 106e6445719SPravin B Shelar if (err) { 107e6445719SPravin B Shelar flex_array_free(buckets); 108e6445719SPravin B Shelar return NULL; 109e6445719SPravin B Shelar } 110e6445719SPravin B Shelar 111e6445719SPravin B Shelar for (i = 0; i < n_buckets; i++) 112e6445719SPravin B Shelar INIT_HLIST_HEAD((struct hlist_head *) 113e6445719SPravin B Shelar flex_array_get(buckets, i)); 114e6445719SPravin B Shelar 115e6445719SPravin B Shelar return buckets; 116e6445719SPravin B Shelar } 117e6445719SPravin B Shelar 118e6445719SPravin B Shelar static void flow_free(struct sw_flow *flow) 119e6445719SPravin B Shelar { 120e6445719SPravin B Shelar kfree((struct sf_flow_acts __force *)flow->sf_acts); 121e6445719SPravin B Shelar kmem_cache_free(flow_cache, flow); 122e6445719SPravin B Shelar } 123e6445719SPravin B Shelar 124e6445719SPravin B Shelar static void rcu_free_flow_callback(struct rcu_head *rcu) 125e6445719SPravin B Shelar { 126e6445719SPravin B Shelar struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu); 127e6445719SPravin B Shelar 128e6445719SPravin B Shelar flow_free(flow); 129e6445719SPravin B Shelar } 130e6445719SPravin B Shelar 131*618ed0c8SPravin B Shelar static void rcu_free_sw_flow_mask_cb(struct rcu_head *rcu) 132*618ed0c8SPravin B Shelar { 133*618ed0c8SPravin B Shelar struct sw_flow_mask *mask = container_of(rcu, struct sw_flow_mask, rcu); 134*618ed0c8SPravin B Shelar 135*618ed0c8SPravin B Shelar kfree(mask); 136*618ed0c8SPravin B Shelar } 137*618ed0c8SPravin B Shelar 138*618ed0c8SPravin B Shelar static void flow_mask_del_ref(struct sw_flow_mask *mask, bool deferred) 139*618ed0c8SPravin B Shelar { 140*618ed0c8SPravin B Shelar if (!mask) 141*618ed0c8SPravin B Shelar return; 142*618ed0c8SPravin B Shelar 143*618ed0c8SPravin B Shelar BUG_ON(!mask->ref_count); 144*618ed0c8SPravin B Shelar mask->ref_count--; 145*618ed0c8SPravin B Shelar 146*618ed0c8SPravin B Shelar if (!mask->ref_count) { 147*618ed0c8SPravin B Shelar list_del_rcu(&mask->list); 148*618ed0c8SPravin B Shelar if (deferred) 149*618ed0c8SPravin B Shelar call_rcu(&mask->rcu, rcu_free_sw_flow_mask_cb); 150*618ed0c8SPravin B Shelar else 151*618ed0c8SPravin B Shelar kfree(mask); 152*618ed0c8SPravin B Shelar } 153*618ed0c8SPravin B Shelar } 154*618ed0c8SPravin B Shelar 155e6445719SPravin B Shelar void ovs_flow_free(struct sw_flow *flow, bool deferred) 156e6445719SPravin B Shelar { 157e6445719SPravin B Shelar if (!flow) 158e6445719SPravin B Shelar return; 159e6445719SPravin B Shelar 160*618ed0c8SPravin B Shelar flow_mask_del_ref(flow->mask, deferred); 161e6445719SPravin B Shelar 162e6445719SPravin B Shelar if (deferred) 163e6445719SPravin B Shelar call_rcu(&flow->rcu, rcu_free_flow_callback); 164e6445719SPravin B Shelar else 165e6445719SPravin B Shelar flow_free(flow); 166e6445719SPravin B Shelar } 167e6445719SPravin B Shelar 168e6445719SPravin B Shelar static void free_buckets(struct flex_array *buckets) 169e6445719SPravin B Shelar { 170e6445719SPravin B Shelar flex_array_free(buckets); 171e6445719SPravin B Shelar } 172e6445719SPravin B Shelar 173b637e498SPravin B Shelar static void __table_instance_destroy(struct table_instance *ti) 174e6445719SPravin B Shelar { 175e6445719SPravin B Shelar int i; 176e6445719SPravin B Shelar 177b637e498SPravin B Shelar if (ti->keep_flows) 178e6445719SPravin B Shelar goto skip_flows; 179e6445719SPravin B Shelar 180b637e498SPravin B Shelar for (i = 0; i < ti->n_buckets; i++) { 181e6445719SPravin B Shelar struct sw_flow *flow; 182b637e498SPravin B Shelar struct hlist_head *head = flex_array_get(ti->buckets, i); 183e6445719SPravin B Shelar struct hlist_node *n; 184b637e498SPravin B Shelar int ver = ti->node_ver; 185e6445719SPravin B Shelar 186e6445719SPravin B Shelar hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) { 187e6445719SPravin B Shelar hlist_del(&flow->hash_node[ver]); 188e6445719SPravin B Shelar ovs_flow_free(flow, false); 189e6445719SPravin B Shelar } 190e6445719SPravin B Shelar } 191e6445719SPravin B Shelar 192e6445719SPravin B Shelar skip_flows: 193b637e498SPravin B Shelar free_buckets(ti->buckets); 194b637e498SPravin B Shelar kfree(ti); 195e6445719SPravin B Shelar } 196e6445719SPravin B Shelar 197b637e498SPravin B Shelar static struct table_instance *table_instance_alloc(int new_size) 198e6445719SPravin B Shelar { 199b637e498SPravin B Shelar struct table_instance *ti = kmalloc(sizeof(*ti), GFP_KERNEL); 200e6445719SPravin B Shelar 201b637e498SPravin B Shelar if (!ti) 202e6445719SPravin B Shelar return NULL; 203e6445719SPravin B Shelar 204b637e498SPravin B Shelar ti->buckets = alloc_buckets(new_size); 205e6445719SPravin B Shelar 206b637e498SPravin B Shelar if (!ti->buckets) { 207b637e498SPravin B Shelar kfree(ti); 208e6445719SPravin B Shelar return NULL; 209e6445719SPravin B Shelar } 210b637e498SPravin B Shelar ti->n_buckets = new_size; 211b637e498SPravin B Shelar ti->node_ver = 0; 212b637e498SPravin B Shelar ti->keep_flows = false; 213b637e498SPravin B Shelar get_random_bytes(&ti->hash_seed, sizeof(u32)); 214b637e498SPravin B Shelar 215b637e498SPravin B Shelar return ti; 216b637e498SPravin B Shelar } 217b637e498SPravin B Shelar 218b637e498SPravin B Shelar int ovs_flow_tbl_init(struct flow_table *table) 219b637e498SPravin B Shelar { 220b637e498SPravin B Shelar struct table_instance *ti; 221b637e498SPravin B Shelar 222b637e498SPravin B Shelar ti = table_instance_alloc(TBL_MIN_BUCKETS); 223b637e498SPravin B Shelar 224b637e498SPravin B Shelar if (!ti) 225b637e498SPravin B Shelar return -ENOMEM; 226b637e498SPravin B Shelar 227b637e498SPravin B Shelar rcu_assign_pointer(table->ti, ti); 228b637e498SPravin B Shelar INIT_LIST_HEAD(&table->mask_list); 229b637e498SPravin B Shelar table->last_rehash = jiffies; 230e6445719SPravin B Shelar table->count = 0; 231b637e498SPravin B Shelar return 0; 232e6445719SPravin B Shelar } 233e6445719SPravin B Shelar 234e6445719SPravin B Shelar static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu) 235e6445719SPravin B Shelar { 236b637e498SPravin B Shelar struct table_instance *ti = container_of(rcu, struct table_instance, rcu); 237e6445719SPravin B Shelar 238b637e498SPravin B Shelar __table_instance_destroy(ti); 239b637e498SPravin B Shelar } 240b637e498SPravin B Shelar 241b637e498SPravin B Shelar static void table_instance_destroy(struct table_instance *ti, bool deferred) 242b637e498SPravin B Shelar { 243b637e498SPravin B Shelar if (!ti) 244b637e498SPravin B Shelar return; 245b637e498SPravin B Shelar 246b637e498SPravin B Shelar if (deferred) 247b637e498SPravin B Shelar call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb); 248b637e498SPravin B Shelar else 249b637e498SPravin B Shelar __table_instance_destroy(ti); 250e6445719SPravin B Shelar } 251e6445719SPravin B Shelar 252*618ed0c8SPravin B Shelar void ovs_flow_tbl_destroy(struct flow_table *table) 253e6445719SPravin B Shelar { 254b637e498SPravin B Shelar struct table_instance *ti = ovsl_dereference(table->ti); 255e6445719SPravin B Shelar 256*618ed0c8SPravin B Shelar table_instance_destroy(ti, false); 257e6445719SPravin B Shelar } 258e6445719SPravin B Shelar 259b637e498SPravin B Shelar struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti, 260e6445719SPravin B Shelar u32 *bucket, u32 *last) 261e6445719SPravin B Shelar { 262e6445719SPravin B Shelar struct sw_flow *flow; 263e6445719SPravin B Shelar struct hlist_head *head; 264e6445719SPravin B Shelar int ver; 265e6445719SPravin B Shelar int i; 266e6445719SPravin B Shelar 267b637e498SPravin B Shelar ver = ti->node_ver; 268b637e498SPravin B Shelar while (*bucket < ti->n_buckets) { 269e6445719SPravin B Shelar i = 0; 270b637e498SPravin B Shelar head = flex_array_get(ti->buckets, *bucket); 271e6445719SPravin B Shelar hlist_for_each_entry_rcu(flow, head, hash_node[ver]) { 272e6445719SPravin B Shelar if (i < *last) { 273e6445719SPravin B Shelar i++; 274e6445719SPravin B Shelar continue; 275e6445719SPravin B Shelar } 276e6445719SPravin B Shelar *last = i + 1; 277e6445719SPravin B Shelar return flow; 278e6445719SPravin B Shelar } 279e6445719SPravin B Shelar (*bucket)++; 280e6445719SPravin B Shelar *last = 0; 281e6445719SPravin B Shelar } 282e6445719SPravin B Shelar 283e6445719SPravin B Shelar return NULL; 284e6445719SPravin B Shelar } 285e6445719SPravin B Shelar 286b637e498SPravin B Shelar static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash) 287e6445719SPravin B Shelar { 288b637e498SPravin B Shelar hash = jhash_1word(hash, ti->hash_seed); 289b637e498SPravin B Shelar return flex_array_get(ti->buckets, 290b637e498SPravin B Shelar (hash & (ti->n_buckets - 1))); 291e6445719SPravin B Shelar } 292e6445719SPravin B Shelar 293b637e498SPravin B Shelar static void table_instance_insert(struct table_instance *ti, struct sw_flow *flow) 294e6445719SPravin B Shelar { 295e6445719SPravin B Shelar struct hlist_head *head; 296e6445719SPravin B Shelar 297b637e498SPravin B Shelar head = find_bucket(ti, flow->hash); 298b637e498SPravin B Shelar hlist_add_head_rcu(&flow->hash_node[ti->node_ver], head); 299e6445719SPravin B Shelar } 300e6445719SPravin B Shelar 301b637e498SPravin B Shelar static void flow_table_copy_flows(struct table_instance *old, 302b637e498SPravin B Shelar struct table_instance *new) 303e6445719SPravin B Shelar { 304e6445719SPravin B Shelar int old_ver; 305e6445719SPravin B Shelar int i; 306e6445719SPravin B Shelar 307e6445719SPravin B Shelar old_ver = old->node_ver; 308e6445719SPravin B Shelar new->node_ver = !old_ver; 309e6445719SPravin B Shelar 310e6445719SPravin B Shelar /* Insert in new table. */ 311e6445719SPravin B Shelar for (i = 0; i < old->n_buckets; i++) { 312e6445719SPravin B Shelar struct sw_flow *flow; 313e6445719SPravin B Shelar struct hlist_head *head; 314e6445719SPravin B Shelar 315e6445719SPravin B Shelar head = flex_array_get(old->buckets, i); 316e6445719SPravin B Shelar 317e6445719SPravin B Shelar hlist_for_each_entry(flow, head, hash_node[old_ver]) 318b637e498SPravin B Shelar table_instance_insert(new, flow); 319e6445719SPravin B Shelar } 320e6445719SPravin B Shelar 321e6445719SPravin B Shelar old->keep_flows = true; 322e6445719SPravin B Shelar } 323e6445719SPravin B Shelar 324b637e498SPravin B Shelar static struct table_instance *table_instance_rehash(struct table_instance *ti, 325e6445719SPravin B Shelar int n_buckets) 326e6445719SPravin B Shelar { 327b637e498SPravin B Shelar struct table_instance *new_ti; 328e6445719SPravin B Shelar 329b637e498SPravin B Shelar new_ti = table_instance_alloc(n_buckets); 330b637e498SPravin B Shelar if (!new_ti) 331*618ed0c8SPravin B Shelar return NULL; 332e6445719SPravin B Shelar 333b637e498SPravin B Shelar flow_table_copy_flows(ti, new_ti); 334e6445719SPravin B Shelar 335b637e498SPravin B Shelar return new_ti; 336e6445719SPravin B Shelar } 337e6445719SPravin B Shelar 338b637e498SPravin B Shelar int ovs_flow_tbl_flush(struct flow_table *flow_table) 339e6445719SPravin B Shelar { 340b637e498SPravin B Shelar struct table_instance *old_ti; 341b637e498SPravin B Shelar struct table_instance *new_ti; 342e6445719SPravin B Shelar 343b637e498SPravin B Shelar old_ti = ovsl_dereference(flow_table->ti); 344b637e498SPravin B Shelar new_ti = table_instance_alloc(TBL_MIN_BUCKETS); 345b637e498SPravin B Shelar if (!new_ti) 346b637e498SPravin B Shelar return -ENOMEM; 347b637e498SPravin B Shelar 348b637e498SPravin B Shelar rcu_assign_pointer(flow_table->ti, new_ti); 349b637e498SPravin B Shelar flow_table->last_rehash = jiffies; 350b637e498SPravin B Shelar flow_table->count = 0; 351b637e498SPravin B Shelar 352b637e498SPravin B Shelar table_instance_destroy(old_ti, true); 353b637e498SPravin B Shelar return 0; 354e6445719SPravin B Shelar } 355e6445719SPravin B Shelar 356e6445719SPravin B Shelar static u32 flow_hash(const struct sw_flow_key *key, int key_start, 357e6445719SPravin B Shelar int key_end) 358e6445719SPravin B Shelar { 359e6445719SPravin B Shelar u32 *hash_key = (u32 *)((u8 *)key + key_start); 360e6445719SPravin B Shelar int hash_u32s = (key_end - key_start) >> 2; 361e6445719SPravin B Shelar 362e6445719SPravin B Shelar /* Make sure number of hash bytes are multiple of u32. */ 363e6445719SPravin B Shelar BUILD_BUG_ON(sizeof(long) % sizeof(u32)); 364e6445719SPravin B Shelar 365e6445719SPravin B Shelar return jhash2(hash_key, hash_u32s, 0); 366e6445719SPravin B Shelar } 367e6445719SPravin B Shelar 368e6445719SPravin B Shelar static int flow_key_start(const struct sw_flow_key *key) 369e6445719SPravin B Shelar { 370e6445719SPravin B Shelar if (key->tun_key.ipv4_dst) 371e6445719SPravin B Shelar return 0; 372e6445719SPravin B Shelar else 373e6445719SPravin B Shelar return rounddown(offsetof(struct sw_flow_key, phy), 374e6445719SPravin B Shelar sizeof(long)); 375e6445719SPravin B Shelar } 376e6445719SPravin B Shelar 377e6445719SPravin B Shelar static bool cmp_key(const struct sw_flow_key *key1, 378e6445719SPravin B Shelar const struct sw_flow_key *key2, 379e6445719SPravin B Shelar int key_start, int key_end) 380e6445719SPravin B Shelar { 381e6445719SPravin B Shelar const long *cp1 = (long *)((u8 *)key1 + key_start); 382e6445719SPravin B Shelar const long *cp2 = (long *)((u8 *)key2 + key_start); 383e6445719SPravin B Shelar long diffs = 0; 384e6445719SPravin B Shelar int i; 385e6445719SPravin B Shelar 386e6445719SPravin B Shelar for (i = key_start; i < key_end; i += sizeof(long)) 387e6445719SPravin B Shelar diffs |= *cp1++ ^ *cp2++; 388e6445719SPravin B Shelar 389e6445719SPravin B Shelar return diffs == 0; 390e6445719SPravin B Shelar } 391e6445719SPravin B Shelar 392e6445719SPravin B Shelar static bool flow_cmp_masked_key(const struct sw_flow *flow, 393e6445719SPravin B Shelar const struct sw_flow_key *key, 394e6445719SPravin B Shelar int key_start, int key_end) 395e6445719SPravin B Shelar { 396e6445719SPravin B Shelar return cmp_key(&flow->key, key, key_start, key_end); 397e6445719SPravin B Shelar } 398e6445719SPravin B Shelar 399e6445719SPravin B Shelar bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow, 400e6445719SPravin B Shelar struct sw_flow_match *match) 401e6445719SPravin B Shelar { 402e6445719SPravin B Shelar struct sw_flow_key *key = match->key; 403e6445719SPravin B Shelar int key_start = flow_key_start(key); 404e6445719SPravin B Shelar int key_end = match->range.end; 405e6445719SPravin B Shelar 406e6445719SPravin B Shelar return cmp_key(&flow->unmasked_key, key, key_start, key_end); 407e6445719SPravin B Shelar } 408e6445719SPravin B Shelar 409b637e498SPravin B Shelar static struct sw_flow *masked_flow_lookup(struct table_instance *ti, 410e6445719SPravin B Shelar const struct sw_flow_key *unmasked, 411e6445719SPravin B Shelar struct sw_flow_mask *mask) 412e6445719SPravin B Shelar { 413e6445719SPravin B Shelar struct sw_flow *flow; 414e6445719SPravin B Shelar struct hlist_head *head; 415e6445719SPravin B Shelar int key_start = mask->range.start; 416e6445719SPravin B Shelar int key_end = mask->range.end; 417e6445719SPravin B Shelar u32 hash; 418e6445719SPravin B Shelar struct sw_flow_key masked_key; 419e6445719SPravin B Shelar 420e6445719SPravin B Shelar ovs_flow_mask_key(&masked_key, unmasked, mask); 421e6445719SPravin B Shelar hash = flow_hash(&masked_key, key_start, key_end); 422b637e498SPravin B Shelar head = find_bucket(ti, hash); 423b637e498SPravin B Shelar hlist_for_each_entry_rcu(flow, head, hash_node[ti->node_ver]) { 424e6445719SPravin B Shelar if (flow->mask == mask && 425e6445719SPravin B Shelar flow_cmp_masked_key(flow, &masked_key, 426e6445719SPravin B Shelar key_start, key_end)) 427e6445719SPravin B Shelar return flow; 428e6445719SPravin B Shelar } 429e6445719SPravin B Shelar return NULL; 430e6445719SPravin B Shelar } 431e6445719SPravin B Shelar 432e6445719SPravin B Shelar struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl, 433e6445719SPravin B Shelar const struct sw_flow_key *key) 434e6445719SPravin B Shelar { 435b637e498SPravin B Shelar struct table_instance *ti = rcu_dereference(tbl->ti); 436e6445719SPravin B Shelar struct sw_flow_mask *mask; 437b637e498SPravin B Shelar struct sw_flow *flow; 438e6445719SPravin B Shelar 439b637e498SPravin B Shelar list_for_each_entry_rcu(mask, &tbl->mask_list, list) { 440b637e498SPravin B Shelar flow = masked_flow_lookup(ti, key, mask); 441e6445719SPravin B Shelar if (flow) /* Found */ 442b637e498SPravin B Shelar return flow; 443b637e498SPravin B Shelar } 444b637e498SPravin B Shelar return NULL; 445e6445719SPravin B Shelar } 446e6445719SPravin B Shelar 447b637e498SPravin B Shelar static struct table_instance *table_instance_expand(struct table_instance *ti) 448b637e498SPravin B Shelar { 449b637e498SPravin B Shelar return table_instance_rehash(ti, ti->n_buckets * 2); 450e6445719SPravin B Shelar } 451e6445719SPravin B Shelar 452e6445719SPravin B Shelar void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow) 453e6445719SPravin B Shelar { 454b637e498SPravin B Shelar struct table_instance *ti = ovsl_dereference(table->ti); 455b637e498SPravin B Shelar 456e6445719SPravin B Shelar BUG_ON(table->count == 0); 457b637e498SPravin B Shelar hlist_del_rcu(&flow->hash_node[ti->node_ver]); 458e6445719SPravin B Shelar table->count--; 459e6445719SPravin B Shelar } 460e6445719SPravin B Shelar 461*618ed0c8SPravin B Shelar static struct sw_flow_mask *mask_alloc(void) 462e6445719SPravin B Shelar { 463e6445719SPravin B Shelar struct sw_flow_mask *mask; 464e6445719SPravin B Shelar 465e6445719SPravin B Shelar mask = kmalloc(sizeof(*mask), GFP_KERNEL); 466e6445719SPravin B Shelar if (mask) 467e6445719SPravin B Shelar mask->ref_count = 0; 468e6445719SPravin B Shelar 469e6445719SPravin B Shelar return mask; 470e6445719SPravin B Shelar } 471e6445719SPravin B Shelar 472*618ed0c8SPravin B Shelar static void mask_add_ref(struct sw_flow_mask *mask) 473e6445719SPravin B Shelar { 474e6445719SPravin B Shelar mask->ref_count++; 475e6445719SPravin B Shelar } 476e6445719SPravin B Shelar 477e6445719SPravin B Shelar static bool mask_equal(const struct sw_flow_mask *a, 478e6445719SPravin B Shelar const struct sw_flow_mask *b) 479e6445719SPravin B Shelar { 480e6445719SPravin B Shelar u8 *a_ = (u8 *)&a->key + a->range.start; 481e6445719SPravin B Shelar u8 *b_ = (u8 *)&b->key + b->range.start; 482e6445719SPravin B Shelar 483e6445719SPravin B Shelar return (a->range.end == b->range.end) 484e6445719SPravin B Shelar && (a->range.start == b->range.start) 485e6445719SPravin B Shelar && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0); 486e6445719SPravin B Shelar } 487e6445719SPravin B Shelar 488*618ed0c8SPravin B Shelar static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl, 489e6445719SPravin B Shelar const struct sw_flow_mask *mask) 490e6445719SPravin B Shelar { 491e6445719SPravin B Shelar struct list_head *ml; 492e6445719SPravin B Shelar 493b637e498SPravin B Shelar list_for_each(ml, &tbl->mask_list) { 494e6445719SPravin B Shelar struct sw_flow_mask *m; 495e6445719SPravin B Shelar m = container_of(ml, struct sw_flow_mask, list); 496e6445719SPravin B Shelar if (mask_equal(mask, m)) 497e6445719SPravin B Shelar return m; 498e6445719SPravin B Shelar } 499e6445719SPravin B Shelar 500e6445719SPravin B Shelar return NULL; 501e6445719SPravin B Shelar } 502e6445719SPravin B Shelar 503e6445719SPravin B Shelar /** 504e6445719SPravin B Shelar * add a new mask into the mask list. 505e6445719SPravin B Shelar * The caller needs to make sure that 'mask' is not the same 506e6445719SPravin B Shelar * as any masks that are already on the list. 507e6445719SPravin B Shelar */ 508*618ed0c8SPravin B Shelar static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow, 509*618ed0c8SPravin B Shelar struct sw_flow_mask *new) 510e6445719SPravin B Shelar { 511*618ed0c8SPravin B Shelar struct sw_flow_mask *mask; 512*618ed0c8SPravin B Shelar mask = flow_mask_find(tbl, new); 513*618ed0c8SPravin B Shelar if (!mask) { 514*618ed0c8SPravin B Shelar /* Allocate a new mask if none exsits. */ 515*618ed0c8SPravin B Shelar mask = mask_alloc(); 516*618ed0c8SPravin B Shelar if (!mask) 517*618ed0c8SPravin B Shelar return -ENOMEM; 518*618ed0c8SPravin B Shelar mask->key = new->key; 519*618ed0c8SPravin B Shelar mask->range = new->range; 520b637e498SPravin B Shelar list_add_rcu(&mask->list, &tbl->mask_list); 521e6445719SPravin B Shelar } 522e6445719SPravin B Shelar 523*618ed0c8SPravin B Shelar mask_add_ref(mask); 524*618ed0c8SPravin B Shelar flow->mask = mask; 525*618ed0c8SPravin B Shelar return 0; 526*618ed0c8SPravin B Shelar } 527*618ed0c8SPravin B Shelar 528*618ed0c8SPravin B Shelar int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow, 529*618ed0c8SPravin B Shelar struct sw_flow_mask *mask) 530*618ed0c8SPravin B Shelar { 531*618ed0c8SPravin B Shelar struct table_instance *new_ti = NULL; 532*618ed0c8SPravin B Shelar struct table_instance *ti; 533*618ed0c8SPravin B Shelar int err; 534*618ed0c8SPravin B Shelar 535*618ed0c8SPravin B Shelar err = flow_mask_insert(table, flow, mask); 536*618ed0c8SPravin B Shelar if (err) 537*618ed0c8SPravin B Shelar return err; 538*618ed0c8SPravin B Shelar 539*618ed0c8SPravin B Shelar flow->hash = flow_hash(&flow->key, flow->mask->range.start, 540*618ed0c8SPravin B Shelar flow->mask->range.end); 541*618ed0c8SPravin B Shelar ti = ovsl_dereference(table->ti); 542*618ed0c8SPravin B Shelar table_instance_insert(ti, flow); 543*618ed0c8SPravin B Shelar table->count++; 544*618ed0c8SPravin B Shelar 545*618ed0c8SPravin B Shelar /* Expand table, if necessary, to make room. */ 546*618ed0c8SPravin B Shelar if (table->count > ti->n_buckets) 547*618ed0c8SPravin B Shelar new_ti = table_instance_expand(ti); 548*618ed0c8SPravin B Shelar else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL)) 549*618ed0c8SPravin B Shelar new_ti = table_instance_rehash(ti, ti->n_buckets); 550*618ed0c8SPravin B Shelar 551*618ed0c8SPravin B Shelar if (new_ti) { 552*618ed0c8SPravin B Shelar rcu_assign_pointer(table->ti, new_ti); 553*618ed0c8SPravin B Shelar table_instance_destroy(ti, true); 554*618ed0c8SPravin B Shelar table->last_rehash = jiffies; 555*618ed0c8SPravin B Shelar } 556*618ed0c8SPravin B Shelar return 0; 557*618ed0c8SPravin B Shelar } 558*618ed0c8SPravin B Shelar 559e6445719SPravin B Shelar /* Initializes the flow module. 560e6445719SPravin B Shelar * Returns zero if successful or a negative error code. */ 561e6445719SPravin B Shelar int ovs_flow_init(void) 562e6445719SPravin B Shelar { 563e6445719SPravin B Shelar BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long)); 564e6445719SPravin B Shelar BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long)); 565e6445719SPravin B Shelar 566e6445719SPravin B Shelar flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0, 567e6445719SPravin B Shelar 0, NULL); 568e6445719SPravin B Shelar if (flow_cache == NULL) 569e6445719SPravin B Shelar return -ENOMEM; 570e6445719SPravin B Shelar 571e6445719SPravin B Shelar return 0; 572e6445719SPravin B Shelar } 573e6445719SPravin B Shelar 574e6445719SPravin B Shelar /* Uninitializes the flow module. */ 575e6445719SPravin B Shelar void ovs_flow_exit(void) 576e6445719SPravin B Shelar { 577e6445719SPravin B Shelar kmem_cache_destroy(flow_cache); 578e6445719SPravin B Shelar } 579