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