Skip to content

Commit

Permalink
ipc: use monotonic clock
Browse files Browse the repository at this point in the history
[ upstream commit cc994d3 ]

Currently, the mp uses gettimeofday() API to get the time, and used as
timeout parameter.

But the time which gets from gettimeofday() API isn't monotonically
increasing. The process may fail if the system time is changed.

This fixes it by using clock_gettime() API with monotonic attribution.

Fixes: 783b6e5 ("eal: add synchronous multi-process communication")
Fixes: f05e260 ("eal: add IPC asynchronous request")

Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
Signed-off-by: Min Hu (Connor) <humin29@huawei.com>
Acked-by: Morten Brørup <mb@smartsharesystems.com>
  • Loading branch information
fengchengwen authored and cpaelzer committed Jun 10, 2021
1 parent 6285f76 commit 0bc04a9
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions lib/librte_eal/common/eal_common_proc.c
Expand Up @@ -483,14 +483,11 @@ async_reply_handle_thread_unsafe(void *arg)
struct pending_request *req = (struct pending_request *)arg;
enum async_action action;
struct timespec ts_now;
struct timeval now;

if (gettimeofday(&now, NULL) < 0) {
if (clock_gettime(CLOCK_MONOTONIC, &ts_now) < 0) {
RTE_LOG(ERR, EAL, "Cannot get current time\n");
goto no_trigger;
}
ts_now.tv_nsec = now.tv_usec * 1000;
ts_now.tv_sec = now.tv_sec;

action = process_async_request(req, &ts_now);

Expand Down Expand Up @@ -884,6 +881,7 @@ mp_request_sync(const char *dst, struct rte_mp_msg *req,
struct rte_mp_reply *reply, const struct timespec *ts)
{
int ret;
pthread_condattr_t attr;
struct rte_mp_msg msg, *tmp;
struct pending_request pending_req, *exist;

Expand All @@ -892,7 +890,9 @@ mp_request_sync(const char *dst, struct rte_mp_msg *req,
strlcpy(pending_req.dst, dst, sizeof(pending_req.dst));
pending_req.request = req;
pending_req.reply = &msg;
pthread_cond_init(&pending_req.sync.cond, NULL);
pthread_condattr_init(&attr);
pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
pthread_cond_init(&pending_req.sync.cond, &attr);

exist = find_pending_request(dst, req->name);
if (exist) {
Expand Down Expand Up @@ -955,8 +955,7 @@ rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply,
int dir_fd, ret = -1;
DIR *mp_dir;
struct dirent *ent;
struct timeval now;
struct timespec end;
struct timespec now, end;

RTE_LOG(DEBUG, EAL, "request: %s\n", req->name);

Expand All @@ -973,15 +972,15 @@ rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply,
return -1;
}

if (gettimeofday(&now, NULL) < 0) {
if (clock_gettime(CLOCK_MONOTONIC, &now) < 0) {
RTE_LOG(ERR, EAL, "Failed to get current time\n");
rte_errno = errno;
goto end;
}

end.tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
end.tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
end.tv_sec = now.tv_sec + ts->tv_sec +
(now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
(now.tv_nsec + ts->tv_nsec) / 1000000000;

/* for secondary process, send request to the primary process only */
if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
Expand Down Expand Up @@ -1055,7 +1054,7 @@ rte_mp_request_async(struct rte_mp_msg *req, const struct timespec *ts,
int dir_fd, ret = 0;
DIR *mp_dir;
struct dirent *ent;
struct timeval now;
struct timespec now;
struct timespec *end;
bool dummy_used = false;

Expand All @@ -1070,7 +1069,7 @@ rte_mp_request_async(struct rte_mp_msg *req, const struct timespec *ts,
return -1;
}

if (gettimeofday(&now, NULL) < 0) {
if (clock_gettime(CLOCK_MONOTONIC, &now) < 0) {
RTE_LOG(ERR, EAL, "Failed to get current time\n");
rte_errno = errno;
return -1;
Expand All @@ -1092,9 +1091,9 @@ rte_mp_request_async(struct rte_mp_msg *req, const struct timespec *ts,
end = &param->end;
reply = &param->user_reply;

end->tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
end->tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
end->tv_sec = now.tv_sec + ts->tv_sec +
(now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
(now.tv_nsec + ts->tv_nsec) / 1000000000;
reply->nb_sent = 0;
reply->nb_received = 0;
reply->msgs = NULL;
Expand Down

0 comments on commit 0bc04a9

Please sign in to comment.