|
4 | 4 | import json
|
5 | 5 | import socket
|
6 | 6 | import struct
|
| 7 | +import os |
7 | 8 | from fcntl import ioctl
|
8 | 9 | import binascii
|
9 | 10 |
|
@@ -213,10 +214,48 @@ def garp_send(arp_entries, map_mac_ip_per_vlan):
|
213 | 214 |
|
214 | 215 | return
|
215 | 216 |
|
| 217 | +def get_default_entries(db, route): |
| 218 | + key = 'ROUTE_TABLE:%s' % route |
| 219 | + keys = db.keys(db.APPL_DB, key) |
| 220 | + if keys is None: |
| 221 | + return None |
| 222 | + |
| 223 | + entry = db.get_all(db.APPL_DB, key) |
| 224 | + obj = { |
| 225 | + key: entry, |
| 226 | + 'OP': 'SET' |
| 227 | + } |
| 228 | + |
| 229 | + return obj |
| 230 | + |
| 231 | +def generate_default_route_entries(filename): |
| 232 | + db = swsssdk.SonicV2Connector() |
| 233 | + db.connect(db.APPL_DB, False) # Make one attempt only |
| 234 | + |
| 235 | + default_routes_output = [] |
| 236 | + |
| 237 | + ipv4_default = get_default_entries(db, '0.0.0.0/0') |
| 238 | + if ipv4_default is not None: |
| 239 | + default_routes_output.append(ipv4_default) |
| 240 | + |
| 241 | + ipv6_default = get_default_entries(db, '::/0') |
| 242 | + if ipv6_default is not None: |
| 243 | + default_routes_output.append(ipv6_default) |
| 244 | + |
| 245 | + db.close(db.APPL_DB) |
| 246 | + |
| 247 | + if len(default_routes_output) > 0: |
| 248 | + with open(filename, 'w') as fp: |
| 249 | + json.dump(default_routes_output, fp, indent=2, separators=(',', ': ')) |
| 250 | + else: |
| 251 | + if os.path.isfile(filename): |
| 252 | + os.unlink(filename) |
| 253 | + |
216 | 254 |
|
217 | 255 | def main():
|
218 | 256 | all_available_macs, map_mac_ip_per_vlan = generate_fdb_entries('/tmp/fdb.json')
|
219 | 257 | arp_entries = generate_arp_entries('/tmp/arp.json', all_available_macs)
|
| 258 | + generate_default_route_entries('/tmp/default_routes.json') |
220 | 259 | garp_send(arp_entries, map_mac_ip_per_vlan)
|
221 | 260 |
|
222 | 261 | return
|
|
0 commit comments