diff mbox

[1/2] pktio: mac addr functions

Message ID 1416924328-17663-1-git-send-email-maxim.uvarov@linaro.org
State New
Headers show

Commit Message

Maxim Uvarov Nov. 25, 2014, 2:05 p.m. UTC
Signed-off-by: Maxim Uvarov <maxim.uvarov@linaro.org>
---
 Implement mac function according to pktio log:

 https://docs.google.com/document/d/1EHU54p3Kaa-hGUG-W7xquDfQb3aSWF3fEtwmcv5Bts8/edit#heading=h.b8vg8t31zjwt

 platform/linux-generic/include/api/odp_packet_io.h | 24 ++++++++
 platform/linux-generic/odp_packet_io.c             | 66 ++++++++++++++++++++++
 2 files changed, 90 insertions(+)
diff mbox

Patch

diff --git a/platform/linux-generic/include/api/odp_packet_io.h b/platform/linux-generic/include/api/odp_packet_io.h
index c9b0889..d6a6067 100644
--- a/platform/linux-generic/include/api/odp_packet_io.h
+++ b/platform/linux-generic/include/api/odp_packet_io.h
@@ -183,6 +183,30 @@  int odp_pktio_promisc_disable(odp_pktio_t id);
 int odp_pktio_promisc_get_enabled(odp_pktio_t id);
 
 /**
+ * Set the default MAC address of a packet IO interface.
+ *
+ * @param[in] id         ODP packet IO handle.
+ * @param[in] mac_addr   MAC address to be assigned to the interface.
+ * @param[in] addr_size  Size of the address in bytes.
+ *
+ * @return 0 on success, -1 on error.
+ */
+int odp_pktio_set_mac_addr(odp_pktio_t id, const unsigned char *mac_addr,
+			   size_t addr_size);
+
+/**
+ * Get the default MAC address of a packet IO interface.
+ *
+ * @param[in]  id        ODP packet IO handle.
+ * @param[out] mac_addr  Storage for MAC address of the packet IO interface.
+ * @param[out] addr_size Size of the MAC address.
+ *
+ * @return 0 on success, -1 on error.
+ */
+int odp_pktio_get_mac_addr(odp_pktio_t id, unsigned char *mac_addr,
+			   size_t *addr_size);
+
+/**
  * @}
  */
 
diff --git a/platform/linux-generic/odp_packet_io.c b/platform/linux-generic/odp_packet_io.c
index 937f7f0..909cd32 100644
--- a/platform/linux-generic/odp_packet_io.c
+++ b/platform/linux-generic/odp_packet_io.c
@@ -21,6 +21,7 @@ 
 
 #include <string.h>
 #include <sys/ioctl.h>
+#include <linux/if_arp.h>
 
 typedef struct {
 	pktio_entry_t entries[ODP_CONFIG_PKTIO_ENTRIES];
@@ -650,3 +651,68 @@  int odp_pktio_promisc_get_enabled(odp_pktio_t id)
 	else
 		return 0;
 }
+
+int odp_pktio_set_mac_addr(odp_pktio_t id, const unsigned char *mac_addr,
+		size_t addr_size)
+{
+	pktio_entry_t *entry;
+	int sockfd;
+	struct ifreq ifr;
+	int ret;
+
+	entry = get_entry(id);
+	if (entry == NULL) {
+		ODP_DBG("pktio entry %d does not exist\n", id);
+		return -1;
+	}
+
+	if (entry->s.pkt_sock_mmap.sockfd)
+		sockfd = entry->s.pkt_sock_mmap.sockfd;
+	else
+		sockfd = entry->s.pkt_sock.sockfd;
+
+	strncpy(ifr.ifr_name, entry->s.name, IFNAMSIZ);
+	memcpy(ifr.ifr_hwaddr.sa_data, mac_addr, addr_size);
+	ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
+
+	ret = ioctl(sockfd, SIOCSIFHWADDR, &ifr);
+	if (ret < 0) {
+		ODP_DBG("ioctl SIOCSIFHWADDR error\n");
+		return -1;
+	}
+
+	return 0;
+}
+
+int odp_pktio_get_mac_addr(odp_pktio_t id, unsigned char *mac_addr,
+		size_t *addr_size)
+{
+	pktio_entry_t *entry;
+	int sockfd;
+	struct ifreq ifr;
+	int ret;
+
+	entry = get_entry(id);
+	if (entry == NULL) {
+		ODP_DBG("pktio entry %d does not exist\n", id);
+		return -1;
+	}
+
+	if (entry->s.pkt_sock_mmap.sockfd)
+		sockfd = entry->s.pkt_sock_mmap.sockfd;
+	else
+		sockfd = entry->s.pkt_sock.sockfd;
+
+	strncpy(ifr.ifr_name, entry->s.name, IFNAMSIZ);
+
+	ret = ioctl(sockfd, SIOCGIFHWADDR, &ifr);
+	if (ret < 0) {
+		ODP_DBG("ioctl SIOCGIFHWADDR error\n");
+		return -1;
+	}
+
+	*addr_size = ETH_ALEN;
+	memcpy(mac_addr, (unsigned char *)ifr.ifr_ifru.ifru_hwaddr.sa_data,
+	       ETH_ALEN);
+	return 0;
+}