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.
29 #include "poll-loop.h"
33 VLOG_DEFINE_THIS_MODULE(cfm);
35 #define CCM_OPCODE 1 /* CFM message opcode meaning CCM. */
39 uint32_t seq; /* The sequence number of our last CCM. */
41 uint8_t ccm_interval; /* The CCM transmission interval. */
42 int ccm_interval_ms; /* 'ccm_interval' in milliseconds. */
44 long long ccm_sent; /* The time we last sent a CCM. */
45 long long fault_check; /* The time we last checked for faults. */
47 struct hmap x_remote_mps; /* Unexpected remote MPs. */
48 struct hmap x_remote_maids; /* Unexpected remote MAIDs. */
52 ccm_interval_to_ms(uint8_t interval)
55 case 0: NOT_REACHED(); /* Explicitly not supported by 802.1ag. */
56 case 1: return 3; /* Not recommended due to timer resolution. */
57 case 2: return 10; /* Not recommended due to timer resolution. */
62 case 7: return 600000;
63 default: NOT_REACHED(); /* Explicitly not supported by 802.1ag. */
70 ms_to_ccm_interval(int interval_ms)
74 for (i = 7; i > 0; i--) {
75 if (ccm_interval_to_ms(i) <= interval_ms) {
83 static struct cfm_internal *
84 cfm_to_internal(struct cfm *cfm)
86 return CONTAINER_OF(cfm, struct cfm_internal, cfm);
90 hash_mpid(uint8_t mpid)
92 return hash_int(mpid, 0);
96 cfm_is_valid_mpid(uint32_t mpid)
98 /* 802.1ag specification requires MPIDs to be within the range [1, 8191] */
99 return mpid >= 1 && mpid <= 8191;
102 static struct remote_mp *
103 lookup_remote_mp(const struct hmap *hmap, uint16_t mpid)
105 struct remote_mp *rmp;
107 HMAP_FOR_EACH_IN_BUCKET (rmp, node, hash_mpid(mpid), hmap) {
108 if (rmp->mpid == mpid) {
116 static struct remote_maid *
117 lookup_remote_maid(const struct hmap *hmap, uint8_t maid[CCM_MAID_LEN])
120 struct remote_maid *rmaid;
122 hash = hash_bytes(maid, CCM_MAID_LEN, 0);
123 HMAP_FOR_EACH_WITH_HASH (rmaid, node, hash, hmap) {
124 if (memcmp(rmaid->maid, maid, CCM_MAID_LEN) == 0) {
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);
144 hmap_init(&cfmi->x_remote_mps);
145 hmap_init(&cfmi->x_remote_maids);
150 cfm_destroy(struct cfm *cfm)
152 struct cfm_internal *cfmi = cfm_to_internal(cfm);
153 struct remote_mp *rmp, *rmp_next;
154 struct remote_maid *rmaid, *rmaid_next;
160 HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &cfm->remote_mps) {
161 hmap_remove(&cfm->remote_mps, &rmp->node);
165 HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &cfmi->x_remote_mps) {
166 hmap_remove(&cfmi->x_remote_mps, &rmp->node);
170 HMAP_FOR_EACH_SAFE (rmaid, rmaid_next, node, &cfmi->x_remote_maids) {
171 hmap_remove(&cfmi->x_remote_maids, &rmaid->node);
175 hmap_destroy(&cfm->remote_mps);
176 hmap_destroy(&cfmi->x_remote_mps);
177 hmap_destroy(&cfmi->x_remote_maids);
181 /* Should be run periodically to update fault statistics messages. */
183 cfm_run(struct cfm *cfm)
185 long long now = time_msec();
186 struct cfm_internal *cfmi = cfm_to_internal(cfm);
188 /* According to the 802.1ag specification we should assume every other MP
189 * with the same MAID has the same transmission interval that we have. If
190 * an MP has a different interval, cfm_process_heartbeat will register it
191 * as a fault (likely due to a configuration error). Thus we can check all
192 * MPs at once making this quite a bit simpler.
194 * According to the specification we should check when (ccm_interval_ms *
195 * 3.5)ms have passed. We changed the multiplier to 4 to avoid messy
196 * floating point arithmetic and add a bit of wiggle room. */
197 if (now >= cfmi->fault_check + cfmi->ccm_interval_ms * 4) {
199 struct remote_mp *rmp, *rmp_next;
200 struct remote_maid *rmaid, *rmaid_next;
204 HMAP_FOR_EACH (rmp, node, &cfm->remote_mps) {
205 rmp->fault = rmp->fault || cfmi->fault_check > rmp->recv_time;
206 fault = rmp->fault || fault;
209 HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &cfmi->x_remote_mps) {
210 if (cfmi->fault_check > rmp->recv_time) {
211 hmap_remove(&cfmi->x_remote_mps, &rmp->node);
216 HMAP_FOR_EACH_SAFE (rmaid, rmaid_next, node, &cfmi->x_remote_maids) {
217 if (cfmi->fault_check > rmaid->recv_time) {
218 hmap_remove(&cfmi->x_remote_maids, &rmaid->node);
223 fault = (fault || !hmap_is_empty(&cfmi->x_remote_mps)
224 || !hmap_is_empty(&cfmi->x_remote_maids));
227 cfmi->fault_check = now;
231 /* Should be run periodically to check if the CFM module has a CCM message it
234 cfm_should_send_ccm(struct cfm *cfm)
236 struct cfm_internal *cfmi = cfm_to_internal(cfm);
238 return time_msec() >= cfmi->ccm_sent + cfmi->ccm_interval_ms;
241 /* Composes a CCM message into 'ccm'. Messages generated with this function
242 * should be sent whenever cfm_should_send_ccm() indicates. */
244 cfm_compose_ccm(struct cfm *cfm, struct ccm *ccm)
246 struct cfm_internal *cfmi = cfm_to_internal(cfm);
248 cfmi->ccm_sent = time_msec();
250 ccm->mdlevel_version = 0;
251 ccm->opcode = CCM_OPCODE;
252 ccm->tlv_offset = 70;
253 ccm->seq = htonl(++cfmi->seq);
254 ccm->mpid = htons(cfmi->cfm.mpid);
255 ccm->flags = cfmi->ccm_interval;
256 memcpy(ccm->maid, cfmi->cfm.maid, sizeof ccm->maid);
260 cfm_wait(struct cfm *cfm)
263 struct cfm_internal *cfmi = cfm_to_internal(cfm);
265 wait = MIN(cfmi->ccm_sent + cfmi->ccm_interval_ms,
266 cfmi->fault_check + cfmi->ccm_interval_ms * 4);
267 poll_timer_wait_until(wait);
270 /* Should be called whenever a client of the cfm library changes the internals
271 * of 'cfm'. Returns true if 'cfm' is valid. */
273 cfm_configure(struct cfm *cfm)
275 struct cfm_internal *cfmi;
277 if (!cfm_is_valid_mpid(cfm->mpid) || !cfm->interval) {
281 cfmi = cfm_to_internal(cfm);
282 cfmi->ccm_interval = ms_to_ccm_interval(cfm->interval);
283 cfmi->ccm_interval_ms = ccm_interval_to_ms(cfmi->ccm_interval);
285 /* Force a resend and check in case anything changed. */
287 cfmi->fault_check = 0;
291 /* Given an array of MPIDs, updates the 'remote_mps' map of 'cfm' to reflect
292 * it. Invalid MPIDs are skipped. */
294 cfm_update_remote_mps(struct cfm *cfm, const uint16_t *mpids, size_t n_mpids)
296 struct cfm_internal *cfmi = cfm_to_internal(cfm);
298 struct hmap new_rmps;
299 struct remote_mp *rmp, *rmp_next;
301 hmap_init(&new_rmps);
303 for (i = 0; i < n_mpids; i++) {
304 uint16_t mpid = mpids[i];
306 if (!cfm_is_valid_mpid(mpid)
307 || lookup_remote_mp(&new_rmps, mpid)) {
311 if ((rmp = lookup_remote_mp(&cfm->remote_mps, mpid))) {
312 hmap_remove(&cfm->remote_mps, &rmp->node);
313 } else if ((rmp = lookup_remote_mp(&cfmi->x_remote_mps, mpid))) {
314 hmap_remove(&cfmi->x_remote_mps, &rmp->node);
316 rmp = xzalloc(sizeof *rmp);
320 hmap_insert(&new_rmps, &rmp->node, hash_mpid(mpid));
323 hmap_swap(&new_rmps, &cfm->remote_mps);
325 HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &new_rmps) {
326 hmap_remove(&new_rmps, &rmp->node);
330 hmap_destroy(&new_rmps);
333 /* Finds a 'remote_mp' with 'mpid' in 'cfm'. If no such 'remote_mp' exists
335 const struct remote_mp *
336 cfm_get_remote_mp(const struct cfm *cfm, uint16_t mpid)
338 return lookup_remote_mp(&cfm->remote_mps, mpid);
341 /* Generates 'maid' from 'md_name' and 'ma_name'. A NULL parameter indicates
342 * the default should be used. Returns false if unsuccessful. */
344 cfm_generate_maid(const char *md_name, const char *ma_name,
345 uint8_t maid[CCM_MAID_LEN])
348 size_t md_len, ma_len;
358 memset(maid, 0, CCM_MAID_LEN);
360 md_len = strlen(md_name);
361 ma_len = strlen(ma_name);
363 if (!md_len || !ma_len || md_len + ma_len + 4 > CCM_MAID_LEN) {
367 maid[0] = 4; /* MD name string format. */
368 maid[1] = md_len; /* MD name size. */
369 memcpy(&maid[2], md_name, md_len); /* MD name. */
371 ma_p = maid + 2 + md_len;
372 ma_p[0] = 2; /* MA name string format. */
373 ma_p[1] = ma_len; /* MA name size. */
374 memcpy(&ma_p[2], ma_name, ma_len); /* MA name. */
378 /* Returns true if the CFM library should process packets from 'flow'. */
380 cfm_should_process_flow(const struct flow *flow)
382 return (ntohs(flow->dl_type) == ETH_TYPE_CFM
383 && eth_addr_equals(flow->dl_dst, eth_addr_ccm));
386 /* Updates internal statistics relevant to packet 'p'. Should be called on
387 * every packet whose flow returned true when passed to
388 * cfm_should_process_flow. */
390 cfm_process_heartbeat(struct cfm *cfm, const struct ofpbuf *p)
395 uint8_t ccm_interval;
396 struct remote_mp *rmp;
398 struct cfm_internal *cfmi = cfm_to_internal(cfm);
399 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
401 ccm = ofpbuf_at(p, (uint8_t *)p->l3 - (uint8_t *)p->data, CCM_LEN);
404 VLOG_INFO_RL(&rl, "Received an un-parseable 802.1ag CCM heartbeat.");
408 if (ccm->opcode != CCM_OPCODE) {
409 VLOG_INFO_RL(&rl, "Received an unsupported 802.1ag message. "
410 "(opcode %u)", ccm->opcode);
414 if (memcmp(ccm->maid, cfm->maid, sizeof ccm->maid)) {
415 struct remote_maid *rmaid;
417 rmaid = lookup_remote_maid(&cfmi->x_remote_maids, ccm->maid);
419 rmaid->recv_time = time_msec();
421 rmaid = xzalloc(sizeof *rmaid);
422 rmaid->recv_time = time_msec();
423 memcpy(rmaid->maid, ccm->maid, sizeof rmaid->maid);
424 hmap_insert(&cfmi->x_remote_maids, &rmaid->node,
425 hash_bytes(ccm->maid, CCM_MAID_LEN, 0));
429 ccm_mpid = ntohs(ccm->mpid);
430 ccm_seq = ntohl(ccm->seq);
431 ccm_interval = ccm->flags & 0x7;
433 rmp = lookup_remote_mp(&cfm->remote_mps, ccm_mpid);
436 rmp = lookup_remote_mp(&cfmi->x_remote_mps, ccm_mpid);
440 rmp = xzalloc(sizeof *rmp);
441 rmp->mpid = ccm_mpid;
442 hmap_insert(&cfmi->x_remote_mps, &rmp->node, hash_mpid(ccm_mpid));
446 rmp->recv_time = time_msec();
447 rmp->fault = ccm_interval != cfmi->ccm_interval;