1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2018 Intel Corporation
18 #include <sys/socket.h>
22 #include <rte_alarm.h>
23 #include <rte_common.h>
24 #include <rte_cycles.h>
26 #include <rte_errno.h>
27 #include <rte_lcore.h>
30 #include "eal_memcfg.h"
31 #include "eal_private.h"
32 #include "eal_filesystem.h"
33 #include "eal_internal_cfg.h"
35 static int mp_fd = -1;
36 static pthread_t mp_handle_tid;
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) {
201 rte_mp_action_register(const char *name, rte_mp_t action)
203 struct action_entry *entry;
204 const struct internal_config *internal_conf =
205 eal_get_internal_configuration();
207 if (validate_action_name(name) != 0)
210 if (internal_conf->no_shconf) {
211 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is disabled\n");
216 entry = malloc(sizeof(struct action_entry));
221 strlcpy(entry->action_name, name, sizeof(entry->action_name));
222 entry->action = action;
224 pthread_mutex_lock(&mp_mutex_action);
225 if (find_action_entry_by_name(name) != NULL) {
226 pthread_mutex_unlock(&mp_mutex_action);
231 TAILQ_INSERT_TAIL(&action_entry_list, entry, next);
232 pthread_mutex_unlock(&mp_mutex_action);
237 rte_mp_action_unregister(const char *name)
239 struct action_entry *entry;
240 const struct internal_config *internal_conf =
241 eal_get_internal_configuration();
243 if (validate_action_name(name) != 0)
246 if (internal_conf->no_shconf) {
247 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is disabled\n");
251 pthread_mutex_lock(&mp_mutex_action);
252 entry = find_action_entry_by_name(name);
254 pthread_mutex_unlock(&mp_mutex_action);
257 TAILQ_REMOVE(&action_entry_list, entry, next);
258 pthread_mutex_unlock(&mp_mutex_action);
263 read_msg(struct mp_msg_internal *m, struct sockaddr_un *s)
268 char control[CMSG_SPACE(sizeof(m->msg.fds))];
269 struct cmsghdr *cmsg;
270 int buflen = sizeof(*m) - sizeof(m->msg.fds);
272 memset(&msgh, 0, sizeof(msgh));
274 iov.iov_len = buflen;
277 msgh.msg_namelen = sizeof(*s);
280 msgh.msg_control = control;
281 msgh.msg_controllen = sizeof(control);
284 msglen = recvmsg(mp_fd, &msgh, 0);
286 /* zero length message means socket was closed */
294 RTE_LOG(ERR, EAL, "recvmsg failed, %s\n", strerror(errno));
298 if (msglen != buflen || (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) {
299 RTE_LOG(ERR, EAL, "truncated msg\n");
303 /* read auxiliary FDs if any */
304 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
305 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
306 if ((cmsg->cmsg_level == SOL_SOCKET) &&
307 (cmsg->cmsg_type == SCM_RIGHTS)) {
308 memcpy(m->msg.fds, CMSG_DATA(cmsg), sizeof(m->msg.fds));
312 /* sanity-check the response */
313 if (m->msg.num_fds < 0 || m->msg.num_fds > RTE_MP_MAX_FD_NUM) {
314 RTE_LOG(ERR, EAL, "invalid number of fd's received\n");
317 if (m->msg.len_param < 0 || m->msg.len_param > RTE_MP_MAX_PARAM_LEN) {
318 RTE_LOG(ERR, EAL, "invalid received data length\n");
325 process_msg(struct mp_msg_internal *m, struct sockaddr_un *s)
327 struct pending_request *pending_req;
328 struct action_entry *entry;
329 struct rte_mp_msg *msg = &m->msg;
330 rte_mp_t action = NULL;
331 const struct internal_config *internal_conf =
332 eal_get_internal_configuration();
334 RTE_LOG(DEBUG, EAL, "msg: %s\n", msg->name);
336 if (m->type == MP_REP || m->type == MP_IGN) {
337 struct pending_request *req = NULL;
339 pthread_mutex_lock(&pending_requests.lock);
340 pending_req = find_pending_request(s->sun_path, msg->name);
342 memcpy(pending_req->reply, msg, sizeof(*msg));
343 /* -1 indicates that we've been asked to ignore */
344 pending_req->reply_received =
345 m->type == MP_REP ? 1 : -1;
347 if (pending_req->type == REQUEST_TYPE_SYNC)
348 pthread_cond_signal(&pending_req->sync.cond);
349 else if (pending_req->type == REQUEST_TYPE_ASYNC)
350 req = async_reply_handle_thread_unsafe(
353 RTE_LOG(ERR, EAL, "Drop mp reply: %s\n", msg->name);
354 pthread_mutex_unlock(&pending_requests.lock);
357 trigger_async_action(req);
361 pthread_mutex_lock(&mp_mutex_action);
362 entry = find_action_entry_by_name(msg->name);
364 action = entry->action;
365 pthread_mutex_unlock(&mp_mutex_action);
368 if (m->type == MP_REQ && !internal_conf->init_complete) {
369 /* if this is a request, and init is not yet complete,
370 * and callback wasn't registered, we should tell the
371 * requester to ignore our existence because we're not
372 * yet ready to process this request.
374 struct rte_mp_msg dummy;
376 memset(&dummy, 0, sizeof(dummy));
377 strlcpy(dummy.name, msg->name, sizeof(dummy.name));
378 mp_send(&dummy, s->sun_path, MP_IGN);
380 RTE_LOG(ERR, EAL, "Cannot find action: %s\n",
383 } else if (action(msg, s->sun_path) < 0) {
384 RTE_LOG(ERR, EAL, "Fail to handle message: %s\n", msg->name);
389 mp_handle(void *arg __rte_unused)
391 struct mp_msg_internal msg;
392 struct sockaddr_un sa;
397 ret = read_msg(&msg, &sa);
401 process_msg(&msg, &sa);
408 timespec_cmp(const struct timespec *a, const struct timespec *b)
410 if (a->tv_sec < b->tv_sec)
412 if (a->tv_sec > b->tv_sec)
414 if (a->tv_nsec < b->tv_nsec)
416 if (a->tv_nsec > b->tv_nsec)
422 ACTION_FREE, /**< free the action entry, but don't trigger callback */
423 ACTION_TRIGGER /**< trigger callback, then free action entry */
426 static enum async_action
427 process_async_request(struct pending_request *sr, const struct timespec *now)
429 struct async_request_param *param;
430 struct rte_mp_reply *reply;
431 bool timeout, last_msg;
433 param = sr->async.param;
434 reply = ¶m->user_reply;
436 /* did we timeout? */
437 timeout = timespec_cmp(¶m->end, now) <= 0;
439 /* if we received a response, adjust relevant data and copy message. */
440 if (sr->reply_received == 1 && sr->reply) {
441 struct rte_mp_msg *msg, *user_msgs, *tmp;
444 user_msgs = reply->msgs;
446 tmp = realloc(user_msgs, sizeof(*msg) *
447 (reply->nb_received + 1));
449 RTE_LOG(ERR, EAL, "Fail to alloc reply for request %s:%s\n",
450 sr->dst, sr->request->name);
451 /* this entry is going to be removed and its message
452 * dropped, but we don't want to leak memory, so
457 reply->msgs = user_msgs;
458 memcpy(&user_msgs[reply->nb_received],
460 reply->nb_received++;
463 /* mark this request as processed */
464 param->n_responses_processed++;
465 } else if (sr->reply_received == -1) {
466 /* we were asked to ignore this process */
468 } else if (timeout) {
469 /* count it as processed response, but don't increment
472 param->n_responses_processed++;
477 last_msg = param->n_responses_processed == reply->nb_sent;
479 return last_msg ? ACTION_TRIGGER : ACTION_FREE;
483 trigger_async_action(struct pending_request *sr)
485 struct async_request_param *param;
486 struct rte_mp_reply *reply;
488 param = sr->async.param;
489 reply = ¶m->user_reply;
491 param->clb(sr->request, reply);
494 free(sr->async.param->user_reply.msgs);
495 free(sr->async.param);
500 static struct pending_request *
501 async_reply_handle_thread_unsafe(void *arg)
503 struct pending_request *req = (struct pending_request *)arg;
504 enum async_action action;
505 struct timespec ts_now;
507 if (clock_gettime(CLOCK_MONOTONIC, &ts_now) < 0) {
508 RTE_LOG(ERR, EAL, "Cannot get current time\n");
512 action = process_async_request(req, &ts_now);
514 TAILQ_REMOVE(&pending_requests.requests, req, next);
516 if (rte_eal_alarm_cancel(async_reply_handle, req) < 0) {
517 /* if we failed to cancel the alarm because it's already in
518 * progress, don't proceed because otherwise we will end up
519 * handling the same message twice.
521 if (rte_errno == EINPROGRESS) {
522 RTE_LOG(DEBUG, EAL, "Request handling is already in progress\n");
525 RTE_LOG(ERR, EAL, "Failed to cancel alarm\n");
528 if (action == ACTION_TRIGGER)
536 async_reply_handle(void *arg)
538 struct pending_request *req;
540 pthread_mutex_lock(&pending_requests.lock);
541 req = async_reply_handle_thread_unsafe(arg);
542 pthread_mutex_unlock(&pending_requests.lock);
545 trigger_async_action(req);
551 struct sockaddr_un un;
554 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
555 snprintf(peer_name, sizeof(peer_name),
556 "%d_%"PRIx64, getpid(), rte_rdtsc());
558 mp_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
560 RTE_LOG(ERR, EAL, "failed to create unix socket\n");
564 memset(&un, 0, sizeof(un));
565 un.sun_family = AF_UNIX;
567 create_socket_path(peer_name, un.sun_path, sizeof(un.sun_path));
569 unlink(un.sun_path); /* May still exist since last run */
571 if (bind(mp_fd, (struct sockaddr *)&un, sizeof(un)) < 0) {
572 RTE_LOG(ERR, EAL, "failed to bind %s: %s\n",
573 un.sun_path, strerror(errno));
578 RTE_LOG(INFO, EAL, "Multi-process socket %s\n", un.sun_path);
583 close_socket_fd(int fd)
588 create_socket_path(peer_name, path, sizeof(path));
593 rte_mp_channel_init(void)
597 const struct internal_config *internal_conf =
598 eal_get_internal_configuration();
600 /* in no shared files mode, we do not have secondary processes support,
601 * so no need to initialize IPC.
603 if (internal_conf->no_shconf) {
604 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC will be disabled\n");
609 /* create filter path */
610 create_socket_path("*", path, sizeof(path));
611 strlcpy(mp_filter, basename(path), sizeof(mp_filter));
613 /* path may have been modified, so recreate it */
614 create_socket_path("*", path, sizeof(path));
615 strlcpy(mp_dir_path, dirname(path), sizeof(mp_dir_path));
617 /* lock the directory */
618 dir_fd = open(mp_dir_path, O_RDONLY);
620 RTE_LOG(ERR, EAL, "failed to open %s: %s\n",
621 mp_dir_path, strerror(errno));
625 if (flock(dir_fd, LOCK_EX)) {
626 RTE_LOG(ERR, EAL, "failed to lock %s: %s\n",
627 mp_dir_path, strerror(errno));
632 if (open_socket_fd() < 0) {
637 if (rte_ctrl_thread_create(&mp_handle_tid, "rte_mp_handle",
638 NULL, mp_handle, NULL) < 0) {
639 RTE_LOG(ERR, EAL, "failed to create mp thread: %s\n",
647 /* unlock the directory */
648 flock(dir_fd, LOCK_UN);
655 rte_mp_channel_cleanup(void)
664 pthread_cancel(mp_handle_tid);
665 pthread_join(mp_handle_tid, NULL);
670 * Return -1, as fail to send message and it's caused by the local side.
671 * Return 0, as fail to send message and it's caused by the remote side.
672 * Return 1, as succeed to send message.
676 send_msg(const char *dst_path, struct rte_mp_msg *msg, int type)
681 struct cmsghdr *cmsg;
682 struct sockaddr_un dst;
683 struct mp_msg_internal m;
684 int fd_size = msg->num_fds * sizeof(int);
685 char control[CMSG_SPACE(fd_size)];
688 memcpy(&m.msg, msg, sizeof(*msg));
690 memset(&dst, 0, sizeof(dst));
691 dst.sun_family = AF_UNIX;
692 strlcpy(dst.sun_path, dst_path, sizeof(dst.sun_path));
694 memset(&msgh, 0, sizeof(msgh));
695 memset(control, 0, sizeof(control));
698 iov.iov_len = sizeof(m) - sizeof(msg->fds);
700 msgh.msg_name = &dst;
701 msgh.msg_namelen = sizeof(dst);
704 msgh.msg_control = control;
705 msgh.msg_controllen = sizeof(control);
707 cmsg = CMSG_FIRSTHDR(&msgh);
708 cmsg->cmsg_len = CMSG_LEN(fd_size);
709 cmsg->cmsg_level = SOL_SOCKET;
710 cmsg->cmsg_type = SCM_RIGHTS;
711 memcpy(CMSG_DATA(cmsg), msg->fds, fd_size);
714 snd = sendmsg(mp_fd, &msgh, 0);
715 } while (snd < 0 && errno == EINTR);
719 /* Check if it caused by peer process exits */
720 if (errno == ECONNREFUSED &&
721 rte_eal_process_type() == RTE_PROC_PRIMARY) {
725 RTE_LOG(ERR, EAL, "failed to send to (%s) due to %s\n",
726 dst_path, strerror(errno));
734 mp_send(struct rte_mp_msg *msg, const char *peer, int type)
740 if (!peer && (rte_eal_process_type() == RTE_PROC_SECONDARY))
741 peer = eal_mp_socket_path();
744 if (send_msg(peer, msg, type) < 0)
750 /* broadcast to all secondary processes */
751 mp_dir = opendir(mp_dir_path);
753 RTE_LOG(ERR, EAL, "Unable to open directory %s\n",
759 dir_fd = dirfd(mp_dir);
760 /* lock the directory to prevent processes spinning up while we send */
761 if (flock(dir_fd, LOCK_SH)) {
762 RTE_LOG(ERR, EAL, "Unable to lock directory %s\n",
769 while ((ent = readdir(mp_dir))) {
772 if (fnmatch(mp_filter, ent->d_name, 0) != 0)
775 snprintf(path, sizeof(path), "%s/%s", mp_dir_path,
777 if (send_msg(path, msg, type) < 0)
781 flock(dir_fd, LOCK_UN);
783 /* dir_fd automatically closed on closedir */
789 check_input(const struct rte_mp_msg *msg)
792 RTE_LOG(ERR, EAL, "Msg cannot be NULL\n");
797 if (validate_action_name(msg->name) != 0)
800 if (msg->len_param < 0) {
801 RTE_LOG(ERR, EAL, "Message data length is negative\n");
806 if (msg->num_fds < 0) {
807 RTE_LOG(ERR, EAL, "Number of fd's is negative\n");
812 if (msg->len_param > RTE_MP_MAX_PARAM_LEN) {
813 RTE_LOG(ERR, EAL, "Message data is too long\n");
818 if (msg->num_fds > RTE_MP_MAX_FD_NUM) {
819 RTE_LOG(ERR, EAL, "Cannot send more than %d FDs\n",
829 rte_mp_sendmsg(struct rte_mp_msg *msg)
831 const struct internal_config *internal_conf =
832 eal_get_internal_configuration();
834 if (check_input(msg) != 0)
837 if (internal_conf->no_shconf) {
838 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is disabled\n");
843 RTE_LOG(DEBUG, EAL, "sendmsg: %s\n", msg->name);
844 return mp_send(msg, NULL, MP_MSG);
848 mp_request_async(const char *dst, struct rte_mp_msg *req,
849 struct async_request_param *param, const struct timespec *ts)
851 struct rte_mp_msg *reply_msg;
852 struct pending_request *pending_req, *exist;
855 pending_req = calloc(1, sizeof(*pending_req));
856 reply_msg = calloc(1, sizeof(*reply_msg));
857 if (pending_req == NULL || reply_msg == NULL) {
858 RTE_LOG(ERR, EAL, "Could not allocate space for sync request\n");
864 pending_req->type = REQUEST_TYPE_ASYNC;
865 strlcpy(pending_req->dst, dst, sizeof(pending_req->dst));
866 pending_req->request = req;
867 pending_req->reply = reply_msg;
868 pending_req->async.param = param;
870 /* queue already locked by caller */
872 exist = find_pending_request(dst, req->name);
874 RTE_LOG(ERR, EAL, "A pending request %s:%s\n", dst, req->name);
880 ret = send_msg(dst, req, MP_REQ);
882 RTE_LOG(ERR, EAL, "Fail to send request %s:%s\n",
886 } else if (ret == 0) {
890 param->user_reply.nb_sent++;
892 /* if alarm set fails, we simply ignore the reply */
893 if (rte_eal_alarm_set(ts->tv_sec * 1000000 + ts->tv_nsec / 1000,
894 async_reply_handle, pending_req) < 0) {
895 RTE_LOG(ERR, EAL, "Fail to set alarm for request %s:%s\n",
900 TAILQ_INSERT_TAIL(&pending_requests.requests, pending_req, next);
910 mp_request_sync(const char *dst, struct rte_mp_msg *req,
911 struct rte_mp_reply *reply, const struct timespec *ts)
914 pthread_condattr_t attr;
915 struct rte_mp_msg msg, *tmp;
916 struct pending_request pending_req, *exist;
918 pending_req.type = REQUEST_TYPE_SYNC;
919 pending_req.reply_received = 0;
920 strlcpy(pending_req.dst, dst, sizeof(pending_req.dst));
921 pending_req.request = req;
922 pending_req.reply = &msg;
923 pthread_condattr_init(&attr);
924 pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
925 pthread_cond_init(&pending_req.sync.cond, &attr);
927 exist = find_pending_request(dst, req->name);
929 RTE_LOG(ERR, EAL, "A pending request %s:%s\n", dst, req->name);
934 ret = send_msg(dst, req, MP_REQ);
936 RTE_LOG(ERR, EAL, "Fail to send request %s:%s\n",
942 TAILQ_INSERT_TAIL(&pending_requests.requests, &pending_req, next);
947 ret = pthread_cond_timedwait(&pending_req.sync.cond,
948 &pending_requests.lock, ts);
949 } while (ret != 0 && ret != ETIMEDOUT);
951 TAILQ_REMOVE(&pending_requests.requests, &pending_req, next);
953 if (pending_req.reply_received == 0) {
954 RTE_LOG(ERR, EAL, "Fail to recv reply for request %s:%s\n",
956 rte_errno = ETIMEDOUT;
959 if (pending_req.reply_received == -1) {
960 RTE_LOG(DEBUG, EAL, "Asked to ignore response\n");
961 /* not receiving this message is not an error, so decrement
962 * number of sent messages
968 tmp = realloc(reply->msgs, sizeof(msg) * (reply->nb_received + 1));
970 RTE_LOG(ERR, EAL, "Fail to alloc reply for request %s:%s\n",
975 memcpy(&tmp[reply->nb_received], &msg, sizeof(msg));
977 reply->nb_received++;
982 rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply,
983 const struct timespec *ts)
985 int dir_fd, ret = -1;
988 struct timespec now, end;
989 const struct internal_config *internal_conf =
990 eal_get_internal_configuration();
992 RTE_LOG(DEBUG, EAL, "request: %s\n", req->name);
995 reply->nb_received = 0;
998 if (check_input(req) != 0)
1001 if (internal_conf->no_shconf) {
1002 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is disabled\n");
1003 rte_errno = ENOTSUP;
1007 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0) {
1008 RTE_LOG(ERR, EAL, "Failed to get current time\n");
1013 end.tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
1014 end.tv_sec = now.tv_sec + ts->tv_sec +
1015 (now.tv_nsec + ts->tv_nsec) / 1000000000;
1017 /* for secondary process, send request to the primary process only */
1018 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1019 pthread_mutex_lock(&pending_requests.lock);
1020 ret = mp_request_sync(eal_mp_socket_path(), req, reply, &end);
1021 pthread_mutex_unlock(&pending_requests.lock);
1025 /* for primary process, broadcast request, and collect reply 1 by 1 */
1026 mp_dir = opendir(mp_dir_path);
1028 RTE_LOG(ERR, EAL, "Unable to open directory %s\n", mp_dir_path);
1033 dir_fd = dirfd(mp_dir);
1034 /* lock the directory to prevent processes spinning up while we send */
1035 if (flock(dir_fd, LOCK_SH)) {
1036 RTE_LOG(ERR, EAL, "Unable to lock directory %s\n",
1042 pthread_mutex_lock(&pending_requests.lock);
1043 while ((ent = readdir(mp_dir))) {
1044 char path[PATH_MAX];
1046 if (fnmatch(mp_filter, ent->d_name, 0) != 0)
1049 snprintf(path, sizeof(path), "%s/%s", mp_dir_path,
1052 /* unlocks the mutex while waiting for response,
1055 if (mp_request_sync(path, req, reply, &end))
1061 pthread_mutex_unlock(&pending_requests.lock);
1062 /* unlock the directory */
1063 flock(dir_fd, LOCK_UN);
1066 /* dir_fd automatically closed on closedir */
1072 reply->nb_received = 0;
1079 rte_mp_request_async(struct rte_mp_msg *req, const struct timespec *ts,
1080 rte_mp_async_reply_t clb)
1082 struct rte_mp_msg *copy;
1083 struct pending_request *dummy;
1084 struct async_request_param *param;
1085 struct rte_mp_reply *reply;
1086 int dir_fd, ret = 0;
1089 struct timespec now;
1090 struct timespec *end;
1091 bool dummy_used = false;
1092 const struct internal_config *internal_conf =
1093 eal_get_internal_configuration();
1095 RTE_LOG(DEBUG, EAL, "request: %s\n", req->name);
1097 if (check_input(req) != 0)
1100 if (internal_conf->no_shconf) {
1101 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is disabled\n");
1102 rte_errno = ENOTSUP;
1106 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0) {
1107 RTE_LOG(ERR, EAL, "Failed to get current time\n");
1111 copy = calloc(1, sizeof(*copy));
1112 dummy = calloc(1, sizeof(*dummy));
1113 param = calloc(1, sizeof(*param));
1114 if (copy == NULL || dummy == NULL || param == NULL) {
1115 RTE_LOG(ERR, EAL, "Failed to allocate memory for async reply\n");
1121 memcpy(copy, req, sizeof(*copy));
1123 param->n_responses_processed = 0;
1126 reply = ¶m->user_reply;
1128 end->tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
1129 end->tv_sec = now.tv_sec + ts->tv_sec +
1130 (now.tv_nsec + ts->tv_nsec) / 1000000000;
1132 reply->nb_received = 0;
1135 /* we have to lock the request queue here, as we will be adding a bunch
1136 * of requests to the queue at once, and some of the replies may arrive
1137 * before we add all of the requests to the queue.
1139 pthread_mutex_lock(&pending_requests.lock);
1141 /* we have to ensure that callback gets triggered even if we don't send
1142 * anything, therefore earlier we have allocated a dummy request. fill
1143 * it, and put it on the queue if we don't send any requests.
1145 dummy->type = REQUEST_TYPE_ASYNC;
1146 dummy->request = copy;
1147 dummy->reply = NULL;
1148 dummy->async.param = param;
1149 dummy->reply_received = 1; /* short-circuit the timeout */
1151 /* for secondary process, send request to the primary process only */
1152 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1153 ret = mp_request_async(eal_mp_socket_path(), copy, param, ts);
1155 /* if we didn't send anything, put dummy request on the queue */
1156 if (ret == 0 && reply->nb_sent == 0) {
1157 TAILQ_INSERT_TAIL(&pending_requests.requests, dummy,
1162 pthread_mutex_unlock(&pending_requests.lock);
1164 /* if we couldn't send anything, clean up */
1170 /* for primary process, broadcast request */
1171 mp_dir = opendir(mp_dir_path);
1173 RTE_LOG(ERR, EAL, "Unable to open directory %s\n", mp_dir_path);
1177 dir_fd = dirfd(mp_dir);
1179 /* lock the directory to prevent processes spinning up while we send */
1180 if (flock(dir_fd, LOCK_SH)) {
1181 RTE_LOG(ERR, EAL, "Unable to lock directory %s\n",
1187 while ((ent = readdir(mp_dir))) {
1188 char path[PATH_MAX];
1190 if (fnmatch(mp_filter, ent->d_name, 0) != 0)
1193 snprintf(path, sizeof(path), "%s/%s", mp_dir_path,
1196 if (mp_request_async(path, copy, param, ts))
1199 /* if we didn't send anything, put dummy request on the queue */
1200 if (ret == 0 && reply->nb_sent == 0) {
1201 TAILQ_INSERT_HEAD(&pending_requests.requests, dummy, next);
1205 /* finally, unlock the queue */
1206 pthread_mutex_unlock(&pending_requests.lock);
1208 /* unlock the directory */
1209 flock(dir_fd, LOCK_UN);
1211 /* dir_fd automatically closed on closedir */
1214 /* if dummy was unused, free it */
1222 pthread_mutex_unlock(&pending_requests.lock);
1231 rte_mp_reply(struct rte_mp_msg *msg, const char *peer)
1233 RTE_LOG(DEBUG, EAL, "reply: %s\n", msg->name);
1234 const struct internal_config *internal_conf =
1235 eal_get_internal_configuration();
1237 if (check_input(msg) != 0)
1241 RTE_LOG(ERR, EAL, "peer is not specified\n");
1246 if (internal_conf->no_shconf) {
1247 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is disabled\n");
1251 return mp_send(msg, peer, MP_REP);
1254 /* Internally, the status of the mp feature is represented as a three-state:
1255 * - "unknown" as long as no secondary process attached to a primary process
1256 * and there was no call to rte_mp_disable yet,
1257 * - "enabled" as soon as a secondary process attaches to a primary process,
1258 * - "disabled" when a primary process successfully called rte_mp_disable,
1267 set_mp_status(enum mp_status status)
1269 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1273 expected = MP_STATUS_UNKNOWN;
1275 if (__atomic_compare_exchange_n(&mcfg->mp_status, &expected, desired,
1276 false, __ATOMIC_RELAXED, __ATOMIC_RELAXED))
1279 return __atomic_load_n(&mcfg->mp_status, __ATOMIC_RELAXED) == desired;
1283 rte_mp_disable(void)
1285 return set_mp_status(MP_STATUS_DISABLED);
1289 __rte_mp_enable(void)
1291 return set_mp_status(MP_STATUS_ENABLED);