Class: Trema::Controller Abstract

Inherits:
App
  • Object
show all
Includes:
Timers, DefaultLogger
Defined in:
ruby/trema/controller.rb,
ruby/trema/controller.c

Overview

This class is abstract.

The base class of Trema controller. Subclass and override handlers to implement a custom OpenFlow controller.

Constant Summary

OFPP_MAX =
INT2NUM( OFPP_MAX )
OFPP_IN_PORT =
INT2NUM( OFPP_IN_PORT )
OFPP_TABLE =
INT2NUM( OFPP_TABLE )
OFPP_NORMAL =
INT2NUM( OFPP_NORMAL )
OFPP_FLOOD =
INT2NUM( OFPP_FLOOD )
OFPP_ALL =
INT2NUM( OFPP_ALL )
OFPP_CONTROLLER =
INT2NUM( OFPP_CONTROLLER )
OFPP_LOCAL =
INT2NUM( OFPP_LOCAL )
OFPP_NONE =
INT2NUM( OFPP_NONE )

Instance Method Summary (collapse)

Methods included from DefaultLogger

#critical, #debug, #error, #info, #notice, #warn

Instance Method Details

- (Object) barrier_reply(datapath_id, message)

This method is abstract.

Barrier Reply message handler. Override this to implement a custom handler.

Parameters:

  • datapath_id (Integer)

    the datapath from which a message is sent.

  • message (BarrierReply)

    the Barrier Reply message.



165
# File 'ruby/trema/controller.rb', line 165

handler :barrier_reply

- (Object) daemonize!

Run as a daemon.



226
227
228
229
230
231
232
233
234
235
236
# File 'ruby/trema/controller.rb', line 226

def daemonize!
  fork do
    ::Process.setsid
    fork do
      STDIN.close
      STDOUT.reopen "/dev/null", "a"
      STDERR.reopen "/dev/null", "a"
      self.run!
    end
  end
end

- (Object) features_reply(datapath_id, message)

This method is abstract.

Features Reply message handler. Override this to implement a custom handler.

Parameters:

  • datapath_id (Integer)

    the datapath from which a message is sent.

  • message (FeaturesReply)

    the Features Reply message.



139
# File 'ruby/trema/controller.rb', line 139

handler :features_reply

- (Object) flow_removed(datapath_id, message)

This method is abstract.

Flow Removed message handler. Override this to implement a custom handler.

Parameters:

  • datapath_id (Integer)

    the datapath from which a message is sent.

  • message (FlowRemoved)

    the Flow Removed message.



100
# File 'ruby/trema/controller.rb', line 100

handler :flow_removed

- (Object) get_config_reply(datapath_id, message)

This method is abstract.

Get Config Reply message handler. Override this to implement a custom handler.

Parameters:

  • datapath_id (Integer)

    the datapath from which a message is sent.

  • message (GetConfigReply)

    the Get Config Reply message.



178
# File 'ruby/trema/controller.rb', line 178

handler :get_config_reply

- (Object) list_switches_reply(datapath_ids)

This method is abstract.

List Switches Reply message handler. Override this to implement a custom handler.

Parameters:

  • datapath_ids (Array<Integer>)

    the datapath IDs of connected OpenFlow switches.



74
# File 'ruby/trema/controller.rb', line 74

handler :list_switches_reply

- (String) name

Name of the controller.

Returns:



244
245
246
# File 'ruby/trema/controller.rb', line 244

def name
  self.class.to_s.split( "::" ).last
end

- (Object) openflow_error(datapath_id, message)

This method is abstract.

OpenFlow Error message handler. Override this to implement a custom handler.

Parameters:

  • datapath_id (Integer)

    the datapath from which a message is sent.

  • message (OpenflowError)

    the OpenFlow Error message.



126
# File 'ruby/trema/controller.rb', line 126

handler :openflow_error

- (Object) packet_in(datapath_id, message)

This method is abstract.

Packet In message handler. Override this to implement a custom handler.

Parameters:

  • datapath_id (Integer)

    the datapath from which a message is sent.

  • message (PacketIn)

    the Packet In message.



87
# File 'ruby/trema/controller.rb', line 87

handler :packet_in

- (Object) port_status(datapath_id, message)

This method is abstract.

Port Status message handler. Override this to implement a custom handler.

Parameters:

  • datapath_id (Integer)

    the datapath from which a message is sent.

  • message (PortStatus)

    the Port Status message.



113
# File 'ruby/trema/controller.rb', line 113

handler :port_status

- (Object) queue_get_config_reply(datapath_id, message)

This method is abstract.

Queue Get Config Reply message handler. Override this to implement a custom handler.

Parameters:

  • datapath_id (Integer)

    the datapath from which a message is sent.

  • message (QueueGetConfigReply)

    the Queue Get Config Reply message.



191
# File 'ruby/trema/controller.rb', line 191

handler :queue_get_config_reply

- (Object) run!

Starts this controller. Usually you do not need to invoke explicitly, because this is called implicitly by “trema run” command.



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
# File 'ruby/trema/controller.c', line 552

static VALUE
controller_run( VALUE self ) {
  setenv( "TREMA_HOME", RSTRING_PTR( rb_funcall( mTrema, rb_intern( "home" ), 0 ) ), 1 );

  VALUE name = rb_funcall( self, rb_intern( "name" ), 0 );
  rb_gv_set( "$PROGRAM_NAME", name );

  int argc = 3;
  char **argv = xmalloc( sizeof( char * ) * ( uint32_t ) ( argc + 1 ) );
  argv[ 0 ] = RSTRING_PTR( name );
  argv[ 1 ] = ( char * ) ( uintptr_t ) "--name";
  argv[ 2 ] = RSTRING_PTR( name );
  argv[ 3 ] = NULL;
  init_trema( &argc, &argv );
  xfree( argv );

  set_switch_ready_handler( handle_switch_ready, ( void * ) self );
  set_features_reply_handler( handle_features_reply, ( void * ) self );
  set_packet_in_handler( handle_packet_in, ( void * ) self );
  set_flow_removed_handler( handle_flow_removed, ( void * ) self );
  set_switch_disconnected_handler( handle_switch_disconnected, ( void * ) self );
  set_port_status_handler( handle_port_status, ( void * ) self );
  set_stats_reply_handler( handle_stats_reply, ( void * ) self );
  set_error_handler( handle_openflow_error, ( void * ) self );
  set_get_config_reply_handler( handle_get_config_reply, ( void * ) self );
  set_barrier_reply_handler( handle_barrier_reply, ( void * ) self );
  set_vendor_handler( handle_vendor, ( void * ) self );
  set_queue_get_config_reply_handler( handle_queue_get_config_reply, ( void * ) self );
  set_list_switches_reply_handler( handle_list_switches_reply );

  struct itimerspec interval;
  interval.it_interval.tv_sec = 1;
  interval.it_interval.tv_nsec = 0;
  interval.it_value.tv_sec = 0;
  interval.it_value.tv_nsec = 0;
  add_timer_event_callback( &interval, handle_timer_event, ( void * ) self );

  if ( rb_respond_to( self, rb_intern( "start" ) ) ) {
    rb_funcall( self, rb_intern( "start" ), 0 );
  }

  rb_funcall( self, rb_intern( "start_trema" ), 0 );

  return self;
}

- (Object) send_flow_mod_add(datapath_id, options = {})

Sends a flow_mod message to add a flow into the datapath.

Examples:

def packet_in datapath_id, message
  send_flow_mod_add datapath_id, :match => Match.from(message), :actions => SendOutPort.new(OFPP_FLOOD)
end

Parameters:

  • datapath_id (Number)

    the datapath to which a message is sent.

  • options (Hash) (defaults to: {})

    the options to create a message with.

Options Hash (options):

  • :transaction_id (Number, nil) — default: nil

    Transaction ids, message sequence numbers matching requests to replies. nil means automatically generate transaction ID.

  • :match (Match, nil) — default: nil

    A Match object describing the fields of the flow.

  • :idle_timeout (Number) — default: 0

    The idle time in seconds before discarding. Zero means flow never expires.

  • :cookie (Number)

    An opaque issued identifier.

  • :hard_timeout (Number) — default: 0

    The maximum time before discarding in seconds. Zero means flow never expires.

  • :priority (Number) — default: 0xffff

    The priority level of the flow entry.

  • :buffer_id (Number) — default: 0xffffffff

    The buffer ID assigned by the datapath of a buffered packet to apply the flow to. If 0xffffffff, no buffered packet is to be applied to flow actions.

  • :out_port (Number) — default: 0xffff

    If the option contains a value other than OFPP_NONE(0xffff), it introduces a constraint when deleting flow entries.

  • :send_flow_rem (Boolean) — default: true

    If true, send a flow_removed message when the flow expires or is deleted.

  • :check_overlap (Boolean) — default: false

    If true, check for overlapping entries first, i.e. if there are conflicting entries with the same priority, the flow is not added and the modification fails.

  • :emerg (Boolean) — default: false

    If true, the switch must consider this flow entry as an emergency entry, and only use it for forwarding when disconnected from the controller.

  • :actions (SendOutPort, Array<SendOutPort>, nil) — default: nil

    The sequence of actions specifying the actions to perform on the flow's packets.



365
366
367
368
# File 'ruby/trema/controller.c', line 365

static VALUE
controller_send_flow_mod_add( int argc, VALUE *argv, VALUE self ) {
  return controller_send_flow_mod( OFPFC_ADD, argc, argv, self );
}

- (Object) send_flow_mod_delete(datapath_id, options = {})

Sends a flow_mod_delete message to delete all matching flows. Both flow_mod delete and flow_mod delete strict commands would delete matched flows. The strict option adds the flow priority to the matched criteria. Accepts the same options as #send_flow_mod_add with the following additional option.

Options Hash (options):

  • :strict (Symbol)

    If set to true delete_strict command is invoked otherwise the delete command is invoked.



406
407
408
409
410
411
412
413
414
# File 'ruby/trema/controller.c', line 406

static VALUE
controller_send_flow_mod_delete( int argc, VALUE *argv, VALUE self ) {
  uint16_t command = OFPFC_DELETE;

  if ( get_strict( argc, argv ) == Qtrue ) {
    command = OFPFC_DELETE_STRICT;
  }
  return controller_send_flow_mod( command, argc, argv, self );
}

- (Object) send_flow_mod_modify(datapath, options = {})

Sends a flow_mod message to either modify or modify strict a flow from datapath. Both flow_mod modify and flow_mod modify strict commands would modify matched flow actions. The strict option adds the flow priority to the matched criteria. Accepts the same options as #send_flow_mod_add with the following additional option.

Options Hash (options):

  • :strict (Symbol)

    If set to true modify_strict command is invoked otherwise the modify command is invoked.



383
384
385
386
387
388
389
390
391
# File 'ruby/trema/controller.c', line 383

static VALUE
controller_send_flow_mod_modify( int argc, VALUE *argv, VALUE self ) {
  uint16_t command = OFPFC_MODIFY;

  if ( get_strict( argc, argv ) == Qtrue ) {
    command = OFPFC_MODIFY_STRICT;
  }
  return controller_send_flow_mod( command, argc, argv, self );
}

- (Object) send_list_switches_request



74
75
76
77
78
# File 'ruby/trema/controller.c', line 74

static VALUE
controller_send_list_switches_request( VALUE self ) {
  send_list_switches_request( ( void * ) self );
  return self;
}

- (Object) send_message(datapath_id, message)

Sends an OpenFlow message to the datapath.

Examples:

send_message datapath_id, FeaturesRequest.new

Parameters:



65
66
67
68
69
70
71
# File 'ruby/trema/controller.c', line 65

static VALUE
controller_send_message( VALUE self, VALUE datapath_id, VALUE message ) {
  buffer *buf;
  Data_Get_Struct( message, buffer, buf );
  send_openflow_message( NUM2ULL( datapath_id ), buf );
  return self;
}

- (Object) send_packet_out(datapath_id, options = {})

Sends a packet_out message to have a packet processed by the datapath.

Examples:

send_packet_out(
  datapath_id,
  :packet_in => message,
  :actions => Trema::SendOutPort.new(port_no)
)

Parameters:

  • datapath_id (Number)

    the datapath to which a message is sent.

  • options (Hash) (defaults to: {})

    the options to create a message with.

Options Hash (options):

  • :packet_in (PacketIn) — default: nil

    The PacketIn object received by packet_in handler. If this option is not nil, :buffer_id, :data, and :in_port option is set automatically according to the value of :packet_in.

  • :in_port (Number) — default: OFPP_NONE

    The port to use in flow table lookup when :actions send the packet to the special OFPP_TABLE port.

  • :buffer_id (Number) — default: 0xffffffff

    The buffer ID assigned by the datapath. If 0xffffffff, the frame is not buffered, and the entire frame must be passed in :data.

  • :data (String, nil) — default: nil

    The entire Ethernet frame. Should be of length 0 if buffer_id is 0xffffffff, and should be of length >0 otherwise.

  • :actions (Action, Array<Action>, nil) — default: nil

    The sequence of actions specifying the actions to perform on the frame.

  • :zero_padding (Boolean) — default: false

    If true, fill up to minimum ethernet frame size.



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
# File 'ruby/trema/controller.c', line 461

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;
}

- (Object) shutdown!

In the context of trema framework stops this controller and its applications.



603
604
605
606
607
# File 'ruby/trema/controller.c', line 603

static VALUE
controller_shutdown( VALUE self ) {
  stop_trema();
  return self;
}

- (Object) stats_reply(datapath_id, message)

This method is abstract.

Stats Reply message handler. Override this to implement a custom handler.

Parameters:

  • datapath_id (Integer)

    the datapath from which a message is sent.

  • message (StatsReply)

    the Stats Reply message.



152
# File 'ruby/trema/controller.rb', line 152

handler :stats_reply

- (Object) switch_disconnected(datapath_id)

This method is abstract.

Switch Disconnected event handler. Override this to implement a custom handler.

Parameters:

  • datapath_id (Integer)

    the datapath ID of disconnected OpenFlow switch.



62
# File 'ruby/trema/controller.rb', line 62

handler :switch_disconnected

- (Object) switch_ready(datapath_id)

This method is abstract.

Switch Ready event handler. Override this to implement a custom handler.

Parameters:

  • datapath_id (Integer)

    the datapath ID of connected OpenFlow switch.



51
# File 'ruby/trema/controller.rb', line 51

handler :switch_ready

- (Object) vendor(datapath_id, message)

This method is abstract.

Vendor message handler. Override this to implement a custom handler.

Parameters:

  • datapath_id (Integer)

    the datapath from which a message is sent.

  • message (Vendor)

    the Vendor message.



204
# File 'ruby/trema/controller.rb', line 204

handler :vendor