diff mbox

[PATCHv2,2/5] linux-generic: pktio: support using netmap: prefix

Message ID 1453488486-12176-3-git-send-email-stuart.haslam@linaro.org
State New
Headers show

Commit Message

Stuart Haslam Jan. 22, 2016, 6:48 p.m. UTC
The netmap API requires interface names to be prefixed with "netmap:",
the pktio code will do that automatically, e.g. odp_pktio_open("eth0")
becomes nm_open("netmap:eth0"). But it will append the netmap: even if
the original name already has that prefix, so odp_pktio_open("netmap:eth0")
fails.

It's useful to be able to explicitly use the netmap interface, especially
when testing, if you don't want fallback to other pktio types.

Signed-off-by: Stuart Haslam <stuart.haslam@linaro.org>
---
 platform/linux-generic/include/odp_packet_netmap.h |  2 ++
 platform/linux-generic/pktio/netmap.c              | 26 +++++++++++++++-------
 2 files changed, 20 insertions(+), 8 deletions(-)
diff mbox

Patch

diff --git a/platform/linux-generic/include/odp_packet_netmap.h b/platform/linux-generic/include/odp_packet_netmap.h
index 0577dfe..7caa92f 100644
--- a/platform/linux-generic/include/odp_packet_netmap.h
+++ b/platform/linux-generic/include/odp_packet_netmap.h
@@ -10,6 +10,7 @@ 
 #include <odp/pool.h>
 
 #include <linux/if_ether.h>
+#include <net/if.h>
 
 /** Packet socket using netmap mmaped rings for both Rx and Tx */
 typedef struct {
@@ -20,6 +21,7 @@  typedef struct {
 	uint32_t if_flags;		/**< interface flags */
 	int sockfd;			/**< control socket */
 	unsigned char if_mac[ETH_ALEN]; /**< eth mac address */
+	char ifname[IF_NAMESIZE];	/**< interface name to be used in ioctl */
 } pkt_netmap_t;
 
 #endif
diff --git a/platform/linux-generic/pktio/netmap.c b/platform/linux-generic/pktio/netmap.c
index 774e813..f15ffff 100644
--- a/platform/linux-generic/pktio/netmap.c
+++ b/platform/linux-generic/pktio/netmap.c
@@ -42,8 +42,7 @@  static int netmap_do_ioctl(pktio_entry_t *pktio_entry, unsigned long cmd,
 	int fd = pkt_nm->sockfd;
 
 	memset(&ifr, 0, sizeof(ifr));
-	snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s",
-		 pktio_entry->s.name);
+	snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", pkt_nm->ifname);
 
 	switch (cmd) {
 	case SIOCSIFFLAGS:
@@ -102,7 +101,8 @@  static int netmap_close(pktio_entry_t *pktio_entry)
 static int netmap_open(odp_pktio_t id ODP_UNUSED, pktio_entry_t *pktio_entry,
 		       const char *netdev, odp_pool_t pool)
 {
-	char ifname[IFNAMSIZ + 7]; /* netmap:<ifname> */
+	char nmname[IF_NAMESIZE + 7]; /* netmap:<ifname> */
+	const char *prefix;
 	int err;
 	int sockfd;
 	int i;
@@ -124,20 +124,30 @@  static int netmap_open(odp_pktio_t id ODP_UNUSED, pktio_entry_t *pktio_entry,
 		odp_buffer_pool_headroom(pool) -
 		odp_buffer_pool_tailroom(pool);
 
+	/* allow interface to be opened with or without the netmap: prefix */
 	snprintf(pktio_entry->s.name, sizeof(pktio_entry->s.name), "%s",
 		 netdev);
-	snprintf(ifname, sizeof(ifname), "netmap:%s", netdev);
+
+	if (strncmp(netdev, "netmap:", 7) == 0) {
+		netdev += 7;
+		prefix = "";
+	} else {
+		prefix = "netmap:";
+	}
+
+	snprintf(pkt_nm->ifname, sizeof(pkt_nm->ifname), "%s", netdev);
+	snprintf(nmname, sizeof(nmname), "%s%s", prefix, pktio_entry->s.name);
 
 	if (mmap_desc.mem == NULL)
-		pkt_nm->rx_desc = nm_open(ifname, NULL, NETMAP_NO_TX_POLL,
+		pkt_nm->rx_desc = nm_open(nmname, NULL, NETMAP_NO_TX_POLL,
 					  NULL);
 	else
-		pkt_nm->rx_desc = nm_open(ifname, NULL, NETMAP_NO_TX_POLL |
+		pkt_nm->rx_desc = nm_open(nmname, NULL, NETMAP_NO_TX_POLL |
 					  NM_OPEN_NO_MMAP, &mmap_desc);
-	pkt_nm->tx_desc = nm_open(ifname, NULL, NM_OPEN_NO_MMAP, &mmap_desc);
+	pkt_nm->tx_desc = nm_open(nmname, NULL, NM_OPEN_NO_MMAP, &mmap_desc);
 
 	if (pkt_nm->rx_desc == NULL || pkt_nm->tx_desc == NULL) {
-		ODP_ERR("nm_open(%s) failed\n", ifname);
+		ODP_ERR("nm_open(%s) failed\n", nmname);
 		goto error;
 	}