Commit 9250cea8 authored by Alessandro Rubini's avatar Alessandro Rubini

wrs-socket: simplify the code retrieving timestamps

This turns the while(1) with internal retry++ to a loop based on retry.
With two changes:

1- when recvmsg() gets EAGAIN (or other errors), we loop over instead
of returning. Maybe this doesn't happen anymore thanks to the change
from POLLIN to POLLERR (previous commit), but this is philosophically
correct anyways.

2- we don't print anything when poll() returns 0, as these messages
were redundant, especially when there were many in a row. And if
nothing is there we expect to get 0. So we removed these:

   poll_tx_timestamp: poll() = 0 (Success)

Still, knowing the timestamp was late is useful, but a single message
suffices, and it's there now.  If you wonder why we loop instead of
using a longer timeout, the reason is we must get rid of timestamps
for older frames (the ones that timeout at the previous iteration).
Signed-off-by: Alessandro Rubini's avatarAlessandro Rubini <rubini@gnudd.com>
parent 4c6953fb
......@@ -349,7 +349,7 @@ static void poll_tx_timestamp(struct pp_instance *ppi, void *pkt, int len,
} control;
struct cmsghdr *cmsg;
struct pollfd pfd;
int res, retry = 0;;
int res, retry;
struct sock_extended_err *serr = NULL;
struct scm_timestamping *sts = NULL;
......@@ -369,32 +369,39 @@ static void poll_tx_timestamp(struct pp_instance *ppi, void *pkt, int len,
pfd.fd = fd;
pfd.events = POLLERR;
while (1) { /* Not forever: we break after a few runs */
#define N_RETRY 5
for (retry = 0; retry < N_RETRY; retry++) {
errno = 0;
res = poll(&pfd, 1, 20 /* ms */);
if (res != 1) {
if (res < 0 && errno != EAGAIN) {
pp_diag(ppi, time, 1, "%s: poll() = %i (%s)\n",
__func__, res, strerror(errno));
if (retry++ > 5)
return;
continue;
}
if (res < 1)
continue;
res = recvmsg(fd, &msg, MSG_ERRQUEUE);
if (res <= 0) {
/* sometimes we got EAGAIN despite poll() = 1 */
pp_diag(ppi, time, 1, "%s: recvmsg() = %i (%s)\n",
__func__, res, strerror(errno));
return;
continue;
}
/* Now, check if this frame is our frame. If not, retry */
if (!memcmp(data, pkt, len))
break;
pp_diag(ppi, time, 1, "%s: recvmsg(): not our frame\n",
__func__);
/* We won't pop out wrong stamps forever... */
if (retry++ > 5)
return;
}
if (retry) {
pp_diag(ppi, time, 1, "%s: %i iterations. %s\n", __func__,
retry, errno ? strerror(errno) : "");
}
if (retry == N_RETRY) /* we got nothing */
return;
if (!t) /* maybe caller is not interested, though we popped it out */
return;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment