2 * Copyright (c) 2008, 2009 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"
27 #include "poll-loop.h"
32 #define THIS_MODULE VLM_mac_learning
35 /* Returns the number of seconds since 'e' was last learned. */
37 mac_entry_age(const struct mac_entry *e)
39 time_t remaining = e->expires - time_now();
40 return MAC_ENTRY_IDLE_TIME - remaining;
44 mac_table_hash(const uint8_t mac[ETH_ADDR_LEN], uint16_t vlan)
46 return hash_bytes(mac, ETH_ADDR_LEN, vlan);
49 static struct mac_entry *
50 mac_entry_from_lru_node(struct list *list)
52 return CONTAINER_OF(list, struct mac_entry, lru_node);
55 /* Returns a tag that represents that 'mac' is on an unknown port in 'vlan'.
56 * (When we learn where 'mac' is in 'vlan', this allows flows that were
57 * flooded to be revalidated.) */
59 make_unknown_mac_tag(const struct mac_learning *ml,
60 const uint8_t mac[ETH_ADDR_LEN], uint16_t vlan)
62 uint32_t h = hash_int(ml->secret, mac_table_hash(mac, vlan));
63 return tag_create_deterministic(h);
67 mac_table_bucket(const struct mac_learning *ml,
68 const uint8_t mac[ETH_ADDR_LEN],
71 uint32_t hash = mac_table_hash(mac, vlan);
72 const struct list *list = &ml->table[hash & MAC_HASH_BITS];
73 return (struct list *) list;
76 static struct mac_entry *
77 search_bucket(struct list *bucket, const uint8_t mac[ETH_ADDR_LEN],
81 LIST_FOR_EACH (e, struct mac_entry, hash_node, bucket) {
82 if (eth_addr_equals(e->mac, mac) && e->vlan == vlan) {
89 /* If the LRU list is not empty, stores the least-recently-used entry in '*e'
90 * and returns true. Otherwise, if the LRU list is empty, stores NULL in '*e'
91 * and return false. */
93 get_lru(struct mac_learning *ml, struct mac_entry **e)
95 if (!list_is_empty(&ml->lrus)) {
96 *e = mac_entry_from_lru_node(ml->lrus.next);
104 /* Removes 'e' from the 'ml' hash table. 'e' must not already be on the free
107 free_mac_entry(struct mac_learning *ml, struct mac_entry *e)
109 list_remove(&e->hash_node);
110 list_remove(&e->lru_node);
111 list_push_front(&ml->free, &e->lru_node);
114 /* Creates and returns a new MAC learning table. */
115 struct mac_learning *
116 mac_learning_create(void)
118 struct mac_learning *ml;
121 ml = xmalloc(sizeof *ml);
122 list_init(&ml->lrus);
123 list_init(&ml->free);
124 for (i = 0; i < MAC_HASH_SIZE; i++) {
125 list_init(&ml->table[i]);
127 for (i = 0; i < MAC_MAX; i++) {
128 struct mac_entry *s = &ml->entries[i];
129 list_push_front(&ml->free, &s->lru_node);
131 ml->secret = random_uint32();
135 /* Destroys MAC learning table 'ml'. */
137 mac_learning_destroy(struct mac_learning *ml)
142 /* Attempts to make 'ml' learn from the fact that a frame from 'src_mac' was
143 * just observed arriving from 'src_port' on the given 'vlan'.
145 * Returns nonzero if we actually learned something from this, zero if it just
146 * confirms what we already knew. The nonzero return value is the tag of flows
147 * that now need revalidation.
149 * The 'vlan' parameter is used to maintain separate per-VLAN learning tables.
150 * Specify 0 if this behavior is undesirable. */
152 mac_learning_learn(struct mac_learning *ml,
153 const uint8_t src_mac[ETH_ADDR_LEN], uint16_t vlan,
159 if (eth_addr_is_multicast(src_mac)) {
160 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 30);
161 VLOG_DBG_RL(&rl, "multicast packet source "ETH_ADDR_FMT,
162 ETH_ADDR_ARGS(src_mac));
166 bucket = mac_table_bucket(ml, src_mac, vlan);
167 e = search_bucket(bucket, src_mac, vlan);
169 if (!list_is_empty(&ml->free)) {
170 e = mac_entry_from_lru_node(ml->free.next);
172 e = mac_entry_from_lru_node(ml->lrus.next);
173 list_remove(&e->hash_node);
175 memcpy(e->mac, src_mac, ETH_ADDR_LEN);
176 list_push_front(bucket, &e->hash_node);
179 e->tag = make_unknown_mac_tag(ml, src_mac, vlan);
182 /* Make the entry most-recently-used. */
183 list_remove(&e->lru_node);
184 list_push_back(&ml->lrus, &e->lru_node);
185 e->expires = time_now() + MAC_ENTRY_IDLE_TIME;
187 /* Did we learn something? */
188 if (e->port != src_port) {
189 tag_type old_tag = e->tag;
191 e->tag = tag_create_random();
192 COVERAGE_INC(mac_learning_learned);
198 /* Looks up MAC 'dst' for VLAN 'vlan' in 'ml'. Returns the port on which a
199 * frame destined for 'dst' should be sent, -1 if unknown. */
201 mac_learning_lookup(const struct mac_learning *ml,
202 const uint8_t dst[ETH_ADDR_LEN], uint16_t vlan)
205 return mac_learning_lookup_tag(ml, dst, vlan, &tag);
208 /* Looks up MAC 'dst' for VLAN 'vlan' in 'ml'. Returns the port on which a
209 * frame destined for 'dst' should be sent, -1 if unknown.
211 * Adds to '*tag' (which the caller must have initialized) the tag that should
212 * be attached to any flow created based on the return value, if any, to allow
213 * those flows to be revalidated when the MAC learning entry changes. */
215 mac_learning_lookup_tag(const struct mac_learning *ml,
216 const uint8_t dst[ETH_ADDR_LEN], uint16_t vlan,
219 if (eth_addr_is_multicast(dst)) {
222 struct mac_entry *e = search_bucket(mac_table_bucket(ml, dst, vlan),
228 *tag |= make_unknown_mac_tag(ml, dst, vlan);
234 /* Expires all the mac-learning entries in 'ml'. The tags in 'ml' are
235 * discarded, so the client is responsible for revalidating any flows that
236 * depend on 'ml', if necessary. */
238 mac_learning_flush(struct mac_learning *ml)
241 while (get_lru(ml, &e)){
242 free_mac_entry(ml, e);
247 mac_learning_run(struct mac_learning *ml, struct tag_set *set)
250 while (get_lru(ml, &e) && time_now() >= e->expires) {
251 COVERAGE_INC(mac_learning_expired);
253 tag_set_add(set, e->tag);
255 free_mac_entry(ml, e);
260 mac_learning_wait(struct mac_learning *ml)
262 if (!list_is_empty(&ml->lrus)) {
263 struct mac_entry *e = mac_entry_from_lru_node(ml->lrus.next);
264 poll_timer_wait((e->expires - time_now()) * 1000);