Skip to content

Commit

Permalink
drivers: eth: native: Add native-sim support to 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 <[email protected]>
  • Loading branch information
jukkar authored and carlescufi committed Oct 20, 2023
1 parent 0bd323a commit c345f59
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 50 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
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 @@ -642,7 +642,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: 9 additions & 27 deletions drivers/ethernet/eth_native_posix_adapt.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,25 @@
#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>
#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 +84,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 @@ -140,8 +126,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 @@ -151,17 +136,14 @@ 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)
{
return ssystem("ip link set dev %s promisc %s",
if_name, enable ? "on" : "off");
}
#endif /* CONFIG_NET_PROMISCUOUS_MODE */
18 changes: 2 additions & 16 deletions drivers/ethernet/eth_native_posix_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,12 @@
#ifndef ZEPHYR_DRIVERS_ETHERNET_ETH_NATIVE_POSIX_PRIV_H_
#define ZEPHYR_DRIVERS_ETHERNET_ETH_NATIVE_POSIX_PRIV_H_

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_wait_data(int fd);
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);

#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 c345f59

Please sign in to comment.