Update L2 header offset after VLAN tag rewrite
[vpp.git] / src / vcl / vcom_socket.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_vcom_socket_h
17 #define included_vcom_socket_h
18
19 #include <string.h>
20
21 #include <vcl/vcom_glibc_socket.h>
22 #include <vppinfra/types.h>
23 #include <sys/socket.h>
24
25 #define INVALID_SESSION_ID (~0)
26 #define INVALID_FD (~0)
27
28 #define INVALID_VEP_IDX INVALID_SESSION_ID
29 #define INVALID_EPFD INVALID_FD
30
31 typedef enum
32 {
33   SOCKET_TYPE_UNBOUND = 0,
34   SOCKET_TYPE_KERNEL_BOUND,
35   SOCKET_TYPE_VPPCOM_BOUND
36 } vcom_socket_type_t;
37
38 typedef enum
39 {
40   EPOLL_TYPE_UNBOUND = 0,
41   EPOLL_TYPE_KERNEL_BOUND,
42   EPOLL_TYPE_VPPCOM_BOUND
43 } vcom_epoll_type_t;
44
45 typedef enum
46 {
47   FD_TYPE_INVALID = 0,
48   FD_TYPE_KERNEL,
49   FD_TYPE_EPOLL,
50   FD_TYPE_VCOM_SOCKET,
51   /* add new types here */
52   /* FD_TYPE_MAX should be the last entry */
53   FD_TYPE_MAX
54 } vcom_fd_type_t;
55
56 typedef struct
57 {
58   /* file descriptor -
59    * fd 0, 1, 2 have special meaning and are reserved,
60    * -1 denote invalid fd */
61   i32 fd;
62
63   /* session id - -1 denote invalid sid */
64   i32 sid;
65
66   /* socket type */
67   vcom_socket_type_t type;
68
69   /* vcom socket attributes here */
70
71 } vcom_socket_t;
72
73 typedef struct
74 {
75   /* epoll file descriptor -
76    * epfd 0, 1, 2 have special meaning and are reserved,
77    * -1 denote invalid epfd */
78   i32 epfd;
79
80   /* vep idx - -1 denote invalid vep_idx */
81   i32 vep_idx;
82
83   /* epoll type */
84   vcom_epoll_type_t type;
85
86   /* flags - 0 or EPOLL_CLOEXEC */
87   i32 flags;
88
89   /* vcom epoll attributes here */
90
91   /*
92    * 00. count of file descriptors currently registered
93    *     on this epoll instance.
94    * 01. number of file descriptors in the epoll set.
95    * 02. EPOLL_CTL_ADD, EPOLL_CTL_MOD, EPOLL_CTL_DEL
96    *     update the count.
97    * 03. cached for frequent access.
98    * */
99   i32 count;
100
101   /* close( ) called on this epoll instance */
102   /* 0 - close ( ) not called, 1 - close( ) called. */
103   u32 close;
104
105 } vcom_epoll_t;
106
107 typedef struct
108 {
109   /* "container" of this item */
110   i32 epfd;
111
112   /* fd - file descriptor information this item refers to */
113   i32 fd;
114   /* next and prev fd in the "epoll set" of epfd */
115   i32 next_fd;
116   i32 prev_fd;
117
118   /* vcom fd type */
119   vcom_fd_type_t type;
120
121   /* interested events and the source fd */
122   struct epoll_event event;
123
124   /* ready events and the source fd */
125   struct epoll_event revent;
126
127   /* epitem attributes here */
128
129 } vcom_epitem_t;
130
131 typedef union vcom_epitem_key
132 {
133   struct
134   {
135     i32 fd;
136     i32 epfd;
137   };
138   i64 key;
139 } __EPOLL_PACKED vcom_epitem_key_t;
140
141 static inline char *
142 vcom_socket_type_str (vcom_socket_type_t t)
143 {
144   switch (t)
145     {
146     case SOCKET_TYPE_UNBOUND:
147       return "SOCKET_TYPE_UNBOUND";
148
149     case SOCKET_TYPE_KERNEL_BOUND:
150       return "SOCKET_TYPE_KERNEL_BOUND";
151
152     case SOCKET_TYPE_VPPCOM_BOUND:
153       return "SOCKET_TYPE_VPPCOM_BOUND";
154
155     default:
156       return "SOCKET_TYPE_UNKNOWN";
157     }
158 }
159
160 static inline char *
161 vcom_socket_epoll_type_str (vcom_epoll_type_t t)
162 {
163   switch (t)
164     {
165     case EPOLL_TYPE_UNBOUND:
166       return "EPOLL_TYPE_UNBOUND";
167
168     case EPOLL_TYPE_KERNEL_BOUND:
169       return "EPOLL_TYPE_KERNEL_BOUND";
170
171     case EPOLL_TYPE_VPPCOM_BOUND:
172       return "EPOLL_TYPE_VPPCOM_BOUND";
173
174     default:
175       return "EPOLL_TYPE_UNKNOWN";
176     }
177 }
178
179 static inline char *
180 vcom_socket_vcom_fd_type_str (vcom_fd_type_t t)
181 {
182   switch (t)
183     {
184     case FD_TYPE_KERNEL:
185       return "FD_TYPE_KERNEL";
186
187     case FD_TYPE_EPOLL:
188       return "FD_TYPE_EPOLL";
189
190     case FD_TYPE_VCOM_SOCKET:
191       return "FD_TYPE_VCOM_SOCKET";
192
193     default:
194       return "FD_TYPE_UNKNOWN";
195     }
196 }
197
198 static inline int
199 vcom_socket_type_is_vppcom_bound (vcom_socket_type_t t)
200 {
201   return t == SOCKET_TYPE_VPPCOM_BOUND;
202 }
203
204 static inline int
205 vcom_socket_epoll_type_is_vppcom_bound (vcom_epoll_type_t t)
206 {
207   return t == EPOLL_TYPE_VPPCOM_BOUND;
208 }
209
210 static inline void
211 vsocket_init (vcom_socket_t * vsock)
212 {
213   memset (vsock, 0, sizeof (*vsock));
214
215   vsock->fd = INVALID_FD;
216   vsock->sid = INVALID_SESSION_ID;
217   vsock->type = SOCKET_TYPE_UNBOUND;
218   /* vcom socket attributes init here */
219 }
220
221 static inline void
222 vepoll_init (vcom_epoll_t * vepoll)
223 {
224   memset (vepoll, 0, sizeof (*vepoll));
225
226   vepoll->epfd = INVALID_EPFD;
227   vepoll->vep_idx = INVALID_VEP_IDX;
228   vepoll->type = EPOLL_TYPE_UNBOUND;
229   vepoll->flags = 0;
230
231   vepoll->count = 0;
232   vepoll->close = 0;
233   /* vcom epoll attributes init here */
234 }
235
236 static inline void
237 vepitem_init (vcom_epitem_t * vepitem)
238 {
239   struct epoll_event event = {.events = 0,.data.fd = INVALID_FD };
240
241   memset (vepitem, 0, sizeof (*vepitem));
242
243   vepitem->epfd = INVALID_EPFD;
244
245   vepitem->fd = INVALID_FD;
246   vepitem->next_fd = INVALID_FD;
247   vepitem->prev_fd = INVALID_FD;
248
249   vepitem->type = FD_TYPE_INVALID;
250
251   vepitem->event = event;
252   vepitem->revent = event;
253   /* vepoll attributes init here */
254 }
255
256 static inline void
257 vepitemkey_init (vcom_epitem_key_t * epfdfd)
258 {
259   memset (epfdfd, 0, sizeof (*epfdfd));
260
261   epfdfd->epfd = INVALID_EPFD;
262   epfdfd->fd = INVALID_FD;
263 }
264
265 static inline void
266 vsocket_set (vcom_socket_t * vsock, i32 fd, i32 sid, vcom_socket_type_t type)
267 {
268   vsock->fd = fd;
269   vsock->sid = sid;
270   vsock->type = type;
271   /* vcom socket attributes set here */
272 }
273
274 static inline void
275 vepoll_set (vcom_epoll_t * vepoll,
276             i32 epfd, i32 vep_idx,
277             vcom_epoll_type_t type, i32 flags, i32 count, u32 close)
278 {
279   vepoll->epfd = epfd;
280   vepoll->vep_idx = vep_idx;
281   vepoll->type = type;
282   vepoll->flags = flags;
283
284   vepoll->count = count;
285   vepoll->close = close;
286   /* vcom epoll attributes set here */
287 }
288
289 static inline void
290 vepitem_set (vcom_epitem_t * vepitem,
291              i32 epfd,
292              i32 fd, i32 next_fd, i32 prev_fd,
293              vcom_fd_type_t type,
294              struct epoll_event event, struct epoll_event revent)
295 {
296   vepitem->epfd = epfd;
297
298   vepitem->fd = fd;
299   vepitem->next_fd = next_fd;
300   vepitem->prev_fd = prev_fd;
301
302   vepitem->type = type;
303
304   vepitem->event = event;
305   vepitem->revent = revent;
306   /* vcom epitem attributes set here */
307 }
308
309 static inline void
310 vepitemkey_set (vcom_epitem_key_t * epfdfd, i32 epfd, i32 fd)
311 {
312   epfdfd->epfd = epfd;
313   epfdfd->fd = fd;
314 }
315
316 static inline int
317 vsocket_is_vppcom_bound (vcom_socket_t * vsock)
318 {
319   return vcom_socket_type_is_vppcom_bound (vsock->type);
320 }
321
322 static inline int
323 vepoll_is_vppcom_bound (vcom_epoll_t * vepoll)
324 {
325   return vcom_socket_epoll_type_is_vppcom_bound (vepoll->type);
326 }
327
328 int vcom_socket_main_init (void);
329
330 void vcom_socket_main_destroy (void);
331
332 void vcom_socket_main_show (void);
333
334 int vcom_socket_is_vcom_fd (int fd);
335
336 int vcom_socket_is_vcom_epfd (int epfd);
337
338 int vcom_socket_close (int __fd);
339
340 ssize_t vcom_socket_read (int __fd, void *__buf, size_t __nbytes);
341
342 ssize_t vcom_socket_readv (int __fd, const struct iovec *__iov, int __iovcnt);
343
344 ssize_t vcom_socket_write (int __fd, const void *__buf, size_t __n);
345
346 ssize_t vcom_socket_writev (int __fd, const struct iovec *__iov,
347                             int __iovcnt);
348
349 int vcom_socket_fcntl_va (int __fd, int __cmd, va_list __ap);
350
351 int vcom_socket_ioctl_va (int __fd, unsigned long int __cmd, va_list __ap);
352
353 int
354 vcom_socket_select (int vcom_nfds, fd_set * __restrict vcom_readfds,
355                     fd_set * __restrict vcom_writefds,
356                     fd_set * __restrict vcom_exceptfds,
357                     struct timeval *__restrict timeout);
358
359
360 int vcom_socket_socket (int __domain, int __type, int __protocol);
361
362 int
363 vcom_socket_socketpair (int __domain, int __type, int __protocol,
364                         int __fds[2]);
365
366 int vcom_socket_bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len);
367
368 int
369 vcom_socket_getsockname (int __fd, __SOCKADDR_ARG __addr,
370                          socklen_t * __restrict __len);
371
372 int
373 vcom_socket_connect (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len);
374
375 int
376 vcom_socket_getpeername (int __fd, __SOCKADDR_ARG __addr,
377                          socklen_t * __restrict __len);
378
379 ssize_t
380 vcom_socket_send (int __fd, const void *__buf, size_t __n, int __flags);
381
382 ssize_t vcom_socket_recv (int __fd, void *__buf, size_t __n, int __flags);
383
384 /*
385  * RETURN   1 if __fd is (SOCK_STREAM, SOCK_SEQPACKET),
386  * 0 otherwise
387  * */
388 int vcom_socket_is_connection_mode_socket (int __fd);
389
390 ssize_t
391 vcom_socket_sendto (int __fd, const void *__buf, size_t __n,
392                     int __flags, __CONST_SOCKADDR_ARG __addr,
393                     socklen_t __addr_len);
394
395 ssize_t
396 vcom_socket_recvfrom (int __fd, void *__restrict __buf, size_t __n,
397                       int __flags, __SOCKADDR_ARG __addr,
398                       socklen_t * __restrict __addr_len);
399
400 ssize_t
401 vcom_socket_sendmsg (int __fd, const struct msghdr *__message, int __flags);
402
403 #ifdef __USE_GNU
404 int
405 vcom_socket_sendmmsg (int __fd, struct mmsghdr *__vmessages,
406                       unsigned int __vlen, int __flags);
407 #endif
408
409 ssize_t vcom_socket_recvmsg (int __fd, struct msghdr *__message, int __flags);
410
411 #ifdef __USE_GNU
412 int
413 vcom_socket_recvmmsg (int __fd, struct mmsghdr *__vmessages,
414                       unsigned int __vlen, int __flags,
415                       struct timespec *__tmo);
416 #endif
417
418 int
419 vcom_socket_getsockopt (int __fd, int __level, int __optname,
420                         void *__restrict __optval,
421                         socklen_t * __restrict __optlen);
422
423 int
424 vcom_socket_setsockopt (int __fd, int __level, int __optname,
425                         const void *__optval, socklen_t __optlen);
426
427 int vcom_socket_listen (int __fd, int __n);
428
429 int
430 vcom_socket_accept (int __fd, __SOCKADDR_ARG __addr,
431                     socklen_t * __restrict __addr_len);
432
433 #ifdef __USE_GNU
434 int
435 vcom_socket_accept4 (int __fd, __SOCKADDR_ARG __addr,
436                      socklen_t * __restrict __addr_len, int __flags);
437 #endif
438
439 int vcom_socket_shutdown (int __fd, int __how);
440
441 int vcom_socket_epoll_create1 (int __flags);
442
443 int
444 vcom_socket_epoll_ctl (int __epfd, int __op, int __fd,
445                        struct epoll_event *__event);
446
447 int
448 vcom_socket_epoll_pwait (int __epfd, struct epoll_event *__events,
449                          int __maxevents, int __timeout,
450                          const __sigset_t * __ss);
451
452 /*
453  * handle only vcom fds
454  */
455 int vcom_socket_poll (struct pollfd *__fds, nfds_t __nfds, int __timeout);
456
457 #ifdef __USE_GNU
458 int
459 vcom_socket_ppoll (struct pollfd *__fds, nfds_t __nfds,
460                    const struct timespec *__timeout, const __sigset_t * __ss);
461 #endif
462
463 #endif /* included_vcom_socket_h */
464
465 /*
466  * fd.io coding-style-patch-verification: ON
467  *
468  * Local Variables:
469  * eval: (c-set-style "gnu")
470  * End:
471  */