@@ -11,14 +11,13 @@ import argparse
11
11
import json
12
12
import os
13
13
import requests
14
- import subprocess
15
14
import sys
16
15
import time
17
16
import traceback
18
17
import warnings
19
18
20
19
from sonic_py_common import logger
21
- from swsssdk import ConfigDBConnector
20
+ from swsssdk import ConfigDBConnector , SonicV2Connector
22
21
from netaddr import IPAddress , IPNetwork
23
22
24
23
@@ -76,6 +75,15 @@ def connect_config_db():
76
75
config_db = ConfigDBConnector ()
77
76
config_db .connect ()
78
77
78
+ #
79
+ # Global variable of app_db
80
+ #
81
+ appl_db = None
82
+
83
+ def connect_app_db ():
84
+ global appl_db
85
+ appl_db = SonicV2Connector (host = "127.0.0.1" )
86
+ appl_db .connect (appl_db .APPL_DB )
79
87
80
88
#
81
89
# Check if a DIP returned from ferret is in any of this switch's VLANs
@@ -125,13 +133,9 @@ def get_switch_hwsku():
125
133
126
134
127
135
def extract_ip_ver_addr (ip_prefix ):
128
- addr = ip_prefix .split ('/' )[0 ]
129
-
130
- if ':' in addr :
131
- ver = 6
132
- else :
133
- ver = 4
134
-
136
+ ip = IPNetwork (ip_prefix )
137
+ addr = str (ip .ip )
138
+ ver = ip .ip .version
135
139
return (ver , addr )
136
140
137
141
@@ -201,65 +205,40 @@ def get_vlan_interface_vxlan_id(vlan_intf_name):
201
205
return vlan_intf_name [4 :]
202
206
203
207
204
- def get_vlan_addr (vlan_intf_name , ip_ver ):
208
+ def get_vlan_addr_prefix (vlan_intf_name , ip_ver ):
205
209
vlan_intfs = config_db .get_table ('VLAN_INTERFACE' )
206
210
vlan_addr = []
211
+ vlan_prefix = []
207
212
208
213
for intf in vlan_intfs .keys ():
209
214
if not is_ip_prefix_in_key (intf ):
210
215
continue
211
216
if vlan_intf_name in intf :
212
- intf_ip_prefix = intf [1 ]
213
- (intf_ip_ver , intf_ip_addr ) = extract_ip_ver_addr (intf_ip_prefix )
217
+ intf_ip = IPNetwork (intf [1 ])
218
+ intf_ip_addr = str (intf_ip .ip )
219
+ intf_ip_ver = intf_ip .ip .version
220
+ intf_prefixlen = intf_ip .prefixlen
214
221
if intf_ip_ver == ip_ver :
215
222
vlan_addr .append (intf_ip_addr )
223
+ vlan_prefix .append (intf_prefixlen )
216
224
217
- return vlan_addr
225
+ return vlan_addr , vlan_prefix
218
226
219
227
220
228
def get_vlan_addresses (vlan_interface ):
221
229
vlan_id = get_vlan_interface_vlan_id (vlan_interface )
222
230
vxlan_id = get_vlan_interface_vxlan_id (vlan_interface )
223
231
224
232
mac_addr = None
225
- ipv4_addr = []
226
- ipv6_addr = []
227
-
228
- '''
229
- Sample output for "ip addr show Vlan101"
230
-
231
- 218: Vlan101@Bridge: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
232
- link/ether 98:5d:82:01:d8:48 brd ff:ff:ff:ff:ff:ff
233
- inet 10.171.22.113/29 scope global Vlan101
234
- valid_lft forever preferred_lft forever
235
- inet6 2603:10b0:10f:843c::1/64 scope global
236
- valid_lft forever preferred_lft forever
237
- inet6 fe80::9a5d:82ff:fe01:d848/64 scope link
238
- valid_lft forever preferred_lft forever
239
- '''
240
- try :
241
- out = subprocess .check_output (['ip' , 'addr' , 'show' , vlan_interface ])
242
- for line in out .splitlines ():
243
- keys = line .strip ().split (' ' )
244
- if keys [0 ] == 'inet' :
245
- ipv4_addr .append (keys [1 ].split ('/' )[0 ])
246
- elif keys [0 ] == 'inet6' :
247
- ipv6_addr .append (keys [1 ].split ('/' )[0 ])
248
- elif keys [0 ] == 'link/ether' :
249
- mac_addr = keys [1 ]
250
- except Exception :
251
- log .log_error ('failed to get %s addresses from o.s.' % vlan_interface )
233
+ ipv4_addr , ipv4_prefix = get_vlan_addr_prefix (vlan_interface , 4 )
234
+ ipv6_addr , ipv6_prefix = get_vlan_addr_prefix (vlan_interface , 6 )
252
235
236
+ metadata = config_db .get_table ('DEVICE_METADATA' )
237
+ mac_addr = metadata ['localhost' ]['mac' ]
253
238
if not mac_addr :
254
239
mac_addr = get_vlan_interface_mac_address (vlan_interface )
255
240
256
- if not ipv4_addr :
257
- ipv4_addr = get_vlan_addr (vlan_interface , 4 )
258
-
259
- if not ipv6_addr :
260
- ipv6_addr = get_vlan_addr (vlan_interface , 6 )
261
-
262
- return vlan_id , vxlan_id , ipv4_addr , ipv6_addr , mac_addr
241
+ return vlan_id , vxlan_id , ipv4_addr , ipv4_prefix , ipv6_addr , ipv6_prefix , mac_addr
263
242
264
243
#
265
244
# Set up neighbor advertiser slice on Ferret
@@ -281,28 +260,46 @@ def construct_neighbor_advertiser_slice():
281
260
282
261
all_vlan_interfaces = get_vlan_interfaces ()
283
262
263
+ vlan_intf_table = config_db .get_table ('VLAN_INTERFACE' )
264
+
265
+ vxlanPort = appl_db .get (appl_db .APPL_DB , 'SWITCH_TABLE:switch' , 'vxlan_port' )
266
+
284
267
for vlan_interface in all_vlan_interfaces :
285
- vlan_id , vxlan_id , ipv4_addr , ipv6_addr , mac_addr = get_vlan_addresses (vlan_interface )
268
+ vlan_id , vxlan_id , ipv4_addr , ipv4_prefix , ipv6_addr , ipv6_prefix , mac_addr = get_vlan_addresses (vlan_interface )
286
269
287
270
if not mac_addr :
288
271
log .log_warning ('Cannot find mac addr of vlan interface {}' .format (vlan_interface ))
289
272
continue
290
273
291
274
ipv4_mappings = []
275
+ ipv6_mappings = []
276
+ ctr = 0
292
277
for addr in ipv4_addr :
278
+ if 'proxy_arp' in vlan_intf_table [vlan_interface ] and vlan_intf_table [vlan_interface ]['proxy_arp' ] == 'enabled' :
279
+ ipPrefixLen = str (ipv4_prefix [ctr ])
280
+ else :
281
+ ipPrefixLen = '32'
293
282
mapping = {
294
283
'ipAddr' : addr ,
284
+ 'ipPrefixLen' : ipPrefixLen ,
295
285
'macAddr' : mac_addr
296
286
}
297
287
ipv4_mappings .append (mapping )
288
+ ctr += 1
298
289
299
- ipv6_mappings = []
290
+ ctr = 0
300
291
for addr in ipv6_addr :
292
+ if 'proxy_arp' in vlan_intf_table [vlan_interface ] and vlan_intf_table [vlan_interface ]['proxy_arp' ] == 'enabled' :
293
+ ipPrefixLen = str (ipv6_prefix [ctr ])
294
+ else :
295
+ ipPrefixLen = '128'
301
296
mapping = {
302
297
'ipAddr' : addr ,
298
+ 'ipPrefixLen' : ipPrefixLen ,
303
299
'macAddr' : mac_addr
304
300
}
305
301
ipv6_mappings .append (mapping )
302
+ ctr += 1
306
303
307
304
vlan_interface_obj = {
308
305
'vlanId' : vlan_id ,
@@ -311,6 +308,9 @@ def construct_neighbor_advertiser_slice():
311
308
'ipv6AddrMappings' : ipv6_mappings
312
309
}
313
310
311
+ if vxlanPort :
312
+ vlan_interface_obj ['vxlanPort' ] = vxlanPort
313
+
314
314
vlan_interfaces_obj .append (vlan_interface_obj )
315
315
316
316
slice_obj = {
@@ -539,7 +539,7 @@ def main():
539
539
sys .exit (1 )
540
540
541
541
connect_config_db ()
542
-
542
+ connect_app_db ()
543
543
if operation_mode == 'set' :
544
544
set_success = False
545
545
0 commit comments