mac-learning: Make the mac-learning module thread safe.
[sliver-openvswitch.git] / lib / mac-learning.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
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 "ovs-atomic.h"
24 #include "ovs-thread.h"
25 #include "packets.h"
26 #include "tag.h"
27 #include "timeval.h"
28
29 struct mac_learning;
30
31 /* Default maximum size of a MAC learning table, in entries. */
32 #define MAC_DEFAULT_MAX 2048
33
34 /* Time, in seconds, before expiring a mac_entry due to inactivity. */
35 #define MAC_ENTRY_DEFAULT_IDLE_TIME 300
36
37 /* Time, in seconds, to lock an entry updated by a gratuitous ARP to avoid
38  * relearning based on a reflection from a bond slave. */
39 #define MAC_GRAT_ARP_LOCK_TIME 5
40
41 /* A MAC learning table entry.
42  * Guarded by owning 'mac_learning''s rwlock */
43 struct mac_entry {
44     struct hmap_node hmap_node; /* Node in a mac_learning hmap. */
45     time_t expires;             /* Expiration time. */
46     time_t grat_arp_lock;       /* Gratuitous ARP lock expiration time. */
47     uint8_t mac[ETH_ADDR_LEN];  /* Known MAC address. */
48     uint16_t vlan;              /* VLAN tag. */
49     tag_type tag;               /* Tag for this learning entry. */
50
51     /* The following are marked guarded to prevent users from iterating over or
52      * accessing a mac_entry without hodling the parent mac_learning rwlock. */
53     struct list lru_node OVS_GUARDED; /* Element in 'lrus' list. */
54
55     /* Learned port. */
56     union {
57         void *p;
58         ofp_port_t ofp_port;
59     } port OVS_GUARDED;
60 };
61
62 /* Returns true if mac_learning_insert() just created 'mac' and the caller has
63  * not yet properly initialized it. */
64 static inline bool mac_entry_is_new(const struct mac_entry *mac)
65 {
66     return !mac->tag;
67 }
68
69 /* Sets a gratuitous ARP lock on 'mac' that will expire in
70  * MAC_GRAT_ARP_LOCK_TIME seconds. */
71 static inline void mac_entry_set_grat_arp_lock(struct mac_entry *mac)
72 {
73     mac->grat_arp_lock = time_now() + MAC_GRAT_ARP_LOCK_TIME;
74 }
75
76 /* Returns true if a gratuitous ARP lock is in effect on 'mac', false if none
77  * has ever been asserted or if it has expired. */
78 static inline bool mac_entry_is_grat_arp_locked(const struct mac_entry *mac)
79 {
80     return time_now() < mac->grat_arp_lock;
81 }
82
83 /* MAC learning table. */
84 struct mac_learning {
85     struct hmap table;          /* Learning table. */
86     struct list lrus OVS_GUARDED; /* In-use entries, least recently used at the
87                                      front, most recently used at the back. */
88     uint32_t secret;            /* Secret for randomizing hash table. */
89     unsigned long *flood_vlans; /* Bitmap of learning disabled VLANs. */
90     unsigned int idle_time;     /* Max age before deleting an entry. */
91     size_t max_entries;         /* Max number of learned MACs. */
92     struct tag_set tags;        /* Tags which have changed. */
93     atomic_int ref_cnt;
94     struct ovs_rwlock rwlock;
95 };
96
97 int mac_entry_age(const struct mac_learning *ml, const struct mac_entry *e)
98     OVS_REQ_RDLOCK(ml->rwlock);
99
100 /* Basics. */
101 struct mac_learning *mac_learning_create(unsigned int idle_time);
102 struct mac_learning *mac_learning_ref(const struct mac_learning *);
103 void mac_learning_unref(struct mac_learning *);
104
105 void mac_learning_run(struct mac_learning *ml, struct tag_set *)
106     OVS_REQ_WRLOCK(ml->rwlock);
107 void mac_learning_wait(struct mac_learning *ml)
108     OVS_REQ_RDLOCK(ml->rwlock);
109
110 /* Configuration. */
111 bool mac_learning_set_flood_vlans(struct mac_learning *ml,
112                                   const unsigned long *bitmap)
113     OVS_REQ_WRLOCK(ml->rwlock);
114 void mac_learning_set_idle_time(struct mac_learning *ml,
115                                 unsigned int idle_time)
116     OVS_REQ_WRLOCK(ml->rwlock);
117 void mac_learning_set_max_entries(struct mac_learning *ml, size_t max_entries)
118     OVS_REQ_WRLOCK(ml->rwlock);
119
120 /* Learning. */
121 bool mac_learning_may_learn(const struct mac_learning *ml,
122                             const uint8_t src_mac[ETH_ADDR_LEN],
123                             uint16_t vlan)
124     OVS_REQ_RDLOCK(ml->rwlock);
125 struct mac_entry *mac_learning_insert(struct mac_learning *ml,
126                                       const uint8_t src[ETH_ADDR_LEN],
127                                       uint16_t vlan)
128     OVS_REQ_WRLOCK(ml->rwlock);
129 void mac_learning_changed(struct mac_learning *ml, struct mac_entry *e)
130     OVS_REQ_WRLOCK(ml->rwlock);
131
132 /* Lookup. */
133 struct mac_entry *mac_learning_lookup(const struct mac_learning *ml,
134                                       const uint8_t dst[ETH_ADDR_LEN],
135                                       uint16_t vlan, tag_type *)
136     OVS_REQ_RDLOCK(ml->rwlock);
137
138 /* Flushing. */
139 void mac_learning_expire(struct mac_learning *ml, struct mac_entry *e)
140     OVS_REQ_WRLOCK(ml->rwlock);
141 void mac_learning_flush(struct mac_learning *ml, struct tag_set *)
142     OVS_REQ_WRLOCK(ml->rwlock);
143
144 #endif /* mac-learning.h */