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