@@ -46,7 +46,6 @@ typedef struct odp_pktout_queue_t {
#define ODP_PKTIO_MACADDR_MAXSIZE 16
#define ODP_PKTIN_NO_WAIT 0
-#define ODP_PKTIN_WAIT UINT64_MAX
/**
* @}
@@ -40,6 +40,9 @@
* Must be power of two. */
#define SLEEP_CHECK 32
+/* Max wait time supported to avoid potential overflow */
+#define MAX_WAIT_TIME (UINT64_MAX / 1024)
+
static pktio_table_t *pktio_tbl;
/* pktio pointer entries ( for inlines) */
@@ -1699,33 +1702,31 @@ int odp_pktin_recv_tmo(odp_pktin_queue_t queue, odp_packet_t packets[], int num,
while (1) {
ret = entry->s.ops->recv(entry, queue.index, packets, num);
- if (ret != 0)
+ if (ret != 0 || wait == 0)
return ret;
- if (wait == 0)
- return 0;
-
- if (wait != ODP_PKTIN_WAIT) {
- /* Avoid unnecessary system calls. Record the start time
- * only when needed and after the first call to recv. */
- if (odp_unlikely(!started)) {
- odp_time_t t;
-
- t = odp_time_local_from_ns(wait * 1000);
- started = 1;
- t1 = odp_time_sum(odp_time_local(), t);
- }
+ /* Avoid unnecessary system calls. Record the start time
+ * only when needed and after the first call to recv. */
+ if (odp_unlikely(!started)) {
+ odp_time_t t;
+
+ /* Avoid overflow issues for large wait times */
+ if (wait > MAX_WAIT_TIME)
+ wait = MAX_WAIT_TIME;
+ t = odp_time_local_from_ns(wait * 1000);
+ started = 1;
+ t1 = odp_time_sum(odp_time_local(), t);
+ }
- /* Check every SLEEP_CHECK rounds if total wait time
- * has been exceeded. */
- if ((++sleep_round & (SLEEP_CHECK - 1)) == 0) {
- t2 = odp_time_local();
+ /* Check every SLEEP_CHECK rounds if total wait time
+ * has been exceeded. */
+ if ((++sleep_round & (SLEEP_CHECK - 1)) == 0) {
+ t2 = odp_time_local();
- if (odp_time_cmp(t2, t1) > 0)
- return 0;
- }
- wait = wait > SLEEP_USEC ? wait - SLEEP_USEC : 0;
+ if (odp_time_cmp(t2, t1) > 0)
+ return 0;
}
+ wait = wait > SLEEP_USEC ? wait - SLEEP_USEC : 0;
nanosleep(&ts, NULL);
}
@@ -1779,25 +1780,26 @@ int odp_pktin_recv_mq_tmo(const odp_pktin_queue_t queues[], unsigned num_q,
if (wait == 0)
return 0;
- if (wait != ODP_PKTIN_WAIT) {
- if (odp_unlikely(!started)) {
- odp_time_t t;
+ if (odp_unlikely(!started)) {
+ odp_time_t t;
- t = odp_time_local_from_ns(wait * 1000);
- started = 1;
- t1 = odp_time_sum(odp_time_local(), t);
- }
+ /* Avoid overflow issues for large wait times */
+ if (wait > MAX_WAIT_TIME)
+ wait = MAX_WAIT_TIME;
+ t = odp_time_local_from_ns(wait * 1000);
+ started = 1;
+ t1 = odp_time_sum(odp_time_local(), t);
+ }
- /* Check every SLEEP_CHECK rounds if total wait time
- * has been exceeded. */
- if ((++sleep_round & (SLEEP_CHECK - 1)) == 0) {
- t2 = odp_time_local();
+ /* Check every SLEEP_CHECK rounds if total wait time
+ * has been exceeded. */
+ if ((++sleep_round & (SLEEP_CHECK - 1)) == 0) {
+ t2 = odp_time_local();
- if (odp_time_cmp(t2, t1) > 0)
- return 0;
- }
- wait = wait > SLEEP_USEC ? wait - SLEEP_USEC : 0;
+ if (odp_time_cmp(t2, t1) > 0)
+ return 0;
}
+ wait = wait > SLEEP_USEC ? wait - SLEEP_USEC : 0;
nanosleep(&ts, NULL);
}
@@ -834,8 +834,7 @@ static int netmap_recv_tmo(pktio_entry_t *pktio_entry, int index,
FD_ZERO(&readfds);
maxfd = netmap_fd_set(pktio_entry, index, &readfds);
- if (select(maxfd + 1, &readfds, NULL, NULL,
- usecs == ODP_PKTIN_WAIT ? NULL : &timeout) == 0)
+ if (select(maxfd + 1, &readfds, NULL, NULL, &timeout) == 0)
return 0;
return netmap_recv(pktio_entry, index, pkt_table, num);
@@ -872,8 +871,7 @@ static int netmap_recv_mq_tmo(pktio_entry_t *pktio_entry[], int index[],
timeout.tv_sec = usecs / (1000 * 1000);
timeout.tv_usec = usecs - timeout.tv_sec * (1000ULL * 1000ULL);
- if (select(maxfd + 1, &readfds, NULL, NULL,
- usecs == ODP_PKTIN_WAIT ? NULL : &timeout) == 0)
+ if (select(maxfd + 1, &readfds, NULL, NULL, &timeout) == 0)
return 0;
for (i = 0; i < num_q; i++) {
@@ -107,8 +107,7 @@ static int sock_recv_mq_tmo_select(pktio_entry_t * const *entry,
timeout.tv_sec = usecs / (1000 * 1000);
timeout.tv_usec = usecs - timeout.tv_sec * (1000ULL * 1000ULL);
- if (select(maxfd + 1, readfds, NULL, NULL,
- usecs == ODP_PKTIN_WAIT ? NULL : &timeout) == 0)
+ if (select(maxfd + 1, readfds, NULL, NULL, &timeout) == 0)
return 0;
for (i = 0; i < num_q; i++) {
@@ -726,8 +726,7 @@ static int sock_recv_tmo(pktio_entry_t *pktio_entry, int index,
FD_ZERO(&readfds);
maxfd = sock_fd_set(pktio_entry, index, &readfds);
- if (select(maxfd + 1, &readfds, NULL, NULL,
- usecs == ODP_PKTIN_WAIT ? NULL : &timeout) == 0)
+ if (select(maxfd + 1, &readfds, NULL, NULL, &timeout) == 0)
return 0;
return sock_mmsg_recv(pktio_entry, index, pkt_table, num);
@@ -764,8 +763,7 @@ static int sock_recv_mq_tmo(pktio_entry_t *pktio_entry[], int index[],
maxfd = maxfd2;
}
- if (select(maxfd + 1, &readfds, NULL, NULL,
- usecs == ODP_PKTIN_WAIT ? NULL : &timeout) == 0)
+ if (select(maxfd + 1, &readfds, NULL, NULL, &timeout) == 0)
return 0;
for (i = 0; i < num_q; i++) {
@@ -692,8 +692,7 @@ static int sock_mmap_recv_tmo(pktio_entry_t *pktio_entry, int index,
FD_ZERO(&readfds);
maxfd = sock_mmap_fd_set(pktio_entry, index, &readfds);
- if (select(maxfd + 1, &readfds, NULL, NULL,
- usecs == ODP_PKTIN_WAIT ? NULL : &timeout) == 0)
+ if (select(maxfd + 1, &readfds, NULL, NULL, &timeout) == 0)
return 0;
return sock_mmap_recv(pktio_entry, index, pkt_table, num);
@@ -730,8 +729,7 @@ static int sock_mmap_recv_mq_tmo(pktio_entry_t *pktio_entry[], int index[],
timeout.tv_sec = usecs / (1000 * 1000);
timeout.tv_usec = usecs - timeout.tv_sec * (1000ULL * 1000ULL);
- if (select(maxfd + 1, &readfds, NULL, NULL,
- usecs == ODP_PKTIN_WAIT ? NULL : &timeout) == 0)
+ if (select(maxfd + 1, &readfds, NULL, NULL, &timeout) == 0)
return 0;
for (i = 0; i < num_q; i++) {