cfm: Show extended mode in cfm/show appctl command.
[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  zero[6];
75
76     /* TLV space. */
77     uint8_t end_tlv;
78 } __attribute__((packed));
79 BUILD_ASSERT_DECL(CCM_LEN == sizeof(struct ccm));
80
81 struct cfm {
82     char *name;                 /* Name of this CFM object. */
83     struct hmap_node hmap_node; /* Node in all_cfms list. */
84
85     uint64_t mpid;
86     bool extended;         /* Extended mode. */
87     bool fault;            /* Indicates connectivity fault. */
88     bool unexpected_recv;  /* Received an unexpected CCM. */
89
90     uint32_t seq;          /* The sequence number of our last CCM. */
91     uint8_t ccm_interval;  /* The CCM transmission interval. */
92     int ccm_interval_ms;   /* 'ccm_interval' in milliseconds. */
93     uint8_t maid[CCM_MAID_LEN]; /* The MAID of this CFM. */
94
95     struct timer tx_timer;    /* Send CCM when expired. */
96     struct timer fault_timer; /* Check for faults when expired. */
97
98     struct hmap remote_mps;   /* Remote MPs. */
99
100     /* Result of cfm_get_remote_mpids(). Updated only during fault check to
101      * avoid flapping. */
102     uint64_t *rmps_array;     /* Cache of remote_mps. */
103     size_t rmps_array_len;    /* Number of rmps in 'rmps_array'. */
104 };
105
106 /* Remote MPs represent foreign network entities that are configured to have
107  * the same MAID as this CFM instance. */
108 struct remote_mp {
109     uint64_t mpid;         /* The Maintenance Point ID of this 'remote_mp'. */
110     struct hmap_node node; /* Node in 'remote_mps' map. */
111
112     bool recv;           /* CCM was received since last fault check. */
113     bool rdi;            /* Remote Defect Indicator. Indicates remote_mp isn't
114                             receiving CCMs that it's expecting to. */
115 };
116
117 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
118 static struct hmap all_cfms = HMAP_INITIALIZER(&all_cfms);
119
120 static void cfm_unixctl_show(struct unixctl_conn *, const char *args,
121                              void *aux);
122
123 static const uint8_t *
124 cfm_ccm_addr(const struct cfm *cfm)
125 {
126     return cfm->extended ? eth_addr_ccm_x : eth_addr_ccm;
127 }
128
129 static void
130 cfm_generate_maid(struct cfm *cfm)
131 {
132     const char *ovs_md_name = "ovs";
133     const char *ovs_ma_name = "ovs";
134     uint8_t *ma_p;
135     size_t md_len, ma_len;
136
137     memset(cfm->maid, 0, CCM_MAID_LEN);
138
139     md_len = strlen(ovs_md_name);
140     ma_len = strlen(ovs_ma_name);
141
142     assert(md_len && ma_len && md_len + ma_len + 4 <= CCM_MAID_LEN);
143
144     cfm->maid[0] = 4;                           /* MD name string format. */
145     cfm->maid[1] = md_len;                      /* MD name size. */
146     memcpy(&cfm->maid[2], ovs_md_name, md_len); /* MD name. */
147
148     ma_p = cfm->maid + 2 + md_len;
149     ma_p[0] = 2;                           /* MA name string format. */
150     ma_p[1] = ma_len;                      /* MA name size. */
151     memcpy(&ma_p[2], ovs_ma_name, ma_len); /* MA name. */
152 }
153
154 static int
155 ccm_interval_to_ms(uint8_t interval)
156 {
157     switch (interval) {
158     case 0:  NOT_REACHED(); /* Explicitly not supported by 802.1ag. */
159     case 1:  return 3;      /* Not recommended due to timer resolution. */
160     case 2:  return 10;     /* Not recommended due to timer resolution. */
161     case 3:  return 100;
162     case 4:  return 1000;
163     case 5:  return 10000;
164     case 6:  return 60000;
165     case 7:  return 600000;
166     default: NOT_REACHED(); /* Explicitly not supported by 802.1ag. */
167     }
168
169     NOT_REACHED();
170 }
171
172 static long long int
173 cfm_fault_interval(struct cfm *cfm)
174 {
175     /* According to the 802.1ag specification we should assume every other MP
176      * with the same MAID has the same transmission interval that we have.  If
177      * an MP has a different interval, cfm_process_heartbeat will register it
178      * as a fault (likely due to a configuration error).  Thus we can check all
179      * MPs at once making this quite a bit simpler.
180      *
181      * According to the specification we should check when (ccm_interval_ms *
182      * 3.5)ms have passed. */
183     return (cfm->ccm_interval_ms * 7) / 2;
184 }
185
186 static uint8_t
187 ms_to_ccm_interval(int interval_ms)
188 {
189     uint8_t i;
190
191     for (i = 7; i > 0; i--) {
192         if (ccm_interval_to_ms(i) <= interval_ms) {
193             return i;
194         }
195     }
196
197     return 1;
198 }
199
200 static uint32_t
201 hash_mpid(uint64_t mpid)
202 {
203     return hash_bytes(&mpid, sizeof mpid, 0);
204 }
205
206 static bool
207 cfm_is_valid_mpid(bool extended, uint64_t mpid)
208 {
209     /* 802.1ag specification requires MPIDs to be within the range [1, 8191].
210      * In extended mode we relax this requirement. */
211     return mpid >= 1 && (extended || mpid <= 8191);
212 }
213
214 static struct remote_mp *
215 lookup_remote_mp(const struct cfm *cfm, uint64_t mpid)
216 {
217     struct remote_mp *rmp;
218
219     HMAP_FOR_EACH_IN_BUCKET (rmp, node, hash_mpid(mpid), &cfm->remote_mps) {
220         if (rmp->mpid == mpid) {
221             return rmp;
222         }
223     }
224
225     return NULL;
226 }
227
228 void
229 cfm_init(void)
230 {
231     unixctl_command_register("cfm/show", "[interface]", cfm_unixctl_show,
232                              NULL);
233 }
234
235 /* Allocates a 'cfm' object called 'name'.  'cfm' should be initialized by
236  * cfm_configure() before use. */
237 struct cfm *
238 cfm_create(const char *name)
239 {
240     struct cfm *cfm;
241
242     cfm = xzalloc(sizeof *cfm);
243     cfm->name = xstrdup(name);
244     hmap_init(&cfm->remote_mps);
245     cfm_generate_maid(cfm);
246     hmap_insert(&all_cfms, &cfm->hmap_node, hash_string(cfm->name, 0));
247     return cfm;
248 }
249
250 void
251 cfm_destroy(struct cfm *cfm)
252 {
253     struct remote_mp *rmp, *rmp_next;
254
255     if (!cfm) {
256         return;
257     }
258
259     HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &cfm->remote_mps) {
260         hmap_remove(&cfm->remote_mps, &rmp->node);
261         free(rmp);
262     }
263
264     hmap_destroy(&cfm->remote_mps);
265     hmap_remove(&all_cfms, &cfm->hmap_node);
266     free(cfm->rmps_array);
267     free(cfm->name);
268     free(cfm);
269 }
270
271 /* Should be run periodically to update fault statistics messages. */
272 void
273 cfm_run(struct cfm *cfm)
274 {
275     if (timer_expired(&cfm->fault_timer)) {
276         long long int interval = cfm_fault_interval(cfm);
277         struct remote_mp *rmp, *rmp_next;
278
279         cfm->fault = cfm->unexpected_recv;
280         cfm->unexpected_recv = false;
281
282         cfm->rmps_array_len = 0;
283         free(cfm->rmps_array);
284         cfm->rmps_array = xmalloc(hmap_count(&cfm->remote_mps) *
285                                   sizeof *cfm->rmps_array);
286
287         HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &cfm->remote_mps) {
288
289             if (!rmp->recv) {
290                 VLOG_DBG("%s: no CCM from RMP %"PRIu64" in the last %lldms",
291                          cfm->name, rmp->mpid, interval);
292                 hmap_remove(&cfm->remote_mps, &rmp->node);
293                 free(rmp);
294             } else {
295                 rmp->recv = false;
296
297                 if (rmp->mpid == cfm->mpid) {
298                     VLOG_WARN_RL(&rl,"%s: received CCM with local MPID"
299                                  " %"PRIu64, cfm->name, rmp->mpid);
300                     cfm->fault = true;
301                 }
302
303                 if (rmp->rdi) {
304                     VLOG_DBG("%s: RDI bit flagged from RMP %"PRIu64, cfm->name,
305                              rmp->mpid);
306                     cfm->fault = true;
307                 }
308
309                 cfm->rmps_array[cfm->rmps_array_len++] = rmp->mpid;
310             }
311         }
312
313         if (hmap_is_empty(&cfm->remote_mps)) {
314             cfm->fault = true;
315         }
316
317         timer_set_duration(&cfm->fault_timer, interval);
318     }
319 }
320
321 /* Should be run periodically to check if the CFM module has a CCM message it
322  * wishes to send. */
323 bool
324 cfm_should_send_ccm(struct cfm *cfm)
325 {
326     return timer_expired(&cfm->tx_timer);
327 }
328
329 /* Composes a CCM message into 'packet'.  Messages generated with this function
330  * should be sent whenever cfm_should_send_ccm() indicates. */
331 void
332 cfm_compose_ccm(struct cfm *cfm, struct ofpbuf *packet,
333                 uint8_t eth_src[ETH_ADDR_LEN])
334 {
335     struct ccm *ccm;
336
337     timer_set_duration(&cfm->tx_timer, cfm->ccm_interval_ms);
338     ccm = eth_compose(packet, cfm_ccm_addr(cfm), eth_src, ETH_TYPE_CFM,
339                       sizeof *ccm);
340     ccm->mdlevel_version = 0;
341     ccm->opcode = CCM_OPCODE;
342     ccm->tlv_offset = 70;
343     ccm->seq = htonl(++cfm->seq);
344     ccm->flags = cfm->ccm_interval;
345     memcpy(ccm->maid, cfm->maid, sizeof ccm->maid);
346     memset(ccm->zero, 0, sizeof ccm->zero);
347     ccm->end_tlv = 0;
348
349     if (cfm->extended) {
350         ccm->mpid = htons(hash_mpid(cfm->mpid));
351         ccm->mpid64 = htonll(cfm->mpid);
352     } else {
353         ccm->mpid = htons(cfm->mpid);
354         ccm->mpid64 = htonll(0);
355     }
356
357     if (cfm->ccm_interval == 0) {
358         assert(cfm->extended);
359         ccm->interval_ms_x = htons(cfm->ccm_interval_ms);
360     }
361
362     if (hmap_is_empty(&cfm->remote_mps)) {
363         ccm->flags |= CCM_RDI_MASK;
364     }
365 }
366
367 void
368 cfm_wait(struct cfm *cfm)
369 {
370     timer_wait(&cfm->tx_timer);
371     timer_wait(&cfm->fault_timer);
372 }
373
374 /* Configures 'cfm' with settings from 's'. */
375 bool
376 cfm_configure(struct cfm *cfm, const struct cfm_settings *s)
377 {
378     uint8_t interval;
379     int interval_ms;
380
381     if (!cfm_is_valid_mpid(s->extended, s->mpid) || s->interval <= 0) {
382         return false;
383     }
384
385     cfm->mpid = s->mpid;
386     cfm->extended = s->extended;
387     interval = ms_to_ccm_interval(s->interval);
388     interval_ms = ccm_interval_to_ms(interval);
389
390     if (cfm->extended && interval_ms != s->interval) {
391         interval = 0;
392         interval_ms = MIN(s->interval, UINT16_MAX);
393     }
394
395     if (interval != cfm->ccm_interval || interval_ms != cfm->ccm_interval_ms) {
396         cfm->ccm_interval = interval;
397         cfm->ccm_interval_ms = interval_ms;
398
399         timer_set_expired(&cfm->tx_timer);
400         timer_set_duration(&cfm->fault_timer, cfm_fault_interval(cfm));
401     }
402
403     return true;
404 }
405
406 /* Returns true if 'cfm' should process packets from 'flow'. */
407 bool
408 cfm_should_process_flow(const struct cfm *cfm, const struct flow *flow)
409 {
410     return (ntohs(flow->dl_type) == ETH_TYPE_CFM
411             && eth_addr_equals(flow->dl_dst, cfm_ccm_addr(cfm)));
412 }
413
414 /* Updates internal statistics relevant to packet 'p'.  Should be called on
415  * every packet whose flow returned true when passed to
416  * cfm_should_process_flow. */
417 void
418 cfm_process_heartbeat(struct cfm *cfm, const struct ofpbuf *p)
419 {
420     struct ccm *ccm;
421     struct eth_header *eth;
422
423     eth = p->l2;
424     ccm = ofpbuf_at(p, (uint8_t *)p->l3 - (uint8_t *)p->data, CCM_ACCEPT_LEN);
425
426     if (!ccm) {
427         VLOG_INFO_RL(&rl, "%s: Received an unparseable 802.1ag CCM heartbeat.",
428                      cfm->name);
429         return;
430     }
431
432     if (ccm->opcode != CCM_OPCODE) {
433         VLOG_INFO_RL(&rl, "%s: Received an unsupported 802.1ag message. "
434                      "(opcode %u)", cfm->name, ccm->opcode);
435         return;
436     }
437
438     /* According to the 802.1ag specification, reception of a CCM with an
439      * incorrect ccm_interval, unexpected MAID, or unexpected MPID should
440      * trigger a fault.  We ignore this requirement for several reasons.
441      *
442      * Faults can cause a controller or Open vSwitch to make potentially
443      * expensive changes to the network topology.  It seems prudent to trigger
444      * them judiciously, especially when CFM is used to check slave status of
445      * bonds. Furthermore, faults can be maliciously triggered by crafting
446      * invalid CCMs. */
447     if (memcmp(ccm->maid, cfm->maid, sizeof ccm->maid)) {
448         cfm->unexpected_recv = true;
449         VLOG_WARN_RL(&rl, "%s: Received unexpected remote MAID from MAC "
450                      ETH_ADDR_FMT, cfm->name, ETH_ADDR_ARGS(eth->eth_src));
451     } else {
452         uint8_t ccm_interval = ccm->flags & 0x7;
453         bool ccm_rdi = ccm->flags & CCM_RDI_MASK;
454         uint16_t ccm_interval_ms_x = ntohs(ccm->interval_ms_x);
455
456         struct remote_mp *rmp;
457         uint64_t ccm_mpid;
458
459         ccm_mpid = cfm->extended ? ntohll(ccm->mpid64) : ntohs(ccm->mpid);
460
461         if (ccm_interval != cfm->ccm_interval) {
462             VLOG_WARN_RL(&rl, "%s: received a CCM with an invalid interval"
463                          " (%"PRIu8") from RMP %"PRIu64, cfm->name,
464                          ccm_interval, ccm_mpid);
465         }
466
467         if (cfm->extended && ccm_interval == 0
468             && ccm_interval_ms_x != cfm->ccm_interval_ms) {
469             VLOG_WARN_RL(&rl, "%s: received a CCM with an invalid extended"
470                          " interval (%"PRIu16"ms) from RMP %"PRIu64, cfm->name,
471                          ccm_interval_ms_x, ccm_mpid);
472         }
473
474         rmp = lookup_remote_mp(cfm, ccm_mpid);
475         if (rmp) {
476             rmp->recv = true;
477             rmp->rdi = ccm_rdi;
478         } else if (hmap_count(&cfm->remote_mps) < CFM_MAX_RMPS) {
479             rmp = xmalloc(sizeof *rmp);
480             rmp->mpid = ccm_mpid;
481             rmp->recv = true;
482             rmp->rdi = ccm_rdi;
483             hmap_insert(&cfm->remote_mps, &rmp->node, hash_mpid(rmp->mpid));
484         } else {
485             cfm->unexpected_recv = true;
486             VLOG_WARN_RL(&rl, "%s: dropped CCM with MPID %"PRIu64" from MAC "
487                          ETH_ADDR_FMT, cfm->name, ccm_mpid,
488                          ETH_ADDR_ARGS(eth->eth_src));
489         }
490
491         VLOG_DBG("%s: received CCM (seq %"PRIu32") (mpid %"PRIu64")"
492                  " (interval %"PRIu8") (RDI %s)", cfm->name, ntohl(ccm->seq),
493                  ccm_mpid, ccm_interval, ccm_rdi ? "true" : "false");
494     }
495 }
496
497 /* Gets the fault status of 'cfm'.  Returns true when 'cfm' has detected
498  * connectivity problems, false otherwise. */
499 bool
500 cfm_get_fault(const struct cfm *cfm)
501 {
502     return cfm->fault;
503 }
504
505 /* Populates 'rmps' with an array of remote maintenance points reachable by
506  * 'cfm'. The number of remote maintenance points is written to 'n_rmps'.
507  * 'cfm' retains ownership of the array written to 'rmps' */
508 void
509 cfm_get_remote_mpids(const struct cfm *cfm, const uint64_t **rmps,
510                      size_t *n_rmps)
511 {
512     *rmps = cfm->rmps_array;
513     *n_rmps = cfm->rmps_array_len;
514 }
515
516 static struct cfm *
517 cfm_find(const char *name)
518 {
519     struct cfm *cfm;
520
521     HMAP_FOR_EACH_WITH_HASH (cfm, hmap_node, hash_string(name, 0), &all_cfms) {
522         if (!strcmp(cfm->name, name)) {
523             return cfm;
524         }
525     }
526     return NULL;
527 }
528
529 static void
530 cfm_print_details(struct ds *ds, const struct cfm *cfm)
531 {
532     struct remote_mp *rmp;
533
534     ds_put_format(ds, "---- %s ----\n", cfm->name);
535     ds_put_format(ds, "MPID %"PRIu64":%s%s%s\n", cfm->mpid,
536                   cfm->extended ? " extended" : "",
537                   cfm->fault ? " fault" : "",
538                   cfm->unexpected_recv ? " unexpected_recv" : "");
539
540     ds_put_format(ds, "\tinterval: %dms\n", cfm->ccm_interval_ms);
541     ds_put_format(ds, "\tnext CCM tx: %lldms\n",
542                   timer_msecs_until_expired(&cfm->tx_timer));
543     ds_put_format(ds, "\tnext fault check: %lldms\n",
544                   timer_msecs_until_expired(&cfm->fault_timer));
545
546     HMAP_FOR_EACH (rmp, node, &cfm->remote_mps) {
547         ds_put_format(ds, "Remote MPID %"PRIu64":%s\n",
548                       rmp->mpid,
549                       rmp->rdi ? " rdi" : "");
550         ds_put_format(ds, "\trecv since check: %s\n",
551                       rmp->recv ? "true" : "false");
552     }
553 }
554
555 static void
556 cfm_unixctl_show(struct unixctl_conn *conn,
557                  const char *args, void *aux OVS_UNUSED)
558 {
559     struct ds ds = DS_EMPTY_INITIALIZER;
560     const struct cfm *cfm;
561
562     if (strlen(args)) {
563         cfm = cfm_find(args);
564         if (!cfm) {
565             unixctl_command_reply(conn, 501, "no such CFM object");
566             return;
567         }
568         cfm_print_details(&ds, cfm);
569     } else {
570         HMAP_FOR_EACH (cfm, hmap_node, &all_cfms) {
571             cfm_print_details(&ds, cfm);
572         }
573     }
574
575     unixctl_command_reply(conn, 200, ds_cstr(&ds));
576     ds_destroy(&ds);
577 }