diff mbox

[PATCHv5,2/6] API: promisc mode manipulation functions

Message ID 1417694439-13861-3-git-send-email-maxim.uvarov@linaro.org
State New
Headers show

Commit Message

Maxim Uvarov Dec. 4, 2014, noon UTC
Define API and implement promisc functions for linux-generic.

Signed-off-by: Maxim Uvarov <maxim.uvarov@linaro.org>
---
 platform/linux-generic/include/api/odp_packet_io.h | 24 +++++++
 platform/linux-generic/odp_packet_io.c             | 84 ++++++++++++++++++++++
 2 files changed, 108 insertions(+)

Comments

Ola Liljedahl Dec. 5, 2014, 10:31 a.m. UTC | #1
On 5 December 2014 at 09:37, Savolainen, Petri (NSN - FI/Espoo)
<petri.savolainen@nsn.com> wrote:
>
>
>> -----Original Message-----
>> From: lng-odp-bounces@lists.linaro.org [mailto:lng-odp-
>> bounces@lists.linaro.org] On Behalf Of ext Maxim Uvarov
>> Sent: Thursday, December 04, 2014 2:01 PM
>> To: lng-odp@lists.linaro.org
>> Subject: [lng-odp] [PATCHv5 2/6] API: promisc mode manipulation functions
>>
>> Define API and implement promisc functions for linux-generic.
>>
>> Signed-off-by: Maxim Uvarov <maxim.uvarov@linaro.org>
>> ---
>>  platform/linux-generic/include/api/odp_packet_io.h | 24 +++++++
>>  platform/linux-generic/odp_packet_io.c             | 84
>> ++++++++++++++++++++++
>>  2 files changed, 108 insertions(+)
>>
>> diff --git a/platform/linux-generic/include/api/odp_packet_io.h
>> b/platform/linux-generic/include/api/odp_packet_io.h
>> index 5f7ba7c..df2b138 100644
>> --- a/platform/linux-generic/include/api/odp_packet_io.h
>> +++ b/platform/linux-generic/include/api/odp_packet_io.h
>> @@ -153,6 +153,30 @@ int odp_pktio_set_mtu(odp_pktio_t id, int mtu);
>>  int odp_pktio_mtu(odp_pktio_t id);
>>
>>  /**
>> + * Enable promiscuous mode on a packet IO interface.
>> + *
>> + * @param[in] id     ODP packet IO handle.
>> + * @param[in] enable    1 enabled, 0 disabled.
>
> @param[in] enable    1 to enable, 0 to disable
>
>> + *
>> + * @retval  0 on success.
>> + * @retval -1 on a bad pktio id
Isn't this really a programming error to specify an invalid pktio
handle? Or is there any case where this would be "normal" usage?

Programming errors are such errors that the application cannot be
expected to handle automatically (i.e. the code must be modified,
recompiled and redeployed).
As a general design rule, I treat all errors that are defined as
programming errors as fatal and call abort/ODP_ABORT. Find errors
early and contain them. Robustness does not come from passing around
(or ignoring) error codes for unexpected errors.


>> + * @retval -1 any other error
>
> No need to list all possible reasons for errors here. Return 0 or !0
> @retval non-zero on an error
>
>> + */
>> +int odp_pktio_promisc_mode_set(odp_pktio_t id, odp_bool_t enable);
>> +
>> +/**
>> + * Determine if promiscuous mode is enabled for a packet IO interface.
>> + *
>> + * @param[in] id ODP packet IO handle.
>> + *
>> + * @retval  1 if promiscuous mode is enabled.
>> + * @retval  0 if promiscuous mode is disabled.
>> + * @retval -1 on a bad pktio id
>> + * @retval -1 any other error
>
> No need to list all possible reasons for errors here
> @retval -1 on an error
>
>
> -Petri
>
>> +*/
>> +int odp_pktio_promisc_mode(odp_pktio_t id);
>> +
>> +/**
>>   * @}
>>   */
>>
>> diff --git a/platform/linux-generic/odp_packet_io.c b/platform/linux-
>> generic/odp_packet_io.c
>> index faa197f..b0c00c0 100644
>> --- a/platform/linux-generic/odp_packet_io.c
>> +++ b/platform/linux-generic/odp_packet_io.c
>> @@ -486,6 +486,15 @@ int pktin_deq_multi(queue_entry_t *qentry,
>> odp_buffer_hdr_t *buf_hdr[], int num)
>>       return nbr;
>>  }
>>
>> +/** function should be called with locked entry */
>> +static int sockfd_from_pktio_entry(pktio_entry_t *entry)
>> +{
>> +     if (entry->s.pkt_sock_mmap.sockfd > -1)
>> +             return entry->s.pkt_sock_mmap.sockfd;
>> +     else
>> +             return entry->s.pkt_sock.sockfd;
>> +}
>> +
>>  int odp_pktio_set_mtu(odp_pktio_t id, int mtu)
>>  {
>>       pktio_entry_t *entry;
>> @@ -549,3 +558,78 @@ int odp_pktio_mtu(odp_pktio_t id)
>>
>>       return ifr.ifr_mtu;
>>  }
>> +
>> +int odp_pktio_promisc_mode_set(odp_pktio_t id, odp_bool_t enable)
>> +{
>> +     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;
>> +     }
>> +
>> +     lock_entry(entry);
>> +
>> +     sockfd = sockfd_from_pktio_entry(entry);
>> +     strncpy(ifr.ifr_name, entry->s.name, IFNAMSIZ - 1);
>> +     ifr.ifr_name[IFNAMSIZ - 1] = 0;
>> +
>> +     ret = ioctl(sockfd, SIOCGIFFLAGS, &ifr);
>> +     if (ret < 0) {
>> +             unlock_entry(entry);
>> +             ODP_DBG("ioctl SIOCGIFFLAGS error\n");
>> +             return -1;
>> +     }
>> +
>> +     if (enable)
>> +             ifr.ifr_flags |= IFF_PROMISC;
>> +     else
>> +             ifr.ifr_flags &= ~(IFF_PROMISC);
>> +
>> +     ret = ioctl(sockfd, SIOCSIFFLAGS, &ifr);
>> +     if (ret < 0) {
>> +             unlock_entry(entry);
>> +             ODP_DBG("ioctl SIOCSIFFLAGS error\n");
>> +             return -1;
>> +     }
>> +
>> +     unlock_entry(entry);
>> +     return 0;
>> +}
>> +
>> +int odp_pktio_promisc_mode(odp_pktio_t id)
>> +{
>> +     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;
>> +     }
>> +
>> +     lock_entry(entry);
>> +
>> +     sockfd = sockfd_from_pktio_entry(entry);
>> +     strncpy(ifr.ifr_name, entry->s.name, IFNAMSIZ - 1);
>> +     ifr.ifr_name[IFNAMSIZ - 1] = 0;
>> +
>> +     ret = ioctl(sockfd, SIOCGIFFLAGS, &ifr);
>> +     if (ret < 0) {
>> +             ODP_DBG("ioctl SIOCGIFFLAGS error\n");
>> +             unlock_entry(entry);
>> +             return -1;
>> +     }
>> +     unlock_entry(entry);
>> +
>> +     if (ifr.ifr_flags & IFF_PROMISC)
>> +             return 1;
>> +     else
>> +             return 0;
>> +}
>> --
>> 1.8.5.1.163.gd7aced9
>>
>>
>> _______________________________________________
>> lng-odp mailing list
>> lng-odp@lists.linaro.org
>> http://lists.linaro.org/mailman/listinfo/lng-odp
>
> _______________________________________________
> lng-odp mailing list
> lng-odp@lists.linaro.org
> http://lists.linaro.org/mailman/listinfo/lng-odp
Maxim Uvarov Dec. 5, 2014, 3:30 p.m. UTC | #2
On 12/05/2014 01:31 PM, Ola Liljedahl wrote:
> On 5 December 2014 at 09:37, Savolainen, Petri (NSN - FI/Espoo)
> <petri.savolainen@nsn.com> wrote:
>>
>>> -----Original Message-----
>>> From: lng-odp-bounces@lists.linaro.org [mailto:lng-odp-
>>> bounces@lists.linaro.org] On Behalf Of ext Maxim Uvarov
>>> Sent: Thursday, December 04, 2014 2:01 PM
>>> To: lng-odp@lists.linaro.org
>>> Subject: [lng-odp] [PATCHv5 2/6] API: promisc mode manipulation functions
>>>
>>> Define API and implement promisc functions for linux-generic.
>>>
>>> Signed-off-by: Maxim Uvarov <maxim.uvarov@linaro.org>
>>> ---
>>>   platform/linux-generic/include/api/odp_packet_io.h | 24 +++++++
>>>   platform/linux-generic/odp_packet_io.c             | 84
>>> ++++++++++++++++++++++
>>>   2 files changed, 108 insertions(+)
>>>
>>> diff --git a/platform/linux-generic/include/api/odp_packet_io.h
>>> b/platform/linux-generic/include/api/odp_packet_io.h
>>> index 5f7ba7c..df2b138 100644
>>> --- a/platform/linux-generic/include/api/odp_packet_io.h
>>> +++ b/platform/linux-generic/include/api/odp_packet_io.h
>>> @@ -153,6 +153,30 @@ int odp_pktio_set_mtu(odp_pktio_t id, int mtu);
>>>   int odp_pktio_mtu(odp_pktio_t id);
>>>
>>>   /**
>>> + * Enable promiscuous mode on a packet IO interface.
>>> + *
>>> + * @param[in] id     ODP packet IO handle.
>>> + * @param[in] enable    1 enabled, 0 disabled.
>> @param[in] enable    1 to enable, 0 to disable
>>
>>> + *
>>> + * @retval  0 on success.
>>> + * @retval -1 on a bad pktio id
> Isn't this really a programming error to specify an invalid pktio
> handle? Or is there any case where this would be "normal" usage?
>
> Programming errors are such errors that the application cannot be
> expected to handle automatically (i.e. the code must be modified,
> recompiled and redeployed).
> As a general design rule, I treat all errors that are defined as
> programming errors as fatal and call abort/ODP_ABORT. Find errors
> early and contain them. Robustness does not come from passing around
> (or ignoring) error codes for unexpected errors.
>
Here might be issue that pktio was destroyed by other process before 
this function locks pktio.
In that case it's not programming error. Only this function should 
return error.

Maxim.

>>> + * @retval -1 any other error
>> No need to list all possible reasons for errors here. Return 0 or !0
>> @retval non-zero on an error
>>
>>> + */
>>> +int odp_pktio_promisc_mode_set(odp_pktio_t id, odp_bool_t enable);
>>> +
>>> +/**
>>> + * Determine if promiscuous mode is enabled for a packet IO interface.
>>> + *
>>> + * @param[in] id ODP packet IO handle.
>>> + *
>>> + * @retval  1 if promiscuous mode is enabled.
>>> + * @retval  0 if promiscuous mode is disabled.
>>> + * @retval -1 on a bad pktio id
>>> + * @retval -1 any other error
>> No need to list all possible reasons for errors here
>> @retval -1 on an error
>>
>>
>> -Petri
>>
>>> +*/
>>> +int odp_pktio_promisc_mode(odp_pktio_t id);
>>> +
>>> +/**
>>>    * @}
>>>    */
>>>
>>> diff --git a/platform/linux-generic/odp_packet_io.c b/platform/linux-
>>> generic/odp_packet_io.c
>>> index faa197f..b0c00c0 100644
>>> --- a/platform/linux-generic/odp_packet_io.c
>>> +++ b/platform/linux-generic/odp_packet_io.c
>>> @@ -486,6 +486,15 @@ int pktin_deq_multi(queue_entry_t *qentry,
>>> odp_buffer_hdr_t *buf_hdr[], int num)
>>>        return nbr;
>>>   }
>>>
>>> +/** function should be called with locked entry */
>>> +static int sockfd_from_pktio_entry(pktio_entry_t *entry)
>>> +{
>>> +     if (entry->s.pkt_sock_mmap.sockfd > -1)
>>> +             return entry->s.pkt_sock_mmap.sockfd;
>>> +     else
>>> +             return entry->s.pkt_sock.sockfd;
>>> +}
>>> +
>>>   int odp_pktio_set_mtu(odp_pktio_t id, int mtu)
>>>   {
>>>        pktio_entry_t *entry;
>>> @@ -549,3 +558,78 @@ int odp_pktio_mtu(odp_pktio_t id)
>>>
>>>        return ifr.ifr_mtu;
>>>   }
>>> +
>>> +int odp_pktio_promisc_mode_set(odp_pktio_t id, odp_bool_t enable)
>>> +{
>>> +     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;
>>> +     }
>>> +
>>> +     lock_entry(entry);
>>> +
>>> +     sockfd = sockfd_from_pktio_entry(entry);
>>> +     strncpy(ifr.ifr_name, entry->s.name, IFNAMSIZ - 1);
>>> +     ifr.ifr_name[IFNAMSIZ - 1] = 0;
>>> +
>>> +     ret = ioctl(sockfd, SIOCGIFFLAGS, &ifr);
>>> +     if (ret < 0) {
>>> +             unlock_entry(entry);
>>> +             ODP_DBG("ioctl SIOCGIFFLAGS error\n");
>>> +             return -1;
>>> +     }
>>> +
>>> +     if (enable)
>>> +             ifr.ifr_flags |= IFF_PROMISC;
>>> +     else
>>> +             ifr.ifr_flags &= ~(IFF_PROMISC);
>>> +
>>> +     ret = ioctl(sockfd, SIOCSIFFLAGS, &ifr);
>>> +     if (ret < 0) {
>>> +             unlock_entry(entry);
>>> +             ODP_DBG("ioctl SIOCSIFFLAGS error\n");
>>> +             return -1;
>>> +     }
>>> +
>>> +     unlock_entry(entry);
>>> +     return 0;
>>> +}
>>> +
>>> +int odp_pktio_promisc_mode(odp_pktio_t id)
>>> +{
>>> +     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;
>>> +     }
>>> +
>>> +     lock_entry(entry);
>>> +
>>> +     sockfd = sockfd_from_pktio_entry(entry);
>>> +     strncpy(ifr.ifr_name, entry->s.name, IFNAMSIZ - 1);
>>> +     ifr.ifr_name[IFNAMSIZ - 1] = 0;
>>> +
>>> +     ret = ioctl(sockfd, SIOCGIFFLAGS, &ifr);
>>> +     if (ret < 0) {
>>> +             ODP_DBG("ioctl SIOCGIFFLAGS error\n");
>>> +             unlock_entry(entry);
>>> +             return -1;
>>> +     }
>>> +     unlock_entry(entry);
>>> +
>>> +     if (ifr.ifr_flags & IFF_PROMISC)
>>> +             return 1;
>>> +     else
>>> +             return 0;
>>> +}
>>> --
>>> 1.8.5.1.163.gd7aced9
>>>
>>>
>>> _______________________________________________
>>> lng-odp mailing list
>>> lng-odp@lists.linaro.org
>>> http://lists.linaro.org/mailman/listinfo/lng-odp
>> _______________________________________________
>> lng-odp mailing list
>> lng-odp@lists.linaro.org
>> http://lists.linaro.org/mailman/listinfo/lng-odp
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 5f7ba7c..df2b138 100644
--- a/platform/linux-generic/include/api/odp_packet_io.h
+++ b/platform/linux-generic/include/api/odp_packet_io.h
@@ -153,6 +153,30 @@  int odp_pktio_set_mtu(odp_pktio_t id, int mtu);
 int odp_pktio_mtu(odp_pktio_t id);
 
 /**
+ * Enable promiscuous mode on a packet IO interface.
+ *
+ * @param[in] id	ODP packet IO handle.
+ * @param[in] enable    1 enabled, 0 disabled.
+ *
+ * @retval  0 on success.
+ * @retval -1 on a bad pktio id
+ * @retval -1 any other error
+ */
+int odp_pktio_promisc_mode_set(odp_pktio_t id, odp_bool_t enable);
+
+/**
+ * Determine if promiscuous mode is enabled for a packet IO interface.
+ *
+ * @param[in] id ODP packet IO handle.
+ *
+ * @retval  1 if promiscuous mode is enabled.
+ * @retval  0 if promiscuous mode is disabled.
+ * @retval -1 on a bad pktio id
+ * @retval -1 any other error
+*/
+int odp_pktio_promisc_mode(odp_pktio_t id);
+
+/**
  * @}
  */
 
diff --git a/platform/linux-generic/odp_packet_io.c b/platform/linux-generic/odp_packet_io.c
index faa197f..b0c00c0 100644
--- a/platform/linux-generic/odp_packet_io.c
+++ b/platform/linux-generic/odp_packet_io.c
@@ -486,6 +486,15 @@  int pktin_deq_multi(queue_entry_t *qentry, odp_buffer_hdr_t *buf_hdr[], int num)
 	return nbr;
 }
 
+/** function should be called with locked entry */
+static int sockfd_from_pktio_entry(pktio_entry_t *entry)
+{
+	if (entry->s.pkt_sock_mmap.sockfd > -1)
+		return entry->s.pkt_sock_mmap.sockfd;
+	else
+		return entry->s.pkt_sock.sockfd;
+}
+
 int odp_pktio_set_mtu(odp_pktio_t id, int mtu)
 {
 	pktio_entry_t *entry;
@@ -549,3 +558,78 @@  int odp_pktio_mtu(odp_pktio_t id)
 
 	return ifr.ifr_mtu;
 }
+
+int odp_pktio_promisc_mode_set(odp_pktio_t id, odp_bool_t enable)
+{
+	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;
+	}
+
+	lock_entry(entry);
+
+	sockfd = sockfd_from_pktio_entry(entry);
+	strncpy(ifr.ifr_name, entry->s.name, IFNAMSIZ - 1);
+	ifr.ifr_name[IFNAMSIZ - 1] = 0;
+
+	ret = ioctl(sockfd, SIOCGIFFLAGS, &ifr);
+	if (ret < 0) {
+		unlock_entry(entry);
+		ODP_DBG("ioctl SIOCGIFFLAGS error\n");
+		return -1;
+	}
+
+	if (enable)
+		ifr.ifr_flags |= IFF_PROMISC;
+	else
+		ifr.ifr_flags &= ~(IFF_PROMISC);
+
+	ret = ioctl(sockfd, SIOCSIFFLAGS, &ifr);
+	if (ret < 0) {
+		unlock_entry(entry);
+		ODP_DBG("ioctl SIOCSIFFLAGS error\n");
+		return -1;
+	}
+
+	unlock_entry(entry);
+	return 0;
+}
+
+int odp_pktio_promisc_mode(odp_pktio_t id)
+{
+	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;
+	}
+
+	lock_entry(entry);
+
+	sockfd = sockfd_from_pktio_entry(entry);
+	strncpy(ifr.ifr_name, entry->s.name, IFNAMSIZ - 1);
+	ifr.ifr_name[IFNAMSIZ - 1] = 0;
+
+	ret = ioctl(sockfd, SIOCGIFFLAGS, &ifr);
+	if (ret < 0) {
+		ODP_DBG("ioctl SIOCGIFFLAGS error\n");
+		unlock_entry(entry);
+		return -1;
+	}
+	unlock_entry(entry);
+
+	if (ifr.ifr_flags & IFF_PROMISC)
+		return 1;
+	else
+		return 0;
+}