aboutsummaryrefslogtreecommitdiffstats
path: root/src/vcl/vcom_socket.h
blob: e86bab35672830ed1735ffba490a980493a716b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef included_vcom_socket_h
#define included_vcom_socket_h

#include <string.h>

#include <vcl/vcom_glibc_socket.h>
#include <vppinfra/types.h>
#include <sys/socket.h>

#define INVALID_SESSION_ID (~0)
#define INVALID_FD (~0)

#define INVALID_VEP_IDX INVALID_SESSION_ID
#define INVALID_EPFD INVALID_FD

typedef enum
{
  SOCKET_TYPE_UNBOUND = 0,
  SOCKET_TYPE_KERNEL_BOUND,
  SOCKET_TYPE_VPPCOM_BOUND
} vcom_socket_type_t;

typedef enum
{
  EPOLL_TYPE_UNBOUND = 0,
  EPOLL_TYPE_KERNEL_BOUND,
  EPOLL_TYPE_VPPCOM_BOUND
} vcom_epoll_type_t;

typedef enum
{
  FD_TYPE_INVALID = 0,
  FD_TYPE_KERNEL,
  FD_TYPE_EPOLL,
  FD_TYPE_VCOM_SOCKET,
  /* add new types here */
  /* FD_TYPE_MAX should be the last entry */
  FD_TYPE_MAX
} vcom_fd_type_t;

typedef struct
{
  /* file descriptor -
   * fd 0, 1, 2 have special meaning and are reserved,
   * -1 denote invalid fd */
  i32 fd;

  /* session id - -1 denote invalid sid */
  i32 sid;

  /* socket type */
  vcom_socket_type_t type;

  /* vcom socket attributes here */

} vcom_socket_t;

typedef struct
{
  /* epoll file descriptor -
   * epfd 0, 1, 2 have special meaning and are reserved,
   * -1 denote invalid epfd */
  i32 epfd;

  /* vep idx - -1 denote invalid vep_idx */
  i32 vep_idx;

  /* epoll type */
  vcom_epoll_type_t type;

  /* flags - 0 or EPOLL_CLOEXEC */
  i32 flags;

  /* vcom epoll attributes here */

  /*
   * 00. count of file descriptors currently registered
   *     on this epoll instance.
   * 01. number of file descriptors in the epoll set.
   * 02. EPOLL_CTL_ADD, EPOLL_CTL_MOD, EPOLL_CTL_DEL
   *     update the count.
   * 03. cached for frequent access.
   * */
  i32 count;
  i32 vcl_cnt;
  i32 libc_cnt;

  /* close( ) called on this epoll instance */
  /* 0 - close ( ) not called, 1 - close( ) called. */
  u32 close;

} vcom_epoll_t;

typedef struct
{
  /* "container" of this item */
  i32 epfd;

  /* fd - file descriptor information this item refers to */
  i32 fd;
  /* next and prev fd in the "epoll set" of epfd */
  i32 next_fd;
  i32 prev_fd;

  /* vcom fd type */
  vcom_fd_type_t type;

  /* interested events and the source fd */
  struct epoll_event event;

  /* ready events and the source fd */
  struct epoll_event revent;

  /* epitem attributes here */

} vcom_epitem_t;

typedef union vcom_epitem_key
{
  struct
  {
    i32 fd;
    i32 epfd;
  };
  i64 key;
} __EPOLL_PACKED vcom_epitem_key_t;

static inline char *
vcom_socket_type_str (vcom_socket_type_t t)
{
  switch (t)
    {
    case SOCKET_TYPE_UNBOUND:
      return "SOCKET_TYPE_UNBOUND";

    case SOCKET_TYPE_KERNEL_BOUND:
      return "SOCKET_TYPE_KERNEL_BOUND";

    case SOCKET_TYPE_VPPCOM_BOUND:
      return "SOCKET_TYPE_VPPCOM_BOUND";

    default:
      return "SOCKET_TYPE_UNKNOWN";
    }
}

static inline char *
vcom_socket_epoll_type_str (vcom_epoll_type_t t)
{
  switch (t)
    {
    case EPOLL_TYPE_UNBOUND:
      return "EPOLL_TYPE_UNBOUND";

    case EPOLL_TYPE_KERNEL_BOUND:
      return "EPOLL_TYPE_KERNEL_BOUND";

    case EPOLL_TYPE_VPPCOM_BOUND:
      return "EPOLL_TYPE_VPPCOM_BOUND";

    default:
      return "EPOLL_TYPE_UNKNOWN";
    }
}

static inline char *
vcom_socket_vcom_fd_type_str (vcom_fd_type_t t)
{
  switch (t)
    {
    case FD_TYPE_KERNEL:
      return "FD_TYPE_KERNEL";

    case FD_TYPE_EPOLL:
      return "FD_TYPE_EPOLL";

    case FD_TYPE_VCOM_SOCKET:
      return "FD_TYPE_VCOM_SOCKET";

    default:
      return "FD_TYPE_UNKNOWN";
    }
}

static inline int
vcom_socket_type_is_vppcom_bound (vcom_socket_type_t t)
{
  return t == SOCKET_TYPE_VPPCOM_BOUND;
}

static inline int
vcom_socket_epoll_type_is_vppcom_bound (vcom_epoll_type_t t)
{
  return t == EPOLL_TYPE_VPPCOM_BOUND;
}

static inline void
vsocket_init (vcom_socket_t * vsock)
{
  memset (vsock, 0, sizeof (*vsock));

  vsock->fd = INVALID_FD;
  vsock->sid = INVALID_SESSION_ID;
  vsock->type = SOCKET_TYPE_UNBOUND;
  /* vcom socket attributes init here */
}

static inline void
vepoll_init (vcom_epoll_t * vepoll)
{
  memset (vepoll, 0, sizeof (*vepoll));

  vepoll->epfd = INVALID_EPFD;
  vepoll->vep_idx = INVALID_VEP_IDX;
  vepoll->type = EPOLL_TYPE_UNBOUND;
  vepoll->flags = 0;

  vepoll->count = 0;
  vepoll->close = 0;
  /* vcom epoll attributes init here */
}

static inline void
vepitem_init (vcom_epitem_t * vepitem)
{
  struct epoll_event event = {.events = 0,.data.fd = INVALID_FD };

  memset (vepitem, 0, sizeof (*vepitem));

  vepitem->epfd = INVALID_EPFD;

  vepitem->fd = INVALID_FD;
  vepitem->next_fd = INVALID_FD;
  vepitem->prev_fd = INVALID_FD;

  vepitem->type = FD_TYPE_INVALID;

  vepitem->event = event;
  vepitem->revent = event;
  /* vepoll attributes init here */
}

static inline void
vepitemkey_init (vcom_epitem_key_t * epfdfd)
{
  memset (epfdfd, 0, sizeof (*epfdfd));

  epfdfd->epfd = INVALID_EPFD;
  epfdfd->fd = INVALID_FD;
}

static inline void
vsocket_set (vcom_socket_t * vsock, i32 fd, i32 sid, vcom_socket_type_t type)
{
  vsock->fd = fd;
  vsock->sid = sid;
  vsock->type = type;
  /* vcom socket attributes set here */
}

static inline void
vepoll_set (vcom_epoll_t * vepoll,
	    i32 epfd, i32 vep_idx,
	    vcom_epoll_type_t type, i32 flags, i32 count, u32 close)
{
  vepoll->epfd = epfd;
  vepoll->vep_idx = vep_idx;
  vepoll->type = type;
  vepoll->flags = flags;

  vepoll->count = count;
  vepoll->close = close;
  /* vcom epoll attributes set here */
}

static inline void
vepitem_set (vcom_epitem_t * vepitem,
	     i32 epfd,
	     i32 fd, i32 next_fd, i32 prev_fd,
	     vcom_fd_type_t type,
	     struct epoll_event event, struct epoll_event revent)
{
  vepitem->epfd = epfd;

  vepitem->fd = fd;
  vepitem->next_fd = next_fd;
  vepitem->prev_fd = prev_fd;

  vepitem->type = type;

  vepitem->event = event;
  vepitem->revent = revent;
  /* vcom epitem attributes set here */
}

static inline void
vepitemkey_set (vcom_epitem_key_t * epfdfd, i32 epfd, i32 fd)
{
  epfdfd->epfd = epfd;
  epfdfd->fd = fd;
}

static inline int
vsocket_is_vppcom_bound (vcom_socket_t * vsock)
{
  return vcom_socket_type_is_vppcom_bound (vsock->type);
}

static inline int
vepoll_is_vppcom_bound (vcom_epoll_t * vepoll)
{
  return vcom_socket_epoll_type_is_vppcom_bound (vepoll->type);
}

int vcom_socket_main_init (void);

void vcom_socket_main_destroy (void);

void vcom_socket_main_show (void);

int vcom_socket_is_vcom_fd (int fd);

int vcom_socket_is_vcom_epfd (int epfd);

int vcom_socket_close (int __fd);

ssize_t vcom_socket_read (int __fd, void *__buf, size_t __nbytes);

ssize_t vcom_socket_readv (int __fd, const struct iovec *__iov, int __iovcnt);

ssize_t vcom_socket_write (int __fd, const void *__buf, size_t __n);

ssize_t vcom_socket_writev (int __fd, const struct iovec *__iov,
			    int __iovcnt);

int vcom_socket_fcntl_va (int __fd, int __cmd, va_list __ap);

int vcom_socket_ioctl_va (int __fd, unsigned long int __cmd, va_list __ap);

int
vcom_socket_select (int vcom_nfds, fd_set * __restrict vcom_readfds,
		    fd_set * __restrict vcom_writefds,
		    fd_set * __restrict vcom_exceptfds,
		    struct timeval *__restrict timeout);


int vcom_socket_socket (int __domain, int __type, int __protocol);

int
vcom_socket_socketpair (int __domain, int __type, int __protocol,
			int __fds[2]);

int vcom_socket_bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len);

int
vcom_socket_getsockname (int __fd, __SOCKADDR_ARG __addr,
			 socklen_t * __restrict __len);

int
vcom_socket_connect (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len);

int
vcom_socket_getpeername (int __fd, __SOCKADDR_ARG __addr,
			 socklen_t * __restrict __len);

ssize_t
vcom_socket_send (int __fd, const void *__buf, size_t __n, int __flags);

ssize_t vcom_socket_recv (int __fd, void *__buf, size_t __n, int __flags);

/*
 * RETURN   1 if __fd is (SOCK_STREAM, SOCK_SEQPACKET),
 * 0 otherwise
 * */
int vcom_socket_is_connection_mode_socket (int __fd);

ssize_t
vcom_socket_sendto (int __fd, const void *__buf, size_t __n,
		    int __flags, __CONST_SOCKADDR_ARG __addr,
		    socklen_t __addr_len);

ssize_t
vcom_socket_recvfrom (int __fd, void *__restrict __buf, size_t __n,
		      int __flags, __SOCKADDR_ARG __addr,
		      socklen_t * __restrict __addr_len);

ssize_t
vcom_socket_sendmsg (int __fd, const struct msghdr *__message, int __flags);

#ifdef __USE_GNU
int
vcom_socket_sendmmsg (int __fd, struct mmsghdr *__vmessages,
		      unsigned int __vlen, int __flags);
#endif

ssize_t vcom_socket_recvmsg (int __fd, struct msghdr *__message, int __flags);

#ifdef __USE_GNU
int
vcom_socket_recvmmsg (int __fd, struct mmsghdr *__vmessages,
		      unsigned int __vlen, int __flags,
		      struct timespec *__tmo);
#endif

int
vcom_socket_getsockopt (int __fd, int __level, int __optname,
			void *__restrict __optval,
			socklen_t * __restrict __optlen);

int
vcom_socket_setsockopt (int __fd, int __level, int __optname,
			const void *__optval, socklen_t __optlen);

int vcom_socket_listen (int __fd, int __n);

int
vcom_socket_accept (int __fd, __SOCKADDR_ARG __addr,
		    socklen_t * __restrict __addr_len);

#ifdef __USE_GNU
int
vcom_socket_accept4 (int __fd, __SOCKADDR_ARG __addr,
		     socklen_t * __restrict __addr_len, int __flags);
#endif

int vcom_socket_shutdown (int __fd, int __how);

int vcom_socket_epoll_create1 (int __flags);

int
vcom_socket_epoll_ctl (int __epfd, int __op, int __fd,
		       struct epoll_event *__event);

int
vcom_socket_epoll_pwait (int __epfd, struct epoll_event *__events,
			 int __maxevents, int __timeout,
			 const __sigset_t * __ss);

/*
 * handle only vcom fds
 */
int vcom_socket_poll (struct pollfd *__fds, nfds_t __nfds, int __timeout);

#ifdef __USE_GNU
int
vcom_socket_ppoll (struct pollfd *__fds, nfds_t __nfds,
		   const struct timespec *__timeout, const __sigset_t * __ss);
#endif

#endif /* included_vcom_socket_h */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */