From 45fc68e904d7e048011730eea197977e93b2e3a6 Mon Sep 17 00:00:00 2001 From: liqinhui Date: Wed, 8 May 2024 17:13:12 +0800 Subject: [PATCH] sim/net: Support to set the MTU of the sim netdevice. Signed-off-by: liqinhui --- arch/sim/Kconfig | 8 ++++++++ arch/sim/src/sim/posix/sim_tapdev.c | 14 ++++++++++++-- arch/sim/src/sim/sim_netdriver.c | 3 ++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/arch/sim/Kconfig b/arch/sim/Kconfig index a3099ab9c4..f5d8286456 100644 --- a/arch/sim/Kconfig +++ b/arch/sim/Kconfig @@ -223,6 +223,14 @@ config SIM_NETDEV_VPNKIT endchoice +config SIM_NETDEV_MTU + int "The MTU of Simulated Network Device" + default 1500 + range 1280 65535 if NET_IPv6 + range 576 65535 if !NET_IPv6 + ---help--- + The MTU of the network devices. + config SIM_NETDEV_NUMBER int "Number of Simulated Network Device" default 1 diff --git a/arch/sim/src/sim/posix/sim_tapdev.c b/arch/sim/src/sim/posix/sim_tapdev.c index 34e33eb90c..348924c31f 100644 --- a/arch/sim/src/sim/posix/sim_tapdev.c +++ b/arch/sim/src/sim/posix/sim_tapdev.c @@ -249,10 +249,20 @@ void sim_tapdev_init(int devidx, void *priv, close(tapdevfd); return; } -#else +#endif + memset(&ifr, 0, sizeof(ifr)); strncpy(ifr.ifr_name, gdevname[devidx], IFNAMSIZ); -#endif + ifr.ifr_mtu = CONFIG_SIM_NETDEV_MTU; + ret = ioctl(sockfd, SIOCSIFMTU, &ifr); + if (ret < 0) + { + syslog(LOG_ERR, "TAPDEV: ioctl failed (can't set MTU " + "for %s): %d\n", gdevname[devidx], -ret); + close(sockfd); + close(tapdevfd); + return; + } ret = ioctl(sockfd, SIOCGIFMTU, &ifr); close(sockfd); diff --git a/arch/sim/src/sim/sim_netdriver.c b/arch/sim/src/sim/sim_netdriver.c index 5b49c65f98..3d68ba2655 100644 --- a/arch/sim/src/sim/sim_netdriver.c +++ b/arch/sim/src/sim/sim_netdriver.c @@ -71,7 +71,8 @@ #include "sim_internal.h" -#define SIM_NETDEV_BUFSIZE (MAX_NETDEV_PKTSIZE + CONFIG_NET_GUARDSIZE) +#define SIM_NETDEV_BUFSIZE (CONFIG_SIM_NETDEV_MTU + ETH_HDRLEN + \ + CONFIG_NET_GUARDSIZE) /* We don't know packet length before receiving, so we can only offload it * when netpkt's buffer is long enough.