]>
Commit | Line | Data |
---|---|---|
b31bcf60 | 1 | /* |
af573985 | 2 | * Copyright (c) 2010, 2011 Nicira Networks. |
b31bcf60 EJ |
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 | ||
20c8e971 | 24 | #include "dynamic-string.h" |
b31bcf60 EJ |
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" | |
6fabb78d | 31 | #include "timer.h" |
b31bcf60 EJ |
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. */ | |
b31bcf60 EJ |
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 | ||
6fabb78d EJ |
46 | struct timer tx_timer; /* Send CCM when expired. */ |
47 | struct timer fault_timer; /* Check for faults when expired. */ | |
1c2e2d2f | 48 | |
0dd17bfd | 49 | long long x_recv_time; |
b31bcf60 EJ |
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 | ||
aac19178 EJ |
70 | static long long int |
71 | cfm_fault_interval(struct cfm_internal *cfmi) | |
72 | { | |
73 | /* According to the 802.1ag specification we should assume every other MP | |
74 | * with the same MAID has the same transmission interval that we have. If | |
75 | * an MP has a different interval, cfm_process_heartbeat will register it | |
76 | * as a fault (likely due to a configuration error). Thus we can check all | |
77 | * MPs at once making this quite a bit simpler. | |
78 | * | |
79 | * According to the specification we should check when (ccm_interval_ms * | |
80 | * 3.5)ms have passed. */ | |
81 | return (cfmi->ccm_interval_ms * 7) / 2; | |
82 | } | |
83 | ||
b31bcf60 EJ |
84 | static uint8_t |
85 | ms_to_ccm_interval(int interval_ms) | |
86 | { | |
87 | uint8_t i; | |
88 | ||
89 | for (i = 7; i > 0; i--) { | |
90 | if (ccm_interval_to_ms(i) <= interval_ms) { | |
91 | return i; | |
92 | } | |
93 | } | |
94 | ||
95 | return 1; | |
96 | } | |
97 | ||
98 | static struct cfm_internal * | |
20c8e971 | 99 | cfm_to_internal(const struct cfm *cfm) |
b31bcf60 EJ |
100 | { |
101 | return CONTAINER_OF(cfm, struct cfm_internal, cfm); | |
102 | } | |
103 | ||
104 | static uint32_t | |
105 | hash_mpid(uint8_t mpid) | |
106 | { | |
107 | return hash_int(mpid, 0); | |
108 | } | |
109 | ||
110 | static bool | |
111 | cfm_is_valid_mpid(uint32_t mpid) | |
112 | { | |
113 | /* 802.1ag specification requires MPIDs to be within the range [1, 8191] */ | |
114 | return mpid >= 1 && mpid <= 8191; | |
115 | } | |
116 | ||
117 | static struct remote_mp * | |
118 | lookup_remote_mp(const struct hmap *hmap, uint16_t mpid) | |
119 | { | |
120 | struct remote_mp *rmp; | |
121 | ||
122 | HMAP_FOR_EACH_IN_BUCKET (rmp, node, hash_mpid(mpid), hmap) { | |
123 | if (rmp->mpid == mpid) { | |
124 | return rmp; | |
125 | } | |
126 | } | |
127 | ||
128 | return NULL; | |
129 | } | |
130 | ||
b31bcf60 EJ |
131 | /* Allocates a 'cfm' object. This object should have its 'mpid', 'maid', |
132 | * 'eth_src', and 'interval' filled out. When changes are made to the 'cfm' | |
133 | * object, cfm_configure should be called before using it. */ | |
134 | struct cfm * | |
135 | cfm_create(void) | |
136 | { | |
137 | struct cfm *cfm; | |
138 | struct cfm_internal *cfmi; | |
139 | ||
140 | cfmi = xzalloc(sizeof *cfmi); | |
141 | cfm = &cfmi->cfm; | |
0dd17bfd | 142 | cfmi->x_recv_time = LLONG_MIN; |
b31bcf60 EJ |
143 | |
144 | hmap_init(&cfm->remote_mps); | |
b31bcf60 EJ |
145 | return cfm; |
146 | } | |
147 | ||
148 | void | |
149 | cfm_destroy(struct cfm *cfm) | |
150 | { | |
1c2e2d2f | 151 | struct cfm_internal *cfmi = cfm_to_internal(cfm); |
b31bcf60 | 152 | struct remote_mp *rmp, *rmp_next; |
b31bcf60 EJ |
153 | |
154 | if (!cfm) { | |
155 | return; | |
156 | } | |
157 | ||
158 | HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &cfm->remote_mps) { | |
159 | hmap_remove(&cfm->remote_mps, &rmp->node); | |
160 | free(rmp); | |
161 | } | |
162 | ||
b31bcf60 | 163 | hmap_destroy(&cfm->remote_mps); |
1c2e2d2f | 164 | free(cfmi); |
b31bcf60 EJ |
165 | } |
166 | ||
a58727fb EJ |
167 | /* Should be run periodically to update fault statistics messages. */ |
168 | void | |
b31bcf60 EJ |
169 | cfm_run(struct cfm *cfm) |
170 | { | |
171 | long long now = time_msec(); | |
172 | struct cfm_internal *cfmi = cfm_to_internal(cfm); | |
173 | ||
6fabb78d | 174 | if (timer_expired(&cfmi->fault_timer)) { |
b31bcf60 | 175 | bool fault; |
0dd17bfd | 176 | struct remote_mp *rmp; |
76c9c423 | 177 | long long int interval; |
b31bcf60 | 178 | |
76c9c423 EJ |
179 | interval = cfm_fault_interval(cfmi); |
180 | fault = now < cfmi->x_recv_time + interval; | |
b31bcf60 EJ |
181 | |
182 | HMAP_FOR_EACH (rmp, node, &cfm->remote_mps) { | |
76c9c423 EJ |
183 | if (rmp->recv_time < timer_enabled_at(&cfmi->fault_timer, interval) |
184 | || timer_expired_at(&cfmi->fault_timer, rmp->recv_time)) { | |
6fabb78d EJ |
185 | rmp->fault = true; |
186 | } | |
187 | ||
188 | if (rmp->fault) { | |
189 | fault = true; | |
190 | } | |
b31bcf60 EJ |
191 | } |
192 | ||
6fabb78d | 193 | cfm->fault = fault; |
76c9c423 | 194 | timer_set_duration(&cfmi->fault_timer, interval); |
b31bcf60 | 195 | } |
a58727fb | 196 | } |
b31bcf60 | 197 | |
a58727fb EJ |
198 | /* Should be run periodically to check if the CFM module has a CCM message it |
199 | * wishes to send. */ | |
200 | bool | |
201 | cfm_should_send_ccm(struct cfm *cfm) | |
202 | { | |
203 | struct cfm_internal *cfmi = cfm_to_internal(cfm); | |
b31bcf60 | 204 | |
6fabb78d | 205 | return timer_expired(&cfmi->tx_timer); |
a58727fb EJ |
206 | } |
207 | ||
208 | /* Composes a CCM message into 'ccm'. Messages generated with this function | |
209 | * should be sent whenever cfm_should_send_ccm() indicates. */ | |
210 | void | |
211 | cfm_compose_ccm(struct cfm *cfm, struct ccm *ccm) | |
212 | { | |
213 | struct cfm_internal *cfmi = cfm_to_internal(cfm); | |
214 | ||
6fabb78d | 215 | timer_set_duration(&cfmi->tx_timer, cfmi->ccm_interval_ms); |
a58727fb EJ |
216 | |
217 | ccm->mdlevel_version = 0; | |
218 | ccm->opcode = CCM_OPCODE; | |
219 | ccm->tlv_offset = 70; | |
220 | ccm->seq = htonl(++cfmi->seq); | |
221 | ccm->mpid = htons(cfmi->cfm.mpid); | |
222 | ccm->flags = cfmi->ccm_interval; | |
223 | memcpy(ccm->maid, cfmi->cfm.maid, sizeof ccm->maid); | |
b31bcf60 EJ |
224 | } |
225 | ||
226 | void | |
227 | cfm_wait(struct cfm *cfm) | |
228 | { | |
b31bcf60 EJ |
229 | struct cfm_internal *cfmi = cfm_to_internal(cfm); |
230 | ||
6fabb78d EJ |
231 | timer_wait(&cfmi->tx_timer); |
232 | timer_wait(&cfmi->fault_timer); | |
b31bcf60 EJ |
233 | } |
234 | ||
235 | /* Should be called whenever a client of the cfm library changes the internals | |
236 | * of 'cfm'. Returns true if 'cfm' is valid. */ | |
237 | bool | |
238 | cfm_configure(struct cfm *cfm) | |
239 | { | |
9aa952b2 EJ |
240 | struct cfm_internal *cfmi = cfm_to_internal(cfm); |
241 | uint8_t interval; | |
b31bcf60 EJ |
242 | |
243 | if (!cfm_is_valid_mpid(cfm->mpid) || !cfm->interval) { | |
244 | return false; | |
245 | } | |
246 | ||
9aa952b2 EJ |
247 | interval = ms_to_ccm_interval(cfm->interval); |
248 | ||
249 | if (interval != cfmi->ccm_interval) { | |
250 | cfmi->ccm_interval = interval; | |
251 | cfmi->ccm_interval_ms = ccm_interval_to_ms(interval); | |
252 | ||
9aa952b2 | 253 | timer_set_expired(&cfmi->tx_timer); |
aac19178 | 254 | timer_set_duration(&cfmi->fault_timer, cfm_fault_interval(cfmi)); |
9aa952b2 | 255 | } |
b31bcf60 | 256 | |
b31bcf60 EJ |
257 | return true; |
258 | } | |
259 | ||
260 | /* Given an array of MPIDs, updates the 'remote_mps' map of 'cfm' to reflect | |
261 | * it. Invalid MPIDs are skipped. */ | |
262 | void | |
263 | cfm_update_remote_mps(struct cfm *cfm, const uint16_t *mpids, size_t n_mpids) | |
264 | { | |
265 | size_t i; | |
266 | struct hmap new_rmps; | |
267 | struct remote_mp *rmp, *rmp_next; | |
268 | ||
269 | hmap_init(&new_rmps); | |
270 | ||
271 | for (i = 0; i < n_mpids; i++) { | |
272 | uint16_t mpid = mpids[i]; | |
273 | ||
274 | if (!cfm_is_valid_mpid(mpid) | |
275 | || lookup_remote_mp(&new_rmps, mpid)) { | |
276 | continue; | |
277 | } | |
278 | ||
279 | if ((rmp = lookup_remote_mp(&cfm->remote_mps, mpid))) { | |
280 | hmap_remove(&cfm->remote_mps, &rmp->node); | |
b31bcf60 EJ |
281 | } else { |
282 | rmp = xzalloc(sizeof *rmp); | |
283 | rmp->mpid = mpid; | |
284 | } | |
285 | ||
286 | hmap_insert(&new_rmps, &rmp->node, hash_mpid(mpid)); | |
287 | } | |
288 | ||
289 | hmap_swap(&new_rmps, &cfm->remote_mps); | |
290 | ||
291 | HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &new_rmps) { | |
292 | hmap_remove(&new_rmps, &rmp->node); | |
293 | free(rmp); | |
294 | } | |
295 | ||
296 | hmap_destroy(&new_rmps); | |
297 | } | |
298 | ||
299 | /* Finds a 'remote_mp' with 'mpid' in 'cfm'. If no such 'remote_mp' exists | |
300 | * returns NULL. */ | |
301 | const struct remote_mp * | |
302 | cfm_get_remote_mp(const struct cfm *cfm, uint16_t mpid) | |
303 | { | |
304 | return lookup_remote_mp(&cfm->remote_mps, mpid); | |
305 | } | |
306 | ||
307 | /* Generates 'maid' from 'md_name' and 'ma_name'. A NULL parameter indicates | |
308 | * the default should be used. Returns false if unsuccessful. */ | |
309 | bool | |
310 | cfm_generate_maid(const char *md_name, const char *ma_name, | |
311 | uint8_t maid[CCM_MAID_LEN]) | |
312 | { | |
313 | uint8_t *ma_p; | |
314 | size_t md_len, ma_len; | |
315 | ||
316 | if (!md_name) { | |
317 | md_name = "ovs"; | |
318 | } | |
319 | ||
320 | if (!ma_name) { | |
321 | ma_name = "ovs"; | |
322 | } | |
323 | ||
324 | memset(maid, 0, CCM_MAID_LEN); | |
325 | ||
326 | md_len = strlen(md_name); | |
327 | ma_len = strlen(ma_name); | |
328 | ||
329 | if (!md_len || !ma_len || md_len + ma_len + 4 > CCM_MAID_LEN) { | |
330 | return false; | |
331 | } | |
332 | ||
333 | maid[0] = 4; /* MD name string format. */ | |
334 | maid[1] = md_len; /* MD name size. */ | |
335 | memcpy(&maid[2], md_name, md_len); /* MD name. */ | |
336 | ||
337 | ma_p = maid + 2 + md_len; | |
338 | ma_p[0] = 2; /* MA name string format. */ | |
339 | ma_p[1] = ma_len; /* MA name size. */ | |
340 | memcpy(&ma_p[2], ma_name, ma_len); /* MA name. */ | |
341 | return true; | |
342 | } | |
343 | ||
344 | /* Returns true if the CFM library should process packets from 'flow'. */ | |
345 | bool | |
346 | cfm_should_process_flow(const struct flow *flow) | |
347 | { | |
348 | return (ntohs(flow->dl_type) == ETH_TYPE_CFM | |
15df7ea8 | 349 | && eth_addr_equals(flow->dl_dst, eth_addr_ccm)); |
b31bcf60 EJ |
350 | } |
351 | ||
352 | /* Updates internal statistics relevant to packet 'p'. Should be called on | |
353 | * every packet whose flow returned true when passed to | |
354 | * cfm_should_process_flow. */ | |
355 | void | |
356 | cfm_process_heartbeat(struct cfm *cfm, const struct ofpbuf *p) | |
357 | { | |
358 | struct ccm *ccm; | |
359 | uint16_t ccm_mpid; | |
b31bcf60 EJ |
360 | uint8_t ccm_interval; |
361 | struct remote_mp *rmp; | |
0dd17bfd | 362 | struct eth_header *eth; |
b31bcf60 EJ |
363 | |
364 | struct cfm_internal *cfmi = cfm_to_internal(cfm); | |
365 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20); | |
366 | ||
0dd17bfd | 367 | eth = p->l2; |
b31bcf60 EJ |
368 | ccm = ofpbuf_at(p, (uint8_t *)p->l3 - (uint8_t *)p->data, CCM_LEN); |
369 | ||
370 | if (!ccm) { | |
371 | VLOG_INFO_RL(&rl, "Received an un-parseable 802.1ag CCM heartbeat."); | |
372 | return; | |
373 | } | |
374 | ||
375 | if (ccm->opcode != CCM_OPCODE) { | |
376 | VLOG_INFO_RL(&rl, "Received an unsupported 802.1ag message. " | |
377 | "(opcode %u)", ccm->opcode); | |
378 | return; | |
379 | } | |
380 | ||
5e809322 EJ |
381 | /* According to the 802.1ag specification, reception of a CCM with an |
382 | * incorrect ccm_interval should trigger a fault. We ignore this | |
383 | * requirement for several reasons. | |
384 | * | |
385 | * Faults can cause a controller or Open vSwitch to make potentially | |
386 | * expensive changes to the network topology. It seems prudent to trigger | |
387 | * them judiciously, especially when CFM is used to check slave status of | |
388 | * bonds. Furthermore, faults can be maliciously triggered by crafting | |
389 | * invalid CCMs. */ | |
b31bcf60 | 390 | if (memcmp(ccm->maid, cfm->maid, sizeof ccm->maid)) { |
0dd17bfd | 391 | cfmi->x_recv_time = time_msec(); |
f805c4cc | 392 | cfm->fault = true; |
0dd17bfd EJ |
393 | VLOG_WARN_RL(&rl, "Received unexpected remote MAID from MAC " |
394 | ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_src)); | |
f805c4cc EJ |
395 | } else { |
396 | ccm_mpid = ntohs(ccm->mpid); | |
f805c4cc | 397 | ccm_interval = ccm->flags & 0x7; |
b31bcf60 | 398 | |
f805c4cc | 399 | rmp = lookup_remote_mp(&cfm->remote_mps, ccm_mpid); |
b31bcf60 | 400 | |
0dd17bfd EJ |
401 | if (rmp) { |
402 | rmp->recv_time = time_msec(); | |
5e809322 EJ |
403 | |
404 | if (ccm_interval != cfmi->ccm_interval) { | |
405 | VLOG_WARN_RL(&rl, "received a CCM with an invalid interval" | |
406 | " (%"PRIu8") from RMP %"PRIu16, ccm_interval, | |
407 | rmp->mpid); | |
408 | } | |
0dd17bfd EJ |
409 | } else { |
410 | cfmi->x_recv_time = time_msec(); | |
f805c4cc | 411 | cfm->fault = true; |
0dd17bfd EJ |
412 | VLOG_WARN_RL(&rl, "Received unexpected remote MPID %d from MAC " |
413 | ETH_ADDR_FMT, ccm_mpid, ETH_ADDR_ARGS(eth->eth_src)); | |
f805c4cc | 414 | } |
b31bcf60 | 415 | } |
b31bcf60 | 416 | } |
20c8e971 | 417 | |
20c8e971 EJ |
418 | void |
419 | cfm_dump_ds(const struct cfm *cfm, struct ds *ds) | |
420 | { | |
421 | const struct cfm_internal *cfmi = cfm_to_internal(cfm); | |
422 | long long int now = time_msec(); | |
423 | struct remote_mp *rmp; | |
424 | ||
425 | ds_put_format(ds, "MPID %"PRIu16": %s\n", cfm->mpid, | |
426 | cfm->fault ? "fault" : ""); | |
427 | ||
428 | ds_put_format(ds, "\tinterval: %dms\n", cfmi->ccm_interval_ms); | |
6fabb78d EJ |
429 | ds_put_format(ds, "\tnext CCM tx: %lldms\n", |
430 | timer_msecs_until_expired(&cfmi->tx_timer)); | |
431 | ds_put_format(ds, "\tnext fault check: %lldms\n", | |
432 | timer_msecs_until_expired(&cfmi->fault_timer)); | |
20c8e971 | 433 | |
6fcdfcd0 EJ |
434 | if (cfmi->x_recv_time != LLONG_MIN) { |
435 | ds_put_format(ds, "\ttime since bad CCM rx: %lldms\n", | |
436 | now - cfmi->x_recv_time); | |
437 | } | |
438 | ||
20c8e971 EJ |
439 | ds_put_cstr(ds, "\n"); |
440 | HMAP_FOR_EACH (rmp, node, &cfm->remote_mps) { | |
0dd17bfd EJ |
441 | ds_put_format(ds, "Remote MPID %"PRIu16": %s\n", rmp->mpid, |
442 | rmp->fault ? "fault" : ""); | |
443 | ds_put_format(ds, "\ttime since CCM rx: %lldms\n", | |
444 | time_msec() - rmp->recv_time); | |
20c8e971 EJ |
445 | } |
446 | } |