Module: Trema::Command

Includes:
Util
Defined in:
ruby/trema/command/up.rb,
ruby/trema/command/run.rb,
ruby/trema/command/kill.rb,
ruby/trema/command/ruby.rb,
ruby/trema/command/netns.rb,
ruby/trema/command/shell.rb,
ruby/trema/command/killall.rb,
ruby/trema/command/port_up.rb,
ruby/trema/command/version.rb,
ruby/trema/command/port_down.rb,
ruby/trema/command/dump_flows.rb,
ruby/trema/command/show_stats.rb,
ruby/trema/command/reset_stats.rb,
ruby/trema/command/send_packets.rb

Instance Method Summary collapse

Methods included from Util

#assert_trema_is_built, #cleanup, #cleanup_current_session, #find_app_by_name, #find_host_by_name, #find_switch_by_name, #running?, #sh

Instance Method Details

#shellObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'ruby/trema/command/shell.rb', line 30

def shell
  begin
    undef :kill

    require "tempfile"
    require "trema"
    require "trema/shell"
    f = Tempfile.open( "irbrc" )
    f.print "include Trema::Shell\nENV[ \"TREMA_HOME\" ] = Trema.home\n$config = Trema::DSL::Configuration.new\n$context = Trema::DSL::Context.new( $config )\n"
    f.close
    load f.path
    IRB.start
  ensure
    cleanup $config
  end
end

#trema_dump_flows(switches) ⇒ Object



28
29
30
31
32
33
34
# File 'ruby/trema/command/dump_flows.rb', line 28

def trema_dump_flows switches
  switches.each do | each |
    switch = find_switch_by_name( each )
    exit_now! "No switch named `#{ each }` found!" if switch.nil?
    puts Trema::Ofctl.new.dump_flows( switch )
  end
end

#trema_kill(name) ⇒ Object



27
28
29
30
31
# File 'ruby/trema/command/kill.rb', line 27

def trema_kill name
  unless maybe_kill( name )
    exit_now! "unknown name: #{ name }"
  end
end

#trema_killallObject



27
28
29
# File 'ruby/trema/command/killall.rb', line 27

def trema_killall
  cleanup_current_session
end

#trema_netns(netns) ⇒ Object



27
28
29
# File 'ruby/trema/command/netns.rb', line 27

def trema_netns netns
  sh "sudo ip netns exec #{ netns } #{ ENV[ 'SHELL' ] }"
end

#trema_port_down(switch_name, port) ⇒ Object



28
29
30
31
32
33
34
# File 'ruby/trema/command/port_down.rb', line 28

def trema_port_down switch_name, port
  switch = find_switch_by_name( switch_name )
  raise "unknown switch: #{ switch_name }" if switch.nil?

  error = switch.bring_port_down( port.to_i )
  raise error if $?.exitstatus != 0
end

#trema_port_up(switch_name, port) ⇒ Object



28
29
30
31
32
33
34
# File 'ruby/trema/command/port_up.rb', line 28

def trema_port_up switch_name, port
  switch = find_switch_by_name( switch_name )
  raise "unknown switch: #{ switch_name }" if switch.nil?

  error = switch.bring_port_up( port.to_i )
  raise error if $?.exitstatus != 0
end

#trema_reset_stats(name) ⇒ Object



28
29
30
31
32
# File 'ruby/trema/command/reset_stats.rb', line 28

def trema_reset_stats name
  host = find_host_by_name( name )
  raise "unknown host: #{ name }" if host.nil?
  Trema::Cli.new( host ).reset_stats
end

#trema_rubyObject



27
28
29
# File 'ruby/trema/command/ruby.rb', line 27

def trema_ruby
  sh "x-www-browser http://rubydoc.info/github/trema/trema/master/frames"
end

#trema_run(options) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'ruby/trema/command/run.rb', line 28

def trema_run options
  @config_file = options[ :conf ] || nil

  if options[ :daemonize ]
    $run_as_daemon = true
  end
  if options[ :tremashark ]
    $use_tremashark = true
  end

  need_cleanup = ( not running? )

  if $run_as_daemon
    Trema::DSL::Runner.new( load_config ).daemonize
  else
    begin
      Trema::DSL::Runner.new( load_config ).run
    rescue SystemExit
      # This is OK
    ensure
      cleanup_current_session if need_cleanup
    end
  end
end

#trema_send_packets(source_name, dest_name, options) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'ruby/trema/command/send_packets.rb', line 29

def trema_send_packets source_name, dest_name, options
  source = find_host_by_name( source_name )
  raise "unknown host: #{ source_name }" if source.nil?

  dest = find_host_by_name( dest_name )
  raise "unknown host: #{ dest_name }" if dest.nil?

  Trema::Cli.new( source ).send_packets( dest, options )
end

#trema_show_stats(hostname, tx, rx) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'ruby/trema/command/show_stats.rb', line 28

def trema_show_stats hostname, tx, rx
  cli = get_cli( hostname )

  unless tx || rx
    show_tx_and_rx cli
  end
  cli.show_tx_stats if tx
  cli.show_rx_stats if rx
end

#trema_up(name) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'ruby/trema/command/up.rb', line 27

def trema_up name
  switch = find_switch_by_name( name )
  if switch
    switch.run
  else
    # TODO: support vlink
    raise "Unknown name: #{ name }"
  end
end

#trema_versionObject



24
25
26
# File 'ruby/trema/command/version.rb', line 24

def trema_version
  puts "trema version #{ Trema::VERSION }"
end