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