mac-learning: Make the mac-learning module thread safe.
[sliver-openvswitch.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 <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 "tag.h"
29 #include "timeval.h"
30 #include "unaligned.h"
31 #include "util.h"
32 #include "vlan-bitmap.h"
33 #include "vlog.h"
34
35 VLOG_DEFINE_THIS_MODULE(mac_learning);
36
37 COVERAGE_DEFINE(mac_learning_learned);
38 COVERAGE_DEFINE(mac_learning_expired);
39
40 /* Returns the number of seconds since 'e' (within 'ml') was last learned. */
41 int
42 mac_entry_age(const struct mac_learning *ml, const struct mac_entry *e)
43 {
44     time_t remaining = e->expires - time_now();
45     return ml->idle_time - remaining;
46 }
47
48 static uint32_t
49 mac_table_hash(const struct mac_learning *ml, const uint8_t mac[ETH_ADDR_LEN],
50                uint16_t vlan)
51 {
52     unsigned int mac1 = get_unaligned_u32(ALIGNED_CAST(uint32_t *, mac));
53     unsigned int mac2 = get_unaligned_u16(ALIGNED_CAST(uint16_t *, mac + 4));
54     return hash_3words(mac1, mac2 | (vlan << 16), ml->secret);
55 }
56
57 static struct mac_entry *
58 mac_entry_from_lru_node(struct list *list)
59 {
60     return CONTAINER_OF(list, struct mac_entry, lru_node);
61 }
62
63 /* Returns a tag that represents that 'mac' is on an unknown port in 'vlan'.
64  * (When we learn where 'mac' is in 'vlan', this allows flows that were
65  * flooded to be revalidated.) */
66 static tag_type
67 make_unknown_mac_tag(const struct mac_learning *ml,
68                      const uint8_t mac[ETH_ADDR_LEN], uint16_t vlan)
69 {
70     return tag_create_deterministic(mac_table_hash(ml, mac, vlan));
71 }
72
73 static struct mac_entry *
74 mac_entry_lookup(const struct mac_learning *ml,
75                  const uint8_t mac[ETH_ADDR_LEN], uint16_t vlan)
76 {
77     struct mac_entry *e;
78
79     HMAP_FOR_EACH_WITH_HASH (e, hmap_node, mac_table_hash(ml, mac, vlan),
80                              &ml->table) {
81         if (e->vlan == vlan && eth_addr_equals(e->mac, mac)) {
82             return e;
83         }
84     }
85     return NULL;
86 }
87
88 /* If the LRU list is not empty, stores the least-recently-used entry in '*e'
89  * and returns true.  Otherwise, if the LRU list is empty, stores NULL in '*e'
90  * and return false. */
91 static bool
92 get_lru(struct mac_learning *ml, struct mac_entry **e)
93     OVS_REQ_RDLOCK(ml->rwlock)
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 and an initial maximum of MAC_DEFAULT_MAX
114  * entries. */
115 struct mac_learning *
116 mac_learning_create(unsigned int idle_time)
117 {
118     struct mac_learning *ml;
119
120     ml = xmalloc(sizeof *ml);
121     list_init(&ml->lrus);
122     hmap_init(&ml->table);
123     ml->secret = random_uint32();
124     ml->flood_vlans = NULL;
125     ml->idle_time = normalize_idle_time(idle_time);
126     ml->max_entries = MAC_DEFAULT_MAX;
127     tag_set_init(&ml->tags);
128     atomic_init(&ml->ref_cnt, 1);
129     ovs_rwlock_init(&ml->rwlock);
130     return ml;
131 }
132
133 struct mac_learning *
134 mac_learning_ref(const struct mac_learning *ml_)
135 {
136     struct mac_learning *ml = CONST_CAST(struct mac_learning *, ml_);
137     if (ml) {
138         int orig;
139         atomic_add(&ml->ref_cnt, 1, &orig);
140         ovs_assert(orig > 0);
141     }
142     return ml;
143 }
144
145 /* Unreferences (and possibly destroys) MAC learning table 'ml'. */
146 void
147 mac_learning_unref(struct mac_learning *ml)
148 {
149     int orig;
150
151     if (!ml) {
152         return;
153     }
154
155     atomic_sub(&ml->ref_cnt, 1, &orig);
156     ovs_assert(orig > 0);
157     if (orig == 1) {
158         struct mac_entry *e, *next;
159
160         HMAP_FOR_EACH_SAFE (e, next, hmap_node, &ml->table) {
161             hmap_remove(&ml->table, &e->hmap_node);
162             free(e);
163         }
164         hmap_destroy(&ml->table);
165
166         bitmap_free(ml->flood_vlans);
167         ovs_rwlock_destroy(&ml->rwlock);
168         free(ml);
169     }
170 }
171
172 /* Provides a bitmap of VLANs which have learning disabled, that is, VLANs on
173  * which all packets are flooded.  Returns true if the set has changed from the
174  * previous value. */
175 bool
176 mac_learning_set_flood_vlans(struct mac_learning *ml,
177                              const unsigned long *bitmap)
178 {
179     if (vlan_bitmap_equal(ml->flood_vlans, bitmap)) {
180         return false;
181     } else {
182         bitmap_free(ml->flood_vlans);
183         ml->flood_vlans = vlan_bitmap_clone(bitmap);
184         return true;
185     }
186 }
187
188 /* Changes the MAC aging timeout of 'ml' to 'idle_time' seconds. */
189 void
190 mac_learning_set_idle_time(struct mac_learning *ml, unsigned int idle_time)
191 {
192     idle_time = normalize_idle_time(idle_time);
193     if (idle_time != ml->idle_time) {
194         struct mac_entry *e;
195         int delta;
196
197         delta = (int) idle_time - (int) ml->idle_time;
198         LIST_FOR_EACH (e, lru_node, &ml->lrus) {
199             e->expires += delta;
200         }
201         ml->idle_time = idle_time;
202     }
203 }
204
205 /* Sets the maximum number of entries in 'ml' to 'max_entries', adjusting it
206  * to be within a reasonable range. */
207 void
208 mac_learning_set_max_entries(struct mac_learning *ml, size_t max_entries)
209 {
210     ml->max_entries = (max_entries < 10 ? 10
211                        : max_entries > 1000 * 1000 ? 1000 * 1000
212                        : max_entries);
213 }
214
215 static bool
216 is_learning_vlan(const struct mac_learning *ml, uint16_t vlan)
217 {
218     return !ml->flood_vlans || !bitmap_is_set(ml->flood_vlans, vlan);
219 }
220
221 /* Returns true if 'src_mac' may be learned on 'vlan' for 'ml'.
222  * Returns false if 'ml' is NULL, if src_mac is not valid for learning, or if
223  * 'vlan' is configured on 'ml' to flood all packets. */
224 bool
225 mac_learning_may_learn(const struct mac_learning *ml,
226                        const uint8_t src_mac[ETH_ADDR_LEN], uint16_t vlan)
227 {
228     return ml && is_learning_vlan(ml, vlan) && !eth_addr_is_multicast(src_mac);
229 }
230
231 /* Searches 'ml' for and returns a MAC learning entry for 'src_mac' in 'vlan',
232  * inserting a new entry if necessary.  The caller must have already verified,
233  * by calling mac_learning_may_learn(), that 'src_mac' and 'vlan' are
234  * learnable.
235  *
236  * If the returned MAC entry is new (as may be determined by calling
237  * mac_entry_is_new()), then the caller must pass the new entry to
238  * mac_learning_changed().  The caller must also initialize the new entry's
239  * 'port' member.  Otherwise calling those functions is at the caller's
240  * discretion. */
241 struct mac_entry *
242 mac_learning_insert(struct mac_learning *ml,
243                     const uint8_t src_mac[ETH_ADDR_LEN], uint16_t vlan)
244 {
245     struct mac_entry *e;
246
247     e = mac_entry_lookup(ml, src_mac, vlan);
248     if (!e) {
249         uint32_t hash = mac_table_hash(ml, src_mac, vlan);
250
251         if (hmap_count(&ml->table) >= ml->max_entries) {
252             get_lru(ml, &e);
253             mac_learning_expire(ml, e);
254         }
255
256         e = xmalloc(sizeof *e);
257         hmap_insert(&ml->table, &e->hmap_node, hash);
258         memcpy(e->mac, src_mac, ETH_ADDR_LEN);
259         e->vlan = vlan;
260         e->tag = 0;
261         e->grat_arp_lock = TIME_MIN;
262     } else {
263         list_remove(&e->lru_node);
264     }
265
266     /* Mark 'e' as recently used. */
267     list_push_back(&ml->lrus, &e->lru_node);
268     e->expires = time_now() + ml->idle_time;
269
270     return e;
271 }
272
273 /* Changes 'e''s tag to a new, randomly selected one.  Causes
274  * mac_learning_run() to flag for revalidation the tag that would have been
275  * previously used for this entry's MAC and VLAN (either before 'e' was
276  * inserted, if it is new, or otherwise before its port was updated.)
277  *
278  * The client should call this function after obtaining a MAC learning entry
279  * from mac_learning_insert(), if the entry is either new or if its learned
280  * port has changed. */
281 void
282 mac_learning_changed(struct mac_learning *ml, struct mac_entry *e)
283 {
284     tag_type tag = e->tag ? e->tag : make_unknown_mac_tag(ml, e->mac, e->vlan);
285
286     COVERAGE_INC(mac_learning_learned);
287
288     e->tag = tag_create_random();
289     tag_set_add(&ml->tags, tag);
290 }
291
292 /* Looks up MAC 'dst' for VLAN 'vlan' in 'ml' and returns the associated MAC
293  * learning entry, if any.  If 'tag' is nonnull, then the tag that associates
294  * 'dst' and 'vlan' with its currently learned port will be OR'd into
295  * '*tag'. */
296 struct mac_entry *
297 mac_learning_lookup(const struct mac_learning *ml,
298                     const uint8_t dst[ETH_ADDR_LEN], uint16_t vlan,
299                     tag_type *tag)
300 {
301     if (eth_addr_is_multicast(dst)) {
302         /* No tag because the treatment of multicast destinations never
303          * changes. */
304         return NULL;
305     } else if (!is_learning_vlan(ml, vlan)) {
306         /* We don't tag this property.  The set of learning VLANs changes so
307          * rarely that we revalidate every flow when it changes. */
308         return NULL;
309     } else {
310         struct mac_entry *e = mac_entry_lookup(ml, dst, vlan);
311
312         ovs_assert(e == NULL || e->tag != 0);
313         if (tag) {
314             /* Tag either the learned port or the lack thereof. */
315             *tag |= e ? e->tag : make_unknown_mac_tag(ml, dst, vlan);
316         }
317         return e;
318     }
319 }
320
321 /* Expires 'e' from the 'ml' hash table. */
322 void
323 mac_learning_expire(struct mac_learning *ml, struct mac_entry *e)
324 {
325     hmap_remove(&ml->table, &e->hmap_node);
326     list_remove(&e->lru_node);
327     free(e);
328 }
329
330 /* Expires all the mac-learning entries in 'ml'.  If not NULL, the tags in 'ml'
331  * are added to 'tags'.  Otherwise the tags in 'ml' are discarded.  The client
332  * is responsible for revalidating any flows that depend on 'ml', if
333  * necessary. */
334 void
335 mac_learning_flush(struct mac_learning *ml, struct tag_set *tags)
336 {
337     struct mac_entry *e;
338     while (get_lru(ml, &e)){
339         if (tags) {
340             tag_set_add(tags, e->tag);
341         }
342         mac_learning_expire(ml, e);
343     }
344     hmap_shrink(&ml->table);
345 }
346
347 void
348 mac_learning_run(struct mac_learning *ml, struct tag_set *set)
349 {
350     struct mac_entry *e;
351
352     if (set) {
353         tag_set_union(set, &ml->tags);
354     }
355     tag_set_init(&ml->tags);
356
357     while (get_lru(ml, &e)
358            && (hmap_count(&ml->table) > ml->max_entries
359                || time_now() >= e->expires)) {
360         COVERAGE_INC(mac_learning_expired);
361         if (set) {
362             tag_set_add(set, e->tag);
363         }
364         mac_learning_expire(ml, e);
365     }
366 }
367
368 void
369 mac_learning_wait(struct mac_learning *ml)
370 {
371     if (hmap_count(&ml->table) > ml->max_entries
372         || !tag_set_is_empty(&ml->tags)) {
373         poll_immediate_wake();
374     } else if (!list_is_empty(&ml->lrus)) {
375         struct mac_entry *e = mac_entry_from_lru_node(ml->lrus.next);
376         poll_timer_wait_until(e->expires * 1000LL);
377     }
378 }