Skip to content

Commit 5355920

Browse files
jukkarnashif
authored andcommitted
tests: net: bridge: Update the bridging tests
Make sure that tests work after overhaul. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent 6986b1e commit 5355920

File tree

3 files changed

+55
-16
lines changed

3 files changed

+55
-16
lines changed

tests/net/bridge/prj.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ CONFIG_NET_PKT_RX_COUNT=20
1010
CONFIG_NET_BUF_RX_COUNT=20
1111
CONFIG_NET_BUF_TX_COUNT=20
1212
CONFIG_ZTEST=y
13+
CONFIG_NET_ETHERNET_BRIDGE_ETH_INTERFACE_COUNT=3
14+
CONFIG_NET_IF_MAX_IPV6_COUNT=4
15+
CONFIG_NET_IF_MAX_IPV4_COUNT=4

tests/net/bridge/src/main.c

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2021 BayLibre SAS
3+
* Copyright (c) 2024 Nordic Semiconductor
34
*
45
* SPDX-License-Identifier: Apache-2.0
56
*/
@@ -21,13 +22,17 @@ LOG_MODULE_REGISTER(net_test, NET_LOG_LEVEL);
2122
#include <zephyr/net/net_if.h>
2223
#include <zephyr/net/ethernet.h>
2324
#include <zephyr/net/ethernet_bridge.h>
25+
#include <zephyr/net/virtual.h>
26+
#include <zephyr/net/promiscuous.h>
2427

2528
#if NET_LOG_LEVEL >= LOG_LEVEL_DBG
2629
#define DBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
2730
#else
2831
#define DBG(fmt, ...)
2932
#endif
3033

34+
static struct net_if *bridge;
35+
3136
struct eth_fake_context {
3237
struct net_if *iface;
3338
struct net_pkt *sent_pkt;
@@ -173,6 +178,15 @@ static void iface_cb(struct net_if *iface, void *user_data)
173178
fake_iface[if_count++] = iface;
174179
}
175180
}
181+
182+
if (net_if_l2(iface) == &NET_L2_GET_NAME(VIRTUAL)) {
183+
enum virtual_interface_caps caps;
184+
185+
caps = net_virtual_get_iface_capabilities(iface);
186+
if (caps & VIRTUAL_INTERFACE_BRIDGE) {
187+
bridge = iface;
188+
}
189+
}
176190
}
177191

178192
static int orig_rx_num_blocks;
@@ -260,7 +274,7 @@ static void _recv_data(struct net_if *iface)
260274
ret = net_pkt_write(pkt, data, sizeof(data));
261275
zassert_equal(ret, 0, "");
262276

263-
DBG("Fake recv pkt %p\n", pkt);
277+
DBG("[%d] Fake recv pkt %p\n", net_if_get_by_iface(iface), pkt);
264278
ret = net_recv_data(iface, pkt);
265279
zassert_equal(ret, 0, "");
266280
}
@@ -284,24 +298,24 @@ static void test_recv_before_bridging(void)
284298
check_free_packet_count();
285299
}
286300

287-
static ETH_BRIDGE_INIT(test_bridge);
288-
289301
static void test_setup_bridge(void)
290302
{
291303
int ret;
292304

293305
/* add our interfaces to the bridge */
294-
ret = eth_bridge_iface_add(&test_bridge, fake_iface[0]);
306+
ret = eth_bridge_iface_add(bridge, fake_iface[0]);
295307
zassert_equal(ret, 0, "");
296-
ret = eth_bridge_iface_add(&test_bridge, fake_iface[1]);
308+
ret = eth_bridge_iface_add(bridge, fake_iface[1]);
297309
zassert_equal(ret, 0, "");
298-
ret = eth_bridge_iface_add(&test_bridge, fake_iface[2]);
310+
311+
/* Try to add the bridge twice, there should be no error */
312+
ret = eth_bridge_iface_add(bridge, fake_iface[1]);
299313
zassert_equal(ret, 0, "");
300314

301-
/* enable tx for them except fake_iface[1] */
302-
ret = eth_bridge_iface_allow_tx(fake_iface[0], true);
315+
ret = eth_bridge_iface_add(bridge, fake_iface[2]);
303316
zassert_equal(ret, 0, "");
304-
ret = eth_bridge_iface_allow_tx(fake_iface[2], true);
317+
318+
ret = net_if_up(bridge);
305319
zassert_equal(ret, 0, "");
306320
}
307321

@@ -318,16 +332,14 @@ static void test_recv_with_bridge(void)
318332
/* give time to the processing threads to run */
319333
k_sleep(K_MSEC(100));
320334

321-
/* nothing should have been transmitted on fake_iface[1] */
322-
zassert_is_null(eth_fake_data[1].sent_pkt, "");
323-
324335
/*
325336
* fake_iface[0] and fake_iface[2] should have sent the packet
326337
* but only if it didn't come from them.
327338
* We skip fake_iface[1] handled above.
328339
*/
329340
for (j = 0; j < 3; j += 2) {
330341
struct net_pkt *pkt = eth_fake_data[j].sent_pkt;
342+
struct net_eth_hdr *hdr;
331343

332344
if (eth_fake_data[j].iface == fake_iface[i]) {
333345
zassert_is_null(pkt, "");
@@ -338,7 +350,7 @@ static void test_recv_with_bridge(void)
338350
zassert_not_null(pkt, "");
339351

340352
/* make sure nothing messed up our ethernet header */
341-
struct net_eth_hdr *hdr = NET_ETH_HDR(pkt);
353+
hdr = NET_ETH_HDR(pkt);
342354

343355
zassert_equal(hdr->dst.addr[0], 0xb2, "");
344356
zassert_equal(hdr->src.addr[0], 0xa2, "");
@@ -356,24 +368,46 @@ static void test_recv_after_bridging(void)
356368
{
357369
int ret;
358370

371+
ret = net_if_down(bridge);
372+
zassert_equal(ret, 0, "");
373+
359374
/* remove our interfaces from the bridge */
360-
ret = eth_bridge_iface_remove(&test_bridge, fake_iface[0]);
375+
ret = eth_bridge_iface_remove(bridge, fake_iface[0]);
361376
zassert_equal(ret, 0, "");
362-
ret = eth_bridge_iface_remove(&test_bridge, fake_iface[1]);
377+
ret = eth_bridge_iface_remove(bridge, fake_iface[1]);
363378
zassert_equal(ret, 0, "");
364-
ret = eth_bridge_iface_remove(&test_bridge, fake_iface[2]);
379+
ret = eth_bridge_iface_remove(bridge, fake_iface[2]);
365380
zassert_equal(ret, 0, "");
366381

382+
/* If there are not enough interfaces in the bridge, it is not created */
383+
ret = net_if_up(bridge);
384+
zassert_equal(ret, -ENOENT, "");
385+
386+
eth_fake_data[0].sent_pkt = eth_fake_data[1].sent_pkt =
387+
eth_fake_data[2].sent_pkt = NULL;
388+
367389
/* things should have returned to the pre-bridging state */
368390
test_recv_before_bridging();
369391
}
370392

393+
/* Make sure bridge interface support promiscuous API */
394+
ZTEST(net_eth_bridge, test_verify_promisc_mode)
395+
{
396+
int ret;
397+
398+
ret = net_promisc_mode_on(bridge);
399+
zassert_equal(ret, 0, "");
400+
}
401+
371402
ZTEST(net_eth_bridge, test_net_eth_bridge)
372403
{
404+
DBG("Before bridging\n");
373405
test_iface_setup();
374406
test_recv_before_bridging();
407+
DBG("With bridging\n");
375408
test_setup_bridge();
376409
test_recv_with_bridge();
410+
DBG("After bridging\n");
377411
test_recv_after_bridging();
378412
}
379413

tests/net/bridge/testcase.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ tests:
99
extra_configs:
1010
- CONFIG_NET_IPV4=n
1111
- CONFIG_NET_IPV6=n
12+
- CONFIG_NET_CONFIG_NEED_IPV4=n
13+
- CONFIG_NET_CONFIG_NEED_IPV6=n
1214
platform_exclude:
1315
- mg100
1416
- pinnacle_100_dvk

0 commit comments

Comments
 (0)