pytest_esp_eth.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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_idf import IdfDut
  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. netifs.sort(reverse=True)
  22. logging.info('detected interfaces: %s', str(netifs))
  23. for netif in netifs:
  24. # if no interface defined, try to find it automatically
  25. if my_if == '':
  26. if netif.find('eth') == 0 or netif.find('enp') == 0 or netif.find('eno') == 0:
  27. self.target_if = netif
  28. break
  29. else:
  30. if netif.find(my_if) == 0:
  31. self.target_if = my_if
  32. break
  33. if self.target_if == '':
  34. raise RuntimeError('network interface not found')
  35. logging.info('Use %s for testing', self.target_if)
  36. @contextlib.contextmanager
  37. def configure_eth_if(self, eth_type:int=0) -> Iterator[socket.socket]:
  38. if eth_type == 0:
  39. eth_type = self.eth_type
  40. so = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(eth_type))
  41. so.bind((self.target_if, 0))
  42. try:
  43. yield so
  44. finally:
  45. so.close()
  46. def send_eth_packet(self, mac: str) -> None:
  47. with self.configure_eth_if() as so:
  48. so.settimeout(10)
  49. payload = bytearray(1010)
  50. for i, _ in enumerate(payload):
  51. payload[i] = i & 0xff
  52. eth_frame = Ether(dst=mac, src=so.getsockname()[4], type=self.eth_type) / raw(payload)
  53. try:
  54. so.send(raw(eth_frame))
  55. except Exception as e:
  56. raise e
  57. def recv_resp_poke(self, mac:str, i:int=0) -> None:
  58. eth_type_ctrl = self.eth_type + 1
  59. with self.configure_eth_if(eth_type_ctrl) as so:
  60. so.settimeout(30)
  61. for _ in range(10):
  62. try:
  63. eth_frame = Ether(so.recv(60))
  64. except Exception as e:
  65. raise e
  66. if mac == eth_frame.src and eth_frame.load[0] == 0xfa:
  67. if eth_frame.load[1] != i:
  68. raise RuntimeError('Missed Poke Packet')
  69. logging.info('Poke Packet received...')
  70. eth_frame.dst = eth_frame.src
  71. eth_frame.src = so.getsockname()[4]
  72. eth_frame.load = bytes.fromhex('fb') # POKE_RESP code
  73. so.send(raw(eth_frame))
  74. break
  75. else:
  76. logging.warning('Unexpected Control packet')
  77. logging.warning('Expected Ctrl command: 0xfa, actual: 0x%x', eth_frame.load[0])
  78. logging.warning('Source MAC %s', eth_frame.src)
  79. else:
  80. raise RuntimeError('No Poke Packet!')
  81. def traffic_gen(self, mac: str, pipe_rcv:connection.Connection) -> None:
  82. with self.configure_eth_if() as so:
  83. payload = bytes.fromhex('ff') # DUMMY_TRAFFIC code
  84. payload += bytes(1485)
  85. eth_frame = Ether(dst=mac, src=so.getsockname()[4], type=self.eth_type) / raw(payload)
  86. try:
  87. while pipe_rcv.poll() is not True:
  88. so.send(raw(eth_frame))
  89. except Exception as e:
  90. raise e
  91. def ethernet_test(dut: IdfDut) -> None:
  92. dut.run_all_single_board_cases(group='ethernet', timeout=980)
  93. def ethernet_int_emac_hal_test(dut: IdfDut) -> None:
  94. dut.run_all_single_board_cases(group='emac_hal')
  95. def ethernet_l2_test(dut: IdfDut) -> None:
  96. target_if = EthTestIntf(ETH_TYPE)
  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. with target_if.configure_eth_if() as so:
  101. so.settimeout(30)
  102. dut.write('"ethernet broadcast transmit"')
  103. res = dut.expect(
  104. 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})'
  105. )
  106. # wait for POKE msg to be sure the switch already started forwarding the port's traffic
  107. # (there might be slight delay due to the RSTP execution)
  108. dut_mac = res.group(1).decode('utf-8')
  109. target_if.recv_resp_poke(mac=dut_mac)
  110. for _ in range(10):
  111. eth_frame = Ether(so.recv(1024))
  112. if dut_mac == eth_frame.src:
  113. break
  114. else:
  115. raise RuntimeError('No broadcast received from expected DUT MAC addr')
  116. for i in range(0, 1010):
  117. if eth_frame.load[i] != i & 0xff:
  118. raise RuntimeError('Packet content mismatch')
  119. dut.expect_unity_test_output()
  120. dut.expect_exact("Enter next test, or 'enter' to see menu")
  121. dut.write('"ethernet recv_pkt"')
  122. res = dut.expect(
  123. 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})'
  124. )
  125. dut_mac = res.group(1).decode('utf-8')
  126. # wait for POKE msg to be sure the switch already started forwarding the port's traffic
  127. # (there might be slight delay due to the RSTP execution)
  128. target_if.recv_resp_poke(mac=dut_mac)
  129. target_if.send_eth_packet('ff:ff:ff:ff:ff:ff') # broadcast frame
  130. target_if.send_eth_packet('01:00:5e:00:00:00') # IPv4 multicast frame (some SPI Eth modules filter multicast other than IP)
  131. target_if.send_eth_packet(mac=dut_mac) # unicast frame
  132. dut.expect_unity_test_output(extra_before=res.group(1))
  133. dut.expect_exact("Enter next test, or 'enter' to see menu")
  134. dut.write('"ethernet start/stop stress test under heavy traffic"')
  135. res = dut.expect(
  136. 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})'
  137. )
  138. dut_mac = res.group(1).decode('utf-8')
  139. # Start/stop under heavy Tx traffic
  140. for tx_i in range(10):
  141. target_if.recv_resp_poke(dut_mac, tx_i)
  142. dut.expect_exact('Ethernet stopped')
  143. for rx_i in range(10):
  144. target_if.recv_resp_poke(dut_mac, rx_i)
  145. # Start/stop under heavy Rx traffic
  146. pipe_rcv, pipe_send = Pipe(False)
  147. tx_proc = Process(target=target_if.traffic_gen, args=(dut_mac, pipe_rcv, ))
  148. tx_proc.start()
  149. dut.expect_exact('Ethernet stopped')
  150. pipe_send.send(0) # just send some dummy data
  151. tx_proc.join(5)
  152. if tx_proc.exitcode is None:
  153. tx_proc.terminate()
  154. dut.expect_unity_test_output(extra_before=res.group(1))
  155. # ----------- IP101 -----------
  156. @pytest.mark.esp32
  157. @pytest.mark.ethernet
  158. @pytest.mark.parametrize('config', [
  159. 'default_ip101',
  160. 'release_ip101',
  161. 'single_core_ip101'
  162. ], indirect=True)
  163. @pytest.mark.flaky(reruns=3, reruns_delay=5)
  164. def test_esp_ethernet(dut: IdfDut) -> None:
  165. ethernet_test(dut)
  166. @pytest.mark.esp32
  167. @pytest.mark.ethernet
  168. @pytest.mark.parametrize('config', [
  169. 'default_ip101',
  170. ], indirect=True)
  171. def test_esp_emac_hal(dut: IdfDut) -> None:
  172. ethernet_int_emac_hal_test(dut)
  173. @pytest.mark.esp32
  174. @pytest.mark.ip101
  175. @pytest.mark.parametrize('config', [
  176. 'default_ip101',
  177. ], indirect=True)
  178. def test_esp_eth_ip101(dut: IdfDut) -> None:
  179. ethernet_l2_test(dut)
  180. # ----------- LAN8720 -----------
  181. @pytest.mark.esp32
  182. @pytest.mark.eth_lan8720
  183. @pytest.mark.nightly_run
  184. @pytest.mark.parametrize('config', [
  185. 'default_lan8720',
  186. ], indirect=True)
  187. def test_esp_eth_lan8720(dut: IdfDut) -> None:
  188. ethernet_test(dut)
  189. dut.serial.hard_reset()
  190. ethernet_l2_test(dut)
  191. # ----------- RTL8201 -----------
  192. @pytest.mark.esp32
  193. @pytest.mark.eth_rtl8201
  194. @pytest.mark.nightly_run
  195. @pytest.mark.parametrize('config', [
  196. 'default_rtl8201',
  197. ], indirect=True)
  198. def test_esp_eth_rtl8201(dut: IdfDut) -> None:
  199. ethernet_test(dut)
  200. dut.serial.hard_reset()
  201. ethernet_l2_test(dut)
  202. # ----------- KSZ8041 -----------
  203. @pytest.mark.esp32
  204. @pytest.mark.eth_ksz8041
  205. @pytest.mark.nightly_run
  206. @pytest.mark.parametrize('config', [
  207. 'default_ksz8041',
  208. ], indirect=True)
  209. def test_esp_eth_ksz8041(dut: IdfDut) -> None:
  210. ethernet_test(dut)
  211. dut.serial.hard_reset()
  212. ethernet_l2_test(dut)
  213. # ----------- DP83848 -----------
  214. @pytest.mark.esp32
  215. @pytest.mark.eth_dp83848
  216. @pytest.mark.nightly_run
  217. @pytest.mark.parametrize('config', [
  218. 'default_dp83848',
  219. ], indirect=True)
  220. def test_esp_eth_dp83848(dut: IdfDut) -> None:
  221. ethernet_test(dut)
  222. dut.serial.hard_reset()
  223. ethernet_l2_test(dut)
  224. # ----------- W5500 -----------
  225. @pytest.mark.esp32
  226. @pytest.mark.eth_w5500
  227. @pytest.mark.nightly_run
  228. @pytest.mark.parametrize('config', [
  229. 'default_w5500',
  230. ], indirect=True)
  231. def test_esp_eth_w5500(dut: IdfDut) -> None:
  232. ethernet_test(dut)
  233. dut.serial.hard_reset()
  234. ethernet_l2_test(dut)
  235. # ----------- KSZ8851SNL -----------
  236. @pytest.mark.esp32
  237. @pytest.mark.eth_ksz8851snl
  238. @pytest.mark.nightly_run
  239. @pytest.mark.parametrize('config', [
  240. 'default_ksz8851snl',
  241. ], indirect=True)
  242. def test_esp_eth_ksz8851snl(dut: IdfDut) -> None:
  243. ethernet_test(dut)
  244. dut.serial.hard_reset()
  245. ethernet_l2_test(dut)
  246. # ----------- DM9051 -----------
  247. @pytest.mark.esp32
  248. @pytest.mark.eth_dm9051
  249. @pytest.mark.nightly_run
  250. @pytest.mark.parametrize('config', [
  251. 'default_dm9051',
  252. ], indirect=True)
  253. def test_esp_eth_dm9051(dut: IdfDut) -> None:
  254. ethernet_test(dut)
  255. dut.serial.hard_reset()
  256. ethernet_l2_test(dut)