]> git.proxmox.com Git - mirror_ovs.git/blob - lib/mac-learning.c
Global replace of Nicira Networks.
[mirror_ovs.git] / lib / mac-learning.c
1 /*
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include <config.h>
18 #include "mac-learning.h"
19
20 #include <assert.h>
21 #include <inttypes.h>
22 #include <stdlib.h>
23
24 #include "bitmap.h"
25 #include "coverage.h"
26 #include "hash.h"
27 #include "list.h"
28 #include "poll-loop.h"
29 #include "tag.h"
30 #include "timeval.h"
31 #include "unaligned.h"
32 #include "util.h"
33 #include "vlan-bitmap.h"
34 #include "vlog.h"
35
36 VLOG_DEFINE_THIS_MODULE(mac_learning);
37
38 COVERAGE_DEFINE(mac_learning_learned);
39 COVERAGE_DEFINE(mac_learning_expired);
40
41 /* Returns the number of seconds since 'e' (within 'ml') was last learned. */
42 int
43 mac_entry_age(const struct mac_learning *ml, const struct mac_entry *e)
44 {
45 time_t remaining = e->expires - time_now();
46 return ml->idle_time - remaining;
47 }
48
49 static uint32_t
50 mac_table_hash(const struct mac_learning *ml, const uint8_t mac[ETH_ADDR_LEN],
51 uint16_t vlan)
52 {
53 unsigned int mac1 = get_unaligned_u32((uint32_t *) mac);
54 unsigned int mac2 = get_unaligned_u16((uint16_t *) (mac + 4));
55 return hash_3words(mac1, mac2 | (vlan << 16), ml->secret);
56 }
57
58 static struct mac_entry *
59 mac_entry_from_lru_node(struct list *list)
60 {
61 return CONTAINER_OF(list, struct mac_entry, lru_node);
62 }
63
64 /* Returns a tag that represents that 'mac' is on an unknown port in 'vlan'.
65 * (When we learn where 'mac' is in 'vlan', this allows flows that were
66 * flooded to be revalidated.) */
67 static tag_type
68 make_unknown_mac_tag(const struct mac_learning *ml,
69 const uint8_t mac[ETH_ADDR_LEN], uint16_t vlan)
70 {
71 return tag_create_deterministic(mac_table_hash(ml, mac, vlan));
72 }
73
74 static struct mac_entry *
75 mac_entry_lookup(const struct mac_learning *ml,
76 const uint8_t mac[ETH_ADDR_LEN], uint16_t vlan)
77 {
78 struct mac_entry *e;
79
80 HMAP_FOR_EACH_WITH_HASH (e, hmap_node, mac_table_hash(ml, mac, vlan),
81 &ml->table) {
82 if (e->vlan == vlan && eth_addr_equals(e->mac, mac)) {
83 return e;
84 }
85 }
86 return NULL;
87 }
88
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. */
92 static bool
93 get_lru(struct mac_learning *ml, struct mac_entry **e)
94 {
95 if (!list_is_empty(&ml->lrus)) {
96 *e = mac_entry_from_lru_node(ml->lrus.next);
97 return true;
98 } else {
99 *e = NULL;
100 return false;
101 }
102 }
103
104 static unsigned int
105 normalize_idle_time(unsigned int idle_time)
106 {
107 return (idle_time < 15 ? 15
108 : idle_time > 3600 ? 3600
109 : idle_time);
110 }
111
112 /* Creates and returns a new MAC learning table with an initial MAC aging
113 * timeout of 'idle_time' seconds. */
114 struct mac_learning *
115 mac_learning_create(unsigned int idle_time)
116 {
117 struct mac_learning *ml;
118
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 return ml;
126 }
127
128 /* Destroys MAC learning table 'ml'. */
129 void
130 mac_learning_destroy(struct mac_learning *ml)
131 {
132 if (ml) {
133 struct mac_entry *e, *next;
134
135 HMAP_FOR_EACH_SAFE (e, next, hmap_node, &ml->table) {
136 hmap_remove(&ml->table, &e->hmap_node);
137 free(e);
138 }
139 hmap_destroy(&ml->table);
140
141 bitmap_free(ml->flood_vlans);
142 free(ml);
143 }
144 }
145
146 /* Provides a bitmap of VLANs which have learning disabled, that is, VLANs on
147 * which all packets are flooded. Returns true if the set has changed from the
148 * previous value. */
149 bool
150 mac_learning_set_flood_vlans(struct mac_learning *ml,
151 const unsigned long *bitmap)
152 {
153 if (vlan_bitmap_equal(ml->flood_vlans, bitmap)) {
154 return false;
155 } else {
156 bitmap_free(ml->flood_vlans);
157 ml->flood_vlans = vlan_bitmap_clone(bitmap);
158 return true;
159 }
160 }
161
162 /* Changes the MAC aging timeout of 'ml' to 'idle_time' seconds. */
163 void
164 mac_learning_set_idle_time(struct mac_learning *ml, unsigned int idle_time)
165 {
166 idle_time = normalize_idle_time(idle_time);
167 if (idle_time != ml->idle_time) {
168 struct mac_entry *e;
169 int delta;
170
171 delta = (int) idle_time - (int) ml->idle_time;
172 LIST_FOR_EACH (e, lru_node, &ml->lrus) {
173 e->expires += delta;
174 }
175 ml->idle_time = idle_time;
176 }
177 }
178
179 static bool
180 is_learning_vlan(const struct mac_learning *ml, uint16_t vlan)
181 {
182 return !ml->flood_vlans || !bitmap_is_set(ml->flood_vlans, vlan);
183 }
184
185 /* Returns true if 'src_mac' may be learned on 'vlan' for 'ml'.
186 * Returns false if 'ml' is NULL, if src_mac is not valid for learning, or if
187 * 'vlan' is configured on 'ml' to flood all packets. */
188 bool
189 mac_learning_may_learn(const struct mac_learning *ml,
190 const uint8_t src_mac[ETH_ADDR_LEN], uint16_t vlan)
191 {
192 return ml && is_learning_vlan(ml, vlan) && !eth_addr_is_multicast(src_mac);
193 }
194
195 /* Searches 'ml' for and returns a MAC learning entry for 'src_mac' in 'vlan',
196 * inserting a new entry if necessary. The caller must have already verified,
197 * by calling mac_learning_may_learn(), that 'src_mac' and 'vlan' are
198 * learnable.
199 *
200 * If the returned MAC entry is new (as may be determined by calling
201 * mac_entry_is_new()), then the caller must pass the new entry to
202 * mac_learning_changed(). The caller must also initialize the new entry's
203 * 'port' member. Otherwise calling those functions is at the caller's
204 * discretion. */
205 struct mac_entry *
206 mac_learning_insert(struct mac_learning *ml,
207 const uint8_t src_mac[ETH_ADDR_LEN], uint16_t vlan)
208 {
209 struct mac_entry *e;
210
211 e = mac_entry_lookup(ml, src_mac, vlan);
212 if (!e) {
213 uint32_t hash = mac_table_hash(ml, src_mac, vlan);
214
215 if (hmap_count(&ml->table) >= MAC_MAX) {
216 get_lru(ml, &e);
217 mac_learning_expire(ml, e);
218 }
219
220 e = xmalloc(sizeof *e);
221 hmap_insert(&ml->table, &e->hmap_node, hash);
222 memcpy(e->mac, src_mac, ETH_ADDR_LEN);
223 e->vlan = vlan;
224 e->tag = 0;
225 e->grat_arp_lock = TIME_MIN;
226 } else {
227 list_remove(&e->lru_node);
228 }
229
230 /* Mark 'e' as recently used. */
231 list_push_back(&ml->lrus, &e->lru_node);
232 e->expires = time_now() + ml->idle_time;
233
234 return e;
235 }
236
237 /* Changes 'e''s tag to a new, randomly selected one, and returns the tag that
238 * would have been previously used for this entry's MAC and VLAN (either before
239 * 'e' was inserted, if it is new, or otherwise before its port was updated.)
240 *
241 * The client should call this function after obtaining a MAC learning entry
242 * from mac_learning_insert(), if the entry is either new or if its learned
243 * port has changed. */
244 tag_type
245 mac_learning_changed(struct mac_learning *ml, struct mac_entry *e)
246 {
247 tag_type old_tag = e->tag;
248
249 COVERAGE_INC(mac_learning_learned);
250
251 e->tag = tag_create_random();
252 return old_tag ? old_tag : make_unknown_mac_tag(ml, e->mac, e->vlan);
253 }
254
255 /* Looks up MAC 'dst' for VLAN 'vlan' in 'ml' and returns the associated MAC
256 * learning entry, if any. If 'tag' is nonnull, then the tag that associates
257 * 'dst' and 'vlan' with its currently learned port will be OR'd into
258 * '*tag'. */
259 struct mac_entry *
260 mac_learning_lookup(const struct mac_learning *ml,
261 const uint8_t dst[ETH_ADDR_LEN], uint16_t vlan,
262 tag_type *tag)
263 {
264 if (eth_addr_is_multicast(dst)) {
265 /* No tag because the treatment of multicast destinations never
266 * changes. */
267 return NULL;
268 } else if (!is_learning_vlan(ml, vlan)) {
269 /* We don't tag this property. The set of learning VLANs changes so
270 * rarely that we revalidate every flow when it changes. */
271 return NULL;
272 } else {
273 struct mac_entry *e = mac_entry_lookup(ml, dst, vlan);
274
275 assert(e == NULL || e->tag != 0);
276 if (tag) {
277 /* Tag either the learned port or the lack thereof. */
278 *tag |= e ? e->tag : make_unknown_mac_tag(ml, dst, vlan);
279 }
280 return e;
281 }
282 }
283
284 /* Expires 'e' from the 'ml' hash table. */
285 void
286 mac_learning_expire(struct mac_learning *ml, struct mac_entry *e)
287 {
288 hmap_remove(&ml->table, &e->hmap_node);
289 list_remove(&e->lru_node);
290 free(e);
291 }
292
293 /* Expires all the mac-learning entries in 'ml'. If not NULL, the tags in 'ml'
294 * are added to 'tags'. Otherwise the tags in 'ml' are discarded. The client
295 * is responsible for revalidating any flows that depend on 'ml', if
296 * necessary. */
297 void
298 mac_learning_flush(struct mac_learning *ml, struct tag_set *tags)
299 {
300 struct mac_entry *e;
301 while (get_lru(ml, &e)){
302 if (tags) {
303 tag_set_add(tags, e->tag);
304 }
305 mac_learning_expire(ml, e);
306 }
307 hmap_shrink(&ml->table);
308 }
309
310 void
311 mac_learning_run(struct mac_learning *ml, struct tag_set *set)
312 {
313 struct mac_entry *e;
314 while (get_lru(ml, &e) && time_now() >= e->expires) {
315 COVERAGE_INC(mac_learning_expired);
316 if (set) {
317 tag_set_add(set, e->tag);
318 }
319 mac_learning_expire(ml, e);
320 }
321 }
322
323 void
324 mac_learning_wait(struct mac_learning *ml)
325 {
326 if (!list_is_empty(&ml->lrus)) {
327 struct mac_entry *e = mac_entry_from_lru_node(ml->lrus.next);
328 poll_timer_wait_until(e->expires * 1000LL);
329 }
330 }