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_sync_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 snprintf(buf, len, "%s", prefix);
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 strcpy(entry->action_name, 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 *sync_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 sync_req = find_sync_request(s->sun_path, msg->name);
296 memcpy(sync_req->reply, msg, sizeof(*msg));
297 /* -1 indicates that we've been asked to ignore */
298 sync_req->reply_received = m->type == MP_REP ? 1 : -1;
300 if (sync_req->type == REQUEST_TYPE_SYNC)
301 pthread_cond_signal(&sync_req->sync.cond);
302 else if (sync_req->type == REQUEST_TYPE_ASYNC)
304 &pending_requests.async_cond);
306 RTE_LOG(ERR, EAL, "Drop mp reply: %s\n", msg->name);
307 pthread_mutex_unlock(&pending_requests.lock);
311 pthread_mutex_lock(&mp_mutex_action);
312 entry = find_action_entry_by_name(msg->name);
314 action = entry->action;
315 pthread_mutex_unlock(&mp_mutex_action);
318 if (m->type == MP_REQ && !internal_config.init_complete) {
319 /* if this is a request, and init is not yet complete,
320 * and callback wasn't registered, we should tell the
321 * requester to ignore our existence because we're not
322 * yet ready to process this request.
324 struct rte_mp_msg dummy;
325 memset(&dummy, 0, sizeof(dummy));
326 mp_send(&dummy, s->sun_path, MP_IGN);
328 RTE_LOG(ERR, EAL, "Cannot find action: %s\n",
331 } else if (action(msg, s->sun_path) < 0) {
332 RTE_LOG(ERR, EAL, "Fail to handle message: %s\n", msg->name);
337 mp_handle(void *arg __rte_unused)
339 struct mp_msg_internal msg;
340 struct sockaddr_un sa;
343 if (read_msg(&msg, &sa) == 0)
344 process_msg(&msg, &sa);
351 timespec_cmp(const struct timespec *a, const struct timespec *b)
353 if (a->tv_sec < b->tv_sec)
355 if (a->tv_sec > b->tv_sec)
357 if (a->tv_nsec < b->tv_nsec)
359 if (a->tv_nsec > b->tv_nsec)
365 ACTION_NONE, /**< don't do anything */
366 ACTION_FREE, /**< free the action entry, but don't trigger callback */
367 ACTION_TRIGGER /**< trigger callback, then free action entry */
370 static enum async_action
371 process_async_request(struct pending_request *sr, const struct timespec *now)
373 struct async_request_param *param;
374 struct rte_mp_reply *reply;
375 bool timeout, received, last_msg;
377 param = sr->async.param;
378 reply = ¶m->user_reply;
380 /* did we timeout? */
381 timeout = timespec_cmp(¶m->end, now) <= 0;
383 /* did we receive a response? */
384 received = sr->reply_received != 0;
386 /* if we didn't time out, and we didn't receive a response, ignore */
387 if (!timeout && !received)
390 /* if we received a response, adjust relevant data and copy mesasge. */
391 if (sr->reply_received == 1 && sr->reply) {
392 struct rte_mp_msg *msg, *user_msgs, *tmp;
395 user_msgs = reply->msgs;
397 tmp = realloc(user_msgs, sizeof(*msg) *
398 (reply->nb_received + 1));
400 RTE_LOG(ERR, EAL, "Fail to alloc reply for request %s:%s\n",
401 sr->dst, sr->request->name);
402 /* this entry is going to be removed and its message
403 * dropped, but we don't want to leak memory, so
408 reply->msgs = user_msgs;
409 memcpy(&user_msgs[reply->nb_received],
411 reply->nb_received++;
414 /* mark this request as processed */
415 param->n_responses_processed++;
416 } else if (sr->reply_received == -1) {
417 /* we were asked to ignore this process */
422 last_msg = param->n_responses_processed == reply->nb_sent;
424 return last_msg ? ACTION_TRIGGER : ACTION_FREE;
428 trigger_async_action(struct pending_request *sr)
430 struct async_request_param *param;
431 struct rte_mp_reply *reply;
433 param = sr->async.param;
434 reply = ¶m->user_reply;
436 param->clb(sr->request, reply);
439 free(sr->async.param->user_reply.msgs);
440 free(sr->async.param);
445 async_reply_handle(void *arg __rte_unused)
447 struct pending_request *sr;
449 struct timespec timeout, ts_now;
451 struct pending_request *trigger = NULL;
454 bool timedwait = false;
456 pthread_mutex_lock(&pending_requests.lock);
458 /* scan through the list and see if there are any timeouts that
459 * are earlier than our current timeout.
461 TAILQ_FOREACH(sr, &pending_requests.requests, next) {
462 if (sr->type != REQUEST_TYPE_ASYNC)
464 if (!timedwait || timespec_cmp(&sr->async.param->end,
466 memcpy(&timeout, &sr->async.param->end,
471 /* sometimes, we don't even wait */
472 if (sr->reply_received) {
481 ret = pthread_cond_timedwait(
482 &pending_requests.async_cond,
483 &pending_requests.lock, &timeout);
485 ret = pthread_cond_wait(&pending_requests.async_cond,
486 &pending_requests.lock);
488 if (gettimeofday(&now, NULL) < 0) {
489 RTE_LOG(ERR, EAL, "Cannot get current time\n");
492 ts_now.tv_nsec = now.tv_usec * 1000;
493 ts_now.tv_sec = now.tv_sec;
495 if (ret == 0 || ret == ETIMEDOUT) {
496 struct pending_request *next;
497 /* we've either been woken up, or we timed out */
499 /* we have still the lock, check if anything needs
502 TAILQ_FOREACH_SAFE(sr, &pending_requests.requests, next,
504 enum async_action action;
505 if (sr->type != REQUEST_TYPE_ASYNC)
508 action = process_async_request(sr, &ts_now);
509 if (action == ACTION_FREE) {
510 TAILQ_REMOVE(&pending_requests.requests,
513 } else if (action == ACTION_TRIGGER &&
515 TAILQ_REMOVE(&pending_requests.requests,
521 pthread_mutex_unlock(&pending_requests.lock);
523 trigger_async_action(trigger);
528 RTE_LOG(ERR, EAL, "ERROR: asynchronous requests disabled\n");
536 char peer_name[PATH_MAX] = {0};
537 struct sockaddr_un un;
539 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
540 snprintf(peer_name, sizeof(peer_name),
541 "%d_%"PRIx64, getpid(), rte_rdtsc());
543 mp_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
545 RTE_LOG(ERR, EAL, "failed to create unix socket\n");
549 memset(&un, 0, sizeof(un));
550 un.sun_family = AF_UNIX;
552 create_socket_path(peer_name, un.sun_path, sizeof(un.sun_path));
554 unlink(un.sun_path); /* May still exist since last run */
556 if (bind(mp_fd, (struct sockaddr *)&un, sizeof(un)) < 0) {
557 RTE_LOG(ERR, EAL, "failed to bind %s: %s\n",
558 un.sun_path, strerror(errno));
563 RTE_LOG(INFO, EAL, "Multi-process socket %s\n", un.sun_path);
568 unlink_sockets(const char *filter)
574 mp_dir = opendir(mp_dir_path);
576 RTE_LOG(ERR, EAL, "Unable to open directory %s\n", mp_dir_path);
579 dir_fd = dirfd(mp_dir);
581 while ((ent = readdir(mp_dir))) {
582 if (fnmatch(filter, ent->d_name, 0) == 0)
583 unlinkat(dir_fd, ent->d_name, 0);
591 rte_mp_channel_init(void)
593 char thread_name[RTE_MAX_THREAD_NAME_LEN];
596 pthread_t mp_handle_tid, async_reply_handle_tid;
598 /* create filter path */
599 create_socket_path("*", path, sizeof(path));
600 snprintf(mp_filter, sizeof(mp_filter), "%s", basename(path));
602 /* path may have been modified, so recreate it */
603 create_socket_path("*", path, sizeof(path));
604 snprintf(mp_dir_path, sizeof(mp_dir_path), "%s", dirname(path));
606 /* lock the directory */
607 dir_fd = open(mp_dir_path, O_RDONLY);
609 RTE_LOG(ERR, EAL, "failed to open %s: %s\n",
610 mp_dir_path, strerror(errno));
614 if (flock(dir_fd, LOCK_EX)) {
615 RTE_LOG(ERR, EAL, "failed to lock %s: %s\n",
616 mp_dir_path, strerror(errno));
621 if (rte_eal_process_type() == RTE_PROC_PRIMARY &&
622 unlink_sockets(mp_filter)) {
623 RTE_LOG(ERR, EAL, "failed to unlink mp sockets\n");
628 if (open_socket_fd() < 0) {
633 if (pthread_create(&mp_handle_tid, NULL, mp_handle, NULL) < 0) {
634 RTE_LOG(ERR, EAL, "failed to create mp thead: %s\n",
641 if (pthread_create(&async_reply_handle_tid, NULL,
642 async_reply_handle, NULL) < 0) {
643 RTE_LOG(ERR, EAL, "failed to create mp thead: %s\n",
651 /* try best to set thread name */
652 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN, "rte_mp_handle");
653 rte_thread_setname(mp_handle_tid, thread_name);
655 /* try best to set thread name */
656 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN, "rte_mp_async_handle");
657 rte_thread_setname(async_reply_handle_tid, thread_name);
659 /* unlock the directory */
660 flock(dir_fd, LOCK_UN);
667 * Return -1, as fail to send message and it's caused by the local side.
668 * Return 0, as fail to send message and it's caused by the remote side.
669 * Return 1, as succeed to send message.
673 send_msg(const char *dst_path, struct rte_mp_msg *msg, int type)
678 struct cmsghdr *cmsg;
679 struct sockaddr_un dst;
680 struct mp_msg_internal m;
681 int fd_size = msg->num_fds * sizeof(int);
682 char control[CMSG_SPACE(fd_size)];
685 memcpy(&m.msg, msg, sizeof(*msg));
687 memset(&dst, 0, sizeof(dst));
688 dst.sun_family = AF_UNIX;
689 snprintf(dst.sun_path, sizeof(dst.sun_path), "%s", dst_path);
691 memset(&msgh, 0, sizeof(msgh));
692 memset(control, 0, sizeof(control));
695 iov.iov_len = sizeof(m) - sizeof(msg->fds);
697 msgh.msg_name = &dst;
698 msgh.msg_namelen = sizeof(dst);
701 msgh.msg_control = control;
702 msgh.msg_controllen = sizeof(control);
704 cmsg = CMSG_FIRSTHDR(&msgh);
705 cmsg->cmsg_len = CMSG_LEN(fd_size);
706 cmsg->cmsg_level = SOL_SOCKET;
707 cmsg->cmsg_type = SCM_RIGHTS;
708 memcpy(CMSG_DATA(cmsg), msg->fds, fd_size);
711 snd = sendmsg(mp_fd, &msgh, 0);
712 } while (snd < 0 && errno == EINTR);
716 /* Check if it caused by peer process exits */
717 if (errno == ECONNREFUSED &&
718 rte_eal_process_type() == RTE_PROC_PRIMARY) {
722 if (errno == ENOBUFS) {
723 RTE_LOG(ERR, EAL, "Peer cannot receive message %s\n",
727 RTE_LOG(ERR, EAL, "failed to send to (%s) due to %s\n",
728 dst_path, strerror(errno));
736 mp_send(struct rte_mp_msg *msg, const char *peer, int type)
742 if (!peer && (rte_eal_process_type() == RTE_PROC_SECONDARY))
743 peer = eal_mp_socket_path();
746 if (send_msg(peer, msg, type) < 0)
752 /* broadcast to all secondary processes */
753 mp_dir = opendir(mp_dir_path);
755 RTE_LOG(ERR, EAL, "Unable to open directory %s\n",
761 dir_fd = dirfd(mp_dir);
762 /* lock the directory to prevent processes spinning up while we send */
763 if (flock(dir_fd, LOCK_EX)) {
764 RTE_LOG(ERR, EAL, "Unable to lock directory %s\n",
771 while ((ent = readdir(mp_dir))) {
774 if (fnmatch(mp_filter, ent->d_name, 0) != 0)
777 snprintf(path, sizeof(path), "%s/%s", mp_dir_path,
779 if (send_msg(path, msg, type) < 0)
783 flock(dir_fd, LOCK_UN);
785 /* dir_fd automatically closed on closedir */
791 check_input(const struct rte_mp_msg *msg)
794 RTE_LOG(ERR, EAL, "Msg cannot be NULL\n");
799 if (validate_action_name(msg->name))
802 if (msg->len_param > RTE_MP_MAX_PARAM_LEN) {
803 RTE_LOG(ERR, EAL, "Message data is too long\n");
808 if (msg->num_fds > RTE_MP_MAX_FD_NUM) {
809 RTE_LOG(ERR, EAL, "Cannot send more than %d FDs\n",
818 int __rte_experimental
819 rte_mp_sendmsg(struct rte_mp_msg *msg)
821 if (!check_input(msg))
824 RTE_LOG(DEBUG, EAL, "sendmsg: %s\n", msg->name);
825 return mp_send(msg, NULL, MP_MSG);
829 mp_request_async(const char *dst, struct rte_mp_msg *req,
830 struct async_request_param *param)
832 struct rte_mp_msg *reply_msg;
833 struct pending_request *sync_req, *exist;
836 sync_req = malloc(sizeof(*sync_req));
837 reply_msg = malloc(sizeof(*reply_msg));
838 if (sync_req == NULL || reply_msg == NULL) {
839 RTE_LOG(ERR, EAL, "Could not allocate space for sync request\n");
845 memset(sync_req, 0, sizeof(*sync_req));
846 memset(reply_msg, 0, sizeof(*reply_msg));
848 sync_req->type = REQUEST_TYPE_ASYNC;
849 strcpy(sync_req->dst, dst);
850 sync_req->request = req;
851 sync_req->reply = reply_msg;
852 sync_req->async.param = param;
854 /* queue already locked by caller */
856 exist = find_sync_request(dst, req->name);
858 TAILQ_INSERT_TAIL(&pending_requests.requests, sync_req, next);
860 RTE_LOG(ERR, EAL, "A pending request %s:%s\n", dst, req->name);
866 ret = send_msg(dst, req, MP_REQ);
868 RTE_LOG(ERR, EAL, "Fail to send request %s:%s\n",
872 } else if (ret == 0) {
877 param->user_reply.nb_sent++;
887 mp_request_sync(const char *dst, struct rte_mp_msg *req,
888 struct rte_mp_reply *reply, const struct timespec *ts)
891 struct rte_mp_msg msg, *tmp;
892 struct pending_request sync_req, *exist;
894 sync_req.type = REQUEST_TYPE_SYNC;
895 sync_req.reply_received = 0;
896 strcpy(sync_req.dst, dst);
897 sync_req.request = req;
898 sync_req.reply = &msg;
899 pthread_cond_init(&sync_req.sync.cond, NULL);
901 pthread_mutex_lock(&pending_requests.lock);
902 exist = find_sync_request(dst, req->name);
904 TAILQ_INSERT_TAIL(&pending_requests.requests, &sync_req, next);
906 RTE_LOG(ERR, EAL, "A pending request %s:%s\n", dst, req->name);
908 pthread_mutex_unlock(&pending_requests.lock);
912 ret = send_msg(dst, req, MP_REQ);
914 RTE_LOG(ERR, EAL, "Fail to send request %s:%s\n",
923 ret = pthread_cond_timedwait(&sync_req.sync.cond,
924 &pending_requests.lock, ts);
925 } while (ret != 0 && ret != ETIMEDOUT);
927 /* We got the lock now */
928 TAILQ_REMOVE(&pending_requests.requests, &sync_req, next);
929 pthread_mutex_unlock(&pending_requests.lock);
931 if (sync_req.reply_received == 0) {
932 RTE_LOG(ERR, EAL, "Fail to recv reply for request %s:%s\n",
934 rte_errno = ETIMEDOUT;
937 if (sync_req.reply_received == -1) {
938 RTE_LOG(DEBUG, EAL, "Asked to ignore response\n");
939 /* not receiving this message is not an error, so decrement
940 * number of sent messages
946 tmp = realloc(reply->msgs, sizeof(msg) * (reply->nb_received + 1));
948 RTE_LOG(ERR, EAL, "Fail to alloc reply for request %s:%s\n",
953 memcpy(&tmp[reply->nb_received], &msg, sizeof(msg));
955 reply->nb_received++;
959 int __rte_experimental
960 rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply,
961 const struct timespec *ts)
969 RTE_LOG(DEBUG, EAL, "request: %s\n", req->name);
971 if (check_input(req) == false)
973 if (gettimeofday(&now, NULL) < 0) {
974 RTE_LOG(ERR, EAL, "Faile to get current time\n");
979 end.tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
980 end.tv_sec = now.tv_sec + ts->tv_sec +
981 (now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
984 reply->nb_received = 0;
987 /* for secondary process, send request to the primary process only */
988 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
989 return mp_request_sync(eal_mp_socket_path(), req, reply, &end);
991 /* for primary process, broadcast request, and collect reply 1 by 1 */
992 mp_dir = opendir(mp_dir_path);
994 RTE_LOG(ERR, EAL, "Unable to open directory %s\n", mp_dir_path);
999 dir_fd = dirfd(mp_dir);
1000 /* lock the directory to prevent processes spinning up while we send */
1001 if (flock(dir_fd, LOCK_EX)) {
1002 RTE_LOG(ERR, EAL, "Unable to lock directory %s\n",
1009 while ((ent = readdir(mp_dir))) {
1010 char path[PATH_MAX];
1012 if (fnmatch(mp_filter, ent->d_name, 0) != 0)
1015 snprintf(path, sizeof(path), "%s/%s", mp_dir_path,
1018 if (mp_request_sync(path, req, reply, &end))
1021 /* unlock the directory */
1022 flock(dir_fd, LOCK_UN);
1024 /* dir_fd automatically closed on closedir */
1029 int __rte_experimental
1030 rte_mp_request_async(struct rte_mp_msg *req, const struct timespec *ts,
1031 rte_mp_async_reply_t clb)
1033 struct rte_mp_msg *copy;
1034 struct pending_request *dummy;
1035 struct async_request_param *param;
1036 struct rte_mp_reply *reply;
1037 int dir_fd, ret = 0;
1041 struct timespec *end;
1042 bool dummy_used = false;
1044 RTE_LOG(DEBUG, EAL, "request: %s\n", req->name);
1046 if (check_input(req) == false)
1048 if (gettimeofday(&now, NULL) < 0) {
1049 RTE_LOG(ERR, EAL, "Faile to get current time\n");
1053 copy = malloc(sizeof(*copy));
1054 dummy = malloc(sizeof(*dummy));
1055 param = malloc(sizeof(*param));
1056 if (copy == NULL || dummy == NULL || param == NULL) {
1057 RTE_LOG(ERR, EAL, "Failed to allocate memory for async reply\n");
1062 memset(copy, 0, sizeof(*copy));
1063 memset(dummy, 0, sizeof(*dummy));
1064 memset(param, 0, sizeof(*param));
1067 memcpy(copy, req, sizeof(*copy));
1069 param->n_responses_processed = 0;
1072 reply = ¶m->user_reply;
1074 end->tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
1075 end->tv_sec = now.tv_sec + ts->tv_sec +
1076 (now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
1078 reply->nb_received = 0;
1081 /* we have to lock the request queue here, as we will be adding a bunch
1082 * of requests to the queue at once, and some of the replies may arrive
1083 * before we add all of the requests to the queue.
1085 pthread_mutex_lock(&pending_requests.lock);
1087 /* we have to ensure that callback gets triggered even if we don't send
1088 * anything, therefore earlier we have allocated a dummy request. fill
1089 * it, and put it on the queue if we don't send any requests.
1091 dummy->type = REQUEST_TYPE_ASYNC;
1092 dummy->request = copy;
1093 dummy->reply = NULL;
1094 dummy->async.param = param;
1095 dummy->reply_received = 1; /* short-circuit the timeout */
1097 /* for secondary process, send request to the primary process only */
1098 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1099 ret = mp_request_async(eal_mp_socket_path(), copy, param);
1101 /* if we didn't send anything, put dummy request on the queue */
1102 if (ret == 0 && reply->nb_sent == 0) {
1103 TAILQ_INSERT_TAIL(&pending_requests.requests, dummy,
1108 pthread_mutex_unlock(&pending_requests.lock);
1110 /* if we couldn't send anything, clean up */
1116 /* for primary process, broadcast request */
1117 mp_dir = opendir(mp_dir_path);
1119 RTE_LOG(ERR, EAL, "Unable to open directory %s\n", mp_dir_path);
1123 dir_fd = dirfd(mp_dir);
1125 /* lock the directory to prevent processes spinning up while we send */
1126 if (flock(dir_fd, LOCK_EX)) {
1127 RTE_LOG(ERR, EAL, "Unable to lock directory %s\n",
1133 while ((ent = readdir(mp_dir))) {
1134 char path[PATH_MAX];
1136 if (fnmatch(mp_filter, ent->d_name, 0) != 0)
1139 snprintf(path, sizeof(path), "%s/%s", mp_dir_path,
1142 if (mp_request_async(path, copy, param))
1145 /* if we didn't send anything, put dummy request on the queue */
1146 if (ret == 0 && reply->nb_sent == 0) {
1147 TAILQ_INSERT_HEAD(&pending_requests.requests, dummy, next);
1151 /* trigger async request thread wake up */
1152 pthread_cond_signal(&pending_requests.async_cond);
1154 /* finally, unlock the queue */
1155 pthread_mutex_unlock(&pending_requests.lock);
1157 /* unlock the directory */
1158 flock(dir_fd, LOCK_UN);
1160 /* dir_fd automatically closed on closedir */
1163 /* if dummy was unused, free it */
1171 pthread_mutex_unlock(&pending_requests.lock);
1179 int __rte_experimental
1180 rte_mp_reply(struct rte_mp_msg *msg, const char *peer)
1182 RTE_LOG(DEBUG, EAL, "reply: %s\n", msg->name);
1184 if (check_input(msg) == false)
1188 RTE_LOG(ERR, EAL, "peer is not specified\n");
1193 return mp_send(msg, peer, MP_REP);