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