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_alarm.h>
24 #include <rte_common.h>
25 #include <rte_cycles.h>
27 #include <rte_errno.h>
28 #include <rte_lcore.h>
30 #include <rte_tailq.h>
32 #include "eal_private.h"
33 #include "eal_filesystem.h"
34 #include "eal_internal_cfg.h"
36 static int mp_fd = -1;
37 static char mp_filter[PATH_MAX]; /* Filter for secondary process sockets */
38 static char mp_dir_path[PATH_MAX]; /* The directory path for all mp sockets */
39 static pthread_mutex_t mp_mutex_action = PTHREAD_MUTEX_INITIALIZER;
40 static char peer_name[PATH_MAX];
43 TAILQ_ENTRY(action_entry) next;
44 char action_name[RTE_MP_MAX_NAME_LEN];
48 /** Double linked list of actions. */
49 TAILQ_HEAD(action_entry_list, action_entry);
51 static struct action_entry_list action_entry_list =
52 TAILQ_HEAD_INITIALIZER(action_entry_list);
55 MP_MSG, /* Share message with peers, will not block */
56 MP_REQ, /* Request for information, Will block for a reply */
57 MP_REP, /* Response to previously-received request */
58 MP_IGN, /* Response telling requester to ignore this response */
61 struct mp_msg_internal {
63 struct rte_mp_msg msg;
66 struct async_request_param {
67 rte_mp_async_reply_t clb;
68 struct rte_mp_reply user_reply;
70 int n_responses_processed;
73 struct pending_request {
74 TAILQ_ENTRY(pending_request) next;
80 struct rte_mp_msg *request;
81 struct rte_mp_msg *reply;
86 struct async_request_param *param;
94 TAILQ_HEAD(pending_request_list, pending_request);
97 struct pending_request_list requests;
99 } pending_requests = {
100 .requests = TAILQ_HEAD_INITIALIZER(pending_requests.requests),
101 .lock = PTHREAD_MUTEX_INITIALIZER,
102 /**< used in async requests only */
105 /* forward declarations */
107 mp_send(struct rte_mp_msg *msg, const char *peer, int type);
109 /* for use with alarm callback */
111 async_reply_handle(void *arg);
113 /* for use with process_msg */
114 static struct pending_request *
115 async_reply_handle_thread_unsafe(void *arg);
118 trigger_async_action(struct pending_request *req);
120 static struct pending_request *
121 find_pending_request(const char *dst, const char *act_name)
123 struct pending_request *r;
125 TAILQ_FOREACH(r, &pending_requests.requests, next) {
126 if (!strcmp(r->dst, dst) &&
127 !strcmp(r->request->name, act_name))
135 create_socket_path(const char *name, char *buf, int len)
137 const char *prefix = eal_mp_socket_path();
139 if (strlen(name) > 0)
140 snprintf(buf, len, "%s_%s", prefix, name);
142 strlcpy(buf, prefix, len);
146 rte_eal_primary_proc_alive(const char *config_file_path)
150 if (config_file_path)
151 config_fd = open(config_file_path, O_RDONLY);
155 path = eal_runtime_config_path();
156 config_fd = open(path, O_RDONLY);
161 int ret = lockf(config_fd, F_TEST, 0);
167 static struct action_entry *
168 find_action_entry_by_name(const char *name)
170 struct action_entry *entry;
172 TAILQ_FOREACH(entry, &action_entry_list, next) {
173 if (strncmp(entry->action_name, name, RTE_MP_MAX_NAME_LEN) == 0)
181 validate_action_name(const char *name)
184 RTE_LOG(ERR, EAL, "Action name cannot be NULL\n");
188 if (strnlen(name, RTE_MP_MAX_NAME_LEN) == 0) {
189 RTE_LOG(ERR, EAL, "Length of action name is zero\n");
193 if (strnlen(name, RTE_MP_MAX_NAME_LEN) == RTE_MP_MAX_NAME_LEN) {
200 int __rte_experimental
201 rte_mp_action_register(const char *name, rte_mp_t action)
203 struct action_entry *entry;
205 if (validate_action_name(name) != 0)
208 if (internal_config.no_shconf) {
209 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is disabled\n");
214 entry = malloc(sizeof(struct action_entry));
219 strlcpy(entry->action_name, name, sizeof(entry->action_name));
220 entry->action = action;
222 pthread_mutex_lock(&mp_mutex_action);
223 if (find_action_entry_by_name(name) != NULL) {
224 pthread_mutex_unlock(&mp_mutex_action);
229 TAILQ_INSERT_TAIL(&action_entry_list, entry, next);
230 pthread_mutex_unlock(&mp_mutex_action);
234 void __rte_experimental
235 rte_mp_action_unregister(const char *name)
237 struct action_entry *entry;
239 if (validate_action_name(name) != 0)
242 pthread_mutex_lock(&mp_mutex_action);
243 entry = find_action_entry_by_name(name);
245 pthread_mutex_unlock(&mp_mutex_action);
248 TAILQ_REMOVE(&action_entry_list, entry, next);
249 pthread_mutex_unlock(&mp_mutex_action);
254 read_msg(struct mp_msg_internal *m, struct sockaddr_un *s)
259 char control[CMSG_SPACE(sizeof(m->msg.fds))];
260 struct cmsghdr *cmsg;
261 int buflen = sizeof(*m) - sizeof(m->msg.fds);
263 memset(&msgh, 0, sizeof(msgh));
265 iov.iov_len = buflen;
268 msgh.msg_namelen = sizeof(*s);
271 msgh.msg_control = control;
272 msgh.msg_controllen = sizeof(control);
274 msglen = recvmsg(mp_fd, &msgh, 0);
276 RTE_LOG(ERR, EAL, "recvmsg failed, %s\n", strerror(errno));
280 if (msglen != buflen || (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) {
281 RTE_LOG(ERR, EAL, "truncted msg\n");
285 /* read auxiliary FDs if any */
286 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
287 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
288 if ((cmsg->cmsg_level == SOL_SOCKET) &&
289 (cmsg->cmsg_type == SCM_RIGHTS)) {
290 memcpy(m->msg.fds, CMSG_DATA(cmsg), sizeof(m->msg.fds));
294 /* sanity-check the response */
295 if (m->msg.num_fds < 0 || m->msg.num_fds > RTE_MP_MAX_FD_NUM) {
296 RTE_LOG(ERR, EAL, "invalid number of fd's received\n");
299 if (m->msg.len_param < 0 || m->msg.len_param > RTE_MP_MAX_PARAM_LEN) {
300 RTE_LOG(ERR, EAL, "invalid received data length\n");
307 process_msg(struct mp_msg_internal *m, struct sockaddr_un *s)
309 struct pending_request *pending_req;
310 struct action_entry *entry;
311 struct rte_mp_msg *msg = &m->msg;
312 rte_mp_t action = NULL;
314 RTE_LOG(DEBUG, EAL, "msg: %s\n", msg->name);
316 if (m->type == MP_REP || m->type == MP_IGN) {
317 struct pending_request *req = NULL;
319 pthread_mutex_lock(&pending_requests.lock);
320 pending_req = find_pending_request(s->sun_path, msg->name);
322 memcpy(pending_req->reply, msg, sizeof(*msg));
323 /* -1 indicates that we've been asked to ignore */
324 pending_req->reply_received =
325 m->type == MP_REP ? 1 : -1;
327 if (pending_req->type == REQUEST_TYPE_SYNC)
328 pthread_cond_signal(&pending_req->sync.cond);
329 else if (pending_req->type == REQUEST_TYPE_ASYNC)
330 req = async_reply_handle_thread_unsafe(
333 RTE_LOG(ERR, EAL, "Drop mp reply: %s\n", msg->name);
334 pthread_mutex_unlock(&pending_requests.lock);
337 trigger_async_action(req);
341 pthread_mutex_lock(&mp_mutex_action);
342 entry = find_action_entry_by_name(msg->name);
344 action = entry->action;
345 pthread_mutex_unlock(&mp_mutex_action);
348 if (m->type == MP_REQ && !internal_config.init_complete) {
349 /* if this is a request, and init is not yet complete,
350 * and callback wasn't registered, we should tell the
351 * requester to ignore our existence because we're not
352 * yet ready to process this request.
354 struct rte_mp_msg dummy;
356 memset(&dummy, 0, sizeof(dummy));
357 strlcpy(dummy.name, msg->name, sizeof(dummy.name));
358 mp_send(&dummy, s->sun_path, MP_IGN);
360 RTE_LOG(ERR, EAL, "Cannot find action: %s\n",
363 } else if (action(msg, s->sun_path) < 0) {
364 RTE_LOG(ERR, EAL, "Fail to handle message: %s\n", msg->name);
369 mp_handle(void *arg __rte_unused)
371 struct mp_msg_internal msg;
372 struct sockaddr_un sa;
375 if (read_msg(&msg, &sa) == 0)
376 process_msg(&msg, &sa);
383 timespec_cmp(const struct timespec *a, const struct timespec *b)
385 if (a->tv_sec < b->tv_sec)
387 if (a->tv_sec > b->tv_sec)
389 if (a->tv_nsec < b->tv_nsec)
391 if (a->tv_nsec > b->tv_nsec)
397 ACTION_FREE, /**< free the action entry, but don't trigger callback */
398 ACTION_TRIGGER /**< trigger callback, then free action entry */
401 static enum async_action
402 process_async_request(struct pending_request *sr, const struct timespec *now)
404 struct async_request_param *param;
405 struct rte_mp_reply *reply;
406 bool timeout, last_msg;
408 param = sr->async.param;
409 reply = ¶m->user_reply;
411 /* did we timeout? */
412 timeout = timespec_cmp(¶m->end, now) <= 0;
414 /* if we received a response, adjust relevant data and copy mesasge. */
415 if (sr->reply_received == 1 && sr->reply) {
416 struct rte_mp_msg *msg, *user_msgs, *tmp;
419 user_msgs = reply->msgs;
421 tmp = realloc(user_msgs, sizeof(*msg) *
422 (reply->nb_received + 1));
424 RTE_LOG(ERR, EAL, "Fail to alloc reply for request %s:%s\n",
425 sr->dst, sr->request->name);
426 /* this entry is going to be removed and its message
427 * dropped, but we don't want to leak memory, so
432 reply->msgs = user_msgs;
433 memcpy(&user_msgs[reply->nb_received],
435 reply->nb_received++;
438 /* mark this request as processed */
439 param->n_responses_processed++;
440 } else if (sr->reply_received == -1) {
441 /* we were asked to ignore this process */
443 } else if (timeout) {
444 /* count it as processed response, but don't increment
447 param->n_responses_processed++;
452 last_msg = param->n_responses_processed == reply->nb_sent;
454 return last_msg ? ACTION_TRIGGER : ACTION_FREE;
458 trigger_async_action(struct pending_request *sr)
460 struct async_request_param *param;
461 struct rte_mp_reply *reply;
463 param = sr->async.param;
464 reply = ¶m->user_reply;
466 param->clb(sr->request, reply);
469 free(sr->async.param->user_reply.msgs);
470 free(sr->async.param);
475 static struct pending_request *
476 async_reply_handle_thread_unsafe(void *arg)
478 struct pending_request *req = (struct pending_request *)arg;
479 enum async_action action;
480 struct timespec ts_now;
483 if (gettimeofday(&now, NULL) < 0) {
484 RTE_LOG(ERR, EAL, "Cannot get current time\n");
487 ts_now.tv_nsec = now.tv_usec * 1000;
488 ts_now.tv_sec = now.tv_sec;
490 action = process_async_request(req, &ts_now);
492 TAILQ_REMOVE(&pending_requests.requests, req, next);
494 if (rte_eal_alarm_cancel(async_reply_handle, req) < 0) {
495 /* if we failed to cancel the alarm because it's already in
496 * progress, don't proceed because otherwise we will end up
497 * handling the same message twice.
499 if (rte_errno == EINPROGRESS) {
500 RTE_LOG(DEBUG, EAL, "Request handling is already in progress\n");
503 RTE_LOG(ERR, EAL, "Failed to cancel alarm\n");
506 if (action == ACTION_TRIGGER)
514 async_reply_handle(void *arg)
516 struct pending_request *req;
518 pthread_mutex_lock(&pending_requests.lock);
519 req = async_reply_handle_thread_unsafe(arg);
520 pthread_mutex_unlock(&pending_requests.lock);
523 trigger_async_action(req);
529 struct sockaddr_un un;
532 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
533 snprintf(peer_name, sizeof(peer_name),
534 "%d_%"PRIx64, getpid(), rte_rdtsc());
536 mp_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
538 RTE_LOG(ERR, EAL, "failed to create unix socket\n");
542 memset(&un, 0, sizeof(un));
543 un.sun_family = AF_UNIX;
545 create_socket_path(peer_name, un.sun_path, sizeof(un.sun_path));
547 unlink(un.sun_path); /* May still exist since last run */
549 if (bind(mp_fd, (struct sockaddr *)&un, sizeof(un)) < 0) {
550 RTE_LOG(ERR, EAL, "failed to bind %s: %s\n",
551 un.sun_path, strerror(errno));
556 RTE_LOG(INFO, EAL, "Multi-process socket %s\n", un.sun_path);
561 close_socket_fd(void)
569 create_socket_path(peer_name, path, sizeof(path));
574 rte_mp_channel_init(void)
578 pthread_t mp_handle_tid;
580 /* in no shared files mode, we do not have secondary processes support,
581 * so no need to initialize IPC.
583 if (internal_config.no_shconf) {
584 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC will be disabled\n");
589 /* create filter path */
590 create_socket_path("*", path, sizeof(path));
591 strlcpy(mp_filter, basename(path), sizeof(mp_filter));
593 /* path may have been modified, so recreate it */
594 create_socket_path("*", path, sizeof(path));
595 strlcpy(mp_dir_path, dirname(path), sizeof(mp_dir_path));
597 /* lock the directory */
598 dir_fd = open(mp_dir_path, O_RDONLY);
600 RTE_LOG(ERR, EAL, "failed to open %s: %s\n",
601 mp_dir_path, strerror(errno));
605 if (flock(dir_fd, LOCK_EX)) {
606 RTE_LOG(ERR, EAL, "failed to lock %s: %s\n",
607 mp_dir_path, strerror(errno));
612 if (open_socket_fd() < 0) {
617 if (rte_ctrl_thread_create(&mp_handle_tid, "rte_mp_handle",
618 NULL, mp_handle, NULL) < 0) {
619 RTE_LOG(ERR, EAL, "failed to create mp thead: %s\n",
627 /* unlock the directory */
628 flock(dir_fd, LOCK_UN);
635 rte_mp_channel_cleanup(void)
641 * Return -1, as fail to send message and it's caused by the local side.
642 * Return 0, as fail to send message and it's caused by the remote side.
643 * Return 1, as succeed to send message.
647 send_msg(const char *dst_path, struct rte_mp_msg *msg, int type)
652 struct cmsghdr *cmsg;
653 struct sockaddr_un dst;
654 struct mp_msg_internal m;
655 int fd_size = msg->num_fds * sizeof(int);
656 char control[CMSG_SPACE(fd_size)];
659 memcpy(&m.msg, msg, sizeof(*msg));
661 memset(&dst, 0, sizeof(dst));
662 dst.sun_family = AF_UNIX;
663 strlcpy(dst.sun_path, dst_path, sizeof(dst.sun_path));
665 memset(&msgh, 0, sizeof(msgh));
666 memset(control, 0, sizeof(control));
669 iov.iov_len = sizeof(m) - sizeof(msg->fds);
671 msgh.msg_name = &dst;
672 msgh.msg_namelen = sizeof(dst);
675 msgh.msg_control = control;
676 msgh.msg_controllen = sizeof(control);
678 cmsg = CMSG_FIRSTHDR(&msgh);
679 cmsg->cmsg_len = CMSG_LEN(fd_size);
680 cmsg->cmsg_level = SOL_SOCKET;
681 cmsg->cmsg_type = SCM_RIGHTS;
682 memcpy(CMSG_DATA(cmsg), msg->fds, fd_size);
685 snd = sendmsg(mp_fd, &msgh, 0);
686 } while (snd < 0 && errno == EINTR);
690 /* Check if it caused by peer process exits */
691 if (errno == ECONNREFUSED &&
692 rte_eal_process_type() == RTE_PROC_PRIMARY) {
696 RTE_LOG(ERR, EAL, "failed to send to (%s) due to %s\n",
697 dst_path, strerror(errno));
705 mp_send(struct rte_mp_msg *msg, const char *peer, int type)
711 if (!peer && (rte_eal_process_type() == RTE_PROC_SECONDARY))
712 peer = eal_mp_socket_path();
715 if (send_msg(peer, msg, type) < 0)
721 /* broadcast to all secondary processes */
722 mp_dir = opendir(mp_dir_path);
724 RTE_LOG(ERR, EAL, "Unable to open directory %s\n",
730 dir_fd = dirfd(mp_dir);
731 /* lock the directory to prevent processes spinning up while we send */
732 if (flock(dir_fd, LOCK_SH)) {
733 RTE_LOG(ERR, EAL, "Unable to lock directory %s\n",
740 while ((ent = readdir(mp_dir))) {
743 if (fnmatch(mp_filter, ent->d_name, 0) != 0)
746 snprintf(path, sizeof(path), "%s/%s", mp_dir_path,
748 if (send_msg(path, msg, type) < 0)
752 flock(dir_fd, LOCK_UN);
754 /* dir_fd automatically closed on closedir */
760 check_input(const struct rte_mp_msg *msg)
763 RTE_LOG(ERR, EAL, "Msg cannot be NULL\n");
768 if (validate_action_name(msg->name) != 0)
771 if (msg->len_param < 0) {
772 RTE_LOG(ERR, EAL, "Message data length is negative\n");
777 if (msg->num_fds < 0) {
778 RTE_LOG(ERR, EAL, "Number of fd's is negative\n");
783 if (msg->len_param > RTE_MP_MAX_PARAM_LEN) {
784 RTE_LOG(ERR, EAL, "Message data is too long\n");
789 if (msg->num_fds > RTE_MP_MAX_FD_NUM) {
790 RTE_LOG(ERR, EAL, "Cannot send more than %d FDs\n",
799 int __rte_experimental
800 rte_mp_sendmsg(struct rte_mp_msg *msg)
802 if (check_input(msg) != 0)
805 RTE_LOG(DEBUG, EAL, "sendmsg: %s\n", msg->name);
806 return mp_send(msg, NULL, MP_MSG);
810 mp_request_async(const char *dst, struct rte_mp_msg *req,
811 struct async_request_param *param, const struct timespec *ts)
813 struct rte_mp_msg *reply_msg;
814 struct pending_request *pending_req, *exist;
817 pending_req = calloc(1, sizeof(*pending_req));
818 reply_msg = calloc(1, sizeof(*reply_msg));
819 if (pending_req == NULL || reply_msg == NULL) {
820 RTE_LOG(ERR, EAL, "Could not allocate space for sync request\n");
826 pending_req->type = REQUEST_TYPE_ASYNC;
827 strlcpy(pending_req->dst, dst, sizeof(pending_req->dst));
828 pending_req->request = req;
829 pending_req->reply = reply_msg;
830 pending_req->async.param = param;
832 /* queue already locked by caller */
834 exist = find_pending_request(dst, req->name);
836 RTE_LOG(ERR, EAL, "A pending request %s:%s\n", dst, req->name);
842 ret = send_msg(dst, req, MP_REQ);
844 RTE_LOG(ERR, EAL, "Fail to send request %s:%s\n",
848 } else if (ret == 0) {
852 param->user_reply.nb_sent++;
854 /* if alarm set fails, we simply ignore the reply */
855 if (rte_eal_alarm_set(ts->tv_sec * 1000000 + ts->tv_nsec / 1000,
856 async_reply_handle, pending_req) < 0) {
857 RTE_LOG(ERR, EAL, "Fail to set alarm for request %s:%s\n",
862 TAILQ_INSERT_TAIL(&pending_requests.requests, pending_req, next);
872 mp_request_sync(const char *dst, struct rte_mp_msg *req,
873 struct rte_mp_reply *reply, const struct timespec *ts)
876 struct rte_mp_msg msg, *tmp;
877 struct pending_request pending_req, *exist;
879 pending_req.type = REQUEST_TYPE_SYNC;
880 pending_req.reply_received = 0;
881 strlcpy(pending_req.dst, dst, sizeof(pending_req.dst));
882 pending_req.request = req;
883 pending_req.reply = &msg;
884 pthread_cond_init(&pending_req.sync.cond, NULL);
886 exist = find_pending_request(dst, req->name);
888 RTE_LOG(ERR, EAL, "A pending request %s:%s\n", dst, req->name);
893 ret = send_msg(dst, req, MP_REQ);
895 RTE_LOG(ERR, EAL, "Fail to send request %s:%s\n",
901 TAILQ_INSERT_TAIL(&pending_requests.requests, &pending_req, next);
906 ret = pthread_cond_timedwait(&pending_req.sync.cond,
907 &pending_requests.lock, ts);
908 } while (ret != 0 && ret != ETIMEDOUT);
910 TAILQ_REMOVE(&pending_requests.requests, &pending_req, next);
912 if (pending_req.reply_received == 0) {
913 RTE_LOG(ERR, EAL, "Fail to recv reply for request %s:%s\n",
915 rte_errno = ETIMEDOUT;
918 if (pending_req.reply_received == -1) {
919 RTE_LOG(DEBUG, EAL, "Asked to ignore response\n");
920 /* not receiving this message is not an error, so decrement
921 * number of sent messages
927 tmp = realloc(reply->msgs, sizeof(msg) * (reply->nb_received + 1));
929 RTE_LOG(ERR, EAL, "Fail to alloc reply for request %s:%s\n",
934 memcpy(&tmp[reply->nb_received], &msg, sizeof(msg));
936 reply->nb_received++;
940 int __rte_experimental
941 rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply,
942 const struct timespec *ts)
944 int dir_fd, ret = -1;
950 RTE_LOG(DEBUG, EAL, "request: %s\n", req->name);
953 reply->nb_received = 0;
956 if (check_input(req) != 0)
959 if (internal_config.no_shconf) {
960 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is disabled\n");
964 if (gettimeofday(&now, NULL) < 0) {
965 RTE_LOG(ERR, EAL, "Failed to get current time\n");
970 end.tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
971 end.tv_sec = now.tv_sec + ts->tv_sec +
972 (now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
974 /* for secondary process, send request to the primary process only */
975 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
976 pthread_mutex_lock(&pending_requests.lock);
977 ret = mp_request_sync(eal_mp_socket_path(), req, reply, &end);
978 pthread_mutex_unlock(&pending_requests.lock);
982 /* for primary process, broadcast request, and collect reply 1 by 1 */
983 mp_dir = opendir(mp_dir_path);
985 RTE_LOG(ERR, EAL, "Unable to open directory %s\n", mp_dir_path);
990 dir_fd = dirfd(mp_dir);
991 /* lock the directory to prevent processes spinning up while we send */
992 if (flock(dir_fd, LOCK_SH)) {
993 RTE_LOG(ERR, EAL, "Unable to lock directory %s\n",
999 pthread_mutex_lock(&pending_requests.lock);
1000 while ((ent = readdir(mp_dir))) {
1001 char path[PATH_MAX];
1003 if (fnmatch(mp_filter, ent->d_name, 0) != 0)
1006 snprintf(path, sizeof(path), "%s/%s", mp_dir_path,
1009 /* unlocks the mutex while waiting for response,
1012 if (mp_request_sync(path, req, reply, &end))
1018 pthread_mutex_unlock(&pending_requests.lock);
1019 /* unlock the directory */
1020 flock(dir_fd, LOCK_UN);
1023 /* dir_fd automatically closed on closedir */
1029 reply->nb_received = 0;
1035 int __rte_experimental
1036 rte_mp_request_async(struct rte_mp_msg *req, const struct timespec *ts,
1037 rte_mp_async_reply_t clb)
1039 struct rte_mp_msg *copy;
1040 struct pending_request *dummy;
1041 struct async_request_param *param;
1042 struct rte_mp_reply *reply;
1043 int dir_fd, ret = 0;
1047 struct timespec *end;
1048 bool dummy_used = false;
1050 RTE_LOG(DEBUG, EAL, "request: %s\n", req->name);
1052 if (check_input(req) != 0)
1055 if (internal_config.no_shconf) {
1056 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is disabled\n");
1060 if (gettimeofday(&now, NULL) < 0) {
1061 RTE_LOG(ERR, EAL, "Faile to get current time\n");
1065 copy = calloc(1, sizeof(*copy));
1066 dummy = calloc(1, sizeof(*dummy));
1067 param = calloc(1, sizeof(*param));
1068 if (copy == NULL || dummy == NULL || param == NULL) {
1069 RTE_LOG(ERR, EAL, "Failed to allocate memory for async reply\n");
1075 memcpy(copy, req, sizeof(*copy));
1077 param->n_responses_processed = 0;
1080 reply = ¶m->user_reply;
1082 end->tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
1083 end->tv_sec = now.tv_sec + ts->tv_sec +
1084 (now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
1086 reply->nb_received = 0;
1089 /* we have to lock the request queue here, as we will be adding a bunch
1090 * of requests to the queue at once, and some of the replies may arrive
1091 * before we add all of the requests to the queue.
1093 pthread_mutex_lock(&pending_requests.lock);
1095 /* we have to ensure that callback gets triggered even if we don't send
1096 * anything, therefore earlier we have allocated a dummy request. fill
1097 * it, and put it on the queue if we don't send any requests.
1099 dummy->type = REQUEST_TYPE_ASYNC;
1100 dummy->request = copy;
1101 dummy->reply = NULL;
1102 dummy->async.param = param;
1103 dummy->reply_received = 1; /* short-circuit the timeout */
1105 /* for secondary process, send request to the primary process only */
1106 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1107 ret = mp_request_async(eal_mp_socket_path(), copy, param, ts);
1109 /* if we didn't send anything, put dummy request on the queue */
1110 if (ret == 0 && reply->nb_sent == 0) {
1111 TAILQ_INSERT_TAIL(&pending_requests.requests, dummy,
1116 pthread_mutex_unlock(&pending_requests.lock);
1118 /* if we couldn't send anything, clean up */
1124 /* for primary process, broadcast request */
1125 mp_dir = opendir(mp_dir_path);
1127 RTE_LOG(ERR, EAL, "Unable to open directory %s\n", mp_dir_path);
1131 dir_fd = dirfd(mp_dir);
1133 /* lock the directory to prevent processes spinning up while we send */
1134 if (flock(dir_fd, LOCK_SH)) {
1135 RTE_LOG(ERR, EAL, "Unable to lock directory %s\n",
1141 while ((ent = readdir(mp_dir))) {
1142 char path[PATH_MAX];
1144 if (fnmatch(mp_filter, ent->d_name, 0) != 0)
1147 snprintf(path, sizeof(path), "%s/%s", mp_dir_path,
1150 if (mp_request_async(path, copy, param, ts))
1153 /* if we didn't send anything, put dummy request on the queue */
1154 if (ret == 0 && reply->nb_sent == 0) {
1155 TAILQ_INSERT_HEAD(&pending_requests.requests, dummy, next);
1159 /* finally, unlock the queue */
1160 pthread_mutex_unlock(&pending_requests.lock);
1162 /* unlock the directory */
1163 flock(dir_fd, LOCK_UN);
1165 /* dir_fd automatically closed on closedir */
1168 /* if dummy was unused, free it */
1176 pthread_mutex_unlock(&pending_requests.lock);
1184 int __rte_experimental
1185 rte_mp_reply(struct rte_mp_msg *msg, const char *peer)
1187 RTE_LOG(DEBUG, EAL, "reply: %s\n", msg->name);
1189 if (check_input(msg) != 0)
1193 RTE_LOG(ERR, EAL, "peer is not specified\n");
1198 if (internal_config.no_shconf) {
1199 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is disabled\n");
1203 return mp_send(msg, peer, MP_REP);