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
|
# File 'ruby/trema/controller.c', line 446
static VALUE
controller_send_packet_out( int argc, VALUE *argv, VALUE self ) {
VALUE datapath_id = Qnil;
VALUE options = Qnil;
rb_scan_args( argc, argv, "11", &datapath_id, &options );
// Defaults.
uint32_t buffer_id = UINT32_MAX;
uint16_t in_port = OFPP_NONE;
openflow_actions *actions = create_actions();
const buffer *data = NULL;
buffer *allocated_data = NULL;
VALUE opt_zero_padding = Qnil;
if ( options != Qnil ) {
VALUE opt_message = rb_hash_aref( options, ID2SYM( rb_intern( "packet_in" ) ) );
if ( opt_message != Qnil ) {
packet_in *message;
Data_Get_Struct( opt_message, packet_in, message );
if ( NUM2ULL( datapath_id ) == message->datapath_id ) {
buffer_id = message->buffer_id;
in_port = message->in_port;
}
data = ( buffer_id == UINT32_MAX ? message->data : NULL );
}
VALUE opt_buffer_id = rb_hash_aref( options, ID2SYM( rb_intern( "buffer_id" ) ) );
if ( opt_buffer_id != Qnil ) {
buffer_id = ( uint32_t ) NUM2ULONG( opt_buffer_id );
}
VALUE opt_in_port = rb_hash_aref( options, ID2SYM( rb_intern( "in_port" ) ) );
if ( opt_in_port != Qnil ) {
in_port = ( uint16_t ) NUM2UINT( opt_in_port );
}
VALUE opt_action = rb_hash_aref( options, ID2SYM( rb_intern( "actions" ) ) );
if ( opt_action != Qnil ) {
form_actions( opt_action, actions );
}
VALUE opt_data = rb_hash_aref( options, ID2SYM( rb_intern( "data" ) ) );
if ( opt_data != Qnil ) {
Check_Type( opt_data, T_STRING );
uint16_t length = ( uint16_t ) RSTRING_LEN( opt_data );
allocated_data = alloc_buffer_with_length( length );
memcpy( append_back_buffer( allocated_data, length ), RSTRING_PTR( opt_data ), length );
data = allocated_data;
}
opt_zero_padding = rb_hash_aref( options, ID2SYM( rb_intern( "zero_padding" ) ) );
if ( opt_zero_padding != Qnil ) {
if ( TYPE( opt_zero_padding ) != T_TRUE && TYPE( opt_zero_padding ) != T_FALSE ) {
rb_raise( rb_eTypeError, ":zero_padding must be true or false" );
}
}
}
if ( data != NULL && data->length + ETH_FCS_LENGTH < ETH_MINIMUM_LENGTH &&
opt_zero_padding != Qnil && TYPE( opt_zero_padding ) == T_TRUE ) {
if ( allocated_data == NULL ) {
allocated_data = duplicate_buffer( data );
data = allocated_data;
}
fill_ether_padding( allocated_data );
}
buffer *packet_out = create_packet_out(
get_transaction_id(),
buffer_id,
in_port,
actions,
data
);
send_openflow_message( NUM2ULL( datapath_id ), packet_out );
if ( allocated_data != NULL ) {
free_buffer( allocated_data );
}
free_buffer( packet_out );
delete_actions( actions );
return self;
}
|