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 /* Ethernet destination address of CCM packets. */
40 static const uint8_t eth_addr_ccm[6] = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x30 };
42 #define ETH_TYPE_CFM 0x8902
44 /* A 'ccm' represents a Continuity Check Message from the 802.1ag
45 * specification. Continuity Check Messages are broadcast periodically so that
46 * hosts can determine whom they have connectivity to. */
48 #define CCM_MAID_LEN 48
49 #define CCM_OPCODE 1 /* CFM message opcode meaning CCM. */
51 uint8_t mdlevel_version; /* MD Level and Version */
57 uint8_t maid[CCM_MAID_LEN];
58 uint8_t zero[16]; /* Defined by ITU-T Y.1731 should be zero */
59 } __attribute__((packed));
60 BUILD_ASSERT_DECL(CCM_LEN == sizeof(struct ccm));
64 struct list list_node; /* Node in all_cfms list. */
66 bool fault; /* Indicates connectivity fault. */
67 char *name; /* Name of this CFM object. */
69 uint32_t seq; /* The sequence number of our last CCM. */
70 uint8_t ccm_interval; /* The CCM transmission interval. */
71 int ccm_interval_ms; /* 'ccm_interval' in milliseconds. */
72 uint8_t maid[CCM_MAID_LEN]; /* The MAID of this CFM. */
74 struct timer tx_timer; /* Send CCM when expired. */
75 struct timer fault_timer; /* Check for faults when expired. */
77 struct hmap remote_mps; /* Expected remote MPs. */
80 /* Remote MPs represent foreign network entities that are configured to have
81 * the same MAID as this CFM instance. */
83 uint16_t mpid; /* The Maintenance Point ID of this 'remote_mp'. */
84 struct hmap_node node; /* Node in 'remote_mps' map. */
86 bool recv; /* CCM was received since last fault check. */
87 bool fault; /* Indicates a connectivity fault. */
90 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
91 static struct list all_cfms = LIST_INITIALIZER(&all_cfms);
93 static void cfm_unixctl_show(struct unixctl_conn *, const char *args,
97 cfm_generate_maid(struct cfm *cfm)
99 const char *ovs_md_name = "ovs_md";
100 const char *ovs_ma_name = "ovs_ma";
102 size_t md_len, ma_len;
104 memset(cfm->maid, 0, CCM_MAID_LEN);
106 md_len = strlen(ovs_md_name);
107 ma_len = strlen(ovs_ma_name);
109 assert(md_len && ma_len && md_len + ma_len + 4 <= CCM_MAID_LEN);
111 cfm->maid[0] = 4; /* MD name string format. */
112 cfm->maid[1] = md_len; /* MD name size. */
113 memcpy(&cfm->maid[2], ovs_md_name, md_len); /* MD name. */
115 ma_p = cfm->maid + 2 + md_len;
116 ma_p[0] = 2; /* MA name string format. */
117 ma_p[1] = ma_len; /* MA name size. */
118 memcpy(&ma_p[2], ovs_ma_name, ma_len); /* MA name. */
122 ccm_interval_to_ms(uint8_t interval)
125 case 0: NOT_REACHED(); /* Explicitly not supported by 802.1ag. */
126 case 1: return 3; /* Not recommended due to timer resolution. */
127 case 2: return 10; /* Not recommended due to timer resolution. */
130 case 5: return 10000;
131 case 6: return 60000;
132 case 7: return 600000;
133 default: NOT_REACHED(); /* Explicitly not supported by 802.1ag. */
140 cfm_fault_interval(struct cfm *cfm)
142 /* According to the 802.1ag specification we should assume every other MP
143 * with the same MAID has the same transmission interval that we have. If
144 * an MP has a different interval, cfm_process_heartbeat will register it
145 * as a fault (likely due to a configuration error). Thus we can check all
146 * MPs at once making this quite a bit simpler.
148 * According to the specification we should check when (ccm_interval_ms *
149 * 3.5)ms have passed. */
150 return (cfm->ccm_interval_ms * 7) / 2;
154 ms_to_ccm_interval(int interval_ms)
158 for (i = 7; i > 0; i--) {
159 if (ccm_interval_to_ms(i) <= interval_ms) {
168 hash_mpid(uint8_t mpid)
170 return hash_int(mpid, 0);
174 cfm_is_valid_mpid(uint32_t mpid)
176 /* 802.1ag specification requires MPIDs to be within the range [1, 8191] */
177 return mpid >= 1 && mpid <= 8191;
180 static struct remote_mp *
181 lookup_remote_mp(const struct hmap *hmap, uint16_t mpid)
183 struct remote_mp *rmp;
185 HMAP_FOR_EACH_IN_BUCKET (rmp, node, hash_mpid(mpid), hmap) {
186 if (rmp->mpid == mpid) {
197 unixctl_command_register("cfm/show", cfm_unixctl_show, NULL);
200 /* Allocates a 'cfm' object. This object should have its 'mpid', 'maid',
201 * 'eth_src', and 'interval' filled out. cfm_configure() should be called
202 * whenever changes are made to 'cfm', and before cfm_run() is called for the
209 cfm = xzalloc(sizeof *cfm);
210 hmap_init(&cfm->remote_mps);
211 cfm_generate_maid(cfm);
212 list_push_back(&all_cfms, &cfm->list_node);
217 cfm_destroy(struct cfm *cfm)
219 struct remote_mp *rmp, *rmp_next;
225 HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &cfm->remote_mps) {
226 hmap_remove(&cfm->remote_mps, &rmp->node);
230 hmap_destroy(&cfm->remote_mps);
231 list_remove(&cfm->list_node);
235 /* Should be run periodically to update fault statistics messages. */
237 cfm_run(struct cfm *cfm)
240 if (timer_expired(&cfm->fault_timer)) {
241 long long int interval = cfm_fault_interval(cfm);
242 struct remote_mp *rmp;
245 HMAP_FOR_EACH (rmp, node, &cfm->remote_mps) {
246 rmp->fault = !rmp->recv;
251 VLOG_DBG("No CCM from RMP %"PRIu16" in the last %lldms",
252 rmp->mpid, interval);
257 VLOG_DBG("All RMPs received CCMs in the last %lldms", interval);
260 timer_set_duration(&cfm->fault_timer, interval);
264 /* Should be run periodically to check if the CFM module has a CCM message it
267 cfm_should_send_ccm(struct cfm *cfm)
270 return timer_expired(&cfm->tx_timer);
273 /* Composes a CCM message into 'packet'. Messages generated with this function
274 * should be sent whenever cfm_should_send_ccm() indicates. */
276 cfm_compose_ccm(struct cfm *cfm, struct ofpbuf *packet,
277 uint8_t eth_src[ETH_ADDR_LEN])
281 timer_set_duration(&cfm->tx_timer, cfm->ccm_interval_ms);
283 ccm = eth_compose(packet, eth_addr_ccm, eth_src, ETH_TYPE_CFM,
285 ccm->mdlevel_version = 0;
286 ccm->opcode = CCM_OPCODE;
287 ccm->tlv_offset = 70;
288 ccm->seq = htonl(++cfm->seq);
289 ccm->mpid = htons(cfm->mpid);
290 ccm->flags = cfm->ccm_interval;
291 memcpy(ccm->maid, cfm->maid, sizeof ccm->maid);
295 cfm_wait(struct cfm *cfm)
298 timer_wait(&cfm->tx_timer);
299 timer_wait(&cfm->fault_timer);
302 /* Configures 'cfm' with settings from 's'. */
304 cfm_configure(struct cfm *cfm, const struct cfm_settings *s)
308 struct hmap new_rmps;
309 struct remote_mp *rmp, *rmp_next;
311 if (!cfm_is_valid_mpid(s->mpid) || s->interval <= 0
312 || s->n_remote_mpids <= 0) {
317 interval = ms_to_ccm_interval(s->interval);
319 if (!cfm->name || strcmp(s->name, cfm->name)) {
321 cfm->name = xstrdup(s->name);
324 if (interval != cfm->ccm_interval) {
325 cfm->ccm_interval = interval;
326 cfm->ccm_interval_ms = ccm_interval_to_ms(interval);
328 timer_set_expired(&cfm->tx_timer);
329 timer_set_duration(&cfm->fault_timer, cfm_fault_interval(cfm));
332 hmap_init(&new_rmps);
333 for (i = 0; i < s->n_remote_mpids; i++) {
334 uint16_t mpid = s->remote_mpids[i];
336 if (!cfm_is_valid_mpid(mpid)
337 || lookup_remote_mp(&new_rmps, mpid)) {
341 if ((rmp = lookup_remote_mp(&cfm->remote_mps, mpid))) {
342 hmap_remove(&cfm->remote_mps, &rmp->node);
344 rmp = xzalloc(sizeof *rmp);
348 hmap_insert(&new_rmps, &rmp->node, hash_mpid(mpid));
351 hmap_swap(&new_rmps, &cfm->remote_mps);
352 HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &new_rmps) {
353 hmap_remove(&new_rmps, &rmp->node);
356 hmap_destroy(&new_rmps);
361 /* Returns true if the CFM library should process packets from 'flow'. */
363 cfm_should_process_flow(const struct flow *flow)
365 return (ntohs(flow->dl_type) == ETH_TYPE_CFM
366 && eth_addr_equals(flow->dl_dst, eth_addr_ccm));
369 /* Updates internal statistics relevant to packet 'p'. Should be called on
370 * every packet whose flow returned true when passed to
371 * cfm_should_process_flow. */
373 cfm_process_heartbeat(struct cfm *cfm, const struct ofpbuf *p)
377 uint8_t ccm_interval;
378 struct remote_mp *rmp;
379 struct eth_header *eth;
382 ccm = ofpbuf_at(p, (uint8_t *)p->l3 - (uint8_t *)p->data, CCM_LEN);
385 VLOG_INFO_RL(&rl, "Received an un-parseable 802.1ag CCM heartbeat.");
389 if (ccm->opcode != CCM_OPCODE) {
390 VLOG_INFO_RL(&rl, "Received an unsupported 802.1ag message. "
391 "(opcode %u)", ccm->opcode);
395 /* According to the 802.1ag specification, reception of a CCM with an
396 * incorrect ccm_interval, unexpected MAID, or unexpected MPID should
397 * trigger a fault. We ignore this requirement for several reasons.
399 * Faults can cause a controller or Open vSwitch to make potentially
400 * expensive changes to the network topology. It seems prudent to trigger
401 * them judiciously, especially when CFM is used to check slave status of
402 * bonds. Furthermore, faults can be maliciously triggered by crafting
404 if (memcmp(ccm->maid, cfm->maid, sizeof ccm->maid)) {
405 VLOG_WARN_RL(&rl, "Received unexpected remote MAID from MAC "
406 ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_src));
408 ccm_mpid = ntohs(ccm->mpid);
409 ccm_interval = ccm->flags & 0x7;
411 rmp = lookup_remote_mp(&cfm->remote_mps, ccm_mpid);
416 if (ccm_interval != cfm->ccm_interval) {
417 VLOG_WARN_RL(&rl, "received a CCM with an invalid interval"
418 " (%"PRIu8") from RMP %"PRIu16, ccm_interval,
422 VLOG_WARN_RL(&rl, "Received unexpected remote MPID %d from MAC "
423 ETH_ADDR_FMT, ccm_mpid, ETH_ADDR_ARGS(eth->eth_src));
426 VLOG_DBG("Received CCM (mpid %"PRIu16") (interval %"PRIu8")", ccm_mpid,
431 /* Gets the fault status of 'cfm'. Returns true when 'cfm' has detected
432 * connectivity problems, false otherwise. */
434 cfm_get_fault(const struct cfm *cfm)
440 cfm_find(const char *name)
444 LIST_FOR_EACH (cfm, list_node, &all_cfms) {
445 if (cfm->name && !strcmp(cfm->name, name)) {
453 cfm_unixctl_show(struct unixctl_conn *conn,
454 const char *args, void *aux OVS_UNUSED)
456 struct ds ds = DS_EMPTY_INITIALIZER;
457 const struct cfm *cfm;
458 struct remote_mp *rmp;
460 cfm = cfm_find(args);
462 unixctl_command_reply(conn, 501, "no such CFM object");
466 ds_put_format(&ds, "MPID %"PRIu16": %s\n", cfm->mpid,
467 cfm->fault ? "fault" : "");
469 ds_put_format(&ds, "\tinterval: %dms\n", cfm->ccm_interval_ms);
470 ds_put_format(&ds, "\tnext CCM tx: %lldms\n",
471 timer_msecs_until_expired(&cfm->tx_timer));
472 ds_put_format(&ds, "\tnext fault check: %lldms\n",
473 timer_msecs_until_expired(&cfm->fault_timer));
475 ds_put_cstr(&ds, "\n");
476 HMAP_FOR_EACH (rmp, node, &cfm->remote_mps) {
477 ds_put_format(&ds, "Remote MPID %"PRIu16": %s\n", rmp->mpid,
478 rmp->fault ? "fault" : "");
479 ds_put_format(&ds, "\trecv since check: %s",
480 rmp->recv ? "true" : "false");
483 unixctl_command_reply(conn, 200, ds_cstr(&ds));