]> git.proxmox.com Git - ovs.git/blame - lib/mac-learning.h
datapath-windows: Avoid BSOD when switch context is NULL
[ovs.git] / lib / mac-learning.h
CommitLineData
064af421 1/*
37bec3d3 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 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"
509c0149
EJ
23#include "ovs-atomic.h"
24#include "ovs-thread.h"
064af421 25#include "packets.h"
db8077c3 26#include "timeval.h"
064af421 27
e764773c
BP
28struct mac_learning;
29
c4069512
BP
30/* Default maximum size of a MAC learning table, in entries. */
31#define MAC_DEFAULT_MAX 2048
962ff3d6 32
321943f7 33/* Time, in seconds, before expiring a mac_entry due to inactivity. */
e764773c 34#define MAC_ENTRY_DEFAULT_IDLE_TIME 300
321943f7 35
7febb910
JG
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
509c0149
EJ
40/* A MAC learning table entry.
41 * Guarded by owning 'mac_learning''s rwlock */
962ff3d6 42struct mac_entry {
8ea45fdc 43 struct hmap_node hmap_node; /* Node in a mac_learning hmap. */
962ff3d6 44 time_t expires; /* Expiration time. */
7febb910 45 time_t grat_arp_lock; /* Gratuitous ARP lock expiration time. */
962ff3d6
BP
46 uint8_t mac[ETH_ADDR_LEN]; /* Known MAC address. */
47 uint16_t vlan; /* VLAN tag. */
1bfe9681 48
509c0149
EJ
49 /* The following are marked guarded to prevent users from iterating over or
50 * accessing a mac_entry without hodling the parent mac_learning rwlock. */
51 struct list lru_node OVS_GUARDED; /* Element in 'lrus' list. */
52
1bfe9681
BP
53 /* Learned port. */
54 union {
1648ddd7 55 void *p;
4e022ec0 56 ofp_port_t ofp_port;
509c0149 57 } port OVS_GUARDED;
962ff3d6
BP
58};
59
db8077c3
BP
60/* Sets a gratuitous ARP lock on 'mac' that will expire in
61 * MAC_GRAT_ARP_LOCK_TIME seconds. */
62static inline void mac_entry_set_grat_arp_lock(struct mac_entry *mac)
63{
64 mac->grat_arp_lock = time_now() + MAC_GRAT_ARP_LOCK_TIME;
65}
66
67/* Returns true if a gratuitous ARP lock is in effect on 'mac', false if none
68 * has ever been asserted or if it has expired. */
69static inline bool mac_entry_is_grat_arp_locked(const struct mac_entry *mac)
70{
33065c43 71 return time_now() < mac->grat_arp_lock;
db8077c3
BP
72}
73
962ff3d6
BP
74/* MAC learning table. */
75struct mac_learning {
8ea45fdc 76 struct hmap table; /* Learning table. */
509c0149
EJ
77 struct list lrus OVS_GUARDED; /* In-use entries, least recently used at the
78 front, most recently used at the back. */
8f30d09a
BP
79 uint32_t secret; /* Secret for randomizing hash table. */
80 unsigned long *flood_vlans; /* Bitmap of learning disabled VLANs. */
e764773c 81 unsigned int idle_time; /* Max age before deleting an entry. */
c4069512 82 size_t max_entries; /* Max number of learned MACs. */
37bec3d3 83 struct ovs_refcount ref_cnt;
509c0149 84 struct ovs_rwlock rwlock;
30618594 85 bool need_revalidate;
962ff3d6
BP
86};
87
509c0149
EJ
88int mac_entry_age(const struct mac_learning *ml, const struct mac_entry *e)
89 OVS_REQ_RDLOCK(ml->rwlock);
90
db8077c3 91/* Basics. */
e764773c 92struct mac_learning *mac_learning_create(unsigned int idle_time);
5d989517
EJ
93struct mac_learning *mac_learning_ref(const struct mac_learning *);
94void mac_learning_unref(struct mac_learning *);
db8077c3 95
30618594 96bool mac_learning_run(struct mac_learning *ml) OVS_REQ_WRLOCK(ml->rwlock);
509c0149
EJ
97void mac_learning_wait(struct mac_learning *ml)
98 OVS_REQ_RDLOCK(ml->rwlock);
db8077c3
BP
99
100/* Configuration. */
509c0149
EJ
101bool mac_learning_set_flood_vlans(struct mac_learning *ml,
102 const unsigned long *bitmap)
103 OVS_REQ_WRLOCK(ml->rwlock);
104void mac_learning_set_idle_time(struct mac_learning *ml,
105 unsigned int idle_time)
106 OVS_REQ_WRLOCK(ml->rwlock);
107void mac_learning_set_max_entries(struct mac_learning *ml, size_t max_entries)
108 OVS_REQ_WRLOCK(ml->rwlock);
db8077c3
BP
109
110/* Learning. */
509c0149 111bool mac_learning_may_learn(const struct mac_learning *ml,
db8077c3 112 const uint8_t src_mac[ETH_ADDR_LEN],
509c0149
EJ
113 uint16_t vlan)
114 OVS_REQ_RDLOCK(ml->rwlock);
115struct mac_entry *mac_learning_insert(struct mac_learning *ml,
db8077c3 116 const uint8_t src[ETH_ADDR_LEN],
509c0149
EJ
117 uint16_t vlan)
118 OVS_REQ_WRLOCK(ml->rwlock);
30618594 119void mac_learning_changed(struct mac_learning *ml) OVS_REQ_WRLOCK(ml->rwlock);
db8077c3
BP
120
121/* Lookup. */
509c0149 122struct mac_entry *mac_learning_lookup(const struct mac_learning *ml,
db8077c3 123 const uint8_t dst[ETH_ADDR_LEN],
30618594 124 uint16_t vlan)
509c0149 125 OVS_REQ_RDLOCK(ml->rwlock);
db8077c3
BP
126
127/* Flushing. */
509c0149
EJ
128void mac_learning_expire(struct mac_learning *ml, struct mac_entry *e)
129 OVS_REQ_WRLOCK(ml->rwlock);
30618594 130void mac_learning_flush(struct mac_learning *ml) OVS_REQ_WRLOCK(ml->rwlock);
064af421
BP
131
132#endif /* mac-learning.h */