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