-
Notifications
You must be signed in to change notification settings - Fork 0
/
sr_rtable_hw.c
316 lines (304 loc) · 11.2 KB
/
sr_rtable_hw.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
/* Filename: sr_rtable_hw.c */
#include <stdio.h> /* snprintf() */
#include <stdlib.h>
#include "sr_rtable_hw.h"
#include "cli/helper.h"
#include "sr_router.h"
#include "cli/cli.h"
#include "sr_integration.h"
#include "reg_defines.h"
#include "sr_neighbor.h"
/**
* Initialize the routing table
*
* @param rtable A pointer to the routing table to initialize
*/
void hw_rrtable_init(struct sr_router *router){
printf(" ** hw_rrtable_init(..) called \n");
// initialize routing table list
router->hw_rtable = (hw_rtable_t*) malloc_or_die(sizeof(hw_rtable_t));
router->hw_rtable->hw_rtable_list = llist_new();
// initialize lock
pthread_mutex_init(&router->hw_rtable->hw_lock_rtable, NULL);
}
/**
* Free memory associated with all members of the routing table, but not
* the routing table itself.
*
* @param rtable A pointer to the routing table to destroy
*/
void hw_rrtable_destroy(hw_rtable_t *hw_rtable){
printf(" ** hw_rrtable_destroy(..) called \n");
// clean routing table list
pthread_mutex_lock(&hw_rtable->hw_lock_rtable);
hw_rtable->hw_rtable_list = llist_delete_no_count(hw_rtable->hw_rtable_list);
pthread_mutex_unlock(&hw_rtable->hw_lock_rtable);
// delete lock
pthread_mutex_destroy(&hw_rtable->hw_lock_rtable);
// write to hw
hw_rrtable_write_hw();
}
/**
* Add a route to the routing table in a threadsafe manner. Each entry is
* marked as either static or dynamic depending on its.
*
* @param rtable The routing table to add to
* @param dest The destination IP address
* @param mask The subnet mask
* @param intf The interface this subnet is on
*/
void hw_rrtable_route_add( hw_rtable_t *hw_rtable,
addr_ip_t dest, addr_ip_t mask,
uint16_t primary, uint16_t backup) {
printf(" ** hw_rrtable_route_add(..) called \n");
// get subnet
dest = dest & mask;
// make route_t object
hw_route_t* route = hw_make_route_t(dest, primary, backup, mask);
// lock table
pthread_mutex_lock(&hw_rtable->hw_lock_rtable);
// add route to table
hw_rtable->hw_rtable_list = llist_insert_sorted(hw_rtable->hw_rtable_list, predicate_ip_sort_hw_route_t, (void*) route);
// unblock table
pthread_mutex_unlock(&hw_rtable->hw_lock_rtable);
// write to hw
hw_rrtable_write_hw();
}
/**
* Remove the specified route from the routing table, if it exists. Return TRUE
* on success, FALSE if the route does not exist. Threadsafe.
*
* @param rtable A pointer to the routing table to remove the route from
* @param dest The destination IP of the route
* @param mask The subnet mask of the route
*
* @return bool indicating success (TRUE) or non-existent route (FALSE)
*/
bool hw_rrtable_route_remove( hw_rtable_t *hw_rtable, addr_ip_t dest, addr_ip_t mask ) {
printf(" ** hw_rrtable_route_remove(..) called \n");
// lock table
pthread_mutex_lock(&hw_rtable->hw_lock_rtable);
// remove route from table
node* ret = llist_remove(hw_rtable->hw_rtable_list, predicate_ip_sort_hw_route_t, (void*) &dest);
// unlock table
pthread_mutex_unlock(&hw_rtable->hw_lock_rtable);
if(ret == NULL)
return FALSE;
else {
hw_rtable->hw_rtable_list = ret;
// write to hw
hw_rrtable_write_hw();
return TRUE;
}
}
/**
* Removes all routes from a routing table (threadsafe).
*
* @param rtable A pointer to the routing table from which all routes will be
* removed.
*/
void hw_rrtable_purge_all( hw_rtable_t* hw_rtable ) {
printf(" ** hw_rrtable_purge_all(..) called \n");
// lock table
pthread_mutex_lock(&hw_rtable->hw_lock_rtable);
// remove route from table
hw_rtable->hw_rtable_list = llist_delete_no_count(hw_rtable->hw_rtable_list);
// unlock table
pthread_mutex_unlock(&hw_rtable->hw_lock_rtable);
// write to hw
hw_rrtable_write_hw();
}
/**
* Fills a buffer with a string representation of the routing table
*
*/
void hw_rrtable_to_string() {
// get instance of router
struct sr_instance* sr_inst = get_sr();
struct sr_router* router = (struct sr_router*)sr_get_subsystem(sr_inst);
char *str;
// first display the next hop table
asprintf(&str, "-----------------------\n");
cli_send_str(str);
free(str);
asprintf(&str, "|%-5s|%-15s|\n", "Index", "Next Hop IP");
cli_send_str(str);
free(str);
asprintf(&str, "-----------------------\n");
cli_send_str(str);
free(str);
// get next hop addresses from each interface
int i = 0;
for(i = 0; i < router->num_interfaces; i++) {
pthread_mutex_lock(&router->interface[i].neighbor_lock);
node* neighbor_node = router->interface[i].neighbor_list;
// check if interface has a neighbor or not
if(neighbor_node != NULL) {
neighbor_t* neighbor = neighbor_node->data;
asprintf(&str, "|%-5s|%-15s|\n", router->interface[i].name, quick_ip_to_string(neighbor->ip));
cli_send_str(str);
free(str);
}
pthread_mutex_unlock(&router->interface[i].neighbor_lock);
}
asprintf(&str, "-----------------------\n");
cli_send_str(str);
free(str);
// now multipath routing table
asprintf(&str, "---------------------------------------------------------\n");
cli_send_str(str);
free(str);
asprintf(&str, "|%-15s|%-15s|%-11s|%-11s|\n", "Destination", "Mask", "Primary", "Backup");
cli_send_str(str);
free(str);
asprintf(&str, "---------------------------------------------------------\n");
cli_send_str(str);
free(str);
node* head;
hw_route_t* entry;
pthread_mutex_lock(&router->hw_rtable->hw_lock_rtable);
head = router->hw_rtable->hw_rtable_list;
while(head != NULL) {
entry = (hw_route_t*) head->data;
asprintf(&str, "|%-15s|", quick_ip_to_string(entry->destination));
cli_send_str(str);
free(str);
asprintf(&str, "%-15s|%-11s|%-11s|\n", quick_ip_to_string(entry->subnet_mask), get_interface_from_multiple_hw_ports(entry->primary), get_interface_from_multiple_hw_ports(entry->backup));
cli_send_str(str);
free(str);
head = head->next;
}
asprintf(&str, "---------------------------------------------------------\n");
cli_send_str(str);
free(str);
pthread_mutex_unlock(&router->hw_rtable->hw_lock_rtable);
}
hw_route_t* hw_make_route_t(addr_ip_t dst, uint16_t primary, uint16_t backup, addr_ip_t subnet_mask) {
hw_route_t* route = (hw_route_t*) malloc_or_die(sizeof(hw_route_t));
route->destination = dst;
route->primary = primary;
route->backup = backup;
route->subnet_mask = subnet_mask;
return route;
}
void hw_rrtable_write_hw() {
#ifdef _CPUMODE_
printf(" ** hw_rrtable_write_hw(..) called \n");
struct sr_instance* sr_inst = get_sr();
struct sr_router* router = (struct sr_router*)sr_get_subsystem(sr_inst);
pthread_mutex_lock(&router->hw_rtable->hw_lock_rtable);
hw_route_t* route = NULL;
node *head = router->hw_rtable->hw_rtable_list;
int i = 0;
while(head != NULL && i != ROUTER_OP_LUT_ROUTE_TABLE_DEPTH) {
route = (hw_route_t*) head->data;
// Destination IP
writeReg(&router->hw_device, ROUTER_OP_LUT_ROUTE_TABLE_ENTRY_IP_REG, ntohl(route->destination));
// Mask
writeReg(&router->hw_device, ROUTER_OP_LUT_ROUTE_TABLE_ENTRY_MASK_REG, ntohl(route->subnet_mask));
//Output ports
writeReg(&router->hw_device, ROUTER_OP_LUT_ROUTE_TABLE_ENTRY_PRIMARY_OUTPUT_PORT_REG, route->primary + route->backup);
writeReg(&router->hw_device, ROUTER_OP_LUT_ROUTE_TABLE_ENTRY_BACKUP_OUTPUT_PORT_REG, route->backup);
// row index
writeReg(&router->hw_device, ROUTER_OP_LUT_ROUTE_TABLE_WR_ADDR_REG, i);
head = head->next;
i++;
}
pthread_mutex_unlock(&router->hw_rtable->hw_lock_rtable);
#endif
}
void hw_rrtable_read_hw() {
#ifdef _CPUMODE_
printf(" ** hw_rrtable_read_hw(..) called \n");
struct sr_instance* sr_inst = get_sr();
struct sr_router* router = (struct sr_router*)sr_get_subsystem(sr_inst);
char* str;
//first read the hop table
asprintf(&str, "-----------------------\n");
cli_send_str(str);
free(str);
asprintf(&str, "|%-5s|%-15s|\n", "Index", "Next Hop IP");
cli_send_str(str);
free(str);
asprintf(&str, "-----------------------\n");
cli_send_str(str);
free(str);
int i = 0;
uint32_t hop;
for(i = 0; i < router->num_interfaces; i++) {
switch(i) {
case 0:
readReg(&router->hw_device, ROUTER_OP_LUT_NEXT_HOP_IP_0_REG, &hop);
hop = htonl(hop);
if(hop != 0) {
asprintf(&str, "|%-5s|%-15s|\n", "eth0", quick_ip_to_string(hop));
cli_send_str(str);
free(str);
}
break;
case 1:
readReg(&router->hw_device, ROUTER_OP_LUT_NEXT_HOP_IP_1_REG, &hop);
hop = htonl(hop);
if(hop != 0) {
asprintf(&str, "|%-5s|%-15s|\n", "eth1", quick_ip_to_string(hop));
cli_send_str(str);
free(str);
}
break;
case 2:
readReg(&router->hw_device, ROUTER_OP_LUT_NEXT_HOP_IP_2_REG, &hop);
hop = htonl(hop);
if(hop != 0) {
asprintf(&str, "|%-5s|%-15s|\n", "eth2", quick_ip_to_string(hop));
cli_send_str(str);
free(str);
}
break;
case 3:
readReg(&router->hw_device, ROUTER_OP_LUT_NEXT_HOP_IP_3_REG, &hop);
hop = htonl(hop);
if(hop != 0) {
asprintf(&str, "|%-5s|%-15s|\n", "eth3", quick_ip_to_string(hop));
cli_send_str(str);
free(str);
}
break;
}
}
asprintf(&str, "-----------------------\n");
cli_send_str(str);
free(str);
uint32_t destination, mask, primary = 0, backup = 0;
// now multipath routing table
asprintf(&str, "---------------------------------------------------------\n");
cli_send_str(str);
free(str);
asprintf(&str, "|%-15s|%-15s|%-11s|%-11s|\n", "Destination", "Mask", "Primary", "Backup");
cli_send_str(str);
free(str);
asprintf(&str, "---------------------------------------------------------\n");
cli_send_str(str);
free(str);
for(i = 0; i < ROUTER_OP_LUT_ROUTE_TABLE_DEPTH; i++) {
// write index to read register
writeReg(&router->hw_device, ROUTER_OP_LUT_ROUTE_TABLE_RD_ADDR_REG, i);
// read values from registers
readReg(&router->hw_device, ROUTER_OP_LUT_ROUTE_TABLE_ENTRY_IP_REG, &destination);
readReg(&router->hw_device, ROUTER_OP_LUT_ROUTE_TABLE_ENTRY_MASK_REG, &mask);
readReg(&router->hw_device, ROUTER_OP_LUT_ROUTE_TABLE_ENTRY_PRIMARY_OUTPUT_PORT_REG, &primary);
readReg(&router->hw_device, ROUTER_OP_LUT_ROUTE_TABLE_ENTRY_BACKUP_OUTPUT_PORT_REG, &backup);
//check for blank entries
if(destination == 0)
break;
asprintf(&str, "|%-15s|", quick_ip_to_string(htonl(destination)));
cli_send_str(str);
free(str);
asprintf(&str, "%-15s|%-11s|%-11s|\n", quick_ip_to_string(htonl(mask)), get_interface_from_multiple_hw_ports(primary), get_interface_from_multiple_hw_ports(backup));
cli_send_str(str);
free(str);
}
asprintf(&str, "---------------------------------------------------------\n");
cli_send_str(str);
free(str);
#endif
}