2 * Copyright (c) 2008, 2009 Nicira Networks.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include "mac-learning.h"
27 #include "poll-loop.h"
32 #define THIS_MODULE VLM_mac_learning
35 #define MAC_HASH_BITS 10
36 #define MAC_HASH_MASK (MAC_HASH_SIZE - 1)
37 #define MAC_HASH_SIZE (1u << MAC_HASH_BITS)
41 /* A MAC learning table entry. */
43 struct list hash_node; /* Element in a mac_learning 'table' list. */
44 struct list lru_node; /* Element in 'lrus' or 'free' list. */
45 time_t expires; /* Expiration time. */
46 uint8_t mac[ETH_ADDR_LEN]; /* Known MAC address. */
47 uint16_t vlan; /* VLAN tag. */
48 int port; /* Port on which MAC was most recently seen. */
49 tag_type tag; /* Tag for this learning entry. */
52 /* MAC learning table. */
54 struct list free; /* Not-in-use entries. */
55 struct list lrus; /* In-use entries, least recently used at the
56 front, most recently used at the back. */
57 struct list table[MAC_HASH_SIZE]; /* Hash table. */
58 struct mac_entry entries[MAC_MAX]; /* All entries. */
59 uint32_t secret; /* Secret for */
63 mac_table_hash(const uint8_t mac[ETH_ADDR_LEN], uint16_t vlan)
65 return hash_bytes(mac, ETH_ADDR_LEN, vlan);
68 static struct mac_entry *
69 mac_entry_from_lru_node(struct list *list)
71 return CONTAINER_OF(list, struct mac_entry, lru_node);
74 /* Returns a tag that represents that 'mac' is on an unknown port in 'vlan'.
75 * (When we learn where 'mac' is in 'vlan', this allows flows that were
76 * flooded to be revalidated.) */
78 make_unknown_mac_tag(const struct mac_learning *ml,
79 const uint8_t mac[ETH_ADDR_LEN], uint16_t vlan)
81 uint32_t h = hash_int(ml->secret, mac_table_hash(mac, vlan));
82 return tag_create_deterministic(h);
86 mac_table_bucket(const struct mac_learning *ml,
87 const uint8_t mac[ETH_ADDR_LEN],
90 uint32_t hash = mac_table_hash(mac, vlan);
91 const struct list *list = &ml->table[hash & MAC_HASH_BITS];
92 return (struct list *) list;
95 static struct mac_entry *
96 search_bucket(struct list *bucket, const uint8_t mac[ETH_ADDR_LEN],
100 LIST_FOR_EACH (e, struct mac_entry, hash_node, bucket) {
101 if (eth_addr_equals(e->mac, mac) && e->vlan == vlan) {
108 /* If the LRU list is not empty, stores the least-recently-used entry in '*e'
109 * and returns true. Otherwise, if the LRU list is empty, stores NULL in '*e'
110 * and return false. */
112 get_lru(struct mac_learning *ml, struct mac_entry **e)
114 if (!list_is_empty(&ml->lrus)) {
115 *e = mac_entry_from_lru_node(ml->lrus.next);
123 /* Removes 'e' from the 'ml' hash table. 'e' must not already be on the free
126 free_mac_entry(struct mac_learning *ml, struct mac_entry *e)
128 list_remove(&e->hash_node);
129 list_remove(&e->lru_node);
130 list_push_front(&ml->free, &e->lru_node);
133 /* Creates and returns a new MAC learning table. */
134 struct mac_learning *
135 mac_learning_create(void)
137 struct mac_learning *ml;
140 ml = xmalloc(sizeof *ml);
141 list_init(&ml->lrus);
142 list_init(&ml->free);
143 for (i = 0; i < MAC_HASH_SIZE; i++) {
144 list_init(&ml->table[i]);
146 for (i = 0; i < MAC_MAX; i++) {
147 struct mac_entry *s = &ml->entries[i];
148 list_push_front(&ml->free, &s->lru_node);
150 ml->secret = random_uint32();
154 /* Destroys MAC learning table 'ml'. */
156 mac_learning_destroy(struct mac_learning *ml)
161 /* Attempts to make 'ml' learn from the fact that a frame from 'src_mac' was
162 * just observed arriving from 'src_port' on the given 'vlan'.
164 * Returns nonzero if we actually learned something from this, zero if it just
165 * confirms what we already knew. The nonzero return value is the tag of flows
166 * that now need revalidation.
168 * The 'vlan' parameter is used to maintain separate per-VLAN learning tables.
169 * Specify 0 if this behavior is undesirable. */
171 mac_learning_learn(struct mac_learning *ml,
172 const uint8_t src_mac[ETH_ADDR_LEN], uint16_t vlan,
178 if (eth_addr_is_multicast(src_mac)) {
179 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 30);
180 VLOG_DBG_RL(&rl, "multicast packet source "ETH_ADDR_FMT,
181 ETH_ADDR_ARGS(src_mac));
185 bucket = mac_table_bucket(ml, src_mac, vlan);
186 e = search_bucket(bucket, src_mac, vlan);
188 if (!list_is_empty(&ml->free)) {
189 e = mac_entry_from_lru_node(ml->free.next);
191 e = mac_entry_from_lru_node(ml->lrus.next);
192 list_remove(&e->hash_node);
194 memcpy(e->mac, src_mac, ETH_ADDR_LEN);
195 list_push_front(bucket, &e->hash_node);
198 e->tag = make_unknown_mac_tag(ml, src_mac, vlan);
201 /* Make the entry most-recently-used. */
202 list_remove(&e->lru_node);
203 list_push_back(&ml->lrus, &e->lru_node);
204 e->expires = time_now() + 60;
206 /* Did we learn something? */
207 if (e->port != src_port) {
208 tag_type old_tag = e->tag;
210 e->tag = tag_create_random();
211 COVERAGE_INC(mac_learning_learned);
217 /* Looks up MAC 'dst' for VLAN 'vlan' in 'ml'. Returns the port on which a
218 * frame destined for 'dst' should be sent, -1 if unknown. */
220 mac_learning_lookup(const struct mac_learning *ml,
221 const uint8_t dst[ETH_ADDR_LEN], uint16_t vlan)
224 return mac_learning_lookup_tag(ml, dst, vlan, &tag);
227 /* Looks up MAC 'dst' for VLAN 'vlan' in 'ml'. Returns the port on which a
228 * frame destined for 'dst' should be sent, -1 if unknown.
230 * Adds to '*tag' (which the caller must have initialized) the tag that should
231 * be attached to any flow created based on the return value, if any, to allow
232 * those flows to be revalidated when the MAC learning entry changes. */
234 mac_learning_lookup_tag(const struct mac_learning *ml,
235 const uint8_t dst[ETH_ADDR_LEN], uint16_t vlan,
238 if (eth_addr_is_multicast(dst)) {
241 struct mac_entry *e = search_bucket(mac_table_bucket(ml, dst, vlan),
247 *tag |= make_unknown_mac_tag(ml, dst, vlan);
253 /* Expires all the mac-learning entries in 'ml'. The tags in 'ml' are
254 * discarded, so the client is responsible for revalidating any flows that
255 * depend on 'ml', if necessary. */
257 mac_learning_flush(struct mac_learning *ml)
260 while (get_lru(ml, &e)){
261 free_mac_entry(ml, e);
266 mac_learning_run(struct mac_learning *ml, struct tag_set *set)
269 while (get_lru(ml, &e) && time_now() >= e->expires) {
270 COVERAGE_INC(mac_learning_expired);
272 tag_set_add(set, e->tag);
274 free_mac_entry(ml, e);
279 mac_learning_wait(struct mac_learning *ml)
281 if (!list_is_empty(&ml->lrus)) {
282 struct mac_entry *e = mac_entry_from_lru_node(ml->lrus.next);
283 poll_timer_wait((e->expires - time_now()) * 1000);