pytest_esp_eth.py 4.7 KB

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