[8/8] pcapng: windows compatability
Checks
Commit Message
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
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>
@@ -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')
@@ -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)
{