-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsr_integration.c
298 lines (266 loc) · 9.95 KB
/
sr_integration.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
/*-----------------------------------------------------------------------------
* file: sr_integration.c
* date: Tue Feb 03 11:29:17 PST 2004
* Author: Martin Casado <[email protected]>
*
* Description:
*
* Methods called by the lowest-level of the network system to talk with
* the network subsystem.
*
* This is the entry point of integration for the network layer.
*
*---------------------------------------------------------------------------*/
#include <stdlib.h>
#include <netinet/ip.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include "sr_vns.h"
#include "sr_base_internal.h"
#include "sr_router.h"
#include "sr_common.h"
#include "sr_ethernet.h"
#include "sr_arp_cache.h"
#include "linked_list.h"
#include "sr_arp.h"
#include "cli/helper.h"
#include "sr_ip.h"
#include "sr_rtable.h"
#include "sr_pwospf.h"
#include "sr_rt.h"
#include "sr_neighbor_db.h"
#include "sr_rtable_hw.h"
#ifdef _CPUMODE_
#include "sr_cpu_extension_nf2.h"
#include "nf2util.h"
#include "nf2.h"
#endif
struct sr_instance* get_sr();
int sr_integ_low_level_output(struct sr_instance*,
uint8_t*,
unsigned int,
const char*);
/*-----------------------------------------------------------------------------
* Method: sr_integ_init(..)
* Scope: global
*
*
* First method called during router initialization. Called before connecting
* to VNS, reading in hardware information etc.
*
*---------------------------------------------------------------------------*/
void sr_integ_init(struct sr_instance* sr)
{
printf(" ** sr_integ_init(..) called \n");
sr_router* subsystem = (sr_router*)malloc(sizeof(sr_router));
assert(subsystem);
sr_set_subsystem(get_sr(), subsystem);
// initialize arp and interfaces and routing table
arp_cache_init(subsystem);
interface_init(subsystem);
rrtable_init(subsystem);
hw_rrtable_init(subsystem);
//enable ospf by default
toggle_ospf_status(subsystem, TRUE);
//disable reroute/multipath
toggle_reroute_multipath_status(subsystem, FALSE);
//flush hw registers
#ifdef _CPUMODE_
hw_init(subsystem);
writeReg(&subsystem->hw_device, CPCI_REG_CTRL, 0x00010100);
usleep(2000);
#endif
} /* -- sr_integ_init -- */
/*-----------------------------------------------------------------------------
* Method: sr_integ_hw_setup(..)
* Scope: global
*
* Called after all initial hardware information (interfaces) have been
* received. Can be used to start subprocesses (such as dynamic-routing
* protocol) which require interface information during initialization.
*
*---------------------------------------------------------------------------*/
void sr_integ_hw_setup(struct sr_instance* sr)
{
printf(" ** sr_integ_hw(..) called \n");
struct sr_router* subsystem = (struct sr_router*)sr_get_subsystem(sr);
printf(" ** sr_integ_hw(..) adding entries to static rtable \n");
// read rtable file
sr_load_rt(sr, sr->rtable);
// add static entries to routing table
printf(" ** sr_integ_hw(..) initial state of routing table\n");
llist_display_all(subsystem->rtable->rtable_list, display_route_t);
// set link state info
uint32_t area_id = PWOSPF_AREA_ID;
uint16_t lsuint = PWOSPF_LSU_INTERVAL;
set_ls_info_id(subsystem, area_id, lsuint);
// get hardware fds for each hw interface
#ifdef _CPUMODE_
sr_cpu_init_interface_socket(subsystem);
#endif
// initialize neighbor db
neighbor_db_init(subsystem);
// display initial state of neighbor db
printf(" ** sr_integ_hw(..) initial state of neighbor db\n");
display_neighbor_vertices(subsystem);
// initialize pwospf
pwospf_init(subsystem);
} /* -- sr_integ_hw_setup -- */
/*---------------------------------------------------------------------
* Method: sr_integ_input(struct sr_instance*,
* uint8_t* packet,
* char* interface)
* Scope: Global
*
* This method is called each time the router receives a packet on the
* interface. The packet buffer, the packet length and the receiving
* interface are passed in as parameters. The packet is complete with
* ethernet headers.
*
* Note: Both the packet buffer and the character's memory are handled
* by sr_vns_comm.c that means do NOT delete either. Make a copy of the
* packet instead if you intend to keep it around beyond the scope of
* the method call.
*
*---------------------------------------------------------------------*/
void sr_integ_input(struct sr_instance* sr,
const uint8_t * packet/* borrowed */,
unsigned int len,
const char* interface/* borrowed */)
{
/* -- INTEGRATION PACKET ENTRY POINT!-- */
printf(" ** sr_integ_input(..) called \n");
// get global router instance
struct sr_router* subsystem = (struct sr_router*)sr_get_subsystem(sr);
// get interface object for this interface name
interface_t *intf = get_interface_name(subsystem, interface);
printf(" ********************* new packet received with length %u on interface: %s\n", len, intf->name);
// check if interface is active
if(intf->enabled == TRUE) {
// create a copy of the packet
uint8_t* packet_copy = (uint8_t*) malloc_or_die(len);
memcpy(packet_copy, packet, len);
// send to ethernet frame handler
ethernet_handle_frame(subsystem, packet_copy, len, intf);
} else {
printf(" ** sr_integ_input(..): interface: %s is down. Dropping packet\n", intf->name);
}
} /* -- sr_integ_input -- */
/*-----------------------------------------------------------------------------
* Method: sr_integ_add_interface(..)
* Scope: global
*
* Called for each interface read in during hardware initialization.
* struct sr_vns_if is defined in sr_base_internal.h
*
*---------------------------------------------------------------------------*/
void sr_integ_add_interface(struct sr_instance* sr,
struct sr_vns_if* vns_if/* borrowed */)
{
printf(" ** sr_integ_add_interface(..) called \n");
struct sr_router* subsystem = (struct sr_router*)sr_get_subsystem(sr);
subsystem->interface[subsystem->num_interfaces] = set_interface(vns_if, subsystem->num_interfaces);
#ifdef _CPUMODE_
write_interface_hw(subsystem);
#endif
subsystem->num_interfaces = subsystem->num_interfaces + 1;
printf(" ** sr_integ_add_interface(..) current no. of interfaces: %u\n", subsystem->num_interfaces);
} /* -- sr_integ_add_interface -- */
struct sr_instance* get_sr() {
struct sr_instance* sr;
sr = sr_get_global_instance( NULL );
assert( sr );
return sr;
}
/*-----------------------------------------------------------------------------
* Method: sr_integ_low_level_output(..)
* Scope: global
*
* Send a packet to VNS to be injected into the topology
*
*---------------------------------------------------------------------------*/
int sr_integ_low_level_output(struct sr_instance* sr /* borrowed */,
uint8_t* buf /* borrowed */ ,
unsigned int len,
const char* iface /* borrowed */)
{
#ifdef _CPUMODE_
return sr_cpu_output(sr, buf /*lent*/, len, iface);
#else
return sr_vns_send_packet(sr, buf /*lent*/, len, iface);
#endif /* _CPUMODE_ */
} /* -- sr_vns_integ_output -- */
/*-----------------------------------------------------------------------------
* Method: sr_integ_destroy(..)
* Scope: global
*
* For memory deallocation pruposes on shutdown.
*
*---------------------------------------------------------------------------*/
void sr_integ_destroy(struct sr_instance* sr)
{
printf(" ** sr_integ_destroy(..) called \n");
} /* -- sr_integ_destroy -- */
/*-----------------------------------------------------------------------------
* Method: sr_integ_findsrcip(..)
* Scope: global
*
* Called by the transport layer for outgoing packets generated by the
* router. Expects source address in network byte order.
*
*---------------------------------------------------------------------------*/
uint32_t sr_integ_findsrcip(uint32_t dest /* nbo */)
{
/* --
* e.g.
*
* struct sr_instance* sr = sr_get_global_instance();
* struct my_router* mr = (struct my_router*)
* sr_get_subsystem(sr);
* return my_findsrcip(mr, dest);
* -- */
struct sr_router* subsystem = (struct sr_router*)sr_get_subsystem(get_sr());
// look up in routing table
route_info_t route_info = rrtable_find_route(subsystem->rtable, dest);
return route_info.intf->ip;
} /* -- ip_findsrcip -- */
/*-----------------------------------------------------------------------------
* Method: sr_integ_ip_output(..)
* Scope: global
*
* Called by the transport layer for outgoing packets that need IP
* encapsulation.
*
*---------------------------------------------------------------------------*/
uint32_t sr_integ_ip_output(uint8_t* payload /* given */,
uint8_t proto,
uint32_t src, /* nbo */
uint32_t dest, /* nbo */
int len)
{
/* --
* e.g.
*
* struct sr_instance* sr = sr_get_global_instance();
* struct my_router* mr = (struct my_router*)
* sr_get_subsystem(sr);
* return my_ip_output(mr, payload, proto, src, dest, len);
* -- */
struct in_addr dst_ip, src_ip;
memcpy(&dst_ip, &src, 4);
memcpy(&src_ip, &dest, 4);
make_ip_packet(payload, len, src_ip, dst_ip, proto);
return 0;
} /* -- ip_integ_route -- */
/*-----------------------------------------------------------------------------
* Method: sr_integ_close(..)
* Scope: global
*
* Called when the router is closing connection to VNS.
*
*---------------------------------------------------------------------------*/
void sr_integ_close(struct sr_instance* sr)
{
printf(" ** sr_integ_close(..) called \n");
} /* -- sr_integ_close -- */