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