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> 28500f8087SFrancesco Fusco #include <linux/hash.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 #define TBL_MIN_BUCKETS 1024 48b637e498SPravin B Shelar #define REHASH_INTERVAL (10 * 60 * HZ) 49b637e498SPravin B Shelar 50e6445719SPravin B Shelar static struct kmem_cache *flow_cache; 5163e7959cSJarno Rajahalme struct kmem_cache *flow_stats_cache __read_mostly; 52e6445719SPravin B Shelar 53e6445719SPravin B Shelar static u16 range_n_bytes(const struct sw_flow_key_range *range) 54e6445719SPravin B Shelar { 55e6445719SPravin B Shelar return range->end - range->start; 56e6445719SPravin B Shelar } 57e6445719SPravin B Shelar 58e6445719SPravin B Shelar void ovs_flow_mask_key(struct sw_flow_key *dst, const struct sw_flow_key *src, 59e6445719SPravin B Shelar const struct sw_flow_mask *mask) 60e6445719SPravin B Shelar { 617085130bSDaniele Di Proietto const long *m = (const long *)((const u8 *)&mask->key + 627085130bSDaniele Di Proietto mask->range.start); 637085130bSDaniele Di Proietto const long *s = (const long *)((const u8 *)src + 647085130bSDaniele Di Proietto mask->range.start); 65e6445719SPravin B Shelar long *d = (long *)((u8 *)dst + mask->range.start); 66e6445719SPravin B Shelar int i; 67e6445719SPravin B Shelar 68e6445719SPravin B Shelar /* The memory outside of the 'mask->range' are not set since 69e6445719SPravin B Shelar * further operations on 'dst' only uses contents within 70e6445719SPravin B Shelar * 'mask->range'. 71e6445719SPravin B Shelar */ 72e6445719SPravin B Shelar for (i = 0; i < range_n_bytes(&mask->range); i += sizeof(long)) 73e6445719SPravin B Shelar *d++ = *s++ & *m++; 74e6445719SPravin B Shelar } 75e6445719SPravin B Shelar 7623dabf88SJarno Rajahalme struct sw_flow *ovs_flow_alloc(void) 77e6445719SPravin B Shelar { 78e6445719SPravin B Shelar struct sw_flow *flow; 7963e7959cSJarno Rajahalme struct flow_stats *stats; 8063e7959cSJarno Rajahalme int node; 81e6445719SPravin B Shelar 82e6445719SPravin B Shelar flow = kmem_cache_alloc(flow_cache, GFP_KERNEL); 83e6445719SPravin B Shelar if (!flow) 84e6445719SPravin B Shelar return ERR_PTR(-ENOMEM); 85e6445719SPravin B Shelar 86e6445719SPravin B Shelar flow->sf_acts = NULL; 87e6445719SPravin B Shelar flow->mask = NULL; 8863e7959cSJarno Rajahalme flow->stats_last_writer = NUMA_NO_NODE; 89e6445719SPravin B Shelar 9063e7959cSJarno Rajahalme /* Initialize the default stat node. */ 9163e7959cSJarno Rajahalme stats = kmem_cache_alloc_node(flow_stats_cache, 9263e7959cSJarno Rajahalme GFP_KERNEL | __GFP_ZERO, 0); 9363e7959cSJarno Rajahalme if (!stats) 94e298e505SPravin B Shelar goto err; 95e298e505SPravin B Shelar 9663e7959cSJarno Rajahalme spin_lock_init(&stats->lock); 97e298e505SPravin B Shelar 9863e7959cSJarno Rajahalme RCU_INIT_POINTER(flow->stats[0], stats); 9963e7959cSJarno Rajahalme 10063e7959cSJarno Rajahalme for_each_node(node) 10163e7959cSJarno Rajahalme if (node != 0) 10263e7959cSJarno Rajahalme RCU_INIT_POINTER(flow->stats[node], NULL); 10363e7959cSJarno Rajahalme 104e6445719SPravin B Shelar return flow; 105e298e505SPravin B Shelar err: 106ece37c87SWei Yongjun kmem_cache_free(flow_cache, flow); 107e298e505SPravin B Shelar return ERR_PTR(-ENOMEM); 108e6445719SPravin B Shelar } 109e6445719SPravin B Shelar 110b637e498SPravin B Shelar int ovs_flow_tbl_count(struct flow_table *table) 111b637e498SPravin B Shelar { 112b637e498SPravin B Shelar return table->count; 113b637e498SPravin B Shelar } 114b637e498SPravin B Shelar 115e6445719SPravin B Shelar static struct flex_array *alloc_buckets(unsigned int n_buckets) 116e6445719SPravin B Shelar { 117e6445719SPravin B Shelar struct flex_array *buckets; 118e6445719SPravin B Shelar int i, err; 119e6445719SPravin B Shelar 120e6445719SPravin B Shelar buckets = flex_array_alloc(sizeof(struct hlist_head), 121e6445719SPravin B Shelar n_buckets, GFP_KERNEL); 122e6445719SPravin B Shelar if (!buckets) 123e6445719SPravin B Shelar return NULL; 124e6445719SPravin B Shelar 125e6445719SPravin B Shelar err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL); 126e6445719SPravin B Shelar if (err) { 127e6445719SPravin B Shelar flex_array_free(buckets); 128e6445719SPravin B Shelar return NULL; 129e6445719SPravin B Shelar } 130e6445719SPravin B Shelar 131e6445719SPravin B Shelar for (i = 0; i < n_buckets; i++) 132e6445719SPravin B Shelar INIT_HLIST_HEAD((struct hlist_head *) 133e6445719SPravin B Shelar flex_array_get(buckets, i)); 134e6445719SPravin B Shelar 135e6445719SPravin B Shelar return buckets; 136e6445719SPravin B Shelar } 137e6445719SPravin B Shelar 138e6445719SPravin B Shelar static void flow_free(struct sw_flow *flow) 139e6445719SPravin B Shelar { 14063e7959cSJarno Rajahalme int node; 14163e7959cSJarno Rajahalme 142eb072659SJarno Rajahalme kfree((struct sw_flow_actions __force *)flow->sf_acts); 14363e7959cSJarno Rajahalme for_each_node(node) 14463e7959cSJarno Rajahalme if (flow->stats[node]) 14563e7959cSJarno Rajahalme kmem_cache_free(flow_stats_cache, 14663e7959cSJarno Rajahalme (struct flow_stats __force *)flow->stats[node]); 147e6445719SPravin B Shelar kmem_cache_free(flow_cache, flow); 148e6445719SPravin B Shelar } 149e6445719SPravin B Shelar 150e6445719SPravin B Shelar static void rcu_free_flow_callback(struct rcu_head *rcu) 151e6445719SPravin B Shelar { 152e6445719SPravin B Shelar struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu); 153e6445719SPravin B Shelar 154e6445719SPravin B Shelar flow_free(flow); 155e6445719SPravin B Shelar } 156e6445719SPravin B Shelar 157e80857ccSAndy Zhou void ovs_flow_free(struct sw_flow *flow, bool deferred) 158618ed0c8SPravin B Shelar { 159e80857ccSAndy Zhou if (!flow) 160618ed0c8SPravin B Shelar return; 161618ed0c8SPravin 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 173e80857ccSAndy Zhou 174b637e498SPravin B Shelar static void __table_instance_destroy(struct table_instance *ti) 175e6445719SPravin B Shelar { 176b637e498SPravin B Shelar free_buckets(ti->buckets); 177b637e498SPravin B Shelar kfree(ti); 178e6445719SPravin B Shelar } 179e6445719SPravin B Shelar 180b637e498SPravin B Shelar static struct table_instance *table_instance_alloc(int new_size) 181e6445719SPravin B Shelar { 182b637e498SPravin B Shelar struct table_instance *ti = kmalloc(sizeof(*ti), GFP_KERNEL); 183e6445719SPravin B Shelar 184b637e498SPravin B Shelar if (!ti) 185e6445719SPravin B Shelar return NULL; 186e6445719SPravin B Shelar 187b637e498SPravin B Shelar ti->buckets = alloc_buckets(new_size); 188e6445719SPravin B Shelar 189b637e498SPravin B Shelar if (!ti->buckets) { 190b637e498SPravin B Shelar kfree(ti); 191e6445719SPravin B Shelar return NULL; 192e6445719SPravin B Shelar } 193b637e498SPravin B Shelar ti->n_buckets = new_size; 194b637e498SPravin B Shelar ti->node_ver = 0; 195b637e498SPravin B Shelar ti->keep_flows = false; 196b637e498SPravin B Shelar get_random_bytes(&ti->hash_seed, sizeof(u32)); 197b637e498SPravin B Shelar 198b637e498SPravin B Shelar return ti; 199b637e498SPravin B Shelar } 200b637e498SPravin B Shelar 201b637e498SPravin B Shelar int ovs_flow_tbl_init(struct flow_table *table) 202b637e498SPravin B Shelar { 203b637e498SPravin B Shelar struct table_instance *ti; 204b637e498SPravin B Shelar 205b637e498SPravin B Shelar ti = table_instance_alloc(TBL_MIN_BUCKETS); 206b637e498SPravin B Shelar 207b637e498SPravin B Shelar if (!ti) 208b637e498SPravin B Shelar return -ENOMEM; 209b637e498SPravin B Shelar 210b637e498SPravin B Shelar rcu_assign_pointer(table->ti, ti); 211b637e498SPravin B Shelar INIT_LIST_HEAD(&table->mask_list); 212b637e498SPravin B Shelar table->last_rehash = jiffies; 213e6445719SPravin B Shelar table->count = 0; 214b637e498SPravin B Shelar return 0; 215e6445719SPravin B Shelar } 216e6445719SPravin B Shelar 217e6445719SPravin B Shelar static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu) 218e6445719SPravin B Shelar { 219b637e498SPravin B Shelar struct table_instance *ti = container_of(rcu, struct table_instance, rcu); 220e6445719SPravin B Shelar 221b637e498SPravin B Shelar __table_instance_destroy(ti); 222b637e498SPravin B Shelar } 223b637e498SPravin B Shelar 224b637e498SPravin B Shelar static void table_instance_destroy(struct table_instance *ti, bool deferred) 225b637e498SPravin B Shelar { 226e80857ccSAndy Zhou int i; 227e80857ccSAndy Zhou 228b637e498SPravin B Shelar if (!ti) 229b637e498SPravin B Shelar return; 230b637e498SPravin B Shelar 231e80857ccSAndy Zhou if (ti->keep_flows) 232e80857ccSAndy Zhou goto skip_flows; 233e80857ccSAndy Zhou 234e80857ccSAndy Zhou for (i = 0; i < ti->n_buckets; i++) { 235e80857ccSAndy Zhou struct sw_flow *flow; 236e80857ccSAndy Zhou struct hlist_head *head = flex_array_get(ti->buckets, i); 237e80857ccSAndy Zhou struct hlist_node *n; 238e80857ccSAndy Zhou int ver = ti->node_ver; 239e80857ccSAndy Zhou 240e80857ccSAndy Zhou hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) { 241e80857ccSAndy Zhou hlist_del_rcu(&flow->hash_node[ver]); 242e80857ccSAndy Zhou ovs_flow_free(flow, deferred); 243e80857ccSAndy Zhou } 244e80857ccSAndy Zhou } 245e80857ccSAndy Zhou 246e80857ccSAndy Zhou skip_flows: 247b637e498SPravin B Shelar if (deferred) 248b637e498SPravin B Shelar call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb); 249b637e498SPravin B Shelar else 250b637e498SPravin B Shelar __table_instance_destroy(ti); 251e6445719SPravin B Shelar } 252e6445719SPravin B Shelar 253e80857ccSAndy Zhou void ovs_flow_tbl_destroy(struct flow_table *table, bool deferred) 254e6445719SPravin B Shelar { 255b637e498SPravin B Shelar struct table_instance *ti = ovsl_dereference(table->ti); 256e6445719SPravin B Shelar 257e80857ccSAndy Zhou table_instance_destroy(ti, deferred); 258e6445719SPravin B Shelar } 259e6445719SPravin B Shelar 260b637e498SPravin B Shelar struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti, 261e6445719SPravin B Shelar u32 *bucket, u32 *last) 262e6445719SPravin B Shelar { 263e6445719SPravin B Shelar struct sw_flow *flow; 264e6445719SPravin B Shelar struct hlist_head *head; 265e6445719SPravin B Shelar int ver; 266e6445719SPravin B Shelar int i; 267e6445719SPravin B Shelar 268b637e498SPravin B Shelar ver = ti->node_ver; 269b637e498SPravin B Shelar while (*bucket < ti->n_buckets) { 270e6445719SPravin B Shelar i = 0; 271b637e498SPravin B Shelar head = flex_array_get(ti->buckets, *bucket); 272e6445719SPravin B Shelar hlist_for_each_entry_rcu(flow, head, hash_node[ver]) { 273e6445719SPravin B Shelar if (i < *last) { 274e6445719SPravin B Shelar i++; 275e6445719SPravin B Shelar continue; 276e6445719SPravin B Shelar } 277e6445719SPravin B Shelar *last = i + 1; 278e6445719SPravin B Shelar return flow; 279e6445719SPravin B Shelar } 280e6445719SPravin B Shelar (*bucket)++; 281e6445719SPravin B Shelar *last = 0; 282e6445719SPravin B Shelar } 283e6445719SPravin B Shelar 284e6445719SPravin B Shelar return NULL; 285e6445719SPravin B Shelar } 286e6445719SPravin B Shelar 287b637e498SPravin B Shelar static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash) 288e6445719SPravin B Shelar { 289b637e498SPravin B Shelar hash = jhash_1word(hash, ti->hash_seed); 290b637e498SPravin B Shelar return flex_array_get(ti->buckets, 291b637e498SPravin B Shelar (hash & (ti->n_buckets - 1))); 292e6445719SPravin B Shelar } 293e6445719SPravin B Shelar 294b637e498SPravin B Shelar static void table_instance_insert(struct table_instance *ti, struct sw_flow *flow) 295e6445719SPravin B Shelar { 296e6445719SPravin B Shelar struct hlist_head *head; 297e6445719SPravin B Shelar 298b637e498SPravin B Shelar head = find_bucket(ti, flow->hash); 299b637e498SPravin B Shelar hlist_add_head_rcu(&flow->hash_node[ti->node_ver], head); 300e6445719SPravin B Shelar } 301e6445719SPravin B Shelar 302b637e498SPravin B Shelar static void flow_table_copy_flows(struct table_instance *old, 303b637e498SPravin B Shelar struct table_instance *new) 304e6445719SPravin B Shelar { 305e6445719SPravin B Shelar int old_ver; 306e6445719SPravin B Shelar int i; 307e6445719SPravin B Shelar 308e6445719SPravin B Shelar old_ver = old->node_ver; 309e6445719SPravin B Shelar new->node_ver = !old_ver; 310e6445719SPravin B Shelar 311e6445719SPravin B Shelar /* Insert in new table. */ 312e6445719SPravin B Shelar for (i = 0; i < old->n_buckets; i++) { 313e6445719SPravin B Shelar struct sw_flow *flow; 314e6445719SPravin B Shelar struct hlist_head *head; 315e6445719SPravin B Shelar 316e6445719SPravin B Shelar head = flex_array_get(old->buckets, i); 317e6445719SPravin B Shelar 318e6445719SPravin B Shelar hlist_for_each_entry(flow, head, hash_node[old_ver]) 319b637e498SPravin B Shelar table_instance_insert(new, flow); 320e6445719SPravin B Shelar } 321e6445719SPravin B Shelar 322e6445719SPravin B Shelar old->keep_flows = true; 323e6445719SPravin B Shelar } 324e6445719SPravin B Shelar 325b637e498SPravin B Shelar static struct table_instance *table_instance_rehash(struct table_instance *ti, 326e6445719SPravin B Shelar int n_buckets) 327e6445719SPravin B Shelar { 328b637e498SPravin B Shelar struct table_instance *new_ti; 329e6445719SPravin B Shelar 330b637e498SPravin B Shelar new_ti = table_instance_alloc(n_buckets); 331b637e498SPravin B Shelar if (!new_ti) 332618ed0c8SPravin B Shelar return NULL; 333e6445719SPravin B Shelar 334b637e498SPravin B Shelar flow_table_copy_flows(ti, new_ti); 335e6445719SPravin B Shelar 336b637e498SPravin B Shelar return new_ti; 337e6445719SPravin B Shelar } 338e6445719SPravin B Shelar 339b637e498SPravin B Shelar int ovs_flow_tbl_flush(struct flow_table *flow_table) 340e6445719SPravin B Shelar { 341b637e498SPravin B Shelar struct table_instance *old_ti; 342b637e498SPravin B Shelar struct table_instance *new_ti; 343e6445719SPravin B Shelar 344b637e498SPravin B Shelar old_ti = ovsl_dereference(flow_table->ti); 345b637e498SPravin B Shelar new_ti = table_instance_alloc(TBL_MIN_BUCKETS); 346b637e498SPravin B Shelar if (!new_ti) 347b637e498SPravin B Shelar return -ENOMEM; 348b637e498SPravin B Shelar 349b637e498SPravin B Shelar rcu_assign_pointer(flow_table->ti, new_ti); 350b637e498SPravin B Shelar flow_table->last_rehash = jiffies; 351b637e498SPravin B Shelar flow_table->count = 0; 352b637e498SPravin B Shelar 353b637e498SPravin B Shelar table_instance_destroy(old_ti, true); 354b637e498SPravin B Shelar return 0; 355e6445719SPravin B Shelar } 356e6445719SPravin B Shelar 357e6445719SPravin B Shelar static u32 flow_hash(const struct sw_flow_key *key, int key_start, 358e6445719SPravin B Shelar int key_end) 359e6445719SPravin B Shelar { 3607085130bSDaniele Di Proietto const u32 *hash_key = (const u32 *)((const u8 *)key + key_start); 361e6445719SPravin B Shelar int hash_u32s = (key_end - key_start) >> 2; 362e6445719SPravin B Shelar 363e6445719SPravin B Shelar /* Make sure number of hash bytes are multiple of u32. */ 364e6445719SPravin B Shelar BUILD_BUG_ON(sizeof(long) % sizeof(u32)); 365e6445719SPravin B Shelar 366500f8087SFrancesco Fusco return arch_fast_hash2(hash_key, hash_u32s, 0); 367e6445719SPravin B Shelar } 368e6445719SPravin B Shelar 369e6445719SPravin B Shelar static int flow_key_start(const struct sw_flow_key *key) 370e6445719SPravin B Shelar { 371e6445719SPravin B Shelar if (key->tun_key.ipv4_dst) 372e6445719SPravin B Shelar return 0; 373e6445719SPravin B Shelar else 374e6445719SPravin B Shelar return rounddown(offsetof(struct sw_flow_key, phy), 375e6445719SPravin B Shelar sizeof(long)); 376e6445719SPravin B Shelar } 377e6445719SPravin B Shelar 378e6445719SPravin B Shelar static bool cmp_key(const struct sw_flow_key *key1, 379e6445719SPravin B Shelar const struct sw_flow_key *key2, 380e6445719SPravin B Shelar int key_start, int key_end) 381e6445719SPravin B Shelar { 3827085130bSDaniele Di Proietto const long *cp1 = (const long *)((const u8 *)key1 + key_start); 3837085130bSDaniele Di Proietto const long *cp2 = (const long *)((const u8 *)key2 + key_start); 384e6445719SPravin B Shelar long diffs = 0; 385e6445719SPravin B Shelar int i; 386e6445719SPravin B Shelar 387e6445719SPravin B Shelar for (i = key_start; i < key_end; i += sizeof(long)) 388e6445719SPravin B Shelar diffs |= *cp1++ ^ *cp2++; 389e6445719SPravin B Shelar 390e6445719SPravin B Shelar return diffs == 0; 391e6445719SPravin B Shelar } 392e6445719SPravin B Shelar 393e6445719SPravin B Shelar static bool flow_cmp_masked_key(const struct sw_flow *flow, 394e6445719SPravin B Shelar const struct sw_flow_key *key, 395e6445719SPravin B Shelar int key_start, int key_end) 396e6445719SPravin B Shelar { 397e6445719SPravin B Shelar return cmp_key(&flow->key, key, key_start, key_end); 398e6445719SPravin B Shelar } 399e6445719SPravin B Shelar 400e6445719SPravin B Shelar bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow, 401e6445719SPravin B Shelar struct sw_flow_match *match) 402e6445719SPravin B Shelar { 403e6445719SPravin B Shelar struct sw_flow_key *key = match->key; 404e6445719SPravin B Shelar int key_start = flow_key_start(key); 405e6445719SPravin B Shelar int key_end = match->range.end; 406e6445719SPravin B Shelar 407e6445719SPravin B Shelar return cmp_key(&flow->unmasked_key, key, key_start, key_end); 408e6445719SPravin B Shelar } 409e6445719SPravin B Shelar 410b637e498SPravin B Shelar static struct sw_flow *masked_flow_lookup(struct table_instance *ti, 411e6445719SPravin B Shelar const struct sw_flow_key *unmasked, 412e6445719SPravin B Shelar struct sw_flow_mask *mask) 413e6445719SPravin B Shelar { 414e6445719SPravin B Shelar struct sw_flow *flow; 415e6445719SPravin B Shelar struct hlist_head *head; 416e6445719SPravin B Shelar int key_start = mask->range.start; 417e6445719SPravin B Shelar int key_end = mask->range.end; 418e6445719SPravin B Shelar u32 hash; 419e6445719SPravin B Shelar struct sw_flow_key masked_key; 420e6445719SPravin B Shelar 421e6445719SPravin B Shelar ovs_flow_mask_key(&masked_key, unmasked, mask); 422e6445719SPravin B Shelar hash = flow_hash(&masked_key, key_start, key_end); 423b637e498SPravin B Shelar head = find_bucket(ti, hash); 424b637e498SPravin B Shelar hlist_for_each_entry_rcu(flow, head, hash_node[ti->node_ver]) { 4258ddd0946SPravin B Shelar if (flow->mask == mask && flow->hash == hash && 426e6445719SPravin B Shelar flow_cmp_masked_key(flow, &masked_key, 427e6445719SPravin B Shelar key_start, key_end)) 428e6445719SPravin B Shelar return flow; 429e6445719SPravin B Shelar } 430e6445719SPravin B Shelar return NULL; 431e6445719SPravin B Shelar } 432e6445719SPravin B Shelar 4335bb50632SAndy Zhou struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl, 4341bd7116fSAndy Zhou const struct sw_flow_key *key, 4351bd7116fSAndy Zhou u32 *n_mask_hit) 436e6445719SPravin B Shelar { 437663efa36SJesse Gross struct table_instance *ti = rcu_dereference_ovsl(tbl->ti); 438e6445719SPravin B Shelar struct sw_flow_mask *mask; 439b637e498SPravin B Shelar struct sw_flow *flow; 440e6445719SPravin B Shelar 4411bd7116fSAndy Zhou *n_mask_hit = 0; 442b637e498SPravin B Shelar list_for_each_entry_rcu(mask, &tbl->mask_list, list) { 4431bd7116fSAndy Zhou (*n_mask_hit)++; 444b637e498SPravin B Shelar flow = masked_flow_lookup(ti, key, mask); 445e6445719SPravin B Shelar if (flow) /* Found */ 446b637e498SPravin B Shelar return flow; 447b637e498SPravin B Shelar } 448b637e498SPravin B Shelar return NULL; 449e6445719SPravin B Shelar } 450e6445719SPravin B Shelar 4515bb50632SAndy Zhou struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl, 4525bb50632SAndy Zhou const struct sw_flow_key *key) 4535bb50632SAndy Zhou { 4545bb50632SAndy Zhou u32 __always_unused n_mask_hit; 4555bb50632SAndy Zhou 4565bb50632SAndy Zhou return ovs_flow_tbl_lookup_stats(tbl, key, &n_mask_hit); 4575bb50632SAndy Zhou } 4585bb50632SAndy Zhou 459*4a46b24eSAlex Wang struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl, 460*4a46b24eSAlex Wang struct sw_flow_match *match) 461*4a46b24eSAlex Wang { 462*4a46b24eSAlex Wang struct table_instance *ti = rcu_dereference_ovsl(tbl->ti); 463*4a46b24eSAlex Wang struct sw_flow_mask *mask; 464*4a46b24eSAlex Wang struct sw_flow *flow; 465*4a46b24eSAlex Wang 466*4a46b24eSAlex Wang /* Always called under ovs-mutex. */ 467*4a46b24eSAlex Wang list_for_each_entry(mask, &tbl->mask_list, list) { 468*4a46b24eSAlex Wang flow = masked_flow_lookup(ti, match->key, mask); 469*4a46b24eSAlex Wang if (flow && ovs_flow_cmp_unmasked_key(flow, match)) /* Found */ 470*4a46b24eSAlex Wang return flow; 471*4a46b24eSAlex Wang } 472*4a46b24eSAlex Wang return NULL; 473*4a46b24eSAlex Wang } 474*4a46b24eSAlex Wang 4751bd7116fSAndy Zhou int ovs_flow_tbl_num_masks(const struct flow_table *table) 4761bd7116fSAndy Zhou { 4771bd7116fSAndy Zhou struct sw_flow_mask *mask; 4781bd7116fSAndy Zhou int num = 0; 4791bd7116fSAndy Zhou 4801bd7116fSAndy Zhou list_for_each_entry(mask, &table->mask_list, list) 4811bd7116fSAndy Zhou num++; 4821bd7116fSAndy Zhou 4831bd7116fSAndy Zhou return num; 4841bd7116fSAndy Zhou } 4851bd7116fSAndy Zhou 486b637e498SPravin B Shelar static struct table_instance *table_instance_expand(struct table_instance *ti) 487b637e498SPravin B Shelar { 488b637e498SPravin B Shelar return table_instance_rehash(ti, ti->n_buckets * 2); 489e6445719SPravin B Shelar } 490e6445719SPravin B Shelar 49156c19868SJarno Rajahalme /* Remove 'mask' from the mask list, if it is not needed any more. */ 49256c19868SJarno Rajahalme static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask) 49356c19868SJarno Rajahalme { 49456c19868SJarno Rajahalme if (mask) { 49556c19868SJarno Rajahalme /* ovs-lock is required to protect mask-refcount and 49656c19868SJarno Rajahalme * mask list. 49756c19868SJarno Rajahalme */ 49856c19868SJarno Rajahalme ASSERT_OVSL(); 49956c19868SJarno Rajahalme BUG_ON(!mask->ref_count); 50056c19868SJarno Rajahalme mask->ref_count--; 50156c19868SJarno Rajahalme 50256c19868SJarno Rajahalme if (!mask->ref_count) { 50356c19868SJarno Rajahalme list_del_rcu(&mask->list); 50456c19868SJarno Rajahalme kfree_rcu(mask, rcu); 50556c19868SJarno Rajahalme } 50656c19868SJarno Rajahalme } 50756c19868SJarno Rajahalme } 50856c19868SJarno Rajahalme 50956c19868SJarno Rajahalme /* Must be called with OVS mutex held. */ 510e6445719SPravin B Shelar void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow) 511e6445719SPravin B Shelar { 512b637e498SPravin B Shelar struct table_instance *ti = ovsl_dereference(table->ti); 513b637e498SPravin B Shelar 514e6445719SPravin B Shelar BUG_ON(table->count == 0); 515b637e498SPravin B Shelar hlist_del_rcu(&flow->hash_node[ti->node_ver]); 516e6445719SPravin B Shelar table->count--; 51756c19868SJarno Rajahalme 51856c19868SJarno Rajahalme /* RCU delete the mask. 'flow->mask' is not NULLed, as it should be 51956c19868SJarno Rajahalme * accessible as long as the RCU read lock is held. 52056c19868SJarno Rajahalme */ 52156c19868SJarno Rajahalme flow_mask_remove(table, flow->mask); 522e6445719SPravin B Shelar } 523e6445719SPravin B Shelar 524618ed0c8SPravin B Shelar static struct sw_flow_mask *mask_alloc(void) 525e6445719SPravin B Shelar { 526e6445719SPravin B Shelar struct sw_flow_mask *mask; 527e6445719SPravin B Shelar 528e6445719SPravin B Shelar mask = kmalloc(sizeof(*mask), GFP_KERNEL); 529e6445719SPravin B Shelar if (mask) 530e80857ccSAndy Zhou mask->ref_count = 1; 531e6445719SPravin B Shelar 532e6445719SPravin B Shelar return mask; 533e6445719SPravin B Shelar } 534e6445719SPravin B Shelar 535e6445719SPravin B Shelar static bool mask_equal(const struct sw_flow_mask *a, 536e6445719SPravin B Shelar const struct sw_flow_mask *b) 537e6445719SPravin B Shelar { 5387085130bSDaniele Di Proietto const u8 *a_ = (const u8 *)&a->key + a->range.start; 5397085130bSDaniele Di Proietto const u8 *b_ = (const u8 *)&b->key + b->range.start; 540e6445719SPravin B Shelar 541e6445719SPravin B Shelar return (a->range.end == b->range.end) 542e6445719SPravin B Shelar && (a->range.start == b->range.start) 543e6445719SPravin B Shelar && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0); 544e6445719SPravin B Shelar } 545e6445719SPravin B Shelar 546618ed0c8SPravin B Shelar static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl, 547e6445719SPravin B Shelar const struct sw_flow_mask *mask) 548e6445719SPravin B Shelar { 549e6445719SPravin B Shelar struct list_head *ml; 550e6445719SPravin B Shelar 551b637e498SPravin B Shelar list_for_each(ml, &tbl->mask_list) { 552e6445719SPravin B Shelar struct sw_flow_mask *m; 553e6445719SPravin B Shelar m = container_of(ml, struct sw_flow_mask, list); 554e6445719SPravin B Shelar if (mask_equal(mask, m)) 555e6445719SPravin B Shelar return m; 556e6445719SPravin B Shelar } 557e6445719SPravin B Shelar 558e6445719SPravin B Shelar return NULL; 559e6445719SPravin B Shelar } 560e6445719SPravin B Shelar 561d1211908SBen Pfaff /* Add 'mask' into the mask list, if it is not already there. */ 562618ed0c8SPravin B Shelar static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow, 563618ed0c8SPravin B Shelar struct sw_flow_mask *new) 564e6445719SPravin B Shelar { 565618ed0c8SPravin B Shelar struct sw_flow_mask *mask; 566618ed0c8SPravin B Shelar mask = flow_mask_find(tbl, new); 567618ed0c8SPravin B Shelar if (!mask) { 568618ed0c8SPravin B Shelar /* Allocate a new mask if none exsits. */ 569618ed0c8SPravin B Shelar mask = mask_alloc(); 570618ed0c8SPravin B Shelar if (!mask) 571618ed0c8SPravin B Shelar return -ENOMEM; 572618ed0c8SPravin B Shelar mask->key = new->key; 573618ed0c8SPravin B Shelar mask->range = new->range; 574b637e498SPravin B Shelar list_add_rcu(&mask->list, &tbl->mask_list); 575e80857ccSAndy Zhou } else { 576e80857ccSAndy Zhou BUG_ON(!mask->ref_count); 577e80857ccSAndy Zhou mask->ref_count++; 578e6445719SPravin B Shelar } 579e6445719SPravin B Shelar 580618ed0c8SPravin B Shelar flow->mask = mask; 581618ed0c8SPravin B Shelar return 0; 582618ed0c8SPravin B Shelar } 583618ed0c8SPravin B Shelar 58456c19868SJarno Rajahalme /* Must be called with OVS mutex held. */ 585618ed0c8SPravin B Shelar int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow, 586618ed0c8SPravin B Shelar struct sw_flow_mask *mask) 587618ed0c8SPravin B Shelar { 588618ed0c8SPravin B Shelar struct table_instance *new_ti = NULL; 589618ed0c8SPravin B Shelar struct table_instance *ti; 590618ed0c8SPravin B Shelar int err; 591618ed0c8SPravin B Shelar 592618ed0c8SPravin B Shelar err = flow_mask_insert(table, flow, mask); 593618ed0c8SPravin B Shelar if (err) 594618ed0c8SPravin B Shelar return err; 595618ed0c8SPravin B Shelar 596618ed0c8SPravin B Shelar flow->hash = flow_hash(&flow->key, flow->mask->range.start, 597618ed0c8SPravin B Shelar flow->mask->range.end); 598618ed0c8SPravin B Shelar ti = ovsl_dereference(table->ti); 599618ed0c8SPravin B Shelar table_instance_insert(ti, flow); 600618ed0c8SPravin B Shelar table->count++; 601618ed0c8SPravin B Shelar 602618ed0c8SPravin B Shelar /* Expand table, if necessary, to make room. */ 603618ed0c8SPravin B Shelar if (table->count > ti->n_buckets) 604618ed0c8SPravin B Shelar new_ti = table_instance_expand(ti); 605618ed0c8SPravin B Shelar else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL)) 606618ed0c8SPravin B Shelar new_ti = table_instance_rehash(ti, ti->n_buckets); 607618ed0c8SPravin B Shelar 608618ed0c8SPravin B Shelar if (new_ti) { 609618ed0c8SPravin B Shelar rcu_assign_pointer(table->ti, new_ti); 610618ed0c8SPravin B Shelar table_instance_destroy(ti, true); 611618ed0c8SPravin B Shelar table->last_rehash = jiffies; 612618ed0c8SPravin B Shelar } 613618ed0c8SPravin B Shelar return 0; 614618ed0c8SPravin B Shelar } 615618ed0c8SPravin B Shelar 616e6445719SPravin B Shelar /* Initializes the flow module. 617e6445719SPravin B Shelar * Returns zero if successful or a negative error code. */ 618e6445719SPravin B Shelar int ovs_flow_init(void) 619e6445719SPravin B Shelar { 620e6445719SPravin B Shelar BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long)); 621e6445719SPravin B Shelar BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long)); 622e6445719SPravin B Shelar 62363e7959cSJarno Rajahalme flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow) 62463e7959cSJarno Rajahalme + (num_possible_nodes() 62563e7959cSJarno Rajahalme * sizeof(struct flow_stats *)), 62663e7959cSJarno Rajahalme 0, 0, NULL); 627e6445719SPravin B Shelar if (flow_cache == NULL) 628e6445719SPravin B Shelar return -ENOMEM; 629e6445719SPravin B Shelar 63063e7959cSJarno Rajahalme flow_stats_cache 63163e7959cSJarno Rajahalme = kmem_cache_create("sw_flow_stats", sizeof(struct flow_stats), 63263e7959cSJarno Rajahalme 0, SLAB_HWCACHE_ALIGN, NULL); 63363e7959cSJarno Rajahalme if (flow_stats_cache == NULL) { 63463e7959cSJarno Rajahalme kmem_cache_destroy(flow_cache); 63563e7959cSJarno Rajahalme flow_cache = NULL; 63663e7959cSJarno Rajahalme return -ENOMEM; 63763e7959cSJarno Rajahalme } 63863e7959cSJarno Rajahalme 639e6445719SPravin B Shelar return 0; 640e6445719SPravin B Shelar } 641e6445719SPravin B Shelar 642e6445719SPravin B Shelar /* Uninitializes the flow module. */ 643e6445719SPravin B Shelar void ovs_flow_exit(void) 644e6445719SPravin B Shelar { 64563e7959cSJarno Rajahalme kmem_cache_destroy(flow_stats_cache); 646e6445719SPravin B Shelar kmem_cache_destroy(flow_cache); 647e6445719SPravin B Shelar } 648