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