pytest_esp_eth.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 = 0x2222
  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 Exception('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, 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. try:
  61. eth_frame = Ether(so.recv(60))
  62. if eth_frame.load[0] == 0xfa:
  63. if eth_frame.load[1] != i:
  64. raise Exception('Missed Poke Packet')
  65. logging.info('Poke Packet received...')
  66. eth_frame.dst = eth_frame.src
  67. eth_frame.src = so.getsockname()[4]
  68. eth_frame.load = bytes.fromhex('fb') # POKE_RESP code
  69. so.send(raw(eth_frame))
  70. except Exception as e:
  71. raise e
  72. def traffic_gen(self, mac: str, pipe_rcv:connection.Connection) -> None:
  73. with self.configure_eth_if() as so:
  74. payload = bytes.fromhex('ff') # DUMMY_TRAFFIC code
  75. payload += bytes(1485)
  76. eth_frame = Ether(dst=mac, src=so.getsockname()[4], type=self.eth_type) / raw(payload)
  77. try:
  78. while pipe_rcv.poll() is not True:
  79. so.send(raw(eth_frame))
  80. except Exception as e:
  81. raise e
  82. def ethernet_test(dut: Dut) -> None:
  83. dut.expect_exact('Press ENTER to see the list of tests')
  84. dut.write('\n')
  85. dut.expect_exact('Enter test for running.')
  86. dut.write('[ethernet]')
  87. dut.expect_unity_test_output(timeout=980)
  88. def ethernet_int_emac_hal_test(dut: Dut) -> None:
  89. dut.expect_exact('Press ENTER to see the list of tests')
  90. dut.write('\n')
  91. dut.expect_exact('Enter test for running.')
  92. dut.write('[emac_hal]')
  93. dut.expect_unity_test_output()
  94. def ethernet_l2_test(dut: Dut) -> None:
  95. target_if = EthTestIntf(ETH_TYPE)
  96. dut.expect_exact('Press ENTER to see the list of tests')
  97. dut.write('\n')
  98. dut.expect_exact('Enter test for running.')
  99. with target_if.configure_eth_if() as so:
  100. so.settimeout(30)
  101. dut.write('"ethernet broadcast transmit"')
  102. # wait for POKE msg to be sure the switch already started forwarding the port's traffic
  103. # (there might be slight delay due to the RSTP execution)
  104. target_if.recv_resp_poke()
  105. eth_frame = Ether(so.recv(1024))
  106. for i in range(0, 1010):
  107. if eth_frame.load[i] != i & 0xff:
  108. raise Exception('Packet content mismatch')
  109. dut.expect_unity_test_output()
  110. dut.expect_exact("Enter next test, or 'enter' to see menu")
  111. dut.write('"ethernet recv_pkt"')
  112. res = dut.expect(
  113. r'([\s\S]*)'
  114. 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})'
  115. )
  116. # wait for POKE msg to be sure the switch already started forwarding the port's traffic
  117. # (there might be slight delay due to the RSTP execution)
  118. target_if.recv_resp_poke()
  119. target_if.send_eth_packet('ff:ff:ff:ff:ff:ff') # broadcast frame
  120. target_if.send_eth_packet('01:00:00:00:00:00') # multicast frame
  121. target_if.send_eth_packet(res.group(2)) # unicast frame
  122. dut.expect_unity_test_output(extra_before=res.group(1))
  123. dut.expect_exact("Enter next test, or 'enter' to see menu")
  124. dut.write('"ethernet start/stop stress test under heavy traffic"')
  125. res = dut.expect(
  126. r'([\s\S]*)'
  127. 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})'
  128. )
  129. # Start/stop under heavy Tx traffic
  130. for tx_i in range(10):
  131. target_if.recv_resp_poke(tx_i)
  132. dut.expect_exact('Ethernet stopped')
  133. for rx_i in range(10):
  134. target_if.recv_resp_poke(rx_i)
  135. # Start/stop under heavy Rx traffic
  136. pipe_rcv, pipe_send = Pipe(False)
  137. tx_proc = Process(target=target_if.traffic_gen, args=(res.group(2), pipe_rcv, ))
  138. tx_proc.start()
  139. dut.expect_exact('Ethernet stopped')
  140. pipe_send.send(0) # just send some dummy data
  141. tx_proc.join(5)
  142. if tx_proc.exitcode is None:
  143. tx_proc.terminate()
  144. dut.expect_unity_test_output(extra_before=res.group(1))
  145. @pytest.mark.esp32
  146. @pytest.mark.ethernet
  147. @pytest.mark.parametrize('config', [
  148. 'default_ip101',
  149. 'release_ip101',
  150. 'single_core_ip101'
  151. ], indirect=True)
  152. @pytest.mark.flaky(reruns=3, reruns_delay=5)
  153. def test_esp_ethernet(dut: Dut) -> None:
  154. ethernet_test(dut)
  155. @pytest.mark.esp32
  156. @pytest.mark.ethernet
  157. @pytest.mark.parametrize('config', [
  158. 'default_ip101',
  159. ], indirect=True)
  160. @pytest.mark.flaky(reruns=3, reruns_delay=5)
  161. def test_esp_emac_hal(dut: Dut) -> None:
  162. ethernet_int_emac_hal_test(dut)
  163. @pytest.mark.esp32
  164. @pytest.mark.ip101
  165. @pytest.mark.parametrize('config', [
  166. 'default_ip101',
  167. ], indirect=True)
  168. @pytest.mark.flaky(reruns=3, reruns_delay=5)
  169. def test_esp_eth_ip101(dut: Dut) -> None:
  170. ethernet_l2_test(dut)
  171. @pytest.mark.esp32
  172. @pytest.mark.lan8720
  173. @pytest.mark.parametrize('config', [
  174. 'default_lan8720',
  175. ], indirect=True)
  176. @pytest.mark.flaky(reruns=3, reruns_delay=5)
  177. def test_esp_eth_lan8720(dut: Dut) -> None:
  178. ethernet_l2_test(dut)