From 6d7610aeea7f18606dd7389402b6cf80c62045e5 Mon Sep 17 00:00:00 2001 From: Lawrence Lee Date: Thu, 24 Mar 2022 22:58:22 +0000 Subject: [PATCH] [tun_pkt]: Wait for AsyncSniffer to init fully Signed-off-by: Lawrence Lee --- .../docker-orchagent/tunnel_packet_handler.py | 86 ++++++++++--------- 1 file changed, 46 insertions(+), 40 deletions(-) diff --git a/dockers/docker-orchagent/tunnel_packet_handler.py b/dockers/docker-orchagent/tunnel_packet_handler.py index 3d9b48b450b6..682316eb6d49 100755 --- a/dockers/docker-orchagent/tunnel_packet_handler.py +++ b/dockers/docker-orchagent/tunnel_packet_handler.py @@ -51,6 +51,10 @@ def __init__(self): self._portchannel_intfs = None self.up_portchannels = None self.netlink_api = IPRoute() + self.sniffer = None + self.self_ip = '' + self.packet_filter = '' + self.sniff_intfs = [] @property def portchannel_intfs(self): @@ -270,6 +274,39 @@ def sniffer_restart_required(self, messages): return True return False + def start_sniffer(self): + """ + Starts an AsyncSniffer and waits for it to inititalize fully + """ + self.sniffer = AsyncSniffer( + iface=self.sniff_intfs, + filter=self.packet_filter, + prn=self.ping_inner_dst, + store=0 + ) + self.sniffer.start() + + while not hasattr(self.sniffer, 'stop_cb'): + time.sleep(0.1) + + def ping_inner_dst(self, packet): + """ + Pings the inner destination IP for an encapsulated packet + + Args: + packet: The encapsulated packet received + """ + inner_packet_type = self.get_inner_pkt_type(packet) + if inner_packet_type and packet[IP].dst == self.self_ip: + cmds = ['timeout', '0.2', 'ping', '-c1', + '-W1', '-i0', '-n', '-q'] + if inner_packet_type == IPv6: + cmds.append('-6') + dst_ip = packet[IP].payload[inner_packet_type].dst + cmds.append(dst_ip) + logger.log_info("Running command '{}'".format(' '.join(cmds))) + subprocess.run(cmds, stdout=subprocess.DEVNULL) + def listen_for_tunnel_pkts(self): """ Listens for tunnel packets that are trapped to CPU @@ -277,59 +314,28 @@ def listen_for_tunnel_pkts(self): These packets may be trapped if there is no neighbor info for the inner packet destination IP in the hardware. """ - - def _ping_inner_dst(packet): - """ - Pings the inner destination IP for an encapsulated packet - - Args: - packet: The encapsulated packet received - """ - inner_packet_type = self.get_inner_pkt_type(packet) - if inner_packet_type and packet[IP].dst == self_ip: - cmds = ['timeout', '0.2', 'ping', '-c1', - '-W1', '-i0', '-n', '-q'] - if inner_packet_type == IPv6: - cmds.append('-6') - dst_ip = packet[IP].payload[inner_packet_type].dst - cmds.append(dst_ip) - logger.log_info("Running command '{}'".format(' '.join(cmds))) - subprocess.run(cmds, stdout=subprocess.DEVNULL) - - self_ip, peer_ip = self.get_ipinip_tunnel_addrs() - if self_ip is None or peer_ip is None: + self.self_ip, peer_ip = self.get_ipinip_tunnel_addrs() + if self.self_ip is None or peer_ip is None: logger.log_notice('Could not get tunnel addresses from ' 'config DB, exiting...') return None - packet_filter = 'host {} and host {}'.format(self_ip, peer_ip) + self.packet_filter = 'host {} and host {}'.format(self.self_ip, peer_ip) logger.log_notice('Starting tunnel packet handler for {}' - .format(packet_filter)) + .format(self.packet_filter)) - sniff_intfs = self.get_up_portchannels() - logger.log_info("Listening on interfaces {}".format(sniff_intfs)) + self.sniff_intfs = self.get_up_portchannels() + logger.log_info("Listening on interfaces {}".format(self.sniff_intfs)) - sniffer = AsyncSniffer( - iface=sniff_intfs, - filter=packet_filter, - prn=_ping_inner_dst, - store=0 - ) - sniffer.start() + self.start_sniffer() while True: msgs = self.wait_for_netlink_msgs() if self.sniffer_restart_required(msgs): - sniffer.stop() + self.sniffer.stop() sniff_intfs = self.get_up_portchannels() logger.log_notice('Restarting tunnel packet handler on ' 'interfaces {}'.format(sniff_intfs)) - sniffer = AsyncSniffer( - iface=sniff_intfs, - filter=packet_filter, - prn=_ping_inner_dst, - store=0 - ) - sniffer.start() + self.start_sniffer() def run(self): """