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.
103 /* close( ) called on this epoll instance */
104 /* 0 - close ( ) not called, 1 - close( ) called. */
111 /* "container" of this item */
114 /* fd - file descriptor information this item refers to */
116 /* next and prev fd in the "epoll set" of epfd */
123 /* interested events and the source fd */
124 struct epoll_event event;
126 /* ready events and the source fd */
127 struct epoll_event revent;
129 /* epitem attributes here */
133 typedef union vcom_epitem_key
141 } __EPOLL_PACKED vcom_epitem_key_t;
144 vcom_socket_type_str (vcom_socket_type_t t)
148 case SOCKET_TYPE_UNBOUND:
149 return "SOCKET_TYPE_UNBOUND";
151 case SOCKET_TYPE_KERNEL_BOUND:
152 return "SOCKET_TYPE_KERNEL_BOUND";
154 case SOCKET_TYPE_VPPCOM_BOUND:
155 return "SOCKET_TYPE_VPPCOM_BOUND";
158 return "SOCKET_TYPE_UNKNOWN";
163 vcom_socket_epoll_type_str (vcom_epoll_type_t t)
167 case EPOLL_TYPE_UNBOUND:
168 return "EPOLL_TYPE_UNBOUND";
170 case EPOLL_TYPE_KERNEL_BOUND:
171 return "EPOLL_TYPE_KERNEL_BOUND";
173 case EPOLL_TYPE_VPPCOM_BOUND:
174 return "EPOLL_TYPE_VPPCOM_BOUND";
177 return "EPOLL_TYPE_UNKNOWN";
182 vcom_socket_vcom_fd_type_str (vcom_fd_type_t t)
187 return "FD_TYPE_KERNEL";
190 return "FD_TYPE_EPOLL";
192 case FD_TYPE_VCOM_SOCKET:
193 return "FD_TYPE_VCOM_SOCKET";
196 return "FD_TYPE_UNKNOWN";
201 vcom_socket_type_is_vppcom_bound (vcom_socket_type_t t)
203 return t == SOCKET_TYPE_VPPCOM_BOUND;
207 vcom_socket_epoll_type_is_vppcom_bound (vcom_epoll_type_t t)
209 return t == EPOLL_TYPE_VPPCOM_BOUND;
213 vsocket_init (vcom_socket_t * vsock)
215 memset (vsock, 0, sizeof (*vsock));
217 vsock->fd = INVALID_FD;
218 vsock->sid = INVALID_SESSION_ID;
219 vsock->type = SOCKET_TYPE_UNBOUND;
220 /* vcom socket attributes init here */
224 vepoll_init (vcom_epoll_t * vepoll)
226 memset (vepoll, 0, sizeof (*vepoll));
228 vepoll->epfd = INVALID_EPFD;
229 vepoll->vep_idx = INVALID_VEP_IDX;
230 vepoll->type = EPOLL_TYPE_UNBOUND;
235 /* vcom epoll attributes init here */
239 vepitem_init (vcom_epitem_t * vepitem)
241 struct epoll_event event = {.events = 0,.data.fd = INVALID_FD };
243 memset (vepitem, 0, sizeof (*vepitem));
245 vepitem->epfd = INVALID_EPFD;
247 vepitem->fd = INVALID_FD;
248 vepitem->next_fd = INVALID_FD;
249 vepitem->prev_fd = INVALID_FD;
251 vepitem->type = FD_TYPE_INVALID;
253 vepitem->event = event;
254 vepitem->revent = event;
255 /* vepoll attributes init here */
259 vepitemkey_init (vcom_epitem_key_t * epfdfd)
261 memset (epfdfd, 0, sizeof (*epfdfd));
263 epfdfd->epfd = INVALID_EPFD;
264 epfdfd->fd = INVALID_FD;
268 vsocket_set (vcom_socket_t * vsock, i32 fd, i32 sid, vcom_socket_type_t type)
273 /* vcom socket attributes set here */
277 vepoll_set (vcom_epoll_t * vepoll,
278 i32 epfd, i32 vep_idx,
279 vcom_epoll_type_t type, i32 flags, i32 count, u32 close)
282 vepoll->vep_idx = vep_idx;
284 vepoll->flags = flags;
286 vepoll->count = count;
287 vepoll->close = close;
288 /* vcom epoll attributes set here */
292 vepitem_set (vcom_epitem_t * vepitem,
294 i32 fd, i32 next_fd, i32 prev_fd,
296 struct epoll_event event, struct epoll_event revent)
298 vepitem->epfd = epfd;
301 vepitem->next_fd = next_fd;
302 vepitem->prev_fd = prev_fd;
304 vepitem->type = type;
306 vepitem->event = event;
307 vepitem->revent = revent;
308 /* vcom epitem attributes set here */
312 vepitemkey_set (vcom_epitem_key_t * epfdfd, i32 epfd, i32 fd)
319 vsocket_is_vppcom_bound (vcom_socket_t * vsock)
321 return vcom_socket_type_is_vppcom_bound (vsock->type);
325 vepoll_is_vppcom_bound (vcom_epoll_t * vepoll)
327 return vcom_socket_epoll_type_is_vppcom_bound (vepoll->type);
330 int vcom_socket_main_init (void);
332 void vcom_socket_main_destroy (void);
334 void vcom_socket_main_show (void);
336 int vcom_socket_is_vcom_fd (int fd);
338 int vcom_socket_is_vcom_epfd (int epfd);
340 int vcom_socket_close (int __fd);
342 ssize_t vcom_socket_read (int __fd, void *__buf, size_t __nbytes);
344 ssize_t vcom_socket_readv (int __fd, const struct iovec *__iov, int __iovcnt);
346 ssize_t vcom_socket_write (int __fd, const void *__buf, size_t __n);
348 ssize_t vcom_socket_writev (int __fd, const struct iovec *__iov,
351 int vcom_socket_fcntl_va (int __fd, int __cmd, va_list __ap);
353 int vcom_socket_ioctl_va (int __fd, unsigned long int __cmd, va_list __ap);
356 vcom_socket_select (int vcom_nfds, fd_set * __restrict vcom_readfds,
357 fd_set * __restrict vcom_writefds,
358 fd_set * __restrict vcom_exceptfds,
359 struct timeval *__restrict timeout);
362 int vcom_socket_socket (int __domain, int __type, int __protocol);
365 vcom_socket_socketpair (int __domain, int __type, int __protocol,
368 int vcom_socket_bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len);
371 vcom_socket_getsockname (int __fd, __SOCKADDR_ARG __addr,
372 socklen_t * __restrict __len);
375 vcom_socket_connect (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len);
378 vcom_socket_getpeername (int __fd, __SOCKADDR_ARG __addr,
379 socklen_t * __restrict __len);
382 vcom_socket_send (int __fd, const void *__buf, size_t __n, int __flags);
384 ssize_t vcom_socket_recv (int __fd, void *__buf, size_t __n, int __flags);
387 * RETURN 1 if __fd is (SOCK_STREAM, SOCK_SEQPACKET),
390 int vcom_socket_is_connection_mode_socket (int __fd);
393 vcom_socket_sendto (int __fd, const void *__buf, size_t __n,
394 int __flags, __CONST_SOCKADDR_ARG __addr,
395 socklen_t __addr_len);
398 vcom_socket_recvfrom (int __fd, void *__restrict __buf, size_t __n,
399 int __flags, __SOCKADDR_ARG __addr,
400 socklen_t * __restrict __addr_len);
403 vcom_socket_sendmsg (int __fd, const struct msghdr *__message, int __flags);
407 vcom_socket_sendmmsg (int __fd, struct mmsghdr *__vmessages,
408 unsigned int __vlen, int __flags);
411 ssize_t vcom_socket_recvmsg (int __fd, struct msghdr *__message, int __flags);
415 vcom_socket_recvmmsg (int __fd, struct mmsghdr *__vmessages,
416 unsigned int __vlen, int __flags,
417 struct timespec *__tmo);
421 vcom_socket_getsockopt (int __fd, int __level, int __optname,
422 void *__restrict __optval,
423 socklen_t * __restrict __optlen);
426 vcom_socket_setsockopt (int __fd, int __level, int __optname,
427 const void *__optval, socklen_t __optlen);
429 int vcom_socket_listen (int __fd, int __n);
432 vcom_socket_accept (int __fd, __SOCKADDR_ARG __addr,
433 socklen_t * __restrict __addr_len);
437 vcom_socket_accept4 (int __fd, __SOCKADDR_ARG __addr,
438 socklen_t * __restrict __addr_len, int __flags);
441 int vcom_socket_shutdown (int __fd, int __how);
443 int vcom_socket_epoll_create1 (int __flags);
446 vcom_socket_epoll_ctl (int __epfd, int __op, int __fd,
447 struct epoll_event *__event);
450 vcom_socket_epoll_pwait (int __epfd, struct epoll_event *__events,
451 int __maxevents, int __timeout,
452 const __sigset_t * __ss);
455 * handle only vcom fds
457 int vcom_socket_poll (struct pollfd *__fds, nfds_t __nfds, int __timeout);
461 vcom_socket_ppoll (struct pollfd *__fds, nfds_t __nfds,
462 const struct timespec *__timeout, const __sigset_t * __ss);
465 #endif /* included_vcom_socket_h */
468 * fd.io coding-style-patch-verification: ON
471 * eval: (c-set-style "gnu")