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