Added define ODP_PKTIO_MACADDR_MAXSIZE which specifies the recommended
output buffer size for odp_pktio_mac_addr().
odp_pktio_mac_addr() takes output buffer size as input and returns
number of chars written (on success), <0 on failure.
Updated the implementation.
Updated all usages in example and test programs.

Signed-off-by: Ola Liljedahl <[email protected]>
---
(This document/code contribution attached is provided under the terms of
agreement LES-LTM-21309)

 include/odp/api/packet_io.h                           | 19 +++++++++++++------
 .../linux-generic/include/odp/plat/packet_io_types.h  |  2 ++
 platform/linux-generic/odp_packet_io.c                | 11 ++++++-----
 test/validation/odp_pktio.c                           |  8 ++++----
 4 files changed, 25 insertions(+), 15 deletions(-)

diff --git a/include/odp/api/packet_io.h b/include/odp/api/packet_io.h
index 3e4bac4..9d18489 100644
--- a/include/odp/api/packet_io.h
+++ b/include/odp/api/packet_io.h
@@ -18,6 +18,7 @@
 extern "C" {
 #endif
 
+#include <sys/types.h>
 
 /** @defgroup odp_packet_io ODP PACKET IO
  *  Operations on a packet.
@@ -39,6 +40,12 @@ extern "C" {
  * odp_pktio_t value to indicate any port
  */
 
+/*
+ * @def ODP_PKTIO_MACADDR_MAXSIZE
+ * Minimum size of output buffer for odp_pktio_mac_addr()
+ * Actual MAC address sizes may be different.
+ */
+
 /**
  * Open an ODP packet IO instance
  *
@@ -169,14 +176,14 @@ 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      id        ODP packet IO handle
+ * @param[out] mac_addr  Output buffer (use ODP_PKTIO_MACADDR_MAXSIZE)
+ * @param       size Size of output buffer
  *
- * @retval Number of bytes written on success, 0 on failure.
+ * @return Number of bytes written (actual size of MAC address)
+ * @retval <0 on failure
  */
-size_t odp_pktio_mac_addr(odp_pktio_t id, void *mac_addr,
-                         size_t addr_size);
+ssize_t odp_pktio_mac_addr(odp_pktio_t id, void *mac_addr, ssize_t size);
 
 /**
  * Setup per-port default class-of-service.
diff --git a/platform/linux-generic/include/odp/plat/packet_io_types.h 
b/platform/linux-generic/include/odp/plat/packet_io_types.h
index dfa7cba..60592e3 100644
--- a/platform/linux-generic/include/odp/plat/packet_io_types.h
+++ b/platform/linux-generic/include/odp/plat/packet_io_types.h
@@ -32,6 +32,8 @@ typedef odp_handle_t odp_pktio_t;
 
 #define ODP_PKTIO_ANY _odp_cast_scalar(odp_pktio_t, ~0)
 
+#define ODP_PKTIO_MACADDR_MAXSIZE 16
+
 /** Get printable format of odp_pktio_t */
 static inline uint64_t odp_pktio_to_u64(odp_pktio_t hdl)
 {
diff --git a/platform/linux-generic/odp_packet_io.c 
b/platform/linux-generic/odp_packet_io.c
index edc9c09..b3bdd1c 100644
--- a/platform/linux-generic/odp_packet_io.c
+++ b/platform/linux-generic/odp_packet_io.c
@@ -799,18 +799,19 @@ 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)
+ssize_t odp_pktio_mac_addr(odp_pktio_t id, void *mac_addr, ssize_t addr_size)
 {
        pktio_entry_t *entry;
 
-       if (addr_size < ETH_ALEN)
-               return 0;
+       if (addr_size < ETH_ALEN) {
+               /* Output buffer too small */
+               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);
diff --git a/test/validation/odp_pktio.c b/test/validation/odp_pktio.c
index 2d29593..dfb5fc0 100644
--- a/test/validation/odp_pktio.c
+++ b/test/validation/odp_pktio.c
@@ -455,22 +455,22 @@ 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;
+       ssize_t mac_len;
        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);
+       mac_len = odp_pktio_mac_addr(pktio, mac_addr, sizeof(mac_addr));
        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. */
+       /* Fail case: wrong addr_size. Expected <0. */
        mac_len = odp_pktio_mac_addr(pktio, mac_addr, 2);
-       CU_ASSERT(0 == mac_len);
+       CU_ASSERT(mac_len < 0);
 
        ret = odp_pktio_close(pktio);
        CU_ASSERT(0 == ret);
-- 
1.9.1


_______________________________________________
lng-odp mailing list
[email protected]
http://lists.linaro.org/mailman/listinfo/lng-odp

Reply via email to