summaryrefslogtreecommitdiff
path: root/drivers/net/memif/rte_eth_memif.h
blob: 8269212cb8d6ba4f8557400af1b21dadfe91508d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright 2018-2019 Cisco Systems, Inc.  All rights reserved.
 */

#ifndef _RTE_ETH_MEMIF_H_
#define _RTE_ETH_MEMIF_H_

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif				/* GNU_SOURCE */

#include <sys/queue.h>

#include <rte_ethdev_driver.h>
#include <rte_ether.h>
#include <rte_interrupts.h>

#include "memif.h"

#define ETH_MEMIF_DEFAULT_SOCKET_FILENAME	"/run/memif.sock"
#define ETH_MEMIF_DEFAULT_RING_SIZE		10
#define ETH_MEMIF_DEFAULT_PKT_BUFFER_SIZE	2048

#define ETH_MEMIF_MAX_NUM_Q_PAIRS		255
#define ETH_MEMIF_MAX_LOG2_RING_SIZE		14
#define ETH_MEMIF_MAX_REGION_NUM		256

#define ETH_MEMIF_SHM_NAME_SIZE			32
#define ETH_MEMIF_DISC_STRING_SIZE		96
#define ETH_MEMIF_SECRET_SIZE			24

extern int memif_logtype;

#define MIF_LOG(level, fmt, args...) \
	rte_log(RTE_LOG_ ## level, memif_logtype, \
		"%s(): " fmt "\n", __func__, ##args)

enum memif_role_t {
	MEMIF_ROLE_MASTER,
	MEMIF_ROLE_SLAVE,
};

struct memif_region {
	void *addr;				/**< shared memory address */
	memif_region_size_t region_size;	/**< shared memory size */
	int fd;					/**< shared memory file descriptor */
	uint32_t pkt_buffer_offset;
	/**< offset from 'addr' to first packet buffer */
};

struct memif_queue {
	struct rte_mempool *mempool;		/**< mempool for RX packets */
	struct pmd_internals *pmd;		/**< device internals */

	memif_ring_type_t type;			/**< ring type */
	memif_region_index_t region;		/**< shared memory region index */

	uint16_t in_port;			/**< port id */

	memif_region_offset_t ring_offset;
	/**< ring offset from start of shm region (ring - memif_region.addr) */

	uint16_t last_head;			/**< last ring head */
	uint16_t last_tail;			/**< last ring tail */

	/* rx/tx info */
	uint64_t n_pkts;			/**< number of rx/tx packets */
	uint64_t n_bytes;			/**< number of rx/tx bytes */

	memif_ring_t *ring;			/**< pointer to ring */

	struct rte_intr_handle intr_handle;	/**< interrupt handle */

	memif_log2_ring_size_t log2_ring_size;	/**< log2 of ring size */
};

struct pmd_internals {
	memif_interface_id_t id;		/**< unique id */
	enum memif_role_t role;			/**< device role */
	uint32_t flags;				/**< device status flags */
#define ETH_MEMIF_FLAG_CONNECTING	(1 << 0)
/**< device is connecting */
#define ETH_MEMIF_FLAG_CONNECTED	(1 << 1)
/**< device is connected */
#define ETH_MEMIF_FLAG_ZERO_COPY	(1 << 2)
/**< device is zero-copy enabled */
#define ETH_MEMIF_FLAG_DISABLED		(1 << 3)
/**< device has not been configured and can not accept connection requests */

	char *socket_filename;			/**< pointer to socket filename */
	char secret[ETH_MEMIF_SECRET_SIZE]; /**< secret (optional security parameter) */

	struct memif_control_channel *cc;	/**< control channel */

	/* remote info */
	char remote_name[RTE_DEV_NAME_MAX_LEN];		/**< remote app name */
	char remote_if_name[RTE_DEV_NAME_MAX_LEN];	/**< remote peer name */

	struct {
		memif_log2_ring_size_t log2_ring_size; /**< log2 of ring size */
		uint8_t num_s2m_rings;		/**< number of slave to master rings */
		uint8_t num_m2s_rings;		/**< number of master to slave rings */
		uint16_t pkt_buffer_size;	/**< buffer size */
	} cfg;					/**< Configured parameters (max values) */

	struct {
		memif_log2_ring_size_t log2_ring_size; /**< log2 of ring size */
		uint8_t num_s2m_rings;		/**< number of slave to master rings */
		uint8_t num_m2s_rings;		/**< number of master to slave rings */
		uint16_t pkt_buffer_size;	/**< buffer size */
	} run;
	/**< Parameters used in active connection */

	char local_disc_string[ETH_MEMIF_DISC_STRING_SIZE];
	/**< local disconnect reason */
	char remote_disc_string[ETH_MEMIF_DISC_STRING_SIZE];
	/**< remote disconnect reason */

	struct rte_vdev_device *vdev;		/**< vdev handle */
};

struct pmd_process_private {
	struct memif_region *regions[ETH_MEMIF_MAX_REGION_NUM];
	/**< shared memory regions */
	memif_region_index_t regions_num;	/**< number of regions */
};

/**
 * Unmap shared memory and free regions from memory.
 *
 * @param proc_private
 *   device process private data
 */
void memif_free_regions(struct pmd_process_private *proc_private);

/**
 * Finalize connection establishment process. Map shared memory file
 * (master role), initialize ring queue, set link status up.
 *
 * @param dev
 *   memif device
 * @return
 *   - On success, zero.
 *   - On failure, a negative value.
 */
int memif_connect(struct rte_eth_dev *dev);

/**
 * Create shared memory file and initialize ring queue.
 * Only called by slave when establishing connection
 *
 * @param dev
 *   memif device
 * @return
 *   - On success, zero.
 *   - On failure, a negative value.
 */
int memif_init_regions_and_queues(struct rte_eth_dev *dev);

/**
 * Get memif version string.
 *
 * @return
 *   - memif version string
 */
const char *memif_version(void);

#ifndef MFD_HUGETLB
#ifndef __NR_memfd_create

#if defined __x86_64__
#define __NR_memfd_create 319
#elif defined __x86_32__
#define __NR_memfd_create 1073742143
#elif defined __arm__
#define __NR_memfd_create 385
#elif defined __aarch64__
#define __NR_memfd_create 279
#elif defined __powerpc__
#define __NR_memfd_create 360
#elif defined __i386__
#define __NR_memfd_create 356
#else
#error "__NR_memfd_create unknown for this architecture"
#endif

#endif				/* __NR_memfd_create */

static inline int memfd_create(const char *name, unsigned int flags)
{
	return syscall(__NR_memfd_create, name, flags);
}
#endif				/* MFD_HUGETLB */

#ifndef F_LINUX_SPECIFIC_BASE
#define F_LINUX_SPECIFIC_BASE 1024
#endif

#ifndef MFD_ALLOW_SEALING
#define MFD_ALLOW_SEALING       0x0002U
#endif

#ifndef F_ADD_SEALS
#define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
#define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)

#define F_SEAL_SEAL     0x0001	/* prevent further seals from being set */
#define F_SEAL_SHRINK   0x0002	/* prevent file from shrinking */
#define F_SEAL_GROW     0x0004	/* prevent file from growing */
#define F_SEAL_WRITE    0x0008	/* prevent writes */
#endif

#endif				/* RTE_ETH_MEMIF_H */