mac-learning: Simplify memory management.
[sliver-openvswitch.git] / lib / mac-learning.h
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 #ifndef MAC_LEARNING_H
18 #define MAC_LEARNING_H 1
19
20 #include <time.h>
21 #include "hmap.h"
22 #include "list.h"
23 #include "packets.h"
24 #include "tag.h"
25 #include "timeval.h"
26
27 #define MAC_MAX 2048
28
29 /* Time, in seconds, before expiring a mac_entry due to inactivity. */
30 #define MAC_ENTRY_IDLE_TIME 60
31
32 /* Time, in seconds, to lock an entry updated by a gratuitous ARP to avoid
33  * relearning based on a reflection from a bond slave. */
34 #define MAC_GRAT_ARP_LOCK_TIME 5
35
36 /* A MAC learning table entry. */
37 struct mac_entry {
38     struct hmap_node hmap_node; /* Node in a mac_learning hmap. */
39     struct list lru_node;       /* Element in 'lrus' list. */
40     time_t expires;             /* Expiration time. */
41     time_t grat_arp_lock;       /* Gratuitous ARP lock expiration time. */
42     uint8_t mac[ETH_ADDR_LEN];  /* Known MAC address. */
43     uint16_t vlan;              /* VLAN tag. */
44     tag_type tag;               /* Tag for this learning entry. */
45
46     /* Learned port. */
47     union {
48         void *p;
49         int i;
50     } port;
51 };
52
53 int mac_entry_age(const struct mac_entry *);
54
55 /* Returns true if mac_learning_insert() just created 'mac' and the caller has
56  * not yet properly initialized it. */
57 static inline bool mac_entry_is_new(const struct mac_entry *mac)
58 {
59     return !mac->tag;
60 }
61
62 /* Sets a gratuitous ARP lock on 'mac' that will expire in
63  * MAC_GRAT_ARP_LOCK_TIME seconds. */
64 static inline void mac_entry_set_grat_arp_lock(struct mac_entry *mac)
65 {
66     mac->grat_arp_lock = time_now() + MAC_GRAT_ARP_LOCK_TIME;
67 }
68
69 /* Returns true if a gratuitous ARP lock is in effect on 'mac', false if none
70  * has ever been asserted or if it has expired. */
71 static inline bool mac_entry_is_grat_arp_locked(const struct mac_entry *mac)
72 {
73     return time_now() < mac->grat_arp_lock;
74 }
75
76 /* MAC learning table. */
77 struct mac_learning {
78     struct hmap table;          /* Learning table. */
79     struct list lrus;           /* In-use entries, least recently used at the
80                                    front, most recently used at the back. */
81     uint32_t secret;            /* Secret for randomizing hash table. */
82     unsigned long *flood_vlans; /* Bitmap of learning disabled VLANs. */
83 };
84
85 /* Basics. */
86 struct mac_learning *mac_learning_create(void);
87 void mac_learning_destroy(struct mac_learning *);
88
89 void mac_learning_run(struct mac_learning *, struct tag_set *);
90 void mac_learning_wait(struct mac_learning *);
91
92 /* Configuration. */
93 bool mac_learning_set_flood_vlans(struct mac_learning *,
94                                   const unsigned long *bitmap);
95
96 /* Learning. */
97 bool mac_learning_may_learn(const struct mac_learning *,
98                             const uint8_t src_mac[ETH_ADDR_LEN],
99                             uint16_t vlan);
100 struct mac_entry *mac_learning_insert(struct mac_learning *,
101                                       const uint8_t src[ETH_ADDR_LEN],
102                                       uint16_t vlan);
103 tag_type mac_learning_changed(struct mac_learning *, struct mac_entry *);
104
105 /* Lookup. */
106 struct mac_entry *mac_learning_lookup(const struct mac_learning *,
107                                       const uint8_t dst[ETH_ADDR_LEN],
108                                       uint16_t vlan, tag_type *);
109
110 /* Flushing. */
111 void mac_learning_expire(struct mac_learning *, struct mac_entry *);
112 void mac_learning_flush(struct mac_learning *);
113
114 #endif /* mac-learning.h */