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.133.124]) by passt.top (Postfix) with ESMTP id EBF5E5A027B for ; Wed, 14 Feb 2024 09:56:32 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707900992; 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=KmQdhE7h1saYK2s+8jGecprL9AcQd6qngpC4EsAI5qc=; b=Am5OWY1/H28ZCxuufWj2+EFr9ghtT/0o9QH6G8fX07bUKWR57g0tBmQI3tGFAiKnrRW3Iw g6aKq8CMaVs2bh9Yr/jL7ZW+r7DYmlAH9OI2ZCM9BUOpJQ4t+Z0TypEz/9odLP788RzXd7 VlRSxJtbmYswsE60NkX0whdz742NuX0= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-680-xXL3VHCvNzqzrEVkPEGXHQ-1; Wed, 14 Feb 2024 03:56:29 -0500 X-MC-Unique: xXL3VHCvNzqzrEVkPEGXHQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (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 796AA2806401 for ; Wed, 14 Feb 2024 08:56:29 +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 5FDF51121337; Wed, 14 Feb 2024 08:56:29 +0000 (UTC) From: Laurent Vivier To: passt-dev@passt.top Subject: [PATCH v2 2/8] pcap: add pcap_iov() Date: Wed, 14 Feb 2024 09:56:22 +0100 Message-ID: <20240214085628.210783-3-lvivier@redhat.com> In-Reply-To: <20240214085628.210783-1-lvivier@redhat.com> References: <20240214085628.210783-1-lvivier@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 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: S3LYIVPSXB2QU26LYWHRPLI5X5AMO4PA X-Message-ID-Hash: S3LYIVPSXB2QU26LYWHRPLI5X5AMO4PA 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: Signed-off-by: Laurent Vivier --- Notes: v2: - introduce pcap_header(), a common helper to write packet header - use writev() rather than write() in a loop - add functions comment pcap.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++------- pcap.h | 1 + 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/pcap.c b/pcap.c index 501d52d4992b..3869a403dd0f 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 the pcap file descriptor (pcap_fd). + * + * @len: Length of the packet data. + * @tv: Pointer to a timeval struct containing the timestamp for the packet. + * + * Returns; -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,35 @@ 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, unsigned int 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(), n = %u\n", n); +} + /** * pcap_init() - Initialise pcap file * @c: Execution context diff --git a/pcap.h b/pcap.h index da5a7e846b72..732a0ddf14cc 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, unsigned int n); void pcap_init(struct ctx *c); #endif /* PCAP_H */ -- 2.42.0