arch/sim: Handle tap device initializition failure gracefully

This commit is contained in:
Xiang Xiao 2020-02-24 17:33:23 +08:00 committed by Gregory Nutt
parent 96adb827a3
commit b9b86fcc63
1 changed files with 30 additions and 8 deletions

View File

@ -100,7 +100,7 @@ void netdriver_setmacaddr(unsigned char *macaddr);
#ifdef TAPDEV_DEBUG
static int gdrop = 0;
#endif
static int gtapdevfd;
static int gtapdevfd = -1;
static char gdevname[IFNAMSIZ];
#ifdef CONFIG_SIM_NET_HOST_ROUTE
@ -167,6 +167,7 @@ static void set_macaddr(void)
void tapdev_init(void)
{
struct ifreq ifr;
int tapdevfd;
int ret;
#ifdef CONFIG_SIM_NET_BRIDGE
@ -175,10 +176,10 @@ void tapdev_init(void)
/* Open the tap device */
gtapdevfd = open(DEVTAP, O_RDWR, 0644);
if (gtapdevfd < 0)
tapdevfd = open(DEVTAP, O_RDWR, 0644);
if (tapdevfd < 0)
{
printf("TAPDEV: open failed: %d\r\n", -gtapdevfd);
printf("TAPDEV: open failed: %d\r\n", -tapdevfd);
return;
}
@ -186,10 +187,11 @@ void tapdev_init(void)
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
ret = ioctl(gtapdevfd, TUNSETIFF, (unsigned long) &ifr);
ret = ioctl(tapdevfd, TUNSETIFF, (unsigned long) &ifr);
if (ret < 0)
{
printf("TAPDEV: ioctl failed: %d\r\n", -ret);
close(tapdevfd);
return;
}
@ -206,6 +208,7 @@ void tapdev_init(void)
if (sockfd < 0)
{
printf("TAPDEV: Can't open socket: %d\r\n", -sockfd);
close(tapdevfd);
return;
}
@ -216,16 +219,19 @@ void tapdev_init(void)
ifr.ifr_ifindex = if_nametoindex(gdevname);
ret = ioctl(sockfd, SIOCBRADDIF, &ifr);
close(sockfd);
if (ret < 0)
{
printf("TAPDEV: ioctl failed (can't add interface %s to "
"bridge %s): %d\r\n",
gdevname, CONFIG_SIM_NET_BRIDGE_DEVICE, -ret);
}
close(sockfd);
close(tapdevfd);
return;
}
#endif
gtapdevfd = tapdevfd;
/* Set the MAC address */
set_macaddr();
@ -277,6 +283,12 @@ unsigned int tapdev_read(unsigned char *buf, unsigned int buflen)
void tapdev_send(unsigned char *buf, unsigned int buflen)
{
int ret;
if (gtapdevfd < 0)
{
return;
}
#ifdef TAPDEV_DEBUG
printf("tapdev_send: sending %d bytes\r\n", buflen);
@ -308,6 +320,11 @@ void tapdev_ifup(in_addr_t ifaddr)
struct sockaddr_in *addr;
#endif
if (gtapdevfd < 0)
{
return;
}
/* Get a socket with which to manipulate the tap device */
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
@ -369,6 +386,11 @@ void tapdev_ifdown(void)
int sockfd;
int ret;
if (gtapdevfd < 0)
{
return;
}
if (((struct sockaddr_in *)&ghostroute.rt_dst)->sin_addr.s_addr != 0)
{
/* Get a socket with which to manipulate the tap device */