xref: /dpdk/drivers/net/memif/memif.h (revision e77506397fc8005c5129e22e9e2d15d5876790fd)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2018-2019 Cisco Systems, Inc.  All rights reserved.
3  */
4 
5 #ifndef _MEMIF_H_
6 #define _MEMIF_H_
7 
8 #define MEMIF_COOKIE		0x3E31F20
9 #define MEMIF_VERSION_MAJOR	2
10 #define MEMIF_VERSION_MINOR	0
11 #define MEMIF_VERSION		((MEMIF_VERSION_MAJOR << 8) | MEMIF_VERSION_MINOR)
12 #define MEMIF_NAME_SZ		32
13 
14 /*
15  * C2S: direction client -> server
16  * S2C: direction server -> client
17  */
18 
19 /*
20  *  Type definitions
21  */
22 
23 typedef enum memif_msg_type {
24 	MEMIF_MSG_TYPE_NONE,
25 	MEMIF_MSG_TYPE_ACK,
26 	MEMIF_MSG_TYPE_HELLO,
27 	MEMIF_MSG_TYPE_INIT,
28 	MEMIF_MSG_TYPE_ADD_REGION,
29 	MEMIF_MSG_TYPE_ADD_RING,
30 	MEMIF_MSG_TYPE_CONNECT,
31 	MEMIF_MSG_TYPE_CONNECTED,
32 	MEMIF_MSG_TYPE_DISCONNECT,
33 } memif_msg_type_t;
34 
35 typedef enum {
36 	MEMIF_RING_C2S, /**< buffer ring in direction client -> server */
37 	MEMIF_RING_S2C, /**< buffer ring in direction server -> client */
38 } memif_ring_type_t;
39 
40 typedef enum {
41 	MEMIF_INTERFACE_MODE_ETHERNET,
42 	MEMIF_INTERFACE_MODE_IP,
43 	MEMIF_INTERFACE_MODE_PUNT_INJECT,
44 } memif_interface_mode_t;
45 
46 typedef uint16_t memif_region_index_t;
47 typedef uint32_t memif_region_offset_t;
48 typedef uint64_t memif_region_size_t;
49 typedef uint16_t memif_ring_index_t;
50 typedef uint32_t memif_interface_id_t;
51 typedef uint16_t memif_version_t;
52 typedef uint8_t memif_log2_ring_size_t;
53 
54 /*
55  *  Socket messages
56  */
57 
58  /**
59   * S2C
60   * Contains server interfaces configuration.
61   */
62 typedef struct __rte_packed_begin {
63 	uint8_t name[MEMIF_NAME_SZ]; /**< Client app name. In this case DPDK version */
64 	memif_version_t min_version; /**< lowest supported memif version */
65 	memif_version_t max_version; /**< highest supported memif version */
66 	memif_region_index_t max_region; /**< maximum num of regions */
67 	memif_ring_index_t max_s2c_ring; /**< maximum num of S2C ring */
68 	memif_ring_index_t max_c2s_ring; /**< maximum num of C2S rings */
69 	memif_log2_ring_size_t max_log2_ring_size; /**< maximum ring size (as log2) */
70 } __rte_packed_end memif_msg_hello_t;
71 
72 /**
73  * C2S
74  * Contains information required to identify interface
75  * to which the client wants to connect.
76  */
77 typedef struct __rte_packed_begin {
78 	memif_version_t version;		/**< memif version */
79 	memif_interface_id_t id;		/**< interface id */
80 	memif_interface_mode_t mode:8;		/**< interface mode */
81 	uint8_t secret[24];			/**< optional security parameter */
82 	uint8_t name[MEMIF_NAME_SZ]; /**< Client app name. In this case DPDK version */
83 } __rte_packed_end memif_msg_init_t;
84 
85 /**
86  * C2S
87  * Request server to add new shared memory region to server interface.
88  * Shared files file descriptor is passed in cmsghdr.
89  */
90 typedef struct __rte_packed_begin {
91 	memif_region_index_t index;		/**< shm regions index */
92 	memif_region_size_t size;		/**< shm region size */
93 } __rte_packed_end memif_msg_add_region_t;
94 
95 /**
96  * C2S
97  * Request server to add new ring to server interface.
98  */
99 typedef struct __rte_packed_begin {
100 	uint16_t flags;				/**< flags */
101 #define MEMIF_MSG_ADD_RING_FLAG_C2S 1		/**< ring is in C2S direction */
102 	memif_ring_index_t index;		/**< ring index */
103 	memif_region_index_t region; /**< region index on which this ring is located */
104 	memif_region_offset_t offset;		/**< buffer start offset */
105 	memif_log2_ring_size_t log2_ring_size;	/**< ring size (log2) */
106 	uint16_t private_hdr_size;		/**< used for private metadata */
107 } __rte_packed_end memif_msg_add_ring_t;
108 
109 /**
110  * C2S
111  * Finalize connection establishment.
112  */
113 typedef struct __rte_packed_begin {
114 	uint8_t if_name[MEMIF_NAME_SZ];		/**< client interface name */
115 } __rte_packed_end memif_msg_connect_t;
116 
117 /**
118  * S2C
119  * Finalize connection establishment.
120  */
121 typedef struct __rte_packed_begin {
122 	uint8_t if_name[MEMIF_NAME_SZ];		/**< server interface name */
123 } __rte_packed_end memif_msg_connected_t;
124 
125 /**
126  * C2S & S2C
127  * Disconnect interfaces.
128  */
129 typedef struct __rte_packed_begin {
130 	uint32_t code;				/**< error code */
131 	uint8_t string[96];			/**< disconnect reason */
132 } __rte_packed_end memif_msg_disconnect_t;
133 
134 typedef struct __rte_aligned(128) __rte_packed_begin
135 {
136 	memif_msg_type_t type:16;
137 	union {
138 		memif_msg_hello_t hello;
139 		memif_msg_init_t init;
140 		memif_msg_add_region_t add_region;
141 		memif_msg_add_ring_t add_ring;
142 		memif_msg_connect_t connect;
143 		memif_msg_connected_t connected;
144 		memif_msg_disconnect_t disconnect;
145 	};
146 } __rte_packed_end memif_msg_t;
147 
148 /*
149  *  Ring and Descriptor Layout
150  */
151 
152 /**
153  * Buffer descriptor.
154  */
155 typedef struct __rte_packed_begin {
156 	uint16_t flags;				/**< flags */
157 #define MEMIF_DESC_FLAG_NEXT 1			/**< is chained buffer */
158 	memif_region_index_t region; /**< region index on which the buffer is located */
159 	uint32_t length;			/**< buffer length */
160 	memif_region_offset_t offset;		/**< buffer offset */
161 	uint32_t metadata;
162 } __rte_packed_end memif_desc_t;
163 
164 #define MEMIF_CACHELINE_ALIGN_MARK(mark) \
165 	alignas(RTE_CACHE_LINE_SIZE) RTE_MARKER mark;
166 
167 typedef struct {
168 	MEMIF_CACHELINE_ALIGN_MARK(cacheline0);
169 	uint32_t cookie;			/**< MEMIF_COOKIE */
170 	uint16_t flags;				/**< flags */
171 #define MEMIF_RING_FLAG_MASK_INT 1		/**< disable interrupt mode */
172 	RTE_ATOMIC(uint16_t) head;			/**< pointer to ring buffer head */
173 	MEMIF_CACHELINE_ALIGN_MARK(cacheline1);
174 	RTE_ATOMIC(uint16_t) tail;			/**< pointer to ring buffer tail */
175 	MEMIF_CACHELINE_ALIGN_MARK(cacheline2);
176 	memif_desc_t desc[0];			/**< buffer descriptors */
177 } memif_ring_t;
178 
179 #endif				/* _MEMIF_H_ */
180