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'
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, #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_up?, #load_ip_list, #mode_path, #rename, #render_opts, #save_ip_list, #set, #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_down?, #is_up?, #rename, #render_opts, #set, 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.



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

def routes
  @routes
end

Class Method Details

.setupObject



14
15
16
17
18
19
20
21
22
23
24
25
# 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



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/routed.rb', line 87

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, [:route, :add] + r.ip_spec + [:dev, veth]) if r

    # 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



173
174
175
176
177
178
179
180
181
# File 'lib/osctld/net_interface/routed.rb', line 173

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

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

    ip(route.ip_version, [: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)


83
84
85
# File 'lib/osctld/net_interface/routed.rb', line 83

def can_run_distconfig?
  exclusively { !veth.nil? }
end

#create(opts) ⇒ Object

Parameters:

  • opts (Hash)

Options Hash (opts):

  • name (String)


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

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)


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

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)


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

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)


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

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

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

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

#del_ip(addr, keep_route) ⇒ Object



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

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
    routes_to_remove.each do |route|
      ip(v, [:route, :del] + route.ip_spec + [:dev, veth])
    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



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

def del_route(addr)
  route = @routes.remove(addr)
  return unless route

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

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


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

def get_ipv6_link_local
  link = exclusively { veth.clone }

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

  fail "unable to find link-local IPv6 address for #{veth}" unless ifaddr

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

#has_route?(addr) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#load(cfg) ⇒ Object



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

def load(cfg)
  super

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

#saveObject



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

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

#setupObject



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 49

def setup
  super

  return if ct.fresh_state != :running

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

  if iplist.exitstatus == 1
    log(
      :info,
      ct,
      "veth '#{veth}' of container '#{ct.id}' no longer exists, "+
      "ignoring"
    )
    @veth = nil
    return
  end
end

#up(veth) ⇒ Object



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

def up(veth)
  super

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

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

  File.write(File.join('/proc/sys/net/ipv4/conf', veth, 'rp_filter'), '1')
end