tests: net: bridge: Update the bridging tests

Make sure that tests work after overhaul.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
This commit is contained in:
Jukka Rissanen 2024-09-05 12:34:48 +03:00 committed by Anas Nashif
commit 5355920f98
3 changed files with 56 additions and 17 deletions

View file

@ -10,3 +10,6 @@ CONFIG_NET_PKT_RX_COUNT=20
CONFIG_NET_BUF_RX_COUNT=20
CONFIG_NET_BUF_TX_COUNT=20
CONFIG_ZTEST=y
CONFIG_NET_ETHERNET_BRIDGE_ETH_INTERFACE_COUNT=3
CONFIG_NET_IF_MAX_IPV6_COUNT=4
CONFIG_NET_IF_MAX_IPV4_COUNT=4

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2021 BayLibre SAS
* Copyright (c) 2024 Nordic Semiconductor
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -21,6 +22,8 @@ LOG_MODULE_REGISTER(net_test, NET_LOG_LEVEL);
#include <zephyr/net/net_if.h>
#include <zephyr/net/ethernet.h>
#include <zephyr/net/ethernet_bridge.h>
#include <zephyr/net/virtual.h>
#include <zephyr/net/promiscuous.h>
#if NET_LOG_LEVEL >= LOG_LEVEL_DBG
#define DBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
@ -28,6 +31,8 @@ LOG_MODULE_REGISTER(net_test, NET_LOG_LEVEL);
#define DBG(fmt, ...)
#endif
static struct net_if *bridge;
struct eth_fake_context {
struct net_if *iface;
struct net_pkt *sent_pkt;
@ -173,6 +178,15 @@ static void iface_cb(struct net_if *iface, void *user_data)
fake_iface[if_count++] = iface;
}
}
if (net_if_l2(iface) == &NET_L2_GET_NAME(VIRTUAL)) {
enum virtual_interface_caps caps;
caps = net_virtual_get_iface_capabilities(iface);
if (caps & VIRTUAL_INTERFACE_BRIDGE) {
bridge = iface;
}
}
}
static int orig_rx_num_blocks;
@ -260,7 +274,7 @@ static void _recv_data(struct net_if *iface)
ret = net_pkt_write(pkt, data, sizeof(data));
zassert_equal(ret, 0, "");
DBG("Fake recv pkt %p\n", pkt);
DBG("[%d] Fake recv pkt %p\n", net_if_get_by_iface(iface), pkt);
ret = net_recv_data(iface, pkt);
zassert_equal(ret, 0, "");
}
@ -284,24 +298,24 @@ static void test_recv_before_bridging(void)
check_free_packet_count();
}
static ETH_BRIDGE_INIT(test_bridge);
static void test_setup_bridge(void)
{
int ret;
/* add our interfaces to the bridge */
ret = eth_bridge_iface_add(&test_bridge, fake_iface[0]);
ret = eth_bridge_iface_add(bridge, fake_iface[0]);
zassert_equal(ret, 0, "");
ret = eth_bridge_iface_add(&test_bridge, fake_iface[1]);
zassert_equal(ret, 0, "");
ret = eth_bridge_iface_add(&test_bridge, fake_iface[2]);
ret = eth_bridge_iface_add(bridge, fake_iface[1]);
zassert_equal(ret, 0, "");
/* enable tx for them except fake_iface[1] */
ret = eth_bridge_iface_allow_tx(fake_iface[0], true);
/* Try to add the bridge twice, there should be no error */
ret = eth_bridge_iface_add(bridge, fake_iface[1]);
zassert_equal(ret, 0, "");
ret = eth_bridge_iface_allow_tx(fake_iface[2], true);
ret = eth_bridge_iface_add(bridge, fake_iface[2]);
zassert_equal(ret, 0, "");
ret = net_if_up(bridge);
zassert_equal(ret, 0, "");
}
@ -318,9 +332,6 @@ static void test_recv_with_bridge(void)
/* give time to the processing threads to run */
k_sleep(K_MSEC(100));
/* nothing should have been transmitted on fake_iface[1] */
zassert_is_null(eth_fake_data[1].sent_pkt, "");
/*
* fake_iface[0] and fake_iface[2] should have sent the packet
* but only if it didn't come from them.
@ -328,6 +339,7 @@ static void test_recv_with_bridge(void)
*/
for (j = 0; j < 3; j += 2) {
struct net_pkt *pkt = eth_fake_data[j].sent_pkt;
struct net_eth_hdr *hdr;
if (eth_fake_data[j].iface == fake_iface[i]) {
zassert_is_null(pkt, "");
@ -338,7 +350,7 @@ static void test_recv_with_bridge(void)
zassert_not_null(pkt, "");
/* make sure nothing messed up our ethernet header */
struct net_eth_hdr *hdr = NET_ETH_HDR(pkt);
hdr = NET_ETH_HDR(pkt);
zassert_equal(hdr->dst.addr[0], 0xb2, "");
zassert_equal(hdr->src.addr[0], 0xa2, "");
@ -356,24 +368,46 @@ static void test_recv_after_bridging(void)
{
int ret;
ret = net_if_down(bridge);
zassert_equal(ret, 0, "");
/* remove our interfaces from the bridge */
ret = eth_bridge_iface_remove(&test_bridge, fake_iface[0]);
ret = eth_bridge_iface_remove(bridge, fake_iface[0]);
zassert_equal(ret, 0, "");
ret = eth_bridge_iface_remove(&test_bridge, fake_iface[1]);
ret = eth_bridge_iface_remove(bridge, fake_iface[1]);
zassert_equal(ret, 0, "");
ret = eth_bridge_iface_remove(&test_bridge, fake_iface[2]);
ret = eth_bridge_iface_remove(bridge, fake_iface[2]);
zassert_equal(ret, 0, "");
/* If there are not enough interfaces in the bridge, it is not created */
ret = net_if_up(bridge);
zassert_equal(ret, -ENOENT, "");
eth_fake_data[0].sent_pkt = eth_fake_data[1].sent_pkt =
eth_fake_data[2].sent_pkt = NULL;
/* things should have returned to the pre-bridging state */
test_recv_before_bridging();
}
/* Make sure bridge interface support promiscuous API */
ZTEST(net_eth_bridge, test_verify_promisc_mode)
{
int ret;
ret = net_promisc_mode_on(bridge);
zassert_equal(ret, 0, "");
}
ZTEST(net_eth_bridge, test_net_eth_bridge)
{
DBG("Before bridging\n");
test_iface_setup();
test_recv_before_bridging();
DBG("With bridging\n");
test_setup_bridge();
test_recv_with_bridge();
DBG("After bridging\n");
test_recv_after_bridging();
}

View file

@ -9,6 +9,8 @@ tests:
extra_configs:
- CONFIG_NET_IPV4=n
- CONFIG_NET_IPV6=n
- CONFIG_NET_CONFIG_NEED_IPV4=n
- CONFIG_NET_CONFIG_NEED_IPV6=n
platform_exclude:
- mg100
- pinnacle_100_dvk