New upstream version 18.02
[deb_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/time.h>
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <sys/un.h>
20 #include <unistd.h>
21
22 #include <rte_common.h>
23 #include <rte_cycles.h>
24 #include <rte_eal.h>
25 #include <rte_errno.h>
26 #include <rte_lcore.h>
27 #include <rte_log.h>
28
29 #include "eal_private.h"
30 #include "eal_filesystem.h"
31 #include "eal_internal_cfg.h"
32
33 static int mp_fd = -1;
34 static char mp_filter[PATH_MAX];   /* Filter for secondary process sockets */
35 static char mp_dir_path[PATH_MAX]; /* The directory path for all mp sockets */
36 static pthread_mutex_t mp_mutex_action = PTHREAD_MUTEX_INITIALIZER;
37
38 struct action_entry {
39         TAILQ_ENTRY(action_entry) next;
40         char action_name[RTE_MP_MAX_NAME_LEN];
41         rte_mp_t action;
42 };
43
44 /** Double linked list of actions. */
45 TAILQ_HEAD(action_entry_list, action_entry);
46
47 static struct action_entry_list action_entry_list =
48         TAILQ_HEAD_INITIALIZER(action_entry_list);
49
50 enum mp_type {
51         MP_MSG, /* Share message with peers, will not block */
52         MP_REQ, /* Request for information, Will block for a reply */
53         MP_REP, /* Response to previously-received request */
54 };
55
56 struct mp_msg_internal {
57         int type;
58         struct rte_mp_msg msg;
59 };
60
61 struct sync_request {
62         TAILQ_ENTRY(sync_request) next;
63         int reply_received;
64         char dst[PATH_MAX];
65         struct rte_mp_msg *request;
66         struct rte_mp_msg *reply;
67         pthread_cond_t cond;
68 };
69
70 TAILQ_HEAD(sync_request_list, sync_request);
71
72 static struct {
73         struct sync_request_list requests;
74         pthread_mutex_t lock;
75 } sync_requests = {
76         .requests = TAILQ_HEAD_INITIALIZER(sync_requests.requests),
77         .lock = PTHREAD_MUTEX_INITIALIZER
78 };
79
80 static struct sync_request *
81 find_sync_request(const char *dst, const char *act_name)
82 {
83         struct sync_request *r;
84
85         TAILQ_FOREACH(r, &sync_requests.requests, next) {
86                 if (!strcmp(r->dst, dst) &&
87                     !strcmp(r->request->name, act_name))
88                         break;
89         }
90
91         return r;
92 }
93
94 int
95 rte_eal_primary_proc_alive(const char *config_file_path)
96 {
97         int config_fd;
98
99         if (config_file_path)
100                 config_fd = open(config_file_path, O_RDONLY);
101         else {
102                 const char *path;
103
104                 path = eal_runtime_config_path();
105                 config_fd = open(path, O_RDONLY);
106         }
107         if (config_fd < 0)
108                 return 0;
109
110         int ret = lockf(config_fd, F_TEST, 0);
111         close(config_fd);
112
113         return !!ret;
114 }
115
116 static struct action_entry *
117 find_action_entry_by_name(const char *name)
118 {
119         struct action_entry *entry;
120
121         TAILQ_FOREACH(entry, &action_entry_list, next) {
122                 if (strncmp(entry->action_name, name, RTE_MP_MAX_NAME_LEN) == 0)
123                         break;
124         }
125
126         return entry;
127 }
128
129 static int
130 validate_action_name(const char *name)
131 {
132         if (name == NULL) {
133                 RTE_LOG(ERR, EAL, "Action name cannot be NULL\n");
134                 rte_errno = EINVAL;
135                 return -1;
136         }
137         if (strnlen(name, RTE_MP_MAX_NAME_LEN) == 0) {
138                 RTE_LOG(ERR, EAL, "Length of action name is zero\n");
139                 rte_errno = EINVAL;
140                 return -1;
141         }
142         if (strnlen(name, RTE_MP_MAX_NAME_LEN) == RTE_MP_MAX_NAME_LEN) {
143                 rte_errno = E2BIG;
144                 return -1;
145         }
146         return 0;
147 }
148
149 int __rte_experimental
150 rte_mp_action_register(const char *name, rte_mp_t action)
151 {
152         struct action_entry *entry;
153
154         if (validate_action_name(name))
155                 return -1;
156
157         entry = malloc(sizeof(struct action_entry));
158         if (entry == NULL) {
159                 rte_errno = ENOMEM;
160                 return -1;
161         }
162         strcpy(entry->action_name, name);
163         entry->action = action;
164
165         pthread_mutex_lock(&mp_mutex_action);
166         if (find_action_entry_by_name(name) != NULL) {
167                 pthread_mutex_unlock(&mp_mutex_action);
168                 rte_errno = EEXIST;
169                 free(entry);
170                 return -1;
171         }
172         TAILQ_INSERT_TAIL(&action_entry_list, entry, next);
173         pthread_mutex_unlock(&mp_mutex_action);
174         return 0;
175 }
176
177 void __rte_experimental
178 rte_mp_action_unregister(const char *name)
179 {
180         struct action_entry *entry;
181
182         if (validate_action_name(name))
183                 return;
184
185         pthread_mutex_lock(&mp_mutex_action);
186         entry = find_action_entry_by_name(name);
187         if (entry == NULL) {
188                 pthread_mutex_unlock(&mp_mutex_action);
189                 return;
190         }
191         TAILQ_REMOVE(&action_entry_list, entry, next);
192         pthread_mutex_unlock(&mp_mutex_action);
193         free(entry);
194 }
195
196 static int
197 read_msg(struct mp_msg_internal *m, struct sockaddr_un *s)
198 {
199         int msglen;
200         struct iovec iov;
201         struct msghdr msgh;
202         char control[CMSG_SPACE(sizeof(m->msg.fds))];
203         struct cmsghdr *cmsg;
204         int buflen = sizeof(*m) - sizeof(m->msg.fds);
205
206         memset(&msgh, 0, sizeof(msgh));
207         iov.iov_base = m;
208         iov.iov_len  = buflen;
209
210         msgh.msg_name = s;
211         msgh.msg_namelen = sizeof(*s);
212         msgh.msg_iov = &iov;
213         msgh.msg_iovlen = 1;
214         msgh.msg_control = control;
215         msgh.msg_controllen = sizeof(control);
216
217         msglen = recvmsg(mp_fd, &msgh, 0);
218         if (msglen < 0) {
219                 RTE_LOG(ERR, EAL, "recvmsg failed, %s\n", strerror(errno));
220                 return -1;
221         }
222
223         if (msglen != buflen || (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) {
224                 RTE_LOG(ERR, EAL, "truncted msg\n");
225                 return -1;
226         }
227
228         /* read auxiliary FDs if any */
229         for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
230                 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
231                 if ((cmsg->cmsg_level == SOL_SOCKET) &&
232                         (cmsg->cmsg_type == SCM_RIGHTS)) {
233                         memcpy(m->msg.fds, CMSG_DATA(cmsg), sizeof(m->msg.fds));
234                         break;
235                 }
236         }
237
238         return 0;
239 }
240
241 static void
242 process_msg(struct mp_msg_internal *m, struct sockaddr_un *s)
243 {
244         struct sync_request *sync_req;
245         struct action_entry *entry;
246         struct rte_mp_msg *msg = &m->msg;
247         rte_mp_t action = NULL;
248
249         RTE_LOG(DEBUG, EAL, "msg: %s\n", msg->name);
250
251         if (m->type == MP_REP) {
252                 pthread_mutex_lock(&sync_requests.lock);
253                 sync_req = find_sync_request(s->sun_path, msg->name);
254                 if (sync_req) {
255                         memcpy(sync_req->reply, msg, sizeof(*msg));
256                         sync_req->reply_received = 1;
257                         pthread_cond_signal(&sync_req->cond);
258                 } else
259                         RTE_LOG(ERR, EAL, "Drop mp reply: %s\n", msg->name);
260                 pthread_mutex_unlock(&sync_requests.lock);
261                 return;
262         }
263
264         pthread_mutex_lock(&mp_mutex_action);
265         entry = find_action_entry_by_name(msg->name);
266         if (entry != NULL)
267                 action = entry->action;
268         pthread_mutex_unlock(&mp_mutex_action);
269
270         if (!action)
271                 RTE_LOG(ERR, EAL, "Cannot find action: %s\n", msg->name);
272         else if (action(msg, s->sun_path) < 0)
273                 RTE_LOG(ERR, EAL, "Fail to handle message: %s\n", msg->name);
274 }
275
276 static void *
277 mp_handle(void *arg __rte_unused)
278 {
279         struct mp_msg_internal msg;
280         struct sockaddr_un sa;
281
282         while (1) {
283                 if (read_msg(&msg, &sa) == 0)
284                         process_msg(&msg, &sa);
285         }
286
287         return NULL;
288 }
289
290 static int
291 open_socket_fd(void)
292 {
293         struct sockaddr_un un;
294         const char *prefix = eal_mp_socket_path();
295
296         mp_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
297         if (mp_fd < 0) {
298                 RTE_LOG(ERR, EAL, "failed to create unix socket\n");
299                 return -1;
300         }
301
302         memset(&un, 0, sizeof(un));
303         un.sun_family = AF_UNIX;
304         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
305                 snprintf(un.sun_path, sizeof(un.sun_path), "%s", prefix);
306         else {
307                 snprintf(un.sun_path, sizeof(un.sun_path), "%s_%d_%"PRIx64,
308                          prefix, getpid(), rte_rdtsc());
309         }
310         unlink(un.sun_path); /* May still exist since last run */
311         if (bind(mp_fd, (struct sockaddr *)&un, sizeof(un)) < 0) {
312                 RTE_LOG(ERR, EAL, "failed to bind %s: %s\n",
313                         un.sun_path, strerror(errno));
314                 close(mp_fd);
315                 return -1;
316         }
317
318         RTE_LOG(INFO, EAL, "Multi-process socket %s\n", un.sun_path);
319         return mp_fd;
320 }
321
322 static int
323 unlink_sockets(const char *filter)
324 {
325         int dir_fd;
326         DIR *mp_dir;
327         struct dirent *ent;
328
329         mp_dir = opendir(mp_dir_path);
330         if (!mp_dir) {
331                 RTE_LOG(ERR, EAL, "Unable to open directory %s\n", mp_dir_path);
332                 return -1;
333         }
334         dir_fd = dirfd(mp_dir);
335
336         while ((ent = readdir(mp_dir))) {
337                 if (fnmatch(filter, ent->d_name, 0) == 0)
338                         unlinkat(dir_fd, ent->d_name, 0);
339         }
340
341         closedir(mp_dir);
342         return 0;
343 }
344
345 static void
346 unlink_socket_by_path(const char *path)
347 {
348         char *filename;
349         char *fullpath = strdup(path);
350
351         if (!fullpath)
352                 return;
353         filename = basename(fullpath);
354         unlink_sockets(filename);
355         free(fullpath);
356         RTE_LOG(INFO, EAL, "Remove socket %s\n", path);
357 }
358
359 int
360 rte_mp_channel_init(void)
361 {
362         char thread_name[RTE_MAX_THREAD_NAME_LEN];
363         char *path;
364         pthread_t tid;
365
366         snprintf(mp_filter, PATH_MAX, ".%s_unix_*",
367                  internal_config.hugefile_prefix);
368
369         path = strdup(eal_mp_socket_path());
370         snprintf(mp_dir_path, PATH_MAX, "%s", dirname(path));
371         free(path);
372
373         if (rte_eal_process_type() == RTE_PROC_PRIMARY &&
374             unlink_sockets(mp_filter)) {
375                 RTE_LOG(ERR, EAL, "failed to unlink mp sockets\n");
376                 return -1;
377         }
378
379         if (open_socket_fd() < 0)
380                 return -1;
381
382         if (pthread_create(&tid, NULL, mp_handle, NULL) < 0) {
383                 RTE_LOG(ERR, EAL, "failed to create mp thead: %s\n",
384                         strerror(errno));
385                 close(mp_fd);
386                 mp_fd = -1;
387                 return -1;
388         }
389
390         /* try best to set thread name */
391         snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN, "rte_mp_handle");
392         rte_thread_setname(tid, thread_name);
393         return 0;
394 }
395
396 /**
397  * Return -1, as fail to send message and it's caused by the local side.
398  * Return 0, as fail to send message and it's caused by the remote side.
399  * Return 1, as succeed to send message.
400  *
401  */
402 static int
403 send_msg(const char *dst_path, struct rte_mp_msg *msg, int type)
404 {
405         int snd;
406         struct iovec iov;
407         struct msghdr msgh;
408         struct cmsghdr *cmsg;
409         struct sockaddr_un dst;
410         struct mp_msg_internal m;
411         int fd_size = msg->num_fds * sizeof(int);
412         char control[CMSG_SPACE(fd_size)];
413
414         m.type = type;
415         memcpy(&m.msg, msg, sizeof(*msg));
416
417         memset(&dst, 0, sizeof(dst));
418         dst.sun_family = AF_UNIX;
419         snprintf(dst.sun_path, sizeof(dst.sun_path), "%s", dst_path);
420
421         memset(&msgh, 0, sizeof(msgh));
422         memset(control, 0, sizeof(control));
423
424         iov.iov_base = &m;
425         iov.iov_len = sizeof(m) - sizeof(msg->fds);
426
427         msgh.msg_name = &dst;
428         msgh.msg_namelen = sizeof(dst);
429         msgh.msg_iov = &iov;
430         msgh.msg_iovlen = 1;
431         msgh.msg_control = control;
432         msgh.msg_controllen = sizeof(control);
433
434         cmsg = CMSG_FIRSTHDR(&msgh);
435         cmsg->cmsg_len = CMSG_LEN(fd_size);
436         cmsg->cmsg_level = SOL_SOCKET;
437         cmsg->cmsg_type = SCM_RIGHTS;
438         memcpy(CMSG_DATA(cmsg), msg->fds, fd_size);
439
440         do {
441                 snd = sendmsg(mp_fd, &msgh, 0);
442         } while (snd < 0 && errno == EINTR);
443
444         if (snd < 0) {
445                 rte_errno = errno;
446                 /* Check if it caused by peer process exits */
447                 if (errno == -ECONNREFUSED) {
448                         /* We don't unlink the primary's socket here */
449                         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
450                                 unlink_socket_by_path(dst_path);
451                         return 0;
452                 }
453                 if (errno == -ENOBUFS) {
454                         RTE_LOG(ERR, EAL, "Peer cannot receive message %s\n",
455                                 dst_path);
456                         return 0;
457                 }
458                 RTE_LOG(ERR, EAL, "failed to send to (%s) due to %s\n",
459                         dst_path, strerror(errno));
460                 return -1;
461         }
462
463         return 1;
464 }
465
466 static int
467 mp_send(struct rte_mp_msg *msg, const char *peer, int type)
468 {
469         int ret = 0;
470         DIR *mp_dir;
471         struct dirent *ent;
472
473         if (!peer && (rte_eal_process_type() == RTE_PROC_SECONDARY))
474                 peer = eal_mp_socket_path();
475
476         if (peer) {
477                 if (send_msg(peer, msg, type) < 0)
478                         return -1;
479                 else
480                         return 0;
481         }
482
483         /* broadcast to all secondary processes */
484         mp_dir = opendir(mp_dir_path);
485         if (!mp_dir) {
486                 RTE_LOG(ERR, EAL, "Unable to open directory %s\n",
487                                 mp_dir_path);
488                 rte_errno = errno;
489                 return -1;
490         }
491         while ((ent = readdir(mp_dir))) {
492                 if (fnmatch(mp_filter, ent->d_name, 0) != 0)
493                         continue;
494
495                 if (send_msg(ent->d_name, msg, type) < 0)
496                         ret = -1;
497         }
498
499         closedir(mp_dir);
500         return ret;
501 }
502
503 static bool
504 check_input(const struct rte_mp_msg *msg)
505 {
506         if (msg == NULL) {
507                 RTE_LOG(ERR, EAL, "Msg cannot be NULL\n");
508                 rte_errno = EINVAL;
509                 return false;
510         }
511
512         if (validate_action_name(msg->name))
513                 return false;
514
515         if (msg->len_param > RTE_MP_MAX_PARAM_LEN) {
516                 RTE_LOG(ERR, EAL, "Message data is too long\n");
517                 rte_errno = E2BIG;
518                 return false;
519         }
520
521         if (msg->num_fds > RTE_MP_MAX_FD_NUM) {
522                 RTE_LOG(ERR, EAL, "Cannot send more than %d FDs\n",
523                         RTE_MP_MAX_FD_NUM);
524                 rte_errno = E2BIG;
525                 return false;
526         }
527
528         return true;
529 }
530
531 int __rte_experimental
532 rte_mp_sendmsg(struct rte_mp_msg *msg)
533 {
534         if (!check_input(msg))
535                 return -1;
536
537         RTE_LOG(DEBUG, EAL, "sendmsg: %s\n", msg->name);
538         return mp_send(msg, NULL, MP_MSG);
539 }
540
541 static int
542 mp_request_one(const char *dst, struct rte_mp_msg *req,
543                struct rte_mp_reply *reply, const struct timespec *ts)
544 {
545         int ret;
546         struct timeval now;
547         struct rte_mp_msg msg, *tmp;
548         struct sync_request sync_req, *exist;
549
550         sync_req.reply_received = 0;
551         strcpy(sync_req.dst, dst);
552         sync_req.request = req;
553         sync_req.reply = &msg;
554         pthread_cond_init(&sync_req.cond, NULL);
555
556         pthread_mutex_lock(&sync_requests.lock);
557         exist = find_sync_request(dst, req->name);
558         if (!exist)
559                 TAILQ_INSERT_TAIL(&sync_requests.requests, &sync_req, next);
560         pthread_mutex_unlock(&sync_requests.lock);
561         if (exist) {
562                 RTE_LOG(ERR, EAL, "A pending request %s:%s\n", dst, req->name);
563                 rte_errno = EEXIST;
564                 return -1;
565         }
566
567         ret = send_msg(dst, req, MP_REQ);
568         if (ret < 0) {
569                 RTE_LOG(ERR, EAL, "Fail to send request %s:%s\n",
570                         dst, req->name);
571                 return -1;
572         } else if (ret == 0)
573                 return 0;
574
575         reply->nb_sent++;
576
577         pthread_mutex_lock(&sync_requests.lock);
578         do {
579                 pthread_cond_timedwait(&sync_req.cond, &sync_requests.lock, ts);
580                 /* Check spurious wakeups */
581                 if (sync_req.reply_received == 1)
582                         break;
583                 /* Check if time is out */
584                 if (gettimeofday(&now, NULL) < 0)
585                         break;
586                 if (now.tv_sec < ts->tv_sec)
587                         break;
588                 else if (now.tv_sec == ts->tv_sec &&
589                          now.tv_usec * 1000 < ts->tv_nsec)
590                         break;
591         } while (1);
592         /* We got the lock now */
593         TAILQ_REMOVE(&sync_requests.requests, &sync_req, next);
594         pthread_mutex_unlock(&sync_requests.lock);
595
596         if (sync_req.reply_received == 0) {
597                 RTE_LOG(ERR, EAL, "Fail to recv reply for request %s:%s\n",
598                         dst, req->name);
599                 rte_errno = ETIMEDOUT;
600                 return -1;
601         }
602
603         tmp = realloc(reply->msgs, sizeof(msg) * (reply->nb_received + 1));
604         if (!tmp) {
605                 RTE_LOG(ERR, EAL, "Fail to alloc reply for request %s:%s\n",
606                         dst, req->name);
607                 rte_errno = ENOMEM;
608                 return -1;
609         }
610         memcpy(&tmp[reply->nb_received], &msg, sizeof(msg));
611         reply->msgs = tmp;
612         reply->nb_received++;
613         return 0;
614 }
615
616 int __rte_experimental
617 rte_mp_request(struct rte_mp_msg *req, struct rte_mp_reply *reply,
618                 const struct timespec *ts)
619 {
620         int ret = 0;
621         DIR *mp_dir;
622         struct dirent *ent;
623         struct timeval now;
624         struct timespec end;
625
626         RTE_LOG(DEBUG, EAL, "request: %s\n", req->name);
627
628         if (check_input(req) == false)
629                 return -1;
630         if (gettimeofday(&now, NULL) < 0) {
631                 RTE_LOG(ERR, EAL, "Faile to get current time\n");
632                 rte_errno = errno;
633                 return -1;
634         }
635
636         end.tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
637         end.tv_sec = now.tv_sec + ts->tv_sec +
638                         (now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
639
640         reply->nb_sent = 0;
641         reply->nb_received = 0;
642         reply->msgs = NULL;
643
644         /* for secondary process, send request to the primary process only */
645         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
646                 return mp_request_one(eal_mp_socket_path(), req, reply, &end);
647
648         /* for primary process, broadcast request, and collect reply 1 by 1 */
649         mp_dir = opendir(mp_dir_path);
650         if (!mp_dir) {
651                 RTE_LOG(ERR, EAL, "Unable to open directory %s\n", mp_dir_path);
652                 rte_errno = errno;
653                 return -1;
654         }
655
656         while ((ent = readdir(mp_dir))) {
657                 if (fnmatch(mp_filter, ent->d_name, 0) != 0)
658                         continue;
659
660                 if (mp_request_one(ent->d_name, req, reply, &end))
661                         ret = -1;
662         }
663
664         closedir(mp_dir);
665         return ret;
666 }
667
668 int __rte_experimental
669 rte_mp_reply(struct rte_mp_msg *msg, const char *peer)
670 {
671
672         RTE_LOG(DEBUG, EAL, "reply: %s\n", msg->name);
673
674         if (check_input(msg) == false)
675                 return -1;
676
677         if (peer == NULL) {
678                 RTE_LOG(ERR, EAL, "peer is not specified\n");
679                 rte_errno = EINVAL;
680                 return -1;
681         }
682
683         return mp_send(msg, peer, MP_REP);
684 }