Class: OsCtld::NetInterface::Veth

Inherits:
Base
  • Object
show all
Includes:
OsCtl::Lib::Utils::Log, OsCtl::Lib::Utils::System, Utils::SwitchUser
Defined in:
lib/osctld/net_interface/veth.rb

Direct Known Subclasses

Bridge, Routed

Instance Attribute Summary collapse

Attributes inherited from Base

#ct, #hwaddr, #index, #name

Instance Method Summary collapse

Methods included from Utils::SwitchUser

#ct_attach, #ct_syscmd

Methods inherited from Base

#can_add_ip?, #can_run_distconfig?, #dup, #initialize, #set, setup, type, #type

Methods included from Lockable

#exclusively, included, #inclusively, #init_lock, #lock, #unlock

Constructor Details

This class inherits a constructor from OsCtld::NetInterface::Base

Instance Attribute Details

#vethObject (readonly)

Returns the value of attribute veth



10
11
12
# File 'lib/osctld/net_interface/veth.rb', line 10

def veth
  @veth
end

Instance Method Details

#active_ip_versionsObject



121
122
123
# File 'lib/osctld/net_interface/veth.rb', line 121

def active_ip_versions
  inclusively { [4, 6].delete_if { |v| @ips[v].empty? } }
end

#add_ip(addr) ⇒ Object

Parameters:

  • addr (IPAddress)


144
145
146
# File 'lib/osctld/net_interface/veth.rb', line 144

def add_ip(addr)
  exclusively { @ips[addr.ipv4? ? 4 : 6] << addr }
end

#create(opts) ⇒ Object



12
13
14
15
16
# File 'lib/osctld/net_interface/veth.rb', line 12

def create(opts)
  super

  @ips = {4 => [], 6 => []}
end

#del_all_ips(ip_v = nil) ⇒ Object

Parameters:

  • ip_v (Integer, nil) (defaults to: nil)


154
155
156
157
158
159
160
# File 'lib/osctld/net_interface/veth.rb', line 154

def del_all_ips(ip_v = nil)
  exclusively do
    (ip_v ? [ip_v] : [4, 6]).each do |v|
      @ips[v].clone.each { |addr| del_ip(addr) }
    end
  end
end

#del_ip(addr) ⇒ Object

Parameters:

  • addr (IPAddress)


149
150
151
# File 'lib/osctld/net_interface/veth.rb', line 149

def del_ip(addr)
  exclusively { @ips[addr.ipv4? ? 4 : 6].delete_if { |v| v == addr } }
end

#down(veth) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/osctld/net_interface/veth.rb', line 109

def down(veth)
  exclusively { @veth = nil }

  Eventd.report(
    :ct_netif,
    action: :down,
    pool: ct.pool.name,
    id: ct.id,
    name: name,
  )
end

#fetch_veth_nameObject (protected)



163
164
165
166
167
# File 'lib/osctld/net_interface/veth.rb', line 163

def fetch_veth_name
  v = ContainerControl::Commands::VethName.run!(ct, index)
  log(:info, ct, "Discovered name for veth ##{index}: #{v}")
  v
end

#has_ip?(addr, prefix: true) ⇒ Boolean

Parameters:

  • addr (IPAddress)
  • prefix (Boolean)

    check also address prefix

Returns:

  • (Boolean)


131
132
133
134
135
136
137
138
139
140
141
# File 'lib/osctld/net_interface/veth.rb', line 131

def has_ip?(addr, prefix: true)
  ip_v = addr.ipv4? ? 4 : 6

  exclusively do
    if prefix
      @ips[ip_v].include?(addr)
    else
      @ips[ip_v].detect { |v| v.to_s == addr.to_s } ? true : false
    end
  end
end

#hook_path(mode, name = nil) ⇒ Object (protected)



177
178
179
# File 'lib/osctld/net_interface/veth.rb', line 177

def hook_path(mode, name = nil)
  File.join(mode_path(mode), "#{@ct.id}.#{name || self.name}")
end

#ips(v) ⇒ Object



125
126
127
# File 'lib/osctld/net_interface/veth.rb', line 125

def ips(v)
  inclusively { @ips[v].clone }
end

#load(cfg) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/osctld/net_interface/veth.rb', line 18

def load(cfg)
  super

  if cfg['ip_addresses']
    @ips = load_ip_list(cfg['ip_addresses']) do |ips|
      ips.map { |ip| IPAddress.parse(ip) }
    end

  else
    @ips = {4 => [], 6 => []}
  end
end

#load_ip_list(ip_list) {|value| ... } ⇒ Hash<Integer, String>, Hash<Integer, Array<String>> (protected)

Take an IP list stored in a config file and return an internal representation, see ##save_ip_list.

Yield Parameters:

  • value (String, Array<String>)

Returns:

  • (Hash<Integer, String>, Hash<Integer, Array<String>>)


204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/osctld/net_interface/veth.rb', line 204

def load_ip_list(ip_list)
  Hash[ ip_list.map do |ip_v, value|
    # Load also integer keys for backward compatibility
    if [4, 6].include?(ip_v)
      [ip_v, yield(value)]

    elsif /^v(4|6)$/ =~ ip_v
      [$1.to_i, yield(value)]

    else
      fail "unsupported IP version '#{ip_v}': expected v4 or v6"
    end
  end]
end

#mode_path(mode) ⇒ Object (protected)



173
174
175
# File 'lib/osctld/net_interface/veth.rb', line 173

def mode_path(mode)
  File.join(veth_hook_dir, mode)
end

#rename(new_name) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/osctld/net_interface/veth.rb', line 69

def rename(new_name)
  %w(up down).each do |v|
    begin
      File.unlink(hook_path(v, name))

    rescue Errno::ENOENT
      # pass
    end

    File.symlink(OsCtld::hook_src("veth-#{v}"), hook_path(v, new_name))
  end

  super
end

#render_optsObject



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/osctld/net_interface/veth.rb', line 84

def render_opts
  inclusively do
    {
      name: name,
      index: index,
      hwaddr: hwaddr,
      hook_veth_up: hook_path('up'),
      hook_veth_down: hook_path('down'),
    }
  end
end

#saveObject



31
32
33
34
35
36
37
# File 'lib/osctld/net_interface/veth.rb', line 31

def save
  inclusively do
    super.merge(
      'ip_addresses' => save_ip_list(@ips) { |v| v.map(&:to_string) },
    )
  end
end

#save_ip_list(ip_list) {|value| ... } ⇒ Hash<String, String>, Hash<String, Array<String>> (protected)

Take an internal representation of an IP list and return a version to store in the config file.

The internal representation is a hash, where keys are IP versions as integer and the yielded value is either a list of addresses, i.e. an array of string, or just one address (string). The caller decides how to encode the value.

The returned hash has IP versions in the hash encoded as strings, i.e. `v4` or v6`. This is to allow storing the config in JSON, which does not support integer object keys.

Yield Parameters:

  • value (String, Array<String>)

Returns:

  • (Hash<String, String>, Hash<String, Array<String>>)


195
196
197
# File 'lib/osctld/net_interface/veth.rb', line 195

def save_ip_list(ip_list)
  Hash[ip_list.map { |ip_v, value| ["v#{ip_v}", yield(value)] }]
end

#setupObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/osctld/net_interface/veth.rb', line 39

def setup
  # Setup links for veth up/down hooks in rundir
  #
  # Because a CT can have multiple veth interfaces and they can be of
  # different types, we need to create hooks for specific veth interfaces,
  # so that we can identify which veth was the hook called for. We simply
  # symlink the hook to rundir and the symlink's name identifies the veth.
  Dir.mkdir(veth_hook_dir, 0711) unless Dir.exist?(veth_hook_dir)

  %w(up down).each do |v|
    Dir.mkdir(mode_path(v), 0711) unless Dir.exist?(mode_path(v))

    symlink = hook_path(v)
    hook_src = OsCtld::hook_src("veth-#{v}")

    if File.symlink?(symlink)
      if File.readlink(symlink) == hook_src
        next
      else
        File.unlink(symlink)
      end
    end

    File.symlink(hook_src, symlink)
  end

  return if ct.current_state != :running
  @veth = fetch_veth_name
end

#up(veth) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/osctld/net_interface/veth.rb', line 96

def up(veth)
  exclusively { @veth = veth }

  Eventd.report(
    :ct_netif,
    action: :up,
    pool: ct.pool.name,
    id: ct.id,
    name: name,
    veth: veth,
  )
end

#veth_hook_dirObject (protected)



169
170
171
# File 'lib/osctld/net_interface/veth.rb', line 169

def veth_hook_dir
  File.join(ct.pool.hook_dir, 'veth')
end