Ejemplo n.º 1
0
int
main( int argc, char *argv[] ) {
  init_trema( &argc, &argv );
  add_periodic_event_callback( 30, timeout, NULL );

  add_periodic_event_callback( 2, ( void ( * )( void * ) ) send_list_switches_request, NULL );
  set_list_switches_reply_handler( handle_list_switches_reply );

  start_trema();
}
Ejemplo n.º 2
0
/*
 * Starts this controller. Usually you do not need to invoke
 * explicitly, because this is called implicitly by "trema run"
 * command.
 */
static VALUE
controller_run( VALUE self ) {
  setenv( "TREMA_HOME", STR2CSTR( 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 ] = STR2CSTR( name );
  argv[ 1 ] = ( char * ) ( uintptr_t ) "--name";
  argv[ 2 ] = STR2CSTR( 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" ) ) == Qtrue ) {
    rb_funcall( self, rb_intern( "start" ), 0 );
  }

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

  return self;
}
Ejemplo n.º 3
0
static void
unregistration_retry( void *user_data ) {
  struct switch_info *sw_info = user_data;

  if ( sw_info->retry_count == 0 ) {
    stop_switch();
    return;
  }
  sw_info->retry_count--;
  sw_info->running_timer = false;
  debug( "Checking switch manager's switch list." );
  set_list_switches_reply_handler( confirm_self_dpid_is_unregistered );
  if ( send_list_switches_request( sw_info ) ) {
    switch_set_timeout( UNREGISTRATION_TIMEOUT, unregistration_timeout, sw_info );
  }
  else {
    error( "Failed to send switch list request to switch manager." );
    stop_switch();
  }
}
int
main( int argc, char *argv[] ) {
  init_trema( &argc, &argv );
  show_desc show_desc;
  show_desc.db = create_hash( compare_datapath_id, hash_datapath_id );
  show_desc.count = 0;

  set_list_switches_reply_handler( handle_list_switches_reply );
#ifdef TREMA_EDGE
  set_multipart_reply_handler( handle_stats_reply, &show_desc );
#else
  set_stats_reply_handler( handle_stats_reply, &show_desc );
  set_features_reply_handler( handle_features_reply, &show_desc );
#endif
  add_periodic_event_callback( 10, timed_out, NULL );

  send_list_switches_request( &show_desc );


  start_trema();

  return 0;
}
Ejemplo n.º 5
0
int
switch_event_disconnected( struct switch_info *sw_info ) {
  int old_state = sw_info->state;

  if ( sw_info->state == SWITCH_STATE_DISCONNECTED ||
       sw_info->state == SWITCH_STATE_CONNECTION_FAILED ) {
    debug( "already disconnected" );
    return -1;
  }

  if ( sw_info->state == SWITCH_STATE_COMPLETED ) {
    sw_info->state = SWITCH_STATE_DISCONNECTED;
  }
  else {
    sw_info->state = SWITCH_STATE_CONNECTION_FAILED;
  }

  if ( old_state == SWITCH_STATE_COMPLETED ) {
    switch_unset_timeout( echo_reply_timeout, NULL );
  }

  if ( sw_info->fragment_buf != NULL ) {
    free_buffer( sw_info->fragment_buf );
    sw_info->fragment_buf = NULL;
  }

  if ( sw_info->send_queue != NULL ) {
    delete_message_queue( sw_info->send_queue );
    sw_info->send_queue = NULL;
  }

  if ( sw_info->recv_queue != NULL ) {
    delete_message_queue( sw_info->recv_queue );
    sw_info->recv_queue = NULL;
  }

  if ( sw_info->secure_channel_fd >= 0 ) {
    set_readable( switch_info.secure_channel_fd, false );
    set_writable( switch_info.secure_channel_fd, false );
    delete_fd_handler( switch_info.secure_channel_fd );
  }

  uint8_t state = MESSENGER_OPENFLOW_DISCONNECTED;
  if ( sw_info->state == SWITCH_STATE_CONNECTION_FAILED ) {
    state = MESSENGER_OPENFLOW_FAILD_TO_CONNECT;
  }

  debug( "Notify switch_disconnected to switch manager." );
  char switch_manager[] =  SWITCH_MANAGER;
  list_element switch_manager_only_list;
  switch_manager_only_list.next = NULL;
  switch_manager_only_list.data = switch_manager;
  service_send_to_application( &switch_manager_only_list, state, &sw_info->datapath_id, NULL );

  // Check switch_manager registration
  debug( "Checking switch manager's switch list." );
  sw_info->retry_count = UNREGISTRATION_RETRY_COUNT;
  set_list_switches_reply_handler( confirm_self_dpid_is_unregistered );
  if ( send_list_switches_request( sw_info ) ) {
    switch_set_timeout( UNREGISTRATION_TIMEOUT, unregistration_timeout, sw_info );
  }
  else {
    error( "Failed to send switch list request to switch manager." );
    stop_switch();
  }

  return 0;
}
Ejemplo n.º 6
0
int
switch_event_recv_featuresreply( struct switch_info *sw_info, uint64_t *dpid ) {
  int ret;
  char new_service_name[ SWITCH_MANAGER_PREFIX_STR_LEN + SWITCH_MANAGER_DPID_STR_LEN + 1 ];
  const uint16_t new_service_name_len = SWITCH_MANAGER_PREFIX_STR_LEN + SWITCH_MANAGER_DPID_STR_LEN + 1;

  switch ( sw_info->state ) {
  case SWITCH_STATE_WAIT_FEATURES_REPLY:

    sw_info->datapath_id = *dpid;
    sw_info->state = SWITCH_STATE_WAIT_REGISTRATION;

    // cancel to features_reply_wait-timeout timer
    switch_unset_timeout( switch_event_timeout_features_reply, NULL );

    // TODO: set keepalive-timeout
    snprintf( new_service_name, new_service_name_len, "%s%#" PRIx64, SWITCH_MANAGER_PREFIX, sw_info->datapath_id );

    // checking duplicate service
    pid_t pid = get_pid_by_trema_name( new_service_name );
    if ( pid > 0 ) {
      debug( "duplicated datapath-id %#" PRIx64, sw_info->datapath_id );
      if ( !terminate_trema_process( pid ) ) {
        return -1;
      }
    }
    // rename service_name of messenger
    rename_message_received_callback( get_trema_name(), new_service_name );

    // rename management service name
    char *management_service_name = xstrdup( get_management_service_name( get_trema_name() ) );
    char *new_management_service_name = xstrdup( get_management_service_name( new_service_name ) );
    rename_message_requested_callback( management_service_name, new_management_service_name );
    xfree( management_service_name );
    xfree( new_management_service_name );

    debug( "Rename service name from %s to %s.", get_trema_name(), new_service_name );
    if ( messenger_dump_enabled() ) {
      stop_messenger_dump();
      start_messenger_dump( new_service_name, DEFAULT_DUMP_SERVICE_NAME );
    }
    set_trema_name( new_service_name );

    // reset to default config
    ret = ofpmsg_send_setconfig( sw_info );
    if ( ret < 0 ) {
      error( "Failed to send setconfig." );
      return ret;
    }
    if ( switch_info.flow_cleanup ) {
      debug( "Deleting all flows." );
      ret = ofpmsg_send_delete_all_flows( sw_info );
      if ( ret < 0 ) {
        error( "Failed to send delete all flows." );
        return ret;
      }
    }

    // switch_ready to switch_manager
    debug( "Notify switch_ready to switch manager." );
    char switch_manager[] =  SWITCH_MANAGER;
    list_element switch_manager_only_list;
    switch_manager_only_list.next = NULL;
    switch_manager_only_list.data = switch_manager;
    service_send_to_application( &switch_manager_only_list, MESSENGER_OPENFLOW_READY, &sw_info->datapath_id, NULL );

    init_event_forward_interface();
    // Check switch_manager registration
    debug( "Checking switch manager's switch list." );
    sw_info->retry_count = REGISTRATION_RETRY_COUNT;
    set_list_switches_reply_handler( confirm_self_dpid_is_registered );
    if ( send_list_switches_request( sw_info ) ) {
      switch_set_timeout( REGISTRATION_TIMEOUT, registration_timeout, sw_info );
    }
    else {
      error( "Failed to send switch list request to switch manager." );
      return -1;
    }
    break;

  case SWITCH_STATE_WAIT_REGISTRATION:
  case SWITCH_STATE_COMPLETED:
    // NOP
    break;

  default:
    notice( "Invalid event 'features reply' from a switch." );
    return -1;

    break;
  }

  return 0;
}