Class: OsCtld::NetInterface::Routed

Inherits:
Veth
  • Object
show all
Extended by:
OsCtl::Lib::Utils::System, Utils::Ip
Includes:
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

#veth

Attributes inherited from Base

#ct, #hwaddr, #index, #name

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Ip

ip

Methods inherited from Veth

#active_ip_versions, #down, #fetch_veth_name, #has_ip?, #hook_path, #ips, #load_ip_list, #mode_path, #rename, #render_opts, #save_ip_list, #veth_hook_dir

Methods included from Utils::SwitchUser

#ct_attach, #ct_control, #ct_exec, #ct_runscript, #ct_syscmd, #init_script, #unlink_file

Methods inherited from Base

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



29
30
31
# File 'lib/osctld/net_interface/routed.rb', line 29

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



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

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} dev #{name}",
      valid_rcs: [2]
    )
    ct_syscmd(
      ct,
      "ip -#{v} route add default via #{via} dev #{name}",
      valid_rcs: [2]
    )
  end
end

#add_route(addr, via: nil) ⇒ Object



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

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)


85
86
87
# File 'lib/osctld/net_interface/routed.rb', line 85

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

#create(opts) ⇒ Object

Parameters:

  • opts (Hash)

Options Hash (opts):

  • name (String)


33
34
35
36
37
# File 'lib/osctld/net_interface/routed.rb', line 33

def create(opts)
  super

  @routes = Routing::Table.new
end

#default_via(v) ⇒ Object



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)


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

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)


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

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



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

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



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

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 ifaddr.addr.ip_address.split('%').first
end

#has_route?(addr) ⇒ Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/osctld/net_interface/routed.rb', line 171

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

#load(cfg) ⇒ Object



39
40
41
42
43
# File 'lib/osctld/net_interface/routed.rb', line 39

def load(cfg)
  super

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

#saveObject



45
46
47
48
49
# File 'lib/osctld/net_interface/routed.rb', line 45

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

#setupObject



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

def setup
  super

  return if ct.current_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



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

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