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 75*e298e505SPravin B Shelar struct sw_flow *ovs_flow_alloc(bool percpu_stats) 76e6445719SPravin B Shelar { 77e6445719SPravin B Shelar struct sw_flow *flow; 78*e298e505SPravin B Shelar int cpu; 79e6445719SPravin B Shelar 80e6445719SPravin B Shelar flow = kmem_cache_alloc(flow_cache, GFP_KERNEL); 81e6445719SPravin B Shelar if (!flow) 82e6445719SPravin B Shelar return ERR_PTR(-ENOMEM); 83e6445719SPravin B Shelar 84e6445719SPravin B Shelar flow->sf_acts = NULL; 85e6445719SPravin B Shelar flow->mask = NULL; 86e6445719SPravin B Shelar 87*e298e505SPravin B Shelar flow->stats.is_percpu = percpu_stats; 88*e298e505SPravin B Shelar 89*e298e505SPravin B Shelar if (!percpu_stats) { 90*e298e505SPravin B Shelar flow->stats.stat = kzalloc(sizeof(*flow->stats.stat), GFP_KERNEL); 91*e298e505SPravin B Shelar if (!flow->stats.stat) 92*e298e505SPravin B Shelar goto err; 93*e298e505SPravin B Shelar 94*e298e505SPravin B Shelar spin_lock_init(&flow->stats.stat->lock); 95*e298e505SPravin B Shelar } else { 96*e298e505SPravin B Shelar flow->stats.cpu_stats = alloc_percpu(struct flow_stats); 97*e298e505SPravin B Shelar if (!flow->stats.cpu_stats) 98*e298e505SPravin B Shelar goto err; 99*e298e505SPravin B Shelar 100*e298e505SPravin B Shelar for_each_possible_cpu(cpu) { 101*e298e505SPravin B Shelar struct flow_stats *cpu_stats; 102*e298e505SPravin B Shelar 103*e298e505SPravin B Shelar cpu_stats = per_cpu_ptr(flow->stats.cpu_stats, cpu); 104*e298e505SPravin B Shelar spin_lock_init(&cpu_stats->lock); 105*e298e505SPravin B Shelar } 106*e298e505SPravin B Shelar } 107e6445719SPravin B Shelar return flow; 108*e298e505SPravin B Shelar err: 109*e298e505SPravin B Shelar kfree(flow); 110*e298e505SPravin B Shelar return ERR_PTR(-ENOMEM); 111e6445719SPravin B Shelar } 112e6445719SPravin B Shelar 113b637e498SPravin B Shelar int ovs_flow_tbl_count(struct flow_table *table) 114b637e498SPravin B Shelar { 115b637e498SPravin B Shelar return table->count; 116b637e498SPravin B Shelar } 117b637e498SPravin B Shelar 118e6445719SPravin B Shelar static struct flex_array *alloc_buckets(unsigned int n_buckets) 119e6445719SPravin B Shelar { 120e6445719SPravin B Shelar struct flex_array *buckets; 121e6445719SPravin B Shelar int i, err; 122e6445719SPravin B Shelar 123e6445719SPravin B Shelar buckets = flex_array_alloc(sizeof(struct hlist_head), 124e6445719SPravin B Shelar n_buckets, GFP_KERNEL); 125e6445719SPravin B Shelar if (!buckets) 126e6445719SPravin B Shelar return NULL; 127e6445719SPravin B Shelar 128e6445719SPravin B Shelar err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL); 129e6445719SPravin B Shelar if (err) { 130e6445719SPravin B Shelar flex_array_free(buckets); 131e6445719SPravin B Shelar return NULL; 132e6445719SPravin B Shelar } 133e6445719SPravin B Shelar 134e6445719SPravin B Shelar for (i = 0; i < n_buckets; i++) 135e6445719SPravin B Shelar INIT_HLIST_HEAD((struct hlist_head *) 136e6445719SPravin B Shelar flex_array_get(buckets, i)); 137e6445719SPravin B Shelar 138e6445719SPravin B Shelar return buckets; 139e6445719SPravin B Shelar } 140e6445719SPravin B Shelar 141e6445719SPravin B Shelar static void flow_free(struct sw_flow *flow) 142e6445719SPravin B Shelar { 143e6445719SPravin B Shelar kfree((struct sf_flow_acts __force *)flow->sf_acts); 144*e298e505SPravin B Shelar if (flow->stats.is_percpu) 145*e298e505SPravin B Shelar free_percpu(flow->stats.cpu_stats); 146*e298e505SPravin B Shelar else 147*e298e505SPravin B Shelar kfree(flow->stats.stat); 148e6445719SPravin B Shelar kmem_cache_free(flow_cache, flow); 149e6445719SPravin B Shelar } 150e6445719SPravin B Shelar 151e6445719SPravin B Shelar static void rcu_free_flow_callback(struct rcu_head *rcu) 152e6445719SPravin B Shelar { 153e6445719SPravin B Shelar struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu); 154e6445719SPravin B Shelar 155e6445719SPravin B Shelar flow_free(flow); 156e6445719SPravin B Shelar } 157e6445719SPravin B Shelar 158618ed0c8SPravin B Shelar static void rcu_free_sw_flow_mask_cb(struct rcu_head *rcu) 159618ed0c8SPravin B Shelar { 160618ed0c8SPravin B Shelar struct sw_flow_mask *mask = container_of(rcu, struct sw_flow_mask, rcu); 161618ed0c8SPravin B Shelar 162618ed0c8SPravin B Shelar kfree(mask); 163618ed0c8SPravin B Shelar } 164618ed0c8SPravin B Shelar 165618ed0c8SPravin B Shelar static void flow_mask_del_ref(struct sw_flow_mask *mask, bool deferred) 166618ed0c8SPravin B Shelar { 167618ed0c8SPravin B Shelar if (!mask) 168618ed0c8SPravin B Shelar return; 169618ed0c8SPravin B Shelar 170618ed0c8SPravin B Shelar BUG_ON(!mask->ref_count); 171618ed0c8SPravin B Shelar mask->ref_count--; 172618ed0c8SPravin B Shelar 173618ed0c8SPravin B Shelar if (!mask->ref_count) { 174618ed0c8SPravin B Shelar list_del_rcu(&mask->list); 175618ed0c8SPravin B Shelar if (deferred) 176618ed0c8SPravin B Shelar call_rcu(&mask->rcu, rcu_free_sw_flow_mask_cb); 177618ed0c8SPravin B Shelar else 178618ed0c8SPravin B Shelar kfree(mask); 179618ed0c8SPravin B Shelar } 180618ed0c8SPravin B Shelar } 181618ed0c8SPravin B Shelar 182e6445719SPravin B Shelar void ovs_flow_free(struct sw_flow *flow, bool deferred) 183e6445719SPravin B Shelar { 184e6445719SPravin B Shelar if (!flow) 185e6445719SPravin B Shelar return; 186e6445719SPravin B Shelar 187618ed0c8SPravin B Shelar flow_mask_del_ref(flow->mask, deferred); 188e6445719SPravin B Shelar 189e6445719SPravin B Shelar if (deferred) 190e6445719SPravin B Shelar call_rcu(&flow->rcu, rcu_free_flow_callback); 191e6445719SPravin B Shelar else 192e6445719SPravin B Shelar flow_free(flow); 193e6445719SPravin B Shelar } 194e6445719SPravin B Shelar 195e6445719SPravin B Shelar static void free_buckets(struct flex_array *buckets) 196e6445719SPravin B Shelar { 197e6445719SPravin B Shelar flex_array_free(buckets); 198e6445719SPravin B Shelar } 199e6445719SPravin B Shelar 200b637e498SPravin B Shelar static void __table_instance_destroy(struct table_instance *ti) 201e6445719SPravin B Shelar { 202e6445719SPravin B Shelar int i; 203e6445719SPravin B Shelar 204b637e498SPravin B Shelar if (ti->keep_flows) 205e6445719SPravin B Shelar goto skip_flows; 206e6445719SPravin B Shelar 207b637e498SPravin B Shelar for (i = 0; i < ti->n_buckets; i++) { 208e6445719SPravin B Shelar struct sw_flow *flow; 209b637e498SPravin B Shelar struct hlist_head *head = flex_array_get(ti->buckets, i); 210e6445719SPravin B Shelar struct hlist_node *n; 211b637e498SPravin B Shelar int ver = ti->node_ver; 212e6445719SPravin B Shelar 213e6445719SPravin B Shelar hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) { 214e6445719SPravin B Shelar hlist_del(&flow->hash_node[ver]); 215e6445719SPravin B Shelar ovs_flow_free(flow, false); 216e6445719SPravin B Shelar } 217e6445719SPravin B Shelar } 218e6445719SPravin B Shelar 219e6445719SPravin B Shelar skip_flows: 220b637e498SPravin B Shelar free_buckets(ti->buckets); 221b637e498SPravin B Shelar kfree(ti); 222e6445719SPravin B Shelar } 223e6445719SPravin B Shelar 224b637e498SPravin B Shelar static struct table_instance *table_instance_alloc(int new_size) 225e6445719SPravin B Shelar { 226b637e498SPravin B Shelar struct table_instance *ti = kmalloc(sizeof(*ti), GFP_KERNEL); 227e6445719SPravin B Shelar 228b637e498SPravin B Shelar if (!ti) 229e6445719SPravin B Shelar return NULL; 230e6445719SPravin B Shelar 231b637e498SPravin B Shelar ti->buckets = alloc_buckets(new_size); 232e6445719SPravin B Shelar 233b637e498SPravin B Shelar if (!ti->buckets) { 234b637e498SPravin B Shelar kfree(ti); 235e6445719SPravin B Shelar return NULL; 236e6445719SPravin B Shelar } 237b637e498SPravin B Shelar ti->n_buckets = new_size; 238b637e498SPravin B Shelar ti->node_ver = 0; 239b637e498SPravin B Shelar ti->keep_flows = false; 240b637e498SPravin B Shelar get_random_bytes(&ti->hash_seed, sizeof(u32)); 241b637e498SPravin B Shelar 242b637e498SPravin B Shelar return ti; 243b637e498SPravin B Shelar } 244b637e498SPravin B Shelar 245b637e498SPravin B Shelar int ovs_flow_tbl_init(struct flow_table *table) 246b637e498SPravin B Shelar { 247b637e498SPravin B Shelar struct table_instance *ti; 248b637e498SPravin B Shelar 249b637e498SPravin B Shelar ti = table_instance_alloc(TBL_MIN_BUCKETS); 250b637e498SPravin B Shelar 251b637e498SPravin B Shelar if (!ti) 252b637e498SPravin B Shelar return -ENOMEM; 253b637e498SPravin B Shelar 254b637e498SPravin B Shelar rcu_assign_pointer(table->ti, ti); 255b637e498SPravin B Shelar INIT_LIST_HEAD(&table->mask_list); 256b637e498SPravin B Shelar table->last_rehash = jiffies; 257e6445719SPravin B Shelar table->count = 0; 258b637e498SPravin B Shelar return 0; 259e6445719SPravin B Shelar } 260e6445719SPravin B Shelar 261e6445719SPravin B Shelar static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu) 262e6445719SPravin B Shelar { 263b637e498SPravin B Shelar struct table_instance *ti = container_of(rcu, struct table_instance, rcu); 264e6445719SPravin B Shelar 265b637e498SPravin B Shelar __table_instance_destroy(ti); 266b637e498SPravin B Shelar } 267b637e498SPravin B Shelar 268b637e498SPravin B Shelar static void table_instance_destroy(struct table_instance *ti, bool deferred) 269b637e498SPravin B Shelar { 270b637e498SPravin B Shelar if (!ti) 271b637e498SPravin B Shelar return; 272b637e498SPravin B Shelar 273b637e498SPravin B Shelar if (deferred) 274b637e498SPravin B Shelar call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb); 275b637e498SPravin B Shelar else 276b637e498SPravin B Shelar __table_instance_destroy(ti); 277e6445719SPravin B Shelar } 278e6445719SPravin B Shelar 279618ed0c8SPravin B Shelar void ovs_flow_tbl_destroy(struct flow_table *table) 280e6445719SPravin B Shelar { 281b637e498SPravin B Shelar struct table_instance *ti = ovsl_dereference(table->ti); 282e6445719SPravin B Shelar 283618ed0c8SPravin B Shelar table_instance_destroy(ti, false); 284e6445719SPravin B Shelar } 285e6445719SPravin B Shelar 286b637e498SPravin B Shelar struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti, 287e6445719SPravin B Shelar u32 *bucket, u32 *last) 288e6445719SPravin B Shelar { 289e6445719SPravin B Shelar struct sw_flow *flow; 290e6445719SPravin B Shelar struct hlist_head *head; 291e6445719SPravin B Shelar int ver; 292e6445719SPravin B Shelar int i; 293e6445719SPravin B Shelar 294b637e498SPravin B Shelar ver = ti->node_ver; 295b637e498SPravin B Shelar while (*bucket < ti->n_buckets) { 296e6445719SPravin B Shelar i = 0; 297b637e498SPravin B Shelar head = flex_array_get(ti->buckets, *bucket); 298e6445719SPravin B Shelar hlist_for_each_entry_rcu(flow, head, hash_node[ver]) { 299e6445719SPravin B Shelar if (i < *last) { 300e6445719SPravin B Shelar i++; 301e6445719SPravin B Shelar continue; 302e6445719SPravin B Shelar } 303e6445719SPravin B Shelar *last = i + 1; 304e6445719SPravin B Shelar return flow; 305e6445719SPravin B Shelar } 306e6445719SPravin B Shelar (*bucket)++; 307e6445719SPravin B Shelar *last = 0; 308e6445719SPravin B Shelar } 309e6445719SPravin B Shelar 310e6445719SPravin B Shelar return NULL; 311e6445719SPravin B Shelar } 312e6445719SPravin B Shelar 313b637e498SPravin B Shelar static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash) 314e6445719SPravin B Shelar { 315b637e498SPravin B Shelar hash = jhash_1word(hash, ti->hash_seed); 316b637e498SPravin B Shelar return flex_array_get(ti->buckets, 317b637e498SPravin B Shelar (hash & (ti->n_buckets - 1))); 318e6445719SPravin B Shelar } 319e6445719SPravin B Shelar 320b637e498SPravin B Shelar static void table_instance_insert(struct table_instance *ti, struct sw_flow *flow) 321e6445719SPravin B Shelar { 322e6445719SPravin B Shelar struct hlist_head *head; 323e6445719SPravin B Shelar 324b637e498SPravin B Shelar head = find_bucket(ti, flow->hash); 325b637e498SPravin B Shelar hlist_add_head_rcu(&flow->hash_node[ti->node_ver], head); 326e6445719SPravin B Shelar } 327e6445719SPravin B Shelar 328b637e498SPravin B Shelar static void flow_table_copy_flows(struct table_instance *old, 329b637e498SPravin B Shelar struct table_instance *new) 330e6445719SPravin B Shelar { 331e6445719SPravin B Shelar int old_ver; 332e6445719SPravin B Shelar int i; 333e6445719SPravin B Shelar 334e6445719SPravin B Shelar old_ver = old->node_ver; 335e6445719SPravin B Shelar new->node_ver = !old_ver; 336e6445719SPravin B Shelar 337e6445719SPravin B Shelar /* Insert in new table. */ 338e6445719SPravin B Shelar for (i = 0; i < old->n_buckets; i++) { 339e6445719SPravin B Shelar struct sw_flow *flow; 340e6445719SPravin B Shelar struct hlist_head *head; 341e6445719SPravin B Shelar 342e6445719SPravin B Shelar head = flex_array_get(old->buckets, i); 343e6445719SPravin B Shelar 344e6445719SPravin B Shelar hlist_for_each_entry(flow, head, hash_node[old_ver]) 345b637e498SPravin B Shelar table_instance_insert(new, flow); 346e6445719SPravin B Shelar } 347e6445719SPravin B Shelar 348e6445719SPravin B Shelar old->keep_flows = true; 349e6445719SPravin B Shelar } 350e6445719SPravin B Shelar 351b637e498SPravin B Shelar static struct table_instance *table_instance_rehash(struct table_instance *ti, 352e6445719SPravin B Shelar int n_buckets) 353e6445719SPravin B Shelar { 354b637e498SPravin B Shelar struct table_instance *new_ti; 355e6445719SPravin B Shelar 356b637e498SPravin B Shelar new_ti = table_instance_alloc(n_buckets); 357b637e498SPravin B Shelar if (!new_ti) 358618ed0c8SPravin B Shelar return NULL; 359e6445719SPravin B Shelar 360b637e498SPravin B Shelar flow_table_copy_flows(ti, new_ti); 361e6445719SPravin B Shelar 362b637e498SPravin B Shelar return new_ti; 363e6445719SPravin B Shelar } 364e6445719SPravin B Shelar 365b637e498SPravin B Shelar int ovs_flow_tbl_flush(struct flow_table *flow_table) 366e6445719SPravin B Shelar { 367b637e498SPravin B Shelar struct table_instance *old_ti; 368b637e498SPravin B Shelar struct table_instance *new_ti; 369e6445719SPravin B Shelar 370b637e498SPravin B Shelar old_ti = ovsl_dereference(flow_table->ti); 371b637e498SPravin B Shelar new_ti = table_instance_alloc(TBL_MIN_BUCKETS); 372b637e498SPravin B Shelar if (!new_ti) 373b637e498SPravin B Shelar return -ENOMEM; 374b637e498SPravin B Shelar 375b637e498SPravin B Shelar rcu_assign_pointer(flow_table->ti, new_ti); 376b637e498SPravin B Shelar flow_table->last_rehash = jiffies; 377b637e498SPravin B Shelar flow_table->count = 0; 378b637e498SPravin B Shelar 379b637e498SPravin B Shelar table_instance_destroy(old_ti, true); 380b637e498SPravin B Shelar return 0; 381e6445719SPravin B Shelar } 382e6445719SPravin B Shelar 383e6445719SPravin B Shelar static u32 flow_hash(const struct sw_flow_key *key, int key_start, 384e6445719SPravin B Shelar int key_end) 385e6445719SPravin B Shelar { 386e6445719SPravin B Shelar u32 *hash_key = (u32 *)((u8 *)key + key_start); 387e6445719SPravin B Shelar int hash_u32s = (key_end - key_start) >> 2; 388e6445719SPravin B Shelar 389e6445719SPravin B Shelar /* Make sure number of hash bytes are multiple of u32. */ 390e6445719SPravin B Shelar BUILD_BUG_ON(sizeof(long) % sizeof(u32)); 391e6445719SPravin B Shelar 392e6445719SPravin B Shelar return jhash2(hash_key, hash_u32s, 0); 393e6445719SPravin B Shelar } 394e6445719SPravin B Shelar 395e6445719SPravin B Shelar static int flow_key_start(const struct sw_flow_key *key) 396e6445719SPravin B Shelar { 397e6445719SPravin B Shelar if (key->tun_key.ipv4_dst) 398e6445719SPravin B Shelar return 0; 399e6445719SPravin B Shelar else 400e6445719SPravin B Shelar return rounddown(offsetof(struct sw_flow_key, phy), 401e6445719SPravin B Shelar sizeof(long)); 402e6445719SPravin B Shelar } 403e6445719SPravin B Shelar 404e6445719SPravin B Shelar static bool cmp_key(const struct sw_flow_key *key1, 405e6445719SPravin B Shelar const struct sw_flow_key *key2, 406e6445719SPravin B Shelar int key_start, int key_end) 407e6445719SPravin B Shelar { 408e6445719SPravin B Shelar const long *cp1 = (long *)((u8 *)key1 + key_start); 409e6445719SPravin B Shelar const long *cp2 = (long *)((u8 *)key2 + key_start); 410e6445719SPravin B Shelar long diffs = 0; 411e6445719SPravin B Shelar int i; 412e6445719SPravin B Shelar 413e6445719SPravin B Shelar for (i = key_start; i < key_end; i += sizeof(long)) 414e6445719SPravin B Shelar diffs |= *cp1++ ^ *cp2++; 415e6445719SPravin B Shelar 416e6445719SPravin B Shelar return diffs == 0; 417e6445719SPravin B Shelar } 418e6445719SPravin B Shelar 419e6445719SPravin B Shelar static bool flow_cmp_masked_key(const struct sw_flow *flow, 420e6445719SPravin B Shelar const struct sw_flow_key *key, 421e6445719SPravin B Shelar int key_start, int key_end) 422e6445719SPravin B Shelar { 423e6445719SPravin B Shelar return cmp_key(&flow->key, key, key_start, key_end); 424e6445719SPravin B Shelar } 425e6445719SPravin B Shelar 426e6445719SPravin B Shelar bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow, 427e6445719SPravin B Shelar struct sw_flow_match *match) 428e6445719SPravin B Shelar { 429e6445719SPravin B Shelar struct sw_flow_key *key = match->key; 430e6445719SPravin B Shelar int key_start = flow_key_start(key); 431e6445719SPravin B Shelar int key_end = match->range.end; 432e6445719SPravin B Shelar 433e6445719SPravin B Shelar return cmp_key(&flow->unmasked_key, key, key_start, key_end); 434e6445719SPravin B Shelar } 435e6445719SPravin B Shelar 436b637e498SPravin B Shelar static struct sw_flow *masked_flow_lookup(struct table_instance *ti, 437e6445719SPravin B Shelar const struct sw_flow_key *unmasked, 438e6445719SPravin B Shelar struct sw_flow_mask *mask) 439e6445719SPravin B Shelar { 440e6445719SPravin B Shelar struct sw_flow *flow; 441e6445719SPravin B Shelar struct hlist_head *head; 442e6445719SPravin B Shelar int key_start = mask->range.start; 443e6445719SPravin B Shelar int key_end = mask->range.end; 444e6445719SPravin B Shelar u32 hash; 445e6445719SPravin B Shelar struct sw_flow_key masked_key; 446e6445719SPravin B Shelar 447e6445719SPravin B Shelar ovs_flow_mask_key(&masked_key, unmasked, mask); 448e6445719SPravin B Shelar hash = flow_hash(&masked_key, key_start, key_end); 449b637e498SPravin B Shelar head = find_bucket(ti, hash); 450b637e498SPravin B Shelar hlist_for_each_entry_rcu(flow, head, hash_node[ti->node_ver]) { 4518ddd0946SPravin B Shelar if (flow->mask == mask && flow->hash == hash && 452e6445719SPravin B Shelar flow_cmp_masked_key(flow, &masked_key, 453e6445719SPravin B Shelar key_start, key_end)) 454e6445719SPravin B Shelar return flow; 455e6445719SPravin B Shelar } 456e6445719SPravin B Shelar return NULL; 457e6445719SPravin B Shelar } 458e6445719SPravin B Shelar 4595bb50632SAndy Zhou struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl, 4601bd7116fSAndy Zhou const struct sw_flow_key *key, 4611bd7116fSAndy Zhou u32 *n_mask_hit) 462e6445719SPravin B Shelar { 463663efa36SJesse Gross struct table_instance *ti = rcu_dereference_ovsl(tbl->ti); 464e6445719SPravin B Shelar struct sw_flow_mask *mask; 465b637e498SPravin B Shelar struct sw_flow *flow; 466e6445719SPravin B Shelar 4671bd7116fSAndy Zhou *n_mask_hit = 0; 468b637e498SPravin B Shelar list_for_each_entry_rcu(mask, &tbl->mask_list, list) { 4691bd7116fSAndy Zhou (*n_mask_hit)++; 470b637e498SPravin B Shelar flow = masked_flow_lookup(ti, key, mask); 471e6445719SPravin B Shelar if (flow) /* Found */ 472b637e498SPravin B Shelar return flow; 473b637e498SPravin B Shelar } 474b637e498SPravin B Shelar return NULL; 475e6445719SPravin B Shelar } 476e6445719SPravin B Shelar 4775bb50632SAndy Zhou struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl, 4785bb50632SAndy Zhou const struct sw_flow_key *key) 4795bb50632SAndy Zhou { 4805bb50632SAndy Zhou u32 __always_unused n_mask_hit; 4815bb50632SAndy Zhou 4825bb50632SAndy Zhou return ovs_flow_tbl_lookup_stats(tbl, key, &n_mask_hit); 4835bb50632SAndy Zhou } 4845bb50632SAndy Zhou 4851bd7116fSAndy Zhou int ovs_flow_tbl_num_masks(const struct flow_table *table) 4861bd7116fSAndy Zhou { 4871bd7116fSAndy Zhou struct sw_flow_mask *mask; 4881bd7116fSAndy Zhou int num = 0; 4891bd7116fSAndy Zhou 4901bd7116fSAndy Zhou list_for_each_entry(mask, &table->mask_list, list) 4911bd7116fSAndy Zhou num++; 4921bd7116fSAndy Zhou 4931bd7116fSAndy Zhou return num; 4941bd7116fSAndy Zhou } 4951bd7116fSAndy Zhou 496b637e498SPravin B Shelar static struct table_instance *table_instance_expand(struct table_instance *ti) 497b637e498SPravin B Shelar { 498b637e498SPravin B Shelar return table_instance_rehash(ti, ti->n_buckets * 2); 499e6445719SPravin B Shelar } 500e6445719SPravin B Shelar 501e6445719SPravin B Shelar void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow) 502e6445719SPravin B Shelar { 503b637e498SPravin B Shelar struct table_instance *ti = ovsl_dereference(table->ti); 504b637e498SPravin B Shelar 505e6445719SPravin B Shelar BUG_ON(table->count == 0); 506b637e498SPravin B Shelar hlist_del_rcu(&flow->hash_node[ti->node_ver]); 507e6445719SPravin B Shelar table->count--; 508e6445719SPravin B Shelar } 509e6445719SPravin B Shelar 510618ed0c8SPravin B Shelar static struct sw_flow_mask *mask_alloc(void) 511e6445719SPravin B Shelar { 512e6445719SPravin B Shelar struct sw_flow_mask *mask; 513e6445719SPravin B Shelar 514e6445719SPravin B Shelar mask = kmalloc(sizeof(*mask), GFP_KERNEL); 515e6445719SPravin B Shelar if (mask) 516e6445719SPravin B Shelar mask->ref_count = 0; 517e6445719SPravin B Shelar 518e6445719SPravin B Shelar return mask; 519e6445719SPravin B Shelar } 520e6445719SPravin B Shelar 521618ed0c8SPravin B Shelar static void mask_add_ref(struct sw_flow_mask *mask) 522e6445719SPravin B Shelar { 523e6445719SPravin B Shelar mask->ref_count++; 524e6445719SPravin B Shelar } 525e6445719SPravin B Shelar 526e6445719SPravin B Shelar static bool mask_equal(const struct sw_flow_mask *a, 527e6445719SPravin B Shelar const struct sw_flow_mask *b) 528e6445719SPravin B Shelar { 529e6445719SPravin B Shelar u8 *a_ = (u8 *)&a->key + a->range.start; 530e6445719SPravin B Shelar u8 *b_ = (u8 *)&b->key + b->range.start; 531e6445719SPravin B Shelar 532e6445719SPravin B Shelar return (a->range.end == b->range.end) 533e6445719SPravin B Shelar && (a->range.start == b->range.start) 534e6445719SPravin B Shelar && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0); 535e6445719SPravin B Shelar } 536e6445719SPravin B Shelar 537618ed0c8SPravin B Shelar static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl, 538e6445719SPravin B Shelar const struct sw_flow_mask *mask) 539e6445719SPravin B Shelar { 540e6445719SPravin B Shelar struct list_head *ml; 541e6445719SPravin B Shelar 542b637e498SPravin B Shelar list_for_each(ml, &tbl->mask_list) { 543e6445719SPravin B Shelar struct sw_flow_mask *m; 544e6445719SPravin B Shelar m = container_of(ml, struct sw_flow_mask, list); 545e6445719SPravin B Shelar if (mask_equal(mask, m)) 546e6445719SPravin B Shelar return m; 547e6445719SPravin B Shelar } 548e6445719SPravin B Shelar 549e6445719SPravin B Shelar return NULL; 550e6445719SPravin B Shelar } 551e6445719SPravin B Shelar 552d1211908SBen Pfaff /* Add 'mask' into the mask list, if it is not already there. */ 553618ed0c8SPravin B Shelar static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow, 554618ed0c8SPravin B Shelar struct sw_flow_mask *new) 555e6445719SPravin B Shelar { 556618ed0c8SPravin B Shelar struct sw_flow_mask *mask; 557618ed0c8SPravin B Shelar mask = flow_mask_find(tbl, new); 558618ed0c8SPravin B Shelar if (!mask) { 559618ed0c8SPravin B Shelar /* Allocate a new mask if none exsits. */ 560618ed0c8SPravin B Shelar mask = mask_alloc(); 561618ed0c8SPravin B Shelar if (!mask) 562618ed0c8SPravin B Shelar return -ENOMEM; 563618ed0c8SPravin B Shelar mask->key = new->key; 564618ed0c8SPravin B Shelar mask->range = new->range; 565b637e498SPravin B Shelar list_add_rcu(&mask->list, &tbl->mask_list); 566e6445719SPravin B Shelar } 567e6445719SPravin B Shelar 568618ed0c8SPravin B Shelar mask_add_ref(mask); 569618ed0c8SPravin B Shelar flow->mask = mask; 570618ed0c8SPravin B Shelar return 0; 571618ed0c8SPravin B Shelar } 572618ed0c8SPravin B Shelar 573618ed0c8SPravin B Shelar int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow, 574618ed0c8SPravin B Shelar struct sw_flow_mask *mask) 575618ed0c8SPravin B Shelar { 576618ed0c8SPravin B Shelar struct table_instance *new_ti = NULL; 577618ed0c8SPravin B Shelar struct table_instance *ti; 578618ed0c8SPravin B Shelar int err; 579618ed0c8SPravin B Shelar 580618ed0c8SPravin B Shelar err = flow_mask_insert(table, flow, mask); 581618ed0c8SPravin B Shelar if (err) 582618ed0c8SPravin B Shelar return err; 583618ed0c8SPravin B Shelar 584618ed0c8SPravin B Shelar flow->hash = flow_hash(&flow->key, flow->mask->range.start, 585618ed0c8SPravin B Shelar flow->mask->range.end); 586618ed0c8SPravin B Shelar ti = ovsl_dereference(table->ti); 587618ed0c8SPravin B Shelar table_instance_insert(ti, flow); 588618ed0c8SPravin B Shelar table->count++; 589618ed0c8SPravin B Shelar 590618ed0c8SPravin B Shelar /* Expand table, if necessary, to make room. */ 591618ed0c8SPravin B Shelar if (table->count > ti->n_buckets) 592618ed0c8SPravin B Shelar new_ti = table_instance_expand(ti); 593618ed0c8SPravin B Shelar else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL)) 594618ed0c8SPravin B Shelar new_ti = table_instance_rehash(ti, ti->n_buckets); 595618ed0c8SPravin B Shelar 596618ed0c8SPravin B Shelar if (new_ti) { 597618ed0c8SPravin B Shelar rcu_assign_pointer(table->ti, new_ti); 598618ed0c8SPravin B Shelar table_instance_destroy(ti, true); 599618ed0c8SPravin B Shelar table->last_rehash = jiffies; 600618ed0c8SPravin B Shelar } 601618ed0c8SPravin B Shelar return 0; 602618ed0c8SPravin B Shelar } 603618ed0c8SPravin B Shelar 604e6445719SPravin B Shelar /* Initializes the flow module. 605e6445719SPravin B Shelar * Returns zero if successful or a negative error code. */ 606e6445719SPravin B Shelar int ovs_flow_init(void) 607e6445719SPravin B Shelar { 608e6445719SPravin B Shelar BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long)); 609e6445719SPravin B Shelar BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long)); 610e6445719SPravin B Shelar 611e6445719SPravin B Shelar flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0, 612e6445719SPravin B Shelar 0, NULL); 613e6445719SPravin B Shelar if (flow_cache == NULL) 614e6445719SPravin B Shelar return -ENOMEM; 615e6445719SPravin B Shelar 616e6445719SPravin B Shelar return 0; 617e6445719SPravin B Shelar } 618e6445719SPravin B Shelar 619e6445719SPravin B Shelar /* Uninitializes the flow module. */ 620e6445719SPravin B Shelar void ovs_flow_exit(void) 621e6445719SPravin B Shelar { 622e6445719SPravin B Shelar kmem_cache_destroy(flow_cache); 623e6445719SPravin B Shelar } 624