tg_helpers.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:taf 作者: taf3 项目源码 文件源码
def arp_packets_sending(self, env, tg_port, sw_port, packet_num, ipgap, arp_packet=None, offset=0, retry=False):
        cmd = "match -f 5555 -p 30001 get_rules table 9"
        sw_t9 = {}
        sw_instances = self.table_9_test_preparation(env, tg_port, sw_port, ipgap=ipgap,
                                                     packet_num=packet_num, arp_packet=arp_packet,
                                                     offset=offset)
        # wait for table 9 to be populated
        time.sleep(5)
        # check table 9 entries
        assert sw_instances, "No end point ports found connected to the host"
        for switch in sw_instances:
            output = switch.ui.cli_send_command(cmd).stdout
            sw_t9[switch] = len(re.findall(r'^table\s+:\s+9', output, re.MULTILINE))
        if not retry:
            return sw_t9

        failed = False
        for switch, count in sw_t9.items():
            if count != packet_num:
                failed = True
                break
        if not failed:
            return sw_t9
        # in case not all entries are learned fast, try add them again
        arp_packet = deepcopy(arp_packet)
        srcmac = arp_packet[0]['Ether']['src']
        srcip = arp_packet[1]['ARP']['psrc']
        count = 2
        while failed and count:
            count -= 1
            self.class_logger.info("Adding not learned MAC addresses to the table 9")
            sw_instances = [switch for switch in env.switch[1].node.values() if switch.id != sw_port.split()[0]]
            for switch in sw_instances:
                output = switch.ui.cli_send_command("match -f 5555 -p 30001 get_rules table 9").stdout
                learned = re.findall(r'ethernet.dst_mac\s=\s((?:[\w]{2}:){5}[\w]{2})', output, re.MULTILINE)
                to_add = [el for el in [str(EUI(EUI(srcmac).value + ix, dialect=mac_unix_expanded))
                                        for ix in range(packet_num)] if el not in learned]
                self.class_logger.info("{} entries are missing for switch {}".format(len(to_add), switch.name))
                for mac in to_add:
                    diff = EUI(mac).value - EUI(srcmac).value
                    arp_packet[0]['Ether']['src'] = str(EUI(EUI(srcmac).value + diff, dialect=mac_unix_expanded))
                    arp_packet[1]['ARP']['psrc'] = str(IPAddress(IPAddress(srcip).value + diff))
                    # Prepare and send stream
                    arp_stream = env.tg[1].set_stream(arp_packet, count=1, iface=tg_port)
                    env.tg[1].send_stream(arp_stream)

            self.class_logger.info("Verify table#9 entries")
            for switch in sw_instances:
                output = switch.ui.cli_send_command(cmd).stdout
                if packet_num == len(re.findall(r'^table\s+:\s+9', output, re.MULTILINE)):
                    failed = False
                else:
                    failed = True
                    time.sleep(3)
                    break

        for switch in sw_instances:
            output = switch.ui.cli_send_command(cmd).stdout
            sw_t9[switch] = len(re.findall(r'^table\s+:\s+9', output, re.MULTILINE))
        return sw_t9
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号