2018-03-29 18:31:45 +00:00
|
|
|
/* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
* Copyright(c) 2010-2018 Intel Corporation
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
2019-03-06 16:22:39 +00:00
|
|
|
#ifdef RTE_EXEC_ENV_LINUX
|
2018-03-29 18:31:45 +00:00
|
|
|
#include <linux/if.h>
|
|
|
|
#include <linux/if_tun.h>
|
|
|
|
#endif
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-04-17 13:17:19 +00:00
|
|
|
#include <rte_string_fns.h>
|
|
|
|
|
2018-03-29 18:31:45 +00:00
|
|
|
#include "tap.h"
|
|
|
|
|
|
|
|
#define TAP_DEV "/dev/net/tun"
|
|
|
|
|
|
|
|
static struct tap_list tap_list;
|
|
|
|
|
|
|
|
int
|
|
|
|
tap_init(void)
|
|
|
|
{
|
|
|
|
TAILQ_INIT(&tap_list);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct tap *
|
|
|
|
tap_find(const char *name)
|
|
|
|
{
|
|
|
|
struct tap *tap;
|
|
|
|
|
|
|
|
if (name == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(tap, &tap_list, node)
|
|
|
|
if (strcmp(tap->name, name) == 0)
|
|
|
|
return tap;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-03-06 16:22:39 +00:00
|
|
|
#ifndef RTE_EXEC_ENV_LINUX
|
2018-03-29 18:31:45 +00:00
|
|
|
|
|
|
|
struct tap *
|
|
|
|
tap_create(const char *name __rte_unused)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
struct tap *
|
|
|
|
tap_create(const char *name)
|
|
|
|
{
|
|
|
|
struct tap *tap;
|
|
|
|
struct ifreq ifr;
|
|
|
|
int fd, status;
|
|
|
|
|
|
|
|
/* Check input params */
|
|
|
|
if ((name == NULL) ||
|
|
|
|
tap_find(name))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Resource create */
|
|
|
|
fd = open(TAP_DEV, O_RDWR | O_NONBLOCK);
|
|
|
|
if (fd < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
|
|
|
ifr.ifr_flags = IFF_TAP | IFF_NO_PI; /* No packet information */
|
2019-04-03 14:45:04 +00:00
|
|
|
strlcpy(ifr.ifr_name, name, IFNAMSIZ);
|
2018-03-29 18:31:45 +00:00
|
|
|
|
|
|
|
status = ioctl(fd, TUNSETIFF, (void *) &ifr);
|
2018-04-18 16:58:09 +00:00
|
|
|
if (status < 0) {
|
|
|
|
close(fd);
|
2018-03-29 18:31:45 +00:00
|
|
|
return NULL;
|
2018-04-18 16:58:09 +00:00
|
|
|
}
|
2018-03-29 18:31:45 +00:00
|
|
|
|
|
|
|
/* Node allocation */
|
|
|
|
tap = calloc(1, sizeof(struct tap));
|
2018-04-18 16:58:09 +00:00
|
|
|
if (tap == NULL) {
|
|
|
|
close(fd);
|
2018-03-29 18:31:45 +00:00
|
|
|
return NULL;
|
2018-04-18 16:58:09 +00:00
|
|
|
}
|
2018-03-29 18:31:45 +00:00
|
|
|
/* Node fill in */
|
2018-04-17 13:17:19 +00:00
|
|
|
strlcpy(tap->name, name, sizeof(tap->name));
|
2018-03-29 18:31:45 +00:00
|
|
|
tap->fd = fd;
|
|
|
|
|
|
|
|
/* Node add to list */
|
|
|
|
TAILQ_INSERT_TAIL(&tap_list, tap, node);
|
|
|
|
|
|
|
|
return tap;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|