#include <rte_common.h>
#include <rte_mbuf.h>
#include <rte_ethdev.h>
+#include <rte_ethdev_vdev.h>
#include <rte_malloc.h>
#include <rte_vdev.h>
#include <rte_kvargs.h>
#define ETH_TAP_SPEED_ARG "speed"
#define ETH_TAP_REMOTE_ARG "remote"
-#ifdef IFF_MULTI_QUEUE
-#define RTE_PMD_TAP_MAX_QUEUES 16
-#else
-#define RTE_PMD_TAP_MAX_QUEUES 1
-#endif
-
#define FLOWER_KERNEL_VERSION KERNEL_VERSION(4, 2, 0)
#define FLOWER_VLAN_KERNEL_VERSION KERNEL_VERSION(4, 9, 0)
tap_trigger = (tap_trigger + 1) | 0x80000000;
}
+/* Specifies on what netdevices the ioctl should be applied */
+enum ioctl_mode {
+ LOCAL_AND_REMOTE,
+ LOCAL_ONLY,
+ REMOTE_ONLY,
+};
+
static int
tap_ioctl(struct pmd_internals *pmd, unsigned long request,
- struct ifreq *ifr, int set);
+ struct ifreq *ifr, int set, enum ioctl_mode mode);
static int tap_intr_handle_set(struct rte_eth_dev *dev, int set);
if (qid == 0) {
struct ifreq ifr;
- if (tap_ioctl(pmd, SIOCGIFHWADDR, &ifr, 0) < 0)
- goto error;
- rte_memcpy(&pmd->eth_addr, ifr.ifr_hwaddr.sa_data,
+ /*
+ * pmd->eth_addr contains the desired MAC, either from remote
+ * or from a random assignment. Sync it with the tap netdevice.
+ */
+ ifr.ifr_hwaddr.sa_family = AF_LOCAL;
+ rte_memcpy(ifr.ifr_hwaddr.sa_data, &pmd->eth_addr,
ETHER_ADDR_LEN);
+ if (tap_ioctl(pmd, SIOCSIFHWADDR, &ifr, 0, LOCAL_ONLY) < 0)
+ goto error;
pmd->if_index = if_nametoindex(pmd->name);
if (!pmd->if_index) {
static int
tap_ioctl(struct pmd_internals *pmd, unsigned long request,
- struct ifreq *ifr, int set)
+ struct ifreq *ifr, int set, enum ioctl_mode mode)
{
short req_flags = ifr->ifr_flags;
- int remote = !!pmd->remote_if_index;
+ int remote = pmd->remote_if_index &&
+ (mode == REMOTE_ONLY || mode == LOCAL_AND_REMOTE);
+ if (!pmd->remote_if_index && mode == REMOTE_ONLY)
+ return 0;
/*
* If there is a remote netdevice, apply ioctl on it, then apply it on
* the tap netdevice.
*/
- if (request == SIOCGIFFLAGS && !set) {
- /*
- * Special case for getting flags. If set is given,
- * then return the flags from the remote netdevice only.
- * Otherwise return the flags from the tap netdevice.
- */
- remote = 0;
- }
apply:
if (remote)
snprintf(ifr->ifr_name, IFNAMSIZ, "%s", pmd->remote_iface);
- else
+ else if (mode == LOCAL_ONLY || mode == LOCAL_AND_REMOTE)
snprintf(ifr->ifr_name, IFNAMSIZ, "%s", pmd->name);
switch (request) {
case SIOCSIFFLAGS:
ifr->ifr_flags &= ~req_flags;
break;
case SIOCGIFFLAGS:
- if (remote && set)
- remote = 0; /* don't loop */
- break;
case SIOCGIFHWADDR:
- /* Set remote MAC on the tap netdevice */
- if (!remote && pmd->remote_if_index) {
- request = SIOCSIFHWADDR;
- goto apply;
- }
- break;
case SIOCSIFHWADDR:
case SIOCSIFMTU:
break;
}
if (ioctl(pmd->ioctl_sock, request, ifr) < 0)
goto error;
- if (remote--)
+ if (remote-- && mode == LOCAL_AND_REMOTE)
goto apply;
return 0;
struct ifreq ifr = { .ifr_flags = IFF_UP };
dev->data->dev_link.link_status = ETH_LINK_DOWN;
- return tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0);
+ return tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_AND_REMOTE);
}
static int
struct ifreq ifr = { .ifr_flags = IFF_UP };
dev->data->dev_link.link_status = ETH_LINK_UP;
- return tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1);
+ return tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE);
}
static int
struct ifreq ifr = { .ifr_flags = 0 };
if (pmd->remote_if_index) {
- tap_ioctl(pmd, SIOCGIFFLAGS, &ifr, 1);
+ tap_ioctl(pmd, SIOCGIFFLAGS, &ifr, 0, REMOTE_ONLY);
if (!(ifr.ifr_flags & IFF_UP) ||
!(ifr.ifr_flags & IFF_RUNNING)) {
dev_link->link_status = ETH_LINK_DOWN;
return 0;
}
}
- tap_ioctl(pmd, SIOCGIFFLAGS, &ifr, 0);
+ tap_ioctl(pmd, SIOCGIFFLAGS, &ifr, 0, LOCAL_ONLY);
dev_link->link_status =
((ifr.ifr_flags & IFF_UP) && (ifr.ifr_flags & IFF_RUNNING) ?
ETH_LINK_UP :
struct ifreq ifr = { .ifr_flags = IFF_PROMISC };
dev->data->promiscuous = 1;
- tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1);
+ tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE);
if (pmd->remote_if_index)
tap_flow_implicit_create(pmd, TAP_REMOTE_PROMISC);
}
struct ifreq ifr = { .ifr_flags = IFF_PROMISC };
dev->data->promiscuous = 0;
- tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0);
+ tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_AND_REMOTE);
if (pmd->remote_if_index)
tap_flow_implicit_destroy(pmd, TAP_REMOTE_PROMISC);
}
struct ifreq ifr = { .ifr_flags = IFF_ALLMULTI };
dev->data->all_multicast = 1;
- tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1);
+ tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 1, LOCAL_AND_REMOTE);
if (pmd->remote_if_index)
tap_flow_implicit_create(pmd, TAP_REMOTE_ALLMULTI);
}
struct ifreq ifr = { .ifr_flags = IFF_ALLMULTI };
dev->data->all_multicast = 0;
- tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0);
+ tap_ioctl(pmd, SIOCSIFFLAGS, &ifr, 0, LOCAL_AND_REMOTE);
if (pmd->remote_if_index)
tap_flow_implicit_destroy(pmd, TAP_REMOTE_ALLMULTI);
}
dev->data->name);
return;
}
+ /* Check the actual current MAC address on the tap netdevice */
+ if (tap_ioctl(pmd, SIOCGIFHWADDR, &ifr, 0, LOCAL_ONLY) != 0) {
+ RTE_LOG(ERR, PMD,
+ "%s: couldn't check current tap MAC address\n",
+ dev->data->name);
+ return;
+ }
+ if (is_same_ether_addr((struct ether_addr *)&ifr.ifr_hwaddr.sa_data,
+ mac_addr))
+ return;
ifr.ifr_hwaddr.sa_family = AF_LOCAL;
rte_memcpy(ifr.ifr_hwaddr.sa_data, mac_addr, ETHER_ADDR_LEN);
- if (tap_ioctl(pmd, SIOCSIFHWADDR, &ifr, 1) < 0)
+ if (tap_ioctl(pmd, SIOCSIFHWADDR, &ifr, 1, LOCAL_AND_REMOTE) < 0)
return;
rte_memcpy(&pmd->eth_addr, mac_addr, ETHER_ADDR_LEN);
+ if (pmd->remote_if_index) {
+ /* Replace MAC redirection rule after a MAC change */
+ if (tap_flow_implicit_destroy(pmd, TAP_REMOTE_LOCAL_MAC) < 0) {
+ RTE_LOG(ERR, PMD,
+ "%s: Couldn't delete MAC redirection rule\n",
+ dev->data->name);
+ return;
+ }
+ if (tap_flow_implicit_create(pmd, TAP_REMOTE_LOCAL_MAC) < 0)
+ RTE_LOG(ERR, PMD,
+ "%s: Couldn't add MAC redirection rule\n",
+ dev->data->name);
+ }
}
static int
struct ifreq ifr;
ifr.ifr_mtu = dev->data->mtu;
- if (tap_ioctl(pmd, SIOCSIFMTU, &ifr, 1) < 0) {
+ if (tap_ioctl(pmd, SIOCSIFMTU, &ifr, 1,
+ LOCAL_AND_REMOTE) < 0) {
close(fd);
return -1;
}
struct pmd_internals *internals = dev->data->dev_private;
struct rx_queue *rxq = &internals->rxq[rx_queue_id];
struct rte_mbuf **tmp = &rxq->pool;
- struct iovec (*iovecs)[nb_rx_desc + 1];
+ long iov_max = sysconf(_SC_IOV_MAX);
+ uint16_t nb_desc = RTE_MIN(nb_rx_desc, iov_max - 1);
+ struct iovec (*iovecs)[nb_desc + 1];
int data_off = RTE_PKTMBUF_HEADROOM;
- uint16_t buf_size;
int ret = 0;
int fd;
int i;
rxq->mp = mp;
rxq->trigger_seen = 1; /* force initial burst */
rxq->in_port = dev->data->port_id;
- rxq->nb_rx_desc = nb_rx_desc;
+ rxq->nb_rx_desc = nb_desc;
iovecs = rte_zmalloc_socket(dev->data->name, sizeof(*iovecs), 0,
socket_id);
if (!iovecs) {
RTE_LOG(WARNING, PMD,
"%s: Couldn't allocate %d RX descriptors\n",
- dev->data->name, nb_rx_desc);
+ dev->data->name, nb_desc);
return -ENOMEM;
}
rxq->iovecs = iovecs;
- /* Now get the space available for data in the mbuf */
- buf_size = (uint16_t)(rte_pktmbuf_data_room_size(mp) -
- RTE_PKTMBUF_HEADROOM);
-
- if (buf_size < ETH_FRAME_LEN) {
- RTE_LOG(WARNING, PMD,
- "%s: %d bytes will not fit in mbuf (%d bytes)\n",
- dev->data->name, ETH_FRAME_LEN, buf_size);
- ret = -ENOMEM;
- goto error;
- }
-
fd = rx_setup_queue(dev, internals, rx_queue_id);
if (fd == -1) {
ret = fd;
(*rxq->iovecs)[0].iov_len = sizeof(struct tun_pi);
(*rxq->iovecs)[0].iov_base = &rxq->pi;
- for (i = 1; i <= nb_rx_desc; i++) {
+ for (i = 1; i <= nb_desc; i++) {
*tmp = rte_pktmbuf_alloc(rxq->mp);
if (!*tmp) {
RTE_LOG(WARNING, PMD,
struct ifreq ifr = { .ifr_mtu = mtu };
int err = 0;
- err = tap_ioctl(pmd, SIOCSIFMTU, &ifr, 1);
+ err = tap_ioctl(pmd, SIOCSIFMTU, &ifr, 1, LOCAL_AND_REMOTE);
if (!err)
dev->data->mtu = mtu;
}
static void
-tap_dev_intr_handler(struct rte_intr_handle *intr_handle __rte_unused,
- void *cb_arg)
+tap_dev_intr_handler(void *cb_arg)
{
struct rte_eth_dev *dev = cb_arg;
struct pmd_internals *pmd = dev->data->dev_private;
}
static int
-eth_dev_tap_create(const char *name, char *tap_name, char *remote_iface)
+eth_dev_tap_create(struct rte_vdev_device *vdev, char *tap_name,
+ char *remote_iface)
{
int numa_node = rte_socket_id();
- struct rte_eth_dev *dev = NULL;
- struct pmd_internals *pmd = NULL;
- struct rte_eth_dev_data *data = NULL;
+ struct rte_eth_dev *dev;
+ struct pmd_internals *pmd;
+ struct rte_eth_dev_data *data;
int i;
RTE_LOG(DEBUG, PMD, " TAP device on numa %u\n", rte_socket_id());
goto error_exit;
}
- pmd = rte_zmalloc_socket(tap_name, sizeof(*pmd), 0, numa_node);
- if (!pmd) {
- RTE_LOG(ERR, PMD, "TAP Unable to allocate internal struct\n");
- goto error_exit;
- }
-
- /* name in allocation and data->name must be consistent */
- snprintf(data->name, sizeof(data->name), "%s", name);
- dev = rte_eth_dev_allocate(name);
+ dev = rte_eth_vdev_allocate(vdev, sizeof(*pmd));
if (!dev) {
RTE_LOG(ERR, PMD, "TAP Unable to allocate device struct\n");
goto error_exit;
}
+ pmd = dev->data->dev_private;
snprintf(pmd->name, sizeof(pmd->name), "%s", tap_name);
-
pmd->nb_queues = RTE_PMD_TAP_MAX_QUEUES;
pmd->ioctl_sock = socket(AF_INET, SOCK_DGRAM, 0);
}
/* Setup some default values */
+ rte_memcpy(data, dev->data, sizeof(*data));
data->dev_private = pmd;
- data->port_id = dev->data->port_id;
- data->mtu = dev->data->mtu;
data->dev_flags = RTE_ETH_DEV_DETACHABLE | RTE_ETH_DEV_INTR_LSC;
- data->kdrv = RTE_KDRV_NONE;
- data->drv_name = pmd_tap_drv.driver.name;
data->numa_node = numa_node;
+ data->drv_name = pmd_tap_drv.driver.name;
data->dev_link = pmd_link;
data->mac_addrs = &pmd->eth_addr;
dev->data = data;
dev->dev_ops = &ops;
- dev->driver = NULL;
dev->rx_pkt_burst = pmd_rx_burst;
dev->tx_pkt_burst = pmd_tx_burst;
*/
pmd->nlsk_fd = nl_init(0);
if (strlen(remote_iface)) {
+ struct ifreq ifr;
+
pmd->remote_if_index = if_nametoindex(remote_iface);
snprintf(pmd->remote_iface, RTE_ETH_NAME_MAX_LEN,
"%s", remote_iface);
- if (!pmd->remote_if_index)
+ if (!pmd->remote_if_index) {
RTE_LOG(ERR, PMD, "Could not find %s ifindex: "
"remote interface will remain unconfigured\n",
remote_iface);
+ return 0;
+ }
+ if (tap_ioctl(pmd, SIOCGIFHWADDR, &ifr, 0, REMOTE_ONLY) < 0) {
+ RTE_LOG(ERR, PMD, "Could not get remote MAC address\n");
+ goto error_exit;
+ }
+ rte_memcpy(&pmd->eth_addr, ifr.ifr_hwaddr.sa_data,
+ ETHER_ADDR_LEN);
+ } else {
+ eth_random_addr((uint8_t *)&pmd->eth_addr);
}
return 0;
error_exit:
- RTE_LOG(DEBUG, PMD, "TAP Unable to initialize %s\n", name);
+ RTE_LOG(DEBUG, PMD, "TAP Unable to initialize %s\n",
+ rte_vdev_device_name(vdev));
rte_free(data);
- rte_free(pmd);
-
- rte_eth_dev_release_port(dev);
-
return -EINVAL;
}
/* Open a TAP interface device.
*/
static int
-rte_pmd_tap_probe(const char *name, const char *params)
+rte_pmd_tap_probe(struct rte_vdev_device *dev)
{
+ const char *name, *params;
int ret;
struct rte_kvargs *kvlist = NULL;
int speed;
char tap_name[RTE_ETH_NAME_MAX_LEN];
char remote_iface[RTE_ETH_NAME_MAX_LEN];
+ name = rte_vdev_device_name(dev);
+ params = rte_vdev_device_args(dev);
+
speed = ETH_SPEED_NUM_10G;
snprintf(tap_name, sizeof(tap_name), "%s%d",
DEFAULT_TAP_NAME, tap_unit++);
RTE_LOG(NOTICE, PMD, "Initializing pmd_tap for %s as %s\n",
name, tap_name);
- ret = eth_dev_tap_create(name, tap_name, remote_iface);
+ ret = eth_dev_tap_create(dev, tap_name, remote_iface);
leave:
if (ret == -1) {
/* detach a TAP device.
*/
static int
-rte_pmd_tap_remove(const char *name)
+rte_pmd_tap_remove(struct rte_vdev_device *dev)
{
struct rte_eth_dev *eth_dev = NULL;
struct pmd_internals *internals;
rte_socket_id());
/* find the ethdev entry */
- eth_dev = rte_eth_dev_allocated(name);
+ eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
if (!eth_dev)
return 0;
};
RTE_PMD_REGISTER_VDEV(net_tap, pmd_tap_drv);
RTE_PMD_REGISTER_ALIAS(net_tap, eth_tap);
-RTE_PMD_REGISTER_PARAM_STRING(net_tap, "iface=<string>,speed=N");
+RTE_PMD_REGISTER_PARAM_STRING(net_tap,
+ ETH_TAP_IFACE_ARG "=<string> "
+ ETH_TAP_SPEED_ARG "=<int> "
+ ETH_TAP_REMOTE_ARG "=<string>");