test: net: offloaded_netdev: Add tests for IPv4/6 address registration

Make sure it's possible to register IPv4/6 addresses on an offloaded
interface. Add an extra build configuration to make sure it's also
possible when native IP stack is disabled.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
This commit is contained in:
Robert Lubos 2024-09-19 13:48:34 +02:00 committed by Fabio Baltieri
commit 4595295028
3 changed files with 76 additions and 5 deletions

View file

@ -7,3 +7,9 @@ CONFIG_ZTEST=y
CONFIG_TEST_USERSPACE=y
CONFIG_NET_OFFLOAD=y
CONFIG_TEST_RANDOM_GENERATOR=y
CONFIG_NET_IPV4=y
CONFIG_NET_IPV6=y
CONFIG_NET_IPV6_NBR_CACHE=n
CONFIG_NET_IPV6_MLD=n
CONFIG_NET_IF_MAX_IPV4_COUNT=4
CONFIG_NET_IF_MAX_IPV6_COUNT=4

View file

@ -19,6 +19,11 @@
#include <zephyr/net/net_if.h>
#include <zephyr/net/net_l2.h>
static struct in_addr test_addr_ipv4 = { { { 192, 0, 2, 1 } } };
static struct in6_addr test_addr_ipv6 = { { {
0x20, 0x01, 0x0d, 0xb8, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x1
} } };
/* Dummy socket creator for socket-offloaded ifaces */
int offload_socket(int family, int type, int proto)
{
@ -36,6 +41,8 @@ static void sock_offload_l2_iface_init(struct net_if *iface)
*/
net_if_socket_offload_set(iface, offload_socket);
net_if_flag_set(iface, NET_IF_NO_AUTO_START);
net_if_flag_set(iface, NET_IF_IPV4);
net_if_flag_set(iface, NET_IF_IPV6);
}
/* Dummy init function for net-offloaded ifaces */
@ -46,6 +53,8 @@ static void net_offload_l2_iface_init(struct net_if *iface)
*/
iface->if_dev->offload = &net_offload_api;
net_if_flag_set(iface, NET_IF_NO_AUTO_START);
net_if_flag_set(iface, NET_IF_IPV4);
net_if_flag_set(iface, NET_IF_IPV6);
}
/* Tracks the total number of ifaces that are up (theoretically). */
@ -327,5 +336,58 @@ ZTEST(net_offloaded_netdev, test_up_down_sock_off_impl_fail_down)
"Iface under test should have failed to go up");
}
static void test_addr_add_common(struct net_if *test_iface, const char *off_type)
{
struct net_if *lookup_iface;
struct net_if_addr *ipv4_addr;
struct net_if_addr *ipv6_addr;
/* Bring iface up before test */
(void)net_if_up(test_iface);
ipv4_addr = net_if_ipv4_addr_add(test_iface, &test_addr_ipv4,
NET_ADDR_MANUAL, 0);
zassert_not_null(ipv4_addr,
"Failed to add IPv4 address to a %s offloaded interface",
off_type);
ipv6_addr = net_if_ipv6_addr_add(test_iface, &test_addr_ipv6,
NET_ADDR_MANUAL, 0);
zassert_not_null(ipv6_addr,
"Failed to add IPv6 address to a socket %s interface",
off_type);
lookup_iface = NULL;
zassert_equal_ptr(net_if_ipv4_addr_lookup(&test_addr_ipv4, &lookup_iface),
ipv4_addr,
"Failed to find IPv4 address on a %s offloaded interface");
zassert_equal_ptr(lookup_iface, test_iface, "Wrong interface");
lookup_iface = NULL;
zassert_equal_ptr(net_if_ipv6_addr_lookup(&test_addr_ipv6, &lookup_iface),
ipv6_addr,
"Failed to find IPv6 address on a %s offloaded interface");
zassert_equal_ptr(lookup_iface, test_iface, "Wrong interface");
zassert_true(net_if_ipv4_addr_rm(test_iface, &test_addr_ipv4),
"Failed to remove IPv4 address from a %s offloaded interface",
off_type);
zassert_true(net_if_ipv6_addr_rm(test_iface, &test_addr_ipv6),
"Failed to remove IPv4 address from a %s offloaded interface",
off_type);
}
ZTEST(net_offloaded_netdev, test_addr_add_sock_off_impl)
{
struct net_if *test_iface = NET_IF_GET(sock_offload_test_impl, 0);
test_addr_add_common(test_iface, "offloaded");
}
ZTEST(net_offloaded_netdev, test_addr_add_net_off_impl)
{
struct net_if *test_iface = NET_IF_GET(net_offload_test_impl, 0);
test_addr_add_common(test_iface, "net");
}
ZTEST_SUITE(net_offloaded_netdev, NULL, NULL, net_offloaded_netdev_before, NULL, NULL);

View file

@ -1,9 +1,12 @@
common:
min_ram: 16
depends_on: netif
tags:
- net
- iface
- userspace
tests:
net.offloaded_netdev:
tags:
- net
- iface
- userspace
net.offloaded_netdev: {}
net.offloaded_netdev.no_native:
extra_configs:
- CONFIG_NET_NATIVE=n