]>
git.proxmox.com Git - mirror_ovs.git/blob - lib/mac-learning.c
2 * Copyright (c) 2008, 2009, 2010 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"
33 #define THIS_MODULE VLM_mac_learning
36 /* Returns the number of seconds since 'e' was last learned. */
38 mac_entry_age(const struct mac_entry
*e
)
40 time_t remaining
= e
->expires
- time_now();
41 return MAC_ENTRY_IDLE_TIME
- remaining
;
45 mac_table_hash(const uint8_t mac
[ETH_ADDR_LEN
], uint16_t vlan
)
47 return hash_bytes(mac
, ETH_ADDR_LEN
, vlan
);
50 static struct mac_entry
*
51 mac_entry_from_lru_node(struct list
*list
)
53 return CONTAINER_OF(list
, struct mac_entry
, lru_node
);
56 /* Returns a tag that represents that 'mac' is on an unknown port in 'vlan'.
57 * (When we learn where 'mac' is in 'vlan', this allows flows that were
58 * flooded to be revalidated.) */
60 make_unknown_mac_tag(const struct mac_learning
*ml
,
61 const uint8_t mac
[ETH_ADDR_LEN
], uint16_t vlan
)
63 uint32_t h
= hash_int(ml
->secret
, mac_table_hash(mac
, vlan
));
64 return tag_create_deterministic(h
);
68 mac_table_bucket(const struct mac_learning
*ml
,
69 const uint8_t mac
[ETH_ADDR_LEN
],
72 uint32_t hash
= mac_table_hash(mac
, vlan
);
73 const struct list
*list
= &ml
->table
[hash
& MAC_HASH_BITS
];
74 return (struct list
*) list
;
77 static struct mac_entry
*
78 search_bucket(struct list
*bucket
, const uint8_t mac
[ETH_ADDR_LEN
],
82 LIST_FOR_EACH (e
, struct mac_entry
, hash_node
, bucket
) {
83 if (eth_addr_equals(e
->mac
, mac
) && e
->vlan
== vlan
) {
90 /* If the LRU list is not empty, stores the least-recently-used entry in '*e'
91 * and returns true. Otherwise, if the LRU list is empty, stores NULL in '*e'
92 * and return false. */
94 get_lru(struct mac_learning
*ml
, struct mac_entry
**e
)
96 if (!list_is_empty(&ml
->lrus
)) {
97 *e
= mac_entry_from_lru_node(ml
->lrus
.next
);
105 /* Removes 'e' from the 'ml' hash table. 'e' must not already be on the free
108 free_mac_entry(struct mac_learning
*ml
, struct mac_entry
*e
)
110 list_remove(&e
->hash_node
);
111 list_remove(&e
->lru_node
);
112 list_push_front(&ml
->free
, &e
->lru_node
);
115 /* Creates and returns a new MAC learning table. */
116 struct mac_learning
*
117 mac_learning_create(void)
119 struct mac_learning
*ml
;
122 ml
= xmalloc(sizeof *ml
);
123 list_init(&ml
->lrus
);
124 list_init(&ml
->free
);
125 for (i
= 0; i
< MAC_HASH_SIZE
; i
++) {
126 list_init(&ml
->table
[i
]);
128 for (i
= 0; i
< MAC_MAX
; i
++) {
129 struct mac_entry
*s
= &ml
->entries
[i
];
130 list_push_front(&ml
->free
, &s
->lru_node
);
132 ml
->secret
= random_uint32();
133 ml
->flood_vlans
= NULL
;
137 /* Destroys MAC learning table 'ml'. */
139 mac_learning_destroy(struct mac_learning
*ml
)
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. It takes ownership of the bitmap. Returns
149 * true if the set has changed from the previous value. */
151 mac_learning_set_flood_vlans(struct mac_learning
*ml
, unsigned long *bitmap
)
153 bool ret
= (bitmap
== NULL
154 ? ml
->flood_vlans
!= NULL
155 : (ml
->flood_vlans
== NULL
156 || !bitmap_equal(bitmap
, ml
->flood_vlans
, 4096)));
158 bitmap_free(ml
->flood_vlans
);
159 ml
->flood_vlans
= bitmap
;
165 is_learning_vlan(const struct mac_learning
*ml
, uint16_t vlan
)
167 return !(ml
->flood_vlans
&& bitmap_is_set(ml
->flood_vlans
, vlan
));
170 /* Attempts to make 'ml' learn from the fact that a frame from 'src_mac' was
171 * just observed arriving from 'src_port' on the given 'vlan'.
173 * Returns nonzero if we actually learned something from this, zero if it just
174 * confirms what we already knew. The nonzero return value is the tag of flows
175 * that now need revalidation.
177 * The 'vlan' parameter is used to maintain separate per-VLAN learning tables.
178 * Specify 0 if this behavior is undesirable. */
180 mac_learning_learn(struct mac_learning
*ml
,
181 const uint8_t src_mac
[ETH_ADDR_LEN
], uint16_t vlan
,
187 if (!is_learning_vlan(ml
, vlan
)) {
191 if (eth_addr_is_multicast(src_mac
)) {
192 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(30, 30);
193 VLOG_DBG_RL(&rl
, "multicast packet source "ETH_ADDR_FMT
,
194 ETH_ADDR_ARGS(src_mac
));
198 bucket
= mac_table_bucket(ml
, src_mac
, vlan
);
199 e
= search_bucket(bucket
, src_mac
, vlan
);
201 if (!list_is_empty(&ml
->free
)) {
202 e
= mac_entry_from_lru_node(ml
->free
.next
);
204 e
= mac_entry_from_lru_node(ml
->lrus
.next
);
205 list_remove(&e
->hash_node
);
207 memcpy(e
->mac
, src_mac
, ETH_ADDR_LEN
);
208 list_push_front(bucket
, &e
->hash_node
);
211 e
->tag
= make_unknown_mac_tag(ml
, src_mac
, vlan
);
214 /* Make the entry most-recently-used. */
215 list_remove(&e
->lru_node
);
216 list_push_back(&ml
->lrus
, &e
->lru_node
);
217 e
->expires
= time_now() + MAC_ENTRY_IDLE_TIME
;
219 /* Did we learn something? */
220 if (e
->port
!= src_port
) {
221 tag_type old_tag
= e
->tag
;
223 e
->tag
= tag_create_random();
224 COVERAGE_INC(mac_learning_learned
);
230 /* Looks up MAC 'dst' for VLAN 'vlan' in 'ml'. Returns the port on which a
231 * frame destined for 'dst' should be sent, -1 if unknown. */
233 mac_learning_lookup(const struct mac_learning
*ml
,
234 const uint8_t dst
[ETH_ADDR_LEN
], uint16_t vlan
)
237 return mac_learning_lookup_tag(ml
, dst
, vlan
, &tag
);
240 /* Looks up MAC 'dst' for VLAN 'vlan' in 'ml'. Returns the port on which a
241 * frame destined for 'dst' should be sent, -1 if unknown.
243 * Adds to '*tag' (which the caller must have initialized) the tag that should
244 * be attached to any flow created based on the return value, if any, to allow
245 * those flows to be revalidated when the MAC learning entry changes. */
247 mac_learning_lookup_tag(const struct mac_learning
*ml
,
248 const uint8_t dst
[ETH_ADDR_LEN
], uint16_t vlan
,
251 if (eth_addr_is_multicast(dst
) || !is_learning_vlan(ml
, vlan
)) {
254 struct mac_entry
*e
= search_bucket(mac_table_bucket(ml
, dst
, vlan
),
260 *tag
|= make_unknown_mac_tag(ml
, dst
, vlan
);
266 /* Expires all the mac-learning entries in 'ml'. The tags in 'ml' are
267 * discarded, so the client is responsible for revalidating any flows that
268 * depend on 'ml', if necessary. */
270 mac_learning_flush(struct mac_learning
*ml
)
273 while (get_lru(ml
, &e
)){
274 free_mac_entry(ml
, e
);
279 mac_learning_run(struct mac_learning
*ml
, struct tag_set
*set
)
282 while (get_lru(ml
, &e
) && time_now() >= e
->expires
) {
283 COVERAGE_INC(mac_learning_expired
);
285 tag_set_add(set
, e
->tag
);
287 free_mac_entry(ml
, e
);
292 mac_learning_wait(struct mac_learning
*ml
)
294 if (!list_is_empty(&ml
->lrus
)) {
295 struct mac_entry
*e
= mac_entry_from_lru_node(ml
->lrus
.next
);
296 poll_timer_wait((e
->expires
- time_now()) * 1000);