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