Merge citrix branch into master.
[cascardo/ovs.git] / lib / mac-learning.h
1 /*
2  * Copyright (c) 2008, 2009 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 #define MAC_HASH_BITS 10
26 #define MAC_HASH_MASK (MAC_HASH_SIZE - 1)
27 #define MAC_HASH_SIZE (1u << MAC_HASH_BITS)
28
29 #define MAC_MAX 2048
30
31 /* Time, in seconds, before expiring a mac_entry due to inactivity. */
32 #define MAC_ENTRY_IDLE_TIME 60
33
34 /* A MAC learning table entry. */
35 struct mac_entry {
36     struct list hash_node;      /* Element in a mac_learning 'table' list. */
37     struct list lru_node;       /* Element in 'lrus' or 'free' list. */
38     time_t expires;             /* Expiration time. */
39     uint8_t mac[ETH_ADDR_LEN];  /* Known MAC address. */
40     uint16_t vlan;              /* VLAN tag. */
41     int port;                   /* Port on which MAC was most recently seen. */
42     tag_type tag;               /* Tag for this learning entry. */
43 };
44
45 int mac_entry_age(const struct mac_entry *);
46
47 /* MAC learning table. */
48 struct mac_learning {
49     struct list free;           /* Not-in-use entries. */
50     struct list lrus;           /* In-use entries, least recently used at the
51                                    front, most recently used at the back. */
52     struct list table[MAC_HASH_SIZE]; /* Hash table. */
53     struct mac_entry entries[MAC_MAX]; /* All entries. */
54     uint32_t secret;            /* Secret for  */
55     unsigned long *non_learning_vlans; /* Bitmap of learning disabled VLANs. */
56 };
57
58 struct mac_learning *mac_learning_create(void);
59 void mac_learning_destroy(struct mac_learning *);
60 bool mac_learning_set_disabled_vlans(struct mac_learning *,
61                                      unsigned long *bitmap);
62 tag_type mac_learning_learn(struct mac_learning *,
63                             const uint8_t src[ETH_ADDR_LEN], uint16_t vlan,
64                             uint16_t src_port);
65 int mac_learning_lookup(const struct mac_learning *,
66                         const uint8_t dst[ETH_ADDR_LEN], uint16_t vlan);
67 int mac_learning_lookup_tag(const struct mac_learning *,
68                             const uint8_t dst[ETH_ADDR_LEN],
69                             uint16_t vlan, tag_type *tag);
70 void mac_learning_flush(struct mac_learning *);
71 void mac_learning_run(struct mac_learning *, struct tag_set *);
72 void mac_learning_wait(struct mac_learning *);
73
74 #endif /* mac-learning.h */