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



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

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

#add_ip(addr) ⇒ Object

Parameters:

  • addr (IPAddress)


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

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)


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

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)


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

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

#down(host_veth = nil) ⇒ Object



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
196
# File 'lib/osctld/net_interface/veth.rb', line 166

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

#dup(new_ct) ⇒ Object



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

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

#fetch_veth_nameObject (protected)



251
252
253
254
255
# File 'lib/osctld/net_interface/veth.rb', line 251

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)


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

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)



302
303
304
# File 'lib/osctld/net_interface/veth.rb', line 302

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

#ifb_vethObject (protected)



257
258
259
# File 'lib/osctld/net_interface/veth.rb', line 257

def ifb_veth
  "ifb#{veth}"
end

#ips(v) ⇒ Object



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

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

#is_up?Boolean

Returns:

  • (Boolean)


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

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)

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

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


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

def load_ip_list(ip_list)
  ip_list.to_h 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
      [::Regexp.last_match(1).to_i, yield(value)]

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

#mode_path(mode) ⇒ Object (protected)



298
299
300
# File 'lib/osctld/net_interface/veth.rb', line 298

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

#rename(new_name) ⇒ Object



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

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



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

def render_opts
  inclusively do
    {
      name:,
      index:,
      hwaddr:,
      tx_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>>)


320
321
322
# File 'lib/osctld/net_interface/veth.rb', line 320

def save_ip_list(ip_list)
  ip_list.to_h { |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

  return unless opts[:max_tx] && opts[:max_tx] != orig_max_tx

  if max_tx > 0
    set_shaper_tx
  else
    unset_shaper_tx
  end
end

#set_shaper_rxObject (protected)



261
262
263
264
# File 'lib/osctld/net_interface/veth.rb', line 261

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)



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

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

  return if 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

#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
119
120
# 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, 0o711)
  rescue Errno::EEXIST
    # ignore
  end

  %w[up down].each do |v|
    begin
      Dir.mkdir(mode_path(v), 0o711)
    rescue Errno::EEXIST
      # ignore
    end

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

    if File.symlink?(symlink)
      next if File.readlink(symlink) == hook_src

      File.unlink(symlink)

    end

    File.symlink(hook_src, symlink)
  end

  return if ct.fresh_state != :running

  @veth = fetch_veth_name
end

#unset_shaper_rxObject (protected)



266
267
268
# File 'lib/osctld/net_interface/veth.rb', line 266

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

#unset_shaper_txObject (protected)



288
289
290
291
292
# File 'lib/osctld/net_interface/veth.rb', line 288

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



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

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:,
    veth:
  )
end

#veth_hook_dirObject (protected)



294
295
296
# File 'lib/osctld/net_interface/veth.rb', line 294

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