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