pytest_esp_eth.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: CC0-1.0
  3. import contextlib
  4. import logging
  5. import os
  6. import socket
  7. from multiprocessing import Pipe, Process, connection
  8. from typing import Iterator
  9. import pytest
  10. from pytest_embedded import Dut
  11. from scapy.all import Ether, raw
  12. ETH_TYPE = 0x2222
  13. class EthTestIntf(object):
  14. def __init__(self, eth_type: int):
  15. self.target_if = ''
  16. self.eth_type = eth_type
  17. def find_target_if(self) -> None:
  18. # try to determine which interface to use
  19. netifs = os.listdir('/sys/class/net/')
  20. logging.info('detected interfaces: %s', str(netifs))
  21. for netif in netifs:
  22. if netif.find('eth') == 0 or netif.find('enp') == 0 or netif.find('eno') == 0:
  23. self.target_if = netif
  24. break
  25. if self.target_if == '':
  26. raise Exception('no network interface found')
  27. logging.info('Use %s for testing', self.target_if)
  28. @contextlib.contextmanager
  29. def configure_eth_if(self) -> Iterator[socket.socket]:
  30. so = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(self.eth_type))
  31. so.bind((self.target_if, 0))
  32. try:
  33. yield so
  34. finally:
  35. so.close()
  36. def send_eth_packet(self, mac: str) -> None:
  37. with self.configure_eth_if() as so:
  38. so.settimeout(10)
  39. payload = bytearray(1010)
  40. for i, _ in enumerate(payload):
  41. payload[i] = i & 0xff
  42. eth_frame = Ether(dst=mac, src=so.getsockname()[4], type=self.eth_type) / raw(payload)
  43. try:
  44. so.send(raw(eth_frame))
  45. except Exception as e:
  46. raise e
  47. def recv_resp_poke(self, i: int) -> None:
  48. with self.configure_eth_if() as so:
  49. so.settimeout(10)
  50. try:
  51. eth_frame = Ether(so.recv(60))
  52. if eth_frame.type == self.eth_type and eth_frame.load[0] == 0xfa:
  53. if eth_frame.load[1] != i:
  54. raise Exception('Missed Poke Packet')
  55. eth_frame.dst = eth_frame.src
  56. eth_frame.src = so.getsockname()[4]
  57. eth_frame.load = bytes.fromhex('fb') # POKE_RESP code
  58. so.send(raw(eth_frame))
  59. except Exception as e:
  60. raise e
  61. def traffic_gen(self, mac: str, pipe_rcv:connection.Connection) -> None:
  62. with self.configure_eth_if() as so:
  63. payload = bytes.fromhex('ff') # DUMMY_TRAFFIC code
  64. payload += bytes(1485)
  65. eth_frame = Ether(dst=mac, src=so.getsockname()[4], type=self.eth_type) / raw(payload)
  66. try:
  67. while pipe_rcv.poll() is not True:
  68. so.send(raw(eth_frame))
  69. except Exception as e:
  70. raise e
  71. def actual_test(dut: Dut) -> None:
  72. target_if = EthTestIntf(ETH_TYPE)
  73. target_if.find_target_if()
  74. dut.expect_exact('Press ENTER to see the list of tests')
  75. dut.write('\n')
  76. dut.expect_exact('Enter test for running.')
  77. dut.write('"start_and_stop"')
  78. dut.expect_unity_test_output()
  79. dut.expect_exact("Enter next test, or 'enter' to see menu")
  80. dut.write('"get_set_mac"')
  81. dut.expect_unity_test_output()
  82. dut.expect_exact("Enter next test, or 'enter' to see menu")
  83. with target_if.configure_eth_if() as so:
  84. so.settimeout(30)
  85. dut.write('"ethernet_broadcast_transmit"')
  86. eth_frame = Ether(so.recv(1024))
  87. for i in range(0, 1010):
  88. if eth_frame.load[i] != i & 0xff:
  89. raise Exception('Packet content mismatch')
  90. dut.expect_unity_test_output()
  91. dut.expect_exact("Enter next test, or 'enter' to see menu")
  92. dut.write('"recv_pkt"')
  93. res = dut.expect(
  94. r'([\s\S]*)'
  95. r'DUT MAC: ([0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2})'
  96. )
  97. target_if.send_eth_packet('ff:ff:ff:ff:ff:ff') # broadcast frame
  98. target_if.send_eth_packet('01:00:00:00:00:00') # multicast frame
  99. target_if.send_eth_packet(res.group(2)) # unicast frame
  100. dut.expect_unity_test_output(extra_before=res.group(1))
  101. dut.expect_exact("Enter next test, or 'enter' to see menu")
  102. dut.write('"start_stop_stress_test"')
  103. res = dut.expect(
  104. r'([\s\S]*)'
  105. r'DUT MAC: ([0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2})'
  106. )
  107. # Start/stop under heavy Tx traffic
  108. for tx_i in range(10):
  109. target_if.recv_resp_poke(tx_i)
  110. # Start/stop under heavy Rx traffic
  111. pipe_rcv, pipe_send = Pipe(False)
  112. tx_proc = Process(target=target_if.traffic_gen, args=(res.group(2), pipe_rcv, ))
  113. tx_proc.start()
  114. try:
  115. for rx_i in range(10):
  116. target_if.recv_resp_poke(rx_i)
  117. finally:
  118. pipe_send.send(0) # just send some dummy data
  119. tx_proc.join(5)
  120. if tx_proc.exitcode is None:
  121. tx_proc.terminate()
  122. dut.expect_unity_test_output(extra_before=res.group(1))
  123. @pytest.mark.esp32
  124. @pytest.mark.ip101
  125. @pytest.mark.parametrize('config', [
  126. 'ip101',
  127. ], indirect=True)
  128. @pytest.mark.flaky(reruns=3, reruns_delay=5)
  129. def test_esp_eth_ip101(dut: Dut) -> None:
  130. actual_test(dut)
  131. @pytest.mark.esp32
  132. @pytest.mark.lan8720
  133. @pytest.mark.parametrize('config', [
  134. 'lan8720',
  135. ], indirect=True)
  136. @pytest.mark.flaky(reruns=3, reruns_delay=5)
  137. def test_esp_eth_lan8720(dut: Dut) -> None:
  138. actual_test(dut)