diff --git a/tests/common/fixtures/ptfhost_utils.py b/tests/common/fixtures/ptfhost_utils.py index 0f1d7dc94c9..068a904c5db 100644 --- a/tests/common/fixtures/ptfhost_utils.py +++ b/tests/common/fixtures/ptfhost_utils.py @@ -177,7 +177,11 @@ def run_icmp_responder(duthost, ptfhost, tbinfo): templ = Template(open(os.path.join(TEMPLATES_DIR, ICMP_RESPONDER_CONF_TEMPL)).read()) ptf_indices = duthost.get_extended_minigraph_facts(tbinfo)["minigraph_ptf_indices"] vlan_intfs = duthost.get_vlan_intfs() + vlan_table = duthost.get_running_config_facts()['VLAN'] + vlan_name = list(vlan_table.keys())[0] + vlan_mac = vlan_table[vlan_name]['mac'] icmp_responder_args = " ".join("-i eth%s" % ptf_indices[_] for _ in vlan_intfs) + icmp_responder_args += " " + "-m {}".format(vlan_mac) ptfhost.copy( content=templ.render(icmp_responder_args=icmp_responder_args), dest=os.path.join(SUPERVISOR_CONFIG_DIR, "icmp_responder.conf") diff --git a/tests/scripts/icmp_responder.py b/tests/scripts/icmp_responder.py index 0722c47f3bb..5dca3cf4c80 100644 --- a/tests/scripts/icmp_responder.py +++ b/tests/scripts/icmp_responder.py @@ -6,7 +6,7 @@ from select import select -def respond_to_icmp_request(socket, request): +def respond_to_icmp_request(socket, request, dst_mac=None): """Respond to ICMP request.""" reply = request.copy() reply[ICMP].type = 0 @@ -14,7 +14,13 @@ def respond_to_icmp_request(socket, request): reply[ICMP].chksum = None reply[IP].src, reply[IP].dst = request[IP].dst, request[IP].src reply[IP].chksum = None - reply[Ether].src, reply[Ether].dst = request[Ether].dst, request[Ether].src + + if dst_mac is None: + icmp_reply_dst_mac = request[Ether].src + else: + icmp_reply_dst_mac = dst_mac + + reply[Ether].src, reply[Ether].dst = request[Ether].dst, icmp_reply_dst_mac socket.send(reply) @@ -23,7 +29,7 @@ class ICMPSniffer(object): TYPE_ECHO_REQUEST = 8 - def __init__(self, ifaces, request_handler=None): + def __init__(self, ifaces, request_handler=None, dst_mac=None): """ Init ICMP sniffer. @@ -37,6 +43,7 @@ def __init__(self, ifaces, request_handler=None): self.sniff_sockets.append(conf.L2socket(type=ETH_P_IP, iface=iface, filter="icmp")) self.iface_hwaddr[iface] = get_if_hwaddr(iface) self.request_handler = request_handler + self.dst_mac = dst_mac def __call__(self): try: @@ -46,7 +53,7 @@ def __call__(self): packet = s.recv() if packet is not None: if packet[ICMP].type == self.TYPE_ECHO_REQUEST and self.request_handler: - self.request_handler(s, packet) + self.request_handler(s, packet, self.dst_mac) finally: for s in self.sniff_sockets: s.close() @@ -55,8 +62,10 @@ def __call__(self): if __name__ == "__main__": parser = argparse.ArgumentParser(description="ICMP responder") parser.add_argument("--intf", "-i", dest="ifaces", required=True, action="append", help="interface to listen for ICMP request") + parser.add_argument("--dst_mac", "-m", dest="dst_mac", required=False, action="store", help="The destination MAC to use for ICMP echo replies") args = parser.parse_args() ifaces = args.ifaces + dst_mac = args.dst_mac - icmp_sniffer = ICMPSniffer(ifaces, request_handler=respond_to_icmp_request) + icmp_sniffer = ICMPSniffer(ifaces, request_handler=respond_to_icmp_request, dst_mac=dst_mac) icmp_sniffer()