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