Commit c345f599 authored by Jukka Rissanen's avatar Jukka Rissanen Committed by Carles Cufi
Browse files

drivers: eth: native: Add native-sim support to posix Ethernet driver



This will enable Ethernet driver to be used when compiling
for native-sim board.

Signed-off-by: default avatarJukka Rissanen <jukka.rissanen@nordicsemi.no>
parent 0bd323ab
Loading
Loading
Loading
Loading
+7 −2
Original line number Diff line number Diff line
@@ -49,7 +49,12 @@ if(CONFIG_ETH_NATIVE_POSIX)
    PROPERTIES COMPILE_DEFINITIONS
    "NO_POSIX_CHEATS;_BSD_SOURCE;_DEFAULT_SOURCE"
    )
  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)
+1 −1
Original line number Diff line number Diff line
@@ -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.
+4 −4
Original line number Diff line number Diff line
@@ -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;
	}
@@ -460,7 +460,7 @@ 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);
@@ -468,7 +468,7 @@ static void eth_iface_init(struct net_if *iface)
	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 {
@@ -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,
+9 −27
Original line number Diff line number Diff line
@@ -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;
	}
@@ -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);

@@ -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;
@@ -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 */
+2 −16
Original line number Diff line number Diff line
@@ -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_ */