void ovs_flow_mask_key(struct sw_flow_key *dst, conststruct sw_flow_key *src, bool full, conststruct sw_flow_mask *mask)
{ int start = full ? 0 : mask->range.start; int len = full ? sizeof *dst : range_n_bytes(&mask->range); constlong *m = (constlong *)((const u8 *)&mask->key + start); constlong *s = (constlong *)((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++;
}
int ovs_flow_tbl_count(conststruct flow_table *table)
{ return table->count;
}
staticvoid flow_free(struct sw_flow *flow)
{ 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); /* We open code this to make sure cpu 0 is always considered */ for (cpu = 0; cpu < nr_cpu_ids;
cpu = cpumask_next(cpu, flow->cpu_used_mask)) { if (flow->stats[cpu])
kmem_cache_free(flow_stats_cache,
(struct sw_flow_stats __force *)flow->stats[cpu]);
}
staticvoid 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;
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);
}
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);
}
/* 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. */ staticvoid 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);
}
}
/* 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(sizeof(*new), GFP_KERNEL); if (!new) return NULL;
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]) {
/* No need for locking this function is called from RCU callback or * error path.
*/ void ovs_flow_tbl_destroy(struct flow_table *table)
{ struct table_instance *ti = rcu_dereference_raw(table->ti); struct table_instance *ufid_ti = rcu_dereference_raw(table->ufid_ti); struct mask_cache *mc = rcu_dereference_raw(table->mask_cache); struct mask_array *ma = rcu_dereference_raw(table->mask_array);
/* Flow lookup does full lookup on flow table. It starts with * mask from index passed in *index. * This function MUST be called with BH disabled due to the use * of CPU specific variables.
*/ staticstruct sw_flow *flow_lookup(struct flow_table *tbl, struct table_instance *ti, struct mask_array *ma, conststruct sw_flow_key *key,
u32 *n_mask_hit,
u32 *n_cache_hit,
u32 *index)
{ struct mask_array_stats *stats = this_cpu_ptr(ma->masks_usage_stats); struct sw_flow *flow; struct sw_flow_mask *mask; int i;
if (likely(*index < ma->max)) {
mask = rcu_dereference_ovsl(ma->masks[*index]); if (mask) {
flow = masked_flow_lookup(ti, key, mask, n_mask_hit); if (flow) {
u64_stats_update_begin(&stats->syncp);
stats->usage_cntrs[*index]++;
u64_stats_update_end(&stats->syncp);
(*n_cache_hit)++; return flow;
}
}
}
for (i = 0; i < ma->max; i++) {
if (i == *index) continue;
mask = rcu_dereference_ovsl(ma->masks[i]); if (unlikely(!mask)) break;
/* * mask_cache maps flow to probable mask. This cache is not tightly * coupled cache, It means updates to mask list can result in inconsistent * cache entry in mask cache. * This is per cpu cache and is divided in MC_HASH_SEGS segments. * In case of a hash collision the entry is hashed in next segment.
* */ struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl, conststruct sw_flow_key *key,
u32 skb_hash,
u32 *n_mask_hit,
u32 *n_cache_hit)
{ struct mask_cache *mc = rcu_dereference(tbl->mask_cache); struct mask_array *ma = rcu_dereference(tbl->mask_array); struct table_instance *ti = rcu_dereference(tbl->ti); struct mask_cache_entry *entries, *ce; struct sw_flow *flow;
u32 hash; int seg;
/* Pre and post recirulation flows usually have the same skb_hash * value. To avoid hash collisions, rehash the 'skb_hash' with
* 'recirc_id'. */ if (key->recirc_id)
skb_hash = jhash_1word(skb_hash, key->recirc_id);
ce = NULL;
hash = skb_hash;
entries = this_cpu_ptr(mc->mask_cache);
/* Find the cache entry 'ce' to operate on. */ for (seg = 0; seg < MC_HASH_SEGS; seg++) { int index = hash & (mc->cache_size - 1); struct mask_cache_entry *e;
e = &entries[index]; if (e->skb_hash == skb_hash) {
flow = flow_lookup(tbl, ti, ma, key, n_mask_hit,
n_cache_hit, &e->mask_index); if (!flow)
e->skb_hash = 0; return flow;
}
if (!ce || e->skb_hash < ce->skb_hash)
ce = e; /* A better replacement cache candidate. */
hash >>= MC_HASH_SHIFT;
}
/* Cache miss, do full lookup. */
flow = flow_lookup(tbl, ti, ma, key, n_mask_hit, n_cache_hit,
&ce->mask_index); if (flow)
ce->skb_hash = skb_hash;
/* This function gets called trough the netlink interface and therefore * is preemptible. However, flow_lookup() function needs to be called * with BH disabled due to CPU specific variables.
*/
local_bh_disable();
flow = flow_lookup(tbl, ti, ma, key, &n_mask_hit, &n_cache_hit, &index);
local_bh_enable(); return flow;
}
ma = ovsl_dereference(tbl->mask_array); for (i = 0; i < ma->max; i++) { struct sw_flow_mask *t;
t = ovsl_dereference(ma->masks[i]);
if (t && mask_equal(mask, t)) return t;
}
return NULL;
}
/* Add 'mask' into the mask list, if it is not already there. */ staticint flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow, conststruct sw_flow_mask *new)
{ struct sw_flow_mask *mask;
mask = flow_mask_find(tbl, new); if (!mask) { /* Allocate a new mask if none exists. */
mask = mask_alloc(); if (!mask) return -ENOMEM;
mask->key = new->key;
mask->range = new->range;
/* Add mask to mask-list. */ if (tbl_mask_array_add_mask(tbl, mask)) {
kfree(mask); return -ENOMEM;
}
} else {
BUG_ON(!mask->ref_count);
mask->ref_count++;
}
flow->mask = mask; return 0;
}
/* Must be called with OVS mutex held. */ staticvoid flow_key_insert(struct flow_table *table, struct sw_flow *flow)
{ struct table_instance *new_ti = NULL; struct table_instance *ti;
flow->flow_table.hash = flow_hash(&flow->key, &flow->mask->range);
ti = ovsl_dereference(table->ti);
table_instance_insert(ti, flow);
table->count++;
/* Expand table, if necessary, to make room. */ if (table->count > ti->n_buckets)
new_ti = table_instance_expand(ti, false); elseif (time_after(jiffies, table->last_rehash + REHASH_INTERVAL))
new_ti = table_instance_rehash(ti, ti->n_buckets, false);
/* Must be called with OVS mutex held. */ int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow, conststruct sw_flow_mask *mask)
{ int err;
err = flow_mask_insert(table, flow, mask); if (err) return err;
flow_key_insert(table, flow); if (ovs_identifier_is_ufid(&flow->id))
flow_ufid_insert(table, flow);
/* Must be called with OVS mutex held. */ void ovs_flow_masks_rebalance(struct flow_table *table)
{ struct mask_array *ma = rcu_dereference_ovsl(table->mask_array); struct mask_count *masks_and_count; struct mask_array *new; int masks_entries = 0; int i;
/* Build array of all current entries with use counters. */
masks_and_count = kmalloc_array(ma->max, sizeof(*masks_and_count),
GFP_KERNEL); if (!masks_and_count) return;
for (i = 0; i < ma->max; i++) { struct sw_flow_mask *mask; int cpu;
mask = rcu_dereference_ovsl(ma->masks[i]); if (unlikely(!mask)) break;
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));
masks_and_count[i].counter += counter;
}
/* Subtract the zero count value. */
masks_and_count[i].counter -= ma->masks_usage_zero_cntr[i];
/* Rather than calling tbl_mask_array_reset_counters() * below when no change is needed, do it inline here.
*/
ma->masks_usage_zero_cntr[i] += masks_and_count[i].counter;
}
/* If the order is the same, nothing to do... */ for (i = 0; i < masks_entries; i++) { if (i != masks_and_count[i].index) break;
} if (i == masks_entries) goto free_mask_entries;
/* Rebuilt the new list in order of usage. */ new = tbl_mask_array_alloc(ma->max); if (!new) goto free_mask_entries;
for (i = 0; i < masks_entries; i++) { int index = masks_and_count[i].index;
if (ovsl_dereference(ma->masks[index]))
new->masks[new->count++] = ma->masks[index];
}
/* Initializes the flow module.
* Returns zero if successful or a negative error code. */ int ovs_flow_init(void)
{
BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long));
BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long));
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.