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