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 char mp_filter[PATH_MAX]; /* Filter for secondary process sockets */
39 static char mp_dir_path[PATH_MAX]; /* The directory path for all mp sockets */
40 static pthread_mutex_t mp_mutex_action = PTHREAD_MUTEX_INITIALIZER;
41 static char peer_name[PATH_MAX];
44 TAILQ_ENTRY(action_entry) next;
45 char action_name[RTE_MP_MAX_NAME_LEN];
49 /** Double linked list of actions. */
50 TAILQ_HEAD(action_entry_list, action_entry);
52 static struct action_entry_list action_entry_list =
53 TAILQ_HEAD_INITIALIZER(action_entry_list);
56 MP_MSG, /* Share message with peers, will not block */
57 MP_REQ, /* Request for information, Will block for a reply */
58 MP_REP, /* Response to previously-received request */
59 MP_IGN, /* Response telling requester to ignore this response */
62 struct mp_msg_internal {
64 struct rte_mp_msg msg;
67 struct async_request_param {
68 rte_mp_async_reply_t clb;
69 struct rte_mp_reply user_reply;
71 int n_responses_processed;
74 struct pending_request {
75 TAILQ_ENTRY(pending_request) next;
81 struct rte_mp_msg *request;
82 struct rte_mp_msg *reply;
87 struct async_request_param *param;
95 TAILQ_HEAD(pending_request_list, pending_request);
98 struct pending_request_list requests;
100 } pending_requests = {
101 .requests = TAILQ_HEAD_INITIALIZER(pending_requests.requests),
102 .lock = PTHREAD_MUTEX_INITIALIZER,
103 /**< used in async requests only */
106 /* forward declarations */
108 mp_send(struct rte_mp_msg *msg, const char *peer, int type);
110 /* for use with alarm callback */
112 async_reply_handle(void *arg);
114 /* for use with process_msg */
115 static struct pending_request *
116 async_reply_handle_thread_unsafe(void *arg);
119 trigger_async_action(struct pending_request *req);
121 static struct pending_request *
122 find_pending_request(const char *dst, const char *act_name)
124 struct pending_request *r;
126 TAILQ_FOREACH(r, &pending_requests.requests, next) {
127 if (!strcmp(r->dst, dst) &&
128 !strcmp(r->request->name, act_name))
136 create_socket_path(const char *name, char *buf, int len)
138 const char *prefix = eal_mp_socket_path();
140 if (strlen(name) > 0)
141 snprintf(buf, len, "%s_%s", prefix, name);
143 strlcpy(buf, prefix, len);
147 rte_eal_primary_proc_alive(const char *config_file_path)
151 if (config_file_path)
152 config_fd = open(config_file_path, O_RDONLY);
156 path = eal_runtime_config_path();
157 config_fd = open(path, O_RDONLY);
162 int ret = lockf(config_fd, F_TEST, 0);
168 static struct action_entry *
169 find_action_entry_by_name(const char *name)
171 struct action_entry *entry;
173 TAILQ_FOREACH(entry, &action_entry_list, next) {
174 if (strncmp(entry->action_name, name, RTE_MP_MAX_NAME_LEN) == 0)
182 validate_action_name(const char *name)
185 RTE_LOG(ERR, EAL, "Action name cannot be NULL\n");
189 if (strnlen(name, RTE_MP_MAX_NAME_LEN) == 0) {
190 RTE_LOG(ERR, EAL, "Length of action name is zero\n");
194 if (strnlen(name, RTE_MP_MAX_NAME_LEN) == RTE_MP_MAX_NAME_LEN) {
202 rte_mp_action_register(const char *name, rte_mp_t action)
204 struct action_entry *entry;
205 const struct internal_config *internal_conf =
206 eal_get_internal_configuration();
208 if (validate_action_name(name) != 0)
211 if (internal_conf->no_shconf) {
212 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is disabled\n");
217 entry = malloc(sizeof(struct action_entry));
222 strlcpy(entry->action_name, name, sizeof(entry->action_name));
223 entry->action = action;
225 pthread_mutex_lock(&mp_mutex_action);
226 if (find_action_entry_by_name(name) != NULL) {
227 pthread_mutex_unlock(&mp_mutex_action);
232 TAILQ_INSERT_TAIL(&action_entry_list, entry, next);
233 pthread_mutex_unlock(&mp_mutex_action);
238 rte_mp_action_unregister(const char *name)
240 struct action_entry *entry;
241 const struct internal_config *internal_conf =
242 eal_get_internal_configuration();
244 if (validate_action_name(name) != 0)
247 if (internal_conf->no_shconf) {
248 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is disabled\n");
252 pthread_mutex_lock(&mp_mutex_action);
253 entry = find_action_entry_by_name(name);
255 pthread_mutex_unlock(&mp_mutex_action);
258 TAILQ_REMOVE(&action_entry_list, entry, next);
259 pthread_mutex_unlock(&mp_mutex_action);
264 read_msg(struct mp_msg_internal *m, struct sockaddr_un *s)
269 char control[CMSG_SPACE(sizeof(m->msg.fds))];
270 struct cmsghdr *cmsg;
271 int buflen = sizeof(*m) - sizeof(m->msg.fds);
273 memset(&msgh, 0, sizeof(msgh));
275 iov.iov_len = buflen;
278 msgh.msg_namelen = sizeof(*s);
281 msgh.msg_control = control;
282 msgh.msg_controllen = sizeof(control);
284 msglen = recvmsg(mp_fd, &msgh, 0);
286 RTE_LOG(ERR, EAL, "recvmsg failed, %s\n", strerror(errno));
290 if (msglen != buflen || (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) {
291 RTE_LOG(ERR, EAL, "truncated msg\n");
295 /* read auxiliary FDs if any */
296 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
297 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
298 if ((cmsg->cmsg_level == SOL_SOCKET) &&
299 (cmsg->cmsg_type == SCM_RIGHTS)) {
300 memcpy(m->msg.fds, CMSG_DATA(cmsg), sizeof(m->msg.fds));
304 /* sanity-check the response */
305 if (m->msg.num_fds < 0 || m->msg.num_fds > RTE_MP_MAX_FD_NUM) {
306 RTE_LOG(ERR, EAL, "invalid number of fd's received\n");
309 if (m->msg.len_param < 0 || m->msg.len_param > RTE_MP_MAX_PARAM_LEN) {
310 RTE_LOG(ERR, EAL, "invalid received data length\n");
317 process_msg(struct mp_msg_internal *m, struct sockaddr_un *s)
319 struct pending_request *pending_req;
320 struct action_entry *entry;
321 struct rte_mp_msg *msg = &m->msg;
322 rte_mp_t action = NULL;
323 const struct internal_config *internal_conf =
324 eal_get_internal_configuration();
326 RTE_LOG(DEBUG, EAL, "msg: %s\n", msg->name);
328 if (m->type == MP_REP || m->type == MP_IGN) {
329 struct pending_request *req = NULL;
331 pthread_mutex_lock(&pending_requests.lock);
332 pending_req = find_pending_request(s->sun_path, msg->name);
334 memcpy(pending_req->reply, msg, sizeof(*msg));
335 /* -1 indicates that we've been asked to ignore */
336 pending_req->reply_received =
337 m->type == MP_REP ? 1 : -1;
339 if (pending_req->type == REQUEST_TYPE_SYNC)
340 pthread_cond_signal(&pending_req->sync.cond);
341 else if (pending_req->type == REQUEST_TYPE_ASYNC)
342 req = async_reply_handle_thread_unsafe(
345 RTE_LOG(ERR, EAL, "Drop mp reply: %s\n", msg->name);
346 pthread_mutex_unlock(&pending_requests.lock);
349 trigger_async_action(req);
353 pthread_mutex_lock(&mp_mutex_action);
354 entry = find_action_entry_by_name(msg->name);
356 action = entry->action;
357 pthread_mutex_unlock(&mp_mutex_action);
360 if (m->type == MP_REQ && !internal_conf->init_complete) {
361 /* if this is a request, and init is not yet complete,
362 * and callback wasn't registered, we should tell the
363 * requester to ignore our existence because we're not
364 * yet ready to process this request.
366 struct rte_mp_msg dummy;
368 memset(&dummy, 0, sizeof(dummy));
369 strlcpy(dummy.name, msg->name, sizeof(dummy.name));
370 mp_send(&dummy, s->sun_path, MP_IGN);
372 RTE_LOG(ERR, EAL, "Cannot find action: %s\n",
375 } else if (action(msg, s->sun_path) < 0) {
376 RTE_LOG(ERR, EAL, "Fail to handle message: %s\n", msg->name);
381 mp_handle(void *arg __rte_unused)
383 struct mp_msg_internal msg;
384 struct sockaddr_un sa;
387 if (read_msg(&msg, &sa) == 0)
388 process_msg(&msg, &sa);
395 timespec_cmp(const struct timespec *a, const struct timespec *b)
397 if (a->tv_sec < b->tv_sec)
399 if (a->tv_sec > b->tv_sec)
401 if (a->tv_nsec < b->tv_nsec)
403 if (a->tv_nsec > b->tv_nsec)
409 ACTION_FREE, /**< free the action entry, but don't trigger callback */
410 ACTION_TRIGGER /**< trigger callback, then free action entry */
413 static enum async_action
414 process_async_request(struct pending_request *sr, const struct timespec *now)
416 struct async_request_param *param;
417 struct rte_mp_reply *reply;
418 bool timeout, last_msg;
420 param = sr->async.param;
421 reply = ¶m->user_reply;
423 /* did we timeout? */
424 timeout = timespec_cmp(¶m->end, now) <= 0;
426 /* if we received a response, adjust relevant data and copy message. */
427 if (sr->reply_received == 1 && sr->reply) {
428 struct rte_mp_msg *msg, *user_msgs, *tmp;
431 user_msgs = reply->msgs;
433 tmp = realloc(user_msgs, sizeof(*msg) *
434 (reply->nb_received + 1));
436 RTE_LOG(ERR, EAL, "Fail to alloc reply for request %s:%s\n",
437 sr->dst, sr->request->name);
438 /* this entry is going to be removed and its message
439 * dropped, but we don't want to leak memory, so
444 reply->msgs = user_msgs;
445 memcpy(&user_msgs[reply->nb_received],
447 reply->nb_received++;
450 /* mark this request as processed */
451 param->n_responses_processed++;
452 } else if (sr->reply_received == -1) {
453 /* we were asked to ignore this process */
455 } else if (timeout) {
456 /* count it as processed response, but don't increment
459 param->n_responses_processed++;
464 last_msg = param->n_responses_processed == reply->nb_sent;
466 return last_msg ? ACTION_TRIGGER : ACTION_FREE;
470 trigger_async_action(struct pending_request *sr)
472 struct async_request_param *param;
473 struct rte_mp_reply *reply;
475 param = sr->async.param;
476 reply = ¶m->user_reply;
478 param->clb(sr->request, reply);
481 free(sr->async.param->user_reply.msgs);
482 free(sr->async.param);
487 static struct pending_request *
488 async_reply_handle_thread_unsafe(void *arg)
490 struct pending_request *req = (struct pending_request *)arg;
491 enum async_action action;
492 struct timespec ts_now;
495 if (gettimeofday(&now, NULL) < 0) {
496 RTE_LOG(ERR, EAL, "Cannot get current time\n");
499 ts_now.tv_nsec = now.tv_usec * 1000;
500 ts_now.tv_sec = now.tv_sec;
502 action = process_async_request(req, &ts_now);
504 TAILQ_REMOVE(&pending_requests.requests, req, next);
506 if (rte_eal_alarm_cancel(async_reply_handle, req) < 0) {
507 /* if we failed to cancel the alarm because it's already in
508 * progress, don't proceed because otherwise we will end up
509 * handling the same message twice.
511 if (rte_errno == EINPROGRESS) {
512 RTE_LOG(DEBUG, EAL, "Request handling is already in progress\n");
515 RTE_LOG(ERR, EAL, "Failed to cancel alarm\n");
518 if (action == ACTION_TRIGGER)
526 async_reply_handle(void *arg)
528 struct pending_request *req;
530 pthread_mutex_lock(&pending_requests.lock);
531 req = async_reply_handle_thread_unsafe(arg);
532 pthread_mutex_unlock(&pending_requests.lock);
535 trigger_async_action(req);
541 struct sockaddr_un un;
544 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
545 snprintf(peer_name, sizeof(peer_name),
546 "%d_%"PRIx64, getpid(), rte_rdtsc());
548 mp_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
550 RTE_LOG(ERR, EAL, "failed to create unix socket\n");
554 memset(&un, 0, sizeof(un));
555 un.sun_family = AF_UNIX;
557 create_socket_path(peer_name, un.sun_path, sizeof(un.sun_path));
559 unlink(un.sun_path); /* May still exist since last run */
561 if (bind(mp_fd, (struct sockaddr *)&un, sizeof(un)) < 0) {
562 RTE_LOG(ERR, EAL, "failed to bind %s: %s\n",
563 un.sun_path, strerror(errno));
568 RTE_LOG(INFO, EAL, "Multi-process socket %s\n", un.sun_path);
573 close_socket_fd(void)
581 create_socket_path(peer_name, path, sizeof(path));
586 rte_mp_channel_init(void)
590 pthread_t mp_handle_tid;
591 const struct internal_config *internal_conf =
592 eal_get_internal_configuration();
594 /* in no shared files mode, we do not have secondary processes support,
595 * so no need to initialize IPC.
597 if (internal_conf->no_shconf) {
598 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC will be disabled\n");
603 /* create filter path */
604 create_socket_path("*", path, sizeof(path));
605 strlcpy(mp_filter, basename(path), sizeof(mp_filter));
607 /* path may have been modified, so recreate it */
608 create_socket_path("*", path, sizeof(path));
609 strlcpy(mp_dir_path, dirname(path), sizeof(mp_dir_path));
611 /* lock the directory */
612 dir_fd = open(mp_dir_path, O_RDONLY);
614 RTE_LOG(ERR, EAL, "failed to open %s: %s\n",
615 mp_dir_path, strerror(errno));
619 if (flock(dir_fd, LOCK_EX)) {
620 RTE_LOG(ERR, EAL, "failed to lock %s: %s\n",
621 mp_dir_path, strerror(errno));
626 if (open_socket_fd() < 0) {
631 if (rte_ctrl_thread_create(&mp_handle_tid, "rte_mp_handle",
632 NULL, mp_handle, NULL) < 0) {
633 RTE_LOG(ERR, EAL, "failed to create mp thread: %s\n",
641 /* unlock the directory */
642 flock(dir_fd, LOCK_UN);
649 rte_mp_channel_cleanup(void)
655 * Return -1, as fail to send message and it's caused by the local side.
656 * Return 0, as fail to send message and it's caused by the remote side.
657 * Return 1, as succeed to send message.
661 send_msg(const char *dst_path, struct rte_mp_msg *msg, int type)
666 struct cmsghdr *cmsg;
667 struct sockaddr_un dst;
668 struct mp_msg_internal m;
669 int fd_size = msg->num_fds * sizeof(int);
670 char control[CMSG_SPACE(fd_size)];
673 memcpy(&m.msg, msg, sizeof(*msg));
675 memset(&dst, 0, sizeof(dst));
676 dst.sun_family = AF_UNIX;
677 strlcpy(dst.sun_path, dst_path, sizeof(dst.sun_path));
679 memset(&msgh, 0, sizeof(msgh));
680 memset(control, 0, sizeof(control));
683 iov.iov_len = sizeof(m) - sizeof(msg->fds);
685 msgh.msg_name = &dst;
686 msgh.msg_namelen = sizeof(dst);
689 msgh.msg_control = control;
690 msgh.msg_controllen = sizeof(control);
692 cmsg = CMSG_FIRSTHDR(&msgh);
693 cmsg->cmsg_len = CMSG_LEN(fd_size);
694 cmsg->cmsg_level = SOL_SOCKET;
695 cmsg->cmsg_type = SCM_RIGHTS;
696 memcpy(CMSG_DATA(cmsg), msg->fds, fd_size);
699 snd = sendmsg(mp_fd, &msgh, 0);
700 } while (snd < 0 && errno == EINTR);
704 /* Check if it caused by peer process exits */
705 if (errno == ECONNREFUSED &&
706 rte_eal_process_type() == RTE_PROC_PRIMARY) {
710 RTE_LOG(ERR, EAL, "failed to send to (%s) due to %s\n",
711 dst_path, strerror(errno));
719 mp_send(struct rte_mp_msg *msg, const char *peer, int type)
725 if (!peer && (rte_eal_process_type() == RTE_PROC_SECONDARY))
726 peer = eal_mp_socket_path();
729 if (send_msg(peer, msg, type) < 0)
735 /* broadcast to all secondary processes */
736 mp_dir = opendir(mp_dir_path);
738 RTE_LOG(ERR, EAL, "Unable to open directory %s\n",
744 dir_fd = dirfd(mp_dir);
745 /* lock the directory to prevent processes spinning up while we send */
746 if (flock(dir_fd, LOCK_SH)) {
747 RTE_LOG(ERR, EAL, "Unable to lock directory %s\n",
754 while ((ent = readdir(mp_dir))) {
757 if (fnmatch(mp_filter, ent->d_name, 0) != 0)
760 snprintf(path, sizeof(path), "%s/%s", mp_dir_path,
762 if (send_msg(path, msg, type) < 0)
766 flock(dir_fd, LOCK_UN);
768 /* dir_fd automatically closed on closedir */
774 check_input(const struct rte_mp_msg *msg)
777 RTE_LOG(ERR, EAL, "Msg cannot be NULL\n");
782 if (validate_action_name(msg->name) != 0)
785 if (msg->len_param < 0) {
786 RTE_LOG(ERR, EAL, "Message data length is negative\n");
791 if (msg->num_fds < 0) {
792 RTE_LOG(ERR, EAL, "Number of fd's is negative\n");
797 if (msg->len_param > RTE_MP_MAX_PARAM_LEN) {
798 RTE_LOG(ERR, EAL, "Message data is too long\n");
803 if (msg->num_fds > RTE_MP_MAX_FD_NUM) {
804 RTE_LOG(ERR, EAL, "Cannot send more than %d FDs\n",
814 rte_mp_sendmsg(struct rte_mp_msg *msg)
816 const struct internal_config *internal_conf =
817 eal_get_internal_configuration();
819 if (check_input(msg) != 0)
822 if (internal_conf->no_shconf) {
823 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is disabled\n");
828 RTE_LOG(DEBUG, EAL, "sendmsg: %s\n", msg->name);
829 return mp_send(msg, NULL, MP_MSG);
833 mp_request_async(const char *dst, struct rte_mp_msg *req,
834 struct async_request_param *param, const struct timespec *ts)
836 struct rte_mp_msg *reply_msg;
837 struct pending_request *pending_req, *exist;
840 pending_req = calloc(1, sizeof(*pending_req));
841 reply_msg = calloc(1, sizeof(*reply_msg));
842 if (pending_req == NULL || reply_msg == NULL) {
843 RTE_LOG(ERR, EAL, "Could not allocate space for sync request\n");
849 pending_req->type = REQUEST_TYPE_ASYNC;
850 strlcpy(pending_req->dst, dst, sizeof(pending_req->dst));
851 pending_req->request = req;
852 pending_req->reply = reply_msg;
853 pending_req->async.param = param;
855 /* queue already locked by caller */
857 exist = find_pending_request(dst, req->name);
859 RTE_LOG(ERR, EAL, "A pending request %s:%s\n", dst, req->name);
865 ret = send_msg(dst, req, MP_REQ);
867 RTE_LOG(ERR, EAL, "Fail to send request %s:%s\n",
871 } else if (ret == 0) {
875 param->user_reply.nb_sent++;
877 /* if alarm set fails, we simply ignore the reply */
878 if (rte_eal_alarm_set(ts->tv_sec * 1000000 + ts->tv_nsec / 1000,
879 async_reply_handle, pending_req) < 0) {
880 RTE_LOG(ERR, EAL, "Fail to set alarm for request %s:%s\n",
885 TAILQ_INSERT_TAIL(&pending_requests.requests, pending_req, next);
895 mp_request_sync(const char *dst, struct rte_mp_msg *req,
896 struct rte_mp_reply *reply, const struct timespec *ts)
899 struct rte_mp_msg msg, *tmp;
900 struct pending_request pending_req, *exist;
902 pending_req.type = REQUEST_TYPE_SYNC;
903 pending_req.reply_received = 0;
904 strlcpy(pending_req.dst, dst, sizeof(pending_req.dst));
905 pending_req.request = req;
906 pending_req.reply = &msg;
907 pthread_cond_init(&pending_req.sync.cond, NULL);
909 exist = find_pending_request(dst, req->name);
911 RTE_LOG(ERR, EAL, "A pending request %s:%s\n", dst, req->name);
916 ret = send_msg(dst, req, MP_REQ);
918 RTE_LOG(ERR, EAL, "Fail to send request %s:%s\n",
924 TAILQ_INSERT_TAIL(&pending_requests.requests, &pending_req, next);
929 ret = pthread_cond_timedwait(&pending_req.sync.cond,
930 &pending_requests.lock, ts);
931 } while (ret != 0 && ret != ETIMEDOUT);
933 TAILQ_REMOVE(&pending_requests.requests, &pending_req, next);
935 if (pending_req.reply_received == 0) {
936 RTE_LOG(ERR, EAL, "Fail to recv reply for request %s:%s\n",
938 rte_errno = ETIMEDOUT;
941 if (pending_req.reply_received == -1) {
942 RTE_LOG(DEBUG, EAL, "Asked to ignore response\n");
943 /* not receiving this message is not an error, so decrement
944 * number of sent messages
950 tmp = realloc(reply->msgs, sizeof(msg) * (reply->nb_received + 1));
952 RTE_LOG(ERR, EAL, "Fail to alloc reply for request %s:%s\n",
957 memcpy(&tmp[reply->nb_received], &msg, sizeof(msg));
959 reply->nb_received++;
964 rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply,
965 const struct timespec *ts)
967 int dir_fd, ret = -1;
972 const struct internal_config *internal_conf =
973 eal_get_internal_configuration();
975 RTE_LOG(DEBUG, EAL, "request: %s\n", req->name);
978 reply->nb_received = 0;
981 if (check_input(req) != 0)
984 if (internal_conf->no_shconf) {
985 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is disabled\n");
990 if (gettimeofday(&now, NULL) < 0) {
991 RTE_LOG(ERR, EAL, "Failed to get current time\n");
996 end.tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
997 end.tv_sec = now.tv_sec + ts->tv_sec +
998 (now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
1000 /* for secondary process, send request to the primary process only */
1001 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1002 pthread_mutex_lock(&pending_requests.lock);
1003 ret = mp_request_sync(eal_mp_socket_path(), req, reply, &end);
1004 pthread_mutex_unlock(&pending_requests.lock);
1008 /* for primary process, broadcast request, and collect reply 1 by 1 */
1009 mp_dir = opendir(mp_dir_path);
1011 RTE_LOG(ERR, EAL, "Unable to open directory %s\n", mp_dir_path);
1016 dir_fd = dirfd(mp_dir);
1017 /* lock the directory to prevent processes spinning up while we send */
1018 if (flock(dir_fd, LOCK_SH)) {
1019 RTE_LOG(ERR, EAL, "Unable to lock directory %s\n",
1025 pthread_mutex_lock(&pending_requests.lock);
1026 while ((ent = readdir(mp_dir))) {
1027 char path[PATH_MAX];
1029 if (fnmatch(mp_filter, ent->d_name, 0) != 0)
1032 snprintf(path, sizeof(path), "%s/%s", mp_dir_path,
1035 /* unlocks the mutex while waiting for response,
1038 if (mp_request_sync(path, req, reply, &end))
1044 pthread_mutex_unlock(&pending_requests.lock);
1045 /* unlock the directory */
1046 flock(dir_fd, LOCK_UN);
1049 /* dir_fd automatically closed on closedir */
1055 reply->nb_received = 0;
1062 rte_mp_request_async(struct rte_mp_msg *req, const struct timespec *ts,
1063 rte_mp_async_reply_t clb)
1065 struct rte_mp_msg *copy;
1066 struct pending_request *dummy;
1067 struct async_request_param *param;
1068 struct rte_mp_reply *reply;
1069 int dir_fd, ret = 0;
1073 struct timespec *end;
1074 bool dummy_used = false;
1075 const struct internal_config *internal_conf =
1076 eal_get_internal_configuration();
1078 RTE_LOG(DEBUG, EAL, "request: %s\n", req->name);
1080 if (check_input(req) != 0)
1083 if (internal_conf->no_shconf) {
1084 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is disabled\n");
1085 rte_errno = ENOTSUP;
1089 if (gettimeofday(&now, NULL) < 0) {
1090 RTE_LOG(ERR, EAL, "Failed to get current time\n");
1094 copy = calloc(1, sizeof(*copy));
1095 dummy = calloc(1, sizeof(*dummy));
1096 param = calloc(1, sizeof(*param));
1097 if (copy == NULL || dummy == NULL || param == NULL) {
1098 RTE_LOG(ERR, EAL, "Failed to allocate memory for async reply\n");
1104 memcpy(copy, req, sizeof(*copy));
1106 param->n_responses_processed = 0;
1109 reply = ¶m->user_reply;
1111 end->tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
1112 end->tv_sec = now.tv_sec + ts->tv_sec +
1113 (now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
1115 reply->nb_received = 0;
1118 /* we have to lock the request queue here, as we will be adding a bunch
1119 * of requests to the queue at once, and some of the replies may arrive
1120 * before we add all of the requests to the queue.
1122 pthread_mutex_lock(&pending_requests.lock);
1124 /* we have to ensure that callback gets triggered even if we don't send
1125 * anything, therefore earlier we have allocated a dummy request. fill
1126 * it, and put it on the queue if we don't send any requests.
1128 dummy->type = REQUEST_TYPE_ASYNC;
1129 dummy->request = copy;
1130 dummy->reply = NULL;
1131 dummy->async.param = param;
1132 dummy->reply_received = 1; /* short-circuit the timeout */
1134 /* for secondary process, send request to the primary process only */
1135 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1136 ret = mp_request_async(eal_mp_socket_path(), copy, param, ts);
1138 /* if we didn't send anything, put dummy request on the queue */
1139 if (ret == 0 && reply->nb_sent == 0) {
1140 TAILQ_INSERT_TAIL(&pending_requests.requests, dummy,
1145 pthread_mutex_unlock(&pending_requests.lock);
1147 /* if we couldn't send anything, clean up */
1153 /* for primary process, broadcast request */
1154 mp_dir = opendir(mp_dir_path);
1156 RTE_LOG(ERR, EAL, "Unable to open directory %s\n", mp_dir_path);
1160 dir_fd = dirfd(mp_dir);
1162 /* lock the directory to prevent processes spinning up while we send */
1163 if (flock(dir_fd, LOCK_SH)) {
1164 RTE_LOG(ERR, EAL, "Unable to lock directory %s\n",
1170 while ((ent = readdir(mp_dir))) {
1171 char path[PATH_MAX];
1173 if (fnmatch(mp_filter, ent->d_name, 0) != 0)
1176 snprintf(path, sizeof(path), "%s/%s", mp_dir_path,
1179 if (mp_request_async(path, copy, param, ts))
1182 /* if we didn't send anything, put dummy request on the queue */
1183 if (ret == 0 && reply->nb_sent == 0) {
1184 TAILQ_INSERT_HEAD(&pending_requests.requests, dummy, next);
1188 /* finally, unlock the queue */
1189 pthread_mutex_unlock(&pending_requests.lock);
1191 /* unlock the directory */
1192 flock(dir_fd, LOCK_UN);
1194 /* dir_fd automatically closed on closedir */
1197 /* if dummy was unused, free it */
1205 pthread_mutex_unlock(&pending_requests.lock);
1214 rte_mp_reply(struct rte_mp_msg *msg, const char *peer)
1216 RTE_LOG(DEBUG, EAL, "reply: %s\n", msg->name);
1217 const struct internal_config *internal_conf =
1218 eal_get_internal_configuration();
1220 if (check_input(msg) != 0)
1224 RTE_LOG(ERR, EAL, "peer is not specified\n");
1229 if (internal_conf->no_shconf) {
1230 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is disabled\n");
1234 return mp_send(msg, peer, MP_REP);
1237 /* Internally, the status of the mp feature is represented as a three-state:
1238 * - "unknown" as long as no secondary process attached to a primary process
1239 * and there was no call to rte_mp_disable yet,
1240 * - "enabled" as soon as a secondary process attaches to a primary process,
1241 * - "disabled" when a primary process successfully called rte_mp_disable,
1250 set_mp_status(enum mp_status status)
1252 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1256 expected = MP_STATUS_UNKNOWN;
1258 if (__atomic_compare_exchange_n(&mcfg->mp_status, &expected, desired,
1259 false, __ATOMIC_RELAXED, __ATOMIC_RELAXED))
1262 return __atomic_load_n(&mcfg->mp_status, __ATOMIC_RELAXED) == desired;
1266 rte_mp_disable(void)
1268 return set_mp_status(MP_STATUS_DISABLED);
1272 __rte_mp_enable(void)
1274 return set_mp_status(MP_STATUS_ENABLED);