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