mac-learning: Convert to hmap.
[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 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(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 /* Creates and returns a new MAC learning table. */
102 struct mac_learning *
103 mac_learning_create(void)
104 {
105     struct mac_learning *ml;
106     int i;
107
108     ml = xmalloc(sizeof *ml);
109     list_init(&ml->lrus);
110     list_init(&ml->free);
111     hmap_init(&ml->table);
112     for (i = 0; i < MAC_MAX; i++) {
113         struct mac_entry *s = &ml->entries[i];
114         list_push_front(&ml->free, &s->lru_node);
115     }
116     ml->secret = random_uint32();
117     ml->flood_vlans = NULL;
118     return ml;
119 }
120
121 /* Destroys MAC learning table 'ml'. */
122 void
123 mac_learning_destroy(struct mac_learning *ml)
124 {
125     if (ml) {
126         hmap_destroy(&ml->table);
127         bitmap_free(ml->flood_vlans);
128         free(ml);
129     }
130 }
131
132 /* Provides a bitmap of VLANs which have learning disabled, that is, VLANs on
133  * which all packets are flooded.  Returns true if the set has changed from the
134  * previous value. */
135 bool
136 mac_learning_set_flood_vlans(struct mac_learning *ml,
137                              const unsigned long *bitmap)
138 {
139     if (vlan_bitmap_equal(ml->flood_vlans, bitmap)) {
140         return false;
141     } else {
142         bitmap_free(ml->flood_vlans);
143         ml->flood_vlans = vlan_bitmap_clone(bitmap);
144         return true;
145     }
146 }
147
148 static bool
149 is_learning_vlan(const struct mac_learning *ml, uint16_t vlan)
150 {
151     return vlan_bitmap_contains(ml->flood_vlans, vlan);
152 }
153
154 /* Returns true if 'src_mac' may be learned on 'vlan' for 'ml'.
155  * Returns false if 'ml' is NULL, if src_mac is not valid for learning, or if
156  * 'vlan' is configured on 'ml' to flood all packets. */
157 bool
158 mac_learning_may_learn(const struct mac_learning *ml,
159                        const uint8_t src_mac[ETH_ADDR_LEN], uint16_t vlan)
160 {
161     return ml && is_learning_vlan(ml, vlan) && !eth_addr_is_multicast(src_mac);
162 }
163
164 /* Searches 'ml' for and returns a MAC learning entry for 'src_mac' in 'vlan',
165  * inserting a new entry if necessary.  The caller must have already verified,
166  * by calling mac_learning_may_learn(), that 'src_mac' and 'vlan' are
167  * learnable.
168  *
169  * If the returned MAC entry is new (as may be determined by calling
170  * mac_entry_is_new()), then the caller must pass the new entry to
171  * mac_learning_changed().  The caller must also initialize the new entry's
172  * 'port' member.  Otherwise calling those functions is at the caller's
173  * discretion. */
174 struct mac_entry *
175 mac_learning_insert(struct mac_learning *ml,
176                     const uint8_t src_mac[ETH_ADDR_LEN], uint16_t vlan)
177 {
178     struct mac_entry *e;
179
180     e = mac_entry_lookup(ml, src_mac, vlan);
181     if (!e) {
182         if (!list_is_empty(&ml->free)) {
183             e = mac_entry_from_lru_node(ml->free.next);
184         } else {
185             e = mac_entry_from_lru_node(ml->lrus.next);
186             hmap_remove(&ml->table, &e->hmap_node);
187         }
188         hmap_insert(&ml->table, &e->hmap_node, mac_table_hash(src_mac, vlan));
189         memcpy(e->mac, src_mac, ETH_ADDR_LEN);
190         e->vlan = vlan;
191         e->tag = 0;
192         e->grat_arp_lock = TIME_MIN;
193     }
194
195     /* Mark 'e' as recently used. */
196     list_remove(&e->lru_node);
197     list_push_back(&ml->lrus, &e->lru_node);
198     e->expires = time_now() + MAC_ENTRY_IDLE_TIME;
199
200     return e;
201 }
202
203 /* Changes 'e''s tag to a new, randomly selected one, and returns the tag that
204  * would have been previously used for this entry's MAC and VLAN (either before
205  * 'e' was inserted, if it is new, or otherwise before its port was updated.)
206  *
207  * The client should call this function after obtaining a MAC learning entry
208  * from mac_learning_insert(), if the entry is either new or if its learned
209  * port has changed. */
210 tag_type
211 mac_learning_changed(struct mac_learning *ml, struct mac_entry *e)
212 {
213     tag_type old_tag = e->tag;
214
215     COVERAGE_INC(mac_learning_learned);
216
217     e->tag = tag_create_random();
218     return old_tag ? old_tag : make_unknown_mac_tag(ml, e->mac, e->vlan);
219 }
220
221 /* Looks up MAC 'dst' for VLAN 'vlan' in 'ml' and returns the associated MAC
222  * learning entry, if any.  If 'tag' is nonnull, then the tag that associates
223  * 'dst' and 'vlan' with its currently learned port will be OR'd into
224  * '*tag'. */
225 struct mac_entry *
226 mac_learning_lookup(const struct mac_learning *ml,
227                     const uint8_t dst[ETH_ADDR_LEN], uint16_t vlan,
228                     tag_type *tag)
229 {
230     if (eth_addr_is_multicast(dst)) {
231         /* No tag because the treatment of multicast destinations never
232          * changes. */
233         return NULL;
234     } else if (!is_learning_vlan(ml, vlan)) {
235         /* We don't tag this property.  The set of learning VLANs changes so
236          * rarely that we revalidate every flow when it changes. */
237         return NULL;
238     } else {
239         struct mac_entry *e = mac_entry_lookup(ml, dst, vlan);
240
241         assert(e == NULL || e->tag != 0);
242         if (tag) {
243             /* Tag either the learned port or the lack thereof. */
244             *tag |= e ? e->tag : make_unknown_mac_tag(ml, dst, vlan);
245         }
246         return e;
247     }
248 }
249
250 /* Expires 'e' from the 'ml' hash table.  'e' must not already be on the free
251  * list. */
252 void
253 mac_learning_expire(struct mac_learning *ml, struct mac_entry *e)
254 {
255     hmap_remove(&ml->table, &e->hmap_node);
256     list_remove(&e->lru_node);
257     list_push_front(&ml->free, &e->lru_node);
258 }
259
260 /* Expires all the mac-learning entries in 'ml'.  The tags in 'ml' are
261  * discarded, so the client is responsible for revalidating any flows that
262  * depend on 'ml', if necessary. */
263 void
264 mac_learning_flush(struct mac_learning *ml)
265 {
266     struct mac_entry *e;
267     while (get_lru(ml, &e)){
268         mac_learning_expire(ml, e);
269     }
270 }
271
272 void
273 mac_learning_run(struct mac_learning *ml, struct tag_set *set)
274 {
275     struct mac_entry *e;
276     while (get_lru(ml, &e) && time_now() >= e->expires) {
277         COVERAGE_INC(mac_learning_expired);
278         if (set) {
279             tag_set_add(set, e->tag);
280         }
281         mac_learning_expire(ml, e);
282     }
283 }
284
285 void
286 mac_learning_wait(struct mac_learning *ml)
287 {
288     if (!list_is_empty(&ml->lrus)) {
289         struct mac_entry *e = mac_entry_from_lru_node(ml->lrus.next);
290         poll_timer_wait_until(e->expires * 1000LL);
291     }
292 }