]> git.proxmox.com Git - mirror_ovs.git/blame - lib/mac-learning.h
vswitchd: Make the maximum size of MAC learning tables user-configurable.
[mirror_ovs.git] / lib / mac-learning.h
CommitLineData
064af421 1/*
e0edde6f 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
064af421 3 *
a14bc59f
BP
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:
064af421 7 *
a14bc59f
BP
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.
064af421
BP
15 */
16
17#ifndef MAC_LEARNING_H
18#define MAC_LEARNING_H 1
19
962ff3d6 20#include <time.h>
8ea45fdc 21#include "hmap.h"
962ff3d6 22#include "list.h"
064af421
BP
23#include "packets.h"
24#include "tag.h"
db8077c3 25#include "timeval.h"
064af421 26
e764773c
BP
27struct mac_learning;
28
c4069512
BP
29/* Default maximum size of a MAC learning table, in entries. */
30#define MAC_DEFAULT_MAX 2048
962ff3d6 31
321943f7 32/* Time, in seconds, before expiring a mac_entry due to inactivity. */
e764773c 33#define MAC_ENTRY_DEFAULT_IDLE_TIME 300
321943f7 34
7febb910
JG
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
38
962ff3d6
BP
39/* A MAC learning table entry. */
40struct mac_entry {
8ea45fdc 41 struct hmap_node hmap_node; /* Node in a mac_learning hmap. */
16a5d1e4 42 struct list lru_node; /* Element in 'lrus' list. */
962ff3d6 43 time_t expires; /* Expiration time. */
7febb910 44 time_t grat_arp_lock; /* Gratuitous ARP lock expiration time. */
962ff3d6
BP
45 uint8_t mac[ETH_ADDR_LEN]; /* Known MAC address. */
46 uint16_t vlan; /* VLAN tag. */
962ff3d6 47 tag_type tag; /* Tag for this learning entry. */
1bfe9681
BP
48
49 /* Learned port. */
50 union {
1648ddd7 51 void *p;
1bfe9681
BP
52 int i;
53 } port;
962ff3d6
BP
54};
55
e764773c 56int mac_entry_age(const struct mac_learning *, const struct mac_entry *);
321943f7 57
db8077c3
BP
58/* Returns true if mac_learning_insert() just created 'mac' and the caller has
59 * not yet properly initialized it. */
60static inline bool mac_entry_is_new(const struct mac_entry *mac)
61{
62 return !mac->tag;
63}
64
65/* Sets a gratuitous ARP lock on 'mac' that will expire in
66 * MAC_GRAT_ARP_LOCK_TIME seconds. */
67static inline void mac_entry_set_grat_arp_lock(struct mac_entry *mac)
68{
69 mac->grat_arp_lock = time_now() + MAC_GRAT_ARP_LOCK_TIME;
70}
71
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. */
74static inline bool mac_entry_is_grat_arp_locked(const struct mac_entry *mac)
75{
33065c43 76 return time_now() < mac->grat_arp_lock;
db8077c3
BP
77}
78
962ff3d6
BP
79/* MAC learning table. */
80struct mac_learning {
8ea45fdc 81 struct hmap table; /* Learning table. */
962ff3d6
BP
82 struct list lrus; /* In-use entries, least recently used at the
83 front, most recently used at the back. */
8f30d09a
BP
84 uint32_t secret; /* Secret for randomizing hash table. */
85 unsigned long *flood_vlans; /* Bitmap of learning disabled VLANs. */
e764773c 86 unsigned int idle_time; /* Max age before deleting an entry. */
c4069512 87 size_t max_entries; /* Max number of learned MACs. */
962ff3d6
BP
88};
89
db8077c3 90/* Basics. */
e764773c 91struct mac_learning *mac_learning_create(unsigned int idle_time);
064af421 92void mac_learning_destroy(struct mac_learning *);
db8077c3
BP
93
94void mac_learning_run(struct mac_learning *, struct tag_set *);
95void mac_learning_wait(struct mac_learning *);
96
97/* Configuration. */
8f30d09a 98bool mac_learning_set_flood_vlans(struct mac_learning *,
2a4ae635 99 const unsigned long *bitmap);
e764773c 100void mac_learning_set_idle_time(struct mac_learning *, unsigned int idle_time);
c4069512 101void mac_learning_set_max_entries(struct mac_learning *, size_t max_entries);
db8077c3
BP
102
103/* Learning. */
104bool mac_learning_may_learn(const struct mac_learning *,
105 const uint8_t src_mac[ETH_ADDR_LEN],
106 uint16_t vlan);
107struct mac_entry *mac_learning_insert(struct mac_learning *,
108 const uint8_t src[ETH_ADDR_LEN],
109 uint16_t vlan);
110tag_type mac_learning_changed(struct mac_learning *, struct mac_entry *);
111
112/* Lookup. */
113struct mac_entry *mac_learning_lookup(const struct mac_learning *,
114 const uint8_t dst[ETH_ADDR_LEN],
115 uint16_t vlan, tag_type *);
116
117/* Flushing. */
356180a8 118void mac_learning_expire(struct mac_learning *, struct mac_entry *);
d0040604 119void mac_learning_flush(struct mac_learning *, struct tag_set *);
064af421
BP
120
121#endif /* mac-learning.h */