vswitchd: Make the MAC entry aging time configurable.
[sliver-openvswitch.git] / lib / mac-learning.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2012 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 "list.h"
22 #include "packets.h"
23 #include "tag.h"
24
25 struct mac_learning;
26
27 #define MAC_HASH_BITS 10
28 #define MAC_HASH_MASK (MAC_HASH_SIZE - 1)
29 #define MAC_HASH_SIZE (1u << MAC_HASH_BITS)
30
31 #define MAC_MAX 2048
32
33 /* Time, in seconds, before expiring a mac_entry due to inactivity. */
34 #define MAC_ENTRY_DEFAULT_IDLE_TIME 300
35
36 /* Time, in seconds, to lock an entry updated by a gratuitous ARP to avoid
37  * relearning based on a reflection from a bond slave. */
38 #define MAC_GRAT_ARP_LOCK_TIME 5
39
40 enum grat_arp_lock_type {
41     GRAT_ARP_LOCK_NONE,
42     GRAT_ARP_LOCK_SET,
43     GRAT_ARP_LOCK_CHECK
44 };
45
46 /* A MAC learning table entry. */
47 struct mac_entry {
48     struct list hash_node;      /* Element in a mac_learning 'table' list. */
49     struct list lru_node;       /* Element in 'lrus' or 'free' list. */
50     time_t expires;             /* Expiration time. */
51     time_t grat_arp_lock;       /* Gratuitous ARP lock expiration time. */
52     uint8_t mac[ETH_ADDR_LEN];  /* Known MAC address. */
53     uint16_t vlan;              /* VLAN tag. */
54     int port;                   /* Port on which MAC was most recently seen. */
55     tag_type tag;               /* Tag for this learning entry. */
56 };
57
58 int mac_entry_age(const struct mac_learning *, const struct mac_entry *);
59
60 /* MAC learning table. */
61 struct mac_learning {
62     struct list free;           /* Not-in-use entries. */
63     struct list lrus;           /* In-use entries, least recently used at the
64                                    front, most recently used at the back. */
65     struct list table[MAC_HASH_SIZE]; /* Hash table. */
66     struct mac_entry entries[MAC_MAX]; /* All entries. */
67     uint32_t secret;            /* Secret for randomizing hash table. */
68     unsigned long *flood_vlans; /* Bitmap of learning disabled VLANs. */
69     unsigned int idle_time;     /* Max age before deleting an entry. */
70 };
71
72 struct mac_learning *mac_learning_create(unsigned int idle_time);
73 void mac_learning_destroy(struct mac_learning *);
74 bool mac_learning_set_flood_vlans(struct mac_learning *,
75                                   unsigned long *bitmap);
76 void mac_learning_set_idle_time(struct mac_learning *, unsigned int idle_time);
77 tag_type mac_learning_learn(struct mac_learning *,
78                             const uint8_t src[ETH_ADDR_LEN], uint16_t vlan,
79                             uint16_t src_port, enum grat_arp_lock_type
80                             lock_type);
81 int mac_learning_lookup(const struct mac_learning *,
82                         const uint8_t dst[ETH_ADDR_LEN], uint16_t vlan,
83                         bool *is_grat_arp_locked);
84 int mac_learning_lookup_tag(const struct mac_learning *,
85                             const uint8_t dst[ETH_ADDR_LEN],
86                             uint16_t vlan, tag_type *tag,
87                             bool *is_grat_arp_locked);
88 void mac_learning_flush(struct mac_learning *);
89 void mac_learning_run(struct mac_learning *, struct tag_set *);
90 void mac_learning_wait(struct mac_learning *);
91
92 #endif /* mac-learning.h */