From: "Mário Kuka" <kuka@cesnet.cz>
To: kuka@cesnet.cz
Cc: dev@dpdk.org, mdr@ashroe.eu, reshma.pattan@intel.com,
stephen@networkplumber.org
Subject: [PATCH v2 2/2] pcapng: check if writev() returns a partial write
Date: Fri, 29 Jul 2022 09:18:41 +0200 [thread overview]
Message-ID: <20220729071841.18198-3-kuka@cesnet.cz> (raw)
In-Reply-To: <20220729071841.18198-1-kuka@cesnet.cz>
The result from wrtiev() is not checked. When writev() returns
a partial write, the output file will not contain all packets from the
pkts buffer and some packets may be partially written, which is
undesirable behavior.
To avoid this problem, we have to check the number of bytes returned
from the writev(), and if we get a partial write, we need to call the
writev() function again on any ivo buffers that were not written or
were written partially.
Fixes: 8d23ce8f5ee9 ("pcapng: add new library for writing pcapng files")
Cc: stephen@networkplumber.org
Signed-off-by: Mário Kuka <kuka@cesnet.cz>
---
lib/pcapng/rte_pcapng.c | 67 +++++++++++++++++++++++++++++++++++++++--
1 file changed, 65 insertions(+), 2 deletions(-)
diff --git a/lib/pcapng/rte_pcapng.c b/lib/pcapng/rte_pcapng.c
index e41cf909e1..7c1136337c 100644
--- a/lib/pcapng/rte_pcapng.c
+++ b/lib/pcapng/rte_pcapng.c
@@ -551,6 +551,69 @@ rte_pcapng_copy(uint16_t port_id, uint32_t queue,
return NULL;
}
+/*
+ * Update iov after writev() has returned written. We must find how many iov
+ * buffers (from beginning) have been written. The first buffer that was not
+ * written fully is to be updated accordingly.
+ *
+ * Returns offset of buffer that was not written fully.
+ */
+static int
+pcapng_update_iov(struct iovec *iov, const int count, size_t written)
+{
+ int i;
+
+ for (i = 0; written > 0 && i < count; ++i) {
+ if (written < iov[i].iov_len) {
+ /* found buffer that was not written fully */
+ iov[i].iov_base = RTE_PTR_ADD(iov[i].iov_base, written);
+ iov[i].iov_len -= written;
+
+ return i;
+ }
+
+ written -= iov[i].iov_len;
+ }
+
+ return count;
+}
+
+/*
+ * Writes all iovcnt buffers of data described by iov to the file associated with
+ * the file descriptor fd.
+ */
+static ssize_t
+pcapng_writev(int fd, struct iovec *iov, const int count)
+{
+ size_t total = 0;
+ int at = 0;
+
+ while (at < count) {
+ /*
+ * Note: writev() can return the following on a write request:
+ * Complete:
+ * written = [sum of all iov.iov_len]
+ * Partial:
+ * written < [sum of all iov.iov_len]
+ * Deferred:
+ * written = -1, errno = [EAGAIN]
+ *
+ * Partial and deferred writes are only possible with O_NONBLOCK set.
+ *
+ * If we get a partial result, we have to call the writev() again on any ivo buffers
+ * that have not been fully written.
+ */
+ ssize_t written = writev(fd, &iov[at], count - at);
+ if (unlikely(written < 0))
+ return written;
+
+ total += written;
+ at += pcapng_update_iov(&iov[at], count - at, written);
+ }
+
+ return total;
+}
+
/* Write pre-formatted packets to file. */
ssize_t
rte_pcapng_write_packets(rte_pcapng_t *self,
@@ -577,7 +640,7 @@ rte_pcapng_write_packets(rte_pcapng_t *self,
* Note: this assumes that max segments per mbuf < IOV_MAX
*/
if (unlikely(cnt + m->nb_segs >= IOV_MAX)) {
- ret = writev(self->outfd, iov, cnt);
+ ret = pcapng_writev(self->outfd, iov, cnt);
if (unlikely(ret < 0)) {
rte_errno = errno;
return -1;
@@ -598,7 +661,7 @@ rte_pcapng_write_packets(rte_pcapng_t *self,
} while ((m = m->next));
}
- ret = writev(self->outfd, iov, cnt);
+ ret = pcapng_writev(self->outfd, iov, cnt);
if (unlikely(ret < 0)) {
rte_errno = errno;
return -1;
--
2.31.1
next prev parent reply other threads:[~2022-07-29 16:03 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-25 15:28 [PATCH] pcapng: fix write more packets than IOV_MAX limit Mário Kuka
2022-07-25 15:57 ` Stephen Hemminger
2022-07-25 16:10 ` Stephen Hemminger
2022-07-29 7:18 ` [PATCH v2 0/2] pcapng: fix some issues with writing packets Mário Kuka
2022-07-29 7:18 ` [PATCH v2 1/2] pcapng: fix write more packets than IOV_MAX limit Mário Kuka
2022-07-29 7:18 ` Mário Kuka [this message]
2022-07-29 16:00 ` [PATCH v2 2/2] pcapng: check if writev() returns a partial write Stephen Hemminger
2022-07-29 17:08 ` Mário Kuka
2022-07-29 18:14 ` Stephen Hemminger
2022-08-01 8:42 ` Mário Kuka
2022-07-29 15:58 ` [PATCH v2 0/2] pcapng: fix some issues with writing packets Stephen Hemminger
2022-07-29 17:33 ` Mário Kuka
2022-08-01 8:40 ` [PATCH v3] pcapng: fix write more packets than IOV_MAX limit Mário Kuka
2022-08-01 15:33 ` Stephen Hemminger
2022-10-10 0:40 ` Thomas Monjalon
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=20220729071841.18198-3-kuka@cesnet.cz \
--to=kuka@cesnet.cz \
--cc=dev@dpdk.org \
--cc=mdr@ashroe.eu \
--cc=reshma.pattan@intel.com \
--cc=stephen@networkplumber.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).