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