-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfdb.c
143 lines (112 loc) · 2.8 KB
/
fdb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "fdb.h"
#include <err.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <syslog.h>
#define FDB_DECREASE_TTL_INTERVAL 1
void fdb_decrease_ttl_thread_init (struct fdb * fdb);
void * fdb_decrease_ttl_thread (void * param);
struct fdb *
init_fdb (void)
{
struct fdb * fdb;
fdb = (struct fdb *) malloc (sizeof (struct fdb));
init_hash (&fdb->fdb, 6);
fdb->fdb_max_ttl = FDB_DEFAULT_CACHE_MAX_TTL;
fdb_decrease_ttl_thread_init (fdb);
return fdb;
}
void
destroy_fdb (struct fdb * fdb)
{
pthread_cancel (fdb->decrease_ttl_t);
destroy_hash (&fdb->fdb);
return;
}
int
fdb_add_entry (struct fdb * fdb, u_int8_t * mac, struct sockaddr_storage vtep_addr)
{
struct fdb_entry * entry;
entry = (struct fdb_entry *) malloc (sizeof (struct fdb_entry));
memset (entry, 0, sizeof (struct fdb_entry));
entry->vtep_addr = vtep_addr;
entry->ttl = fdb->fdb_max_ttl;
memcpy (entry->mac, mac, ETH_ALEN);
return insert_hash (&fdb->fdb, entry, mac);
}
int
fdb_del_entry (struct fdb * fdb, u_int8_t * mac)
{
struct fdb_entry * entry;
if ((entry = delete_hash (&fdb->fdb, mac)) != NULL) {
free (entry);
return 1;
}
return -1;
}
struct fdb_entry *
fdb_search_entry (struct fdb * fdb, u_int8_t * mac)
{
return search_hash (&fdb->fdb, mac);
}
struct sockaddr *
fdb_search_vtep_addr (struct fdb * fdb, u_int8_t * mac)
{
struct fdb_entry * entry;
if ((entry = search_hash (&fdb->fdb, mac)) == NULL)
return NULL;
return (struct sockaddr *) &entry->vtep_addr;
}
void
fdb_decrease_ttl_thread_init (struct fdb * fdb)
{
pthread_attr_t attr;
pthread_attr_init (&attr);
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
pthread_create (&fdb->decrease_ttl_t, &attr, fdb_decrease_ttl_thread, fdb);
return;
}
void *
fdb_decrease_ttl_thread (void * param)
{
int n;
struct fdb * fdb = (struct fdb *) param;
struct hashnode * ptr, * prev;
struct fdb_entry * entry;
struct hash * hash = &fdb->fdb;
while (1) {
for (n = 0; n < HASH_TABLE_SIZE; n++) {
pthread_mutex_lock (&hash->mutex[n]);
prev = &hash->table[n];
for (ptr = hash->table[n].next; ptr != NULL; ptr = ptr->next) {
entry = (struct fdb_entry *) ptr->data;
entry->ttl--;
if (entry->ttl <= 0) {
prev->next = ptr->next;
free (ptr);
free (entry);
ptr = prev;
fdb->fdb.count--;
#ifdef LOGGING_FDB_CHANGE
syslog (LOG_INFO,
"delete entry "
"%02x:%02x:%02x:%02x:%02x:%02x",
entry->mac[0], entry->mac[1],
entry->mac[2], entry->mac[3],
entry->mac[4], entry->mac[5]);
syslog (LOG_INFO, "Delete, Number of FDB entry is %d",
fdb->fdb.count);
#endif
} else
prev = ptr;
}
pthread_mutex_unlock (&hash->mutex[n]);
}
sleep (FDB_DECREASE_TTL_INTERVAL);
}
/* not reached */
return NULL;
}