cfm: Fix appctl negative report.
[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     long long x_recv_time;
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(const 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 /* Allocates a 'cfm' object.  This object should have its 'mpid', 'maid',
117  * 'eth_src', and 'interval' filled out.  When changes are made to the 'cfm'
118  * object, cfm_configure should be called before using it. */
119 struct cfm *
120 cfm_create(void)
121 {
122     struct cfm *cfm;
123     struct cfm_internal *cfmi;
124
125     cfmi = xzalloc(sizeof *cfmi);
126     cfm  = &cfmi->cfm;
127     cfmi->x_recv_time = LLONG_MIN;
128
129     hmap_init(&cfm->remote_mps);
130     return cfm;
131 }
132
133 void
134 cfm_destroy(struct cfm *cfm)
135 {
136     struct cfm_internal *cfmi = cfm_to_internal(cfm);
137     struct remote_mp *rmp, *rmp_next;
138
139     if (!cfm) {
140         return;
141     }
142
143     HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &cfm->remote_mps) {
144         hmap_remove(&cfm->remote_mps, &rmp->node);
145         free(rmp);
146     }
147
148     hmap_destroy(&cfm->remote_mps);
149     free(cfmi);
150 }
151
152 /* Should be run periodically to update fault statistics messages. */
153 void
154 cfm_run(struct cfm *cfm)
155 {
156     long long now = time_msec();
157     struct cfm_internal *cfmi = cfm_to_internal(cfm);
158     long long fault_interval;
159
160     /* According to the 802.1ag specification we should assume every other MP
161      * with the same MAID has the same transmission interval that we have.  If
162      * an MP has a different interval, cfm_process_heartbeat will register it
163      * as a fault (likely due to a configuration error).  Thus we can check all
164      * MPs at once making this quite a bit simpler.
165      *
166      * According to the specification we should check when (ccm_interval_ms *
167      * 3.5)ms have passed. */
168     fault_interval = (cfmi->ccm_interval_ms * 7) / 2;
169     if (now >= cfmi->fault_check + fault_interval) {
170         bool fault;
171         struct remote_mp *rmp;
172
173         fault = now < cfmi->x_recv_time + fault_interval;
174
175         HMAP_FOR_EACH (rmp, node, &cfm->remote_mps) {
176             rmp->fault = rmp->fault || cfmi->fault_check > rmp->recv_time;
177             fault      = rmp->fault || fault;
178         }
179
180         cfm->fault        = fault;
181         cfmi->fault_check = now;
182     }
183 }
184
185 /* Should be run periodically to check if the CFM module has a CCM message it
186  * wishes to send. */
187 bool
188 cfm_should_send_ccm(struct cfm *cfm)
189 {
190     struct cfm_internal *cfmi = cfm_to_internal(cfm);
191
192     return time_msec() >= cfmi->ccm_sent + cfmi->ccm_interval_ms;
193 }
194
195 /* Composes a CCM message into 'ccm'.  Messages generated with this function
196  * should be sent whenever cfm_should_send_ccm() indicates. */
197 void
198 cfm_compose_ccm(struct cfm *cfm, struct ccm *ccm)
199 {
200     struct cfm_internal *cfmi = cfm_to_internal(cfm);
201
202     cfmi->ccm_sent = time_msec();
203
204     ccm->mdlevel_version = 0;
205     ccm->opcode = CCM_OPCODE;
206     ccm->tlv_offset = 70;
207     ccm->seq = htonl(++cfmi->seq);
208     ccm->mpid = htons(cfmi->cfm.mpid);
209     ccm->flags = cfmi->ccm_interval;
210     memcpy(ccm->maid, cfmi->cfm.maid, sizeof ccm->maid);
211 }
212
213 void
214 cfm_wait(struct cfm *cfm)
215 {
216     long long wait;
217     struct cfm_internal *cfmi = cfm_to_internal(cfm);
218
219     wait = MIN(cfmi->ccm_sent + cfmi->ccm_interval_ms,
220                cfmi->fault_check + cfmi->ccm_interval_ms * 4);
221     poll_timer_wait_until(wait);
222 }
223
224 /* Should be called whenever a client of the cfm library changes the internals
225  * of 'cfm'. Returns true if 'cfm' is valid. */
226 bool
227 cfm_configure(struct cfm *cfm)
228 {
229     struct cfm_internal *cfmi;
230
231     if (!cfm_is_valid_mpid(cfm->mpid) || !cfm->interval) {
232         return false;
233     }
234
235     cfmi                  = cfm_to_internal(cfm);
236     cfmi->ccm_interval    = ms_to_ccm_interval(cfm->interval);
237     cfmi->ccm_interval_ms = ccm_interval_to_ms(cfmi->ccm_interval);
238
239     /* Force a resend and check in case anything changed. */
240     cfmi->ccm_sent    = 0;
241     cfmi->fault_check = 0;
242     return true;
243 }
244
245 /* Given an array of MPIDs, updates the 'remote_mps' map of 'cfm' to reflect
246  * it.  Invalid MPIDs are skipped. */
247 void
248 cfm_update_remote_mps(struct cfm *cfm, const uint16_t *mpids, size_t n_mpids)
249 {
250     size_t i;
251     struct hmap new_rmps;
252     struct remote_mp *rmp, *rmp_next;
253
254     hmap_init(&new_rmps);
255
256     for (i = 0; i < n_mpids; i++) {
257         uint16_t mpid = mpids[i];
258
259         if (!cfm_is_valid_mpid(mpid)
260             || lookup_remote_mp(&new_rmps, mpid)) {
261             continue;
262         }
263
264         if ((rmp = lookup_remote_mp(&cfm->remote_mps, mpid))) {
265             hmap_remove(&cfm->remote_mps, &rmp->node);
266         } else {
267             rmp = xzalloc(sizeof *rmp);
268             rmp->mpid = mpid;
269         }
270
271         hmap_insert(&new_rmps, &rmp->node, hash_mpid(mpid));
272     }
273
274     hmap_swap(&new_rmps, &cfm->remote_mps);
275
276     HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &new_rmps) {
277         hmap_remove(&new_rmps, &rmp->node);
278         free(rmp);
279     }
280
281     hmap_destroy(&new_rmps);
282 }
283
284 /* Finds a 'remote_mp' with 'mpid' in 'cfm'.  If no such 'remote_mp' exists
285  * returns NULL. */
286 const struct remote_mp *
287 cfm_get_remote_mp(const struct cfm *cfm, uint16_t mpid)
288 {
289     return lookup_remote_mp(&cfm->remote_mps, mpid);
290 }
291
292 /* Generates 'maid' from 'md_name' and 'ma_name'.  A NULL parameter indicates
293  * the default should be used. Returns false if unsuccessful. */
294 bool
295 cfm_generate_maid(const char *md_name, const char *ma_name,
296                   uint8_t maid[CCM_MAID_LEN])
297 {
298     uint8_t *ma_p;
299     size_t md_len, ma_len;
300
301     if (!md_name) {
302         md_name = "ovs";
303     }
304
305     if (!ma_name) {
306         ma_name = "ovs";
307     }
308
309     memset(maid, 0, CCM_MAID_LEN);
310
311     md_len = strlen(md_name);
312     ma_len = strlen(ma_name);
313
314     if (!md_len || !ma_len || md_len + ma_len + 4 > CCM_MAID_LEN) {
315         return false;
316     }
317
318     maid[0] = 4;                       /* MD name string format. */
319     maid[1] = md_len;                  /* MD name size. */
320     memcpy(&maid[2], md_name, md_len); /* MD name. */
321
322     ma_p    = maid + 2 + md_len;
323     ma_p[0] = 2;                       /* MA name string format. */
324     ma_p[1] = ma_len;                  /* MA name size. */
325     memcpy(&ma_p[2], ma_name, ma_len); /* MA name. */
326     return true;
327 }
328
329 /* Returns true if the CFM library should process packets from 'flow'. */
330 bool
331 cfm_should_process_flow(const struct flow *flow)
332 {
333     return (ntohs(flow->dl_type) == ETH_TYPE_CFM
334             && eth_addr_equals(flow->dl_dst, eth_addr_ccm));
335 }
336
337 /* Updates internal statistics relevant to packet 'p'.  Should be called on
338  * every packet whose flow returned true when passed to
339  * cfm_should_process_flow. */
340 void
341 cfm_process_heartbeat(struct cfm *cfm, const struct ofpbuf *p)
342 {
343     struct ccm *ccm;
344     uint16_t ccm_mpid;
345     uint8_t ccm_interval;
346     struct remote_mp *rmp;
347     struct eth_header *eth;
348
349     struct cfm_internal *cfmi        = cfm_to_internal(cfm);
350     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
351
352     eth = p->l2;
353     ccm = ofpbuf_at(p, (uint8_t *)p->l3 - (uint8_t *)p->data, CCM_LEN);
354
355     if (!ccm) {
356         VLOG_INFO_RL(&rl, "Received an un-parseable 802.1ag CCM heartbeat.");
357         return;
358     }
359
360     if (ccm->opcode != CCM_OPCODE) {
361         VLOG_INFO_RL(&rl, "Received an unsupported 802.1ag message. "
362                      "(opcode %u)", ccm->opcode);
363         return;
364     }
365
366     if (memcmp(ccm->maid, cfm->maid, sizeof ccm->maid)) {
367         cfmi->x_recv_time = time_msec();
368         cfm->fault = true;
369         VLOG_WARN_RL(&rl, "Received unexpected remote MAID from MAC "
370                      ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_src));
371     } else {
372         ccm_mpid = ntohs(ccm->mpid);
373         ccm_interval = ccm->flags & 0x7;
374
375         rmp = lookup_remote_mp(&cfm->remote_mps, ccm_mpid);
376
377         if (rmp) {
378             rmp->recv_time = time_msec();
379             rmp->fault = ccm_interval != cfmi->ccm_interval;
380             cfm->fault = rmp->fault || cfm->fault;
381         } else {
382             cfmi->x_recv_time = time_msec();
383             cfm->fault = true;
384             VLOG_WARN_RL(&rl, "Received unexpected remote MPID %d from MAC "
385                          ETH_ADDR_FMT, ccm_mpid, ETH_ADDR_ARGS(eth->eth_src));
386         }
387     }
388 }
389
390 void
391 cfm_dump_ds(const struct cfm *cfm, struct ds *ds)
392 {
393     const struct cfm_internal *cfmi = cfm_to_internal(cfm);
394     long long int now = time_msec();
395     struct remote_mp *rmp;
396
397     ds_put_format(ds, "MPID %"PRIu16": %s\n", cfm->mpid,
398                   cfm->fault ? "fault" : "");
399
400     ds_put_format(ds, "\tinterval: %dms\n", cfmi->ccm_interval_ms);
401     ds_put_format(ds, "\ttime since CCM tx: %lldms\n", now - cfmi->ccm_sent);
402     ds_put_format(ds, "\ttime since fault check: %lldms\n",
403                   now - cfmi->fault_check);
404
405     if (cfmi->x_recv_time != LLONG_MIN) {
406         ds_put_format(ds, "\ttime since bad CCM rx: %lldms\n",
407                       now - cfmi->x_recv_time);
408     }
409
410     ds_put_cstr(ds, "\n");
411     HMAP_FOR_EACH (rmp, node, &cfm->remote_mps) {
412         ds_put_format(ds, "Remote MPID %"PRIu16": %s\n", rmp->mpid,
413                       rmp->fault ? "fault" : "");
414         ds_put_format(ds, "\ttime since CCM rx: %lldms\n",
415                       time_msec() - rmp->recv_time);
416     }
417 }