-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmaptable.c
More file actions
executable file
·449 lines (385 loc) · 10.1 KB
/
Copy pathmaptable.c
File metadata and controls
executable file
·449 lines (385 loc) · 10.1 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
* XenLoop -- A High Performance Inter-VM Network Loopback
*
* Installation and Usage instructions
*
* Authors:
* Jian Wang - Binghamton University (jianwang@cs.binghamton.edu)
* Kartik Gopalan - Binghamton University (kartik@cs.binghamton.edu)
*
* Copyright (C) 2007-2009 Kartik Gopalan, Jian Wang
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include "maptable.h"
#include "debug.h"
#include "bififo.h"
extern void send_destroy_chn_msg(u8 *dest_mac);
extern wait_queue_head_t swq;
static DEFINE_SPINLOCK(glock);
ulong hash(u8 *pmac){
return (pmac[3] + pmac[4] + pmac[5]) % XENLOOP_HASH_SIZE;
}
int equal(void *pmac1, void *pmac2)
{
if (memcmp(pmac1, pmac2, ETH_ALEN) == 0)
return 1;
else
return 0;
};
// hash an IPv4 address
ulong hash_ip(u32 ip) {
return ip % XENLOOP_HASH_SIZE;
}
// check if two IPv4 address are equal
// returns 1 if equal, 0 otherwise
int equal_ip(u32 ip1, u32 ip2) {
return ip1 == ip2;
}
// insert into a table
// key is a MAC address, value is a domid
inline void insert_table(HashTable * ht, void * key, u8 domid)
{
Bucket * b = &ht->table[hash(key)];
Entry * e;
ulong flags;
e = kmem_cache_alloc(ht->entries, GFP_ATOMIC);
BUG_ON(!e);
memcpy(e->mac, key, ETH_ALEN);
e->domid = domid;
e->timestamp = jiffies;
e->del_timer = 0;
e->status = XENLOOP_STATUS_INIT;
e->listen_flag = 0xff;
e->bfh = NULL;
e->retry_count = 0;
e->ip = 0;
spin_lock_irqsave(&glock, flags);
list_add(&e->mapping, &(b->bucket));
ht->count++;
spin_unlock_irqrestore(&glock, flags);
}
// insert an Entry at an IPv4 key in a table
// used for IP (not MAC) table
// NOTE: pointer to reference is stored, not copied!
inline void insert_table_ip(HashTable* ht, u32 ip, Entry* e) {
Bucket * b = &ht->table[hash_ip(ip)];
// Entry * e;
ulong flags;
// e = kmem_cache_alloc(ht->entries, GFP_ATOMIC);
BUG_ON(!e);
// memcpy((void*)e, (void*)old_entry, sizeof(Entry));
e->ip = ip;
spin_lock_irqsave(&glock, flags);
// list_add(&e->mapping, &(b->bucket));
list_add(&e->ip_mapping, &(b->bucket));
ht->count++;
spin_unlock_irqrestore(&glock, flags);
}
// remove an entry from the table
// NOTE: deallocates memory for Entries, make sure it isn't references anywhere else
inline void remove_entry(HashTable *ht, Entry *e, struct list_head *x) {
ulong flags;
TRACE_ENTRY;
spin_lock_irqsave(&glock, flags);
list_del(x);
ht->count--;
spin_unlock_irqrestore(&glock, flags);
// change status first, so suspend doesn't call this while we're disconnecting
e->status = XENLOOP_STATUS_INIT;
if (e->bfh) {
if(e->listen_flag) {
bf_destroy(e->bfh);
} else {
bf_disconnect(e->bfh);
}
e->bfh = NULL;
}
if(e->del_timer) {
del_timer(&e->ack_timer);
}
kmem_cache_free(ht->entries, e);
DPRINTK("Delete Guest: deleted one guest mac =" MAC_FMT " Domid = %d.\n", \
MAC_NTOA(e->mac), e->domid);
TRACE_EXIT;
}
// remove the entry keyed at MAC address 'mac'
inline void remove_entry_mac(HashTable* ht, void* mac) {
Bucket * b = &ht->table[hash(mac)];
if(!list_empty(&b->bucket)) {
struct list_head * x;
Entry * e;
list_for_each(x, &(b->bucket)) {
e = list_entry(x, Entry, mapping);
if(equal(mac, (u8 *) e->mac)) {
remove_entry(ht, e, x);
break;
}
}
}
}
// remove the reference to an entry at an IP
inline void remove_entry_ip(HashTable* ht, u32 ip) {
ulong flags;
Bucket * b = &ht->table[hash_ip(ip)];
if(!list_empty(&b->bucket)) {
struct list_head * x;
Entry * e;
list_for_each(x, &(b->bucket)) {
e = list_entry(x, Entry, ip_mapping);
if(ip == e->ip) {
e->ip = 0;
spin_lock_irqsave(&glock, flags);
list_del(x);
ht->count--;
spin_unlock_irqrestore(&glock, flags);
break;
}
}
}
}
// lookup the Entry according to bififo handle pointer 'key'
inline Entry* lookup_bfh(HashTable * ht, void * key)
{
int i;
struct list_head * x, * y;
Entry * e;
for(i = 0; i < XENLOOP_HASH_SIZE; i++) {
list_for_each_safe(x, y, &(ht->table[i].bucket)) {
e = list_entry(x, Entry, mapping);
if(key == e->bfh) {
return e;
}
}
}
return NULL;
}
// lookup a MAC address key in the table
inline void * lookup_table(HashTable * ht, void * key)
{
Entry * d = NULL;
Bucket * b = &ht->table[hash(key)];
if(!list_empty(&b->bucket)) {
struct list_head * x;
Entry * e;
list_for_each(x, &(b->bucket)) {
e = list_entry(x, Entry, mapping);
if(equal(key, (u8 *) e->mac)) {
d = e;
break;
}
}
}
return d;
}
// lookup an IP address key in the table
inline void * lookup_table_ip(HashTable * ht, u32 ip) {
Entry * d = NULL;
Bucket * b = &ht->table[hash_ip(ip)];
if(!list_empty(&b->bucket)) {
struct list_head * x;
Entry * e;
list_for_each(x, &(b->bucket)) {
e = list_entry(x, Entry, ip_mapping);
if(e->ip == ip) {
d = e;
break;
}
}
}
return d;
}
// return 1 if there is a suspended entry in the table, 0 otherwise
inline int has_suspend_entry(HashTable * ht)
{
int i;
Entry *e;
struct list_head *x, *y;
Bucket * table = ht->table;
for(i = 0; i < XENLOOP_HASH_SIZE; i++) {
list_for_each_safe(x, y, &(table[i].bucket)) {
e = list_entry(x, Entry, mapping);
if (e->status == XENLOOP_STATUS_SUSPEND)
return 1;
}
}
return 0;
}
// mark all entries in the table as suspended
inline void mark_suspend(HashTable * ht)
{
int i;
Entry *e;
struct list_head *x, *y;
Bucket * table = ht->table;
TRACE_ENTRY;
for(i = 0; i < XENLOOP_HASH_SIZE; i++) {
list_for_each_safe(x, y, &(table[i].bucket)) {
e = list_entry(x, Entry, mapping);
if (check_descriptor(e->bfh)) {
BF_SUSPEND_IN(e->bfh) = 1;
BF_SUSPEND_OUT(e->bfh) = 1;
bf_notify(e->bfh->port);
}
e->status = XENLOOP_STATUS_SUSPEND;
}
}
TRACE_EXIT;
}
// notify all bififos in the table, sends an event to the other side of the bififos
void notify_all_bfs(HashTable * ht)
{
int i;
Entry *e;
struct list_head *x, *y;
Bucket * table = ht->table;
TRACE_ENTRY;
for(i = 0; i < XENLOOP_HASH_SIZE; i++) {
list_for_each_safe(x, y, &(table[i].bucket)) {
e = list_entry(x, Entry, ip_mapping);
if ( check_descriptor(e->bfh) && (xf_size( e->bfh->out ) > 0) )
bf_notify(e->bfh->port);
}
}
TRACE_EXIT;
}
// check if any entries have timed out (timestamps are too old)
// mark them as suspended if they are
inline void check_timeout(HashTable * ht)
{
int i, found = 0;
Entry *e;
struct list_head *x, *y;
Bucket * table = ht->table;
for(i = 0; i < XENLOOP_HASH_SIZE; i++) {
list_for_each_safe(x, y, &(table[i].bucket)) {
e = list_entry(x, Entry, mapping);
if ((jiffies - e->timestamp) > (5*DISCOVER_TIMEOUT*HZ)) {
if (check_descriptor(e->bfh)) {
BF_SUSPEND_IN(e->bfh) = 1;
BF_SUSPEND_OUT(e->bfh) = 1;
}
DPRINTK("marking entry as suspended\n");
e->status = XENLOOP_STATUS_SUSPEND;
found = 1;
}
}
}
if (found)
wake_up_interruptible(&swq);
}
// update the timestamps in keys corresponding to the array of MAC address 'mac'
inline void update_table(HashTable * ht, u8 *mac, int mac_count)
{
int i,j,found = 0;
Entry *e;
void *p;
struct list_head *x, *y;
Bucket * table = ht->table;
for(j = 0; j < XENLOOP_HASH_SIZE; j++) {
list_for_each_safe(x, y, &(table[j].bucket)) {
e = list_entry(x, Entry, mapping);
for(i = 0, p = mac; i < mac_count; i++, p+= ETH_ALEN) {
if (equal(p, e->mac)) {
e->timestamp = jiffies;
found = 1;
break;
}
}
if (found) {
found = 0;
continue;
}
if (check_descriptor(e->bfh)) {
BF_SUSPEND_IN(e->bfh) = 1;
BF_SUSPEND_OUT(e->bfh) = 1;
}
e->status = XENLOOP_STATUS_SUSPEND;
found = 0;
wake_up_interruptible(&swq);
}
}
}
// initialize a MAC hash table
// NOTE: allocates kmeme cache for Entries
int init_hash_table(HashTable * ht, char * name)
{
int i;
ht->count = 0;
ht->entries = kmem_cache_create(name, sizeof(Entry), 0, 0, NULL);
if(!ht->entries) {
EPRINTK("hashtable(): slab caches failed.\n");
return -ENOMEM;
}
for(i = 0; i < XENLOOP_HASH_SIZE; i++) {
INIT_LIST_HEAD(&(ht->table[i].bucket));
}
return 0;
}
// initialize an IP hash table
// NOTE: does not allocate any memory, all Entries stored as references
int init_hash_table_ip(HashTable* ht) {
int i;
ht->count = 0;
ht->entries = NULL;
for(i = 0; i < XENLOOP_HASH_SIZE; i++) {
INIT_LIST_HEAD(&(ht->table[i].bucket));
}
return 0;
}
// remove all entries marked suspended
void clean_suspended_entries(HashTable * ht, HashTable* ip_ht)
{
int i;
Entry *e;
struct list_head *x, *y;
Bucket * table = ht->table;
DPRINTK("clean suspended entries\n");
for(i = 0; i < XENLOOP_HASH_SIZE; i++) {
list_for_each_safe(x, y, &(table[i].bucket)) {
e = list_entry(x, Entry, mapping);
if (e->status == XENLOOP_STATUS_SUSPEND) {
if(e->ip) {
remove_entry_ip(ip_ht, e->ip);
}
remove_entry(ht, e, x);
}
}
}
}
// remove all entries in the table
void clean_table(HashTable * ht)
{
int i;
Entry *e;
struct list_head *x, *y;
Bucket * table = ht->table;
DPRINTK("clean table\n");
for(i = 0; i < XENLOOP_HASH_SIZE; i++) {
list_for_each_safe(x, y, &(table[i].bucket)) {
e = list_entry(x, Entry, mapping);
remove_entry(ht, e, x);
}
}
kmem_cache_destroy(ht->entries);
//BUG_ON(kmem_cache_destroy(ht->entries));
}