2 * Copyright (c) 2010, 2011 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
25 #include "dynamic-string.h"
31 #include "poll-loop.h"
37 VLOG_DEFINE_THIS_MODULE(cfm);
39 #define CFM_MAX_RMPS 256
41 /* Ethernet destination address of CCM packets. */
42 static const uint8_t eth_addr_ccm[6] = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x30 };
44 #define ETH_TYPE_CFM 0x8902
46 /* A 'ccm' represents a Continuity Check Message from the 802.1ag
47 * specification. Continuity Check Messages are broadcast periodically so that
48 * hosts can determine whom they have connectivity to. */
50 #define CCM_MAID_LEN 48
51 #define CCM_OPCODE 1 /* CFM message opcode meaning CCM. */
52 #define CCM_RDI_MASK 0x80
54 uint8_t mdlevel_version; /* MD Level and Version */
60 uint8_t maid[CCM_MAID_LEN];
61 uint8_t zero[16]; /* Defined by ITU-T Y.1731 should be zero */
62 } __attribute__((packed));
63 BUILD_ASSERT_DECL(CCM_LEN == sizeof(struct ccm));
66 char *name; /* Name of this CFM object. */
67 struct hmap_node hmap_node; /* Node in all_cfms list. */
70 bool fault; /* Indicates connectivity fault. */
71 bool unexpected_recv; /* Received an unexpected CCM. */
73 uint32_t seq; /* The sequence number of our last CCM. */
74 uint8_t ccm_interval; /* The CCM transmission interval. */
75 int ccm_interval_ms; /* 'ccm_interval' in milliseconds. */
76 uint8_t maid[CCM_MAID_LEN]; /* The MAID of this CFM. */
78 struct timer tx_timer; /* Send CCM when expired. */
79 struct timer fault_timer; /* Check for faults when expired. */
81 struct hmap remote_mps; /* Remote MPs. */
84 /* Remote MPs represent foreign network entities that are configured to have
85 * the same MAID as this CFM instance. */
87 uint16_t mpid; /* The Maintenance Point ID of this 'remote_mp'. */
88 struct hmap_node node; /* Node in 'remote_mps' map. */
90 bool recv; /* CCM was received since last fault check. */
91 bool rdi; /* Remote Defect Indicator. Indicates remote_mp isn't
92 receiving CCMs that it's expecting to. */
95 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
96 static struct hmap all_cfms = HMAP_INITIALIZER(&all_cfms);
98 static void cfm_unixctl_show(struct unixctl_conn *, const char *args,
102 cfm_generate_maid(struct cfm *cfm)
104 const char *ovs_md_name = "ovs";
105 const char *ovs_ma_name = "ovs";
107 size_t md_len, ma_len;
109 memset(cfm->maid, 0, CCM_MAID_LEN);
111 md_len = strlen(ovs_md_name);
112 ma_len = strlen(ovs_ma_name);
114 assert(md_len && ma_len && md_len + ma_len + 4 <= CCM_MAID_LEN);
116 cfm->maid[0] = 4; /* MD name string format. */
117 cfm->maid[1] = md_len; /* MD name size. */
118 memcpy(&cfm->maid[2], ovs_md_name, md_len); /* MD name. */
120 ma_p = cfm->maid + 2 + md_len;
121 ma_p[0] = 2; /* MA name string format. */
122 ma_p[1] = ma_len; /* MA name size. */
123 memcpy(&ma_p[2], ovs_ma_name, ma_len); /* MA name. */
127 ccm_interval_to_ms(uint8_t interval)
130 case 0: NOT_REACHED(); /* Explicitly not supported by 802.1ag. */
131 case 1: return 3; /* Not recommended due to timer resolution. */
132 case 2: return 10; /* Not recommended due to timer resolution. */
135 case 5: return 10000;
136 case 6: return 60000;
137 case 7: return 600000;
138 default: NOT_REACHED(); /* Explicitly not supported by 802.1ag. */
145 cfm_fault_interval(struct cfm *cfm)
147 /* According to the 802.1ag specification we should assume every other MP
148 * with the same MAID has the same transmission interval that we have. If
149 * an MP has a different interval, cfm_process_heartbeat will register it
150 * as a fault (likely due to a configuration error). Thus we can check all
151 * MPs at once making this quite a bit simpler.
153 * According to the specification we should check when (ccm_interval_ms *
154 * 3.5)ms have passed. */
155 return (cfm->ccm_interval_ms * 7) / 2;
159 ms_to_ccm_interval(int interval_ms)
163 for (i = 7; i > 0; i--) {
164 if (ccm_interval_to_ms(i) <= interval_ms) {
173 hash_mpid(uint8_t mpid)
175 return hash_int(mpid, 0);
179 cfm_is_valid_mpid(uint32_t mpid)
181 /* 802.1ag specification requires MPIDs to be within the range [1, 8191] */
182 return mpid >= 1 && mpid <= 8191;
185 static struct remote_mp *
186 lookup_remote_mp(const struct cfm *cfm, uint16_t mpid)
188 struct remote_mp *rmp;
190 HMAP_FOR_EACH_IN_BUCKET (rmp, node, hash_mpid(mpid), &cfm->remote_mps) {
191 if (rmp->mpid == mpid) {
202 unixctl_command_register("cfm/show", cfm_unixctl_show, NULL);
205 /* Allocates a 'cfm' object called 'name'. 'cfm' should be initialized by
206 * cfm_configure() before use. */
208 cfm_create(const char *name)
212 cfm = xzalloc(sizeof *cfm);
213 cfm->name = xstrdup(name);
214 hmap_init(&cfm->remote_mps);
215 cfm_generate_maid(cfm);
216 hmap_insert(&all_cfms, &cfm->hmap_node, hash_string(cfm->name, 0));
221 cfm_destroy(struct cfm *cfm)
223 struct remote_mp *rmp, *rmp_next;
229 HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &cfm->remote_mps) {
230 hmap_remove(&cfm->remote_mps, &rmp->node);
234 hmap_destroy(&cfm->remote_mps);
235 hmap_remove(&all_cfms, &cfm->hmap_node);
240 /* Should be run periodically to update fault statistics messages. */
242 cfm_run(struct cfm *cfm)
244 if (timer_expired(&cfm->fault_timer)) {
245 long long int interval = cfm_fault_interval(cfm);
246 struct remote_mp *rmp, *rmp_next;
248 cfm->fault = cfm->unexpected_recv;
249 cfm->unexpected_recv = false;
251 HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &cfm->remote_mps) {
254 VLOG_DBG("%s: No CCM from RMP %"PRIu16" in the last %lldms",
255 cfm->name, rmp->mpid, interval);
256 hmap_remove(&cfm->remote_mps, &rmp->node);
261 if (rmp->mpid == cfm->mpid) {
262 VLOG_WARN_RL(&rl, "%s: Received CCM with local MPID (%d)",
263 cfm->name, rmp->mpid);
268 VLOG_DBG("%s: RDI bit flagged from RMP %"PRIu16, cfm->name,
275 if (hmap_is_empty(&cfm->remote_mps)) {
279 timer_set_duration(&cfm->fault_timer, interval);
283 /* Should be run periodically to check if the CFM module has a CCM message it
286 cfm_should_send_ccm(struct cfm *cfm)
288 return timer_expired(&cfm->tx_timer);
291 /* Composes a CCM message into 'packet'. Messages generated with this function
292 * should be sent whenever cfm_should_send_ccm() indicates. */
294 cfm_compose_ccm(struct cfm *cfm, struct ofpbuf *packet,
295 uint8_t eth_src[ETH_ADDR_LEN])
299 timer_set_duration(&cfm->tx_timer, cfm->ccm_interval_ms);
301 ccm = eth_compose(packet, eth_addr_ccm, eth_src, ETH_TYPE_CFM,
303 ccm->mdlevel_version = 0;
304 ccm->opcode = CCM_OPCODE;
305 ccm->tlv_offset = 70;
306 ccm->seq = htonl(++cfm->seq);
307 ccm->mpid = htons(cfm->mpid);
308 ccm->flags = cfm->ccm_interval;
309 memcpy(ccm->maid, cfm->maid, sizeof ccm->maid);
310 memset(ccm->zero, 0, sizeof ccm->zero);
312 if (hmap_is_empty(&cfm->remote_mps)) {
313 ccm->flags |= CCM_RDI_MASK;
318 cfm_wait(struct cfm *cfm)
321 timer_wait(&cfm->tx_timer);
322 timer_wait(&cfm->fault_timer);
325 /* Configures 'cfm' with settings from 's'. */
327 cfm_configure(struct cfm *cfm, const struct cfm_settings *s)
331 if (!cfm_is_valid_mpid(s->mpid) || s->interval <= 0) {
336 interval = ms_to_ccm_interval(s->interval);
338 if (interval != cfm->ccm_interval) {
339 cfm->ccm_interval = interval;
340 cfm->ccm_interval_ms = ccm_interval_to_ms(interval);
342 timer_set_expired(&cfm->tx_timer);
343 timer_set_duration(&cfm->fault_timer, cfm_fault_interval(cfm));
349 /* Returns true if the CFM library should process packets from 'flow'. */
351 cfm_should_process_flow(const struct flow *flow)
353 return (ntohs(flow->dl_type) == ETH_TYPE_CFM
354 && eth_addr_equals(flow->dl_dst, eth_addr_ccm));
357 /* Updates internal statistics relevant to packet 'p'. Should be called on
358 * every packet whose flow returned true when passed to
359 * cfm_should_process_flow. */
361 cfm_process_heartbeat(struct cfm *cfm, const struct ofpbuf *p)
366 uint8_t ccm_interval;
367 struct remote_mp *rmp;
368 struct eth_header *eth;
371 ccm = ofpbuf_at(p, (uint8_t *)p->l3 - (uint8_t *)p->data, CCM_LEN);
374 VLOG_INFO_RL(&rl, "%s: Received an unparseable 802.1ag CCM heartbeat.",
379 if (ccm->opcode != CCM_OPCODE) {
380 VLOG_INFO_RL(&rl, "%s: Received an unsupported 802.1ag message. "
381 "(opcode %u)", cfm->name, ccm->opcode);
385 /* According to the 802.1ag specification, reception of a CCM with an
386 * incorrect ccm_interval, unexpected MAID, or unexpected MPID should
387 * trigger a fault. We ignore this requirement for several reasons.
389 * Faults can cause a controller or Open vSwitch to make potentially
390 * expensive changes to the network topology. It seems prudent to trigger
391 * them judiciously, especially when CFM is used to check slave status of
392 * bonds. Furthermore, faults can be maliciously triggered by crafting
394 if (memcmp(ccm->maid, cfm->maid, sizeof ccm->maid)) {
395 cfm->unexpected_recv = true;
396 VLOG_WARN_RL(&rl, "%s: Received unexpected remote MAID from MAC "
397 ETH_ADDR_FMT, cfm->name, ETH_ADDR_ARGS(eth->eth_src));
399 ccm_mpid = ntohs(ccm->mpid);
400 ccm_interval = ccm->flags & 0x7;
401 ccm_rdi = ccm->flags & CCM_RDI_MASK;
403 if (ccm_interval != cfm->ccm_interval) {
404 VLOG_WARN_RL(&rl, "%s: received a CCM with an invalid interval"
405 " (%"PRIu8") from RMP %"PRIu16, cfm->name,
406 ccm_interval, ccm_mpid);
409 rmp = lookup_remote_mp(cfm, ccm_mpid);
413 } else if (hmap_count(&cfm->remote_mps) < CFM_MAX_RMPS) {
414 rmp = xmalloc(sizeof *rmp);
415 rmp->mpid = ccm_mpid;
418 hmap_insert(&cfm->remote_mps, &rmp->node, hash_mpid(rmp->mpid));
420 cfm->unexpected_recv = true;
421 VLOG_WARN_RL(&rl, "%s: dropped CCM with MPID %d from MAC "
422 ETH_ADDR_FMT, cfm->name, ccm_mpid,
423 ETH_ADDR_ARGS(eth->eth_src));
426 VLOG_DBG("%s: Received CCM (seq %"PRIu32") (mpid %"PRIu16")"
427 " (interval %"PRIu8") (RDI %s)", cfm->name, ntohl(ccm->seq),
428 ccm_mpid, ccm_interval, ccm_rdi ? "true" : "false");
432 /* Gets the fault status of 'cfm'. Returns true when 'cfm' has detected
433 * connectivity problems, false otherwise. */
435 cfm_get_fault(const struct cfm *cfm)
441 cfm_find(const char *name)
445 HMAP_FOR_EACH_WITH_HASH (cfm, hmap_node, hash_string(name, 0), &all_cfms) {
446 if (!strcmp(cfm->name, name)) {
454 cfm_unixctl_show(struct unixctl_conn *conn,
455 const char *args, void *aux OVS_UNUSED)
457 struct ds ds = DS_EMPTY_INITIALIZER;
458 const struct cfm *cfm;
459 struct remote_mp *rmp;
461 cfm = cfm_find(args);
463 unixctl_command_reply(conn, 501, "no such CFM object");
467 ds_put_format(&ds, "MPID %"PRIu16":%s%s\n", cfm->mpid,
468 cfm->fault ? " fault" : "",
469 cfm->unexpected_recv ? " unexpected_recv" : "");
471 ds_put_format(&ds, "\tinterval: %dms\n", cfm->ccm_interval_ms);
472 ds_put_format(&ds, "\tnext CCM tx: %lldms\n",
473 timer_msecs_until_expired(&cfm->tx_timer));
474 ds_put_format(&ds, "\tnext fault check: %lldms\n",
475 timer_msecs_until_expired(&cfm->fault_timer));
477 ds_put_cstr(&ds, "\n");
478 HMAP_FOR_EACH (rmp, node, &cfm->remote_mps) {
479 ds_put_format(&ds, "Remote MPID %"PRIu16":%s\n", rmp->mpid,
480 rmp->rdi ? " rdi" : "");
481 ds_put_format(&ds, "\trecv since check: %s",
482 rmp->recv ? "true" : "false");
485 unixctl_command_reply(conn, 200, ds_cstr(&ds));