Author: afedorov
Date: Tue Apr  7 17:06:33 2020
New Revision: 359704
URL: https://svnweb.freebsd.org/changeset/base/359704

Log:
  Add VIRTIO_NET_F_MTU flag support for the bhyve virtio-net device.
  The flag can be enabled using the new 'mtu' option:
  bhyve -s X:Y:Z,virtio-net,[tapN|valeX:N],mtu=9000
  
  Reported by:  vmaffione, jhb
  Approved by:  vmaffione (mentor)
  Differential Revision:        https://reviews.freebsd.org/D23971

Modified:
  head/usr.sbin/bhyve/net_backends.h
  head/usr.sbin/bhyve/net_utils.c
  head/usr.sbin/bhyve/net_utils.h
  head/usr.sbin/bhyve/pci_virtio_net.c

Modified: head/usr.sbin/bhyve/net_backends.h
==============================================================================
--- head/usr.sbin/bhyve/net_backends.h  Tue Apr  7 17:05:05 2020        
(r359703)
+++ head/usr.sbin/bhyve/net_backends.h  Tue Apr  7 17:06:33 2020        
(r359704)
@@ -59,6 +59,7 @@ void  netbe_rx_enable(net_backend_t *be);
  */
 #define        VIRTIO_NET_F_CSUM       (1 <<  0) /* host handles partial cksum 
*/
 #define        VIRTIO_NET_F_GUEST_CSUM (1 <<  1) /* guest handles partial 
cksum */
+#define        VIRTIO_NET_F_MTU        (1 <<  3) /* initial MTU advice */
 #define        VIRTIO_NET_F_MAC        (1 <<  5) /* host supplies MAC */
 #define        VIRTIO_NET_F_GSO_DEPREC (1 <<  6) /* deprecated: host handles 
GSO */
 #define        VIRTIO_NET_F_GUEST_TSO4 (1 <<  7) /* guest can rcv TSOv4 */
@@ -76,6 +77,7 @@ void  netbe_rx_enable(net_backend_t *be);
 #define        VIRTIO_NET_F_CTRL_VLAN  (1 << 19) /* control channel VLAN 
filtering */
 #define        VIRTIO_NET_F_GUEST_ANNOUNCE \
                                (1 << 21) /* guest can send gratuitous pkts */
+#define        VIRTIO_NET_F_MQ         (1 << 22) /* host supports multiple VQ 
pairs */
 
 /*
  * Fixed network header size

Modified: head/usr.sbin/bhyve/net_utils.c
==============================================================================
--- head/usr.sbin/bhyve/net_utils.c     Tue Apr  7 17:05:05 2020        
(r359703)
+++ head/usr.sbin/bhyve/net_utils.c     Tue Apr  7 17:06:33 2020        
(r359704)
@@ -31,9 +31,12 @@ __FBSDID("$FreeBSD$");
 #include <sys/types.h>
 #include <net/ethernet.h>
 
+#include <assert.h>
 #include <errno.h>
+#include <limits.h>
 #include <md5.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 
 #include "bhyverun.h"
@@ -59,6 +62,37 @@ net_parsemac(char *mac_str, uint8_t *mac_addr)
                memcpy(mac_addr, ea->octet, ETHER_ADDR_LEN);
 
         return (0);
+}
+
+int
+net_parsemtu(const char *mtu_str, unsigned long *mtu)
+{
+       char *end;
+       unsigned long val;
+
+       assert(mtu_str != NULL);
+
+       if (*mtu_str == '-')
+               goto err;
+
+       val = strtoul(mtu_str, &end, 0);
+
+       if (*end != '\0')
+               goto err;
+
+       if (val == ULONG_MAX)
+               return (ERANGE);
+
+       if (val == 0 && errno == EINVAL)
+               return (EINVAL);
+
+       *mtu = val;
+
+       return (0);
+
+err:
+       errno = EINVAL;
+       return (EINVAL);
 }
 
 void

Modified: head/usr.sbin/bhyve/net_utils.h
==============================================================================
--- head/usr.sbin/bhyve/net_utils.h     Tue Apr  7 17:05:05 2020        
(r359703)
+++ head/usr.sbin/bhyve/net_utils.h     Tue Apr  7 17:06:33 2020        
(r359704)
@@ -35,5 +35,6 @@
 
 void   net_genmac(struct pci_devinst *pi, uint8_t *macaddr);
 int    net_parsemac(char *mac_str, uint8_t *mac_addr);
+int    net_parsemtu(const char *mtu_str, unsigned long *mtu);
 
 #endif /* _NET_UTILS_H_ */

Modified: head/usr.sbin/bhyve/pci_virtio_net.c
==============================================================================
--- head/usr.sbin/bhyve/pci_virtio_net.c        Tue Apr  7 17:05:05 2020        
(r359703)
+++ head/usr.sbin/bhyve/pci_virtio_net.c        Tue Apr  7 17:06:33 2020        
(r359704)
@@ -67,6 +67,9 @@ __FBSDID("$FreeBSD$");
 
 #define VTNET_MAX_PKT_LEN      (65536 + 64)
 
+#define VTNET_MIN_MTU  ETHERMIN
+#define VTNET_MAX_MTU  65535
+
 #define VTNET_S_HOSTCAPS      \
   ( VIRTIO_NET_F_MAC | VIRTIO_NET_F_STATUS | \
     VIRTIO_F_NOTIFY_ON_EMPTY | VIRTIO_RING_F_INDIRECT_DESC)
@@ -77,6 +80,8 @@ __FBSDID("$FreeBSD$");
 struct virtio_net_config {
        uint8_t  mac[6];
        uint16_t status;
+       uint16_t max_virtqueue_pairs;
+       uint16_t mtu;
 } __packed;
 
 /*
@@ -532,6 +537,8 @@ pci_vtnet_init(struct vmctx *ctx, struct pci_devinst *
        struct pci_vtnet_softc *sc;
        char tname[MAXCOMLEN + 1];
        int mac_provided;
+       int mtu_provided;
+       unsigned long mtu = ETHERMTU;
 
        /*
         * Allocate data structures for further virtio initializations.
@@ -557,6 +564,7 @@ pci_vtnet_init(struct vmctx *ctx, struct pci_devinst *
         * if specified.
         */
        mac_provided = 0;
+       mtu_provided = 0;
        if (opts != NULL) {
                char *devname;
                char *vtopts;
@@ -585,6 +593,17 @@ pci_vtnet_init(struct vmctx *ctx, struct pci_devinst *
                                if (err)
                                        break;
                                mac_provided = 1;
+                       } else if (strcmp(key, "mtu") == 0) {
+                               err = net_parsemtu(value, &mtu);
+                               if (err)
+                                       break;
+
+                               if (mtu < VTNET_MIN_MTU || mtu > VTNET_MAX_MTU) 
{
+                                       err = EINVAL;
+                                       errno = EINVAL;
+                                       break;
+                               }
+                               mtu_provided = 1;
                        }
                }
 
@@ -608,6 +627,17 @@ pci_vtnet_init(struct vmctx *ctx, struct pci_devinst *
        if (!mac_provided) {
                net_genmac(pi, sc->vsc_config.mac);
        }
+
+       sc->vsc_config.mtu = mtu;
+       if (mtu_provided) {
+               sc->vsc_consts.vc_hv_caps |= VIRTIO_NET_F_MTU;
+       }
+
+       /* 
+        * Since we do not actually support multiqueue,
+        * set the maximum virtqueue pairs to 1. 
+        */
+       sc->vsc_config.max_virtqueue_pairs = 1;
 
        /* initialize config space */
        pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_NET);
_______________________________________________
svn-src-head@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to