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