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