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