def update_data(self): """ Update redis (caches config) Pulls the table references for each interface. """ self.route_dest_map = {} self.route_dest_list = [] ## The nexthop for loopbacks should be all zero for loip in self.loips: sub_id = ip2byte_tuple(loip) + (255, 255, 255, 255) + (self.tos, ) + (0, 0, 0, 0) self.route_dest_list.append(sub_id) self.route_dest_map[sub_id] = self.loips[loip].packed # Get list of front end asic namespaces for multi-asic platform. # This list will be empty for single asic platform. front_ns = multi_asic.get_all_namespaces()['front_ns'] ipnstr = "0.0.0.0/0" ipn = ipaddress.ip_network(ipnstr) route_str = "ROUTE_TABLE:0.0.0.0/0" for db_conn in Namespace.get_non_host_dbs(self.db_conn): # For multi-asic platform, proceed to get routes only for # front end namespaces. # For single-asic platform, front_ns will be empty list. if front_ns and db_conn.namespace not in front_ns: continue port_table = multi_asic.get_port_table_for_asic(db_conn.namespace) ent = db_conn.get_all(mibs.APPL_DB, route_str, blocking=False) if ent is None: continue nexthops = ent["nexthop"] ifnames = ent["ifname"] for nh, ifn in zip(nexthops.split(','), ifnames.split(',')): ## Ignore non front panel interfaces ## TODO: non front panel interfaces should not be in APPL_DB at very beginning ## This is to workaround the bug in current sonic-swss implementation if ifn == "eth0" or ifn == "lo" or ifn == "docker0": continue # Ignore internal asic routes if multi_asic.is_port_channel_internal(ifn, db_conn.namespace): continue if (ifn in port_table and multi_asic.PORT_ROLE in port_table[ifn] and port_table[ifn][multi_asic.PORT_ROLE] == multi_asic.INTERNAL_PORT): continue sub_id = ip2byte_tuple(ipn.network_address) + ip2byte_tuple( ipn.netmask) + (self.tos, ) + ip2byte_tuple(nh) self.route_dest_list.append(sub_id) self.route_dest_map[sub_id] = ipn.network_address.packed self.route_dest_list.sort()
def update_data(self): self.session_status_map = {} self.session_status_list = [] for neigh_key, db_index in self.neigh_state_map.items(): neigh_str = neigh_key neigh_str = neigh_str.split('|')[1] neigh_info = self.db_conn[db_index].get_all(mibs.STATE_DB, neigh_key, blocking=False) if neigh_info: state = neigh_info['state'] ip = ipaddress.ip_address(neigh_str) if type(ip) is ipaddress.IPv4Address: oid_head = (1, 4) else: oid_head = (2, 16) oid_ip = ip2byte_tuple(neigh_str) if state.isdigit(): status = 6 elif state in STATE_CODE: status = STATE_CODE[state] else: continue oid = oid_head + oid_ip self.session_status_list.append(oid) self.session_status_map[oid] = status self.session_status_list.sort()
def update_rem_if_mgmt(self, if_oid, if_name): lldp_kvs = Namespace.dbs_get_all(self.db_conn, mibs.APPL_DB, mibs.lldp_entry_table(if_name)) if not lldp_kvs or 'lldp_rem_man_addr' not in lldp_kvs: # this interfaces doesn't have remote lldp data, or the peer doesn't advertise his mgmt address return try: mgmt_ip_str = lldp_kvs['lldp_rem_man_addr'] mgmt_ip_str = mgmt_ip_str.strip() if len(mgmt_ip_str) == 0: # the peer advertise an emtpy mgmt address return mgmt_ip_set = set() for mgmt_ip in mgmt_ip_str.split(','): time_mark = int(lldp_kvs['lldp_rem_time_mark']) remote_index = int(lldp_kvs['lldp_rem_index']) subtype = self.get_subtype(mgmt_ip) if not subtype: logger.warning("Invalid management IP {}".format(mgmt_ip)) continue mgmt_ip_tuple = ip2byte_tuple(mgmt_ip) if mgmt_ip_tuple in mgmt_ip_set: continue elif subtype == ManAddrConst.man_addr_subtype_ipv4: addr_subtype_sub_oid = 4 else: addr_subtype_sub_oid = 16 mgmt_ip_set.add(mgmt_ip_tuple) mgmt_ip_sub_oid = (addr_subtype_sub_oid, *mgmt_ip_tuple) self.if_range.append((time_mark, if_oid, remote_index, subtype, *mgmt_ip_sub_oid)) except (KeyError, AttributeError) as e: logger.warning("Error updating remote mgmt addr: {}".format(e)) return self.if_range.sort()
def update_data(self): """ Update redis (caches config) Pulls the table references for each interface. """ self.nexthop_map = {} self.route_list = [] route_entries = Namespace.dbs_keys(self.db_conn, mibs.APPL_DB, "ROUTE_TABLE:*") if not route_entries: return for route_entry in route_entries: routestr = route_entry ipnstr = routestr[len("ROUTE_TABLE:"):] if ipnstr == "0.0.0.0/0": ipn = ipaddress.ip_network(ipnstr) ent = Namespace.dbs_get_all(self.db_conn, mibs.APPL_DB, routestr, blocking=True) nexthops = ent["nexthop"] for nh in nexthops.split(','): # TODO: if ipn contains IP range, create more sub_id here sub_id = ip2byte_tuple(ipn.network_address) self.route_list.append(sub_id) self.nexthop_map[sub_id] = ipaddress.ip_address(nh).packed break # Just need the first nexthop self.route_list.sort()
def _update_arp_info(self, dev, mac, ip): if_index = mibs.get_index_from_str(dev) if if_index is None: return mactuple = mac_decimals(mac) machex = ''.join(chr(b) for b in mactuple) # if MAC is all zero #if not any(mac): continue iptuple = ip2byte_tuple(ip) subid = (if_index, ) + iptuple self.arp_dest_map[subid] = machex self.arp_dest_list.append(subid)
def reinit_data(self): """ Subclass update data routine. """ self.man_addr_list = [] self.mgmt_ip_str = None # establish connection to application database. self.db_conn.connect(mibs.APPL_DB) mgmt_ip_bytes = self.db_conn.get(mibs.APPL_DB, mibs.LOC_CHASSIS_TABLE, 'lldp_loc_man_addr') if not mgmt_ip_bytes: logger.warning("Missing lldp_loc_man_addr from APPL DB") return self.mgmt_ip_str = mgmt_ip_bytes logger.debug("Got mgmt ip from db : {}".format(self.mgmt_ip_str)) try: addr_subtype_sub_oid = 4 mgmt_ip_sub_oid = None for mgmt_ip in self.mgmt_ip_str.split(','): if '.' in mgmt_ip: mgmt_ip_tuple = ip2byte_tuple(mgmt_ip) mgmt_ip_sub_oid = (addr_subtype_sub_oid, *mgmt_ip_tuple) break else: logger.error( "Could not find IPv4 address in lldp_loc_man_addr") return except ValueError: logger.error("Invalid local mgmt IP {}".format(self.mgmt_ip_str)) return sub_oid = (ManAddrConst.man_addr_subtype_ipv4, *mgmt_ip_sub_oid) self.man_addr_list.append(sub_oid)