pytest_esp_eth.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. # SPDX-FileCopyrightText: 2022-2023 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 = 0x3300
  13. class EthTestIntf(object):
  14. def __init__(self, eth_type: int, my_if: str = ''):
  15. self.target_if = ''
  16. self.eth_type = eth_type
  17. self.find_target_if(my_if)
  18. def find_target_if(self, my_if: str = '') -> None:
  19. # try to determine which interface to use
  20. netifs = os.listdir('/sys/class/net/')
  21. logging.info('detected interfaces: %s', str(netifs))
  22. for netif in netifs:
  23. # if no interface defined, try to find it automatically
  24. if my_if == '':
  25. if netif.find('eth') == 0 or netif.find('enp') == 0 or netif.find('eno') == 0:
  26. self.target_if = netif
  27. break
  28. else:
  29. if netif.find(my_if) == 0:
  30. self.target_if = my_if
  31. break
  32. if self.target_if == '':
  33. raise RuntimeError('network interface not found')
  34. logging.info('Use %s for testing', self.target_if)
  35. @contextlib.contextmanager
  36. def configure_eth_if(self, eth_type:int=0) -> Iterator[socket.socket]:
  37. if eth_type == 0:
  38. eth_type = self.eth_type
  39. so = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(eth_type))
  40. so.bind((self.target_if, 0))
  41. try:
  42. yield so
  43. finally:
  44. so.close()
  45. def send_eth_packet(self, mac: str) -> None:
  46. with self.configure_eth_if() as so:
  47. so.settimeout(10)
  48. payload = bytearray(1010)
  49. for i, _ in enumerate(payload):
  50. payload[i] = i & 0xff
  51. eth_frame = Ether(dst=mac, src=so.getsockname()[4], type=self.eth_type) / raw(payload)
  52. try:
  53. so.send(raw(eth_frame))
  54. except Exception as e:
  55. raise e
  56. def recv_resp_poke(self, mac:str, i:int=0) -> None:
  57. eth_type_ctrl = self.eth_type + 1
  58. with self.configure_eth_if(eth_type_ctrl) as so:
  59. so.settimeout(30)
  60. for _ in range(10):
  61. try:
  62. eth_frame = Ether(so.recv(60))
  63. except Exception as e:
  64. raise e
  65. if mac == eth_frame.src and eth_frame.load[0] == 0xfa:
  66. if eth_frame.load[1] != i:
  67. raise RuntimeError('Missed Poke Packet')
  68. logging.info('Poke Packet received...')
  69. eth_frame.dst = eth_frame.src
  70. eth_frame.src = so.getsockname()[4]
  71. eth_frame.load = bytes.fromhex('fb') # POKE_RESP code
  72. so.send(raw(eth_frame))
  73. break
  74. else:
  75. logging.warning('Unexpected Control packet')
  76. logging.warning('Expected Ctrl command: 0xfa, actual: 0x%x', eth_frame.load[0])
  77. logging.warning('Source MAC %s', eth_frame.src)
  78. else:
  79. raise RuntimeError('No Poke Packet!')
  80. def traffic_gen(self, mac: str, pipe_rcv:connection.Connection) -> None:
  81. with self.configure_eth_if() as so:
  82. payload = bytes.fromhex('ff') # DUMMY_TRAFFIC code
  83. payload += bytes(1485)
  84. eth_frame = Ether(dst=mac, src=so.getsockname()[4], type=self.eth_type) / raw(payload)
  85. try:
  86. while pipe_rcv.poll() is not True:
  87. so.send(raw(eth_frame))
  88. except Exception as e:
  89. raise e
  90. def ethernet_test(dut: Dut) -> None:
  91. dut.expect_exact('Press ENTER to see the list of tests')
  92. dut.write('\n')
  93. dut.expect_exact('Enter test for running.')
  94. dut.write('[ethernet]')
  95. dut.expect_unity_test_output(timeout=980)
  96. def ethernet_int_emac_hal_test(dut: Dut) -> None:
  97. dut.expect_exact('Press ENTER to see the list of tests')
  98. dut.write('\n')
  99. dut.expect_exact('Enter test for running.')
  100. dut.write('[emac_hal]')
  101. dut.expect_unity_test_output()
  102. def ethernet_l2_test(dut: Dut) -> None:
  103. target_if = EthTestIntf(ETH_TYPE)
  104. dut.expect_exact('Press ENTER to see the list of tests')
  105. dut.write('\n')
  106. dut.expect_exact('Enter test for running.')
  107. with target_if.configure_eth_if() as so:
  108. so.settimeout(30)
  109. dut.write('"ethernet broadcast transmit"')
  110. res = dut.expect(
  111. 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})'
  112. )
  113. # wait for POKE msg to be sure the switch already started forwarding the port's traffic
  114. # (there might be slight delay due to the RSTP execution)
  115. dut_mac = res.group(1).decode('utf-8')
  116. target_if.recv_resp_poke(mac=dut_mac)
  117. for _ in range(10):
  118. eth_frame = Ether(so.recv(1024))
  119. if dut_mac == eth_frame.src:
  120. break
  121. else:
  122. raise RuntimeError('No broadcast received from expected DUT MAC addr')
  123. for i in range(0, 1010):
  124. if eth_frame.load[i] != i & 0xff:
  125. raise RuntimeError('Packet content mismatch')
  126. dut.expect_unity_test_output()
  127. dut.expect_exact("Enter next test, or 'enter' to see menu")
  128. dut.write('"ethernet recv_pkt"')
  129. res = dut.expect(
  130. 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})'
  131. )
  132. dut_mac = res.group(1).decode('utf-8')
  133. # wait for POKE msg to be sure the switch already started forwarding the port's traffic
  134. # (there might be slight delay due to the RSTP execution)
  135. target_if.recv_resp_poke(mac=dut_mac)
  136. target_if.send_eth_packet('ff:ff:ff:ff:ff:ff') # broadcast frame
  137. target_if.send_eth_packet('01:00:00:00:00:00') # multicast frame
  138. target_if.send_eth_packet(mac=dut_mac) # unicast frame
  139. dut.expect_unity_test_output(extra_before=res.group(1))
  140. dut.expect_exact("Enter next test, or 'enter' to see menu")
  141. dut.write('"ethernet start/stop stress test under heavy traffic"')
  142. res = dut.expect(
  143. 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})'
  144. )
  145. dut_mac = res.group(1).decode('utf-8')
  146. # Start/stop under heavy Tx traffic
  147. for tx_i in range(10):
  148. target_if.recv_resp_poke(dut_mac, tx_i)
  149. dut.expect_exact('Ethernet stopped')
  150. for rx_i in range(10):
  151. target_if.recv_resp_poke(dut_mac, rx_i)
  152. # Start/stop under heavy Rx traffic
  153. pipe_rcv, pipe_send = Pipe(False)
  154. tx_proc = Process(target=target_if.traffic_gen, args=(dut_mac, pipe_rcv, ))
  155. tx_proc.start()
  156. dut.expect_exact('Ethernet stopped')
  157. pipe_send.send(0) # just send some dummy data
  158. tx_proc.join(5)
  159. if tx_proc.exitcode is None:
  160. tx_proc.terminate()
  161. dut.expect_unity_test_output(extra_before=res.group(1))
  162. @pytest.mark.esp32
  163. @pytest.mark.ethernet
  164. @pytest.mark.parametrize('config', [
  165. 'default_ip101',
  166. 'release_ip101',
  167. 'single_core_ip101'
  168. ], indirect=True)
  169. @pytest.mark.flaky(reruns=3, reruns_delay=5)
  170. def test_esp_ethernet(dut: Dut) -> None:
  171. ethernet_test(dut)
  172. @pytest.mark.esp32
  173. @pytest.mark.ethernet
  174. @pytest.mark.parametrize('config', [
  175. 'default_ip101',
  176. ], indirect=True)
  177. def test_esp_emac_hal(dut: Dut) -> None:
  178. ethernet_int_emac_hal_test(dut)
  179. @pytest.mark.esp32
  180. @pytest.mark.ip101
  181. @pytest.mark.parametrize('config', [
  182. 'default_ip101',
  183. ], indirect=True)
  184. def test_esp_eth_ip101(dut: Dut) -> None:
  185. ethernet_l2_test(dut)
  186. @pytest.mark.esp32
  187. @pytest.mark.lan8720
  188. @pytest.mark.parametrize('config', [
  189. 'default_lan8720',
  190. ], indirect=True)
  191. def test_esp_eth_lan8720(dut: Dut) -> None:
  192. ethernet_l2_test(dut)