diff mbox

[02/16] api: odp_pktio.h: odp_pktio_mac_addr() return bufsz

Message ID 1422528008-29845-3-git-send-email-ola.liljedahl@linaro.org
State New
Headers show

Commit Message

Ola Liljedahl Jan. 29, 2015, 10:39 a.m. UTC
odp_pktio_mac_addr() definition modified to pass output buffer size by
reference and the variable is updated with the number of bytes written on
success or the minimum required buffer size should it be too small.
odp_pktio_mac_addr() returns 0 on success and <0 on failure.
Updated the implementation and usages of this call to match.

Signed-off-by: Ola Liljedahl <ola.liljedahl@linaro.org>
---
(This document/code contribution attached is provided under the terms of
agreement LES-LTM-21309)

 platform/linux-generic/include/api/odp_packet_io.h | 13 +++++------
 platform/linux-generic/odp_packet_io.c             | 17 +++++++++------
 test/validation/odp_pktio.c                        | 25 ++++++++++++++--------
 3 files changed, 34 insertions(+), 21 deletions(-)
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 6e479aa..019cffe 100644
--- a/platform/linux-generic/include/api/odp_packet_io.h
+++ b/platform/linux-generic/include/api/odp_packet_io.h
@@ -159,14 +159,15 @@  int odp_pktio_promisc_mode(odp_pktio_t id);
 /**
  * Get the default MAC address of a packet IO interface.
  *
- * @param	id	  ODP packet IO handle.
- * @param[out]	mac_addr  Storage for MAC address of the packet IO interface.
- * @param	addr_size Storage size for the address
+ * @param	hdl  ODP packet IO handle
+ * @param[out]	buf  Output buffer
+ * @param[in,out] bufsz Size of output buffer, updated with size of data
+ * actually written or minimum required buffer size
  *
- * @retval Number of bytes written on success, 0 on failure.
+ * @return 0 on success and '*bufsz' updated with size of data written
+ * @retval <0 on failure and '*bufsz' updated with required buffer size
  */
-size_t odp_pktio_mac_addr(odp_pktio_t id, void *mac_addr,
-			  size_t addr_size);
+int odp_pktio_mac_addr(odp_pktio_t hdl, void *buf, size_t *bufsz);
 
 /**
  * Setup per-port default class-of-service.
diff --git a/platform/linux-generic/odp_packet_io.c b/platform/linux-generic/odp_packet_io.c
index 52125fc..0dcd85b 100644
--- a/platform/linux-generic/odp_packet_io.c
+++ b/platform/linux-generic/odp_packet_io.c
@@ -798,18 +798,22 @@  int odp_pktio_promisc_mode(odp_pktio_t id)
 }
 
 
-size_t odp_pktio_mac_addr(odp_pktio_t id, void *mac_addr,
-		       size_t addr_size)
+int odp_pktio_mac_addr(odp_pktio_t id, void *mac_addr,
+		       size_t *addr_size)
 {
 	pktio_entry_t *entry;
 
-	if (addr_size < ETH_ALEN)
-		return 0;
+	if (*addr_size < ETH_ALEN) {
+		/* Output buffer too small
+		 * Update *addr_size with required size */
+		*addr_size = ETH_ALEN;
+		return -1;
+	}
 
 	entry = get_pktio_entry(id);
 	if (entry == NULL) {
 		ODP_DBG("pktio entry %d does not exist\n", id);
-		return 0;
+		return -1;
 	}
 
 	lock_entry(entry);
@@ -839,5 +843,6 @@  size_t odp_pktio_mac_addr(odp_pktio_t id, void *mac_addr,
 
 	unlock_entry(entry);
 
-	return ETH_ALEN;
+	*addr_size = ETH_ALEN;
+	return 0;
 }
diff --git a/test/validation/odp_pktio.c b/test/validation/odp_pktio.c
index 0985771..46155fd 100644
--- a/test/validation/odp_pktio.c
+++ b/test/validation/odp_pktio.c
@@ -49,13 +49,18 @@  static void pktio_pkt_set_macs(odp_packet_t pkt,
 {
 	uint32_t len;
 	odph_ethhdr_t *eth = (odph_ethhdr_t *)odp_packet_l2_ptr(pkt, &len);
+	size_t bufsz;
 	int ret;
 
-	ret = odp_pktio_mac_addr(src->id, &eth->src, sizeof(eth->src));
-	CU_ASSERT(ret == ODPH_ETHADDR_LEN);
+	bufsz = sizeof(eth->src);
+	ret = odp_pktio_mac_addr(src->id, &eth->src, &bufsz);
+	CU_ASSERT(ret == 0);
+	CU_ASSERT(bufsz == ODPH_ETHADDR_LEN);
 
-	ret = odp_pktio_mac_addr(dst->id, &eth->dst, sizeof(eth->dst));
-	CU_ASSERT(ret == ODPH_ETHADDR_LEN);
+	bufsz = sizeof(eth->dst);
+	ret = odp_pktio_mac_addr(dst->id, &eth->dst, &bufsz);
+	CU_ASSERT(ret == 0);
+	CU_ASSERT(bufsz == ODPH_ETHADDR_LEN);
 }
 
 static int pktio_pkt_set_seq(odp_packet_t pkt)
@@ -450,22 +455,24 @@  static void test_odp_pktio_promisc(void)
 static void test_odp_pktio_mac(void)
 {
 	unsigned char mac_addr[ODPH_ETHADDR_LEN];
-	size_t mac_len;
+	size_t mac_len = sizeof(mac_addr);
 	int ret;
 	odp_pktio_t pktio = create_pktio(iface_name[0]);
 
 	printf("testing mac for %s\n", iface_name[0]);
 
-	mac_len = odp_pktio_mac_addr(pktio, mac_addr, ODPH_ETHADDR_LEN);
+	ret = odp_pktio_mac_addr(pktio, mac_addr, &mac_len);
+	CU_ASSERT(ret == 0);
 	CU_ASSERT(ODPH_ETHADDR_LEN == mac_len);
 
 	printf(" %X:%X:%X:%X:%X:%X ",
 	       mac_addr[0], mac_addr[1], mac_addr[2],
 	       mac_addr[3], mac_addr[4], mac_addr[5]);
 
-	/* Fail case: wrong addr_size. Expected 0. */
-	mac_len = odp_pktio_mac_addr(pktio, mac_addr, 2);
-	CU_ASSERT(0 == mac_len);
+	/* Fail case: wrong addr_size. Expected <0. */
+	mac_len = 2;
+	ret = odp_pktio_mac_addr(pktio, mac_addr, &mac_len);
+	CU_ASSERT(ret < 0);
 
 	ret = odp_pktio_close(pktio);
 	CU_ASSERT(0 == ret);