-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouting_switch.c
644 lines (501 loc) · 18.4 KB
/
routing_switch.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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
/*
* Sample routing switch (switching HUB) application.
*
* This application provides a switching HUB function using multiple
* openflow switches.
*
* Author: Shuji Ishii
*
* Copyright (C) 2008-2011 NEC Corporation
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <arpa/inet.h>
#include <getopt.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "trema.h"
#include "fdb.h"
#include "libpathresolver.h"
#include "libtopology.h"
#include "port.h"
#include "topology_service_interface_option_parser.h"
static const uint16_t FLOW_TIMER = 60;
static const uint16_t PACKET_IN_DISCARD_DURATION = 1;
typedef struct routing_switch_options {
uint16_t idle_timeout;
} routing_switch_options;
typedef struct routing_switch {
uint16_t idle_timeout;
list_element *switches;
hash_table *fdb;
pathresolver *pathresolver;
} routing_switch;
static void
modify_flow_entry( const pathresolver_hop *h, const buffer *original_packet, uint16_t idle_timeout ) {
const uint32_t wildcards = 0;
struct ofp_match match;
set_match_from_packet( &match, h->in_port_no, wildcards, original_packet );
uint32_t transaction_id = get_transaction_id();
openflow_actions *actions = create_actions();
const uint16_t max_len = UINT16_MAX;
append_action_output( actions, h->out_port_no, max_len );
const uint16_t hard_timeout = 0;
const uint16_t priority = UINT16_MAX;
const uint32_t buffer_id = UINT32_MAX;
const uint16_t flags = 0;
buffer *flow_mod = create_flow_mod( transaction_id, match, get_cookie(),
OFPFC_ADD, idle_timeout, hard_timeout,
priority, buffer_id,
h->out_port_no, flags, actions );
send_openflow_message( h->dpid, flow_mod );
delete_actions( actions );
free_buffer( flow_mod );
}
static void
send_packet_out( uint64_t datapath_id, openflow_actions *actions, const buffer *original ) {
const uint32_t transaction_id = get_transaction_id();
const uint32_t buffer_id = UINT32_MAX;
const uint16_t in_port = OFPP_NONE;
buffer *copy = NULL;
const buffer *packet = original;
if ( original->length + ETH_FCS_LENGTH < ETH_MINIMUM_LENGTH ) {
copy = duplicate_buffer( original );
fill_ether_padding( copy );
packet = copy;
}
buffer *packet_out = create_packet_out( transaction_id, buffer_id, in_port,
actions, packet );
send_openflow_message( datapath_id, packet_out );
free_buffer( packet_out );
if ( copy != NULL ) {
free_buffer( copy );
}
}
static void
output_packet( const buffer *packet, uint64_t dpid, uint16_t port_no ) {
openflow_actions *actions = create_actions();
const uint16_t max_len = UINT16_MAX;
append_action_output( actions, port_no, max_len );
send_packet_out( dpid, actions, packet );
delete_actions( actions );
}
static void
output_packet_from_last_switch( const pathresolver_hop *last_hop, const buffer *packet ) {
output_packet( packet, last_hop->dpid, last_hop->out_port_no );
}
static uint32_t
count_hops( const dlist_element *hops ) {
uint32_t i = 0;
for ( const dlist_element *e = hops; e != NULL; e = e->next ) {
i++;
}
return i;
}
static void
discard_packet_in( uint64_t datapath_id, uint16_t in_port, const buffer *packet ) {
const uint32_t wildcards = 0;
struct ofp_match match;
set_match_from_packet( &match, in_port, wildcards, packet );
char match_str[ 1024 ];
match_to_string( &match, match_str, sizeof( match_str ) );
const uint16_t idle_timeout = 0;
const uint16_t hard_timeout = PACKET_IN_DISCARD_DURATION;
const uint16_t priority = UINT16_MAX;
const uint32_t buffer_id = UINT32_MAX;
const uint16_t flags = 0;
info( "Discarding packets for a certain period ( datapath_id = %#" PRIx64
", match = [%s], duration = %u [sec] ).", datapath_id, match_str, hard_timeout );
buffer *flow_mod = create_flow_mod( get_transaction_id(), match, get_cookie(),
OFPFC_ADD, idle_timeout, hard_timeout,
priority, buffer_id,
OFPP_NONE, flags, NULL );
send_openflow_message( datapath_id, flow_mod );
free_buffer( flow_mod );
}
static void
make_path( routing_switch *routing_switch, uint64_t in_datapath_id, uint16_t in_port,
uint64_t out_datapath_id, uint16_t out_port, const buffer *packet ) {
dlist_element *hops = resolve_path( routing_switch->pathresolver, in_datapath_id, in_port, out_datapath_id, out_port );
if ( hops == NULL ) {
warn( "No available path found ( %#" PRIx64 ":%u -> %#" PRIx64 ":%u ).",
in_datapath_id, in_port, out_datapath_id, out_port );
discard_packet_in( in_datapath_id, in_port, packet );
return;
}
// count elements
uint32_t hop_count = count_hops( hops );
// send flow entry from tail switch
for ( dlist_element *e = get_last_element( hops ); e != NULL; e = e->prev, hop_count-- ) {
uint16_t idle_timer = ( uint16_t ) ( routing_switch->idle_timeout + hop_count );
modify_flow_entry( e->data, packet, idle_timer );
} // for(;;)
// send packet out for tail switch
dlist_element *e = get_last_element( hops );
pathresolver_hop *last_hop = e->data;
output_packet_from_last_switch( last_hop, packet );
// free them
free_hop_list( hops );
}
static void
set_miss_send_len_maximum( uint64_t datapath_id ) {
uint32_t id = get_transaction_id();
const uint16_t config_flags = OFPC_FRAG_NORMAL;
const uint16_t miss_send_len = UINT16_MAX;
buffer *buf = create_set_config( id, config_flags, miss_send_len );
send_openflow_message( datapath_id, buf );
free_buffer( buf );
}
static void
receive_features_reply( uint64_t datapath_id, uint32_t transaction_id,
uint32_t n_buffers, uint8_t n_tables,
uint32_t capabilities, uint32_t actions,
const list_element *phy_ports, void *user_data ) {
UNUSED( transaction_id );
UNUSED( n_buffers );
UNUSED( n_tables );
UNUSED( capabilities );
UNUSED( actions );
UNUSED( phy_ports );
UNUSED( user_data );
set_miss_send_len_maximum( datapath_id );
}
static void
handle_switch_ready( uint64_t datapath_id, void *user_data ) {
UNUSED( user_data );
uint32_t id = get_transaction_id();
buffer *buf = create_features_request( id );
send_openflow_message( datapath_id, buf );
free_buffer( buf );
}
static void
port_status_updated( void *user_data, const topology_port_status *status ) {
assert( user_data != NULL );
assert( status != NULL );
routing_switch *routing_switch = user_data;
debug( "Port status updated: dpid:%#" PRIx64 ", port:%u, %s, %s",
status->dpid, status->port_no,
( status->status == TD_PORT_UP ? "up" : "down" ),
( status->external == TD_PORT_EXTERNAL ? "external" : "internal or inactive" ) );
if ( status->port_no > OFPP_MAX && status->port_no != OFPP_LOCAL ) {
warn( "Ignore this update ( port = %u )", status->port_no );
return;
}
port_info *p = lookup_port( routing_switch->switches, status->dpid, status->port_no );
delete_fdb_entries( routing_switch->fdb, status->dpid, status->port_no );
if ( status->status == TD_PORT_UP ) {
if ( p != NULL ) {
update_port( p, status->external );
return;
}
add_port( &routing_switch->switches, status->dpid, status->port_no, status->external );
}
else {
if ( p == NULL ) {
debug( "Ignore this update (not found nor already deleted)" );
return;
}
delete_port( &routing_switch->switches, p );
}
}
static int
build_packet_out_actions( port_info *port, openflow_actions *actions, uint64_t dpid, uint16_t in_port ) {
const uint16_t max_len = UINT16_MAX;
if ( !port->external_link || port->switch_to_switch_reverse_link ) {
// don't send to not external port
return 0;
}
if ( port->dpid == dpid && port->port_no == in_port ) {
// don't send to input port
return 0;
}
append_action_output( actions, port->port_no, max_len );
return 1;
}
static void
send_packet_out_for_each_switch( switch_info *sw, const buffer *packet, uint64_t dpid, uint16_t in_port ) {
openflow_actions *actions = create_actions();
int number_of_actions = foreach_port( sw->ports, build_packet_out_actions, actions, dpid, in_port );
// check if no action is build
if ( number_of_actions > 0 ) {
send_packet_out( sw->dpid, actions, packet );
}
delete_actions( actions );
}
static void
flood_packet( uint64_t datapath_id, uint16_t in_port, const buffer *packet, list_element *switches ) {
foreach_switch( switches, send_packet_out_for_each_switch, packet, datapath_id, in_port );
}
static void
handle_packet_in( uint64_t datapath_id, uint32_t transaction_id,
uint32_t buffer_id, uint16_t total_len,
uint16_t in_port, uint8_t reason, const buffer *data,
void *user_data ) {
assert( in_port != 0 );
assert( data != NULL );
assert( user_data != NULL );
routing_switch *routing_switch = user_data;
debug( "Packet-In received ( datapath_id = %#" PRIx64 ", transaction_id = %#lx, "
"buffer_id = %#lx, total_len = %u, in_port = %u, reason = %#x, "
"data_len = %u ).", datapath_id, transaction_id, buffer_id,
total_len, in_port, reason, data->length );
const port_info *port = lookup_port( routing_switch->switches, datapath_id, in_port );
if ( port == NULL ) {
debug( "Ignoring Packet-In from unknown port." );
return;
}
packet_info packet_info = get_packet_info( data );
const uint8_t *src = packet_info.eth_macsa;
const uint8_t *dst = packet_info.eth_macda;
if ( in_port > OFPP_MAX && in_port != OFPP_LOCAL ) {
error( "Packet-In from invalid port ( in_port = %u ).", in_port );
return;
}
if ( !port->external_link || port->switch_to_switch_reverse_link ) {
if ( !port->external_link
&& port->switch_to_switch_link
&& port->switch_to_switch_reverse_link
&& !is_ether_multicast( dst )
&& lookup_fdb( routing_switch->fdb, src, &datapath_id, &in_port ) ) {
debug( "Found a Packet-In from switch-to-switch link." );
}
else {
debug( "Ignoring Packet-In from not external link." );
return;
}
}
if ( !update_fdb( routing_switch->fdb, src, datapath_id, in_port ) ) {
return;
}
uint16_t out_port;
uint64_t out_datapath_id;
if ( lookup_fdb( routing_switch->fdb, dst, &out_datapath_id, &out_port ) ) {
// Host is located, so resolve path and send flowmod
if ( ( datapath_id == out_datapath_id ) && ( in_port == out_port ) ) {
// in and out are same
return;
}
make_path( routing_switch, datapath_id, in_port, out_datapath_id, out_port, data );
}
else {
// Host's location is unknown, so flood packet
flood_packet( datapath_id, in_port, data, routing_switch->switches );
}
}
static void
init_ports( list_element **switches, size_t n_entries, const topology_port_status *s ) {
for ( size_t i = 0; i < n_entries; i++ ) {
if ( s[ i ].status == TD_PORT_UP ) {
add_port( switches, s[ i ].dpid, s[ i ].port_no, s[ i ].external );
}
}
}
static void
update_port_status_by_link( list_element *switches, const topology_link_status *s ) {
port_info *port = lookup_port( switches, s->from_dpid, s->from_portno );
if ( port != NULL ) {
debug( "Link status updated: dpid:%#" PRIx64 ", port:%u, %s",
s->from_dpid, s->from_portno,
( s->status == TD_LINK_UP ? "up" : "down" ) );
port->switch_to_switch_link = ( s->status == TD_LINK_UP );
}
if ( s->to_portno == 0 ) {
return;
}
port_info *peer = lookup_port( switches, s->to_dpid, s->to_portno );
if ( peer != NULL ) {
debug( "Reverse link status updated: dpid:%#" PRIx64 ", port:%u, %s",
s->to_dpid, s->to_portno,
( s->status == TD_LINK_UP ? "up" : "down" ) );
peer->switch_to_switch_reverse_link = ( s->status == TD_LINK_UP );
}
}
static void
link_status_updated( void *user_data, const topology_link_status *status ) {
assert( user_data != NULL );
assert( status != NULL );
routing_switch *routing_switch = user_data;
update_topology( routing_switch->pathresolver, status );
update_port_status_by_link( routing_switch->switches, status );
}
static void
update_link_status( routing_switch *routing_switch, size_t n_entries,
const topology_link_status *status ) {
for ( size_t i = 0; i < n_entries; i++ ) {
update_topology( routing_switch->pathresolver, &status[ i ] );
update_port_status_by_link( routing_switch->switches, &status[ i ] );
}
}
static void
init_last_stage( void *user_data, size_t n_entries, const topology_link_status *status ) {
assert( user_data != NULL );
routing_switch *routing_switch = user_data;
update_link_status( routing_switch, n_entries, status );
add_callback_link_status_updated( link_status_updated, routing_switch );
}
static void
init_second_stage( void *user_data, size_t n_entries, const topology_port_status *s ) {
assert( user_data != NULL );
routing_switch *routing_switch = user_data;
// Initialize ports
init_ports( &routing_switch->switches, n_entries, s );
// Initialize aging FDB
init_age_fdb( routing_switch->fdb );
// Set asynchronous event handlers
// (0) Set features_request_reply handler
set_features_reply_handler( receive_features_reply, routing_switch );
// (1) Set switch_ready handler
set_switch_ready_handler( handle_switch_ready, routing_switch );
// (2) Set port status update callback
add_callback_port_status_updated( port_status_updated, routing_switch );
// (3) Set packet-in handler
set_packet_in_handler( handle_packet_in, routing_switch );
// (4) Get all link status
get_all_link_status( init_last_stage, routing_switch );
}
static void
after_subscribed( void *user_data ) {
assert( user_data != NULL );
// Get all ports' status
// init_last_stage() will be called
get_all_port_status( init_second_stage, user_data );
}
static routing_switch *
create_routing_switch( const char *topology_service, const routing_switch_options *options ) {
assert( topology_service != NULL );
assert( options != NULL );
// Allocate routing_switch object
routing_switch *routing_switch = xmalloc( sizeof( struct routing_switch ) );
routing_switch->idle_timeout = options->idle_timeout;
routing_switch->switches = NULL;
routing_switch->fdb = NULL;
info( "idle_timeout is set to %u [sec].", routing_switch->idle_timeout );
// Create pathresolver table
routing_switch->pathresolver = create_pathresolver();
// Create forwarding database
routing_switch->fdb = create_fdb();
// Initialize port database
routing_switch->switches = create_ports( &routing_switch->switches );
// Initialize libraries
init_libtopology( topology_service );
// Ask topology manager to notify any topology change events.
// after_subscribed() will be called
subscribe_topology( after_subscribed, routing_switch );
return routing_switch;
}
static void
delete_routing_switch( routing_switch *routing_switch ) {
assert( routing_switch != NULL );
// Delete pathresolver table
delete_pathresolver( routing_switch->pathresolver );
// Finalize libraries
finalize_libtopology();
// Delete ports
delete_all_ports( &routing_switch->switches );
// Delete forwarding database
delete_fdb( routing_switch->fdb );
// Delete routing_switch object
xfree( routing_switch );
}
static char option_description[] = " -i, --idle_timeout=TIMEOUT Idle timeout value of flow entry\n";
static char short_options[] = "i:";
static struct option long_options[] = {
{ "idle_timeout", 1, NULL, 'i' },
{ NULL, 0, NULL, 0 },
};
static void
reset_getopt() {
optind = 0;
opterr = 1;
}
void usage( void );
static void
init_routing_switch_options( routing_switch_options *options, int *argc, char **argv[] ) {
assert( options != NULL );
assert( argc != NULL );
assert( *argc >= 0 );
assert( argv != NULL );
// set default values
options->idle_timeout = FLOW_TIMER;
int argc_tmp = *argc;
char *new_argv[ *argc ];
for ( int i = 0; i <= *argc; ++i ) {
new_argv[ i ] = ( *argv )[ i ];
}
int c;
uint32_t idle_timeout;
while ( ( c = getopt_long( *argc, *argv, short_options, long_options, NULL ) ) != -1 ) {
switch ( c ) {
case 'i':
idle_timeout = ( uint32_t ) atoi( optarg );
if ( idle_timeout == 0 || idle_timeout > UINT16_MAX ) {
printf( "Invalid idle_timeout value.\n" );
usage();
finalize_topology_service_interface_options();
exit( EXIT_SUCCESS );
return;
}
options->idle_timeout = ( uint16_t ) idle_timeout;
break;
default:
continue;
}
if ( optarg == 0 || strchr( new_argv[ optind - 1 ], '=' ) != NULL ) {
argc_tmp -= 1;
new_argv[ optind - 1 ] = NULL;
}
else {
argc_tmp -= 2;
new_argv[ optind - 1 ] = NULL;
new_argv[ optind - 2 ] = NULL;
}
}
for ( int i = 0, j = 0; i < *argc; ++i ) {
if ( new_argv[ i ] != NULL ) {
( *argv )[ j ] = new_argv[ i ];
j++;
}
}
( *argv )[ *argc ] = NULL;
*argc = argc_tmp;
reset_getopt();
}
void
usage() {
topology_service_interface_usage( get_executable_name(), "Switching HUB.", option_description );
}
int
main( int argc, char *argv[] ) {
// Initialize Trema world
init_trema( &argc, &argv );
routing_switch_options options;
init_routing_switch_options( &options, &argc, &argv );
init_topology_service_interface_options( &argc, &argv );
// Initialize routing_switch
routing_switch *routing_switch = create_routing_switch( get_topology_service_interface_name(), &options );
// Main loop
start_trema();
// Finalize routing_switch
delete_routing_switch( routing_switch );
return 0;
}
/*
* Local variables:
* c-basic-offset: 2
* indent-tabs-mode: nil
* End:
*/