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 static struct ofpbuf *
114 compose_ccm(struct cfm_internal *cfmi)
117 struct ofpbuf *packet;
118 struct eth_header *eth;
120 packet = ofpbuf_new(ETH_HEADER_LEN + CCM_LEN + 2);
122 ofpbuf_reserve(packet, 2);
124 eth = ofpbuf_put_zeros(packet, ETH_HEADER_LEN);
125 ccm = ofpbuf_put_zeros(packet, CCM_LEN);
127 memcpy(eth->eth_dst, eth_addr_ccm, ETH_ADDR_LEN);
128 memcpy(eth->eth_src, cfmi->cfm.eth_src, sizeof eth->eth_src);
129 eth->eth_type = htons(ETH_TYPE_CFM);
131 ccm->mdlevel_version = 0;
132 ccm->opcode = CCM_OPCODE;
133 ccm->tlv_offset = 70;
134 ccm->seq = htonl(++cfmi->seq);
135 ccm->mpid = htons(cfmi->cfm.mpid);
136 ccm->flags = cfmi->ccm_interval;
137 memcpy(ccm->maid, cfmi->cfm.maid, sizeof ccm->maid);
141 /* Allocates a 'cfm' object. This object should have its 'mpid', 'maid',
142 * 'eth_src', and 'interval' filled out. When changes are made to the 'cfm'
143 * object, cfm_configure should be called before using it. */
148 struct cfm_internal *cfmi;
150 cfmi = xzalloc(sizeof *cfmi);
153 hmap_init(&cfm->remote_mps);
154 hmap_init(&cfm->x_remote_mps);
155 hmap_init(&cfm->x_remote_maids);
160 cfm_destroy(struct cfm *cfm)
162 struct remote_mp *rmp, *rmp_next;
163 struct remote_maid *rmaid, *rmaid_next;
169 HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &cfm->remote_mps) {
170 hmap_remove(&cfm->remote_mps, &rmp->node);
174 HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &cfm->x_remote_mps) {
175 hmap_remove(&cfm->x_remote_mps, &rmp->node);
179 HMAP_FOR_EACH_SAFE (rmaid, rmaid_next, node, &cfm->x_remote_maids) {
180 hmap_remove(&cfm->x_remote_maids, &rmaid->node);
184 hmap_destroy(&cfm->remote_mps);
185 hmap_destroy(&cfm->x_remote_mps);
186 hmap_destroy(&cfm->x_remote_maids);
187 free(cfm_to_internal(cfm));
190 /* Should be run periodically to update fault statistics and generate CCM
191 * messages. If necessary, returns a packet which the caller is responsible
192 * for sending, un-initing, and deallocating. Otherwise returns NULL. */
194 cfm_run(struct cfm *cfm)
196 long long now = time_msec();
197 struct cfm_internal *cfmi = cfm_to_internal(cfm);
199 /* According to the 802.1ag specification we should assume every other MP
200 * with the same MAID has the same transmission interval that we have. If
201 * an MP has a different interval, cfm_process_heartbeat will register it
202 * as a fault (likely due to a configuration error). Thus we can check all
203 * MPs at once making this quite a bit simpler.
205 * According to the specification we should check when (ccm_interval_ms *
206 * 3.5)ms have passed. We changed the multiplier to 4 to avoid messy
207 * floating point arithmetic and add a bit of wiggle room. */
208 if (now >= cfmi->fault_check + cfmi->ccm_interval_ms * 4) {
210 struct remote_mp *rmp, *rmp_next;
211 struct remote_maid *rmaid, *rmaid_next;
215 HMAP_FOR_EACH (rmp, node, &cfm->remote_mps) {
216 rmp->fault = rmp->fault || cfmi->fault_check > rmp->recv_time;
217 fault = rmp->fault || fault;
220 HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &cfm->x_remote_mps) {
221 if (cfmi->fault_check > rmp->recv_time) {
222 hmap_remove(&cfm->x_remote_mps, &rmp->node);
227 HMAP_FOR_EACH_SAFE (rmaid, rmaid_next, node, &cfm->x_remote_maids) {
228 if (cfmi->fault_check > rmaid->recv_time) {
229 hmap_remove(&cfm->x_remote_maids, &rmaid->node);
234 fault = (fault || !hmap_is_empty(&cfm->x_remote_mps)
235 || !hmap_is_empty(&cfm->x_remote_maids));
238 cfmi->fault_check = now;
241 if (now >= cfmi->ccm_sent + cfmi->ccm_interval_ms) {
242 cfmi->ccm_sent = now;
243 return compose_ccm(cfmi);
250 cfm_wait(struct cfm *cfm)
253 struct cfm_internal *cfmi = cfm_to_internal(cfm);
255 wait = MIN(cfmi->ccm_sent + cfmi->ccm_interval_ms,
256 cfmi->fault_check + cfmi->ccm_interval_ms * 4);
257 poll_timer_wait_until(wait);
260 /* Should be called whenever a client of the cfm library changes the internals
261 * of 'cfm'. Returns true if 'cfm' is valid. */
263 cfm_configure(struct cfm *cfm)
265 struct cfm_internal *cfmi;
267 if (!cfm_is_valid_mpid(cfm->mpid) || !cfm->interval) {
271 cfmi = cfm_to_internal(cfm);
272 cfmi->ccm_interval = ms_to_ccm_interval(cfm->interval);
273 cfmi->ccm_interval_ms = ccm_interval_to_ms(cfmi->ccm_interval);
275 /* Force a resend and check in case anything changed. */
277 cfmi->fault_check = 0;
281 /* Given an array of MPIDs, updates the 'remote_mps' map of 'cfm' to reflect
282 * it. Invalid MPIDs are skipped. */
284 cfm_update_remote_mps(struct cfm *cfm, const uint16_t *mpids, size_t n_mpids)
287 struct hmap new_rmps;
288 struct remote_mp *rmp, *rmp_next;
290 hmap_init(&new_rmps);
292 for (i = 0; i < n_mpids; i++) {
293 uint16_t mpid = mpids[i];
295 if (!cfm_is_valid_mpid(mpid)
296 || lookup_remote_mp(&new_rmps, mpid)) {
300 if ((rmp = lookup_remote_mp(&cfm->remote_mps, mpid))) {
301 hmap_remove(&cfm->remote_mps, &rmp->node);
302 } else if ((rmp = lookup_remote_mp(&cfm->x_remote_mps, mpid))) {
303 hmap_remove(&cfm->x_remote_mps, &rmp->node);
305 rmp = xzalloc(sizeof *rmp);
309 hmap_insert(&new_rmps, &rmp->node, hash_mpid(mpid));
312 hmap_swap(&new_rmps, &cfm->remote_mps);
314 HMAP_FOR_EACH_SAFE (rmp, rmp_next, node, &new_rmps) {
315 hmap_remove(&new_rmps, &rmp->node);
319 hmap_destroy(&new_rmps);
322 /* Finds a 'remote_mp' with 'mpid' in 'cfm'. If no such 'remote_mp' exists
324 const struct remote_mp *
325 cfm_get_remote_mp(const struct cfm *cfm, uint16_t mpid)
327 return lookup_remote_mp(&cfm->remote_mps, mpid);
330 /* Generates 'maid' from 'md_name' and 'ma_name'. A NULL parameter indicates
331 * the default should be used. Returns false if unsuccessful. */
333 cfm_generate_maid(const char *md_name, const char *ma_name,
334 uint8_t maid[CCM_MAID_LEN])
337 size_t md_len, ma_len;
347 memset(maid, 0, CCM_MAID_LEN);
349 md_len = strlen(md_name);
350 ma_len = strlen(ma_name);
352 if (!md_len || !ma_len || md_len + ma_len + 4 > CCM_MAID_LEN) {
356 maid[0] = 4; /* MD name string format. */
357 maid[1] = md_len; /* MD name size. */
358 memcpy(&maid[2], md_name, md_len); /* MD name. */
360 ma_p = maid + 2 + md_len;
361 ma_p[0] = 2; /* MA name string format. */
362 ma_p[1] = ma_len; /* MA name size. */
363 memcpy(&ma_p[2], ma_name, ma_len); /* MA name. */
367 /* Returns true if the CFM library should process packets from 'flow'. */
369 cfm_should_process_flow(const struct flow *flow)
371 return (ntohs(flow->dl_type) == ETH_TYPE_CFM
372 && eth_addr_equals(flow->dl_dst, eth_addr_ccm));
375 /* Updates internal statistics relevant to packet 'p'. Should be called on
376 * every packet whose flow returned true when passed to
377 * cfm_should_process_flow. */
379 cfm_process_heartbeat(struct cfm *cfm, const struct ofpbuf *p)
384 uint8_t ccm_interval;
385 struct remote_mp *rmp;
387 struct cfm_internal *cfmi = cfm_to_internal(cfm);
388 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
390 ccm = ofpbuf_at(p, (uint8_t *)p->l3 - (uint8_t *)p->data, CCM_LEN);
393 VLOG_INFO_RL(&rl, "Received an un-parseable 802.1ag CCM heartbeat.");
397 if (ccm->opcode != CCM_OPCODE) {
398 VLOG_INFO_RL(&rl, "Received an unsupported 802.1ag message. "
399 "(opcode %u)", ccm->opcode);
403 if (memcmp(ccm->maid, cfm->maid, sizeof ccm->maid)) {
405 struct remote_maid *rmaid;
407 hash = hash_bytes(ccm->maid, sizeof ccm->maid, 0);
409 HMAP_FOR_EACH_IN_BUCKET (rmaid, node, hash, &cfm->x_remote_maids) {
410 if (memcmp(rmaid->maid, ccm->maid, sizeof rmaid->maid) == 0) {
411 rmaid->recv_time = time_msec();
416 rmaid = xzalloc(sizeof *rmaid);
417 rmaid->recv_time = time_msec();
418 memcpy(rmaid->maid, ccm->maid, sizeof rmaid->maid);
419 hmap_insert(&cfm->x_remote_maids, &rmaid->node, hash);
423 ccm_mpid = ntohs(ccm->mpid);
424 ccm_seq = ntohl(ccm->seq);
425 ccm_interval = ccm->flags & 0x7;
427 rmp = lookup_remote_mp(&cfm->remote_mps, ccm_mpid);
430 rmp = lookup_remote_mp(&cfm->x_remote_mps, ccm_mpid);
434 rmp = xzalloc(sizeof *rmp);
435 rmp->mpid = ccm_mpid;
436 hmap_insert(&cfm->x_remote_mps, &rmp->node, hash_mpid(ccm_mpid));
440 rmp->recv_time = time_msec();
441 rmp->fault = ccm_interval != cfmi->ccm_interval;