-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy patharp_cache.c
227 lines (206 loc) · 5.04 KB
/
arp_cache.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "ether.h"
#include "arp.h"
#include "lib.h"
#include "list.h"
#include "compile.h"
#define arp_cache_head (&arp_cache[0])
#define arp_cache_end (&arp_cache[ARP_CACHE_SZ])
static struct arpentry arp_cache[ARP_CACHE_SZ];
/* Lock Definition */
#ifdef STATIC_MUTEX
pthread_mutex_t arp_cache_mutex = PTHREAD_MUTEX_INITIALIZER;
#else
pthread_mutex_t arp_cache_mutex;
/* Why are they not defined in pthread.h? */
#ifndef PTHREAD_MUTEX_NORMAL
#define PTHREAD_MUTEX_NORMAL PTHREAD_MUTEX_TIMED_NP
#endif
#endif /* STATIC_MUTEX */
/* Lock Init */
static _inline void arp_cache_lock_init(void)
{
#ifndef STATIC_MUTEX
/* It is evil to init pthread mutex dynamically X< */
pthread_mutexattr_t attr;
if (pthread_mutexattr_init(&attr) != 0)
perrx("pthread_mutexattr_init");
if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL) != 0)
perrx("pthread_mutexattr_settype");
if (pthread_mutex_init(&arp_cache_mutex, &attr) != 0)
perrx("pthread_mutex_init");
#endif
}
/* Lock Function */
#ifdef DEBUG_ARPCACHE_LOCK
#define arp_cache_lock() do { dbg("lock"); pthread_mutex_lock(&arp_cache_mutex); } while(0)
#define arp_cache_unlock() do { dbg("unlock"); pthread_mutex_unlock(&arp_cache_mutex); } while(0)
#else
static _inline void arp_cache_lock(void)
{
pthread_mutex_lock(&arp_cache_mutex);
}
static _inline void arp_cache_unlock(void)
{
pthread_mutex_unlock(&arp_cache_mutex);
}
#endif /* end DEBUG_ARPCACHE_LOCK */
void arp_queue_send(struct arpentry *ae)
{
struct pkbuf *pkb;
while (!list_empty(&ae->ae_list)) {
pkb = list_first_entry(&ae->ae_list, struct pkbuf, pk_list);
list_del(ae->ae_list.next);
arpdbg("send pending packet");
netdev_tx(ae->ae_dev, pkb, pkb->pk_len - ETH_HRD_SZ,
pkb->pk_pro, ae->ae_hwaddr);
}
}
void arp_queue_drop(struct arpentry *ae)
{
struct pkbuf *pkb;
while (!list_empty(&ae->ae_list)) {
pkb = list_first_entry(&ae->ae_list, struct pkbuf, pk_list);
list_del(ae->ae_list.next);
arpdbg("drop pending packet");
free_pkb(pkb);
}
}
struct arpentry *arp_alloc(void)
{
static int next = 0;
int i;
struct arpentry *ae;
arp_cache_lock();
/* round-robin loop algorithm */
for (i = 0; i < ARP_CACHE_SZ; i++) {
if (arp_cache[next].ae_state == ARP_FREE)
break;
next = (next + 1) % ARP_CACHE_SZ;
}
/* not found */
if (i >= ARP_CACHE_SZ) {
arpdbg("arp cache is full");
arp_cache_unlock();
return NULL;
}
/* init */
ae = &arp_cache[next];
ae->ae_dev = NULL;
ae->ae_retry = ARP_REQ_RETRY;
ae->ae_ttl = ARP_WAITTIME;
ae->ae_state = ARP_WAITING;
ae->ae_pro = ETH_P_IP; /* default protocol */
list_init(&ae->ae_list);
/* for next time allocation */
next = (next + 1) % ARP_CACHE_SZ;
arp_cache_unlock();
return ae;
}
int arp_insert(struct netdev *nd, unsigned short pro,
unsigned int ipaddr, unsigned char *hwaddr)
{
struct arpentry *ae;
ae = arp_alloc();
if (!ae)
return -1;
ae->ae_dev = nd;
ae->ae_pro = pro;
ae->ae_ttl = ARP_TIMEOUT;
ae->ae_ipaddr = ipaddr;
ae->ae_state = ARP_RESOLVED;
hwacpy(ae->ae_hwaddr, hwaddr);
return 0;
}
struct arpentry *arp_lookup(unsigned short pro, unsigned int ipaddr)
{
struct arpentry *ae, *ret = NULL;
arp_cache_lock();
arpdbg("pro:%d "IPFMT, pro, ipfmt(ipaddr));
for (ae = arp_cache_head; ae < arp_cache_end; ae++) {
if (ae->ae_state == ARP_FREE)
continue;
if (ae->ae_pro == pro && ae->ae_ipaddr == ipaddr) {
ret = ae;
break;
}
}
arp_cache_unlock();
return ret;
}
struct arpentry *arp_lookup_resolv(unsigned short pro, unsigned int ipaddr)
{
struct arpentry *ae;
ae = arp_lookup(pro, ipaddr);
if (ae && ae->ae_pro == ARP_RESOLVED)
return ae;
return NULL;
}
void arp_timer(int delta)
{
struct arpentry *ae;
arp_cache_lock();
for (ae = arp_cache_head; ae < arp_cache_end; ae++) {
if (ae->ae_state == ARP_FREE)
continue;
ae->ae_ttl -= delta;
if (ae->ae_ttl <= 0) {
if ((ae->ae_state == ARP_WAITING && --ae->ae_retry < 0)
|| ae->ae_state == ARP_RESOLVED) {
if (ae->ae_state == ARP_WAITING)
arp_queue_drop(ae);
ae->ae_state = ARP_FREE;
} else {
/* retry arp request */
ae->ae_ttl = ARP_WAITTIME;
arp_cache_unlock();
arp_request(ae);
arp_cache_lock();
}
}
}
arp_cache_unlock();
}
void arp_cache_init(void)
{
int i;
for (i = 0; i < ARP_CACHE_SZ; i++)
arp_cache[i].ae_state = ARP_FREE;
dbg("ARP CACHE INIT");
arp_cache_lock_init();
dbg("ARP CACHE SEMAPHORE INIT");
}
static const char *__arpstate[] = {
NULL,
"Free",
"Waiting",
"Resolved"
};
static _inline const char *arpstate(struct arpentry *ae)
{
return __arpstate[ae->ae_state];
}
char *ipnfmt(unsigned int ipaddr)
{
static char ipbuf[16];
snprintf(ipbuf, 16, IPFMT, ipfmt(ipaddr));
return ipbuf;
}
void arp_cache_traverse(void)
{
struct arpentry *ae;
int first;
arp_cache_lock();
first = 1;
for (ae = arp_cache_head; ae < arp_cache_end; ae++) {
if (ae->ae_state == ARP_FREE)
continue;
if (first) {
printf("State Timeout(s) HWaddress Address\n");
first = 0;
}
printf("%-9s%-12d" MACFMT " %s\n",
arpstate(ae), ((ae->ae_ttl < 0) ? 0 : ae->ae_ttl),
macfmt(ae->ae_hwaddr), ipnfmt(ae->ae_ipaddr));
}
arp_cache_unlock();
}