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);
285 msglen = recvmsg(mp_fd, &msgh, 0);
287 RTE_LOG(ERR, EAL, "recvmsg failed, %s\n", strerror(errno));
291 if (msglen != buflen || (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) {
292 RTE_LOG(ERR, EAL, "truncated msg\n");
296 /* read auxiliary FDs if any */
297 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
298 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
299 if ((cmsg->cmsg_level == SOL_SOCKET) &&
300 (cmsg->cmsg_type == SCM_RIGHTS)) {
301 memcpy(m->msg.fds, CMSG_DATA(cmsg), sizeof(m->msg.fds));
305 /* sanity-check the response */
306 if (m->msg.num_fds < 0 || m->msg.num_fds > RTE_MP_MAX_FD_NUM) {
307 RTE_LOG(ERR, EAL, "invalid number of fd's received\n");
310 if (m->msg.len_param < 0 || m->msg.len_param > RTE_MP_MAX_PARAM_LEN) {
311 RTE_LOG(ERR, EAL, "invalid received data length\n");
318 process_msg(struct mp_msg_internal *m, struct sockaddr_un *s)
320 struct pending_request *pending_req;
321 struct action_entry *entry;
322 struct rte_mp_msg *msg = &m->msg;
323 rte_mp_t action = NULL;
324 const struct internal_config *internal_conf =
325 eal_get_internal_configuration();
327 RTE_LOG(DEBUG, EAL, "msg: %s\n", msg->name);
329 if (m->type == MP_REP || m->type == MP_IGN) {
330 struct pending_request *req = NULL;
332 pthread_mutex_lock(&pending_requests.lock);
333 pending_req = find_pending_request(s->sun_path, msg->name);
335 memcpy(pending_req->reply, msg, sizeof(*msg));
336 /* -1 indicates that we've been asked to ignore */
337 pending_req->reply_received =
338 m->type == MP_REP ? 1 : -1;
340 if (pending_req->type == REQUEST_TYPE_SYNC)
341 pthread_cond_signal(&pending_req->sync.cond);
342 else if (pending_req->type == REQUEST_TYPE_ASYNC)
343 req = async_reply_handle_thread_unsafe(
346 RTE_LOG(ERR, EAL, "Drop mp reply: %s\n", msg->name);
347 pthread_mutex_unlock(&pending_requests.lock);
350 trigger_async_action(req);
354 pthread_mutex_lock(&mp_mutex_action);
355 entry = find_action_entry_by_name(msg->name);
357 action = entry->action;
358 pthread_mutex_unlock(&mp_mutex_action);
361 if (m->type == MP_REQ && !internal_conf->init_complete) {
362 /* if this is a request, and init is not yet complete,
363 * and callback wasn't registered, we should tell the
364 * requester to ignore our existence because we're not
365 * yet ready to process this request.
367 struct rte_mp_msg dummy;
369 memset(&dummy, 0, sizeof(dummy));
370 strlcpy(dummy.name, msg->name, sizeof(dummy.name));
371 mp_send(&dummy, s->sun_path, MP_IGN);
373 RTE_LOG(ERR, EAL, "Cannot find action: %s\n",
376 } else if (action(msg, s->sun_path) < 0) {
377 RTE_LOG(ERR, EAL, "Fail to handle message: %s\n", msg->name);
382 mp_handle(void *arg __rte_unused)
384 struct mp_msg_internal msg;
385 struct sockaddr_un sa;
388 if (read_msg(&msg, &sa) == 0)
389 process_msg(&msg, &sa);
396 timespec_cmp(const struct timespec *a, const struct timespec *b)
398 if (a->tv_sec < b->tv_sec)
400 if (a->tv_sec > b->tv_sec)
402 if (a->tv_nsec < b->tv_nsec)
404 if (a->tv_nsec > b->tv_nsec)
410 ACTION_FREE, /**< free the action entry, but don't trigger callback */
411 ACTION_TRIGGER /**< trigger callback, then free action entry */
414 static enum async_action
415 process_async_request(struct pending_request *sr, const struct timespec *now)
417 struct async_request_param *param;
418 struct rte_mp_reply *reply;
419 bool timeout, last_msg;
421 param = sr->async.param;
422 reply = ¶m->user_reply;
424 /* did we timeout? */
425 timeout = timespec_cmp(¶m->end, now) <= 0;
427 /* if we received a response, adjust relevant data and copy message. */
428 if (sr->reply_received == 1 && sr->reply) {
429 struct rte_mp_msg *msg, *user_msgs, *tmp;
432 user_msgs = reply->msgs;
434 tmp = realloc(user_msgs, sizeof(*msg) *
435 (reply->nb_received + 1));
437 RTE_LOG(ERR, EAL, "Fail to alloc reply for request %s:%s\n",
438 sr->dst, sr->request->name);
439 /* this entry is going to be removed and its message
440 * dropped, but we don't want to leak memory, so
445 reply->msgs = user_msgs;
446 memcpy(&user_msgs[reply->nb_received],
448 reply->nb_received++;
451 /* mark this request as processed */
452 param->n_responses_processed++;
453 } else if (sr->reply_received == -1) {
454 /* we were asked to ignore this process */
456 } else if (timeout) {
457 /* count it as processed response, but don't increment
460 param->n_responses_processed++;
465 last_msg = param->n_responses_processed == reply->nb_sent;
467 return last_msg ? ACTION_TRIGGER : ACTION_FREE;
471 trigger_async_action(struct pending_request *sr)
473 struct async_request_param *param;
474 struct rte_mp_reply *reply;
476 param = sr->async.param;
477 reply = ¶m->user_reply;
479 param->clb(sr->request, reply);
482 free(sr->async.param->user_reply.msgs);
483 free(sr->async.param);
488 static struct pending_request *
489 async_reply_handle_thread_unsafe(void *arg)
491 struct pending_request *req = (struct pending_request *)arg;
492 enum async_action action;
493 struct timespec ts_now;
495 if (clock_gettime(CLOCK_MONOTONIC, &ts_now) < 0) {
496 RTE_LOG(ERR, EAL, "Cannot get current time\n");
500 action = process_async_request(req, &ts_now);
502 TAILQ_REMOVE(&pending_requests.requests, req, next);
504 if (rte_eal_alarm_cancel(async_reply_handle, req) < 0) {
505 /* if we failed to cancel the alarm because it's already in
506 * progress, don't proceed because otherwise we will end up
507 * handling the same message twice.
509 if (rte_errno == EINPROGRESS) {
510 RTE_LOG(DEBUG, EAL, "Request handling is already in progress\n");
513 RTE_LOG(ERR, EAL, "Failed to cancel alarm\n");
516 if (action == ACTION_TRIGGER)
524 async_reply_handle(void *arg)
526 struct pending_request *req;
528 pthread_mutex_lock(&pending_requests.lock);
529 req = async_reply_handle_thread_unsafe(arg);
530 pthread_mutex_unlock(&pending_requests.lock);
533 trigger_async_action(req);
539 struct sockaddr_un un;
542 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
543 snprintf(peer_name, sizeof(peer_name),
544 "%d_%"PRIx64, getpid(), rte_rdtsc());
546 mp_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
548 RTE_LOG(ERR, EAL, "failed to create unix socket\n");
552 memset(&un, 0, sizeof(un));
553 un.sun_family = AF_UNIX;
555 create_socket_path(peer_name, un.sun_path, sizeof(un.sun_path));
557 unlink(un.sun_path); /* May still exist since last run */
559 if (bind(mp_fd, (struct sockaddr *)&un, sizeof(un)) < 0) {
560 RTE_LOG(ERR, EAL, "failed to bind %s: %s\n",
561 un.sun_path, strerror(errno));
566 RTE_LOG(INFO, EAL, "Multi-process socket %s\n", un.sun_path);
571 close_socket_fd(int fd)
576 create_socket_path(peer_name, path, sizeof(path));
581 rte_mp_channel_init(void)
585 const struct internal_config *internal_conf =
586 eal_get_internal_configuration();
588 /* in no shared files mode, we do not have secondary processes support,
589 * so no need to initialize IPC.
591 if (internal_conf->no_shconf) {
592 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC will be disabled\n");
597 /* create filter path */
598 create_socket_path("*", path, sizeof(path));
599 strlcpy(mp_filter, basename(path), sizeof(mp_filter));
601 /* path may have been modified, so recreate it */
602 create_socket_path("*", path, sizeof(path));
603 strlcpy(mp_dir_path, dirname(path), sizeof(mp_dir_path));
605 /* lock the directory */
606 dir_fd = open(mp_dir_path, O_RDONLY);
608 RTE_LOG(ERR, EAL, "failed to open %s: %s\n",
609 mp_dir_path, strerror(errno));
613 if (flock(dir_fd, LOCK_EX)) {
614 RTE_LOG(ERR, EAL, "failed to lock %s: %s\n",
615 mp_dir_path, strerror(errno));
620 if (open_socket_fd() < 0) {
625 if (rte_ctrl_thread_create(&mp_handle_tid, "rte_mp_handle",
626 NULL, mp_handle, NULL) < 0) {
627 RTE_LOG(ERR, EAL, "failed to create mp thread: %s\n",
635 /* unlock the directory */
636 flock(dir_fd, LOCK_UN);
643 rte_mp_channel_cleanup(void)
652 pthread_cancel(mp_handle_tid);
653 pthread_join(mp_handle_tid, NULL);
658 * Return -1, as fail to send message and it's caused by the local side.
659 * Return 0, as fail to send message and it's caused by the remote side.
660 * Return 1, as succeed to send message.
664 send_msg(const char *dst_path, struct rte_mp_msg *msg, int type)
669 struct cmsghdr *cmsg;
670 struct sockaddr_un dst;
671 struct mp_msg_internal m;
672 int fd_size = msg->num_fds * sizeof(int);
673 char control[CMSG_SPACE(fd_size)];
676 memcpy(&m.msg, msg, sizeof(*msg));
678 memset(&dst, 0, sizeof(dst));
679 dst.sun_family = AF_UNIX;
680 strlcpy(dst.sun_path, dst_path, sizeof(dst.sun_path));
682 memset(&msgh, 0, sizeof(msgh));
683 memset(control, 0, sizeof(control));
686 iov.iov_len = sizeof(m) - sizeof(msg->fds);
688 msgh.msg_name = &dst;
689 msgh.msg_namelen = sizeof(dst);
692 msgh.msg_control = control;
693 msgh.msg_controllen = sizeof(control);
695 cmsg = CMSG_FIRSTHDR(&msgh);
696 cmsg->cmsg_len = CMSG_LEN(fd_size);
697 cmsg->cmsg_level = SOL_SOCKET;
698 cmsg->cmsg_type = SCM_RIGHTS;
699 memcpy(CMSG_DATA(cmsg), msg->fds, fd_size);
702 snd = sendmsg(mp_fd, &msgh, 0);
703 } while (snd < 0 && errno == EINTR);
707 /* Check if it caused by peer process exits */
708 if (errno == ECONNREFUSED &&
709 rte_eal_process_type() == RTE_PROC_PRIMARY) {
713 RTE_LOG(ERR, EAL, "failed to send to (%s) due to %s\n",
714 dst_path, strerror(errno));
722 mp_send(struct rte_mp_msg *msg, const char *peer, int type)
728 if (!peer && (rte_eal_process_type() == RTE_PROC_SECONDARY))
729 peer = eal_mp_socket_path();
732 if (send_msg(peer, msg, type) < 0)
738 /* broadcast to all secondary processes */
739 mp_dir = opendir(mp_dir_path);
741 RTE_LOG(ERR, EAL, "Unable to open directory %s\n",
747 dir_fd = dirfd(mp_dir);
748 /* lock the directory to prevent processes spinning up while we send */
749 if (flock(dir_fd, LOCK_SH)) {
750 RTE_LOG(ERR, EAL, "Unable to lock directory %s\n",
757 while ((ent = readdir(mp_dir))) {
760 if (fnmatch(mp_filter, ent->d_name, 0) != 0)
763 snprintf(path, sizeof(path), "%s/%s", mp_dir_path,
765 if (send_msg(path, msg, type) < 0)
769 flock(dir_fd, LOCK_UN);
771 /* dir_fd automatically closed on closedir */
777 check_input(const struct rte_mp_msg *msg)
780 RTE_LOG(ERR, EAL, "Msg cannot be NULL\n");
785 if (validate_action_name(msg->name) != 0)
788 if (msg->len_param < 0) {
789 RTE_LOG(ERR, EAL, "Message data length is negative\n");
794 if (msg->num_fds < 0) {
795 RTE_LOG(ERR, EAL, "Number of fd's is negative\n");
800 if (msg->len_param > RTE_MP_MAX_PARAM_LEN) {
801 RTE_LOG(ERR, EAL, "Message data is too long\n");
806 if (msg->num_fds > RTE_MP_MAX_FD_NUM) {
807 RTE_LOG(ERR, EAL, "Cannot send more than %d FDs\n",
817 rte_mp_sendmsg(struct rte_mp_msg *msg)
819 const struct internal_config *internal_conf =
820 eal_get_internal_configuration();
822 if (check_input(msg) != 0)
825 if (internal_conf->no_shconf) {
826 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is disabled\n");
831 RTE_LOG(DEBUG, EAL, "sendmsg: %s\n", msg->name);
832 return mp_send(msg, NULL, MP_MSG);
836 mp_request_async(const char *dst, struct rte_mp_msg *req,
837 struct async_request_param *param, const struct timespec *ts)
839 struct rte_mp_msg *reply_msg;
840 struct pending_request *pending_req, *exist;
843 pending_req = calloc(1, sizeof(*pending_req));
844 reply_msg = calloc(1, sizeof(*reply_msg));
845 if (pending_req == NULL || reply_msg == NULL) {
846 RTE_LOG(ERR, EAL, "Could not allocate space for sync request\n");
852 pending_req->type = REQUEST_TYPE_ASYNC;
853 strlcpy(pending_req->dst, dst, sizeof(pending_req->dst));
854 pending_req->request = req;
855 pending_req->reply = reply_msg;
856 pending_req->async.param = param;
858 /* queue already locked by caller */
860 exist = find_pending_request(dst, req->name);
862 RTE_LOG(ERR, EAL, "A pending request %s:%s\n", dst, req->name);
868 ret = send_msg(dst, req, MP_REQ);
870 RTE_LOG(ERR, EAL, "Fail to send request %s:%s\n",
874 } else if (ret == 0) {
878 param->user_reply.nb_sent++;
880 /* if alarm set fails, we simply ignore the reply */
881 if (rte_eal_alarm_set(ts->tv_sec * 1000000 + ts->tv_nsec / 1000,
882 async_reply_handle, pending_req) < 0) {
883 RTE_LOG(ERR, EAL, "Fail to set alarm for request %s:%s\n",
888 TAILQ_INSERT_TAIL(&pending_requests.requests, pending_req, next);
898 mp_request_sync(const char *dst, struct rte_mp_msg *req,
899 struct rte_mp_reply *reply, const struct timespec *ts)
902 pthread_condattr_t attr;
903 struct rte_mp_msg msg, *tmp;
904 struct pending_request pending_req, *exist;
906 pending_req.type = REQUEST_TYPE_SYNC;
907 pending_req.reply_received = 0;
908 strlcpy(pending_req.dst, dst, sizeof(pending_req.dst));
909 pending_req.request = req;
910 pending_req.reply = &msg;
911 pthread_condattr_init(&attr);
912 pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
913 pthread_cond_init(&pending_req.sync.cond, &attr);
915 exist = find_pending_request(dst, req->name);
917 RTE_LOG(ERR, EAL, "A pending request %s:%s\n", dst, req->name);
922 ret = send_msg(dst, req, MP_REQ);
924 RTE_LOG(ERR, EAL, "Fail to send request %s:%s\n",
930 TAILQ_INSERT_TAIL(&pending_requests.requests, &pending_req, next);
935 ret = pthread_cond_timedwait(&pending_req.sync.cond,
936 &pending_requests.lock, ts);
937 } while (ret != 0 && ret != ETIMEDOUT);
939 TAILQ_REMOVE(&pending_requests.requests, &pending_req, next);
941 if (pending_req.reply_received == 0) {
942 RTE_LOG(ERR, EAL, "Fail to recv reply for request %s:%s\n",
944 rte_errno = ETIMEDOUT;
947 if (pending_req.reply_received == -1) {
948 RTE_LOG(DEBUG, EAL, "Asked to ignore response\n");
949 /* not receiving this message is not an error, so decrement
950 * number of sent messages
956 tmp = realloc(reply->msgs, sizeof(msg) * (reply->nb_received + 1));
958 RTE_LOG(ERR, EAL, "Fail to alloc reply for request %s:%s\n",
963 memcpy(&tmp[reply->nb_received], &msg, sizeof(msg));
965 reply->nb_received++;
970 rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply,
971 const struct timespec *ts)
973 int dir_fd, ret = -1;
976 struct timespec now, end;
977 const struct internal_config *internal_conf =
978 eal_get_internal_configuration();
980 RTE_LOG(DEBUG, EAL, "request: %s\n", req->name);
983 reply->nb_received = 0;
986 if (check_input(req) != 0)
989 if (internal_conf->no_shconf) {
990 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is disabled\n");
995 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0) {
996 RTE_LOG(ERR, EAL, "Failed to get current time\n");
1001 end.tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
1002 end.tv_sec = now.tv_sec + ts->tv_sec +
1003 (now.tv_nsec + ts->tv_nsec) / 1000000000;
1005 /* for secondary process, send request to the primary process only */
1006 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1007 pthread_mutex_lock(&pending_requests.lock);
1008 ret = mp_request_sync(eal_mp_socket_path(), req, reply, &end);
1009 pthread_mutex_unlock(&pending_requests.lock);
1013 /* for primary process, broadcast request, and collect reply 1 by 1 */
1014 mp_dir = opendir(mp_dir_path);
1016 RTE_LOG(ERR, EAL, "Unable to open directory %s\n", mp_dir_path);
1021 dir_fd = dirfd(mp_dir);
1022 /* lock the directory to prevent processes spinning up while we send */
1023 if (flock(dir_fd, LOCK_SH)) {
1024 RTE_LOG(ERR, EAL, "Unable to lock directory %s\n",
1030 pthread_mutex_lock(&pending_requests.lock);
1031 while ((ent = readdir(mp_dir))) {
1032 char path[PATH_MAX];
1034 if (fnmatch(mp_filter, ent->d_name, 0) != 0)
1037 snprintf(path, sizeof(path), "%s/%s", mp_dir_path,
1040 /* unlocks the mutex while waiting for response,
1043 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);
1054 /* dir_fd automatically closed on closedir */
1060 reply->nb_received = 0;
1067 rte_mp_request_async(struct rte_mp_msg *req, const struct timespec *ts,
1068 rte_mp_async_reply_t clb)
1070 struct rte_mp_msg *copy;
1071 struct pending_request *dummy;
1072 struct async_request_param *param;
1073 struct rte_mp_reply *reply;
1074 int dir_fd, ret = 0;
1077 struct timespec now;
1078 struct timespec *end;
1079 bool dummy_used = false;
1080 const struct internal_config *internal_conf =
1081 eal_get_internal_configuration();
1083 RTE_LOG(DEBUG, EAL, "request: %s\n", req->name);
1085 if (check_input(req) != 0)
1088 if (internal_conf->no_shconf) {
1089 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is disabled\n");
1090 rte_errno = ENOTSUP;
1094 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0) {
1095 RTE_LOG(ERR, EAL, "Failed to get current time\n");
1099 copy = calloc(1, sizeof(*copy));
1100 dummy = calloc(1, sizeof(*dummy));
1101 param = calloc(1, sizeof(*param));
1102 if (copy == NULL || dummy == NULL || param == NULL) {
1103 RTE_LOG(ERR, EAL, "Failed to allocate memory for async reply\n");
1109 memcpy(copy, req, sizeof(*copy));
1111 param->n_responses_processed = 0;
1114 reply = ¶m->user_reply;
1116 end->tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
1117 end->tv_sec = now.tv_sec + ts->tv_sec +
1118 (now.tv_nsec + ts->tv_nsec) / 1000000000;
1120 reply->nb_received = 0;
1123 /* we have to lock the request queue here, as we will be adding a bunch
1124 * of requests to the queue at once, and some of the replies may arrive
1125 * before we add all of the requests to the queue.
1127 pthread_mutex_lock(&pending_requests.lock);
1129 /* we have to ensure that callback gets triggered even if we don't send
1130 * anything, therefore earlier we have allocated a dummy request. fill
1131 * it, and put it on the queue if we don't send any requests.
1133 dummy->type = REQUEST_TYPE_ASYNC;
1134 dummy->request = copy;
1135 dummy->reply = NULL;
1136 dummy->async.param = param;
1137 dummy->reply_received = 1; /* short-circuit the timeout */
1139 /* for secondary process, send request to the primary process only */
1140 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1141 ret = mp_request_async(eal_mp_socket_path(), copy, param, ts);
1143 /* if we didn't send anything, put dummy request on the queue */
1144 if (ret == 0 && reply->nb_sent == 0) {
1145 TAILQ_INSERT_TAIL(&pending_requests.requests, dummy,
1150 pthread_mutex_unlock(&pending_requests.lock);
1152 /* if we couldn't send anything, clean up */
1158 /* for primary process, broadcast request */
1159 mp_dir = opendir(mp_dir_path);
1161 RTE_LOG(ERR, EAL, "Unable to open directory %s\n", mp_dir_path);
1165 dir_fd = dirfd(mp_dir);
1167 /* lock the directory to prevent processes spinning up while we send */
1168 if (flock(dir_fd, LOCK_SH)) {
1169 RTE_LOG(ERR, EAL, "Unable to lock directory %s\n",
1175 while ((ent = readdir(mp_dir))) {
1176 char path[PATH_MAX];
1178 if (fnmatch(mp_filter, ent->d_name, 0) != 0)
1181 snprintf(path, sizeof(path), "%s/%s", mp_dir_path,
1184 if (mp_request_async(path, copy, param, ts))
1187 /* if we didn't send anything, put dummy request on the queue */
1188 if (ret == 0 && reply->nb_sent == 0) {
1189 TAILQ_INSERT_HEAD(&pending_requests.requests, dummy, next);
1193 /* finally, unlock the queue */
1194 pthread_mutex_unlock(&pending_requests.lock);
1196 /* unlock the directory */
1197 flock(dir_fd, LOCK_UN);
1199 /* dir_fd automatically closed on closedir */
1202 /* if dummy was unused, free it */
1210 pthread_mutex_unlock(&pending_requests.lock);
1219 rte_mp_reply(struct rte_mp_msg *msg, const char *peer)
1221 RTE_LOG(DEBUG, EAL, "reply: %s\n", msg->name);
1222 const struct internal_config *internal_conf =
1223 eal_get_internal_configuration();
1225 if (check_input(msg) != 0)
1229 RTE_LOG(ERR, EAL, "peer is not specified\n");
1234 if (internal_conf->no_shconf) {
1235 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is disabled\n");
1239 return mp_send(msg, peer, MP_REP);
1242 /* Internally, the status of the mp feature is represented as a three-state:
1243 * - "unknown" as long as no secondary process attached to a primary process
1244 * and there was no call to rte_mp_disable yet,
1245 * - "enabled" as soon as a secondary process attaches to a primary process,
1246 * - "disabled" when a primary process successfully called rte_mp_disable,
1255 set_mp_status(enum mp_status status)
1257 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1261 expected = MP_STATUS_UNKNOWN;
1263 if (__atomic_compare_exchange_n(&mcfg->mp_status, &expected, desired,
1264 false, __ATOMIC_RELAXED, __ATOMIC_RELAXED))
1267 return __atomic_load_n(&mcfg->mp_status, __ATOMIC_RELAXED) == desired;
1271 rte_mp_disable(void)
1273 return set_mp_status(MP_STATUS_DISABLED);
1277 __rte_mp_enable(void)
1279 return set_mp_status(MP_STATUS_ENABLED);