1
1
/*
2
2
* Copyright (c) 2021 BayLibre SAS
3
+ * Copyright (c) 2024 Nordic Semiconductor
3
4
*
4
5
* SPDX-License-Identifier: Apache-2.0
5
6
*/
@@ -21,13 +22,17 @@ LOG_MODULE_REGISTER(net_test, NET_LOG_LEVEL);
21
22
#include <zephyr/net/net_if.h>
22
23
#include <zephyr/net/ethernet.h>
23
24
#include <zephyr/net/ethernet_bridge.h>
25
+ #include <zephyr/net/virtual.h>
26
+ #include <zephyr/net/promiscuous.h>
24
27
25
28
#if NET_LOG_LEVEL >= LOG_LEVEL_DBG
26
29
#define DBG (fmt , ...) printk(fmt, ##__VA_ARGS__)
27
30
#else
28
31
#define DBG (fmt , ...)
29
32
#endif
30
33
34
+ static struct net_if * bridge ;
35
+
31
36
struct eth_fake_context {
32
37
struct net_if * iface ;
33
38
struct net_pkt * sent_pkt ;
@@ -173,6 +178,15 @@ static void iface_cb(struct net_if *iface, void *user_data)
173
178
fake_iface [if_count ++ ] = iface ;
174
179
}
175
180
}
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
+ }
176
190
}
177
191
178
192
static int orig_rx_num_blocks ;
@@ -260,7 +274,7 @@ static void _recv_data(struct net_if *iface)
260
274
ret = net_pkt_write (pkt , data , sizeof (data ));
261
275
zassert_equal (ret , 0 , "" );
262
276
263
- DBG ("Fake recv pkt %p\n" , pkt );
277
+ DBG ("[%d] Fake recv pkt %p\n" , net_if_get_by_iface ( iface ) , pkt );
264
278
ret = net_recv_data (iface , pkt );
265
279
zassert_equal (ret , 0 , "" );
266
280
}
@@ -284,24 +298,24 @@ static void test_recv_before_bridging(void)
284
298
check_free_packet_count ();
285
299
}
286
300
287
- static ETH_BRIDGE_INIT (test_bridge );
288
-
289
301
static void test_setup_bridge (void )
290
302
{
291
303
int ret ;
292
304
293
305
/* 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 ]);
295
307
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 ]);
297
309
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 ]);
299
313
zassert_equal (ret , 0 , "" );
300
314
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 ]);
303
316
zassert_equal (ret , 0 , "" );
304
- ret = eth_bridge_iface_allow_tx (fake_iface [2 ], true);
317
+
318
+ ret = net_if_up (bridge );
305
319
zassert_equal (ret , 0 , "" );
306
320
}
307
321
@@ -318,16 +332,14 @@ static void test_recv_with_bridge(void)
318
332
/* give time to the processing threads to run */
319
333
k_sleep (K_MSEC (100 ));
320
334
321
- /* nothing should have been transmitted on fake_iface[1] */
322
- zassert_is_null (eth_fake_data [1 ].sent_pkt , "" );
323
-
324
335
/*
325
336
* fake_iface[0] and fake_iface[2] should have sent the packet
326
337
* but only if it didn't come from them.
327
338
* We skip fake_iface[1] handled above.
328
339
*/
329
340
for (j = 0 ; j < 3 ; j += 2 ) {
330
341
struct net_pkt * pkt = eth_fake_data [j ].sent_pkt ;
342
+ struct net_eth_hdr * hdr ;
331
343
332
344
if (eth_fake_data [j ].iface == fake_iface [i ]) {
333
345
zassert_is_null (pkt , "" );
@@ -338,7 +350,7 @@ static void test_recv_with_bridge(void)
338
350
zassert_not_null (pkt , "" );
339
351
340
352
/* make sure nothing messed up our ethernet header */
341
- struct net_eth_hdr * hdr = NET_ETH_HDR (pkt );
353
+ hdr = NET_ETH_HDR (pkt );
342
354
343
355
zassert_equal (hdr -> dst .addr [0 ], 0xb2 , "" );
344
356
zassert_equal (hdr -> src .addr [0 ], 0xa2 , "" );
@@ -356,24 +368,46 @@ static void test_recv_after_bridging(void)
356
368
{
357
369
int ret ;
358
370
371
+ ret = net_if_down (bridge );
372
+ zassert_equal (ret , 0 , "" );
373
+
359
374
/* 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 ]);
361
376
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 ]);
363
378
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 ]);
365
380
zassert_equal (ret , 0 , "" );
366
381
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
+
367
389
/* things should have returned to the pre-bridging state */
368
390
test_recv_before_bridging ();
369
391
}
370
392
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
+
371
402
ZTEST (net_eth_bridge , test_net_eth_bridge )
372
403
{
404
+ DBG ("Before bridging\n" );
373
405
test_iface_setup ();
374
406
test_recv_before_bridging ();
407
+ DBG ("With bridging\n" );
375
408
test_setup_bridge ();
376
409
test_recv_with_bridge ();
410
+ DBG ("After bridging\n" );
377
411
test_recv_after_bridging ();
378
412
}
379
413
0 commit comments