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