memif: complete refactor of socket handling code
[vpp.git] / src / plugins / memif / memif.h
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2017 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17
18 #ifndef _MEMIF_H_
19 #define _MEMIF_H_
20
21 #ifndef MEMIF_CACHELINE_SIZE
22 #define MEMIF_CACHELINE_SIZE 64
23 #endif
24
25 /*
26  *  Type definitions
27  */
28
29 typedef enum memif_msg_type
30 {
31   MEMIF_MSG_TYPE_NONE = 0,
32   MEMIF_MSG_TYPE_ACK = 1,
33   MEMIF_MSG_TYPE_HELLO = 2,
34   MEMIF_MSG_TYPE_INIT = 3,
35   MEMIF_MSG_TYPE_ADD_REGION = 4,
36   MEMIF_MSG_TYPE_ADD_RING = 5,
37   MEMIF_MSG_TYPE_CONNECT = 6,
38   MEMIF_MSG_TYPE_CONNECTED = 7,
39   MEMIF_MSG_TYPE_DISCONNECT = 8,
40 } memif_msg_type_t;
41
42 typedef enum
43 {
44   MEMIF_RING_S2M = 0,
45   MEMIF_RING_M2S = 1
46 } memif_ring_type_t;
47
48 typedef enum
49 {
50   MEMIF_INTERFACE_MODE_ETHERNET = 0,
51   MEMIF_INTERFACE_MODE_IP = 1,
52   MEMIF_INTERFACE_MODE_PUNT_INJECT = 2,
53 } memif_interface_mode_t;
54
55 typedef uint16_t memif_region_index_t;
56 typedef uint16_t memif_ring_index_t;
57 typedef uint32_t memif_interface_id_t;
58 typedef uint16_t memif_version_t;
59
60 /*
61  *  Socket messages
62  */
63
64 typedef struct __attribute__ ((packed))
65 {
66   uint8_t name[32];
67   memif_version_t min_version;
68   memif_version_t max_version;
69   memif_region_index_t max_region;
70   memif_ring_index_t max_m2s_ring;
71   memif_ring_index_t max_s2m_ring;
72   uint8_t max_log2_ring_size;
73 } memif_msg_hello_t;
74
75 typedef struct __attribute__ ((packed))
76 {
77   memif_version_t version;
78   memif_interface_id_t id;
79   memif_interface_mode_t mode:8;
80   uint8_t secret[24];
81   uint8_t name[32];
82 } memif_msg_init_t;
83
84 typedef struct __attribute__ ((packed))
85 {
86   memif_region_index_t index;
87   uint32_t size;
88 } memif_msg_add_region_t;
89
90 typedef struct __attribute__ ((packed))
91 {
92   uint16_t flags;
93 #define MEMIF_MSG_ADD_RING_FLAG_S2M     (1 << 0)
94   memif_ring_index_t index;
95   memif_region_index_t region;
96   uint32_t offset;
97   uint8_t log2_ring_size;
98 } memif_msg_add_ring_t;
99
100 typedef struct __attribute__ ((packed))
101 {
102   uint8_t if_name[32];
103 } memif_msg_connect_t;
104
105 typedef struct __attribute__ ((packed))
106 {
107   uint8_t if_name[32];
108 } memif_msg_connected_t;
109
110 typedef struct __attribute__ ((packed))
111 {
112   uint32_t code;
113   uint8_t string[96];
114 } memif_msg_disconnect_t;
115
116 typedef struct __attribute__ ((packed, aligned (128)))
117 {
118   memif_msg_type_t type:16;
119   union
120   {
121     memif_msg_hello_t hello;
122     memif_msg_init_t init;
123     memif_msg_add_region_t add_region;
124     memif_msg_add_ring_t add_ring;
125     memif_msg_connect_t connect;
126     memif_msg_connected_t connected;
127     memif_msg_disconnect_t disconnect;
128   };
129 } memif_msg_t;
130
131 _Static_assert (sizeof (memif_msg_t) == 128,
132                 "Size of memif_msg_t must be 128");
133
134 /*
135  *  Ring and Descriptor Layout
136  */
137
138 typedef struct __attribute__ ((packed))
139 {
140   uint16_t flags;
141 #define MEMIF_DESC_FLAG_NEXT (1 << 0)
142   memif_region_index_t region;
143   uint32_t buffer_length;
144   uint32_t length;
145   uint8_t reserved[4];
146   uint64_t offset;
147   uint64_t metadata;
148 } memif_desc_t;
149
150 _Static_assert (sizeof (memif_desc_t) == 32,
151                 "Size of memif_dsct_t must be 32");
152
153 #define MEMIF_CACHELINE_ALIGN_MARK(mark) \
154   uint8_t mark[0] __attribute__((aligned(MEMIF_CACHELINE_SIZE)))
155
156 typedef struct
157 {
158   MEMIF_CACHELINE_ALIGN_MARK (cacheline0);
159   uint32_t cookie;
160   uint16_t flags;
161 #define MEMIF_RING_FLAG_MASK_INT 1
162   volatile uint16_t head;
163     MEMIF_CACHELINE_ALIGN_MARK (cacheline1);
164   volatile uint16_t tail;
165     MEMIF_CACHELINE_ALIGN_MARK (cacheline2);
166   memif_desc_t desc[0];
167 } memif_ring_t;
168
169 #endif /* _MEMIF_H_ */
170
171 /*
172  * fd.io coding-style-patch-verification: ON
173  *
174  * Local Variables:
175  * eval: (c-set-style "gnu")
176  * End:
177  */