For both TCP and UDP, we request vhost-user buffers that are large
enough to reach ETH_ZLEN (60 bytes), so padding is just a matter of
increasing the appropriate iov_len and clearing bytes in the buffer
as needed.
Link: https://bugs.passt.top/show_bug.cgi?id=166
Signed-off-by: Stefano Brivio
---
tcp.c | 2 --
tcp_internal.h | 1 +
tcp_vu.c | 32 ++++++++++++++++++++++++++++----
udp_vu.c | 13 ++++++++++++-
4 files changed, 41 insertions(+), 7 deletions(-)
diff --git a/tcp.c b/tcp.c
index c5486bc..8cd062f 100644
--- a/tcp.c
+++ b/tcp.c
@@ -341,8 +341,6 @@ enum {
};
#endif
-/* MSS rounding: see SET_MSS() */
-#define MSS_DEFAULT 536
#define WINDOW_DEFAULT 14600 /* RFC 6928 */
#define RTO_INIT 1 /* s, RFC 6298 */
diff --git a/tcp_internal.h b/tcp_internal.h
index 5f8fb35..d2295c9 100644
--- a/tcp_internal.h
+++ b/tcp_internal.h
@@ -12,6 +12,7 @@
#define BUF_DISCARD_SIZE (1 << 20)
#define DISCARD_IOV_NUM DIV_ROUND_UP(MAX_WINDOW, BUF_DISCARD_SIZE)
+#define MSS_DEFAULT /* and minimum */ 536 /* as it comes from minimum MTU */
#define MSS4 ROUND_DOWN(IP_MAX_MTU - \
sizeof(struct tcphdr) - \
sizeof(struct iphdr), \
diff --git a/tcp_vu.c b/tcp_vu.c
index 1c81ce3..638813c 100644
--- a/tcp_vu.c
+++ b/tcp_vu.c
@@ -60,6 +60,26 @@ static size_t tcp_vu_hdrlen(bool v6)
return hdrlen;
}
+/**
+ * tcp_vu_pad() - Pad 802.3 frame to minimum length (60 bytes) if needed
+ * @iov: iovec array storing 802.3 frame with TCP segment inside
+ * @cnt: Number of entries in @iov
+ */
+static void tcp_vu_pad(struct iovec *iov, size_t cnt)
+{
+ size_t l2len, pad;
+
+ ASSERT(iov_size(iov, cnt) >= sizeof(struct virtio_net_hdr_mrg_rxbuf));
+ l2len = iov_size(iov, cnt) - sizeof(struct virtio_net_hdr_mrg_rxbuf);
+ if (l2len >= ETH_ZLEN)
+ return;
+
+ pad = ETH_ZLEN - l2len;
+
+ memset((char *)iov[cnt - 1].iov_base + iov[cnt - 1].iov_len, 0, pad);
+ iov[cnt - 1].iov_len += pad;
+}
+
/**
* tcp_vu_send_flag() - Send segment with flags to vhost-user (no payload)
* @c: Execution context
@@ -91,12 +111,11 @@ int tcp_vu_send_flag(const struct ctx *c, struct tcp_tap_conn *conn, int flags)
vu_set_element(&flags_elem[0], NULL, &flags_iov[0]);
elem_cnt = vu_collect(vdev, vq, &flags_elem[0], 1,
- hdrlen + sizeof(struct tcp_syn_opts), NULL);
+ MAX(hdrlen + sizeof(*opts), ETH_ZLEN), NULL);
if (elem_cnt != 1)
return -1;
- ASSERT(flags_elem[0].in_sg[0].iov_len >=
- hdrlen + sizeof(struct tcp_syn_opts));
+ ASSERT(flags_elem[0].in_sg[0].iov_len >= hdrlen + sizeof(*opts));
vu_set_vnethdr(vdev, flags_elem[0].in_sg[0].iov_base, 1);
@@ -138,6 +157,8 @@ int tcp_vu_send_flag(const struct ctx *c, struct tcp_tap_conn *conn, int flags)
tcp_fill_headers(c, conn, NULL, eh, ip4h, ip6h, th, &payload,
NULL, seq, !*c->pcap);
+ tcp_vu_pad(&flags_elem[0].in_sg[0], 1);
+
if (*c->pcap) {
pcap_iov(&flags_elem[0].in_sg[0], 1,
sizeof(struct virtio_net_hdr_mrg_rxbuf));
@@ -211,7 +232,8 @@ static ssize_t tcp_vu_sock_recv(const struct ctx *c, struct vu_virtq *vq,
cnt = vu_collect(vdev, vq, &elem[elem_cnt],
VIRTQUEUE_MAX_SIZE - elem_cnt,
- MIN(mss, fillsize) + hdrlen, &frame_size);
+ MAX(MIN(mss, fillsize) + hdrlen, ETH_ZLEN),
+ &frame_size);
if (cnt == 0)
break;
@@ -456,6 +478,8 @@ int tcp_vu_data_from_sock(const struct ctx *c, struct tcp_tap_conn *conn)
tcp_vu_prepare(c, conn, iov, buf_cnt, &check, !*c->pcap, push);
+ tcp_vu_pad(iov, buf_cnt);
+
if (*c->pcap) {
pcap_iov(iov, buf_cnt,
sizeof(struct virtio_net_hdr_mrg_rxbuf));
diff --git a/udp_vu.c b/udp_vu.c
index 099677f..33dbb9a 100644
--- a/udp_vu.c
+++ b/udp_vu.c
@@ -72,8 +72,8 @@ static int udp_vu_sock_recv(const struct ctx *c, struct vu_virtq *vq, int s,
{
const struct vu_dev *vdev = c->vdev;
int iov_cnt, idx, iov_used;
+ size_t off, hdrlen, l2len;
struct msghdr msg = { 0 };
- size_t off, hdrlen;
ASSERT(!c->no_udp);
@@ -116,6 +116,17 @@ static int udp_vu_sock_recv(const struct ctx *c, struct vu_virtq *vq, int s,
iov_vu[idx].iov_len = off;
iov_used = idx + !!off;
+ /* pad 802.3 frame to 60 bytes if needed */
+ l2len = *dlen + hdrlen - sizeof(struct virtio_net_hdr_mrg_rxbuf);
+ if (l2len < ETH_ZLEN) {
+ size_t pad = ETH_ZLEN - l2len;
+
+ memset((char *)iov_vu[idx].iov_base + iov_vu[idx].iov_len,
+ 0, pad);
+
+ iov_vu[idx].iov_len += pad;
+ }
+
vu_set_vnethdr(vdev, iov_vu[0].iov_base, iov_used);
/* release unused buffers */
--
2.43.0