2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef MAC_LEARNING_H
18 #define MAC_LEARNING_H 1
26 #define MAC_HASH_BITS 10
27 #define MAC_HASH_MASK (MAC_HASH_SIZE - 1)
28 #define MAC_HASH_SIZE (1u << MAC_HASH_BITS)
32 /* Time, in seconds, before expiring a mac_entry due to inactivity. */
33 #define MAC_ENTRY_IDLE_TIME 60
35 /* Time, in seconds, to lock an entry updated by a gratuitous ARP to avoid
36 * relearning based on a reflection from a bond slave. */
37 #define MAC_GRAT_ARP_LOCK_TIME 5
39 /* A MAC learning table entry. */
41 struct list hash_node; /* Element in a mac_learning 'table' list. */
42 struct list lru_node; /* Element in 'lrus' or 'free' list. */
43 time_t expires; /* Expiration time. */
44 time_t grat_arp_lock; /* Gratuitous ARP lock expiration time. */
45 uint8_t mac[ETH_ADDR_LEN]; /* Known MAC address. */
46 uint16_t vlan; /* VLAN tag. */
47 tag_type tag; /* Tag for this learning entry. */
56 int mac_entry_age(const struct mac_entry *);
58 /* Returns true if mac_learning_insert() just created 'mac' and the caller has
59 * not yet properly initialized it. */
60 static inline bool mac_entry_is_new(const struct mac_entry *mac)
65 /* Sets a gratuitous ARP lock on 'mac' that will expire in
66 * MAC_GRAT_ARP_LOCK_TIME seconds. */
67 static inline void mac_entry_set_grat_arp_lock(struct mac_entry *mac)
69 mac->grat_arp_lock = time_now() + MAC_GRAT_ARP_LOCK_TIME;
72 /* Returns true if a gratuitous ARP lock is in effect on 'mac', false if none
73 * has ever been asserted or if it has expired. */
74 static inline bool mac_entry_is_grat_arp_locked(const struct mac_entry *mac)
76 return time_now() < mac->grat_arp_lock;
79 /* MAC learning table. */
81 struct list free; /* Not-in-use entries. */
82 struct list lrus; /* In-use entries, least recently used at the
83 front, most recently used at the back. */
84 struct list table[MAC_HASH_SIZE]; /* Hash table. */
85 struct mac_entry entries[MAC_MAX]; /* All entries. */
86 uint32_t secret; /* Secret for randomizing hash table. */
87 unsigned long *flood_vlans; /* Bitmap of learning disabled VLANs. */
91 struct mac_learning *mac_learning_create(void);
92 void mac_learning_destroy(struct mac_learning *);
94 void mac_learning_run(struct mac_learning *, struct tag_set *);
95 void mac_learning_wait(struct mac_learning *);
98 bool mac_learning_set_flood_vlans(struct mac_learning *,
99 unsigned long *bitmap);
102 bool mac_learning_may_learn(const struct mac_learning *,
103 const uint8_t src_mac[ETH_ADDR_LEN],
105 struct mac_entry *mac_learning_insert(struct mac_learning *,
106 const uint8_t src[ETH_ADDR_LEN],
108 tag_type mac_learning_changed(struct mac_learning *, struct mac_entry *);
111 struct mac_entry *mac_learning_lookup(const struct mac_learning *,
112 const uint8_t dst[ETH_ADDR_LEN],
113 uint16_t vlan, tag_type *);
116 void mac_learning_expire(struct mac_learning *, struct mac_entry *);
117 void mac_learning_flush(struct mac_learning *);
119 #endif /* mac-learning.h */