-
Notifications
You must be signed in to change notification settings - Fork 0
/
sr_interface.c
457 lines (435 loc) · 15.3 KB
/
sr_interface.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
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
450
451
452
453
454
455
456
/* Filename: sr_interface.c
*/
#include "sr_interface.h"
#include "sr_common.h"
#include "sr_base_internal.h"
#include "sr_integration.h"
#include "cli/helper.h"
#include "sr_router.h"
#include "sr_neighbor.h"
#include "cli/cli.h"
#include "sr_cpu_extension_nf2.h"
#include "reg_defines.h"
#include "sr_pwospf.h"
#include <string.h>
#include <stdlib.h>
interface_t set_interface(struct sr_vns_if* vns_intf, int intf_num) {
printf(" ** set_interface(..) called \n");
interface_t intf;
strcpy(intf.name, vns_intf->name);
intf.mac = make_mac_addr(vns_intf->addr[0], vns_intf->addr[1], vns_intf->addr[2], vns_intf->addr[3], vns_intf->addr[4], vns_intf->addr[5]);
intf.ip = vns_intf->ip;
intf.subnet_mask = vns_intf->mask;
intf.enabled = TRUE;
intf.neighbor_list = llist_new();
pthread_mutex_init(&intf.neighbor_lock, NULL);
intf.helloint = PWOSPF_HELLO_INTERVAL;
#ifdef _CPUMODE_
pthread_mutex_init(&intf.hw_lock, NULL);
intf.hw_port = get_hw_port(intf_num);
#endif
printf(" ** set_interface(..) displaying interface object \n");
display_interface(&intf);
return intf;
}
void interface_init(struct sr_router* subsystem) {
printf(" ** interface_init(..) called \n");
subsystem->num_interfaces = 0;
}
interface_t make_interface(char* name, addr_mac_t mac, addr_ip_t ip, bool enabled, addr_ip_t subnet_mask) {
printf(" ** make_interface(..) called \n");
interface_t intf;
strcpy(intf.name, name);
intf.mac = mac;
intf.ip = ip;
intf.enabled = enabled;
intf.subnet_mask = subnet_mask;
intf.neighbor_list = llist_new();
pthread_mutex_init(&intf.neighbor_lock, NULL);
intf.helloint = PWOSPF_HELLO_INTERVAL;
#ifdef _CPUMODE_
pthread_mutex_init(&intf.hw_lock, NULL);
#endif
return intf;
}
interface_t* get_interface_name(struct sr_router* router, const char* name) {
printf(" ** get_interface_name(..) called for name: %s\n", name);
int i = 0;
for(i = 0; i < router->num_interfaces ; i++) {
if(strcmp(router->interface[i].name, name) == 0) {
printf(" ** get_interface_name(..) found name: %s\n", name);
return &router->interface[i];
}
}
printf(" ** get_interface_name(..) not found name: %s\n", name);
return NULL;
}
interface_t* get_interface_ip(struct sr_router* router, addr_ip_t ip) {
printf(" ** get_interface_ip(..) called \n");
int i = 0;
for(i = 0; i < router->num_interfaces ; i++) {
if(router->interface[i].ip == ip)
return &router->interface[i];
}
return NULL;
}
void display_interface(interface_t* intf) {
printf(" ** name: %s, mac: %s, ip: %s ", intf->name, quick_mac_to_string(intf->mac.octet), quick_ip_to_string(intf->ip));
printf("subnet mask: %s, hello interval: %u, ", quick_ip_to_string(intf->subnet_mask), intf->helloint);
if(intf->enabled == TRUE)
printf("status: enabled\n");
else
printf("status: disabled\n");
}
void toggle_interface(interface_t* intf, int enabled) {
if (enabled == 1)
intf->enabled = TRUE;
else
intf->enabled = FALSE;
}
bool check_interface(interface_t* intf) {
printf(" ** check_interface(..) called \n");
printf("************* %u\n", intf->enabled);
return intf->enabled;
}
void display_all_interfaces() {
// get instance of router
struct sr_instance* sr_inst = get_sr();
struct sr_router* router = (struct sr_router*)sr_get_subsystem(sr_inst);
int i;
for( i = 0; i<router->num_interfaces; i++) {
display_interface(&router->interface[i]);
}
}
void display_all_interfaces_str() {
char *str;
asprintf(&str, "Name, MAC, IP, Mask, Hello Interval, Status\n");
cli_send_str(str);
free(str);
// get instance of router
struct sr_instance* sr_inst = get_sr();
struct sr_router* router = (struct sr_router*)sr_get_subsystem(sr_inst);
int i;
interface_t* intf;
for( i = 0; i<router->num_interfaces; i++) {
intf = &router->interface[i];
asprintf(&str, "%s, %s, %s, ", intf->name, quick_mac_to_string(intf->mac.octet), quick_ip_to_string(intf->ip));
cli_send_str(str);
free(str);
asprintf(&str, "%s, %u, ", quick_ip_to_string(intf->subnet_mask), intf->helloint);
cli_send_str(str);
free(str);
if(intf->enabled == TRUE) {
asprintf(&str, "enabled\n");
cli_send_str(str);
free(str);
}
else {
asprintf(&str, "disabled\n");
cli_send_str(str);
free(str);
}
}
}
void display_all_interfaces_neighbors_str() {
char *str;
node* head;
neighbor_t* entry;
// get instance of router
struct sr_instance* sr_inst = get_sr();
struct sr_router* router = (struct sr_router*)sr_get_subsystem(sr_inst);
int i;
interface_t* intf;
for( i = 0; i<router->num_interfaces; i++) {
intf = &router->interface[i];
cli_send_str("\n-----------------------------------------------------------------------------\n");
cli_send_str("Interface: \n");
asprintf(&str, "%-4s|%-17s|%-15s|%-15s|%-14s|Status\n", "Name", "MAC", "IP", "Mask", "Hello Interval");
cli_send_str(str);
free(str);
asprintf(&str, "%-4s|%-16s|%-15s|", intf->name, quick_mac_to_string(intf->mac.octet), quick_ip_to_string(intf->ip));
cli_send_str(str);
free(str);
asprintf(&str, "%-15s|%-14u|", quick_ip_to_string(intf->subnet_mask), intf->helloint);
cli_send_str(str);
free(str);
if(intf->enabled == TRUE) {
asprintf(&str, "enabled\n");
cli_send_str(str);
free(str);
}
else {
asprintf(&str, "disabled\n");
cli_send_str(str);
free(str);
}
// now display its neighbors
pthread_mutex_lock(&intf->neighbor_lock);
head = intf->neighbor_list;
while(head != NULL) {
entry = (neighbor_t*) head->data;
cli_send_str("Neighbor(s): \n");
asprintf(&str, "%-15s|%-15s|%-17s|%-5s|%-15s|LSU\n", "ID", "IP", "Timestamp", "Hello", "Mask");
cli_send_str(str);
free(str);
asprintf(&str, "%-15s|", quick_ip_to_string(entry->id));
cli_send_str(str);
free(str);
asprintf(&str, "%-15s|", quick_ip_to_string(entry->ip));
cli_send_str(str);
free(str);
asprintf(&str, "%ld.%-6ld|%-5u|%-15s|", entry->timestamp->tv_sec, entry->timestamp->tv_usec, ntohs(entry->helloint), quick_ip_to_string(entry->mask));
cli_send_str(str);
free(str);
if(entry->last_adverts == NULL) {
cli_send_str("No\n");
} else {
cli_send_str("Yes\n");
}
head = head->next;
}
cli_send_str("\n-----------------------------------------------------------------------------\n");
pthread_mutex_unlock(&intf->neighbor_lock);
}
}
void write_interface_hw(struct sr_router* subsystem) {
#ifdef _CPUMODE_
printf(" ** write_interface_hw(..) called \n");
unsigned int mac_hi = 0;
unsigned int mac_lo = 0;
pthread_mutex_lock(&subsystem->interface[subsystem->num_interfaces].hw_lock);
/*MAC address needs to be of the form 00:00:AA:BB:CC:DD:EE:FF
* Hi: 00:00:AA:BB
* Lo: CC:DD:EE:FF
*/
mac_hi |= ((unsigned int)subsystem->interface[subsystem->num_interfaces].mac.octet[0]) << 8;
mac_hi |= ((unsigned int)subsystem->interface[subsystem->num_interfaces].mac.octet[1]);
mac_lo |= ((unsigned int)subsystem->interface[subsystem->num_interfaces].mac.octet[2]) << 24;
mac_lo |= ((unsigned int)subsystem->interface[subsystem->num_interfaces].mac.octet[3]) << 16;
mac_lo |= ((unsigned int)subsystem->interface[subsystem->num_interfaces].mac.octet[4]) << 8;
mac_lo |= ((unsigned int)subsystem->interface[subsystem->num_interfaces].mac.octet[5]);
//IMP: IP addresses are in network order
writeReg(&subsystem->hw_device, ROUTER_OP_LUT_DST_IP_FILTER_TABLE_ENTRY_IP_REG, ntohl(subsystem->interface[subsystem->num_interfaces].ip));
writeReg(&subsystem->hw_device, ROUTER_OP_LUT_DST_IP_FILTER_TABLE_WR_ADDR_REG, subsystem->num_interfaces);
if(subsystem->num_interfaces == 0) {
//IMP: MAC addresses are already in host order
writeReg(&subsystem->hw_device, ROUTER_OP_LUT_MAC_0_HI_REG, mac_hi);
writeReg(&subsystem->hw_device, ROUTER_OP_LUT_MAC_0_LO_REG, mac_lo);
} else if(subsystem->num_interfaces == 1) {
writeReg(&subsystem->hw_device, ROUTER_OP_LUT_MAC_1_HI_REG, mac_hi);
writeReg(&subsystem->hw_device, ROUTER_OP_LUT_MAC_1_LO_REG, mac_lo);
} else if(subsystem->num_interfaces == 2) {
writeReg(&subsystem->hw_device, ROUTER_OP_LUT_MAC_2_HI_REG, mac_hi);
writeReg(&subsystem->hw_device, ROUTER_OP_LUT_MAC_2_LO_REG, mac_lo);
} else if(subsystem->num_interfaces == 3) {
writeReg(&subsystem->hw_device, ROUTER_OP_LUT_MAC_3_HI_REG, mac_hi);
writeReg(&subsystem->hw_device, ROUTER_OP_LUT_MAC_3_LO_REG, mac_lo);
// last interface
//add allspfrouters ip
int i = subsystem->num_interfaces + 1;
char* all_spf_routers = ALL_SPF_ROUTERS_IP;
writeReg(&subsystem->hw_device, ROUTER_OP_LUT_DST_IP_FILTER_TABLE_ENTRY_IP_REG, ntohl(make_ip_addr(all_spf_routers)));
writeReg(&subsystem->hw_device, ROUTER_OP_LUT_DST_IP_FILTER_TABLE_WR_ADDR_REG, i);
// now add zeroes to the rest
i++;
while(i < ROUTER_OP_LUT_DST_IP_FILTER_TABLE_DEPTH) {
writeReg(&subsystem->hw_device, ROUTER_OP_LUT_DST_IP_FILTER_TABLE_ENTRY_IP_REG, 0);
writeReg(&subsystem->hw_device, ROUTER_OP_LUT_DST_IP_FILTER_TABLE_WR_ADDR_REG, i);
i++;
}
}
pthread_mutex_unlock(&subsystem->interface[subsystem->num_interfaces].hw_lock);
#endif
}
uint32_t get_hw_port(int intf_num) {
if(intf_num == 0)
return 1;
else if(intf_num == 1)
return 4;
else if(intf_num == 2)
return 16;
return 64;
}
interface_t get_hw_intf(int hw_port) {
struct sr_instance* sr_inst = get_sr();
struct sr_router* router = (struct sr_router*)sr_get_subsystem(sr_inst);
if(hw_port == 1)
return router->interface[0];
else if(hw_port == 4)
return router->interface[1];
else if(hw_port == 16)
return router->interface[2];
return router->interface[3];
}
void read_interface_hw() {
#ifdef _CPUMODE_
printf(" ** read_interface_hw(..) called \n");
struct sr_instance* sr_inst = get_sr();
struct sr_router* router = (struct sr_router*)sr_get_subsystem(sr_inst);
char *str;
unsigned int mac[2];
uint32_t ip;
int i;
// show interface names and mac addresses
asprintf(&str, "Name, MAC\n");
cli_send_str(str);
free(str);
// read hi, lo mac for eth0
readReg(&router->hw_device, ROUTER_OP_LUT_MAC_0_HI_REG, &mac[0]);
readReg(&router->hw_device, ROUTER_OP_LUT_MAC_0_LO_REG, &mac[1]);
mac[0] = htonl(mac[0]);
mac[1] = htonl(mac[1]);
asprintf(&str, "%s, %s\n", "eth0", quick_mac_to_string(((uint8_t*)mac) + 2));
cli_send_str(str);
free(str);
// read hi, lo mac for eth1
readReg(&router->hw_device, ROUTER_OP_LUT_MAC_1_HI_REG, &mac[0]);
readReg(&router->hw_device, ROUTER_OP_LUT_MAC_1_LO_REG, &mac[1]);
mac[0] = htonl(mac[0]);
mac[1] = htonl(mac[1]);
asprintf(&str, "%s, %s\n", "eth1", quick_mac_to_string(((uint8_t*)mac) + 2));
cli_send_str(str);
free(str);
// read hi, lo mac for eth2
readReg(&router->hw_device, ROUTER_OP_LUT_MAC_2_HI_REG, &mac[0]);
readReg(&router->hw_device, ROUTER_OP_LUT_MAC_2_LO_REG, &mac[1]);
mac[0] = htonl(mac[0]);
mac[1] = htonl(mac[1]);
asprintf(&str, "%s, %s\n", "eth2", quick_mac_to_string(((uint8_t*)mac) + 2));
cli_send_str(str);
free(str);
// read hi, lo mac for eth3
readReg(&router->hw_device, ROUTER_OP_LUT_MAC_3_HI_REG, &mac[0]);
readReg(&router->hw_device, ROUTER_OP_LUT_MAC_3_LO_REG, &mac[1]);
mac[0] = htonl(mac[0]);
mac[1] = htonl(mac[1]);
asprintf(&str, "%s, %s\n", "eth3", quick_mac_to_string(((uint8_t*)mac) + 2));
cli_send_str(str);
free(str);
// read ip filter table
asprintf(&str, "IP\n");
cli_send_str(str);
free(str);
for (i = 0; i < ROUTER_OP_LUT_DST_IP_FILTER_TABLE_DEPTH; i++) {
// write index to register
writeReg(&router->hw_device, ROUTER_OP_LUT_DST_IP_FILTER_TABLE_RD_ADDR_REG, i);
// read value
readReg(&router->hw_device, ROUTER_OP_LUT_DST_IP_FILTER_TABLE_ENTRY_IP_REG, &ip);
ip = htonl(ip);
//check for blank entries
if( ip == 0)
break;
asprintf(&str, "%s\n", quick_ip_to_string(ip));
cli_send_str(str);
free(str);
}
#endif
}
void read_info_hw() {
#ifdef _CPUMODE_
printf(" ** read_interface_hw(..) called \n");
struct sr_instance* sr_inst = get_sr();
struct sr_router* router = (struct sr_router*)sr_get_subsystem(sr_inst);
char *str;
uint32_t data;
// read arp misses
readReg(&router->hw_device, ROUTER_OP_LUT_ARP_NUM_MISSES_REG, &data);
asprintf(&str, "arp misses: %lu\n", data);
cli_send_str(str);
free(str);
// read lpm misses
readReg(&router->hw_device, ROUTER_OP_LUT_LPM_NUM_MISSES_REG, &data);
asprintf(&str, "lpm misses: %lu\n", data);
cli_send_str(str);
free(str);
// read cpu pkts send
readReg(&router->hw_device, ROUTER_OP_LUT_NUM_CPU_PKTS_SENT_REG, &data);
asprintf(&str, "cpu packets sent: %lu\n", data);
cli_send_str(str);
free(str);
readReg(&router->hw_device, ROUTER_OP_LUT_NUM_BAD_OPTS_VER_REG, &data);
asprintf(&str, "bad opts version: %lu\n", data);
cli_send_str(str);
free(str);
readReg(&router->hw_device, ROUTER_OP_LUT_NUM_BAD_CHKSUMS_REG, &data);
asprintf(&str, "bad checksums: %lu\n", data);
cli_send_str(str);
free(str);
readReg(&router->hw_device, ROUTER_OP_LUT_NUM_BAD_TTLS_REG, &data);
asprintf(&str, "bad ttls: %lu\n", data);
cli_send_str(str);
free(str);
readReg(&router->hw_device, ROUTER_OP_LUT_NUM_NON_IP_RCVD_REG, &data);
asprintf(&str, "non IP received: %lu\n", data);
cli_send_str(str);
free(str);
readReg(&router->hw_device, ROUTER_OP_LUT_NUM_PKTS_FORWARDED_REG, &data);
asprintf(&str, "packets forwarded: %lu\n", data);
cli_send_str(str);
free(str);
readReg(&router->hw_device, ROUTER_OP_LUT_NUM_WRONG_DEST_REG, &data);
asprintf(&str, "wrong destination: %lu\n", data);
cli_send_str(str);
free(str);
readReg(&router->hw_device, ROUTER_OP_LUT_NUM_FILTERED_PKTS_REG, &data);
asprintf(&str, "filtered packets: %lu\n", data);
cli_send_str(str);
free(str);
#endif
}
uint16_t get_hw_port_from_name(char* intf_name) {
if(strcmp("eth0", intf_name) == 0)
return 1;
else if(strcmp("eth1", intf_name) == 0)
return 4;
else if(strcmp("eth2", intf_name) == 0)
return 16;
return 64;
}
uint16_t get_num_from_name(char* intf_name) {
if(strcmp("eth0", intf_name) == 0)
return 0;
else if(strcmp("eth1", intf_name) == 0)
return 1;
else if(strcmp("eth2", intf_name) == 0)
return 2;
return 3;
}
char* get_interface_from_multiple_hw_ports(uint16_t ports) {
switch(ports) {
case 0:
return "none";
case 1:
return "eth0";
case 4:
return "eth1";
case 5:
return "eth0,1";
case 16:
return "eth2";
case 17:
return "eth0,2";
case 20:
return "eth1,2";
case 21:
return "eth0,1,2";
case 64:
return "eth3";
case 65:
return "eth0,3";
case 68:
return "eth1,3";
case 69:
return "eth0,1,3";
case 80:
return "eth2,3";
case 81:
return "eth0,2,3";
case 84:
return "eth1,2,3";
case 85:
return "eth0,1,2,3";
default:
return "unknown";
}
}