summaryrefslogtreecommitdiff
path: root/lib/cfm.c
diff options
context:
space:
mode:
authorJarno Rajahalme <jrajahalme@nicira.com>2014-08-29 10:34:52 -0700
committerJarno Rajahalme <jrajahalme@nicira.com>2014-08-29 10:34:52 -0700
commit7e2b277bed0878b5350f61de9e43d1a5b4d7c34b (patch)
tree377f03dd0c24fd392c1166bcf94f338ea449a3e2 /lib/cfm.c
parent94f756daf21af97876266015e758aa96556ff6e8 (diff)
downloadopenvswitch-7e2b277bed0878b5350f61de9e43d1a5b4d7c34b.tar.gz
lib/cfm: Use relaxed atomics and optimize cfm_should_process_flow().
The atomics here do not synchronize the state of any other variables, so we can use relaxed atomics. cfm_should_process_flow() is rearranged to set the megaflow mask bits only if necessary, and to avoid the atomic operation on non-CFM packets. Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com> Acked-by: Ben Pfaff <blp@nicira.com>
Diffstat (limited to 'lib/cfm.c')
-rw-r--r--lib/cfm.c40
1 files changed, 28 insertions, 12 deletions
diff --git a/lib/cfm.c b/lib/cfm.c
index d83a68c18..e3ae271e3 100644
--- a/lib/cfm.c
+++ b/lib/cfm.c
@@ -128,6 +128,9 @@ struct cfm {
recomputed. */
long long int last_tx; /* Last CCM transmission time. */
+ /* These bools are atomic to allow readers to check their values
+ * without taking 'mutex'. Such readers do not assume the values they
+ * read are synchronized with any other members. */
atomic_bool check_tnl_key; /* Verify the tunnel key of inbound packets? */
atomic_bool extended; /* Extended mode. */
struct ovs_refcount ref_cnt;
@@ -184,7 +187,9 @@ static const uint8_t *
cfm_ccm_addr(struct cfm *cfm)
{
bool extended;
- atomic_read(&cfm->extended, &extended);
+
+ atomic_read_relaxed(&cfm->extended, &extended);
+
return extended ? eth_addr_ccm_x : eth_addr_ccm;
}
@@ -578,6 +583,8 @@ cfm_compose_ccm(struct cfm *cfm, struct ofpbuf *packet,
eth_push_vlan(packet, htons(ETH_TYPE_VLAN), htons(tci));
}
+ atomic_read_relaxed(&cfm->extended, &extended);
+
ccm = ofpbuf_l3(packet);
ccm->mdlevel_version = 0;
ccm->opcode = CCM_OPCODE;
@@ -588,7 +595,6 @@ cfm_compose_ccm(struct cfm *cfm, struct ofpbuf *packet,
memset(ccm->zero, 0, sizeof ccm->zero);
ccm->end_tlv = 0;
- atomic_read(&cfm->extended, &extended);
if (extended) {
ccm->mpid = htons(hash_mpid(cfm->mpid));
ccm->mpid64 = htonll(cfm->mpid);
@@ -664,8 +670,8 @@ cfm_configure(struct cfm *cfm, const struct cfm_settings *s)
interval = ms_to_ccm_interval(s->interval);
interval_ms = ccm_interval_to_ms(interval);
- atomic_store(&cfm->check_tnl_key, s->check_tnl_key);
- atomic_store(&cfm->extended, s->extended);
+ atomic_store_relaxed(&cfm->check_tnl_key, s->check_tnl_key);
+ atomic_store_relaxed(&cfm->extended, s->extended);
cfm->ccm_vlan = s->ccm_vlan;
cfm->ccm_pcp = s->ccm_pcp & (VLAN_PCP_MASK >> VLAN_PCP_SHIFT);
@@ -717,14 +723,23 @@ cfm_should_process_flow(const struct cfm *cfm_, const struct flow *flow,
struct cfm *cfm = CONST_CAST(struct cfm *, cfm_);
bool check_tnl_key;
- atomic_read(&cfm->check_tnl_key, &check_tnl_key);
+ /* Most packets are not CFM. */
+ if (OVS_LIKELY(flow->dl_type != htons(ETH_TYPE_CFM))) {
+ return false;
+ }
+
memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
+ if (OVS_UNLIKELY(!eth_addr_equals(flow->dl_dst, cfm_ccm_addr(cfm)))) {
+ return false;
+ }
+
+ atomic_read_relaxed(&cfm->check_tnl_key, &check_tnl_key);
+
if (check_tnl_key) {
memset(&wc->masks.tunnel.tun_id, 0xff, sizeof wc->masks.tunnel.tun_id);
+ return flow->tunnel.tun_id == htonll(0);
}
- return (ntohs(flow->dl_type) == ETH_TYPE_CFM
- && eth_addr_equals(flow->dl_dst, cfm_ccm_addr(cfm))
- && (!check_tnl_key || flow->tunnel.tun_id == htonll(0)));
+ return true;
}
/* Updates internal statistics relevant to packet 'p'. Should be called on
@@ -736,9 +751,12 @@ cfm_process_heartbeat(struct cfm *cfm, const struct ofpbuf *p)
{
struct ccm *ccm;
struct eth_header *eth;
+ bool extended;
ovs_mutex_lock(&mutex);
+ atomic_read_relaxed(&cfm->extended, &extended);
+
eth = ofpbuf_l2(p);
ccm = ofpbuf_at(p, (uint8_t *)ofpbuf_l3(p) - (uint8_t *)ofpbuf_data(p),
CCM_ACCEPT_LEN);
@@ -777,10 +795,8 @@ cfm_process_heartbeat(struct cfm *cfm, const struct ofpbuf *p)
uint64_t ccm_mpid;
uint32_t ccm_seq;
bool ccm_opdown;
- bool extended;
enum cfm_fault_reason cfm_fault = 0;
- atomic_read(&cfm->extended, &extended);
if (extended) {
ccm_mpid = ntohll(ccm->mpid64);
ccm_opdown = ccm->opdown;
@@ -931,7 +947,7 @@ cfm_get_opup__(const struct cfm *cfm_) OVS_REQUIRES(mutex)
struct cfm *cfm = CONST_CAST(struct cfm *, cfm_);
bool extended;
- atomic_read(&cfm->extended, &extended);
+ atomic_read_relaxed(&cfm->extended, &extended);
return extended ? cfm->remote_opup : -1;
}
@@ -1006,7 +1022,7 @@ cfm_print_details(struct ds *ds, struct cfm *cfm) OVS_REQUIRES(mutex)
bool extended;
int fault;
- atomic_read(&cfm->extended, &extended);
+ atomic_read_relaxed(&cfm->extended, &extended);
ds_put_format(ds, "---- %s ----\n", cfm->name);
ds_put_format(ds, "MPID %"PRIu64":%s%s\n", cfm->mpid,