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. */
49 ccm_interval_to_ms(uint8_t interval)
52 case 0: NOT_REACHED(); /* Explicitly not supported by 802.1ag. */
53 case 1: return 3; /* Not recommended due to timer resolution. */
54 case 2: return 10; /* Not recommended due to timer resolution. */
59 case 7: return 600000;
60 default: NOT_REACHED(); /* Explicitly not supported by 802.1ag. */
67 ms_to_ccm_interval(int interval_ms)
71 for (i = 7; i > 0; i--) {
72 if (ccm_interval_to_ms(i) <= interval_ms) {
80 static struct cfm_internal *
81 cfm_to_internal(struct cfm *cfm)
83 return CONTAINER_OF(cfm, struct cfm_internal, cfm);
87 hash_mpid(uint8_t mpid)
89 return hash_int(mpid, 0);
93 cfm_is_valid_mpid(uint32_t mpid)
95 /* 802.1ag specification requires MPIDs to be within the range [1, 8191] */
96 return mpid >= 1 && mpid <= 8191;
99 static struct remote_mp *
100 lookup_remote_mp(const struct hmap *hmap, uint16_t mpid)
102 struct remote_mp *rmp;
104 HMAP_FOR_EACH_IN_BUCKET (rmp, node, hash_mpid(mpid), hmap) {
105 if (rmp->mpid == mpid) {
113 /* Allocates a 'cfm' object. This object should have its 'mpid', 'maid',
114 * 'eth_src', and 'interval' filled out. When changes are made to the 'cfm'
115 * object, cfm_configure should be called before using it. */
120 struct cfm_internal *cfmi;
122 cfmi = xzalloc(sizeof *cfmi);
125 hmap_init(&cfm->remote_mps);
126 hmap_init(&cfm->x_remote_mps);
127 hmap_init(&cfm->x_remote_maids);
132 cfm_destroy(struct cfm *cfm)
134 struct remote_mp *rmp, *rmp_next;
135 struct remote_maid *rmaid, *rmaid_next;
141 HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &cfm->remote_mps) {
142 hmap_remove(&cfm->remote_mps, &rmp->node);
146 HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &cfm->x_remote_mps) {
147 hmap_remove(&cfm->x_remote_mps, &rmp->node);
151 HMAP_FOR_EACH_SAFE (rmaid, rmaid_next, node, &cfm->x_remote_maids) {
152 hmap_remove(&cfm->x_remote_maids, &rmaid->node);
156 hmap_destroy(&cfm->remote_mps);
157 hmap_destroy(&cfm->x_remote_mps);
158 hmap_destroy(&cfm->x_remote_maids);
159 free(cfm_to_internal(cfm));
162 /* Should be run periodically to update fault statistics messages. */
164 cfm_run(struct cfm *cfm)
166 long long now = time_msec();
167 struct cfm_internal *cfmi = cfm_to_internal(cfm);
169 /* According to the 802.1ag specification we should assume every other MP
170 * with the same MAID has the same transmission interval that we have. If
171 * an MP has a different interval, cfm_process_heartbeat will register it
172 * as a fault (likely due to a configuration error). Thus we can check all
173 * MPs at once making this quite a bit simpler.
175 * According to the specification we should check when (ccm_interval_ms *
176 * 3.5)ms have passed. We changed the multiplier to 4 to avoid messy
177 * floating point arithmetic and add a bit of wiggle room. */
178 if (now >= cfmi->fault_check + cfmi->ccm_interval_ms * 4) {
180 struct remote_mp *rmp, *rmp_next;
181 struct remote_maid *rmaid, *rmaid_next;
185 HMAP_FOR_EACH (rmp, node, &cfm->remote_mps) {
186 rmp->fault = rmp->fault || cfmi->fault_check > rmp->recv_time;
187 fault = rmp->fault || fault;
190 HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &cfm->x_remote_mps) {
191 if (cfmi->fault_check > rmp->recv_time) {
192 hmap_remove(&cfm->x_remote_mps, &rmp->node);
197 HMAP_FOR_EACH_SAFE (rmaid, rmaid_next, node, &cfm->x_remote_maids) {
198 if (cfmi->fault_check > rmaid->recv_time) {
199 hmap_remove(&cfm->x_remote_maids, &rmaid->node);
204 fault = (fault || !hmap_is_empty(&cfm->x_remote_mps)
205 || !hmap_is_empty(&cfm->x_remote_maids));
208 cfmi->fault_check = now;
212 /* Should be run periodically to check if the CFM module has a CCM message it
215 cfm_should_send_ccm(struct cfm *cfm)
217 struct cfm_internal *cfmi = cfm_to_internal(cfm);
219 return time_msec() >= cfmi->ccm_sent + cfmi->ccm_interval_ms;
222 /* Composes a CCM message into 'ccm'. Messages generated with this function
223 * should be sent whenever cfm_should_send_ccm() indicates. */
225 cfm_compose_ccm(struct cfm *cfm, struct ccm *ccm)
227 struct cfm_internal *cfmi = cfm_to_internal(cfm);
229 cfmi->ccm_sent = time_msec();
231 ccm->mdlevel_version = 0;
232 ccm->opcode = CCM_OPCODE;
233 ccm->tlv_offset = 70;
234 ccm->seq = htonl(++cfmi->seq);
235 ccm->mpid = htons(cfmi->cfm.mpid);
236 ccm->flags = cfmi->ccm_interval;
237 memcpy(ccm->maid, cfmi->cfm.maid, sizeof ccm->maid);
241 cfm_wait(struct cfm *cfm)
244 struct cfm_internal *cfmi = cfm_to_internal(cfm);
246 wait = MIN(cfmi->ccm_sent + cfmi->ccm_interval_ms,
247 cfmi->fault_check + cfmi->ccm_interval_ms * 4);
248 poll_timer_wait_until(wait);
251 /* Should be called whenever a client of the cfm library changes the internals
252 * of 'cfm'. Returns true if 'cfm' is valid. */
254 cfm_configure(struct cfm *cfm)
256 struct cfm_internal *cfmi;
258 if (!cfm_is_valid_mpid(cfm->mpid) || !cfm->interval) {
262 cfmi = cfm_to_internal(cfm);
263 cfmi->ccm_interval = ms_to_ccm_interval(cfm->interval);
264 cfmi->ccm_interval_ms = ccm_interval_to_ms(cfmi->ccm_interval);
266 /* Force a resend and check in case anything changed. */
268 cfmi->fault_check = 0;
272 /* Given an array of MPIDs, updates the 'remote_mps' map of 'cfm' to reflect
273 * it. Invalid MPIDs are skipped. */
275 cfm_update_remote_mps(struct cfm *cfm, const uint16_t *mpids, size_t n_mpids)
278 struct hmap new_rmps;
279 struct remote_mp *rmp, *rmp_next;
281 hmap_init(&new_rmps);
283 for (i = 0; i < n_mpids; i++) {
284 uint16_t mpid = mpids[i];
286 if (!cfm_is_valid_mpid(mpid)
287 || lookup_remote_mp(&new_rmps, mpid)) {
291 if ((rmp = lookup_remote_mp(&cfm->remote_mps, mpid))) {
292 hmap_remove(&cfm->remote_mps, &rmp->node);
293 } else if ((rmp = lookup_remote_mp(&cfm->x_remote_mps, mpid))) {
294 hmap_remove(&cfm->x_remote_mps, &rmp->node);
296 rmp = xzalloc(sizeof *rmp);
300 hmap_insert(&new_rmps, &rmp->node, hash_mpid(mpid));
303 hmap_swap(&new_rmps, &cfm->remote_mps);
305 HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &new_rmps) {
306 hmap_remove(&new_rmps, &rmp->node);
310 hmap_destroy(&new_rmps);
313 /* Finds a 'remote_mp' with 'mpid' in 'cfm'. If no such 'remote_mp' exists
315 const struct remote_mp *
316 cfm_get_remote_mp(const struct cfm *cfm, uint16_t mpid)
318 return lookup_remote_mp(&cfm->remote_mps, mpid);
321 /* Generates 'maid' from 'md_name' and 'ma_name'. A NULL parameter indicates
322 * the default should be used. Returns false if unsuccessful. */
324 cfm_generate_maid(const char *md_name, const char *ma_name,
325 uint8_t maid[CCM_MAID_LEN])
328 size_t md_len, ma_len;
338 memset(maid, 0, CCM_MAID_LEN);
340 md_len = strlen(md_name);
341 ma_len = strlen(ma_name);
343 if (!md_len || !ma_len || md_len + ma_len + 4 > CCM_MAID_LEN) {
347 maid[0] = 4; /* MD name string format. */
348 maid[1] = md_len; /* MD name size. */
349 memcpy(&maid[2], md_name, md_len); /* MD name. */
351 ma_p = maid + 2 + md_len;
352 ma_p[0] = 2; /* MA name string format. */
353 ma_p[1] = ma_len; /* MA name size. */
354 memcpy(&ma_p[2], ma_name, ma_len); /* MA name. */
358 /* Returns true if the CFM library should process packets from 'flow'. */
360 cfm_should_process_flow(const struct flow *flow)
362 return (ntohs(flow->dl_type) == ETH_TYPE_CFM
363 && eth_addr_equals(flow->dl_dst, eth_addr_ccm));
366 /* Updates internal statistics relevant to packet 'p'. Should be called on
367 * every packet whose flow returned true when passed to
368 * cfm_should_process_flow. */
370 cfm_process_heartbeat(struct cfm *cfm, const struct ofpbuf *p)
375 uint8_t ccm_interval;
376 struct remote_mp *rmp;
378 struct cfm_internal *cfmi = cfm_to_internal(cfm);
379 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
381 ccm = ofpbuf_at(p, (uint8_t *)p->l3 - (uint8_t *)p->data, CCM_LEN);
384 VLOG_INFO_RL(&rl, "Received an un-parseable 802.1ag CCM heartbeat.");
388 if (ccm->opcode != CCM_OPCODE) {
389 VLOG_INFO_RL(&rl, "Received an unsupported 802.1ag message. "
390 "(opcode %u)", ccm->opcode);
394 if (memcmp(ccm->maid, cfm->maid, sizeof ccm->maid)) {
396 struct remote_maid *rmaid;
398 hash = hash_bytes(ccm->maid, sizeof ccm->maid, 0);
400 HMAP_FOR_EACH_IN_BUCKET (rmaid, node, hash, &cfm->x_remote_maids) {
401 if (memcmp(rmaid->maid, ccm->maid, sizeof rmaid->maid) == 0) {
402 rmaid->recv_time = time_msec();
407 rmaid = xzalloc(sizeof *rmaid);
408 rmaid->recv_time = time_msec();
409 memcpy(rmaid->maid, ccm->maid, sizeof rmaid->maid);
410 hmap_insert(&cfm->x_remote_maids, &rmaid->node, hash);
414 ccm_mpid = ntohs(ccm->mpid);
415 ccm_seq = ntohl(ccm->seq);
416 ccm_interval = ccm->flags & 0x7;
418 rmp = lookup_remote_mp(&cfm->remote_mps, ccm_mpid);
421 rmp = lookup_remote_mp(&cfm->x_remote_mps, ccm_mpid);
425 rmp = xzalloc(sizeof *rmp);
426 rmp->mpid = ccm_mpid;
427 hmap_insert(&cfm->x_remote_mps, &rmp->node, hash_mpid(ccm_mpid));
431 rmp->recv_time = time_msec();
432 rmp->fault = ccm_interval != cfmi->ccm_interval;