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"
34 VLOG_DEFINE_THIS_MODULE(mac_learning);
36 COVERAGE_DEFINE(mac_learning_learned);
37 COVERAGE_DEFINE(mac_learning_expired);
39 /* Returns the number of seconds since 'e' was last learned. */
41 mac_entry_age(const struct mac_entry *e)
43 time_t remaining = e->expires - time_now();
44 return MAC_ENTRY_IDLE_TIME - remaining;
48 mac_table_hash(const uint8_t mac[ETH_ADDR_LEN], uint16_t vlan)
50 return hash_bytes(mac, ETH_ADDR_LEN, vlan);
53 static struct mac_entry *
54 mac_entry_from_lru_node(struct list *list)
56 return CONTAINER_OF(list, struct mac_entry, lru_node);
59 /* Returns a tag that represents that 'mac' is on an unknown port in 'vlan'.
60 * (When we learn where 'mac' is in 'vlan', this allows flows that were
61 * flooded to be revalidated.) */
63 make_unknown_mac_tag(const struct mac_learning *ml,
64 const uint8_t mac[ETH_ADDR_LEN], uint16_t vlan)
66 uint32_t h = hash_int(ml->secret, mac_table_hash(mac, vlan));
67 return tag_create_deterministic(h);
71 mac_table_bucket(const struct mac_learning *ml,
72 const uint8_t mac[ETH_ADDR_LEN],
75 uint32_t hash = mac_table_hash(mac, vlan);
76 const struct list *list = &ml->table[hash & MAC_HASH_BITS];
77 return (struct list *) list;
80 static struct mac_entry *
81 search_bucket(struct list *bucket, const uint8_t mac[ETH_ADDR_LEN],
85 LIST_FOR_EACH (e, hash_node, bucket) {
86 if (eth_addr_equals(e->mac, mac) && e->vlan == vlan) {
93 /* If the LRU list is not empty, stores the least-recently-used entry in '*e'
94 * and returns true. Otherwise, if the LRU list is empty, stores NULL in '*e'
95 * and return false. */
97 get_lru(struct mac_learning *ml, struct mac_entry **e)
99 if (!list_is_empty(&ml->lrus)) {
100 *e = mac_entry_from_lru_node(ml->lrus.next);
108 /* Creates and returns a new MAC learning table. */
109 struct mac_learning *
110 mac_learning_create(void)
112 struct mac_learning *ml;
115 ml = xmalloc(sizeof *ml);
116 list_init(&ml->lrus);
117 list_init(&ml->free);
118 for (i = 0; i < MAC_HASH_SIZE; i++) {
119 list_init(&ml->table[i]);
121 for (i = 0; i < MAC_MAX; i++) {
122 struct mac_entry *s = &ml->entries[i];
123 list_push_front(&ml->free, &s->lru_node);
125 ml->secret = random_uint32();
126 ml->flood_vlans = NULL;
130 /* Destroys MAC learning table 'ml'. */
132 mac_learning_destroy(struct mac_learning *ml)
135 bitmap_free(ml->flood_vlans);
140 /* Provides a bitmap of VLANs which have learning disabled, that is, VLANs on
141 * which all packets are flooded. It takes ownership of the bitmap. Returns
142 * true if the set has changed from the previous value. */
144 mac_learning_set_flood_vlans(struct mac_learning *ml, unsigned long *bitmap)
146 bool ret = (bitmap == NULL
147 ? ml->flood_vlans != NULL
148 : (ml->flood_vlans == NULL
149 || !bitmap_equal(bitmap, ml->flood_vlans, 4096)));
151 bitmap_free(ml->flood_vlans);
152 ml->flood_vlans = bitmap;
158 is_learning_vlan(const struct mac_learning *ml, uint16_t vlan)
160 return !(ml->flood_vlans && bitmap_is_set(ml->flood_vlans, vlan));
163 /* Returns true if 'src_mac' may be learned on 'vlan' for 'ml'.
164 * Returns false if 'ml' is NULL, if src_mac is not valid for learning, or if
165 * 'vlan' is configured on 'ml' to flood all packets. */
167 mac_learning_may_learn(const struct mac_learning *ml,
168 const uint8_t src_mac[ETH_ADDR_LEN], uint16_t vlan)
170 return ml && is_learning_vlan(ml, vlan) && !eth_addr_is_multicast(src_mac);
173 /* Searches 'ml' for and returns a MAC learning entry for 'src_mac' in 'vlan',
174 * inserting a new entry if necessary. The caller must have already verified,
175 * by calling mac_learning_may_learn(), that 'src_mac' and 'vlan' are
178 * If the returned MAC entry is new (as may be determined by calling
179 * mac_entry_is_new()), then the caller must pass the new entry to
180 * mac_learning_changed(). The caller must also initialize the new entry's
181 * 'port' member. Otherwise calling those functions is at the caller's
184 mac_learning_insert(struct mac_learning *ml,
185 const uint8_t src_mac[ETH_ADDR_LEN], uint16_t vlan)
190 bucket = mac_table_bucket(ml, src_mac, vlan);
191 e = search_bucket(bucket, src_mac, vlan);
193 if (!list_is_empty(&ml->free)) {
194 e = mac_entry_from_lru_node(ml->free.next);
196 e = mac_entry_from_lru_node(ml->lrus.next);
197 list_remove(&e->hash_node);
199 list_push_front(bucket, &e->hash_node);
200 memcpy(e->mac, src_mac, ETH_ADDR_LEN);
203 e->grat_arp_lock = TIME_MIN;
206 /* Mark 'e' as recently used. */
207 list_remove(&e->lru_node);
208 list_push_back(&ml->lrus, &e->lru_node);
209 e->expires = time_now() + MAC_ENTRY_IDLE_TIME;
214 /* Changes 'e''s tag to a new, randomly selected one, and returns the tag that
215 * would have been previously used for this entry's MAC and VLAN (either before
216 * 'e' was inserted, if it is new, or otherwise before its port was updated.)
218 * The client should call this function after obtaining a MAC learning entry
219 * from mac_learning_insert(), if the entry is either new or if its learned
220 * port has changed. */
222 mac_learning_changed(struct mac_learning *ml, struct mac_entry *e)
224 tag_type old_tag = e->tag;
226 COVERAGE_INC(mac_learning_learned);
228 e->tag = tag_create_random();
229 return old_tag ? old_tag : make_unknown_mac_tag(ml, e->mac, e->vlan);
232 /* Looks up MAC 'dst' for VLAN 'vlan' in 'ml' and returns the associated MAC
233 * learning entry, if any. If 'tag' is nonnull, then the tag that associates
234 * 'dst' and 'vlan' with its currently learned port will be OR'd into
237 mac_learning_lookup(const struct mac_learning *ml,
238 const uint8_t dst[ETH_ADDR_LEN], uint16_t vlan,
241 if (eth_addr_is_multicast(dst)) {
242 /* No tag because the treatment of multicast destinations never
245 } else if (!is_learning_vlan(ml, vlan)) {
246 /* We don't tag this property. The set of learning VLANs changes so
247 * rarely that we revalidate every flow when it changes. */
250 struct mac_entry *e = search_bucket(mac_table_bucket(ml, dst, vlan),
252 assert(e == NULL || e->tag != 0);
254 /* Tag either the learned port or the lack thereof. */
255 *tag |= e ? e->tag : make_unknown_mac_tag(ml, dst, vlan);
261 /* Expires 'e' from the 'ml' hash table. 'e' must not already be on the free
264 mac_learning_expire(struct mac_learning *ml, struct mac_entry *e)
266 list_remove(&e->hash_node);
267 list_remove(&e->lru_node);
268 list_push_front(&ml->free, &e->lru_node);
271 /* Expires all the mac-learning entries in 'ml'. The tags in 'ml' are
272 * discarded, so the client is responsible for revalidating any flows that
273 * depend on 'ml', if necessary. */
275 mac_learning_flush(struct mac_learning *ml)
278 while (get_lru(ml, &e)){
279 mac_learning_expire(ml, e);
284 mac_learning_run(struct mac_learning *ml, struct tag_set *set)
287 while (get_lru(ml, &e) && time_now() >= e->expires) {
288 COVERAGE_INC(mac_learning_expired);
290 tag_set_add(set, e->tag);
292 mac_learning_expire(ml, e);
297 mac_learning_wait(struct mac_learning *ml)
299 if (!list_is_empty(&ml->lrus)) {
300 struct mac_entry *e = mac_entry_from_lru_node(ml->lrus.next);
301 poll_timer_wait_until(e->expires * 1000LL);