diff --git a/usr.sbin/bhyve/net_backends.h b/usr.sbin/bhyve/net_backends.h index 25c1468622dd..75d1a25adced 100644 --- a/usr.sbin/bhyve/net_backends.h +++ b/usr.sbin/bhyve/net_backends.h @@ -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 diff --git a/usr.sbin/bhyve/net_utils.c b/usr.sbin/bhyve/net_utils.c index d545d486d517..d602cac3eb0a 100644 --- a/usr.sbin/bhyve/net_utils.c +++ b/usr.sbin/bhyve/net_utils.c @@ -31,9 +31,12 @@ __FBSDID("$FreeBSD$"); #include #include +#include #include +#include #include #include +#include #include #include "bhyverun.h" @@ -61,6 +64,37 @@ net_parsemac(char *mac_str, uint8_t *mac_addr) 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 net_genmac(struct pci_devinst *pi, uint8_t *macaddr) { diff --git a/usr.sbin/bhyve/net_utils.h b/usr.sbin/bhyve/net_utils.h index 3c83519931c7..1ca20ddb74ab 100644 --- a/usr.sbin/bhyve/net_utils.h +++ b/usr.sbin/bhyve/net_utils.h @@ -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_ */ diff --git a/usr.sbin/bhyve/pci_virtio_net.c b/usr.sbin/bhyve/pci_virtio_net.c index 52893f283919..adc273128585 100644 --- a/usr.sbin/bhyve/pci_virtio_net.c +++ b/usr.sbin/bhyve/pci_virtio_net.c @@ -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 *pi, char *opts) 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 *pi, char *opts) * 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 *pi, char *opts) 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; } } @@ -609,6 +628,17 @@ pci_vtnet_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) 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); pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);