Module: Kanrisuru::Core::IP

Extended by:
OsPackage::Define
Defined in:
lib/kanrisuru/core/ip.rb,
lib/kanrisuru/core/ip/types.rb,
lib/kanrisuru/core/ip/commands.rb,
lib/kanrisuru/core/ip/constants.rb,
lib/kanrisuru/core/ip/parsers/base.rb,
lib/kanrisuru/core/ip/parsers/link.rb,
lib/kanrisuru/core/ip/parsers/rule.rb,
lib/kanrisuru/core/ip/commands/link.rb,
lib/kanrisuru/core/ip/commands/rule.rb,
lib/kanrisuru/core/ip/parsers/route.rb,
lib/kanrisuru/core/ip/commands/route.rb,
lib/kanrisuru/core/ip/parsers/address.rb,
lib/kanrisuru/core/ip/parsers/version.rb,
lib/kanrisuru/core/ip/commands/address.rb,
lib/kanrisuru/core/ip/parsers/maddress.rb,
lib/kanrisuru/core/ip/commands/maddress.rb,
lib/kanrisuru/core/ip/parsers/neighbour.rb,
lib/kanrisuru/core/ip/commands/neighbour.rb,
lib/kanrisuru/core/ip/parsers/address_label.rb,
lib/kanrisuru/core/ip/commands/address_label.rb,
lib/kanrisuru/core/ip/commands/link_set_opts.rb,
lib/kanrisuru/core/ip/commands/link_type_opts.rb

Defined Under Namespace

Modules: Parser Classes: IPAddressInfo, IPAddressLabel, IPAddressProperty, IPLinkProperty, IPMAddress, IPMAddressEntry, IPNeighbour, IPNeighbourStats, IPRoute, IPRule, IPStatRX, IPStatTX, IPStats

Constant Summary collapse

IPROUTE2_JSON_VERSION =
180_129
IP_ROUTE_TYPES =
%w[
  unicast unreachable blackhole prohibit local
  broadcast throw nat via anycast multicast
].freeze
IP_FAMILIES =
%w[inet inet6 link].freeze
IP_SCOPES =
%w[global site link host].freeze
%w[
  vlan veth vcan vxcan dummy ifb macvlan macvtap
  bridge bond team ipoib ip6tnl ipip sit vxlan
  gre gretap erspan ip6gre ip6gretap ip6erspan
  vti nlmon team_slave bond_slave bridge_slave
  ipvlan ipvtap geneve vrf macsec netdevsim rmnet
  xfrm bareudp hsr
].freeze

Instance Method Summary collapse

Methods included from OsPackage::Define

extended, os_define

Instance Method Details

#ip(object, action = nil, opts = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/kanrisuru/core/ip.rb', line 17

def ip(object, action = nil, opts = {})
  if action.instance_of?(Hash)
    opts = action || {}
    action = 'show'
  elsif action.nil?
    action = 'show'
    opts = {}
  end

  case object
  when 'address', 'addr', 'a'
    ip_address(action, opts)
  when 'addrlabel', 'addrl'
    ip_address_label(action, opts)
  when 'link', 'l'
    ip_link(action, opts)
  when 'maddress', 'maddr', 'm'
    ip_maddress(action, opts)
  when 'neighbour', 'neigh', 'n'
    ip_neighbour(action, opts)
  when 'route', 'r', 'ro', 'rou', 'rout'
    ip_route(action, opts)
  when 'rule', 'ru'
    ip_rule(action, opts)
  end
end

#ip_address(action, opts) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/kanrisuru/core/ip/commands/address.rb', line 6

def ip_address(action, opts)
  case action
  when 'show', 'list'
    version = ip_version.to_i
    command = ip_address_show(opts, version)
  when 'add'
    command = ip_address_add(opts)
  when 'del', 'delete'
    command = ip_address_delete(opts)
  when 'flush'
    command = ip_address_flush(opts)
  end

  execute_shell(command)

  Kanrisuru::Result.new(command) do |cmd|
    Parser::Address.parse(cmd, action, version)
  end
end

#ip_address_add(opts) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/kanrisuru/core/ip/commands/address.rb', line 48

def ip_address_add(opts)
  command = Kanrisuru::Command.new('ip address add')
  command << opts[:address]

  command.append_arg('dev', opts[:dev])
  command.append_arg('label', opts[:label])
  command.append_arg('scope', opts[:scope])
  command
end

#ip_address_delete(opts) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/kanrisuru/core/ip/commands/address.rb', line 58

def ip_address_delete(opts)
  command = Kanrisuru::Command.new('ip address del')
  command << opts[:address]

  command.append_arg('dev', opts[:dev])
  command.append_arg('label', opts[:label])
  command.append_arg('scope', opts[:scope])
  command
end

#ip_address_flush(opts) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/kanrisuru/core/ip/commands/address.rb', line 68

def ip_address_flush(opts)
  command = Kanrisuru::Command.new('ip address flush')

  command.append_arg('dev', opts[:dev])
  command.append_arg('scope', opts[:scope])
  command.append_arg('to', opts[:prefix])
  command.append_arg('label', opts[:label])

  command.append_flag('dynamic', opts[:dynamic])
  command.append_flag('permanent', opts[:permanent])
  command.append_flag('tenative', opts[:tenative])
  command.append_flag('deprecated', opts[:deprecated])
  command.append_flag('primary', opts[:primary])
  command.append_flag('secondary', opts[:secondary])
  command
end

#ip_address_label(action, opts) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/kanrisuru/core/ip/commands/address_label.rb', line 6

def ip_address_label(action, opts)
  case action
  when 'show', 'list'
    version = ip_version.to_i
    command = ip_address_label_show(opts, version)
  when 'flush'
    command = ip_address_label_flush(opts)
  when 'add'
    command = ip_address_label_add(opts)
  when 'del'
    command = ip_address_label_delete(opts)
  end

  execute_shell(command)

  Kanrisuru::Result.new(command) do |cmd|
    Parser::AddressLabel.parse(cmd, action, version)
  end
end

#ip_address_label_add(opts) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/kanrisuru/core/ip/commands/address_label.rb', line 34

def ip_address_label_add(opts)
  command = Kanrisuru::Command.new('ip addrlabel add')
  command.append_arg('prefix', opts[:prefix])
  command.append_arg('dev', opts[:dev])
  command.append_arg('label', opts[:label])
  command
end

#ip_address_label_delete(opts) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/kanrisuru/core/ip/commands/address_label.rb', line 26

def ip_address_label_delete(opts)
  command = Kanrisuru::Command.new('ip addrlabel del')
  command.append_arg('prefix', opts[:prefix])
  command.append_arg('dev', opts[:dev])
  command.append_arg('label', opts[:label])
  command
end

#ip_address_label_flush(_opts) ⇒ Object



42
43
44
# File 'lib/kanrisuru/core/ip/commands/address_label.rb', line 42

def ip_address_label_flush(_opts)
  Kanrisuru::Command.new('ip addrlabel flush')
end

#ip_address_label_show(_opts, version) ⇒ Object



46
47
48
49
50
51
# File 'lib/kanrisuru/core/ip/commands/address_label.rb', line 46

def ip_address_label_show(_opts, version)
  command = Kanrisuru::Command.new('ip')
  command.append_flag('-json') if version >= IPROUTE2_JSON_VERSION
  command << 'addrlabel list'
  command
end

#ip_address_show(opts, version) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/kanrisuru/core/ip/commands/address.rb', line 26

def ip_address_show(opts, version)
  command = Kanrisuru::Command.new('ip')
  command.append_flag('-json') if version >= IPROUTE2_JSON_VERSION
  command.append_flag('-s', opts[:stats])
  command.append_arg('-family', opts[:family])
  command << 'address show'

  command.append_arg('dev', opts[:dev])
  command.append_arg('scope', opts[:scope])
  command.append_arg('to', opts[:prefix])
  command.append_arg('label', opts[:label])

  command.append_flag('dynamic', opts[:dynamic])
  command.append_flag('permanent', opts[:permanent])
  command.append_flag('tenative', opts[:tenative])
  command.append_flag('deprecated', opts[:deprecated])
  command.append_flag('primary', opts[:primary])
  command.append_flag('secondary', opts[:secondary])
  command.append_flag('up', opts[:up])
  command
end


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/kanrisuru/core/ip/commands/link.rb', line 6

def ip_link(action, opts)
  case action
  when 'show', 'list'
    version = ip_version.to_i
    command = ip_link_show(opts, version)
  when 'add', 'a'
    command = ip_link_add(opts)
  when 'delete', 'del'
    command = ip_link_delete(opts)
  when 'set'
    command = ip_link_set(opts)
  end

  execute_shell(command)

  Kanrisuru::Result.new(command) do |cmd|
    Parser::Link.parse(cmd, action, version)
  end
end


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/kanrisuru/core/ip/commands/link.rb', line 43

def ip_link_add(opts)
  command = Kanrisuru::Command.new('ip link add')

  command.append_arg('dev', opts[:dev])
  command.append_arg('name', opts[:name])
  command.append_arg('mtu', opts[:mtu])
  command.append_arg('index', opts[:index])

  command.append_arg('numtxqueues', opts[:numtxqueues])
  command.append_arg('numrxqueues', opts[:numrxqueues])
  command.append_arg('gso_max_size', opts[:gso_max_size])
  command.append_arg('gso_max_segs', opts[:gso_max_segs])

  if Kanrisuru::Util.present?(opts[:type])
    raise ArgumentError, 'invalid link type' unless IP_LINK_TYPES.include?(opts[:type])

    command.append_arg('type', opts[:type])

    ip_link_type_opts(command, opts) if Kanrisuru::Util.present?(opts[:type_opts])
  end

  command
end


157
158
159
160
161
162
# File 'lib/kanrisuru/core/ip/commands/link_type_opts.rb', line 157

def ip_link_bareudp_opts(command, opts)
  command.append_arg('dstport', opts[:dstport])
  command.append_arg('ethertype', opts[:ethertype])
  command.append_arg('srcportmin', opts[:srcportmin])
  command.append_flag_no('multiproto', opts[:multiproto])
end


277
278
279
# File 'lib/kanrisuru/core/ip/commands/link_type_opts.rb', line 277

def ip_link_bond_slave_opts(command, opts)
  command.append_arg('queue_id', opts[:queue_id])
end


188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/kanrisuru/core/ip/commands/link_type_opts.rb', line 188

def ip_link_bridge_opts(command, opts)
  command.append_arg('ageing_time', opts[:ageing_time])
  command.append_arg('group_fwd_mask', opts[:group_fwd_mask])
  command.append_arg('group_address', opts[:group_address])
  command.append_arg('forward_delay', opts[:forward_delay])
  command.append_arg('hello_time', opts[:hello_time])
  command.append_arg('max_age', opts[:max_age])
  command.append_arg('stp_state', opts[:stp_state])
  command.append_arg('priority', opts[:priority])
  command.append_arg('vlan_filtering', opts[:vlan_filtering])
  command.append_arg('vlan_protocol', opts[:vlan_protocol])
  command.append_arg('vlan_default_pvid', opts[:vlan_default_pvid])
  command.append_arg('vlan_stats_enabled', opts[:vlan_stats_enabled])
  command.append_arg('vlan_stats_per_port', opts[:vlan_stats_per_port])
  command.append_arg('mcast_snooping', opts[:mcast_snooping])
  command.append_arg('mcast_router', opts[:mcast_router])
  command.append_arg('mcast_query_use_ifaddr', opts[:mcast_query_use_ifaddr])
  command.append_arg('mcast_querier', opts[:mcast_querier])
  command.append_arg('mcast_querier_interval', opts[:mcast_querier_interval])
  command.append_arg('mcast_hash_elasticity', opts[:mcast_hash_elasticity])
  command.append_arg('mcast_hash_max', opts[:mcast_hash_max])
  command.append_arg('mcast_last_member_count', opts[:mcast_last_member_count])
  command.append_arg('mcast_last_member_interval', opts[:mcast_last_member_interval])
  command.append_arg('mcast_startup_query_count', opts[:mcast_startup_query_count])
  command.append_arg('mcast_startup_query_interval', opts[:mcast_startup_query_interval])
  command.append_arg('mcast_query_interval', opts[:mcast_query_interval])
  command.append_arg('mcast_query_response_interval', opts[:mcast_query_response_interval])
  command.append_arg('mcast_membership_interval', opts[:mcast_membership_interval])
  command.append_arg('mcast_stats_enabled', opts[:mcast_stats_enabled])
  command.append_arg('mcast_igmp_version', opts[:mcast_igmp_version])
  command.append_arg('mcast_mld_version', opts[:mcast_mld_version])
  command.append_arg('nf_call_iptables', opts[:nf_call_iptables])
  command.append_arg('nf_call_ip6tables', opts[:nf_call_ip6tables])
  command.append_arg('nf_call_arptables', opts[:nf_call_arptables])
end


253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/kanrisuru/core/ip/commands/link_type_opts.rb', line 253

def ip_link_bridge_slave_opts(command, opts)
  command.append_flag('fdb_flush', opts[:fdb_flush])
  command.append_arg('state', opts[:state])
  command.append_arg('priority', opts[:priority])
  command.append_arg('cost', opts[:cost])
  command.append_arg('guard', opts[:guard])
  command.append_arg('hairpin', opts[:hairpin])
  command.append_arg('fastleave', opts[:fastleave])
  command.append_arg('root_block', opts[:root_block])
  command.append_arg('learning', opts[:learning])
  command.append_arg('flood', opts[:flood])
  command.append_arg('proxy_arp', opts[:proxy_arp])
  command.append_arg('proxy_arp_wifi', opts[:proxy_arp_wifi])
  command.append_arg('mcast_router', opts[:mcast_router])
  command.append_arg('mcast_fast_leave', opts[:mcast_fast_leave])
  command.append_arg('mcast_flood', opts[:mcast_flood])
  command.append_arg('mcast_to_unicast', opts[:mcast_to_unicast])
  command.append_arg('group_fwd_mask', opts[:group_fwd_mask])
  command.append_arg('neigh_suppress', opts[:neigh_suppress])
  command.append_arg('neigh_suppress', opts[:neigh_suppress])
  command.append_arg('backup_port', opts[:backup_port])
  command.append_flag('nobackup_port', opts[:nobackup_port])
end


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/kanrisuru/core/ip/commands/link_set_opts.rb', line 6

def ip_link_common_opts(command, opts)
  command.append_arg('arp', opts[:arp])
  command.append_arg('multicast', opts[:multicast])
  command.append_arg('allmulticast', opts[:allmulticast])
  command.append_arg('promisc', opts[:promisc])
  command.append_arg('protodown', opts[:protodown])
  command.append_arg('protodown_reason', opts[:protodown_reason])
  command.append_arg('dynamic', opts[:dynamic])
  command.append_arg('name', opts[:name])
  command.append_arg('txqueuelen', opts[:txqueuelen])
  command.append_arg('txqlen', opts[:txqlen])
  command.append_arg('mtu', opts[:mtu])
  command.append_arg('address', opts[:address])
  command.append_arg('broadcast', opts[:broadcast])
  command.append_arg('brd', opts[:brd])
  command.append_arg('peer', opts[:peer])
  command.append_arg('netns', opts[:netns])
  command.append_arg('alias', opts[:alias])
end


67
68
69
70
71
72
73
# File 'lib/kanrisuru/core/ip/commands/link.rb', line 67

def ip_link_delete(opts)
  command = Kanrisuru::Command.new('ip link delete')
  command.append_arg('dev', opts[:dev])
  command.append_arg('group', opts[:group])
  command.append_arg('type', opts[:type])
  command
end


131
132
133
134
135
136
137
138
139
140
# File 'lib/kanrisuru/core/ip/commands/link_type_opts.rb', line 131

def ip_link_erspan_opts(command, opts)
  command.append_arg('remote', opts[:remote])
  command.append_arg('local', opts[:local])
  command.append_arg('erspan_ver', opts[:erspan_ver])
  command.append_arg('erspan', opts[:erspan])
  command.append_arg('erspan_dir', opts[:erspan_dir])
  command.append_arg('erspan_hwid', opts[:erspan_hwid])
  command.append_flag_no('allow-localremote', opts[:allow_localremote])
  command.append_flag('external', opts[:external])
end


142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/kanrisuru/core/ip/commands/link_type_opts.rb', line 142

def ip_link_geneve_opts(command, opts)
  command.append_arg('id', opts[:id])
  command.append_arg('remote', opts[:remote])
  command.append_arg('ttl', opts[:ttl])
  command.append_arg('tos', opts[:tos])
  command.append_arg('df', opts[:df])
  command.append_arg('flowlabel', opts[:flowlabel])
  command.append_arg('dstport', opts[:dstport])

  command.append_flag_no('external', opts[:external])
  command.append_flag_no('udpcsum', opts[:udpcsum])
  command.append_flag_no('udp6zerocsumtx', opts[:udp6zerocsumtx])
  command.append_flag_no('udp6zerocsumrx', opts[:udp6zerocsumrx])
end


99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/kanrisuru/core/ip/commands/link_type_opts.rb', line 99

def ip_link_gre_gretap_opts(command, opts)
  ip_link_shared_sit_gre_opts(command, opts)
  ip_link_shared_io_opts(command, opts)

  command.append_arg('ttl', opts[:ttl])
  command.append_arg('tos', opts[:tos])

  command.append_flag_no('pmtudisc', opts[:pmtudisc])
  command.append_flag_no('ignore-df', opts[:ignore_df])

  command.append_arg('dev', opts[:dev])
end


180
181
182
183
184
185
186
# File 'lib/kanrisuru/core/ip/commands/link_type_opts.rb', line 180

def ip_link_hsr_opts(command, opts)
  command.append_arg('slave1', opts[:slave1])
  command.append_arg('slave2', opts[:slave2])
  command.append_arg('supervision', opts[:supervision])
  command.append_arg('version', opts[:version])
  command.append_arg('proto', opts[:proto])
end


112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/kanrisuru/core/ip/commands/link_type_opts.rb', line 112

def ip_link_ip6gre_ip6gretap_opts(command, opts)
  command.append_arg('remote', opts[:remote])
  command.append_arg('local', opts[:local])
  ip_link_shared_io_opts(command, opts)

  command.append_arg('hoplimit', opts[:hoplimit])
  command.append_arg('encaplimit', opts[:encaplimit])
  command.append_arg('flowlabel', opts[:flowlabel])

  command.append_flag_no('allow-localremote', opts[:allow_localremote])
  command.append_arg('tclass', opts[:tclass])
  command.append_arg('external', opts[:external])
end


94
95
96
97
# File 'lib/kanrisuru/core/ip/commands/link_type_opts.rb', line 94

def ip_link_ipip_sit_opts(command, opts)
  ip_link_shared_sit_gre_opts(command, opts)
  command.append_arg('mode', opts[:mode])
end


126
127
128
129
# File 'lib/kanrisuru/core/ip/commands/link_type_opts.rb', line 126

def ip_link_ipoib_opts(command, opts)
  command.append_arg('pkey', opts[:pkey])
  command.append_arg('mode', opts[:mode])
end


224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/kanrisuru/core/ip/commands/link_type_opts.rb', line 224

def ip_link_macsec_opts(command, opts)
  command.append_arg('address', opts[:address])
  command.append_arg('port', opts[:port])
  command.append_arg('sci', opts[:sci])
  command.append_arg('cipher', opts[:cipher])
  command.append_arg('icvlen', opts[:icvlen])
  command.append_arg('encrypt', opts[:encrypt])
  command.append_arg('send_sci', opts[:send_sci])
  command.append_arg('end_station', opts[:end_station])
  command.append_arg('scb', opts[:scb])
  command.append_arg('protect', opts[:protect])
  command.append_arg('replay', opts[:replay])
  command.append_arg('window', opts[:window])
  command.append_arg('validate', opts[:validate])
  command.append_arg('encodingsa', opts[:encodingsa])
end


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/kanrisuru/core/ip/commands/link_type_opts.rb', line 164

def ip_link_macvlan_macvtap_opts(command, opts)
  command.append_arg('mode', opts[:mode])
  command.append_arg('flag', opts[:flag])

  case opts[:mode_opts]
  when 'add', 'del'
    command << opts[:mode_opts]
    command << opts[:mac_address]
  when 'set'
    command << 'set'
    command.append_array(opts[:mac_address])
  when 'flush'
    command << 'flush'
  end
end


245
246
247
# File 'lib/kanrisuru/core/ip/commands/link_type_opts.rb', line 245

def ip_link_rmnet_opts(command, opts)
  command.append_arg('mux_id', opts[:mux_id])
end

Raises:

  • (ArgumentError)


75
76
77
78
79
80
81
82
83
84
85
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
# File 'lib/kanrisuru/core/ip/commands/link.rb', line 75

def ip_link_set(opts)
  command = Kanrisuru::Command.new('ip link set')
  raise ArgumentError, 'no device defined' unless opts[:dev]

  command.append_arg('dev', opts[:dev])
  command.append_arg('group', opts[:group])

  case opts[:direction]
  when 'up'
    command.append_flag('up')
  when 'down'
    command.append_flag('down')
  end

  ip_link_common_opts(command, opts)

  ip_link_vf_opts(command, opts) if Kanrisuru::Util.present?(opts[:vf])

  ip_link_xdp_opts(command, opts) if Kanrisuru::Util.present?(opts[:xdp])

  if opts[:master] == false
    command.append_flag('nomaster')
  else
    command.append_arg('master', opts[:master])
  end

  command.append_arg('addrgenmode', opts[:addrgenmode])
  command.append_flag('link-netnsid', opts[:link_netnsid])

  if Kanrisuru::Util.present?(opts[:type])
    raise ArgumentError, 'invalid link type' unless IP_LINK_TYPES.include?(opts[:type])

    command.append_arg('type', opts[:type])

    ip_link_type_opts(command, opts) if Kanrisuru::Util.present?(opts[:type_opts])
  end

  command
end


281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/kanrisuru/core/ip/commands/link_type_opts.rb', line 281

def ip_link_shared_io_opts(command, opts)
  command.append_flag_no('iseq', opts[:iseq])
  command.append_flag_no('oseq', opts[:oseq])

  command.append_flag_no('icsum', opts[:icsum])
  command.append_flag_no('ocsum', opts[:ocsum])

  if opts[:okey] == false
    command.append_flag('nookey', opts[:okey])
  elsif opts[:okey].instance_of?(Integer)
    command.append_arg('okey', opts[:okey])
  end

  if opts[:ikey] == false
    command.append_flag('noikey', opts[:ikey])
  elsif opts[:ikey].instance_of?(Integer)
    command.append_arg('ikey', opts[:ikey])
  end
end


301
302
303
304
305
306
307
308
309
310
# File 'lib/kanrisuru/core/ip/commands/link_type_opts.rb', line 301

def ip_link_shared_sit_gre_opts(command, opts)
  command.append_arg('remote', opts[:remote])
  command.append_arg('local', opts[:local])
  command.append_arg('encap', opts[:encap])
  command.append_arg('encap-sport', opts[:encap_sport])

  command.append_flag_no('encap-csum', opts[:encap_csum])
  command.append_flag_no('encap-remcsum', opts[:encap_remcsum])
  command.append_flag('external', opts[:external])
end


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/kanrisuru/core/ip/commands/link.rb', line 26

def ip_link_show(opts, version)
  command = Kanrisuru::Command.new('ip')
  command.append_flag('-json') if version >= IPROUTE2_JSON_VERSION
  command.append_flag('-s', opts[:stats])
  command.append_arg('-family', opts[:family])
  command << 'link show'

  command.append_arg('dev', opts[:dev])
  command.append_arg('group', opts[:group])
  command.append_arg('master', opts[:master])
  command.append_arg('type', opts[:type])
  command.append_arg('vrf', opts[:vrf])

  command.append_flag('up', opts[:up])
  command
end


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/kanrisuru/core/ip/commands/link_type_opts.rb', line 6

def ip_link_type_opts(command, opts)
  case opts[:type]
  when 'vlan'
    ip_link_vlan_opts(command, opts[:type_opts])
  when 'vxlan'
    ip_link_vxlan_opts(command, opts[:type_opts])
  when 'veth', 'vxcan'
    ip_link_veth_vxcan_opts(command, opts[:type_opts])
  when 'ipip', 'sit'
    ip_link_ipip_sit_opts(command, opts[:type_opts])
  when 'gre', 'gretap'
    ip_link_gre_gretap_opts(command, opts[:type_opts])
  when 'ip6gre', 'ip6gretap'
    ip_link_ip6gre_ip6gretap_opts(command, opts[:type_opts])
  when 'ipoib'
    ip_link_ipoib_opts(command, opts[:type_opts])
  when 'erspan', 'ip6erspan'
    ip_link_erspan_opts(command, opts[:type_opts])
  when 'geneve'
    ip_link_geneve_opts(command, opts[:type_opts])
  when 'bareudp'
    ip_link_bareudp_opts(command, opts[:type_opts])
  when 'macvlan', 'macvtap'
    ip_link_macvlan_macvtap_opts(command, opts[:type_opts])
  when 'hsr'
    ip_link_hsr_opts(command, opts[:type_opts])
  when 'bridge'
    ip_link_bridge_opts(command, opts[:type_opts])
  when 'macsec'
    ip_link_macsec_opts(command, opts[:type_opts])
  when 'vrf'
    ip_link_vrf_opts(command, opts[:type_opts])
  when 'rmnet'
    ip_link_rmnet_opts(command, opts[:type_opts])
  when 'xfrm'
    ip_link_xfrm_opts(command, opts[:type_opts])
  when 'bridge_slave'
    ip_link_bridge_slave_opts(command, opts[:type_opts])
  when 'bond_slave'
    ip_link_bond_slave_opts(command, opts[:type_opts])
  end
end


90
91
92
# File 'lib/kanrisuru/core/ip/commands/link_type_opts.rb', line 90

def ip_link_veth_vxcan_opts(command, opts)
  command.append_arg('peer name', opts[:peer_name])
end


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/kanrisuru/core/ip/commands/link_set_opts.rb', line 39

def ip_link_vf_opts(command, opts)
  command.append_arg('vf', opts[:vf])
  command.append_arg('mac', opts[:mac])

  if Kanrisuru::Util.present?(opts[:vlan])
    command.append_arg('vlan', opts[:vlan])
    command.append_arg('qos', opts[:qos])
  end

  command.append_arg('proto', opts[:proto])
  command.append_arg('rate', opts[:rate])
  command.append_arg('max_tx_rate', opts[:max_tx_rate])
  command.append_arg('min_tx_rate', opts[:min_tx_rate])
  command.append_arg('spoofchk', opts[:spoofchk])
  command.append_arg('query_rss', opts[:query_rss])
  command.append_arg('state', opts[:state])
  command.append_arg('trust', opts[:trust])
  command.append_arg('node_guid', opts[:node_guid])
  command.append_arg('port_guid', opts[:port_guid])
end


49
50
51
52
53
54
55
56
57
58
59
# File 'lib/kanrisuru/core/ip/commands/link_type_opts.rb', line 49

def ip_link_vlan_opts(command, opts)
  command.append_arg('protocol', opts[:protocol])
  command.append_arg('id', opts[:id])
  command.append_arg('reorder_hdr', opts[:reorder_hdr])
  command.append_arg('gvrp', opts[:gvrp])
  command.append_arg('mvrp', opts[:mvrp])
  command.append_arg('loose_binding', opts[:loose_binding])
  command.append_arg('bridge_binding', opts[:bridge_binding])
  command.append_arg('ingress-qos-map', opts[:ingress_qos_map])
  command.append_arg('egress-qos-map', opts[:egress_qos_map])
end


241
242
243
# File 'lib/kanrisuru/core/ip/commands/link_type_opts.rb', line 241

def ip_link_vrf_opts(command, opts)
  command.append_arg('table', opts[:table])
end


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/kanrisuru/core/ip/commands/link_type_opts.rb', line 61

def ip_link_vxlan_opts(command, opts)
  command.append_arg('id', opts[:id])
  command.append_arg('dev', opts[:dev])
  command.append_arg('group', opts[:group])
  command.append_arg('remote', opts[:remote])
  command.append_arg('local', opts[:local])
  command.append_arg('ttl', opts[:ttl])
  command.append_arg('tos', opts[:tos])
  command.append_arg('df', opts[:df])
  command.append_arg('flowlabel', opts[:flowlabel])
  command.append_arg('dstport', opts[:dstport])
  command.append_arg('srcport', opts[:srcport])

  command.append_flag_no('learning', opts[:learning])
  command.append_flag_no('rsc', opts[:rsc])
  command.append_flag_no('proxy', opts[:proxy])
  command.append_flag_no('l2miss', opts[:l2miss])
  command.append_flag_no('l3miss', opts[:l3miss])
  command.append_flag_no('udpcsum', opts[:udpcsum])
  command.append_flag_no('udp6zerocsumtx', opts[:udp6zerocsumtx])
  command.append_flag_no('udp6zerocsumrx', opts[:udp6zerocsumrx])

  command.append_arg('ageing', opts[:ageing])
  command.append_arg('maxaddress', opts[:maxaddress])

  command.append_flag('gbp', opts[:gbp])
  command.append_flag('gpe', opts[:gpe])
end


26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/kanrisuru/core/ip/commands/link_set_opts.rb', line 26

def ip_link_xdp_opts(command, opts)
  command.append_arg('xdp', opts[:xdp])

  case opts[:xdp]
  when 'object'
    command << opts[:object]
    command.append_arg('section', opts[:section])
    command.append_flag('verbose', opts[:verbose])
  when 'pinned'
    command << opts[:pinned]
  end
end


249
250
251
# File 'lib/kanrisuru/core/ip/commands/link_type_opts.rb', line 249

def ip_link_xfrm_opts(command, opts)
  command.append_arg('if_id', opts[:if_id])
end

#ip_maddress(action, opts) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/kanrisuru/core/ip/commands/maddress.rb', line 6

def ip_maddress(action, opts)
  case action
  when 'show', 'list'
    version = ip_version.to_i
    command = ip_maddress_show(opts, version)
  when 'add', 'delete', 'del'
    command = ip_maddress_modify(action, opts)
  end

  execute_shell(command)

  Kanrisuru::Result.new(command) do |cmd|
    Parser::Maddress.parse(cmd, action, version)
  end
end

#ip_maddress_modify(action, opts) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/kanrisuru/core/ip/commands/maddress.rb', line 32

def ip_maddress_modify(action, opts)
  command = Kanrisuru::Command.new('ip maddress')
  command << action

  command.append_arg('address', opts[:address])
  command.append_arg('dev', opts[:dev])
  command
end

#ip_maddress_show(opts, version) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/kanrisuru/core/ip/commands/maddress.rb', line 22

def ip_maddress_show(opts, version)
  command = Kanrisuru::Command.new('ip')
  command.append_flag('-json') if version >= IPROUTE2_JSON_VERSION
  command.append_arg('-family', opts[:family])
  command << 'maddress show'

  command.append_arg('dev', opts[:dev])
  command
end

#ip_neighbour(action, opts) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/kanrisuru/core/ip/commands/neighbour.rb', line 6

def ip_neighbour(action, opts)
  case action
  when 'show', 'list'
    version = ip_version.to_i
    command = ip_neighbour_show(opts, version)
  when 'add', 'change', 'replace', 'del', 'delete'
    command = ip_neighbour_modify(action, opts)
  when 'flush'
    command = ip_neighbour_flush(opts)
  end

  execute_shell(command)

  Kanrisuru::Result.new(command) do |cmd|
    Parser::Neighbour.parse(cmd, action, version)
  end
end

#ip_neighbour_flush(opts) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/kanrisuru/core/ip/commands/neighbour.rb', line 58

def ip_neighbour_flush(opts)
  command = Kanrisuru::Command.new('ip')
  command.append_flag('-s', opts[:stats])
  command << 'neighbour flush'

  command.append_arg('to', opts[:to])
  command.append_arg('dev', opts[:dev])
  command.append_arg('nud', opts[:nud])

  command.append_flag('unused', opts[:unused])
  command
end

#ip_neighbour_modify(action, opts) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/kanrisuru/core/ip/commands/neighbour.rb', line 40

def ip_neighbour_modify(action, opts)
  command = Kanrisuru::Command.new('ip neighbour')
  command << action

  command.append_arg('to', opts[:to])
  command.append_arg('dev', opts[:dev])

  if action != 'del' && action != 'delete'
    command.append_arg('lladdr', opts[:lladdr])
    command.append_arg('nud', opts[:nud])
  end

  command.append_flag('proxy', opts[:proxy])
  command.append_flag('router', opts[:router])
  command.append_flag('extern_learn', opts[:extern_learn])
  command
end

#ip_neighbour_show(opts, version) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/kanrisuru/core/ip/commands/neighbour.rb', line 24

def ip_neighbour_show(opts, version)
  command = Kanrisuru::Command.new('ip')
  command.append_flag('-json') if version >= IPROUTE2_JSON_VERSION
  command.append_flag('-s', opts[:stats])

  command << 'neighbour show'

  command.append_arg('to', opts[:to])
  command.append_arg('dev', opts[:dev])
  command.append_arg('nud', opts[:nud])

  command.append_flag('proxy', opts[:proxy])
  command.append_flag('unused', opts[:unused])
  command
end

#ip_route(action, opts) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/kanrisuru/core/ip/commands/route.rb', line 6

def ip_route(action, opts)
  case action
  when 'show', 'list'
    version = ip_version.to_i
    command = ip_route_show(opts, version)
  when 'flush'
    command = ip_route_flush(opts)
  when 'add', 'change', 'append', 'del', 'delete', 'replace'
    command = ip_route_modify(action, opts)
  when 'get'
    command = ip_route_get(opts)
  end

  execute_shell(command)

  Kanrisuru::Result.new(command) do |cmd|
    Parser::Route.parse(cmd, action, version)
  end
end

#ip_route_common_opts(command, opts) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/kanrisuru/core/ip/commands/route.rb', line 131

def ip_route_common_opts(command, opts)
  command.append_arg('to', opts[:to])
  command.append_arg('dev', opts[:dev])
  command.append_arg('protocol', opts[:protocol])
  command.append_arg('type', opts[:type])
  command.append_arg('table', opts[:table])
  command.append_arg('tos', opts[:tos])
  command.append_arg('dsfield', opts[:dsfield])
  command.append_arg('via', opts[:via])
  command.append_arg('vrf', opts[:vrf])
  command.append_arg('src', opts[:src])

  command.append_arg('realm', opts[:realm])
  command.append_arg('realms', opts[:realms])
  command.append_arg('scope', opts[:scope])

  command.append_flag('cloned', opts[:cloned])
  command.append_flag('cached', opts[:cached])
end

#ip_route_flush(opts) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/kanrisuru/core/ip/commands/route.rb', line 101

def ip_route_flush(opts)
  command = Kanrisuru::Command.new('ip')
  command.append_arg('-family', opts[:family])
  command << 'route flush'

  ip_route_common_opts(command, opts)

  command
end

#ip_route_get(opts) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/kanrisuru/core/ip/commands/route.rb', line 111

def ip_route_get(opts)
  command = Kanrisuru::Command.new('ip route get')

  command.append_flag('fibmatch', opts[:fibmatch])
  command.append_arg('to', opts[:to])
  command.append_arg('from', opts[:from])
  command.append_arg('tos', opts[:tos])
  command.append_arg('dsfield', opts[:dsfield])
  command.append_arg('iif', opts[:iif])
  command.append_arg('oif', opts[:oif])
  command.append_arg('mark', opts[:mark])
  command.append_arg('vrf', opts[:vrf])
  command.append_arg('ipproto', opts[:ipproto])
  command.append_arg('sport', opts[:sport])
  command.append_arg('dport', opts[:dport])

  command.append_flag('connected', opts[:connected])
  command
end

#ip_route_modify(action, opts) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
97
98
99
# File 'lib/kanrisuru/core/ip/commands/route.rb', line 37

def ip_route_modify(action, opts)
  command = Kanrisuru::Command.new('ip route')
  command << action

  command.append_arg('to', opts[:to])
  command.append_arg('tos', opts[:tos])
  command.append_arg('dsfield', opts[:dsfield])
  command.append_arg('metric', opts[:metric])
  command.append_arg('preference', opts[:preference])
  command.append_arg('table', opts[:table])
  command.append_arg('vrf', opts[:vrf])
  command.append_arg('dev', opts[:dev])
  command.append_arg('via', opts[:via])
  command.append_arg('src', opts[:src])
  command.append_arg('realm', opts[:realm])

  if Kanrisuru::Util.present?(opts[:mtu])
    if Kanrisuru::Util.present?(opts[:mtu_lock])
      command.append_arg('mtu lock', opts[:mtu])
    else
      command.append_arg('mtu', opts[:mtu])
    end
  end

  command.append_arg('window', opts[:window])
  command.append_arg('rtt', opts[:rtt])
  command.append_arg('rttvar', opts[:rttvar])
  command.append_arg('rto_min', opts[:rto_min])
  command.append_arg('ssthresh', opts[:ssthresh])
  command.append_arg('cwnd', opts[:cwnd])
  command.append_arg('initcwnd', opts[:initcwnd])
  command.append_arg('initrwnd', opts[:initrwnd])
  command.append_arg('features', opts[:features])
  command.append_arg('quickack', opts[:quickack])
  command.append_arg('fastopen_no_cookie', opts[:fastopen_no_cookie])

  if Kanrisuru::Util.present?(opts[:congctl])
    if Kanrisuru::Util.present?(opts[:congctl_lock])
      command.append_arg('congctl lock', opts[:congctl])
    else
      command.append_arg('congctl', opts[:congctl])
    end
  end

  command.append_arg('advmss', opts[:advmss])
  command.append_arg('reordering', opts[:reordering])

  if Kanrisuru::Util.present?(opts[:next_hop])
    next_hop = opts[:next_hop]

    command << 'next_hop'
    command.append_arg('via', next_hop[:via])
    command.append_arg('dev', next_hop[:dev])
    command.append_arg('weight', next_hop[:weight])
  end

  command.append_arg('scope', opts[:scope])
  command.append_arg('protocol', opts[:protocol])
  command.append_flag('onlink', opts[:onlink])
  command.append_arg('pref', opts[:pref])
  command.append_arg('nhid', opts[:nhid])
  command
end

#ip_route_show(opts, version) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/kanrisuru/core/ip/commands/route.rb', line 26

def ip_route_show(opts, version)
  command = Kanrisuru::Command.new('ip')
  command.append_flag('-json') if version >= IPROUTE2_JSON_VERSION
  command.append_arg('-family', opts[:family])
  command << 'route show'

  ip_route_common_opts(command, opts)

  command
end

#ip_rule(action, opts) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/kanrisuru/core/ip/commands/rule.rb', line 6

def ip_rule(action, opts)
  case action
  when 'show', 'list'
    version = ip_version.to_i
    command = ip_rule_show(opts, version)
  when 'flush'
    command = ip_rule_flush(opts)
  when 'add', 'delete', 'del'
    command = ip_rule_modify(action, opts)
  end

  execute_shell(command)

  Kanrisuru::Result.new(command) do |cmd|
    Parser::Rule.parse(cmd, action, version)
  end
end

#ip_rule_flush(opts) ⇒ Object



42
43
44
45
46
# File 'lib/kanrisuru/core/ip/commands/rule.rb', line 42

def ip_rule_flush(opts)
  command = Kanrisuru::Command.new('ip rule flush')
  command.append_arg('protocol', opts[:protocol])
  command
end

#ip_rule_modify(action, opts) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/kanrisuru/core/ip/commands/rule.rb', line 24

def ip_rule_modify(action, opts)
  command = Kanrisuru::Command.new('ip rule')
  command << action

  command.append_arg('type', opts[:type])
  command.append_arg('from', opts[:from])
  command.append_arg('to', opts[:to])
  command.append_arg('iif', opts[:iif])
  command.append_arg('tos', opts[:tos])
  command.append_arg('dsfield', opts[:dsfield])
  command.append_arg('fwmark', opts[:fwmark])
  command.append_arg('priority', opts[:priority])
  command.append_arg('table', opts[:table])
  command.append_arg('realms', opts[:realms])
  command.append_arg('nat', opts[:nat])
  command
end

#ip_rule_show(_opts, version) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/kanrisuru/core/ip/commands/rule.rb', line 48

def ip_rule_show(_opts, version)
  command = Kanrisuru::Command.new('ip')
  command.append_flag('-json') if version >= IPROUTE2_JSON_VERSION

  command << 'rule show'
  command
end

#ip_versionObject



16
17
18
19
20
21
22
23
# File 'lib/kanrisuru/core/ip/commands.rb', line 16

def ip_version
  command = Kanrisuru::Command.new('ip -V')
  execute_shell(command)

  Kanrisuru::Result.new(command) do |cmd|
    Parser::Version.parse(cmd)
  end
end