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