X-Git-Url: http://git.droids-corp.org/?a=blobdiff_plain;f=lib%2Flibrte_eal%2Fcommon%2Feal_common_proc.c;h=ad02c00316754fef2d861cf649beabeb856552d1;hb=dd7b7f9a52529f605b80c8391a13775e2aa6d832;hp=f98622f4e9be5077e8b4c5b2f5a3730b8187a6c4;hpb=f05e26051c15dd208277c7e27c77f67720d5d4b1;p=dpdk.git diff --git a/lib/librte_eal/common/eal_common_proc.c b/lib/librte_eal/common/eal_common_proc.c index f98622f4e9..ad02c00316 100644 --- a/lib/librte_eal/common/eal_common_proc.c +++ b/lib/librte_eal/common/eal_common_proc.c @@ -129,7 +129,7 @@ create_socket_path(const char *name, char *buf, int len) if (strlen(name) > 0) snprintf(buf, len, "%s_%s", prefix, name); else - snprintf(buf, len, "%s", prefix); + strlcpy(buf, prefix, len); } int @@ -200,7 +200,7 @@ rte_mp_action_register(const char *name, rte_mp_t action) rte_errno = ENOMEM; return -1; } - strcpy(entry->action_name, name); + strlcpy(entry->action_name, name, sizeof(entry->action_name)); entry->action = action; pthread_mutex_lock(&mp_mutex_action); @@ -323,6 +323,7 @@ process_msg(struct mp_msg_internal *m, struct sockaddr_un *s) */ struct rte_mp_msg dummy; memset(&dummy, 0, sizeof(dummy)); + strlcpy(dummy.name, msg->name, sizeof(dummy.name)); mp_send(&dummy, s->sun_path, MP_IGN); } else { RTE_LOG(ERR, EAL, "Cannot find action: %s\n", @@ -441,89 +442,112 @@ trigger_async_action(struct pending_request *sr) free(sr->request); } +static struct pending_request * +check_trigger(struct timespec *ts) +{ + struct pending_request *next, *cur, *trigger = NULL; + + TAILQ_FOREACH_SAFE(cur, &pending_requests.requests, next, next) { + enum async_action action; + if (cur->type != REQUEST_TYPE_ASYNC) + continue; + + action = process_async_request(cur, ts); + if (action == ACTION_FREE) { + TAILQ_REMOVE(&pending_requests.requests, cur, next); + free(cur); + } else if (action == ACTION_TRIGGER) { + TAILQ_REMOVE(&pending_requests.requests, cur, next); + trigger = cur; + break; + } + } + return trigger; +} + +static void +wait_for_async_messages(void) +{ + struct pending_request *sr; + struct timespec timeout; + bool timedwait = false; + bool nowait = false; + int ret; + + /* scan through the list and see if there are any timeouts that + * are earlier than our current timeout. + */ + TAILQ_FOREACH(sr, &pending_requests.requests, next) { + if (sr->type != REQUEST_TYPE_ASYNC) + continue; + if (!timedwait || timespec_cmp(&sr->async.param->end, + &timeout) < 0) { + memcpy(&timeout, &sr->async.param->end, + sizeof(timeout)); + timedwait = true; + } + + /* sometimes, we don't even wait */ + if (sr->reply_received) { + nowait = true; + break; + } + } + + if (nowait) + return; + + do { + ret = timedwait ? + pthread_cond_timedwait( + &pending_requests.async_cond, + &pending_requests.lock, + &timeout) : + pthread_cond_wait( + &pending_requests.async_cond, + &pending_requests.lock); + } while (ret != 0 && ret != ETIMEDOUT); + + /* we've been woken up or timed out */ +} + static void * async_reply_handle(void *arg __rte_unused) { - struct pending_request *sr; struct timeval now; - struct timespec timeout, ts_now; + struct timespec ts_now; while (1) { struct pending_request *trigger = NULL; - int ret; - bool nowait = false; - bool timedwait = false; pthread_mutex_lock(&pending_requests.lock); - /* scan through the list and see if there are any timeouts that - * are earlier than our current timeout. - */ - TAILQ_FOREACH(sr, &pending_requests.requests, next) { - if (sr->type != REQUEST_TYPE_ASYNC) - continue; - if (!timedwait || timespec_cmp(&sr->async.param->end, - &timeout) < 0) { - memcpy(&timeout, &sr->async.param->end, - sizeof(timeout)); - timedwait = true; - } - - /* sometimes, we don't even wait */ - if (sr->reply_received) { - nowait = true; - break; - } - } - - if (nowait) - ret = 0; - else if (timedwait) - ret = pthread_cond_timedwait( - &pending_requests.async_cond, - &pending_requests.lock, &timeout); - else - ret = pthread_cond_wait(&pending_requests.async_cond, - &pending_requests.lock); + /* we exit this function holding the lock */ + wait_for_async_messages(); if (gettimeofday(&now, NULL) < 0) { + pthread_mutex_unlock(&pending_requests.lock); RTE_LOG(ERR, EAL, "Cannot get current time\n"); break; } ts_now.tv_nsec = now.tv_usec * 1000; ts_now.tv_sec = now.tv_sec; - if (ret == 0 || ret == ETIMEDOUT) { - struct pending_request *next; - /* we've either been woken up, or we timed out */ + do { + trigger = check_trigger(&ts_now); + /* unlock request list */ + pthread_mutex_unlock(&pending_requests.lock); - /* we have still the lock, check if anything needs - * processing. - */ - TAILQ_FOREACH_SAFE(sr, &pending_requests.requests, next, - next) { - enum async_action action; - if (sr->type != REQUEST_TYPE_ASYNC) - continue; - - action = process_async_request(sr, &ts_now); - if (action == ACTION_FREE) { - TAILQ_REMOVE(&pending_requests.requests, - sr, next); - free(sr); - } else if (action == ACTION_TRIGGER && - trigger == NULL) { - TAILQ_REMOVE(&pending_requests.requests, - sr, next); - trigger = sr; - } + if (trigger) { + trigger_async_action(trigger); + free(trigger); + + /* we've triggered a callback, but there may be + * more, so lock the list and check again. + */ + pthread_mutex_lock(&pending_requests.lock); } - } - pthread_mutex_unlock(&pending_requests.lock); - if (trigger) { - trigger_async_action(trigger); - free(trigger); - } - }; + } while (trigger); + } RTE_LOG(ERR, EAL, "ERROR: asynchronous requests disabled\n"); @@ -597,11 +621,11 @@ rte_mp_channel_init(void) /* create filter path */ create_socket_path("*", path, sizeof(path)); - snprintf(mp_filter, sizeof(mp_filter), "%s", basename(path)); + strlcpy(mp_filter, basename(path), sizeof(mp_filter)); /* path may have been modified, so recreate it */ create_socket_path("*", path, sizeof(path)); - snprintf(mp_dir_path, sizeof(mp_dir_path), "%s", dirname(path)); + strlcpy(mp_dir_path, dirname(path), sizeof(mp_dir_path)); /* lock the directory */ dir_fd = open(mp_dir_path, O_RDONLY); @@ -649,11 +673,11 @@ rte_mp_channel_init(void) } /* try best to set thread name */ - snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN, "rte_mp_handle"); + strlcpy(thread_name, "rte_mp_handle", RTE_MAX_THREAD_NAME_LEN); rte_thread_setname(mp_handle_tid, thread_name); /* try best to set thread name */ - snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN, "rte_mp_async_handle"); + strlcpy(thread_name, "rte_mp_async_handle", RTE_MAX_THREAD_NAME_LEN); rte_thread_setname(async_reply_handle_tid, thread_name); /* unlock the directory */ @@ -686,7 +710,7 @@ send_msg(const char *dst_path, struct rte_mp_msg *msg, int type) memset(&dst, 0, sizeof(dst)); dst.sun_family = AF_UNIX; - snprintf(dst.sun_path, sizeof(dst.sun_path), "%s", dst_path); + strlcpy(dst.sun_path, dst_path, sizeof(dst.sun_path)); memset(&msgh, 0, sizeof(msgh)); memset(control, 0, sizeof(control)); @@ -846,7 +870,7 @@ mp_request_async(const char *dst, struct rte_mp_msg *req, memset(reply_msg, 0, sizeof(*reply_msg)); sync_req->type = REQUEST_TYPE_ASYNC; - strcpy(sync_req->dst, dst); + strlcpy(sync_req->dst, dst, sizeof(sync_req->dst)); sync_req->request = req; sync_req->reply = reply_msg; sync_req->async.param = param; @@ -854,9 +878,7 @@ mp_request_async(const char *dst, struct rte_mp_msg *req, /* queue already locked by caller */ exist = find_sync_request(dst, req->name); - if (!exist) { - TAILQ_INSERT_TAIL(&pending_requests.requests, sync_req, next); - } else { + if (exist) { RTE_LOG(ERR, EAL, "A pending request %s:%s\n", dst, req->name); rte_errno = EEXIST; ret = -1; @@ -873,6 +895,7 @@ mp_request_async(const char *dst, struct rte_mp_msg *req, ret = 0; goto fail; } + TAILQ_INSERT_TAIL(&pending_requests.requests, sync_req, next); param->user_reply.nb_sent++; @@ -893,19 +916,15 @@ mp_request_sync(const char *dst, struct rte_mp_msg *req, sync_req.type = REQUEST_TYPE_SYNC; sync_req.reply_received = 0; - strcpy(sync_req.dst, dst); + strlcpy(sync_req.dst, dst, sizeof(sync_req.dst)); sync_req.request = req; sync_req.reply = &msg; pthread_cond_init(&sync_req.sync.cond, NULL); - pthread_mutex_lock(&pending_requests.lock); exist = find_sync_request(dst, req->name); - if (!exist) - TAILQ_INSERT_TAIL(&pending_requests.requests, &sync_req, next); if (exist) { RTE_LOG(ERR, EAL, "A pending request %s:%s\n", dst, req->name); rte_errno = EEXIST; - pthread_mutex_unlock(&pending_requests.lock); return -1; } @@ -917,6 +936,8 @@ mp_request_sync(const char *dst, struct rte_mp_msg *req, } else if (ret == 0) return 0; + TAILQ_INSERT_TAIL(&pending_requests.requests, &sync_req, next); + reply->nb_sent++; do { @@ -924,9 +945,7 @@ mp_request_sync(const char *dst, struct rte_mp_msg *req, &pending_requests.lock, ts); } while (ret != 0 && ret != ETIMEDOUT); - /* We got the lock now */ TAILQ_REMOVE(&pending_requests.requests, &sync_req, next); - pthread_mutex_unlock(&pending_requests.lock); if (sync_req.reply_received == 0) { RTE_LOG(ERR, EAL, "Fail to recv reply for request %s:%s\n", @@ -985,8 +1004,12 @@ rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply, reply->msgs = NULL; /* for secondary process, send request to the primary process only */ - if (rte_eal_process_type() == RTE_PROC_SECONDARY) - return mp_request_sync(eal_mp_socket_path(), req, reply, &end); + if (rte_eal_process_type() == RTE_PROC_SECONDARY) { + pthread_mutex_lock(&pending_requests.lock); + ret = mp_request_sync(eal_mp_socket_path(), req, reply, &end); + pthread_mutex_unlock(&pending_requests.lock); + return ret; + } /* for primary process, broadcast request, and collect reply 1 by 1 */ mp_dir = opendir(mp_dir_path); @@ -1006,6 +1029,7 @@ rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply, return -1; } + pthread_mutex_lock(&pending_requests.lock); while ((ent = readdir(mp_dir))) { char path[PATH_MAX]; @@ -1015,9 +1039,13 @@ rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply, snprintf(path, sizeof(path), "%s/%s", mp_dir_path, ent->d_name); + /* unlocks the mutex while waiting for response, + * locks on receive + */ if (mp_request_sync(path, req, reply, &end)) ret = -1; } + pthread_mutex_unlock(&pending_requests.lock); /* unlock the directory */ flock(dir_fd, LOCK_UN);