Class: OsCtld::NetInterface::Routed

Inherits:
NetInterface::Veth
  • Object
show all
Extended by:
OsCtl::Lib::Utils::System, Utils::Ip
Defined in:
lib/osctld/net_interface/routed.rb

Constant Summary collapse

INTERFACE =
'osrtr0'.freeze
DEFAULT_IPV4 =
IPAddress.parse('255.255.255.254/32')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Ip

delete_link_by_ifindex, ip, link_ifindex_by_name, tc

Instance Attribute Details

#routes ⇒ Object (readonly)

Returns the value of attribute routes.



26
27
28
# File 'lib/osctld/net_interface/routed.rb', line 26

def routes
  @routes
end

Class Method Details

.setup ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/osctld/net_interface/routed.rb', line 14

def self.setup
  begin
    ip(:all, [:link, :show, :dev, INTERFACE])
    return
  rescue SystemCommandFailed => e
    raise if e.rc != 1
  end

  ip(:all, [:link, :add, INTERFACE, :type, :dummy])
  ip(4, [:addr, :add, DEFAULT_IPV4.to_string, :dev, INTERFACE])
end

Instance Method Details

#add_ip(addr, route) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/osctld/net_interface/routed.rb', line 124

def add_ip(addr, route)
  super(addr)

  v = addr.ipv4? ? 4 : 6
  r = @routes.add(route) if route && !@routes.contains?(route)

  ct.inclusively do
    next if ct.state != :running

    # Add host route
    ip(v, %i[route add] + r.ip_spec + [:dev, veth]) if r && enable

    # Add IP within the CT
    ct_syscmd(
      ct,
      ['ip', "-#{v}", 'addr', 'add', addr.to_string, 'dev', name],
      valid_rcs: [2]
    )

    # Ensure the default route exists
    via = default_via(v)

    ct_syscmd(
      ct,
      ['ip', "-#{v}", 'route', 'add', via.to_s, 'dev', name],
      valid_rcs: [2]
    )
    ct_syscmd(
      ct,
      ['ip', "-#{v}", 'route', 'add', 'default', 'via', via.to_s, 'dev', name],
      valid_rcs: [2]
    )
  end
end

#add_route(addr, via: nil) ⇒ Object



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

def add_route(addr, via: nil)
  route = @routes.add(addr, via:)
  return unless enable

  ct.inclusively do
    next if ct.state != :running

    ip(route.ip_version, %i[route add] + route.ip_spec + [:dev, veth])
  end
end

#can_run_distconfig? ⇒ Boolean

DistConfig can be run only after the interface has been created

Returns:

  • (Boolean)


120
121
122
# File 'lib/osctld/net_interface/routed.rb', line 120

def can_run_distconfig?
  exclusively { !veth.nil? && !@host_link_tainted }
end

#create(opts) ⇒ Object

Parameters:

  • opts (Hash)

Options Hash (opts):

  • name (String)


30
31
32
33
34
# File 'lib/osctld/net_interface/routed.rb', line 30

def create(opts)
  super

  @routes = Routing::Table.new
end

#default_via(v) ⇒ IPAddress::IPv4, IPAddress::IPv6

Parameters:

  • v (4, 6) —

    IP version

Returns:

  • (IPAddress::IPv4, IPAddress::IPv6)


250
251
252
253
254
255
256
257
# File 'lib/osctld/net_interface/routed.rb', line 250

def default_via(v)
  case v
  when 4
    DEFAULT_IPV4
  when 6
    get_ipv6_link_local
  end
end

#del_all_ips(ip_v, keep_routes) ⇒ Object

Parameters:

  • ip_v (Integer, nil)


200
201
202
203
204
205
206
# File 'lib/osctld/net_interface/routed.rb', line 200

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

#del_all_routes(ip_v = nil) ⇒ Object

Parameters:

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


235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/osctld/net_interface/routed.rb', line 235

def del_all_routes(ip_v = nil)
  removed = @routes.remove_all(ip_v)
  return unless enable

  ct.inclusively do
    next if ct.state != :running

    removed.each do |route|
      ip(route.ip_version, %i[route del] + route.ip_spec + [:dev, veth])
    end
  end
end

#del_ip(addr, keep_route) ⇒ Object



159
160
161
162
163
164
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
196
197
# File 'lib/osctld/net_interface/routed.rb', line 159

def del_ip(addr, keep_route)
  super(addr)

  routes_to_remove = []
  v = addr.ipv4? ? 4 : 6

  unless keep_route
    r = @routes.remove(addr)
    routes_to_remove << r if r
  end

  # Remove all routes that are routed _via_ `addr`
  @routes.remove_version_if(v) do |route|
    if route.via && route.via.to_s == addr.to_s
      routes_to_remove << route
      true
    else
      false
    end
  end

  ct.inclusively do
    next if ct.state != :running

    # Remove host route
    if enable
      routes_to_remove.each do |route|
        ip(v, %i[route del] + route.ip_spec + [:dev, veth])
      end
    end

    # Remove IP from within the CT
    ct_syscmd(
      ct,
      ['ip', "-#{v}", 'addr', 'del', addr.to_string, 'dev', name],
      valid_rcs: [2]
    )
  end
end

#del_route(addr) ⇒ Object



223
224
225
226
227
228
229
230
231
232
# File 'lib/osctld/net_interface/routed.rb', line 223

def del_route(addr)
  route = @routes.remove(addr)
  return if !route || !enable

  ct.inclusively do
    next if ct.state != :running

    ip(route.ip_version, %i[route del] + route.ip_spec + [:dev, veth])
  end
end

#dup(new_ct) ⇒ Object



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

def dup(new_ct)
  ret = super
  ret.instance_variable_set('@routes', Routing::Table.load(routes.dump))
  ret
end


267
268
269
270
271
272
273
274
275
276
277
# File 'lib/osctld/net_interface/routed.rb', line 267

def get_ipv6_link_local
  link = exclusively { veth.clone }

  local_ifaddr = Socket.getifaddrs.detect do |ifaddr|
    ifaddr.name == link && ifaddr.addr.ip? && ifaddr.addr.ipv6?
  end

  raise "unable to find link-local IPv6 address for #{veth}" unless local_ifaddr

  IPAddress.parse(local_ifaddr.addr.ip_address.split('%').first)
end

#has_route?(addr) ⇒ Boolean

Returns:

  • (Boolean)


208
209
210
# File 'lib/osctld/net_interface/routed.rb', line 208

def has_route?(addr)
  @routes.contains?(addr)
end

#load(cfg) ⇒ Object



36
37
38
39
40
# File 'lib/osctld/net_interface/routed.rb', line 36

def load(cfg)
  super

  @routes = Routing::Table.load(cfg['routes'] || {})
end

#save ⇒ Object



42
43
44
45
46
# File 'lib/osctld/net_interface/routed.rb', line 42

def save
  super.merge({
    'routes' => @routes.dump
  })
end

#set(opts) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/osctld/net_interface/routed.rb', line 48

def set(opts)
  route_mutation_started = false

  NetInterface.sync_host_link_registry do
    orig_enable = enable

    super

    if veth && opts.has_key?(:enable) && opts[:enable] && !orig_enable
      [4, 6].each do |v|
        next if @routes.empty?(v)

        @routes.each_version(v) do |route|
          route_mutation_started = true
          ip(v, %i[route add] + route.ip_spec + [:dev, veth])
        end
      end
    end
  end
rescue StandardError
  taint_host_link! if route_mutation_started
  raise
end

#setup(discover_host_links: true) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/osctld/net_interface/routed.rb', line 72

def setup(discover_host_links: true)
  NetInterface.sync_host_link_registry do
    super

    return unless discover_host_links
    # A persisted taint is a fail-closed recovery record. In particular, do
    # not run even read-only name-based host operations against a name which
    # may now belong to an unrelated link.
    return if host_link_tainted?
    return if ct.fresh_state != :running

    iplist = ip(:all, [:addr, :show, :dev, veth], valid_rcs: [1])

    return unless iplist.exitstatus == 1

    log(
      :info,
      ct,
      "veth '#{veth}' of container '#{ct.id}' no longer exists, " \
      'ignoring'
    )
    taint_host_link_on_setup!
    nil
  end
end

#up(veth) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/osctld/net_interface/routed.rb', line 98

def up(veth)
  NetInterface.sync_host_link_registry do
    super(veth)

    if enable
      [4, 6].each do |v|
        next if @routes.empty?(v)

        @routes.each_version(v) do |route|
          ip(v, %i[route add] + route.ip_spec + [:dev, veth])
        end
      end
    end

    File.write(File.join('/proc/sys/net/ipv4/conf', veth, 'rp_filter'), '1')
  rescue StandardError
    taint_host_link!
    raise
  end
end