Clean up for vcl.am, making vppcom.h C++ aware
[vpp.git] / src / vcl / vppcom.h
1 /*
2  * Copyright (c) 2017 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/poll.h>
22 #include <sys/epoll.h>
23
24 /* *INDENT-OFF* */
25 #ifdef __cplusplus
26 extern "C"
27 {
28 #endif
29 /* *INDENT-ON* */
30
31 /*
32  * VPPCOM Public API Definitions, Enums, and Data Structures
33  */
34 #define INVALID_SESSION_ID                   (~0)
35 #define VPPCOM_VRF_DEFAULT                   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
46 typedef enum
47 {
48   VPPCOM_PROTO_TCP = 0,
49   VPPCOM_PROTO_UDP,
50 } vppcom_proto_t;
51
52 static inline char *
53 vppcom_proto_str (vppcom_proto_t proto)
54 {
55   char *proto_str;
56
57   switch (proto)
58     {
59     case VPPCOM_PROTO_TCP:
60       proto_str = "VPPCOM_PROTO_TCP";
61       break;
62     case VPPCOM_PROTO_UDP:
63       proto_str = "VPPCOM_PROTO_UDP";
64       break;
65     default:
66       proto_str = "UNKNOWN";
67       break;
68     }
69   return proto_str;
70 }
71
72 typedef enum
73 {
74   VPPCOM_IS_IP6 = 0,
75   VPPCOM_IS_IP4,
76 } vppcom_is_ip4_t;
77
78 typedef struct vppcom_endpt_t_
79 {
80   uint32_t vrf;
81   uint8_t is_cut_thru;
82   uint8_t is_ip4;
83   uint8_t *ip;
84   uint16_t port;
85 } vppcom_endpt_t;
86
87 typedef enum
88 {
89   VPPCOM_OK = 0,
90   VPPCOM_EAGAIN = -EAGAIN,
91   VPPCOM_EFAULT = -EFAULT,
92   VPPCOM_ENOMEM = -ENOMEM,
93   VPPCOM_EINVAL = -EINVAL,
94   VPPCOM_EBADFD = -EBADFD,
95   VPPCOM_EAFNOSUPPORT = -EAFNOSUPPORT,
96   VPPCOM_ECONNABORTED = -ECONNABORTED,
97   VPPCOM_ECONNRESET = -ECONNRESET,
98   VPPCOM_ENOTCONN = -ENOTCONN,
99   VPPCOM_ECONNREFUSED = -ECONNREFUSED,
100   VPPCOM_ETIMEDOUT = -ETIMEDOUT,
101 } vppcom_error_t;
102
103 typedef enum
104 {
105   VPPCOM_ATTR_GET_NREAD,
106   VPPCOM_ATTR_GET_NWRITE,
107   VPPCOM_ATTR_GET_FLAGS,
108   VPPCOM_ATTR_SET_FLAGS,
109   VPPCOM_ATTR_GET_LCL_ADDR,
110   VPPCOM_ATTR_GET_PEER_ADDR,
111   VPPCOM_ATTR_GET_LIBC_EPFD,
112   VPPCOM_ATTR_SET_LIBC_EPFD,
113   VPPCOM_ATTR_GET_PROTOCOL,
114   VPPCOM_ATTR_GET_LISTEN,
115   VPPCOM_ATTR_GET_ERROR,
116   VPPCOM_ATTR_GET_TX_FIFO_LEN,
117   VPPCOM_ATTR_SET_TX_FIFO_LEN,
118   VPPCOM_ATTR_GET_RX_FIFO_LEN,
119   VPPCOM_ATTR_SET_RX_FIFO_LEN,
120   VPPCOM_ATTR_GET_REUSEADDR,
121   VPPCOM_ATTR_SET_REUSEADDR,
122   VPPCOM_ATTR_GET_REUSEPORT,
123   VPPCOM_ATTR_SET_REUSEPORT,
124   VPPCOM_ATTR_GET_BROADCAST,
125   VPPCOM_ATTR_SET_BROADCAST,
126   VPPCOM_ATTR_GET_V6ONLY,
127   VPPCOM_ATTR_SET_V6ONLY,
128   VPPCOM_ATTR_GET_KEEPALIVE,
129   VPPCOM_ATTR_SET_KEEPALIVE,
130   VPPCOM_ATTR_GET_TCP_NODELAY,
131   VPPCOM_ATTR_SET_TCP_NODELAY,
132   VPPCOM_ATTR_GET_TCP_KEEPIDLE,
133   VPPCOM_ATTR_SET_TCP_KEEPIDLE,
134   VPPCOM_ATTR_GET_TCP_KEEPINTVL,
135   VPPCOM_ATTR_SET_TCP_KEEPINTVL,
136   VPPCOM_ATTR_GET_TCP_USER_MSS,
137   VPPCOM_ATTR_SET_TCP_USER_MSS,
138 } vppcom_attr_op_t;
139
140 typedef struct _vcl_poll
141 {
142   uint32_t fds_ndx;
143   uint32_t sid;
144   short events;
145   short *revents;
146 } vcl_poll_t;
147
148 /*
149  * VPPCOM Public API Functions
150  */
151 static inline const char *
152 vppcom_retval_str (int retval)
153 {
154   char *st;
155
156   switch (retval)
157     {
158     case VPPCOM_OK:
159       st = "VPPCOM_OK";
160       break;
161
162     case VPPCOM_EAGAIN:
163       st = "VPPCOM_EAGAIN";
164       break;
165
166     case VPPCOM_EFAULT:
167       st = "VPPCOM_EFAULT";
168       break;
169
170     case VPPCOM_ENOMEM:
171       st = "VPPCOM_ENOMEM";
172       break;
173
174     case VPPCOM_EINVAL:
175       st = "VPPCOM_EINVAL";
176       break;
177
178     case VPPCOM_EBADFD:
179       st = "VPPCOM_EBADFD";
180       break;
181
182     case VPPCOM_EAFNOSUPPORT:
183       st = "VPPCOM_EAFNOSUPPORT";
184       break;
185
186     case VPPCOM_ECONNABORTED:
187       st = "VPPCOM_ECONNABORTED";
188       break;
189
190     case VPPCOM_ECONNRESET:
191       st = "VPPCOM_ECONNRESET";
192       break;
193
194     case VPPCOM_ENOTCONN:
195       st = "VPPCOM_ENOTCONN";
196       break;
197
198     case VPPCOM_ECONNREFUSED:
199       st = "VPPCOM_ECONNREFUSED";
200       break;
201
202     case VPPCOM_ETIMEDOUT:
203       st = "VPPCOM_ETIMEDOUT";
204       break;
205
206     default:
207       st = "UNKNOWN_STATE";
208       break;
209     }
210
211   return st;
212 }
213
214 /* TBD: make these constructor/destructor function */
215 extern int vppcom_app_create (char *app_name);
216 extern void vppcom_app_destroy (void);
217
218 extern int vppcom_session_create (uint32_t vrf, uint8_t proto,
219                                   uint8_t is_nonblocking);
220 extern int vppcom_session_close (uint32_t session_index);
221
222 extern int vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep);
223 extern int vppcom_session_listen (uint32_t session_index, uint32_t q_len);
224 extern int vppcom_session_accept (uint32_t session_index,
225                                   vppcom_endpt_t * client_ep, uint32_t flags);
226
227 extern int vppcom_session_connect (uint32_t session_index,
228                                    vppcom_endpt_t * server_ep);
229 extern int vppcom_session_read (uint32_t session_index, void *buf, size_t n);
230 extern int vppcom_session_write (uint32_t session_index, void *buf, size_t n);
231
232 extern int vppcom_select (unsigned long n_bits,
233                           unsigned long *read_map,
234                           unsigned long *write_map,
235                           unsigned long *except_map, double wait_for_time);
236
237 extern int vppcom_epoll_create (void);
238 extern int vppcom_epoll_ctl (uint32_t vep_idx, int op,
239                              uint32_t session_index,
240                              struct epoll_event *event);
241 extern int vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
242                               int maxevents, double wait_for_time);
243 extern int vppcom_session_attr (uint32_t session_index, uint32_t op,
244                                 void *buffer, uint32_t * buflen);
245 extern int vppcom_session_recvfrom (uint32_t session_index, void *buffer,
246                                     uint32_t buflen, int flags,
247                                     vppcom_endpt_t * ep);
248 extern int vppcom_session_sendto (uint32_t session_index, 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
254 /* *INDENT-OFF* */
255 #ifdef __cplusplus
256 }
257 #endif
258 /* *INDENT-ON* */
259
260 #endif /* included_vppcom_h */
261
262 /*
263  * fd.io coding-style-patch-verification: ON
264  *
265  * Local Variables:
266  * eval: (c-set-style "gnu")
267  * End:
268  */