1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016 Intel Corporation
7 #include <rte_common.h>
9 #include <rte_keepalive.h>
13 struct rte_keepalive_shm *rte_keepalive_shm_create(void)
17 struct rte_keepalive_shm *ka_shm;
19 /* If any existing object is not unlinked, it makes it all too easy
20 * for clients to end up with stale shared memory blocks when
21 * restarted. Unlinking makes sure subsequent shm_open by clients
22 * will get the new block mapped below.
24 if (shm_unlink(RTE_KEEPALIVE_SHM_NAME) == -1 && errno != ENOENT)
25 printf("Warning: Error unlinking stale %s (%s)\n",
26 RTE_KEEPALIVE_SHM_NAME, strerror(errno));
28 fd = shm_open(RTE_KEEPALIVE_SHM_NAME,
29 O_CREAT | O_TRUNC | O_RDWR, 0666);
32 "Failed to open %s as SHM (%s)\n",
33 RTE_KEEPALIVE_SHM_NAME,
35 else if (ftruncate(fd, sizeof(struct rte_keepalive_shm)) != 0)
37 "Failed to resize SHM (%s)\n", strerror(errno));
39 ka_shm = (struct rte_keepalive_shm *) mmap(
40 0, sizeof(struct rte_keepalive_shm),
41 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
43 if (ka_shm == MAP_FAILED)
45 "Failed to mmap SHM (%s)\n", strerror(errno));
47 memset(ka_shm, 0, sizeof(struct rte_keepalive_shm));
49 /* Initialize the semaphores for IPC/SHM use */
50 if (sem_init(&ka_shm->core_died, 1, 0) != 0) {
52 "Failed to setup SHM semaphore (%s)\n",
55 sizeof(struct rte_keepalive_shm));
59 /* Set all cores to 'not present' */
61 idx_core < RTE_KEEPALIVE_MAXCORES;
63 ka_shm->core_state[idx_core] =
65 ka_shm->core_last_seen_times[idx_core] = 0;
74 void rte_keepalive_relayed_state(struct rte_keepalive_shm *shm,
75 const int id_core, const enum rte_keepalive_state core_state,
76 __rte_unused uint64_t last_alive)
80 shm->core_state[id_core] = core_state;
81 shm->core_last_seen_times[id_core] = last_alive;
83 if (core_state == RTE_KEEPALIVE_SHM_DEAD) {
84 /* Since core has died, also signal ka_agent.
86 * Limit number of times semaphore can be incremented, in case
87 * ka_agent is not active.
89 if (sem_getvalue(&shm->core_died, &count) == -1) {
90 RTE_LOG(INFO, EAL, "Semaphore check failed(%s)\n",
97 if (sem_post(&shm->core_died) != 0)
99 "Failed to increment semaphore (%s)\n",
104 void rte_keepalive_shm_cleanup(struct rte_keepalive_shm *ka_shm)
106 if (shm_unlink(RTE_KEEPALIVE_SHM_NAME) == -1 && errno != ENOENT)
107 printf("Warning: Error unlinking %s (%s)\n",
108 RTE_KEEPALIVE_SHM_NAME, strerror(errno));
110 if (ka_shm && munmap(ka_shm, sizeof(struct rte_keepalive_shm)) != 0)
111 printf("Warning: munmap() failed\n");