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_memcfg.h"
33 #include "eal_private.h"
34 #include "eal_filesystem.h"
35 #include "eal_internal_cfg.h"
37 static int mp_fd = -1;
38 static pthread_t mp_handle_tid;
39 static char mp_filter[PATH_MAX]; /* Filter for secondary process sockets */
40 static char mp_dir_path[PATH_MAX]; /* The directory path for all mp sockets */
41 static pthread_mutex_t mp_mutex_action = PTHREAD_MUTEX_INITIALIZER;
42 static char peer_name[PATH_MAX];
45 TAILQ_ENTRY(action_entry) next;
46 char action_name[RTE_MP_MAX_NAME_LEN];
50 /** Double linked list of actions. */
51 TAILQ_HEAD(action_entry_list, action_entry);
53 static struct action_entry_list action_entry_list =
54 TAILQ_HEAD_INITIALIZER(action_entry_list);
57 MP_MSG, /* Share message with peers, will not block */
58 MP_REQ, /* Request for information, Will block for a reply */
59 MP_REP, /* Response to previously-received request */
60 MP_IGN, /* Response telling requester to ignore this response */
63 struct mp_msg_internal {
65 struct rte_mp_msg msg;
68 struct async_request_param {
69 rte_mp_async_reply_t clb;
70 struct rte_mp_reply user_reply;
72 int n_responses_processed;
75 struct pending_request {
76 TAILQ_ENTRY(pending_request) next;
82 struct rte_mp_msg *request;
83 struct rte_mp_msg *reply;
88 struct async_request_param *param;
96 TAILQ_HEAD(pending_request_list, pending_request);
99 struct pending_request_list requests;
100 pthread_mutex_t lock;
101 } pending_requests = {
102 .requests = TAILQ_HEAD_INITIALIZER(pending_requests.requests),
103 .lock = PTHREAD_MUTEX_INITIALIZER,
104 /**< used in async requests only */
107 /* forward declarations */
109 mp_send(struct rte_mp_msg *msg, const char *peer, int type);
111 /* for use with alarm callback */
113 async_reply_handle(void *arg);
115 /* for use with process_msg */
116 static struct pending_request *
117 async_reply_handle_thread_unsafe(void *arg);
120 trigger_async_action(struct pending_request *req);
122 static struct pending_request *
123 find_pending_request(const char *dst, const char *act_name)
125 struct pending_request *r;
127 TAILQ_FOREACH(r, &pending_requests.requests, next) {
128 if (!strcmp(r->dst, dst) &&
129 !strcmp(r->request->name, act_name))
137 create_socket_path(const char *name, char *buf, int len)
139 const char *prefix = eal_mp_socket_path();
141 if (strlen(name) > 0)
142 snprintf(buf, len, "%s_%s", prefix, name);
144 strlcpy(buf, prefix, len);
148 rte_eal_primary_proc_alive(const char *config_file_path)
152 if (config_file_path)
153 config_fd = open(config_file_path, O_RDONLY);
157 path = eal_runtime_config_path();
158 config_fd = open(path, O_RDONLY);
163 int ret = lockf(config_fd, F_TEST, 0);
169 static struct action_entry *
170 find_action_entry_by_name(const char *name)
172 struct action_entry *entry;
174 TAILQ_FOREACH(entry, &action_entry_list, next) {
175 if (strncmp(entry->action_name, name, RTE_MP_MAX_NAME_LEN) == 0)
183 validate_action_name(const char *name)
186 RTE_LOG(ERR, EAL, "Action name cannot be NULL\n");
190 if (strnlen(name, RTE_MP_MAX_NAME_LEN) == 0) {
191 RTE_LOG(ERR, EAL, "Length of action name is zero\n");
195 if (strnlen(name, RTE_MP_MAX_NAME_LEN) == RTE_MP_MAX_NAME_LEN) {
203 rte_mp_action_register(const char *name, rte_mp_t action)
205 struct action_entry *entry;
206 const struct internal_config *internal_conf =
207 eal_get_internal_configuration();
209 if (validate_action_name(name) != 0)
212 if (internal_conf->no_shconf) {
213 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is disabled\n");
218 entry = malloc(sizeof(struct action_entry));
223 strlcpy(entry->action_name, name, sizeof(entry->action_name));
224 entry->action = action;
226 pthread_mutex_lock(&mp_mutex_action);
227 if (find_action_entry_by_name(name) != NULL) {
228 pthread_mutex_unlock(&mp_mutex_action);
233 TAILQ_INSERT_TAIL(&action_entry_list, entry, next);
234 pthread_mutex_unlock(&mp_mutex_action);
239 rte_mp_action_unregister(const char *name)
241 struct action_entry *entry;
242 const struct internal_config *internal_conf =
243 eal_get_internal_configuration();
245 if (validate_action_name(name) != 0)
248 if (internal_conf->no_shconf) {
249 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is disabled\n");
253 pthread_mutex_lock(&mp_mutex_action);
254 entry = find_action_entry_by_name(name);
256 pthread_mutex_unlock(&mp_mutex_action);
259 TAILQ_REMOVE(&action_entry_list, entry, next);
260 pthread_mutex_unlock(&mp_mutex_action);
265 read_msg(struct mp_msg_internal *m, struct sockaddr_un *s)
270 char control[CMSG_SPACE(sizeof(m->msg.fds))];
271 struct cmsghdr *cmsg;
272 int buflen = sizeof(*m) - sizeof(m->msg.fds);
274 memset(&msgh, 0, sizeof(msgh));
276 iov.iov_len = buflen;
279 msgh.msg_namelen = sizeof(*s);
282 msgh.msg_control = control;
283 msgh.msg_controllen = sizeof(control);
286 msglen = recvmsg(mp_fd, &msgh, 0);
288 /* zero length message means socket was closed */
296 RTE_LOG(ERR, EAL, "recvmsg failed, %s\n", strerror(errno));
300 if (msglen != buflen || (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) {
301 RTE_LOG(ERR, EAL, "truncated msg\n");
305 /* read auxiliary FDs if any */
306 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
307 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
308 if ((cmsg->cmsg_level == SOL_SOCKET) &&
309 (cmsg->cmsg_type == SCM_RIGHTS)) {
310 memcpy(m->msg.fds, CMSG_DATA(cmsg), sizeof(m->msg.fds));
314 /* sanity-check the response */
315 if (m->msg.num_fds < 0 || m->msg.num_fds > RTE_MP_MAX_FD_NUM) {
316 RTE_LOG(ERR, EAL, "invalid number of fd's received\n");
319 if (m->msg.len_param < 0 || m->msg.len_param > RTE_MP_MAX_PARAM_LEN) {
320 RTE_LOG(ERR, EAL, "invalid received data length\n");
327 process_msg(struct mp_msg_internal *m, struct sockaddr_un *s)
329 struct pending_request *pending_req;
330 struct action_entry *entry;
331 struct rte_mp_msg *msg = &m->msg;
332 rte_mp_t action = NULL;
333 const struct internal_config *internal_conf =
334 eal_get_internal_configuration();
336 RTE_LOG(DEBUG, EAL, "msg: %s\n", msg->name);
338 if (m->type == MP_REP || m->type == MP_IGN) {
339 struct pending_request *req = NULL;
341 pthread_mutex_lock(&pending_requests.lock);
342 pending_req = find_pending_request(s->sun_path, msg->name);
344 memcpy(pending_req->reply, msg, sizeof(*msg));
345 /* -1 indicates that we've been asked to ignore */
346 pending_req->reply_received =
347 m->type == MP_REP ? 1 : -1;
349 if (pending_req->type == REQUEST_TYPE_SYNC)
350 pthread_cond_signal(&pending_req->sync.cond);
351 else if (pending_req->type == REQUEST_TYPE_ASYNC)
352 req = async_reply_handle_thread_unsafe(
355 RTE_LOG(ERR, EAL, "Drop mp reply: %s\n", msg->name);
356 pthread_mutex_unlock(&pending_requests.lock);
359 trigger_async_action(req);
363 pthread_mutex_lock(&mp_mutex_action);
364 entry = find_action_entry_by_name(msg->name);
366 action = entry->action;
367 pthread_mutex_unlock(&mp_mutex_action);
370 if (m->type == MP_REQ && !internal_conf->init_complete) {
371 /* if this is a request, and init is not yet complete,
372 * and callback wasn't registered, we should tell the
373 * requester to ignore our existence because we're not
374 * yet ready to process this request.
376 struct rte_mp_msg dummy;
378 memset(&dummy, 0, sizeof(dummy));
379 strlcpy(dummy.name, msg->name, sizeof(dummy.name));
380 mp_send(&dummy, s->sun_path, MP_IGN);
382 RTE_LOG(ERR, EAL, "Cannot find action: %s\n",
385 } else if (action(msg, s->sun_path) < 0) {
386 RTE_LOG(ERR, EAL, "Fail to handle message: %s\n", msg->name);
391 mp_handle(void *arg __rte_unused)
393 struct mp_msg_internal msg;
394 struct sockaddr_un sa;
399 ret = read_msg(&msg, &sa);
403 process_msg(&msg, &sa);
410 timespec_cmp(const struct timespec *a, const struct timespec *b)
412 if (a->tv_sec < b->tv_sec)
414 if (a->tv_sec > b->tv_sec)
416 if (a->tv_nsec < b->tv_nsec)
418 if (a->tv_nsec > b->tv_nsec)
424 ACTION_FREE, /**< free the action entry, but don't trigger callback */
425 ACTION_TRIGGER /**< trigger callback, then free action entry */
428 static enum async_action
429 process_async_request(struct pending_request *sr, const struct timespec *now)
431 struct async_request_param *param;
432 struct rte_mp_reply *reply;
433 bool timeout, last_msg;
435 param = sr->async.param;
436 reply = ¶m->user_reply;
438 /* did we timeout? */
439 timeout = timespec_cmp(¶m->end, now) <= 0;
441 /* if we received a response, adjust relevant data and copy message. */
442 if (sr->reply_received == 1 && sr->reply) {
443 struct rte_mp_msg *msg, *user_msgs, *tmp;
446 user_msgs = reply->msgs;
448 tmp = realloc(user_msgs, sizeof(*msg) *
449 (reply->nb_received + 1));
451 RTE_LOG(ERR, EAL, "Fail to alloc reply for request %s:%s\n",
452 sr->dst, sr->request->name);
453 /* this entry is going to be removed and its message
454 * dropped, but we don't want to leak memory, so
459 reply->msgs = user_msgs;
460 memcpy(&user_msgs[reply->nb_received],
462 reply->nb_received++;
465 /* mark this request as processed */
466 param->n_responses_processed++;
467 } else if (sr->reply_received == -1) {
468 /* we were asked to ignore this process */
470 } else if (timeout) {
471 /* count it as processed response, but don't increment
474 param->n_responses_processed++;
479 last_msg = param->n_responses_processed == reply->nb_sent;
481 return last_msg ? ACTION_TRIGGER : ACTION_FREE;
485 trigger_async_action(struct pending_request *sr)
487 struct async_request_param *param;
488 struct rte_mp_reply *reply;
490 param = sr->async.param;
491 reply = ¶m->user_reply;
493 param->clb(sr->request, reply);
496 free(sr->async.param->user_reply.msgs);
497 free(sr->async.param);
502 static struct pending_request *
503 async_reply_handle_thread_unsafe(void *arg)
505 struct pending_request *req = (struct pending_request *)arg;
506 enum async_action action;
507 struct timespec ts_now;
509 if (clock_gettime(CLOCK_MONOTONIC, &ts_now) < 0) {
510 RTE_LOG(ERR, EAL, "Cannot get current time\n");
514 action = process_async_request(req, &ts_now);
516 TAILQ_REMOVE(&pending_requests.requests, req, next);
518 if (rte_eal_alarm_cancel(async_reply_handle, req) < 0) {
519 /* if we failed to cancel the alarm because it's already in
520 * progress, don't proceed because otherwise we will end up
521 * handling the same message twice.
523 if (rte_errno == EINPROGRESS) {
524 RTE_LOG(DEBUG, EAL, "Request handling is already in progress\n");
527 RTE_LOG(ERR, EAL, "Failed to cancel alarm\n");
530 if (action == ACTION_TRIGGER)
538 async_reply_handle(void *arg)
540 struct pending_request *req;
542 pthread_mutex_lock(&pending_requests.lock);
543 req = async_reply_handle_thread_unsafe(arg);
544 pthread_mutex_unlock(&pending_requests.lock);
547 trigger_async_action(req);
553 struct sockaddr_un un;
556 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
557 snprintf(peer_name, sizeof(peer_name),
558 "%d_%"PRIx64, getpid(), rte_rdtsc());
560 mp_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
562 RTE_LOG(ERR, EAL, "failed to create unix socket\n");
566 memset(&un, 0, sizeof(un));
567 un.sun_family = AF_UNIX;
569 create_socket_path(peer_name, un.sun_path, sizeof(un.sun_path));
571 unlink(un.sun_path); /* May still exist since last run */
573 if (bind(mp_fd, (struct sockaddr *)&un, sizeof(un)) < 0) {
574 RTE_LOG(ERR, EAL, "failed to bind %s: %s\n",
575 un.sun_path, strerror(errno));
580 RTE_LOG(INFO, EAL, "Multi-process socket %s\n", un.sun_path);
585 close_socket_fd(int fd)
590 create_socket_path(peer_name, path, sizeof(path));
595 rte_mp_channel_init(void)
599 const struct internal_config *internal_conf =
600 eal_get_internal_configuration();
602 /* in no shared files mode, we do not have secondary processes support,
603 * so no need to initialize IPC.
605 if (internal_conf->no_shconf) {
606 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC will be disabled\n");
611 /* create filter path */
612 create_socket_path("*", path, sizeof(path));
613 strlcpy(mp_filter, basename(path), sizeof(mp_filter));
615 /* path may have been modified, so recreate it */
616 create_socket_path("*", path, sizeof(path));
617 strlcpy(mp_dir_path, dirname(path), sizeof(mp_dir_path));
619 /* lock the directory */
620 dir_fd = open(mp_dir_path, O_RDONLY);
622 RTE_LOG(ERR, EAL, "failed to open %s: %s\n",
623 mp_dir_path, strerror(errno));
627 if (flock(dir_fd, LOCK_EX)) {
628 RTE_LOG(ERR, EAL, "failed to lock %s: %s\n",
629 mp_dir_path, strerror(errno));
634 if (open_socket_fd() < 0) {
639 if (rte_ctrl_thread_create(&mp_handle_tid, "rte_mp_handle",
640 NULL, mp_handle, NULL) < 0) {
641 RTE_LOG(ERR, EAL, "failed to create mp thread: %s\n",
649 /* unlock the directory */
650 flock(dir_fd, LOCK_UN);
657 rte_mp_channel_cleanup(void)
666 pthread_cancel(mp_handle_tid);
667 pthread_join(mp_handle_tid, NULL);
672 * Return -1, as fail to send message and it's caused by the local side.
673 * Return 0, as fail to send message and it's caused by the remote side.
674 * Return 1, as succeed to send message.
678 send_msg(const char *dst_path, struct rte_mp_msg *msg, int type)
683 struct cmsghdr *cmsg;
684 struct sockaddr_un dst;
685 struct mp_msg_internal m;
686 int fd_size = msg->num_fds * sizeof(int);
687 char control[CMSG_SPACE(fd_size)];
690 memcpy(&m.msg, msg, sizeof(*msg));
692 memset(&dst, 0, sizeof(dst));
693 dst.sun_family = AF_UNIX;
694 strlcpy(dst.sun_path, dst_path, sizeof(dst.sun_path));
696 memset(&msgh, 0, sizeof(msgh));
697 memset(control, 0, sizeof(control));
700 iov.iov_len = sizeof(m) - sizeof(msg->fds);
702 msgh.msg_name = &dst;
703 msgh.msg_namelen = sizeof(dst);
706 msgh.msg_control = control;
707 msgh.msg_controllen = sizeof(control);
709 cmsg = CMSG_FIRSTHDR(&msgh);
710 cmsg->cmsg_len = CMSG_LEN(fd_size);
711 cmsg->cmsg_level = SOL_SOCKET;
712 cmsg->cmsg_type = SCM_RIGHTS;
713 memcpy(CMSG_DATA(cmsg), msg->fds, fd_size);
716 snd = sendmsg(mp_fd, &msgh, 0);
717 } while (snd < 0 && errno == EINTR);
721 /* Check if it caused by peer process exits */
722 if (errno == ECONNREFUSED &&
723 rte_eal_process_type() == RTE_PROC_PRIMARY) {
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_SH)) {
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) != 0)
802 if (msg->len_param < 0) {
803 RTE_LOG(ERR, EAL, "Message data length is negative\n");
808 if (msg->num_fds < 0) {
809 RTE_LOG(ERR, EAL, "Number of fd's is negative\n");
814 if (msg->len_param > RTE_MP_MAX_PARAM_LEN) {
815 RTE_LOG(ERR, EAL, "Message data is too long\n");
820 if (msg->num_fds > RTE_MP_MAX_FD_NUM) {
821 RTE_LOG(ERR, EAL, "Cannot send more than %d FDs\n",
831 rte_mp_sendmsg(struct rte_mp_msg *msg)
833 const struct internal_config *internal_conf =
834 eal_get_internal_configuration();
836 if (check_input(msg) != 0)
839 if (internal_conf->no_shconf) {
840 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is disabled\n");
845 RTE_LOG(DEBUG, EAL, "sendmsg: %s\n", msg->name);
846 return mp_send(msg, NULL, MP_MSG);
850 mp_request_async(const char *dst, struct rte_mp_msg *req,
851 struct async_request_param *param, const struct timespec *ts)
853 struct rte_mp_msg *reply_msg;
854 struct pending_request *pending_req, *exist;
857 pending_req = calloc(1, sizeof(*pending_req));
858 reply_msg = calloc(1, sizeof(*reply_msg));
859 if (pending_req == NULL || reply_msg == NULL) {
860 RTE_LOG(ERR, EAL, "Could not allocate space for sync request\n");
866 pending_req->type = REQUEST_TYPE_ASYNC;
867 strlcpy(pending_req->dst, dst, sizeof(pending_req->dst));
868 pending_req->request = req;
869 pending_req->reply = reply_msg;
870 pending_req->async.param = param;
872 /* queue already locked by caller */
874 exist = find_pending_request(dst, req->name);
876 RTE_LOG(ERR, EAL, "A pending request %s:%s\n", dst, req->name);
882 ret = send_msg(dst, req, MP_REQ);
884 RTE_LOG(ERR, EAL, "Fail to send request %s:%s\n",
888 } else if (ret == 0) {
892 param->user_reply.nb_sent++;
894 /* if alarm set fails, we simply ignore the reply */
895 if (rte_eal_alarm_set(ts->tv_sec * 1000000 + ts->tv_nsec / 1000,
896 async_reply_handle, pending_req) < 0) {
897 RTE_LOG(ERR, EAL, "Fail to set alarm for request %s:%s\n",
902 TAILQ_INSERT_TAIL(&pending_requests.requests, pending_req, next);
912 mp_request_sync(const char *dst, struct rte_mp_msg *req,
913 struct rte_mp_reply *reply, const struct timespec *ts)
916 pthread_condattr_t attr;
917 struct rte_mp_msg msg, *tmp;
918 struct pending_request pending_req, *exist;
920 pending_req.type = REQUEST_TYPE_SYNC;
921 pending_req.reply_received = 0;
922 strlcpy(pending_req.dst, dst, sizeof(pending_req.dst));
923 pending_req.request = req;
924 pending_req.reply = &msg;
925 pthread_condattr_init(&attr);
926 pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
927 pthread_cond_init(&pending_req.sync.cond, &attr);
929 exist = find_pending_request(dst, req->name);
931 RTE_LOG(ERR, EAL, "A pending request %s:%s\n", dst, req->name);
936 ret = send_msg(dst, req, MP_REQ);
938 RTE_LOG(ERR, EAL, "Fail to send request %s:%s\n",
944 TAILQ_INSERT_TAIL(&pending_requests.requests, &pending_req, next);
949 ret = pthread_cond_timedwait(&pending_req.sync.cond,
950 &pending_requests.lock, ts);
951 } while (ret != 0 && ret != ETIMEDOUT);
953 TAILQ_REMOVE(&pending_requests.requests, &pending_req, next);
955 if (pending_req.reply_received == 0) {
956 RTE_LOG(ERR, EAL, "Fail to recv reply for request %s:%s\n",
958 rte_errno = ETIMEDOUT;
961 if (pending_req.reply_received == -1) {
962 RTE_LOG(DEBUG, EAL, "Asked to ignore response\n");
963 /* not receiving this message is not an error, so decrement
964 * number of sent messages
970 tmp = realloc(reply->msgs, sizeof(msg) * (reply->nb_received + 1));
972 RTE_LOG(ERR, EAL, "Fail to alloc reply for request %s:%s\n",
977 memcpy(&tmp[reply->nb_received], &msg, sizeof(msg));
979 reply->nb_received++;
984 rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply,
985 const struct timespec *ts)
987 int dir_fd, ret = -1;
990 struct timespec now, end;
991 const struct internal_config *internal_conf =
992 eal_get_internal_configuration();
994 RTE_LOG(DEBUG, EAL, "request: %s\n", req->name);
997 reply->nb_received = 0;
1000 if (check_input(req) != 0)
1003 if (internal_conf->no_shconf) {
1004 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is disabled\n");
1005 rte_errno = ENOTSUP;
1009 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0) {
1010 RTE_LOG(ERR, EAL, "Failed to get current time\n");
1015 end.tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
1016 end.tv_sec = now.tv_sec + ts->tv_sec +
1017 (now.tv_nsec + ts->tv_nsec) / 1000000000;
1019 /* for secondary process, send request to the primary process only */
1020 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1021 pthread_mutex_lock(&pending_requests.lock);
1022 ret = mp_request_sync(eal_mp_socket_path(), req, reply, &end);
1023 pthread_mutex_unlock(&pending_requests.lock);
1027 /* for primary process, broadcast request, and collect reply 1 by 1 */
1028 mp_dir = opendir(mp_dir_path);
1030 RTE_LOG(ERR, EAL, "Unable to open directory %s\n", mp_dir_path);
1035 dir_fd = dirfd(mp_dir);
1036 /* lock the directory to prevent processes spinning up while we send */
1037 if (flock(dir_fd, LOCK_SH)) {
1038 RTE_LOG(ERR, EAL, "Unable to lock directory %s\n",
1044 pthread_mutex_lock(&pending_requests.lock);
1045 while ((ent = readdir(mp_dir))) {
1046 char path[PATH_MAX];
1048 if (fnmatch(mp_filter, ent->d_name, 0) != 0)
1051 snprintf(path, sizeof(path), "%s/%s", mp_dir_path,
1054 /* unlocks the mutex while waiting for response,
1057 if (mp_request_sync(path, req, reply, &end))
1063 pthread_mutex_unlock(&pending_requests.lock);
1064 /* unlock the directory */
1065 flock(dir_fd, LOCK_UN);
1068 /* dir_fd automatically closed on closedir */
1074 reply->nb_received = 0;
1081 rte_mp_request_async(struct rte_mp_msg *req, const struct timespec *ts,
1082 rte_mp_async_reply_t clb)
1084 struct rte_mp_msg *copy;
1085 struct pending_request *dummy;
1086 struct async_request_param *param;
1087 struct rte_mp_reply *reply;
1088 int dir_fd, ret = 0;
1091 struct timespec now;
1092 struct timespec *end;
1093 bool dummy_used = false;
1094 const struct internal_config *internal_conf =
1095 eal_get_internal_configuration();
1097 RTE_LOG(DEBUG, EAL, "request: %s\n", req->name);
1099 if (check_input(req) != 0)
1102 if (internal_conf->no_shconf) {
1103 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is disabled\n");
1104 rte_errno = ENOTSUP;
1108 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0) {
1109 RTE_LOG(ERR, EAL, "Failed to get current time\n");
1113 copy = calloc(1, sizeof(*copy));
1114 dummy = calloc(1, sizeof(*dummy));
1115 param = calloc(1, sizeof(*param));
1116 if (copy == NULL || dummy == NULL || param == NULL) {
1117 RTE_LOG(ERR, EAL, "Failed to allocate memory for async reply\n");
1123 memcpy(copy, req, sizeof(*copy));
1125 param->n_responses_processed = 0;
1128 reply = ¶m->user_reply;
1130 end->tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
1131 end->tv_sec = now.tv_sec + ts->tv_sec +
1132 (now.tv_nsec + ts->tv_nsec) / 1000000000;
1134 reply->nb_received = 0;
1137 /* we have to lock the request queue here, as we will be adding a bunch
1138 * of requests to the queue at once, and some of the replies may arrive
1139 * before we add all of the requests to the queue.
1141 pthread_mutex_lock(&pending_requests.lock);
1143 /* we have to ensure that callback gets triggered even if we don't send
1144 * anything, therefore earlier we have allocated a dummy request. fill
1145 * it, and put it on the queue if we don't send any requests.
1147 dummy->type = REQUEST_TYPE_ASYNC;
1148 dummy->request = copy;
1149 dummy->reply = NULL;
1150 dummy->async.param = param;
1151 dummy->reply_received = 1; /* short-circuit the timeout */
1153 /* for secondary process, send request to the primary process only */
1154 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1155 ret = mp_request_async(eal_mp_socket_path(), copy, param, ts);
1157 /* if we didn't send anything, put dummy request on the queue */
1158 if (ret == 0 && reply->nb_sent == 0) {
1159 TAILQ_INSERT_TAIL(&pending_requests.requests, dummy,
1164 pthread_mutex_unlock(&pending_requests.lock);
1166 /* if we couldn't send anything, clean up */
1172 /* for primary process, broadcast request */
1173 mp_dir = opendir(mp_dir_path);
1175 RTE_LOG(ERR, EAL, "Unable to open directory %s\n", mp_dir_path);
1179 dir_fd = dirfd(mp_dir);
1181 /* lock the directory to prevent processes spinning up while we send */
1182 if (flock(dir_fd, LOCK_SH)) {
1183 RTE_LOG(ERR, EAL, "Unable to lock directory %s\n",
1189 while ((ent = readdir(mp_dir))) {
1190 char path[PATH_MAX];
1192 if (fnmatch(mp_filter, ent->d_name, 0) != 0)
1195 snprintf(path, sizeof(path), "%s/%s", mp_dir_path,
1198 if (mp_request_async(path, copy, param, ts))
1201 /* if we didn't send anything, put dummy request on the queue */
1202 if (ret == 0 && reply->nb_sent == 0) {
1203 TAILQ_INSERT_HEAD(&pending_requests.requests, dummy, next);
1207 /* finally, unlock the queue */
1208 pthread_mutex_unlock(&pending_requests.lock);
1210 /* unlock the directory */
1211 flock(dir_fd, LOCK_UN);
1213 /* dir_fd automatically closed on closedir */
1216 /* if dummy was unused, free it */
1224 pthread_mutex_unlock(&pending_requests.lock);
1233 rte_mp_reply(struct rte_mp_msg *msg, const char *peer)
1235 RTE_LOG(DEBUG, EAL, "reply: %s\n", msg->name);
1236 const struct internal_config *internal_conf =
1237 eal_get_internal_configuration();
1239 if (check_input(msg) != 0)
1243 RTE_LOG(ERR, EAL, "peer is not specified\n");
1248 if (internal_conf->no_shconf) {
1249 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is disabled\n");
1253 return mp_send(msg, peer, MP_REP);
1256 /* Internally, the status of the mp feature is represented as a three-state:
1257 * - "unknown" as long as no secondary process attached to a primary process
1258 * and there was no call to rte_mp_disable yet,
1259 * - "enabled" as soon as a secondary process attaches to a primary process,
1260 * - "disabled" when a primary process successfully called rte_mp_disable,
1269 set_mp_status(enum mp_status status)
1271 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1275 expected = MP_STATUS_UNKNOWN;
1277 if (__atomic_compare_exchange_n(&mcfg->mp_status, &expected, desired,
1278 false, __ATOMIC_RELAXED, __ATOMIC_RELAXED))
1281 return __atomic_load_n(&mcfg->mp_status, __ATOMIC_RELAXED) == desired;
1285 rte_mp_disable(void)
1287 return set_mp_status(MP_STATUS_DISABLED);
1291 __rte_mp_enable(void)
1293 return set_mp_status(MP_STATUS_ENABLED);