1e6445719SPravin B Shelar /* 29b996e54SPravin B Shelar * Copyright (c) 2007-2014 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> 2887545899SDaniel Borkmann #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 #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 11012eb18f7SThomas Graf int ovs_flow_tbl_count(const 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 2539b996e54SPravin B Shelar /* No need for locking this function is called from RCU callback or 2549b996e54SPravin B Shelar * error path. 2559b996e54SPravin B Shelar */ 2569b996e54SPravin B Shelar void ovs_flow_tbl_destroy(struct flow_table *table) 257e6445719SPravin B Shelar { 2589b996e54SPravin B Shelar struct table_instance *ti = rcu_dereference_raw(table->ti); 259e6445719SPravin B Shelar 2609b996e54SPravin B Shelar table_instance_destroy(ti, false); 261e6445719SPravin B Shelar } 262e6445719SPravin B Shelar 263b637e498SPravin B Shelar struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti, 264e6445719SPravin B Shelar u32 *bucket, u32 *last) 265e6445719SPravin B Shelar { 266e6445719SPravin B Shelar struct sw_flow *flow; 267e6445719SPravin B Shelar struct hlist_head *head; 268e6445719SPravin B Shelar int ver; 269e6445719SPravin B Shelar int i; 270e6445719SPravin B Shelar 271b637e498SPravin B Shelar ver = ti->node_ver; 272b637e498SPravin B Shelar while (*bucket < ti->n_buckets) { 273e6445719SPravin B Shelar i = 0; 274b637e498SPravin B Shelar head = flex_array_get(ti->buckets, *bucket); 275e6445719SPravin B Shelar hlist_for_each_entry_rcu(flow, head, hash_node[ver]) { 276e6445719SPravin B Shelar if (i < *last) { 277e6445719SPravin B Shelar i++; 278e6445719SPravin B Shelar continue; 279e6445719SPravin B Shelar } 280e6445719SPravin B Shelar *last = i + 1; 281e6445719SPravin B Shelar return flow; 282e6445719SPravin B Shelar } 283e6445719SPravin B Shelar (*bucket)++; 284e6445719SPravin B Shelar *last = 0; 285e6445719SPravin B Shelar } 286e6445719SPravin B Shelar 287e6445719SPravin B Shelar return NULL; 288e6445719SPravin B Shelar } 289e6445719SPravin B Shelar 290b637e498SPravin B Shelar static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash) 291e6445719SPravin B Shelar { 292b637e498SPravin B Shelar hash = jhash_1word(hash, ti->hash_seed); 293b637e498SPravin B Shelar return flex_array_get(ti->buckets, 294b637e498SPravin B Shelar (hash & (ti->n_buckets - 1))); 295e6445719SPravin B Shelar } 296e6445719SPravin B Shelar 297b637e498SPravin B Shelar static void table_instance_insert(struct table_instance *ti, struct sw_flow *flow) 298e6445719SPravin B Shelar { 299e6445719SPravin B Shelar struct hlist_head *head; 300e6445719SPravin B Shelar 301b637e498SPravin B Shelar head = find_bucket(ti, flow->hash); 302b637e498SPravin B Shelar hlist_add_head_rcu(&flow->hash_node[ti->node_ver], head); 303e6445719SPravin B Shelar } 304e6445719SPravin B Shelar 305b637e498SPravin B Shelar static void flow_table_copy_flows(struct table_instance *old, 306b637e498SPravin B Shelar struct table_instance *new) 307e6445719SPravin B Shelar { 308e6445719SPravin B Shelar int old_ver; 309e6445719SPravin B Shelar int i; 310e6445719SPravin B Shelar 311e6445719SPravin B Shelar old_ver = old->node_ver; 312e6445719SPravin B Shelar new->node_ver = !old_ver; 313e6445719SPravin B Shelar 314e6445719SPravin B Shelar /* Insert in new table. */ 315e6445719SPravin B Shelar for (i = 0; i < old->n_buckets; i++) { 316e6445719SPravin B Shelar struct sw_flow *flow; 317e6445719SPravin B Shelar struct hlist_head *head; 318e6445719SPravin B Shelar 319e6445719SPravin B Shelar head = flex_array_get(old->buckets, i); 320e6445719SPravin B Shelar 321e6445719SPravin B Shelar hlist_for_each_entry(flow, head, hash_node[old_ver]) 322b637e498SPravin B Shelar table_instance_insert(new, flow); 323e6445719SPravin B Shelar } 324e6445719SPravin B Shelar 325e6445719SPravin B Shelar old->keep_flows = true; 326e6445719SPravin B Shelar } 327e6445719SPravin B Shelar 328b637e498SPravin B Shelar static struct table_instance *table_instance_rehash(struct table_instance *ti, 329e6445719SPravin B Shelar int n_buckets) 330e6445719SPravin B Shelar { 331b637e498SPravin B Shelar struct table_instance *new_ti; 332e6445719SPravin B Shelar 333b637e498SPravin B Shelar new_ti = table_instance_alloc(n_buckets); 334b637e498SPravin B Shelar if (!new_ti) 335618ed0c8SPravin B Shelar return NULL; 336e6445719SPravin B Shelar 337b637e498SPravin B Shelar flow_table_copy_flows(ti, new_ti); 338e6445719SPravin B Shelar 339b637e498SPravin B Shelar return new_ti; 340e6445719SPravin B Shelar } 341e6445719SPravin B Shelar 342b637e498SPravin B Shelar int ovs_flow_tbl_flush(struct flow_table *flow_table) 343e6445719SPravin B Shelar { 344b637e498SPravin B Shelar struct table_instance *old_ti; 345b637e498SPravin B Shelar struct table_instance *new_ti; 346e6445719SPravin B Shelar 347b637e498SPravin B Shelar old_ti = ovsl_dereference(flow_table->ti); 348b637e498SPravin B Shelar new_ti = table_instance_alloc(TBL_MIN_BUCKETS); 349b637e498SPravin B Shelar if (!new_ti) 350b637e498SPravin B Shelar return -ENOMEM; 351b637e498SPravin B Shelar 352b637e498SPravin B Shelar rcu_assign_pointer(flow_table->ti, new_ti); 353b637e498SPravin B Shelar flow_table->last_rehash = jiffies; 354b637e498SPravin B Shelar flow_table->count = 0; 355b637e498SPravin B Shelar 356b637e498SPravin B Shelar table_instance_destroy(old_ti, true); 357b637e498SPravin B Shelar return 0; 358e6445719SPravin B Shelar } 359e6445719SPravin B Shelar 360e6445719SPravin B Shelar static u32 flow_hash(const struct sw_flow_key *key, int key_start, 361e6445719SPravin B Shelar int key_end) 362e6445719SPravin B Shelar { 3637085130bSDaniele Di Proietto const u32 *hash_key = (const u32 *)((const u8 *)key + key_start); 364e6445719SPravin B Shelar int hash_u32s = (key_end - key_start) >> 2; 365e6445719SPravin B Shelar 366e6445719SPravin B Shelar /* Make sure number of hash bytes are multiple of u32. */ 367e6445719SPravin B Shelar BUILD_BUG_ON(sizeof(long) % sizeof(u32)); 368e6445719SPravin B Shelar 36987545899SDaniel Borkmann return jhash2(hash_key, hash_u32s, 0); 370e6445719SPravin B Shelar } 371e6445719SPravin B Shelar 372e6445719SPravin B Shelar static int flow_key_start(const struct sw_flow_key *key) 373e6445719SPravin B Shelar { 374e6445719SPravin B Shelar if (key->tun_key.ipv4_dst) 375e6445719SPravin B Shelar return 0; 376e6445719SPravin B Shelar else 377e6445719SPravin B Shelar return rounddown(offsetof(struct sw_flow_key, phy), 378e6445719SPravin B Shelar sizeof(long)); 379e6445719SPravin B Shelar } 380e6445719SPravin B Shelar 381e6445719SPravin B Shelar static bool cmp_key(const struct sw_flow_key *key1, 382e6445719SPravin B Shelar const struct sw_flow_key *key2, 383e6445719SPravin B Shelar int key_start, int key_end) 384e6445719SPravin B Shelar { 3857085130bSDaniele Di Proietto const long *cp1 = (const long *)((const u8 *)key1 + key_start); 3867085130bSDaniele Di Proietto const long *cp2 = (const long *)((const u8 *)key2 + key_start); 387e6445719SPravin B Shelar long diffs = 0; 388e6445719SPravin B Shelar int i; 389e6445719SPravin B Shelar 390e6445719SPravin B Shelar for (i = key_start; i < key_end; i += sizeof(long)) 391e6445719SPravin B Shelar diffs |= *cp1++ ^ *cp2++; 392e6445719SPravin B Shelar 393e6445719SPravin B Shelar return diffs == 0; 394e6445719SPravin B Shelar } 395e6445719SPravin B Shelar 396e6445719SPravin B Shelar static bool flow_cmp_masked_key(const struct sw_flow *flow, 397e6445719SPravin B Shelar const struct sw_flow_key *key, 398e6445719SPravin B Shelar int key_start, int key_end) 399e6445719SPravin B Shelar { 400e6445719SPravin B Shelar return cmp_key(&flow->key, key, key_start, key_end); 401e6445719SPravin B Shelar } 402e6445719SPravin B Shelar 403e6445719SPravin B Shelar bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow, 40412eb18f7SThomas Graf const struct sw_flow_match *match) 405e6445719SPravin B Shelar { 406e6445719SPravin B Shelar struct sw_flow_key *key = match->key; 407e6445719SPravin B Shelar int key_start = flow_key_start(key); 408e6445719SPravin B Shelar int key_end = match->range.end; 409e6445719SPravin B Shelar 410e6445719SPravin B Shelar return cmp_key(&flow->unmasked_key, key, key_start, key_end); 411e6445719SPravin B Shelar } 412e6445719SPravin B Shelar 413b637e498SPravin B Shelar static struct sw_flow *masked_flow_lookup(struct table_instance *ti, 414e6445719SPravin B Shelar const struct sw_flow_key *unmasked, 41512eb18f7SThomas Graf const struct sw_flow_mask *mask) 416e6445719SPravin B Shelar { 417e6445719SPravin B Shelar struct sw_flow *flow; 418e6445719SPravin B Shelar struct hlist_head *head; 419e6445719SPravin B Shelar int key_start = mask->range.start; 420e6445719SPravin B Shelar int key_end = mask->range.end; 421e6445719SPravin B Shelar u32 hash; 422e6445719SPravin B Shelar struct sw_flow_key masked_key; 423e6445719SPravin B Shelar 424e6445719SPravin B Shelar ovs_flow_mask_key(&masked_key, unmasked, mask); 425e6445719SPravin B Shelar hash = flow_hash(&masked_key, key_start, key_end); 426b637e498SPravin B Shelar head = find_bucket(ti, hash); 427b637e498SPravin B Shelar hlist_for_each_entry_rcu(flow, head, hash_node[ti->node_ver]) { 4288ddd0946SPravin B Shelar if (flow->mask == mask && flow->hash == hash && 429e6445719SPravin B Shelar flow_cmp_masked_key(flow, &masked_key, 430e6445719SPravin B Shelar key_start, key_end)) 431e6445719SPravin B Shelar return flow; 432e6445719SPravin B Shelar } 433e6445719SPravin B Shelar return NULL; 434e6445719SPravin B Shelar } 435e6445719SPravin B Shelar 4365bb50632SAndy Zhou struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl, 4371bd7116fSAndy Zhou const struct sw_flow_key *key, 4381bd7116fSAndy Zhou u32 *n_mask_hit) 439e6445719SPravin B Shelar { 440663efa36SJesse Gross struct table_instance *ti = rcu_dereference_ovsl(tbl->ti); 441e6445719SPravin B Shelar struct sw_flow_mask *mask; 442b637e498SPravin B Shelar struct sw_flow *flow; 443e6445719SPravin B Shelar 4441bd7116fSAndy Zhou *n_mask_hit = 0; 445b637e498SPravin B Shelar list_for_each_entry_rcu(mask, &tbl->mask_list, list) { 4461bd7116fSAndy Zhou (*n_mask_hit)++; 447b637e498SPravin B Shelar flow = masked_flow_lookup(ti, key, mask); 448e6445719SPravin B Shelar if (flow) /* Found */ 449b637e498SPravin B Shelar return flow; 450b637e498SPravin B Shelar } 451b637e498SPravin B Shelar return NULL; 452e6445719SPravin B Shelar } 453e6445719SPravin B Shelar 4545bb50632SAndy Zhou struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl, 4555bb50632SAndy Zhou const struct sw_flow_key *key) 4565bb50632SAndy Zhou { 4575bb50632SAndy Zhou u32 __always_unused n_mask_hit; 4585bb50632SAndy Zhou 4595bb50632SAndy Zhou return ovs_flow_tbl_lookup_stats(tbl, key, &n_mask_hit); 4605bb50632SAndy Zhou } 4615bb50632SAndy Zhou 4624a46b24eSAlex Wang struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl, 46312eb18f7SThomas Graf const struct sw_flow_match *match) 4644a46b24eSAlex Wang { 4654a46b24eSAlex Wang struct table_instance *ti = rcu_dereference_ovsl(tbl->ti); 4664a46b24eSAlex Wang struct sw_flow_mask *mask; 4674a46b24eSAlex Wang struct sw_flow *flow; 4684a46b24eSAlex Wang 4694a46b24eSAlex Wang /* Always called under ovs-mutex. */ 4704a46b24eSAlex Wang list_for_each_entry(mask, &tbl->mask_list, list) { 4714a46b24eSAlex Wang flow = masked_flow_lookup(ti, match->key, mask); 4724a46b24eSAlex Wang if (flow && ovs_flow_cmp_unmasked_key(flow, match)) /* Found */ 4734a46b24eSAlex Wang return flow; 4744a46b24eSAlex Wang } 4754a46b24eSAlex Wang return NULL; 4764a46b24eSAlex Wang } 4774a46b24eSAlex Wang 4781bd7116fSAndy Zhou int ovs_flow_tbl_num_masks(const struct flow_table *table) 4791bd7116fSAndy Zhou { 4801bd7116fSAndy Zhou struct sw_flow_mask *mask; 4811bd7116fSAndy Zhou int num = 0; 4821bd7116fSAndy Zhou 4831bd7116fSAndy Zhou list_for_each_entry(mask, &table->mask_list, list) 4841bd7116fSAndy Zhou num++; 4851bd7116fSAndy Zhou 4861bd7116fSAndy Zhou return num; 4871bd7116fSAndy Zhou } 4881bd7116fSAndy Zhou 489b637e498SPravin B Shelar static struct table_instance *table_instance_expand(struct table_instance *ti) 490b637e498SPravin B Shelar { 491b637e498SPravin B Shelar return table_instance_rehash(ti, ti->n_buckets * 2); 492e6445719SPravin B Shelar } 493e6445719SPravin B Shelar 49456c19868SJarno Rajahalme /* Remove 'mask' from the mask list, if it is not needed any more. */ 49556c19868SJarno Rajahalme static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask) 49656c19868SJarno Rajahalme { 49756c19868SJarno Rajahalme if (mask) { 49856c19868SJarno Rajahalme /* ovs-lock is required to protect mask-refcount and 49956c19868SJarno Rajahalme * mask list. 50056c19868SJarno Rajahalme */ 50156c19868SJarno Rajahalme ASSERT_OVSL(); 50256c19868SJarno Rajahalme BUG_ON(!mask->ref_count); 50356c19868SJarno Rajahalme mask->ref_count--; 50456c19868SJarno Rajahalme 50556c19868SJarno Rajahalme if (!mask->ref_count) { 50656c19868SJarno Rajahalme list_del_rcu(&mask->list); 50756c19868SJarno Rajahalme kfree_rcu(mask, rcu); 50856c19868SJarno Rajahalme } 50956c19868SJarno Rajahalme } 51056c19868SJarno Rajahalme } 51156c19868SJarno Rajahalme 51256c19868SJarno Rajahalme /* Must be called with OVS mutex held. */ 513e6445719SPravin B Shelar void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow) 514e6445719SPravin B Shelar { 515b637e498SPravin B Shelar struct table_instance *ti = ovsl_dereference(table->ti); 516b637e498SPravin B Shelar 517e6445719SPravin B Shelar BUG_ON(table->count == 0); 518b637e498SPravin B Shelar hlist_del_rcu(&flow->hash_node[ti->node_ver]); 519e6445719SPravin B Shelar table->count--; 52056c19868SJarno Rajahalme 52156c19868SJarno Rajahalme /* RCU delete the mask. 'flow->mask' is not NULLed, as it should be 52256c19868SJarno Rajahalme * accessible as long as the RCU read lock is held. 52356c19868SJarno Rajahalme */ 52456c19868SJarno Rajahalme flow_mask_remove(table, flow->mask); 525e6445719SPravin B Shelar } 526e6445719SPravin B Shelar 527618ed0c8SPravin B Shelar static struct sw_flow_mask *mask_alloc(void) 528e6445719SPravin B Shelar { 529e6445719SPravin B Shelar struct sw_flow_mask *mask; 530e6445719SPravin B Shelar 531e6445719SPravin B Shelar mask = kmalloc(sizeof(*mask), GFP_KERNEL); 532e6445719SPravin B Shelar if (mask) 533e80857ccSAndy Zhou mask->ref_count = 1; 534e6445719SPravin B Shelar 535e6445719SPravin B Shelar return mask; 536e6445719SPravin B Shelar } 537e6445719SPravin B Shelar 538e6445719SPravin B Shelar static bool mask_equal(const struct sw_flow_mask *a, 539e6445719SPravin B Shelar const struct sw_flow_mask *b) 540e6445719SPravin B Shelar { 5417085130bSDaniele Di Proietto const u8 *a_ = (const u8 *)&a->key + a->range.start; 5427085130bSDaniele Di Proietto const u8 *b_ = (const u8 *)&b->key + b->range.start; 543e6445719SPravin B Shelar 544e6445719SPravin B Shelar return (a->range.end == b->range.end) 545e6445719SPravin B Shelar && (a->range.start == b->range.start) 546e6445719SPravin B Shelar && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0); 547e6445719SPravin B Shelar } 548e6445719SPravin B Shelar 549618ed0c8SPravin B Shelar static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl, 550e6445719SPravin B Shelar const struct sw_flow_mask *mask) 551e6445719SPravin B Shelar { 552e6445719SPravin B Shelar struct list_head *ml; 553e6445719SPravin B Shelar 554b637e498SPravin B Shelar list_for_each(ml, &tbl->mask_list) { 555e6445719SPravin B Shelar struct sw_flow_mask *m; 556e6445719SPravin B Shelar m = container_of(ml, struct sw_flow_mask, list); 557e6445719SPravin B Shelar if (mask_equal(mask, m)) 558e6445719SPravin B Shelar return m; 559e6445719SPravin B Shelar } 560e6445719SPravin B Shelar 561e6445719SPravin B Shelar return NULL; 562e6445719SPravin B Shelar } 563e6445719SPravin B Shelar 564d1211908SBen Pfaff /* Add 'mask' into the mask list, if it is not already there. */ 565618ed0c8SPravin B Shelar static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow, 56612eb18f7SThomas Graf const struct sw_flow_mask *new) 567e6445719SPravin B Shelar { 568618ed0c8SPravin B Shelar struct sw_flow_mask *mask; 569618ed0c8SPravin B Shelar mask = flow_mask_find(tbl, new); 570618ed0c8SPravin B Shelar if (!mask) { 571618ed0c8SPravin B Shelar /* Allocate a new mask if none exsits. */ 572618ed0c8SPravin B Shelar mask = mask_alloc(); 573618ed0c8SPravin B Shelar if (!mask) 574618ed0c8SPravin B Shelar return -ENOMEM; 575618ed0c8SPravin B Shelar mask->key = new->key; 576618ed0c8SPravin B Shelar mask->range = new->range; 577b637e498SPravin B Shelar list_add_rcu(&mask->list, &tbl->mask_list); 578e80857ccSAndy Zhou } else { 579e80857ccSAndy Zhou BUG_ON(!mask->ref_count); 580e80857ccSAndy Zhou mask->ref_count++; 581e6445719SPravin B Shelar } 582e6445719SPravin B Shelar 583618ed0c8SPravin B Shelar flow->mask = mask; 584618ed0c8SPravin B Shelar return 0; 585618ed0c8SPravin B Shelar } 586618ed0c8SPravin B Shelar 58756c19868SJarno Rajahalme /* Must be called with OVS mutex held. */ 588*d29ab6f8SJoe Stringer static void flow_key_insert(struct flow_table *table, struct sw_flow *flow) 589618ed0c8SPravin B Shelar { 590618ed0c8SPravin B Shelar struct table_instance *new_ti = NULL; 591618ed0c8SPravin B Shelar struct table_instance *ti; 592618ed0c8SPravin B Shelar 593618ed0c8SPravin B Shelar flow->hash = flow_hash(&flow->key, flow->mask->range.start, 594618ed0c8SPravin B Shelar flow->mask->range.end); 595618ed0c8SPravin B Shelar ti = ovsl_dereference(table->ti); 596618ed0c8SPravin B Shelar table_instance_insert(ti, flow); 597618ed0c8SPravin B Shelar table->count++; 598618ed0c8SPravin B Shelar 599618ed0c8SPravin B Shelar /* Expand table, if necessary, to make room. */ 600618ed0c8SPravin B Shelar if (table->count > ti->n_buckets) 601618ed0c8SPravin B Shelar new_ti = table_instance_expand(ti); 602618ed0c8SPravin B Shelar else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL)) 603618ed0c8SPravin B Shelar new_ti = table_instance_rehash(ti, ti->n_buckets); 604618ed0c8SPravin B Shelar 605618ed0c8SPravin B Shelar if (new_ti) { 606618ed0c8SPravin B Shelar rcu_assign_pointer(table->ti, new_ti); 607618ed0c8SPravin B Shelar table_instance_destroy(ti, true); 608618ed0c8SPravin B Shelar table->last_rehash = jiffies; 609618ed0c8SPravin B Shelar } 610*d29ab6f8SJoe Stringer } 611*d29ab6f8SJoe Stringer 612*d29ab6f8SJoe Stringer /* Must be called with OVS mutex held. */ 613*d29ab6f8SJoe Stringer int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow, 614*d29ab6f8SJoe Stringer const struct sw_flow_mask *mask) 615*d29ab6f8SJoe Stringer { 616*d29ab6f8SJoe Stringer int err; 617*d29ab6f8SJoe Stringer 618*d29ab6f8SJoe Stringer err = flow_mask_insert(table, flow, mask); 619*d29ab6f8SJoe Stringer if (err) 620*d29ab6f8SJoe Stringer return err; 621*d29ab6f8SJoe Stringer flow_key_insert(table, flow); 622*d29ab6f8SJoe Stringer 623618ed0c8SPravin B Shelar return 0; 624618ed0c8SPravin B Shelar } 625618ed0c8SPravin B Shelar 626e6445719SPravin B Shelar /* Initializes the flow module. 627e6445719SPravin B Shelar * Returns zero if successful or a negative error code. */ 628e6445719SPravin B Shelar int ovs_flow_init(void) 629e6445719SPravin B Shelar { 630e6445719SPravin B Shelar BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long)); 631e6445719SPravin B Shelar BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long)); 632e6445719SPravin B Shelar 63363e7959cSJarno Rajahalme flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow) 63463e7959cSJarno Rajahalme + (num_possible_nodes() 63563e7959cSJarno Rajahalme * sizeof(struct flow_stats *)), 63663e7959cSJarno Rajahalme 0, 0, NULL); 637e6445719SPravin B Shelar if (flow_cache == NULL) 638e6445719SPravin B Shelar return -ENOMEM; 639e6445719SPravin B Shelar 64063e7959cSJarno Rajahalme flow_stats_cache 64163e7959cSJarno Rajahalme = kmem_cache_create("sw_flow_stats", sizeof(struct flow_stats), 64263e7959cSJarno Rajahalme 0, SLAB_HWCACHE_ALIGN, NULL); 64363e7959cSJarno Rajahalme if (flow_stats_cache == NULL) { 64463e7959cSJarno Rajahalme kmem_cache_destroy(flow_cache); 64563e7959cSJarno Rajahalme flow_cache = NULL; 64663e7959cSJarno Rajahalme return -ENOMEM; 64763e7959cSJarno Rajahalme } 64863e7959cSJarno Rajahalme 649e6445719SPravin B Shelar return 0; 650e6445719SPravin B Shelar } 651e6445719SPravin B Shelar 652e6445719SPravin B Shelar /* Uninitializes the flow module. */ 653e6445719SPravin B Shelar void ovs_flow_exit(void) 654e6445719SPravin B Shelar { 65563e7959cSJarno Rajahalme kmem_cache_destroy(flow_stats_cache); 656e6445719SPravin B Shelar kmem_cache_destroy(flow_cache); 657e6445719SPravin B Shelar } 658