Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 45 additions & 40 deletions scripts/ipintutil
Original file line number Diff line number Diff line change
Expand Up @@ -151,52 +151,57 @@ def get_ip_intfs_in_namespace(af, namespace, display):
"""
ip_intfs = {}
interfaces = multi_asic_util.multi_asic_get_ip_intf_from_ns(namespace)
if af == 2:
afstr = "inet"
elif af == 10:
afstr = "inet6"
if_db = multi_asic_util.multi_asic_get_addr_info_from_ns(namespace)
bgp_peer = get_bgp_peer()
for iface in interfaces:
ip_intf_attr = []
if namespace != constants.DEFAULT_NAMESPACE and skip_ip_intf_display(iface, display):
continue
try:
ipaddresses = multi_asic_util.multi_asic_get_ip_intf_addr_from_ns(namespace, iface)
except ValueError:
ipaddresses = if_db.get(iface, {}).get("addr_info", [])
ifaddresses = []
bgp_neighs = {}
ip_intf_attr = []
for addr in ipaddresses:
if addr["family"] != afstr:
continue
neighbor_name = 'N/A'
neighbor_ip = 'N/A'
local_ip = str(addr['local'])
netmask = str(addr["prefixlen"])
scopestr = ""
if ("scope" in addr) and (addr["scope"] == "link"):
scopestr="%{}".format(iface)
local_ip_with_mask = "{}{}/{}".format(local_ip, scopestr, str(netmask))
ifaddresses.append(["", local_ip_with_mask])
try:
neighbor_name = bgp_peer[local_ip][0]
neighbor_ip = bgp_peer[local_ip][1]
except KeyError:
pass

bgp_neighs.update({local_ip_with_mask: [neighbor_name, neighbor_ip]})

if len(ifaddresses) > 0:
admin = "up" if "UP" in if_db[iface]["flags"] else "down"
oper = "down" if if_db[iface]["operstate"] == "DOWN" else "up"
master = get_if_master(iface)
else:
continue
if af in ipaddresses:
ifaddresses = []
bgp_neighs = {}
ip_intf_attr = []
for ipaddr in ipaddresses[af]:
neighbor_name = 'N/A'
neighbor_ip = 'N/A'
local_ip = str(ipaddr['addr'])
if af == netifaces.AF_INET:
netmask = netaddr.IPAddress(ipaddr['netmask']).netmask_bits()
else:
netmask = ipaddr['netmask'].split('/', 1)[-1]
local_ip_with_mask = "{}/{}".format(local_ip, str(netmask))
ifaddresses.append(["", local_ip_with_mask])
try:
neighbor_name = bgp_peer[local_ip][0]
neighbor_ip = bgp_peer[local_ip][1]
except KeyError:
pass

bgp_neighs.update({local_ip_with_mask: [neighbor_name, neighbor_ip]})

if len(ifaddresses) > 0:
admin = get_if_admin_state(iface, namespace)
oper = get_if_oper_state(iface, namespace)
master = get_if_master(iface)

ip_intf_attr = {
"vrf": master,
"ipaddr": natsorted(ifaddresses),
"admin": admin,
"oper": oper,
"bgp_neighs": bgp_neighs,
"ns": namespace
}

ip_intfs[iface] = ip_intf_attr

ip_intf_attr = {
"vrf": master,
"ipaddr": natsorted(ifaddresses),
"admin": admin,
"oper": oper,
"bgp_neighs": bgp_neighs,
"ns": namespace
}

ip_intfs[iface] = ip_intf_attr
return ip_intfs


Expand Down
13 changes: 13 additions & 0 deletions utilities_common/multi_asic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from sonic_py_common import multi_asic, device_info
from utilities_common import constants
from utilities_common.general import load_db_config
import subprocess
import json


class MultiAsic(object):
Expand Down Expand Up @@ -165,6 +167,17 @@ def multi_asic_args(parser=None):
help='Display interfaces for specific namespace')
return parser

### Function to get the address info for all interfaces in a namespace
def multi_asic_get_addr_info_from_ns(namespace):
ip_addr_db_ns_cmd = f"ip netns exec {namespace} " if namespace != constants.DEFAULT_NAMESPACE else ""
ip_addr_db_ns_cmd += " ip -json addr show"
out = subprocess.check_output(ip_addr_db_ns_cmd, shell=True)
data = json.loads(out)
if_db = {}
for item in data:
if_db[item["ifname"]] = item
return if_db

def multi_asic_get_ip_intf_from_ns(namespace):
import pyroute2
if namespace != constants.DEFAULT_NAMESPACE:
Expand Down
Loading