Skip to content

Commit 315f547

Browse files
committed
T75: fixup, support ipv6
1 parent f7cf9f0 commit 315f547

2 files changed

Lines changed: 28 additions & 23 deletions

File tree

smoketest/scripts/cli/test_system_flow-accounting.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,28 @@
3232

3333
class TestSystemFlowAccounting(VyOSUnitTestSHIM.TestCase):
3434

35-
def _get_iptables_watched_interfaces(self, table, chain, column_name):
36-
iptables_command = f'iptables -vn -t {table} -L {chain}'
35+
def _get_iptables_watched_interfaces(self, command, table, chain, column_name):
36+
iptables_command = f'{command} -vn -t {table} -L {chain}'
3737
data = cmd(iptables_command, message='Failed to get flows list')
3838
data = data.splitlines()
39-
self.assertGreaterEqual(len(data), 2, "Unexpected output of iptables, should be at least two lines");
39+
self.assertGreaterEqual(len(data), 2, "Unexpected output of {command}, should be at least two lines");
4040
column_index = data[1].split().index(column_name)
4141
interfaces = [line.split()[column_index] for line in data[2:] if 'NETFLOW' in line]
4242
return interfaces
4343

44-
def _get_iptables_watched_ingress_interfaces(self):
45-
return self._get_iptables_watched_interfaces('raw', 'PREROUTING', 'in')
44+
def _get_iptables_watched_ingress_interfaces(self, command):
45+
return self._get_iptables_watched_interfaces(command, 'raw', 'PREROUTING', 'in')
4646

47-
def _get_iptables_watched_egress_interfaces(self):
48-
return self._get_iptables_watched_interfaces('mangle', 'POSTROUTING', 'out')
47+
def _get_iptables_watched_egress_interfaces(self, command):
48+
return self._get_iptables_watched_interfaces(command, 'mangle', 'POSTROUTING', 'out')
4949

5050
def _assert_ingress_interfaces(self, interfaces):
51-
self.assertEqual(set(self._get_iptables_watched_ingress_interfaces()), set(interfaces))
51+
for command in 'iptables', 'ip6tables':
52+
self.assertEqual(set(self._get_iptables_watched_ingress_interfaces(command)), set(interfaces), command)
5253

5354
def _assert_egress_interfaces(self, interfaces):
54-
self.assertEqual(set(self._get_iptables_watched_egress_interfaces()), set(interfaces))
55+
for command in 'iptables', 'ip6tables':
56+
self.assertEqual(set(self._get_iptables_watched_egress_interfaces(command)), set(interfaces), command)
5557

5658
@classmethod
5759
def setUpClass(cls):
@@ -94,7 +96,6 @@ def test_basic(self):
9496
self.cli_commit()
9597

9698
# verify configuration
97-
watched_interfaces = self._get_iptables_watched_ingress_interfaces()
9899
self._assert_ingress_interfaces(Section.interfaces('ethernet'))
99100
self._assert_egress_interfaces([])
100101

src/conf_mode/system_flow-accounting.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@
5050

5151

5252
# get iptables rule dict for chain in table
53-
def _iptables_get_rules(chain, table):
53+
def _iptables_get_rules(command, chain, table):
5454
# define list with rules
5555
rules = []
5656

5757
# run iptables, save output and split it by lines
58-
iptables_command = f'iptables -vn -t {table} -L {chain}'
58+
iptables_command = f'{command} -vn -t {table} -L {chain}'
5959
tmp = cmd(iptables_command, message='Failed to get flows list')
6060
lines = tmp.splitlines()
6161

@@ -67,9 +67,9 @@ def _iptables_get_rules(chain, table):
6767

6868
# Check that format is as expected
6969
if len(lines) < 2:
70-
raise ConfigError(f'Unexpected output from iptables, too few lines')
70+
raise ConfigError(f'Unexpected output from {command}, too few lines')
7171
if not lines[0].startswith(f'Chain {chain}'):
72-
raise ConfigError(f'Unexpected first line in output of iptables: "{lines[0]}"')
72+
raise ConfigError(f'Unexpected first line in output of {command}: "{lines[0]}"')
7373
columns = lines[1].split();
7474

7575
# parse each line and add information to list
@@ -94,7 +94,7 @@ def _iptables_get_rules(chain, table):
9494
# return list with rules
9595
return rules
9696

97-
def _iptables_config(configured_ifaces, direction, length=None):
97+
def _iptables_config(command, configured_ifaces, direction):
9898
# define list of nftables commands to modify settings
9999
iptables_commands = []
100100

@@ -113,7 +113,7 @@ def _iptables_config(configured_ifaces, direction, length=None):
113113
configured_ifaces_extended.append({ 'iface': iface })
114114

115115
# get currently configured interfaces with iptables rules
116-
active_rules = _iptables_get_rules(iptables_chain, iptables_table)
116+
active_rules = _iptables_get_rules(command, iptables_chain, iptables_table)
117117

118118
# compare current active list with configured one and delete excessive interfaces, add missed
119119
active_ifaces = []
@@ -132,7 +132,7 @@ def _iptables_config(configured_ifaces, direction, length=None):
132132
# rulenums are not changed
133133
rulenums_delete.sort(reverse=True)
134134
for rulenum in rulenums_delete:
135-
iptables_commands.append(f'iptables -t {iptables_table} -D {iptables_chain} {rulenum}')
135+
iptables_commands.append(f'{command} -t {iptables_table} -D {iptables_chain} {rulenum}')
136136

137137
# do not create new rules for already configured interfaces
138138
for iface in active_ifaces:
@@ -144,13 +144,17 @@ def _iptables_config(configured_ifaces, direction, length=None):
144144
iface = iface_extended['iface']
145145
iface_option = "o" if direction == "egress" else "i"
146146
#iptables -t raw -A PREROUTING -j NETFLOW -i eth0
147-
rule_definition = f'iptables -t {iptables_table} -A {iptables_chain} -j NETFLOW -{iface_option} {iface}'
147+
rule_definition = f'{command} -t {iptables_table} -A {iptables_chain} -j NETFLOW -{iface_option} {iface}'
148148
iptables_commands.append(rule_definition)
149149

150150
# change iptables
151151
for command in iptables_commands:
152152
cmd(command, raising=ConfigError)
153153

154+
def _iptables_config_v4_and_v6(configured_ifaces, direction):
155+
for command in 'iptables', 'ip6tables':
156+
_iptables_config(command, configured_ifaces, direction)
157+
154158

155159
def get_config(config=None):
156160
if config:
@@ -268,8 +272,8 @@ def apply(flow_config):
268272
# all iptables usage of ipt_NETFLOW
269273
# When flow_config is disabled everything should be cleaned-up too
270274
if need_reload or not flow_config:
271-
_iptables_config([], 'ingress')
272-
_iptables_config([], 'egress')
275+
_iptables_config_v4_and_v6([], 'ingress')
276+
_iptables_config_v4_and_v6([], 'egress')
273277

274278
# Stop flow-accounting module
275279
unload_kmod(module_name)
@@ -285,13 +289,13 @@ def apply(flow_config):
285289
# configure iptables for defined interfaces
286290
if 'interface' in flow_config['netflow']:
287291
interfaces = flow_config['netflow']['interface']
288-
_iptables_config(interfaces, 'ingress')
292+
_iptables_config_v4_and_v6(interfaces, 'ingress')
289293

290294
# configure egress the same way if configured otherwise remove it
291295
if 'enable_egress' in flow_config:
292-
_iptables_config(interfaces, 'egress')
296+
_iptables_config_v4_and_v6(interfaces, 'egress')
293297
else:
294-
_iptables_config([], 'egress')
298+
_iptables_config_v4_and_v6([], 'egress')
295299

296300

297301
if __name__ == '__main__':

0 commit comments

Comments
 (0)