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