Class: OsCtld::NetInterface::Routed
- Inherits:
-
Veth
- Object
- Base
- Veth
- OsCtld::NetInterface::Routed
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, #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
#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
Instance Attribute Details
#routes ⇒ Object
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
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
|
# File 'lib/osctld/net_interface/routed.rb', line 86
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
ip(v, %i[route add] + r.ip_spec + [:dev, veth]) if r
ct_syscmd(
ct,
['ip', "-#{v}", 'addr', 'add', addr.to_string, 'dev', name],
valid_rcs: [2]
)
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
172
173
174
175
176
177
178
179
180
|
# File 'lib/osctld/net_interface/routed.rb', line 172
def add_route(addr, via: nil)
route = @routes.add(addr, via:)
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
82
83
84
|
# File 'lib/osctld/net_interface/routed.rb', line 82
def can_run_distconfig?
exclusively { !veth.nil? }
end
|
#create(opts) ⇒ Object
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
208
209
210
211
212
213
214
215
|
# File 'lib/osctld/net_interface/routed.rb', line 208
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
160
161
162
163
164
165
166
|
# File 'lib/osctld/net_interface/routed.rb', line 160
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
194
195
196
197
198
199
200
201
202
203
204
|
# File 'lib/osctld/net_interface/routed.rb', line 194
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, %i[route del] + route.ip_spec + [:dev, veth])
end
end
end
|
#del_ip(addr, keep_route) ⇒ Object
121
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
|
# File 'lib/osctld/net_interface/routed.rb', line 121
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
@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
routes_to_remove.each do |route|
ip(v, %i[route del] + route.ip_spec + [:dev, veth])
end
ct_syscmd(
ct,
['ip', "-#{v}", 'addr', 'del', addr.to_string, 'dev', name],
valid_rcs: [2]
)
end
end
|
#del_route(addr) ⇒ Object
182
183
184
185
186
187
188
189
190
191
|
# File 'lib/osctld/net_interface/routed.rb', line 182
def del_route(addr)
route = @routes.remove(addr)
return unless route
ct.inclusively do
next if ct.state != :running
ip(route.ip_version, %i[route del] + route.ip_spec + [:dev, veth])
end
end
|
#get_ipv6_link_local ⇒ Object
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 }
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
168
169
170
|
# File 'lib/osctld/net_interface/routed.rb', line 168
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
|
#setup ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/osctld/net_interface/routed.rb', line 48
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
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/osctld/net_interface/routed.rb', line 67
def up(veth)
super
[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
File.write(File.join('/proc/sys/net/ipv4/conf', veth, 'rp_filter'), '1')
end
|