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