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