net/memif: introduce memory interface PMD
[dpdk.git] / drivers / net / memif / memif.h
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  * S2M: direction slave -> master
16  * M2S: direction master -> slave
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_S2M, /**< buffer ring in direction slave -> master */
37         MEMIF_RING_M2S, /**< buffer ring in direction master -> slave */
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   * M2S
60   * Contains master interfaces configuration.
61   */
62 typedef struct __rte_packed {
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_m2s_ring; /**< maximum num of M2S ring */
68         memif_ring_index_t max_s2m_ring; /**< maximum num of S2M rings */
69         memif_log2_ring_size_t max_log2_ring_size; /**< maximum ring size (as log2) */
70 } memif_msg_hello_t;
71
72 /**
73  * S2M
74  * Contains information required to identify interface
75  * to which the slave wants to connect.
76  */
77 typedef struct __rte_packed {
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 } memif_msg_init_t;
84
85 /**
86  * S2M
87  * Request master to add new shared memory region to master interface.
88  * Shared files file descriptor is passed in cmsghdr.
89  */
90 typedef struct __rte_packed {
91         memif_region_index_t index;             /**< shm regions index */
92         memif_region_size_t size;               /**< shm region size */
93 } memif_msg_add_region_t;
94
95 /**
96  * S2M
97  * Request master to add new ring to master interface.
98  */
99 typedef struct __rte_packed {
100         uint16_t flags;                         /**< flags */
101 #define MEMIF_MSG_ADD_RING_FLAG_S2M 1           /**< ring is in S2M 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 } memif_msg_add_ring_t;
108
109 /**
110  * S2M
111  * Finalize connection establishment.
112  */
113 typedef struct __rte_packed {
114         uint8_t if_name[MEMIF_NAME_SZ];         /**< slave interface name */
115 } memif_msg_connect_t;
116
117 /**
118  * M2S
119  * Finalize connection establishment.
120  */
121 typedef struct __rte_packed {
122         uint8_t if_name[MEMIF_NAME_SZ];         /**< master interface name */
123 } memif_msg_connected_t;
124
125 /**
126  * S2M & M2S
127  * Disconnect interfaces.
128  */
129 typedef struct __rte_packed {
130         uint32_t code;                          /**< error code */
131         uint8_t string[96];                     /**< disconnect reason */
132 } memif_msg_disconnect_t;
133
134 typedef struct __rte_packed __rte_aligned(128)
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 } memif_msg_t;
147
148 /*
149  *  Ring and Descriptor Layout
150  */
151
152 /**
153  * Buffer descriptor.
154  */
155 typedef struct __rte_packed {
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 } memif_desc_t;
163
164 #define MEMIF_CACHELINE_ALIGN_MARK(mark) \
165         uint8_t mark[0] __rte_aligned(RTE_CACHE_LINE_SIZE)
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         volatile uint16_t head;                 /**< pointer to ring buffer head */
173         MEMIF_CACHELINE_ALIGN_MARK(cacheline1);
174         volatile 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_ */