cfm: Expose detailed fault status in the database.
[sliver-openvswitch.git] / lib / cfm.c
1 /*
2  * Copyright (c) 2010, 2011 Nicira Networks.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <config.h>
18 #include "cfm.h"
19
20 #include <assert.h>
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "byte-order.h"
26 #include "dynamic-string.h"
27 #include "flow.h"
28 #include "hash.h"
29 #include "hmap.h"
30 #include "ofpbuf.h"
31 #include "packets.h"
32 #include "poll-loop.h"
33 #include "timer.h"
34 #include "timeval.h"
35 #include "unixctl.h"
36 #include "vlog.h"
37
38 VLOG_DEFINE_THIS_MODULE(cfm);
39
40 #define CFM_MAX_RMPS 256
41
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
46 };
47
48 #define ETH_TYPE_CFM 0x8902
49
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.
53  *
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. */
57 #define CCM_LEN 75
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
62 struct ccm {
63     uint8_t  mdlevel_version; /* MD Level and Version */
64     uint8_t  opcode;
65     uint8_t  flags;
66     uint8_t  tlv_offset;
67     ovs_be32 seq;
68     ovs_be16 mpid;
69     uint8_t  maid[CCM_MAID_LEN];
70
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. */
75     uint8_t  zero[5];
76
77     /* TLV space. */
78     uint8_t end_tlv;
79 } __attribute__((packed));
80 BUILD_ASSERT_DECL(CCM_LEN == sizeof(struct ccm));
81
82 struct cfm {
83     char *name;                 /* Name of this CFM object. */
84     struct hmap_node hmap_node; /* Node in all_cfms list. */
85
86     uint64_t mpid;
87     bool extended;         /* Extended mode. */
88     int fault;             /* Connectivity fault status. */
89     int recv_fault;        /* Bit mask of faults occuring on receive. */
90     bool opup;             /* Operational State. */
91     bool remote_opup;      /* Remote Operational State. */
92
93     int fault_override;    /* Manual override of 'fault' status.
94                               Ignored if negative. */
95
96     uint32_t seq;          /* The sequence number of our last CCM. */
97     uint8_t ccm_interval;  /* The CCM transmission interval. */
98     int ccm_interval_ms;   /* 'ccm_interval' in milliseconds. */
99     uint16_t ccm_vlan;     /* Vlan tag of CCM PDUs. */
100     uint8_t ccm_pcp;       /* Priority of CCM PDUs. */
101     uint8_t maid[CCM_MAID_LEN]; /* The MAID of this CFM. */
102
103     struct timer tx_timer;    /* Send CCM when expired. */
104     struct timer fault_timer; /* Check for faults when expired. */
105
106     struct hmap remote_mps;   /* Remote MPs. */
107
108     /* Result of cfm_get_remote_mpids(). Updated only during fault check to
109      * avoid flapping. */
110     uint64_t *rmps_array;     /* Cache of remote_mps. */
111     size_t rmps_array_len;    /* Number of rmps in 'rmps_array'. */
112 };
113
114 /* Remote MPs represent foreign network entities that are configured to have
115  * the same MAID as this CFM instance. */
116 struct remote_mp {
117     uint64_t mpid;         /* The Maintenance Point ID of this 'remote_mp'. */
118     struct hmap_node node; /* Node in 'remote_mps' map. */
119
120     bool recv;           /* CCM was received since last fault check. */
121     bool rdi;            /* Remote Defect Indicator. Indicates remote_mp isn't
122                             receiving CCMs that it's expecting to. */
123     bool opup;           /* Operational State. */
124     uint32_t seq;        /* Most recently received sequence number. */
125 };
126
127 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(20, 30);
128 static struct hmap all_cfms = HMAP_INITIALIZER(&all_cfms);
129
130 static unixctl_cb_func cfm_unixctl_show;
131 static unixctl_cb_func cfm_unixctl_set_fault;
132
133 static const uint8_t *
134 cfm_ccm_addr(const struct cfm *cfm)
135 {
136     return cfm->extended ? eth_addr_ccm_x : eth_addr_ccm;
137 }
138
139 /* Returns the string representation of the given cfm_fault_reason 'reason'. */
140 const char *
141 cfm_fault_reason_to_str(int reason) {
142     switch (reason) {
143 #define CFM_FAULT_REASON(NAME, STR) case CFM_FAULT_##NAME: return #STR;
144         CFM_FAULT_REASONS
145 #undef CFM_FAULT_REASON
146     default: return "<unknown>";
147     }
148 }
149
150 static void
151 ds_put_cfm_fault(struct ds *ds, int fault)
152 {
153     size_t length = ds->length;
154     int i;
155
156     for (i = 0; i < CFM_FAULT_N_REASONS; i++) {
157         int reason = 1 << i;
158
159         if (fault & reason) {
160             ds_put_format(ds, "%s ", cfm_fault_reason_to_str(reason));
161         }
162     }
163
164     if (ds->length > length) {
165         ds_truncate(ds, ds->length - 1);
166     }
167 }
168
169 static void
170 cfm_generate_maid(struct cfm *cfm)
171 {
172     const char *ovs_md_name = "ovs";
173     const char *ovs_ma_name = "ovs";
174     uint8_t *ma_p;
175     size_t md_len, ma_len;
176
177     memset(cfm->maid, 0, CCM_MAID_LEN);
178
179     md_len = strlen(ovs_md_name);
180     ma_len = strlen(ovs_ma_name);
181
182     assert(md_len && ma_len && md_len + ma_len + 4 <= CCM_MAID_LEN);
183
184     cfm->maid[0] = 4;                           /* MD name string format. */
185     cfm->maid[1] = md_len;                      /* MD name size. */
186     memcpy(&cfm->maid[2], ovs_md_name, md_len); /* MD name. */
187
188     ma_p = cfm->maid + 2 + md_len;
189     ma_p[0] = 2;                           /* MA name string format. */
190     ma_p[1] = ma_len;                      /* MA name size. */
191     memcpy(&ma_p[2], ovs_ma_name, ma_len); /* MA name. */
192 }
193
194 static int
195 ccm_interval_to_ms(uint8_t interval)
196 {
197     switch (interval) {
198     case 0:  NOT_REACHED(); /* Explicitly not supported by 802.1ag. */
199     case 1:  return 3;      /* Not recommended due to timer resolution. */
200     case 2:  return 10;     /* Not recommended due to timer resolution. */
201     case 3:  return 100;
202     case 4:  return 1000;
203     case 5:  return 10000;
204     case 6:  return 60000;
205     case 7:  return 600000;
206     default: NOT_REACHED(); /* Explicitly not supported by 802.1ag. */
207     }
208
209     NOT_REACHED();
210 }
211
212 static long long int
213 cfm_fault_interval(struct cfm *cfm)
214 {
215     /* According to the 802.1ag specification we should assume every other MP
216      * with the same MAID has the same transmission interval that we have.  If
217      * an MP has a different interval, cfm_process_heartbeat will register it
218      * as a fault (likely due to a configuration error).  Thus we can check all
219      * MPs at once making this quite a bit simpler.
220      *
221      * According to the specification we should check when (ccm_interval_ms *
222      * 3.5)ms have passed. */
223     return (cfm->ccm_interval_ms * 7) / 2;
224 }
225
226 static uint8_t
227 ms_to_ccm_interval(int interval_ms)
228 {
229     uint8_t i;
230
231     for (i = 7; i > 0; i--) {
232         if (ccm_interval_to_ms(i) <= interval_ms) {
233             return i;
234         }
235     }
236
237     return 1;
238 }
239
240 static uint32_t
241 hash_mpid(uint64_t mpid)
242 {
243     return hash_bytes(&mpid, sizeof mpid, 0);
244 }
245
246 static bool
247 cfm_is_valid_mpid(bool extended, uint64_t mpid)
248 {
249     /* 802.1ag specification requires MPIDs to be within the range [1, 8191].
250      * In extended mode we relax this requirement. */
251     return mpid >= 1 && (extended || mpid <= 8191);
252 }
253
254 static struct remote_mp *
255 lookup_remote_mp(const struct cfm *cfm, uint64_t mpid)
256 {
257     struct remote_mp *rmp;
258
259     HMAP_FOR_EACH_IN_BUCKET (rmp, node, hash_mpid(mpid), &cfm->remote_mps) {
260         if (rmp->mpid == mpid) {
261             return rmp;
262         }
263     }
264
265     return NULL;
266 }
267
268 void
269 cfm_init(void)
270 {
271     unixctl_command_register("cfm/show", "[interface]", 0, 1, cfm_unixctl_show,
272                              NULL);
273     unixctl_command_register("cfm/set-fault", "[interface] normal|false|true",
274                              1, 2, cfm_unixctl_set_fault, NULL);
275 }
276
277 /* Allocates a 'cfm' object called 'name'.  'cfm' should be initialized by
278  * cfm_configure() before use. */
279 struct cfm *
280 cfm_create(const char *name)
281 {
282     struct cfm *cfm;
283
284     cfm = xzalloc(sizeof *cfm);
285     cfm->name = xstrdup(name);
286     hmap_init(&cfm->remote_mps);
287     cfm_generate_maid(cfm);
288     hmap_insert(&all_cfms, &cfm->hmap_node, hash_string(cfm->name, 0));
289     cfm->remote_opup = true;
290     cfm->fault_override = -1;
291     return cfm;
292 }
293
294 void
295 cfm_destroy(struct cfm *cfm)
296 {
297     struct remote_mp *rmp, *rmp_next;
298
299     if (!cfm) {
300         return;
301     }
302
303     HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &cfm->remote_mps) {
304         hmap_remove(&cfm->remote_mps, &rmp->node);
305         free(rmp);
306     }
307
308     hmap_destroy(&cfm->remote_mps);
309     hmap_remove(&all_cfms, &cfm->hmap_node);
310     free(cfm->rmps_array);
311     free(cfm->name);
312     free(cfm);
313 }
314
315 /* Should be run periodically to update fault statistics messages. */
316 void
317 cfm_run(struct cfm *cfm)
318 {
319     if (timer_expired(&cfm->fault_timer)) {
320         long long int interval = cfm_fault_interval(cfm);
321         struct remote_mp *rmp, *rmp_next;
322         bool old_cfm_fault = cfm->fault;
323
324         cfm->fault = cfm->recv_fault;
325         cfm->recv_fault = 0;
326
327         cfm->rmps_array_len = 0;
328         free(cfm->rmps_array);
329         cfm->rmps_array = xmalloc(hmap_count(&cfm->remote_mps) *
330                                   sizeof *cfm->rmps_array);
331
332         cfm->remote_opup = true;
333         HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &cfm->remote_mps) {
334
335             if (!rmp->recv) {
336                 VLOG_DBG("%s: no CCM from RMP %"PRIu64" in the last %lldms",
337                          cfm->name, rmp->mpid, interval);
338                 hmap_remove(&cfm->remote_mps, &rmp->node);
339                 free(rmp);
340             } else {
341                 rmp->recv = false;
342
343                 if (rmp->mpid == cfm->mpid) {
344                     VLOG_WARN_RL(&rl,"%s: received CCM with local MPID"
345                                  " %"PRIu64, cfm->name, rmp->mpid);
346                     cfm->fault |= CFM_FAULT_LOOPBACK;
347                 }
348
349                 if (rmp->rdi) {
350                     VLOG_DBG("%s: RDI bit flagged from RMP %"PRIu64, cfm->name,
351                              rmp->mpid);
352                     cfm->fault |= CFM_FAULT_RDI;
353                 }
354
355                 if (!rmp->opup) {
356                     cfm->remote_opup = rmp->opup;
357                 }
358
359                 cfm->rmps_array[cfm->rmps_array_len++] = rmp->mpid;
360             }
361         }
362
363         if (hmap_is_empty(&cfm->remote_mps)) {
364             cfm->fault |= CFM_FAULT_RECV;
365         }
366
367         if (old_cfm_fault != cfm->fault) {
368             struct ds ds = DS_EMPTY_INITIALIZER;
369
370             ds_put_cfm_fault(&ds, cfm->fault);
371             VLOG_INFO_RL(&rl, "%s: CFM fault status changed: %s", cfm->name,
372                          ds_cstr_ro(&ds));
373             ds_destroy(&ds);
374         }
375
376         timer_set_duration(&cfm->fault_timer, interval);
377     }
378 }
379
380 /* Should be run periodically to check if the CFM module has a CCM message it
381  * wishes to send. */
382 bool
383 cfm_should_send_ccm(struct cfm *cfm)
384 {
385     return timer_expired(&cfm->tx_timer);
386 }
387
388 /* Composes a CCM message into 'packet'.  Messages generated with this function
389  * should be sent whenever cfm_should_send_ccm() indicates. */
390 void
391 cfm_compose_ccm(struct cfm *cfm, struct ofpbuf *packet,
392                 uint8_t eth_src[ETH_ADDR_LEN])
393 {
394     struct ccm *ccm;
395
396     timer_set_duration(&cfm->tx_timer, cfm->ccm_interval_ms);
397     eth_compose(packet, cfm_ccm_addr(cfm), eth_src, ETH_TYPE_CFM, sizeof *ccm);
398
399     if (cfm->ccm_vlan || cfm->ccm_pcp) {
400         uint16_t tci = cfm->ccm_vlan | (cfm->ccm_pcp << VLAN_PCP_SHIFT);
401         eth_push_vlan(packet, htons(tci));
402     }
403
404     ccm = packet->l3;
405     ccm->mdlevel_version = 0;
406     ccm->opcode = CCM_OPCODE;
407     ccm->tlv_offset = 70;
408     ccm->seq = htonl(++cfm->seq);
409     ccm->flags = cfm->ccm_interval;
410     memcpy(ccm->maid, cfm->maid, sizeof ccm->maid);
411     memset(ccm->zero, 0, sizeof ccm->zero);
412     ccm->end_tlv = 0;
413
414     if (cfm->extended) {
415         ccm->mpid = htons(hash_mpid(cfm->mpid));
416         ccm->mpid64 = htonll(cfm->mpid);
417         ccm->opdown = !cfm->opup;
418     } else {
419         ccm->mpid = htons(cfm->mpid);
420         ccm->mpid64 = htonll(0);
421         ccm->opdown = 0;
422     }
423
424     if (cfm->ccm_interval == 0) {
425         assert(cfm->extended);
426         ccm->interval_ms_x = htons(cfm->ccm_interval_ms);
427     }
428
429     if (hmap_is_empty(&cfm->remote_mps)) {
430         ccm->flags |= CCM_RDI_MASK;
431     }
432 }
433
434 void
435 cfm_wait(struct cfm *cfm)
436 {
437     timer_wait(&cfm->tx_timer);
438     timer_wait(&cfm->fault_timer);
439 }
440
441 /* Configures 'cfm' with settings from 's'. */
442 bool
443 cfm_configure(struct cfm *cfm, const struct cfm_settings *s)
444 {
445     uint8_t interval;
446     int interval_ms;
447
448     if (!cfm_is_valid_mpid(s->extended, s->mpid) || s->interval <= 0) {
449         return false;
450     }
451
452     cfm->mpid = s->mpid;
453     cfm->extended = s->extended;
454     cfm->opup = s->opup;
455     interval = ms_to_ccm_interval(s->interval);
456     interval_ms = ccm_interval_to_ms(interval);
457
458     cfm->ccm_vlan = s->ccm_vlan & VLAN_VID_MASK;
459     cfm->ccm_pcp = s->ccm_pcp & (VLAN_PCP_MASK >> VLAN_PCP_SHIFT);
460     if (cfm->extended && interval_ms != s->interval) {
461         interval = 0;
462         interval_ms = MIN(s->interval, UINT16_MAX);
463     }
464
465     if (interval != cfm->ccm_interval || interval_ms != cfm->ccm_interval_ms) {
466         cfm->ccm_interval = interval;
467         cfm->ccm_interval_ms = interval_ms;
468
469         timer_set_expired(&cfm->tx_timer);
470         timer_set_duration(&cfm->fault_timer, cfm_fault_interval(cfm));
471     }
472
473     return true;
474 }
475
476 /* Returns true if 'cfm' should process packets from 'flow'. */
477 bool
478 cfm_should_process_flow(const struct cfm *cfm, const struct flow *flow)
479 {
480     return (ntohs(flow->dl_type) == ETH_TYPE_CFM
481             && eth_addr_equals(flow->dl_dst, cfm_ccm_addr(cfm)));
482 }
483
484 /* Updates internal statistics relevant to packet 'p'.  Should be called on
485  * every packet whose flow returned true when passed to
486  * cfm_should_process_flow. */
487 void
488 cfm_process_heartbeat(struct cfm *cfm, const struct ofpbuf *p)
489 {
490     struct ccm *ccm;
491     struct eth_header *eth;
492
493     eth = p->l2;
494     ccm = ofpbuf_at(p, (uint8_t *)p->l3 - (uint8_t *)p->data, CCM_ACCEPT_LEN);
495
496     if (!ccm) {
497         VLOG_INFO_RL(&rl, "%s: Received an unparseable 802.1ag CCM heartbeat.",
498                      cfm->name);
499         return;
500     }
501
502     if (ccm->opcode != CCM_OPCODE) {
503         VLOG_INFO_RL(&rl, "%s: Received an unsupported 802.1ag message. "
504                      "(opcode %u)", cfm->name, ccm->opcode);
505         return;
506     }
507
508     /* According to the 802.1ag specification, reception of a CCM with an
509      * incorrect ccm_interval, unexpected MAID, or unexpected MPID should
510      * trigger a fault.  We ignore this requirement for several reasons.
511      *
512      * Faults can cause a controller or Open vSwitch to make potentially
513      * expensive changes to the network topology.  It seems prudent to trigger
514      * them judiciously, especially when CFM is used to check slave status of
515      * bonds. Furthermore, faults can be maliciously triggered by crafting
516      * invalid CCMs. */
517     if (memcmp(ccm->maid, cfm->maid, sizeof ccm->maid)) {
518         cfm->recv_fault |= CFM_FAULT_MAID;
519         VLOG_WARN_RL(&rl, "%s: Received unexpected remote MAID from MAC "
520                      ETH_ADDR_FMT, cfm->name, ETH_ADDR_ARGS(eth->eth_src));
521     } else {
522         uint8_t ccm_interval = ccm->flags & 0x7;
523         bool ccm_rdi = ccm->flags & CCM_RDI_MASK;
524         uint16_t ccm_interval_ms_x = ntohs(ccm->interval_ms_x);
525
526         struct remote_mp *rmp;
527         uint64_t ccm_mpid;
528         uint32_t ccm_seq;
529         bool ccm_opdown;
530
531         if (cfm->extended) {
532             ccm_mpid = ntohll(ccm->mpid64);
533             ccm_opdown = ccm->opdown;
534         } else {
535             ccm_mpid = ntohs(ccm->mpid);
536             ccm_opdown = false;
537         }
538         ccm_seq = ntohl(ccm->seq);
539
540         if (ccm_interval != cfm->ccm_interval) {
541             VLOG_WARN_RL(&rl, "%s: received a CCM with an invalid interval"
542                          " (%"PRIu8") from RMP %"PRIu64, cfm->name,
543                          ccm_interval, ccm_mpid);
544         }
545
546         if (cfm->extended && ccm_interval == 0
547             && ccm_interval_ms_x != cfm->ccm_interval_ms) {
548             VLOG_WARN_RL(&rl, "%s: received a CCM with an invalid extended"
549                          " interval (%"PRIu16"ms) from RMP %"PRIu64, cfm->name,
550                          ccm_interval_ms_x, ccm_mpid);
551         }
552
553         rmp = lookup_remote_mp(cfm, ccm_mpid);
554         if (!rmp) {
555             if (hmap_count(&cfm->remote_mps) < CFM_MAX_RMPS) {
556                 rmp = xzalloc(sizeof *rmp);
557                 hmap_insert(&cfm->remote_mps, &rmp->node, hash_mpid(ccm_mpid));
558             } else {
559                 cfm->recv_fault |= CFM_FAULT_OVERFLOW;
560                 VLOG_WARN_RL(&rl,
561                              "%s: dropped CCM with MPID %"PRIu64" from MAC "
562                              ETH_ADDR_FMT, cfm->name, ccm_mpid,
563                              ETH_ADDR_ARGS(eth->eth_src));
564             }
565         }
566
567         VLOG_DBG("%s: received CCM (seq %"PRIu32") (mpid %"PRIu64")"
568                  " (interval %"PRIu8") (RDI %s)", cfm->name, ccm_seq,
569                  ccm_mpid, ccm_interval, ccm_rdi ? "true" : "false");
570
571         if (rmp) {
572             if (rmp->seq && ccm_seq != (rmp->seq + 1)) {
573                 VLOG_WARN_RL(&rl, "%s: (mpid %"PRIu64") detected sequence"
574                              " numbers which indicate possible connectivity"
575                              " problems (previous %"PRIu32") (current %"PRIu32
576                              ")", cfm->name, ccm_mpid, rmp->seq, ccm_seq);
577             }
578
579             rmp->mpid = ccm_mpid;
580             rmp->recv = true;
581             rmp->seq = ccm_seq;
582             rmp->rdi = ccm_rdi;
583             rmp->opup = !ccm_opdown;
584         }
585     }
586 }
587
588 /* Gets the fault status of 'cfm'.  Returns a bit mask of 'cfm_fault_reason's
589  * indicating the cause of the connectivity fault, or zero if there is no
590  * fault. */
591 int
592 cfm_get_fault(const struct cfm *cfm)
593 {
594     if (cfm->fault_override >= 0) {
595         return cfm->fault_override ? CFM_FAULT_OVERRIDE : 0;
596     }
597     return cfm->fault;
598 }
599
600 /* Gets the operational state of 'cfm'.  'cfm' is considered operationally down
601  * if it has received a CCM with the operationally down bit set from any of its
602  * remote maintenance points. Returns true if 'cfm' is operationally up. False
603  * otherwise. */
604 bool
605 cfm_get_opup(const struct cfm *cfm)
606 {
607     return cfm->remote_opup;
608 }
609
610 /* Populates 'rmps' with an array of remote maintenance points reachable by
611  * 'cfm'. The number of remote maintenance points is written to 'n_rmps'.
612  * 'cfm' retains ownership of the array written to 'rmps' */
613 void
614 cfm_get_remote_mpids(const struct cfm *cfm, const uint64_t **rmps,
615                      size_t *n_rmps)
616 {
617     *rmps = cfm->rmps_array;
618     *n_rmps = cfm->rmps_array_len;
619 }
620
621 static struct cfm *
622 cfm_find(const char *name)
623 {
624     struct cfm *cfm;
625
626     HMAP_FOR_EACH_WITH_HASH (cfm, hmap_node, hash_string(name, 0), &all_cfms) {
627         if (!strcmp(cfm->name, name)) {
628             return cfm;
629         }
630     }
631     return NULL;
632 }
633
634 static void
635 cfm_print_details(struct ds *ds, const struct cfm *cfm)
636 {
637     struct remote_mp *rmp;
638
639     ds_put_format(ds, "---- %s ----\n", cfm->name);
640     ds_put_format(ds, "MPID %"PRIu64":%s%s\n", cfm->mpid,
641                   cfm->extended ? " extended" : "",
642                   cfm->fault_override >= 0 ? " fault_override" : "");
643
644
645     if (cfm_get_fault(cfm)) {
646         ds_put_cstr(ds, "\tfault: ");
647         ds_put_cfm_fault(ds, cfm_get_fault(cfm));
648         ds_put_cstr(ds, "\n");
649     }
650
651     ds_put_format(ds, "\topstate: %s\n", cfm->opup ? "up" : "down");
652     ds_put_format(ds, "\tremote_opstate: %s\n",
653                   cfm->remote_opup ? "up" : "down");
654     ds_put_format(ds, "\tinterval: %dms\n", cfm->ccm_interval_ms);
655     ds_put_format(ds, "\tnext CCM tx: %lldms\n",
656                   timer_msecs_until_expired(&cfm->tx_timer));
657     ds_put_format(ds, "\tnext fault check: %lldms\n",
658                   timer_msecs_until_expired(&cfm->fault_timer));
659
660     HMAP_FOR_EACH (rmp, node, &cfm->remote_mps) {
661         ds_put_format(ds, "Remote MPID %"PRIu64":%s\n",
662                       rmp->mpid,
663                       rmp->rdi ? " rdi" : "");
664         ds_put_format(ds, "\trecv since check: %s\n",
665                       rmp->recv ? "true" : "false");
666         ds_put_format(ds, "\topstate: %s\n", rmp->opup? "up" : "down");
667     }
668 }
669
670 static void
671 cfm_unixctl_show(struct unixctl_conn *conn, int argc, const char *argv[],
672                  void *aux OVS_UNUSED)
673 {
674     struct ds ds = DS_EMPTY_INITIALIZER;
675     const struct cfm *cfm;
676
677     if (argc > 1) {
678         cfm = cfm_find(argv[1]);
679         if (!cfm) {
680             unixctl_command_reply(conn, 501, "no such CFM object");
681             return;
682         }
683         cfm_print_details(&ds, cfm);
684     } else {
685         HMAP_FOR_EACH (cfm, hmap_node, &all_cfms) {
686             cfm_print_details(&ds, cfm);
687         }
688     }
689
690     unixctl_command_reply(conn, 200, ds_cstr(&ds));
691     ds_destroy(&ds);
692 }
693
694 static void
695 cfm_unixctl_set_fault(struct unixctl_conn *conn, int argc, const char *argv[],
696                       void *aux OVS_UNUSED)
697 {
698     const char *fault_str = argv[argc - 1];
699     int fault_override;
700     struct cfm *cfm;
701
702     if (!strcasecmp("true", fault_str)) {
703         fault_override = 1;
704     } else if (!strcasecmp("false", fault_str)) {
705         fault_override = 0;
706     } else if (!strcasecmp("normal", fault_str)) {
707         fault_override = -1;
708     } else {
709         unixctl_command_reply(conn, 501, "unknown fault string");
710         return;
711     }
712
713     if (argc > 2) {
714         cfm = cfm_find(argv[1]);
715         if (!cfm) {
716             unixctl_command_reply(conn, 501, "no such CFM object");
717             return;
718         }
719         cfm->fault_override = fault_override;
720     } else {
721         HMAP_FOR_EACH (cfm, hmap_node, &all_cfms) {
722             cfm->fault_override = fault_override;
723         }
724     }
725
726     unixctl_command_reply(conn, 200, "OK");
727 }