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