sim/tapdev: follow the tunnel MTU size

Change-Id: Ia32255517650d95ea3a675ee9fe5b69e923fb51a
Signed-off-by: chao.an <anchao@xiaomi.com>
This commit is contained in:
chao.an 2020-05-14 16:24:21 +08:00 committed by patacongo
parent 9cc2f50405
commit 8bce416c25
3 changed files with 53 additions and 13 deletions

View File

@ -373,6 +373,7 @@ void vpnkit_ifdown(void);
#ifdef CONFIG_SIM_NETDEV #ifdef CONFIG_SIM_NETDEV
int netdriver_init(void); int netdriver_init(void);
void netdriver_setmacaddr(unsigned char *macaddr); void netdriver_setmacaddr(unsigned char *macaddr);
void netdriver_setmtu(int mtu);
void netdriver_loop(void); void netdriver_loop(void);
#endif #endif

View File

@ -48,6 +48,7 @@
#include <debug.h> #include <debug.h>
#include <string.h> #include <string.h>
#include <nuttx/kmalloc.h>
#include <nuttx/wqueue.h> #include <nuttx/wqueue.h>
#include <nuttx/net/net.h> #include <nuttx/net/net.h>
#include <nuttx/net/netdev.h> #include <nuttx/net/netdev.h>
@ -69,10 +70,6 @@ static struct work_s g_timer_work;
static struct work_s g_avail_work; static struct work_s g_avail_work;
static struct work_s g_recv_work; static struct work_s g_recv_work;
/* A single packet buffer is used */
static uint8_t g_pktbuf[MAX_NETDEV_PKTSIZE + CONFIG_NET_GUARDSIZE];
/* Ethernet peripheral state */ /* Ethernet peripheral state */
static struct net_driver_s g_sim_dev; static struct net_driver_s g_sim_dev;
@ -126,10 +123,12 @@ static void netdriver_recv_work(FAR void *arg)
net_lock(); net_lock();
/* netdev_read will return 0 on a timeout event and >0 on a data received event */ /* netdev_read will return 0 on a timeout event and > 0
* on a data received event
*/
dev->d_len = netdev_read((FAR unsigned char *)dev->d_buf, dev->d_len = netdev_read((FAR unsigned char *)dev->d_buf,
CONFIG_NET_ETH_PKTSIZE); dev->d_pktsize);
if (dev->d_len > 0) if (dev->d_len > 0)
{ {
NETDEV_RXPACKETS(dev); NETDEV_RXPACKETS(dev);
@ -149,7 +148,9 @@ static void netdriver_recv_work(FAR void *arg)
pkt_input(dev); pkt_input(dev);
#endif /* CONFIG_NET_PKT */ #endif /* CONFIG_NET_PKT */
/* We only accept IP packets of the configured type and ARP packets */ /* We only accept IP packets of the configured type
* and ARP packets
*/
#ifdef CONFIG_NET_IPv4 #ifdef CONFIG_NET_IPv4
if (eth->type == HTONS(ETHTYPE_IP)) if (eth->type == HTONS(ETHTYPE_IP))
@ -325,14 +326,29 @@ static int netdriver_txavail(FAR struct net_driver_s *dev)
int netdriver_init(void) int netdriver_init(void)
{ {
FAR struct net_driver_s *dev = &g_sim_dev; FAR struct net_driver_s *dev = &g_sim_dev;
void *pktbuf;
int pktsize;
/* Internal initialization */ /* Internal initialization */
netdev_init(); netdev_init();
/* Update the buffer size */
pktsize = dev->d_pktsize ? dev->d_pktsize :
(MAX_NETDEV_PKTSIZE + CONFIG_NET_GUARDSIZE);
/* Allocate packet buffer */
pktbuf = kmm_malloc(pktsize);
if (pktbuf == NULL)
{
return -ENOMEM;
}
/* Set callbacks */ /* Set callbacks */
dev->d_buf = g_pktbuf; /* Single packet buffer */ dev->d_buf = pktbuf;
dev->d_ifup = netdriver_ifup; dev->d_ifup = netdriver_ifup;
dev->d_ifdown = netdriver_ifdown; dev->d_ifdown = netdriver_ifdown;
dev->d_txavail = netdriver_txavail; dev->d_txavail = netdriver_txavail;
@ -347,6 +363,11 @@ void netdriver_setmacaddr(unsigned char *macaddr)
memcpy(g_sim_dev.d_mac.ether.ether_addr_octet, macaddr, IFHWADDRLEN); memcpy(g_sim_dev.d_mac.ether.ether_addr_octet, macaddr, IFHWADDRLEN);
} }
void netdriver_setmtu(int mtu)
{
g_sim_dev.d_pktsize = mtu;
}
void netdriver_loop(void) void netdriver_loop(void)
{ {
if (work_available(&g_recv_work) && netdev_avail()) if (work_available(&g_recv_work) && netdev_avail())

View File

@ -92,6 +92,7 @@ struct sel_arg_struct
****************************************************************************/ ****************************************************************************/
void netdriver_setmacaddr(unsigned char *macaddr); void netdriver_setmacaddr(unsigned char *macaddr);
void netdriver_setmtu(int mtu);
/**************************************************************************** /****************************************************************************
* Private Data * Private Data
@ -219,15 +220,29 @@ void tapdev_init(void)
ifr.ifr_ifindex = if_nametoindex(gdevname); ifr.ifr_ifindex = if_nametoindex(gdevname);
ret = ioctl(sockfd, SIOCBRADDIF, &ifr); ret = ioctl(sockfd, SIOCBRADDIF, &ifr);
close(sockfd);
if (ret < 0) if (ret < 0)
{ {
printf("TAPDEV: ioctl failed (can't add interface %s to " printf("TAPDEV: ioctl failed (can't add interface %s to "
"bridge %s): %d\r\n", "bridge %s): %d\r\n",
gdevname, CONFIG_SIM_NET_BRIDGE_DEVICE, -ret); gdevname, CONFIG_SIM_NET_BRIDGE_DEVICE, -ret);
close(sockfd);
close(tapdevfd); close(tapdevfd);
return; return;
} }
ret = ioctl(sockfd, SIOCGIFMTU, &ifr);
close(sockfd);
if (ret < 0)
{
printf("TAPDEV: ioctl failed (can't get MTU "
"from %s): %d\r\n", gdevname, -ret);
close(tapdevfd);
return;
}
else
{
netdriver_setmtu(ifr.ifr_mtu);
}
#endif #endif
gtapdevfd = tapdevfd; gtapdevfd = tapdevfd;
@ -341,7 +356,8 @@ void tapdev_ifup(in_addr_t ifaddr)
ret = ioctl(sockfd, SIOCGIFFLAGS, (unsigned long)&ifr); ret = ioctl(sockfd, SIOCGIFFLAGS, (unsigned long)&ifr);
if (ret < 0) if (ret < 0)
{ {
printf("TAPDEV: ioctl failed (can't get interface flags): %d\r\n", -ret); printf("TAPDEV: ioctl failed "
"(can't get interface flags): %d\r\n", -ret);
close(sockfd); close(sockfd);
return; return;
} }
@ -350,7 +366,8 @@ void tapdev_ifup(in_addr_t ifaddr)
ret = ioctl(sockfd, SIOCSIFFLAGS, (unsigned long)&ifr); ret = ioctl(sockfd, SIOCSIFFLAGS, (unsigned long)&ifr);
if (ret < 0) if (ret < 0)
{ {
printf("TAPDEV: ioctl failed (can't set interface flags): %d\r\n", -ret); printf("TAPDEV: ioctl failed "
"(can't set interface flags): %d\r\n", -ret);
close(sockfd); close(sockfd);
return; return;
} }
@ -405,7 +422,8 @@ void tapdev_ifdown(void)
ret = ioctl(sockfd, SIOCDELRT, (unsigned long)&ghostroute); ret = ioctl(sockfd, SIOCDELRT, (unsigned long)&ghostroute);
if (ret < 0) if (ret < 0)
{ {
printf("TAPDEV: ioctl failed (can't delete host route): %d\r\n", -ret); printf("TAPDEV: ioctl failed "
"(can't delete host route): %d\r\n", -ret);
} }
close(sockfd); close(sockfd);