2023-01-30 03:29:58 -05:00
|
|
|
# SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
|
2022-01-18 23:12:15 -05:00
|
|
|
# SPDX-License-Identifier: CC0-1.0
|
|
|
|
|
|
|
|
import contextlib
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import socket
|
2022-03-08 09:51:17 -05:00
|
|
|
from multiprocessing import Pipe, Process, connection
|
2022-01-18 23:12:15 -05:00
|
|
|
from typing import Iterator
|
|
|
|
|
|
|
|
import pytest
|
2023-10-31 15:27:30 -04:00
|
|
|
from pytest_embedded_idf import IdfDut
|
2022-01-18 07:55:06 -05:00
|
|
|
from scapy.all import Ether, raw
|
2022-01-18 23:12:15 -05:00
|
|
|
|
2023-03-31 10:23:15 -04:00
|
|
|
ETH_TYPE = 0x3300
|
2022-03-31 08:51:40 -04:00
|
|
|
|
2022-01-18 23:12:15 -05:00
|
|
|
|
2022-07-18 04:57:03 -04:00
|
|
|
class EthTestIntf(object):
|
2022-11-08 11:54:39 -05:00
|
|
|
def __init__(self, eth_type: int, my_if: str = ''):
|
2022-07-18 04:57:03 -04:00
|
|
|
self.target_if = ''
|
|
|
|
self.eth_type = eth_type
|
2022-11-08 11:54:39 -05:00
|
|
|
self.find_target_if(my_if)
|
2022-07-18 04:57:03 -04:00
|
|
|
|
2022-11-08 11:54:39 -05:00
|
|
|
def find_target_if(self, my_if: str = '') -> None:
|
2022-07-18 04:57:03 -04:00
|
|
|
# try to determine which interface to use
|
|
|
|
netifs = os.listdir('/sys/class/net/')
|
2023-10-31 15:27:30 -04:00
|
|
|
netifs.sort(reverse=True)
|
2022-07-18 04:57:03 -04:00
|
|
|
logging.info('detected interfaces: %s', str(netifs))
|
|
|
|
|
|
|
|
for netif in netifs:
|
2022-11-08 11:54:39 -05:00
|
|
|
# if no interface defined, try to find it automatically
|
|
|
|
if my_if == '':
|
|
|
|
if netif.find('eth') == 0 or netif.find('enp') == 0 or netif.find('eno') == 0:
|
|
|
|
self.target_if = netif
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
if netif.find(my_if) == 0:
|
|
|
|
self.target_if = my_if
|
|
|
|
break
|
2022-07-18 04:57:03 -04:00
|
|
|
if self.target_if == '':
|
2023-03-31 10:23:15 -04:00
|
|
|
raise RuntimeError('network interface not found')
|
2022-07-18 04:57:03 -04:00
|
|
|
logging.info('Use %s for testing', self.target_if)
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
2023-01-30 03:29:58 -05:00
|
|
|
def configure_eth_if(self, eth_type:int=0) -> Iterator[socket.socket]:
|
|
|
|
if eth_type == 0:
|
|
|
|
eth_type = self.eth_type
|
|
|
|
so = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(eth_type))
|
2022-07-18 04:57:03 -04:00
|
|
|
so.bind((self.target_if, 0))
|
2022-01-18 07:55:06 -05:00
|
|
|
try:
|
2022-07-18 04:57:03 -04:00
|
|
|
yield so
|
|
|
|
finally:
|
|
|
|
so.close()
|
|
|
|
|
|
|
|
def send_eth_packet(self, mac: str) -> None:
|
|
|
|
with self.configure_eth_if() as so:
|
|
|
|
so.settimeout(10)
|
|
|
|
payload = bytearray(1010)
|
|
|
|
for i, _ in enumerate(payload):
|
|
|
|
payload[i] = i & 0xff
|
|
|
|
eth_frame = Ether(dst=mac, src=so.getsockname()[4], type=self.eth_type) / raw(payload)
|
|
|
|
try:
|
2022-01-18 07:55:06 -05:00
|
|
|
so.send(raw(eth_frame))
|
2022-07-18 04:57:03 -04:00
|
|
|
except Exception as e:
|
|
|
|
raise e
|
|
|
|
|
2023-03-31 10:23:15 -04:00
|
|
|
def recv_resp_poke(self, mac:str, i:int=0) -> None:
|
2023-01-30 03:29:58 -05:00
|
|
|
eth_type_ctrl = self.eth_type + 1
|
|
|
|
with self.configure_eth_if(eth_type_ctrl) as so:
|
|
|
|
so.settimeout(30)
|
2023-03-31 10:23:15 -04:00
|
|
|
for _ in range(10):
|
|
|
|
try:
|
|
|
|
eth_frame = Ether(so.recv(60))
|
|
|
|
except Exception as e:
|
|
|
|
raise e
|
|
|
|
if mac == eth_frame.src and eth_frame.load[0] == 0xfa:
|
2022-07-18 04:57:03 -04:00
|
|
|
if eth_frame.load[1] != i:
|
2023-03-31 10:23:15 -04:00
|
|
|
raise RuntimeError('Missed Poke Packet')
|
2023-01-30 03:29:58 -05:00
|
|
|
logging.info('Poke Packet received...')
|
2022-07-18 04:57:03 -04:00
|
|
|
eth_frame.dst = eth_frame.src
|
|
|
|
eth_frame.src = so.getsockname()[4]
|
|
|
|
eth_frame.load = bytes.fromhex('fb') # POKE_RESP code
|
|
|
|
so.send(raw(eth_frame))
|
2023-03-31 10:23:15 -04:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
logging.warning('Unexpected Control packet')
|
|
|
|
logging.warning('Expected Ctrl command: 0xfa, actual: 0x%x', eth_frame.load[0])
|
|
|
|
logging.warning('Source MAC %s', eth_frame.src)
|
|
|
|
else:
|
|
|
|
raise RuntimeError('No Poke Packet!')
|
2022-07-18 04:57:03 -04:00
|
|
|
|
|
|
|
def traffic_gen(self, mac: str, pipe_rcv:connection.Connection) -> None:
|
|
|
|
with self.configure_eth_if() as so:
|
|
|
|
payload = bytes.fromhex('ff') # DUMMY_TRAFFIC code
|
|
|
|
payload += bytes(1485)
|
|
|
|
eth_frame = Ether(dst=mac, src=so.getsockname()[4], type=self.eth_type) / raw(payload)
|
|
|
|
try:
|
|
|
|
while pipe_rcv.poll() is not True:
|
|
|
|
so.send(raw(eth_frame))
|
|
|
|
except Exception as e:
|
|
|
|
raise e
|
2022-01-18 23:12:15 -05:00
|
|
|
|
|
|
|
|
2023-10-31 15:27:30 -04:00
|
|
|
def ethernet_test(dut: IdfDut) -> None:
|
|
|
|
dut.run_all_single_board_cases(group='ethernet', timeout=980)
|
2022-11-08 11:54:39 -05:00
|
|
|
|
|
|
|
|
2023-10-31 15:27:30 -04:00
|
|
|
def ethernet_int_emac_hal_test(dut: IdfDut) -> None:
|
|
|
|
dut.run_all_single_board_cases(group='emac_hal')
|
2022-07-18 04:57:03 -04:00
|
|
|
|
2022-01-18 23:12:15 -05:00
|
|
|
|
2023-10-31 15:27:30 -04:00
|
|
|
def ethernet_l2_test(dut: IdfDut) -> None:
|
2022-11-08 11:54:39 -05:00
|
|
|
target_if = EthTestIntf(ETH_TYPE)
|
|
|
|
|
|
|
|
dut.expect_exact('Press ENTER to see the list of tests')
|
|
|
|
dut.write('\n')
|
|
|
|
dut.expect_exact('Enter test for running.')
|
|
|
|
|
2022-07-18 04:57:03 -04:00
|
|
|
with target_if.configure_eth_if() as so:
|
2022-01-18 23:12:15 -05:00
|
|
|
so.settimeout(30)
|
2022-11-08 11:54:39 -05:00
|
|
|
dut.write('"ethernet broadcast transmit"')
|
2023-03-31 10:23:15 -04:00
|
|
|
res = dut.expect(
|
|
|
|
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})'
|
|
|
|
)
|
2023-01-30 03:29:58 -05:00
|
|
|
|
|
|
|
# wait for POKE msg to be sure the switch already started forwarding the port's traffic
|
|
|
|
# (there might be slight delay due to the RSTP execution)
|
2023-03-31 10:23:15 -04:00
|
|
|
dut_mac = res.group(1).decode('utf-8')
|
|
|
|
target_if.recv_resp_poke(mac=dut_mac)
|
|
|
|
|
|
|
|
for _ in range(10):
|
|
|
|
eth_frame = Ether(so.recv(1024))
|
|
|
|
if dut_mac == eth_frame.src:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise RuntimeError('No broadcast received from expected DUT MAC addr')
|
2023-01-30 03:29:58 -05:00
|
|
|
|
2022-01-18 07:55:06 -05:00
|
|
|
for i in range(0, 1010):
|
|
|
|
if eth_frame.load[i] != i & 0xff:
|
2023-03-31 10:23:15 -04:00
|
|
|
raise RuntimeError('Packet content mismatch')
|
2022-01-18 23:12:15 -05:00
|
|
|
dut.expect_unity_test_output()
|
|
|
|
|
|
|
|
dut.expect_exact("Enter next test, or 'enter' to see menu")
|
2022-11-08 11:54:39 -05:00
|
|
|
dut.write('"ethernet recv_pkt"')
|
2022-01-18 23:12:15 -05:00
|
|
|
res = dut.expect(
|
|
|
|
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})'
|
|
|
|
)
|
2023-03-31 10:23:15 -04:00
|
|
|
dut_mac = res.group(1).decode('utf-8')
|
2023-01-30 03:29:58 -05:00
|
|
|
# wait for POKE msg to be sure the switch already started forwarding the port's traffic
|
|
|
|
# (there might be slight delay due to the RSTP execution)
|
2023-03-31 10:23:15 -04:00
|
|
|
target_if.recv_resp_poke(mac=dut_mac)
|
2022-07-18 04:57:03 -04:00
|
|
|
target_if.send_eth_packet('ff:ff:ff:ff:ff:ff') # broadcast frame
|
2023-10-31 15:27:30 -04:00
|
|
|
target_if.send_eth_packet('01:00:5e:00:00:00') # IPv4 multicast frame (some SPI Eth modules filter multicast other than IP)
|
2023-03-31 10:23:15 -04:00
|
|
|
target_if.send_eth_packet(mac=dut_mac) # unicast frame
|
2022-01-18 23:12:15 -05:00
|
|
|
dut.expect_unity_test_output(extra_before=res.group(1))
|
|
|
|
|
2022-01-18 07:55:06 -05:00
|
|
|
dut.expect_exact("Enter next test, or 'enter' to see menu")
|
2022-11-08 11:54:39 -05:00
|
|
|
dut.write('"ethernet start/stop stress test under heavy traffic"')
|
2022-01-18 07:55:06 -05:00
|
|
|
res = dut.expect(
|
|
|
|
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})'
|
|
|
|
)
|
2023-03-31 10:23:15 -04:00
|
|
|
dut_mac = res.group(1).decode('utf-8')
|
2022-01-18 07:55:06 -05:00
|
|
|
# Start/stop under heavy Tx traffic
|
|
|
|
for tx_i in range(10):
|
2023-03-31 10:23:15 -04:00
|
|
|
target_if.recv_resp_poke(dut_mac, tx_i)
|
2023-01-30 03:29:58 -05:00
|
|
|
dut.expect_exact('Ethernet stopped')
|
|
|
|
|
|
|
|
for rx_i in range(10):
|
2023-03-31 10:23:15 -04:00
|
|
|
target_if.recv_resp_poke(dut_mac, rx_i)
|
2023-01-30 03:29:58 -05:00
|
|
|
# Start/stop under heavy Rx traffic
|
|
|
|
pipe_rcv, pipe_send = Pipe(False)
|
2023-03-31 10:23:15 -04:00
|
|
|
tx_proc = Process(target=target_if.traffic_gen, args=(dut_mac, pipe_rcv, ))
|
2023-01-30 03:29:58 -05:00
|
|
|
tx_proc.start()
|
|
|
|
dut.expect_exact('Ethernet stopped')
|
2022-07-18 04:57:03 -04:00
|
|
|
pipe_send.send(0) # just send some dummy data
|
2022-06-23 11:23:58 -04:00
|
|
|
tx_proc.join(5)
|
|
|
|
if tx_proc.exitcode is None:
|
|
|
|
tx_proc.terminate()
|
2023-01-30 03:29:58 -05:00
|
|
|
|
2022-07-18 04:57:03 -04:00
|
|
|
dut.expect_unity_test_output(extra_before=res.group(1))
|
2022-01-18 07:55:06 -05:00
|
|
|
|
2022-01-18 23:12:15 -05:00
|
|
|
|
2023-10-31 15:27:30 -04:00
|
|
|
# ----------- IP101 -----------
|
2022-11-08 11:54:39 -05:00
|
|
|
@pytest.mark.esp32
|
|
|
|
@pytest.mark.ethernet
|
|
|
|
@pytest.mark.parametrize('config', [
|
|
|
|
'default_ip101',
|
|
|
|
'release_ip101',
|
|
|
|
'single_core_ip101'
|
|
|
|
], indirect=True)
|
|
|
|
@pytest.mark.flaky(reruns=3, reruns_delay=5)
|
2023-10-31 15:27:30 -04:00
|
|
|
def test_esp_ethernet(dut: IdfDut) -> None:
|
2022-11-08 11:54:39 -05:00
|
|
|
ethernet_test(dut)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.esp32
|
|
|
|
@pytest.mark.ethernet
|
|
|
|
@pytest.mark.parametrize('config', [
|
|
|
|
'default_ip101',
|
|
|
|
], indirect=True)
|
2023-10-31 15:27:30 -04:00
|
|
|
def test_esp_emac_hal(dut: IdfDut) -> None:
|
2022-11-08 11:54:39 -05:00
|
|
|
ethernet_int_emac_hal_test(dut)
|
|
|
|
|
|
|
|
|
2022-01-18 23:12:15 -05:00
|
|
|
@pytest.mark.esp32
|
|
|
|
@pytest.mark.ip101
|
|
|
|
@pytest.mark.parametrize('config', [
|
2022-11-08 11:54:39 -05:00
|
|
|
'default_ip101',
|
2022-01-18 23:12:15 -05:00
|
|
|
], indirect=True)
|
2023-10-31 15:27:30 -04:00
|
|
|
def test_esp_eth_ip101(dut: IdfDut) -> None:
|
2022-11-08 11:54:39 -05:00
|
|
|
ethernet_l2_test(dut)
|
2022-01-18 23:12:15 -05:00
|
|
|
|
|
|
|
|
2023-10-31 15:27:30 -04:00
|
|
|
# ----------- LAN8720 -----------
|
2022-01-18 23:12:15 -05:00
|
|
|
@pytest.mark.esp32
|
2023-10-31 15:27:30 -04:00
|
|
|
@pytest.mark.eth_lan8720
|
2022-01-18 23:12:15 -05:00
|
|
|
@pytest.mark.parametrize('config', [
|
2022-11-08 11:54:39 -05:00
|
|
|
'default_lan8720',
|
2022-01-18 23:12:15 -05:00
|
|
|
], indirect=True)
|
2023-10-31 15:27:30 -04:00
|
|
|
def test_esp_eth_lan8720(dut: IdfDut) -> None:
|
|
|
|
ethernet_test(dut)
|
|
|
|
dut.serial.hard_reset()
|
|
|
|
ethernet_l2_test(dut)
|
|
|
|
|
|
|
|
|
|
|
|
# ----------- RTL8201 -----------
|
|
|
|
@pytest.mark.esp32
|
|
|
|
@pytest.mark.eth_rtl8201
|
|
|
|
@pytest.mark.parametrize('config', [
|
|
|
|
'default_rtl8201',
|
|
|
|
], indirect=True)
|
|
|
|
def test_esp_eth_rtl8201(dut: IdfDut) -> None:
|
|
|
|
ethernet_test(dut)
|
|
|
|
dut.serial.hard_reset()
|
|
|
|
ethernet_l2_test(dut)
|
|
|
|
|
|
|
|
|
|
|
|
# ----------- KSZ8041 -----------
|
|
|
|
@pytest.mark.esp32
|
|
|
|
@pytest.mark.eth_ksz8041
|
|
|
|
@pytest.mark.parametrize('config', [
|
|
|
|
'default_ksz8041',
|
|
|
|
], indirect=True)
|
|
|
|
def test_esp_eth_ksz8041(dut: IdfDut) -> None:
|
|
|
|
ethernet_test(dut)
|
|
|
|
dut.serial.hard_reset()
|
|
|
|
ethernet_l2_test(dut)
|
|
|
|
|
|
|
|
|
|
|
|
# ----------- DP83848 -----------
|
|
|
|
@pytest.mark.esp32
|
|
|
|
@pytest.mark.eth_dp83848
|
|
|
|
@pytest.mark.parametrize('config', [
|
|
|
|
'default_dp83848',
|
|
|
|
], indirect=True)
|
|
|
|
def test_esp_eth_dp83848(dut: IdfDut) -> None:
|
|
|
|
ethernet_test(dut)
|
|
|
|
dut.serial.hard_reset()
|
|
|
|
ethernet_l2_test(dut)
|
|
|
|
|
|
|
|
|
|
|
|
# ----------- W5500 -----------
|
|
|
|
@pytest.mark.esp32
|
|
|
|
@pytest.mark.eth_w5500
|
|
|
|
@pytest.mark.parametrize('config', [
|
|
|
|
'default_w5500',
|
|
|
|
], indirect=True)
|
|
|
|
def test_esp_eth_w5500(dut: IdfDut) -> None:
|
|
|
|
ethernet_test(dut)
|
|
|
|
dut.serial.hard_reset()
|
|
|
|
ethernet_l2_test(dut)
|
|
|
|
|
|
|
|
|
|
|
|
# ----------- KSZ8851SNL -----------
|
|
|
|
@pytest.mark.esp32
|
|
|
|
@pytest.mark.eth_ksz8851snl
|
|
|
|
@pytest.mark.parametrize('config', [
|
|
|
|
'default_ksz8851snl',
|
|
|
|
], indirect=True)
|
|
|
|
def test_esp_eth_ksz8851snl(dut: IdfDut) -> None:
|
|
|
|
ethernet_test(dut)
|
|
|
|
dut.serial.hard_reset()
|
|
|
|
ethernet_l2_test(dut)
|
|
|
|
|
|
|
|
|
|
|
|
# ----------- DM9051 -----------
|
|
|
|
@pytest.mark.esp32
|
|
|
|
@pytest.mark.eth_dm9051
|
|
|
|
@pytest.mark.parametrize('config', [
|
|
|
|
'default_dm9051',
|
|
|
|
], indirect=True)
|
|
|
|
def test_esp_eth_dm9051(dut: IdfDut) -> None:
|
|
|
|
ethernet_test(dut)
|
|
|
|
dut.serial.hard_reset()
|
2022-11-08 11:54:39 -05:00
|
|
|
ethernet_l2_test(dut)
|