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 : 712 : create_socket_path(const char *name, char *buf, int len)
137 : : {
138 : 712 : const char *prefix = eal_mp_socket_path();
139 : :
140 [ + + ]: 712 : if (strlen(name) > 0)
141 : 410 : snprintf(buf, len, "%s_%s", prefix, name);
142 : : else
143 : 302 : strlcpy(buf, prefix, len);
144 : 712 : }
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 : 1535 : find_action_entry_by_name(const char *name)
171 : : {
172 : : struct action_entry *entry;
173 : :
174 [ + + ]: 3498 : TAILQ_FOREACH(entry, &action_entry_list, next) {
175 [ + + ]: 2565 : if (strncmp(entry->action_name, name, RTE_MP_MAX_NAME_LEN) == 0)
176 : : break;
177 : : }
178 : :
179 : 1535 : return entry;
180 : : }
181 : :
182 : : static int
183 : 2641 : validate_action_name(const char *name)
184 : : {
185 [ - + ]: 2641 : if (name == NULL) {
186 : 0 : EAL_LOG(ERR, "Action name cannot be NULL");
187 : 0 : rte_errno = EINVAL;
188 : 0 : return -1;
189 : : }
190 [ - + ]: 2641 : 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 [ - + ]: 2641 : 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 : 752 : 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 : 752 : eal_get_internal_configuration();
209 : :
210 [ + - ]: 752 : if (validate_action_name(name) != 0)
211 : : return -1;
212 : :
213 [ + + ]: 752 : 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 : 724 : entry = malloc(sizeof(struct action_entry));
220 [ - + ]: 724 : if (entry == NULL) {
221 : 0 : rte_errno = ENOMEM;
222 : 0 : return -1;
223 : : }
224 : 724 : strlcpy(entry->action_name, name, sizeof(entry->action_name));
225 : 724 : entry->action = action;
226 : :
227 : 724 : pthread_mutex_lock(&mp_mutex_action);
228 [ - + ]: 724 : 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 : 724 : TAILQ_INSERT_TAIL(&action_entry_list, entry, next);
235 : 724 : pthread_mutex_unlock(&mp_mutex_action);
236 : 724 : return 0;
237 : : }
238 : :
239 : : RTE_EXPORT_SYMBOL(rte_mp_action_unregister)
240 : : void
241 : 785 : rte_mp_action_unregister(const char *name)
242 : : {
243 : : struct action_entry *entry;
244 : : const struct internal_config *internal_conf =
245 : 785 : eal_get_internal_configuration();
246 : :
247 [ + - ]: 785 : if (validate_action_name(name) != 0)
248 : : return;
249 : :
250 [ + + ]: 785 : if (internal_conf->no_shconf) {
251 : 30 : EAL_LOG(DEBUG, "No shared files mode enabled, IPC is disabled");
252 : 30 : return;
253 : : }
254 : :
255 : 755 : pthread_mutex_lock(&mp_mutex_action);
256 : 755 : entry = find_action_entry_by_name(name);
257 [ + + ]: 755 : if (entry == NULL) {
258 : 209 : pthread_mutex_unlock(&mp_mutex_action);
259 : 209 : return;
260 : : }
261 [ + + ]: 546 : TAILQ_REMOVE(&action_entry_list, entry, next);
262 : 546 : pthread_mutex_unlock(&mp_mutex_action);
263 : 546 : free(entry);
264 : : }
265 : :
266 : : static int
267 : 264 : 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 : 264 : iov.iov_base = m;
278 : 264 : iov.iov_len = buflen;
279 : :
280 : 264 : msgh.msg_name = s;
281 : 264 : msgh.msg_namelen = sizeof(*s);
282 : 264 : msgh.msg_iov = &iov;
283 : 264 : msgh.msg_iovlen = 1;
284 : 264 : msgh.msg_control = control;
285 : 264 : msgh.msg_controllen = sizeof(control);
286 : :
287 : 264 : retry:
288 : 264 : 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 : 178 : mp_handle(void *arg __rte_unused)
406 : : {
407 : : struct mp_msg_internal msg;
408 : : struct sockaddr_un sa;
409 : : int fd;
410 : :
411 [ + + ]: 287 : while ((fd = rte_atomic_load_explicit(&mp_fd, rte_memory_order_relaxed)) >= 0) {
412 : : int ret;
413 : :
414 : 264 : 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 : 178 : open_socket_fd(void)
567 : : {
568 : : struct sockaddr_un un;
569 : :
570 : 178 : peer_name[0] = '\0';
571 [ + + ]: 178 : if (rte_eal_process_type() == RTE_PROC_SECONDARY)
572 : 27 : snprintf(peer_name, sizeof(peer_name),
573 : : "%d_%"PRIx64, getpid(), rte_rdtsc());
574 : :
575 : 178 : mp_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
576 [ - + ]: 178 : 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 : 178 : un.sun_family = AF_UNIX;
583 : :
584 : 178 : create_socket_path(peer_name, un.sun_path, sizeof(un.sun_path));
585 : :
586 : 178 : unlink(un.sun_path); /* May still exist since last run */
587 : :
588 [ - + ]: 178 : 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 : 178 : EAL_LOG(INFO, "Multi-process socket %s", un.sun_path);
596 : 178 : return mp_fd;
597 : : }
598 : :
599 : : static void
600 : 178 : close_socket_fd(int fd)
601 : : {
602 : : char path[PATH_MAX];
603 : :
604 : 178 : close(fd);
605 : 178 : create_socket_path(peer_name, path, sizeof(path));
606 : 178 : unlink(path);
607 : 178 : }
608 : :
609 : : int
610 : 185 : rte_mp_channel_init(void)
611 : : {
612 : : char path[PATH_MAX];
613 : : int dir_fd;
614 : : const struct internal_config *internal_conf =
615 : 185 : 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 [ + + ]: 185 : 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 : 178 : create_socket_path("*", path, sizeof(path));
628 : 178 : strlcpy(mp_filter, basename(path), sizeof(mp_filter));
629 : :
630 : : /* path may have been modified, so recreate it */
631 : 178 : create_socket_path("*", path, sizeof(path));
632 : 178 : strlcpy(mp_dir_path, dirname(path), sizeof(mp_dir_path));
633 : :
634 : : /* lock the directory */
635 : : dir_fd = open(mp_dir_path, O_RDONLY);
636 [ - + ]: 178 : if (dir_fd < 0) {
637 : 0 : EAL_LOG(ERR, "failed to open %s: %s",
638 : : mp_dir_path, strerror(errno));
639 : 0 : return -1;
640 : : }
641 : :
642 [ - + ]: 178 : if (flock(dir_fd, LOCK_EX)) {
643 : 0 : EAL_LOG(ERR, "failed to lock %s: %s",
644 : : mp_dir_path, strerror(errno));
645 : 0 : close(dir_fd);
646 : 0 : return -1;
647 : : }
648 : :
649 [ - + ]: 178 : if (open_socket_fd() < 0) {
650 : 0 : close(dir_fd);
651 : 0 : return -1;
652 : : }
653 : :
654 [ - + ]: 178 : if (rte_thread_create_internal_control(&mp_handle_tid, "mp-msg",
655 : : mp_handle, NULL) < 0) {
656 : 0 : EAL_LOG(ERR, "failed to create mp thread: %s",
657 : : strerror(errno));
658 : 0 : close(dir_fd);
659 : 0 : close(rte_atomic_exchange_explicit(&mp_fd, -1, rte_memory_order_relaxed));
660 : 0 : return -1;
661 : : }
662 : :
663 : : /* unlock the directory */
664 : 178 : flock(dir_fd, LOCK_UN);
665 : 178 : close(dir_fd);
666 : :
667 : 178 : return 0;
668 : : }
669 : :
670 : : void
671 : 252 : rte_mp_channel_cleanup(void)
672 : : {
673 : : int fd;
674 : :
675 : 252 : fd = rte_atomic_exchange_explicit(&mp_fd, -1, rte_memory_order_relaxed);
676 [ + + ]: 252 : if (fd < 0)
677 : : return;
678 : :
679 : 178 : pthread_cancel((pthread_t)mp_handle_tid.opaque_id);
680 : 178 : rte_thread_join(mp_handle_tid, NULL);
681 : 178 : close_socket_fd(fd);
682 : : }
683 : :
684 : : /**
685 : : * Return -1, as fail to send message and it's caused by the local side.
686 : : * Return 0, as fail to send message and it's caused by the remote side.
687 : : * Return 1, as succeed to send message.
688 : : */
689 : : static int
690 : 111 : send_msg(const char *dst_path, struct rte_mp_msg *msg, int type)
691 : : {
692 : : int snd;
693 : : struct iovec iov;
694 : : struct msghdr msgh;
695 : : struct cmsghdr *cmsg;
696 : : struct sockaddr_un dst;
697 : : struct mp_msg_internal m;
698 : 111 : int fd_size = msg->num_fds * sizeof(int);
699 : 111 : const int32_t control_sz = CMSG_SPACE(fd_size);
700 : : char control[CMSG_SPACE(sizeof(msg->fds))];
701 : :
702 [ + - ]: 111 : m.type = type;
703 : : memcpy(&m.msg, msg, sizeof(*msg));
704 : :
705 : : memset(&dst, 0, sizeof(dst));
706 [ + - ]: 111 : dst.sun_family = AF_UNIX;
707 : : strlcpy(dst.sun_path, dst_path, sizeof(dst.sun_path));
708 : :
709 : : memset(&msgh, 0, sizeof(msgh));
710 : : memset(control, 0, sizeof(control));
711 : :
712 : 111 : iov.iov_base = &m;
713 : 111 : iov.iov_len = sizeof(m) - sizeof(msg->fds);
714 : :
715 : 111 : msgh.msg_name = &dst;
716 : 111 : msgh.msg_namelen = sizeof(dst);
717 : 111 : msgh.msg_iov = &iov;
718 : 111 : msgh.msg_iovlen = 1;
719 : 111 : msgh.msg_control = control;
720 : 111 : msgh.msg_controllen = control_sz;
721 : :
722 [ + - ]: 111 : cmsg = CMSG_FIRSTHDR(&msgh);
723 : 111 : cmsg->cmsg_len = CMSG_LEN(fd_size);
724 : 111 : cmsg->cmsg_level = SOL_SOCKET;
725 : 111 : cmsg->cmsg_type = SCM_RIGHTS;
726 : 111 : memcpy(CMSG_DATA(cmsg), msg->fds, fd_size);
727 : :
728 : : do {
729 : 111 : snd = sendmsg(mp_fd, &msgh, 0);
730 [ + + - + ]: 111 : } while (snd < 0 && errno == EINTR);
731 : :
732 [ + + ]: 111 : if (snd < 0) {
733 : 2 : rte_errno = errno;
734 : : /* Check if it caused by peer process exits */
735 [ - + - - ]: 2 : if (errno == ECONNREFUSED &&
736 : 0 : rte_eal_process_type() == RTE_PROC_PRIMARY) {
737 : 0 : unlink(dst_path);
738 : 0 : return 0;
739 : : }
740 : 2 : EAL_LOG(ERR, "failed to send to (%s) due to %s",
741 : : dst_path, strerror(errno));
742 : 2 : return -1;
743 : : }
744 : :
745 : : return 1;
746 : : }
747 : :
748 : : static int
749 : 56 : mp_send(struct rte_mp_msg *msg, const char *peer, int type)
750 : : {
751 : : int dir_fd, ret = 0;
752 : : DIR *mp_dir;
753 : : struct dirent *ent;
754 : :
755 [ + + + + ]: 56 : if (!peer && (rte_eal_process_type() == RTE_PROC_SECONDARY))
756 : 1 : peer = eal_mp_socket_path();
757 : :
758 [ + + ]: 56 : if (peer) {
759 [ + - ]: 54 : if (send_msg(peer, msg, type) < 0)
760 : : return -1;
761 : : else
762 : 54 : return 0;
763 : : }
764 : :
765 : : /* broadcast to all secondary processes */
766 : 2 : mp_dir = opendir(mp_dir_path);
767 [ - + ]: 2 : if (!mp_dir) {
768 : 0 : EAL_LOG(ERR, "Unable to open directory %s",
769 : : mp_dir_path);
770 : 0 : rte_errno = errno;
771 : 0 : return -1;
772 : : }
773 : :
774 : 2 : dir_fd = dirfd(mp_dir);
775 : : /* lock the directory to prevent processes spinning up while we send */
776 [ - + ]: 2 : if (flock(dir_fd, LOCK_SH)) {
777 : 0 : EAL_LOG(ERR, "Unable to lock directory %s",
778 : : mp_dir_path);
779 : 0 : rte_errno = errno;
780 : 0 : closedir(mp_dir);
781 : 0 : return -1;
782 : : }
783 : :
784 [ + + ]: 37 : while ((ent = readdir(mp_dir))) {
785 : : char path[PATH_MAX];
786 : :
787 [ + + ]: 35 : if (fnmatch(mp_filter, ent->d_name, 0) != 0)
788 : 33 : continue;
789 : :
790 : : snprintf(path, sizeof(path), "%s/%s", mp_dir_path,
791 : : ent->d_name);
792 [ - + ]: 2 : if (send_msg(path, msg, type) < 0)
793 : : ret = -1;
794 : : }
795 : : /* unlock the dir */
796 : 2 : flock(dir_fd, LOCK_UN);
797 : :
798 : : /* dir_fd automatically closed on closedir */
799 : 2 : closedir(mp_dir);
800 : 2 : return ret;
801 : : }
802 : :
803 : : static int
804 : 1104 : check_input(const struct rte_mp_msg *msg)
805 : : {
806 [ - + ]: 1104 : if (msg == NULL) {
807 : 0 : EAL_LOG(ERR, "Msg cannot be NULL");
808 : 0 : rte_errno = EINVAL;
809 : 0 : return -1;
810 : : }
811 : :
812 [ + - ]: 1104 : if (validate_action_name(msg->name) != 0)
813 : : return -1;
814 : :
815 [ - + ]: 1104 : if (msg->len_param < 0) {
816 : 0 : EAL_LOG(ERR, "Message data length is negative");
817 : 0 : rte_errno = EINVAL;
818 : 0 : return -1;
819 : : }
820 : :
821 [ - + ]: 1104 : if (msg->num_fds < 0) {
822 : 0 : EAL_LOG(ERR, "Number of fd's is negative");
823 : 0 : rte_errno = EINVAL;
824 : 0 : return -1;
825 : : }
826 : :
827 [ - + ]: 1104 : if (msg->len_param > RTE_MP_MAX_PARAM_LEN) {
828 : 0 : EAL_LOG(ERR, "Message data is too long");
829 : 0 : rte_errno = E2BIG;
830 : 0 : return -1;
831 : : }
832 : :
833 [ - + ]: 1104 : if (msg->num_fds > RTE_MP_MAX_FD_NUM) {
834 : 0 : EAL_LOG(ERR, "Cannot send more than %d FDs",
835 : : RTE_MP_MAX_FD_NUM);
836 : 0 : rte_errno = E2BIG;
837 : 0 : return -1;
838 : : }
839 : :
840 : : return 0;
841 : : }
842 : :
843 : : RTE_EXPORT_SYMBOL(rte_mp_sendmsg)
844 : : int
845 : 3 : rte_mp_sendmsg(struct rte_mp_msg *msg)
846 : : {
847 : : const struct internal_config *internal_conf =
848 : 3 : eal_get_internal_configuration();
849 : :
850 [ + - ]: 3 : if (check_input(msg) != 0)
851 : : return -1;
852 : :
853 [ - + ]: 3 : if (internal_conf->no_shconf) {
854 : 0 : EAL_LOG(DEBUG, "No shared files mode enabled, IPC is disabled");
855 : 0 : rte_errno = ENOTSUP;
856 : 0 : return -1;
857 : : }
858 : :
859 : 3 : EAL_LOG(DEBUG, "sendmsg: %s", msg->name);
860 : 3 : return mp_send(msg, NULL, MP_MSG);
861 : : }
862 : :
863 : : static int
864 : 1 : mp_request_async(const char *dst, struct rte_mp_msg *req,
865 : : struct async_request_param *param, const struct timespec *ts)
866 : : {
867 : : struct rte_mp_msg *reply_msg;
868 : : struct pending_request *pending_req, *exist;
869 : : int ret = -1;
870 : :
871 : 1 : pending_req = calloc(1, sizeof(*pending_req));
872 : 1 : reply_msg = calloc(1, sizeof(*reply_msg));
873 [ - + ]: 1 : if (pending_req == NULL || reply_msg == NULL) {
874 : 0 : EAL_LOG(ERR, "Could not allocate space for sync request");
875 : 0 : rte_errno = ENOMEM;
876 : : ret = -1;
877 : 0 : goto fail;
878 : : }
879 : :
880 : 1 : pending_req->type = REQUEST_TYPE_ASYNC;
881 : 1 : strlcpy(pending_req->dst, dst, sizeof(pending_req->dst));
882 : 1 : pending_req->request = req;
883 : 1 : pending_req->reply = reply_msg;
884 : 1 : pending_req->async.param = param;
885 : :
886 : : /* queue already locked by caller */
887 : :
888 : 1 : exist = find_pending_request(dst, req->name);
889 [ - + ]: 1 : if (exist) {
890 : 0 : EAL_LOG(ERR, "A pending request %s:%s", dst, req->name);
891 : 0 : rte_errno = EEXIST;
892 : : ret = -1;
893 : 0 : goto fail;
894 : : }
895 : :
896 : 1 : ret = send_msg(dst, req, MP_REQ);
897 [ - + ]: 1 : if (ret < 0) {
898 : 0 : EAL_LOG(ERR, "Fail to send request %s:%s",
899 : : dst, req->name);
900 : : ret = -1;
901 : 0 : goto fail;
902 [ - + ]: 1 : } else if (ret == 0) {
903 : : ret = 0;
904 : 0 : goto fail;
905 : : }
906 : 1 : param->user_reply.nb_sent++;
907 : :
908 : : /* if alarm set fails, we simply ignore the reply */
909 [ - + ]: 1 : if (rte_eal_alarm_set(ts->tv_sec * 1000000 + ts->tv_nsec / 1000,
910 : : async_reply_handle, pending_req) < 0) {
911 : 0 : EAL_LOG(ERR, "Fail to set alarm for request %s:%s",
912 : : dst, req->name);
913 : : ret = -1;
914 : 0 : goto fail;
915 : : }
916 : 1 : TAILQ_INSERT_TAIL(&pending_requests.requests, pending_req, next);
917 : :
918 : 1 : return 0;
919 : 0 : fail:
920 : 0 : free(pending_req);
921 : 0 : free(reply_msg);
922 : 0 : return ret;
923 : : }
924 : :
925 : : static int
926 : 54 : mp_request_sync(const char *dst, struct rte_mp_msg *req,
927 : : struct rte_mp_reply *reply, const struct timespec *ts)
928 : : {
929 : : int ret;
930 : : pthread_condattr_t attr;
931 : : struct rte_mp_msg msg, *tmp;
932 : : struct pending_request pending_req, *exist;
933 : :
934 : 54 : pending_req.type = REQUEST_TYPE_SYNC;
935 : 54 : pending_req.reply_received = 0;
936 : : strlcpy(pending_req.dst, dst, sizeof(pending_req.dst));
937 : 54 : pending_req.request = req;
938 : 54 : pending_req.reply = &msg;
939 : 54 : pthread_condattr_init(&attr);
940 : 54 : pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
941 : 54 : pthread_cond_init(&pending_req.sync.cond, &attr);
942 : :
943 : 54 : exist = find_pending_request(dst, req->name);
944 [ - + ]: 54 : if (exist) {
945 : 0 : EAL_LOG(ERR, "A pending request %s:%s", dst, req->name);
946 : 0 : rte_errno = EEXIST;
947 : 0 : return -1;
948 : : }
949 : :
950 : 54 : ret = send_msg(dst, req, MP_REQ);
951 [ + + ]: 54 : if (ret < 0) {
952 : 2 : EAL_LOG(ERR, "Fail to send request %s:%s",
953 : : dst, req->name);
954 : 2 : return -1;
955 [ + - ]: 52 : } else if (ret == 0)
956 : : return 0;
957 : :
958 : 52 : TAILQ_INSERT_TAIL(&pending_requests.requests, &pending_req, next);
959 : :
960 : 52 : reply->nb_sent++;
961 : :
962 : : do {
963 : 52 : ret = pthread_cond_timedwait(&pending_req.sync.cond,
964 : : &pending_requests.lock, ts);
965 [ - + ]: 52 : } while (ret != 0 && ret != ETIMEDOUT);
966 : :
967 [ - + ]: 52 : TAILQ_REMOVE(&pending_requests.requests, &pending_req, next);
968 : :
969 [ - + ]: 52 : if (pending_req.reply_received == 0) {
970 : 0 : EAL_LOG(ERR, "Fail to recv reply for request %s:%s",
971 : : dst, req->name);
972 : 0 : rte_errno = ETIMEDOUT;
973 : 0 : return -1;
974 : : }
975 [ - + ]: 52 : if (pending_req.reply_received == -1) {
976 : 0 : EAL_LOG(DEBUG, "Asked to ignore response");
977 : : /* not receiving this message is not an error, so decrement
978 : : * number of sent messages
979 : : */
980 : 0 : reply->nb_sent--;
981 : 0 : return 0;
982 : : }
983 : :
984 : 52 : tmp = realloc(reply->msgs, sizeof(msg) * (reply->nb_received + 1));
985 [ - + ]: 52 : if (!tmp) {
986 : 0 : EAL_LOG(ERR, "Fail to alloc reply for request %s:%s",
987 : : dst, req->name);
988 : 0 : rte_errno = ENOMEM;
989 : 0 : return -1;
990 : : }
991 : 52 : memcpy(&tmp[reply->nb_received], &msg, sizeof(msg));
992 : 52 : reply->msgs = tmp;
993 : 52 : reply->nb_received++;
994 : 52 : return 0;
995 : : }
996 : :
997 : : RTE_EXPORT_SYMBOL(rte_mp_request_sync)
998 : : int
999 : 1047 : rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply,
1000 : : const struct timespec *ts)
1001 : : {
1002 : : int dir_fd, ret = -1;
1003 : : DIR *mp_dir;
1004 : : struct dirent *ent;
1005 : : struct timespec now, end;
1006 : : const struct internal_config *internal_conf =
1007 : 1047 : eal_get_internal_configuration();
1008 : :
1009 : 1047 : EAL_LOG(DEBUG, "request: %s", req->name);
1010 : :
1011 : 1047 : reply->nb_sent = 0;
1012 : 1047 : reply->nb_received = 0;
1013 : 1047 : reply->msgs = NULL;
1014 : :
1015 [ - + ]: 1047 : if (check_input(req) != 0)
1016 : 0 : goto end;
1017 : :
1018 [ + + ]: 1047 : if (internal_conf->no_shconf) {
1019 : 3 : EAL_LOG(DEBUG, "No shared files mode enabled, IPC is disabled");
1020 : 3 : rte_errno = ENOTSUP;
1021 : 3 : return -1;
1022 : : }
1023 : :
1024 [ - + ]: 1044 : if (clock_gettime(CLOCK_MONOTONIC, &now) < 0) {
1025 : 0 : EAL_LOG(ERR, "Failed to get current time");
1026 : 0 : rte_errno = errno;
1027 : 0 : goto end;
1028 : : }
1029 : :
1030 : 1044 : end.tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
1031 : 1044 : end.tv_sec = now.tv_sec + ts->tv_sec +
1032 : 1044 : (now.tv_nsec + ts->tv_nsec) / 1000000000;
1033 : :
1034 : : /* for secondary process, send request to the primary process only */
1035 [ + + ]: 1044 : if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1036 : 54 : pthread_mutex_lock(&pending_requests.lock);
1037 : 54 : ret = mp_request_sync(eal_mp_socket_path(), req, reply, &end);
1038 : 54 : pthread_mutex_unlock(&pending_requests.lock);
1039 : 54 : goto end;
1040 : : }
1041 : :
1042 : : /* for primary process, broadcast request, and collect reply 1 by 1 */
1043 : 990 : mp_dir = opendir(mp_dir_path);
1044 [ - + ]: 990 : if (!mp_dir) {
1045 : 0 : EAL_LOG(ERR, "Unable to open directory %s", mp_dir_path);
1046 : 0 : rte_errno = errno;
1047 : 0 : goto end;
1048 : : }
1049 : :
1050 : 990 : dir_fd = dirfd(mp_dir);
1051 : : /* lock the directory to prevent processes spinning up while we send */
1052 [ - + ]: 990 : if (flock(dir_fd, LOCK_SH)) {
1053 : 0 : EAL_LOG(ERR, "Unable to lock directory %s",
1054 : : mp_dir_path);
1055 : 0 : rte_errno = errno;
1056 : 0 : goto close_end;
1057 : : }
1058 : :
1059 : 990 : pthread_mutex_lock(&pending_requests.lock);
1060 [ + + ]: 16764 : while ((ent = readdir(mp_dir))) {
1061 : : char path[PATH_MAX];
1062 : :
1063 [ + - ]: 15774 : if (fnmatch(mp_filter, ent->d_name, 0) != 0)
1064 : 15774 : continue;
1065 : :
1066 : : snprintf(path, sizeof(path), "%s/%s", mp_dir_path,
1067 : : ent->d_name);
1068 : :
1069 : : /* unlocks the mutex while waiting for response,
1070 : : * locks on receive
1071 : : */
1072 [ # # ]: 0 : if (mp_request_sync(path, req, reply, &end))
1073 : 0 : goto unlock_end;
1074 : : }
1075 : : ret = 0;
1076 : :
1077 : 990 : unlock_end:
1078 : 990 : pthread_mutex_unlock(&pending_requests.lock);
1079 : : /* unlock the directory */
1080 : 990 : flock(dir_fd, LOCK_UN);
1081 : :
1082 : 990 : close_end:
1083 : : /* dir_fd automatically closed on closedir */
1084 : 990 : closedir(mp_dir);
1085 : :
1086 : 1044 : end:
1087 [ + + ]: 1044 : if (ret) {
1088 : 2 : free(reply->msgs);
1089 : 2 : reply->nb_received = 0;
1090 : 2 : reply->msgs = NULL;
1091 : : }
1092 : : return ret;
1093 : : }
1094 : :
1095 : : RTE_EXPORT_SYMBOL(rte_mp_request_async)
1096 : : int
1097 : 1 : rte_mp_request_async(struct rte_mp_msg *req, const struct timespec *ts,
1098 : : rte_mp_async_reply_t clb)
1099 : : {
1100 : : struct rte_mp_msg *copy;
1101 : : struct pending_request *dummy;
1102 : : struct async_request_param *param;
1103 : : struct rte_mp_reply *reply;
1104 : : int dir_fd, ret = 0;
1105 : : DIR *mp_dir;
1106 : : struct dirent *ent;
1107 : : struct timespec now;
1108 : : struct timespec *end;
1109 : : bool dummy_used = false;
1110 : : const struct internal_config *internal_conf =
1111 : 1 : eal_get_internal_configuration();
1112 : :
1113 : 1 : EAL_LOG(DEBUG, "request: %s", req->name);
1114 : :
1115 [ + - ]: 1 : if (check_input(req) != 0)
1116 : : return -1;
1117 : :
1118 [ - + ]: 1 : if (internal_conf->no_shconf) {
1119 : 0 : EAL_LOG(DEBUG, "No shared files mode enabled, IPC is disabled");
1120 : 0 : rte_errno = ENOTSUP;
1121 : 0 : return -1;
1122 : : }
1123 : :
1124 [ - + ]: 1 : if (clock_gettime(CLOCK_MONOTONIC, &now) < 0) {
1125 : 0 : EAL_LOG(ERR, "Failed to get current time");
1126 : 0 : rte_errno = errno;
1127 : 0 : return -1;
1128 : : }
1129 : 1 : copy = calloc(1, sizeof(*copy));
1130 : 1 : dummy = calloc(1, sizeof(*dummy));
1131 : 1 : param = calloc(1, sizeof(*param));
1132 [ + - - + ]: 1 : if (copy == NULL || dummy == NULL || param == NULL) {
1133 : 0 : EAL_LOG(ERR, "Failed to allocate memory for async reply");
1134 : 0 : rte_errno = ENOMEM;
1135 : 0 : goto fail;
1136 : : }
1137 : :
1138 : : /* copy message */
1139 : : memcpy(copy, req, sizeof(*copy));
1140 : :
1141 : 1 : param->n_responses_processed = 0;
1142 : 1 : param->clb = clb;
1143 : : end = ¶m->end;
1144 : : reply = ¶m->user_reply;
1145 : :
1146 : 1 : end->tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
1147 : 1 : end->tv_sec = now.tv_sec + ts->tv_sec +
1148 : 1 : (now.tv_nsec + ts->tv_nsec) / 1000000000;
1149 : 1 : reply->nb_sent = 0;
1150 : 1 : reply->nb_received = 0;
1151 : 1 : reply->msgs = NULL;
1152 : :
1153 : : /* we have to lock the request queue here, as we will be adding a bunch
1154 : : * of requests to the queue at once, and some of the replies may arrive
1155 : : * before we add all of the requests to the queue.
1156 : : */
1157 : 1 : pthread_mutex_lock(&pending_requests.lock);
1158 : :
1159 : : /* we have to ensure that callback gets triggered even if we don't send
1160 : : * anything, therefore earlier we have allocated a dummy request. fill
1161 : : * it, and put it on the queue if we don't send any requests.
1162 : : */
1163 : 1 : dummy->type = REQUEST_TYPE_ASYNC;
1164 : 1 : dummy->request = copy;
1165 : 1 : dummy->reply = NULL;
1166 : 1 : dummy->async.param = param;
1167 : 1 : dummy->reply_received = 1; /* short-circuit the timeout */
1168 : :
1169 : : /* for secondary process, send request to the primary process only */
1170 [ - + ]: 1 : if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1171 : 0 : ret = mp_request_async(eal_mp_socket_path(), copy, param, ts);
1172 : :
1173 : : /* if we didn't send anything, put dummy request on the queue */
1174 [ # # # # ]: 0 : if (ret == 0 && reply->nb_sent == 0) {
1175 : 0 : TAILQ_INSERT_TAIL(&pending_requests.requests, dummy,
1176 : : next);
1177 : : dummy_used = true;
1178 : : }
1179 : :
1180 : 0 : pthread_mutex_unlock(&pending_requests.lock);
1181 : :
1182 : : /* if we couldn't send anything, clean up */
1183 [ # # ]: 0 : if (ret != 0)
1184 : 0 : goto fail;
1185 : : return 0;
1186 : : }
1187 : :
1188 : : /* for primary process, broadcast request */
1189 : 1 : mp_dir = opendir(mp_dir_path);
1190 [ - + ]: 1 : if (!mp_dir) {
1191 : 0 : EAL_LOG(ERR, "Unable to open directory %s", mp_dir_path);
1192 : 0 : rte_errno = errno;
1193 : 0 : goto unlock_fail;
1194 : : }
1195 : 1 : dir_fd = dirfd(mp_dir);
1196 : :
1197 : : /* lock the directory to prevent processes spinning up while we send */
1198 [ - + ]: 1 : if (flock(dir_fd, LOCK_SH)) {
1199 : 0 : EAL_LOG(ERR, "Unable to lock directory %s",
1200 : : mp_dir_path);
1201 : 0 : rte_errno = errno;
1202 : 0 : goto closedir_fail;
1203 : : }
1204 : :
1205 [ + + ]: 26 : while ((ent = readdir(mp_dir))) {
1206 : : char path[PATH_MAX];
1207 : :
1208 [ + + ]: 25 : if (fnmatch(mp_filter, ent->d_name, 0) != 0)
1209 : 24 : continue;
1210 : :
1211 : : snprintf(path, sizeof(path), "%s/%s", mp_dir_path,
1212 : : ent->d_name);
1213 : :
1214 [ - + ]: 1 : if (mp_request_async(path, copy, param, ts))
1215 : : ret = -1;
1216 : : }
1217 : : /* if we didn't send anything, put dummy request on the queue */
1218 [ + - - + ]: 1 : if (ret == 0 && reply->nb_sent == 0) {
1219 [ # # ]: 0 : TAILQ_INSERT_HEAD(&pending_requests.requests, dummy, next);
1220 : : dummy_used = true;
1221 : : }
1222 : :
1223 : : /* finally, unlock the queue */
1224 : 1 : pthread_mutex_unlock(&pending_requests.lock);
1225 : :
1226 : : /* unlock the directory */
1227 : 1 : flock(dir_fd, LOCK_UN);
1228 : :
1229 : : /* dir_fd automatically closed on closedir */
1230 : 1 : closedir(mp_dir);
1231 : :
1232 : : /* if dummy was unused, free it */
1233 [ + - ]: 1 : if (!dummy_used)
1234 : 1 : free(dummy);
1235 : :
1236 : : return ret;
1237 : : closedir_fail:
1238 : 0 : closedir(mp_dir);
1239 : 0 : unlock_fail:
1240 : 0 : pthread_mutex_unlock(&pending_requests.lock);
1241 : 0 : fail:
1242 : 0 : free(dummy);
1243 : 0 : free(param);
1244 : 0 : free(copy);
1245 : 0 : return -1;
1246 : : }
1247 : :
1248 : : RTE_EXPORT_SYMBOL(rte_mp_reply)
1249 : : int
1250 : 53 : rte_mp_reply(struct rte_mp_msg *msg, const char *peer)
1251 : : {
1252 : 53 : EAL_LOG(DEBUG, "reply: %s", msg->name);
1253 : : const struct internal_config *internal_conf =
1254 : 53 : eal_get_internal_configuration();
1255 : :
1256 [ + - ]: 53 : if (check_input(msg) != 0)
1257 : : return -1;
1258 : :
1259 [ - + ]: 53 : if (peer == NULL) {
1260 : 0 : EAL_LOG(ERR, "peer is not specified");
1261 : 0 : rte_errno = EINVAL;
1262 : 0 : return -1;
1263 : : }
1264 : :
1265 [ - + ]: 53 : if (internal_conf->no_shconf) {
1266 : 0 : EAL_LOG(DEBUG, "No shared files mode enabled, IPC is disabled");
1267 : 0 : return 0;
1268 : : }
1269 : :
1270 : 53 : return mp_send(msg, peer, MP_REP);
1271 : : }
1272 : :
1273 : : /* Internally, the status of the mp feature is represented as a three-state:
1274 : : * - "unknown" as long as no secondary process attached to a primary process
1275 : : * and there was no call to rte_mp_disable yet,
1276 : : * - "enabled" as soon as a secondary process attaches to a primary process,
1277 : : * - "disabled" when a primary process successfully called rte_mp_disable,
1278 : : */
1279 : : enum mp_status {
1280 : : MP_STATUS_UNKNOWN,
1281 : : MP_STATUS_DISABLED,
1282 : : MP_STATUS_ENABLED,
1283 : : };
1284 : :
1285 : : static bool
1286 : 157 : set_mp_status(enum mp_status status)
1287 : : {
1288 : 157 : struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
1289 : : uint8_t expected;
1290 : : uint8_t desired;
1291 : :
1292 : : expected = MP_STATUS_UNKNOWN;
1293 : 157 : desired = status;
1294 [ + + ]: 157 : if (rte_atomic_compare_exchange_strong_explicit(&mcfg->mp_status, &expected, desired,
1295 : : rte_memory_order_relaxed, rte_memory_order_relaxed))
1296 : : return true;
1297 : :
1298 : 145 : return rte_atomic_load_explicit(&mcfg->mp_status, rte_memory_order_relaxed) == desired;
1299 : : }
1300 : :
1301 : : RTE_EXPORT_SYMBOL(rte_mp_disable)
1302 : : bool
1303 : 130 : rte_mp_disable(void)
1304 : : {
1305 : 130 : return set_mp_status(MP_STATUS_DISABLED);
1306 : : }
1307 : :
1308 : : bool
1309 : 27 : __rte_mp_enable(void)
1310 : : {
1311 : 27 : return set_mp_status(MP_STATUS_ENABLED);
1312 : : }
|