]>
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 "byte-order.h"
26 #include "dynamic-string.h"
32 #include "poll-loop.h"
38 VLOG_DEFINE_THIS_MODULE(cfm
);
40 #define CFM_MAX_RMPS 256
42 /* Ethernet destination address of CCM packets. */
43 static const uint8_t eth_addr_ccm
[6] = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x30 };
44 static const uint8_t eth_addr_ccm_x
[6] = {
45 0x01, 0x23, 0x20, 0x00, 0x00, 0x30
48 #define ETH_TYPE_CFM 0x8902
50 /* A 'ccm' represents a Continuity Check Message from the 802.1ag
51 * specification. Continuity Check Messages are broadcast periodically so that
52 * hosts can determine whom they have connectivity to.
54 * The minimum length of a CCM as specified by IEEE 802.1ag is 75 bytes.
55 * Previous versions of Open vSwitch generated 74-byte CCM messages, so we
56 * accept such messages too. */
58 #define CCM_ACCEPT_LEN 74
59 #define CCM_MAID_LEN 48
60 #define CCM_OPCODE 1 /* CFM message opcode meaning CCM. */
61 #define CCM_RDI_MASK 0x80
63 uint8_t mdlevel_version
; /* MD Level and Version */
69 uint8_t maid
[CCM_MAID_LEN
];
71 /* Defined by ITU-T Y.1731 should be zero */
72 ovs_be16 interval_ms_x
; /* Transmission interval in ms. */
73 ovs_be64 mpid64
; /* MPID in extended mode. */
74 uint8_t opdown
; /* Operationally down. */
79 } __attribute__((packed
));
80 BUILD_ASSERT_DECL(CCM_LEN
== sizeof(struct ccm
));
83 char *name
; /* Name of this CFM object. */
84 struct hmap_node hmap_node
; /* Node in all_cfms list. */
87 bool extended
; /* Extended mode. */
88 bool fault
; /* Indicates connectivity fault. */
89 bool unexpected_recv
; /* Received an unexpected CCM. */
90 bool opup
; /* Operational State. */
91 bool remote_opup
; /* Remote Operational State. */
93 uint32_t seq
; /* The sequence number of our last CCM. */
94 uint8_t ccm_interval
; /* The CCM transmission interval. */
95 int ccm_interval_ms
; /* 'ccm_interval' in milliseconds. */
96 uint16_t ccm_vlan
; /* Vlan tag of CCM PDUs. */
97 uint8_t maid
[CCM_MAID_LEN
]; /* The MAID of this CFM. */
99 struct timer tx_timer
; /* Send CCM when expired. */
100 struct timer fault_timer
; /* Check for faults when expired. */
102 struct hmap remote_mps
; /* Remote MPs. */
104 /* Result of cfm_get_remote_mpids(). Updated only during fault check to
106 uint64_t *rmps_array
; /* Cache of remote_mps. */
107 size_t rmps_array_len
; /* Number of rmps in 'rmps_array'. */
110 /* Remote MPs represent foreign network entities that are configured to have
111 * the same MAID as this CFM instance. */
113 uint64_t mpid
; /* The Maintenance Point ID of this 'remote_mp'. */
114 struct hmap_node node
; /* Node in 'remote_mps' map. */
116 bool recv
; /* CCM was received since last fault check. */
117 bool rdi
; /* Remote Defect Indicator. Indicates remote_mp isn't
118 receiving CCMs that it's expecting to. */
119 bool opup
; /* Operational State. */
122 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 20);
123 static struct hmap all_cfms
= HMAP_INITIALIZER(&all_cfms
);
125 static void cfm_unixctl_show(struct unixctl_conn
*, const char *args
,
128 static const uint8_t *
129 cfm_ccm_addr(const struct cfm
*cfm
)
131 return cfm
->extended
? eth_addr_ccm_x
: eth_addr_ccm
;
135 cfm_generate_maid(struct cfm
*cfm
)
137 const char *ovs_md_name
= "ovs";
138 const char *ovs_ma_name
= "ovs";
140 size_t md_len
, ma_len
;
142 memset(cfm
->maid
, 0, CCM_MAID_LEN
);
144 md_len
= strlen(ovs_md_name
);
145 ma_len
= strlen(ovs_ma_name
);
147 assert(md_len
&& ma_len
&& md_len
+ ma_len
+ 4 <= CCM_MAID_LEN
);
149 cfm
->maid
[0] = 4; /* MD name string format. */
150 cfm
->maid
[1] = md_len
; /* MD name size. */
151 memcpy(&cfm
->maid
[2], ovs_md_name
, md_len
); /* MD name. */
153 ma_p
= cfm
->maid
+ 2 + md_len
;
154 ma_p
[0] = 2; /* MA name string format. */
155 ma_p
[1] = ma_len
; /* MA name size. */
156 memcpy(&ma_p
[2], ovs_ma_name
, ma_len
); /* MA name. */
160 ccm_interval_to_ms(uint8_t interval
)
163 case 0: NOT_REACHED(); /* Explicitly not supported by 802.1ag. */
164 case 1: return 3; /* Not recommended due to timer resolution. */
165 case 2: return 10; /* Not recommended due to timer resolution. */
168 case 5: return 10000;
169 case 6: return 60000;
170 case 7: return 600000;
171 default: NOT_REACHED(); /* Explicitly not supported by 802.1ag. */
178 cfm_fault_interval(struct cfm
*cfm
)
180 /* According to the 802.1ag specification we should assume every other MP
181 * with the same MAID has the same transmission interval that we have. If
182 * an MP has a different interval, cfm_process_heartbeat will register it
183 * as a fault (likely due to a configuration error). Thus we can check all
184 * MPs at once making this quite a bit simpler.
186 * According to the specification we should check when (ccm_interval_ms *
187 * 3.5)ms have passed. */
188 return (cfm
->ccm_interval_ms
* 7) / 2;
192 ms_to_ccm_interval(int interval_ms
)
196 for (i
= 7; i
> 0; i
--) {
197 if (ccm_interval_to_ms(i
) <= interval_ms
) {
206 hash_mpid(uint64_t mpid
)
208 return hash_bytes(&mpid
, sizeof mpid
, 0);
212 cfm_is_valid_mpid(bool extended
, uint64_t mpid
)
214 /* 802.1ag specification requires MPIDs to be within the range [1, 8191].
215 * In extended mode we relax this requirement. */
216 return mpid
>= 1 && (extended
|| mpid
<= 8191);
219 static struct remote_mp
*
220 lookup_remote_mp(const struct cfm
*cfm
, uint64_t mpid
)
222 struct remote_mp
*rmp
;
224 HMAP_FOR_EACH_IN_BUCKET (rmp
, node
, hash_mpid(mpid
), &cfm
->remote_mps
) {
225 if (rmp
->mpid
== mpid
) {
236 unixctl_command_register("cfm/show", "[interface]", cfm_unixctl_show
,
240 /* Allocates a 'cfm' object called 'name'. 'cfm' should be initialized by
241 * cfm_configure() before use. */
243 cfm_create(const char *name
)
247 cfm
= xzalloc(sizeof *cfm
);
248 cfm
->name
= xstrdup(name
);
249 hmap_init(&cfm
->remote_mps
);
250 cfm_generate_maid(cfm
);
251 hmap_insert(&all_cfms
, &cfm
->hmap_node
, hash_string(cfm
->name
, 0));
252 cfm
->remote_opup
= true;
257 cfm_destroy(struct cfm
*cfm
)
259 struct remote_mp
*rmp
, *rmp_next
;
265 HMAP_FOR_EACH_SAFE (rmp
, rmp_next
, node
, &cfm
->remote_mps
) {
266 hmap_remove(&cfm
->remote_mps
, &rmp
->node
);
270 hmap_destroy(&cfm
->remote_mps
);
271 hmap_remove(&all_cfms
, &cfm
->hmap_node
);
272 free(cfm
->rmps_array
);
277 /* Should be run periodically to update fault statistics messages. */
279 cfm_run(struct cfm
*cfm
)
281 if (timer_expired(&cfm
->fault_timer
)) {
282 long long int interval
= cfm_fault_interval(cfm
);
283 struct remote_mp
*rmp
, *rmp_next
;
285 cfm
->fault
= cfm
->unexpected_recv
;
286 cfm
->unexpected_recv
= false;
288 cfm
->rmps_array_len
= 0;
289 free(cfm
->rmps_array
);
290 cfm
->rmps_array
= xmalloc(hmap_count(&cfm
->remote_mps
) *
291 sizeof *cfm
->rmps_array
);
293 cfm
->remote_opup
= true;
294 HMAP_FOR_EACH_SAFE (rmp
, rmp_next
, node
, &cfm
->remote_mps
) {
297 VLOG_DBG("%s: no CCM from RMP %"PRIu64
" in the last %lldms",
298 cfm
->name
, rmp
->mpid
, interval
);
299 hmap_remove(&cfm
->remote_mps
, &rmp
->node
);
304 if (rmp
->mpid
== cfm
->mpid
) {
305 VLOG_WARN_RL(&rl
,"%s: received CCM with local MPID"
306 " %"PRIu64
, cfm
->name
, rmp
->mpid
);
311 VLOG_DBG("%s: RDI bit flagged from RMP %"PRIu64
, cfm
->name
,
317 cfm
->remote_opup
= rmp
->opup
;
320 cfm
->rmps_array
[cfm
->rmps_array_len
++] = rmp
->mpid
;
324 if (hmap_is_empty(&cfm
->remote_mps
)) {
328 timer_set_duration(&cfm
->fault_timer
, interval
);
332 /* Should be run periodically to check if the CFM module has a CCM message it
335 cfm_should_send_ccm(struct cfm
*cfm
)
337 return timer_expired(&cfm
->tx_timer
);
340 /* Composes a CCM message into 'packet'. Messages generated with this function
341 * should be sent whenever cfm_should_send_ccm() indicates. */
343 cfm_compose_ccm(struct cfm
*cfm
, struct ofpbuf
*packet
,
344 uint8_t eth_src
[ETH_ADDR_LEN
])
348 timer_set_duration(&cfm
->tx_timer
, cfm
->ccm_interval_ms
);
349 eth_compose(packet
, cfm_ccm_addr(cfm
), eth_src
, ETH_TYPE_CFM
, sizeof *ccm
);
352 eth_push_vlan(packet
, htons(cfm
->ccm_vlan
));
356 ccm
->mdlevel_version
= 0;
357 ccm
->opcode
= CCM_OPCODE
;
358 ccm
->tlv_offset
= 70;
359 ccm
->seq
= htonl(++cfm
->seq
);
360 ccm
->flags
= cfm
->ccm_interval
;
361 memcpy(ccm
->maid
, cfm
->maid
, sizeof ccm
->maid
);
362 memset(ccm
->zero
, 0, sizeof ccm
->zero
);
366 ccm
->mpid
= htons(hash_mpid(cfm
->mpid
));
367 ccm
->mpid64
= htonll(cfm
->mpid
);
368 ccm
->opdown
= !cfm
->opup
;
370 ccm
->mpid
= htons(cfm
->mpid
);
371 ccm
->mpid64
= htonll(0);
375 if (cfm
->ccm_interval
== 0) {
376 assert(cfm
->extended
);
377 ccm
->interval_ms_x
= htons(cfm
->ccm_interval_ms
);
380 if (hmap_is_empty(&cfm
->remote_mps
)) {
381 ccm
->flags
|= CCM_RDI_MASK
;
386 cfm_wait(struct cfm
*cfm
)
388 timer_wait(&cfm
->tx_timer
);
389 timer_wait(&cfm
->fault_timer
);
392 /* Configures 'cfm' with settings from 's'. */
394 cfm_configure(struct cfm
*cfm
, const struct cfm_settings
*s
)
399 if (!cfm_is_valid_mpid(s
->extended
, s
->mpid
) || s
->interval
<= 0) {
404 cfm
->extended
= s
->extended
;
406 interval
= ms_to_ccm_interval(s
->interval
);
407 interval_ms
= ccm_interval_to_ms(interval
);
409 cfm
->ccm_vlan
= s
->ccm_vlan
& VLAN_VID_MASK
;
410 if (cfm
->extended
&& interval_ms
!= s
->interval
) {
412 interval_ms
= MIN(s
->interval
, UINT16_MAX
);
415 if (interval
!= cfm
->ccm_interval
|| interval_ms
!= cfm
->ccm_interval_ms
) {
416 cfm
->ccm_interval
= interval
;
417 cfm
->ccm_interval_ms
= interval_ms
;
419 timer_set_expired(&cfm
->tx_timer
);
420 timer_set_duration(&cfm
->fault_timer
, cfm_fault_interval(cfm
));
426 /* Returns true if 'cfm' should process packets from 'flow'. */
428 cfm_should_process_flow(const struct cfm
*cfm
, const struct flow
*flow
)
430 return (ntohs(flow
->dl_type
) == ETH_TYPE_CFM
431 && eth_addr_equals(flow
->dl_dst
, cfm_ccm_addr(cfm
)));
434 /* Updates internal statistics relevant to packet 'p'. Should be called on
435 * every packet whose flow returned true when passed to
436 * cfm_should_process_flow. */
438 cfm_process_heartbeat(struct cfm
*cfm
, const struct ofpbuf
*p
)
441 struct eth_header
*eth
;
444 ccm
= ofpbuf_at(p
, (uint8_t *)p
->l3
- (uint8_t *)p
->data
, CCM_ACCEPT_LEN
);
447 VLOG_INFO_RL(&rl
, "%s: Received an unparseable 802.1ag CCM heartbeat.",
452 if (ccm
->opcode
!= CCM_OPCODE
) {
453 VLOG_INFO_RL(&rl
, "%s: Received an unsupported 802.1ag message. "
454 "(opcode %u)", cfm
->name
, ccm
->opcode
);
458 /* According to the 802.1ag specification, reception of a CCM with an
459 * incorrect ccm_interval, unexpected MAID, or unexpected MPID should
460 * trigger a fault. We ignore this requirement for several reasons.
462 * Faults can cause a controller or Open vSwitch to make potentially
463 * expensive changes to the network topology. It seems prudent to trigger
464 * them judiciously, especially when CFM is used to check slave status of
465 * bonds. Furthermore, faults can be maliciously triggered by crafting
467 if (memcmp(ccm
->maid
, cfm
->maid
, sizeof ccm
->maid
)) {
468 cfm
->unexpected_recv
= true;
469 VLOG_WARN_RL(&rl
, "%s: Received unexpected remote MAID from MAC "
470 ETH_ADDR_FMT
, cfm
->name
, ETH_ADDR_ARGS(eth
->eth_src
));
472 uint8_t ccm_interval
= ccm
->flags
& 0x7;
473 bool ccm_rdi
= ccm
->flags
& CCM_RDI_MASK
;
474 uint16_t ccm_interval_ms_x
= ntohs(ccm
->interval_ms_x
);
476 struct remote_mp
*rmp
;
481 ccm_mpid
= ntohll(ccm
->mpid64
);
482 ccm_opdown
= ccm
->opdown
;
484 ccm_mpid
= ntohs(ccm
->mpid
);
488 if (ccm_interval
!= cfm
->ccm_interval
) {
489 VLOG_WARN_RL(&rl
, "%s: received a CCM with an invalid interval"
490 " (%"PRIu8
") from RMP %"PRIu64
, cfm
->name
,
491 ccm_interval
, ccm_mpid
);
494 if (cfm
->extended
&& ccm_interval
== 0
495 && ccm_interval_ms_x
!= cfm
->ccm_interval_ms
) {
496 VLOG_WARN_RL(&rl
, "%s: received a CCM with an invalid extended"
497 " interval (%"PRIu16
"ms) from RMP %"PRIu64
, cfm
->name
,
498 ccm_interval_ms_x
, ccm_mpid
);
501 rmp
= lookup_remote_mp(cfm
, ccm_mpid
);
503 if (hmap_count(&cfm
->remote_mps
) < CFM_MAX_RMPS
) {
504 rmp
= xmalloc(sizeof *rmp
);
505 hmap_insert(&cfm
->remote_mps
, &rmp
->node
, hash_mpid(ccm_mpid
));
507 cfm
->unexpected_recv
= true;
509 "%s: dropped CCM with MPID %"PRIu64
" from MAC "
510 ETH_ADDR_FMT
, cfm
->name
, ccm_mpid
,
511 ETH_ADDR_ARGS(eth
->eth_src
));
516 rmp
->mpid
= ccm_mpid
;
519 rmp
->opup
= !ccm_opdown
;
522 VLOG_DBG("%s: received CCM (seq %"PRIu32
") (mpid %"PRIu64
")"
523 " (interval %"PRIu8
") (RDI %s)", cfm
->name
, ntohl(ccm
->seq
),
524 ccm_mpid
, ccm_interval
, ccm_rdi
? "true" : "false");
528 /* Gets the fault status of 'cfm'. Returns true when 'cfm' has detected
529 * connectivity problems, false otherwise. */
531 cfm_get_fault(const struct cfm
*cfm
)
536 /* Gets the operational state of 'cfm'. 'cfm' is considered operationally down
537 * if it has received a CCM with the operationally down bit set from any of its
538 * remote maintenance points. Returns true if 'cfm' is operationally up. False
541 cfm_get_opup(const struct cfm
*cfm
)
543 return cfm
->remote_opup
;
546 /* Populates 'rmps' with an array of remote maintenance points reachable by
547 * 'cfm'. The number of remote maintenance points is written to 'n_rmps'.
548 * 'cfm' retains ownership of the array written to 'rmps' */
550 cfm_get_remote_mpids(const struct cfm
*cfm
, const uint64_t **rmps
,
553 *rmps
= cfm
->rmps_array
;
554 *n_rmps
= cfm
->rmps_array_len
;
558 cfm_find(const char *name
)
562 HMAP_FOR_EACH_WITH_HASH (cfm
, hmap_node
, hash_string(name
, 0), &all_cfms
) {
563 if (!strcmp(cfm
->name
, name
)) {
571 cfm_print_details(struct ds
*ds
, const struct cfm
*cfm
)
573 struct remote_mp
*rmp
;
575 ds_put_format(ds
, "---- %s ----\n", cfm
->name
);
576 ds_put_format(ds
, "MPID %"PRIu64
":%s%s%s\n", cfm
->mpid
,
577 cfm
->extended
? " extended" : "",
578 cfm
->fault
? " fault" : "",
579 cfm
->unexpected_recv
? " unexpected_recv" : "");
581 ds_put_format(ds
, "\topstate: %s\n", cfm
->opup
? "up" : "down");
582 ds_put_format(ds
, "\tremote_opstate: %s\n",
583 cfm
->remote_opup
? "up" : "down");
584 ds_put_format(ds
, "\tinterval: %dms\n", cfm
->ccm_interval_ms
);
585 ds_put_format(ds
, "\tnext CCM tx: %lldms\n",
586 timer_msecs_until_expired(&cfm
->tx_timer
));
587 ds_put_format(ds
, "\tnext fault check: %lldms\n",
588 timer_msecs_until_expired(&cfm
->fault_timer
));
590 HMAP_FOR_EACH (rmp
, node
, &cfm
->remote_mps
) {
591 ds_put_format(ds
, "Remote MPID %"PRIu64
":%s\n",
593 rmp
->rdi
? " rdi" : "");
594 ds_put_format(ds
, "\trecv since check: %s\n",
595 rmp
->recv
? "true" : "false");
596 ds_put_format(ds
, "\topstate: %s\n", rmp
->opup
? "up" : "down");
601 cfm_unixctl_show(struct unixctl_conn
*conn
,
602 const char *args
, void *aux OVS_UNUSED
)
604 struct ds ds
= DS_EMPTY_INITIALIZER
;
605 const struct cfm
*cfm
;
608 cfm
= cfm_find(args
);
610 unixctl_command_reply(conn
, 501, "no such CFM object");
613 cfm_print_details(&ds
, cfm
);
615 HMAP_FOR_EACH (cfm
, hmap_node
, &all_cfms
) {
616 cfm_print_details(&ds
, cfm
);
620 unixctl_command_reply(conn
, 200, ds_cstr(&ds
));