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