forked from upa/hogelan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfdb.c
102 lines (80 loc) · 1.93 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
#include "fdb.h"
#include <err.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
int
fdb_add_entry (struct hash * fdb, u_int8_t * mac, struct in_addr vtep)
{
struct fdb_entry * entry;
struct sockaddr_in saddr_in;
entry = (struct fdb_entry *) malloc (sizeof (struct fdb_entry));
memset (entry, 0, sizeof (struct fdb_entry));
saddr_in.sin_family = AF_INET;
saddr_in.sin_port = htons (VXLAN_PORT);
saddr_in.sin_addr = vtep;
memcpy (&entry->vtep_addr, &saddr_in, sizeof (saddr_in));
entry->ttl = FDB_CACHE_TTL;
return insert_hash (fdb, entry, mac);
}
int
fdb_del_entry (struct hash * fdb, u_int8_t * mac)
{
struct fdb_entry * entry;
if ((entry = delete_hash (fdb, mac)) != NULL) {
free (entry);
return 1;
}
return -1;
}
struct fdb_entry *
fdb_search_entry (struct hash * fdb, u_int8_t * mac)
{
return search_hash (fdb, mac);
}
struct sockaddr *
fdb_search_vtep_addr (struct hash * fdb, u_int8_t * mac)
{
struct fdb_entry * entry;
if ((entry = search_hash (fdb, mac)) == NULL)
return NULL;
return &entry->vtep_addr;
}
void
fdb_decrease_ttl_init (void)
{
pthread_attr_t attr;
pthread_attr_init (&attr);
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
pthread_create (&vxlan.decrease_ttl_t, &attr, fdb_decrease_ttl, NULL);
return;
}
void *
fdb_decrease_ttl (void * param)
{
int n;
struct hash * fdb = &vxlan.fdb;
struct hashnode * ptr, * prev;
struct fdb_entry * entry;
for (n = 0; n < HASH_TABLE_SIZE; n++) {
pthread_mutex_lock (&fdb->mutex[n]);
prev = &fdb->table[n];
for (ptr = fdb->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;
} else
prev = ptr;
}
pthread_mutex_unlock (&fdb->mutex[n]);
sleep (FDB_DECREASE_TTL_INTERVAL);
}
/* not reached */
return NULL;
}