DPDK patches and discussions
 help / color / mirror / Atom feed
From: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
To: dev@dpdk.org
Cc: Tyler Retzlaff <roretzla@microsoft.com>,
	Mike Wells <mike.wells@telchemy.com>,
	Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>,
	Ferruh Yigit <ferruh.yigit@intel.com>
Subject: [dpdk-dev] [PATCH v2 6/6] net/pcap: build on Windows
Date: Sun, 14 Feb 2021 05:16:16 +0300	[thread overview]
Message-ID: <20210214021616.26970-7-dmitry.kozliuk@gmail.com> (raw)
In-Reply-To: <20210214021616.26970-1-dmitry.kozliuk@gmail.com>

Implement OS-dependent functions and enable PMD build on Windows.

Signed-off-by: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
---
 drivers/net/pcap/meson.build          |  11 ++-
 drivers/net/pcap/pcap_ethdev.c        |   4 -
 drivers/net/pcap/pcap_osdep.h         |   5 ++
 drivers/net/pcap/pcap_osdep_windows.c | 118 ++++++++++++++++++++++++++
 4 files changed, 128 insertions(+), 10 deletions(-)
 create mode 100644 drivers/net/pcap/pcap_osdep_windows.c

diff --git a/drivers/net/pcap/meson.build b/drivers/net/pcap/meson.build
index 9ab95ec3e..614a4e340 100644
--- a/drivers/net/pcap/meson.build
+++ b/drivers/net/pcap/meson.build
@@ -1,19 +1,18 @@
 # SPDX-License-Identifier: BSD-3-Clause
 # Copyright(c) 2017 Intel Corporation
 
-if is_windows
-	build = false
-	reason = 'not supported on Windows'
-	subdir_done()
-endif
-
 if not dpdk_conf.has('RTE_PORT_PCAP')
 	build = false
 	reason = 'missing dependency, "libpcap"'
 endif
+
 sources = files(
 	'pcap_ethdev.c',
 	'pcap_osdep.c',
 	'pcap_osdep_@0@.c'.format(exec_env),
 )
+
 ext_deps += pcap_dep
+if is_windows
+	ext_deps += cc.find_library('iphlpapi', required: true)
+endif
diff --git a/drivers/net/pcap/pcap_ethdev.c b/drivers/net/pcap/pcap_ethdev.c
index 6461213e0..142533c6e 100644
--- a/drivers/net/pcap/pcap_ethdev.c
+++ b/drivers/net/pcap/pcap_ethdev.c
@@ -127,10 +127,6 @@ static struct rte_eth_link pmd_link = {
 
 RTE_LOG_REGISTER(eth_pcap_logtype, pmd.net.pcap, NOTICE);
 
-#define PMD_LOG(level, fmt, args...) \
-	rte_log(RTE_LOG_ ## level, eth_pcap_logtype, \
-		"%s(): " fmt "\n", __func__, ##args)
-
 static int
 eth_pcap_rx_jumbo(struct rte_mempool *mb_pool, struct rte_mbuf *mbuf,
 		const uint8_t *data, uint16_t data_len)
diff --git a/drivers/net/pcap/pcap_osdep.h b/drivers/net/pcap/pcap_osdep.h
index bd00b728a..f7c311ef8 100644
--- a/drivers/net/pcap/pcap_osdep.h
+++ b/drivers/net/pcap/pcap_osdep.h
@@ -8,6 +8,11 @@
 #include <rte_ether.h>
 #include <rte_time.h>
 
+#define PMD_LOG(level, fmt, args...) \
+	rte_log(RTE_LOG_ ## level, eth_pcap_logtype, \
+		"%s(): " fmt "\n", __func__, ##args)
+extern int eth_pcap_logtype;
+
 /*
  * Interface manipulation is always OS-specific.
  */
diff --git a/drivers/net/pcap/pcap_osdep_windows.c b/drivers/net/pcap/pcap_osdep_windows.c
new file mode 100644
index 000000000..d6557ff73
--- /dev/null
+++ b/drivers/net/pcap/pcap_osdep_windows.c
@@ -0,0 +1,118 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright (c) 2021 Dmitry Kozlyuk
+ */
+
+#include <winsock2.h>
+#include <iphlpapi.h>
+#include <strsafe.h>
+
+#include "pcap_osdep.h"
+
+/*
+ * Given a device name like "\Device\NPF_{GUID}" extract the "{GUID}" part.
+ * Return NULL if "{GUID}" part is not found.
+ */
+static const char *
+iface_guid(const char *name)
+{
+	static const size_t GUID_LENGTH = 32 + 4; /* 16 hex bytes + 4 dashes */
+
+	const char *ob, *cb;
+
+	ob = strchr(name, '{');
+	if (ob == NULL)
+		return NULL;
+
+	cb = strchr(ob, '}');
+	if (cb == NULL || cb - ob != GUID_LENGTH + 1) /* + 1 opening '{' */
+		return NULL;
+
+	return ob;
+}
+
+/*
+ * libpcap takes device names like "\Device\NPF_{GUID}",
+ * GetAdapterIndex() takes interface names like "\DEVICE\TCPIP_{GUID}".
+ * Try to convert, fall back to original device name.
+ */
+int
+osdep_iface_index_get(const char *device_name)
+{
+	WCHAR adapter_name[MAX_ADAPTER_NAME_LENGTH];
+	const char *guid;
+	ULONG index;
+	DWORD ret;
+
+	guid = iface_guid(device_name);
+	if (guid != NULL)
+		StringCbPrintfW(adapter_name, sizeof(adapter_name),
+			L"\\DEVICE\\TCPIP_%s", guid);
+	else
+		StringCbPrintfW(adapter_name, sizeof(adapter_name),
+			L"%s", device_name);
+
+	ret = GetAdapterIndex(adapter_name, &index);
+	if (ret != NO_ERROR) {
+		PMD_LOG(ERR, "GetAdapterIndex() = %lu\n", ret);
+		return -1;
+	}
+
+	return index;
+}
+
+/*
+ * libpcap takes device names like "\Device\NPF_{GUID}",
+ * GetAdaptersAddresses() returns names in "{GUID}" form.
+ * Try to extract GUID from device name, fall back to original device name.
+ */
+int
+osdep_iface_mac_get(const char *device_name, struct rte_ether_addr *mac)
+{
+	IP_ADAPTER_ADDRESSES *info = NULL, *cur = NULL;
+	ULONG size, sys_ret;
+	const char *adapter_name;
+	int ret = -1;
+
+	sys_ret = GetAdaptersAddresses(AF_UNSPEC, 0, NULL, NULL, &size);
+	if (sys_ret != ERROR_BUFFER_OVERFLOW) {
+		PMD_LOG(ERR, "GetAdapterAddresses() = %lu, expected %lu\n",
+			sys_ret, ERROR_BUFFER_OVERFLOW);
+		return -1;
+	}
+
+	info = (IP_ADAPTER_ADDRESSES *)malloc(size);
+	if (info == NULL) {
+		PMD_LOG(ERR, "Cannot allocate adapter address info\n");
+		return -1;
+	}
+
+	sys_ret = GetAdaptersAddresses(AF_UNSPEC, 0, NULL, info, &size);
+	if (sys_ret != ERROR_SUCCESS) {
+		PMD_LOG(ERR, "GetAdapterAddresses() = %lu\n", sys_ret);
+		free(info);
+		return -1;
+	}
+
+	adapter_name = iface_guid(device_name);
+	if (adapter_name == NULL)
+		adapter_name = device_name;
+
+	for (cur = info; cur != NULL; cur = cur->Next) {
+		if (strcmp(cur->AdapterName, adapter_name) == 0) {
+			if (cur->PhysicalAddressLength != RTE_ETHER_ADDR_LEN) {
+				PMD_LOG(ERR, "Physical address length: want %u, got %lu",
+					RTE_ETHER_ADDR_LEN,
+					cur->PhysicalAddressLength);
+				return -1;
+			}
+
+			memcpy(mac->addr_bytes, cur->PhysicalAddress,
+				RTE_ETHER_ADDR_LEN);
+			ret = 0;
+			break;
+		}
+	}
+
+	free(info);
+	return ret;
+}
-- 
2.29.2


  parent reply	other threads:[~2021-02-14  2:17 UTC|newest]

Thread overview: 67+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-14  1:20 [dpdk-dev] [PATCH 0/6] " Dmitry Kozlyuk
2021-02-14  1:20 ` [dpdk-dev] [PATCH 1/6] eal: add internal API for current time Dmitry Kozlyuk
2021-03-01 22:31   ` Nick Connolly
2021-03-01 22:36     ` Nick Connolly
2021-02-14  1:20 ` [dpdk-dev] [PATCH 2/6] net/pcap: fix format string Dmitry Kozlyuk
2021-03-01 22:33   ` Nick Connolly
2021-02-14  1:20 ` [dpdk-dev] [PATCH 3/6] net/pcap: move OS-dependent code to separate files Dmitry Kozlyuk
2021-02-14  1:20 ` [dpdk-dev] [PATCH 4/6] net/pcap: add libpcap wrappers Dmitry Kozlyuk
2021-02-14  1:20 ` [dpdk-dev] [PATCH 5/6] config: discover libpcap on Windows Dmitry Kozlyuk
2021-02-14  1:20 ` [dpdk-dev] [PATCH 6/6] net/pcap: build " Dmitry Kozlyuk
2021-02-14  2:16 ` [dpdk-dev] [PATCH v2 0/6] " Dmitry Kozlyuk
2021-02-14  2:16   ` [dpdk-dev] [PATCH v2 1/6] eal: add internal API for current time Dmitry Kozlyuk
2021-03-01 22:39     ` Nick Connolly
2021-03-05 17:50     ` Jie Zhou
2021-03-16  9:18     ` Thomas Monjalon
2021-03-16 18:59     ` Stephen Hemminger
2021-03-16 20:07       ` Dmitry Kozlyuk
2021-03-17  9:50         ` Morten Brørup
2021-02-14  2:16   ` [dpdk-dev] [PATCH v2 2/6] net/pcap: fix format string Dmitry Kozlyuk
2021-02-25 14:45     ` Ferruh Yigit
2021-03-02 11:48       ` [dpdk-dev] [dpdk-stable] " Ferruh Yigit
2021-02-14  2:16   ` [dpdk-dev] [PATCH v2 3/6] net/pcap: move OS-dependent code to separate files Dmitry Kozlyuk
2021-02-25 14:51     ` Ferruh Yigit
2021-02-25 16:05       ` Dmitry Kozlyuk
2021-02-14  2:16   ` [dpdk-dev] [PATCH v2 4/6] net/pcap: add libpcap wrappers Dmitry Kozlyuk
2021-02-25 14:59     ` Ferruh Yigit
2021-02-25 19:04       ` Dmitry Kozlyuk
2021-02-25 20:31         ` Nick Connolly
2021-02-25 23:10           ` Dmitry Kozlyuk
2021-03-01 21:43             ` Nick Connolly
2021-03-01 23:05               ` Dmitry Kozlyuk
2021-03-01 23:23                 ` Dmitry Kozlyuk
2021-03-02 11:22                 ` Nick Connolly
2021-03-03 16:32                   ` Dmitry Kozlyuk
2021-03-03 16:47                     ` Ferruh Yigit
2021-03-03 18:19                       ` Dmitry Kozlyuk
2021-03-03 19:30                         ` Ferruh Yigit
2021-03-03 23:03                           ` Dmitry Kozlyuk
2021-02-14  2:16   ` [dpdk-dev] [PATCH v2 5/6] config: discover libpcap on Windows Dmitry Kozlyuk
2021-02-25 15:02     ` Ferruh Yigit
2021-02-25 16:04       ` Dmitry Kozlyuk
2021-02-25 16:33         ` Bruce Richardson
2021-02-25 17:42           ` Dmitry Kozlyuk
2021-03-16  9:16             ` Thomas Monjalon
2021-03-16  9:37               ` Dmitry Kozlyuk
2021-02-14  2:16   ` Dmitry Kozlyuk [this message]
2021-03-24  0:50   ` [dpdk-dev] [PATCH v3 0/3] net/pcap: build " Dmitry Kozlyuk
2021-03-24  0:50     ` [dpdk-dev] [PATCH v3 1/3] eal/windows: add timespec_get shim for MinGW Dmitry Kozlyuk
2021-03-24  0:50     ` [dpdk-dev] [PATCH v3 2/3] net/pcap: move OS-dependent code to separate files Dmitry Kozlyuk
2021-03-24  0:50     ` [dpdk-dev] [PATCH v3 3/3] net/pcap: build on Windows Dmitry Kozlyuk
2021-04-09 10:51     ` [dpdk-dev] [PATCH v3 0/3] " Ferruh Yigit
2021-04-09 11:03       ` Dmitry Kozlyuk
2021-04-09 11:24         ` Ferruh Yigit
2021-04-15 22:10     ` [dpdk-dev] [PATCH v4 " Dmitry Kozlyuk
2021-04-15 22:10       ` [dpdk-dev] [PATCH v4 1/3] eal/windows: add timespec_get shim for MinGW Dmitry Kozlyuk
2021-04-15 22:10       ` [dpdk-dev] [PATCH v4 2/3] net/pcap: move OS-dependent code to separate files Dmitry Kozlyuk
2021-04-15 22:10       ` [dpdk-dev] [PATCH v4 3/3] net/pcap: build on Windows Dmitry Kozlyuk
2021-04-19 21:05         ` Tyler Retzlaff
2021-04-16 17:22       ` [dpdk-dev] [PATCH v4 0/3] " Ferruh Yigit
2021-04-20 22:20       ` Thomas Monjalon
2021-04-21 14:53         ` Dmitry Kozlyuk
2021-04-21 18:12           ` Thomas Monjalon
2021-04-21 19:33       ` [dpdk-dev] [PATCH v5 " Dmitry Kozlyuk
2021-04-21 19:33         ` [dpdk-dev] [PATCH v5 1/3] eal: add timespec_get shim Dmitry Kozlyuk
2021-04-21 19:33         ` [dpdk-dev] [PATCH v5 2/3] net/pcap: move OS-dependent code to separate files Dmitry Kozlyuk
2021-04-21 19:33         ` [dpdk-dev] [PATCH v5 3/3] net/pcap: build on Windows Dmitry Kozlyuk
2021-04-21 21:54         ` [dpdk-dev] [PATCH v5 0/3] " 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=20210214021616.26970-7-dmitry.kozliuk@gmail.com \
    --to=dmitry.kozliuk@gmail.com \
    --cc=dev@dpdk.org \
    --cc=ferruh.yigit@intel.com \
    --cc=mike.wells@telchemy.com \
    --cc=roretzla@microsoft.com \
    /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).