2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
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"
27 #include "poll-loop.h"
30 #include "unaligned.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' (within 'ml') was last learned. */
42 mac_entry_age(const struct mac_learning *ml, const struct mac_entry *e)
44 time_t remaining = e->expires - time_now();
45 return ml->idle_time - remaining;
49 mac_table_hash(const struct mac_learning *ml, const uint8_t mac[ETH_ADDR_LEN],
52 unsigned int mac1 = get_unaligned_u32((uint32_t *) mac);
53 unsigned int mac2 = get_unaligned_u16((uint16_t *) (mac + 4));
54 return hash_3words(mac1, mac2 | (vlan << 16), ml->secret);
57 static struct mac_entry *
58 mac_entry_from_lru_node(struct list *list)
60 return CONTAINER_OF(list, struct mac_entry, lru_node);
63 /* Returns a tag that represents that 'mac' is on an unknown port in 'vlan'.
64 * (When we learn where 'mac' is in 'vlan', this allows flows that were
65 * flooded to be revalidated.) */
67 make_unknown_mac_tag(const struct mac_learning *ml,
68 const uint8_t mac[ETH_ADDR_LEN], uint16_t vlan)
70 return tag_create_deterministic(mac_table_hash(ml, mac, vlan));
73 static struct mac_entry *
74 mac_entry_lookup(const struct mac_learning *ml,
75 const uint8_t mac[ETH_ADDR_LEN], uint16_t vlan)
79 HMAP_FOR_EACH_WITH_HASH (e, hmap_node, mac_table_hash(ml, mac, vlan),
81 if (e->vlan == vlan && eth_addr_equals(e->mac, mac)) {
88 /* If the LRU list is not empty, stores the least-recently-used entry in '*e'
89 * and returns true. Otherwise, if the LRU list is empty, stores NULL in '*e'
90 * and return false. */
92 get_lru(struct mac_learning *ml, struct mac_entry **e)
94 if (!list_is_empty(&ml->lrus)) {
95 *e = mac_entry_from_lru_node(ml->lrus.next);
104 normalize_idle_time(unsigned int idle_time)
106 return (idle_time < 15 ? 15
107 : idle_time > 3600 ? 3600
111 /* Creates and returns a new MAC learning table with an initial MAC aging
112 * timeout of 'idle_time' seconds and an initial maximum of MAC_DEFAULT_MAX
114 struct mac_learning *
115 mac_learning_create(unsigned int idle_time)
117 struct mac_learning *ml;
119 ml = xmalloc(sizeof *ml);
120 list_init(&ml->lrus);
121 hmap_init(&ml->table);
122 ml->secret = random_uint32();
123 ml->flood_vlans = NULL;
124 ml->idle_time = normalize_idle_time(idle_time);
125 ml->max_entries = MAC_DEFAULT_MAX;
129 /* Destroys MAC learning table 'ml'. */
131 mac_learning_destroy(struct mac_learning *ml)
134 struct mac_entry *e, *next;
136 HMAP_FOR_EACH_SAFE (e, next, hmap_node, &ml->table) {
137 hmap_remove(&ml->table, &e->hmap_node);
140 hmap_destroy(&ml->table);
142 bitmap_free(ml->flood_vlans);
147 /* Provides a bitmap of VLANs which have learning disabled, that is, VLANs on
148 * which all packets are flooded. Returns true if the set has changed from the
151 mac_learning_set_flood_vlans(struct mac_learning *ml,
152 const unsigned long *bitmap)
154 if (vlan_bitmap_equal(ml->flood_vlans, bitmap)) {
157 bitmap_free(ml->flood_vlans);
158 ml->flood_vlans = vlan_bitmap_clone(bitmap);
163 /* Changes the MAC aging timeout of 'ml' to 'idle_time' seconds. */
165 mac_learning_set_idle_time(struct mac_learning *ml, unsigned int idle_time)
167 idle_time = normalize_idle_time(idle_time);
168 if (idle_time != ml->idle_time) {
172 delta = (int) idle_time - (int) ml->idle_time;
173 LIST_FOR_EACH (e, lru_node, &ml->lrus) {
176 ml->idle_time = idle_time;
180 /* Sets the maximum number of entries in 'ml' to 'max_entries', adjusting it
181 * to be within a reasonable range. */
183 mac_learning_set_max_entries(struct mac_learning *ml, size_t max_entries)
185 ml->max_entries = (max_entries < 10 ? 10
186 : max_entries > 1000 * 1000 ? 1000 * 1000
191 is_learning_vlan(const struct mac_learning *ml, uint16_t vlan)
193 return !ml->flood_vlans || !bitmap_is_set(ml->flood_vlans, vlan);
196 /* Returns true if 'src_mac' may be learned on 'vlan' for 'ml'.
197 * Returns false if 'ml' is NULL, if src_mac is not valid for learning, or if
198 * 'vlan' is configured on 'ml' to flood all packets. */
200 mac_learning_may_learn(const struct mac_learning *ml,
201 const uint8_t src_mac[ETH_ADDR_LEN], uint16_t vlan)
203 return ml && is_learning_vlan(ml, vlan) && !eth_addr_is_multicast(src_mac);
206 /* Searches 'ml' for and returns a MAC learning entry for 'src_mac' in 'vlan',
207 * inserting a new entry if necessary. The caller must have already verified,
208 * by calling mac_learning_may_learn(), that 'src_mac' and 'vlan' are
211 * If the returned MAC entry is new (as may be determined by calling
212 * mac_entry_is_new()), then the caller must pass the new entry to
213 * mac_learning_changed(). The caller must also initialize the new entry's
214 * 'port' member. Otherwise calling those functions is at the caller's
217 mac_learning_insert(struct mac_learning *ml,
218 const uint8_t src_mac[ETH_ADDR_LEN], uint16_t vlan)
222 e = mac_entry_lookup(ml, src_mac, vlan);
224 uint32_t hash = mac_table_hash(ml, src_mac, vlan);
226 if (hmap_count(&ml->table) >= ml->max_entries) {
228 mac_learning_expire(ml, e);
231 e = xmalloc(sizeof *e);
232 hmap_insert(&ml->table, &e->hmap_node, hash);
233 memcpy(e->mac, src_mac, ETH_ADDR_LEN);
236 e->grat_arp_lock = TIME_MIN;
238 list_remove(&e->lru_node);
241 /* Mark 'e' as recently used. */
242 list_push_back(&ml->lrus, &e->lru_node);
243 e->expires = time_now() + ml->idle_time;
248 /* Changes 'e''s tag to a new, randomly selected one, and returns the tag that
249 * would have been previously used for this entry's MAC and VLAN (either before
250 * 'e' was inserted, if it is new, or otherwise before its port was updated.)
252 * The client should call this function after obtaining a MAC learning entry
253 * from mac_learning_insert(), if the entry is either new or if its learned
254 * port has changed. */
256 mac_learning_changed(struct mac_learning *ml, struct mac_entry *e)
258 tag_type old_tag = e->tag;
260 COVERAGE_INC(mac_learning_learned);
262 e->tag = tag_create_random();
263 return old_tag ? old_tag : make_unknown_mac_tag(ml, e->mac, e->vlan);
266 /* Looks up MAC 'dst' for VLAN 'vlan' in 'ml' and returns the associated MAC
267 * learning entry, if any. If 'tag' is nonnull, then the tag that associates
268 * 'dst' and 'vlan' with its currently learned port will be OR'd into
271 mac_learning_lookup(const struct mac_learning *ml,
272 const uint8_t dst[ETH_ADDR_LEN], uint16_t vlan,
275 if (eth_addr_is_multicast(dst)) {
276 /* No tag because the treatment of multicast destinations never
279 } else if (!is_learning_vlan(ml, vlan)) {
280 /* We don't tag this property. The set of learning VLANs changes so
281 * rarely that we revalidate every flow when it changes. */
284 struct mac_entry *e = mac_entry_lookup(ml, dst, vlan);
286 ovs_assert(e == NULL || e->tag != 0);
288 /* Tag either the learned port or the lack thereof. */
289 *tag |= e ? e->tag : make_unknown_mac_tag(ml, dst, vlan);
295 /* Expires 'e' from the 'ml' hash table. */
297 mac_learning_expire(struct mac_learning *ml, struct mac_entry *e)
299 hmap_remove(&ml->table, &e->hmap_node);
300 list_remove(&e->lru_node);
304 /* Expires all the mac-learning entries in 'ml'. If not NULL, the tags in 'ml'
305 * are added to 'tags'. Otherwise the tags in 'ml' are discarded. The client
306 * is responsible for revalidating any flows that depend on 'ml', if
309 mac_learning_flush(struct mac_learning *ml, struct tag_set *tags)
312 while (get_lru(ml, &e)){
314 tag_set_add(tags, e->tag);
316 mac_learning_expire(ml, e);
318 hmap_shrink(&ml->table);
322 mac_learning_run(struct mac_learning *ml, struct tag_set *set)
325 while (get_lru(ml, &e)
326 && (hmap_count(&ml->table) > ml->max_entries
327 || time_now() >= e->expires)) {
328 COVERAGE_INC(mac_learning_expired);
330 tag_set_add(set, e->tag);
332 mac_learning_expire(ml, e);
337 mac_learning_wait(struct mac_learning *ml)
339 if (hmap_count(&ml->table) > ml->max_entries) {
340 poll_immediate_wake();
341 } else if (!list_is_empty(&ml->lrus)) {
342 struct mac_entry *e = mac_entry_from_lru_node(ml->lrus.next);
343 poll_timer_wait_until(e->expires * 1000LL);