[8/8] pcapng: windows compatability

Message ID 20221201014440.11896-9-stephen@networkplumber.org (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series Enable building more on Windows |

Checks

Context Check Description
ci/checkpatch warning coding style issues
ci/loongarch-compilation success Compilation OK
ci/loongarch-unit-testing success Unit Testing PASS
ci/iol-broadcom-Performance fail Performance Testing issues
ci/iol-mellanox-Performance success Performance Testing PASS
ci/Intel-compilation success Compilation OK
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/github-robot: build success github build: passed
ci/intel-Testing success Testing PASS
ci/iol-aarch64-unit-testing success Testing PASS
ci/iol-aarch64-compile-testing success Testing PASS
ci/iol-abi-testing warning Testing issues
ci/iol-testing fail Testing issues
ci/iol-x86_64-unit-testing fail Testing issues
ci/iol-x86_64-compile-testing success Testing PASS

Commit Message

Stephen Hemminger Dec. 1, 2022, 1:44 a.m. UTC
  Allow building on Windows, need to provide some compatability
wrappers for writev() and if_nametoindex.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/pcapng/meson.build  |  6 -----
 lib/pcapng/rte_pcapng.c | 59 +++++++++++++++++++++++++++++++++++++++--
 2 files changed, 57 insertions(+), 8 deletions(-)
  

Comments

Tyler Retzlaff Dec. 1, 2022, 11:55 p.m. UTC | #1
On Wed, Nov 30, 2022 at 05:44:39PM -0800, Stephen Hemminger wrote:
> Allow building on Windows, need to provide some compatability
> wrappers for writev() and if_nametoindex.
> 
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> ---

Acked-by: Tyler Retzlaff <roretzla@linux.microsoft.com>
  

Patch

diff --git a/lib/pcapng/meson.build b/lib/pcapng/meson.build
index da938bbcb733..4549925d41b6 100644
--- a/lib/pcapng/meson.build
+++ b/lib/pcapng/meson.build
@@ -1,12 +1,6 @@ 
 # SPDX-License-Identifier: BSD-3-Clause
 # Copyright(c) 2019 Microsoft Corporation
 
-if is_windows
-    build = false
-    reason = 'not supported on Windows'
-    subdir_done()
-endif
-
 sources = files('rte_pcapng.c')
 headers = files('rte_pcapng.h')
 
diff --git a/lib/pcapng/rte_pcapng.c b/lib/pcapng/rte_pcapng.c
index 80d08e1a3bde..888f8b84cd97 100644
--- a/lib/pcapng/rte_pcapng.c
+++ b/lib/pcapng/rte_pcapng.c
@@ -3,15 +3,18 @@ 
  */
 
 #include <errno.h>
-#include <net/if.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sys/uio.h>
 #include <time.h>
 #include <unistd.h>
 
+#ifndef RTE_EXEC_ENV_WINDOWS
+#include <net/if.h>
+#include <sys/uio.h>
+#endif
+
 #include <bus_driver.h>
 #include <rte_common.h>
 #include <rte_cycles.h>
@@ -20,6 +23,7 @@ 
 #include <rte_ethdev.h>
 #include <rte_ether.h>
 #include <rte_mbuf.h>
+#include <rte_os_shim.h>
 #include <rte_pcapng.h>
 #include <rte_reciprocal.h>
 #include <rte_time.h>
@@ -44,6 +48,57 @@  static struct pcapng_time {
 	struct rte_reciprocal_u64 tsc_hz_inverse;
 } pcapng_time;
 
+
+#ifdef RTE_EXEC_ENV_WINDOWS
+/*
+ * Windows does not have writev() call.
+ * Emulate this by copying to a new buffer.
+ * The copy is necessary since pcapng needs to be thread-safe
+ * and do atomic write operations.
+ */
+
+#define IOV_MAX 128
+struct iovec {
+	void   *iov_base;
+	size_t  iov_len;
+};
+
+static ssize_t writev(int fd, const struct iovec *iov, int iovcnt)
+{
+	size_t bytes = 0;
+	uint8_t *ptr;
+	void *tmp_buf;
+	ssize_t ret;
+	int i;
+
+	for (i = 0; i < iovcnt; i++)
+		bytes += iov[i].iov_len;
+
+	if (unlikely(bytes == 0))
+		return 0;
+
+	tmp_buf = malloc(bytes);
+	if (unlikely(tmp_buf == NULL)) {
+		errno = ENOMEM;
+		return -1;
+	}
+
+	ptr = tmp_buf;
+	for (i = 0; i < iovcnt; i++) {
+		rte_memcpy(ptr, iov[i].iov_base, iov[i].iov_len);
+		ptr += iov[i].iov_len;
+	}
+
+	ret = write(fd, tmp_buf, bytes);
+	free(tmp_buf);
+	return ret;
+}
+
+#define IF_NAMESIZE	16
+/* compatiablity wrapper because name is optional */
+#define if_indextoname(ifindex, ifname) NULL
+#endif
+
 static inline void
 pcapng_init(void)
 {