1c9422999SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2e6445719SPravin B Shelar /* 39b996e54SPravin B Shelar * Copyright (c) 2007-2014 Nicira, Inc. 4e6445719SPravin B Shelar */ 5e6445719SPravin B Shelar 6e6445719SPravin B Shelar #include "flow.h" 7e6445719SPravin B Shelar #include "datapath.h" 834ae932aSThomas Graf #include "flow_netlink.h" 9e6445719SPravin B Shelar #include <linux/uaccess.h> 10e6445719SPravin B Shelar #include <linux/netdevice.h> 11e6445719SPravin B Shelar #include <linux/etherdevice.h> 12e6445719SPravin B Shelar #include <linux/if_ether.h> 13e6445719SPravin B Shelar #include <linux/if_vlan.h> 14e6445719SPravin B Shelar #include <net/llc_pdu.h> 15e6445719SPravin B Shelar #include <linux/kernel.h> 1687545899SDaniel Borkmann #include <linux/jhash.h> 17e6445719SPravin B Shelar #include <linux/jiffies.h> 18e6445719SPravin B Shelar #include <linux/llc.h> 19e6445719SPravin B Shelar #include <linux/module.h> 20e6445719SPravin B Shelar #include <linux/in.h> 21e6445719SPravin B Shelar #include <linux/rcupdate.h> 22db74a333SThadeu Lima de Souza Cascardo #include <linux/cpumask.h> 23e6445719SPravin B Shelar #include <linux/if_arp.h> 24e6445719SPravin B Shelar #include <linux/ip.h> 25e6445719SPravin B Shelar #include <linux/ipv6.h> 26e6445719SPravin B Shelar #include <linux/sctp.h> 27e6445719SPravin B Shelar #include <linux/tcp.h> 28e6445719SPravin B Shelar #include <linux/udp.h> 29e6445719SPravin B Shelar #include <linux/icmp.h> 30e6445719SPravin B Shelar #include <linux/icmpv6.h> 31e6445719SPravin B Shelar #include <linux/rculist.h> 32eac87c41SEelco Chaudron #include <linux/sort.h> 33e6445719SPravin B Shelar #include <net/ip.h> 34e6445719SPravin B Shelar #include <net/ipv6.h> 35e6445719SPravin B Shelar #include <net/ndisc.h> 36e6445719SPravin B Shelar 37b637e498SPravin B Shelar #define TBL_MIN_BUCKETS 1024 384bc63b1bSTonghao Zhang #define MASK_ARRAY_SIZE_MIN 16 39b637e498SPravin B Shelar #define REHASH_INTERVAL (10 * 60 * HZ) 40b637e498SPravin B Shelar 419bf24f59SEelco Chaudron #define MC_DEFAULT_HASH_ENTRIES 256 4204b7d136STonghao Zhang #define MC_HASH_SHIFT 8 4304b7d136STonghao Zhang #define MC_HASH_SEGS ((sizeof(uint32_t) * 8) / MC_HASH_SHIFT) 4404b7d136STonghao Zhang 45e6445719SPravin B Shelar static struct kmem_cache *flow_cache; 4663e7959cSJarno Rajahalme struct kmem_cache *flow_stats_cache __read_mostly; 47e6445719SPravin B Shelar 48e6445719SPravin B Shelar static u16 range_n_bytes(const struct sw_flow_key_range *range) 49e6445719SPravin B Shelar { 50e6445719SPravin B Shelar return range->end - range->start; 51e6445719SPravin B Shelar } 52e6445719SPravin B Shelar 53e6445719SPravin B Shelar void ovs_flow_mask_key(struct sw_flow_key *dst, const struct sw_flow_key *src, 54ae5f2fb1SJesse Gross bool full, const struct sw_flow_mask *mask) 55e6445719SPravin B Shelar { 56ae5f2fb1SJesse Gross int start = full ? 0 : mask->range.start; 57ae5f2fb1SJesse Gross int len = full ? sizeof *dst : range_n_bytes(&mask->range); 58ae5f2fb1SJesse Gross const long *m = (const long *)((const u8 *)&mask->key + start); 59ae5f2fb1SJesse Gross const long *s = (const long *)((const u8 *)src + start); 60ae5f2fb1SJesse Gross long *d = (long *)((u8 *)dst + start); 61e6445719SPravin B Shelar int i; 62e6445719SPravin B Shelar 63ae5f2fb1SJesse Gross /* If 'full' is true then all of 'dst' is fully initialized. Otherwise, 64ae5f2fb1SJesse Gross * if 'full' is false the memory outside of the 'mask->range' is left 65ae5f2fb1SJesse Gross * uninitialized. This can be used as an optimization when further 66ae5f2fb1SJesse Gross * operations on 'dst' only use contents within 'mask->range'. 67e6445719SPravin B Shelar */ 68ae5f2fb1SJesse Gross for (i = 0; i < len; i += sizeof(long)) 69e6445719SPravin B Shelar *d++ = *s++ & *m++; 70e6445719SPravin B Shelar } 71e6445719SPravin B Shelar 7223dabf88SJarno Rajahalme struct sw_flow *ovs_flow_alloc(void) 73e6445719SPravin B Shelar { 74e6445719SPravin B Shelar struct sw_flow *flow; 75aef833c5SPablo Neira Ayuso struct sw_flow_stats *stats; 76e6445719SPravin B Shelar 77db74a333SThadeu Lima de Souza Cascardo flow = kmem_cache_zalloc(flow_cache, GFP_KERNEL); 78e6445719SPravin B Shelar if (!flow) 79e6445719SPravin B Shelar return ERR_PTR(-ENOMEM); 80e6445719SPravin B Shelar 81db74a333SThadeu Lima de Souza Cascardo flow->stats_last_writer = -1; 82e6445719SPravin B Shelar 8363e7959cSJarno Rajahalme /* Initialize the default stat node. */ 8463e7959cSJarno Rajahalme stats = kmem_cache_alloc_node(flow_stats_cache, 85598c12d0SKonstantin Khlebnikov GFP_KERNEL | __GFP_ZERO, 86598c12d0SKonstantin Khlebnikov node_online(0) ? 0 : NUMA_NO_NODE); 8763e7959cSJarno Rajahalme if (!stats) 88e298e505SPravin B Shelar goto err; 89e298e505SPravin B Shelar 9063e7959cSJarno Rajahalme spin_lock_init(&stats->lock); 91e298e505SPravin B Shelar 9263e7959cSJarno Rajahalme RCU_INIT_POINTER(flow->stats[0], stats); 9363e7959cSJarno Rajahalme 94c4b2bf6bSTonghao Zhang cpumask_set_cpu(0, &flow->cpu_used_mask); 95c4b2bf6bSTonghao Zhang 96e6445719SPravin B Shelar return flow; 97e298e505SPravin B Shelar err: 98ece37c87SWei Yongjun kmem_cache_free(flow_cache, flow); 99e298e505SPravin B Shelar return ERR_PTR(-ENOMEM); 100e6445719SPravin B Shelar } 101e6445719SPravin B Shelar 10212eb18f7SThomas Graf int ovs_flow_tbl_count(const struct flow_table *table) 103b637e498SPravin B Shelar { 104b637e498SPravin B Shelar return table->count; 105b637e498SPravin B Shelar } 106b637e498SPravin B Shelar 107e6445719SPravin B Shelar static void flow_free(struct sw_flow *flow) 108e6445719SPravin B Shelar { 109db74a333SThadeu Lima de Souza Cascardo int cpu; 11063e7959cSJarno Rajahalme 11174ed7ab9SJoe Stringer if (ovs_identifier_is_key(&flow->id)) 11274ed7ab9SJoe Stringer kfree(flow->id.unmasked_key); 11334ae932aSThomas Graf if (flow->sf_acts) 114cf3266adSTonghao Zhang ovs_nla_free_flow_actions((struct sw_flow_actions __force *) 115cf3266adSTonghao Zhang flow->sf_acts); 116db74a333SThadeu Lima de Souza Cascardo /* We open code this to make sure cpu 0 is always considered */ 117cf3266adSTonghao Zhang for (cpu = 0; cpu < nr_cpu_ids; 118cf3266adSTonghao Zhang cpu = cpumask_next(cpu, &flow->cpu_used_mask)) { 119db74a333SThadeu Lima de Souza Cascardo if (flow->stats[cpu]) 12063e7959cSJarno Rajahalme kmem_cache_free(flow_stats_cache, 121aef833c5SPablo Neira Ayuso (struct sw_flow_stats __force *)flow->stats[cpu]); 122cf3266adSTonghao Zhang } 123cf3266adSTonghao Zhang 124e6445719SPravin B Shelar kmem_cache_free(flow_cache, flow); 125e6445719SPravin B Shelar } 126e6445719SPravin B Shelar 127e6445719SPravin B Shelar static void rcu_free_flow_callback(struct rcu_head *rcu) 128e6445719SPravin B Shelar { 129e6445719SPravin B Shelar struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu); 130e6445719SPravin B Shelar 131e6445719SPravin B Shelar flow_free(flow); 132e6445719SPravin B Shelar } 133e6445719SPravin B Shelar 134e80857ccSAndy Zhou void ovs_flow_free(struct sw_flow *flow, bool deferred) 135618ed0c8SPravin B Shelar { 136e80857ccSAndy Zhou if (!flow) 137618ed0c8SPravin B Shelar return; 138618ed0c8SPravin B Shelar 139e6445719SPravin B Shelar if (deferred) 140e6445719SPravin B Shelar call_rcu(&flow->rcu, rcu_free_flow_callback); 141e6445719SPravin B Shelar else 142e6445719SPravin B Shelar flow_free(flow); 143e6445719SPravin B Shelar } 144e6445719SPravin B Shelar 145b637e498SPravin B Shelar static void __table_instance_destroy(struct table_instance *ti) 146e6445719SPravin B Shelar { 147ee9c5e67SKent Overstreet kvfree(ti->buckets); 148b637e498SPravin B Shelar kfree(ti); 149e6445719SPravin B Shelar } 150e6445719SPravin B Shelar 151b637e498SPravin B Shelar static struct table_instance *table_instance_alloc(int new_size) 152e6445719SPravin B Shelar { 153b637e498SPravin B Shelar struct table_instance *ti = kmalloc(sizeof(*ti), GFP_KERNEL); 154ee9c5e67SKent Overstreet int i; 155e6445719SPravin B Shelar 156b637e498SPravin B Shelar if (!ti) 157e6445719SPravin B Shelar return NULL; 158e6445719SPravin B Shelar 159ee9c5e67SKent Overstreet ti->buckets = kvmalloc_array(new_size, sizeof(struct hlist_head), 160ee9c5e67SKent Overstreet GFP_KERNEL); 161b637e498SPravin B Shelar if (!ti->buckets) { 162b637e498SPravin B Shelar kfree(ti); 163e6445719SPravin B Shelar return NULL; 164e6445719SPravin B Shelar } 165ee9c5e67SKent Overstreet 166ee9c5e67SKent Overstreet for (i = 0; i < new_size; i++) 167ee9c5e67SKent Overstreet INIT_HLIST_HEAD(&ti->buckets[i]); 168ee9c5e67SKent Overstreet 169b637e498SPravin B Shelar ti->n_buckets = new_size; 170b637e498SPravin B Shelar ti->node_ver = 0; 171b637e498SPravin B Shelar get_random_bytes(&ti->hash_seed, sizeof(u32)); 172b637e498SPravin B Shelar 173b637e498SPravin B Shelar return ti; 174b637e498SPravin B Shelar } 175b637e498SPravin B Shelar 176eac87c41SEelco Chaudron static void __mask_array_destroy(struct mask_array *ma) 177eac87c41SEelco Chaudron { 178f981fc3dSEelco Chaudron free_percpu(ma->masks_usage_stats); 179eac87c41SEelco Chaudron kfree(ma); 180eac87c41SEelco Chaudron } 181eac87c41SEelco Chaudron 182eac87c41SEelco Chaudron static void mask_array_rcu_cb(struct rcu_head *rcu) 183eac87c41SEelco Chaudron { 184eac87c41SEelco Chaudron struct mask_array *ma = container_of(rcu, struct mask_array, rcu); 185eac87c41SEelco Chaudron 186eac87c41SEelco Chaudron __mask_array_destroy(ma); 187eac87c41SEelco Chaudron } 188eac87c41SEelco Chaudron 189eac87c41SEelco Chaudron static void tbl_mask_array_reset_counters(struct mask_array *ma) 190eac87c41SEelco Chaudron { 191eac87c41SEelco Chaudron int i, cpu; 192eac87c41SEelco Chaudron 193eac87c41SEelco Chaudron /* As the per CPU counters are not atomic we can not go ahead and 194eac87c41SEelco Chaudron * reset them from another CPU. To be able to still have an approximate 195eac87c41SEelco Chaudron * zero based counter we store the value at reset, and subtract it 196eac87c41SEelco Chaudron * later when processing. 197eac87c41SEelco Chaudron */ 198eac87c41SEelco Chaudron for (i = 0; i < ma->max; i++) { 199eac87c41SEelco Chaudron ma->masks_usage_zero_cntr[i] = 0; 200eac87c41SEelco Chaudron 201eac87c41SEelco Chaudron for_each_possible_cpu(cpu) { 202f981fc3dSEelco Chaudron struct mask_array_stats *stats; 203eac87c41SEelco Chaudron unsigned int start; 204eac87c41SEelco Chaudron u64 counter; 205eac87c41SEelco Chaudron 206f981fc3dSEelco Chaudron stats = per_cpu_ptr(ma->masks_usage_stats, cpu); 207eac87c41SEelco Chaudron do { 208*d120d1a6SThomas Gleixner start = u64_stats_fetch_begin(&stats->syncp); 209f981fc3dSEelco Chaudron counter = stats->usage_cntrs[i]; 210*d120d1a6SThomas Gleixner } while (u64_stats_fetch_retry(&stats->syncp, start)); 211eac87c41SEelco Chaudron 212eac87c41SEelco Chaudron ma->masks_usage_zero_cntr[i] += counter; 213eac87c41SEelco Chaudron } 214eac87c41SEelco Chaudron } 215eac87c41SEelco Chaudron } 216eac87c41SEelco Chaudron 2174bc63b1bSTonghao Zhang static struct mask_array *tbl_mask_array_alloc(int size) 2184bc63b1bSTonghao Zhang { 2194bc63b1bSTonghao Zhang struct mask_array *new; 2204bc63b1bSTonghao Zhang 2214bc63b1bSTonghao Zhang size = max(MASK_ARRAY_SIZE_MIN, size); 2224bc63b1bSTonghao Zhang new = kzalloc(sizeof(struct mask_array) + 223eac87c41SEelco Chaudron sizeof(struct sw_flow_mask *) * size + 224eac87c41SEelco Chaudron sizeof(u64) * size, GFP_KERNEL); 2254bc63b1bSTonghao Zhang if (!new) 2264bc63b1bSTonghao Zhang return NULL; 2274bc63b1bSTonghao Zhang 228eac87c41SEelco Chaudron new->masks_usage_zero_cntr = (u64 *)((u8 *)new + 229eac87c41SEelco Chaudron sizeof(struct mask_array) + 230eac87c41SEelco Chaudron sizeof(struct sw_flow_mask *) * 231eac87c41SEelco Chaudron size); 232eac87c41SEelco Chaudron 233f981fc3dSEelco Chaudron new->masks_usage_stats = __alloc_percpu(sizeof(struct mask_array_stats) + 234f981fc3dSEelco Chaudron sizeof(u64) * size, 235eac87c41SEelco Chaudron __alignof__(u64)); 236f981fc3dSEelco Chaudron if (!new->masks_usage_stats) { 237eac87c41SEelco Chaudron kfree(new); 238eac87c41SEelco Chaudron return NULL; 239eac87c41SEelco Chaudron } 240eac87c41SEelco Chaudron 2414bc63b1bSTonghao Zhang new->count = 0; 2424bc63b1bSTonghao Zhang new->max = size; 2434bc63b1bSTonghao Zhang 2444bc63b1bSTonghao Zhang return new; 2454bc63b1bSTonghao Zhang } 2464bc63b1bSTonghao Zhang 2474bc63b1bSTonghao Zhang static int tbl_mask_array_realloc(struct flow_table *tbl, int size) 2484bc63b1bSTonghao Zhang { 2494bc63b1bSTonghao Zhang struct mask_array *old; 2504bc63b1bSTonghao Zhang struct mask_array *new; 2514bc63b1bSTonghao Zhang 2524bc63b1bSTonghao Zhang new = tbl_mask_array_alloc(size); 2534bc63b1bSTonghao Zhang if (!new) 2544bc63b1bSTonghao Zhang return -ENOMEM; 2554bc63b1bSTonghao Zhang 2564bc63b1bSTonghao Zhang old = ovsl_dereference(tbl->mask_array); 2574bc63b1bSTonghao Zhang if (old) { 2584bc63b1bSTonghao Zhang int i; 2594bc63b1bSTonghao Zhang 2604bc63b1bSTonghao Zhang for (i = 0; i < old->max; i++) { 2614bc63b1bSTonghao Zhang if (ovsl_dereference(old->masks[i])) 2624bc63b1bSTonghao Zhang new->masks[new->count++] = old->masks[i]; 2634bc63b1bSTonghao Zhang } 264eac87c41SEelco Chaudron call_rcu(&old->rcu, mask_array_rcu_cb); 2654bc63b1bSTonghao Zhang } 2664bc63b1bSTonghao Zhang 2674bc63b1bSTonghao Zhang rcu_assign_pointer(tbl->mask_array, new); 2684bc63b1bSTonghao Zhang 2694bc63b1bSTonghao Zhang return 0; 2704bc63b1bSTonghao Zhang } 2714bc63b1bSTonghao Zhang 27250b0e61bSTonghao Zhang static int tbl_mask_array_add_mask(struct flow_table *tbl, 27350b0e61bSTonghao Zhang struct sw_flow_mask *new) 27450b0e61bSTonghao Zhang { 27550b0e61bSTonghao Zhang struct mask_array *ma = ovsl_dereference(tbl->mask_array); 27650b0e61bSTonghao Zhang int err, ma_count = READ_ONCE(ma->count); 27750b0e61bSTonghao Zhang 27850b0e61bSTonghao Zhang if (ma_count >= ma->max) { 27950b0e61bSTonghao Zhang err = tbl_mask_array_realloc(tbl, ma->max + 28050b0e61bSTonghao Zhang MASK_ARRAY_SIZE_MIN); 28150b0e61bSTonghao Zhang if (err) 28250b0e61bSTonghao Zhang return err; 28350b0e61bSTonghao Zhang 28450b0e61bSTonghao Zhang ma = ovsl_dereference(tbl->mask_array); 285eac87c41SEelco Chaudron } else { 286eac87c41SEelco Chaudron /* On every add or delete we need to reset the counters so 287eac87c41SEelco Chaudron * every new mask gets a fair chance of being prioritized. 288eac87c41SEelco Chaudron */ 289eac87c41SEelco Chaudron tbl_mask_array_reset_counters(ma); 29050b0e61bSTonghao Zhang } 29150b0e61bSTonghao Zhang 29250b0e61bSTonghao Zhang BUG_ON(ovsl_dereference(ma->masks[ma_count])); 29350b0e61bSTonghao Zhang 29450b0e61bSTonghao Zhang rcu_assign_pointer(ma->masks[ma_count], new); 29550b0e61bSTonghao Zhang WRITE_ONCE(ma->count, ma_count + 1); 29650b0e61bSTonghao Zhang 29750b0e61bSTonghao Zhang return 0; 29850b0e61bSTonghao Zhang } 29950b0e61bSTonghao Zhang 30050b0e61bSTonghao Zhang static void tbl_mask_array_del_mask(struct flow_table *tbl, 30150b0e61bSTonghao Zhang struct sw_flow_mask *mask) 30250b0e61bSTonghao Zhang { 30350b0e61bSTonghao Zhang struct mask_array *ma = ovsl_dereference(tbl->mask_array); 30450b0e61bSTonghao Zhang int i, ma_count = READ_ONCE(ma->count); 30550b0e61bSTonghao Zhang 30650b0e61bSTonghao Zhang /* Remove the deleted mask pointers from the array */ 30750b0e61bSTonghao Zhang for (i = 0; i < ma_count; i++) { 30850b0e61bSTonghao Zhang if (mask == ovsl_dereference(ma->masks[i])) 30950b0e61bSTonghao Zhang goto found; 31050b0e61bSTonghao Zhang } 31150b0e61bSTonghao Zhang 31250b0e61bSTonghao Zhang BUG(); 31350b0e61bSTonghao Zhang return; 31450b0e61bSTonghao Zhang 31550b0e61bSTonghao Zhang found: 31650b0e61bSTonghao Zhang WRITE_ONCE(ma->count, ma_count - 1); 31750b0e61bSTonghao Zhang 31850b0e61bSTonghao Zhang rcu_assign_pointer(ma->masks[i], ma->masks[ma_count - 1]); 31950b0e61bSTonghao Zhang RCU_INIT_POINTER(ma->masks[ma_count - 1], NULL); 32050b0e61bSTonghao Zhang 32150b0e61bSTonghao Zhang kfree_rcu(mask, rcu); 32250b0e61bSTonghao Zhang 32350b0e61bSTonghao Zhang /* Shrink the mask array if necessary. */ 32450b0e61bSTonghao Zhang if (ma->max >= (MASK_ARRAY_SIZE_MIN * 2) && 32550b0e61bSTonghao Zhang ma_count <= (ma->max / 3)) 32650b0e61bSTonghao Zhang tbl_mask_array_realloc(tbl, ma->max / 2); 327eac87c41SEelco Chaudron else 328eac87c41SEelco Chaudron tbl_mask_array_reset_counters(ma); 329eac87c41SEelco Chaudron 33050b0e61bSTonghao Zhang } 33150b0e61bSTonghao Zhang 33250b0e61bSTonghao Zhang /* Remove 'mask' from the mask list, if it is not needed any more. */ 33350b0e61bSTonghao Zhang static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask) 33450b0e61bSTonghao Zhang { 33550b0e61bSTonghao Zhang if (mask) { 33650b0e61bSTonghao Zhang /* ovs-lock is required to protect mask-refcount and 33750b0e61bSTonghao Zhang * mask list. 33850b0e61bSTonghao Zhang */ 33950b0e61bSTonghao Zhang ASSERT_OVSL(); 34050b0e61bSTonghao Zhang BUG_ON(!mask->ref_count); 34150b0e61bSTonghao Zhang mask->ref_count--; 34250b0e61bSTonghao Zhang 34350b0e61bSTonghao Zhang if (!mask->ref_count) 34450b0e61bSTonghao Zhang tbl_mask_array_del_mask(tbl, mask); 34550b0e61bSTonghao Zhang } 34650b0e61bSTonghao Zhang } 34750b0e61bSTonghao Zhang 3489bf24f59SEelco Chaudron static void __mask_cache_destroy(struct mask_cache *mc) 3499bf24f59SEelco Chaudron { 3509bf24f59SEelco Chaudron free_percpu(mc->mask_cache); 3519bf24f59SEelco Chaudron kfree(mc); 3529bf24f59SEelco Chaudron } 3539bf24f59SEelco Chaudron 3549bf24f59SEelco Chaudron static void mask_cache_rcu_cb(struct rcu_head *rcu) 3559bf24f59SEelco Chaudron { 3569bf24f59SEelco Chaudron struct mask_cache *mc = container_of(rcu, struct mask_cache, rcu); 3579bf24f59SEelco Chaudron 3589bf24f59SEelco Chaudron __mask_cache_destroy(mc); 3599bf24f59SEelco Chaudron } 3609bf24f59SEelco Chaudron 3619bf24f59SEelco Chaudron static struct mask_cache *tbl_mask_cache_alloc(u32 size) 3629bf24f59SEelco Chaudron { 3639bf24f59SEelco Chaudron struct mask_cache_entry __percpu *cache = NULL; 3649bf24f59SEelco Chaudron struct mask_cache *new; 3659bf24f59SEelco Chaudron 3669bf24f59SEelco Chaudron /* Only allow size to be 0, or a power of 2, and does not exceed 3679bf24f59SEelco Chaudron * percpu allocation size. 3689bf24f59SEelco Chaudron */ 3699bf24f59SEelco Chaudron if ((!is_power_of_2(size) && size != 0) || 3709bf24f59SEelco Chaudron (size * sizeof(struct mask_cache_entry)) > PCPU_MIN_UNIT_SIZE) 3719bf24f59SEelco Chaudron return NULL; 3729bf24f59SEelco Chaudron 3739bf24f59SEelco Chaudron new = kzalloc(sizeof(*new), GFP_KERNEL); 3749bf24f59SEelco Chaudron if (!new) 3759bf24f59SEelco Chaudron return NULL; 3769bf24f59SEelco Chaudron 3779bf24f59SEelco Chaudron new->cache_size = size; 3789bf24f59SEelco Chaudron if (new->cache_size > 0) { 3799bf24f59SEelco Chaudron cache = __alloc_percpu(array_size(sizeof(struct mask_cache_entry), 3809bf24f59SEelco Chaudron new->cache_size), 3819bf24f59SEelco Chaudron __alignof__(struct mask_cache_entry)); 3829bf24f59SEelco Chaudron if (!cache) { 3839bf24f59SEelco Chaudron kfree(new); 3849bf24f59SEelco Chaudron return NULL; 3859bf24f59SEelco Chaudron } 3869bf24f59SEelco Chaudron } 3879bf24f59SEelco Chaudron 3889bf24f59SEelco Chaudron new->mask_cache = cache; 3899bf24f59SEelco Chaudron return new; 3909bf24f59SEelco Chaudron } 3919bf24f59SEelco Chaudron int ovs_flow_tbl_masks_cache_resize(struct flow_table *table, u32 size) 3929bf24f59SEelco Chaudron { 393fea07a48SEelco Chaudron struct mask_cache *mc = rcu_dereference_ovsl(table->mask_cache); 3949bf24f59SEelco Chaudron struct mask_cache *new; 3959bf24f59SEelco Chaudron 3969bf24f59SEelco Chaudron if (size == mc->cache_size) 3979bf24f59SEelco Chaudron return 0; 3989bf24f59SEelco Chaudron 3999bf24f59SEelco Chaudron if ((!is_power_of_2(size) && size != 0) || 4009bf24f59SEelco Chaudron (size * sizeof(struct mask_cache_entry)) > PCPU_MIN_UNIT_SIZE) 4019bf24f59SEelco Chaudron return -EINVAL; 4029bf24f59SEelco Chaudron 4039bf24f59SEelco Chaudron new = tbl_mask_cache_alloc(size); 4049bf24f59SEelco Chaudron if (!new) 4059bf24f59SEelco Chaudron return -ENOMEM; 4069bf24f59SEelco Chaudron 4079bf24f59SEelco Chaudron rcu_assign_pointer(table->mask_cache, new); 4089bf24f59SEelco Chaudron call_rcu(&mc->rcu, mask_cache_rcu_cb); 4099bf24f59SEelco Chaudron 4109bf24f59SEelco Chaudron return 0; 4119bf24f59SEelco Chaudron } 4129bf24f59SEelco Chaudron 413b637e498SPravin B Shelar int ovs_flow_tbl_init(struct flow_table *table) 414b637e498SPravin B Shelar { 41574ed7ab9SJoe Stringer struct table_instance *ti, *ufid_ti; 4169bf24f59SEelco Chaudron struct mask_cache *mc; 4174bc63b1bSTonghao Zhang struct mask_array *ma; 418b637e498SPravin B Shelar 4199bf24f59SEelco Chaudron mc = tbl_mask_cache_alloc(MC_DEFAULT_HASH_ENTRIES); 4209bf24f59SEelco Chaudron if (!mc) 421b637e498SPravin B Shelar return -ENOMEM; 422b637e498SPravin B Shelar 4234bc63b1bSTonghao Zhang ma = tbl_mask_array_alloc(MASK_ARRAY_SIZE_MIN); 4244bc63b1bSTonghao Zhang if (!ma) 4254bc63b1bSTonghao Zhang goto free_mask_cache; 4264bc63b1bSTonghao Zhang 42704b7d136STonghao Zhang ti = table_instance_alloc(TBL_MIN_BUCKETS); 42804b7d136STonghao Zhang if (!ti) 4294bc63b1bSTonghao Zhang goto free_mask_array; 43004b7d136STonghao Zhang 43174ed7ab9SJoe Stringer ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS); 43274ed7ab9SJoe Stringer if (!ufid_ti) 43374ed7ab9SJoe Stringer goto free_ti; 43474ed7ab9SJoe Stringer 435b637e498SPravin B Shelar rcu_assign_pointer(table->ti, ti); 43674ed7ab9SJoe Stringer rcu_assign_pointer(table->ufid_ti, ufid_ti); 4374bc63b1bSTonghao Zhang rcu_assign_pointer(table->mask_array, ma); 4389bf24f59SEelco Chaudron rcu_assign_pointer(table->mask_cache, mc); 439b637e498SPravin B Shelar table->last_rehash = jiffies; 440e6445719SPravin B Shelar table->count = 0; 44174ed7ab9SJoe Stringer table->ufid_count = 0; 442b637e498SPravin B Shelar return 0; 44374ed7ab9SJoe Stringer 44474ed7ab9SJoe Stringer free_ti: 44574ed7ab9SJoe Stringer __table_instance_destroy(ti); 4464bc63b1bSTonghao Zhang free_mask_array: 447eac87c41SEelco Chaudron __mask_array_destroy(ma); 44804b7d136STonghao Zhang free_mask_cache: 4499bf24f59SEelco Chaudron __mask_cache_destroy(mc); 45074ed7ab9SJoe Stringer return -ENOMEM; 451e6445719SPravin B Shelar } 452e6445719SPravin B Shelar 453e6445719SPravin B Shelar static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu) 454e6445719SPravin B Shelar { 455cf3266adSTonghao Zhang struct table_instance *ti; 456e6445719SPravin B Shelar 457cf3266adSTonghao Zhang ti = container_of(rcu, struct table_instance, rcu); 458b637e498SPravin B Shelar __table_instance_destroy(ti); 459b637e498SPravin B Shelar } 460b637e498SPravin B Shelar 46150b0e61bSTonghao Zhang static void table_instance_flow_free(struct flow_table *table, 46250b0e61bSTonghao Zhang struct table_instance *ti, 46350b0e61bSTonghao Zhang struct table_instance *ufid_ti, 464df68d64eSTonghao Zhang struct sw_flow *flow) 46550b0e61bSTonghao Zhang { 46650b0e61bSTonghao Zhang hlist_del_rcu(&flow->flow_table.node[ti->node_ver]); 46750b0e61bSTonghao Zhang table->count--; 46850b0e61bSTonghao Zhang 46950b0e61bSTonghao Zhang if (ovs_identifier_is_ufid(&flow->id)) { 47050b0e61bSTonghao Zhang hlist_del_rcu(&flow->ufid_table.node[ufid_ti->node_ver]); 47150b0e61bSTonghao Zhang table->ufid_count--; 47250b0e61bSTonghao Zhang } 47350b0e61bSTonghao Zhang 47450b0e61bSTonghao Zhang flow_mask_remove(table, flow->mask); 47550b0e61bSTonghao Zhang } 47650b0e61bSTonghao Zhang 4771f3a090bSTonghao Zhang /* Must be called with OVS mutex held. */ 4781f3a090bSTonghao Zhang void table_instance_flow_flush(struct flow_table *table, 47950b0e61bSTonghao Zhang struct table_instance *ti, 4801f3a090bSTonghao Zhang struct table_instance *ufid_ti) 481b637e498SPravin B Shelar { 482e80857ccSAndy Zhou int i; 483e80857ccSAndy Zhou 484e80857ccSAndy Zhou for (i = 0; i < ti->n_buckets; i++) { 485ee9c5e67SKent Overstreet struct hlist_head *head = &ti->buckets[i]; 486e80857ccSAndy Zhou struct hlist_node *n; 487cf3266adSTonghao Zhang struct sw_flow *flow; 488e80857ccSAndy Zhou 48950b0e61bSTonghao Zhang hlist_for_each_entry_safe(flow, n, head, 49050b0e61bSTonghao Zhang flow_table.node[ti->node_ver]) { 49150b0e61bSTonghao Zhang 49250b0e61bSTonghao Zhang table_instance_flow_free(table, ti, ufid_ti, 493df68d64eSTonghao Zhang flow); 4941f3a090bSTonghao Zhang ovs_flow_free(flow, true); 4951f3a090bSTonghao Zhang } 496e80857ccSAndy Zhou } 497df68d64eSTonghao Zhang 498df68d64eSTonghao Zhang if (WARN_ON(table->count != 0 || 499df68d64eSTonghao Zhang table->ufid_count != 0)) { 500df68d64eSTonghao Zhang table->count = 0; 501df68d64eSTonghao Zhang table->ufid_count = 0; 502df68d64eSTonghao Zhang } 503e80857ccSAndy Zhou } 504e80857ccSAndy Zhou 5051f3a090bSTonghao Zhang static void table_instance_destroy(struct table_instance *ti, 5061f3a090bSTonghao Zhang struct table_instance *ufid_ti) 5071f3a090bSTonghao Zhang { 508b637e498SPravin B Shelar call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb); 50974ed7ab9SJoe Stringer call_rcu(&ufid_ti->rcu, flow_tbl_destroy_rcu_cb); 510e6445719SPravin B Shelar } 511e6445719SPravin B Shelar 5129b996e54SPravin B Shelar /* No need for locking this function is called from RCU callback or 5139b996e54SPravin B Shelar * error path. 5149b996e54SPravin B Shelar */ 5159b996e54SPravin B Shelar void ovs_flow_tbl_destroy(struct flow_table *table) 516e6445719SPravin B Shelar { 5179b996e54SPravin B Shelar struct table_instance *ti = rcu_dereference_raw(table->ti); 51874ed7ab9SJoe Stringer struct table_instance *ufid_ti = rcu_dereference_raw(table->ufid_ti); 5195845589eSTonghao Zhang struct mask_cache *mc = rcu_dereference_raw(table->mask_cache); 5205845589eSTonghao Zhang struct mask_array *ma = rcu_dereference_raw(table->mask_array); 521e6445719SPravin B Shelar 5229bf24f59SEelco Chaudron call_rcu(&mc->rcu, mask_cache_rcu_cb); 5239bf24f59SEelco Chaudron call_rcu(&ma->rcu, mask_array_rcu_cb); 5241f3a090bSTonghao Zhang table_instance_destroy(ti, ufid_ti); 525e6445719SPravin B Shelar } 526e6445719SPravin B Shelar 527b637e498SPravin B Shelar struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti, 528e6445719SPravin B Shelar u32 *bucket, u32 *last) 529e6445719SPravin B Shelar { 530e6445719SPravin B Shelar struct sw_flow *flow; 531e6445719SPravin B Shelar struct hlist_head *head; 532e6445719SPravin B Shelar int ver; 533e6445719SPravin B Shelar int i; 534e6445719SPravin B Shelar 535b637e498SPravin B Shelar ver = ti->node_ver; 536b637e498SPravin B Shelar while (*bucket < ti->n_buckets) { 537e6445719SPravin B Shelar i = 0; 538ee9c5e67SKent Overstreet head = &ti->buckets[*bucket]; 53974ed7ab9SJoe Stringer hlist_for_each_entry_rcu(flow, head, flow_table.node[ver]) { 540e6445719SPravin B Shelar if (i < *last) { 541e6445719SPravin B Shelar i++; 542e6445719SPravin B Shelar continue; 543e6445719SPravin B Shelar } 544e6445719SPravin B Shelar *last = i + 1; 545e6445719SPravin B Shelar return flow; 546e6445719SPravin B Shelar } 547e6445719SPravin B Shelar (*bucket)++; 548e6445719SPravin B Shelar *last = 0; 549e6445719SPravin B Shelar } 550e6445719SPravin B Shelar 551e6445719SPravin B Shelar return NULL; 552e6445719SPravin B Shelar } 553e6445719SPravin B Shelar 554b637e498SPravin B Shelar static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash) 555e6445719SPravin B Shelar { 556b637e498SPravin B Shelar hash = jhash_1word(hash, ti->hash_seed); 557ee9c5e67SKent Overstreet return &ti->buckets[hash & (ti->n_buckets - 1)]; 558e6445719SPravin B Shelar } 559e6445719SPravin B Shelar 56074ed7ab9SJoe Stringer static void table_instance_insert(struct table_instance *ti, 56174ed7ab9SJoe Stringer struct sw_flow *flow) 562e6445719SPravin B Shelar { 563e6445719SPravin B Shelar struct hlist_head *head; 564e6445719SPravin B Shelar 56574ed7ab9SJoe Stringer head = find_bucket(ti, flow->flow_table.hash); 56674ed7ab9SJoe Stringer hlist_add_head_rcu(&flow->flow_table.node[ti->node_ver], head); 56774ed7ab9SJoe Stringer } 56874ed7ab9SJoe Stringer 56974ed7ab9SJoe Stringer static void ufid_table_instance_insert(struct table_instance *ti, 57074ed7ab9SJoe Stringer struct sw_flow *flow) 57174ed7ab9SJoe Stringer { 57274ed7ab9SJoe Stringer struct hlist_head *head; 57374ed7ab9SJoe Stringer 57474ed7ab9SJoe Stringer head = find_bucket(ti, flow->ufid_table.hash); 57574ed7ab9SJoe Stringer hlist_add_head_rcu(&flow->ufid_table.node[ti->node_ver], head); 576e6445719SPravin B Shelar } 577e6445719SPravin B Shelar 578b637e498SPravin B Shelar static void flow_table_copy_flows(struct table_instance *old, 57974ed7ab9SJoe Stringer struct table_instance *new, bool ufid) 580e6445719SPravin B Shelar { 581e6445719SPravin B Shelar int old_ver; 582e6445719SPravin B Shelar int i; 583e6445719SPravin B Shelar 584e6445719SPravin B Shelar old_ver = old->node_ver; 585e6445719SPravin B Shelar new->node_ver = !old_ver; 586e6445719SPravin B Shelar 587e6445719SPravin B Shelar /* Insert in new table. */ 588e6445719SPravin B Shelar for (i = 0; i < old->n_buckets; i++) { 589e6445719SPravin B Shelar struct sw_flow *flow; 590ee9c5e67SKent Overstreet struct hlist_head *head = &old->buckets[i]; 591e6445719SPravin B Shelar 59274ed7ab9SJoe Stringer if (ufid) 59364948427STonghao Zhang hlist_for_each_entry_rcu(flow, head, 59464948427STonghao Zhang ufid_table.node[old_ver], 59564948427STonghao Zhang lockdep_ovsl_is_held()) 59674ed7ab9SJoe Stringer ufid_table_instance_insert(new, flow); 59774ed7ab9SJoe Stringer else 59864948427STonghao Zhang hlist_for_each_entry_rcu(flow, head, 59964948427STonghao Zhang flow_table.node[old_ver], 60064948427STonghao Zhang lockdep_ovsl_is_held()) 601b637e498SPravin B Shelar table_instance_insert(new, flow); 602e6445719SPravin B Shelar } 603e6445719SPravin B Shelar } 604e6445719SPravin B Shelar 605b637e498SPravin B Shelar static struct table_instance *table_instance_rehash(struct table_instance *ti, 60674ed7ab9SJoe Stringer int n_buckets, bool ufid) 607e6445719SPravin B Shelar { 608b637e498SPravin B Shelar struct table_instance *new_ti; 609e6445719SPravin B Shelar 610b637e498SPravin B Shelar new_ti = table_instance_alloc(n_buckets); 611b637e498SPravin B Shelar if (!new_ti) 612618ed0c8SPravin B Shelar return NULL; 613e6445719SPravin B Shelar 61474ed7ab9SJoe Stringer flow_table_copy_flows(ti, new_ti, ufid); 615e6445719SPravin B Shelar 616b637e498SPravin B Shelar return new_ti; 617e6445719SPravin B Shelar } 618e6445719SPravin B Shelar 619b637e498SPravin B Shelar int ovs_flow_tbl_flush(struct flow_table *flow_table) 620e6445719SPravin B Shelar { 62174ed7ab9SJoe Stringer struct table_instance *old_ti, *new_ti; 62274ed7ab9SJoe Stringer struct table_instance *old_ufid_ti, *new_ufid_ti; 623e6445719SPravin B Shelar 624b637e498SPravin B Shelar new_ti = table_instance_alloc(TBL_MIN_BUCKETS); 625b637e498SPravin B Shelar if (!new_ti) 626b637e498SPravin B Shelar return -ENOMEM; 62774ed7ab9SJoe Stringer new_ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS); 62874ed7ab9SJoe Stringer if (!new_ufid_ti) 62974ed7ab9SJoe Stringer goto err_free_ti; 63074ed7ab9SJoe Stringer 63174ed7ab9SJoe Stringer old_ti = ovsl_dereference(flow_table->ti); 63274ed7ab9SJoe Stringer old_ufid_ti = ovsl_dereference(flow_table->ufid_ti); 633b637e498SPravin B Shelar 634b637e498SPravin B Shelar rcu_assign_pointer(flow_table->ti, new_ti); 63574ed7ab9SJoe Stringer rcu_assign_pointer(flow_table->ufid_ti, new_ufid_ti); 636b637e498SPravin B Shelar flow_table->last_rehash = jiffies; 637b637e498SPravin B Shelar 6381f3a090bSTonghao Zhang table_instance_flow_flush(flow_table, old_ti, old_ufid_ti); 6391f3a090bSTonghao Zhang table_instance_destroy(old_ti, old_ufid_ti); 640b637e498SPravin B Shelar return 0; 64174ed7ab9SJoe Stringer 64274ed7ab9SJoe Stringer err_free_ti: 64374ed7ab9SJoe Stringer __table_instance_destroy(new_ti); 64474ed7ab9SJoe Stringer return -ENOMEM; 645e6445719SPravin B Shelar } 646e6445719SPravin B Shelar 647272c2cf8SJoe Stringer static u32 flow_hash(const struct sw_flow_key *key, 648272c2cf8SJoe Stringer const struct sw_flow_key_range *range) 649e6445719SPravin B Shelar { 650515b65a4STonghao Zhang const u32 *hash_key = (const u32 *)((const u8 *)key + range->start); 651e6445719SPravin B Shelar 652e6445719SPravin B Shelar /* Make sure number of hash bytes are multiple of u32. */ 653515b65a4STonghao Zhang int hash_u32s = range_n_bytes(range) >> 2; 654e6445719SPravin B Shelar 65587545899SDaniel Borkmann return jhash2(hash_key, hash_u32s, 0); 656e6445719SPravin B Shelar } 657e6445719SPravin B Shelar 658e6445719SPravin B Shelar static int flow_key_start(const struct sw_flow_key *key) 659e6445719SPravin B Shelar { 66000a93babSJiri Benc if (key->tun_proto) 661e6445719SPravin B Shelar return 0; 662e6445719SPravin B Shelar else 663e6445719SPravin B Shelar return rounddown(offsetof(struct sw_flow_key, phy), 664e6445719SPravin B Shelar sizeof(long)); 665e6445719SPravin B Shelar } 666e6445719SPravin B Shelar 667e6445719SPravin B Shelar static bool cmp_key(const struct sw_flow_key *key1, 668e6445719SPravin B Shelar const struct sw_flow_key *key2, 669e6445719SPravin B Shelar int key_start, int key_end) 670e6445719SPravin B Shelar { 6717085130bSDaniele Di Proietto const long *cp1 = (const long *)((const u8 *)key1 + key_start); 6727085130bSDaniele Di Proietto const long *cp2 = (const long *)((const u8 *)key2 + key_start); 673e6445719SPravin B Shelar int i; 674e6445719SPravin B Shelar 675e6445719SPravin B Shelar for (i = key_start; i < key_end; i += sizeof(long)) 676b1811447SBaowen Zheng if (*cp1++ ^ *cp2++) 677b1811447SBaowen Zheng return false; 678e6445719SPravin B Shelar 679b1811447SBaowen Zheng return true; 680e6445719SPravin B Shelar } 681e6445719SPravin B Shelar 682e6445719SPravin B Shelar static bool flow_cmp_masked_key(const struct sw_flow *flow, 683e6445719SPravin B Shelar const struct sw_flow_key *key, 684272c2cf8SJoe Stringer const struct sw_flow_key_range *range) 685e6445719SPravin B Shelar { 686272c2cf8SJoe Stringer return cmp_key(&flow->key, key, range->start, range->end); 687e6445719SPravin B Shelar } 688e6445719SPravin B Shelar 68974ed7ab9SJoe Stringer static bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow, 69012eb18f7SThomas Graf const struct sw_flow_match *match) 691e6445719SPravin B Shelar { 692e6445719SPravin B Shelar struct sw_flow_key *key = match->key; 693e6445719SPravin B Shelar int key_start = flow_key_start(key); 694e6445719SPravin B Shelar int key_end = match->range.end; 695e6445719SPravin B Shelar 69674ed7ab9SJoe Stringer BUG_ON(ovs_identifier_is_ufid(&flow->id)); 69774ed7ab9SJoe Stringer return cmp_key(flow->id.unmasked_key, key, key_start, key_end); 698e6445719SPravin B Shelar } 699e6445719SPravin B Shelar 700b637e498SPravin B Shelar static struct sw_flow *masked_flow_lookup(struct table_instance *ti, 701e6445719SPravin B Shelar const struct sw_flow_key *unmasked, 70204b7d136STonghao Zhang const struct sw_flow_mask *mask, 70304b7d136STonghao Zhang u32 *n_mask_hit) 704e6445719SPravin B Shelar { 705e6445719SPravin B Shelar struct sw_flow *flow; 706e6445719SPravin B Shelar struct hlist_head *head; 707e6445719SPravin B Shelar u32 hash; 708e6445719SPravin B Shelar struct sw_flow_key masked_key; 709e6445719SPravin B Shelar 710ae5f2fb1SJesse Gross ovs_flow_mask_key(&masked_key, unmasked, false, mask); 711272c2cf8SJoe Stringer hash = flow_hash(&masked_key, &mask->range); 712b637e498SPravin B Shelar head = find_bucket(ti, hash); 71304b7d136STonghao Zhang (*n_mask_hit)++; 71404b7d136STonghao Zhang 715a2cfb96cSMadhuparna Bhowmik hlist_for_each_entry_rcu(flow, head, flow_table.node[ti->node_ver], 716a2cfb96cSMadhuparna Bhowmik lockdep_ovsl_is_held()) { 71774ed7ab9SJoe Stringer if (flow->mask == mask && flow->flow_table.hash == hash && 718272c2cf8SJoe Stringer flow_cmp_masked_key(flow, &masked_key, &mask->range)) 719e6445719SPravin B Shelar return flow; 720e6445719SPravin B Shelar } 721e6445719SPravin B Shelar return NULL; 722e6445719SPravin B Shelar } 723e6445719SPravin B Shelar 724a7f35e78STonghao Zhang /* Flow lookup does full lookup on flow table. It starts with 725a7f35e78STonghao Zhang * mask from index passed in *index. 726f981fc3dSEelco Chaudron * This function MUST be called with BH disabled due to the use 727f981fc3dSEelco Chaudron * of CPU specific variables. 728a7f35e78STonghao Zhang */ 72904b7d136STonghao Zhang static struct sw_flow *flow_lookup(struct flow_table *tbl, 73004b7d136STonghao Zhang struct table_instance *ti, 7314bc63b1bSTonghao Zhang struct mask_array *ma, 7321bd7116fSAndy Zhou const struct sw_flow_key *key, 7334bc63b1bSTonghao Zhang u32 *n_mask_hit, 7349d2f627bSEelco Chaudron u32 *n_cache_hit, 7354bc63b1bSTonghao Zhang u32 *index) 736e6445719SPravin B Shelar { 737f981fc3dSEelco Chaudron struct mask_array_stats *stats = this_cpu_ptr(ma->masks_usage_stats); 738b637e498SPravin B Shelar struct sw_flow *flow; 73957f7d7b9STonghao Zhang struct sw_flow_mask *mask; 7404bc63b1bSTonghao Zhang int i; 741e6445719SPravin B Shelar 7420a3e0137STonghao Zhang if (likely(*index < ma->max)) { 743a7f35e78STonghao Zhang mask = rcu_dereference_ovsl(ma->masks[*index]); 744a7f35e78STonghao Zhang if (mask) { 745a7f35e78STonghao Zhang flow = masked_flow_lookup(ti, key, mask, n_mask_hit); 746eac87c41SEelco Chaudron if (flow) { 747f981fc3dSEelco Chaudron u64_stats_update_begin(&stats->syncp); 748f981fc3dSEelco Chaudron stats->usage_cntrs[*index]++; 749f981fc3dSEelco Chaudron u64_stats_update_end(&stats->syncp); 7509d2f627bSEelco Chaudron (*n_cache_hit)++; 751a7f35e78STonghao Zhang return flow; 752a7f35e78STonghao Zhang } 753a7f35e78STonghao Zhang } 754eac87c41SEelco Chaudron } 755a7f35e78STonghao Zhang 7564bc63b1bSTonghao Zhang for (i = 0; i < ma->max; i++) { 757a7f35e78STonghao Zhang 758a7f35e78STonghao Zhang if (i == *index) 759a7f35e78STonghao Zhang continue; 7604bc63b1bSTonghao Zhang 7614bc63b1bSTonghao Zhang mask = rcu_dereference_ovsl(ma->masks[i]); 7620a3e0137STonghao Zhang if (unlikely(!mask)) 76357f7d7b9STonghao Zhang break; 764a7f35e78STonghao Zhang 76504b7d136STonghao Zhang flow = masked_flow_lookup(ti, key, mask, n_mask_hit); 7664bc63b1bSTonghao Zhang if (flow) { /* Found */ 7674bc63b1bSTonghao Zhang *index = i; 768f981fc3dSEelco Chaudron u64_stats_update_begin(&stats->syncp); 769f981fc3dSEelco Chaudron stats->usage_cntrs[*index]++; 770f981fc3dSEelco Chaudron u64_stats_update_end(&stats->syncp); 771b637e498SPravin B Shelar return flow; 772b637e498SPravin B Shelar } 7734bc63b1bSTonghao Zhang } 7744bc63b1bSTonghao Zhang 775b637e498SPravin B Shelar return NULL; 776e6445719SPravin B Shelar } 777e6445719SPravin B Shelar 77804b7d136STonghao Zhang /* 77904b7d136STonghao Zhang * mask_cache maps flow to probable mask. This cache is not tightly 78004b7d136STonghao Zhang * coupled cache, It means updates to mask list can result in inconsistent 78104b7d136STonghao Zhang * cache entry in mask cache. 78204b7d136STonghao Zhang * This is per cpu cache and is divided in MC_HASH_SEGS segments. 78304b7d136STonghao Zhang * In case of a hash collision the entry is hashed in next segment. 78404b7d136STonghao Zhang * */ 78504b7d136STonghao Zhang struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl, 78604b7d136STonghao Zhang const struct sw_flow_key *key, 78704b7d136STonghao Zhang u32 skb_hash, 7889d2f627bSEelco Chaudron u32 *n_mask_hit, 7899d2f627bSEelco Chaudron u32 *n_cache_hit) 79004b7d136STonghao Zhang { 7919bf24f59SEelco Chaudron struct mask_cache *mc = rcu_dereference(tbl->mask_cache); 792a7f35e78STonghao Zhang struct mask_array *ma = rcu_dereference(tbl->mask_array); 793a7f35e78STonghao Zhang struct table_instance *ti = rcu_dereference(tbl->ti); 794a7f35e78STonghao Zhang struct mask_cache_entry *entries, *ce; 79504b7d136STonghao Zhang struct sw_flow *flow; 796a7f35e78STonghao Zhang u32 hash; 79704b7d136STonghao Zhang int seg; 79804b7d136STonghao Zhang 79904b7d136STonghao Zhang *n_mask_hit = 0; 8009d2f627bSEelco Chaudron *n_cache_hit = 0; 8019bf24f59SEelco Chaudron if (unlikely(!skb_hash || mc->cache_size == 0)) { 802a7f35e78STonghao Zhang u32 mask_index = 0; 8039d2f627bSEelco Chaudron u32 cache = 0; 8044bc63b1bSTonghao Zhang 8059d2f627bSEelco Chaudron return flow_lookup(tbl, ti, ma, key, n_mask_hit, &cache, 8069d2f627bSEelco Chaudron &mask_index); 8074bc63b1bSTonghao Zhang } 80804b7d136STonghao Zhang 809a7f35e78STonghao Zhang /* Pre and post recirulation flows usually have the same skb_hash 810a7f35e78STonghao Zhang * value. To avoid hash collisions, rehash the 'skb_hash' with 811a7f35e78STonghao Zhang * 'recirc_id'. */ 812a7f35e78STonghao Zhang if (key->recirc_id) 813a7f35e78STonghao Zhang skb_hash = jhash_1word(skb_hash, key->recirc_id); 814a7f35e78STonghao Zhang 815a7f35e78STonghao Zhang ce = NULL; 816a7f35e78STonghao Zhang hash = skb_hash; 8179bf24f59SEelco Chaudron entries = this_cpu_ptr(mc->mask_cache); 81804b7d136STonghao Zhang 819a7f35e78STonghao Zhang /* Find the cache entry 'ce' to operate on. */ 82004b7d136STonghao Zhang for (seg = 0; seg < MC_HASH_SEGS; seg++) { 8219bf24f59SEelco Chaudron int index = hash & (mc->cache_size - 1); 822a7f35e78STonghao Zhang struct mask_cache_entry *e; 82304b7d136STonghao Zhang 824a7f35e78STonghao Zhang e = &entries[index]; 825a7f35e78STonghao Zhang if (e->skb_hash == skb_hash) { 826a7f35e78STonghao Zhang flow = flow_lookup(tbl, ti, ma, key, n_mask_hit, 8279d2f627bSEelco Chaudron n_cache_hit, &e->mask_index); 828a7f35e78STonghao Zhang if (!flow) 829a7f35e78STonghao Zhang e->skb_hash = 0; 83004b7d136STonghao Zhang return flow; 83104b7d136STonghao Zhang } 83204b7d136STonghao Zhang 833a7f35e78STonghao Zhang if (!ce || e->skb_hash < ce->skb_hash) 834a7f35e78STonghao Zhang ce = e; /* A better replacement cache candidate. */ 83504b7d136STonghao Zhang 83604b7d136STonghao Zhang hash >>= MC_HASH_SHIFT; 83704b7d136STonghao Zhang } 83804b7d136STonghao Zhang 839a7f35e78STonghao Zhang /* Cache miss, do full lookup. */ 8409d2f627bSEelco Chaudron flow = flow_lookup(tbl, ti, ma, key, n_mask_hit, n_cache_hit, 8419d2f627bSEelco Chaudron &ce->mask_index); 8424bc63b1bSTonghao Zhang if (flow) 843a7f35e78STonghao Zhang ce->skb_hash = skb_hash; 84404b7d136STonghao Zhang 8459d2f627bSEelco Chaudron *n_cache_hit = 0; 84604b7d136STonghao Zhang return flow; 84704b7d136STonghao Zhang } 84804b7d136STonghao Zhang 8495bb50632SAndy Zhou struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl, 8505bb50632SAndy Zhou const struct sw_flow_key *key) 8515bb50632SAndy Zhou { 85204b7d136STonghao Zhang struct table_instance *ti = rcu_dereference_ovsl(tbl->ti); 8534bc63b1bSTonghao Zhang struct mask_array *ma = rcu_dereference_ovsl(tbl->mask_array); 8544bc63b1bSTonghao Zhang u32 __always_unused n_mask_hit; 8559d2f627bSEelco Chaudron u32 __always_unused n_cache_hit; 856f981fc3dSEelco Chaudron struct sw_flow *flow; 857a7f35e78STonghao Zhang u32 index = 0; 8584bc63b1bSTonghao Zhang 859f981fc3dSEelco Chaudron /* This function gets called trough the netlink interface and therefore 860f981fc3dSEelco Chaudron * is preemptible. However, flow_lookup() function needs to be called 861f981fc3dSEelco Chaudron * with BH disabled due to CPU specific variables. 862f981fc3dSEelco Chaudron */ 863f981fc3dSEelco Chaudron local_bh_disable(); 864f981fc3dSEelco Chaudron flow = flow_lookup(tbl, ti, ma, key, &n_mask_hit, &n_cache_hit, &index); 865f981fc3dSEelco Chaudron local_bh_enable(); 866f981fc3dSEelco Chaudron return flow; 8675bb50632SAndy Zhou } 8685bb50632SAndy Zhou 8694a46b24eSAlex Wang struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl, 87012eb18f7SThomas Graf const struct sw_flow_match *match) 8714a46b24eSAlex Wang { 8724bc63b1bSTonghao Zhang struct mask_array *ma = ovsl_dereference(tbl->mask_array); 8734bc63b1bSTonghao Zhang int i; 8744a46b24eSAlex Wang 8754a46b24eSAlex Wang /* Always called under ovs-mutex. */ 8764bc63b1bSTonghao Zhang for (i = 0; i < ma->max; i++) { 8774bc63b1bSTonghao Zhang struct table_instance *ti = rcu_dereference_ovsl(tbl->ti); 8784bc63b1bSTonghao Zhang u32 __always_unused n_mask_hit; 8794bc63b1bSTonghao Zhang struct sw_flow_mask *mask; 8804bc63b1bSTonghao Zhang struct sw_flow *flow; 8814bc63b1bSTonghao Zhang 8824bc63b1bSTonghao Zhang mask = ovsl_dereference(ma->masks[i]); 8834bc63b1bSTonghao Zhang if (!mask) 8844bc63b1bSTonghao Zhang continue; 8854bc63b1bSTonghao Zhang 88604b7d136STonghao Zhang flow = masked_flow_lookup(ti, match->key, mask, &n_mask_hit); 88774ed7ab9SJoe Stringer if (flow && ovs_identifier_is_key(&flow->id) && 8884bc63b1bSTonghao Zhang ovs_flow_cmp_unmasked_key(flow, match)) { 88974ed7ab9SJoe Stringer return flow; 89074ed7ab9SJoe Stringer } 8914bc63b1bSTonghao Zhang } 8924bc63b1bSTonghao Zhang 89374ed7ab9SJoe Stringer return NULL; 89474ed7ab9SJoe Stringer } 89574ed7ab9SJoe Stringer 89674ed7ab9SJoe Stringer static u32 ufid_hash(const struct sw_flow_id *sfid) 89774ed7ab9SJoe Stringer { 89874ed7ab9SJoe Stringer return jhash(sfid->ufid, sfid->ufid_len, 0); 89974ed7ab9SJoe Stringer } 90074ed7ab9SJoe Stringer 90174ed7ab9SJoe Stringer static bool ovs_flow_cmp_ufid(const struct sw_flow *flow, 90274ed7ab9SJoe Stringer const struct sw_flow_id *sfid) 90374ed7ab9SJoe Stringer { 90474ed7ab9SJoe Stringer if (flow->id.ufid_len != sfid->ufid_len) 90574ed7ab9SJoe Stringer return false; 90674ed7ab9SJoe Stringer 90774ed7ab9SJoe Stringer return !memcmp(flow->id.ufid, sfid->ufid, sfid->ufid_len); 90874ed7ab9SJoe Stringer } 90974ed7ab9SJoe Stringer 910cf3266adSTonghao Zhang bool ovs_flow_cmp(const struct sw_flow *flow, 911cf3266adSTonghao Zhang const struct sw_flow_match *match) 91274ed7ab9SJoe Stringer { 91374ed7ab9SJoe Stringer if (ovs_identifier_is_ufid(&flow->id)) 91474ed7ab9SJoe Stringer return flow_cmp_masked_key(flow, match->key, &match->range); 91574ed7ab9SJoe Stringer 91674ed7ab9SJoe Stringer return ovs_flow_cmp_unmasked_key(flow, match); 91774ed7ab9SJoe Stringer } 91874ed7ab9SJoe Stringer 91974ed7ab9SJoe Stringer struct sw_flow *ovs_flow_tbl_lookup_ufid(struct flow_table *tbl, 92074ed7ab9SJoe Stringer const struct sw_flow_id *ufid) 92174ed7ab9SJoe Stringer { 92274ed7ab9SJoe Stringer struct table_instance *ti = rcu_dereference_ovsl(tbl->ufid_ti); 92374ed7ab9SJoe Stringer struct sw_flow *flow; 92474ed7ab9SJoe Stringer struct hlist_head *head; 92574ed7ab9SJoe Stringer u32 hash; 92674ed7ab9SJoe Stringer 92774ed7ab9SJoe Stringer hash = ufid_hash(ufid); 92874ed7ab9SJoe Stringer head = find_bucket(ti, hash); 929a2cfb96cSMadhuparna Bhowmik hlist_for_each_entry_rcu(flow, head, ufid_table.node[ti->node_ver], 930a2cfb96cSMadhuparna Bhowmik lockdep_ovsl_is_held()) { 93174ed7ab9SJoe Stringer if (flow->ufid_table.hash == hash && 93274ed7ab9SJoe Stringer ovs_flow_cmp_ufid(flow, ufid)) 9334a46b24eSAlex Wang return flow; 9344a46b24eSAlex Wang } 9354a46b24eSAlex Wang return NULL; 9364a46b24eSAlex Wang } 9374a46b24eSAlex Wang 9381bd7116fSAndy Zhou int ovs_flow_tbl_num_masks(const struct flow_table *table) 9391bd7116fSAndy Zhou { 9404bc63b1bSTonghao Zhang struct mask_array *ma = rcu_dereference_ovsl(table->mask_array); 94157f7d7b9STonghao Zhang return READ_ONCE(ma->count); 9421bd7116fSAndy Zhou } 9431bd7116fSAndy Zhou 9449bf24f59SEelco Chaudron u32 ovs_flow_tbl_masks_cache_size(const struct flow_table *table) 9459bf24f59SEelco Chaudron { 9465845589eSTonghao Zhang struct mask_cache *mc = rcu_dereference_ovsl(table->mask_cache); 9479bf24f59SEelco Chaudron 9489bf24f59SEelco Chaudron return READ_ONCE(mc->cache_size); 9499bf24f59SEelco Chaudron } 9509bf24f59SEelco Chaudron 95174ed7ab9SJoe Stringer static struct table_instance *table_instance_expand(struct table_instance *ti, 95274ed7ab9SJoe Stringer bool ufid) 953b637e498SPravin B Shelar { 95474ed7ab9SJoe Stringer return table_instance_rehash(ti, ti->n_buckets * 2, ufid); 955e6445719SPravin B Shelar } 956e6445719SPravin B Shelar 95756c19868SJarno Rajahalme /* Must be called with OVS mutex held. */ 958e6445719SPravin B Shelar void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow) 959e6445719SPravin B Shelar { 960b637e498SPravin B Shelar struct table_instance *ti = ovsl_dereference(table->ti); 96174ed7ab9SJoe Stringer struct table_instance *ufid_ti = ovsl_dereference(table->ufid_ti); 962b637e498SPravin B Shelar 963e6445719SPravin B Shelar BUG_ON(table->count == 0); 964df68d64eSTonghao Zhang table_instance_flow_free(table, ti, ufid_ti, flow); 965e6445719SPravin B Shelar } 966e6445719SPravin B Shelar 967618ed0c8SPravin B Shelar static struct sw_flow_mask *mask_alloc(void) 968e6445719SPravin B Shelar { 969e6445719SPravin B Shelar struct sw_flow_mask *mask; 970e6445719SPravin B Shelar 971e6445719SPravin B Shelar mask = kmalloc(sizeof(*mask), GFP_KERNEL); 972e6445719SPravin B Shelar if (mask) 973e80857ccSAndy Zhou mask->ref_count = 1; 974e6445719SPravin B Shelar 975e6445719SPravin B Shelar return mask; 976e6445719SPravin B Shelar } 977e6445719SPravin B Shelar 978e6445719SPravin B Shelar static bool mask_equal(const struct sw_flow_mask *a, 979e6445719SPravin B Shelar const struct sw_flow_mask *b) 980e6445719SPravin B Shelar { 9817085130bSDaniele Di Proietto const u8 *a_ = (const u8 *)&a->key + a->range.start; 9827085130bSDaniele Di Proietto const u8 *b_ = (const u8 *)&b->key + b->range.start; 983e6445719SPravin B Shelar 984e6445719SPravin B Shelar return (a->range.end == b->range.end) 985e6445719SPravin B Shelar && (a->range.start == b->range.start) 986e6445719SPravin B Shelar && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0); 987e6445719SPravin B Shelar } 988e6445719SPravin B Shelar 989618ed0c8SPravin B Shelar static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl, 990e6445719SPravin B Shelar const struct sw_flow_mask *mask) 991e6445719SPravin B Shelar { 9924bc63b1bSTonghao Zhang struct mask_array *ma; 9934bc63b1bSTonghao Zhang int i; 994e6445719SPravin B Shelar 9954bc63b1bSTonghao Zhang ma = ovsl_dereference(tbl->mask_array); 9964bc63b1bSTonghao Zhang for (i = 0; i < ma->max; i++) { 9974bc63b1bSTonghao Zhang struct sw_flow_mask *t; 9984bc63b1bSTonghao Zhang t = ovsl_dereference(ma->masks[i]); 9994bc63b1bSTonghao Zhang 10004bc63b1bSTonghao Zhang if (t && mask_equal(mask, t)) 10014bc63b1bSTonghao Zhang return t; 1002e6445719SPravin B Shelar } 1003e6445719SPravin B Shelar 1004e6445719SPravin B Shelar return NULL; 1005e6445719SPravin B Shelar } 1006e6445719SPravin B Shelar 1007d1211908SBen Pfaff /* Add 'mask' into the mask list, if it is not already there. */ 1008618ed0c8SPravin B Shelar static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow, 100912eb18f7SThomas Graf const struct sw_flow_mask *new) 1010e6445719SPravin B Shelar { 1011618ed0c8SPravin B Shelar struct sw_flow_mask *mask; 10124bc63b1bSTonghao Zhang 1013618ed0c8SPravin B Shelar mask = flow_mask_find(tbl, new); 1014618ed0c8SPravin B Shelar if (!mask) { 1015618ed0c8SPravin B Shelar /* Allocate a new mask if none exsits. */ 1016618ed0c8SPravin B Shelar mask = mask_alloc(); 1017618ed0c8SPravin B Shelar if (!mask) 1018618ed0c8SPravin B Shelar return -ENOMEM; 1019618ed0c8SPravin B Shelar mask->key = new->key; 1020618ed0c8SPravin B Shelar mask->range = new->range; 10214bc63b1bSTonghao Zhang 10224bc63b1bSTonghao Zhang /* Add mask to mask-list. */ 102357f7d7b9STonghao Zhang if (tbl_mask_array_add_mask(tbl, mask)) { 10244bc63b1bSTonghao Zhang kfree(mask); 102557f7d7b9STonghao Zhang return -ENOMEM; 10264bc63b1bSTonghao Zhang } 1027e80857ccSAndy Zhou } else { 1028e80857ccSAndy Zhou BUG_ON(!mask->ref_count); 1029e80857ccSAndy Zhou mask->ref_count++; 1030e6445719SPravin B Shelar } 1031e6445719SPravin B Shelar 1032618ed0c8SPravin B Shelar flow->mask = mask; 1033618ed0c8SPravin B Shelar return 0; 1034618ed0c8SPravin B Shelar } 1035618ed0c8SPravin B Shelar 103656c19868SJarno Rajahalme /* Must be called with OVS mutex held. */ 1037d29ab6f8SJoe Stringer static void flow_key_insert(struct flow_table *table, struct sw_flow *flow) 1038618ed0c8SPravin B Shelar { 1039618ed0c8SPravin B Shelar struct table_instance *new_ti = NULL; 1040618ed0c8SPravin B Shelar struct table_instance *ti; 1041618ed0c8SPravin B Shelar 104274ed7ab9SJoe Stringer flow->flow_table.hash = flow_hash(&flow->key, &flow->mask->range); 1043618ed0c8SPravin B Shelar ti = ovsl_dereference(table->ti); 1044618ed0c8SPravin B Shelar table_instance_insert(ti, flow); 1045618ed0c8SPravin B Shelar table->count++; 1046618ed0c8SPravin B Shelar 1047618ed0c8SPravin B Shelar /* Expand table, if necessary, to make room. */ 1048618ed0c8SPravin B Shelar if (table->count > ti->n_buckets) 104974ed7ab9SJoe Stringer new_ti = table_instance_expand(ti, false); 1050618ed0c8SPravin B Shelar else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL)) 105174ed7ab9SJoe Stringer new_ti = table_instance_rehash(ti, ti->n_buckets, false); 1052618ed0c8SPravin B Shelar 1053618ed0c8SPravin B Shelar if (new_ti) { 1054618ed0c8SPravin B Shelar rcu_assign_pointer(table->ti, new_ti); 105574ed7ab9SJoe Stringer call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb); 1056618ed0c8SPravin B Shelar table->last_rehash = jiffies; 1057618ed0c8SPravin B Shelar } 1058d29ab6f8SJoe Stringer } 1059d29ab6f8SJoe Stringer 1060d29ab6f8SJoe Stringer /* Must be called with OVS mutex held. */ 106174ed7ab9SJoe Stringer static void flow_ufid_insert(struct flow_table *table, struct sw_flow *flow) 106274ed7ab9SJoe Stringer { 106374ed7ab9SJoe Stringer struct table_instance *ti; 106474ed7ab9SJoe Stringer 106574ed7ab9SJoe Stringer flow->ufid_table.hash = ufid_hash(&flow->id); 106674ed7ab9SJoe Stringer ti = ovsl_dereference(table->ufid_ti); 106774ed7ab9SJoe Stringer ufid_table_instance_insert(ti, flow); 106874ed7ab9SJoe Stringer table->ufid_count++; 106974ed7ab9SJoe Stringer 107074ed7ab9SJoe Stringer /* Expand table, if necessary, to make room. */ 107174ed7ab9SJoe Stringer if (table->ufid_count > ti->n_buckets) { 107274ed7ab9SJoe Stringer struct table_instance *new_ti; 107374ed7ab9SJoe Stringer 107474ed7ab9SJoe Stringer new_ti = table_instance_expand(ti, true); 107574ed7ab9SJoe Stringer if (new_ti) { 107674ed7ab9SJoe Stringer rcu_assign_pointer(table->ufid_ti, new_ti); 107774ed7ab9SJoe Stringer call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb); 107874ed7ab9SJoe Stringer } 107974ed7ab9SJoe Stringer } 108074ed7ab9SJoe Stringer } 108174ed7ab9SJoe Stringer 108274ed7ab9SJoe Stringer /* Must be called with OVS mutex held. */ 1083d29ab6f8SJoe Stringer int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow, 1084d29ab6f8SJoe Stringer const struct sw_flow_mask *mask) 1085d29ab6f8SJoe Stringer { 1086d29ab6f8SJoe Stringer int err; 1087d29ab6f8SJoe Stringer 1088d29ab6f8SJoe Stringer err = flow_mask_insert(table, flow, mask); 1089d29ab6f8SJoe Stringer if (err) 1090d29ab6f8SJoe Stringer return err; 1091d29ab6f8SJoe Stringer flow_key_insert(table, flow); 109274ed7ab9SJoe Stringer if (ovs_identifier_is_ufid(&flow->id)) 109374ed7ab9SJoe Stringer flow_ufid_insert(table, flow); 1094d29ab6f8SJoe Stringer 1095618ed0c8SPravin B Shelar return 0; 1096618ed0c8SPravin B Shelar } 1097618ed0c8SPravin B Shelar 1098eac87c41SEelco Chaudron static int compare_mask_and_count(const void *a, const void *b) 1099eac87c41SEelco Chaudron { 1100eac87c41SEelco Chaudron const struct mask_count *mc_a = a; 1101eac87c41SEelco Chaudron const struct mask_count *mc_b = b; 1102eac87c41SEelco Chaudron 1103eac87c41SEelco Chaudron return (s64)mc_b->counter - (s64)mc_a->counter; 1104eac87c41SEelco Chaudron } 1105eac87c41SEelco Chaudron 1106eac87c41SEelco Chaudron /* Must be called with OVS mutex held. */ 1107eac87c41SEelco Chaudron void ovs_flow_masks_rebalance(struct flow_table *table) 1108eac87c41SEelco Chaudron { 1109eac87c41SEelco Chaudron struct mask_array *ma = rcu_dereference_ovsl(table->mask_array); 1110eac87c41SEelco Chaudron struct mask_count *masks_and_count; 1111eac87c41SEelco Chaudron struct mask_array *new; 1112eac87c41SEelco Chaudron int masks_entries = 0; 1113eac87c41SEelco Chaudron int i; 1114eac87c41SEelco Chaudron 1115eac87c41SEelco Chaudron /* Build array of all current entries with use counters. */ 1116eac87c41SEelco Chaudron masks_and_count = kmalloc_array(ma->max, sizeof(*masks_and_count), 1117eac87c41SEelco Chaudron GFP_KERNEL); 1118eac87c41SEelco Chaudron if (!masks_and_count) 1119eac87c41SEelco Chaudron return; 1120eac87c41SEelco Chaudron 1121eac87c41SEelco Chaudron for (i = 0; i < ma->max; i++) { 1122eac87c41SEelco Chaudron struct sw_flow_mask *mask; 1123eac87c41SEelco Chaudron int cpu; 1124eac87c41SEelco Chaudron 1125eac87c41SEelco Chaudron mask = rcu_dereference_ovsl(ma->masks[i]); 1126eac87c41SEelco Chaudron if (unlikely(!mask)) 1127eac87c41SEelco Chaudron break; 1128eac87c41SEelco Chaudron 1129eac87c41SEelco Chaudron masks_and_count[i].index = i; 1130eac87c41SEelco Chaudron masks_and_count[i].counter = 0; 1131eac87c41SEelco Chaudron 1132eac87c41SEelco Chaudron for_each_possible_cpu(cpu) { 1133f981fc3dSEelco Chaudron struct mask_array_stats *stats; 1134f981fc3dSEelco Chaudron unsigned int start; 1135eac87c41SEelco Chaudron u64 counter; 1136eac87c41SEelco Chaudron 1137f981fc3dSEelco Chaudron stats = per_cpu_ptr(ma->masks_usage_stats, cpu); 1138eac87c41SEelco Chaudron do { 1139*d120d1a6SThomas Gleixner start = u64_stats_fetch_begin(&stats->syncp); 1140f981fc3dSEelco Chaudron counter = stats->usage_cntrs[i]; 1141*d120d1a6SThomas Gleixner } while (u64_stats_fetch_retry(&stats->syncp, start)); 1142eac87c41SEelco Chaudron 1143eac87c41SEelco Chaudron masks_and_count[i].counter += counter; 1144eac87c41SEelco Chaudron } 1145eac87c41SEelco Chaudron 1146eac87c41SEelco Chaudron /* Subtract the zero count value. */ 1147eac87c41SEelco Chaudron masks_and_count[i].counter -= ma->masks_usage_zero_cntr[i]; 1148eac87c41SEelco Chaudron 1149eac87c41SEelco Chaudron /* Rather than calling tbl_mask_array_reset_counters() 1150eac87c41SEelco Chaudron * below when no change is needed, do it inline here. 1151eac87c41SEelco Chaudron */ 1152eac87c41SEelco Chaudron ma->masks_usage_zero_cntr[i] += masks_and_count[i].counter; 1153eac87c41SEelco Chaudron } 1154eac87c41SEelco Chaudron 1155eac87c41SEelco Chaudron if (i == 0) 1156eac87c41SEelco Chaudron goto free_mask_entries; 1157eac87c41SEelco Chaudron 1158eac87c41SEelco Chaudron /* Sort the entries */ 1159eac87c41SEelco Chaudron masks_entries = i; 1160eac87c41SEelco Chaudron sort(masks_and_count, masks_entries, sizeof(*masks_and_count), 1161eac87c41SEelco Chaudron compare_mask_and_count, NULL); 1162eac87c41SEelco Chaudron 1163eac87c41SEelco Chaudron /* If the order is the same, nothing to do... */ 1164eac87c41SEelco Chaudron for (i = 0; i < masks_entries; i++) { 1165eac87c41SEelco Chaudron if (i != masks_and_count[i].index) 1166eac87c41SEelco Chaudron break; 1167eac87c41SEelco Chaudron } 1168eac87c41SEelco Chaudron if (i == masks_entries) 1169eac87c41SEelco Chaudron goto free_mask_entries; 1170eac87c41SEelco Chaudron 1171eac87c41SEelco Chaudron /* Rebuilt the new list in order of usage. */ 1172eac87c41SEelco Chaudron new = tbl_mask_array_alloc(ma->max); 1173eac87c41SEelco Chaudron if (!new) 1174eac87c41SEelco Chaudron goto free_mask_entries; 1175eac87c41SEelco Chaudron 1176eac87c41SEelco Chaudron for (i = 0; i < masks_entries; i++) { 1177eac87c41SEelco Chaudron int index = masks_and_count[i].index; 1178eac87c41SEelco Chaudron 11799bf24f59SEelco Chaudron if (ovsl_dereference(ma->masks[index])) 11809bf24f59SEelco Chaudron new->masks[new->count++] = ma->masks[index]; 1181eac87c41SEelco Chaudron } 1182eac87c41SEelco Chaudron 1183eac87c41SEelco Chaudron rcu_assign_pointer(table->mask_array, new); 1184eac87c41SEelco Chaudron call_rcu(&ma->rcu, mask_array_rcu_cb); 1185eac87c41SEelco Chaudron 1186eac87c41SEelco Chaudron free_mask_entries: 1187eac87c41SEelco Chaudron kfree(masks_and_count); 1188eac87c41SEelco Chaudron } 1189eac87c41SEelco Chaudron 1190e6445719SPravin B Shelar /* Initializes the flow module. 1191e6445719SPravin B Shelar * Returns zero if successful or a negative error code. */ 1192e6445719SPravin B Shelar int ovs_flow_init(void) 1193e6445719SPravin B Shelar { 1194e6445719SPravin B Shelar BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long)); 1195e6445719SPravin B Shelar BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long)); 1196e6445719SPravin B Shelar 119763e7959cSJarno Rajahalme flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow) 1198db74a333SThadeu Lima de Souza Cascardo + (nr_cpu_ids 1199aef833c5SPablo Neira Ayuso * sizeof(struct sw_flow_stats *)), 120063e7959cSJarno Rajahalme 0, 0, NULL); 1201e6445719SPravin B Shelar if (flow_cache == NULL) 1202e6445719SPravin B Shelar return -ENOMEM; 1203e6445719SPravin B Shelar 120463e7959cSJarno Rajahalme flow_stats_cache 1205aef833c5SPablo Neira Ayuso = kmem_cache_create("sw_flow_stats", sizeof(struct sw_flow_stats), 120663e7959cSJarno Rajahalme 0, SLAB_HWCACHE_ALIGN, NULL); 120763e7959cSJarno Rajahalme if (flow_stats_cache == NULL) { 120863e7959cSJarno Rajahalme kmem_cache_destroy(flow_cache); 120963e7959cSJarno Rajahalme flow_cache = NULL; 121063e7959cSJarno Rajahalme return -ENOMEM; 121163e7959cSJarno Rajahalme } 121263e7959cSJarno Rajahalme 1213e6445719SPravin B Shelar return 0; 1214e6445719SPravin B Shelar } 1215e6445719SPravin B Shelar 1216e6445719SPravin B Shelar /* Uninitializes the flow module. */ 1217e6445719SPravin B Shelar void ovs_flow_exit(void) 1218e6445719SPravin B Shelar { 121963e7959cSJarno Rajahalme kmem_cache_destroy(flow_stats_cache); 1220e6445719SPravin B Shelar kmem_cache_destroy(flow_cache); 1221e6445719SPravin B Shelar } 1222