1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2018 Intel Corporation
18 #include <sys/types.h>
19 #include <sys/socket.h>
23 #include <rte_common.h>
24 #include <rte_cycles.h>
26 #include <rte_errno.h>
27 #include <rte_lcore.h>
29 #include <rte_tailq.h>
31 #include "eal_private.h"
32 #include "eal_filesystem.h"
33 #include "eal_internal_cfg.h"
35 static int mp_fd = -1;
36 static char mp_filter[PATH_MAX]; /* Filter for secondary process sockets */
37 static char mp_dir_path[PATH_MAX]; /* The directory path for all mp sockets */
38 static pthread_mutex_t mp_mutex_action = PTHREAD_MUTEX_INITIALIZER;
41 TAILQ_ENTRY(action_entry) next;
42 char action_name[RTE_MP_MAX_NAME_LEN];
46 /** Double linked list of actions. */
47 TAILQ_HEAD(action_entry_list, action_entry);
49 static struct action_entry_list action_entry_list =
50 TAILQ_HEAD_INITIALIZER(action_entry_list);
53 MP_MSG, /* Share message with peers, will not block */
54 MP_REQ, /* Request for information, Will block for a reply */
55 MP_REP, /* Response to previously-received request */
56 MP_IGN, /* Response telling requester to ignore this response */
59 struct mp_msg_internal {
61 struct rte_mp_msg msg;
64 struct async_request_param {
65 rte_mp_async_reply_t clb;
66 struct rte_mp_reply user_reply;
68 int n_responses_processed;
71 struct pending_request {
72 TAILQ_ENTRY(pending_request) next;
78 struct rte_mp_msg *request;
79 struct rte_mp_msg *reply;
84 struct async_request_param *param;
92 TAILQ_HEAD(pending_request_list, pending_request);
95 struct pending_request_list requests;
97 pthread_cond_t async_cond;
98 } pending_requests = {
99 .requests = TAILQ_HEAD_INITIALIZER(pending_requests.requests),
100 .lock = PTHREAD_MUTEX_INITIALIZER,
101 .async_cond = PTHREAD_COND_INITIALIZER
102 /**< used in async requests only */
105 /* forward declarations */
107 mp_send(struct rte_mp_msg *msg, const char *peer, int type);
110 static struct pending_request *
111 find_pending_request(const char *dst, const char *act_name)
113 struct pending_request *r;
115 TAILQ_FOREACH(r, &pending_requests.requests, next) {
116 if (!strcmp(r->dst, dst) &&
117 !strcmp(r->request->name, act_name))
125 create_socket_path(const char *name, char *buf, int len)
127 const char *prefix = eal_mp_socket_path();
129 if (strlen(name) > 0)
130 snprintf(buf, len, "%s_%s", prefix, name);
132 strlcpy(buf, prefix, len);
136 rte_eal_primary_proc_alive(const char *config_file_path)
140 if (config_file_path)
141 config_fd = open(config_file_path, O_RDONLY);
145 path = eal_runtime_config_path();
146 config_fd = open(path, O_RDONLY);
151 int ret = lockf(config_fd, F_TEST, 0);
157 static struct action_entry *
158 find_action_entry_by_name(const char *name)
160 struct action_entry *entry;
162 TAILQ_FOREACH(entry, &action_entry_list, next) {
163 if (strncmp(entry->action_name, name, RTE_MP_MAX_NAME_LEN) == 0)
171 validate_action_name(const char *name)
174 RTE_LOG(ERR, EAL, "Action name cannot be NULL\n");
178 if (strnlen(name, RTE_MP_MAX_NAME_LEN) == 0) {
179 RTE_LOG(ERR, EAL, "Length of action name is zero\n");
183 if (strnlen(name, RTE_MP_MAX_NAME_LEN) == RTE_MP_MAX_NAME_LEN) {
190 int __rte_experimental
191 rte_mp_action_register(const char *name, rte_mp_t action)
193 struct action_entry *entry;
195 if (validate_action_name(name))
198 entry = malloc(sizeof(struct action_entry));
203 strlcpy(entry->action_name, name, sizeof(entry->action_name));
204 entry->action = action;
206 pthread_mutex_lock(&mp_mutex_action);
207 if (find_action_entry_by_name(name) != NULL) {
208 pthread_mutex_unlock(&mp_mutex_action);
213 TAILQ_INSERT_TAIL(&action_entry_list, entry, next);
214 pthread_mutex_unlock(&mp_mutex_action);
218 void __rte_experimental
219 rte_mp_action_unregister(const char *name)
221 struct action_entry *entry;
223 if (validate_action_name(name))
226 pthread_mutex_lock(&mp_mutex_action);
227 entry = find_action_entry_by_name(name);
229 pthread_mutex_unlock(&mp_mutex_action);
232 TAILQ_REMOVE(&action_entry_list, entry, next);
233 pthread_mutex_unlock(&mp_mutex_action);
238 read_msg(struct mp_msg_internal *m, struct sockaddr_un *s)
243 char control[CMSG_SPACE(sizeof(m->msg.fds))];
244 struct cmsghdr *cmsg;
245 int buflen = sizeof(*m) - sizeof(m->msg.fds);
247 memset(&msgh, 0, sizeof(msgh));
249 iov.iov_len = buflen;
252 msgh.msg_namelen = sizeof(*s);
255 msgh.msg_control = control;
256 msgh.msg_controllen = sizeof(control);
258 msglen = recvmsg(mp_fd, &msgh, 0);
260 RTE_LOG(ERR, EAL, "recvmsg failed, %s\n", strerror(errno));
264 if (msglen != buflen || (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) {
265 RTE_LOG(ERR, EAL, "truncted msg\n");
269 /* read auxiliary FDs if any */
270 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
271 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
272 if ((cmsg->cmsg_level == SOL_SOCKET) &&
273 (cmsg->cmsg_type == SCM_RIGHTS)) {
274 memcpy(m->msg.fds, CMSG_DATA(cmsg), sizeof(m->msg.fds));
283 process_msg(struct mp_msg_internal *m, struct sockaddr_un *s)
285 struct pending_request *pending_req;
286 struct action_entry *entry;
287 struct rte_mp_msg *msg = &m->msg;
288 rte_mp_t action = NULL;
290 RTE_LOG(DEBUG, EAL, "msg: %s\n", msg->name);
292 if (m->type == MP_REP || m->type == MP_IGN) {
293 pthread_mutex_lock(&pending_requests.lock);
294 pending_req = find_pending_request(s->sun_path, msg->name);
296 memcpy(pending_req->reply, msg, sizeof(*msg));
297 /* -1 indicates that we've been asked to ignore */
298 pending_req->reply_received =
299 m->type == MP_REP ? 1 : -1;
301 if (pending_req->type == REQUEST_TYPE_SYNC)
302 pthread_cond_signal(&pending_req->sync.cond);
303 else if (pending_req->type == REQUEST_TYPE_ASYNC)
305 &pending_requests.async_cond);
307 RTE_LOG(ERR, EAL, "Drop mp reply: %s\n", msg->name);
308 pthread_mutex_unlock(&pending_requests.lock);
312 pthread_mutex_lock(&mp_mutex_action);
313 entry = find_action_entry_by_name(msg->name);
315 action = entry->action;
316 pthread_mutex_unlock(&mp_mutex_action);
319 if (m->type == MP_REQ && !internal_config.init_complete) {
320 /* if this is a request, and init is not yet complete,
321 * and callback wasn't registered, we should tell the
322 * requester to ignore our existence because we're not
323 * yet ready to process this request.
325 struct rte_mp_msg dummy;
327 memset(&dummy, 0, sizeof(dummy));
328 strlcpy(dummy.name, msg->name, sizeof(dummy.name));
329 mp_send(&dummy, s->sun_path, MP_IGN);
331 RTE_LOG(ERR, EAL, "Cannot find action: %s\n",
334 } else if (action(msg, s->sun_path) < 0) {
335 RTE_LOG(ERR, EAL, "Fail to handle message: %s\n", msg->name);
340 mp_handle(void *arg __rte_unused)
342 struct mp_msg_internal msg;
343 struct sockaddr_un sa;
346 if (read_msg(&msg, &sa) == 0)
347 process_msg(&msg, &sa);
354 timespec_cmp(const struct timespec *a, const struct timespec *b)
356 if (a->tv_sec < b->tv_sec)
358 if (a->tv_sec > b->tv_sec)
360 if (a->tv_nsec < b->tv_nsec)
362 if (a->tv_nsec > b->tv_nsec)
368 ACTION_NONE, /**< don't do anything */
369 ACTION_FREE, /**< free the action entry, but don't trigger callback */
370 ACTION_TRIGGER /**< trigger callback, then free action entry */
373 static enum async_action
374 process_async_request(struct pending_request *sr, const struct timespec *now)
376 struct async_request_param *param;
377 struct rte_mp_reply *reply;
378 bool timeout, received, last_msg;
380 param = sr->async.param;
381 reply = ¶m->user_reply;
383 /* did we timeout? */
384 timeout = timespec_cmp(¶m->end, now) <= 0;
386 /* did we receive a response? */
387 received = sr->reply_received != 0;
389 /* if we didn't time out, and we didn't receive a response, ignore */
390 if (!timeout && !received)
393 /* if we received a response, adjust relevant data and copy mesasge. */
394 if (sr->reply_received == 1 && sr->reply) {
395 struct rte_mp_msg *msg, *user_msgs, *tmp;
398 user_msgs = reply->msgs;
400 tmp = realloc(user_msgs, sizeof(*msg) *
401 (reply->nb_received + 1));
403 RTE_LOG(ERR, EAL, "Fail to alloc reply for request %s:%s\n",
404 sr->dst, sr->request->name);
405 /* this entry is going to be removed and its message
406 * dropped, but we don't want to leak memory, so
411 reply->msgs = user_msgs;
412 memcpy(&user_msgs[reply->nb_received],
414 reply->nb_received++;
417 /* mark this request as processed */
418 param->n_responses_processed++;
419 } else if (sr->reply_received == -1) {
420 /* we were asked to ignore this process */
425 last_msg = param->n_responses_processed == reply->nb_sent;
427 return last_msg ? ACTION_TRIGGER : ACTION_FREE;
431 trigger_async_action(struct pending_request *sr)
433 struct async_request_param *param;
434 struct rte_mp_reply *reply;
436 param = sr->async.param;
437 reply = ¶m->user_reply;
439 param->clb(sr->request, reply);
442 free(sr->async.param->user_reply.msgs);
443 free(sr->async.param);
447 static struct pending_request *
448 check_trigger(struct timespec *ts)
450 struct pending_request *next, *cur, *trigger = NULL;
452 TAILQ_FOREACH_SAFE(cur, &pending_requests.requests, next, next) {
453 enum async_action action;
454 if (cur->type != REQUEST_TYPE_ASYNC)
457 action = process_async_request(cur, ts);
458 if (action == ACTION_FREE) {
459 TAILQ_REMOVE(&pending_requests.requests, cur, next);
461 } else if (action == ACTION_TRIGGER) {
462 TAILQ_REMOVE(&pending_requests.requests, cur, next);
471 wait_for_async_messages(void)
473 struct pending_request *sr;
474 struct timespec timeout;
475 bool timedwait = false;
479 /* scan through the list and see if there are any timeouts that
480 * are earlier than our current timeout.
482 TAILQ_FOREACH(sr, &pending_requests.requests, next) {
483 if (sr->type != REQUEST_TYPE_ASYNC)
485 if (!timedwait || timespec_cmp(&sr->async.param->end,
487 memcpy(&timeout, &sr->async.param->end,
492 /* sometimes, we don't even wait */
493 if (sr->reply_received) {
504 pthread_cond_timedwait(
505 &pending_requests.async_cond,
506 &pending_requests.lock,
509 &pending_requests.async_cond,
510 &pending_requests.lock);
511 } while (ret != 0 && ret != ETIMEDOUT);
513 /* we've been woken up or timed out */
517 async_reply_handle(void *arg __rte_unused)
520 struct timespec ts_now;
522 struct pending_request *trigger = NULL;
524 pthread_mutex_lock(&pending_requests.lock);
526 /* we exit this function holding the lock */
527 wait_for_async_messages();
529 if (gettimeofday(&now, NULL) < 0) {
530 pthread_mutex_unlock(&pending_requests.lock);
531 RTE_LOG(ERR, EAL, "Cannot get current time\n");
534 ts_now.tv_nsec = now.tv_usec * 1000;
535 ts_now.tv_sec = now.tv_sec;
538 trigger = check_trigger(&ts_now);
539 /* unlock request list */
540 pthread_mutex_unlock(&pending_requests.lock);
543 trigger_async_action(trigger);
546 /* we've triggered a callback, but there may be
547 * more, so lock the list and check again.
549 pthread_mutex_lock(&pending_requests.lock);
554 RTE_LOG(ERR, EAL, "ERROR: asynchronous requests disabled\n");
562 char peer_name[PATH_MAX] = {0};
563 struct sockaddr_un un;
565 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
566 snprintf(peer_name, sizeof(peer_name),
567 "%d_%"PRIx64, getpid(), rte_rdtsc());
569 mp_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
571 RTE_LOG(ERR, EAL, "failed to create unix socket\n");
575 memset(&un, 0, sizeof(un));
576 un.sun_family = AF_UNIX;
578 create_socket_path(peer_name, un.sun_path, sizeof(un.sun_path));
580 unlink(un.sun_path); /* May still exist since last run */
582 if (bind(mp_fd, (struct sockaddr *)&un, sizeof(un)) < 0) {
583 RTE_LOG(ERR, EAL, "failed to bind %s: %s\n",
584 un.sun_path, strerror(errno));
589 RTE_LOG(INFO, EAL, "Multi-process socket %s\n", un.sun_path);
594 unlink_sockets(const char *filter)
600 mp_dir = opendir(mp_dir_path);
602 RTE_LOG(ERR, EAL, "Unable to open directory %s\n", mp_dir_path);
605 dir_fd = dirfd(mp_dir);
607 while ((ent = readdir(mp_dir))) {
608 if (fnmatch(filter, ent->d_name, 0) == 0)
609 unlinkat(dir_fd, ent->d_name, 0);
617 rte_mp_channel_init(void)
619 char thread_name[RTE_MAX_THREAD_NAME_LEN];
622 pthread_t mp_handle_tid, async_reply_handle_tid;
624 /* create filter path */
625 create_socket_path("*", path, sizeof(path));
626 strlcpy(mp_filter, basename(path), sizeof(mp_filter));
628 /* path may have been modified, so recreate it */
629 create_socket_path("*", path, sizeof(path));
630 strlcpy(mp_dir_path, dirname(path), sizeof(mp_dir_path));
632 /* lock the directory */
633 dir_fd = open(mp_dir_path, O_RDONLY);
635 RTE_LOG(ERR, EAL, "failed to open %s: %s\n",
636 mp_dir_path, strerror(errno));
640 if (flock(dir_fd, LOCK_EX)) {
641 RTE_LOG(ERR, EAL, "failed to lock %s: %s\n",
642 mp_dir_path, strerror(errno));
647 if (rte_eal_process_type() == RTE_PROC_PRIMARY &&
648 unlink_sockets(mp_filter)) {
649 RTE_LOG(ERR, EAL, "failed to unlink mp sockets\n");
654 if (open_socket_fd() < 0) {
659 if (pthread_create(&mp_handle_tid, NULL, mp_handle, NULL) < 0) {
660 RTE_LOG(ERR, EAL, "failed to create mp thead: %s\n",
668 if (pthread_create(&async_reply_handle_tid, NULL,
669 async_reply_handle, NULL) < 0) {
670 RTE_LOG(ERR, EAL, "failed to create mp thead: %s\n",
678 /* try best to set thread name */
679 strlcpy(thread_name, "rte_mp_handle", RTE_MAX_THREAD_NAME_LEN);
680 rte_thread_setname(mp_handle_tid, thread_name);
682 /* try best to set thread name */
683 strlcpy(thread_name, "rte_mp_async_handle", RTE_MAX_THREAD_NAME_LEN);
684 rte_thread_setname(async_reply_handle_tid, thread_name);
686 /* unlock the directory */
687 flock(dir_fd, LOCK_UN);
694 * Return -1, as fail to send message and it's caused by the local side.
695 * Return 0, as fail to send message and it's caused by the remote side.
696 * Return 1, as succeed to send message.
700 send_msg(const char *dst_path, struct rte_mp_msg *msg, int type)
705 struct cmsghdr *cmsg;
706 struct sockaddr_un dst;
707 struct mp_msg_internal m;
708 int fd_size = msg->num_fds * sizeof(int);
709 char control[CMSG_SPACE(fd_size)];
712 memcpy(&m.msg, msg, sizeof(*msg));
714 memset(&dst, 0, sizeof(dst));
715 dst.sun_family = AF_UNIX;
716 strlcpy(dst.sun_path, dst_path, sizeof(dst.sun_path));
718 memset(&msgh, 0, sizeof(msgh));
719 memset(control, 0, sizeof(control));
722 iov.iov_len = sizeof(m) - sizeof(msg->fds);
724 msgh.msg_name = &dst;
725 msgh.msg_namelen = sizeof(dst);
728 msgh.msg_control = control;
729 msgh.msg_controllen = sizeof(control);
731 cmsg = CMSG_FIRSTHDR(&msgh);
732 cmsg->cmsg_len = CMSG_LEN(fd_size);
733 cmsg->cmsg_level = SOL_SOCKET;
734 cmsg->cmsg_type = SCM_RIGHTS;
735 memcpy(CMSG_DATA(cmsg), msg->fds, fd_size);
738 snd = sendmsg(mp_fd, &msgh, 0);
739 } while (snd < 0 && errno == EINTR);
743 /* Check if it caused by peer process exits */
744 if (errno == ECONNREFUSED &&
745 rte_eal_process_type() == RTE_PROC_PRIMARY) {
749 if (errno == ENOBUFS) {
750 RTE_LOG(ERR, EAL, "Peer cannot receive message %s\n",
754 RTE_LOG(ERR, EAL, "failed to send to (%s) due to %s\n",
755 dst_path, strerror(errno));
763 mp_send(struct rte_mp_msg *msg, const char *peer, int type)
769 if (!peer && (rte_eal_process_type() == RTE_PROC_SECONDARY))
770 peer = eal_mp_socket_path();
773 if (send_msg(peer, msg, type) < 0)
779 /* broadcast to all secondary processes */
780 mp_dir = opendir(mp_dir_path);
782 RTE_LOG(ERR, EAL, "Unable to open directory %s\n",
788 dir_fd = dirfd(mp_dir);
789 /* lock the directory to prevent processes spinning up while we send */
790 if (flock(dir_fd, LOCK_EX)) {
791 RTE_LOG(ERR, EAL, "Unable to lock directory %s\n",
798 while ((ent = readdir(mp_dir))) {
801 if (fnmatch(mp_filter, ent->d_name, 0) != 0)
804 snprintf(path, sizeof(path), "%s/%s", mp_dir_path,
806 if (send_msg(path, msg, type) < 0)
810 flock(dir_fd, LOCK_UN);
812 /* dir_fd automatically closed on closedir */
818 check_input(const struct rte_mp_msg *msg)
821 RTE_LOG(ERR, EAL, "Msg cannot be NULL\n");
826 if (validate_action_name(msg->name))
829 if (msg->len_param > RTE_MP_MAX_PARAM_LEN) {
830 RTE_LOG(ERR, EAL, "Message data is too long\n");
835 if (msg->num_fds > RTE_MP_MAX_FD_NUM) {
836 RTE_LOG(ERR, EAL, "Cannot send more than %d FDs\n",
845 int __rte_experimental
846 rte_mp_sendmsg(struct rte_mp_msg *msg)
848 if (!check_input(msg))
851 RTE_LOG(DEBUG, EAL, "sendmsg: %s\n", msg->name);
852 return mp_send(msg, NULL, MP_MSG);
856 mp_request_async(const char *dst, struct rte_mp_msg *req,
857 struct async_request_param *param)
859 struct rte_mp_msg *reply_msg;
860 struct pending_request *pending_req, *exist;
863 pending_req = calloc(1, sizeof(*pending_req));
864 reply_msg = calloc(1, sizeof(*reply_msg));
865 if (pending_req == NULL || reply_msg == NULL) {
866 RTE_LOG(ERR, EAL, "Could not allocate space for sync request\n");
872 pending_req->type = REQUEST_TYPE_ASYNC;
873 strlcpy(pending_req->dst, dst, sizeof(pending_req->dst));
874 strcpy(pending_req->dst, dst);
875 pending_req->request = req;
876 pending_req->reply = reply_msg;
877 pending_req->async.param = param;
879 /* queue already locked by caller */
881 exist = find_pending_request(dst, req->name);
883 RTE_LOG(ERR, EAL, "A pending request %s:%s\n", dst, req->name);
889 ret = send_msg(dst, req, MP_REQ);
891 RTE_LOG(ERR, EAL, "Fail to send request %s:%s\n",
895 } else if (ret == 0) {
899 TAILQ_INSERT_TAIL(&pending_requests.requests, pending_req, next);
901 param->user_reply.nb_sent++;
911 mp_request_sync(const char *dst, struct rte_mp_msg *req,
912 struct rte_mp_reply *reply, const struct timespec *ts)
915 struct rte_mp_msg msg, *tmp;
916 struct pending_request pending_req, *exist;
918 pending_req.type = REQUEST_TYPE_SYNC;
919 pending_req.reply_received = 0;
920 strlcpy(pending_req.dst, dst, sizeof(pending_req.dst));
921 pending_req.request = req;
922 pending_req.reply = &msg;
923 pthread_cond_init(&pending_req.sync.cond, NULL);
925 exist = find_pending_request(dst, req->name);
927 RTE_LOG(ERR, EAL, "A pending request %s:%s\n", dst, req->name);
932 ret = send_msg(dst, req, MP_REQ);
934 RTE_LOG(ERR, EAL, "Fail to send request %s:%s\n",
940 TAILQ_INSERT_TAIL(&pending_requests.requests, &pending_req, next);
945 ret = pthread_cond_timedwait(&pending_req.sync.cond,
946 &pending_requests.lock, ts);
947 } while (ret != 0 && ret != ETIMEDOUT);
949 TAILQ_REMOVE(&pending_requests.requests, &pending_req, next);
951 if (pending_req.reply_received == 0) {
952 RTE_LOG(ERR, EAL, "Fail to recv reply for request %s:%s\n",
954 rte_errno = ETIMEDOUT;
957 if (pending_req.reply_received == -1) {
958 RTE_LOG(DEBUG, EAL, "Asked to ignore response\n");
959 /* not receiving this message is not an error, so decrement
960 * number of sent messages
966 tmp = realloc(reply->msgs, sizeof(msg) * (reply->nb_received + 1));
968 RTE_LOG(ERR, EAL, "Fail to alloc reply for request %s:%s\n",
973 memcpy(&tmp[reply->nb_received], &msg, sizeof(msg));
975 reply->nb_received++;
979 int __rte_experimental
980 rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply,
981 const struct timespec *ts)
989 RTE_LOG(DEBUG, EAL, "request: %s\n", req->name);
991 if (check_input(req) == false)
993 if (gettimeofday(&now, NULL) < 0) {
994 RTE_LOG(ERR, EAL, "Faile to get current time\n");
999 end.tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
1000 end.tv_sec = now.tv_sec + ts->tv_sec +
1001 (now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
1004 reply->nb_received = 0;
1007 /* for secondary process, send request to the primary process only */
1008 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1009 pthread_mutex_lock(&pending_requests.lock);
1010 ret = mp_request_sync(eal_mp_socket_path(), req, reply, &end);
1011 pthread_mutex_unlock(&pending_requests.lock);
1015 /* for primary process, broadcast request, and collect reply 1 by 1 */
1016 mp_dir = opendir(mp_dir_path);
1018 RTE_LOG(ERR, EAL, "Unable to open directory %s\n", mp_dir_path);
1023 dir_fd = dirfd(mp_dir);
1024 /* lock the directory to prevent processes spinning up while we send */
1025 if (flock(dir_fd, LOCK_EX)) {
1026 RTE_LOG(ERR, EAL, "Unable to lock directory %s\n",
1033 pthread_mutex_lock(&pending_requests.lock);
1034 while ((ent = readdir(mp_dir))) {
1035 char path[PATH_MAX];
1037 if (fnmatch(mp_filter, ent->d_name, 0) != 0)
1040 snprintf(path, sizeof(path), "%s/%s", mp_dir_path,
1043 /* unlocks the mutex while waiting for response,
1046 if (mp_request_sync(path, req, reply, &end))
1049 pthread_mutex_unlock(&pending_requests.lock);
1050 /* unlock the directory */
1051 flock(dir_fd, LOCK_UN);
1053 /* dir_fd automatically closed on closedir */
1058 int __rte_experimental
1059 rte_mp_request_async(struct rte_mp_msg *req, const struct timespec *ts,
1060 rte_mp_async_reply_t clb)
1062 struct rte_mp_msg *copy;
1063 struct pending_request *dummy;
1064 struct async_request_param *param;
1065 struct rte_mp_reply *reply;
1066 int dir_fd, ret = 0;
1070 struct timespec *end;
1071 bool dummy_used = false;
1073 RTE_LOG(DEBUG, EAL, "request: %s\n", req->name);
1075 if (check_input(req) == false)
1077 if (gettimeofday(&now, NULL) < 0) {
1078 RTE_LOG(ERR, EAL, "Faile to get current time\n");
1082 copy = calloc(1, sizeof(*copy));
1083 dummy = calloc(1, sizeof(*dummy));
1084 param = calloc(1, sizeof(*param));
1085 if (copy == NULL || dummy == NULL || param == NULL) {
1086 RTE_LOG(ERR, EAL, "Failed to allocate memory for async reply\n");
1092 memcpy(copy, req, sizeof(*copy));
1094 param->n_responses_processed = 0;
1097 reply = ¶m->user_reply;
1099 end->tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
1100 end->tv_sec = now.tv_sec + ts->tv_sec +
1101 (now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
1103 reply->nb_received = 0;
1106 /* we have to lock the request queue here, as we will be adding a bunch
1107 * of requests to the queue at once, and some of the replies may arrive
1108 * before we add all of the requests to the queue.
1110 pthread_mutex_lock(&pending_requests.lock);
1112 /* we have to ensure that callback gets triggered even if we don't send
1113 * anything, therefore earlier we have allocated a dummy request. fill
1114 * it, and put it on the queue if we don't send any requests.
1116 dummy->type = REQUEST_TYPE_ASYNC;
1117 dummy->request = copy;
1118 dummy->reply = NULL;
1119 dummy->async.param = param;
1120 dummy->reply_received = 1; /* short-circuit the timeout */
1122 /* for secondary process, send request to the primary process only */
1123 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1124 ret = mp_request_async(eal_mp_socket_path(), copy, param);
1126 /* if we didn't send anything, put dummy request on the queue */
1127 if (ret == 0 && reply->nb_sent == 0) {
1128 TAILQ_INSERT_TAIL(&pending_requests.requests, dummy,
1133 pthread_mutex_unlock(&pending_requests.lock);
1135 /* if we couldn't send anything, clean up */
1141 /* for primary process, broadcast request */
1142 mp_dir = opendir(mp_dir_path);
1144 RTE_LOG(ERR, EAL, "Unable to open directory %s\n", mp_dir_path);
1148 dir_fd = dirfd(mp_dir);
1150 /* lock the directory to prevent processes spinning up while we send */
1151 if (flock(dir_fd, LOCK_EX)) {
1152 RTE_LOG(ERR, EAL, "Unable to lock directory %s\n",
1158 while ((ent = readdir(mp_dir))) {
1159 char path[PATH_MAX];
1161 if (fnmatch(mp_filter, ent->d_name, 0) != 0)
1164 snprintf(path, sizeof(path), "%s/%s", mp_dir_path,
1167 if (mp_request_async(path, copy, param))
1170 /* if we didn't send anything, put dummy request on the queue */
1171 if (ret == 0 && reply->nb_sent == 0) {
1172 TAILQ_INSERT_HEAD(&pending_requests.requests, dummy, next);
1176 /* trigger async request thread wake up */
1177 pthread_cond_signal(&pending_requests.async_cond);
1179 /* finally, unlock the queue */
1180 pthread_mutex_unlock(&pending_requests.lock);
1182 /* unlock the directory */
1183 flock(dir_fd, LOCK_UN);
1185 /* dir_fd automatically closed on closedir */
1188 /* if dummy was unused, free it */
1196 pthread_mutex_unlock(&pending_requests.lock);
1204 int __rte_experimental
1205 rte_mp_reply(struct rte_mp_msg *msg, const char *peer)
1207 RTE_LOG(DEBUG, EAL, "reply: %s\n", msg->name);
1209 if (check_input(msg) == false)
1213 RTE_LOG(ERR, EAL, "peer is not specified\n");
1218 return mp_send(msg, peer, MP_REP);