Class: OsCtld::NetInterface::Routed

Inherits:
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

Attributes inherited from Veth

#rx_queues, #tx_queues, #veth

Attributes inherited from Base

#ct, #enable, #hwaddr, #index, #max_rx, #max_tx, #name

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Ip

ip, tc

Methods inherited from Veth

#active_ip_versions, #down, #dup, #fetch_veth_name, #has_ip?, #hook_path, #ifb_veth, #ips, #is_created?, #load_ip_list, #mode_path, #rename, #render_opts, #save_ip_list, #set_shaper_rx, #set_shaper_tx, #unset_shaper_rx, #unset_shaper_tx, #veth_hook_dir

Methods included from Utils::SwitchUser

#ct_attach, #ct_syscmd

Methods inherited from Base

#can_add_ip?, #down, #dup, #has_ip?, #initialize, #ips, #is_created?, #is_up?, #rename, #render_opts, 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

#routesObject (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

.setupObject



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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/osctld/net_interface/routed.rb', line 108

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



196
197
198
199
200
201
202
203
204
205
# File 'lib/osctld/net_interface/routed.rb', line 196

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)


104
105
106
# File 'lib/osctld/net_interface/routed.rb', line 104

def can_run_distconfig?
  exclusively { !veth.nil? }
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)


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

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)


184
185
186
187
188
189
190
# File 'lib/osctld/net_interface/routed.rb', line 184

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)


219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/osctld/net_interface/routed.rb', line 219

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



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/osctld/net_interface/routed.rb', line 143

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



207
208
209
210
211
212
213
214
215
216
# File 'lib/osctld/net_interface/routed.rb', line 207

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


245
246
247
248
249
250
251
252
253
254
255
# File 'lib/osctld/net_interface/routed.rb', line 245

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)


192
193
194
# File 'lib/osctld/net_interface/routed.rb', line 192

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

#saveObject



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
# File 'lib/osctld/net_interface/routed.rb', line 48

def set(opts)
  orig_enable = enable

  super

  # rubocop:disable Style/GuardClause

  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|
        ip(v, %i[route add] + route.ip_spec + [:dev, veth])
      end
    end
  end

  # rubocop:enable Style/GuardClause
end

#setupObject



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

def setup
  super

  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'
  )
  @veth = nil
  nil
end

#up(veth) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/osctld/net_interface/routed.rb', line 87

def up(veth)
  super

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