2 * Copyright (c) 2010, 2011 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include "dynamic-string.h"
30 #include "poll-loop.h"
35 VLOG_DEFINE_THIS_MODULE(cfm);
37 #define CCM_OPCODE 1 /* CFM message opcode meaning CCM. */
41 uint32_t seq; /* The sequence number of our last CCM. */
43 uint8_t ccm_interval; /* The CCM transmission interval. */
44 int ccm_interval_ms; /* 'ccm_interval' in milliseconds. */
46 struct timer tx_timer; /* Send CCM when expired. */
47 struct timer fault_timer; /* Check for faults when expired. */
50 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
53 ccm_interval_to_ms(uint8_t 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. */
63 case 7: return 600000;
64 default: NOT_REACHED(); /* Explicitly not supported by 802.1ag. */
71 cfm_fault_interval(struct cfm_internal *cfmi)
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.
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;
85 ms_to_ccm_interval(int interval_ms)
89 for (i = 7; i > 0; i--) {
90 if (ccm_interval_to_ms(i) <= interval_ms) {
98 static struct cfm_internal *
99 cfm_to_internal(const struct cfm *cfm)
101 return CONTAINER_OF(cfm, struct cfm_internal, cfm);
105 hash_mpid(uint8_t mpid)
107 return hash_int(mpid, 0);
111 cfm_is_valid_mpid(uint32_t mpid)
113 /* 802.1ag specification requires MPIDs to be within the range [1, 8191] */
114 return mpid >= 1 && mpid <= 8191;
117 static struct remote_mp *
118 lookup_remote_mp(const struct hmap *hmap, uint16_t mpid)
120 struct remote_mp *rmp;
122 HMAP_FOR_EACH_IN_BUCKET (rmp, node, hash_mpid(mpid), hmap) {
123 if (rmp->mpid == mpid) {
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. */
138 struct cfm_internal *cfmi;
140 cfmi = xzalloc(sizeof *cfmi);
143 hmap_init(&cfm->remote_mps);
148 cfm_destroy(struct cfm *cfm)
150 struct cfm_internal *cfmi = cfm_to_internal(cfm);
151 struct remote_mp *rmp, *rmp_next;
157 HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &cfm->remote_mps) {
158 hmap_remove(&cfm->remote_mps, &rmp->node);
162 hmap_destroy(&cfm->remote_mps);
166 /* Should be run periodically to update fault statistics messages. */
168 cfm_run(struct cfm *cfm)
170 struct cfm_internal *cfmi = cfm_to_internal(cfm);
172 if (timer_expired(&cfmi->fault_timer)) {
173 long long int interval = cfm_fault_interval(cfmi);
174 struct remote_mp *rmp;
177 HMAP_FOR_EACH (rmp, node, &cfm->remote_mps) {
178 rmp->fault = !rmp->recv;
183 VLOG_DBG("No CCM from RMP %"PRIu16" in the last %lldms",
184 rmp->mpid, interval);
189 VLOG_DBG("All RMPs received CCMs in the last %lldms", interval);
192 timer_set_duration(&cfmi->fault_timer, interval);
196 /* Should be run periodically to check if the CFM module has a CCM message it
199 cfm_should_send_ccm(struct cfm *cfm)
201 struct cfm_internal *cfmi = cfm_to_internal(cfm);
203 return timer_expired(&cfmi->tx_timer);
206 /* Composes a CCM message into 'ccm'. Messages generated with this function
207 * should be sent whenever cfm_should_send_ccm() indicates. */
209 cfm_compose_ccm(struct cfm *cfm, struct ccm *ccm)
211 struct cfm_internal *cfmi = cfm_to_internal(cfm);
213 timer_set_duration(&cfmi->tx_timer, cfmi->ccm_interval_ms);
215 ccm->mdlevel_version = 0;
216 ccm->opcode = CCM_OPCODE;
217 ccm->tlv_offset = 70;
218 ccm->seq = htonl(++cfmi->seq);
219 ccm->mpid = htons(cfmi->cfm.mpid);
220 ccm->flags = cfmi->ccm_interval;
221 memcpy(ccm->maid, cfmi->cfm.maid, sizeof ccm->maid);
225 cfm_wait(struct cfm *cfm)
227 struct cfm_internal *cfmi = cfm_to_internal(cfm);
229 timer_wait(&cfmi->tx_timer);
230 timer_wait(&cfmi->fault_timer);
233 /* Should be called whenever a client of the cfm library changes the internals
234 * of 'cfm'. Returns true if 'cfm' is valid. */
236 cfm_configure(struct cfm *cfm)
238 struct cfm_internal *cfmi = cfm_to_internal(cfm);
241 if (!cfm_is_valid_mpid(cfm->mpid) || !cfm->interval) {
245 interval = ms_to_ccm_interval(cfm->interval);
247 if (interval != cfmi->ccm_interval) {
248 cfmi->ccm_interval = interval;
249 cfmi->ccm_interval_ms = ccm_interval_to_ms(interval);
251 timer_set_expired(&cfmi->tx_timer);
252 timer_set_duration(&cfmi->fault_timer, cfm_fault_interval(cfmi));
258 /* Given an array of MPIDs, updates the 'remote_mps' map of 'cfm' to reflect
259 * it. Invalid MPIDs are skipped. */
261 cfm_update_remote_mps(struct cfm *cfm, const uint16_t *mpids, size_t n_mpids)
264 struct hmap new_rmps;
265 struct remote_mp *rmp, *rmp_next;
267 hmap_init(&new_rmps);
269 for (i = 0; i < n_mpids; i++) {
270 uint16_t mpid = mpids[i];
272 if (!cfm_is_valid_mpid(mpid)
273 || lookup_remote_mp(&new_rmps, mpid)) {
277 if ((rmp = lookup_remote_mp(&cfm->remote_mps, mpid))) {
278 hmap_remove(&cfm->remote_mps, &rmp->node);
280 rmp = xzalloc(sizeof *rmp);
284 hmap_insert(&new_rmps, &rmp->node, hash_mpid(mpid));
287 hmap_swap(&new_rmps, &cfm->remote_mps);
289 HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &new_rmps) {
290 hmap_remove(&new_rmps, &rmp->node);
294 hmap_destroy(&new_rmps);
297 /* Finds a 'remote_mp' with 'mpid' in 'cfm'. If no such 'remote_mp' exists
299 const struct remote_mp *
300 cfm_get_remote_mp(const struct cfm *cfm, uint16_t mpid)
302 return lookup_remote_mp(&cfm->remote_mps, mpid);
305 /* Generates 'maid' from 'md_name' and 'ma_name'. A NULL parameter indicates
306 * the default should be used. Returns false if unsuccessful. */
308 cfm_generate_maid(const char *md_name, const char *ma_name,
309 uint8_t maid[CCM_MAID_LEN])
312 size_t md_len, ma_len;
322 memset(maid, 0, CCM_MAID_LEN);
324 md_len = strlen(md_name);
325 ma_len = strlen(ma_name);
327 if (!md_len || !ma_len || md_len + ma_len + 4 > CCM_MAID_LEN) {
331 maid[0] = 4; /* MD name string format. */
332 maid[1] = md_len; /* MD name size. */
333 memcpy(&maid[2], md_name, md_len); /* MD name. */
335 ma_p = maid + 2 + md_len;
336 ma_p[0] = 2; /* MA name string format. */
337 ma_p[1] = ma_len; /* MA name size. */
338 memcpy(&ma_p[2], ma_name, ma_len); /* MA name. */
342 /* Returns true if the CFM library should process packets from 'flow'. */
344 cfm_should_process_flow(const struct flow *flow)
346 return (ntohs(flow->dl_type) == ETH_TYPE_CFM
347 && eth_addr_equals(flow->dl_dst, eth_addr_ccm));
350 /* Updates internal statistics relevant to packet 'p'. Should be called on
351 * every packet whose flow returned true when passed to
352 * cfm_should_process_flow. */
354 cfm_process_heartbeat(struct cfm *cfm, const struct ofpbuf *p)
358 uint8_t ccm_interval;
359 struct remote_mp *rmp;
360 struct eth_header *eth;
361 struct cfm_internal *cfmi = cfm_to_internal(cfm);
364 ccm = ofpbuf_at(p, (uint8_t *)p->l3 - (uint8_t *)p->data, CCM_LEN);
367 VLOG_INFO_RL(&rl, "Received an un-parseable 802.1ag CCM heartbeat.");
371 if (ccm->opcode != CCM_OPCODE) {
372 VLOG_INFO_RL(&rl, "Received an unsupported 802.1ag message. "
373 "(opcode %u)", ccm->opcode);
377 /* According to the 802.1ag specification, reception of a CCM with an
378 * incorrect ccm_interval, unexpected MAID, or unexpected MPID should
379 * trigger a fault. We ignore this requirement for several reasons.
381 * Faults can cause a controller or Open vSwitch to make potentially
382 * expensive changes to the network topology. It seems prudent to trigger
383 * them judiciously, especially when CFM is used to check slave status of
384 * bonds. Furthermore, faults can be maliciously triggered by crafting
386 if (memcmp(ccm->maid, cfm->maid, sizeof ccm->maid)) {
387 VLOG_WARN_RL(&rl, "Received unexpected remote MAID from MAC "
388 ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_src));
390 ccm_mpid = ntohs(ccm->mpid);
391 ccm_interval = ccm->flags & 0x7;
393 rmp = lookup_remote_mp(&cfm->remote_mps, ccm_mpid);
398 if (ccm_interval != cfmi->ccm_interval) {
399 VLOG_WARN_RL(&rl, "received a CCM with an invalid interval"
400 " (%"PRIu8") from RMP %"PRIu16, ccm_interval,
404 VLOG_WARN_RL(&rl, "Received unexpected remote MPID %d from MAC "
405 ETH_ADDR_FMT, ccm_mpid, ETH_ADDR_ARGS(eth->eth_src));
411 cfm_dump_ds(const struct cfm *cfm, struct ds *ds)
413 const struct cfm_internal *cfmi = cfm_to_internal(cfm);
414 struct remote_mp *rmp;
416 ds_put_format(ds, "MPID %"PRIu16": %s\n", cfm->mpid,
417 cfm->fault ? "fault" : "");
419 ds_put_format(ds, "\tinterval: %dms\n", cfmi->ccm_interval_ms);
420 ds_put_format(ds, "\tnext CCM tx: %lldms\n",
421 timer_msecs_until_expired(&cfmi->tx_timer));
422 ds_put_format(ds, "\tnext fault check: %lldms\n",
423 timer_msecs_until_expired(&cfmi->fault_timer));
425 ds_put_cstr(ds, "\n");
426 HMAP_FOR_EACH (rmp, node, &cfm->remote_mps) {
427 ds_put_format(ds, "Remote MPID %"PRIu16": %s\n", rmp->mpid,
428 rmp->fault ? "fault" : "");
429 ds_put_format(ds, "\trecv since check: %s",
430 rmp->recv ? "true" : "false");