Skip to content

Commit

Permalink
net: eth: Add native-sim support to native-posix Ethernet driver
Browse files Browse the repository at this point in the history
This will enable Ethernet driver to be used when compiling
for native-sim board.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
  • Loading branch information
jukkar committed Oct 3, 2023
1 parent c6c9700 commit 2627559
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 51 deletions.
9 changes: 7 additions & 2 deletions drivers/ethernet/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,13 @@ if(CONFIG_ETH_NATIVE_POSIX)
set_source_files_properties(${native_posix_source_files}
PROPERTIES COMPILE_DEFINITIONS
"NO_POSIX_CHEATS;_BSD_SOURCE;_DEFAULT_SOURCE"
)
zephyr_library_sources(${native_posix_source_files})
)
if (CONFIG_NATIVE_APPLICATION)
zephyr_library_sources(${native_posix_source_files})
else()
zephyr_library_sources(eth_native_posix.c)
target_sources(native_simulator INTERFACE eth_native_posix_adapt.c)
endif()
endif()

add_subdirectory(phy)
2 changes: 1 addition & 1 deletion drivers/ethernet/Kconfig.native_posix
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

menuconfig ETH_NATIVE_POSIX
bool "Native Posix Ethernet driver"
depends on ARCH_POSIX && EXTERNAL_LIBC
depends on ARCH_POSIX && (BOARD_NATIVE_POSIX || BOARD_NATIVE_SIM)
help
Enable native posix ethernet driver. Note, this driver is run inside
a process in your host system.
Expand Down
8 changes: 4 additions & 4 deletions drivers/ethernet/eth_native_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ static void update_gptp(struct net_if *iface, struct net_pkt *pkt,
struct gptp_hdr *hdr;
int ret;

ret = eth_clock_gettime(&timestamp);
ret = eth_clock_gettime(&timestamp.second, &timestamp.nanosecond);
if (ret < 0) {
return;
}
Expand Down Expand Up @@ -460,15 +460,15 @@ static void eth_iface_init(struct net_if *iface)
* change the documentation etc. and break things.
*/
if (CONFIG_ETH_NATIVE_POSIX_INTERFACE_COUNT == 1) {
ctx->if_name = ETH_NATIVE_POSIX_DRV_NAME;
ctx->if_name = CONFIG_ETH_NATIVE_POSIX_DRV_NAME;
}

LOG_DBG("Interface %p using \"%s\"", iface, ctx->if_name);

net_if_set_link_addr(iface, ll_addr->addr, ll_addr->len,
NET_LINK_ETHERNET);

ctx->dev_fd = eth_iface_create(ctx->if_name, false);
ctx->dev_fd = eth_iface_create(CONFIG_ETH_NATIVE_POSIX_DEV_NAME, ctx->if_name, false);
if (ctx->dev_fd < 0) {
LOG_ERR("Cannot create %s (%d)", ctx->if_name, -errno);
} else {
Expand Down Expand Up @@ -671,7 +671,7 @@ static int ptp_clock_get_native_posix(const struct device *clk,
{
ARG_UNUSED(clk);

return eth_clock_gettime(tm);
return eth_clock_gettime(&tm->second, &tm->nanosecond);
}

static int ptp_clock_adjust_native_posix(const struct device *clk,
Expand Down
36 changes: 11 additions & 25 deletions drivers/ethernet/eth_native_posix_adapt.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,29 @@
#include <sys/select.h>
#include <net/if.h>
#include <time.h>
#include <zephyr/arch/posix/posix_trace.h>
#include <inttypes.h>
#include <nsi_tracing.h>

#ifdef __linux
#include <linux/if.h>
#include <linux/if_tun.h>
#endif

/* Zephyr include files. Be very careful here and only include minimum
* things needed.
*/
#define LOG_MODULE_NAME eth_posix_adapt
#define LOG_LEVEL CONFIG_ETHERNET_LOG_LEVEL

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(LOG_MODULE_NAME);

#include <zephyr/types.h>
#include <zephyr/sys_clock.h>

#if defined(CONFIG_NET_GPTP)
#include <zephyr/net/gptp.h>
#if !defined(IS_ENABLED)
#define IS_ENABLED(cond) 0
#endif

#include "eth_native_posix_priv.h"

/* Note that we cannot create the TUN/TAP device from the setup script
* as we need to get a file descriptor to communicate with the interface.
*/
int eth_iface_create(const char *if_name, bool tun_only)
int eth_iface_create(const char *dev_name, const char *if_name, bool tun_only)
{
struct ifreq ifr;
int fd, ret = -EINVAL;

fd = open(ETH_NATIVE_POSIX_DEV_NAME, O_RDWR);
fd = open(dev_name, O_RDWR);
if (fd < 0) {
return -errno;
}
Expand Down Expand Up @@ -98,7 +88,7 @@ static int ssystem(const char *fmt, ...)
vsnprintf(cmd, sizeof(cmd), fmt, ap);
va_end(ap);

posix_print_trace("%s\n", cmd);
nsi_print_trace("%s\n", cmd);

ret = system(cmd);

Expand Down Expand Up @@ -175,8 +165,7 @@ ssize_t eth_write_data(int fd, void *buf, size_t buf_len)
return write(fd, buf, buf_len);
}

#if defined(CONFIG_NET_GPTP)
int eth_clock_gettime(struct net_ptp_time *time)
int eth_clock_gettime(uint64_t *second, uint32_t *nanosecond)
{
struct timespec tp;
int ret;
Expand All @@ -186,14 +175,12 @@ int eth_clock_gettime(struct net_ptp_time *time)
return -errno;
}

time->second = tp.tv_sec;
time->nanosecond = tp.tv_nsec;
*second = (uint64_t)tp.tv_sec;
*nanosecond = (uint32_t)tp.tv_nsec;

return 0;
}
#endif /* CONFIG_NET_GPTP */

#if defined(CONFIG_NET_PROMISCUOUS_MODE)
int eth_promisc_mode(const char *if_name, bool enable)
{
if (!IS_ENABLED(CONFIG_ETH_NATIVE_POSIX_STARTUP_AUTOMATIC)) {
Expand All @@ -203,7 +190,6 @@ int eth_promisc_mode(const char *if_name, bool enable)
return ssystem("ip link set dev %s promisc %s",
if_name, enable ? "on" : "off");
}
#endif /* CONFIG_NET_PROMISCUOUS_MODE */

/* If we have enabled manual setup, then interface cannot be
* taken up or down by the driver as we normally do not have
Expand Down
21 changes: 2 additions & 19 deletions drivers/ethernet/eth_native_posix_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
#ifndef ZEPHYR_DRIVERS_ETHERNET_ETH_NATIVE_POSIX_PRIV_H_
#define ZEPHYR_DRIVERS_ETHERNET_ETH_NATIVE_POSIX_PRIV_H_

#define ETH_NATIVE_POSIX_DRV_NAME CONFIG_ETH_NATIVE_POSIX_DRV_NAME
#define ETH_NATIVE_POSIX_DEV_NAME CONFIG_ETH_NATIVE_POSIX_DEV_NAME

#if defined(CONFIG_ETH_NATIVE_POSIX_STARTUP_AUTOMATIC)
#define ETH_NATIVE_POSIX_SETUP_SCRIPT CONFIG_ETH_NATIVE_POSIX_SETUP_SCRIPT
#define ETH_NATIVE_POSIX_STARTUP_SCRIPT CONFIG_ETH_NATIVE_POSIX_STARTUP_SCRIPT
Expand All @@ -25,7 +22,7 @@
#define ETH_NATIVE_POSIX_STARTUP_SCRIPT_USER ""
#endif

int eth_iface_create(const char *if_name, bool tun_only);
int eth_iface_create(const char *dev_name, const char *if_name, bool tun_only);
int eth_iface_remove(int fd);
int eth_setup_host(const char *if_name);
int eth_start_script(const char *if_name);
Expand All @@ -34,21 +31,7 @@ ssize_t eth_read_data(int fd, void *buf, size_t buf_len);
ssize_t eth_write_data(int fd, void *buf, size_t buf_len);
int eth_if_up(const char *if_name);
int eth_if_down(const char *if_name);

#if defined(CONFIG_NET_GPTP)
int eth_clock_gettime(struct net_ptp_time *time);
#endif

#if defined(CONFIG_NET_PROMISCUOUS_MODE)
int eth_clock_gettime(uint64_t *second, uint32_t *nanosecond);
int eth_promisc_mode(const char *if_name, bool enable);
#else
static inline int eth_promisc_mode(const char *if_name, bool enable)
{
ARG_UNUSED(if_name);
ARG_UNUSED(enable);

return -ENOTSUP;
}
#endif

#endif /* ZEPHYR_DRIVERS_ETHERNET_ETH_NATIVE_POSIX_PRIV_H_ */

0 comments on commit 2627559

Please sign in to comment.