#include <stdlib.h>
#include <string.h>
+#include "dynamic-string.h"
#include "flow.h"
#include "hash.h"
#include "hmap.h"
long long ccm_sent; /* The time we last sent a CCM. */
long long fault_check; /* The time we last checked for faults. */
+
+ long long x_recv_time;
};
static int
}
static struct cfm_internal *
-cfm_to_internal(struct cfm *cfm)
+cfm_to_internal(const struct cfm *cfm)
{
return CONTAINER_OF(cfm, struct cfm_internal, cfm);
}
return NULL;
}
-static struct ofpbuf *
-compose_ccm(struct cfm_internal *cfmi)
-{
- struct ccm *ccm;
- struct ofpbuf *packet;
- struct eth_header *eth;
-
- packet = ofpbuf_new(ETH_HEADER_LEN + CCM_LEN + 2);
-
- ofpbuf_reserve(packet, 2);
-
- eth = ofpbuf_put_zeros(packet, ETH_HEADER_LEN);
- ccm = ofpbuf_put_zeros(packet, CCM_LEN);
-
- memcpy(eth->eth_dst, eth_addr_ccm, ETH_ADDR_LEN);
- memcpy(eth->eth_src, cfmi->cfm.eth_src, sizeof eth->eth_src);
- eth->eth_type = htons(ETH_TYPE_CFM);
-
- ccm->mdlevel_version = 0;
- ccm->opcode = CCM_OPCODE;
- ccm->tlv_offset = 70;
- ccm->seq = htonl(++cfmi->seq);
- ccm->mpid = htons(cfmi->cfm.mpid);
- ccm->flags = cfmi->ccm_interval;
- memcpy(ccm->maid, cfmi->cfm.maid, sizeof ccm->maid);
- return packet;
-}
-
/* Allocates a 'cfm' object. This object should have its 'mpid', 'maid',
* 'eth_src', and 'interval' filled out. When changes are made to the 'cfm'
* object, cfm_configure should be called before using it. */
cfmi = xzalloc(sizeof *cfmi);
cfm = &cfmi->cfm;
+ cfmi->x_recv_time = LLONG_MIN;
hmap_init(&cfm->remote_mps);
- hmap_init(&cfm->x_remote_mps);
- hmap_init(&cfm->x_remote_maids);
return cfm;
}
void
cfm_destroy(struct cfm *cfm)
{
+ struct cfm_internal *cfmi = cfm_to_internal(cfm);
struct remote_mp *rmp, *rmp_next;
- struct remote_maid *rmaid, *rmaid_next;
if (!cfm) {
return;
free(rmp);
}
- HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &cfm->x_remote_mps) {
- hmap_remove(&cfm->x_remote_mps, &rmp->node);
- free(rmp);
- }
-
- HMAP_FOR_EACH_SAFE (rmaid, rmaid_next, node, &cfm->x_remote_maids) {
- hmap_remove(&cfm->x_remote_maids, &rmaid->node);
- free(rmaid);
- }
-
hmap_destroy(&cfm->remote_mps);
- hmap_destroy(&cfm->x_remote_mps);
- hmap_destroy(&cfm->x_remote_maids);
- free(cfm_to_internal(cfm));
+ free(cfmi);
}
-/* Should be run periodically to update fault statistics and generate CCM
- * messages. If necessary, returns a packet which the caller is responsible
- * for sending, un-initing, and deallocating. Otherwise returns NULL. */
-struct ofpbuf *
+/* Should be run periodically to update fault statistics messages. */
+void
cfm_run(struct cfm *cfm)
{
long long now = time_msec();
struct cfm_internal *cfmi = cfm_to_internal(cfm);
+ long long fault_interval;
/* According to the 802.1ag specification we should assume every other MP
* with the same MAID has the same transmission interval that we have. If
* MPs at once making this quite a bit simpler.
*
* According to the specification we should check when (ccm_interval_ms *
- * 3.5)ms have passed. We changed the multiplier to 4 to avoid messy
- * floating point arithmetic and add a bit of wiggle room. */
- if (now >= cfmi->fault_check + cfmi->ccm_interval_ms * 4) {
+ * 3.5)ms have passed. */
+ fault_interval = (cfmi->ccm_interval_ms * 7) / 2;
+ if (now >= cfmi->fault_check + fault_interval) {
bool fault;
- struct remote_mp *rmp, *rmp_next;
- struct remote_maid *rmaid, *rmaid_next;
+ struct remote_mp *rmp;
- fault = false;
+ fault = now < cfmi->x_recv_time + fault_interval;
HMAP_FOR_EACH (rmp, node, &cfm->remote_mps) {
rmp->fault = rmp->fault || cfmi->fault_check > rmp->recv_time;
fault = rmp->fault || fault;
}
- HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &cfm->x_remote_mps) {
- if (cfmi->fault_check > rmp->recv_time) {
- hmap_remove(&cfm->x_remote_mps, &rmp->node);
- free(rmp);
- }
- }
-
- HMAP_FOR_EACH_SAFE (rmaid, rmaid_next, node, &cfm->x_remote_maids) {
- if (cfmi->fault_check > rmaid->recv_time) {
- hmap_remove(&cfm->x_remote_maids, &rmaid->node);
- free(rmaid);
- }
- }
-
- fault = (fault || !hmap_is_empty(&cfm->x_remote_mps)
- || !hmap_is_empty(&cfm->x_remote_maids));
-
cfm->fault = fault;
cfmi->fault_check = now;
}
+}
- if (now >= cfmi->ccm_sent + cfmi->ccm_interval_ms) {
- cfmi->ccm_sent = now;
- return compose_ccm(cfmi);
- }
+/* Should be run periodically to check if the CFM module has a CCM message it
+ * wishes to send. */
+bool
+cfm_should_send_ccm(struct cfm *cfm)
+{
+ struct cfm_internal *cfmi = cfm_to_internal(cfm);
- return NULL;
+ return time_msec() >= cfmi->ccm_sent + cfmi->ccm_interval_ms;
+}
+
+/* Composes a CCM message into 'ccm'. Messages generated with this function
+ * should be sent whenever cfm_should_send_ccm() indicates. */
+void
+cfm_compose_ccm(struct cfm *cfm, struct ccm *ccm)
+{
+ struct cfm_internal *cfmi = cfm_to_internal(cfm);
+
+ cfmi->ccm_sent = time_msec();
+
+ ccm->mdlevel_version = 0;
+ ccm->opcode = CCM_OPCODE;
+ ccm->tlv_offset = 70;
+ ccm->seq = htonl(++cfmi->seq);
+ ccm->mpid = htons(cfmi->cfm.mpid);
+ ccm->flags = cfmi->ccm_interval;
+ memcpy(ccm->maid, cfmi->cfm.maid, sizeof ccm->maid);
}
void
if ((rmp = lookup_remote_mp(&cfm->remote_mps, mpid))) {
hmap_remove(&cfm->remote_mps, &rmp->node);
- } else if ((rmp = lookup_remote_mp(&cfm->x_remote_mps, mpid))) {
- hmap_remove(&cfm->x_remote_mps, &rmp->node);
} else {
rmp = xzalloc(sizeof *rmp);
rmp->mpid = mpid;
{
struct ccm *ccm;
uint16_t ccm_mpid;
- uint32_t ccm_seq;
uint8_t ccm_interval;
struct remote_mp *rmp;
+ struct eth_header *eth;
struct cfm_internal *cfmi = cfm_to_internal(cfm);
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
+ eth = p->l2;
ccm = ofpbuf_at(p, (uint8_t *)p->l3 - (uint8_t *)p->data, CCM_LEN);
if (!ccm) {
}
if (memcmp(ccm->maid, cfm->maid, sizeof ccm->maid)) {
- uint32_t hash;
- struct remote_maid *rmaid;
-
- hash = hash_bytes(ccm->maid, sizeof ccm->maid, 0);
-
- HMAP_FOR_EACH_IN_BUCKET (rmaid, node, hash, &cfm->x_remote_maids) {
- if (memcmp(rmaid->maid, ccm->maid, sizeof rmaid->maid) == 0) {
- rmaid->recv_time = time_msec();
- return;
- }
+ cfmi->x_recv_time = time_msec();
+ cfm->fault = true;
+ VLOG_WARN_RL(&rl, "Received unexpected remote MAID from MAC "
+ ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_src));
+ } else {
+ ccm_mpid = ntohs(ccm->mpid);
+ ccm_interval = ccm->flags & 0x7;
+
+ rmp = lookup_remote_mp(&cfm->remote_mps, ccm_mpid);
+
+ if (rmp) {
+ rmp->recv_time = time_msec();
+ rmp->fault = ccm_interval != cfmi->ccm_interval;
+ cfm->fault = rmp->fault || cfm->fault;
+ } else {
+ cfmi->x_recv_time = time_msec();
+ cfm->fault = true;
+ VLOG_WARN_RL(&rl, "Received unexpected remote MPID %d from MAC "
+ ETH_ADDR_FMT, ccm_mpid, ETH_ADDR_ARGS(eth->eth_src));
}
-
- rmaid = xzalloc(sizeof *rmaid);
- rmaid->recv_time = time_msec();
- memcpy(rmaid->maid, ccm->maid, sizeof rmaid->maid);
- hmap_insert(&cfm->x_remote_maids, &rmaid->node, hash);
- return;
}
+}
- ccm_mpid = ntohs(ccm->mpid);
- ccm_seq = ntohl(ccm->seq);
- ccm_interval = ccm->flags & 0x7;
-
- rmp = lookup_remote_mp(&cfm->remote_mps, ccm_mpid);
-
- if (!rmp) {
- rmp = lookup_remote_mp(&cfm->x_remote_mps, ccm_mpid);
- }
+void
+cfm_dump_ds(const struct cfm *cfm, struct ds *ds)
+{
+ const struct cfm_internal *cfmi = cfm_to_internal(cfm);
+ long long int now = time_msec();
+ struct remote_mp *rmp;
- if (!rmp) {
- rmp = xzalloc(sizeof *rmp);
- rmp->mpid = ccm_mpid;
- hmap_insert(&cfm->x_remote_mps, &rmp->node, hash_mpid(ccm_mpid));
- cfm->fault = true;
+ ds_put_format(ds, "MPID %"PRIu16": %s\n", cfm->mpid,
+ cfm->fault ? "fault" : "");
+
+ ds_put_format(ds, "\tinterval: %dms\n", cfmi->ccm_interval_ms);
+ ds_put_format(ds, "\ttime since CCM tx: %lldms\n", now - cfmi->ccm_sent);
+ ds_put_format(ds, "\ttime since bad CCM rx: %lldms\n",
+ now - cfmi->x_recv_time);
+ ds_put_format(ds, "\ttime since fault check: %lldms\n",
+ now - cfmi->fault_check);
+
+ ds_put_cstr(ds, "\n");
+ HMAP_FOR_EACH (rmp, node, &cfm->remote_mps) {
+ ds_put_format(ds, "Remote MPID %"PRIu16": %s\n", rmp->mpid,
+ rmp->fault ? "fault" : "");
+ ds_put_format(ds, "\ttime since CCM rx: %lldms\n",
+ time_msec() - rmp->recv_time);
}
-
- rmp->recv_time = time_msec();
- rmp->fault = ccm_interval != cfmi->ccm_interval;
}