net/virtio-user: specify MAC of the tap
authorNing Li <muziding001@163.com>
Fri, 29 Dec 2017 03:38:42 +0000 (11:38 +0800)
committerFerruh Yigit <ferruh.yigit@intel.com>
Fri, 30 Mar 2018 12:08:44 +0000 (14:08 +0200)
When using virtio-user with vhost-kernel to exchange
packet with kernel networking stack, application can
set the MAC of the tap interface via parameter.

Signed-off-by: Ning Li <muziding001@163.com>
Reviewed-by: Seán Harte <seanbh@gmail.com>
Tested-by: Seán Harte <seanbh@gmail.com>
Reviewed-by: Jianfeng Tan <jianfeng.tan@intel.com>
drivers/net/virtio/virtio_user/vhost_kernel.c
drivers/net/virtio/virtio_user/vhost_kernel_tap.c
drivers/net/virtio/virtio_user/vhost_kernel_tap.h

index 8d0a1ab..1711ead 100644 (file)
@@ -351,7 +351,8 @@ vhost_kernel_enable_queue_pair(struct virtio_user_dev *dev,
        else
                hdr_size = sizeof(struct virtio_net_hdr);
 
-       tapfd = vhost_kernel_open_tap(&dev->ifname, hdr_size, req_mq);
+       tapfd = vhost_kernel_open_tap(&dev->ifname, hdr_size, req_mq,
+                        (char *)dev->mac_addr);
        if (tapfd < 0) {
                PMD_DRV_LOG(ERR, "fail to open tap for vhost kernel");
                return -1;
index 1a47a34..9ea7ade 100644 (file)
@@ -7,15 +7,19 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <net/if.h>
+#include <net/if_arp.h>
 #include <errno.h>
 #include <string.h>
 #include <limits.h>
 
+#include <rte_ether.h>
+
 #include "vhost_kernel_tap.h"
 #include "../virtio_logs.h"
 
 int
-vhost_kernel_open_tap(char **p_ifname, int hdr_size, int req_mq)
+vhost_kernel_open_tap(char **p_ifname, int hdr_size, int req_mq,
+                        const char *mac)
 {
        unsigned int tap_features;
        int sndbuf = INT_MAX;
@@ -94,6 +98,14 @@ vhost_kernel_open_tap(char **p_ifname, int hdr_size, int req_mq)
                PMD_DRV_LOG(ERR, "TUNSETOFFLOAD ioctl() failed: %s",
                           strerror(errno));
 
+       memset(&ifr, 0, sizeof(ifr));
+       ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
+       memcpy(ifr.ifr_hwaddr.sa_data, mac, ETHER_ADDR_LEN);
+       if (ioctl(tapfd, SIOCSIFHWADDR, (void *)&ifr) == -1) {
+               PMD_DRV_LOG(ERR, "SIOCSIFHWADDR failed: %s", strerror(errno));
+               goto error;
+       }
+
        if (!(*p_ifname))
                *p_ifname = strdup(ifr.ifr_name);
 
index 7d52e6b..01a026f 100644 (file)
@@ -35,4 +35,5 @@
 /* Constants */
 #define PATH_NET_TUN   "/dev/net/tun"
 
-int vhost_kernel_open_tap(char **p_ifname, int hdr_size, int req_mq);
+int vhost_kernel_open_tap(char **p_ifname, int hdr_size, int req_mq,
+                        const char *mac);