diff mbox

[NETMAP,1/2] linux-netmap: implement odp_pktio_mac_addr

Message ID 1427479000-25779-2-git-send-email-ciprian.barbu@linaro.org
State New
Headers show

Commit Message

Ciprian Barbu March 27, 2015, 5:56 p.m. UTC
Signed-off-by: Ciprian Barbu <ciprian.barbu@linaro.org>
---
 platform/linux-netmap/include/odp_packet_netmap.h |  6 +++
 platform/linux-netmap/odp_packet_io.c             |  4 ++
 platform/linux-netmap/odp_packet_netmap.c         | 51 +++++++++++++++++++++++
 3 files changed, 61 insertions(+)
diff mbox

Patch

diff --git a/platform/linux-netmap/include/odp_packet_netmap.h b/platform/linux-netmap/include/odp_packet_netmap.h
index 24d7923..4b75a6d 100644
--- a/platform/linux-netmap/include/odp_packet_netmap.h
+++ b/platform/linux-netmap/include/odp_packet_netmap.h
@@ -54,4 +54,10 @@  int recv_pkt_netmap(pkt_netmap_t * const pkt_nm, odp_packet_t pkt_table[],
  */
 int send_pkt_netmap(pkt_netmap_t * const pkt_nm, odp_packet_t pkt_table[],
 		    unsigned len);
+
+/**
+ * Get mac address
+ */
+void pkt_netmap_mac_addr(pkt_netmap_t * const pkt_nm, void *mac_addr,
+			int addr_size);
 #endif
diff --git a/platform/linux-netmap/odp_packet_io.c b/platform/linux-netmap/odp_packet_io.c
index 554d38e..1957ad0 100644
--- a/platform/linux-netmap/odp_packet_io.c
+++ b/platform/linux-netmap/odp_packet_io.c
@@ -864,6 +864,10 @@  int odp_pktio_mac_addr(odp_pktio_t id, void *mac_addr, int addr_size)
 	}
 
 	switch (entry->s.type) {
+	case ODP_PKTIO_TYPE_NETMAP:
+		pkt_netmap_mac_addr(&entry->s.pkt_nm,
+				    mac_addr, addr_size);
+		break;
 	case ODP_PKTIO_TYPE_SOCKET_BASIC:
 	case ODP_PKTIO_TYPE_SOCKET_MMSG:
 		memcpy(mac_addr, entry->s.pkt_sock.if_mac,
diff --git a/platform/linux-netmap/odp_packet_netmap.c b/platform/linux-netmap/odp_packet_netmap.c
index fb64db2..11954a2 100644
--- a/platform/linux-netmap/odp_packet_netmap.c
+++ b/platform/linux-netmap/odp_packet_netmap.c
@@ -23,6 +23,7 @@ 
 #include <fcntl.h>
 #include <string.h>
 #include <stdlib.h>
+#include <arpa/inet.h>
 
 #include <linux/ethtool.h>
 #include <linux/sockios.h>
@@ -47,6 +48,7 @@ 
  */
 typedef struct netmap_dev_t {
 	char ifname[IFNAMSIZ];
+	unsigned char if_mac[ETH_ALEN];
 	odp_ticketlock_t rx_lock;
 	odp_ticketlock_t tx_lock;
 } netmap_dev_t;
@@ -141,6 +143,41 @@  done:
 	return error;
 }
 
+static void ethaddr_copy(unsigned char mac_dst[], unsigned char mac_src[])
+{
+	memcpy(mac_dst, mac_src, ETH_ALEN);
+}
+
+static int get_mac_addr(pkt_netmap_t *pkt_nm)
+{
+	struct ifreq ethreq;
+	int sockfd;
+	int err;
+
+	sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
+	if (sockfd == -1) {
+		ODP_ERR("socket(): %s\n", strerror(errno));
+		goto error;
+	}
+
+	memset(&ethreq, 0, sizeof(ethreq));
+	snprintf(ethreq.ifr_name, IFNAMSIZ, "%s", pkt_nm->nm_dev->ifname);
+	err = ioctl(sockfd, SIOCGIFHWADDR, &ethreq);
+	if (err != 0) {
+		ODP_ERR("ioctl(SIOCGIFHWADDR): %s\n", strerror(errno));
+		goto error;
+	}
+	ethaddr_copy(pkt_nm->nm_dev->if_mac,
+		     (unsigned char *)ethreq.ifr_ifru.ifru_hwaddr.sa_data);
+
+	return 0;
+
+error:
+	__odp_errno = errno;
+
+	return -1;
+}
+
 int setup_pkt_netmap(pkt_netmap_t * const pkt_nm, const char *netdev,
 		     odp_pool_t pool)
 {
@@ -240,6 +277,11 @@  int setup_pkt_netmap(pkt_netmap_t * const pkt_nm, const char *netdev,
 	/*
 	   nm_do_ioctl(pkt_nm, SIOCETHTOOL, ETHTOOL_SRXCSUM);
 	 */
+	ret = get_mac_addr(pkt_nm);
+	if (ret)
+		ODP_ERR("[%04d] Failed to get MAC addr\n",
+			odp_thread_id());
+
 	ret = nm_do_ioctl(pkt_nm, SIOCETHTOOL, ETHTOOL_STXCSUM);
 	if (ret)
 		ODP_DBG("[%04d] ETHTOOL_STXCSUM not supported\n",
@@ -366,3 +408,12 @@  out:
 
 	return nb_tx;
 }
+
+inline void pkt_netmap_mac_addr(pkt_netmap_t * const pkt_nm, void *mac_addr,
+			 int addr_size)
+{
+	if (addr_size < ETH_ALEN)
+		return;
+
+	memcpy(mac_addr, pkt_nm->nm_dev->if_mac, ETH_ALEN);
+}