From: "Michael S. Tsirkin" <mst@redhat.com>
To: "沈安琪(凛玥)" <amy.saq@antgroup.com>
Cc: netdev@vger.kernel.org, willemdebruijn.kernel@gmail.com,
davem@davemloft.net, jasowang@redhat.com,
谈鉴锋 <henry.tjf@antgroup.com>
Subject: Re: [PATCH 2/2] net/packet: send and receive pkt with given vnet_hdr_sz
Date: Thu, 9 Feb 2023 08:07:09 -0500 [thread overview]
Message-ID: <20230209080612-mutt-send-email-mst@kernel.org> (raw)
In-Reply-To: <1675946595-103034-3-git-send-email-amy.saq@antgroup.com>
On Thu, Feb 09, 2023 at 08:43:15PM +0800, 沈安琪(凛玥) wrote:
> From: "Jianfeng Tan" <henry.tjf@antgroup.com>
>
> When raw socket is used as the backend for kernel vhost, currently it
> will regard the virtio net header as 10-byte, which is not always the
> case since some virtio features need virtio net header other than
> 10-byte, such as mrg_rxbuf and VERSION_1 that both need 12-byte virtio
> net header.
>
> Instead of hardcoding virtio net header length to 10 bytes, tpacket_snd,
> tpacket_rcv, packet_snd and packet_recvmsg now get the virtio net header
> size that is recorded in packet_sock to indicate the exact virtio net
> header size that virtio user actually prepares in the packets. By doing
> so, it can fix the issue of incorrect mac header parsing when these
> virtio features that need virtio net header other than 10-byte are
> enable.
>
> Signed-off-by: Jianfeng Tan <henry.tjf@antgroup.com>
> Co-developed-by: Anqi Shen <amy.saq@antgroup.com>
> Signed-off-by: Anqi Shen <amy.saq@antgroup.com>
Does it handle VERSION_1 though? That one is also LE.
Would it be better to pass a features bitmap instead?
> ---
> net/packet/af_packet.c | 48 +++++++++++++++++++++++++++++++++---------------
> 1 file changed, 33 insertions(+), 15 deletions(-)
>
> diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
> index ab37baf..4f49939 100644
> --- a/net/packet/af_packet.c
> +++ b/net/packet/af_packet.c
> @@ -2092,18 +2092,25 @@ static unsigned int run_filter(struct sk_buff *skb,
> }
>
> static int packet_rcv_vnet(struct msghdr *msg, const struct sk_buff *skb,
> - size_t *len)
> + size_t *len, int vnet_hdr_sz)
> {
> struct virtio_net_hdr vnet_hdr;
> + int ret;
>
> - if (*len < sizeof(vnet_hdr))
> + if (*len < vnet_hdr_sz)
> return -EINVAL;
> - *len -= sizeof(vnet_hdr);
> + *len -= vnet_hdr_sz;
>
> if (virtio_net_hdr_from_skb(skb, &vnet_hdr, vio_le(), true, 0))
> return -EINVAL;
>
> - return memcpy_to_msg(msg, (void *)&vnet_hdr, sizeof(vnet_hdr));
> + ret = memcpy_to_msg(msg, (void *)&vnet_hdr, sizeof(vnet_hdr));
> +
> + /* reserve space for extra info in vnet_hdr if needed */
> + if (ret == 0)
> + iov_iter_advance(&msg->msg_iter, vnet_hdr_sz - sizeof(vnet_hdr));
> +
> + return ret;
> }
>
> /*
> @@ -2311,7 +2318,7 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
> (maclen < 16 ? 16 : maclen)) +
> po->tp_reserve;
> if (po->has_vnet_hdr) {
> - netoff += sizeof(struct virtio_net_hdr);
> + netoff += po->vnet_hdr_sz;
> do_vnet = true;
> }
> macoff = netoff - maclen;
> @@ -2552,16 +2559,23 @@ static int __packet_snd_vnet_parse(struct virtio_net_hdr *vnet_hdr, size_t len)
> }
>
> static int packet_snd_vnet_parse(struct msghdr *msg, size_t *len,
> - struct virtio_net_hdr *vnet_hdr)
> + struct virtio_net_hdr *vnet_hdr, int vnet_hdr_sz)
> {
> - if (*len < sizeof(*vnet_hdr))
> + int ret;
> +
> + if (*len < vnet_hdr_sz)
> return -EINVAL;
> - *len -= sizeof(*vnet_hdr);
> + *len -= vnet_hdr_sz;
>
> if (!copy_from_iter_full(vnet_hdr, sizeof(*vnet_hdr), &msg->msg_iter))
> return -EFAULT;
>
> - return __packet_snd_vnet_parse(vnet_hdr, *len);
> + ret = __packet_snd_vnet_parse(vnet_hdr, *len);
> +
> + /* move iter to point to the start of mac header */
> + if (ret == 0)
> + iov_iter_advance(&msg->msg_iter, vnet_hdr_sz - sizeof(struct virtio_net_hdr));
> + return ret;
> }
>
> static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
> @@ -2730,6 +2744,7 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
> int status = TP_STATUS_AVAILABLE;
> int hlen, tlen, copylen = 0;
> long timeo = 0;
> + int vnet_hdr_sz;
>
> mutex_lock(&po->pg_vec_lock);
>
> @@ -2811,8 +2826,9 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
> tlen = dev->needed_tailroom;
> if (po->has_vnet_hdr) {
> vnet_hdr = data;
> - data += sizeof(*vnet_hdr);
> - tp_len -= sizeof(*vnet_hdr);
> + vnet_hdr_sz = po->vnet_hdr_sz;
> + data += vnet_hdr_sz;
> + tp_len -= vnet_hdr_sz;
> if (tp_len < 0 ||
> __packet_snd_vnet_parse(vnet_hdr, tp_len)) {
> tp_len = -EINVAL;
> @@ -2947,6 +2963,7 @@ static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
> int offset = 0;
> struct packet_sock *po = pkt_sk(sk);
> bool has_vnet_hdr = false;
> + int vnet_hdr_sz;
> int hlen, tlen, linear;
> int extra_len = 0;
>
> @@ -2991,7 +3008,8 @@ static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
> if (sock->type == SOCK_RAW)
> reserve = dev->hard_header_len;
> if (po->has_vnet_hdr) {
> - err = packet_snd_vnet_parse(msg, &len, &vnet_hdr);
> + vnet_hdr_sz = po->vnet_hdr_sz;
> + err = packet_snd_vnet_parse(msg, &len, &vnet_hdr, vnet_hdr_sz);
> if (err)
> goto out_unlock;
> has_vnet_hdr = true;
> @@ -3068,7 +3086,7 @@ static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
> err = virtio_net_hdr_to_skb(skb, &vnet_hdr, vio_le());
> if (err)
> goto out_free;
> - len += sizeof(vnet_hdr);
> + len += vnet_hdr_sz;
> virtio_net_hdr_set_proto(skb, &vnet_hdr);
> }
>
> @@ -3452,10 +3470,10 @@ static int packet_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
> packet_rcv_try_clear_pressure(pkt_sk(sk));
>
> if (pkt_sk(sk)->has_vnet_hdr) {
> - err = packet_rcv_vnet(msg, skb, &len);
> + vnet_hdr_len = pkt_sk(sk)->vnet_hdr_sz;
> + err = packet_rcv_vnet(msg, skb, &len, vnet_hdr_len);
> if (err)
> goto out_free;
> - vnet_hdr_len = sizeof(struct virtio_net_hdr);
> }
>
> /* You lose any data beyond the buffer you gave. If it worries
> --
> 1.8.3.1
next prev parent reply other threads:[~2023-02-09 13:08 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-09 12:43 [PATCH 0/2] net/packet: support of specifying virtio net header size 沈安琪(凛玥)
2023-02-09 12:43 ` [PATCH 1/2] net/packet: add socketopt to set/get vnet_hdr_sz 沈安琪(凛玥)
2023-02-09 14:45 ` Willem de Bruijn
2023-02-10 4:00 ` 沈安琪(凛玥)
2023-02-09 12:43 ` [PATCH 2/2] net/packet: send and receive pkt with given vnet_hdr_sz 沈安琪(凛玥)
2023-02-09 13:07 ` Michael S. Tsirkin [this message]
2023-02-10 4:01 ` 沈安琪(凛玥)
2023-02-10 8:10 ` Michael S. Tsirkin
2023-02-10 15:36 ` Willem de Bruijn
2023-02-12 9:54 ` Michael S. Tsirkin
2023-02-10 15:39 ` Willem de Bruijn
2023-02-13 11:06 ` 沈安琪(凛玥)
2023-02-14 14:28 ` Willem de Bruijn
2023-02-21 9:40 ` 沈安琪(凛玥)
2023-02-21 15:03 ` Willem de Bruijn
2023-02-22 8:04 ` 沈安琪(凛玥)
2023-02-22 11:37 ` Michael S. Tsirkin
2023-02-22 11:43 ` 沈安琪(凛玥)
2023-02-22 15:04 ` Willem de Bruijn
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230209080612-mutt-send-email-mst@kernel.org \
--to=mst@redhat.com \
--cc=amy.saq@antgroup.com \
--cc=davem@davemloft.net \
--cc=henry.tjf@antgroup.com \
--cc=jasowang@redhat.com \
--cc=netdev@vger.kernel.org \
--cc=willemdebruijn.kernel@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).