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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef included_vcom_socket_h
17 #define included_vcom_socket_h
21 #include <vcl/vcom_glibc_socket.h>
22 #include <vppinfra/types.h>
23 #include <sys/socket.h>
25 #define INVALID_SESSION_ID (~0)
26 #define INVALID_FD (~0)
28 #define INVALID_VEP_IDX INVALID_SESSION_ID
29 #define INVALID_EPFD INVALID_FD
33 SOCKET_TYPE_UNBOUND = 0,
34 SOCKET_TYPE_KERNEL_BOUND,
35 SOCKET_TYPE_VPPCOM_BOUND
40 EPOLL_TYPE_UNBOUND = 0,
41 EPOLL_TYPE_KERNEL_BOUND,
42 EPOLL_TYPE_VPPCOM_BOUND
51 /* add new types here */
52 /* FD_TYPE_MAX should be the last entry */
59 * fd 0, 1, 2 have special meaning and are reserved,
60 * -1 denote invalid fd */
63 /* session id - -1 denote invalid sid */
67 vcom_socket_type_t type;
69 /* vcom socket attributes here */
75 /* epoll file descriptor -
76 * epfd 0, 1, 2 have special meaning and are reserved,
77 * -1 denote invalid epfd */
80 /* vep idx - -1 denote invalid vep_idx */
84 vcom_epoll_type_t type;
86 /* flags - 0 or EPOLL_CLOEXEC */
89 /* vcom epoll attributes here */
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
97 * 03. cached for frequent access.
101 /* close( ) called on this epoll instance */
102 /* 0 - close ( ) not called, 1 - close( ) called. */
109 /* "container" of this item */
112 /* fd - file descriptor information this item refers to */
114 /* next and prev fd in the "epoll set" of epfd */
121 /* interested events and the source fd */
122 struct epoll_event event;
124 /* ready events and the source fd */
125 struct epoll_event revent;
127 /* epitem attributes here */
131 typedef union vcom_epitem_key
139 } __EPOLL_PACKED vcom_epitem_key_t;
142 vcom_socket_type_str (vcom_socket_type_t t)
146 case SOCKET_TYPE_UNBOUND:
147 return "SOCKET_TYPE_UNBOUND";
149 case SOCKET_TYPE_KERNEL_BOUND:
150 return "SOCKET_TYPE_KERNEL_BOUND";
152 case SOCKET_TYPE_VPPCOM_BOUND:
153 return "SOCKET_TYPE_VPPCOM_BOUND";
156 return "SOCKET_TYPE_UNKNOWN";
161 vcom_socket_epoll_type_str (vcom_epoll_type_t t)
165 case EPOLL_TYPE_UNBOUND:
166 return "EPOLL_TYPE_UNBOUND";
168 case EPOLL_TYPE_KERNEL_BOUND:
169 return "EPOLL_TYPE_KERNEL_BOUND";
171 case EPOLL_TYPE_VPPCOM_BOUND:
172 return "EPOLL_TYPE_VPPCOM_BOUND";
175 return "EPOLL_TYPE_UNKNOWN";
180 vcom_socket_vcom_fd_type_str (vcom_fd_type_t t)
185 return "FD_TYPE_KERNEL";
188 return "FD_TYPE_EPOLL";
190 case FD_TYPE_VCOM_SOCKET:
191 return "FD_TYPE_VCOM_SOCKET";
194 return "FD_TYPE_UNKNOWN";
199 vcom_socket_type_is_vppcom_bound (vcom_socket_type_t t)
201 return t == SOCKET_TYPE_VPPCOM_BOUND;
205 vcom_socket_epoll_type_is_vppcom_bound (vcom_epoll_type_t t)
207 return t == EPOLL_TYPE_VPPCOM_BOUND;
211 vsocket_init (vcom_socket_t * vsock)
213 memset (vsock, 0, sizeof (*vsock));
215 vsock->fd = INVALID_FD;
216 vsock->sid = INVALID_SESSION_ID;
217 vsock->type = SOCKET_TYPE_UNBOUND;
218 /* vcom socket attributes init here */
222 vepoll_init (vcom_epoll_t * vepoll)
224 memset (vepoll, 0, sizeof (*vepoll));
226 vepoll->epfd = INVALID_EPFD;
227 vepoll->vep_idx = INVALID_VEP_IDX;
228 vepoll->type = EPOLL_TYPE_UNBOUND;
233 /* vcom epoll attributes init here */
237 vepitem_init (vcom_epitem_t * vepitem)
239 struct epoll_event event = {.events = 0,.data.fd = INVALID_FD };
241 memset (vepitem, 0, sizeof (*vepitem));
243 vepitem->epfd = INVALID_EPFD;
245 vepitem->fd = INVALID_FD;
246 vepitem->next_fd = INVALID_FD;
247 vepitem->prev_fd = INVALID_FD;
249 vepitem->type = FD_TYPE_INVALID;
251 vepitem->event = event;
252 vepitem->revent = event;
253 /* vepoll attributes init here */
257 vepitemkey_init (vcom_epitem_key_t * epfdfd)
259 memset (epfdfd, 0, sizeof (*epfdfd));
261 epfdfd->epfd = INVALID_EPFD;
262 epfdfd->fd = INVALID_FD;
266 vsocket_set (vcom_socket_t * vsock, i32 fd, i32 sid, vcom_socket_type_t type)
271 /* vcom socket attributes set here */
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)
280 vepoll->vep_idx = vep_idx;
282 vepoll->flags = flags;
284 vepoll->count = count;
285 vepoll->close = close;
286 /* vcom epoll attributes set here */
290 vepitem_set (vcom_epitem_t * vepitem,
292 i32 fd, i32 next_fd, i32 prev_fd,
294 struct epoll_event event, struct epoll_event revent)
296 vepitem->epfd = epfd;
299 vepitem->next_fd = next_fd;
300 vepitem->prev_fd = prev_fd;
302 vepitem->type = type;
304 vepitem->event = event;
305 vepitem->revent = revent;
306 /* vcom epitem attributes set here */
310 vepitemkey_set (vcom_epitem_key_t * epfdfd, i32 epfd, i32 fd)
317 vsocket_is_vppcom_bound (vcom_socket_t * vsock)
319 return vcom_socket_type_is_vppcom_bound (vsock->type);
323 vepoll_is_vppcom_bound (vcom_epoll_t * vepoll)
325 return vcom_socket_epoll_type_is_vppcom_bound (vepoll->type);
328 int vcom_socket_main_init (void);
330 void vcom_socket_main_destroy (void);
332 void vcom_socket_main_show (void);
334 int vcom_socket_is_vcom_fd (int fd);
336 int vcom_socket_is_vcom_epfd (int epfd);
338 int vcom_socket_close (int __fd);
340 ssize_t vcom_socket_read (int __fd, void *__buf, size_t __nbytes);
342 ssize_t vcom_socket_readv (int __fd, const struct iovec *__iov, int __iovcnt);
344 ssize_t vcom_socket_write (int __fd, const void *__buf, size_t __n);
346 ssize_t vcom_socket_writev (int __fd, const struct iovec *__iov,
349 int vcom_socket_fcntl_va (int __fd, int __cmd, va_list __ap);
351 int vcom_socket_ioctl_va (int __fd, unsigned long int __cmd, va_list __ap);
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);
360 int vcom_socket_socket (int __domain, int __type, int __protocol);
363 vcom_socket_socketpair (int __domain, int __type, int __protocol,
366 int vcom_socket_bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len);
369 vcom_socket_getsockname (int __fd, __SOCKADDR_ARG __addr,
370 socklen_t * __restrict __len);
373 vcom_socket_connect (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len);
376 vcom_socket_getpeername (int __fd, __SOCKADDR_ARG __addr,
377 socklen_t * __restrict __len);
380 vcom_socket_send (int __fd, const void *__buf, size_t __n, int __flags);
382 ssize_t vcom_socket_recv (int __fd, void *__buf, size_t __n, int __flags);
385 * RETURN 1 if __fd is (SOCK_STREAM, SOCK_SEQPACKET),
388 int vcom_socket_is_connection_mode_socket (int __fd);
391 vcom_socket_sendto (int __fd, const void *__buf, size_t __n,
392 int __flags, __CONST_SOCKADDR_ARG __addr,
393 socklen_t __addr_len);
396 vcom_socket_recvfrom (int __fd, void *__restrict __buf, size_t __n,
397 int __flags, __SOCKADDR_ARG __addr,
398 socklen_t * __restrict __addr_len);
401 vcom_socket_sendmsg (int __fd, const struct msghdr *__message, int __flags);
405 vcom_socket_sendmmsg (int __fd, struct mmsghdr *__vmessages,
406 unsigned int __vlen, int __flags);
409 ssize_t vcom_socket_recvmsg (int __fd, struct msghdr *__message, int __flags);
413 vcom_socket_recvmmsg (int __fd, struct mmsghdr *__vmessages,
414 unsigned int __vlen, int __flags,
415 struct timespec *__tmo);
419 vcom_socket_getsockopt (int __fd, int __level, int __optname,
420 void *__restrict __optval,
421 socklen_t * __restrict __optlen);
424 vcom_socket_setsockopt (int __fd, int __level, int __optname,
425 const void *__optval, socklen_t __optlen);
427 int vcom_socket_listen (int __fd, int __n);
430 vcom_socket_accept (int __fd, __SOCKADDR_ARG __addr,
431 socklen_t * __restrict __addr_len);
435 vcom_socket_accept4 (int __fd, __SOCKADDR_ARG __addr,
436 socklen_t * __restrict __addr_len, int __flags);
439 int vcom_socket_shutdown (int __fd, int __how);
441 int vcom_socket_epoll_create1 (int __flags);
444 vcom_socket_epoll_ctl (int __epfd, int __op, int __fd,
445 struct epoll_event *__event);
448 vcom_socket_epoll_pwait (int __epfd, struct epoll_event *__events,
449 int __maxevents, int __timeout,
450 const __sigset_t * __ss);
453 * handle only vcom fds
455 int vcom_socket_poll (struct pollfd *__fds, nfds_t __nfds, int __timeout);
459 vcom_socket_ppoll (struct pollfd *__fds, nfds_t __nfds,
460 const struct timespec *__timeout, const __sigset_t * __ss);
463 #endif /* included_vcom_socket_h */
466 * fd.io coding-style-patch-verification: ON
469 * eval: (c-set-style "gnu")