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