From: Bin Meng <bmeng.cn@gmail.com>
To: "Jason Wang" <jasowang@redhat.com>,
"Philippe Mathieu-Daudé" <philmd@redhat.com>,
"Peter Maydell" <peter.maydell@linaro.org>,
qemu-devel@nongnu.org
Cc: Bin Meng <bmeng.cn@gmail.com>
Subject: [PATCH v4 01/12] net: eth: Add a helper to pad a short Ethernet frame
Date: Tue, 16 Mar 2021 20:04:09 +0800 [thread overview]
Message-ID: <20210316120420.19658-2-bmeng.cn@gmail.com> (raw)
In-Reply-To: <20210316120420.19658-1-bmeng.cn@gmail.com>
Add a helper to pad a short Ethernet frame to the minimum required
length, which can be used by backend codes.
Signed-off-by: Bin Meng <bmeng.cn@gmail.com>
---
Changes in v4:
- change 'ethernet' to 'Ethernet'
- do not inline the helper
- check the padded buffer size to avoid buffer overflow
Changes in v3:
- use 'without' instead of 'sans'
- add a helper to pad short frames
include/net/eth.h | 17 +++++++++++++++++
net/eth.c | 17 +++++++++++++++++
2 files changed, 34 insertions(+)
diff --git a/include/net/eth.h b/include/net/eth.h
index 0671be6916..6aabbdd0d3 100644
--- a/include/net/eth.h
+++ b/include/net/eth.h
@@ -31,6 +31,7 @@
#define ETH_ALEN 6
#define ETH_HLEN 14
+#define ETH_ZLEN 60 /* Min. octets in frame without FCS */
struct eth_header {
uint8_t h_dest[ETH_ALEN]; /* destination eth addr */
@@ -422,4 +423,20 @@ bool
eth_parse_ipv6_hdr(const struct iovec *pkt, int pkt_frags,
size_t ip6hdr_off, eth_ip6_hdr_info *info);
+/**
+ * eth_pad_short_frame - pad a short frame to the minimum Ethernet frame length
+ *
+ * If the Ethernet frame size is shorter than 60 bytes, it will be padded to
+ * 60 bytes at the address @padded_pkt.
+ *
+ * @padded_pkt: buffer address to hold the padded frame
+ * @padded_buflen: buffer length of @padded_pkt. If the frame is padded, it is
+ * written to ETH_ZLEN, otherwise it remains unchanged.
+ * @pkt: address to hold the original Ethernet frame
+ * @pkt_size: size of the original Ethernet frame
+ * @return true if the frame is padded, otherwise false
+ */
+bool eth_pad_short_frame(uint8_t *padded_pkt, size_t *padded_buflen,
+ const void *pkt, size_t pkt_size);
+
#endif
diff --git a/net/eth.c b/net/eth.c
index 1e0821c5f8..f913e4396f 100644
--- a/net/eth.c
+++ b/net/eth.c
@@ -548,3 +548,20 @@ bool eth_parse_ipv6_hdr(const struct iovec *pkt, int pkt_frags,
info->l4proto = ext_hdr.ip6r_nxt;
return true;
}
+
+bool eth_pad_short_frame(uint8_t *padded_pkt, size_t *padded_buflen,
+ const void *pkt, size_t pkt_size)
+{
+ assert(padded_buflen && *padded_buflen >= ETH_ZLEN);
+
+ if (pkt_size >= ETH_ZLEN) {
+ return false;
+ }
+
+ /* pad to minimum Ethernet frame length */
+ memcpy(padded_pkt, pkt, pkt_size);
+ memset(&padded_pkt[pkt_size], 0, ETH_ZLEN - pkt_size);
+ *padded_buflen = ETH_ZLEN;
+
+ return true;
+}
--
2.25.1
next prev parent reply other threads:[~2021-03-16 12:06 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-03-16 12:04 [PATCH v4 00/12] net: Pad short frames for network backends Bin Meng
2021-03-16 12:04 ` Bin Meng [this message]
2021-03-16 15:09 ` [PATCH v4 01/12] net: eth: Add a helper to pad a short Ethernet frame Philippe Mathieu-Daudé
2021-03-17 6:22 ` Bin Meng
2021-03-16 12:04 ` [PATCH v4 02/12] net: Add a 'do_not_pad" to NetClientState Bin Meng
2021-03-16 12:04 ` [PATCH v4 03/12] net: Pad short frames to minimum size before sending from SLiRP/TAP Bin Meng
2021-03-16 15:10 ` Philippe Mathieu-Daudé
2021-03-16 12:04 ` [PATCH v4 04/12] hw/net: virtio-net: Initialize nc->do_not_pad to true Bin Meng
2021-03-16 12:04 ` [PATCH v4 05/12] hw/net: e1000: Remove the logic of padding short frames in the receive path Bin Meng
2021-03-16 12:04 ` [PATCH v4 06/12] hw/net: vmxnet3: " Bin Meng
2021-03-16 12:04 ` [PATCH v4 07/12] hw/net: i82596: " Bin Meng
2021-03-16 12:04 ` [PATCH v4 08/12] hw/net: ne2000: " Bin Meng
2021-03-16 12:04 ` [PATCH v4 09/12] hw/net: pcnet: " Bin Meng
2021-03-16 12:04 ` [PATCH v4 10/12] hw/net: rtl8139: " Bin Meng
2021-03-16 12:04 ` [PATCH v4 11/12] hw/net: sungem: " Bin Meng
2021-03-16 12:04 ` [PATCH v4 12/12] hw/net: sunhme: " Bin Meng
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=20210316120420.19658-2-bmeng.cn@gmail.com \
--to=bmeng.cn@gmail.com \
--cc=jasowang@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=philmd@redhat.com \
--cc=qemu-devel@nongnu.org \
/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).