remove experimental tags from all symbol definitions
[dpdk.git] / lib / librte_eal / common / eal_common_proc.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016-2018 Intel Corporation
3  */
4
5 #include <dirent.h>
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <fnmatch.h>
9 #include <inttypes.h>
10 #include <libgen.h>
11 #include <limits.h>
12 #include <pthread.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/file.h>
17 #include <sys/time.h>
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <sys/un.h>
21 #include <unistd.h>
22
23 #include <rte_alarm.h>
24 #include <rte_common.h>
25 #include <rte_cycles.h>
26 #include <rte_eal.h>
27 #include <rte_errno.h>
28 #include <rte_lcore.h>
29 #include <rte_log.h>
30 #include <rte_tailq.h>
31
32 #include "eal_private.h"
33 #include "eal_filesystem.h"
34 #include "eal_internal_cfg.h"
35
36 static int mp_fd = -1;
37 static char mp_filter[PATH_MAX];   /* Filter for secondary process sockets */
38 static char mp_dir_path[PATH_MAX]; /* The directory path for all mp sockets */
39 static pthread_mutex_t mp_mutex_action = PTHREAD_MUTEX_INITIALIZER;
40 static char peer_name[PATH_MAX];
41
42 struct action_entry {
43         TAILQ_ENTRY(action_entry) next;
44         char action_name[RTE_MP_MAX_NAME_LEN];
45         rte_mp_t action;
46 };
47
48 /** Double linked list of actions. */
49 TAILQ_HEAD(action_entry_list, action_entry);
50
51 static struct action_entry_list action_entry_list =
52         TAILQ_HEAD_INITIALIZER(action_entry_list);
53
54 enum mp_type {
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 */
59 };
60
61 struct mp_msg_internal {
62         int type;
63         struct rte_mp_msg msg;
64 };
65
66 struct async_request_param {
67         rte_mp_async_reply_t clb;
68         struct rte_mp_reply user_reply;
69         struct timespec end;
70         int n_responses_processed;
71 };
72
73 struct pending_request {
74         TAILQ_ENTRY(pending_request) next;
75         enum {
76                 REQUEST_TYPE_SYNC,
77                 REQUEST_TYPE_ASYNC
78         } type;
79         char dst[PATH_MAX];
80         struct rte_mp_msg *request;
81         struct rte_mp_msg *reply;
82         int reply_received;
83         RTE_STD_C11
84         union {
85                 struct {
86                         struct async_request_param *param;
87                 } async;
88                 struct {
89                         pthread_cond_t cond;
90                 } sync;
91         };
92 };
93
94 TAILQ_HEAD(pending_request_list, pending_request);
95
96 static struct {
97         struct pending_request_list requests;
98         pthread_mutex_t lock;
99 } pending_requests = {
100         .requests = TAILQ_HEAD_INITIALIZER(pending_requests.requests),
101         .lock = PTHREAD_MUTEX_INITIALIZER,
102         /**< used in async requests only */
103 };
104
105 /* forward declarations */
106 static int
107 mp_send(struct rte_mp_msg *msg, const char *peer, int type);
108
109 /* for use with alarm callback */
110 static void
111 async_reply_handle(void *arg);
112
113 /* for use with process_msg */
114 static struct pending_request *
115 async_reply_handle_thread_unsafe(void *arg);
116
117 static void
118 trigger_async_action(struct pending_request *req);
119
120 static struct pending_request *
121 find_pending_request(const char *dst, const char *act_name)
122 {
123         struct pending_request *r;
124
125         TAILQ_FOREACH(r, &pending_requests.requests, next) {
126                 if (!strcmp(r->dst, dst) &&
127                     !strcmp(r->request->name, act_name))
128                         break;
129         }
130
131         return r;
132 }
133
134 static void
135 create_socket_path(const char *name, char *buf, int len)
136 {
137         const char *prefix = eal_mp_socket_path();
138
139         if (strlen(name) > 0)
140                 snprintf(buf, len, "%s_%s", prefix, name);
141         else
142                 strlcpy(buf, prefix, len);
143 }
144
145 int
146 rte_eal_primary_proc_alive(const char *config_file_path)
147 {
148         int config_fd;
149
150         if (config_file_path)
151                 config_fd = open(config_file_path, O_RDONLY);
152         else {
153                 const char *path;
154
155                 path = eal_runtime_config_path();
156                 config_fd = open(path, O_RDONLY);
157         }
158         if (config_fd < 0)
159                 return 0;
160
161         int ret = lockf(config_fd, F_TEST, 0);
162         close(config_fd);
163
164         return !!ret;
165 }
166
167 static struct action_entry *
168 find_action_entry_by_name(const char *name)
169 {
170         struct action_entry *entry;
171
172         TAILQ_FOREACH(entry, &action_entry_list, next) {
173                 if (strncmp(entry->action_name, name, RTE_MP_MAX_NAME_LEN) == 0)
174                         break;
175         }
176
177         return entry;
178 }
179
180 static int
181 validate_action_name(const char *name)
182 {
183         if (name == NULL) {
184                 RTE_LOG(ERR, EAL, "Action name cannot be NULL\n");
185                 rte_errno = EINVAL;
186                 return -1;
187         }
188         if (strnlen(name, RTE_MP_MAX_NAME_LEN) == 0) {
189                 RTE_LOG(ERR, EAL, "Length of action name is zero\n");
190                 rte_errno = EINVAL;
191                 return -1;
192         }
193         if (strnlen(name, RTE_MP_MAX_NAME_LEN) == RTE_MP_MAX_NAME_LEN) {
194                 rte_errno = E2BIG;
195                 return -1;
196         }
197         return 0;
198 }
199
200 int
201 rte_mp_action_register(const char *name, rte_mp_t action)
202 {
203         struct action_entry *entry;
204
205         if (validate_action_name(name) != 0)
206                 return -1;
207
208         if (internal_config.no_shconf) {
209                 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is disabled\n");
210                 rte_errno = ENOTSUP;
211                 return -1;
212         }
213
214         entry = malloc(sizeof(struct action_entry));
215         if (entry == NULL) {
216                 rte_errno = ENOMEM;
217                 return -1;
218         }
219         strlcpy(entry->action_name, name, sizeof(entry->action_name));
220         entry->action = action;
221
222         pthread_mutex_lock(&mp_mutex_action);
223         if (find_action_entry_by_name(name) != NULL) {
224                 pthread_mutex_unlock(&mp_mutex_action);
225                 rte_errno = EEXIST;
226                 free(entry);
227                 return -1;
228         }
229         TAILQ_INSERT_TAIL(&action_entry_list, entry, next);
230         pthread_mutex_unlock(&mp_mutex_action);
231         return 0;
232 }
233
234 void
235 rte_mp_action_unregister(const char *name)
236 {
237         struct action_entry *entry;
238
239         if (validate_action_name(name) != 0)
240                 return;
241
242         if (internal_config.no_shconf) {
243                 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is disabled\n");
244                 return;
245         }
246
247         pthread_mutex_lock(&mp_mutex_action);
248         entry = find_action_entry_by_name(name);
249         if (entry == NULL) {
250                 pthread_mutex_unlock(&mp_mutex_action);
251                 return;
252         }
253         TAILQ_REMOVE(&action_entry_list, entry, next);
254         pthread_mutex_unlock(&mp_mutex_action);
255         free(entry);
256 }
257
258 static int
259 read_msg(struct mp_msg_internal *m, struct sockaddr_un *s)
260 {
261         int msglen;
262         struct iovec iov;
263         struct msghdr msgh;
264         char control[CMSG_SPACE(sizeof(m->msg.fds))];
265         struct cmsghdr *cmsg;
266         int buflen = sizeof(*m) - sizeof(m->msg.fds);
267
268         memset(&msgh, 0, sizeof(msgh));
269         iov.iov_base = m;
270         iov.iov_len  = buflen;
271
272         msgh.msg_name = s;
273         msgh.msg_namelen = sizeof(*s);
274         msgh.msg_iov = &iov;
275         msgh.msg_iovlen = 1;
276         msgh.msg_control = control;
277         msgh.msg_controllen = sizeof(control);
278
279         msglen = recvmsg(mp_fd, &msgh, 0);
280         if (msglen < 0) {
281                 RTE_LOG(ERR, EAL, "recvmsg failed, %s\n", strerror(errno));
282                 return -1;
283         }
284
285         if (msglen != buflen || (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) {
286                 RTE_LOG(ERR, EAL, "truncted msg\n");
287                 return -1;
288         }
289
290         /* read auxiliary FDs if any */
291         for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
292                 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
293                 if ((cmsg->cmsg_level == SOL_SOCKET) &&
294                         (cmsg->cmsg_type == SCM_RIGHTS)) {
295                         memcpy(m->msg.fds, CMSG_DATA(cmsg), sizeof(m->msg.fds));
296                         break;
297                 }
298         }
299         /* sanity-check the response */
300         if (m->msg.num_fds < 0 || m->msg.num_fds > RTE_MP_MAX_FD_NUM) {
301                 RTE_LOG(ERR, EAL, "invalid number of fd's received\n");
302                 return -1;
303         }
304         if (m->msg.len_param < 0 || m->msg.len_param > RTE_MP_MAX_PARAM_LEN) {
305                 RTE_LOG(ERR, EAL, "invalid received data length\n");
306                 return -1;
307         }
308         return 0;
309 }
310
311 static void
312 process_msg(struct mp_msg_internal *m, struct sockaddr_un *s)
313 {
314         struct pending_request *pending_req;
315         struct action_entry *entry;
316         struct rte_mp_msg *msg = &m->msg;
317         rte_mp_t action = NULL;
318
319         RTE_LOG(DEBUG, EAL, "msg: %s\n", msg->name);
320
321         if (m->type == MP_REP || m->type == MP_IGN) {
322                 struct pending_request *req = NULL;
323
324                 pthread_mutex_lock(&pending_requests.lock);
325                 pending_req = find_pending_request(s->sun_path, msg->name);
326                 if (pending_req) {
327                         memcpy(pending_req->reply, msg, sizeof(*msg));
328                         /* -1 indicates that we've been asked to ignore */
329                         pending_req->reply_received =
330                                 m->type == MP_REP ? 1 : -1;
331
332                         if (pending_req->type == REQUEST_TYPE_SYNC)
333                                 pthread_cond_signal(&pending_req->sync.cond);
334                         else if (pending_req->type == REQUEST_TYPE_ASYNC)
335                                 req = async_reply_handle_thread_unsafe(
336                                                 pending_req);
337                 } else
338                         RTE_LOG(ERR, EAL, "Drop mp reply: %s\n", msg->name);
339                 pthread_mutex_unlock(&pending_requests.lock);
340
341                 if (req != NULL)
342                         trigger_async_action(req);
343                 return;
344         }
345
346         pthread_mutex_lock(&mp_mutex_action);
347         entry = find_action_entry_by_name(msg->name);
348         if (entry != NULL)
349                 action = entry->action;
350         pthread_mutex_unlock(&mp_mutex_action);
351
352         if (!action) {
353                 if (m->type == MP_REQ && !internal_config.init_complete) {
354                         /* if this is a request, and init is not yet complete,
355                          * and callback wasn't registered, we should tell the
356                          * requester to ignore our existence because we're not
357                          * yet ready to process this request.
358                          */
359                         struct rte_mp_msg dummy;
360
361                         memset(&dummy, 0, sizeof(dummy));
362                         strlcpy(dummy.name, msg->name, sizeof(dummy.name));
363                         mp_send(&dummy, s->sun_path, MP_IGN);
364                 } else {
365                         RTE_LOG(ERR, EAL, "Cannot find action: %s\n",
366                                 msg->name);
367                 }
368         } else if (action(msg, s->sun_path) < 0) {
369                 RTE_LOG(ERR, EAL, "Fail to handle message: %s\n", msg->name);
370         }
371 }
372
373 static void *
374 mp_handle(void *arg __rte_unused)
375 {
376         struct mp_msg_internal msg;
377         struct sockaddr_un sa;
378
379         while (1) {
380                 if (read_msg(&msg, &sa) == 0)
381                         process_msg(&msg, &sa);
382         }
383
384         return NULL;
385 }
386
387 static int
388 timespec_cmp(const struct timespec *a, const struct timespec *b)
389 {
390         if (a->tv_sec < b->tv_sec)
391                 return -1;
392         if (a->tv_sec > b->tv_sec)
393                 return 1;
394         if (a->tv_nsec < b->tv_nsec)
395                 return -1;
396         if (a->tv_nsec > b->tv_nsec)
397                 return 1;
398         return 0;
399 }
400
401 enum async_action {
402         ACTION_FREE, /**< free the action entry, but don't trigger callback */
403         ACTION_TRIGGER /**< trigger callback, then free action entry */
404 };
405
406 static enum async_action
407 process_async_request(struct pending_request *sr, const struct timespec *now)
408 {
409         struct async_request_param *param;
410         struct rte_mp_reply *reply;
411         bool timeout, last_msg;
412
413         param = sr->async.param;
414         reply = &param->user_reply;
415
416         /* did we timeout? */
417         timeout = timespec_cmp(&param->end, now) <= 0;
418
419         /* if we received a response, adjust relevant data and copy mesasge. */
420         if (sr->reply_received == 1 && sr->reply) {
421                 struct rte_mp_msg *msg, *user_msgs, *tmp;
422
423                 msg = sr->reply;
424                 user_msgs = reply->msgs;
425
426                 tmp = realloc(user_msgs, sizeof(*msg) *
427                                 (reply->nb_received + 1));
428                 if (!tmp) {
429                         RTE_LOG(ERR, EAL, "Fail to alloc reply for request %s:%s\n",
430                                 sr->dst, sr->request->name);
431                         /* this entry is going to be removed and its message
432                          * dropped, but we don't want to leak memory, so
433                          * continue.
434                          */
435                 } else {
436                         user_msgs = tmp;
437                         reply->msgs = user_msgs;
438                         memcpy(&user_msgs[reply->nb_received],
439                                         msg, sizeof(*msg));
440                         reply->nb_received++;
441                 }
442
443                 /* mark this request as processed */
444                 param->n_responses_processed++;
445         } else if (sr->reply_received == -1) {
446                 /* we were asked to ignore this process */
447                 reply->nb_sent--;
448         } else if (timeout) {
449                 /* count it as processed response, but don't increment
450                  * nb_received.
451                  */
452                 param->n_responses_processed++;
453         }
454
455         free(sr->reply);
456
457         last_msg = param->n_responses_processed == reply->nb_sent;
458
459         return last_msg ? ACTION_TRIGGER : ACTION_FREE;
460 }
461
462 static void
463 trigger_async_action(struct pending_request *sr)
464 {
465         struct async_request_param *param;
466         struct rte_mp_reply *reply;
467
468         param = sr->async.param;
469         reply = &param->user_reply;
470
471         param->clb(sr->request, reply);
472
473         /* clean up */
474         free(sr->async.param->user_reply.msgs);
475         free(sr->async.param);
476         free(sr->request);
477         free(sr);
478 }
479
480 static struct pending_request *
481 async_reply_handle_thread_unsafe(void *arg)
482 {
483         struct pending_request *req = (struct pending_request *)arg;
484         enum async_action action;
485         struct timespec ts_now;
486         struct timeval now;
487
488         if (gettimeofday(&now, NULL) < 0) {
489                 RTE_LOG(ERR, EAL, "Cannot get current time\n");
490                 goto no_trigger;
491         }
492         ts_now.tv_nsec = now.tv_usec * 1000;
493         ts_now.tv_sec = now.tv_sec;
494
495         action = process_async_request(req, &ts_now);
496
497         TAILQ_REMOVE(&pending_requests.requests, req, next);
498
499         if (rte_eal_alarm_cancel(async_reply_handle, req) < 0) {
500                 /* if we failed to cancel the alarm because it's already in
501                  * progress, don't proceed because otherwise we will end up
502                  * handling the same message twice.
503                  */
504                 if (rte_errno == EINPROGRESS) {
505                         RTE_LOG(DEBUG, EAL, "Request handling is already in progress\n");
506                         goto no_trigger;
507                 }
508                 RTE_LOG(ERR, EAL, "Failed to cancel alarm\n");
509         }
510
511         if (action == ACTION_TRIGGER)
512                 return req;
513 no_trigger:
514         free(req);
515         return NULL;
516 }
517
518 static void
519 async_reply_handle(void *arg)
520 {
521         struct pending_request *req;
522
523         pthread_mutex_lock(&pending_requests.lock);
524         req = async_reply_handle_thread_unsafe(arg);
525         pthread_mutex_unlock(&pending_requests.lock);
526
527         if (req != NULL)
528                 trigger_async_action(req);
529 }
530
531 static int
532 open_socket_fd(void)
533 {
534         struct sockaddr_un un;
535
536         peer_name[0] = '\0';
537         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
538                 snprintf(peer_name, sizeof(peer_name),
539                                 "%d_%"PRIx64, getpid(), rte_rdtsc());
540
541         mp_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
542         if (mp_fd < 0) {
543                 RTE_LOG(ERR, EAL, "failed to create unix socket\n");
544                 return -1;
545         }
546
547         memset(&un, 0, sizeof(un));
548         un.sun_family = AF_UNIX;
549
550         create_socket_path(peer_name, un.sun_path, sizeof(un.sun_path));
551
552         unlink(un.sun_path); /* May still exist since last run */
553
554         if (bind(mp_fd, (struct sockaddr *)&un, sizeof(un)) < 0) {
555                 RTE_LOG(ERR, EAL, "failed to bind %s: %s\n",
556                         un.sun_path, strerror(errno));
557                 close(mp_fd);
558                 return -1;
559         }
560
561         RTE_LOG(INFO, EAL, "Multi-process socket %s\n", un.sun_path);
562         return mp_fd;
563 }
564
565 static void
566 close_socket_fd(void)
567 {
568         char path[PATH_MAX];
569
570         if (mp_fd < 0)
571                 return;
572
573         close(mp_fd);
574         create_socket_path(peer_name, path, sizeof(path));
575         unlink(path);
576 }
577
578 int
579 rte_mp_channel_init(void)
580 {
581         char path[PATH_MAX];
582         int dir_fd;
583         pthread_t mp_handle_tid;
584
585         /* in no shared files mode, we do not have secondary processes support,
586          * so no need to initialize IPC.
587          */
588         if (internal_config.no_shconf) {
589                 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC will be disabled\n");
590                 rte_errno = ENOTSUP;
591                 return -1;
592         }
593
594         /* create filter path */
595         create_socket_path("*", path, sizeof(path));
596         strlcpy(mp_filter, basename(path), sizeof(mp_filter));
597
598         /* path may have been modified, so recreate it */
599         create_socket_path("*", path, sizeof(path));
600         strlcpy(mp_dir_path, dirname(path), sizeof(mp_dir_path));
601
602         /* lock the directory */
603         dir_fd = open(mp_dir_path, O_RDONLY);
604         if (dir_fd < 0) {
605                 RTE_LOG(ERR, EAL, "failed to open %s: %s\n",
606                         mp_dir_path, strerror(errno));
607                 return -1;
608         }
609
610         if (flock(dir_fd, LOCK_EX)) {
611                 RTE_LOG(ERR, EAL, "failed to lock %s: %s\n",
612                         mp_dir_path, strerror(errno));
613                 close(dir_fd);
614                 return -1;
615         }
616
617         if (open_socket_fd() < 0) {
618                 close(dir_fd);
619                 return -1;
620         }
621
622         if (rte_ctrl_thread_create(&mp_handle_tid, "rte_mp_handle",
623                         NULL, mp_handle, NULL) < 0) {
624                 RTE_LOG(ERR, EAL, "failed to create mp thead: %s\n",
625                         strerror(errno));
626                 close(mp_fd);
627                 close(dir_fd);
628                 mp_fd = -1;
629                 return -1;
630         }
631
632         /* unlock the directory */
633         flock(dir_fd, LOCK_UN);
634         close(dir_fd);
635
636         return 0;
637 }
638
639 void
640 rte_mp_channel_cleanup(void)
641 {
642         close_socket_fd();
643 }
644
645 /**
646  * Return -1, as fail to send message and it's caused by the local side.
647  * Return 0, as fail to send message and it's caused by the remote side.
648  * Return 1, as succeed to send message.
649  *
650  */
651 static int
652 send_msg(const char *dst_path, struct rte_mp_msg *msg, int type)
653 {
654         int snd;
655         struct iovec iov;
656         struct msghdr msgh;
657         struct cmsghdr *cmsg;
658         struct sockaddr_un dst;
659         struct mp_msg_internal m;
660         int fd_size = msg->num_fds * sizeof(int);
661         char control[CMSG_SPACE(fd_size)];
662
663         m.type = type;
664         memcpy(&m.msg, msg, sizeof(*msg));
665
666         memset(&dst, 0, sizeof(dst));
667         dst.sun_family = AF_UNIX;
668         strlcpy(dst.sun_path, dst_path, sizeof(dst.sun_path));
669
670         memset(&msgh, 0, sizeof(msgh));
671         memset(control, 0, sizeof(control));
672
673         iov.iov_base = &m;
674         iov.iov_len = sizeof(m) - sizeof(msg->fds);
675
676         msgh.msg_name = &dst;
677         msgh.msg_namelen = sizeof(dst);
678         msgh.msg_iov = &iov;
679         msgh.msg_iovlen = 1;
680         msgh.msg_control = control;
681         msgh.msg_controllen = sizeof(control);
682
683         cmsg = CMSG_FIRSTHDR(&msgh);
684         cmsg->cmsg_len = CMSG_LEN(fd_size);
685         cmsg->cmsg_level = SOL_SOCKET;
686         cmsg->cmsg_type = SCM_RIGHTS;
687         memcpy(CMSG_DATA(cmsg), msg->fds, fd_size);
688
689         do {
690                 snd = sendmsg(mp_fd, &msgh, 0);
691         } while (snd < 0 && errno == EINTR);
692
693         if (snd < 0) {
694                 rte_errno = errno;
695                 /* Check if it caused by peer process exits */
696                 if (errno == ECONNREFUSED &&
697                                 rte_eal_process_type() == RTE_PROC_PRIMARY) {
698                         unlink(dst_path);
699                         return 0;
700                 }
701                 RTE_LOG(ERR, EAL, "failed to send to (%s) due to %s\n",
702                         dst_path, strerror(errno));
703                 return -1;
704         }
705
706         return 1;
707 }
708
709 static int
710 mp_send(struct rte_mp_msg *msg, const char *peer, int type)
711 {
712         int dir_fd, ret = 0;
713         DIR *mp_dir;
714         struct dirent *ent;
715
716         if (!peer && (rte_eal_process_type() == RTE_PROC_SECONDARY))
717                 peer = eal_mp_socket_path();
718
719         if (peer) {
720                 if (send_msg(peer, msg, type) < 0)
721                         return -1;
722                 else
723                         return 0;
724         }
725
726         /* broadcast to all secondary processes */
727         mp_dir = opendir(mp_dir_path);
728         if (!mp_dir) {
729                 RTE_LOG(ERR, EAL, "Unable to open directory %s\n",
730                                 mp_dir_path);
731                 rte_errno = errno;
732                 return -1;
733         }
734
735         dir_fd = dirfd(mp_dir);
736         /* lock the directory to prevent processes spinning up while we send */
737         if (flock(dir_fd, LOCK_SH)) {
738                 RTE_LOG(ERR, EAL, "Unable to lock directory %s\n",
739                         mp_dir_path);
740                 rte_errno = errno;
741                 closedir(mp_dir);
742                 return -1;
743         }
744
745         while ((ent = readdir(mp_dir))) {
746                 char path[PATH_MAX];
747
748                 if (fnmatch(mp_filter, ent->d_name, 0) != 0)
749                         continue;
750
751                 snprintf(path, sizeof(path), "%s/%s", mp_dir_path,
752                          ent->d_name);
753                 if (send_msg(path, msg, type) < 0)
754                         ret = -1;
755         }
756         /* unlock the dir */
757         flock(dir_fd, LOCK_UN);
758
759         /* dir_fd automatically closed on closedir */
760         closedir(mp_dir);
761         return ret;
762 }
763
764 static int
765 check_input(const struct rte_mp_msg *msg)
766 {
767         if (msg == NULL) {
768                 RTE_LOG(ERR, EAL, "Msg cannot be NULL\n");
769                 rte_errno = EINVAL;
770                 return -1;
771         }
772
773         if (validate_action_name(msg->name) != 0)
774                 return -1;
775
776         if (msg->len_param < 0) {
777                 RTE_LOG(ERR, EAL, "Message data length is negative\n");
778                 rte_errno = EINVAL;
779                 return -1;
780         }
781
782         if (msg->num_fds < 0) {
783                 RTE_LOG(ERR, EAL, "Number of fd's is negative\n");
784                 rte_errno = EINVAL;
785                 return -1;
786         }
787
788         if (msg->len_param > RTE_MP_MAX_PARAM_LEN) {
789                 RTE_LOG(ERR, EAL, "Message data is too long\n");
790                 rte_errno = E2BIG;
791                 return -1;
792         }
793
794         if (msg->num_fds > RTE_MP_MAX_FD_NUM) {
795                 RTE_LOG(ERR, EAL, "Cannot send more than %d FDs\n",
796                         RTE_MP_MAX_FD_NUM);
797                 rte_errno = E2BIG;
798                 return -1;
799         }
800
801         return 0;
802 }
803
804 int
805 rte_mp_sendmsg(struct rte_mp_msg *msg)
806 {
807         if (check_input(msg) != 0)
808                 return -1;
809
810         if (internal_config.no_shconf) {
811                 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is disabled\n");
812                 rte_errno = ENOTSUP;
813                 return -1;
814         }
815
816         RTE_LOG(DEBUG, EAL, "sendmsg: %s\n", msg->name);
817         return mp_send(msg, NULL, MP_MSG);
818 }
819
820 static int
821 mp_request_async(const char *dst, struct rte_mp_msg *req,
822                 struct async_request_param *param, const struct timespec *ts)
823 {
824         struct rte_mp_msg *reply_msg;
825         struct pending_request *pending_req, *exist;
826         int ret = -1;
827
828         pending_req = calloc(1, sizeof(*pending_req));
829         reply_msg = calloc(1, sizeof(*reply_msg));
830         if (pending_req == NULL || reply_msg == NULL) {
831                 RTE_LOG(ERR, EAL, "Could not allocate space for sync request\n");
832                 rte_errno = ENOMEM;
833                 ret = -1;
834                 goto fail;
835         }
836
837         pending_req->type = REQUEST_TYPE_ASYNC;
838         strlcpy(pending_req->dst, dst, sizeof(pending_req->dst));
839         pending_req->request = req;
840         pending_req->reply = reply_msg;
841         pending_req->async.param = param;
842
843         /* queue already locked by caller */
844
845         exist = find_pending_request(dst, req->name);
846         if (exist) {
847                 RTE_LOG(ERR, EAL, "A pending request %s:%s\n", dst, req->name);
848                 rte_errno = EEXIST;
849                 ret = -1;
850                 goto fail;
851         }
852
853         ret = send_msg(dst, req, MP_REQ);
854         if (ret < 0) {
855                 RTE_LOG(ERR, EAL, "Fail to send request %s:%s\n",
856                         dst, req->name);
857                 ret = -1;
858                 goto fail;
859         } else if (ret == 0) {
860                 ret = 0;
861                 goto fail;
862         }
863         param->user_reply.nb_sent++;
864
865         /* if alarm set fails, we simply ignore the reply */
866         if (rte_eal_alarm_set(ts->tv_sec * 1000000 + ts->tv_nsec / 1000,
867                               async_reply_handle, pending_req) < 0) {
868                 RTE_LOG(ERR, EAL, "Fail to set alarm for request %s:%s\n",
869                         dst, req->name);
870                 ret = -1;
871                 goto fail;
872         }
873         TAILQ_INSERT_TAIL(&pending_requests.requests, pending_req, next);
874
875         return 0;
876 fail:
877         free(pending_req);
878         free(reply_msg);
879         return ret;
880 }
881
882 static int
883 mp_request_sync(const char *dst, struct rte_mp_msg *req,
884                struct rte_mp_reply *reply, const struct timespec *ts)
885 {
886         int ret;
887         struct rte_mp_msg msg, *tmp;
888         struct pending_request pending_req, *exist;
889
890         pending_req.type = REQUEST_TYPE_SYNC;
891         pending_req.reply_received = 0;
892         strlcpy(pending_req.dst, dst, sizeof(pending_req.dst));
893         pending_req.request = req;
894         pending_req.reply = &msg;
895         pthread_cond_init(&pending_req.sync.cond, NULL);
896
897         exist = find_pending_request(dst, req->name);
898         if (exist) {
899                 RTE_LOG(ERR, EAL, "A pending request %s:%s\n", dst, req->name);
900                 rte_errno = EEXIST;
901                 return -1;
902         }
903
904         ret = send_msg(dst, req, MP_REQ);
905         if (ret < 0) {
906                 RTE_LOG(ERR, EAL, "Fail to send request %s:%s\n",
907                         dst, req->name);
908                 return -1;
909         } else if (ret == 0)
910                 return 0;
911
912         TAILQ_INSERT_TAIL(&pending_requests.requests, &pending_req, next);
913
914         reply->nb_sent++;
915
916         do {
917                 ret = pthread_cond_timedwait(&pending_req.sync.cond,
918                                 &pending_requests.lock, ts);
919         } while (ret != 0 && ret != ETIMEDOUT);
920
921         TAILQ_REMOVE(&pending_requests.requests, &pending_req, next);
922
923         if (pending_req.reply_received == 0) {
924                 RTE_LOG(ERR, EAL, "Fail to recv reply for request %s:%s\n",
925                         dst, req->name);
926                 rte_errno = ETIMEDOUT;
927                 return -1;
928         }
929         if (pending_req.reply_received == -1) {
930                 RTE_LOG(DEBUG, EAL, "Asked to ignore response\n");
931                 /* not receiving this message is not an error, so decrement
932                  * number of sent messages
933                  */
934                 reply->nb_sent--;
935                 return 0;
936         }
937
938         tmp = realloc(reply->msgs, sizeof(msg) * (reply->nb_received + 1));
939         if (!tmp) {
940                 RTE_LOG(ERR, EAL, "Fail to alloc reply for request %s:%s\n",
941                         dst, req->name);
942                 rte_errno = ENOMEM;
943                 return -1;
944         }
945         memcpy(&tmp[reply->nb_received], &msg, sizeof(msg));
946         reply->msgs = tmp;
947         reply->nb_received++;
948         return 0;
949 }
950
951 int
952 rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply,
953                 const struct timespec *ts)
954 {
955         int dir_fd, ret = -1;
956         DIR *mp_dir;
957         struct dirent *ent;
958         struct timeval now;
959         struct timespec end;
960
961         RTE_LOG(DEBUG, EAL, "request: %s\n", req->name);
962
963         reply->nb_sent = 0;
964         reply->nb_received = 0;
965         reply->msgs = NULL;
966
967         if (check_input(req) != 0)
968                 goto end;
969
970         if (internal_config.no_shconf) {
971                 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is disabled\n");
972                 rte_errno = ENOTSUP;
973                 return -1;
974         }
975
976         if (gettimeofday(&now, NULL) < 0) {
977                 RTE_LOG(ERR, EAL, "Failed to get current time\n");
978                 rte_errno = errno;
979                 goto end;
980         }
981
982         end.tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
983         end.tv_sec = now.tv_sec + ts->tv_sec +
984                         (now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
985
986         /* for secondary process, send request to the primary process only */
987         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
988                 pthread_mutex_lock(&pending_requests.lock);
989                 ret = mp_request_sync(eal_mp_socket_path(), req, reply, &end);
990                 pthread_mutex_unlock(&pending_requests.lock);
991                 goto end;
992         }
993
994         /* for primary process, broadcast request, and collect reply 1 by 1 */
995         mp_dir = opendir(mp_dir_path);
996         if (!mp_dir) {
997                 RTE_LOG(ERR, EAL, "Unable to open directory %s\n", mp_dir_path);
998                 rte_errno = errno;
999                 goto end;
1000         }
1001
1002         dir_fd = dirfd(mp_dir);
1003         /* lock the directory to prevent processes spinning up while we send */
1004         if (flock(dir_fd, LOCK_SH)) {
1005                 RTE_LOG(ERR, EAL, "Unable to lock directory %s\n",
1006                         mp_dir_path);
1007                 rte_errno = errno;
1008                 goto close_end;
1009         }
1010
1011         pthread_mutex_lock(&pending_requests.lock);
1012         while ((ent = readdir(mp_dir))) {
1013                 char path[PATH_MAX];
1014
1015                 if (fnmatch(mp_filter, ent->d_name, 0) != 0)
1016                         continue;
1017
1018                 snprintf(path, sizeof(path), "%s/%s", mp_dir_path,
1019                          ent->d_name);
1020
1021                 /* unlocks the mutex while waiting for response,
1022                  * locks on receive
1023                  */
1024                 if (mp_request_sync(path, req, reply, &end))
1025                         goto unlock_end;
1026         }
1027         ret = 0;
1028
1029 unlock_end:
1030         pthread_mutex_unlock(&pending_requests.lock);
1031         /* unlock the directory */
1032         flock(dir_fd, LOCK_UN);
1033
1034 close_end:
1035         /* dir_fd automatically closed on closedir */
1036         closedir(mp_dir);
1037
1038 end:
1039         if (ret) {
1040                 free(reply->msgs);
1041                 reply->nb_received = 0;
1042                 reply->msgs = NULL;
1043         }
1044         return ret;
1045 }
1046
1047 int
1048 rte_mp_request_async(struct rte_mp_msg *req, const struct timespec *ts,
1049                 rte_mp_async_reply_t clb)
1050 {
1051         struct rte_mp_msg *copy;
1052         struct pending_request *dummy;
1053         struct async_request_param *param;
1054         struct rte_mp_reply *reply;
1055         int dir_fd, ret = 0;
1056         DIR *mp_dir;
1057         struct dirent *ent;
1058         struct timeval now;
1059         struct timespec *end;
1060         bool dummy_used = false;
1061
1062         RTE_LOG(DEBUG, EAL, "request: %s\n", req->name);
1063
1064         if (check_input(req) != 0)
1065                 return -1;
1066
1067         if (internal_config.no_shconf) {
1068                 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is disabled\n");
1069                 rte_errno = ENOTSUP;
1070                 return -1;
1071         }
1072
1073         if (gettimeofday(&now, NULL) < 0) {
1074                 RTE_LOG(ERR, EAL, "Faile to get current time\n");
1075                 rte_errno = errno;
1076                 return -1;
1077         }
1078         copy = calloc(1, sizeof(*copy));
1079         dummy = calloc(1, sizeof(*dummy));
1080         param = calloc(1, sizeof(*param));
1081         if (copy == NULL || dummy == NULL || param == NULL) {
1082                 RTE_LOG(ERR, EAL, "Failed to allocate memory for async reply\n");
1083                 rte_errno = ENOMEM;
1084                 goto fail;
1085         }
1086
1087         /* copy message */
1088         memcpy(copy, req, sizeof(*copy));
1089
1090         param->n_responses_processed = 0;
1091         param->clb = clb;
1092         end = &param->end;
1093         reply = &param->user_reply;
1094
1095         end->tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
1096         end->tv_sec = now.tv_sec + ts->tv_sec +
1097                         (now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
1098         reply->nb_sent = 0;
1099         reply->nb_received = 0;
1100         reply->msgs = NULL;
1101
1102         /* we have to lock the request queue here, as we will be adding a bunch
1103          * of requests to the queue at once, and some of the replies may arrive
1104          * before we add all of the requests to the queue.
1105          */
1106         pthread_mutex_lock(&pending_requests.lock);
1107
1108         /* we have to ensure that callback gets triggered even if we don't send
1109          * anything, therefore earlier we have allocated a dummy request. fill
1110          * it, and put it on the queue if we don't send any requests.
1111          */
1112         dummy->type = REQUEST_TYPE_ASYNC;
1113         dummy->request = copy;
1114         dummy->reply = NULL;
1115         dummy->async.param = param;
1116         dummy->reply_received = 1; /* short-circuit the timeout */
1117
1118         /* for secondary process, send request to the primary process only */
1119         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1120                 ret = mp_request_async(eal_mp_socket_path(), copy, param, ts);
1121
1122                 /* if we didn't send anything, put dummy request on the queue */
1123                 if (ret == 0 && reply->nb_sent == 0) {
1124                         TAILQ_INSERT_TAIL(&pending_requests.requests, dummy,
1125                                         next);
1126                         dummy_used = true;
1127                 }
1128
1129                 pthread_mutex_unlock(&pending_requests.lock);
1130
1131                 /* if we couldn't send anything, clean up */
1132                 if (ret != 0)
1133                         goto fail;
1134                 return 0;
1135         }
1136
1137         /* for primary process, broadcast request */
1138         mp_dir = opendir(mp_dir_path);
1139         if (!mp_dir) {
1140                 RTE_LOG(ERR, EAL, "Unable to open directory %s\n", mp_dir_path);
1141                 rte_errno = errno;
1142                 goto unlock_fail;
1143         }
1144         dir_fd = dirfd(mp_dir);
1145
1146         /* lock the directory to prevent processes spinning up while we send */
1147         if (flock(dir_fd, LOCK_SH)) {
1148                 RTE_LOG(ERR, EAL, "Unable to lock directory %s\n",
1149                         mp_dir_path);
1150                 rte_errno = errno;
1151                 goto closedir_fail;
1152         }
1153
1154         while ((ent = readdir(mp_dir))) {
1155                 char path[PATH_MAX];
1156
1157                 if (fnmatch(mp_filter, ent->d_name, 0) != 0)
1158                         continue;
1159
1160                 snprintf(path, sizeof(path), "%s/%s", mp_dir_path,
1161                          ent->d_name);
1162
1163                 if (mp_request_async(path, copy, param, ts))
1164                         ret = -1;
1165         }
1166         /* if we didn't send anything, put dummy request on the queue */
1167         if (ret == 0 && reply->nb_sent == 0) {
1168                 TAILQ_INSERT_HEAD(&pending_requests.requests, dummy, next);
1169                 dummy_used = true;
1170         }
1171
1172         /* finally, unlock the queue */
1173         pthread_mutex_unlock(&pending_requests.lock);
1174
1175         /* unlock the directory */
1176         flock(dir_fd, LOCK_UN);
1177
1178         /* dir_fd automatically closed on closedir */
1179         closedir(mp_dir);
1180
1181         /* if dummy was unused, free it */
1182         if (!dummy_used)
1183                 free(dummy);
1184
1185         return ret;
1186 closedir_fail:
1187         closedir(mp_dir);
1188 unlock_fail:
1189         pthread_mutex_unlock(&pending_requests.lock);
1190 fail:
1191         free(dummy);
1192         free(param);
1193         free(copy);
1194         return -1;
1195 }
1196
1197 int
1198 rte_mp_reply(struct rte_mp_msg *msg, const char *peer)
1199 {
1200         RTE_LOG(DEBUG, EAL, "reply: %s\n", msg->name);
1201
1202         if (check_input(msg) != 0)
1203                 return -1;
1204
1205         if (peer == NULL) {
1206                 RTE_LOG(ERR, EAL, "peer is not specified\n");
1207                 rte_errno = EINVAL;
1208                 return -1;
1209         }
1210
1211         if (internal_config.no_shconf) {
1212                 RTE_LOG(DEBUG, EAL, "No shared files mode enabled, IPC is disabled\n");
1213                 return 0;
1214         }
1215
1216         return mp_send(msg, peer, MP_REP);
1217 }