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