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 CCM_OPCODE 1 /* CFM message opcode meaning CCM. */
43 struct list list_node; /* Node in all_cfms list. */
45 bool fault; /* Indicates connectivity fault. */
46 char *name; /* Name of this CFM object. */
48 uint32_t seq; /* The sequence number of our last CCM. */
49 uint8_t ccm_interval; /* The CCM transmission interval. */
50 int ccm_interval_ms; /* 'ccm_interval' in milliseconds. */
51 uint8_t maid[CCM_MAID_LEN]; /* The MAID of this CFM. */
53 struct timer tx_timer; /* Send CCM when expired. */
54 struct timer fault_timer; /* Check for faults when expired. */
56 struct hmap remote_mps; /* Expected remote MPs. */
59 /* Remote MPs represent foreign network entities that are configured to have
60 * the same MAID as this CFM instance. */
62 uint16_t mpid; /* The Maintenance Point ID of this 'remote_mp'. */
63 struct hmap_node node; /* Node in 'remote_mps' map. */
65 bool recv; /* CCM was received since last fault check. */
66 bool fault; /* Indicates a connectivity fault. */
69 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
70 static struct list all_cfms = LIST_INITIALIZER(&all_cfms);
72 static void cfm_unixctl_show(struct unixctl_conn *, const char *args,
76 cfm_generate_maid(struct cfm *cfm)
78 const char *ovs_md_name = "ovs_md";
79 const char *ovs_ma_name = "ovs_ma";
81 size_t md_len, ma_len;
83 memset(cfm->maid, 0, CCM_MAID_LEN);
85 md_len = strlen(ovs_md_name);
86 ma_len = strlen(ovs_ma_name);
88 assert(md_len && ma_len && md_len + ma_len + 4 <= CCM_MAID_LEN);
90 cfm->maid[0] = 4; /* MD name string format. */
91 cfm->maid[1] = md_len; /* MD name size. */
92 memcpy(&cfm->maid[2], ovs_md_name, md_len); /* MD name. */
94 ma_p = cfm->maid + 2 + md_len;
95 ma_p[0] = 2; /* MA name string format. */
96 ma_p[1] = ma_len; /* MA name size. */
97 memcpy(&ma_p[2], ovs_ma_name, ma_len); /* MA name. */
101 ccm_interval_to_ms(uint8_t interval)
104 case 0: NOT_REACHED(); /* Explicitly not supported by 802.1ag. */
105 case 1: return 3; /* Not recommended due to timer resolution. */
106 case 2: return 10; /* Not recommended due to timer resolution. */
109 case 5: return 10000;
110 case 6: return 60000;
111 case 7: return 600000;
112 default: NOT_REACHED(); /* Explicitly not supported by 802.1ag. */
119 cfm_fault_interval(struct cfm *cfm)
121 /* According to the 802.1ag specification we should assume every other MP
122 * with the same MAID has the same transmission interval that we have. If
123 * an MP has a different interval, cfm_process_heartbeat will register it
124 * as a fault (likely due to a configuration error). Thus we can check all
125 * MPs at once making this quite a bit simpler.
127 * According to the specification we should check when (ccm_interval_ms *
128 * 3.5)ms have passed. */
129 return (cfm->ccm_interval_ms * 7) / 2;
133 ms_to_ccm_interval(int interval_ms)
137 for (i = 7; i > 0; i--) {
138 if (ccm_interval_to_ms(i) <= interval_ms) {
147 hash_mpid(uint8_t mpid)
149 return hash_int(mpid, 0);
153 cfm_is_valid_mpid(uint32_t mpid)
155 /* 802.1ag specification requires MPIDs to be within the range [1, 8191] */
156 return mpid >= 1 && mpid <= 8191;
159 static struct remote_mp *
160 lookup_remote_mp(const struct hmap *hmap, uint16_t mpid)
162 struct remote_mp *rmp;
164 HMAP_FOR_EACH_IN_BUCKET (rmp, node, hash_mpid(mpid), hmap) {
165 if (rmp->mpid == mpid) {
176 unixctl_command_register("cfm/show", cfm_unixctl_show, NULL);
179 /* Allocates a 'cfm' object. This object should have its 'mpid', 'maid',
180 * 'eth_src', and 'interval' filled out. cfm_configure() should be called
181 * whenever changes are made to 'cfm', and before cfm_run() is called for the
188 cfm = xzalloc(sizeof *cfm);
189 hmap_init(&cfm->remote_mps);
190 cfm_generate_maid(cfm);
191 list_push_back(&all_cfms, &cfm->list_node);
196 cfm_destroy(struct cfm *cfm)
198 struct remote_mp *rmp, *rmp_next;
204 HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &cfm->remote_mps) {
205 hmap_remove(&cfm->remote_mps, &rmp->node);
209 hmap_destroy(&cfm->remote_mps);
210 list_remove(&cfm->list_node);
214 /* Should be run periodically to update fault statistics messages. */
216 cfm_run(struct cfm *cfm)
219 if (timer_expired(&cfm->fault_timer)) {
220 long long int interval = cfm_fault_interval(cfm);
221 struct remote_mp *rmp;
224 HMAP_FOR_EACH (rmp, node, &cfm->remote_mps) {
225 rmp->fault = !rmp->recv;
230 VLOG_DBG("No CCM from RMP %"PRIu16" in the last %lldms",
231 rmp->mpid, interval);
236 VLOG_DBG("All RMPs received CCMs in the last %lldms", interval);
239 timer_set_duration(&cfm->fault_timer, interval);
243 /* Should be run periodically to check if the CFM module has a CCM message it
246 cfm_should_send_ccm(struct cfm *cfm)
249 return timer_expired(&cfm->tx_timer);
252 /* Composes a CCM message into 'ccm'. Messages generated with this function
253 * should be sent whenever cfm_should_send_ccm() indicates. */
255 cfm_compose_ccm(struct cfm *cfm, struct ccm *ccm)
257 timer_set_duration(&cfm->tx_timer, cfm->ccm_interval_ms);
259 ccm->mdlevel_version = 0;
260 ccm->opcode = CCM_OPCODE;
261 ccm->tlv_offset = 70;
262 ccm->seq = htonl(++cfm->seq);
263 ccm->mpid = htons(cfm->mpid);
264 ccm->flags = cfm->ccm_interval;
265 memcpy(ccm->maid, cfm->maid, sizeof ccm->maid);
269 cfm_wait(struct cfm *cfm)
272 timer_wait(&cfm->tx_timer);
273 timer_wait(&cfm->fault_timer);
276 /* Configures 'cfm' with settings from 's'. */
278 cfm_configure(struct cfm *cfm, const struct cfm_settings *s)
282 struct hmap new_rmps;
283 struct remote_mp *rmp, *rmp_next;
285 if (!cfm_is_valid_mpid(s->mpid) || s->interval <= 0
286 || s->n_remote_mpids <= 0) {
291 interval = ms_to_ccm_interval(s->interval);
293 if (!cfm->name || strcmp(s->name, cfm->name)) {
295 cfm->name = xstrdup(s->name);
298 if (interval != cfm->ccm_interval) {
299 cfm->ccm_interval = interval;
300 cfm->ccm_interval_ms = ccm_interval_to_ms(interval);
302 timer_set_expired(&cfm->tx_timer);
303 timer_set_duration(&cfm->fault_timer, cfm_fault_interval(cfm));
306 hmap_init(&new_rmps);
307 for (i = 0; i < s->n_remote_mpids; i++) {
308 uint16_t mpid = s->remote_mpids[i];
310 if (!cfm_is_valid_mpid(mpid)
311 || lookup_remote_mp(&new_rmps, mpid)) {
315 if ((rmp = lookup_remote_mp(&cfm->remote_mps, mpid))) {
316 hmap_remove(&cfm->remote_mps, &rmp->node);
318 rmp = xzalloc(sizeof *rmp);
322 hmap_insert(&new_rmps, &rmp->node, hash_mpid(mpid));
325 hmap_swap(&new_rmps, &cfm->remote_mps);
326 HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &new_rmps) {
327 hmap_remove(&new_rmps, &rmp->node);
330 hmap_destroy(&new_rmps);
335 /* Returns true if the CFM library should process packets from 'flow'. */
337 cfm_should_process_flow(const struct flow *flow)
339 return (ntohs(flow->dl_type) == ETH_TYPE_CFM
340 && eth_addr_equals(flow->dl_dst, eth_addr_ccm));
343 /* Updates internal statistics relevant to packet 'p'. Should be called on
344 * every packet whose flow returned true when passed to
345 * cfm_should_process_flow. */
347 cfm_process_heartbeat(struct cfm *cfm, const struct ofpbuf *p)
351 uint8_t ccm_interval;
352 struct remote_mp *rmp;
353 struct eth_header *eth;
356 ccm = ofpbuf_at(p, (uint8_t *)p->l3 - (uint8_t *)p->data, CCM_LEN);
359 VLOG_INFO_RL(&rl, "Received an un-parseable 802.1ag CCM heartbeat.");
363 if (ccm->opcode != CCM_OPCODE) {
364 VLOG_INFO_RL(&rl, "Received an unsupported 802.1ag message. "
365 "(opcode %u)", ccm->opcode);
369 /* According to the 802.1ag specification, reception of a CCM with an
370 * incorrect ccm_interval, unexpected MAID, or unexpected MPID should
371 * trigger a fault. We ignore this requirement for several reasons.
373 * Faults can cause a controller or Open vSwitch to make potentially
374 * expensive changes to the network topology. It seems prudent to trigger
375 * them judiciously, especially when CFM is used to check slave status of
376 * bonds. Furthermore, faults can be maliciously triggered by crafting
378 if (memcmp(ccm->maid, cfm->maid, sizeof ccm->maid)) {
379 VLOG_WARN_RL(&rl, "Received unexpected remote MAID from MAC "
380 ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_src));
382 ccm_mpid = ntohs(ccm->mpid);
383 ccm_interval = ccm->flags & 0x7;
385 rmp = lookup_remote_mp(&cfm->remote_mps, ccm_mpid);
390 if (ccm_interval != cfm->ccm_interval) {
391 VLOG_WARN_RL(&rl, "received a CCM with an invalid interval"
392 " (%"PRIu8") from RMP %"PRIu16, ccm_interval,
396 VLOG_WARN_RL(&rl, "Received unexpected remote MPID %d from MAC "
397 ETH_ADDR_FMT, ccm_mpid, ETH_ADDR_ARGS(eth->eth_src));
400 VLOG_DBG("Received CCM (mpid %"PRIu16") (interval %"PRIu8")", ccm_mpid,
405 /* Gets the fault status of 'cfm'. Returns true when 'cfm' has detected
406 * connectivity problems, false otherwise. */
408 cfm_get_fault(const struct cfm *cfm)
414 cfm_find(const char *name)
418 LIST_FOR_EACH (cfm, list_node, &all_cfms) {
419 if (cfm->name && !strcmp(cfm->name, name)) {
427 cfm_unixctl_show(struct unixctl_conn *conn,
428 const char *args, void *aux OVS_UNUSED)
430 struct ds ds = DS_EMPTY_INITIALIZER;
431 const struct cfm *cfm;
432 struct remote_mp *rmp;
434 cfm = cfm_find(args);
436 unixctl_command_reply(conn, 501, "no such CFM object");
440 ds_put_format(&ds, "MPID %"PRIu16": %s\n", cfm->mpid,
441 cfm->fault ? "fault" : "");
443 ds_put_format(&ds, "\tinterval: %dms\n", cfm->ccm_interval_ms);
444 ds_put_format(&ds, "\tnext CCM tx: %lldms\n",
445 timer_msecs_until_expired(&cfm->tx_timer));
446 ds_put_format(&ds, "\tnext fault check: %lldms\n",
447 timer_msecs_until_expired(&cfm->fault_timer));
449 ds_put_cstr(&ds, "\n");
450 HMAP_FOR_EACH (rmp, node, &cfm->remote_mps) {
451 ds_put_format(&ds, "Remote MPID %"PRIu16": %s\n", rmp->mpid,
452 rmp->fault ? "fault" : "");
453 ds_put_format(&ds, "\trecv since check: %s",
454 rmp->recv ? "true" : "false");
457 unixctl_command_reply(conn, 200, ds_cstr(&ds));