Class: Trema::Netns

Inherits:
NetworkComponent show all
Defined in:
ruby/trema/netns.rb

Overview

The controller class of network namespace

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from NetworkComponent

[], add, clear, each, inherited, size

Constructor Details

#initialize(stanza) ⇒ Netns

Creates a new Trema netns from DSL::Netns

Examples:

netns = Trema::Netns.new( stanza )


50
51
52
53
# File 'ruby/trema/netns.rb', line 50

def initialize stanza
  @stanza = stanza
  Netns.add self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(message, *args) ⇒ Object

Define netns attribute accessors

Examples:

netns.name  # delegated to @stanza[ :name ]

Returns:

  • an attribute value



66
67
68
# File 'ruby/trema/netns.rb', line 66

def method_missing message, *args
  @stanza.__send__ :[], message
end

Instance Attribute Details

#interfaceString

Set a network interface

Examples:

netns.interface #=> "trema0-1"

Returns:



37
38
39
# File 'ruby/trema/netns.rb', line 37

def interface
  @interface
end

Instance Method Details

#netmaskString

Returns netmask

Examples:

host.netmask #=> "255.255.0.0"

Returns:



81
82
83
# File 'ruby/trema/netns.rb', line 81

def netmask
  @stanza[ :netmask ] || "255.255.255.255"
end

#run!Netns

Runs a netns process

Examples:

netns.run! => self

Returns:



96
97
98
99
100
101
102
103
# File 'ruby/trema/netns.rb', line 96

def run!
  sh "sudo ip netns add #{ name }"
  sh "sudo ip link set dev #{ interface } netns #{ name }"
  sh "sudo ip netns exec #{ name } ifconfig lo 127.0.0.1"
  sh "sudo ip netns exec #{ name } ifconfig #{ interface } #{ @stanza[ :ip ] } netmask #{ netmask }" if @stanza[ :ip ]
  sh "sudo ip netns exec #{ name } route add -net #{ @stanza[ :net ] } gw #{ @stanza[ :gw ] }" if @stanza[ :net ] and @stanza[ :gw ]
  self
end

#shutdown!undefined

Kills running netns

Examples:

netns.shutdown!

Returns:

  • (undefined)


116
117
118
# File 'ruby/trema/netns.rb', line 116

def shutdown!
  sh "sudo ip netns delete #{ name }"
end