]>
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 | ||
84c5d450 | 20 | #include <assert.h> |
b31bcf60 EJ |
21 | #include <stdint.h> |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
24 | ||
20c8e971 | 25 | #include "dynamic-string.h" |
b31bcf60 EJ |
26 | #include "flow.h" |
27 | #include "hash.h" | |
28 | #include "hmap.h" | |
29 | #include "ofpbuf.h" | |
30 | #include "packets.h" | |
31 | #include "poll-loop.h" | |
6fabb78d | 32 | #include "timer.h" |
b31bcf60 | 33 | #include "timeval.h" |
9ac3fce4 | 34 | #include "unixctl.h" |
b31bcf60 EJ |
35 | #include "vlog.h" |
36 | ||
37 | VLOG_DEFINE_THIS_MODULE(cfm); | |
38 | ||
39 | #define CCM_OPCODE 1 /* CFM message opcode meaning CCM. */ | |
b31bcf60 EJ |
40 | |
41 | struct cfm_internal { | |
42 | struct cfm cfm; | |
9ac3fce4 | 43 | struct list list_node; /* Node in all_cfms list. */ |
b31bcf60 | 44 | |
9ac3fce4 | 45 | uint32_t seq; /* The sequence number of our last CCM. */ |
b31bcf60 EJ |
46 | uint8_t ccm_interval; /* The CCM transmission interval. */ |
47 | int ccm_interval_ms; /* 'ccm_interval' in milliseconds. */ | |
84c5d450 | 48 | uint8_t maid[CCM_MAID_LEN]; /* The MAID of this CFM. */ |
b31bcf60 | 49 | |
6fabb78d EJ |
50 | struct timer tx_timer; /* Send CCM when expired. */ |
51 | struct timer fault_timer; /* Check for faults when expired. */ | |
93b8df38 EJ |
52 | |
53 | struct hmap remote_mps; /* Expected remote MPs. */ | |
54 | }; | |
55 | ||
56 | /* Remote MPs represent foreign network entities that are configured to have | |
57 | * the same MAID as this CFM instance. */ | |
58 | struct remote_mp { | |
59 | uint16_t mpid; /* The Maintenance Point ID of this 'remote_mp'. */ | |
60 | struct hmap_node node; /* Node in 'remote_mps' map. */ | |
61 | ||
62 | bool recv; /* CCM was received since last fault check. */ | |
63 | bool fault; /* Indicates a connectivity fault. */ | |
b31bcf60 EJ |
64 | }; |
65 | ||
dd986e09 | 66 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20); |
9ac3fce4 EJ |
67 | static struct list all_cfms = LIST_INITIALIZER(&all_cfms); |
68 | ||
69 | static void cfm_unixctl_show(struct unixctl_conn *, const char *args, | |
70 | void *aux); | |
dd986e09 | 71 | |
84c5d450 EJ |
72 | static void |
73 | cfm_generate_maid(struct cfm_internal *cfmi) | |
74 | { | |
75 | const char *ovs_md_name = "ovs_md"; | |
76 | const char *ovs_ma_name = "ovs_ma"; | |
77 | uint8_t *ma_p; | |
78 | size_t md_len, ma_len; | |
79 | ||
80 | memset(cfmi->maid, 0, CCM_MAID_LEN); | |
81 | ||
82 | md_len = strlen(ovs_md_name); | |
83 | ma_len = strlen(ovs_ma_name); | |
84 | ||
85 | assert(md_len && ma_len && md_len + ma_len + 4 <= CCM_MAID_LEN); | |
86 | ||
87 | cfmi->maid[0] = 4; /* MD name string format. */ | |
88 | cfmi->maid[1] = md_len; /* MD name size. */ | |
89 | memcpy(&cfmi->maid[2], ovs_md_name, md_len); /* MD name. */ | |
90 | ||
91 | ma_p = cfmi->maid + 2 + md_len; | |
92 | ma_p[0] = 2; /* MA name string format. */ | |
93 | ma_p[1] = ma_len; /* MA name size. */ | |
94 | memcpy(&ma_p[2], ovs_ma_name, ma_len); /* MA name. */ | |
95 | } | |
96 | ||
b31bcf60 EJ |
97 | static int |
98 | ccm_interval_to_ms(uint8_t interval) | |
99 | { | |
100 | switch (interval) { | |
101 | case 0: NOT_REACHED(); /* Explicitly not supported by 802.1ag. */ | |
102 | case 1: return 3; /* Not recommended due to timer resolution. */ | |
103 | case 2: return 10; /* Not recommended due to timer resolution. */ | |
104 | case 3: return 100; | |
105 | case 4: return 1000; | |
106 | case 5: return 10000; | |
107 | case 6: return 60000; | |
108 | case 7: return 600000; | |
109 | default: NOT_REACHED(); /* Explicitly not supported by 802.1ag. */ | |
110 | } | |
111 | ||
112 | NOT_REACHED(); | |
113 | } | |
114 | ||
aac19178 EJ |
115 | static long long int |
116 | cfm_fault_interval(struct cfm_internal *cfmi) | |
117 | { | |
118 | /* According to the 802.1ag specification we should assume every other MP | |
119 | * with the same MAID has the same transmission interval that we have. If | |
120 | * an MP has a different interval, cfm_process_heartbeat will register it | |
121 | * as a fault (likely due to a configuration error). Thus we can check all | |
122 | * MPs at once making this quite a bit simpler. | |
123 | * | |
124 | * According to the specification we should check when (ccm_interval_ms * | |
125 | * 3.5)ms have passed. */ | |
126 | return (cfmi->ccm_interval_ms * 7) / 2; | |
127 | } | |
128 | ||
b31bcf60 EJ |
129 | static uint8_t |
130 | ms_to_ccm_interval(int interval_ms) | |
131 | { | |
132 | uint8_t i; | |
133 | ||
134 | for (i = 7; i > 0; i--) { | |
135 | if (ccm_interval_to_ms(i) <= interval_ms) { | |
136 | return i; | |
137 | } | |
138 | } | |
139 | ||
140 | return 1; | |
141 | } | |
142 | ||
143 | static struct cfm_internal * | |
20c8e971 | 144 | cfm_to_internal(const struct cfm *cfm) |
b31bcf60 EJ |
145 | { |
146 | return CONTAINER_OF(cfm, struct cfm_internal, cfm); | |
147 | } | |
148 | ||
149 | static uint32_t | |
150 | hash_mpid(uint8_t mpid) | |
151 | { | |
152 | return hash_int(mpid, 0); | |
153 | } | |
154 | ||
155 | static bool | |
156 | cfm_is_valid_mpid(uint32_t mpid) | |
157 | { | |
158 | /* 802.1ag specification requires MPIDs to be within the range [1, 8191] */ | |
159 | return mpid >= 1 && mpid <= 8191; | |
160 | } | |
161 | ||
162 | static struct remote_mp * | |
163 | lookup_remote_mp(const struct hmap *hmap, uint16_t mpid) | |
164 | { | |
165 | struct remote_mp *rmp; | |
166 | ||
167 | HMAP_FOR_EACH_IN_BUCKET (rmp, node, hash_mpid(mpid), hmap) { | |
168 | if (rmp->mpid == mpid) { | |
169 | return rmp; | |
170 | } | |
171 | } | |
172 | ||
173 | return NULL; | |
174 | } | |
175 | ||
9ac3fce4 EJ |
176 | void |
177 | cfm_init(void) | |
178 | { | |
179 | unixctl_command_register("cfm/show", cfm_unixctl_show, NULL); | |
180 | } | |
181 | ||
b31bcf60 | 182 | /* Allocates a 'cfm' object. This object should have its 'mpid', 'maid', |
498b2a5a EJ |
183 | * 'eth_src', and 'interval' filled out. cfm_configure() should be called |
184 | * whenever changes are made to 'cfm', and before cfm_run() is called for the | |
185 | * first time. */ | |
b31bcf60 EJ |
186 | struct cfm * |
187 | cfm_create(void) | |
188 | { | |
189 | struct cfm *cfm; | |
190 | struct cfm_internal *cfmi; | |
191 | ||
192 | cfmi = xzalloc(sizeof *cfmi); | |
193 | cfm = &cfmi->cfm; | |
194 | ||
93b8df38 | 195 | hmap_init(&cfmi->remote_mps); |
84c5d450 | 196 | cfm_generate_maid(cfmi); |
9ac3fce4 | 197 | list_push_back(&all_cfms, &cfmi->list_node); |
b31bcf60 EJ |
198 | return cfm; |
199 | } | |
200 | ||
201 | void | |
202 | cfm_destroy(struct cfm *cfm) | |
203 | { | |
1c2e2d2f | 204 | struct cfm_internal *cfmi = cfm_to_internal(cfm); |
b31bcf60 | 205 | struct remote_mp *rmp, *rmp_next; |
b31bcf60 EJ |
206 | |
207 | if (!cfm) { | |
208 | return; | |
209 | } | |
210 | ||
93b8df38 EJ |
211 | HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &cfmi->remote_mps) { |
212 | hmap_remove(&cfmi->remote_mps, &rmp->node); | |
b31bcf60 EJ |
213 | free(rmp); |
214 | } | |
215 | ||
93b8df38 | 216 | hmap_destroy(&cfmi->remote_mps); |
9ac3fce4 | 217 | list_remove(&cfmi->list_node); |
1c2e2d2f | 218 | free(cfmi); |
b31bcf60 EJ |
219 | } |
220 | ||
a58727fb EJ |
221 | /* Should be run periodically to update fault statistics messages. */ |
222 | void | |
b31bcf60 EJ |
223 | cfm_run(struct cfm *cfm) |
224 | { | |
b31bcf60 EJ |
225 | struct cfm_internal *cfmi = cfm_to_internal(cfm); |
226 | ||
6fabb78d | 227 | if (timer_expired(&cfmi->fault_timer)) { |
aa7f1158 | 228 | long long int interval = cfm_fault_interval(cfmi); |
0dd17bfd | 229 | struct remote_mp *rmp; |
b31bcf60 | 230 | |
aa7f1158 | 231 | cfm->fault = false; |
93b8df38 | 232 | HMAP_FOR_EACH (rmp, node, &cfmi->remote_mps) { |
dd986e09 EJ |
233 | rmp->fault = !rmp->recv; |
234 | rmp->recv = false; | |
6fabb78d EJ |
235 | |
236 | if (rmp->fault) { | |
aa7f1158 | 237 | cfm->fault = true; |
dd986e09 EJ |
238 | VLOG_DBG("No CCM from RMP %"PRIu16" in the last %lldms", |
239 | rmp->mpid, interval); | |
6fabb78d | 240 | } |
b31bcf60 EJ |
241 | } |
242 | ||
dd986e09 EJ |
243 | if (!cfm->fault) { |
244 | VLOG_DBG("All RMPs received CCMs in the last %lldms", interval); | |
245 | } | |
246 | ||
76c9c423 | 247 | timer_set_duration(&cfmi->fault_timer, interval); |
b31bcf60 | 248 | } |
a58727fb | 249 | } |
b31bcf60 | 250 | |
a58727fb EJ |
251 | /* Should be run periodically to check if the CFM module has a CCM message it |
252 | * wishes to send. */ | |
253 | bool | |
254 | cfm_should_send_ccm(struct cfm *cfm) | |
255 | { | |
256 | struct cfm_internal *cfmi = cfm_to_internal(cfm); | |
b31bcf60 | 257 | |
6fabb78d | 258 | return timer_expired(&cfmi->tx_timer); |
a58727fb EJ |
259 | } |
260 | ||
261 | /* Composes a CCM message into 'ccm'. Messages generated with this function | |
262 | * should be sent whenever cfm_should_send_ccm() indicates. */ | |
263 | void | |
264 | cfm_compose_ccm(struct cfm *cfm, struct ccm *ccm) | |
265 | { | |
266 | struct cfm_internal *cfmi = cfm_to_internal(cfm); | |
267 | ||
6fabb78d | 268 | timer_set_duration(&cfmi->tx_timer, cfmi->ccm_interval_ms); |
a58727fb EJ |
269 | |
270 | ccm->mdlevel_version = 0; | |
271 | ccm->opcode = CCM_OPCODE; | |
272 | ccm->tlv_offset = 70; | |
273 | ccm->seq = htonl(++cfmi->seq); | |
274 | ccm->mpid = htons(cfmi->cfm.mpid); | |
275 | ccm->flags = cfmi->ccm_interval; | |
84c5d450 | 276 | memcpy(ccm->maid, cfmi->maid, sizeof ccm->maid); |
b31bcf60 EJ |
277 | } |
278 | ||
279 | void | |
280 | cfm_wait(struct cfm *cfm) | |
281 | { | |
b31bcf60 EJ |
282 | struct cfm_internal *cfmi = cfm_to_internal(cfm); |
283 | ||
6fabb78d EJ |
284 | timer_wait(&cfmi->tx_timer); |
285 | timer_wait(&cfmi->fault_timer); | |
b31bcf60 EJ |
286 | } |
287 | ||
288 | /* Should be called whenever a client of the cfm library changes the internals | |
289 | * of 'cfm'. Returns true if 'cfm' is valid. */ | |
290 | bool | |
291 | cfm_configure(struct cfm *cfm) | |
292 | { | |
9aa952b2 EJ |
293 | struct cfm_internal *cfmi = cfm_to_internal(cfm); |
294 | uint8_t interval; | |
b31bcf60 EJ |
295 | |
296 | if (!cfm_is_valid_mpid(cfm->mpid) || !cfm->interval) { | |
297 | return false; | |
298 | } | |
299 | ||
9aa952b2 EJ |
300 | interval = ms_to_ccm_interval(cfm->interval); |
301 | ||
302 | if (interval != cfmi->ccm_interval) { | |
303 | cfmi->ccm_interval = interval; | |
304 | cfmi->ccm_interval_ms = ccm_interval_to_ms(interval); | |
305 | ||
9aa952b2 | 306 | timer_set_expired(&cfmi->tx_timer); |
aac19178 | 307 | timer_set_duration(&cfmi->fault_timer, cfm_fault_interval(cfmi)); |
9aa952b2 | 308 | } |
b31bcf60 | 309 | |
b31bcf60 EJ |
310 | return true; |
311 | } | |
312 | ||
313 | /* Given an array of MPIDs, updates the 'remote_mps' map of 'cfm' to reflect | |
314 | * it. Invalid MPIDs are skipped. */ | |
315 | void | |
316 | cfm_update_remote_mps(struct cfm *cfm, const uint16_t *mpids, size_t n_mpids) | |
317 | { | |
93b8df38 | 318 | struct cfm_internal *cfmi = cfm_to_internal(cfm); |
b31bcf60 EJ |
319 | size_t i; |
320 | struct hmap new_rmps; | |
321 | struct remote_mp *rmp, *rmp_next; | |
322 | ||
323 | hmap_init(&new_rmps); | |
324 | ||
325 | for (i = 0; i < n_mpids; i++) { | |
326 | uint16_t mpid = mpids[i]; | |
327 | ||
328 | if (!cfm_is_valid_mpid(mpid) | |
329 | || lookup_remote_mp(&new_rmps, mpid)) { | |
330 | continue; | |
331 | } | |
332 | ||
93b8df38 EJ |
333 | if ((rmp = lookup_remote_mp(&cfmi->remote_mps, mpid))) { |
334 | hmap_remove(&cfmi->remote_mps, &rmp->node); | |
b31bcf60 EJ |
335 | } else { |
336 | rmp = xzalloc(sizeof *rmp); | |
337 | rmp->mpid = mpid; | |
338 | } | |
339 | ||
340 | hmap_insert(&new_rmps, &rmp->node, hash_mpid(mpid)); | |
341 | } | |
342 | ||
93b8df38 | 343 | hmap_swap(&new_rmps, &cfmi->remote_mps); |
b31bcf60 EJ |
344 | |
345 | HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &new_rmps) { | |
346 | hmap_remove(&new_rmps, &rmp->node); | |
347 | free(rmp); | |
348 | } | |
349 | ||
350 | hmap_destroy(&new_rmps); | |
351 | } | |
352 | ||
b31bcf60 EJ |
353 | /* Returns true if the CFM library should process packets from 'flow'. */ |
354 | bool | |
355 | cfm_should_process_flow(const struct flow *flow) | |
356 | { | |
357 | return (ntohs(flow->dl_type) == ETH_TYPE_CFM | |
15df7ea8 | 358 | && eth_addr_equals(flow->dl_dst, eth_addr_ccm)); |
b31bcf60 EJ |
359 | } |
360 | ||
361 | /* Updates internal statistics relevant to packet 'p'. Should be called on | |
362 | * every packet whose flow returned true when passed to | |
363 | * cfm_should_process_flow. */ | |
364 | void | |
365 | cfm_process_heartbeat(struct cfm *cfm, const struct ofpbuf *p) | |
366 | { | |
367 | struct ccm *ccm; | |
368 | uint16_t ccm_mpid; | |
b31bcf60 EJ |
369 | uint8_t ccm_interval; |
370 | struct remote_mp *rmp; | |
0dd17bfd | 371 | struct eth_header *eth; |
dd986e09 | 372 | struct cfm_internal *cfmi = cfm_to_internal(cfm); |
b31bcf60 | 373 | |
0dd17bfd | 374 | eth = p->l2; |
b31bcf60 EJ |
375 | ccm = ofpbuf_at(p, (uint8_t *)p->l3 - (uint8_t *)p->data, CCM_LEN); |
376 | ||
377 | if (!ccm) { | |
378 | VLOG_INFO_RL(&rl, "Received an un-parseable 802.1ag CCM heartbeat."); | |
379 | return; | |
380 | } | |
381 | ||
382 | if (ccm->opcode != CCM_OPCODE) { | |
383 | VLOG_INFO_RL(&rl, "Received an unsupported 802.1ag message. " | |
384 | "(opcode %u)", ccm->opcode); | |
385 | return; | |
386 | } | |
387 | ||
5e809322 | 388 | /* According to the 802.1ag specification, reception of a CCM with an |
aa7f1158 EJ |
389 | * incorrect ccm_interval, unexpected MAID, or unexpected MPID should |
390 | * trigger a fault. We ignore this requirement for several reasons. | |
5e809322 EJ |
391 | * |
392 | * Faults can cause a controller or Open vSwitch to make potentially | |
393 | * expensive changes to the network topology. It seems prudent to trigger | |
394 | * them judiciously, especially when CFM is used to check slave status of | |
395 | * bonds. Furthermore, faults can be maliciously triggered by crafting | |
396 | * invalid CCMs. */ | |
84c5d450 | 397 | if (memcmp(ccm->maid, cfmi->maid, sizeof ccm->maid)) { |
0dd17bfd EJ |
398 | VLOG_WARN_RL(&rl, "Received unexpected remote MAID from MAC " |
399 | ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_src)); | |
f805c4cc EJ |
400 | } else { |
401 | ccm_mpid = ntohs(ccm->mpid); | |
f805c4cc | 402 | ccm_interval = ccm->flags & 0x7; |
b31bcf60 | 403 | |
93b8df38 | 404 | rmp = lookup_remote_mp(&cfmi->remote_mps, ccm_mpid); |
b31bcf60 | 405 | |
0dd17bfd | 406 | if (rmp) { |
dd986e09 | 407 | rmp->recv = true; |
5e809322 EJ |
408 | |
409 | if (ccm_interval != cfmi->ccm_interval) { | |
410 | VLOG_WARN_RL(&rl, "received a CCM with an invalid interval" | |
411 | " (%"PRIu8") from RMP %"PRIu16, ccm_interval, | |
412 | rmp->mpid); | |
413 | } | |
0dd17bfd | 414 | } else { |
0dd17bfd EJ |
415 | VLOG_WARN_RL(&rl, "Received unexpected remote MPID %d from MAC " |
416 | ETH_ADDR_FMT, ccm_mpid, ETH_ADDR_ARGS(eth->eth_src)); | |
f805c4cc | 417 | } |
70e3e6af EJ |
418 | |
419 | VLOG_DBG("Received CCM (mpid %"PRIu16") (interval %"PRIu8")", ccm_mpid, | |
420 | ccm_interval); | |
b31bcf60 | 421 | } |
b31bcf60 | 422 | } |
20c8e971 | 423 | |
9ac3fce4 EJ |
424 | static struct cfm_internal * |
425 | cfm_find(const char *name) | |
20c8e971 | 426 | { |
9ac3fce4 EJ |
427 | struct cfm_internal *cfmi; |
428 | ||
429 | LIST_FOR_EACH (cfmi, list_node, &all_cfms) { | |
430 | if (cfmi->cfm.name && !strcmp(cfmi->cfm.name, name)) { | |
431 | return cfmi; | |
432 | } | |
433 | } | |
434 | return NULL; | |
435 | } | |
436 | ||
437 | static void | |
438 | cfm_unixctl_show(struct unixctl_conn *conn, | |
439 | const char *args, void *aux OVS_UNUSED) | |
440 | { | |
441 | struct ds ds = DS_EMPTY_INITIALIZER; | |
442 | const struct cfm_internal *cfmi; | |
20c8e971 EJ |
443 | struct remote_mp *rmp; |
444 | ||
9ac3fce4 EJ |
445 | cfmi = cfm_find(args); |
446 | if (!cfmi) { | |
447 | unixctl_command_reply(conn, 501, "no such CFM object"); | |
448 | return; | |
449 | } | |
20c8e971 | 450 | |
9ac3fce4 EJ |
451 | ds_put_format(&ds, "MPID %"PRIu16": %s\n", cfmi->cfm.mpid, |
452 | cfmi->cfm.fault ? "fault" : ""); | |
453 | ||
454 | ds_put_format(&ds, "\tinterval: %dms\n", cfmi->ccm_interval_ms); | |
455 | ds_put_format(&ds, "\tnext CCM tx: %lldms\n", | |
6fabb78d | 456 | timer_msecs_until_expired(&cfmi->tx_timer)); |
9ac3fce4 | 457 | ds_put_format(&ds, "\tnext fault check: %lldms\n", |
6fabb78d | 458 | timer_msecs_until_expired(&cfmi->fault_timer)); |
20c8e971 | 459 | |
9ac3fce4 | 460 | ds_put_cstr(&ds, "\n"); |
93b8df38 | 461 | HMAP_FOR_EACH (rmp, node, &cfmi->remote_mps) { |
9ac3fce4 | 462 | ds_put_format(&ds, "Remote MPID %"PRIu16": %s\n", rmp->mpid, |
0dd17bfd | 463 | rmp->fault ? "fault" : ""); |
9ac3fce4 | 464 | ds_put_format(&ds, "\trecv since check: %s", |
dd986e09 | 465 | rmp->recv ? "true" : "false"); |
20c8e971 | 466 | } |
9ac3fce4 EJ |
467 | |
468 | unixctl_command_reply(conn, 200, ds_cstr(&ds)); | |
469 | ds_destroy(&ds); | |
20c8e971 | 470 | } |