xref: /dpdk/examples/l2fwd-keepalive/shm.h (revision 13830b98b2bcf6a8a1aa8edde5132907c053861c)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016 Intel Corporation
3  */
4 
5 #define RTE_KEEPALIVE_SHM_NAME "/dpdk_keepalive_shm_name"
6 
7 #define RTE_KEEPALIVE_SHM_ALIVE 1
8 #define RTE_KEEPALIVE_SHM_DEAD 2
9 
10 #include <fcntl.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <sys/mman.h>
14 #include <sys/stat.h>
15 #include <semaphore.h>
16 #include <rte_keepalive.h>
17 #include <rte_log.h>
18 
19 #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1
20 
21 /**
22  * Keepalive SHM structure.
23  *
24  * The shared memory allocated by the primary is this size, and contains the
25  * information as contained within this struct. A secondary may open the SHM,
26  * and read the contents.
27  */
28 struct rte_keepalive_shm {
29 	/** IPC semaphore. Posted when a core dies */
30 	sem_t core_died;
31 
32 	/**
33 	 * Relayed status of each core.
34 	 */
35 	enum rte_keepalive_state core_state[RTE_KEEPALIVE_MAXCORES];
36 
37 	/**
38 	 * Last-seen-alive timestamps for the cores
39 	 */
40 	uint64_t core_last_seen_times[RTE_KEEPALIVE_MAXCORES];
41 };
42 
43 /**
44  * Create shared host memory keepalive object.
45  * @return
46  *  Pointer to SHM keepalive structure, or NULL on failure.
47  */
48 struct rte_keepalive_shm *rte_keepalive_shm_create(void);
49 
50 /**
51  * Relays state for given core
52  * @param *shm
53  *  Pointer to SHM keepalive structure.
54  * @param id_core
55  *  Id of core
56  * @param core_state
57  *  State of core
58  * @param last_alive
59  *  Last seen timestamp for core
60  */
61 void rte_keepalive_relayed_state(struct rte_keepalive_shm *shm,
62 	const int id_core, const enum rte_keepalive_state core_state,
63 	uint64_t last_alive);
64 
65 /** Shutdown cleanup of shared host memory keepalive object.
66  * @param *shm
67  *  Pointer to SHM keepalive structure. May be NULL.
68  *
69  *  If *shm is NULL, this function will only attempt to remove the
70  *  shared host memory handle and not unmap the underlying memory.
71  */
72 void rte_keepalive_shm_cleanup(struct rte_keepalive_shm *ka_shm);
73