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