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