From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 975885A0272 for ; Fri, 27 Jan 2023 06:11:19 +0100 (CET) Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4P35Jd4RWFz4xHV; Fri, 27 Jan 2023 16:11:13 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1674796273; bh=UvpfR6LK4mkg+EWnJxNEv2E74HJ0BPJagVKrtEVVxlg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=F2J38yeM7hg1WNnTusmcbMkVyzWdsmFD07x0Bb2rCoLYWVUVJps1EgVTl9mJXhccf 1oz2j9zNNGdfZivHM6RleMNiOToU65Ayz+GLOCDSAKoVOOcxNe3N2XC8Fhu7mirULf 1e2vi/mXeXxmFVdYPbklhR0BSCkKYeMhya3fokCE= From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH 1/4] tap: Don't pcap frames that didn't get sent Date: Fri, 27 Jan 2023 16:11:07 +1100 Message-Id: <20230127051110.2513363-2-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230127051110.2513363-1-david@gibson.dropbear.id.au> References: <20230127051110.2513363-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: SOXOQZLIOE2XB2FU7IIQF4UTOE4RQDO4 X-Message-ID-Hash: SOXOQZLIOE2XB2FU7IIQF4UTOE4RQDO4 X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Gibson X-Mailman-Version: 3.3.3 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: In tap_send_frames() we send a number of frames to the tap device, then also write them to the pcap capture file (if configured). However the tap send can partially fail (short write()s or similar), meaning that some of the requested frames weren't actually sent, but we still write those frames to the capture file. We do give a debug message in this case, but it's misleading to add frames that we know weren't sent to the capture file. Rework to avoid this. Signed-off-by: David Gibson --- tap.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/tap.c b/tap.c index af9bc15..dd22490 100644 --- a/tap.c +++ b/tap.c @@ -309,10 +309,12 @@ void tap_icmp6_send(const struct ctx *c, * @iov: Array of buffers, each containing one frame * @n: Number of buffers/frames in @iov * + * Returns: number of frames successfully sent + * * #syscalls:pasta write */ -static void tap_send_frames_pasta(struct ctx *c, - const struct iovec *iov, size_t n) +static size_t tap_send_frames_pasta(struct ctx *c, + const struct iovec *iov, size_t n) { size_t i; @@ -324,6 +326,8 @@ static void tap_send_frames_pasta(struct ctx *c, i--; } } + + return n; } /** @@ -356,10 +360,12 @@ static void tap_send_remainder(const struct ctx *c, const struct iovec *iov, * @iov: Array of buffers, each containing one frame * @n: Number of buffers/frames in @iov * + * Returns: number of frames successfully sent + * * #syscalls:passt sendmsg */ -static void tap_send_frames_passt(const struct ctx *c, - const struct iovec *iov, size_t n) +static size_t tap_send_frames_passt(const struct ctx *c, + const struct iovec *iov, size_t n) { struct msghdr mh = { .msg_iov = (void *)iov, @@ -370,7 +376,7 @@ static void tap_send_frames_passt(const struct ctx *c, sent = sendmsg(c->fd_tap, &mh, MSG_NOSIGNAL | MSG_DONTWAIT); if (sent < 0) - return; + return 0; /* Check for any partial frames due to short send */ for (i = 0; i < n; i++) { @@ -385,8 +391,7 @@ static void tap_send_frames_passt(const struct ctx *c, i++; } - if (i < n) - debug("tap: dropped %lu frames due to short send", n - i); + return i; } /** @@ -397,15 +402,20 @@ static void tap_send_frames_passt(const struct ctx *c, */ void tap_send_frames(struct ctx *c, const struct iovec *iov, size_t n) { + size_t m; + if (!n) return; if (c->mode == MODE_PASST) - tap_send_frames_passt(c, iov, n); + m = tap_send_frames_passt(c, iov, n); else - tap_send_frames_pasta(c, iov, n); + m = tap_send_frames_pasta(c, iov, n); + + if (m < n) + debug("tap: dropped %lu frames of %lu due to short send", n - m, n); - pcap_multiple(iov, n, c->mode == MODE_PASST ? sizeof(uint32_t) : 0); + pcap_multiple(iov, m, c->mode == MODE_PASST ? sizeof(uint32_t) : 0); } /** -- 2.39.1