1217 lines · 29.2 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-only/* * Copyright (c) 2007-2014 Nicira, Inc. */ #include "flow.h"#include "datapath.h"#include "flow_netlink.h"#include <linux/uaccess.h>#include <linux/netdevice.h>#include <linux/etherdevice.h>#include <linux/if_ether.h>#include <linux/if_vlan.h>#include <net/llc_pdu.h>#include <linux/kernel.h>#include <linux/jhash.h>#include <linux/jiffies.h>#include <linux/llc.h>#include <linux/module.h>#include <linux/in.h>#include <linux/rcupdate.h>#include <linux/cpumask.h>#include <linux/if_arp.h>#include <linux/ip.h>#include <linux/ipv6.h>#include <linux/sctp.h>#include <linux/tcp.h>#include <linux/udp.h>#include <linux/icmp.h>#include <linux/icmpv6.h>#include <linux/rculist.h>#include <linux/sort.h>#include <net/ip.h>#include <net/ipv6.h>#include <net/ndisc.h> #define TBL_MIN_BUCKETS		1024#define MASK_ARRAY_SIZE_MIN	16#define REHASH_INTERVAL		(10 * 60 * HZ) #define MC_DEFAULT_HASH_ENTRIES	256#define MC_HASH_SHIFT		8#define MC_HASH_SEGS		((sizeof(uint32_t) * 8) / MC_HASH_SHIFT) static struct kmem_cache *flow_cache;struct kmem_cache *flow_stats_cache __read_mostly; static u16 range_n_bytes(const struct sw_flow_key_range *range){	return range->end - range->start;} void ovs_flow_mask_key(struct sw_flow_key *dst, const struct sw_flow_key *src,		       bool full, const struct sw_flow_mask *mask){	int start = full ? 0 : mask->range.start;	int len = full ? sizeof *dst : range_n_bytes(&mask->range);	const long *m = (const long *)((const u8 *)&mask->key + start);	const long *s = (const long *)((const u8 *)src + start);	long *d = (long *)((u8 *)dst + start);	int i; 	/* If 'full' is true then all of 'dst' is fully initialized. Otherwise,	 * if 'full' is false the memory outside of the 'mask->range' is left	 * uninitialized. This can be used as an optimization when further	 * operations on 'dst' only use contents within 'mask->range'.	 */	for (i = 0; i < len; i += sizeof(long))		*d++ = *s++ & *m++;} struct sw_flow *ovs_flow_alloc(void){	struct sw_flow *flow;	struct sw_flow_stats *stats; 	flow = kmem_cache_zalloc(flow_cache, GFP_KERNEL);	if (!flow)		return ERR_PTR(-ENOMEM); 	flow->stats_last_writer = -1;	flow->cpu_used_mask = (struct cpumask *)&flow->stats[nr_cpu_ids]; 	/* Initialize the default stat node. */	stats = kmem_cache_alloc_node(flow_stats_cache,				      GFP_KERNEL | __GFP_ZERO,				      node_online(0) ? 0 : NUMA_NO_NODE);	if (!stats)		goto err; 	spin_lock_init(&stats->lock); 	RCU_INIT_POINTER(flow->stats[0], stats); 	cpumask_set_cpu(0, flow->cpu_used_mask); 	return flow;err:	kmem_cache_free(flow_cache, flow);	return ERR_PTR(-ENOMEM);} int ovs_flow_tbl_count(const struct flow_table *table){	return table->count;} static void flow_free(struct sw_flow *flow){	unsigned int cpu; 	if (ovs_identifier_is_key(&flow->id))		kfree(flow->id.unmasked_key);	if (flow->sf_acts)		ovs_nla_free_flow_actions((struct sw_flow_actions __force *)					  flow->sf_acts); 	for_each_cpu(cpu, flow->cpu_used_mask) {		if (flow->stats[cpu])			kmem_cache_free(flow_stats_cache,					(struct sw_flow_stats __force *)flow->stats[cpu]);	} 	kmem_cache_free(flow_cache, flow);} static void rcu_free_flow_callback(struct rcu_head *rcu){	struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu); 	flow_free(flow);} void ovs_flow_free(struct sw_flow *flow, bool deferred){	if (!flow)		return; 	if (deferred)		call_rcu(&flow->rcu, rcu_free_flow_callback);	else		flow_free(flow);} static void __table_instance_destroy(struct table_instance *ti){	kvfree(ti->buckets);	kfree(ti);} static struct table_instance *table_instance_alloc(int new_size){	struct table_instance *ti = kmalloc_obj(*ti);	int i; 	if (!ti)		return NULL; 	ti->buckets = kvmalloc_objs(struct hlist_head, new_size);	if (!ti->buckets) {		kfree(ti);		return NULL;	} 	for (i = 0; i < new_size; i++)		INIT_HLIST_HEAD(&ti->buckets[i]); 	ti->n_buckets = new_size;	ti->node_ver = 0;	get_random_bytes(&ti->hash_seed, sizeof(u32)); 	return ti;} static void __mask_array_destroy(struct mask_array *ma){	free_percpu(ma->masks_usage_stats);	kfree(ma);} static void mask_array_rcu_cb(struct rcu_head *rcu){	struct mask_array *ma = container_of(rcu, struct mask_array, rcu); 	__mask_array_destroy(ma);} static void tbl_mask_array_reset_counters(struct mask_array *ma){	int i, cpu; 	/* As the per CPU counters are not atomic we can not go ahead and	 * reset them from another CPU. To be able to still have an approximate	 * zero based counter we store the value at reset, and subtract it	 * later when processing.	 */	for (i = 0; i < ma->max; i++) {		ma->masks_usage_zero_cntr[i] = 0; 		for_each_possible_cpu(cpu) {			struct mask_array_stats *stats;			unsigned int start;			u64 counter; 			stats = per_cpu_ptr(ma->masks_usage_stats, cpu);			do {				start = u64_stats_fetch_begin(&stats->syncp);				counter = stats->usage_cntrs[i];			} while (u64_stats_fetch_retry(&stats->syncp, start)); 			ma->masks_usage_zero_cntr[i] += counter;		}	}} static struct mask_array *tbl_mask_array_alloc(int size){	struct mask_array *new; 	size = max(MASK_ARRAY_SIZE_MIN, size);	new = kzalloc(struct_size(new, masks, size) +		      sizeof(u64) * size, GFP_KERNEL);	if (!new)		return NULL; 	new->masks_usage_zero_cntr = (u64 *)((u8 *)new +					     struct_size(new, masks, size)); 	new->masks_usage_stats = __alloc_percpu(sizeof(struct mask_array_stats) +						sizeof(u64) * size,						__alignof__(u64));	if (!new->masks_usage_stats) {		kfree(new);		return NULL;	} 	new->count = 0;	new->max = size; 	return new;} static int tbl_mask_array_realloc(struct flow_table *tbl, int size){	struct mask_array *old;	struct mask_array *new; 	new = tbl_mask_array_alloc(size);	if (!new)		return -ENOMEM; 	old = ovsl_dereference(tbl->mask_array);	if (old) {		int i; 		for (i = 0; i < old->max; i++) {			if (ovsl_dereference(old->masks[i]))				new->masks[new->count++] = old->masks[i];		}		call_rcu(&old->rcu, mask_array_rcu_cb);	} 	rcu_assign_pointer(tbl->mask_array, new); 	return 0;} static int tbl_mask_array_add_mask(struct flow_table *tbl,				   struct sw_flow_mask *new){	struct mask_array *ma = ovsl_dereference(tbl->mask_array);	int err, ma_count = READ_ONCE(ma->count); 	if (ma_count >= ma->max) {		err = tbl_mask_array_realloc(tbl, ma->max +						  MASK_ARRAY_SIZE_MIN);		if (err)			return err; 		ma = ovsl_dereference(tbl->mask_array);	} else {		/* On every add or delete we need to reset the counters so		 * every new mask gets a fair chance of being prioritized.		 */		tbl_mask_array_reset_counters(ma);	} 	BUG_ON(ovsl_dereference(ma->masks[ma_count])); 	rcu_assign_pointer(ma->masks[ma_count], new);	WRITE_ONCE(ma->count, ma_count + 1); 	return 0;} static void tbl_mask_array_del_mask(struct flow_table *tbl,				    struct sw_flow_mask *mask){	struct mask_array *ma = ovsl_dereference(tbl->mask_array);	int i, ma_count = READ_ONCE(ma->count); 	/* Remove the deleted mask pointers from the array */	for (i = 0; i < ma_count; i++) {		if (mask == ovsl_dereference(ma->masks[i]))			goto found;	} 	BUG();	return; found:	WRITE_ONCE(ma->count, ma_count - 1); 	rcu_assign_pointer(ma->masks[i], ma->masks[ma_count - 1]);	RCU_INIT_POINTER(ma->masks[ma_count - 1], NULL); 	kfree_rcu(mask, rcu); 	/* Shrink the mask array if necessary. */	if (ma->max >= (MASK_ARRAY_SIZE_MIN * 2) &&	    ma_count <= (ma->max / 3))		tbl_mask_array_realloc(tbl, ma->max / 2);	else		tbl_mask_array_reset_counters(ma); } /* Remove 'mask' from the mask list, if it is not needed any more. */static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask){	if (mask) {		/* ovs-lock is required to protect mask-refcount and		 * mask list.		 */		ASSERT_OVSL();		BUG_ON(!mask->ref_count);		mask->ref_count--; 		if (!mask->ref_count)			tbl_mask_array_del_mask(tbl, mask);	}} static void __mask_cache_destroy(struct mask_cache *mc){	free_percpu(mc->mask_cache);	kfree(mc);} static void mask_cache_rcu_cb(struct rcu_head *rcu){	struct mask_cache *mc = container_of(rcu, struct mask_cache, rcu); 	__mask_cache_destroy(mc);} static struct mask_cache *tbl_mask_cache_alloc(u32 size){	struct mask_cache_entry __percpu *cache = NULL;	struct mask_cache *new; 	/* Only allow size to be 0, or a power of 2, and does not exceed	 * percpu allocation size.	 */	if ((!is_power_of_2(size) && size != 0) ||	    (size * sizeof(struct mask_cache_entry)) > PCPU_MIN_UNIT_SIZE)		return NULL; 	new = kzalloc_obj(*new);	if (!new)		return NULL; 	new->cache_size = size;	if (new->cache_size > 0) {		cache = __alloc_percpu(array_size(sizeof(struct mask_cache_entry),						  new->cache_size),				       __alignof__(struct mask_cache_entry));		if (!cache) {			kfree(new);			return NULL;		}	} 	new->mask_cache = cache;	return new;}int ovs_flow_tbl_masks_cache_resize(struct flow_table *table, u32 size){	struct mask_cache *mc = rcu_dereference_ovsl(table->mask_cache);	struct mask_cache *new; 	if (size == mc->cache_size)		return 0; 	if ((!is_power_of_2(size) && size != 0) ||	    (size * sizeof(struct mask_cache_entry)) > PCPU_MIN_UNIT_SIZE)		return -EINVAL; 	new = tbl_mask_cache_alloc(size);	if (!new)		return -ENOMEM; 	rcu_assign_pointer(table->mask_cache, new);	call_rcu(&mc->rcu, mask_cache_rcu_cb); 	return 0;} int ovs_flow_tbl_init(struct flow_table *table){	struct table_instance *ti, *ufid_ti;	struct mask_cache *mc;	struct mask_array *ma; 	mc = tbl_mask_cache_alloc(MC_DEFAULT_HASH_ENTRIES);	if (!mc)		return -ENOMEM; 	ma = tbl_mask_array_alloc(MASK_ARRAY_SIZE_MIN);	if (!ma)		goto free_mask_cache; 	ti = table_instance_alloc(TBL_MIN_BUCKETS);	if (!ti)		goto free_mask_array; 	ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS);	if (!ufid_ti)		goto free_ti; 	rcu_assign_pointer(table->ti, ti);	rcu_assign_pointer(table->ufid_ti, ufid_ti);	rcu_assign_pointer(table->mask_array, ma);	rcu_assign_pointer(table->mask_cache, mc);	table->last_rehash = jiffies;	table->count = 0;	table->ufid_count = 0;	return 0; free_ti:	__table_instance_destroy(ti);free_mask_array:	__mask_array_destroy(ma);free_mask_cache:	__mask_cache_destroy(mc);	return -ENOMEM;} static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu){	struct table_instance *ti; 	ti = container_of(rcu, struct table_instance, rcu);	__table_instance_destroy(ti);} static void table_instance_flow_free(struct flow_table *table,				     struct table_instance *ti,				     struct table_instance *ufid_ti,				     struct sw_flow *flow){	hlist_del_rcu(&flow->flow_table.node[ti->node_ver]);	table->count--; 	if (ovs_identifier_is_ufid(&flow->id)) {		hlist_del_rcu(&flow->ufid_table.node[ufid_ti->node_ver]);		table->ufid_count--;	} 	flow_mask_remove(table, flow->mask);} /* Must be called with OVS mutex held. */void table_instance_flow_flush(struct flow_table *table,			       struct table_instance *ti,			       struct table_instance *ufid_ti){	int i; 	for (i = 0; i < ti->n_buckets; i++) {		struct hlist_head *head = &ti->buckets[i];		struct hlist_node *n;		struct sw_flow *flow; 		hlist_for_each_entry_safe(flow, n, head,					  flow_table.node[ti->node_ver]) { 			table_instance_flow_free(table, ti, ufid_ti,						 flow);			ovs_flow_free(flow, true);		}	} 	if (WARN_ON(table->count != 0 ||		    table->ufid_count != 0)) {		table->count = 0;		table->ufid_count = 0;	}} 

Lines 1–500 of 1217.