git: d3b97a1ea012 - stable/13 - bhyve: Define an accessor for net backend private data

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Fri, 18 Nov 2022 15:35:11 UTC
The branch stable/13 has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=d3b97a1ea01233486ddc7693e8a53c59d331c8c2

commit d3b97a1ea01233486ddc7693e8a53c59d331c8c2
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2022-11-11 15:01:40 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2022-11-18 15:17:35 +0000

    bhyve: Define an accessor for net backend private data
    
    Use it to silence warnings about potential unaligned accesses.  No
    functional change intended.
    
    Reviewed by:    corvink, jhb
    Differential Revision:  https://reviews.freebsd.org/D37289
    
    (cherry picked from commit 0ced97acb05197cb2ef30932786eab0512f77e07)
---
 usr.sbin/bhyve/net_backends.c | 38 ++++++++++++++++++++------------------
 1 file changed, 20 insertions(+), 18 deletions(-)

diff --git a/usr.sbin/bhyve/net_backends.c b/usr.sbin/bhyve/net_backends.c
index 5b4a2c0ae257..cc2a06592429 100644
--- a/usr.sbin/bhyve/net_backends.c
+++ b/usr.sbin/bhyve/net_backends.c
@@ -168,10 +168,12 @@ struct net_backend {
 	/* Size of backend-specific private data. */
 	size_t priv_size;
 
-	/* Room for backend-specific data. */
-	char opaque[0];
+	/* Backend-specific private data follows. */
 };
 
+#define	NET_BE_PRIV(be)		((void *)((be) + 1))
+#define	NET_BE_SIZE(be)		(sizeof(*be) + (be)->priv_size)
+
 SET_DECLARE(net_backend_set, struct net_backend);
 
 #define VNET_HDR_LEN	sizeof(struct virtio_net_rxhdr)
@@ -207,7 +209,7 @@ struct tap_priv {
 static void
 tap_cleanup(struct net_backend *be)
 {
-	struct tap_priv *priv = (struct tap_priv *)be->opaque;
+	struct tap_priv *priv = NET_BE_PRIV(be);
 
 	if (priv->mevp) {
 		mevent_delete(priv->mevp);
@@ -222,7 +224,7 @@ static int
 tap_init(struct net_backend *be, const char *devname,
 	 nvlist_t *nvl, net_be_rxeof_t cb, void *param)
 {
-	struct tap_priv *priv = (struct tap_priv *)be->opaque;
+	struct tap_priv *priv = NET_BE_PRIV(be);
 	char tbuf[80];
 	int opt = 1;
 #if defined(INET6) || defined(INET)
@@ -323,7 +325,7 @@ tap_send(struct net_backend *be, const struct iovec *iov, int iovcnt)
 static ssize_t
 tap_peek_recvlen(struct net_backend *be)
 {
-	struct tap_priv *priv = (struct tap_priv *)be->opaque;
+	struct tap_priv *priv = NET_BE_PRIV(be);
 	ssize_t ret;
 
 	if (priv->bbuflen > 0) {
@@ -353,7 +355,7 @@ tap_peek_recvlen(struct net_backend *be)
 static ssize_t
 tap_recv(struct net_backend *be, const struct iovec *iov, int iovcnt)
 {
-	struct tap_priv *priv = (struct tap_priv *)be->opaque;
+	struct tap_priv *priv = NET_BE_PRIV(be);
 	ssize_t ret;
 
 	if (priv->bbuflen > 0) {
@@ -381,7 +383,7 @@ tap_recv(struct net_backend *be, const struct iovec *iov, int iovcnt)
 static void
 tap_recv_enable(struct net_backend *be)
 {
-	struct tap_priv *priv = (struct tap_priv *)be->opaque;
+	struct tap_priv *priv = NET_BE_PRIV(be);
 
 	mevent_enable(priv->mevp);
 }
@@ -389,7 +391,7 @@ tap_recv_enable(struct net_backend *be)
 static void
 tap_recv_disable(struct net_backend *be)
 {
-	struct tap_priv *priv = (struct tap_priv *)be->opaque;
+	struct tap_priv *priv = NET_BE_PRIV(be);
 
 	mevent_disable(priv->mevp);
 }
@@ -453,7 +455,7 @@ static int
 ng_init(struct net_backend *be, const char *devname,
 	 nvlist_t *nvl, net_be_rxeof_t cb, void *param)
 {
-	struct tap_priv *p = (struct tap_priv *)be->opaque;
+	struct tap_priv *p = NET_BE_PRIV(be);
 	struct ngm_connect ngc;
 	const char *value, *nodename;
 	int sbsz;
@@ -629,7 +631,7 @@ netmap_set_vnet_hdr_len(struct net_backend *be, int vnet_hdr_len)
 {
 	int err;
 	struct nmreq req;
-	struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
+	struct netmap_priv *priv = NET_BE_PRIV(be);
 
 	nmreq_init(&req, priv->ifname);
 	req.nr_cmd = NETMAP_BDG_VNET_HDR;
@@ -686,7 +688,7 @@ static int
 netmap_init(struct net_backend *be, const char *devname,
 	    nvlist_t *nvl, net_be_rxeof_t cb, void *param)
 {
-	struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
+	struct netmap_priv *priv = NET_BE_PRIV(be);
 
 	strlcpy(priv->ifname, devname, sizeof(priv->ifname));
 	priv->ifname[sizeof(priv->ifname) - 1] = '\0';
@@ -717,7 +719,7 @@ netmap_init(struct net_backend *be, const char *devname,
 static void
 netmap_cleanup(struct net_backend *be)
 {
-	struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
+	struct netmap_priv *priv = NET_BE_PRIV(be);
 
 	if (priv->mevp) {
 		mevent_delete(priv->mevp);
@@ -732,7 +734,7 @@ static ssize_t
 netmap_send(struct net_backend *be, const struct iovec *iov,
 	    int iovcnt)
 {
-	struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
+	struct netmap_priv *priv = NET_BE_PRIV(be);
 	struct netmap_ring *ring;
 	ssize_t totlen = 0;
 	int nm_buf_size;
@@ -811,7 +813,7 @@ txsync:
 static ssize_t
 netmap_peek_recvlen(struct net_backend *be)
 {
-	struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
+	struct netmap_priv *priv = NET_BE_PRIV(be);
 	struct netmap_ring *ring = priv->rx;
 	uint32_t head = ring->head;
 	ssize_t totlen = 0;
@@ -831,7 +833,7 @@ netmap_peek_recvlen(struct net_backend *be)
 static ssize_t
 netmap_recv(struct net_backend *be, const struct iovec *iov, int iovcnt)
 {
-	struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
+	struct netmap_priv *priv = NET_BE_PRIV(be);
 	struct netmap_slot *slot = NULL;
 	struct netmap_ring *ring;
 	void *iov_frag_buf;
@@ -898,7 +900,7 @@ netmap_recv(struct net_backend *be, const struct iovec *iov, int iovcnt)
 static void
 netmap_recv_enable(struct net_backend *be)
 {
-	struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
+	struct netmap_priv *priv = NET_BE_PRIV(be);
 
 	mevent_enable(priv->mevp);
 }
@@ -906,7 +908,7 @@ netmap_recv_enable(struct net_backend *be)
 static void
 netmap_recv_disable(struct net_backend *be)
 {
-	struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
+	struct netmap_priv *priv = NET_BE_PRIV(be);
 
 	mevent_disable(priv->mevp);
 }
@@ -1021,7 +1023,7 @@ netbe_init(struct net_backend **ret, nvlist_t *nvl, net_be_rxeof_t cb,
 		return (EINVAL);
 	}
 
-	nbe = calloc(1, sizeof(*nbe) + tbe->priv_size);
+	nbe = calloc(1, NET_BE_SIZE(tbe));
 	*nbe = *tbe;	/* copy the template */
 	nbe->fd = -1;
 	nbe->sc = param;