1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2018-2019 Cisco Systems, Inc. All rights reserved.
5 #ifndef _RTE_ETH_MEMIF_H_
6 #define _RTE_ETH_MEMIF_H_
10 #endif /* GNU_SOURCE */
12 #include <sys/queue.h>
14 #include <rte_ethdev_driver.h>
15 #include <rte_ether.h>
16 #include <rte_interrupts.h>
20 #define ETH_MEMIF_DEFAULT_SOCKET_FILENAME "/run/memif.sock"
21 #define ETH_MEMIF_DEFAULT_RING_SIZE 10
22 #define ETH_MEMIF_DEFAULT_PKT_BUFFER_SIZE 2048
24 #define ETH_MEMIF_MAX_NUM_Q_PAIRS 255
25 #define ETH_MEMIF_MAX_LOG2_RING_SIZE 14
26 #define ETH_MEMIF_MAX_REGION_NUM 256
28 #define ETH_MEMIF_SHM_NAME_SIZE 32
29 #define ETH_MEMIF_DISC_STRING_SIZE 96
30 #define ETH_MEMIF_SECRET_SIZE 24
32 extern int memif_logtype;
34 #define MIF_LOG(level, fmt, args...) \
35 rte_log(RTE_LOG_ ## level, memif_logtype, \
36 "%s(): " fmt "\n", __func__, ##args)
44 void *addr; /**< shared memory address */
45 memif_region_size_t region_size; /**< shared memory size */
46 int fd; /**< shared memory file descriptor */
47 uint32_t pkt_buffer_offset;
48 /**< offset from 'addr' to first packet buffer */
52 struct rte_mempool *mempool; /**< mempool for RX packets */
53 struct pmd_internals *pmd; /**< device internals */
55 memif_ring_type_t type; /**< ring type */
56 memif_region_index_t region; /**< shared memory region index */
58 uint16_t in_port; /**< port id */
60 memif_region_offset_t ring_offset;
61 /**< ring offset from start of shm region (ring - memif_region.addr) */
63 uint16_t last_head; /**< last ring head */
64 uint16_t last_tail; /**< last ring tail */
66 struct rte_mbuf **buffers;
67 /**< Stored mbufs. Used in zero-copy tx. Slave stores transmitted
68 * mbufs to free them once master has received them.
72 uint64_t n_pkts; /**< number of rx/tx packets */
73 uint64_t n_bytes; /**< number of rx/tx bytes */
75 struct rte_intr_handle intr_handle; /**< interrupt handle */
77 memif_log2_ring_size_t log2_ring_size; /**< log2 of ring size */
80 struct pmd_internals {
81 memif_interface_id_t id; /**< unique id */
82 enum memif_role_t role; /**< device role */
83 uint32_t flags; /**< device status flags */
84 #define ETH_MEMIF_FLAG_CONNECTING (1 << 0)
85 /**< device is connecting */
86 #define ETH_MEMIF_FLAG_CONNECTED (1 << 1)
87 /**< device is connected */
88 #define ETH_MEMIF_FLAG_ZERO_COPY (1 << 2)
89 /**< device is zero-copy enabled */
90 #define ETH_MEMIF_FLAG_DISABLED (1 << 3)
91 /**< device has not been configured and can not accept connection requests */
93 char *socket_filename; /**< pointer to socket filename */
94 char secret[ETH_MEMIF_SECRET_SIZE]; /**< secret (optional security parameter) */
96 struct memif_control_channel *cc; /**< control channel */
97 rte_spinlock_t cc_lock; /**< control channel lock */
100 char remote_name[RTE_DEV_NAME_MAX_LEN]; /**< remote app name */
101 char remote_if_name[RTE_DEV_NAME_MAX_LEN]; /**< remote peer name */
104 memif_log2_ring_size_t log2_ring_size; /**< log2 of ring size */
105 uint8_t num_s2m_rings; /**< number of slave to master rings */
106 uint8_t num_m2s_rings; /**< number of master to slave rings */
107 uint16_t pkt_buffer_size; /**< buffer size */
108 } cfg; /**< Configured parameters (max values) */
111 memif_log2_ring_size_t log2_ring_size; /**< log2 of ring size */
112 uint8_t num_s2m_rings; /**< number of slave to master rings */
113 uint8_t num_m2s_rings; /**< number of master to slave rings */
114 uint16_t pkt_buffer_size; /**< buffer size */
116 /**< Parameters used in active connection */
118 char local_disc_string[ETH_MEMIF_DISC_STRING_SIZE];
119 /**< local disconnect reason */
120 char remote_disc_string[ETH_MEMIF_DISC_STRING_SIZE];
121 /**< remote disconnect reason */
124 struct pmd_process_private {
125 struct memif_region *regions[ETH_MEMIF_MAX_REGION_NUM];
126 /**< shared memory regions */
127 memif_region_index_t regions_num; /**< number of regions */
131 * Unmap shared memory and free regions from memory.
133 * @param proc_private
134 * device process private data
136 void memif_free_regions(struct rte_eth_dev *dev);
139 * Finalize connection establishment process. Map shared memory file
140 * (master role), initialize ring queue, set link status up.
145 * - On success, zero.
146 * - On failure, a negative value.
148 int memif_connect(struct rte_eth_dev *dev);
151 * Create shared memory file and initialize ring queue.
152 * Only called by slave when establishing connection
157 * - On success, zero.
158 * - On failure, a negative value.
160 int memif_init_regions_and_queues(struct rte_eth_dev *dev);
163 * Get memif version string.
166 * - memif version string
168 const char *memif_version(void);
171 #ifndef __NR_memfd_create
173 #if defined __x86_64__
174 #define __NR_memfd_create 319
175 #elif defined __x86_32__
176 #define __NR_memfd_create 1073742143
177 #elif defined __arm__
178 #define __NR_memfd_create 385
179 #elif defined __aarch64__
180 #define __NR_memfd_create 279
181 #elif defined __powerpc__
182 #define __NR_memfd_create 360
183 #elif defined __i386__
184 #define __NR_memfd_create 356
186 #error "__NR_memfd_create unknown for this architecture"
189 #endif /* __NR_memfd_create */
191 static inline int memfd_create(const char *name, unsigned int flags)
193 return syscall(__NR_memfd_create, name, flags);
195 #endif /* MFD_HUGETLB */
197 #ifndef F_LINUX_SPECIFIC_BASE
198 #define F_LINUX_SPECIFIC_BASE 1024
201 #ifndef MFD_ALLOW_SEALING
202 #define MFD_ALLOW_SEALING 0x0002U
206 #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
207 #define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
209 #define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */
210 #define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */
211 #define F_SEAL_GROW 0x0004 /* prevent file from growing */
212 #define F_SEAL_WRITE 0x0008 /* prevent writes */
215 #endif /* RTE_ETH_MEMIF_H */