]>
git.proxmox.com Git - mirror_ovs.git/blob - lib/cfm.c
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. */
50 #define CCM_RDI_MASK 0x80
52 uint8_t mdlevel_version
; /* MD Level and Version */
58 uint8_t maid
[CCM_MAID_LEN
];
59 uint8_t zero
[16]; /* Defined by ITU-T Y.1731 should be zero */
60 } __attribute__((packed
));
61 BUILD_ASSERT_DECL(CCM_LEN
== sizeof(struct ccm
));
64 char *name
; /* Name of this CFM object. */
65 struct hmap_node hmap_node
; /* Node in all_cfms list. */
68 bool fault
; /* Indicates connectivity fault. */
69 bool recv_fault
; /* Indicates an inability to receive CCMs. */
71 uint32_t seq
; /* The sequence number of our last CCM. */
72 uint8_t ccm_interval
; /* The CCM transmission interval. */
73 int ccm_interval_ms
; /* 'ccm_interval' in milliseconds. */
74 uint8_t maid
[CCM_MAID_LEN
]; /* The MAID of this CFM. */
76 struct timer tx_timer
; /* Send CCM when expired. */
77 struct timer fault_timer
; /* Check for faults when expired. */
79 struct hmap remote_mps
; /* Expected remote MPs. */
82 /* Remote MPs represent foreign network entities that are configured to have
83 * the same MAID as this CFM instance. */
85 uint16_t mpid
; /* The Maintenance Point ID of this 'remote_mp'. */
86 struct hmap_node node
; /* Node in 'remote_mps' map. */
88 bool recv
; /* CCM was received since last fault check. */
89 bool fault
; /* Indicates a connectivity fault. */
90 bool rdi
; /* Remote Defect Indicator. Indicates remote_mp isn't
91 receiving CCMs that it's expecting to. */
94 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 20);
95 static struct hmap all_cfms
= HMAP_INITIALIZER(&all_cfms
);
97 static void cfm_unixctl_show(struct unixctl_conn
*, const char *args
,
101 cfm_generate_maid(struct cfm
*cfm
)
103 const char *ovs_md_name
= "ovs";
104 const char *ovs_ma_name
= "ovs";
106 size_t md_len
, ma_len
;
108 memset(cfm
->maid
, 0, CCM_MAID_LEN
);
110 md_len
= strlen(ovs_md_name
);
111 ma_len
= strlen(ovs_ma_name
);
113 assert(md_len
&& ma_len
&& md_len
+ ma_len
+ 4 <= CCM_MAID_LEN
);
115 cfm
->maid
[0] = 4; /* MD name string format. */
116 cfm
->maid
[1] = md_len
; /* MD name size. */
117 memcpy(&cfm
->maid
[2], ovs_md_name
, md_len
); /* MD name. */
119 ma_p
= cfm
->maid
+ 2 + md_len
;
120 ma_p
[0] = 2; /* MA name string format. */
121 ma_p
[1] = ma_len
; /* MA name size. */
122 memcpy(&ma_p
[2], ovs_ma_name
, ma_len
); /* MA name. */
126 ccm_interval_to_ms(uint8_t interval
)
129 case 0: NOT_REACHED(); /* Explicitly not supported by 802.1ag. */
130 case 1: return 3; /* Not recommended due to timer resolution. */
131 case 2: return 10; /* Not recommended due to timer resolution. */
134 case 5: return 10000;
135 case 6: return 60000;
136 case 7: return 600000;
137 default: NOT_REACHED(); /* Explicitly not supported by 802.1ag. */
144 cfm_fault_interval(struct cfm
*cfm
)
146 /* According to the 802.1ag specification we should assume every other MP
147 * with the same MAID has the same transmission interval that we have. If
148 * an MP has a different interval, cfm_process_heartbeat will register it
149 * as a fault (likely due to a configuration error). Thus we can check all
150 * MPs at once making this quite a bit simpler.
152 * According to the specification we should check when (ccm_interval_ms *
153 * 3.5)ms have passed. */
154 return (cfm
->ccm_interval_ms
* 7) / 2;
158 ms_to_ccm_interval(int interval_ms
)
162 for (i
= 7; i
> 0; i
--) {
163 if (ccm_interval_to_ms(i
) <= interval_ms
) {
172 hash_mpid(uint8_t mpid
)
174 return hash_int(mpid
, 0);
178 cfm_is_valid_mpid(uint32_t mpid
)
180 /* 802.1ag specification requires MPIDs to be within the range [1, 8191] */
181 return mpid
>= 1 && mpid
<= 8191;
184 static struct remote_mp
*
185 lookup_remote_mp(const struct hmap
*hmap
, uint16_t mpid
)
187 struct remote_mp
*rmp
;
189 HMAP_FOR_EACH_IN_BUCKET (rmp
, node
, hash_mpid(mpid
), hmap
) {
190 if (rmp
->mpid
== mpid
) {
201 unixctl_command_register("cfm/show", cfm_unixctl_show
, NULL
);
204 /* Allocates a 'cfm' object called 'name'. 'cfm' should be initialized by
205 * cfm_configure() before use. */
207 cfm_create(const char *name
)
211 cfm
= xzalloc(sizeof *cfm
);
212 cfm
->name
= xstrdup(name
);
213 hmap_init(&cfm
->remote_mps
);
214 cfm_generate_maid(cfm
);
215 hmap_insert(&all_cfms
, &cfm
->hmap_node
, hash_string(cfm
->name
, 0));
220 cfm_destroy(struct cfm
*cfm
)
222 struct remote_mp
*rmp
, *rmp_next
;
228 HMAP_FOR_EACH_SAFE (rmp
, rmp_next
, node
, &cfm
->remote_mps
) {
229 hmap_remove(&cfm
->remote_mps
, &rmp
->node
);
233 hmap_destroy(&cfm
->remote_mps
);
234 hmap_remove(&all_cfms
, &cfm
->hmap_node
);
239 /* Should be run periodically to update fault statistics messages. */
241 cfm_run(struct cfm
*cfm
)
243 if (timer_expired(&cfm
->fault_timer
)) {
244 long long int interval
= cfm_fault_interval(cfm
);
245 struct remote_mp
*rmp
;
248 cfm
->recv_fault
= false;
249 HMAP_FOR_EACH (rmp
, node
, &cfm
->remote_mps
) {
250 rmp
->fault
= !rmp
->recv
;
254 cfm
->recv_fault
= true;
255 VLOG_DBG("%s: No CCM from RMP %"PRIu16
" in the last %lldms",
256 cfm
->name
, rmp
->mpid
, interval
);
257 } else if (rmp
->rdi
) {
259 VLOG_DBG("%s: RDI bit flagged from RMP %"PRIu16
, cfm
->name
,
264 if (cfm
->recv_fault
) {
267 VLOG_DBG("%s: All RMPs received CCMs in the last %lldms",
268 cfm
->name
, interval
);
271 timer_set_duration(&cfm
->fault_timer
, interval
);
275 /* Should be run periodically to check if the CFM module has a CCM message it
278 cfm_should_send_ccm(struct cfm
*cfm
)
280 return timer_expired(&cfm
->tx_timer
);
283 /* Composes a CCM message into 'packet'. Messages generated with this function
284 * should be sent whenever cfm_should_send_ccm() indicates. */
286 cfm_compose_ccm(struct cfm
*cfm
, struct ofpbuf
*packet
,
287 uint8_t eth_src
[ETH_ADDR_LEN
])
291 timer_set_duration(&cfm
->tx_timer
, cfm
->ccm_interval_ms
);
293 ccm
= eth_compose(packet
, eth_addr_ccm
, eth_src
, ETH_TYPE_CFM
,
295 ccm
->mdlevel_version
= 0;
296 ccm
->opcode
= CCM_OPCODE
;
297 ccm
->tlv_offset
= 70;
298 ccm
->seq
= htonl(++cfm
->seq
);
299 ccm
->mpid
= htons(cfm
->mpid
);
300 ccm
->flags
= cfm
->ccm_interval
;
301 memcpy(ccm
->maid
, cfm
->maid
, sizeof ccm
->maid
);
302 memset(ccm
->zero
, 0, sizeof ccm
->zero
);
304 if (cfm
->recv_fault
) {
305 ccm
->flags
|= CCM_RDI_MASK
;
310 cfm_wait(struct cfm
*cfm
)
313 timer_wait(&cfm
->tx_timer
);
314 timer_wait(&cfm
->fault_timer
);
317 /* Configures 'cfm' with settings from 's'. */
319 cfm_configure(struct cfm
*cfm
, const struct cfm_settings
*s
)
323 struct hmap new_rmps
;
324 struct remote_mp
*rmp
, *rmp_next
;
326 if (!cfm_is_valid_mpid(s
->mpid
) || s
->interval
<= 0
327 || s
->n_remote_mpids
<= 0) {
332 interval
= ms_to_ccm_interval(s
->interval
);
334 if (interval
!= cfm
->ccm_interval
) {
335 cfm
->ccm_interval
= interval
;
336 cfm
->ccm_interval_ms
= ccm_interval_to_ms(interval
);
338 timer_set_expired(&cfm
->tx_timer
);
339 timer_set_duration(&cfm
->fault_timer
, cfm_fault_interval(cfm
));
342 hmap_init(&new_rmps
);
343 for (i
= 0; i
< s
->n_remote_mpids
; i
++) {
344 uint16_t mpid
= s
->remote_mpids
[i
];
346 if (!cfm_is_valid_mpid(mpid
)
347 || lookup_remote_mp(&new_rmps
, mpid
)) {
351 if ((rmp
= lookup_remote_mp(&cfm
->remote_mps
, mpid
))) {
352 hmap_remove(&cfm
->remote_mps
, &rmp
->node
);
354 rmp
= xzalloc(sizeof *rmp
);
358 hmap_insert(&new_rmps
, &rmp
->node
, hash_mpid(mpid
));
361 hmap_swap(&new_rmps
, &cfm
->remote_mps
);
362 HMAP_FOR_EACH_SAFE (rmp
, rmp_next
, node
, &new_rmps
) {
363 hmap_remove(&new_rmps
, &rmp
->node
);
366 hmap_destroy(&new_rmps
);
371 /* Returns true if the CFM library should process packets from 'flow'. */
373 cfm_should_process_flow(const struct flow
*flow
)
375 return (ntohs(flow
->dl_type
) == ETH_TYPE_CFM
376 && eth_addr_equals(flow
->dl_dst
, eth_addr_ccm
));
379 /* Updates internal statistics relevant to packet 'p'. Should be called on
380 * every packet whose flow returned true when passed to
381 * cfm_should_process_flow. */
383 cfm_process_heartbeat(struct cfm
*cfm
, const struct ofpbuf
*p
)
388 uint8_t ccm_interval
;
389 struct remote_mp
*rmp
;
390 struct eth_header
*eth
;
393 ccm
= ofpbuf_at(p
, (uint8_t *)p
->l3
- (uint8_t *)p
->data
, CCM_LEN
);
396 VLOG_INFO_RL(&rl
, "%s: Received an unparseable 802.1ag CCM heartbeat.",
401 if (ccm
->opcode
!= CCM_OPCODE
) {
402 VLOG_INFO_RL(&rl
, "%s: Received an unsupported 802.1ag message. "
403 "(opcode %u)", cfm
->name
, ccm
->opcode
);
407 /* According to the 802.1ag specification, reception of a CCM with an
408 * incorrect ccm_interval, unexpected MAID, or unexpected MPID should
409 * trigger a fault. We ignore this requirement for several reasons.
411 * Faults can cause a controller or Open vSwitch to make potentially
412 * expensive changes to the network topology. It seems prudent to trigger
413 * them judiciously, especially when CFM is used to check slave status of
414 * bonds. Furthermore, faults can be maliciously triggered by crafting
416 if (memcmp(ccm
->maid
, cfm
->maid
, sizeof ccm
->maid
)) {
417 VLOG_WARN_RL(&rl
, "%s: Received unexpected remote MAID from MAC "
418 ETH_ADDR_FMT
, cfm
->name
, ETH_ADDR_ARGS(eth
->eth_src
));
420 ccm_mpid
= ntohs(ccm
->mpid
);
421 ccm_interval
= ccm
->flags
& 0x7;
422 ccm_rdi
= ccm
->flags
& CCM_RDI_MASK
;
424 rmp
= lookup_remote_mp(&cfm
->remote_mps
, ccm_mpid
);
430 if (ccm_interval
!= cfm
->ccm_interval
) {
431 VLOG_WARN_RL(&rl
, "%s: received a CCM with an invalid interval"
432 " (%"PRIu8
") from RMP %"PRIu16
, cfm
->name
,
433 ccm_interval
, rmp
->mpid
);
436 VLOG_WARN_RL(&rl
, "%s: Received unexpected remote MPID %d from"
437 " MAC " ETH_ADDR_FMT
, cfm
->name
, ccm_mpid
,
438 ETH_ADDR_ARGS(eth
->eth_src
));
441 VLOG_DBG("%s: Received CCM (seq %"PRIu32
") (mpid %"PRIu16
")"
442 " (interval %"PRIu8
") (RDI %s)", cfm
->name
, ntohl(ccm
->seq
),
443 ccm_mpid
, ccm_interval
, ccm_rdi
? "true" : "false");
447 /* Gets the fault status of 'cfm'. Returns true when 'cfm' has detected
448 * connectivity problems, false otherwise. */
450 cfm_get_fault(const struct cfm
*cfm
)
456 cfm_find(const char *name
)
460 HMAP_FOR_EACH_WITH_HASH (cfm
, hmap_node
, hash_string(name
, 0), &all_cfms
) {
461 if (!strcmp(cfm
->name
, name
)) {
469 cfm_unixctl_show(struct unixctl_conn
*conn
,
470 const char *args
, void *aux OVS_UNUSED
)
472 struct ds ds
= DS_EMPTY_INITIALIZER
;
473 const struct cfm
*cfm
;
474 struct remote_mp
*rmp
;
476 cfm
= cfm_find(args
);
478 unixctl_command_reply(conn
, 501, "no such CFM object");
482 ds_put_format(&ds
, "MPID %"PRIu16
":%s%s\n", cfm
->mpid
,
483 cfm
->fault
? " fault" : "",
484 cfm
->recv_fault
? " recv_fault" : "");
486 ds_put_format(&ds
, "\tinterval: %dms\n", cfm
->ccm_interval_ms
);
487 ds_put_format(&ds
, "\tnext CCM tx: %lldms\n",
488 timer_msecs_until_expired(&cfm
->tx_timer
));
489 ds_put_format(&ds
, "\tnext fault check: %lldms\n",
490 timer_msecs_until_expired(&cfm
->fault_timer
));
492 ds_put_cstr(&ds
, "\n");
493 HMAP_FOR_EACH (rmp
, node
, &cfm
->remote_mps
) {
494 ds_put_format(&ds
, "Remote MPID %"PRIu16
": %s\n", rmp
->mpid
,
495 rmp
->fault
? "fault" : "");
496 ds_put_format(&ds
, "\trecv since check: %s",
497 rmp
->recv
? "true" : "false");
500 unixctl_command_reply(conn
, 200, ds_cstr(&ds
));