5683cb2cf0b1fdf7ecdf9d7cacbe372c73eb07eb
[vpp.git] / src / vcl / vppcom.h
1 /*
2  * Copyright (c) 2017-2020 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #ifndef included_vppcom_h
17 #define included_vppcom_h
18
19 #ifdef __FreeBSD__
20 #include <sys/types.h>
21 #endif /* __FreeBSD__ */
22 #include <netdb.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <poll.h>
26 #include <sys/epoll.h>
27
28 /* clang-format off */
29
30 #ifdef __cplusplus
31 extern "C"
32 {
33 #endif
34
35 /*
36  * VPPCOM Public API Definitions, Enums, and Data Structures
37  */
38 #define INVALID_SESSION_ID                      ((u32)~0)
39 #define VPPCOM_CONF_DEFAULT                     "/etc/vpp/vcl.conf"
40 #define VPPCOM_ENV_CONF                         "VCL_CONFIG"
41 #define VPPCOM_ENV_DEBUG                        "VCL_DEBUG"
42 #define VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP      "VCL_APP_PROXY_TRANSPORT_TCP"
43 #define VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP      "VCL_APP_PROXY_TRANSPORT_UDP"
44 #define VPPCOM_ENV_APP_NAMESPACE_ID             "VCL_APP_NAMESPACE_ID"
45 #define VPPCOM_ENV_APP_NAMESPACE_SECRET         "VCL_APP_NAMESPACE_SECRET"
46 #define VPPCOM_ENV_APP_SCOPE_LOCAL              "VCL_APP_SCOPE_LOCAL"
47 #define VPPCOM_ENV_APP_SCOPE_GLOBAL             "VCL_APP_SCOPE_GLOBAL"
48 #define VPPCOM_ENV_APP_USE_MQ_EVENTFD           "VCL_APP_USE_MQ_EVENTFD"
49 #define VPPCOM_ENV_VPP_API_SOCKET               "VCL_VPP_API_SOCKET"
50 #define VPPCOM_ENV_VPP_SAPI_SOCKET              "VCL_VPP_SAPI_SOCKET"
51
52 typedef enum vppcom_proto_
53 {
54   VPPCOM_PROTO_TCP = 0,
55   VPPCOM_PROTO_UDP,
56   VPPCOM_PROTO_NONE,
57   VPPCOM_PROTO_TLS,
58   VPPCOM_PROTO_QUIC,
59   VPPCOM_PROTO_DTLS,
60   VPPCOM_PROTO_SRTP,
61 } vppcom_proto_t;
62
63 typedef enum
64 {
65   VPPCOM_IS_IP6 = 0,
66   VPPCOM_IS_IP4,
67 } vppcom_is_ip4_t;
68
69 typedef struct vppcom_endpt_tlv_t_
70 {
71   uint32_t data_type;
72   uint32_t data_len;
73   uint8_t data[0];
74 } vppcom_endpt_tlv_t;
75
76 typedef struct vppcom_endpt_t_
77 {
78   uint8_t unused;               /**< unused */
79   uint8_t is_ip4;               /**< flag set if if ip is ipv4 */
80   uint8_t *ip;                  /**< pointer to ip address */
81   uint16_t port;                /**< transport port */
82   uint64_t unused2;             /**< unused */
83   uint32_t app_tlv_len;         /**< length of app provided tlvs */
84   vppcom_endpt_tlv_t *app_tlvs; /**< array of app provided tlvs */
85 } vppcom_endpt_t;
86
87 #define VCL_UDP_OPTS_BASE (VPPCOM_PROTO_UDP << 16)
88 #define VCL_UDP_SEGMENT   (VCL_UDP_OPTS_BASE + 0)
89
90 /* By convention we'll use 127 for IP since we don't support IP as protocol */
91 #define VCL_IP_OPTS_BASE (127 << 16)
92 #define VCL_IP_PKTINFO   (VCL_IP_OPTS_BASE + 1)
93
94 #define VCL_EP_APP_TLV_LEN(tlv_) (sizeof (vppcom_endpt_tlv_t) + tlv->data_len)
95 #define VCL_EP_APP_TLV_POS(ep_, tlv_) ((void *)ep_->app_tlvs - (void *)tlv_)
96 #define VCL_EP_APP_TLV_LEN_LEFT(ep_, tlv_)                                    \
97   (ep_->app_tlv_len - VCL_EP_APP_TLV_POS (ep_, tlv_))
98 #define VCL_EP_NEXT_APP_TLV(ep_, tlv_)                                        \
99   (VCL_EP_APP_TLV_LEN (tlv_) < VCL_EP_APP_TLV_POS (ep_, tlv_) ? (             \
100        (vppcom_endpt_tlv_t *)((uint8_t *)tlv_ + VCL_EP_APP_TLV_LEN (tlv_)))   \
101                                                               : 0)
102
103 typedef uint32_t vcl_session_handle_t;
104
105 typedef struct vppcom_cert_key_pair_
106 {
107   char *cert;
108   char *key;
109   uint32_t cert_len;
110   uint32_t key_len;
111 } vppcom_cert_key_pair_t;
112
113 typedef enum
114 {
115   VPPCOM_OK = 0,
116   VPPCOM_EAGAIN = -EAGAIN,
117   VPPCOM_EWOULDBLOCK = -EWOULDBLOCK,
118   VPPCOM_EINPROGRESS = -EINPROGRESS,
119   VPPCOM_EFAULT = -EFAULT,
120   VPPCOM_ENOMEM = -ENOMEM,
121   VPPCOM_EINVAL = -EINVAL,
122 #ifdef __linux__
123   VPPCOM_EBADFD = -EBADFD,
124 #else
125   VPPCOM_EBADFD = -EBADF,
126 #endif /* __linux__ */
127   VPPCOM_EAFNOSUPPORT = -EAFNOSUPPORT,
128   VPPCOM_ECONNABORTED = -ECONNABORTED,
129   VPPCOM_ECONNRESET = -ECONNRESET,
130   VPPCOM_ENOTCONN = -ENOTCONN,
131   VPPCOM_ECONNREFUSED = -ECONNREFUSED,
132   VPPCOM_ETIMEDOUT = -ETIMEDOUT,
133   VPPCOM_EEXIST = -EEXIST,
134   VPPCOM_ENOPROTOOPT = -ENOPROTOOPT,
135   VPPCOM_EPIPE = -EPIPE,
136   VPPCOM_ENOENT = -ENOENT,
137   VPPCOM_EADDRINUSE = -EADDRINUSE,
138   VPPCOM_ENOTSUP = -ENOTSUP
139 } vppcom_error_t;
140
141 typedef enum
142 {
143   VPPCOM_ATTR_GET_NREAD,
144   VPPCOM_ATTR_GET_NWRITE,
145   VPPCOM_ATTR_GET_FLAGS,
146   VPPCOM_ATTR_SET_FLAGS,
147   VPPCOM_ATTR_GET_LCL_ADDR,
148   VPPCOM_ATTR_SET_LCL_ADDR,
149   VPPCOM_ATTR_GET_PEER_ADDR,
150   VPPCOM_ATTR_GET_LIBC_EPFD,
151   VPPCOM_ATTR_SET_LIBC_EPFD,
152   VPPCOM_ATTR_GET_PROTOCOL,
153   VPPCOM_ATTR_GET_LISTEN,
154   VPPCOM_ATTR_GET_ERROR,
155   VPPCOM_ATTR_GET_TX_FIFO_LEN,
156   VPPCOM_ATTR_SET_TX_FIFO_LEN,
157   VPPCOM_ATTR_GET_RX_FIFO_LEN,
158   VPPCOM_ATTR_SET_RX_FIFO_LEN,
159   VPPCOM_ATTR_GET_REUSEADDR,
160   VPPCOM_ATTR_SET_REUSEADDR,
161   VPPCOM_ATTR_GET_REUSEPORT,
162   VPPCOM_ATTR_SET_REUSEPORT,
163   VPPCOM_ATTR_GET_BROADCAST,
164   VPPCOM_ATTR_SET_BROADCAST,
165   VPPCOM_ATTR_GET_V6ONLY,
166   VPPCOM_ATTR_SET_V6ONLY,
167   VPPCOM_ATTR_GET_KEEPALIVE,
168   VPPCOM_ATTR_SET_KEEPALIVE,
169   VPPCOM_ATTR_GET_TCP_NODELAY,
170   VPPCOM_ATTR_SET_TCP_NODELAY,
171   VPPCOM_ATTR_GET_TCP_KEEPIDLE,
172   VPPCOM_ATTR_SET_TCP_KEEPIDLE,
173   VPPCOM_ATTR_GET_TCP_KEEPINTVL,
174   VPPCOM_ATTR_SET_TCP_KEEPINTVL,
175   VPPCOM_ATTR_GET_TCP_USER_MSS,
176   VPPCOM_ATTR_SET_TCP_USER_MSS,
177   VPPCOM_ATTR_SET_CONNECTED,
178   VPPCOM_ATTR_SET_CKPAIR,
179   VPPCOM_ATTR_SET_VRF,
180   VPPCOM_ATTR_GET_VRF,
181   VPPCOM_ATTR_GET_DOMAIN,
182   VPPCOM_ATTR_SET_ENDPT_EXT_CFG,
183   VPPCOM_ATTR_SET_DSCP,
184   VPPCOM_ATTR_SET_IP_PKTINFO,
185   VPPCOM_ATTR_GET_IP_PKTINFO,
186   VPPCOM_ATTR_GET_ORIGINAL_DST,
187 } vppcom_attr_op_t;
188
189 typedef struct _vcl_poll
190 {
191   uint32_t fds_ndx;
192   vcl_session_handle_t sh;
193   short events;
194   short revents;
195 } vcl_poll_t;
196
197 typedef struct vppcom_data_segment_
198 {
199   unsigned char *data;
200   uint32_t len;
201 } vppcom_data_segment_t;
202
203 typedef vppcom_data_segment_t vppcom_data_segments_t[2];
204
205 typedef unsigned long vcl_si_set;
206
207 /*
208  * VPPCOM Public API Functions
209  */
210
211 extern int vppcom_app_create (const char *app_name);
212 extern void vppcom_app_destroy (void);
213
214 extern int vppcom_session_create (uint8_t proto, uint8_t is_nonblocking);
215 extern int vppcom_session_shutdown (uint32_t session_handle, int how);
216 extern int vppcom_session_close (uint32_t session_handle);
217 extern int vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep);
218 extern int vppcom_session_listen (uint32_t session_handle, uint32_t q_len);
219
220 extern int vppcom_session_accept (uint32_t session_handle,
221                                   vppcom_endpt_t * client_ep, uint32_t flags);
222
223 extern int vppcom_session_connect (uint32_t session_handle,
224                                    vppcom_endpt_t * server_ep);
225 extern int vppcom_session_stream_connect (uint32_t session_handle,
226                                           uint32_t parent_session_handle);
227 extern int vppcom_session_read (uint32_t session_handle, void *buf, size_t n);
228 extern int vppcom_session_write (uint32_t session_handle, void *buf,
229                                  size_t n);
230 extern int vppcom_session_write_msg (uint32_t session_handle, void *buf,
231                                      size_t n);
232
233 extern int vppcom_select (int n_bits, vcl_si_set * read_map,
234                           vcl_si_set * write_map, vcl_si_set * except_map,
235                           double wait_for_time);
236
237 extern int vppcom_epoll_create (void);
238 extern int vppcom_epoll_ctl (uint32_t vep_handle, int op,
239                              uint32_t session_handle,
240                              struct epoll_event *event);
241 extern int vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
242                               int maxevents, double wait_for_time);
243 extern int vppcom_session_attr (uint32_t session_handle, uint32_t op,
244                                 void *buffer, uint32_t * buflen);
245 extern int vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
246                                     uint32_t buflen, int flags,
247                                     vppcom_endpt_t * ep);
248 extern int vppcom_session_sendto (uint32_t session_handle, void *buffer,
249                                   uint32_t buflen, int flags,
250                                   vppcom_endpt_t * ep);
251 extern int vppcom_poll (vcl_poll_t * vp, uint32_t n_sids,
252                         double wait_for_time);
253 extern int vppcom_mq_epoll_fd (void);
254 extern int vppcom_session_index (vcl_session_handle_t session_handle);
255 extern int vppcom_session_worker (vcl_session_handle_t session_handle);
256
257 extern int vppcom_session_read_segments (uint32_t session_handle,
258                                          vppcom_data_segment_t * ds,
259                                          uint32_t n_segments,
260                                          uint32_t max_bytes);
261 extern void vppcom_session_free_segments (uint32_t session_handle,
262                                           uint32_t n_bytes);
263 extern int vppcom_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair);
264 extern int vppcom_del_cert_key_pair (uint32_t ckpair_index);
265 extern int vppcom_unformat_proto (uint8_t * proto, char *proto_str);
266 extern int vppcom_session_is_connectable_listener (uint32_t session_handle);
267 extern int vppcom_session_listener (uint32_t session_handle);
268 extern int vppcom_session_n_accepted (uint32_t session_handle);
269
270 extern const char *vppcom_proto_str (vppcom_proto_t proto);
271 extern const char *vppcom_retval_str (int retval);
272
273 /**
274  * Request from application to register a new worker
275  *
276  * Expectation is that applications will make use of this after a new pthread
277  * is spawned.
278  */
279 extern int vppcom_worker_register (void);
280
281 /**
282  * Unregister current worker
283  */
284 extern void vppcom_worker_unregister (void);
285
286 /**
287  * Retrieve current worker index
288  */
289 extern int vppcom_worker_index (void);
290
291 /**
292  * Set current worker index
293  */
294 extern void vppcom_worker_index_set (int);
295
296 /**
297  * Returns the current worker's message queues epoll fd
298  *
299  * This only works if vcl is configured to do eventfd based message queue
300  * notifications.
301  */
302 extern int vppcom_worker_mqs_epfd (void);
303
304 /**
305  * Returns Session error
306  *
307  * Application can use this API to find the detailed session error
308  */
309 extern int vppcom_session_get_error (uint32_t session_handle);
310
311 /**
312  * Returns true if current worker is disconnected from vpp
313  *
314  * Application can use this API to check if VPP is disconnected
315  * as long as `use-mq-eventfd` is being set
316  */
317 extern int vppcom_worker_is_detached (void);
318
319 #ifdef __cplusplus
320 }
321 #endif
322 /* clang-format on */
323
324 #endif /* included_vppcom_h */
325
326 /*
327  * fd.io coding-style-patch-verification: ON
328  *
329  * Local Variables:
330  * eval: (c-set-style "gnu")
331  * End:
332  */