Class: OsCtld::NetInterface::Veth

Inherits:
Base
  • Object
show all
Includes:
OsCtl::Lib::Utils::Log, OsCtl::Lib::Utils::System, Utils::Ip, 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, #max_rx, #max_tx, #name

Instance Method Summary collapse

Methods included from Utils::SwitchUser

#ct_attach, #ct_syscmd

Methods included from Utils::Ip

#ip, #tc

Methods inherited from Base

#can_add_ip?, #can_run_distconfig?, #initialize, #is_down?, 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

#rx_queuesInteger (readonly)

Number of receive queues

Returns:

  • (Integer)


19
20
21
# File 'lib/osctld/net_interface/veth.rb', line 19

def rx_queues
  @rx_queues
end

#tx_queuesInteger (readonly)

Number of transmit queues

Returns:

  • (Integer)


15
16
17
# File 'lib/osctld/net_interface/veth.rb', line 15

def tx_queues
  @tx_queues
end

#vethObject (readonly)

Returns the value of attribute veth.



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

def veth
  @veth
end

Instance Method Details

#active_ip_versionsObject



201
202
203
# File 'lib/osctld/net_interface/veth.rb', line 201

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

#add_ip(addr) ⇒ Object

Parameters:

  • addr (IPAddress)


224
225
226
# File 'lib/osctld/net_interface/veth.rb', line 224

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

#create(opts) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/osctld/net_interface/veth.rb', line 21

def create(opts)
  super

  @tx_queues = opts.fetch(:tx_queues, 1)
  @rx_queues = opts.fetch(:rx_queues, 1)
  @ips = {4 => [], 6 => []}
end

#del_all_ips(ip_v = nil) ⇒ Object

Parameters:

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


234
235
236
237
238
239
240
# File 'lib/osctld/net_interface/veth.rb', line 234

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)


229
230
231
# File 'lib/osctld/net_interface/veth.rb', line 229

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

#down(host_veth = nil) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/osctld/net_interface/veth.rb', line 165

def down(host_veth = nil)
  veth_name = host_veth || veth
  ifb_name = ifb_veth

  exclusively { @veth = nil }

  # lxc-user-nic does not support deletion of veth interfaces, delete it ourselves
  log(:info, ct, "Removing host veth #{veth_name}")

  begin
    ip(:all, %W(link del #{veth_name}))
  rescue SystemCommandFailed => e
    log(:warn, ct, "Unable to delete host veth #{veth_name}: #{e.message}")
  end

  if max_tx > 0
    begin
      ip(:all, %W(link del #{ifb_name}))
    rescue SystemCommandFailed => e
      log(:warn, ct, "Unable to delete ifb host veth #{ifb_name}: #{e.message}")
    end
  end

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

#dup(new_ct) ⇒ Object



242
243
244
245
246
# File 'lib/osctld/net_interface/veth.rb', line 242

def dup(new_ct)
  ret = super(new_ct)
  ret.instance_variable_set('@veth', nil)
  ret
end

#fetch_veth_nameObject (protected)



249
250
251
252
253
# File 'lib/osctld/net_interface/veth.rb', line 249

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) (defaults to: true)

    check also address prefix

Returns:

  • (Boolean)


211
212
213
214
215
216
217
218
219
220
221
# File 'lib/osctld/net_interface/veth.rb', line 211

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)



300
301
302
# File 'lib/osctld/net_interface/veth.rb', line 300

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

#ifb_vethObject (protected)



255
256
257
# File 'lib/osctld/net_interface/veth.rb', line 255

def ifb_veth
  "ifb#{veth}"
end

#ips(v) ⇒ Object



205
206
207
# File 'lib/osctld/net_interface/veth.rb', line 205

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

#is_up?Boolean

Returns:

  • (Boolean)


197
198
199
# File 'lib/osctld/net_interface/veth.rb', line 197

def is_up?
  inclusively { !veth.nil? }
end

#load(cfg) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/osctld/net_interface/veth.rb', line 29

def load(cfg)
  super

  @tx_queues = cfg.fetch('tx_queues', 1)
  @rx_queues = cfg.fetch('rx_queues', 1)

  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>>)


327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/osctld/net_interface/veth.rb', line 327

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)



296
297
298
# File 'lib/osctld/net_interface/veth.rb', line 296

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

#rename(new_name) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/osctld/net_interface/veth.rb', line 120

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



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/osctld/net_interface/veth.rb', line 135

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

#saveObject



45
46
47
48
49
50
51
52
53
# File 'lib/osctld/net_interface/veth.rb', line 45

def save
  inclusively do
    super.merge(
      'tx_queues' => tx_queues,
      'rx_queues' => rx_queues,
      '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>>)


318
319
320
# File 'lib/osctld/net_interface/veth.rb', line 318

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

#set(opts) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/osctld/net_interface/veth.rb', line 55

def set(opts)
  @tx_queues = opts[:tx_queues] if opts[:tx_queues]
  @rx_queues = opts[:rx_queues] if opts[:rx_queues]

  orig_max_rx = max_rx
  orig_max_tx = max_tx

  # max_tx/rx is assigned by the parent
  super

  return if veth.nil?

  if opts[:max_rx] && opts[:max_rx] != orig_max_rx
    if max_rx > 0
      set_shaper_rx
    else
      unset_shaper_rx
    end
  end

  if opts[:max_tx] && opts[:max_tx] != orig_max_tx
    if max_tx > 0
      set_shaper_tx
    else
      unset_shaper_tx
    end
  end
end

#set_shaper_rxObject (protected)



259
260
261
262
# File 'lib/osctld/net_interface/veth.rb', line 259

def set_shaper_rx
  tc(%W(qdisc delete root dev #{veth}), valid_rcs: [2])
  tc(%W(qdisc add root dev #{veth} cake bandwidth #{max_rx}bit))
end

#set_shaper_txObject (protected)



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/osctld/net_interface/veth.rb', line 268

def set_shaper_tx
  ifb_exists = Dir.exist?("/sys/devices/virtual/net/#{ifb_veth}")

  unless ifb_exists
    ip(:all, %W(link add name #{ifb_veth} type ifb))
    tc(%W(qdisc del dev #{veth} ingress), valid_rcs: [2])
    tc(%W(qdisc add dev #{veth} handle ffff: ingress))
  end

  tc(%W(qdisc del dev #{ifb_veth} root), valid_rcs: [2])
  tc(%W(qdisc add dev #{ifb_veth} root cake bandwidth #{max_tx}bit besteffort))

  unless ifb_exists
    ip(:all, %W(link set #{ifb_veth} up))
    tc(%W(filter add dev #{veth} parent ffff: matchall action mirred egress redirect dev #{ifb_veth}))
  end
end

#setupObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/osctld/net_interface/veth.rb', line 84

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.
  begin
    Dir.mkdir(veth_hook_dir, 0711)
  rescue Errno::EEXIST
  end

  %w(up down).each do |v|
    begin
      Dir.mkdir(mode_path(v), 0711)
    rescue Errno::EEXIST
    end

    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.fresh_state != :running
  @veth = fetch_veth_name
end

#unset_shaper_rxObject (protected)



264
265
266
# File 'lib/osctld/net_interface/veth.rb', line 264

def unset_shaper_rx
  tc(%W(qdisc delete root dev #{veth}), valid_rcs: [2])
end

#unset_shaper_txObject (protected)



286
287
288
289
290
# File 'lib/osctld/net_interface/veth.rb', line 286

def unset_shaper_tx
  tc(%W(filter delete dev #{veth} parent ffff:))
  tc(%W(qdisc delete dev #{veth} handle ffff: ingress))
  ip(:all, %W(link del #{ifb_veth}))
end

#up(veth) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/osctld/net_interface/veth.rb', line 149

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

  set_shaper_rx if max_rx > 0
  set_shaper_tx if max_tx > 0

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

#veth_hook_dirObject (protected)



292
293
294
# File 'lib/osctld/net_interface/veth.rb', line 292

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