]> git.proxmox.com Git - ovs.git/blob - lib/mac-learning.c
datapath-windows: Avoid BSOD when switch context is NULL
[ovs.git] / lib / mac-learning.c
1 /*
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 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 <inttypes.h>
21 #include <stdlib.h>
22
23 #include "bitmap.h"
24 #include "coverage.h"
25 #include "hash.h"
26 #include "list.h"
27 #include "poll-loop.h"
28 #include "timeval.h"
29 #include "unaligned.h"
30 #include "util.h"
31 #include "vlan-bitmap.h"
32
33 COVERAGE_DEFINE(mac_learning_learned);
34 COVERAGE_DEFINE(mac_learning_expired);
35
36 /* Returns the number of seconds since 'e' (within 'ml') was last learned. */
37 int
38 mac_entry_age(const struct mac_learning *ml, const struct mac_entry *e)
39 {
40 time_t remaining = e->expires - time_now();
41 return ml->idle_time - remaining;
42 }
43
44 static uint32_t
45 mac_table_hash(const struct mac_learning *ml, const uint8_t mac[ETH_ADDR_LEN],
46 uint16_t vlan)
47 {
48 return hash_mac(mac, vlan, ml->secret);
49 }
50
51 static struct mac_entry *
52 mac_entry_from_lru_node(struct list *list)
53 {
54 return CONTAINER_OF(list, struct mac_entry, lru_node);
55 }
56
57 static struct mac_entry *
58 mac_entry_lookup(const struct mac_learning *ml,
59 const uint8_t mac[ETH_ADDR_LEN], uint16_t vlan)
60 {
61 struct mac_entry *e;
62
63 HMAP_FOR_EACH_WITH_HASH (e, hmap_node, mac_table_hash(ml, mac, vlan),
64 &ml->table) {
65 if (e->vlan == vlan && eth_addr_equals(e->mac, mac)) {
66 return e;
67 }
68 }
69 return NULL;
70 }
71
72 /* If the LRU list is not empty, stores the least-recently-used entry in '*e'
73 * and returns true. Otherwise, if the LRU list is empty, stores NULL in '*e'
74 * and return false. */
75 static bool
76 get_lru(struct mac_learning *ml, struct mac_entry **e)
77 OVS_REQ_RDLOCK(ml->rwlock)
78 {
79 if (!list_is_empty(&ml->lrus)) {
80 *e = mac_entry_from_lru_node(ml->lrus.next);
81 return true;
82 } else {
83 *e = NULL;
84 return false;
85 }
86 }
87
88 static unsigned int
89 normalize_idle_time(unsigned int idle_time)
90 {
91 return (idle_time < 15 ? 15
92 : idle_time > 3600 ? 3600
93 : idle_time);
94 }
95
96 /* Creates and returns a new MAC learning table with an initial MAC aging
97 * timeout of 'idle_time' seconds and an initial maximum of MAC_DEFAULT_MAX
98 * entries. */
99 struct mac_learning *
100 mac_learning_create(unsigned int idle_time)
101 {
102 struct mac_learning *ml;
103
104 ml = xmalloc(sizeof *ml);
105 list_init(&ml->lrus);
106 hmap_init(&ml->table);
107 ml->secret = random_uint32();
108 ml->flood_vlans = NULL;
109 ml->idle_time = normalize_idle_time(idle_time);
110 ml->max_entries = MAC_DEFAULT_MAX;
111 ml->need_revalidate = false;
112 ovs_refcount_init(&ml->ref_cnt);
113 ovs_rwlock_init(&ml->rwlock);
114 return ml;
115 }
116
117 struct mac_learning *
118 mac_learning_ref(const struct mac_learning *ml_)
119 {
120 struct mac_learning *ml = CONST_CAST(struct mac_learning *, ml_);
121 if (ml) {
122 ovs_refcount_ref(&ml->ref_cnt);
123 }
124 return ml;
125 }
126
127 /* Unreferences (and possibly destroys) MAC learning table 'ml'. */
128 void
129 mac_learning_unref(struct mac_learning *ml)
130 {
131 if (ml && ovs_refcount_unref(&ml->ref_cnt) == 1) {
132 struct mac_entry *e, *next;
133
134 HMAP_FOR_EACH_SAFE (e, next, hmap_node, &ml->table) {
135 hmap_remove(&ml->table, &e->hmap_node);
136 free(e);
137 }
138 hmap_destroy(&ml->table);
139
140 bitmap_free(ml->flood_vlans);
141 ovs_rwlock_destroy(&ml->rwlock);
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 /* Sets the maximum number of entries in 'ml' to 'max_entries', adjusting it
180 * to be within a reasonable range. */
181 void
182 mac_learning_set_max_entries(struct mac_learning *ml, size_t max_entries)
183 {
184 ml->max_entries = (max_entries < 10 ? 10
185 : max_entries > 1000 * 1000 ? 1000 * 1000
186 : max_entries);
187 }
188
189 static bool
190 is_learning_vlan(const struct mac_learning *ml, uint16_t vlan)
191 {
192 return !ml->flood_vlans || !bitmap_is_set(ml->flood_vlans, vlan);
193 }
194
195 /* Returns true if 'src_mac' may be learned on 'vlan' for 'ml'.
196 * Returns false if 'ml' is NULL, if src_mac is not valid for learning, or if
197 * 'vlan' is configured on 'ml' to flood all packets. */
198 bool
199 mac_learning_may_learn(const struct mac_learning *ml,
200 const uint8_t src_mac[ETH_ADDR_LEN], uint16_t vlan)
201 {
202 return ml && is_learning_vlan(ml, vlan) && !eth_addr_is_multicast(src_mac);
203 }
204
205 /* Searches 'ml' for and returns a MAC learning entry for 'src_mac' in 'vlan',
206 * inserting a new entry if necessary. The caller must have already verified,
207 * by calling mac_learning_may_learn(), that 'src_mac' and 'vlan' are
208 * learnable.
209 *
210 * If the returned MAC entry is new (as may be determined by calling
211 * mac_entry_is_new()), then the caller must pass the new entry to
212 * mac_learning_changed(). The caller must also initialize the new entry's
213 * 'port' member. Otherwise calling those functions is at the caller's
214 * discretion. */
215 struct mac_entry *
216 mac_learning_insert(struct mac_learning *ml,
217 const uint8_t src_mac[ETH_ADDR_LEN], uint16_t vlan)
218 {
219 struct mac_entry *e;
220
221 e = mac_entry_lookup(ml, src_mac, vlan);
222 if (!e) {
223 uint32_t hash = mac_table_hash(ml, src_mac, vlan);
224
225 if (hmap_count(&ml->table) >= ml->max_entries) {
226 get_lru(ml, &e);
227 mac_learning_expire(ml, e);
228 }
229
230 e = xmalloc(sizeof *e);
231 hmap_insert(&ml->table, &e->hmap_node, hash);
232 memcpy(e->mac, src_mac, ETH_ADDR_LEN);
233 e->vlan = vlan;
234 e->grat_arp_lock = TIME_MIN;
235 e->port.p = NULL;
236 } else {
237 list_remove(&e->lru_node);
238 }
239
240 /* Mark 'e' as recently used. */
241 list_push_back(&ml->lrus, &e->lru_node);
242 e->expires = time_now() + ml->idle_time;
243
244 return e;
245 }
246
247 /* Changes 'e''s tag to a new, randomly selected one. Causes
248 * mac_learning_run() to flag for revalidation the tag that would have been
249 * previously used for this entry's MAC and VLAN (either before 'e' was
250 * inserted, if it is new, or otherwise before its port was updated.)
251 *
252 * The client should call this function after obtaining a MAC learning entry
253 * from mac_learning_insert(), if the entry is either new or if its learned
254 * port has changed. */
255 void
256 mac_learning_changed(struct mac_learning *ml)
257 {
258 COVERAGE_INC(mac_learning_learned);
259 ml->need_revalidate = true;
260 }
261
262 /* Looks up MAC 'dst' for VLAN 'vlan' in 'ml' and returns the associated MAC
263 * learning entry, if any. If 'tag' is nonnull, then the tag that associates
264 * 'dst' and 'vlan' with its currently learned port will be OR'd into
265 * '*tag'. */
266 struct mac_entry *
267 mac_learning_lookup(const struct mac_learning *ml,
268 const uint8_t dst[ETH_ADDR_LEN], uint16_t vlan)
269 {
270 if (eth_addr_is_multicast(dst)) {
271 /* No tag because the treatment of multicast destinations never
272 * changes. */
273 return NULL;
274 } else if (!is_learning_vlan(ml, vlan)) {
275 /* We don't tag this property. The set of learning VLANs changes so
276 * rarely that we revalidate every flow when it changes. */
277 return NULL;
278 } else {
279 struct mac_entry *e = mac_entry_lookup(ml, dst, vlan);
280
281 ovs_assert(e == NULL || e->port.p != NULL);
282 return e;
283 }
284 }
285
286 /* Expires 'e' from the 'ml' hash table. */
287 void
288 mac_learning_expire(struct mac_learning *ml, struct mac_entry *e)
289 {
290 hmap_remove(&ml->table, &e->hmap_node);
291 list_remove(&e->lru_node);
292 free(e);
293 }
294
295 /* Expires all the mac-learning entries in 'ml'. If not NULL, the tags in 'ml'
296 * are added to 'tags'. Otherwise the tags in 'ml' are discarded. The client
297 * is responsible for revalidating any flows that depend on 'ml', if
298 * necessary. */
299 void
300 mac_learning_flush(struct mac_learning *ml)
301 {
302 struct mac_entry *e;
303 while (get_lru(ml, &e)){
304 ml->need_revalidate = true;
305 mac_learning_expire(ml, e);
306 }
307 hmap_shrink(&ml->table);
308 }
309
310 /* Does periodic work required by 'ml'. Returns true if something changed that
311 * may require flow revalidation. */
312 bool
313 mac_learning_run(struct mac_learning *ml)
314 {
315 bool need_revalidate;
316 struct mac_entry *e;
317
318 while (get_lru(ml, &e)
319 && (hmap_count(&ml->table) > ml->max_entries
320 || time_now() >= e->expires)) {
321 COVERAGE_INC(mac_learning_expired);
322 ml->need_revalidate = true;
323 mac_learning_expire(ml, e);
324 }
325
326 need_revalidate = ml->need_revalidate;
327 ml->need_revalidate = false;
328 return need_revalidate;
329 }
330
331 void
332 mac_learning_wait(struct mac_learning *ml)
333 {
334 if (hmap_count(&ml->table) > ml->max_entries
335 || ml->need_revalidate) {
336 poll_immediate_wake();
337 } else if (!list_is_empty(&ml->lrus)) {
338 struct mac_entry *e = mac_entry_from_lru_node(ml->lrus.next);
339 poll_timer_wait_until(e->expires * 1000LL);
340 }
341 }