vcl: allow non-blocking connects
[vpp.git] / src / vcl / vppcom.h
1 /*
2  * Copyright (c) 2017-2019 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 #include <netdb.h>
20 #include <errno.h>
21 #include <sys/fcntl.h>
22 #include <sys/poll.h>
23 #include <sys/epoll.h>
24
25 /* *INDENT-OFF* */
26 #ifdef __cplusplus
27 extern "C"
28 {
29 #endif
30 /* *INDENT-ON* */
31
32 /*
33  * VPPCOM Public API Definitions, Enums, and Data Structures
34  */
35 #define INVALID_SESSION_ID                      ((u32)~0)
36 #define VPPCOM_CONF_DEFAULT                     "/etc/vpp/vcl.conf"
37 #define VPPCOM_ENV_CONF                         "VCL_CONFIG"
38 #define VPPCOM_ENV_DEBUG                        "VCL_DEBUG"
39 #define VPPCOM_ENV_API_PREFIX                   "VCL_API_PREFIX"
40 #define VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP      "VCL_APP_PROXY_TRANSPORT_TCP"
41 #define VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP      "VCL_APP_PROXY_TRANSPORT_UDP"
42 #define VPPCOM_ENV_APP_NAMESPACE_ID             "VCL_APP_NAMESPACE_ID"
43 #define VPPCOM_ENV_APP_NAMESPACE_SECRET         "VCL_APP_NAMESPACE_SECRET"
44 #define VPPCOM_ENV_APP_SCOPE_LOCAL              "VCL_APP_SCOPE_LOCAL"
45 #define VPPCOM_ENV_APP_SCOPE_GLOBAL             "VCL_APP_SCOPE_GLOBAL"
46 #define VPPCOM_ENV_VPP_API_SOCKET               "VCL_VPP_API_SOCKET"
47
48 typedef enum
49 {
50   VPPCOM_PROTO_TCP = 0,
51   VPPCOM_PROTO_UDP,
52   VPPCOM_PROTO_SCTP,
53   VPPCOM_PROTO_NONE,
54   VPPCOM_PROTO_TLS,
55   VPPCOM_PROTO_UDPC,
56   VPPCOM_PROTO_QUIC,
57 } vppcom_proto_t;
58
59 static inline char *
60 vppcom_proto_str (vppcom_proto_t proto)
61 {
62   char *proto_str;
63
64   switch (proto)
65     {
66     case VPPCOM_PROTO_TCP:
67       proto_str = "TCP";
68       break;
69     case VPPCOM_PROTO_UDP:
70       proto_str = "UDP";
71       break;
72     case VPPCOM_PROTO_SCTP:
73       proto_str = "SCTP";
74       break;
75     case VPPCOM_PROTO_TLS:
76       proto_str = "TLS";
77       break;
78     case VPPCOM_PROTO_UDPC:
79       proto_str = "UDPC";
80       break;
81     case VPPCOM_PROTO_QUIC:
82       proto_str = "QUIC";
83       break;
84     default:
85       proto_str = "UNKNOWN";
86       break;
87     }
88   return proto_str;
89 }
90
91 static inline int
92 vcl_proto_is_dgram (uint8_t proto)
93 {
94   return proto == VPPCOM_PROTO_UDP || proto == VPPCOM_PROTO_UDPC;
95 }
96
97 typedef enum
98 {
99   VPPCOM_IS_IP6 = 0,
100   VPPCOM_IS_IP4,
101 } vppcom_is_ip4_t;
102
103 typedef struct vppcom_endpt_t_
104 {
105   uint8_t is_cut_thru;
106   uint8_t is_ip4;
107   uint8_t *ip;
108   uint16_t port;
109   uint64_t parent_handle;
110 } vppcom_endpt_t;
111
112 typedef uint32_t vcl_session_handle_t;
113
114 typedef enum
115 {
116   VPPCOM_OK = 0,
117   VPPCOM_EAGAIN = -EAGAIN,
118   VPPCOM_EWOULDBLOCK = -EWOULDBLOCK,
119   VPPCOM_EINPROGRESS = -EINPROGRESS,
120   VPPCOM_EFAULT = -EFAULT,
121   VPPCOM_ENOMEM = -ENOMEM,
122   VPPCOM_EINVAL = -EINVAL,
123   VPPCOM_EBADFD = -EBADFD,
124   VPPCOM_EAFNOSUPPORT = -EAFNOSUPPORT,
125   VPPCOM_ECONNABORTED = -ECONNABORTED,
126   VPPCOM_ECONNRESET = -ECONNRESET,
127   VPPCOM_ENOTCONN = -ENOTCONN,
128   VPPCOM_ECONNREFUSED = -ECONNREFUSED,
129   VPPCOM_ETIMEDOUT = -ETIMEDOUT,
130   VPPCOM_EEXIST = -EEXIST
131 } vppcom_error_t;
132
133 typedef enum
134 {
135   VPPCOM_ATTR_GET_NREAD,
136   VPPCOM_ATTR_GET_NWRITE,
137   VPPCOM_ATTR_GET_FLAGS,
138   VPPCOM_ATTR_SET_FLAGS,
139   VPPCOM_ATTR_GET_LCL_ADDR,
140   VPPCOM_ATTR_GET_PEER_ADDR,
141   VPPCOM_ATTR_GET_LIBC_EPFD,
142   VPPCOM_ATTR_SET_LIBC_EPFD,
143   VPPCOM_ATTR_GET_PROTOCOL,
144   VPPCOM_ATTR_GET_LISTEN,
145   VPPCOM_ATTR_GET_ERROR,
146   VPPCOM_ATTR_GET_TX_FIFO_LEN,
147   VPPCOM_ATTR_SET_TX_FIFO_LEN,
148   VPPCOM_ATTR_GET_RX_FIFO_LEN,
149   VPPCOM_ATTR_SET_RX_FIFO_LEN,
150   VPPCOM_ATTR_GET_REUSEADDR,
151   VPPCOM_ATTR_SET_REUSEADDR,
152   VPPCOM_ATTR_GET_REUSEPORT,
153   VPPCOM_ATTR_SET_REUSEPORT,
154   VPPCOM_ATTR_GET_BROADCAST,
155   VPPCOM_ATTR_SET_BROADCAST,
156   VPPCOM_ATTR_GET_V6ONLY,
157   VPPCOM_ATTR_SET_V6ONLY,
158   VPPCOM_ATTR_GET_KEEPALIVE,
159   VPPCOM_ATTR_SET_KEEPALIVE,
160   VPPCOM_ATTR_GET_TCP_NODELAY,
161   VPPCOM_ATTR_SET_TCP_NODELAY,
162   VPPCOM_ATTR_GET_TCP_KEEPIDLE,
163   VPPCOM_ATTR_SET_TCP_KEEPIDLE,
164   VPPCOM_ATTR_GET_TCP_KEEPINTVL,
165   VPPCOM_ATTR_SET_TCP_KEEPINTVL,
166   VPPCOM_ATTR_GET_TCP_USER_MSS,
167   VPPCOM_ATTR_SET_TCP_USER_MSS,
168   VPPCOM_ATTR_SET_SHUT,
169   VPPCOM_ATTR_GET_SHUT,
170 } vppcom_attr_op_t;
171
172 typedef struct _vcl_poll
173 {
174   uint32_t fds_ndx;
175   vcl_session_handle_t sh;
176   short events;
177   short revents;
178 } vcl_poll_t;
179
180 typedef struct vppcom_data_segment_
181 {
182   unsigned char *data;
183   uint32_t len;
184 } vppcom_data_segment_t;
185
186 typedef vppcom_data_segment_t vppcom_data_segments_t[2];
187
188 typedef unsigned long vcl_si_set;
189
190 /*
191  * VPPCOM Public API Functions
192  */
193 static inline const char *
194 vppcom_retval_str (int retval)
195 {
196   char *st;
197
198   switch (retval)
199     {
200     case VPPCOM_OK:
201       st = "VPPCOM_OK";
202       break;
203
204     case VPPCOM_EAGAIN:
205       st = "VPPCOM_EAGAIN";
206       break;
207
208     case VPPCOM_EFAULT:
209       st = "VPPCOM_EFAULT";
210       break;
211
212     case VPPCOM_ENOMEM:
213       st = "VPPCOM_ENOMEM";
214       break;
215
216     case VPPCOM_EINVAL:
217       st = "VPPCOM_EINVAL";
218       break;
219
220     case VPPCOM_EBADFD:
221       st = "VPPCOM_EBADFD";
222       break;
223
224     case VPPCOM_EAFNOSUPPORT:
225       st = "VPPCOM_EAFNOSUPPORT";
226       break;
227
228     case VPPCOM_ECONNABORTED:
229       st = "VPPCOM_ECONNABORTED";
230       break;
231
232     case VPPCOM_ECONNRESET:
233       st = "VPPCOM_ECONNRESET";
234       break;
235
236     case VPPCOM_ENOTCONN:
237       st = "VPPCOM_ENOTCONN";
238       break;
239
240     case VPPCOM_ECONNREFUSED:
241       st = "VPPCOM_ECONNREFUSED";
242       break;
243
244     case VPPCOM_ETIMEDOUT:
245       st = "VPPCOM_ETIMEDOUT";
246       break;
247
248     default:
249       st = "UNKNOWN_STATE";
250       break;
251     }
252
253   return st;
254 }
255
256 /* TBD: make these constructor/destructor function */
257 extern int vppcom_app_create (char *app_name);
258 extern void vppcom_app_destroy (void);
259
260 extern int vppcom_session_create (uint8_t proto, uint8_t is_nonblocking);
261 extern int vppcom_session_close (uint32_t session_handle);
262 extern int vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep);
263 extern int vppcom_session_listen (uint32_t session_handle, uint32_t q_len);
264
265 extern int vppcom_session_accept (uint32_t session_handle,
266                                   vppcom_endpt_t * client_ep, uint32_t flags);
267
268 extern int vppcom_session_connect (uint32_t session_handle,
269                                    vppcom_endpt_t * server_ep);
270 extern int vppcom_session_stream_connect (uint32_t session_handle,
271                                           uint32_t parent_session_handle);
272 extern int vppcom_session_read (uint32_t session_handle, void *buf, size_t n);
273 extern int vppcom_session_write (uint32_t session_handle, void *buf,
274                                  size_t n);
275 extern int vppcom_session_write_msg (uint32_t session_handle, void *buf,
276                                      size_t n);
277
278 extern int vppcom_select (int n_bits, vcl_si_set * read_map,
279                           vcl_si_set * write_map, vcl_si_set * except_map,
280                           double wait_for_time);
281
282 extern int vppcom_epoll_create (void);
283 extern int vppcom_epoll_ctl (uint32_t vep_handle, int op,
284                              uint32_t session_handle,
285                              struct epoll_event *event);
286 extern int vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
287                               int maxevents, double wait_for_time);
288 extern int vppcom_session_attr (uint32_t session_handle, uint32_t op,
289                                 void *buffer, uint32_t * buflen);
290 extern int vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
291                                     uint32_t buflen, int flags,
292                                     vppcom_endpt_t * ep);
293 extern int vppcom_session_sendto (uint32_t session_handle, void *buffer,
294                                   uint32_t buflen, int flags,
295                                   vppcom_endpt_t * ep);
296 extern int vppcom_poll (vcl_poll_t * vp, uint32_t n_sids,
297                         double wait_for_time);
298 extern int vppcom_mq_epoll_fd (void);
299 extern int vppcom_session_index (vcl_session_handle_t session_handle);
300 extern int vppcom_session_worker (vcl_session_handle_t session_handle);
301
302 extern int vppcom_session_read_segments (uint32_t session_handle,
303                                          vppcom_data_segments_t ds);
304 extern void vppcom_session_free_segments (uint32_t session_handle,
305                                           vppcom_data_segments_t ds);
306 extern int vppcom_session_tls_add_cert (uint32_t session_handle, char *cert,
307                                         uint32_t cert_len);
308 extern int vppcom_session_tls_add_key (uint32_t session_handle, char *key,
309                                        uint32_t key_len);
310 extern int vppcom_data_segment_copy (void *buf, vppcom_data_segments_t ds,
311                                      uint32_t max_bytes);
312 extern int vppcom_unformat_proto (uint8_t * proto, char *proto_str);
313 extern int vppcom_session_is_connectable_listener (uint32_t session_handle);
314 extern int vppcom_session_listener (uint32_t session_handle);
315 extern int vppcom_session_n_accepted (uint32_t session_handle);
316
317 /**
318  * Request from application to register a new worker
319  *
320  * Expectation is that applications will make use of this after a new pthread
321  * is spawned.
322  */
323 extern int vppcom_worker_register (void);
324
325 /**
326  * Unregister current worker
327  */
328 extern void vppcom_worker_unregister (void);
329
330 /**
331  * Retrieve current worker index
332  */
333 extern int vppcom_worker_index (void);
334
335 /**
336  * Returns the current worker's message queues epoll fd
337  *
338  * This only works if vcl is configured to do eventfd based message queue
339  * notifications.
340  */
341 extern int vppcom_worker_mqs_epfd (void);
342
343 /* *INDENT-OFF* */
344 #ifdef __cplusplus
345 }
346 #endif
347 /* *INDENT-ON* */
348
349 #endif /* included_vppcom_h */
350
351 /*
352  * fd.io coding-style-patch-verification: ON
353  *
354  * Local Variables:
355  * eval: (c-set-style "gnu")
356  * End:
357  */