From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by passt.top (Postfix) with ESMTP id 7E0BD5A027A for ; Sat, 17 Feb 2024 16:07:32 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708182451; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=nb572FXYncSrlBZRhw6RNBS/FRkc/zMzH99B33uLw68=; b=TTGNoTgETxtu9VJEVMqPSCpIcQKLD7E6FuewRWnE+lrXAreOs3L4IwZ2PRjGBs6TUFrJlr 7MwEOSv65jBG20tAZgBYjPlWT180qpRqi2yGO5bePtCf6niYzxfWtVzrZcVU9TXWPdwEQ5 XTnoKUW8TK+TQ5suEdzwXGH1hfiezPk= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-551-v23qFWfEOT24W5p8ks07Jg-1; Sat, 17 Feb 2024 10:07:27 -0500 X-MC-Unique: v23qFWfEOT24W5p8ks07Jg-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id ED4C788F560 for ; Sat, 17 Feb 2024 15:07:26 +0000 (UTC) Received: from virtlab218.virt.lab.eng.bos.redhat.com (virtlab218.virt.lab.eng.bos.redhat.com [10.19.152.190]) by smtp.corp.redhat.com (Postfix) with ESMTP id D6240492BE4; Sat, 17 Feb 2024 15:07:26 +0000 (UTC) From: Laurent Vivier To: passt-dev@passt.top Subject: [PATCH v3 2/9] pcap: add pcap_iov() Date: Sat, 17 Feb 2024 16:07:18 +0100 Message-ID: <20240217150725.661467-3-lvivier@redhat.com> In-Reply-To: <20240217150725.661467-1-lvivier@redhat.com> References: <20240217150725.661467-1-lvivier@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true Message-ID-Hash: R3N5AED3IKM34BPS4FEKYPQ2YUCMSFTH X-Message-ID-Hash: R3N5AED3IKM34BPS4FEKYPQ2YUCMSFTH X-MailFrom: lvivier@redhat.com 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: Laurent Vivier X-Mailman-Version: 3.3.8 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: Introduce a new function pcap_iov() to capture packet desribed by an IO vector. Move packet header writing to to pcap_header() and use it in pcap_frame() and pcap_iov(). Signed-off-by: Laurent Vivier --- Notes: v3: - update rational - update comment - use strerror(errno) - use size_t for io vector length v2: - introduce pcap_header(), a common helper to write packet header - use writev() rather than write() in a loop - add functions comment pcap.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++------- pcap.h | 1 + 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/pcap.c b/pcap.c index 501d52d4992b..4e213eea8113 100644 --- a/pcap.c +++ b/pcap.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -31,6 +32,7 @@ #include "util.h" #include "passt.h" #include "log.h" +#include "iov.h" #define PCAP_VERSION_MINOR 4 @@ -65,6 +67,28 @@ struct pcap_pkthdr { uint32_t len; }; +/* + * pcap_header() - Write a pcap packet header to pcap file + * + * @len: Length of the packet data. + * @tv: Timestamp for the packet. + * + * Return: -1 in case of error, otherwise, 0 to indicate success. + */ +static int pcap_header(size_t len, const struct timeval *tv) +{ + struct pcap_pkthdr h; + + h.tv_sec = tv->tv_sec; + h.tv_usec = tv->tv_usec; + h.caplen = h.len = len; + + if (write(pcap_fd, &h, sizeof(h)) < 0) + return -1; + + return 0; +} + /** * pcap_frame() - Capture a single frame to pcap file with given timestamp * @pkt: Pointer to data buffer, including L2 headers @@ -75,13 +99,7 @@ struct pcap_pkthdr { */ static int pcap_frame(const char *pkt, size_t len, const struct timeval *tv) { - struct pcap_pkthdr h; - - h.tv_sec = tv->tv_sec; - h.tv_usec = tv->tv_usec; - h.caplen = h.len = len; - - if (write(pcap_fd, &h, sizeof(h)) < 0 || write(pcap_fd, pkt, len) < 0) + if (pcap_header(len, tv) < 0 || write(pcap_fd, pkt, len) < 0) return -errno; return 0; @@ -130,6 +148,36 @@ void pcap_multiple(const struct iovec *iov, unsigned int n, size_t offset) } } +/* + * pcap_iov - Write packet data described by a scatter/gather I/O vector (iov) + * to a pcap file descriptor (pcap_fd). + * + * @iov: Pointer to the array of struct iovec describing the scatter/gather + * I/O vector containing packet data to write, including L2 header + * @n: Number of elements in the iov array. + */ +void pcap_iov(const struct iovec *iov, size_t n) +{ + struct timeval tv; + size_t len; + + if (pcap_fd == -1) + return; + + gettimeofday(&tv, NULL); + + len = iov_size(iov, n); + + if (pcap_header(len, &tv) < 0) { + debug("Cannot write pcap header"); + return; + } + + if (writev(pcap_fd, iov, n) < 0) + debug("Cannot log packet using writev(): %s\n", + strerror(errno)); +} + /** * pcap_init() - Initialise pcap file * @c: Execution context diff --git a/pcap.h b/pcap.h index da5a7e846b72..4950f617f4c8 100644 --- a/pcap.h +++ b/pcap.h @@ -8,6 +8,7 @@ void pcap(const char *pkt, size_t len); void pcap_multiple(const struct iovec *iov, unsigned int n, size_t offset); +void pcap_iov(const struct iovec *iov, size_t n); void pcap_init(struct ctx *c); #endif /* PCAP_H */ -- 2.42.0