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