blob: 6fcc4e5820eeb6a76018c10deb3970b35b231dac [file] [log] [blame]
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001/*
2 * Copyright (c) 2016 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#include <unistd.h>
16#include <stdio.h>
17#include <sys/uio.h>
18#include <limits.h>
19#define __need_IOV_MAX
20#include <bits/stdio_lim.h>
Stevenb59f2272017-10-12 17:10:33 -070021#include <netinet/tcp.h>
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -070022
23#include <vppinfra/types.h>
Dave Wallacee695cb42017-11-02 22:04:42 -040024#include <vppinfra/time.h>
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -070025#include <vppinfra/hash.h>
26#include <vppinfra/pool.h>
27
Dave Wallace5c7cf1c2017-10-24 04:12:18 -040028#include <vcl/vcom_socket.h>
29#include <vcl/vcom_socket_wrapper.h>
30#include <vcl/vcom.h>
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -070031
Dave Wallace5c7cf1c2017-10-24 04:12:18 -040032#include <vcl/vppcom.h>
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -070033
Damjan Marionc981b092017-10-29 19:28:31 +010034#ifndef IOV_MAX
35#define IOV_MAX __IOV_MAX
36#endif
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -070037
38/*
39 * VCOM_SOCKET Private definitions and functions.
40 */
41
42typedef struct vcom_socket_main_t_
43{
44 u8 init;
Dave Wallacee695cb42017-11-02 22:04:42 -040045 clib_time_t clib_time;
46 pid_t my_pid;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -070047
48 /* vcom_socket pool */
49 vcom_socket_t *vsockets;
50
51 /* Hash table for socketidx to fd mapping */
52 uword *sockidx_by_fd;
53
54 /* vcom_epoll pool */
55 vcom_epoll_t *vepolls;
56
57 /* Hash table for epollidx to epfd mapping */
58 uword *epollidx_by_epfd;
59
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -070060 /* common epitem poll for all epfd */
61 /* TBD: epitem poll per epfd */
62 /* vcom_epitem pool */
63 vcom_epitem_t *vepitems;
64
65 /* Hash table for epitemidx to epfdfd mapping */
66 uword *epitemidx_by_epfdfd;
67
68 /* Hash table - key:epfd, value:vec of epitemidx */
69 uword *epitemidxs_by_epfd;
70 /* Hash table - key:fd, value:vec of epitemidx */
71 uword *epitemidxs_by_fd;
72
73} vcom_socket_main_t;
74
75vcom_socket_main_t vcom_socket_main;
76
77
78static int
79vcom_socket_open_socket (int domain, int type, int protocol)
80{
81 int rv = -1;
82
83 /* handle domains implemented by vpp */
84 switch (domain)
85 {
86 case AF_INET:
87 case AF_INET6:
88 /* get socket type and
89 * handle the socket types supported by vpp */
90 switch (type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
91 {
92 case SOCK_STREAM:
93 case SOCK_DGRAM:
94 /* the type argument serves a second purpose,
95 * in addition to specifying a socket type,
96 * it may include the bitwise OR of any of
97 * SOCK_NONBLOCK and SOCK_CLOEXEC, to modify
98 * the behavior of socket. */
99 rv = libc_socket (domain, type, protocol);
100 if (rv == -1)
101 rv = -errno;
102 break;
103
104 default:
105 break;
106 }
107
108 break;
109
110 default:
111 break;
112 }
113
114 return rv;
115}
116
117static int
118vcom_socket_open_epoll (int flags)
119{
120 int rv = -1;
121
122 if (flags < 0)
123 {
124 return -EINVAL;
125 }
126 if (flags && (flags & ~EPOLL_CLOEXEC))
127 {
128 return -EINVAL;
129 }
130
131 /* flags can be either zero or EPOLL_CLOEXEC */
132 rv = libc_epoll_create1 (flags);
133 if (rv == -1)
134 rv = -errno;
135
136 return rv;
137}
138
139static int
140vcom_socket_close_socket (int fd)
141{
142 int rv;
143
144 rv = libc_close (fd);
145 if (rv == -1)
146 rv = -errno;
147
148 return rv;
149}
150
151static int
152vcom_socket_close_epoll (int epfd)
153{
154 int rv;
155
156 rv = libc_close (epfd);
157 if (rv == -1)
158 rv = -errno;
159
160 return rv;
161}
162
163/*
164 * Public API functions
165 */
166
167
168int
169vcom_socket_is_vcom_fd (int fd)
170{
171 vcom_socket_main_t *vsm = &vcom_socket_main;
172 uword *p;
173 vcom_socket_t *vsock;
174
175 p = hash_get (vsm->sockidx_by_fd, fd);
176
177 if (p)
178 {
179 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
180 if (vsock && vsock->type == SOCKET_TYPE_VPPCOM_BOUND)
181 return 1;
182 }
183 return 0;
184}
185
186int
187vcom_socket_is_vcom_epfd (int epfd)
188{
189 vcom_socket_main_t *vsm = &vcom_socket_main;
190 uword *p;
191 vcom_epoll_t *vepoll;
192
193 p = hash_get (vsm->epollidx_by_epfd, epfd);
194
195 if (p)
196 {
197 vepoll = pool_elt_at_index (vsm->vepolls, p[0]);
198 if (vepoll && vepoll->type == EPOLL_TYPE_VPPCOM_BOUND)
199 return 1;
200 }
201 return 0;
202}
203
204static inline int
205vcom_socket_get_sid (int fd)
206{
207 vcom_socket_main_t *vsm = &vcom_socket_main;
208 uword *p;
209 vcom_socket_t *vsock;
210
211 p = hash_get (vsm->sockidx_by_fd, fd);
212
213 if (p)
214 {
215 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
216 if (vsock && vsock->type == SOCKET_TYPE_VPPCOM_BOUND)
217 return vsock->sid;
218 }
219 return INVALID_SESSION_ID;
220}
221
222static inline int
223vcom_socket_get_vep_idx (int epfd)
224{
225 vcom_socket_main_t *vsm = &vcom_socket_main;
226 uword *p;
227 vcom_epoll_t *vepoll;
228
229 p = hash_get (vsm->epollidx_by_epfd, epfd);
230
231 if (p)
232 {
233 vepoll = pool_elt_at_index (vsm->vepolls, p[0]);
234 if (vepoll && vepoll->type == EPOLL_TYPE_VPPCOM_BOUND)
235 return vepoll->vep_idx;
236 }
237 return INVALID_VEP_IDX;
238}
239
240static inline int
241vcom_socket_get_sid_and_vsock (int fd, vcom_socket_t ** vsockp)
242{
243 vcom_socket_main_t *vsm = &vcom_socket_main;
244 uword *p;
245 vcom_socket_t *vsock;
246
247 p = hash_get (vsm->sockidx_by_fd, fd);
248
249 if (p)
250 {
251 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
252 if (vsock && vsock->type == SOCKET_TYPE_VPPCOM_BOUND)
253 {
254 *vsockp = vsock;
255 return vsock->sid;
256 }
257 }
258 return INVALID_SESSION_ID;
259}
260
261static inline int
262vcom_socket_get_vep_idx_and_vepoll (int epfd, vcom_epoll_t ** vepollp)
263{
264 vcom_socket_main_t *vsm = &vcom_socket_main;
265 uword *p;
266 vcom_epoll_t *vepoll;
267
268 p = hash_get (vsm->epollidx_by_epfd, epfd);
269
270 if (p)
271 {
272 vepoll = pool_elt_at_index (vsm->vepolls, p[0]);
273 if (vepoll && vepoll->type == EPOLL_TYPE_VPPCOM_BOUND)
274 {
275 *vepollp = vepoll;
276 return vepoll->vep_idx;
277 }
278 }
279 return INVALID_VEP_IDX;
280}
281
282
283static int
284vcom_socket_close_vepoll (int epfd)
285{
286 int rv = -1;
287 vcom_socket_main_t *vsm = &vcom_socket_main;
288 uword *p;
289 vcom_epoll_t *vepoll;
290
291 p = hash_get (vsm->epollidx_by_epfd, epfd);
292 if (!p)
293 return -EBADF;
294
295 vepoll = pool_elt_at_index (vsm->vepolls, p[0]);
296 if (!vepoll)
297 return -EBADF;
298
299 if (vepoll->type != EPOLL_TYPE_VPPCOM_BOUND)
300 return -EINVAL;
301
302 if (vepoll->count)
303 {
304 if (!vepoll->close)
305 {
306 vepoll->close = 1;
307 return 0;
308 }
309 else
310 {
311 return -EBADF;
312 }
313 }
314
315 /* count is zero */
316 rv = vppcom_session_close (vepoll->vep_idx);
317 rv = vcom_socket_close_epoll (vepoll->epfd);
318
319 vepoll_init (vepoll);
320 hash_unset (vsm->epollidx_by_epfd, epfd);
321 pool_put (vsm->vepolls, vepoll);
322
323 return rv;
324}
325
326static int
327vcom_socket_close_vsock (int fd)
328{
329 int rv = -1;
330 vcom_socket_main_t *vsm = &vcom_socket_main;
331 uword *p;
332 vcom_socket_t *vsock;
333
334 vcom_epitem_t *vepitem;
335
336 i32 *vepitemidxs = 0;
337 i32 *vepitemidxs_var = 0;
338
339 p = hash_get (vsm->sockidx_by_fd, fd);
340 if (!p)
341 return -EBADF;
342
343 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
344 if (!vsock)
345 return -ENOTSOCK;
346
347 if (vsock->type != SOCKET_TYPE_VPPCOM_BOUND)
348 return -EINVAL;
349
350 rv = vppcom_session_close (vsock->sid);
351 rv = vcom_socket_close_socket (vsock->fd);
352
353 vsocket_init (vsock);
354 hash_unset (vsm->sockidx_by_fd, fd);
355 pool_put (vsm->vsockets, vsock);
356
357 /*
358 * NOTE:
359 * Before calling close(), user should remove
360 * this fd from the epoll-set of all epoll instances,
361 * otherwise resource(epitems) leaks ensues.
362 */
363
364 /*
365 * 00. close all epoll instances that are marked as "close"
366 * of which this fd is the "last" remaining member.
367 * 01. epitems associated with this fd are intentionally
368 * not removed, see NOTE: above.
369 * */
370
371 /* does this fd participate in epoll */
372 p = hash_get (vsm->epitemidxs_by_fd, fd);
373 if (p)
374 {
375 vepitemidxs = *(i32 **) p;
376 vec_foreach (vepitemidxs_var, vepitemidxs)
377 {
378 vepitem = pool_elt_at_index (vsm->vepitems, vepitemidxs_var[0]);
379 if (vepitem && vepitem->fd == fd &&
380 vepitem->type == FD_TYPE_VCOM_SOCKET)
381 {
382 i32 vep_idx;
383 vcom_epoll_t *vepoll;
384 if ((vep_idx =
385 vcom_socket_get_vep_idx_and_vepoll (vepitem->epfd,
386 &vepoll)) !=
387 INVALID_VEP_IDX)
388 {
389 if (vepoll->close)
390 {
391 if (vepoll->count == 1)
392 {
393 /*
394 * force count to zero and
395 * close this epoll instance
396 * */
397 vepoll->count = 0;
398 vcom_socket_close_vepoll (vepoll->epfd);
399 }
400 else
401 {
402 vepoll->count -= 1;
403 }
404 }
405 }
406 }
407
408 }
409 }
410
411 return rv;
412}
413
414int
415vcom_socket_close (int __fd)
416{
417 int rv;
418
419 if (vcom_socket_is_vcom_fd (__fd))
420 {
421 rv = vcom_socket_close_vsock (__fd);
422 }
423 else if (vcom_socket_is_vcom_epfd (__fd))
424 {
425 rv = vcom_socket_close_vepoll (__fd);
426 }
427 else
428 {
429 rv = -EBADF;
430 }
431
432 return rv;
433}
434
435ssize_t
436vcom_socket_read (int __fd, void *__buf, size_t __nbytes)
437{
438 int rv = -1;
439 vcom_socket_main_t *vsm = &vcom_socket_main;
440 uword *p;
441 vcom_socket_t *vsock;
442
443 p = hash_get (vsm->sockidx_by_fd, __fd);
444 if (!p)
445 return -EBADF;
446
447 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
448 if (!vsock)
449 return -ENOTSOCK;
450
451 if (vsock->type != SOCKET_TYPE_VPPCOM_BOUND)
452 return -EINVAL;
453
Dave Wallace5c7cf1c2017-10-24 04:12:18 -0400454 if (!__buf)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700455 {
456 return -EINVAL;
457 }
458
459 rv = vcom_fcntl (__fd, F_GETFL, 0);
460 if (rv < 0)
461 {
462 return rv;
463
464 }
465
466 /* is blocking */
467 if (!(rv & O_NONBLOCK))
468 {
469 do
470 {
471 rv = vppcom_session_read (vsock->sid, __buf, __nbytes);
472 }
Dave Wallace60f54822017-10-24 20:47:45 -0400473 /* coverity[CONSTANT_EXPRESSION_RESULT] */
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700474 while (rv == -EAGAIN || rv == -EWOULDBLOCK);
475 return rv;
476 }
477 /* The file descriptor refers to a socket and has been
478 * marked nonblocking(O_NONBLOCK) and the read would
479 * block.
480 * */
481 /* is non blocking */
482 rv = vppcom_session_read (vsock->sid, __buf, __nbytes);
483 return rv;
484}
485
486ssize_t
487vcom_socket_readv (int __fd, const struct iovec * __iov, int __iovcnt)
488{
489 int rv;
490 vcom_socket_main_t *vsm = &vcom_socket_main;
491 uword *p;
492 vcom_socket_t *vsock;
493 ssize_t total = 0, len = 0;
Dave Wallace5c7cf1c2017-10-24 04:12:18 -0400494 int i;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700495
496 p = hash_get (vsm->sockidx_by_fd, __fd);
497 if (!p)
498 return -EBADF;
499
500 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
501 if (!vsock)
502 return -ENOTSOCK;
503
504 if (vsock->type != SOCKET_TYPE_VPPCOM_BOUND)
505 return -EINVAL;
506
507 if (__iov == 0 || __iovcnt == 0 || __iovcnt > IOV_MAX)
508 return -EINVAL;
509
510 /* Sanity check */
Dave Wallace5c7cf1c2017-10-24 04:12:18 -0400511 for (i = 0; i < __iovcnt; ++i)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700512 {
513 if (SSIZE_MAX - len < __iov[i].iov_len)
514 return -EINVAL;
515 len += __iov[i].iov_len;
516 }
517
518 rv = vcom_fcntl (__fd, F_GETFL, 0);
519 if (rv < 0)
520 {
521 return rv;
522 }
523
524 /* is blocking */
525 if (!(rv & O_NONBLOCK))
526 {
527 do
528 {
Dave Wallace5c7cf1c2017-10-24 04:12:18 -0400529 for (i = 0; i < __iovcnt; ++i)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700530 {
531 rv = vppcom_session_read (vsock->sid, __iov[i].iov_base,
532 __iov[i].iov_len);
533 if (rv < 0)
534 break;
535 else
536 {
537 total += rv;
538 if (rv < __iov[i].iov_len)
539 /* Read less than buffer provided, no point to continue */
540 break;
541 }
542 }
543 }
Dave Wallace60f54822017-10-24 20:47:45 -0400544 /* coverity[CONSTANT_EXPRESSION_RESULT] */
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700545 while ((rv == -EAGAIN || rv == -EWOULDBLOCK) && total == 0);
546 return total;
547 }
548
549 /* is non blocking */
Dave Wallace5c7cf1c2017-10-24 04:12:18 -0400550 for (i = 0; i < __iovcnt; ++i)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700551 {
552 rv = vppcom_session_read (vsock->sid, __iov[i].iov_base,
553 __iov[i].iov_len);
554 if (rv < 0)
555 {
556 if (total > 0)
557 break;
558 else
559 {
560 errno = rv;
561 return rv;
562 }
563 }
564 else
565 {
566 total += rv;
567 if (rv < __iov[i].iov_len)
568 /* Read less than buffer provided, no point to continue */
569 break;
570 }
571 }
572 return total;
573}
574
575ssize_t
576vcom_socket_write (int __fd, const void *__buf, size_t __n)
577{
578 int rv = -1;
579 vcom_socket_main_t *vsm = &vcom_socket_main;
580 uword *p;
581 vcom_socket_t *vsock;
582
Dave Wallace5c7cf1c2017-10-24 04:12:18 -0400583 if (!__buf)
584 {
585 return -EINVAL;
586 }
587
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700588 p = hash_get (vsm->sockidx_by_fd, __fd);
589 if (!p)
590 return -EBADF;
591
592 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
593 if (!vsock)
594 return -ENOTSOCK;
595
596 if (vsock->type != SOCKET_TYPE_VPPCOM_BOUND)
597 return -EINVAL;
598
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700599 rv = vppcom_session_write (vsock->sid, (void *) __buf, __n);
600 return rv;
601}
602
603ssize_t
604vcom_socket_writev (int __fd, const struct iovec * __iov, int __iovcnt)
605{
606 int rv = -1;
607 ssize_t total = 0;
608 vcom_socket_main_t *vsm = &vcom_socket_main;
609 uword *p;
610 vcom_socket_t *vsock;
Dave Wallace5c7cf1c2017-10-24 04:12:18 -0400611 int i;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700612
613 p = hash_get (vsm->sockidx_by_fd, __fd);
614 if (!p)
615 return -EBADF;
616
617 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
618 if (!vsock)
619 return -ENOTSOCK;
620
621 if (vsock->type != SOCKET_TYPE_VPPCOM_BOUND)
622 return -EINVAL;
623
624 if (__iov == 0 || __iovcnt == 0 || __iovcnt > IOV_MAX)
625 return -EINVAL;
626
Dave Wallace5c7cf1c2017-10-24 04:12:18 -0400627 for (i = 0; i < __iovcnt; ++i)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700628 {
629 rv = vppcom_session_write (vsock->sid, __iov[i].iov_base,
630 __iov[i].iov_len);
631 if (rv < 0)
632 {
633 if (total > 0)
634 break;
635 else
636 return rv;
637 }
638 else
639 total += rv;
640 }
641 return total;
642}
643
644/*
645 * RETURN: 0 - invalid cmd
646 * 1 - cmd not handled by vcom and vppcom
647 * 2 - cmd handled by vcom socket resource
648 * 3 - cmd handled by vppcom
649 * */
650/* TBD: incomplete list of cmd */
651static int
652vcom_socket_check_fcntl_cmd (int __cmd)
653{
654 switch (__cmd)
655 {
656 /*cmd not handled by vcom and vppcom */
657 /* Fallthrough */
658 case F_DUPFD:
659 case F_DUPFD_CLOEXEC:
660 return 1;
661
662 /* cmd handled by vcom socket resource */
663 /* Fallthrough */
664 case F_GETFD:
665 case F_SETFD:
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700666 case F_GETLK:
667 case F_SETLK:
668 case F_SETLKW:
669 case F_GETOWN:
670 case F_SETOWN:
671 return 2;
672
Stevenb59f2272017-10-12 17:10:33 -0700673 /* cmd handled by vcom and vppcom */
674 case F_SETFL:
675 case F_GETFL:
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700676 return 3;
Stevenb59f2272017-10-12 17:10:33 -0700677
678 /* cmd not handled by vcom and vppcom */
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700679 default:
Stevenb59f2272017-10-12 17:10:33 -0700680 return 1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700681 }
682 return 0;
683}
684
Dave Wallacee22aa742017-10-20 12:30:38 -0400685static inline int
686vcom_session_fcntl_va (int __sid, int __cmd, va_list __ap)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700687{
Stevenb59f2272017-10-12 17:10:33 -0700688 int flags = va_arg (__ap, int);
689 int rv = -EOPNOTSUPP;
690 uint32_t size;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700691
Stevenb59f2272017-10-12 17:10:33 -0700692 size = sizeof (flags);
693 if (__cmd == F_SETFL)
694 {
695 rv = vppcom_session_attr (__sid, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
696 }
697 else if (__cmd == F_GETFL)
698 {
699 rv = vppcom_session_attr (__sid, VPPCOM_ATTR_GET_FLAGS, &flags, &size);
700 if (rv == VPPCOM_OK)
701 rv = flags;
702 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700703
704 return rv;
705}
706
707int
708vcom_socket_fcntl_va (int __fd, int __cmd, va_list __ap)
709{
710 int rv = -EBADF;
711 vcom_socket_main_t *vsm = &vcom_socket_main;
712 uword *p;
713 vcom_socket_t *vsock;
714
715 p = hash_get (vsm->sockidx_by_fd, __fd);
716 if (!p)
717 return -EBADF;
718
719 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
720 if (!vsock)
721 return -ENOTSOCK;
722
723 if (vsock->type != SOCKET_TYPE_VPPCOM_BOUND)
724 return -EINVAL;
725
726 switch (vcom_socket_check_fcntl_cmd (__cmd))
727 {
728 /* invalid cmd */
729 case 0:
730 rv = -EBADF;
731 break;
732 /*cmd not handled by vcom and vppcom */
733 case 1:
Stevenb59f2272017-10-12 17:10:33 -0700734 rv = libc_vfcntl (vsock->fd, __cmd, __ap);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700735 break;
736 /* cmd handled by vcom socket resource */
737 case 2:
738 rv = libc_vfcntl (vsock->fd, __cmd, __ap);
739 break;
740 /* cmd handled by vppcom */
741 case 3:
Dave Wallacee22aa742017-10-20 12:30:38 -0400742 rv = vcom_session_fcntl_va (vsock->sid, __cmd, __ap);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700743 break;
744
745 default:
746 rv = -EINVAL;
747 break;
748 }
749
750 return rv;
751}
752
Stevenb59f2272017-10-12 17:10:33 -0700753/*
754 * RETURN: 0 - invalid cmd
755 * 1 - cmd not handled by vcom and vppcom
756 * 2 - cmd handled by vcom socket resource
757 * 3 - cmd handled by vppcom
758 */
759static int
760vcom_socket_check_ioctl_cmd (unsigned long int __cmd)
761{
762 int rc;
763
764 switch (__cmd)
765 {
766 /* cmd handled by vppcom */
767 case FIONREAD:
768 rc = 3;
769 break;
770
771 /* cmd not handled by vcom and vppcom */
772 default:
773 rc = 1;
774 break;
775 }
776 return rc;
777}
778
Dave Wallacee22aa742017-10-20 12:30:38 -0400779static inline int
780vcom_session_ioctl_va (int __sid, int __cmd, va_list __ap)
Stevenb59f2272017-10-12 17:10:33 -0700781{
782 int rv;
783
Dave Wallace59179392017-11-07 02:20:07 -0500784 switch (__cmd)
785 {
786 case FIONREAD:
787 rv = vppcom_session_attr (__sid, VPPCOM_ATTR_GET_NREAD, 0, 0);
788 break;
789
790 case FIONBIO:
791 {
792 u32 flags = va_arg (__ap, int) ? O_NONBLOCK : 0;
793 u32 len = sizeof (flags);
794 rv = vppcom_session_attr (__sid, VPPCOM_ATTR_SET_FLAGS, &flags, &len);
795 }
796 break;
797
798 default:
799 rv = -EOPNOTSUPP;
800 break;
801 }
Stevenb59f2272017-10-12 17:10:33 -0700802 return rv;
803}
804
805int
806vcom_socket_ioctl_va (int __fd, unsigned long int __cmd, va_list __ap)
807{
808 int rv = -EBADF;
809 vcom_socket_main_t *vsm = &vcom_socket_main;
810 uword *p;
811 vcom_socket_t *vsock;
812
813 p = hash_get (vsm->sockidx_by_fd, __fd);
814 if (!p)
815 return -EBADF;
816
817 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
818 if (!vsock)
819 return -ENOTSOCK;
820
821 if (vsock->type != SOCKET_TYPE_VPPCOM_BOUND)
822 return -EINVAL;
823
824 switch (vcom_socket_check_ioctl_cmd (__cmd))
825 {
826 /* Not supported cmd */
827 case 0:
828 rv = -EOPNOTSUPP;
829 break;
830
831 /* cmd not handled by vcom and vppcom */
832 case 1:
833 rv = libc_vioctl (vsock->fd, __cmd, __ap);
834 break;
835
836 /* cmd handled by vcom socket resource */
837 case 2:
838 rv = libc_vioctl (vsock->fd, __cmd, __ap);
839 break;
840
841 /* cmd handled by vppcom */
842 case 3:
Dave Wallacee22aa742017-10-20 12:30:38 -0400843 rv = vcom_session_ioctl_va (vsock->sid, __cmd, __ap);
Stevenb59f2272017-10-12 17:10:33 -0700844 break;
845
846 default:
847 rv = -EINVAL;
848 break;
849 }
850
851 return rv;
852}
853
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700854static inline int
855vcom_socket_fds_2_sid_fds (
856 /* dest */
857 int *vcom_nsid_fds,
858 fd_set * __restrict vcom_rd_sid_fds,
859 fd_set * __restrict vcom_wr_sid_fds,
860 fd_set * __restrict vcom_ex_sid_fds,
861 /* src */
862 int vcom_nfds,
863 fd_set * __restrict vcom_readfds,
864 fd_set * __restrict vcom_writefds,
865 fd_set * __restrict vcom_exceptfds)
866{
867 int rv = 0;
868 int fd;
869 int sid;
870 /* invalid max_sid is -1 */
871 int max_sid = -1;
872 int nsid = 0;
873
874 /*
875 * set sid in sid sets corresponding to fd's in fd sets
876 * compute nsid and vcom_nsid_fds from sid sets
877 */
878
879 for (fd = 0; fd < vcom_nfds; fd++)
880 {
881 /*
882 * F fd set, src
883 * S sid set, dest
884 */
885#define _(S,F) \
886 if ((F) && (S) && FD_ISSET (fd, (F))) \
887 { \
888 sid = vcom_socket_get_sid (fd); \
889 if (sid != INVALID_SESSION_ID) \
890 { \
891 FD_SET (sid, (S)); \
892 if (sid > max_sid) \
893 { \
894 max_sid = sid; \
895 } \
896 ++nsid; \
897 } \
898 else \
899 { \
900 rv = -EBADFD; \
901 goto done; \
902 } \
903 }
904
905
906 _(vcom_rd_sid_fds, vcom_readfds);
907 _(vcom_wr_sid_fds, vcom_writefds);
908 _(vcom_ex_sid_fds, vcom_exceptfds);
909#undef _
910 }
911
912 *vcom_nsid_fds = max_sid != -1 ? max_sid + 1 : 0;
913 rv = nsid;
914
915done:
916 return rv;
917}
918
919/*
920 * PRE: 00. sid sets were derived from fd sets
921 * 01. sid sets were updated with sids that actually changed
922 * status
923 * 02. fd sets still has watched fds
924 *
925 * This function will modify in place fd sets to indicate which fd's
926 * actually changed status(inferred from sid sets)
927 */
928static inline int
929vcom_socket_sid_fds_2_fds (
930 /* dest */
931 int *new_vcom_nfds,
932 int vcom_nfds,
933 fd_set * __restrict vcom_readfds,
934 fd_set * __restrict vcom_writefds,
935 fd_set * __restrict vcom_exceptfds,
936 /* src */
937 int vcom_nsid_fds,
938 fd_set * __restrict vcom_rd_sid_fds,
939 fd_set * __restrict vcom_wr_sid_fds,
940 fd_set * __restrict vcom_ex_sid_fds)
941{
942 int rv = 0;
943 int fd;
944 int sid;
945 /* invalid max_fd is -1 */
946 int max_fd = -1;
947 int nfd = 0;
948
949
950 /*
951 * modify in place fd sets to indicate which fd's
952 * actually changed status(inferred from sid sets)
953 */
954 for (fd = 0; fd < vcom_nfds; fd++)
955 {
956 /*
957 * F fd set, dest
958 * S sid set, src
959 */
960#define _(S,F) \
961 if ((F) && (S) && FD_ISSET (fd, (F))) \
962 { \
963 sid = vcom_socket_get_sid (fd); \
964 if (sid != INVALID_SESSION_ID) \
965 { \
966 if (!FD_ISSET (sid, (S))) \
967 { \
968 FD_CLR(fd, (F)); \
969 } \
970 } \
971 else \
972 { \
973 rv = -EBADFD; \
974 goto done; \
975 } \
976 }
977
978
979 _(vcom_rd_sid_fds, vcom_readfds);
980 _(vcom_wr_sid_fds, vcom_writefds);
981 _(vcom_ex_sid_fds, vcom_exceptfds);
982#undef _
983 }
984
985 /*
986 * compute nfd and new_vcom_nfds from fd sets
987 */
988 for (fd = 0; fd < vcom_nfds; fd++)
989 {
990
991#define _(F) \
992 if ((F) && FD_ISSET (fd, (F))) \
993 { \
994 if (fd > max_fd) \
995 { \
996 max_fd = fd; \
997 } \
998 ++nfd; \
999 }
1000
1001
1002 _(vcom_readfds);
1003 _(vcom_writefds);
1004 _(vcom_exceptfds);
1005#undef _
1006
1007 }
1008
1009 *new_vcom_nfds = max_fd != -1 ? max_fd + 1 : 0;
1010 rv = nfd;
1011
1012done:
1013 return rv;
1014}
1015
1016/*
1017 * PRE:
1018 * vom_socket_select is always called with
1019 * timeout->tv_sec and timeout->tv_usec set to zero.
1020 * hence vppcom_select return immediately.
1021 */
1022/*
1023 * TBD: do{body;} while(timeout conditional); timeout loop
1024 */
1025int
1026vcom_socket_select (int vcom_nfds, fd_set * __restrict vcom_readfds,
1027 fd_set * __restrict vcom_writefds,
1028 fd_set * __restrict vcom_exceptfds,
1029 struct timeval *__restrict timeout)
1030{
Dave Wallacee22aa742017-10-20 12:30:38 -04001031 static unsigned long vcom_nsid_fds = 0;
1032 int vcom_nsid = 0;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001033 int rv = -EBADF;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001034
1035 int new_vcom_nfds = 0;
1036 int new_vcom_nfd = 0;
1037
1038 /* vcom sid fds */
1039 fd_set vcom_rd_sid_fds;
1040 fd_set vcom_wr_sid_fds;
1041 fd_set vcom_ex_sid_fds;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001042
1043 /* in seconds eg. 3.123456789 seconds */
1044 double time_to_wait = (double) 0;
1045
1046 /* validate inputs */
1047 if (vcom_nfds < 0)
1048 {
1049 return -EINVAL;
1050 }
1051
1052 /* convert timeval timeout to double time_to_wait */
1053 if (timeout)
1054 {
1055 if (timeout->tv_sec == 0 && timeout->tv_usec == 0)
1056 {
1057 /* polling: vppcom_select returns immediately */
1058 time_to_wait = (double) 0;
1059 }
1060 else
1061 {
1062 /*TBD: use timeval api */
1063 time_to_wait = (double) timeout->tv_sec +
1064 (double) timeout->tv_usec / (double) 1000000 +
1065 (double) (timeout->tv_usec % 1000000) / (double) 1000000;
1066 }
1067 }
1068 else
1069 {
1070 /*
1071 * no timeout: vppcom_select can block indefinitely
1072 * waiting for a file descriptor to become ready
1073 * */
1074 /* set to a phantom value */
1075 time_to_wait = ~0;
1076 }
1077
1078 /* zero the sid_sets */
1079 /*
1080 * F fd set
1081 * S sid set
1082 */
1083#define _(S,F) \
1084 if ((F)) \
1085 { \
1086 FD_ZERO ((S)); \
1087 }
1088
1089
1090 _(&vcom_rd_sid_fds, vcom_readfds);
1091 _(&vcom_wr_sid_fds, vcom_writefds);
1092 _(&vcom_ex_sid_fds, vcom_exceptfds);
1093#undef _
1094
Dave Wallacee22aa742017-10-20 12:30:38 -04001095 if (vcom_nfds == 0)
1096 {
1097 if (time_to_wait > 0)
1098 {
1099 if (VCOM_DEBUG > 0)
1100 fprintf (stderr,
1101 "[%d] vcom_socket_select called to "
Dave Wallace59179392017-11-07 02:20:07 -05001102 "emulate delay_ns()!\n", getpid ());
Dave Wallacee22aa742017-10-20 12:30:38 -04001103 rv = vppcom_select (0, NULL, NULL, NULL, time_to_wait);
1104 }
1105 else
1106 {
1107 fprintf (stderr, "[%d] vcom_socket_select called vcom_nfds = 0 "
Dave Wallacee695cb42017-11-02 22:04:42 -04001108 "and invalid time_to_wait (%f)!\n",
Dave Wallace59179392017-11-07 02:20:07 -05001109 getpid (), time_to_wait);
Dave Wallacee22aa742017-10-20 12:30:38 -04001110 }
1111 return 0;
1112 }
1113
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001114 /* populate read, write and except sid_sets */
1115 vcom_nsid = vcom_socket_fds_2_sid_fds (
1116 /* dest */
1117 vcom_readfds || vcom_writefds
1118 || vcom_exceptfds ? (int *)
1119 &vcom_nsid_fds : NULL,
1120 vcom_readfds ? &vcom_rd_sid_fds :
1121 NULL,
1122 vcom_writefds ? &vcom_wr_sid_fds :
1123 NULL,
1124 vcom_exceptfds ? &vcom_ex_sid_fds :
1125 NULL,
1126 /* src */
1127 vcom_nfds,
1128 vcom_readfds,
1129 vcom_writefds, vcom_exceptfds);
1130 if (vcom_nsid < 0)
1131 {
1132 return vcom_nsid;
1133 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001134
1135 rv = vppcom_select (vcom_nsid_fds,
1136 vcom_readfds ? (unsigned long *) &vcom_rd_sid_fds :
1137 NULL,
1138 vcom_writefds ? (unsigned long *) &vcom_wr_sid_fds :
1139 NULL,
1140 vcom_exceptfds ? (unsigned long *) &vcom_ex_sid_fds :
1141 NULL, time_to_wait);
Dave Wallacee22aa742017-10-20 12:30:38 -04001142 if (VCOM_DEBUG > 2)
1143 fprintf (stderr, "[%d] called vppcom_select(): "
Dave Wallace59179392017-11-07 02:20:07 -05001144 "'%04d'='%04d'\n", getpid (), rv, (int) vcom_nsid_fds);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001145
1146 /* check if any file descriptors changed status */
1147 if (rv > 0)
1148 {
1149 /*
1150 * on exit, sets are modified in place to indicate which
1151 * file descriptors actually changed status
1152 * */
1153
1154 /*
1155 * comply with pre-condition
1156 * do not clear vcom fd sets befor calling
1157 * vcom_socket_sid_fds_2_fds
1158 */
1159 new_vcom_nfd = vcom_socket_sid_fds_2_fds (
1160 /* dest */
1161 &new_vcom_nfds,
1162 vcom_nfds,
1163 vcom_readfds,
1164 vcom_writefds,
1165 vcom_exceptfds,
1166 /* src */
1167 vcom_nsid_fds,
1168 vcom_readfds ?
1169 &vcom_rd_sid_fds : NULL,
1170 vcom_writefds ?
1171 &vcom_wr_sid_fds : NULL,
1172 vcom_exceptfds ?
1173 &vcom_ex_sid_fds : NULL);
1174 if (new_vcom_nfd < 0)
1175 {
1176 return new_vcom_nfd;
1177 }
1178 if (new_vcom_nfds < 0)
1179 {
1180 return -EINVAL;
1181 }
1182 rv = new_vcom_nfd;
1183 }
1184 return rv;
1185}
1186
1187
1188int
1189vcom_socket_socket (int __domain, int __type, int __protocol)
1190{
1191 int rv = -1;
1192 vcom_socket_main_t *vsm = &vcom_socket_main;
1193 vcom_socket_t *vsock;
1194
1195 i32 fd;
1196 i32 sid;
1197 i32 sockidx;
1198 u8 is_nonblocking = __type & SOCK_NONBLOCK ? 1 : 0;
1199 int type = __type & ~(SOCK_NONBLOCK | SOCK_CLOEXEC);
1200
1201 fd = vcom_socket_open_socket (__domain, __type, __protocol);
1202 if (fd < 0)
1203 {
1204 rv = fd;
1205 goto out;
1206 }
1207
1208 sid = vppcom_session_create (VPPCOM_VRF_DEFAULT,
1209 (type == SOCK_DGRAM) ?
1210 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP,
1211 is_nonblocking);
1212 if (sid < 0)
1213 {
1214 rv = sid;
1215 goto out_close_socket;
1216 }
1217
1218 pool_get (vsm->vsockets, vsock);
1219 vsocket_init (vsock);
1220
1221 sockidx = vsock - vsm->vsockets;
1222 hash_set (vsm->sockidx_by_fd, fd, sockidx);
1223
1224 vsocket_set (vsock, fd, sid, SOCKET_TYPE_VPPCOM_BOUND);
1225 return fd;
1226
1227out_close_socket:
1228 vcom_socket_close_socket (fd);
1229out:
1230 return rv;
1231}
1232
1233int
1234vcom_socket_socketpair (int __domain, int __type, int __protocol,
1235 int __fds[2])
1236{
1237/* TBD: */
1238 return 0;
1239}
1240
1241int
1242vcom_socket_bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
1243{
1244 int rv = -1;
1245 vcom_socket_main_t *vsm = &vcom_socket_main;
1246 uword *p;
1247 vcom_socket_t *vsock;
1248
1249 vppcom_endpt_t ep;
1250
1251 p = hash_get (vsm->sockidx_by_fd, __fd);
1252 if (!p)
1253 return -EBADF;
1254
1255 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
1256 if (!vsock)
1257 return -ENOTSOCK;
1258
1259 if (vsock->type != SOCKET_TYPE_VPPCOM_BOUND)
1260 return -EINVAL;
1261
1262 if (!__addr)
1263 {
1264 return -EINVAL;
1265 }
1266
1267 ep.vrf = VPPCOM_VRF_DEFAULT;
1268 switch (__addr->sa_family)
1269 {
1270 case AF_INET:
1271 if (__len != sizeof (struct sockaddr_in))
1272 {
1273 return -EINVAL;
1274 }
1275 ep.is_ip4 = VPPCOM_IS_IP4;
1276 ep.ip = (u8 *) & ((const struct sockaddr_in *) __addr)->sin_addr;
1277 ep.port = (u16) ((const struct sockaddr_in *) __addr)->sin_port;
1278 break;
1279
1280 case AF_INET6:
1281 if (__len != sizeof (struct sockaddr_in6))
1282 {
1283 return -EINVAL;
1284 }
1285 ep.is_ip4 = VPPCOM_IS_IP6;
1286 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) __addr)->sin6_addr;
1287 ep.port = (u16) ((const struct sockaddr_in6 *) __addr)->sin6_port;
1288 break;
1289
1290 default:
1291 return -1;
1292 break;
1293 }
1294
1295 rv = vppcom_session_bind (vsock->sid, &ep);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001296 return rv;
1297}
1298
Dave Wallacee22aa742017-10-20 12:30:38 -04001299static inline int
1300vcom_session_getsockname (int sid, vppcom_endpt_t * ep)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001301{
Steven2199aab2017-10-15 20:18:47 -07001302 int rv;
1303 uint32_t size = sizeof (*ep);
1304
1305 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LCL_ADDR, ep, &size);
1306 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001307}
1308
1309int
1310vcom_socket_getsockname (int __fd, __SOCKADDR_ARG __addr,
1311 socklen_t * __restrict __len)
1312{
1313 int rv = -1;
1314 vcom_socket_main_t *vsm = &vcom_socket_main;
1315 uword *p;
1316 vcom_socket_t *vsock;
1317
1318
1319 p = hash_get (vsm->sockidx_by_fd, __fd);
1320 if (!p)
1321 return -EBADF;
1322
1323 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
1324 if (!vsock)
1325 return -ENOTSOCK;
1326
1327 if (vsock->type != SOCKET_TYPE_VPPCOM_BOUND)
1328 return -EINVAL;
1329
1330 if (!__addr || !__len)
1331 return -EFAULT;
1332
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001333 vppcom_endpt_t ep;
1334 ep.ip = (u8 *) & ((const struct sockaddr_in *) __addr)->sin_addr;
Dave Wallacee22aa742017-10-20 12:30:38 -04001335 rv = vcom_session_getsockname (vsock->sid, &ep);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001336 if (rv == 0)
1337 {
1338 if (ep.vrf == VPPCOM_VRF_DEFAULT)
1339 {
1340 __addr->sa_family = ep.is_ip4 == VPPCOM_IS_IP4 ? AF_INET : AF_INET6;
1341 switch (__addr->sa_family)
1342 {
1343 case AF_INET:
1344 ((struct sockaddr_in *) __addr)->sin_port = ep.port;
1345 *__len = sizeof (struct sockaddr_in);
1346 break;
1347
1348 case AF_INET6:
1349 ((struct sockaddr_in6 *) __addr)->sin6_port = ep.port;
1350 *__len = sizeof (struct sockaddr_in6);
1351 break;
1352
1353 default:
1354 break;
1355 }
1356 }
1357 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001358
1359 return rv;
1360}
1361
1362int
1363vcom_socket_connect (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
1364{
1365 int rv = -1;
1366 vcom_socket_main_t *vsm = &vcom_socket_main;
1367 uword *p;
1368 vcom_socket_t *vsock;
1369
1370 vppcom_endpt_t ep;
1371
1372 p = hash_get (vsm->sockidx_by_fd, __fd);
1373 if (p)
1374 {
1375 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
1376
1377 ep.vrf = VPPCOM_VRF_DEFAULT;
1378 switch (__addr->sa_family)
1379 {
1380 case AF_INET:
1381 ep.is_ip4 = VPPCOM_IS_IP4;
1382 ep.ip =
1383 (uint8_t *) & ((const struct sockaddr_in *) __addr)->sin_addr;
1384 ep.port =
1385 (uint16_t) ((const struct sockaddr_in *) __addr)->sin_port;
1386 break;
1387
1388 case AF_INET6:
1389 ep.is_ip4 = VPPCOM_IS_IP6;
1390 ep.ip =
1391 (uint8_t *) & ((const struct sockaddr_in6 *) __addr)->sin6_addr;
1392 ep.port =
1393 (uint16_t) ((const struct sockaddr_in6 *) __addr)->sin6_port;
1394 break;
1395
1396 default:
1397 return -1;
1398 break;
1399 }
1400
1401 rv = vppcom_session_connect (vsock->sid, &ep);
1402 }
1403 return rv;
1404}
1405
Dave Wallacee22aa742017-10-20 12:30:38 -04001406static inline int
1407vcom_session_getpeername (int sid, vppcom_endpt_t * ep)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001408{
Steven2199aab2017-10-15 20:18:47 -07001409 int rv;
1410 uint32_t size = sizeof (*ep);
1411
1412 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_PEER_ADDR, ep, &size);
1413 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001414}
1415
1416int
1417vcom_socket_getpeername (int __fd, __SOCKADDR_ARG __addr,
1418 socklen_t * __restrict __len)
1419{
1420 int rv = -1;
1421 vcom_socket_main_t *vsm = &vcom_socket_main;
1422 uword *p;
1423 vcom_socket_t *vsock;
1424
1425
1426 p = hash_get (vsm->sockidx_by_fd, __fd);
1427 if (!p)
1428 return -EBADF;
1429
1430 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
1431 if (!vsock)
1432 return -ENOTSOCK;
1433
1434 if (vsock->type != SOCKET_TYPE_VPPCOM_BOUND)
1435 return -EINVAL;
1436
1437 if (!__addr || !__len)
1438 return -EFAULT;
1439
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001440 vppcom_endpt_t ep;
1441 ep.ip = (u8 *) & ((const struct sockaddr_in *) __addr)->sin_addr;
Dave Wallacee22aa742017-10-20 12:30:38 -04001442 rv = vcom_session_getpeername (vsock->sid, &ep);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001443 if (rv == 0)
1444 {
1445 if (ep.vrf == VPPCOM_VRF_DEFAULT)
1446 {
1447 __addr->sa_family = ep.is_ip4 == VPPCOM_IS_IP4 ? AF_INET : AF_INET6;
1448 switch (__addr->sa_family)
1449 {
1450 case AF_INET:
1451 ((struct sockaddr_in *) __addr)->sin_port = ep.port;
1452 *__len = sizeof (struct sockaddr_in);
1453 break;
1454
1455 case AF_INET6:
1456 ((struct sockaddr_in6 *) __addr)->sin6_port = ep.port;
1457 *__len = sizeof (struct sockaddr_in6);
1458 break;
1459
1460 default:
1461 break;
1462 }
1463 }
1464 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001465
1466 return rv;
1467}
1468
1469ssize_t
1470vcom_socket_send (int __fd, const void *__buf, size_t __n, int __flags)
1471{
1472 return vcom_socket_sendto (__fd, __buf, __n, __flags, NULL, 0);
1473}
1474
1475ssize_t
1476vcom_socket_recv (int __fd, void *__buf, size_t __n, int __flags)
1477{
1478 int rv = -1;
1479 rv = vcom_socket_recvfrom (__fd, __buf, __n, __flags, NULL, 0);
1480 return rv;
1481}
1482
1483/*
1484 * RETURN 1 if __fd is (SOCK_STREAM, SOCK_SEQPACKET),
1485 * 0 otherwise
1486 * */
1487int
1488vcom_socket_is_connection_mode_socket (int __fd)
1489{
1490 int rv = -1;
1491 /* TBD define new vppcom api */
1492 vcom_socket_main_t *vsm = &vcom_socket_main;
1493 uword *p;
1494 vcom_socket_t *vsock;
1495
1496 int type;
1497 socklen_t optlen;
1498
1499 p = hash_get (vsm->sockidx_by_fd, __fd);
1500
1501 if (p)
1502 {
1503 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
1504 if (vsock && vsock->type == SOCKET_TYPE_VPPCOM_BOUND)
1505 {
1506 optlen = sizeof (type);
1507 rv = libc_getsockopt (__fd, SOL_SOCKET, SO_TYPE, &type, &optlen);
1508 if (rv != 0)
1509 {
1510 return 0;
1511 }
1512 /* get socket type */
1513 switch (type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1514 {
1515 case SOCK_STREAM:
1516 case SOCK_SEQPACKET:
1517 return 1;
1518 break;
1519
1520 default:
1521 return 0;
1522 break;
1523 }
1524 }
1525 }
1526 return 0;
1527}
1528
Dave Wallacee22aa742017-10-20 12:30:38 -04001529static inline ssize_t
1530vcom_session_sendto (int __sid, void *__buf, size_t __n,
1531 int __flags, __CONST_SOCKADDR_ARG __addr,
1532 socklen_t __addr_len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001533{
Dave Wallace617dffa2017-10-26 14:47:06 -04001534 vppcom_endpt_t *ep = 0;
Steven8aa0d782017-10-27 09:34:57 -07001535 vppcom_endpt_t _ep;
Stevenac1f96d2017-10-24 16:03:58 -07001536
Dave Wallace617dffa2017-10-26 14:47:06 -04001537 if (__addr)
Stevenac1f96d2017-10-24 16:03:58 -07001538 {
Dave Wallace617dffa2017-10-26 14:47:06 -04001539 ep = &_ep;
1540 ep->vrf = VPPCOM_VRF_DEFAULT;
1541 switch (__addr->sa_family)
1542 {
1543 case AF_INET:
1544 ep->is_ip4 = VPPCOM_IS_IP4;
1545 ep->ip =
1546 (uint8_t *) & ((const struct sockaddr_in *) __addr)->sin_addr;
1547 ep->port =
1548 (uint16_t) ((const struct sockaddr_in *) __addr)->sin_port;
1549 break;
Stevenac1f96d2017-10-24 16:03:58 -07001550
Dave Wallace617dffa2017-10-26 14:47:06 -04001551 case AF_INET6:
1552 ep->is_ip4 = VPPCOM_IS_IP6;
1553 ep->ip =
1554 (uint8_t *) & ((const struct sockaddr_in6 *) __addr)->sin6_addr;
1555 ep->port =
1556 (uint16_t) ((const struct sockaddr_in6 *) __addr)->sin6_port;
1557 break;
1558
1559 default:
1560 return -EAFNOSUPPORT;
1561 }
Stevenac1f96d2017-10-24 16:03:58 -07001562 }
1563
Dave Wallace617dffa2017-10-26 14:47:06 -04001564 return vppcom_session_sendto (__sid, __buf, __n, __flags, ep);;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001565}
1566
1567ssize_t
1568vcom_socket_sendto (int __fd, const void *__buf, size_t __n,
1569 int __flags, __CONST_SOCKADDR_ARG __addr,
1570 socklen_t __addr_len)
1571{
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001572 vcom_socket_main_t *vsm = &vcom_socket_main;
1573 uword *p;
1574 vcom_socket_t *vsock;
1575
Dave Wallace5c7cf1c2017-10-24 04:12:18 -04001576 if (!__buf)
1577 {
1578 return -EINVAL;
1579 }
1580
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001581 p = hash_get (vsm->sockidx_by_fd, __fd);
1582 if (!p)
1583 return -EBADF;
1584
1585 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
1586 if (!vsock)
1587 return -ENOTSOCK;
1588
Dave Wallace5c7cf1c2017-10-24 04:12:18 -04001589 if (vsock->type != SOCKET_TYPE_VPPCOM_BOUND)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001590 {
1591 return -EINVAL;
1592 }
1593
1594 if (vcom_socket_is_connection_mode_socket (__fd))
1595 {
1596 /* ignore __addr and _addr_len */
1597 /* and EISCONN may be returned when they are not NULL and 0 */
1598 if ((__addr != NULL) || (__addr_len != 0))
1599 {
1600 return -EISCONN;
1601 }
1602 }
1603 else
1604 {
Dave Wallace5c7cf1c2017-10-24 04:12:18 -04001605 if (!__addr)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001606 {
1607 return -EDESTADDRREQ;
1608 }
1609 /* not a vppcom supported address family */
Dave Wallace60f54822017-10-24 20:47:45 -04001610 if (!((__addr->sa_family == AF_INET) ||
1611 (__addr->sa_family == AF_INET6)))
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001612 {
1613 return -EINVAL;
1614 }
1615 }
1616
Dave Wallace617dffa2017-10-26 14:47:06 -04001617 return vcom_session_sendto (vsock->sid, (void *) __buf, (int) __n,
1618 __flags, __addr, __addr_len);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001619}
1620
Dave Wallacee22aa742017-10-20 12:30:38 -04001621static inline ssize_t
1622vcom_session_recvfrom (int __sid, void *__restrict __buf, size_t __n,
1623 int __flags, __SOCKADDR_ARG __addr,
1624 socklen_t * __restrict __addr_len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001625{
Stevenac1f96d2017-10-24 16:03:58 -07001626 int rv;
1627 vppcom_endpt_t ep;
Dave Wallacefaf9d772017-10-26 16:12:04 -04001628 u8 src_addr[sizeof (struct sockaddr_in6)];
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001629
Stevenac1f96d2017-10-24 16:03:58 -07001630 if (__addr)
1631 {
Dave Wallacefaf9d772017-10-26 16:12:04 -04001632 ep.ip = src_addr;
Stevenac1f96d2017-10-24 16:03:58 -07001633 rv = vppcom_session_recvfrom (__sid, __buf, __n, __flags, &ep);
1634
1635 if (rv > 0)
1636 {
1637 if (ep.vrf == VPPCOM_VRF_DEFAULT)
1638 {
1639 __addr->sa_family =
1640 ep.is_ip4 == VPPCOM_IS_IP4 ? AF_INET : AF_INET6;
1641 switch (__addr->sa_family)
1642 {
1643 case AF_INET:
1644 ((struct sockaddr_in *) __addr)->sin_port = ep.port;
Dave Wallacefaf9d772017-10-26 16:12:04 -04001645 memcpy (&((struct sockaddr_in *) __addr)->sin_addr,
1646 src_addr, sizeof (struct in_addr));
1647
Stevenac1f96d2017-10-24 16:03:58 -07001648 *__addr_len = sizeof (struct sockaddr_in);
1649 break;
1650
1651 case AF_INET6:
1652 ((struct sockaddr_in6 *) __addr)->sin6_port = ep.port;
Dave Wallacefaf9d772017-10-26 16:12:04 -04001653 memcpy (((struct sockaddr_in6 *) __addr)->sin6_addr.
1654 __in6_u.__u6_addr8, src_addr,
1655 sizeof (struct in6_addr));
Stevenac1f96d2017-10-24 16:03:58 -07001656 *__addr_len = sizeof (struct sockaddr_in6);
1657 break;
1658
1659 default:
Dave Wallacefaf9d772017-10-26 16:12:04 -04001660 rv = -EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07001661 break;
1662 }
1663 }
1664 else
1665 rv = -1;
1666 }
1667 }
1668 else
1669 rv = vppcom_session_recvfrom (__sid, __buf, __n, __flags, NULL);
1670
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001671 return rv;
1672}
1673
1674ssize_t
1675vcom_socket_recvfrom (int __fd, void *__restrict __buf, size_t __n,
1676 int __flags, __SOCKADDR_ARG __addr,
1677 socklen_t * __restrict __addr_len)
1678{
1679 int rv = -1;
1680 vcom_socket_main_t *vsm = &vcom_socket_main;
1681 uword *p;
1682 vcom_socket_t *vsock;
1683
Stevenac1f96d2017-10-24 16:03:58 -07001684 if (__addr && !__addr_len)
1685 return -EINVAL;
Dave Wallace5c7cf1c2017-10-24 04:12:18 -04001686
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001687 p = hash_get (vsm->sockidx_by_fd, __fd);
1688 if (!p)
1689 return -EBADF;
1690
1691 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
1692 if (!vsock)
1693 return -ENOTSOCK;
1694
Dave Wallace5c7cf1c2017-10-24 04:12:18 -04001695 if (vsock->type != SOCKET_TYPE_VPPCOM_BOUND)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001696 {
1697 return -EINVAL;
1698 }
1699
Dave Wallacee22aa742017-10-20 12:30:38 -04001700 rv = vcom_session_recvfrom (vsock->sid, __buf, __n,
1701 __flags, __addr, __addr_len);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001702 return rv;
1703}
1704
1705/* TBD: move it to vppcom */
Dave Wallacee22aa742017-10-20 12:30:38 -04001706static inline ssize_t
1707vcom_session_sendmsg (int __sid, const struct msghdr *__message, int __flags)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001708{
1709 int rv = -1;
1710 /* rv = vppcom_session_write (__sid, (void *) __message->__buf,
1711 (int)__n); */
1712 return rv;
1713}
1714
1715ssize_t
1716vcom_socket_sendmsg (int __fd, const struct msghdr * __message, int __flags)
1717{
1718 int rv = -1;
1719 vcom_socket_main_t *vsm = &vcom_socket_main;
1720 uword *p;
1721 vcom_socket_t *vsock;
1722
1723 p = hash_get (vsm->sockidx_by_fd, __fd);
1724 if (!p)
1725 return -EBADF;
1726
1727 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
1728 if (!vsock)
1729 return -ENOTSOCK;
1730
1731 if (vcom_socket_is_connection_mode_socket (__fd))
1732 {
1733 /* ignore __addr and _addr_len */
1734 /* and EISCONN may be returned when they are not NULL and 0 */
1735 if ((__message->msg_name != NULL) || (__message->msg_namelen != 0))
1736 {
1737 return -EISCONN;
1738 }
1739 }
1740 else
1741 {
1742 /* TBD: validate __message->msg_name and __message->msg_namelen
1743 * and return -EINVAL on validation error
1744 * */
1745 ;
1746 }
1747
Dave Wallacee22aa742017-10-20 12:30:38 -04001748 rv = vcom_session_sendmsg (vsock->sid, __message, __flags);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001749
1750 return rv;
1751}
1752
1753#ifdef __USE_GNU
1754int
1755vcom_socket_sendmmsg (int __fd, struct mmsghdr *__vmessages,
1756 unsigned int __vlen, int __flags)
1757{
1758
1759 /* TBD: define a new vppcom api */
1760 return 0;
1761}
1762#endif
1763
1764/* TBD: move it to vppcom */
Dave Wallacee22aa742017-10-20 12:30:38 -04001765static inline ssize_t
1766vcom_session_recvmsg (int __sid, struct msghdr *__message, int __flags)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001767{
1768 int rv = -1;
1769 /* rv = vppcom_session_read (__sid, (void *) __message->__buf,
1770 (int)__n); */
1771 rv = -EOPNOTSUPP;
1772 return rv;
1773}
1774
1775ssize_t
1776vcom_socket_recvmsg (int __fd, struct msghdr * __message, int __flags)
1777{
1778 int rv = -1;
1779 vcom_socket_main_t *vsm = &vcom_socket_main;
1780 uword *p;
1781 vcom_socket_t *vsock;
1782
1783 p = hash_get (vsm->sockidx_by_fd, __fd);
1784 if (!p)
1785 return -EBADF;
1786
1787 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
1788 if (!vsock)
1789 return -ENOTSOCK;
1790
1791 if (vsock->type != SOCKET_TYPE_VPPCOM_BOUND)
1792 return -EINVAL;
1793
1794 if (!__message)
1795 {
1796 return -EINVAL;
1797 }
1798
1799 /* validate __flags */
1800
Dave Wallacee22aa742017-10-20 12:30:38 -04001801 rv = vcom_session_recvmsg (vsock->sid, __message, __flags);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001802 return rv;
1803}
1804
1805#ifdef __USE_GNU
1806int
1807vcom_socket_recvmmsg (int __fd, struct mmsghdr *__vmessages,
1808 unsigned int __vlen, int __flags,
1809 struct timespec *__tmo)
1810{
1811 /* TBD: define a new vppcom api */
1812 return 0;
1813}
1814#endif
1815
1816/* TBD: move it to vppcom */
Dave Wallacee22aa742017-10-20 12:30:38 -04001817static inline int
1818vcom_session_get_sockopt (int __sid, int __level, int __optname,
1819 void *__restrict __optval,
1820 socklen_t * __restrict __optlen)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001821{
Stevenac1f96d2017-10-24 16:03:58 -07001822 int rv = 0;
1823
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001824 /* 1. for socket level options that are NOT socket attributes
1825 * and that has corresponding vpp options get from vppcom */
Stevenac1f96d2017-10-24 16:03:58 -07001826 switch (__level)
1827 {
1828 case SOL_SOCKET:
1829 switch (__optname)
1830 {
1831 case SO_ERROR:
1832 *(int *) __optval = 0;
1833 break;
1834 default:
1835 break;
1836 }
1837 default:
1838 break;
1839 }
1840 /* 2. unhandled options */
1841 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001842}
1843
1844int
1845vcom_socket_getsockopt (int __fd, int __level, int __optname,
1846 void *__restrict __optval,
1847 socklen_t * __restrict __optlen)
1848{
1849 int rv = -1;
1850 vcom_socket_main_t *vsm = &vcom_socket_main;
1851 uword *p;
1852 vcom_socket_t *vsock;
1853
Dave Wallacefaf9d772017-10-26 16:12:04 -04001854 if (!__optval || !__optlen)
1855 return -EINVAL;
1856
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001857 p = hash_get (vsm->sockidx_by_fd, __fd);
1858 if (!p)
1859 return -EBADF;
1860
1861 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
1862 if (!vsock)
1863 return -ENOTSOCK;
1864
1865 if (vsock->type != SOCKET_TYPE_VPPCOM_BOUND)
1866 return -EINVAL;
1867
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001868 switch (__level)
1869 {
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001870 case SOL_SOCKET:
1871 switch (__optname)
1872 {
1873/*
1874 * 1. for socket level options that are socket attributes,
1875 * get from libc_getsockopt.
1876 * 2. for socket level options that are NOT socket
1877 * attributes and that has corresponding vpp options
1878 * get from vppcom.
1879 * 3. for socket level options unimplemented
1880 * return -ENOPROTOOPT */
1881 case SO_DEBUG:
1882 case SO_DONTROUTE:
1883 case SO_BROADCAST:
1884 case SO_SNDBUF:
1885 case SO_RCVBUF:
1886 case SO_REUSEADDR:
1887 case SO_REUSEPORT:
1888 case SO_KEEPALIVE:
1889 case SO_TYPE:
1890 case SO_PROTOCOL:
1891 case SO_DOMAIN:
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001892 case SO_OOBINLINE:
1893 case SO_NO_CHECK:
1894 case SO_PRIORITY:
1895 case SO_LINGER:
1896 case SO_BSDCOMPAT:
1897 case SO_TIMESTAMP:
1898 case SO_TIMESTAMPNS:
1899 case SO_TIMESTAMPING:
1900 case SO_RCVTIMEO:
1901 case SO_SNDTIMEO:
1902 case SO_RCVLOWAT:
1903 case SO_SNDLOWAT:
1904 case SO_PASSCRED:
1905 case SO_PEERCRED:
1906 case SO_PEERNAME:
1907 case SO_ACCEPTCONN:
1908 case SO_PASSSEC:
1909 case SO_PEERSEC:
1910 case SO_MARK:
1911 case SO_RXQ_OVFL:
1912 case SO_WIFI_STATUS:
1913 case SO_PEEK_OFF:
1914 case SO_NOFCS:
1915 case SO_BINDTODEVICE:
1916 case SO_GET_FILTER:
1917 case SO_LOCK_FILTER:
1918 case SO_BPF_EXTENSIONS:
1919 case SO_SELECT_ERR_QUEUE:
1920#ifdef CONFIG_NET_RX_BUSY_POLL
1921 case SO_BUSY_POLL:
1922#endif
1923 case SO_MAX_PACING_RATE:
Dave Wallace5c7cf1c2017-10-24 04:12:18 -04001924#ifdef SO_INCOMING_CPU
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001925 case SO_INCOMING_CPU:
Dave Wallace5c7cf1c2017-10-24 04:12:18 -04001926#endif
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001927 rv = libc_getsockopt (__fd, __level, __optname, __optval, __optlen);
1928 if (rv != 0)
1929 {
1930 rv = -errno;
1931 return rv;
1932 }
1933 break;
1934
Stevenac1f96d2017-10-24 16:03:58 -07001935 case SO_ERROR:
1936 rv = vcom_session_get_sockopt (vsock->sid, __level, __optname,
1937 __optval, __optlen);
1938 break;
1939
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001940 default:
1941 /* We implement the SO_SNDLOWAT etc to not be settable
1942 * (1003.1g 7).
1943 */
1944 return -ENOPROTOOPT;
1945 }
1946
1947 break;
1948
1949 default:
1950 /* 1. handle options that are NOT socket level options,
1951 * but have corresponding vpp otions. */
Dave Wallacee22aa742017-10-20 12:30:38 -04001952 rv = vcom_session_get_sockopt (vsock->sid, __level, __optname,
1953 __optval, __optlen);
1954 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001955 }
1956
1957 return rv;
1958}
1959
1960/* TBD: move it to vppcom */
Dave Wallacee22aa742017-10-20 12:30:38 -04001961static inline int
1962vcom_session_setsockopt (int __sid, int __level, int __optname,
1963 const void *__optval, socklen_t __optlen)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001964{
Stevenb59f2272017-10-12 17:10:33 -07001965 int rv = -EOPNOTSUPP;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001966
Stevenb59f2272017-10-12 17:10:33 -07001967 switch (__level)
1968 {
Stevenbd187a82017-10-13 12:52:28 -07001969 case SOL_TCP:
1970 switch (__optname)
1971 {
1972 case TCP_KEEPIDLE:
1973 rv =
1974 vppcom_session_attr (__sid, VPPCOM_ATTR_SET_TCP_KEEPIDLE, 0, 0);
1975 break;
1976 case TCP_KEEPINTVL:
1977 rv =
1978 vppcom_session_attr (__sid, VPPCOM_ATTR_SET_TCP_KEEPINTVL, 0, 0);
1979 break;
1980 default:
1981 break;
1982 }
1983 break;
Stevenb59f2272017-10-12 17:10:33 -07001984 case SOL_IPV6:
1985 switch (__optname)
1986 {
1987 case IPV6_V6ONLY:
1988 rv = vppcom_session_attr (__sid, VPPCOM_ATTR_SET_V6ONLY, 0, 0);
Stevenbd187a82017-10-13 12:52:28 -07001989 break;
Stevenb59f2272017-10-12 17:10:33 -07001990 default:
Stevenbd187a82017-10-13 12:52:28 -07001991 break;
Stevenb59f2272017-10-12 17:10:33 -07001992 }
1993 break;
1994 case SOL_SOCKET:
1995 switch (__optname)
1996 {
Stevenbd187a82017-10-13 12:52:28 -07001997 case SO_KEEPALIVE:
1998 rv = vppcom_session_attr (__sid, VPPCOM_ATTR_SET_KEEPALIVE, 0, 0);
1999 break;
Stevenb59f2272017-10-12 17:10:33 -07002000 case SO_REUSEADDR:
2001 rv = vppcom_session_attr (__sid, VPPCOM_ATTR_SET_REUSEADDR, 0, 0);
Stevenbd187a82017-10-13 12:52:28 -07002002 break;
Stevenb59f2272017-10-12 17:10:33 -07002003 case SO_BROADCAST:
2004 rv = vppcom_session_attr (__sid, VPPCOM_ATTR_SET_BROADCAST, 0, 0);
Stevenbd187a82017-10-13 12:52:28 -07002005 break;
Stevenb59f2272017-10-12 17:10:33 -07002006 default:
Stevenbd187a82017-10-13 12:52:28 -07002007 break;
Stevenb59f2272017-10-12 17:10:33 -07002008 }
2009 break;
2010 default:
Stevenbd187a82017-10-13 12:52:28 -07002011 break;
Stevenb59f2272017-10-12 17:10:33 -07002012 }
2013
2014 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002015}
2016
2017int
2018vcom_socket_setsockopt (int __fd, int __level, int __optname,
2019 const void *__optval, socklen_t __optlen)
2020{
2021 int rv = -1;
2022 vcom_socket_main_t *vsm = &vcom_socket_main;
2023 uword *p;
2024 vcom_socket_t *vsock;
2025
2026 p = hash_get (vsm->sockidx_by_fd, __fd);
2027 if (!p)
2028 return -EBADF;
2029
2030 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
2031 if (!vsock)
2032 return -ENOTSOCK;
2033
2034 if (vsock->type != SOCKET_TYPE_VPPCOM_BOUND)
2035 return -EINVAL;
2036
2037 /*
2038 * Options without arguments
2039 */
2040
2041 if (__optname == SO_BINDTODEVICE)
2042 {
2043 rv = libc_setsockopt (__fd, __level, __optname, __optval, __optlen);
2044 if (rv != 0)
2045 {
2046 rv = -errno;
2047 }
2048 return rv;
2049 }
2050
2051 if (!__optval)
2052 return -EFAULT;
2053
Dave Wallace5c7cf1c2017-10-24 04:12:18 -04002054 if (__optlen < sizeof (int))
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002055 return -EINVAL;
2056
2057 switch (__level)
2058 {
Stevenb59f2272017-10-12 17:10:33 -07002059 case SOL_IPV6:
2060 switch (__optname)
2061 {
2062 case IPV6_V6ONLY:
Dave Wallacee22aa742017-10-20 12:30:38 -04002063 rv = vcom_session_setsockopt (vsock->sid, __level, __optname,
2064 __optval, __optlen);
Stevenbd187a82017-10-13 12:52:28 -07002065 break;
Stevenb59f2272017-10-12 17:10:33 -07002066 default:
2067 return -EOPNOTSUPP;
2068 }
2069 break;
2070 case SOL_TCP:
2071 switch (__optname)
2072 {
2073 case TCP_NODELAY:
2074 return 0;
Stevenbd187a82017-10-13 12:52:28 -07002075 case TCP_KEEPIDLE:
2076 case TCP_KEEPINTVL:
Dave Wallacee22aa742017-10-20 12:30:38 -04002077 rv = vcom_session_setsockopt (vsock->sid, __level, __optname,
2078 __optval, __optlen);
Stevenbd187a82017-10-13 12:52:28 -07002079 break;
Stevenb59f2272017-10-12 17:10:33 -07002080 default:
2081 return -EOPNOTSUPP;
2082 }
2083 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002084 /* handle options at socket level */
2085 case SOL_SOCKET:
2086 switch (__optname)
2087 {
Stevenb59f2272017-10-12 17:10:33 -07002088 case SO_REUSEADDR:
2089 case SO_BROADCAST:
Stevenbd187a82017-10-13 12:52:28 -07002090 case SO_KEEPALIVE:
Dave Wallacee22aa742017-10-20 12:30:38 -04002091 rv = vcom_session_setsockopt (vsock->sid, __level, __optname,
2092 __optval, __optlen);
Stevenbd187a82017-10-13 12:52:28 -07002093 break;
Stevenb59f2272017-10-12 17:10:33 -07002094
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002095 /*
2096 * 1. for socket level options that are socket attributes,
2097 * set it from libc_getsockopt
2098 * 2. for socket level options that are NOT socket
2099 * attributes and that has corresponding vpp options
2100 * set it from vppcom
2101 * 3. for socket level options unimplemented
2102 * return -ENOPROTOOPT */
2103 case SO_DEBUG:
2104 case SO_DONTROUTE:
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002105 case SO_SNDBUF:
2106 case SO_RCVBUF:
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002107 case SO_REUSEPORT:
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002108 case SO_TYPE:
2109 case SO_PROTOCOL:
2110 case SO_DOMAIN:
2111 case SO_ERROR:
2112 case SO_OOBINLINE:
2113 case SO_NO_CHECK:
2114 case SO_PRIORITY:
2115 case SO_LINGER:
2116 case SO_BSDCOMPAT:
2117 case SO_TIMESTAMP:
2118 case SO_TIMESTAMPNS:
2119 case SO_TIMESTAMPING:
2120 case SO_RCVTIMEO:
2121 case SO_SNDTIMEO:
2122 case SO_RCVLOWAT:
2123 case SO_SNDLOWAT:
2124 case SO_PASSCRED:
2125 case SO_PEERCRED:
2126 case SO_PEERNAME:
2127 case SO_ACCEPTCONN:
2128 case SO_PASSSEC:
2129 case SO_PEERSEC:
2130 case SO_MARK:
2131 case SO_RXQ_OVFL:
2132 case SO_WIFI_STATUS:
2133 case SO_PEEK_OFF:
2134 case SO_NOFCS:
2135 /*
2136 * SO_BINDTODEVICE already handled as
2137 * "Options without arguments" */
2138 /* case SO_BINDTODEVICE: */
2139 case SO_GET_FILTER:
2140 case SO_LOCK_FILTER:
2141 case SO_BPF_EXTENSIONS:
2142 case SO_SELECT_ERR_QUEUE:
2143#ifdef CONFIG_NET_RX_BUSY_POLL
2144 case SO_BUSY_POLL:
2145#endif
2146 case SO_MAX_PACING_RATE:
Dave Wallace5c7cf1c2017-10-24 04:12:18 -04002147#ifdef SO_INCOMING_CPU
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002148 case SO_INCOMING_CPU:
Dave Wallace5c7cf1c2017-10-24 04:12:18 -04002149#endif
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002150 rv = libc_setsockopt (__fd, __level, __optname, __optval, __optlen);
2151 if (rv != 0)
2152 {
2153 rv = -errno;
2154 return rv;
2155 }
2156 break;
2157
2158 default:
2159 /* We implement the SO_SNDLOWAT etc to not be settable
2160 * (1003.1g 7).
2161 */
2162 return -ENOPROTOOPT;
2163 }
2164
2165 break;
2166
2167 default:
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002168 return -ENOPROTOOPT;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002169 }
2170
2171 return rv;
2172}
2173
2174int
2175vcom_socket_listen (int __fd, int __n)
2176{
2177 int rv = -1;
2178 vcom_socket_main_t *vsm = &vcom_socket_main;
2179 uword *p;
2180 vcom_socket_t *vsock;
2181
2182 p = hash_get (vsm->sockidx_by_fd, __fd);
2183 if (p)
2184 {
2185 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
2186
2187 /* TBD vppcom to accept __n parameter */
2188 rv = vppcom_session_listen (vsock->sid, __n);
2189 }
2190
2191 return rv;
2192}
2193
2194static int
2195vcom_socket_connected_socket (int __fd, int __sid,
2196 int *__domain,
2197 int *__type, int *__protocol, int flags)
2198{
2199 int rv = -1;
2200 vcom_socket_main_t *vsm = &vcom_socket_main;
2201 vcom_socket_t *vsock;
2202
2203 i32 fd;
2204 i32 sockidx;
2205
2206 socklen_t optlen;
2207
2208 optlen = sizeof (*__domain);
2209 rv = libc_getsockopt (__fd, SOL_SOCKET, SO_DOMAIN, __domain, &optlen);
2210 if (rv != 0)
2211 {
2212 rv = -errno;
2213 goto out;
2214 }
2215
2216 optlen = sizeof (*__type);
2217 rv = libc_getsockopt (__fd, SOL_SOCKET, SO_TYPE, __type, &optlen);
2218 if (rv != 0)
2219 {
2220 rv = -errno;
2221 goto out;
2222 }
2223
2224 optlen = sizeof (*__protocol);
2225 rv = libc_getsockopt (__fd, SOL_SOCKET, SO_PROTOCOL, __protocol, &optlen);
2226 if (rv != 0)
2227 {
2228 rv = -errno;
2229 goto out;
2230 }
2231
2232 fd = vcom_socket_open_socket (*__domain, *__type | flags, *__protocol);
2233 if (fd < 0)
2234 {
2235 rv = fd;
2236 goto out;
2237 }
2238
2239 pool_get (vsm->vsockets, vsock);
2240 vsocket_init (vsock);
2241
2242 sockidx = vsock - vsm->vsockets;
2243 hash_set (vsm->sockidx_by_fd, fd, sockidx);
2244
2245 vsocket_set (vsock, fd, __sid, SOCKET_TYPE_VPPCOM_BOUND);
2246 return fd;
2247
2248out:
2249 return rv;
2250}
2251
2252/* If flag is 0, then accept4() is the same as accept().
2253 * SOCK_NONBLOCK and SOCK_CLOEXEC can be bitwise ORed in flags
2254 */
2255static int
2256vcom_socket_accept_flags (int __fd, __SOCKADDR_ARG __addr,
2257 socklen_t * __restrict __addr_len, int flags)
2258{
2259 int rv = -1;
2260 vcom_socket_main_t *vsm = &vcom_socket_main;
2261 uword *p;
2262 vcom_socket_t *vsock;
2263
2264 int fd;
2265 int sid;
2266 int domain;
2267 int type;
2268 int protocol;
2269
2270 uint8_t addr8[sizeof (struct in6_addr)];
2271 vppcom_endpt_t ep;
2272
2273 ep.ip = addr8;
2274
2275 /* validate flags */
2276
2277 /*
2278 * for documentation
2279 * switch (flags)
2280 * {
2281 * case 0:
2282 * case SOCK_NONBLOCK:
2283 * case SOCK_CLOEXEC:
2284 * case SOCK_NONBLOCK | SOCK_CLOEXEC:
2285 * break;
2286 *
2287 * default:
2288 * return -1;
2289 * }
2290 */
2291 /* flags can be 0 or can be bitwise OR
2292 * of any of SOCK_NONBLOCK and SOCK_CLOEXEC */
2293
Dave Wallace59179392017-11-07 02:20:07 -05002294 if (VCOM_DEBUG > 2)
2295 fprintf (stderr, "[%d] vcom_socket_accept_flags: "
2296 "fd = %d, __addr = %p, __addr_len = %p flags = %d (0x%x)\n",
2297 getpid (), __fd, __addr, __addr_len, flags, flags);
2298
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002299 if (!(!flags || (flags & (SOCK_NONBLOCK | SOCK_CLOEXEC))))
2300 {
2301 /* TBD: return proper error code */
Dave Wallace59179392017-11-07 02:20:07 -05002302 fprintf (stderr, "[%d] ERROR: vcom_socket_accept_flags: "
2303 "invalid flags = %d (0x%x)\n", getpid (), flags, flags);
2304
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002305 return -1;
2306 }
2307
2308 /* TBD: return proper error code */
2309
2310 if (!vcom_socket_is_connection_mode_socket (__fd))
2311 {
Dave Wallace59179392017-11-07 02:20:07 -05002312 fprintf (stderr, "[%d] ERROR: vcom_socket_accept_flags: "
2313 "connection mode socket support TBD!\n", getpid ());
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002314 return -EOPNOTSUPP;
2315 }
2316
2317 p = hash_get (vsm->sockidx_by_fd, __fd);
2318 if (p)
2319 {
2320 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
2321
2322
2323 rv = vcom_fcntl (vsock->fd, F_GETFL, 0);
2324 if (rv < 0)
2325 {
Dave Wallace59179392017-11-07 02:20:07 -05002326 fprintf (stderr, "[%d] ERROR: vcom_socket_accept_flags: "
2327 "vcom_fcnt() returned %d!\n", getpid (), rv);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002328 return rv;
2329 }
2330
2331 /* is blocking */
2332 if (!(rv & O_NONBLOCK))
2333 {
2334 /* socket is not marked as nonblocking
2335 * and no pending connections are present
2336 * on the queue, accept () blocks the caller
2337 * until a connection is present.
2338 */
2339 rv = vppcom_session_accept (vsock->sid, &ep,
2340 -1.0 /* wait forever */ );
2341 }
2342 else
2343 {
2344 /* The file descriptor refers to a socket and has been
2345 * marked nonblocking(O_NONBLOCK) and the accept would
2346 * block.
2347 * */
2348 /* is non blocking */
2349 rv = vppcom_session_accept (vsock->sid, &ep, 0);
2350 /* If the socket is marked nonblocking and
2351 * no pending connections are present on the
2352 * queue, accept fails with the error
2353 * EAGAIN or EWOULDBLOCK
2354 */
2355 if (rv == VPPCOM_ETIMEDOUT)
2356 {
2357 rv = VPPCOM_EAGAIN;
2358 }
2359 }
2360 if (rv < 0)
2361 {
Dave Wallace59179392017-11-07 02:20:07 -05002362 if (rv != VPPCOM_EAGAIN)
2363 fprintf (stderr, "[%d] ERROR: vcom_socket_accept_flags: "
2364 "vppcom_session_accept() returned %d!", getpid (), rv);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002365 return rv;
2366 }
2367
2368 sid = rv;
2369
2370 /* create a new connected socket resource and set flags
2371 * on the new file descriptor.
2372 * update vsockets and sockidx_by_fd table
2373 * */
2374 fd = vcom_socket_connected_socket (__fd, sid,
2375 &domain, &type, &protocol, flags);
2376 if (fd < 0)
2377 {
Dave Wallace59179392017-11-07 02:20:07 -05002378 fprintf (stderr, "[%d] ERROR: vcom_socket_accept_flags: "
2379 "vcom_socket_connected_socket() returned %d!",
2380 getpid (), rv);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002381 return fd;
2382 }
2383
2384 rv = fd;
2385
2386 /* TBD populate __addr and __addr_len */
2387 /* TBD: The returned address is truncated if the buffer
2388 * provided is too small, in this case, __addr_len will
2389 * return a value greater than was supplied to the call.*/
2390 if (__addr)
2391 {
2392 if (ep.is_cut_thru)
2393 {
2394 /* TBD populate __addr and __addr_len */
2395 switch (domain)
2396 {
2397 case AF_INET:
2398 ((struct sockaddr_in *) __addr)->sin_family = AF_INET;
2399 ((struct sockaddr_in *) __addr)->sin_port = ep.port;
2400 memcpy (&((struct sockaddr_in *) __addr)->sin_addr,
2401 addr8, sizeof (struct in_addr));
2402 /* TBD: populate __addr_len */
2403 if (__addr_len)
2404 {
2405 *__addr_len = sizeof (struct sockaddr_in);
2406 }
2407 break;
2408
2409 case AF_INET6:
2410 ((struct sockaddr_in6 *) __addr)->sin6_family = AF_INET6;
2411 ((struct sockaddr_in6 *) __addr)->sin6_port = ep.port;
2412 memcpy (((struct sockaddr_in6 *) __addr)->sin6_addr.
2413 __in6_u.__u6_addr8, addr8,
2414 sizeof (struct in6_addr));
2415 /* TBD: populate __addr_len */
2416 if (__addr_len)
2417 {
2418 *__addr_len = sizeof (struct sockaddr_in6);
2419 }
2420 break;
2421
2422 default:
2423 return -EAFNOSUPPORT;
2424 }
2425 }
2426 else
2427 {
2428 switch (ep.is_ip4)
2429 {
2430 case VPPCOM_IS_IP4:
2431 ((struct sockaddr_in *) __addr)->sin_family = AF_INET;
2432 ((struct sockaddr_in *) __addr)->sin_port = ep.port;
2433 memcpy (&((struct sockaddr_in *) __addr)->sin_addr,
2434 addr8, sizeof (struct in_addr));
2435 /* TBD: populate __addr_len */
2436 if (__addr_len)
2437 {
2438 *__addr_len = sizeof (struct sockaddr_in);
2439 }
2440 break;
2441
2442 case VPPCOM_IS_IP6:
2443 ((struct sockaddr_in6 *) __addr)->sin6_family = AF_INET6;
2444 ((struct sockaddr_in6 *) __addr)->sin6_port = ep.port;
2445 memcpy (((struct sockaddr_in6 *) __addr)->sin6_addr.
2446 __in6_u.__u6_addr8, addr8,
2447 sizeof (struct in6_addr));
2448 /* TBD: populate __addr_len */
2449 if (__addr_len)
2450 {
2451 *__addr_len = sizeof (struct sockaddr_in6);
2452 }
2453 break;
2454
2455 default:
2456 return -EAFNOSUPPORT;
2457 }
2458 }
2459 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002460 }
2461
2462 return rv;
2463}
2464
2465int
2466vcom_socket_accept (int __fd, __SOCKADDR_ARG __addr,
2467 socklen_t * __restrict __addr_len)
2468{
2469 /* set flags to 0 for accept() */
2470 return vcom_socket_accept_flags (__fd, __addr, __addr_len, 0);
2471}
2472
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002473int
2474vcom_socket_accept4 (int __fd, __SOCKADDR_ARG __addr,
2475 socklen_t * __restrict __addr_len, int __flags)
2476{
2477 /* SOCK_NONBLOCK and SOCK_CLOEXEC can be bitwise ORed in flags */
2478 return vcom_socket_accept_flags (__fd, __addr, __addr_len, __flags);
2479}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002480
2481/* TBD: move it to vppcom */
Dave Wallacee22aa742017-10-20 12:30:38 -04002482static inline int
2483vcom_session_shutdown (int __fd, int __how)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002484{
2485 return 0;
2486}
2487
2488int
2489vcom_socket_shutdown (int __fd, int __how)
2490{
2491 int rv = -1;
2492 vcom_socket_main_t *vsm = &vcom_socket_main;
2493 uword *p;
2494 vcom_socket_t *vsock;
2495
2496 p = hash_get (vsm->sockidx_by_fd, __fd);
2497 if (p)
2498 {
2499 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
2500 switch (__how)
2501 {
2502 case SHUT_RD:
2503 case SHUT_WR:
2504 case SHUT_RDWR:
Dave Wallacee22aa742017-10-20 12:30:38 -04002505 rv = vcom_session_shutdown (vsock->sid, __how);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002506 return rv;
2507 break;
2508
2509 default:
2510 return -EINVAL;
2511 break;
2512 }
2513 }
2514
2515 return rv;
2516}
2517
2518int
2519vcom_socket_epoll_create1 (int __flags)
2520{
2521 int rv = -1;
2522 vcom_socket_main_t *vsm = &vcom_socket_main;
2523 vcom_epoll_t *vepoll;
2524
2525 i32 epfd;
2526 i32 vep_idx;
2527 i32 epollidx;
2528
2529 epfd = vcom_socket_open_epoll (__flags);
2530 if (epfd < 0)
2531 {
2532 rv = epfd;
2533 goto out;
2534 }
2535
2536 vep_idx = vppcom_epoll_create ();
2537 if (vep_idx < 0)
2538 {
2539 rv = vep_idx;
2540 goto out_close_epoll;
2541 }
2542
2543 pool_get (vsm->vepolls, vepoll);
2544 vepoll_init (vepoll);
2545
2546 epollidx = vepoll - vsm->vepolls;
2547 hash_set (vsm->epollidx_by_epfd, epfd, epollidx);
2548
2549 vepoll_set (vepoll, epfd, vep_idx, EPOLL_TYPE_VPPCOM_BOUND, __flags, 0, 0);
2550
2551 return epfd;
2552
2553out_close_epoll:
2554 vcom_socket_close_epoll (epfd);
2555out:
2556 return rv;
2557}
2558
2559/*
2560 * PRE: vppcom_epoll_ctl() is successful
2561 * free_vepitem_on_del : 0 - no_pool_put, 1 - pool_put
2562 */
2563int
2564vcom_socket_ctl_vepitem (int __epfd, int __op, int __fd,
2565 struct epoll_event *__event,
2566 i32 vep_idx, vcom_epoll_t * vepoll,
2567 i32 vfd_id, void *vfd, vcom_fd_type_t type,
2568 int free_vepitem_on_del)
2569{
2570 int rv = -1;
2571 vcom_socket_main_t *vsm = &vcom_socket_main;
2572 vcom_epitem_t *vepitem;
2573
2574 vcom_epitem_key_t epfdfd = {.epfd = __epfd,.fd = __fd };
2575 uword *p;
2576 i32 vepitemidx;
2577
2578 i32 *vepitemidxs = 0;
2579
2580 struct epoll_event revent = {.events = 0,.data.fd = INVALID_FD };
2581
2582 i32 vec_idx;
2583
2584 /* perform control operations on the epoll instance */
2585 switch (__op)
2586 {
2587 case EPOLL_CTL_ADD:
2588 /*
2589 * supplied file descriptor is already
2590 * registered with this epoll instance
2591 * */
2592 /* vepitem exists */
2593 p = hash_get (vsm->epitemidx_by_epfdfd, epfdfd.key);
2594 if (p)
2595 {
2596 rv = -EEXIST;
2597 goto out;
2598 }
2599
2600 /* add a new vepitem */
2601 pool_get (vsm->vepitems, vepitem);
2602 vepitem_init (vepitem);
2603
2604 vepitemidx = vepitem - vsm->vepitems;
2605 hash_set (vsm->epitemidx_by_epfdfd, epfdfd.key, vepitemidx);
2606 vepitem_set (vepitem, __epfd, __fd, __fd, __fd, type, *__event, revent);
2607
2608 /* update epitemidxs */
2609 /* by_epfd */
2610 p = hash_get (vsm->epitemidxs_by_epfd, __epfd);
2611 if (!p) /* not exist */
2612 {
2613 vepitemidxs = 0;
2614 vec_add1 (vepitemidxs, vepitemidx);
2615 hash_set (vsm->epitemidxs_by_epfd, __epfd, vepitemidxs);
2616 }
2617 else /* exists */
2618 {
2619 vepitemidxs = *(i32 **) p;
2620 vec_add1 (vepitemidxs, vepitemidx);
2621 hash_set3 (vsm->epitemidxs_by_epfd, __epfd, vepitemidxs, 0);
2622 }
2623 /* update epitemidxs */
2624 /* by_fd */
2625 p = hash_get (vsm->epitemidxs_by_fd, __fd);
2626 if (!p) /* not exist */
2627 {
2628 vepitemidxs = 0;
2629 vec_add1 (vepitemidxs, vepitemidx);
2630 hash_set (vsm->epitemidxs_by_fd, __fd, vepitemidxs);
2631 }
2632 else /* exists */
2633 {
2634 vepitemidxs = *(i32 **) p;
2635 vec_add1 (vepitemidxs, vepitemidx);
2636 hash_set3 (vsm->epitemidxs_by_fd, __fd, vepitemidxs, 0);
2637 }
2638
2639 /* increment vepoll fd count by 1 */
2640 vepoll->count += 1;
2641
2642 rv = 0;
2643 goto out;
2644 break;
2645
2646 case EPOLL_CTL_MOD:
2647 /*
2648 * supplied file descriptor is not
2649 * registered with this epoll instance
2650 * */
2651 /* vepitem not exist */
2652 p = hash_get (vsm->epitemidx_by_epfdfd, epfdfd.key);
2653 if (!p)
2654 {
2655 rv = -ENOENT;
2656 goto out;
2657 }
2658 vepitem = pool_elt_at_index (vsm->vepitems, p[0]);
2659 if (vepitem)
2660 {
2661 vepitem->event = *__event;
2662 vepitem->revent = revent;
2663 }
2664
2665 rv = 0;
2666 goto out;
2667 break;
2668
2669 case EPOLL_CTL_DEL:
2670 /*
2671 * supplied file descriptor is not
2672 * registered with this epoll instance
2673 * */
2674 /* vepitem not exist */
2675 p = hash_get (vsm->epitemidx_by_epfdfd, epfdfd.key);
2676 if (!p)
2677 {
2678 rv = -ENOENT;
2679 goto out;
2680 }
2681 vepitemidx = *(i32 *) p;
2682 hash_unset (vsm->epitemidx_by_epfdfd, epfdfd.key);
2683
2684 /* update epitemidxs */
2685 /* by_epfd */
2686 p = hash_get (vsm->epitemidxs_by_epfd, __epfd);
2687 if (!p) /* not exist */
2688 {
2689 rv = -ENOENT;
2690 goto out;
2691 }
2692 else /* exists */
2693 {
2694 vepitemidxs = *(i32 **) p;
2695 vec_idx = vec_search (vepitemidxs, vepitemidx);
2696 if (vec_idx != ~0)
2697 {
2698 vec_del1 (vepitemidxs, vec_idx);
2699 if (!vec_len (vepitemidxs))
2700 {
2701 vec_free (vepitemidxs);
2702 hash_unset (vsm->epitemidxs_by_epfd, __epfd);
2703 }
2704 }
2705 }
2706
2707 /* update epitemidxs */
2708 /* by_fd */
2709 p = hash_get (vsm->epitemidxs_by_fd, __fd);
2710 if (!p) /* not exist */
2711 {
2712 rv = -ENOENT;
2713 goto out;
2714 }
2715 else /* exists */
2716 {
2717 vepitemidxs = *(i32 **) p;
2718 vec_idx = vec_search (vepitemidxs, vepitemidx);
2719 if (vec_idx != ~0)
2720 {
2721 vec_del1 (vepitemidxs, vec_idx);
2722 if (!vec_len (vepitemidxs))
2723 {
2724 vec_free (vepitemidxs);
2725 hash_unset (vsm->epitemidxs_by_fd, __fd);
2726 }
2727 }
2728 }
2729
2730 /* pool put vepitem */
2731 vepitem = pool_elt_at_index (vsm->vepitems, vepitemidx);
2732 if (free_vepitem_on_del)
2733 {
2734 if (!vepitem)
2735 {
2736 rv = -ENOENT;
2737 goto out;
2738 }
2739 vepitem_init (vepitem);
2740 pool_put (vsm->vepitems, vepitem);
2741 }
2742 else
2743 {
2744 if (!vepitem)
2745 {
2746 vepitem_init (vepitem);
2747 }
2748 }
2749
2750 /* decrement vepoll fd count by 1 */
2751 vepoll->count -= 1;
2752
2753 rv = 0;
2754 goto out;
2755 break;
2756
2757 default:
2758 rv = -EINVAL;
2759 goto out;
2760 break;
2761 }
2762
2763out:
2764 return rv;
2765}
2766
2767/*
2768 * PRE: 00. null pointer check on __event
2769 * 01. all other parameters are validated
2770 */
2771
2772static int
2773vcom_socket_epoll_ctl_internal (int __epfd, int __op, int __fd,
2774 struct epoll_event *__event,
2775 int free_vepitem_on_del)
2776{
2777 int rv = -1;
Dave Wallacee695cb42017-11-02 22:04:42 -04002778 i32 cnt;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002779 vcom_epoll_t *vepoll;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002780 vcom_socket_t *vfd_vsock;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002781 i32 vep_idx;
Dave Wallacee695cb42017-11-02 22:04:42 -04002782 i32 sid;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002783
2784 /* get vep_idx and vepoll */
2785 vep_idx = vcom_socket_get_vep_idx_and_vepoll (__epfd, &vepoll);
2786 if (vep_idx == INVALID_VEP_IDX)
2787 {
2788 return -EBADF;
2789 }
2790
2791 /* get vcom fd type, vfd_id and vfd */
Dave Wallacee695cb42017-11-02 22:04:42 -04002792 sid = vcom_socket_get_sid_and_vsock (__fd, &vfd_vsock);
2793 if ((sid != INVALID_SESSION_ID) &&
2794 vcom_socket_type_is_vppcom_bound (vfd_vsock->type))
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002795 {
Dave Wallacee695cb42017-11-02 22:04:42 -04002796 rv = vppcom_epoll_ctl (vep_idx, __op, sid, __event);
2797 if (rv == VPPCOM_OK)
2798 {
2799 cnt = ((__op == EPOLL_CTL_ADD) ? 1 :
2800 (__op == EPOLL_CTL_DEL) ? -1 : 0);
2801 vepoll->count += cnt;
2802 vepoll->vcl_cnt += cnt;
2803 }
2804 if (VCOM_DEBUG > 0)
2805 fprintf (stderr,
2806 "[%d] vcom_socket_epoll_ctl_i: vppcom_epoll_ctl() "
2807 "returned %d\n\tepfd %d, vep_idx %d, fd %d sid %d op %d"
2808 "\n\tcount %d, vcl_cnt %d, libc_cnt %d\n",
Dave Wallace59179392017-11-07 02:20:07 -05002809 getpid (), rv, __epfd, vep_idx, __fd, sid, __op,
Dave Wallacee695cb42017-11-02 22:04:42 -04002810 vepoll->count, vepoll->vcl_cnt, vepoll->libc_cnt);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002811 }
2812 else
2813 {
Dave Wallacee695cb42017-11-02 22:04:42 -04002814 rv = libc_epoll_ctl (__epfd, __op, __fd, __event);
2815 if (rv == 0)
2816 {
2817 cnt = ((__op == EPOLL_CTL_ADD) ? 1 :
2818 (__op == EPOLL_CTL_DEL) ? -1 : 0);
2819 vepoll->count += cnt;
2820 vepoll->libc_cnt += cnt;
2821 }
2822 if (VCOM_DEBUG > 0)
2823 fprintf (stderr,
2824 "[%d] vcom_socket_epoll_ctl_i: libc_epoll_ctl() "
2825 "returned %d\n\tepfd %d, vep_idx %d, fd %d sid %d op %d"
2826 "\n\tcount %d, vcl_cnt %d, libc_cnt %d\n",
Dave Wallace59179392017-11-07 02:20:07 -05002827 getpid (), rv, __epfd, vep_idx, __fd, sid, __op,
Dave Wallacee695cb42017-11-02 22:04:42 -04002828 vepoll->count, vepoll->vcl_cnt, vepoll->libc_cnt);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002829 }
2830
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002831 return rv;
2832}
2833
2834int
2835vcom_socket_epoll_ctl (int __epfd, int __op, int __fd,
2836 struct epoll_event *__event)
2837{
2838 int rv = -1;
2839
2840 rv = vcom_socket_epoll_ctl_internal (__epfd, __op, __fd, __event, 1);
2841 return rv;
2842}
2843
2844static int
2845vcom_socket_epoll_ctl1 (int __epfd, int __op, int __fd,
2846 struct epoll_event *__event)
2847{
2848 int rv = -1;
2849
2850 rv = vcom_socket_epoll_ctl_internal (__epfd, __op, __fd, __event, 0);
2851 return rv;
2852}
2853
2854int
2855vcom_socket_epoll_pwait (int __epfd, struct epoll_event *__events,
2856 int __maxevents, int __timeout,
2857 const __sigset_t * __ss)
2858{
Dave Wallacee695cb42017-11-02 22:04:42 -04002859 vcom_socket_main_t *vsm = &vcom_socket_main;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002860 int rv = -EBADF;
Dave Wallacee695cb42017-11-02 22:04:42 -04002861 int rv2;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002862 double time_to_wait = (double) 0;
Dave Wallacee695cb42017-11-02 22:04:42 -04002863 double timeout;
2864 vcom_epoll_t *vepoll;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002865 i32 vep_idx;
Dave Wallacee695cb42017-11-02 22:04:42 -04002866 static struct epoll_event *libc_ev = 0;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002867
2868 /* validate __event */
Dave Wallacee695cb42017-11-02 22:04:42 -04002869 if (!__events || (__timeout < -1))
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002870 {
Dave Wallacee695cb42017-11-02 22:04:42 -04002871 fprintf (stderr, "[%d] ERROR: vcom_socket_epoll_pwait: "
Dave Wallace59179392017-11-07 02:20:07 -05002872 "Bad args __events %p, __timeout %d\n", getpid (),
Dave Wallacee695cb42017-11-02 22:04:42 -04002873 __events, __timeout);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002874 rv = -EFAULT;
2875 goto out;
2876 }
2877
Dave Wallacee695cb42017-11-02 22:04:42 -04002878 time_to_wait = ((__timeout > 0) ?
2879 (double) __timeout / (double) 1000 : (double) __timeout);
2880
2881 vep_idx = vcom_socket_get_vep_idx_and_vepoll (__epfd, &vepoll);
2882 if (vep_idx == INVALID_VEP_IDX)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002883 {
Dave Wallacee695cb42017-11-02 22:04:42 -04002884 fprintf (stderr, "[%d] ERROR: vcom_socket_epoll_pwait: "
Dave Wallace59179392017-11-07 02:20:07 -05002885 "Bad epoll fd %d\n", getpid (), __epfd);
Dave Wallacee695cb42017-11-02 22:04:42 -04002886 return -EBADF;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002887 }
Dave Wallacee695cb42017-11-02 22:04:42 -04002888
2889 if (vepoll->count <= 0)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002890 {
Dave Wallacee695cb42017-11-02 22:04:42 -04002891 fprintf (stderr, "[%d] ERROR: vcom_socket_epoll_pwait: No events"
2892 " in epfd!\n\tcount %d, vcl_cnt %d, libc_cnt %d\n",
Dave Wallace59179392017-11-07 02:20:07 -05002893 getpid (), vepoll->count, vepoll->vcl_cnt, vepoll->libc_cnt);
Dave Wallacee695cb42017-11-02 22:04:42 -04002894 rv = -EINVAL;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002895 goto out;
2896 }
2897
Dave Wallacee695cb42017-11-02 22:04:42 -04002898 if (vepoll->libc_cnt == 0)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002899 {
Dave Wallace59179392017-11-07 02:20:07 -05002900 if (VCOM_DEBUG > 2)
2901 fprintf (stderr, "[%d] vcom_socket_epoll_pwait: libc_cnt = 0, "
2902 "calling vppcom_epoll_wait()\n", getpid ());
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002903 rv = vppcom_epoll_wait (vep_idx, __events, __maxevents, time_to_wait);
2904 }
Dave Wallacee695cb42017-11-02 22:04:42 -04002905 else if (vepoll->vcl_cnt == 0)
2906 {
Dave Wallace59179392017-11-07 02:20:07 -05002907 if (VCOM_DEBUG > 2)
2908 fprintf (stderr, "[%d] vcom_socket_epoll_pwait: vcl_cnt = 0, "
2909 "calling libc_epoll_pwait()\n", getpid ());
Dave Wallacee695cb42017-11-02 22:04:42 -04002910 rv = libc_epoll_pwait (__epfd, __events, __maxevents, __timeout, __ss);
2911 }
2912 else
2913 {
Dave Wallace59179392017-11-07 02:20:07 -05002914 if (VCOM_DEBUG > 2)
2915 fprintf (stderr, "[%d] vcom_socket_epoll_pwait: vcl_cnt = %d, "
2916 "libc_cnt = %d -> mixed polling\n", getpid (),
2917 vepoll->vcl_cnt, vepoll->libc_cnt);
Dave Wallacee695cb42017-11-02 22:04:42 -04002918 vec_validate (libc_ev, __maxevents);
2919 timeout = clib_time_now (&vsm->clib_time) + time_to_wait;
2920 do
2921 {
2922 rv = vppcom_epoll_wait (vep_idx, __events, __maxevents, 0);
2923 rv2 = libc_epoll_pwait (__epfd, libc_ev, __maxevents, 1, __ss);
2924 if ((rv > 0) || (rv2 > 0))
2925 {
Dave Wallace59179392017-11-07 02:20:07 -05002926 if (VCOM_DEBUG > 2)
2927 fprintf (stderr, "[%d] vcom_socket_epoll_pwait: "
2928 "rv = %d, rv2 = %d\n", getpid (), rv, rv2);
Dave Wallacee695cb42017-11-02 22:04:42 -04002929 int n = __maxevents - rv;
2930 n = rv2 <= n ? rv2 : n;
2931 rv = (rv > 0) ? rv : 0;
2932
2933 clib_memcpy (&__events[rv], libc_ev, n * sizeof (*libc_ev));
2934 rv += rv2;
2935 goto out;
2936 }
2937 else if ((rv < 0) || (rv2 < 0))
2938 {
2939 if (rv < 0)
2940 fprintf (stderr,
2941 "[%d] ERROR: vppcom_epoll_wait() returned %d\n",
Dave Wallace59179392017-11-07 02:20:07 -05002942 getpid (), rv);
Dave Wallacee695cb42017-11-02 22:04:42 -04002943 if (rv2 < 0)
2944 {
2945 fprintf (stderr,
2946 "[%d] ERROR: libc_epoll_wait() failed, errno %d\n",
Dave Wallace59179392017-11-07 02:20:07 -05002947 getpid (), errno);
Dave Wallacee695cb42017-11-02 22:04:42 -04002948 rv = (rv < 0) ? rv : -errno;
2949 }
2950 goto out;
2951 }
2952 }
2953 while ((__timeout == -1)
2954 || (clib_time_now (&vsm->clib_time) < timeout));
2955 }
2956
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002957out:
Dave Wallacee695cb42017-11-02 22:04:42 -04002958 vec_reset_length (libc_ev);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002959 return rv;
2960}
2961
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002962static inline void
2963vcom_pollfds_2_selectfds (
2964 /* src */
2965 struct pollfd *__fds, nfds_t __nfds,
2966 /* dest */
2967 int vcom_nfds,
2968 fd_set * __restrict vcom_readfds,
2969 fd_set * __restrict vcom_writefds,
2970 fd_set * __restrict vcom_exceptfds)
2971{
2972 nfds_t fds_idx = 0;
2973
2974 for (fds_idx = 0; fds_idx < __nfds; fds_idx++)
2975 {
2976 /* ignore negative fds */
2977 if (__fds[fds_idx].fd < 0)
2978 {
2979 continue;
2980 }
2981
2982 /* for POLLRDHUP, POLLERR, POLLHUP and POLLNVAL */
2983 FD_SET (__fds[fds_idx].fd, vcom_exceptfds);
2984
2985 /* requested events */
2986 if (__fds[fds_idx].events)
2987 {
2988 if (__fds[fds_idx].events & POLLIN)
2989 {
2990 FD_SET (__fds[fds_idx].fd, vcom_readfds);
2991 }
2992 if (__fds[fds_idx].events & POLLPRI)
2993 {
2994 FD_SET (__fds[fds_idx].fd, vcom_readfds);
2995 }
2996 if (__fds[fds_idx].events & POLLOUT)
2997 {
2998 FD_SET (__fds[fds_idx].fd, vcom_writefds);
2999 }
3000#if defined __USE_XOPEN || defined __USE_XOPEN2K8
3001 if (__fds[fds_idx].events & POLLRDNORM)
3002 {
3003 FD_SET (__fds[fds_idx].fd, vcom_readfds);
3004 }
3005 if (__fds[fds_idx].events & POLLRDBAND)
3006 {
3007 FD_SET (__fds[fds_idx].fd, vcom_readfds);
3008 }
3009 if (__fds[fds_idx].events & POLLWRNORM)
3010 {
3011 FD_SET (__fds[fds_idx].fd, vcom_writefds);
3012 }
3013 if (__fds[fds_idx].events & POLLWRBAND)
3014 {
3015 FD_SET (__fds[fds_idx].fd, vcom_writefds);
3016 }
3017#endif
3018 }
3019 } /* for (fds_idx = 0; fds_idx < __nfds; fds_idx++) */
3020}
3021
3022static inline void
3023vcom_selectfds_2_pollfds (
3024 /* dest */
3025 struct pollfd *__fds, nfds_t __nfds, int *nfd,
3026 /* src */
3027 int vcom_nfds,
3028 fd_set * __restrict vcom_readfds,
3029 fd_set * __restrict vcom_writefds,
3030 fd_set * __restrict vcom_exceptfds)
3031{
3032 nfds_t fds_idx = 0;
3033
3034
3035 for (fds_idx = 0; fds_idx < __nfds; fds_idx++)
3036 {
3037 /* ignore negative fds */
3038 if (__fds[fds_idx].fd < 0)
3039 {
3040 __fds[fds_idx].revents = 0;
3041 }
3042
3043 /* for POLLRDHUP, POLLERR, POLLHUP and POLLNVAL */
3044 if (FD_ISSET (__fds[fds_idx].fd, vcom_exceptfds))
3045 {
3046 /*
3047 * TBD: for now any select exception
3048 * is flagged as POLLERR
3049 * */
3050 __fds[fds_idx].revents |= POLLERR;
3051 }
3052
3053 /* requested events */
3054 if (__fds[fds_idx].events & POLLIN)
3055 {
3056 if (FD_ISSET (__fds[fds_idx].fd, vcom_readfds))
3057 {
3058 __fds[fds_idx].revents |= POLLIN;
3059 }
3060 }
3061 if (__fds[fds_idx].events & POLLPRI)
3062 {
3063 if (FD_ISSET (__fds[fds_idx].fd, vcom_readfds))
3064 {
3065 __fds[fds_idx].revents |= POLLIN;
3066 }
3067 }
3068 if (__fds[fds_idx].events & POLLOUT)
3069 {
3070 if (FD_ISSET (__fds[fds_idx].fd, vcom_writefds))
3071 {
3072 __fds[fds_idx].revents |= POLLOUT;
3073 }
3074 }
3075#if defined __USE_XOPEN || defined __USE_XOPEN2K8
3076 if (__fds[fds_idx].events & POLLRDNORM)
3077 {
3078 if (FD_ISSET (__fds[fds_idx].fd, vcom_readfds))
3079 {
3080 __fds[fds_idx].revents |= POLLRDNORM;
3081 }
3082 }
3083 if (__fds[fds_idx].events & POLLRDBAND)
3084 {
3085 if (FD_ISSET (__fds[fds_idx].fd, vcom_readfds))
3086 {
3087 __fds[fds_idx].revents |= POLLRDBAND;
3088 }
3089 }
3090 if (__fds[fds_idx].events & POLLWRNORM)
3091 {
3092 if (FD_ISSET (__fds[fds_idx].fd, vcom_writefds))
3093 {
3094 __fds[fds_idx].revents |= POLLWRNORM;
3095 }
3096 }
3097 if (__fds[fds_idx].events & POLLWRBAND)
3098 {
3099 if (FD_ISSET (__fds[fds_idx].fd, vcom_writefds))
3100 {
3101 __fds[fds_idx].revents |= POLLWRBAND;
3102 }
3103 }
3104#endif
3105 } /* for (fds_idx = 0; fds_idx < __nfds; fds_idx++) */
3106
3107 /*
3108 * nfd:
3109 * the number of structures which have nonzero revents fields
3110 * (in other words, those descriptors with events or
3111 * errors reported)
3112 * */
3113 *nfd = 0;
3114 for (fds_idx = 0; fds_idx < __nfds; fds_idx++)
3115 {
3116 /* ignore negative fds */
3117 if (__fds[fds_idx].fd < 0)
3118 {
3119 continue;
3120 }
3121
3122 if (__fds[fds_idx].revents)
3123 {
3124 (*nfd)++;
3125 }
3126 }
3127}
3128
3129/*
3130 * PRE: parameters are validated,
3131 * vcom_socket_poll is always called with __timeout set to zero
3132 * hence returns immediately
3133 *
3134 * ACTION: handle non negative validated vcom fds and ignore rest
3135 */
3136
3137/*
3138 * implements vcom_socket_poll () interface
3139 *
3140 * internally uses vcom_socket_select ()
3141 * to realize the behavior
3142 * */
3143int
3144vcom_socket_poll_select_impl (struct pollfd *__fds, nfds_t __nfds,
3145 int __timeout)
3146{
3147 int rv;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07003148
3149 nfds_t fds_idx = 0;
3150 int nfd = 0;
3151
3152 /* vcom */
3153 int vcom_nfds = 0;
3154 fd_set vcom_readfds;
3155 fd_set vcom_writefds;
3156 fd_set vcom_exceptfds;
3157 int vcom_nfd = -1;
3158 /* invalid max_vcom_fd is -1 */
3159 int max_vcom_fd = -1;
3160
3161 /* __timeout is zero to get ready events and return immediately */
3162 struct timeval tv = {.tv_sec = 0,.tv_usec = 0 };
3163
3164 /* validate __nfds from select perspective */
Dave Wallace5c7cf1c2017-10-24 04:12:18 -04003165 if (__nfds > FD_SETSIZE)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07003166 {
3167 rv = -EINVAL;
3168 goto poll_done;
3169 }
3170
3171 /* zero vcom fd sets */
3172 /*
3173 * V vcom fd set
3174 */
3175#define _(V) \
3176 FD_ZERO ((V))
3177
3178 _(&vcom_readfds);
3179 _(&vcom_writefds);
3180 _(&vcom_exceptfds);
3181#undef _
3182
3183 vcom_nfds = 0;
3184 vcom_nfd = -1;
3185
3186
3187 for (fds_idx = 0; fds_idx < __nfds; fds_idx++)
3188 {
3189 /* ignore negative fds */
3190 if (__fds[fds_idx].fd < 0)
3191 {
3192 continue;
3193 }
3194
3195 /* non negative validated vcom fds */
3196 if (__fds[fds_idx].fd > FD_SETSIZE)
3197 {
3198 rv = -EINVAL;
3199 goto poll_done;
3200 }
3201
3202 /* max_vcom_fd and vcom_nfd */
3203 if (__fds[fds_idx].fd > max_vcom_fd)
3204 {
3205 /* requested events */
3206 if (__fds[fds_idx].events)
3207 {
3208 max_vcom_fd = __fds[fds_idx].fd;
3209 }
3210 }
3211 ++vcom_nfd;
3212 }
3213
3214 vcom_nfds = max_vcom_fd != -1 ? max_vcom_fd + 1 : 0;
3215
3216 if (!vcom_nfds)
3217 {
3218 rv = vcom_nfds;
3219 goto poll_done;
3220 }
3221
3222 vcom_pollfds_2_selectfds (
3223 /* src */
3224 __fds, __nfds,
3225 /* dest */
3226 vcom_nfds,
3227 &vcom_readfds, &vcom_writefds, &vcom_exceptfds);
3228
3229 /* select on vcom fds */
3230 vcom_nfd = vcom_socket_select (vcom_nfds,
3231 &vcom_readfds,
3232 &vcom_writefds, &vcom_exceptfds, &tv);
Dave Wallacee22aa742017-10-20 12:30:38 -04003233 if (VCOM_DEBUG > 2)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07003234 fprintf (stderr,
3235 "[%d] vcom_socket_select: "
Dave Wallace59179392017-11-07 02:20:07 -05003236 "'%04d'='%04d'\n", getpid (), vcom_nfd, vcom_nfds);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07003237
3238 if (vcom_nfd < 0)
3239 {
3240 rv = vcom_nfd;
3241 goto poll_done;
3242 }
3243
3244 vcom_selectfds_2_pollfds (
3245 /* dest */
3246 __fds, __nfds, &nfd,
3247 /* src */
3248 vcom_nfds,
3249 &vcom_readfds, &vcom_writefds, &vcom_exceptfds);
3250
3251 rv = nfd;
3252
3253poll_done:
3254 return rv;
3255}
3256
3257/*
3258 * TBD: remove this static function once vppcom
3259 * has an implementation in place
3260 *
3261 * ACTION:
3262 */
3263static int
3264vppcom_poll (struct pollfd *__fds, nfds_t __nfds, double time_to_wait)
3265{
3266 return -EOPNOTSUPP;
3267}
3268
3269int
3270vcom_socket_poll_vppcom_impl (struct pollfd *__fds, nfds_t __nfds,
3271 int __timeout)
3272{
3273 nfds_t fds_idx = 0;
3274
3275 /* in seconds eg. 3.123456789 seconds */
3276 double time_to_wait = (double) 0;
3277
3278 i32 sid;
3279 i32 vep_idx;
3280
3281 /* replace vcom fd with session idx */
3282 for (fds_idx = 0; fds_idx < __nfds; fds_idx++)
3283 {
3284 /* ignore negative fds */
3285 if (__fds[fds_idx].fd < 0)
3286 {
3287 continue;
3288 }
3289
3290 /* non negative validated vcom fds */
3291 sid = vcom_socket_get_sid (__fds[fds_idx].fd);
3292 if (sid != INVALID_SESSION_ID)
3293 {
3294 __fds[fds_idx].fd = sid;
3295 }
3296 else
3297 {
3298 /* get vep_idx */
3299 vep_idx = vcom_socket_get_vep_idx (__fds[fds_idx].fd);
3300 if (vep_idx != INVALID_VEP_IDX)
3301 {
3302 __fds[fds_idx].fd = vep_idx;
3303 }
3304 else
3305 {
3306 return -EBADF;
3307 }
3308 }
3309 }
3310
3311 /* validate __timeout */
3312 if (__timeout > 0)
3313 {
3314 time_to_wait = (double) __timeout / (double) 1000;
3315 }
3316 else if (__timeout == 0)
3317 {
3318 time_to_wait = (double) 0;
3319 }
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07003320 else
3321 {
Dave Wallace60f54822017-10-24 20:47:45 -04003322 time_to_wait = ~0;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07003323 }
3324
3325 return vppcom_poll (__fds, __nfds, time_to_wait);
3326}
3327
3328int
3329vcom_socket_poll (struct pollfd *__fds, nfds_t __nfds, int __timeout)
3330{
3331 /* select an implementation */
3332
3333 /* return vcom_socket_poll_vppcom_impl (__fds, __nfds, __timeout); */
3334 return vcom_socket_poll_select_impl (__fds, __nfds, __timeout);
3335}
3336
3337#ifdef __USE_GNU
3338int
3339vcom_socket_ppoll (struct pollfd *__fds, nfds_t __nfds,
3340 const struct timespec *__timeout, const __sigset_t * __ss)
3341{
3342 return -EOPNOTSUPP;
3343}
3344#endif
3345
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07003346int
3347vcom_socket_main_init (void)
3348{
3349 vcom_socket_main_t *vsm = &vcom_socket_main;
3350
3351 if (VCOM_DEBUG > 0)
3352 printf ("vcom_socket_main_init\n");
3353
3354 if (!vsm->init)
3355 {
3356 /* TBD: define FD_MAXSIZE and use it here */
3357 pool_alloc (vsm->vsockets, FD_SETSIZE);
3358 vsm->sockidx_by_fd = hash_create (0, sizeof (i32));
3359
3360 pool_alloc (vsm->vepolls, FD_SETSIZE);
3361 vsm->epollidx_by_epfd = hash_create (0, sizeof (i32));
3362
3363 pool_alloc (vsm->vepitems, FD_SETSIZE);
3364 vsm->epitemidx_by_epfdfd = hash_create (0, sizeof (i32));
3365
3366 vsm->epitemidxs_by_epfd = hash_create (0, sizeof (i32 *));
3367 vsm->epitemidxs_by_fd = hash_create (0, sizeof (i32 *));
3368
Dave Wallacee695cb42017-11-02 22:04:42 -04003369 clib_time_init (&vsm->clib_time);
Dave Wallacee695cb42017-11-02 22:04:42 -04003370
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07003371 vsm->init = 1;
3372 }
3373
3374 return 0;
3375}
3376
3377
3378void
3379vcom_socket_main_show (void)
3380{
3381 vcom_socket_main_t *vsm = &vcom_socket_main;
3382 vcom_socket_t *vsock;
3383
3384 vcom_epoll_t *vepoll;
3385
3386 vcom_epitem_t *vepitem;
3387
3388 i32 epfd;
3389 i32 fd;
3390 i32 *vepitemidxs, *vepitemidxs_var;
3391
3392 if (vsm->init)
3393 {
3394 /* from active list of vsockets show vsock */
3395
3396 /* *INDENT-OFF* */
3397 pool_foreach (vsock, vsm->vsockets,
3398 ({
3399 printf(
3400 "fd='%04d', sid='%08x',type='%-30s'\n",
3401 vsock->fd, vsock->sid,
3402 vcom_socket_type_str (vsock->type));
3403 }));
3404 /* *INDENT-ON* */
3405
3406 /* from active list of vepolls, show vepoll */
3407
3408 /* *INDENT-OFF* */
3409 pool_foreach (vepoll, vsm->vepolls,
3410 ({
3411 printf(
3412 "epfd='%04d', vep_idx='%08x', "
3413 "type='%-30s', "
3414 "flags='%d', count='%d', close='%d'\n",
3415 vepoll->epfd, vepoll->vep_idx,
3416 vcom_socket_epoll_type_str (vepoll->type),
3417 vepoll->flags, vepoll->count, vepoll->close);
3418 }));
3419 /* *INDENT-ON* */
3420
3421 /* from active list of vepitems, show vepitem */
3422
3423 /* *INDENT-OFF* */
3424 pool_foreach (vepitem, vsm->vepitems,
3425 ({
3426 printf(
3427 "epfd='%04d', fd='%04d', "
3428 "next_fd='%04d', prev_fd='%04d', "
3429 "type='%-30s', "
3430 "events='%04x', revents='%04x'\n",
3431 vepitem->epfd, vepitem->fd,
3432 vepitem->next_fd, vepitem->prev_fd,
3433 vcom_socket_vcom_fd_type_str (vepitem->type),
3434 vepitem->event.events, vepitem->revent.events);
3435 }));
3436
3437 /* *INDENT-ON* */
3438
3439 /* show epitemidxs for epfd */
3440 /* *INDENT-OFF* */
3441 hash_foreach (epfd, vepitemidxs,
3442 vsm->epitemidxs_by_epfd,
3443 ({
3444 printf("\n[ '%04d': ", epfd);
3445 vec_foreach (vepitemidxs_var,vepitemidxs)
3446 {
3447 printf("'%04d' ", (int)vepitemidxs_var[0]);
3448 }
3449 printf("]\n");
3450 }));
3451 /* *INDENT-ON* */
3452
3453 /* show epitemidxs for fd */
3454 /* *INDENT-OFF* */
3455 hash_foreach (fd, vepitemidxs,
3456 vsm->epitemidxs_by_fd,
3457 ({
3458 printf("\n{ '%04d': ", fd);
3459 vec_foreach (vepitemidxs_var,vepitemidxs)
3460 {
3461 printf("'%04d' ", (int)vepitemidxs_var[0]);
3462 }
3463 printf("}\n");
3464 }));
3465 /* *INDENT-ON* */
3466
3467 }
3468}
3469
3470void
3471vcom_socket_main_destroy (void)
3472{
3473 vcom_socket_main_t *vsm = &vcom_socket_main;
3474 vcom_socket_t *vsock;
3475
3476 vcom_epoll_t *vepoll;
3477
3478 vcom_epitem_t *vepitem;
3479
3480 i32 epfd;
3481 i32 fd;
3482 i32 *vepitemidxs;
3483
3484
3485 if (VCOM_DEBUG > 0)
3486 printf ("vcom_socket_main_destroy\n");
3487
3488 if (vsm->init)
3489 {
3490
3491 /*
3492 * from active list of vepitems,
3493 * remove all "vepitem" elements from the pool in a safe way
3494 * */
3495
3496 /* *INDENT-OFF* */
3497 pool_flush (vepitem, vsm->vepitems,
3498 ({
Dave Wallace60f54822017-10-24 20:47:45 -04003499 if ((vepitem->type == FD_TYPE_EPOLL) ||
3500 (vepitem->type == FD_TYPE_VCOM_SOCKET))
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07003501 {
Dave Wallace60f54822017-10-24 20:47:45 -04003502 vcom_socket_epoll_ctl1 (vepitem->epfd, EPOLL_CTL_DEL,
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07003503 vepitem->fd, NULL);
3504 vepitem_init (vepitem);
3505 }
3506 }));
3507 /* *INDENT-ON* */
3508
3509 pool_free (vsm->vepitems);
3510 hash_free (vsm->epitemidx_by_epfdfd);
3511
3512 /* free vepitemidxs for each epfd */
3513 /* *INDENT-OFF* */
3514 hash_foreach (epfd, vepitemidxs,
3515 vsm->epitemidxs_by_epfd,
3516 ({
3517 vec_free (vepitemidxs);
3518 }));
3519 /* *INDENT-ON* */
3520 hash_free (vsm->epitemidxs_by_epfd);
3521
3522 /* free vepitemidxs for each fd */
3523 /* *INDENT-OFF* */
3524 hash_foreach (fd, vepitemidxs,
3525 vsm->epitemidxs_by_fd,
3526 ({
3527 vec_free (vepitemidxs);
3528 }));
3529 /* *INDENT-ON* */
3530 hash_free (vsm->epitemidxs_by_fd);
3531
3532
3533 /*
3534 * from active list of vsockets,
3535 * close socket and vppcom session
3536 * */
3537
3538 /* *INDENT-OFF* */
3539 pool_foreach (vsock, vsm->vsockets,
3540 ({
3541 if (vsock->type == SOCKET_TYPE_VPPCOM_BOUND)
3542 {
3543 vppcom_session_close (vsock->sid);
3544 vcom_socket_close_socket (vsock->fd);
3545 vsocket_init (vsock);
3546 }
3547 }));
3548 /* *INDENT-ON* */
3549
3550 /*
3551 * return vsocket element to the pool
3552 * */
3553
3554 /* *INDENT-OFF* */
3555 pool_flush (vsock, vsm->vsockets,
3556 ({
3557 // vsocket_init(vsock);
3558 ;
3559 }));
3560 /* *INDENT-ON* */
3561
3562 pool_free (vsm->vsockets);
3563 hash_free (vsm->sockidx_by_fd);
3564
3565 /*
3566 * from active list of vepolls,
3567 * close epoll and vppcom_epoll
3568 * */
3569
3570 /* *INDENT-OFF* */
3571 pool_foreach (vepoll, vsm->vepolls,
3572 ({
3573 if (vepoll->type == EPOLL_TYPE_VPPCOM_BOUND)
3574 {
3575 vppcom_session_close (vepoll->vep_idx);
3576 vcom_socket_close_epoll (vepoll->epfd); /* TBD: */
3577 vepoll_init (vepoll);
3578 }
3579 }));
3580 /* *INDENT-ON* */
3581
3582 /*
3583 * return vepoll element to the pool
3584 * */
3585
3586 /* *INDENT-OFF* */
3587 pool_flush (vepoll, vsm->vepolls,
3588 ({
3589 // vepoll_init(vepoll);
3590 ;
3591 }));
3592 /* *INDENT-ON* */
3593
3594 pool_free (vsm->vepolls);
3595 hash_free (vsm->epollidx_by_epfd);
3596
3597 vsm->init = 0;
3598 }
3599}
3600
3601
3602/*
3603 * fd.io coding-style-patch-verification: ON
3604 *
3605 * Local Variables:
3606 * eval: (c-set-style "gnu")
3607 * End:
3608 */