Branch data Line data Source code
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/socket.h>
19 : : #include <sys/un.h>
20 : : #include <unistd.h>
21 : :
22 : : #include <rte_alarm.h>
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_thread.h>
30 : :
31 : : #include <eal_export.h>
32 : : #include "eal_memcfg.h"
33 : : #include "eal_private.h"
34 : : #include "eal_filesystem.h"
35 : : #include "eal_internal_cfg.h"
36 : :
37 : : static RTE_ATOMIC(int) mp_fd = -1;
38 : : static rte_thread_t mp_handle_tid;
39 : : static char mp_filter[UNIX_PATH_MAX]; /* Filter for secondary process sockets */
40 : : static char mp_dir_path[UNIX_PATH_MAX]; /* The directory path for all mp sockets */
41 : : static pthread_mutex_t mp_mutex_action = PTHREAD_MUTEX_INITIALIZER;
42 : : static char peer_name[UNIX_PATH_MAX];
43 : :
44 : : struct action_entry {
45 : : TAILQ_ENTRY(action_entry) next;
46 : : char action_name[RTE_MP_MAX_NAME_LEN];
47 : : rte_mp_t action;
48 : : };
49 : :
50 : : /** Double linked list of actions. */
51 : : TAILQ_HEAD(action_entry_list, action_entry);
52 : :
53 : : static struct action_entry_list action_entry_list =
54 : : TAILQ_HEAD_INITIALIZER(action_entry_list);
55 : :
56 : : enum mp_type {
57 : : MP_MSG, /* Share message with peers, will not block */
58 : : MP_REQ, /* Request for information, Will block for a reply */
59 : : MP_REP, /* Response to previously-received request */
60 : : MP_IGN, /* Response telling requester to ignore this response */
61 : : };
62 : :
63 : : /* Message sent from primary EAL to secondary EAL */
64 : : #define EAL_MP "mp_eal"
65 : : struct eal_mp_req {
66 : : enum {
67 : : MP_REQ_NOP = 0,
68 : : MP_REQ_QUIT,
69 : : } type;
70 : : };
71 : :
72 : : struct mp_msg_internal {
73 : : int type;
74 : : struct rte_mp_msg msg;
75 : : };
76 : :
77 : : struct async_request_param {
78 : : rte_mp_async_reply_t clb;
79 : : struct rte_mp_reply user_reply;
80 : : struct timespec end;
81 : : int n_responses_processed;
82 : : };
83 : :
84 : : struct pending_request {
85 : : TAILQ_ENTRY(pending_request) next;
86 : : unsigned long id;
87 : : enum {
88 : : REQUEST_TYPE_SYNC,
89 : : REQUEST_TYPE_ASYNC
90 : : } type;
91 : : char dst[UNIX_PATH_MAX];
92 : : struct rte_mp_msg *request;
93 : : struct rte_mp_msg *reply;
94 : : int reply_received;
95 : : union {
96 : : struct {
97 : : struct async_request_param *param;
98 : : } async;
99 : : struct {
100 : : pthread_cond_t cond;
101 : : } sync;
102 : : };
103 : : };
104 : :
105 : : static unsigned long next_request_id;
106 : :
107 : : TAILQ_HEAD(pending_request_list, pending_request);
108 : :
109 : : static struct {
110 : : struct pending_request_list requests;
111 : : pthread_mutex_t lock;
112 : : } pending_requests = {
113 : : .requests = TAILQ_HEAD_INITIALIZER(pending_requests.requests),
114 : : .lock = PTHREAD_MUTEX_INITIALIZER,
115 : : /**< used in async requests only */
116 : : };
117 : :
118 : : /* forward declarations */
119 : : static int
120 : : mp_send(struct rte_mp_msg *msg, const char *peer, int type);
121 : :
122 : : /* for use with alarm callback */
123 : : static void
124 : : async_reply_handle(void *arg);
125 : :
126 : : /* for use with alarm callback and process_msg */
127 : : static struct pending_request *
128 : : async_reply_handle_thread_unsafe(struct pending_request *req);
129 : :
130 : : static void
131 : : trigger_async_action(struct pending_request *req);
132 : :
133 : : static struct pending_request *
134 : 63 : find_request_by_name(const char *dst, const char *act_name)
135 : : {
136 : : struct pending_request *r;
137 : :
138 [ + + ]: 63 : TAILQ_FOREACH(r, &pending_requests.requests, next) {
139 [ + - ]: 31 : if (!strcmp(r->dst, dst) &&
140 [ - + ]: 31 : !strcmp(r->request->name, act_name))
141 : : break;
142 : : }
143 : :
144 : 63 : return r;
145 : : }
146 : :
147 : : static struct pending_request *
148 : : find_async_request_by_id(unsigned long id)
149 : : {
150 : : struct pending_request *r;
151 : :
152 [ # # ]: 0 : TAILQ_FOREACH(r, &pending_requests.requests, next) {
153 [ # # # # ]: 0 : if (r->id == id && r->type == REQUEST_TYPE_ASYNC)
154 : : return r;
155 : : }
156 : :
157 : : return NULL;
158 : : }
159 : :
160 : : /*
161 : : * Combine prefix and name(optional) to return unix domain socket path
162 : : * return the number of characters that would have been put into buffer.
163 : : */
164 : : static int
165 : 660 : create_socket_path(const char *name, char *buf, size_t len)
166 : : {
167 : 660 : const char *prefix = eal_mp_socket_path();
168 : :
169 [ + + ]: 660 : if (strlen(name) > 0)
170 : 284 : return snprintf(buf, len, "%s_%s", prefix, name);
171 : : else
172 : 376 : return strlcpy(buf, prefix, len);
173 : : }
174 : :
175 : : RTE_EXPORT_SYMBOL(rte_eal_primary_proc_alive)
176 : : int
177 : 0 : rte_eal_primary_proc_alive(const char *config_file_path)
178 : : {
179 : : int config_fd;
180 : :
181 [ # # ]: 0 : if (config_file_path)
182 : : config_fd = open(config_file_path, O_RDONLY);
183 : : else {
184 : : const char *path;
185 : :
186 : 0 : path = eal_runtime_config_path();
187 : : config_fd = open(path, O_RDONLY);
188 : : }
189 [ # # ]: 0 : if (config_fd < 0)
190 : : return 0;
191 : :
192 : 0 : int ret = lockf(config_fd, F_TEST, 0);
193 : 0 : close(config_fd);
194 : :
195 : 0 : return !!ret;
196 : : }
197 : :
198 : : static struct action_entry *
199 : 1741 : find_action_entry_by_name(const char *name)
200 : : {
201 : : struct action_entry *entry;
202 : :
203 [ + + ]: 4098 : TAILQ_FOREACH(entry, &action_entry_list, next) {
204 [ + + ]: 2913 : if (strncmp(entry->action_name, name, RTE_MP_MAX_NAME_LEN) == 0)
205 : : break;
206 : : }
207 : :
208 : 1741 : return entry;
209 : : }
210 : :
211 : : static int
212 : 2259 : validate_action_name(const char *name)
213 : : {
214 [ - + ]: 2259 : if (name == NULL) {
215 : 0 : EAL_LOG(ERR, "Action name cannot be NULL");
216 : 0 : rte_errno = EINVAL;
217 : 0 : return -1;
218 : : }
219 [ - + ]: 2259 : if (strnlen(name, RTE_MP_MAX_NAME_LEN) == 0) {
220 : 0 : EAL_LOG(ERR, "Length of action name is zero");
221 : 0 : rte_errno = EINVAL;
222 : 0 : return -1;
223 : : }
224 [ - + ]: 2259 : if (strnlen(name, RTE_MP_MAX_NAME_LEN) == RTE_MP_MAX_NAME_LEN) {
225 : 0 : rte_errno = E2BIG;
226 : 0 : return -1;
227 : : }
228 : : return 0;
229 : : }
230 : :
231 : : RTE_EXPORT_SYMBOL(rte_mp_action_register)
232 : : int
233 : 774 : rte_mp_action_register(const char *name, rte_mp_t action)
234 : : {
235 : : struct action_entry *entry;
236 : :
237 [ + - ]: 774 : if (validate_action_name(name) != 0)
238 : : return -1;
239 : :
240 [ + + ]: 774 : if (eal_mp_is_disabled()) {
241 : 30 : EAL_LOG(DEBUG, "Multi-process support is disabled");
242 : 30 : rte_errno = ENOTSUP;
243 : 30 : return -1;
244 : : }
245 : :
246 : 744 : entry = malloc(sizeof(struct action_entry));
247 [ - + ]: 744 : if (entry == NULL) {
248 : 0 : rte_errno = ENOMEM;
249 : 0 : return -1;
250 : : }
251 : 744 : strlcpy(entry->action_name, name, sizeof(entry->action_name));
252 : 744 : entry->action = action;
253 : :
254 : 744 : pthread_mutex_lock(&mp_mutex_action);
255 [ - + ]: 744 : if (find_action_entry_by_name(name) != NULL) {
256 : 0 : pthread_mutex_unlock(&mp_mutex_action);
257 : 0 : rte_errno = EEXIST;
258 : 0 : free(entry);
259 : 0 : return -1;
260 : : }
261 : 744 : TAILQ_INSERT_TAIL(&action_entry_list, entry, next);
262 : 744 : pthread_mutex_unlock(&mp_mutex_action);
263 : 744 : return 0;
264 : : }
265 : :
266 : : RTE_EXPORT_SYMBOL(rte_mp_action_unregister)
267 : : void
268 : 965 : rte_mp_action_unregister(const char *name)
269 : : {
270 : : struct action_entry *entry;
271 : :
272 [ + - ]: 965 : if (validate_action_name(name) != 0)
273 : : return;
274 : :
275 : 965 : pthread_mutex_lock(&mp_mutex_action);
276 : 965 : entry = find_action_entry_by_name(name);
277 [ + + ]: 965 : if (entry == NULL) {
278 : 441 : pthread_mutex_unlock(&mp_mutex_action);
279 : 441 : return;
280 : : }
281 [ + + ]: 524 : TAILQ_REMOVE(&action_entry_list, entry, next);
282 : 524 : pthread_mutex_unlock(&mp_mutex_action);
283 : 524 : free(entry);
284 : : }
285 : :
286 : : static int
287 : 283 : read_msg(int fd, struct mp_msg_internal *m, struct sockaddr_un *s)
288 : : {
289 : : int msglen;
290 : : struct iovec iov;
291 : : struct msghdr msgh;
292 : : char control[CMSG_SPACE(sizeof(m->msg.fds))];
293 : : struct cmsghdr *cmsg;
294 : : int buflen = sizeof(*m) - sizeof(m->msg.fds);
295 : :
296 : : memset(&msgh, 0, sizeof(msgh));
297 : 283 : iov.iov_base = m;
298 : 283 : iov.iov_len = buflen;
299 : :
300 : 283 : msgh.msg_name = s;
301 : 283 : msgh.msg_namelen = sizeof(*s);
302 : 283 : msgh.msg_iov = &iov;
303 : 283 : msgh.msg_iovlen = 1;
304 : 283 : msgh.msg_control = control;
305 : 283 : msgh.msg_controllen = sizeof(control);
306 : :
307 : 283 : retry:
308 : 283 : msglen = recvmsg(fd, &msgh, 0);
309 : :
310 : : /* zero length message means socket was closed */
311 [ + - ]: 63 : if (msglen == 0)
312 : : return 0;
313 : :
314 [ - + ]: 63 : if (msglen < 0) {
315 [ # # ]: 0 : if (errno == EINTR)
316 : 0 : goto retry;
317 : :
318 : 0 : EAL_LOG(ERR, "recvmsg failed, %s", strerror(errno));
319 : 0 : return -1;
320 : : }
321 : :
322 [ + - - + ]: 63 : if (msglen != buflen || (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) {
323 : 0 : EAL_LOG(ERR, "truncated msg");
324 : 0 : return -1;
325 : : }
326 : :
327 : : /* read auxiliary FDs if any */
328 [ - + - + ]: 126 : for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
329 : : cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
330 [ # # ]: 0 : if ((cmsg->cmsg_level == SOL_SOCKET) &&
331 : : (cmsg->cmsg_type == SCM_RIGHTS)) {
332 : 0 : memcpy(m->msg.fds, CMSG_DATA(cmsg), sizeof(m->msg.fds));
333 : : break;
334 : : }
335 : : }
336 : : /* sanity-check the response */
337 [ - + ]: 63 : if (m->msg.num_fds < 0 || m->msg.num_fds > RTE_MP_MAX_FD_NUM) {
338 : 0 : EAL_LOG(ERR, "invalid number of fd's received");
339 : 0 : return -1;
340 : : }
341 [ - + ]: 63 : if (m->msg.len_param < 0 || m->msg.len_param > RTE_MP_MAX_PARAM_LEN) {
342 : 0 : EAL_LOG(ERR, "invalid received data length");
343 : 0 : return -1;
344 : : }
345 : : return msglen;
346 : : }
347 : :
348 : : static void
349 : : cleanup_msg_fds(const struct rte_mp_msg *msg)
350 : : {
351 : : int i;
352 : :
353 [ # # # # ]: 0 : for (i = 0; i < msg->num_fds; i++)
354 : 0 : close(msg->fds[i]);
355 : : }
356 : :
357 : : static void
358 : 63 : process_msg(struct mp_msg_internal *m, struct sockaddr_un *s)
359 : : {
360 : : struct pending_request *pending_req;
361 : : struct action_entry *entry;
362 : 63 : struct rte_mp_msg *msg = &m->msg;
363 : : rte_mp_t action = NULL;
364 : : const struct internal_config *internal_conf =
365 : 63 : eal_get_internal_configuration();
366 : :
367 : 63 : EAL_LOG(DEBUG, "msg: %s", msg->name);
368 : :
369 [ + + ]: 63 : if (m->type == MP_REP || m->type == MP_IGN) {
370 : : struct pending_request *req = NULL;
371 : :
372 : 31 : pthread_mutex_lock(&pending_requests.lock);
373 : 31 : pending_req = find_request_by_name(s->sun_path, msg->name);
374 [ + - ]: 31 : if (pending_req) {
375 [ - + ]: 31 : memcpy(pending_req->reply, msg, sizeof(*msg));
376 : : /* -1 indicates that we've been asked to ignore */
377 : 31 : pending_req->reply_received =
378 [ - + ]: 31 : m->type == MP_REP ? 1 : -1;
379 : :
380 [ + - ]: 31 : if (pending_req->type == REQUEST_TYPE_SYNC)
381 : 31 : pthread_cond_signal(&pending_req->sync.cond);
382 [ # # ]: 0 : else if (pending_req->type == REQUEST_TYPE_ASYNC)
383 : 0 : req = async_reply_handle_thread_unsafe(pending_req);
384 : : } else {
385 : 0 : EAL_LOG(ERR, "Drop mp reply: %s", msg->name);
386 : : cleanup_msg_fds(msg);
387 : : }
388 : 31 : pthread_mutex_unlock(&pending_requests.lock);
389 : :
390 [ - + ]: 31 : if (req != NULL)
391 : 0 : trigger_async_action(req);
392 : 31 : return;
393 : : }
394 : :
395 : 32 : pthread_mutex_lock(&mp_mutex_action);
396 : 32 : entry = find_action_entry_by_name(msg->name);
397 [ + - ]: 32 : if (entry != NULL)
398 : 32 : action = entry->action;
399 : 32 : pthread_mutex_unlock(&mp_mutex_action);
400 : :
401 [ - + ]: 32 : if (!action) {
402 [ # # # # ]: 0 : if (m->type == MP_REQ && !internal_conf->init_complete) {
403 : : /* if this is a request, and init is not yet complete,
404 : : * and callback wasn't registered, we should tell the
405 : : * requester to ignore our existence because we're not
406 : : * yet ready to process this request.
407 : : */
408 : : struct rte_mp_msg dummy;
409 : :
410 : : memset(&dummy, 0, sizeof(dummy));
411 : : strlcpy(dummy.name, msg->name, sizeof(dummy.name));
412 : 0 : mp_send(&dummy, s->sun_path, MP_IGN);
413 : : } else {
414 : 0 : EAL_LOG(ERR, "Cannot find action: %s",
415 : : msg->name);
416 : : }
417 : : cleanup_msg_fds(msg);
418 [ - + ]: 32 : } else if (action(msg, s->sun_path) < 0) {
419 : 0 : EAL_LOG(ERR, "Fail to handle message: %s", msg->name);
420 : : }
421 : : }
422 : :
423 : : static uint32_t
424 : 220 : mp_handle(void *arg __rte_unused)
425 : : {
426 : : struct mp_msg_internal msg;
427 : : struct sockaddr_un sa;
428 : : int fd;
429 : :
430 [ + - ]: 283 : while ((fd = rte_atomic_load_explicit(&mp_fd, rte_memory_order_relaxed)) >= 0) {
431 : : int ret;
432 : :
433 : 283 : ret = read_msg(fd, &msg, &sa);
434 [ + - ]: 63 : if (ret <= 0)
435 : : break;
436 : :
437 : 63 : process_msg(&msg, &sa);
438 : : }
439 : :
440 : 0 : return 0;
441 : : }
442 : :
443 : : static int
444 : : timespec_cmp(const struct timespec *a, const struct timespec *b)
445 : : {
446 : 0 : if (a->tv_sec < b->tv_sec)
447 : : return -1;
448 [ # # ]: 0 : if (a->tv_sec > b->tv_sec)
449 : : return 1;
450 [ # # ]: 0 : if (a->tv_nsec < b->tv_nsec)
451 : : return -1;
452 [ # # ]: 0 : if (a->tv_nsec > b->tv_nsec)
453 : 0 : return 1;
454 : : return 0;
455 : : }
456 : :
457 : : enum async_action {
458 : : ACTION_FREE, /**< free the action entry, but don't trigger callback */
459 : : ACTION_TRIGGER /**< trigger callback, then free action entry */
460 : : };
461 : :
462 : : static enum async_action
463 : 0 : process_async_request(struct pending_request *sr, const struct timespec *now)
464 : : {
465 : : struct async_request_param *param;
466 : : struct rte_mp_reply *reply;
467 : : bool timeout, last_msg;
468 : :
469 [ # # ]: 0 : param = sr->async.param;
470 : : reply = ¶m->user_reply;
471 : :
472 : : /* did we timeout? */
473 : : timeout = timespec_cmp(¶m->end, now) <= 0;
474 : :
475 : : /* if we received a response, adjust relevant data and copy message. */
476 [ # # # # ]: 0 : if (sr->reply_received == 1 && sr->reply) {
477 : : struct rte_mp_msg *msg, *user_msgs, *tmp;
478 : :
479 : : msg = sr->reply;
480 : 0 : user_msgs = reply->msgs;
481 : :
482 : 0 : tmp = realloc(user_msgs, sizeof(*msg) *
483 : 0 : (reply->nb_received + 1));
484 [ # # ]: 0 : if (!tmp) {
485 : 0 : EAL_LOG(ERR, "Fail to alloc reply for request %s:%s",
486 : : sr->dst, sr->request->name);
487 : : /* this entry is going to be removed and its message
488 : : * dropped, but we don't want to leak memory, so
489 : : * continue.
490 : : */
491 : : } else {
492 : : user_msgs = tmp;
493 : 0 : reply->msgs = user_msgs;
494 : 0 : memcpy(&user_msgs[reply->nb_received],
495 : : msg, sizeof(*msg));
496 : 0 : reply->nb_received++;
497 : : }
498 : :
499 : : /* mark this request as processed */
500 : 0 : param->n_responses_processed++;
501 [ # # ]: 0 : } else if (sr->reply_received == -1) {
502 : : /* we were asked to ignore this process */
503 : 0 : reply->nb_sent--;
504 [ # # ]: 0 : } else if (timeout) {
505 : : /* count it as processed response, but don't increment
506 : : * nb_received.
507 : : */
508 : 0 : param->n_responses_processed++;
509 : : }
510 : :
511 : 0 : free(sr->reply);
512 : :
513 : 0 : last_msg = param->n_responses_processed == reply->nb_sent;
514 : :
515 : 0 : return last_msg ? ACTION_TRIGGER : ACTION_FREE;
516 : : }
517 : :
518 : : static void
519 : 0 : trigger_async_action(struct pending_request *sr)
520 : : {
521 : : struct async_request_param *param;
522 : : struct rte_mp_reply *reply;
523 : :
524 : 0 : param = sr->async.param;
525 : 0 : reply = ¶m->user_reply;
526 : :
527 : 0 : param->clb(sr->request, reply);
528 : :
529 : : /* clean up */
530 : 0 : free(sr->async.param->user_reply.msgs);
531 : 0 : free(sr->async.param);
532 : 0 : free(sr->request);
533 : 0 : free(sr);
534 : 0 : }
535 : :
536 : : static struct pending_request *
537 : 0 : async_reply_handle_thread_unsafe(struct pending_request *req)
538 : : {
539 : : enum async_action action;
540 : : struct timespec ts_now;
541 : :
542 [ # # ]: 0 : if (clock_gettime(CLOCK_MONOTONIC, &ts_now) < 0) {
543 : 0 : EAL_LOG(ERR, "Cannot get current time");
544 : 0 : goto no_trigger;
545 : : }
546 : :
547 : 0 : action = process_async_request(req, &ts_now);
548 : :
549 [ # # ]: 0 : TAILQ_REMOVE(&pending_requests.requests, req, next);
550 : :
551 [ # # ]: 0 : if (action == ACTION_TRIGGER)
552 : : return req;
553 : 0 : no_trigger:
554 : 0 : free(req);
555 : 0 : return NULL;
556 : : }
557 : :
558 : : static void
559 : 0 : async_reply_handle(void *arg)
560 : : {
561 : : struct pending_request *req;
562 : : /* alarm arg carries the request ID packed into a void * via uintptr_t */
563 : 0 : unsigned long id = (uintptr_t)arg;
564 : :
565 : : /* ensure request ID matches pointer size */
566 : : RTE_BUILD_BUG_ON(sizeof(next_request_id) != sizeof(uintptr_t));
567 : :
568 : 0 : pthread_mutex_lock(&pending_requests.lock);
569 : : req = find_async_request_by_id(id);
570 [ # # ]: 0 : if (req != NULL)
571 : 0 : req = async_reply_handle_thread_unsafe(req);
572 : 0 : pthread_mutex_unlock(&pending_requests.lock);
573 : :
574 [ # # ]: 0 : if (req != NULL)
575 : 0 : trigger_async_action(req);
576 : 0 : }
577 : :
578 : : static int
579 : 220 : open_socket_fd(void)
580 : : {
581 : 220 : struct sockaddr_un un = { .sun_family = AF_UNIX };
582 : :
583 : 220 : peer_name[0] = '\0';
584 [ + + ]: 220 : if (rte_eal_process_type() == RTE_PROC_SECONDARY)
585 : 32 : snprintf(peer_name, sizeof(peer_name),
586 : : "%d_%"PRIx64, getpid(), rte_rdtsc());
587 : :
588 [ - + ]: 220 : if (create_socket_path(peer_name, un.sun_path, sizeof(un.sun_path))
589 : : >= (int)sizeof(un.sun_path)) {
590 : 0 : EAL_LOG(ERR, "peer '%s' socket path too long", peer_name);
591 : 0 : return -1;
592 : : }
593 : :
594 : 220 : mp_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
595 [ - + ]: 220 : if (mp_fd < 0) {
596 : 0 : EAL_LOG(ERR, "failed to create unix socket");
597 : 0 : return -1;
598 : : }
599 : :
600 : 220 : unlink(un.sun_path); /* May still exist since last run */
601 : :
602 [ - + ]: 220 : if (bind(mp_fd, (struct sockaddr *)&un, sizeof(un)) < 0) {
603 : 0 : EAL_LOG(ERR, "failed to bind %s: %s",
604 : : un.sun_path, strerror(errno));
605 : 0 : close(mp_fd);
606 : 0 : return -1;
607 : : }
608 : :
609 : 220 : EAL_LOG(INFO, "Multi-process socket %s", un.sun_path);
610 : 220 : return mp_fd;
611 : : }
612 : :
613 : : static void
614 : 220 : close_socket_fd(int fd)
615 : : {
616 : : char path[UNIX_PATH_MAX];
617 : :
618 : 220 : close(fd);
619 : :
620 [ - + ]: 220 : if (create_socket_path(peer_name, path, sizeof(path)) < 0)
621 : 0 : EAL_LOG(ERR, "file prefix path for peer '%s' too long", peer_name);
622 : : else
623 : 220 : unlink(path);
624 : 220 : }
625 : :
626 : : /* callback in secondary when primary has exited */
627 : : static int
628 : 0 : mp_primary_exited(const struct rte_mp_msg *msg, const void *peer __rte_unused)
629 : : {
630 : : const struct eal_mp_req *req;
631 : :
632 [ # # ]: 0 : if (msg->len_param != sizeof(*req)) {
633 : 0 : EAL_LOG(ERR, "invalid request from primary");
634 : 0 : return -EINVAL;
635 : : }
636 : :
637 : : req = (const struct eal_mp_req *)msg->param;
638 [ # # ]: 0 : if (req->type == MP_REQ_QUIT)
639 : 0 : rte_mp_channel_cleanup();
640 : :
641 : : /* no reply needed */
642 : : return 0;
643 : : }
644 : :
645 : : int
646 : 230 : rte_mp_channel_init(void)
647 : : {
648 : : char path[UNIX_PATH_MAX];
649 : : int dir_fd;
650 : :
651 [ + + ]: 230 : if (eal_mp_is_disabled()) {
652 : 10 : EAL_LOG(DEBUG, "Multi-process support is disabled");
653 : 10 : rte_errno = ENOTSUP;
654 : 10 : return -1;
655 : : }
656 : :
657 : : /* create filter path */
658 [ - + ]: 220 : if (create_socket_path("*", path, sizeof(path)) < 0) {
659 : 0 : EAL_LOG(ERR, "file prefix path too long");
660 : 0 : rte_errno = ENAMETOOLONG;
661 : 0 : return -1;
662 : : }
663 : :
664 : 220 : rte_basename(path, mp_filter, sizeof(mp_filter));
665 : 220 : strlcpy(mp_dir_path, dirname(path), sizeof(mp_dir_path));
666 : :
667 : : /* lock the directory */
668 : : dir_fd = open(mp_dir_path, O_RDONLY);
669 [ - + ]: 220 : if (dir_fd < 0) {
670 : 0 : EAL_LOG(ERR, "failed to open %s: %s",
671 : : mp_dir_path, strerror(errno));
672 : 0 : return -1;
673 : : }
674 : :
675 [ - + ]: 220 : if (flock(dir_fd, LOCK_EX)) {
676 : 0 : EAL_LOG(ERR, "failed to lock %s: %s",
677 : : mp_dir_path, strerror(errno));
678 : 0 : close(dir_fd);
679 : 0 : return -1;
680 : : }
681 : :
682 [ - + ]: 220 : if (open_socket_fd() < 0) {
683 : 0 : close(dir_fd);
684 : 0 : return -1;
685 : : }
686 : :
687 : : /* listen for quit message from primary */
688 [ + + ]: 220 : if (rte_eal_process_type() == RTE_PROC_SECONDARY)
689 : 32 : rte_mp_action_register(EAL_MP, mp_primary_exited);
690 : :
691 [ - + ]: 220 : if (rte_thread_create_internal_control(&mp_handle_tid, "mp-msg",
692 : : mp_handle, NULL) < 0) {
693 : 0 : EAL_LOG(ERR, "failed to create mp thread: %s",
694 : : strerror(errno));
695 : 0 : close(dir_fd);
696 : 0 : close(rte_atomic_exchange_explicit(&mp_fd, -1, rte_memory_order_relaxed));
697 : 0 : return -1;
698 : : }
699 : :
700 : : /* unlock the directory */
701 : 220 : flock(dir_fd, LOCK_UN);
702 : 220 : close(dir_fd);
703 : :
704 : 220 : return 0;
705 : : }
706 : :
707 : : void
708 : 299 : rte_mp_channel_cleanup(void)
709 : : {
710 : : int fd;
711 : :
712 : : /* Primary exiting, tell all secondary processes */
713 [ + + ]: 299 : if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
714 : 266 : struct rte_mp_msg msg = { .name = EAL_MP };
715 : : struct eal_mp_req req = { .type = MP_REQ_QUIT };
716 : :
717 : : memcpy(&msg.param, &req, sizeof(req));
718 : 266 : msg.len_param = sizeof(req);
719 : :
720 : 266 : rte_mp_sendmsg(&msg);
721 : : } else {
722 : 33 : rte_mp_action_unregister(EAL_MP);
723 : : }
724 : :
725 : 299 : fd = rte_atomic_exchange_explicit(&mp_fd, -1, rte_memory_order_relaxed);
726 [ + + ]: 299 : if (fd < 0)
727 : : return;
728 : :
729 [ + - ]: 220 : if (pthread_self() != (pthread_t)mp_handle_tid.opaque_id) {
730 : 220 : pthread_cancel((pthread_t)mp_handle_tid.opaque_id);
731 : 220 : rte_thread_join(mp_handle_tid, NULL);
732 : : }
733 : 220 : close_socket_fd(fd);
734 : : }
735 : :
736 : : /**
737 : : * Return -1, as fail to send message and it's caused by the local side.
738 : : * Return 0, as fail to send message and it's caused by the remote side.
739 : : * Return 1, as succeed to send message.
740 : : */
741 : : static int
742 : 64 : send_msg(const char *dst_path, struct rte_mp_msg *msg, int type)
743 : : {
744 : : int snd;
745 : : struct iovec iov;
746 : : struct msghdr msgh;
747 : : struct cmsghdr *cmsg;
748 : : struct sockaddr_un dst;
749 : : struct mp_msg_internal m;
750 : 64 : int fd_size = msg->num_fds * sizeof(int);
751 : 64 : const int32_t control_sz = CMSG_SPACE(fd_size);
752 : : char control[CMSG_SPACE(sizeof(msg->fds))];
753 : :
754 [ + - ]: 64 : m.type = type;
755 : : memcpy(&m.msg, msg, sizeof(*msg));
756 : :
757 : : memset(&dst, 0, sizeof(dst));
758 [ + - ]: 64 : dst.sun_family = AF_UNIX;
759 : : strlcpy(dst.sun_path, dst_path, sizeof(dst.sun_path));
760 : :
761 : : memset(&msgh, 0, sizeof(msgh));
762 : : memset(control, 0, sizeof(control));
763 : :
764 : 64 : iov.iov_base = &m;
765 : 64 : iov.iov_len = sizeof(m) - sizeof(msg->fds);
766 : :
767 : 64 : msgh.msg_name = &dst;
768 : 64 : msgh.msg_namelen = sizeof(dst);
769 : 64 : msgh.msg_iov = &iov;
770 : 64 : msgh.msg_iovlen = 1;
771 : 64 : msgh.msg_control = control;
772 : 64 : msgh.msg_controllen = control_sz;
773 : :
774 [ + - ]: 64 : cmsg = CMSG_FIRSTHDR(&msgh);
775 : 64 : cmsg->cmsg_len = CMSG_LEN(fd_size);
776 : 64 : cmsg->cmsg_level = SOL_SOCKET;
777 : 64 : cmsg->cmsg_type = SCM_RIGHTS;
778 : 64 : memcpy(CMSG_DATA(cmsg), msg->fds, fd_size);
779 : :
780 : : do {
781 : 64 : snd = sendmsg(mp_fd, &msgh, 0);
782 [ + + - + ]: 64 : } while (snd < 0 && errno == EINTR);
783 : :
784 [ + + ]: 64 : if (snd < 0) {
785 : 1 : rte_errno = errno;
786 : : /* Check if it caused by peer process exits */
787 [ - + - - ]: 1 : if (errno == ECONNREFUSED &&
788 : 0 : rte_eal_process_type() == RTE_PROC_PRIMARY) {
789 : 0 : unlink(dst_path);
790 : 0 : return 0;
791 : : }
792 : 1 : EAL_LOG(ERR, "failed to send to (%s) due to %s",
793 : : dst_path, strerror(errno));
794 : 1 : return -1;
795 : : }
796 : :
797 : : return 1;
798 : : }
799 : :
800 : : static int
801 : 291 : mp_send(struct rte_mp_msg *msg, const char *peer, int type)
802 : : {
803 : : int dir_fd, ret = 0;
804 : : DIR *mp_dir;
805 : : struct dirent *ent;
806 : :
807 [ + + - + ]: 291 : if (!peer && (rte_eal_process_type() == RTE_PROC_SECONDARY))
808 : 0 : peer = eal_mp_socket_path();
809 : :
810 [ + + ]: 291 : if (peer) {
811 [ + - ]: 31 : if (send_msg(peer, msg, type) < 0)
812 : : return -1;
813 : : else
814 : 31 : return 0;
815 : : }
816 : :
817 : : /* broadcast to all secondary processes */
818 : 260 : mp_dir = opendir(mp_dir_path);
819 [ + + ]: 260 : if (!mp_dir) {
820 : 68 : EAL_LOG(ERR, "Unable to open directory %s",
821 : : mp_dir_path);
822 : 68 : rte_errno = errno;
823 : 68 : return -1;
824 : : }
825 : :
826 : 192 : dir_fd = dirfd(mp_dir);
827 : : /* lock the directory to prevent processes spinning up while we send */
828 [ - + ]: 192 : if (flock(dir_fd, LOCK_SH)) {
829 : 0 : EAL_LOG(ERR, "Unable to lock directory %s",
830 : : mp_dir_path);
831 : 0 : rte_errno = errno;
832 : 0 : closedir(mp_dir);
833 : 0 : return -1;
834 : : }
835 : :
836 [ + + ]: 1836 : while ((ent = readdir(mp_dir))) {
837 : : char path[UNIX_PATH_MAX];
838 : :
839 [ + + ]: 1644 : if (fnmatch(mp_filter, ent->d_name, 0) != 0)
840 : 1643 : continue;
841 : :
842 [ - + ]: 1 : if (snprintf(path, sizeof(path), "%s/%s", mp_dir_path, ent->d_name)
843 : : >= (int)sizeof(path)) {
844 : 0 : EAL_LOG(ERR, "Unix domain path %s/%s too long", mp_dir_path, ent->d_name);
845 : : ret = -1;
846 [ - + ]: 1 : } else if (send_msg(path, msg, type) < 0)
847 : : ret = -1;
848 : : }
849 : : /* unlock the dir */
850 : 192 : flock(dir_fd, LOCK_UN);
851 : :
852 : : /* dir_fd automatically closed on closedir */
853 : 192 : closedir(mp_dir);
854 : 192 : return ret;
855 : : }
856 : :
857 : : static int
858 : 520 : check_input(const struct rte_mp_msg *msg)
859 : : {
860 [ - + ]: 520 : if (msg == NULL) {
861 : 0 : EAL_LOG(ERR, "Msg cannot be NULL");
862 : 0 : rte_errno = EINVAL;
863 : 0 : return -1;
864 : : }
865 : :
866 [ + - ]: 520 : if (validate_action_name(msg->name) != 0)
867 : : return -1;
868 : :
869 [ - + ]: 520 : if (msg->len_param < 0) {
870 : 0 : EAL_LOG(ERR, "Message data length is negative");
871 : 0 : rte_errno = EINVAL;
872 : 0 : return -1;
873 : : }
874 : :
875 [ - + ]: 520 : if (msg->num_fds < 0) {
876 : 0 : EAL_LOG(ERR, "Number of fd's is negative");
877 : 0 : rte_errno = EINVAL;
878 : 0 : return -1;
879 : : }
880 : :
881 [ - + ]: 520 : if (msg->len_param > RTE_MP_MAX_PARAM_LEN) {
882 : 0 : EAL_LOG(ERR, "Message data is too long");
883 : 0 : rte_errno = E2BIG;
884 : 0 : return -1;
885 : : }
886 : :
887 [ - + ]: 520 : if (msg->num_fds > RTE_MP_MAX_FD_NUM) {
888 : 0 : EAL_LOG(ERR, "Cannot send more than %d FDs",
889 : : RTE_MP_MAX_FD_NUM);
890 : 0 : rte_errno = E2BIG;
891 : 0 : return -1;
892 : : }
893 : :
894 : : return 0;
895 : : }
896 : :
897 : : RTE_EXPORT_SYMBOL(rte_mp_sendmsg)
898 : : int
899 : 271 : rte_mp_sendmsg(struct rte_mp_msg *msg)
900 : : {
901 [ + - ]: 271 : if (check_input(msg) != 0)
902 : : return -1;
903 : :
904 [ + + ]: 271 : if (eal_mp_is_disabled()) {
905 : 11 : EAL_LOG(DEBUG, "Multi-process support is disabled");
906 : 11 : rte_errno = ENOTSUP;
907 : 11 : return -1;
908 : : }
909 : :
910 : 260 : EAL_LOG(DEBUG, "sendmsg: %s", msg->name);
911 : 260 : return mp_send(msg, NULL, MP_MSG);
912 : : }
913 : :
914 : : static int
915 : 0 : mp_request_async(const char *dst, struct rte_mp_msg *req,
916 : : struct async_request_param *param, const struct timespec *ts)
917 : : {
918 : : struct rte_mp_msg *reply_msg;
919 : : struct pending_request *pending_req, *exist;
920 : : unsigned long id;
921 : : int ret = -1;
922 : :
923 : : /* queue already locked by caller */
924 : :
925 : 0 : exist = find_request_by_name(dst, req->name);
926 [ # # ]: 0 : if (exist) {
927 : 0 : EAL_LOG(ERR, "A pending request %s:%s", dst, req->name);
928 : 0 : rte_errno = EEXIST;
929 : 0 : return -1;
930 : : }
931 : :
932 : 0 : pending_req = calloc(1, sizeof(*pending_req));
933 : 0 : reply_msg = calloc(1, sizeof(*reply_msg));
934 [ # # ]: 0 : if (pending_req == NULL || reply_msg == NULL) {
935 : 0 : EAL_LOG(ERR, "Could not allocate space for async request");
936 : 0 : rte_errno = ENOMEM;
937 : : ret = -1;
938 : 0 : goto fail;
939 : : }
940 : :
941 : 0 : id = ++next_request_id;
942 : 0 : pending_req->type = REQUEST_TYPE_ASYNC;
943 : 0 : pending_req->id = id;
944 : 0 : strlcpy(pending_req->dst, dst, sizeof(pending_req->dst));
945 : 0 : pending_req->request = req;
946 : 0 : pending_req->reply = reply_msg;
947 : 0 : pending_req->async.param = param;
948 : :
949 : 0 : ret = send_msg(dst, req, MP_REQ);
950 [ # # ]: 0 : if (ret < 0) {
951 : 0 : EAL_LOG(ERR, "Fail to send request %s:%s",
952 : : dst, req->name);
953 : : ret = -1;
954 : 0 : goto fail;
955 [ # # ]: 0 : } else if (ret == 0) {
956 : : ret = 0;
957 : 0 : goto fail;
958 : : }
959 : 0 : param->user_reply.nb_sent++;
960 : :
961 : : /* if alarm set fails, we simply ignore the reply */
962 [ # # ]: 0 : if (rte_eal_alarm_set(ts->tv_sec * 1000000 + ts->tv_nsec / 1000,
963 : : async_reply_handle, (void *)(uintptr_t)id) < 0) {
964 : 0 : EAL_LOG(ERR, "Fail to set alarm for request %s:%s",
965 : : dst, req->name);
966 : : ret = -1;
967 : 0 : goto fail;
968 : : }
969 : 0 : TAILQ_INSERT_TAIL(&pending_requests.requests, pending_req, next);
970 : :
971 : 0 : return 0;
972 : 0 : fail:
973 : 0 : free(pending_req);
974 : 0 : free(reply_msg);
975 : 0 : return ret;
976 : : }
977 : :
978 : : static int
979 : 32 : mp_request_sync(const char *dst, struct rte_mp_msg *req,
980 : : struct rte_mp_reply *reply, const struct timespec *ts)
981 : : {
982 : : int ret;
983 : : pthread_condattr_t attr;
984 : : struct rte_mp_msg msg, *tmp;
985 : : struct pending_request pending_req, *exist;
986 : :
987 : 32 : pending_req.type = REQUEST_TYPE_SYNC;
988 : 32 : pending_req.reply_received = 0;
989 : : strlcpy(pending_req.dst, dst, sizeof(pending_req.dst));
990 : 32 : pending_req.request = req;
991 : 32 : pending_req.reply = &msg;
992 : 32 : pthread_condattr_init(&attr);
993 : 32 : pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
994 : 32 : pthread_cond_init(&pending_req.sync.cond, &attr);
995 : :
996 : 32 : exist = find_request_by_name(dst, req->name);
997 [ - + ]: 32 : if (exist) {
998 : 0 : EAL_LOG(ERR, "A pending request %s:%s", dst, req->name);
999 : 0 : rte_errno = EEXIST;
1000 : 0 : return -1;
1001 : : }
1002 : :
1003 : 32 : ret = send_msg(dst, req, MP_REQ);
1004 [ + + ]: 32 : if (ret < 0) {
1005 : 1 : EAL_LOG(ERR, "Fail to send request %s:%s",
1006 : : dst, req->name);
1007 : 1 : return -1;
1008 [ + - ]: 31 : } else if (ret == 0)
1009 : : return 0;
1010 : :
1011 : 31 : TAILQ_INSERT_TAIL(&pending_requests.requests, &pending_req, next);
1012 : :
1013 : 31 : reply->nb_sent++;
1014 : :
1015 : : do {
1016 : 31 : ret = pthread_cond_timedwait(&pending_req.sync.cond,
1017 : : &pending_requests.lock, ts);
1018 [ - + ]: 31 : } while (ret != 0 && ret != ETIMEDOUT);
1019 : :
1020 [ - + ]: 31 : TAILQ_REMOVE(&pending_requests.requests, &pending_req, next);
1021 : :
1022 [ - + ]: 31 : if (pending_req.reply_received == 0) {
1023 : 0 : EAL_LOG(ERR, "Fail to recv reply for request %s:%s",
1024 : : dst, req->name);
1025 : 0 : rte_errno = ETIMEDOUT;
1026 : 0 : return -1;
1027 : : }
1028 [ - + ]: 31 : if (pending_req.reply_received == -1) {
1029 : 0 : EAL_LOG(DEBUG, "Asked to ignore response");
1030 : : /* not receiving this message is not an error, so decrement
1031 : : * number of sent messages
1032 : : */
1033 : 0 : reply->nb_sent--;
1034 : 0 : return 0;
1035 : : }
1036 : :
1037 : 31 : tmp = realloc(reply->msgs, sizeof(msg) * (reply->nb_received + 1));
1038 [ - + ]: 31 : if (!tmp) {
1039 : 0 : EAL_LOG(ERR, "Fail to alloc reply for request %s:%s",
1040 : : dst, req->name);
1041 : 0 : rte_errno = ENOMEM;
1042 : 0 : return -1;
1043 : : }
1044 : 31 : memcpy(&tmp[reply->nb_received], &msg, sizeof(msg));
1045 : 31 : reply->msgs = tmp;
1046 : 31 : reply->nb_received++;
1047 : 31 : return 0;
1048 : : }
1049 : :
1050 : : RTE_EXPORT_SYMBOL(rte_mp_request_sync)
1051 : : int
1052 : 218 : rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply,
1053 : : const struct timespec *ts)
1054 : : {
1055 : : int dir_fd, ret = -1;
1056 : : DIR *mp_dir;
1057 : : struct dirent *ent;
1058 : : struct timespec now, end;
1059 : :
1060 : 218 : EAL_LOG(DEBUG, "request: %s", req->name);
1061 : :
1062 : 218 : reply->nb_sent = 0;
1063 : 218 : reply->nb_received = 0;
1064 : 218 : reply->msgs = NULL;
1065 : :
1066 [ - + ]: 218 : if (check_input(req) != 0)
1067 : 0 : goto end;
1068 : :
1069 [ + + ]: 218 : if (eal_mp_is_disabled()) {
1070 : 9 : EAL_LOG(DEBUG, "Multi-process support is disabled");
1071 : 9 : rte_errno = ENOTSUP;
1072 : 9 : return -1;
1073 : : }
1074 : :
1075 [ - + ]: 209 : if (clock_gettime(CLOCK_MONOTONIC, &now) < 0) {
1076 : 0 : EAL_LOG(ERR, "Failed to get current time");
1077 : 0 : rte_errno = errno;
1078 : 0 : goto end;
1079 : : }
1080 : :
1081 : 209 : end.tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
1082 : 209 : end.tv_sec = now.tv_sec + ts->tv_sec +
1083 : 209 : (now.tv_nsec + ts->tv_nsec) / 1000000000;
1084 : :
1085 : : /* for secondary process, send request to the primary process only */
1086 [ + + ]: 209 : if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1087 : 32 : pthread_mutex_lock(&pending_requests.lock);
1088 : 32 : ret = mp_request_sync(eal_mp_socket_path(), req, reply, &end);
1089 : 32 : pthread_mutex_unlock(&pending_requests.lock);
1090 : 32 : goto end;
1091 : : }
1092 : :
1093 : : /* for primary process, broadcast request, and collect reply 1 by 1 */
1094 : 177 : mp_dir = opendir(mp_dir_path);
1095 [ - + ]: 177 : if (!mp_dir) {
1096 : 0 : EAL_LOG(ERR, "Unable to open directory %s", mp_dir_path);
1097 : 0 : rte_errno = errno;
1098 : 0 : goto end;
1099 : : }
1100 : :
1101 : 177 : dir_fd = dirfd(mp_dir);
1102 : : /* lock the directory to prevent processes spinning up while we send */
1103 [ - + ]: 177 : if (flock(dir_fd, LOCK_SH)) {
1104 : 0 : EAL_LOG(ERR, "Unable to lock directory %s",
1105 : : mp_dir_path);
1106 : 0 : rte_errno = errno;
1107 : 0 : goto close_end;
1108 : : }
1109 : :
1110 : 177 : pthread_mutex_lock(&pending_requests.lock);
1111 [ + + ]: 1858 : while ((ent = readdir(mp_dir))) {
1112 : : char path[UNIX_PATH_MAX];
1113 : :
1114 [ + - ]: 1681 : if (fnmatch(mp_filter, ent->d_name, 0) != 0)
1115 : 1681 : continue;
1116 : :
1117 [ # # ]: 0 : if (snprintf(path, sizeof(path), "%s/%s", mp_dir_path, ent->d_name)
1118 : : >= (int)sizeof(path)) {
1119 : 0 : EAL_LOG(ERR, "Unix domain socket path '%s/%s' too long", mp_dir_path,
1120 : : ent->d_name);
1121 : 0 : rte_errno = ENAMETOOLONG;
1122 : 0 : goto unlock_end;
1123 : : }
1124 : :
1125 : : /* unlocks the mutex while waiting for response,
1126 : : * locks on receive
1127 : : */
1128 [ # # ]: 0 : if (mp_request_sync(path, req, reply, &end))
1129 : 0 : goto unlock_end;
1130 : : }
1131 : : ret = 0;
1132 : :
1133 : 177 : unlock_end:
1134 : 177 : pthread_mutex_unlock(&pending_requests.lock);
1135 : : /* unlock the directory */
1136 : 177 : flock(dir_fd, LOCK_UN);
1137 : :
1138 : 177 : close_end:
1139 : : /* dir_fd automatically closed on closedir */
1140 : 177 : closedir(mp_dir);
1141 : :
1142 : 209 : end:
1143 [ + + ]: 209 : if (ret) {
1144 : 1 : free(reply->msgs);
1145 : 1 : reply->nb_received = 0;
1146 : 1 : reply->msgs = NULL;
1147 : : }
1148 : : return ret;
1149 : : }
1150 : :
1151 : : RTE_EXPORT_SYMBOL(rte_mp_request_async)
1152 : : int
1153 : 0 : rte_mp_request_async(struct rte_mp_msg *req, const struct timespec *ts,
1154 : : rte_mp_async_reply_t clb)
1155 : : {
1156 : : struct rte_mp_msg *copy;
1157 : : struct pending_request *dummy;
1158 : : struct async_request_param *param;
1159 : : struct rte_mp_reply *reply;
1160 : : int dir_fd, ret = 0;
1161 : : DIR *mp_dir;
1162 : : struct dirent *ent;
1163 : : struct timespec now;
1164 : : struct timespec *end;
1165 : : bool dummy_used = false;
1166 : :
1167 : 0 : EAL_LOG(DEBUG, "request: %s", req->name);
1168 : :
1169 [ # # ]: 0 : if (check_input(req) != 0)
1170 : : return -1;
1171 : :
1172 [ # # ]: 0 : if (eal_mp_is_disabled()) {
1173 : 0 : EAL_LOG(DEBUG, "Multi-process support is disabled");
1174 : 0 : rte_errno = ENOTSUP;
1175 : 0 : return -1;
1176 : : }
1177 : :
1178 [ # # ]: 0 : if (clock_gettime(CLOCK_MONOTONIC, &now) < 0) {
1179 : 0 : EAL_LOG(ERR, "Failed to get current time");
1180 : 0 : rte_errno = errno;
1181 : 0 : return -1;
1182 : : }
1183 : 0 : copy = calloc(1, sizeof(*copy));
1184 : 0 : dummy = calloc(1, sizeof(*dummy));
1185 : 0 : param = calloc(1, sizeof(*param));
1186 [ # # # # ]: 0 : if (copy == NULL || dummy == NULL || param == NULL) {
1187 : 0 : EAL_LOG(ERR, "Failed to allocate memory for async reply");
1188 : 0 : rte_errno = ENOMEM;
1189 : 0 : goto fail;
1190 : : }
1191 : :
1192 : : /* copy message */
1193 : : memcpy(copy, req, sizeof(*copy));
1194 : :
1195 : 0 : param->n_responses_processed = 0;
1196 : 0 : param->clb = clb;
1197 : : end = ¶m->end;
1198 : : reply = ¶m->user_reply;
1199 : :
1200 : 0 : end->tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
1201 : 0 : end->tv_sec = now.tv_sec + ts->tv_sec +
1202 : 0 : (now.tv_nsec + ts->tv_nsec) / 1000000000;
1203 : 0 : reply->nb_sent = 0;
1204 : 0 : reply->nb_received = 0;
1205 : 0 : reply->msgs = NULL;
1206 : :
1207 : : /* we have to lock the request queue here, as we will be adding a bunch
1208 : : * of requests to the queue at once, and some of the replies may arrive
1209 : : * before we add all of the requests to the queue.
1210 : : */
1211 : 0 : pthread_mutex_lock(&pending_requests.lock);
1212 : :
1213 : : /* we have to ensure that callback gets triggered even if we don't send
1214 : : * anything, therefore earlier we have allocated a dummy request. fill
1215 : : * it, and put it on the queue if we don't send any requests.
1216 : : */
1217 : 0 : dummy->type = REQUEST_TYPE_ASYNC;
1218 : 0 : dummy->id = ++next_request_id;
1219 : 0 : dummy->request = copy;
1220 : 0 : dummy->reply = NULL;
1221 : 0 : dummy->async.param = param;
1222 : 0 : dummy->reply_received = 1; /* short-circuit the timeout */
1223 : :
1224 : : /* for secondary process, send request to the primary process only */
1225 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1226 : 0 : ret = mp_request_async(eal_mp_socket_path(), copy, param, ts);
1227 : :
1228 : : /* if we didn't send anything, put dummy request on the queue
1229 : : * and set a minimum-delay alarm so the callback fires immediately.
1230 : : */
1231 [ # # # # ]: 0 : if (ret == 0 && reply->nb_sent == 0) {
1232 : 0 : TAILQ_INSERT_TAIL(&pending_requests.requests, dummy, next);
1233 : : dummy_used = true;
1234 [ # # ]: 0 : if (rte_eal_alarm_set(1, async_reply_handle,
1235 : 0 : (void *)(uintptr_t)dummy->id) < 0) {
1236 : 0 : EAL_LOG(ERR, "Fail to set alarm for dummy request");
1237 : : /* roll back the changes */
1238 [ # # ]: 0 : TAILQ_REMOVE(&pending_requests.requests, dummy, next);
1239 : : dummy_used = false;
1240 : : ret = -1;
1241 : 0 : goto unlock_fail;
1242 : : }
1243 : : }
1244 : :
1245 : 0 : pthread_mutex_unlock(&pending_requests.lock);
1246 : :
1247 : : /* if we couldn't send anything, clean up */
1248 [ # # ]: 0 : if (ret != 0)
1249 : 0 : goto fail;
1250 [ # # ]: 0 : if (!dummy_used)
1251 : 0 : free(dummy);
1252 : 0 : return 0;
1253 : : }
1254 : :
1255 : : /* for primary process, broadcast request */
1256 : 0 : mp_dir = opendir(mp_dir_path);
1257 [ # # ]: 0 : if (!mp_dir) {
1258 : 0 : EAL_LOG(ERR, "Unable to open directory %s", mp_dir_path);
1259 : 0 : rte_errno = errno;
1260 : 0 : goto unlock_fail;
1261 : : }
1262 : 0 : dir_fd = dirfd(mp_dir);
1263 : :
1264 : : /* lock the directory to prevent processes spinning up while we send */
1265 [ # # ]: 0 : if (flock(dir_fd, LOCK_SH)) {
1266 : 0 : EAL_LOG(ERR, "Unable to lock directory %s",
1267 : : mp_dir_path);
1268 : 0 : rte_errno = errno;
1269 : 0 : goto closedir_fail;
1270 : : }
1271 : :
1272 [ # # ]: 0 : while ((ent = readdir(mp_dir))) {
1273 : : char path[UNIX_PATH_MAX];
1274 : :
1275 [ # # ]: 0 : if (fnmatch(mp_filter, ent->d_name, 0) != 0)
1276 : 0 : continue;
1277 : :
1278 [ # # ]: 0 : if (snprintf(path, sizeof(path), "%s/%s", mp_dir_path, ent->d_name)
1279 : : >= (int)sizeof(path)) {
1280 : 0 : EAL_LOG(ERR, "Unix domain path %s/%s too long", mp_dir_path, ent->d_name);
1281 : : ret = -1;
1282 [ # # ]: 0 : } else if (mp_request_async(path, copy, param, ts))
1283 : : ret = -1;
1284 : : }
1285 : :
1286 : : /*
1287 : : * On partial failure, roll back all queued requests. We hold the lock
1288 : : * so no one else touches the queue. All requests in this batch share
1289 : : * the same param pointer. Stale alarms will fire and harmlessly find
1290 : : * nothing via ID-based lookup.
1291 : : */
1292 [ # # # # ]: 0 : if (ret != 0 && reply->nb_sent > 0) {
1293 : : struct pending_request *r, *tmp;
1294 : :
1295 [ # # ]: 0 : RTE_TAILQ_FOREACH_SAFE(r, &pending_requests.requests, next, tmp) {
1296 [ # # # # ]: 0 : if (r->type == REQUEST_TYPE_ASYNC && r->async.param == param) {
1297 [ # # ]: 0 : TAILQ_REMOVE(&pending_requests.requests, r, next);
1298 : 0 : free(r->reply);
1299 : : /* r->request == copy, freed below after the loop */
1300 : 0 : free(r);
1301 : : }
1302 : : }
1303 : : /* requests on the queue were removed so keep things consistent */
1304 : 0 : reply->nb_sent = 0;
1305 : : }
1306 : :
1307 : : /* if we didn't send anything, put dummy request on the queue
1308 : : * and set a minimum-delay alarm so the callback fires immediately.
1309 : : */
1310 [ # # # # ]: 0 : if (ret == 0 && reply->nb_sent == 0) {
1311 [ # # ]: 0 : TAILQ_INSERT_HEAD(&pending_requests.requests, dummy, next);
1312 : : dummy_used = true;
1313 : :
1314 [ # # ]: 0 : if (rte_eal_alarm_set(1, async_reply_handle,
1315 : 0 : (void *)(uintptr_t)dummy->id) < 0) {
1316 : 0 : EAL_LOG(ERR, "Fail to set alarm for dummy request");
1317 : : /* roll back the changes */
1318 [ # # ]: 0 : TAILQ_REMOVE(&pending_requests.requests, dummy, next);
1319 : : dummy_used = false;
1320 : : ret = -1;
1321 : 0 : goto closedir_fail;
1322 : : }
1323 : : }
1324 : :
1325 : : /* finally, unlock the queue */
1326 : 0 : pthread_mutex_unlock(&pending_requests.lock);
1327 : :
1328 : : /* unlock the directory */
1329 : 0 : flock(dir_fd, LOCK_UN);
1330 : :
1331 : : /* dir_fd automatically closed on closedir */
1332 : 0 : closedir(mp_dir);
1333 : :
1334 : : /* if dummy was unused, free it */
1335 [ # # ]: 0 : if (!dummy_used)
1336 : 0 : free(dummy);
1337 : : /* if nothing was sent, nobody owns copy/param */
1338 [ # # ]: 0 : if (ret != 0) {
1339 : 0 : free(param);
1340 : 0 : free(copy);
1341 : : }
1342 : :
1343 : : return ret;
1344 : 0 : closedir_fail:
1345 : 0 : closedir(mp_dir);
1346 : 0 : unlock_fail:
1347 : 0 : pthread_mutex_unlock(&pending_requests.lock);
1348 : 0 : fail:
1349 : 0 : free(dummy);
1350 : 0 : free(param);
1351 : 0 : free(copy);
1352 : 0 : return -1;
1353 : : }
1354 : :
1355 : : RTE_EXPORT_SYMBOL(rte_mp_reply)
1356 : : int
1357 : 31 : rte_mp_reply(struct rte_mp_msg *msg, const char *peer)
1358 : : {
1359 : 31 : EAL_LOG(DEBUG, "reply: %s", msg->name);
1360 : :
1361 [ + - ]: 31 : if (check_input(msg) != 0)
1362 : : return -1;
1363 : :
1364 [ - + ]: 31 : if (peer == NULL) {
1365 : 0 : EAL_LOG(ERR, "peer is not specified");
1366 : 0 : rte_errno = EINVAL;
1367 : 0 : return -1;
1368 : : }
1369 : :
1370 [ - + ]: 31 : if (eal_mp_is_disabled()) {
1371 : 0 : EAL_LOG(DEBUG, "Multi-process support is disabled");
1372 : 0 : return 0;
1373 : : }
1374 : :
1375 : 31 : return mp_send(msg, peer, MP_REP);
1376 : : }
1377 : :
1378 : : /* Internally, the status of the mp feature is represented as a three-state:
1379 : : * - "unknown" as long as no secondary process attached to a primary process
1380 : : * and there was no call to rte_mp_disable yet,
1381 : : * - "enabled" as soon as a secondary process attaches to a primary process,
1382 : : * - "disabled" when a primary process successfully called rte_mp_disable,
1383 : : */
1384 : : enum mp_status {
1385 : : MP_STATUS_UNKNOWN,
1386 : : MP_STATUS_DISABLED,
1387 : : MP_STATUS_ENABLED,
1388 : : };
1389 : :
1390 : : static bool
1391 : 172 : set_mp_status(enum mp_status status)
1392 : : {
1393 : 172 : struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1394 : : uint8_t expected;
1395 : : uint8_t desired;
1396 : :
1397 : : expected = MP_STATUS_UNKNOWN;
1398 : 172 : desired = status;
1399 [ + + ]: 172 : if (rte_atomic_compare_exchange_strong_explicit(&mcfg->mp_status, &expected, desired,
1400 : : rte_memory_order_relaxed, rte_memory_order_relaxed))
1401 : : return true;
1402 : :
1403 : 150 : return rte_atomic_load_explicit(&mcfg->mp_status, rte_memory_order_relaxed) == desired;
1404 : : }
1405 : :
1406 : : RTE_EXPORT_SYMBOL(rte_mp_disable)
1407 : : bool
1408 : 140 : rte_mp_disable(void)
1409 : : {
1410 : 140 : return set_mp_status(MP_STATUS_DISABLED);
1411 : : }
1412 : :
1413 : : bool
1414 : 32 : __rte_mp_enable(void)
1415 : : {
1416 : 32 : return set_mp_status(MP_STATUS_ENABLED);
1417 : : }
1418 : :
1419 : : bool
1420 : 1524 : eal_mp_is_disabled(void)
1421 : : {
1422 : 1524 : struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1423 : :
1424 : 1524 : return rte_atomic_load_explicit(&mcfg->mp_status,
1425 : 1524 : rte_memory_order_relaxed) == MP_STATUS_DISABLED;
1426 : : }
|