2 * Copyright (c) 2008, 2009, 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.
18 #include "mac-learning.h"
28 #include "poll-loop.h"
32 #include "vlan-bitmap.h"
35 VLOG_DEFINE_THIS_MODULE(mac_learning);
37 COVERAGE_DEFINE(mac_learning_learned);
38 COVERAGE_DEFINE(mac_learning_expired);
40 /* Returns the number of seconds since 'e' was last learned. */
42 mac_entry_age(const struct mac_entry *e)
44 time_t remaining = e->expires - time_now();
45 return MAC_ENTRY_IDLE_TIME - remaining;
49 mac_table_hash(const struct mac_learning *ml, const uint8_t mac[ETH_ADDR_LEN],
52 return hash_bytes(mac, ETH_ADDR_LEN, vlan ^ ml->secret);
55 static struct mac_entry *
56 mac_entry_from_lru_node(struct list *list)
58 return CONTAINER_OF(list, struct mac_entry, lru_node);
61 /* Returns a tag that represents that 'mac' is on an unknown port in 'vlan'.
62 * (When we learn where 'mac' is in 'vlan', this allows flows that were
63 * flooded to be revalidated.) */
65 make_unknown_mac_tag(const struct mac_learning *ml,
66 const uint8_t mac[ETH_ADDR_LEN], uint16_t vlan)
68 return tag_create_deterministic(mac_table_hash(ml, mac, vlan));
71 static struct mac_entry *
72 mac_entry_lookup(const struct mac_learning *ml,
73 const uint8_t mac[ETH_ADDR_LEN], uint16_t vlan)
77 HMAP_FOR_EACH_WITH_HASH (e, hmap_node, mac_table_hash(ml, mac, vlan),
79 if (e->vlan == vlan && eth_addr_equals(e->mac, mac)) {
86 /* If the LRU list is not empty, stores the least-recently-used entry in '*e'
87 * and returns true. Otherwise, if the LRU list is empty, stores NULL in '*e'
88 * and return false. */
90 get_lru(struct mac_learning *ml, struct mac_entry **e)
92 if (!list_is_empty(&ml->lrus)) {
93 *e = mac_entry_from_lru_node(ml->lrus.next);
101 /* Creates and returns a new MAC learning table. */
102 struct mac_learning *
103 mac_learning_create(void)
105 struct mac_learning *ml;
108 ml = xmalloc(sizeof *ml);
109 list_init(&ml->lrus);
110 list_init(&ml->free);
111 hmap_init(&ml->table);
112 for (i = 0; i < MAC_MAX; i++) {
113 struct mac_entry *s = &ml->entries[i];
114 list_push_front(&ml->free, &s->lru_node);
116 ml->secret = random_uint32();
117 ml->flood_vlans = NULL;
121 /* Destroys MAC learning table 'ml'. */
123 mac_learning_destroy(struct mac_learning *ml)
126 hmap_destroy(&ml->table);
127 bitmap_free(ml->flood_vlans);
132 /* Provides a bitmap of VLANs which have learning disabled, that is, VLANs on
133 * which all packets are flooded. Returns true if the set has changed from the
136 mac_learning_set_flood_vlans(struct mac_learning *ml,
137 const unsigned long *bitmap)
139 if (vlan_bitmap_equal(ml->flood_vlans, bitmap)) {
142 bitmap_free(ml->flood_vlans);
143 ml->flood_vlans = vlan_bitmap_clone(bitmap);
149 is_learning_vlan(const struct mac_learning *ml, uint16_t vlan)
151 return vlan_bitmap_contains(ml->flood_vlans, vlan);
154 /* Returns true if 'src_mac' may be learned on 'vlan' for 'ml'.
155 * Returns false if 'ml' is NULL, if src_mac is not valid for learning, or if
156 * 'vlan' is configured on 'ml' to flood all packets. */
158 mac_learning_may_learn(const struct mac_learning *ml,
159 const uint8_t src_mac[ETH_ADDR_LEN], uint16_t vlan)
161 return ml && is_learning_vlan(ml, vlan) && !eth_addr_is_multicast(src_mac);
164 /* Searches 'ml' for and returns a MAC learning entry for 'src_mac' in 'vlan',
165 * inserting a new entry if necessary. The caller must have already verified,
166 * by calling mac_learning_may_learn(), that 'src_mac' and 'vlan' are
169 * If the returned MAC entry is new (as may be determined by calling
170 * mac_entry_is_new()), then the caller must pass the new entry to
171 * mac_learning_changed(). The caller must also initialize the new entry's
172 * 'port' member. Otherwise calling those functions is at the caller's
175 mac_learning_insert(struct mac_learning *ml,
176 const uint8_t src_mac[ETH_ADDR_LEN], uint16_t vlan)
180 e = mac_entry_lookup(ml, src_mac, vlan);
182 uint32_t hash = mac_table_hash(ml, src_mac, vlan);
184 if (!list_is_empty(&ml->free)) {
185 e = mac_entry_from_lru_node(ml->free.next);
187 e = mac_entry_from_lru_node(ml->lrus.next);
188 hmap_remove(&ml->table, &e->hmap_node);
191 hmap_insert(&ml->table, &e->hmap_node, hash);
192 memcpy(e->mac, src_mac, ETH_ADDR_LEN);
195 e->grat_arp_lock = TIME_MIN;
198 /* Mark 'e' as recently used. */
199 list_remove(&e->lru_node);
200 list_push_back(&ml->lrus, &e->lru_node);
201 e->expires = time_now() + MAC_ENTRY_IDLE_TIME;
206 /* Changes 'e''s tag to a new, randomly selected one, and returns the tag that
207 * would have been previously used for this entry's MAC and VLAN (either before
208 * 'e' was inserted, if it is new, or otherwise before its port was updated.)
210 * The client should call this function after obtaining a MAC learning entry
211 * from mac_learning_insert(), if the entry is either new or if its learned
212 * port has changed. */
214 mac_learning_changed(struct mac_learning *ml, struct mac_entry *e)
216 tag_type old_tag = e->tag;
218 COVERAGE_INC(mac_learning_learned);
220 e->tag = tag_create_random();
221 return old_tag ? old_tag : make_unknown_mac_tag(ml, e->mac, e->vlan);
224 /* Looks up MAC 'dst' for VLAN 'vlan' in 'ml' and returns the associated MAC
225 * learning entry, if any. If 'tag' is nonnull, then the tag that associates
226 * 'dst' and 'vlan' with its currently learned port will be OR'd into
229 mac_learning_lookup(const struct mac_learning *ml,
230 const uint8_t dst[ETH_ADDR_LEN], uint16_t vlan,
233 if (eth_addr_is_multicast(dst)) {
234 /* No tag because the treatment of multicast destinations never
237 } else if (!is_learning_vlan(ml, vlan)) {
238 /* We don't tag this property. The set of learning VLANs changes so
239 * rarely that we revalidate every flow when it changes. */
242 struct mac_entry *e = mac_entry_lookup(ml, dst, vlan);
244 assert(e == NULL || e->tag != 0);
246 /* Tag either the learned port or the lack thereof. */
247 *tag |= e ? e->tag : make_unknown_mac_tag(ml, dst, vlan);
253 /* Expires 'e' from the 'ml' hash table. 'e' must not already be on the free
256 mac_learning_expire(struct mac_learning *ml, struct mac_entry *e)
258 hmap_remove(&ml->table, &e->hmap_node);
259 list_remove(&e->lru_node);
260 list_push_front(&ml->free, &e->lru_node);
263 /* Expires all the mac-learning entries in 'ml'. The tags in 'ml' are
264 * discarded, so the client is responsible for revalidating any flows that
265 * depend on 'ml', if necessary. */
267 mac_learning_flush(struct mac_learning *ml)
270 while (get_lru(ml, &e)){
271 mac_learning_expire(ml, e);
276 mac_learning_run(struct mac_learning *ml, struct tag_set *set)
279 while (get_lru(ml, &e) && time_now() >= e->expires) {
280 COVERAGE_INC(mac_learning_expired);
282 tag_set_add(set, e->tag);
284 mac_learning_expire(ml, e);
289 mac_learning_wait(struct mac_learning *ml)
291 if (!list_is_empty(&ml->lrus)) {
292 struct mac_entry *e = mac_entry_from_lru_node(ml->lrus.next);
293 poll_timer_wait_until(e->expires * 1000LL);