Skip to content

Commit 072ae4a

Browse files
authored
[filter-fdb] Call Filter FDB Main From Within Test Code #1051 (#1086)
* [filter-fdb] Call Filter FDB Main From Within Test Code (#1051) Code coverage requires that python code be run with the same process. Current test code was invoking filter fdb via shell which launches new process and so coverage is not available. This PR calls the main method from within test code. signed-off-by: Tamer Ahmed <tamer.ahmed@microsoft.com> * [filter-fdb] Fix Filter FDB With IPv6 Present in Config DB (#1059) Filter fdb was wiping out IPv4 entries when both IPv4 and IPv6 are associated with VLan interface. The reason is IPv6 network was overwriting IPv4 network. This pr add support to filter both IPv4 and IPv6 addresses signed-off-by: Tamer Ahmed <tamer.ahmed@microsoft.com>
1 parent 8e30ac1 commit 072ae4a

File tree

8 files changed

+47
-41
lines changed

8 files changed

+47
-41
lines changed

fdbutil/__init__.py

Whitespace-only changes.

scripts/filter_fdb_entries.py fdbutil/filter_fdb_entries.py

+19-29
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
#!/usr/bin/env python
2-
1+
import argparse
32
import json
4-
import sys
53
import os
6-
import argparse
4+
import sys
75
import syslog
8-
import traceback
96
import time
7+
import traceback
108

11-
from ipaddress import ip_address, ip_network, ip_interface
129
from collections import defaultdict
10+
from ipaddress import ip_address, ip_network, ip_interface
1311

1412
def get_vlan_cidr_map(filename):
1513
"""
@@ -35,7 +33,9 @@ def get_vlan_cidr_map(filename):
3533
continue
3634
vlan, cidr = tuple(vlan_key.split('|'))
3735
if vlan in config_db_entries["VLAN"]:
38-
vlan_cidr[vlan] = ip_interface(cidr).network
36+
if vlan not in vlan_cidr:
37+
vlan_cidr[vlan] = {4: ip_address("0.0.0.0".decode()), 6: ip_address("::".decode())}
38+
vlan_cidr[vlan][ip_interface(cidr).version] = ip_interface(cidr).network
3939

4040
return vlan_cidr
4141

@@ -65,8 +65,9 @@ def get_arp_entries_map(arp_filename, config_db_filename):
6565
continue
6666
table, vlan, ip = tuple(key.split(':'))
6767
if "NEIGH_TABLE" in table and vlan in vlan_cidr.keys() \
68-
and ip_address(ip) in ip_network(vlan_cidr[vlan]) and "neigh" in config.keys():
69-
arp_map[config["neigh"].replace(':', '-')] = ""
68+
and ip_address(ip) in ip_network(vlan_cidr[vlan][ip_interface(ip).version]) \
69+
and "neigh" in config.keys():
70+
arp_map[config["neigh"].replace(':', '-').upper()] = ""
7071

7172
return arp_map
7273

@@ -94,7 +95,7 @@ def filter_fdb_entries(fdb_filename, arp_filename, config_db_filename, backup_fi
9495
def filter_fdb_entry(fdb_entry):
9596
for key, _ in fdb_entry.items():
9697
if 'FDB_TABLE' in key:
97-
return key.split(':')[-1] in arp_map
98+
return key.split(':')[-1].upper() in arp_map
9899

99100
# malformed entry, default to False so it will be deleted
100101
return False
@@ -124,44 +125,33 @@ def file_exists_or_raise(filename):
124125
if not os.path.exists(filename):
125126
raise Exception("file '{0}' does not exist".format(filename))
126127

127-
def main():
128+
def main(argv=sys.argv):
128129
parser = argparse.ArgumentParser()
129130
parser.add_argument('-f', '--fdb', type=str, default='/tmp/fdb.json', help='fdb file name')
130131
parser.add_argument('-a', '--arp', type=str, default='/tmp/arp.json', help='arp file name')
131132
parser.add_argument('-c', '--config_db', type=str, default='/tmp/config_db.json', help='config db file name')
132133
parser.add_argument('-b', '--backup_file', type=bool, default=True, help='Back up old fdb entries file')
133-
args = parser.parse_args()
134+
args = parser.parse_args(argv[1:])
134135

135136
fdb_filename = args.fdb
136137
arp_filename = args.arp
137138
config_db_filename = args.config_db
138139
backup_file = args.backup_file
139140

141+
res = 0
140142
try:
143+
syslog.openlog('filter_fdb_entries')
141144
file_exists_or_raise(fdb_filename)
142145
file_exists_or_raise(arp_filename)
143146
file_exists_or_raise(config_db_filename)
144147
except Exception as e:
145148
syslog.syslog(syslog.LOG_ERR, "Got an exception %s: Traceback: %s" % (str(e), traceback.format_exc()))
146-
else:
147-
filter_fdb_entries(fdb_filename, arp_filename, config_db_filename, backup_file)
148-
149-
return 0
150-
151-
if __name__ == '__main__':
152-
res = 0
153-
try:
154-
syslog.openlog('filter_fdb_entries')
155-
res = main()
156149
except KeyboardInterrupt:
157150
syslog.syslog(syslog.LOG_NOTICE, "SIGINT received. Quitting")
158151
res = 1
159-
except Exception as e:
160-
syslog.syslog(syslog.LOG_ERR, "Got an exception %s: Traceback: %s" % (str(e), traceback.format_exc()))
161-
res = 2
152+
else:
153+
filter_fdb_entries(fdb_filename, arp_filename, config_db_filename, backup_file)
162154
finally:
163155
syslog.closelog()
164-
try:
165-
sys.exit(res)
166-
except SystemExit:
167-
os._exit(res)
156+
157+
return res

scripts/fast-reboot

+1-1
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ if [[ "$REBOOT_TYPE" = "fast-reboot" ]]; then
378378
379379
FILTER_FDB_ENTRIES_RC=0
380380
# Filter FDB entries using MAC addresses from ARP table
381-
/usr/bin/filter_fdb_entries.py -f $DUMP_DIR/fdb.json -a $DUMP_DIR/arp.json -c $CONFIG_DB_FILE || FILTER_FDB_ENTRIES_RC=$?
381+
/usr/bin/filter_fdb_entries -f $DUMP_DIR/fdb.json -a $DUMP_DIR/arp.json -c $CONFIG_DB_FILE || FILTER_FDB_ENTRIES_RC=$?
382382
if [[ FILTER_FDB_ENTRIES_RC -ne 0 ]]; then
383383
error "Failed to filter FDb entries. Exit code: $FILTER_FDB_ENTRIES_RC"
384384
unload_kernel

setup.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def get_test_suite():
4646
'sfputil',
4747
'pfc',
4848
'psuutil',
49+
'fdbutil',
4950
'show',
5051
'sonic_installer',
5152
'sonic-utilities-tests',
@@ -70,7 +71,6 @@ def get_test_suite():
7071
'scripts/fast-reboot-dump.py',
7172
'scripts/fdbclear',
7273
'scripts/fdbshow',
73-
'scripts/filter_fdb_entries.py',
7474
'scripts/generate_dump',
7575
'scripts/intfutil',
7676
'scripts/lldpshow',
@@ -102,6 +102,7 @@ def get_test_suite():
102102
'counterpoll = counterpoll.main:cli',
103103
'crm = crm.main:cli',
104104
'debug = debug.main:cli',
105+
'filter_fdb_entries = fdbutil.filter_fdb_entries:main',
105106
'pfcwd = pfcwd.main:cli',
106107
'sfputil = sfputil.main:cli',
107108
'pfc = pfc.main:cli',

sonic-utilities-tests/filter_fdb_entries_test.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from collections import defaultdict
99
from filter_fdb_input.test_vectors import filterFdbEntriesTestVector
10+
from fdbutil.filter_fdb_entries import main as filterFdbMain
1011

1112
class TestFilterFdbEntries(object):
1213
"""
@@ -162,16 +163,16 @@ def testFilterFdbEntries(self, testData):
162163
"""
163164
try:
164165
self.__setUp(testData)
165-
166-
stdout, stderr, rc = self.__runCommand([
167-
"scripts/filter_fdb_entries.py",
166+
argv = [
167+
"filter_fdb_entries",
168168
"-a",
169169
self.ARP_FILENAME,
170170
"-f",
171171
self.FDB_FILENAME,
172172
"-c",
173173
self.CONFIG_DB_FILENAME,
174-
])
174+
]
175+
rc = filterFdbMain(argv)
175176
assert rc == 0, "Filter_fdb_entries.py failed with '{0}'".format(stderr)
176177
assert self.__verifyOutput(), "Test failed for test data: {0}".format(testData)
177178
finally:

sonic-utilities-tests/filter_fdb_input/config_db.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,9 @@
11251125
}
11261126
},
11271127
"VLAN_INTERFACE": {
1128-
"Vlan1000|192.168.0.1/21": {}
1128+
"Vlan1000": {},
1129+
"Vlan1000|192.168.0.1/21": {},
1130+
"Vlan1000|fc02:1000::1/64": {}
11291131
},
11301132
"BUFFER_PG": {
11311133
"Ethernet4|0": {

sonic-utilities-tests/filter_fdb_input/expected_fdb.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,13 @@
181181
},
182182
"OP": "SET"
183183
},
184+
{
185+
"FDB_TABLE:Vlan1000:24-8A-07-4C-F5-18": {
186+
"type": "dynamic",
187+
"port": "Ethernet24"
188+
},
189+
"OP": "SET"
190+
},
184191
{
185192
"FDB_TABLE:Vlan1000:72-06-00-01-02-72": {
186193
"type": "dynamic",
@@ -398,4 +405,4 @@
398405
},
399406
"OP": "SET"
400407
}
401-
]
408+
]

sonic-utilities-tests/filter_fdb_input/test_vectors.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@
4343
"Vlan1000": {}
4444
},
4545
"VLAN_INTERFACE": {
46-
"Vlan1000|192.168.0.1/21": {}
46+
"Vlan1000": {},
47+
"Vlan1000|192.168.0.1/21": {},
48+
"Vlan1000|fc02:1000::1/64": {}
4749
},
4850
},
4951
"expected_fdb": [
@@ -80,7 +82,8 @@
8082
"Vlan1000": {}
8183
},
8284
"VLAN_INTERFACE": {
83-
"Vlan1000|192.168.0.1/21": {}
85+
"Vlan1000|192.168.0.1/21": {},
86+
"Vlan1000|fc02:1000::1/64": {}
8487
},
8588
},
8689
"expected_fdb": [
@@ -154,8 +157,10 @@
154157
"Vlan1": {}
155158
},
156159
"VLAN_INTERFACE": {
157-
"Vlan1|25.103.178.1/21": {}
158-
},
160+
"Vlan1|25.103.178.1/21": {},
161+
"Vlan1": {},
162+
"Vlan1|fc02:1000::1/64": {}
163+
},
159164
},
160165
"expected_fdb": [
161166
{

0 commit comments

Comments
 (0)