blob: 5378548f9c22b9da8d14d8bcfce2d3f008828b8f [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>
24#include <vppinfra/hash.h>
25#include <vppinfra/pool.h>
26
27#include <libvcl-ldpreload/vcom_socket.h>
28#include <libvcl-ldpreload/vcom_socket_wrapper.h>
29#include <libvcl-ldpreload/vcom.h>
30
31#include <uri/vppcom.h>
32
33
34/*
35 * VCOM_SOCKET Private definitions and functions.
36 */
37
38typedef struct vcom_socket_main_t_
39{
40 u8 init;
41
42 /* vcom_socket pool */
43 vcom_socket_t *vsockets;
44
45 /* Hash table for socketidx to fd mapping */
46 uword *sockidx_by_fd;
47
48 /* vcom_epoll pool */
49 vcom_epoll_t *vepolls;
50
51 /* Hash table for epollidx to epfd mapping */
52 uword *epollidx_by_epfd;
53
54
55 /* common epitem poll for all epfd */
56 /* TBD: epitem poll per epfd */
57 /* vcom_epitem pool */
58 vcom_epitem_t *vepitems;
59
60 /* Hash table for epitemidx to epfdfd mapping */
61 uword *epitemidx_by_epfdfd;
62
63 /* Hash table - key:epfd, value:vec of epitemidx */
64 uword *epitemidxs_by_epfd;
65 /* Hash table - key:fd, value:vec of epitemidx */
66 uword *epitemidxs_by_fd;
67
68} vcom_socket_main_t;
69
70vcom_socket_main_t vcom_socket_main;
71
72
73static int
74vcom_socket_open_socket (int domain, int type, int protocol)
75{
76 int rv = -1;
77
78 /* handle domains implemented by vpp */
79 switch (domain)
80 {
81 case AF_INET:
82 case AF_INET6:
83 /* get socket type and
84 * handle the socket types supported by vpp */
85 switch (type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
86 {
87 case SOCK_STREAM:
88 case SOCK_DGRAM:
89 /* the type argument serves a second purpose,
90 * in addition to specifying a socket type,
91 * it may include the bitwise OR of any of
92 * SOCK_NONBLOCK and SOCK_CLOEXEC, to modify
93 * the behavior of socket. */
94 rv = libc_socket (domain, type, protocol);
95 if (rv == -1)
96 rv = -errno;
97 break;
98
99 default:
100 break;
101 }
102
103 break;
104
105 default:
106 break;
107 }
108
109 return rv;
110}
111
112static int
113vcom_socket_open_epoll (int flags)
114{
115 int rv = -1;
116
117 if (flags < 0)
118 {
119 return -EINVAL;
120 }
121 if (flags && (flags & ~EPOLL_CLOEXEC))
122 {
123 return -EINVAL;
124 }
125
126 /* flags can be either zero or EPOLL_CLOEXEC */
127 rv = libc_epoll_create1 (flags);
128 if (rv == -1)
129 rv = -errno;
130
131 return rv;
132}
133
134static int
135vcom_socket_close_socket (int fd)
136{
137 int rv;
138
139 rv = libc_close (fd);
140 if (rv == -1)
141 rv = -errno;
142
143 return rv;
144}
145
146static int
147vcom_socket_close_epoll (int epfd)
148{
149 int rv;
150
151 rv = libc_close (epfd);
152 if (rv == -1)
153 rv = -errno;
154
155 return rv;
156}
157
158/*
159 * Public API functions
160 */
161
162
163int
164vcom_socket_is_vcom_fd (int fd)
165{
166 vcom_socket_main_t *vsm = &vcom_socket_main;
167 uword *p;
168 vcom_socket_t *vsock;
169
170 p = hash_get (vsm->sockidx_by_fd, fd);
171
172 if (p)
173 {
174 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
175 if (vsock && vsock->type == SOCKET_TYPE_VPPCOM_BOUND)
176 return 1;
177 }
178 return 0;
179}
180
181int
182vcom_socket_is_vcom_epfd (int epfd)
183{
184 vcom_socket_main_t *vsm = &vcom_socket_main;
185 uword *p;
186 vcom_epoll_t *vepoll;
187
188 p = hash_get (vsm->epollidx_by_epfd, epfd);
189
190 if (p)
191 {
192 vepoll = pool_elt_at_index (vsm->vepolls, p[0]);
193 if (vepoll && vepoll->type == EPOLL_TYPE_VPPCOM_BOUND)
194 return 1;
195 }
196 return 0;
197}
198
199static inline int
200vcom_socket_get_sid (int fd)
201{
202 vcom_socket_main_t *vsm = &vcom_socket_main;
203 uword *p;
204 vcom_socket_t *vsock;
205
206 p = hash_get (vsm->sockidx_by_fd, fd);
207
208 if (p)
209 {
210 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
211 if (vsock && vsock->type == SOCKET_TYPE_VPPCOM_BOUND)
212 return vsock->sid;
213 }
214 return INVALID_SESSION_ID;
215}
216
217static inline int
218vcom_socket_get_vep_idx (int epfd)
219{
220 vcom_socket_main_t *vsm = &vcom_socket_main;
221 uword *p;
222 vcom_epoll_t *vepoll;
223
224 p = hash_get (vsm->epollidx_by_epfd, epfd);
225
226 if (p)
227 {
228 vepoll = pool_elt_at_index (vsm->vepolls, p[0]);
229 if (vepoll && vepoll->type == EPOLL_TYPE_VPPCOM_BOUND)
230 return vepoll->vep_idx;
231 }
232 return INVALID_VEP_IDX;
233}
234
235static inline int
236vcom_socket_get_sid_and_vsock (int fd, vcom_socket_t ** vsockp)
237{
238 vcom_socket_main_t *vsm = &vcom_socket_main;
239 uword *p;
240 vcom_socket_t *vsock;
241
242 p = hash_get (vsm->sockidx_by_fd, fd);
243
244 if (p)
245 {
246 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
247 if (vsock && vsock->type == SOCKET_TYPE_VPPCOM_BOUND)
248 {
249 *vsockp = vsock;
250 return vsock->sid;
251 }
252 }
253 return INVALID_SESSION_ID;
254}
255
256static inline int
257vcom_socket_get_vep_idx_and_vepoll (int epfd, vcom_epoll_t ** vepollp)
258{
259 vcom_socket_main_t *vsm = &vcom_socket_main;
260 uword *p;
261 vcom_epoll_t *vepoll;
262
263 p = hash_get (vsm->epollidx_by_epfd, epfd);
264
265 if (p)
266 {
267 vepoll = pool_elt_at_index (vsm->vepolls, p[0]);
268 if (vepoll && vepoll->type == EPOLL_TYPE_VPPCOM_BOUND)
269 {
270 *vepollp = vepoll;
271 return vepoll->vep_idx;
272 }
273 }
274 return INVALID_VEP_IDX;
275}
276
277
278static int
279vcom_socket_close_vepoll (int epfd)
280{
281 int rv = -1;
282 vcom_socket_main_t *vsm = &vcom_socket_main;
283 uword *p;
284 vcom_epoll_t *vepoll;
285
286 p = hash_get (vsm->epollidx_by_epfd, epfd);
287 if (!p)
288 return -EBADF;
289
290 vepoll = pool_elt_at_index (vsm->vepolls, p[0]);
291 if (!vepoll)
292 return -EBADF;
293
294 if (vepoll->type != EPOLL_TYPE_VPPCOM_BOUND)
295 return -EINVAL;
296
297 if (vepoll->count)
298 {
299 if (!vepoll->close)
300 {
301 vepoll->close = 1;
302 return 0;
303 }
304 else
305 {
306 return -EBADF;
307 }
308 }
309
310 /* count is zero */
311 rv = vppcom_session_close (vepoll->vep_idx);
312 rv = vcom_socket_close_epoll (vepoll->epfd);
313
314 vepoll_init (vepoll);
315 hash_unset (vsm->epollidx_by_epfd, epfd);
316 pool_put (vsm->vepolls, vepoll);
317
318 return rv;
319}
320
321static int
322vcom_socket_close_vsock (int fd)
323{
324 int rv = -1;
325 vcom_socket_main_t *vsm = &vcom_socket_main;
326 uword *p;
327 vcom_socket_t *vsock;
328
329 vcom_epitem_t *vepitem;
330
331 i32 *vepitemidxs = 0;
332 i32 *vepitemidxs_var = 0;
333
334 p = hash_get (vsm->sockidx_by_fd, fd);
335 if (!p)
336 return -EBADF;
337
338 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
339 if (!vsock)
340 return -ENOTSOCK;
341
342 if (vsock->type != SOCKET_TYPE_VPPCOM_BOUND)
343 return -EINVAL;
344
345 rv = vppcom_session_close (vsock->sid);
346 rv = vcom_socket_close_socket (vsock->fd);
347
348 vsocket_init (vsock);
349 hash_unset (vsm->sockidx_by_fd, fd);
350 pool_put (vsm->vsockets, vsock);
351
352 /*
353 * NOTE:
354 * Before calling close(), user should remove
355 * this fd from the epoll-set of all epoll instances,
356 * otherwise resource(epitems) leaks ensues.
357 */
358
359 /*
360 * 00. close all epoll instances that are marked as "close"
361 * of which this fd is the "last" remaining member.
362 * 01. epitems associated with this fd are intentionally
363 * not removed, see NOTE: above.
364 * */
365
366 /* does this fd participate in epoll */
367 p = hash_get (vsm->epitemidxs_by_fd, fd);
368 if (p)
369 {
370 vepitemidxs = *(i32 **) p;
371 vec_foreach (vepitemidxs_var, vepitemidxs)
372 {
373 vepitem = pool_elt_at_index (vsm->vepitems, vepitemidxs_var[0]);
374 if (vepitem && vepitem->fd == fd &&
375 vepitem->type == FD_TYPE_VCOM_SOCKET)
376 {
377 i32 vep_idx;
378 vcom_epoll_t *vepoll;
379 if ((vep_idx =
380 vcom_socket_get_vep_idx_and_vepoll (vepitem->epfd,
381 &vepoll)) !=
382 INVALID_VEP_IDX)
383 {
384 if (vepoll->close)
385 {
386 if (vepoll->count == 1)
387 {
388 /*
389 * force count to zero and
390 * close this epoll instance
391 * */
392 vepoll->count = 0;
393 vcom_socket_close_vepoll (vepoll->epfd);
394 }
395 else
396 {
397 vepoll->count -= 1;
398 }
399 }
400 }
401 }
402
403 }
404 }
405
406 return rv;
407}
408
409int
410vcom_socket_close (int __fd)
411{
412 int rv;
413
414 if (vcom_socket_is_vcom_fd (__fd))
415 {
416 rv = vcom_socket_close_vsock (__fd);
417 }
418 else if (vcom_socket_is_vcom_epfd (__fd))
419 {
420 rv = vcom_socket_close_vepoll (__fd);
421 }
422 else
423 {
424 rv = -EBADF;
425 }
426
427 return rv;
428}
429
430ssize_t
431vcom_socket_read (int __fd, void *__buf, size_t __nbytes)
432{
433 int rv = -1;
434 vcom_socket_main_t *vsm = &vcom_socket_main;
435 uword *p;
436 vcom_socket_t *vsock;
437
438 p = hash_get (vsm->sockidx_by_fd, __fd);
439 if (!p)
440 return -EBADF;
441
442 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
443 if (!vsock)
444 return -ENOTSOCK;
445
446 if (vsock->type != SOCKET_TYPE_VPPCOM_BOUND)
447 return -EINVAL;
448
449 if (!__buf || __nbytes < 0)
450 {
451 return -EINVAL;
452 }
453
454 rv = vcom_fcntl (__fd, F_GETFL, 0);
455 if (rv < 0)
456 {
457 return rv;
458
459 }
460
461 /* is blocking */
462 if (!(rv & O_NONBLOCK))
463 {
464 do
465 {
466 rv = vppcom_session_read (vsock->sid, __buf, __nbytes);
467 }
468 while (rv == -EAGAIN || rv == -EWOULDBLOCK);
469 return rv;
470 }
471 /* The file descriptor refers to a socket and has been
472 * marked nonblocking(O_NONBLOCK) and the read would
473 * block.
474 * */
475 /* is non blocking */
476 rv = vppcom_session_read (vsock->sid, __buf, __nbytes);
477 return rv;
478}
479
480ssize_t
481vcom_socket_readv (int __fd, const struct iovec * __iov, int __iovcnt)
482{
483 int rv;
484 vcom_socket_main_t *vsm = &vcom_socket_main;
485 uword *p;
486 vcom_socket_t *vsock;
487 ssize_t total = 0, len = 0;
488
489 p = hash_get (vsm->sockidx_by_fd, __fd);
490 if (!p)
491 return -EBADF;
492
493 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
494 if (!vsock)
495 return -ENOTSOCK;
496
497 if (vsock->type != SOCKET_TYPE_VPPCOM_BOUND)
498 return -EINVAL;
499
500 if (__iov == 0 || __iovcnt == 0 || __iovcnt > IOV_MAX)
501 return -EINVAL;
502
503 /* Sanity check */
504 for (int i = 0; i < __iovcnt; ++i)
505 {
506 if (SSIZE_MAX - len < __iov[i].iov_len)
507 return -EINVAL;
508 len += __iov[i].iov_len;
509 }
510
511 rv = vcom_fcntl (__fd, F_GETFL, 0);
512 if (rv < 0)
513 {
514 return rv;
515 }
516
517 /* is blocking */
518 if (!(rv & O_NONBLOCK))
519 {
520 do
521 {
522 for (int i = 0; i < __iovcnt; ++i)
523 {
524 rv = vppcom_session_read (vsock->sid, __iov[i].iov_base,
525 __iov[i].iov_len);
526 if (rv < 0)
527 break;
528 else
529 {
530 total += rv;
531 if (rv < __iov[i].iov_len)
532 /* Read less than buffer provided, no point to continue */
533 break;
534 }
535 }
536 }
537 while ((rv == -EAGAIN || rv == -EWOULDBLOCK) && total == 0);
538 return total;
539 }
540
541 /* is non blocking */
542 for (int i = 0; i < __iovcnt; ++i)
543 {
544 rv = vppcom_session_read (vsock->sid, __iov[i].iov_base,
545 __iov[i].iov_len);
546 if (rv < 0)
547 {
548 if (total > 0)
549 break;
550 else
551 {
552 errno = rv;
553 return rv;
554 }
555 }
556 else
557 {
558 total += rv;
559 if (rv < __iov[i].iov_len)
560 /* Read less than buffer provided, no point to continue */
561 break;
562 }
563 }
564 return total;
565}
566
567ssize_t
568vcom_socket_write (int __fd, const void *__buf, size_t __n)
569{
570 int rv = -1;
571 vcom_socket_main_t *vsm = &vcom_socket_main;
572 uword *p;
573 vcom_socket_t *vsock;
574
575 p = hash_get (vsm->sockidx_by_fd, __fd);
576 if (!p)
577 return -EBADF;
578
579 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
580 if (!vsock)
581 return -ENOTSOCK;
582
583 if (vsock->type != SOCKET_TYPE_VPPCOM_BOUND)
584 return -EINVAL;
585
586 if (!__buf || __n < 0)
587 {
588 return -EINVAL;
589 }
590
591 rv = vppcom_session_write (vsock->sid, (void *) __buf, __n);
592 return rv;
593}
594
595ssize_t
596vcom_socket_writev (int __fd, const struct iovec * __iov, int __iovcnt)
597{
598 int rv = -1;
599 ssize_t total = 0;
600 vcom_socket_main_t *vsm = &vcom_socket_main;
601 uword *p;
602 vcom_socket_t *vsock;
603
604 p = hash_get (vsm->sockidx_by_fd, __fd);
605 if (!p)
606 return -EBADF;
607
608 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
609 if (!vsock)
610 return -ENOTSOCK;
611
612 if (vsock->type != SOCKET_TYPE_VPPCOM_BOUND)
613 return -EINVAL;
614
615 if (__iov == 0 || __iovcnt == 0 || __iovcnt > IOV_MAX)
616 return -EINVAL;
617
618 for (int i = 0; i < __iovcnt; ++i)
619 {
620 rv = vppcom_session_write (vsock->sid, __iov[i].iov_base,
621 __iov[i].iov_len);
622 if (rv < 0)
623 {
624 if (total > 0)
625 break;
626 else
627 return rv;
628 }
629 else
630 total += rv;
631 }
632 return total;
633}
634
635/*
636 * RETURN: 0 - invalid cmd
637 * 1 - cmd not handled by vcom and vppcom
638 * 2 - cmd handled by vcom socket resource
639 * 3 - cmd handled by vppcom
640 * */
641/* TBD: incomplete list of cmd */
642static int
643vcom_socket_check_fcntl_cmd (int __cmd)
644{
645 switch (__cmd)
646 {
647 /*cmd not handled by vcom and vppcom */
648 /* Fallthrough */
649 case F_DUPFD:
650 case F_DUPFD_CLOEXEC:
651 return 1;
652
653 /* cmd handled by vcom socket resource */
654 /* Fallthrough */
655 case F_GETFD:
656 case F_SETFD:
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700657 case F_GETLK:
658 case F_SETLK:
659 case F_SETLKW:
660 case F_GETOWN:
661 case F_SETOWN:
662 return 2;
663
Stevenb59f2272017-10-12 17:10:33 -0700664 /* cmd handled by vcom and vppcom */
665 case F_SETFL:
666 case F_GETFL:
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700667 return 3;
Stevenb59f2272017-10-12 17:10:33 -0700668
669 /* cmd not handled by vcom and vppcom */
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700670 default:
Stevenb59f2272017-10-12 17:10:33 -0700671 return 1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700672 }
673 return 0;
674}
675
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700676static int
Stevenb59f2272017-10-12 17:10:33 -0700677vppcom_session_fcntl_va (int __sid, int __cmd, va_list __ap)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700678{
Stevenb59f2272017-10-12 17:10:33 -0700679 int flags = va_arg (__ap, int);
680 int rv = -EOPNOTSUPP;
681 uint32_t size;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700682
Stevenb59f2272017-10-12 17:10:33 -0700683 size = sizeof (flags);
684 if (__cmd == F_SETFL)
685 {
686 rv = vppcom_session_attr (__sid, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
687 }
688 else if (__cmd == F_GETFL)
689 {
690 rv = vppcom_session_attr (__sid, VPPCOM_ATTR_GET_FLAGS, &flags, &size);
691 if (rv == VPPCOM_OK)
692 rv = flags;
693 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700694
695 return rv;
696}
697
698int
699vcom_socket_fcntl_va (int __fd, int __cmd, va_list __ap)
700{
701 int rv = -EBADF;
702 vcom_socket_main_t *vsm = &vcom_socket_main;
703 uword *p;
704 vcom_socket_t *vsock;
705
706 p = hash_get (vsm->sockidx_by_fd, __fd);
707 if (!p)
708 return -EBADF;
709
710 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
711 if (!vsock)
712 return -ENOTSOCK;
713
714 if (vsock->type != SOCKET_TYPE_VPPCOM_BOUND)
715 return -EINVAL;
716
717 switch (vcom_socket_check_fcntl_cmd (__cmd))
718 {
719 /* invalid cmd */
720 case 0:
721 rv = -EBADF;
722 break;
723 /*cmd not handled by vcom and vppcom */
724 case 1:
Stevenb59f2272017-10-12 17:10:33 -0700725 rv = libc_vfcntl (vsock->fd, __cmd, __ap);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700726 break;
727 /* cmd handled by vcom socket resource */
728 case 2:
729 rv = libc_vfcntl (vsock->fd, __cmd, __ap);
730 break;
731 /* cmd handled by vppcom */
732 case 3:
733 rv = vppcom_session_fcntl_va (vsock->sid, __cmd, __ap);
734 break;
735
736 default:
737 rv = -EINVAL;
738 break;
739 }
740
741 return rv;
742}
743
Stevenb59f2272017-10-12 17:10:33 -0700744/*
745 * RETURN: 0 - invalid cmd
746 * 1 - cmd not handled by vcom and vppcom
747 * 2 - cmd handled by vcom socket resource
748 * 3 - cmd handled by vppcom
749 */
750static int
751vcom_socket_check_ioctl_cmd (unsigned long int __cmd)
752{
753 int rc;
754
755 switch (__cmd)
756 {
757 /* cmd handled by vppcom */
758 case FIONREAD:
759 rc = 3;
760 break;
761
762 /* cmd not handled by vcom and vppcom */
763 default:
764 rc = 1;
765 break;
766 }
767 return rc;
768}
769
770static int
771vppcom_session_ioctl_va (int __sid, int __cmd, va_list __ap)
772{
773 int rv;
774
775 if (__cmd == FIONREAD)
776 rv = vppcom_session_attr (__sid, VPPCOM_ATTR_GET_NREAD, 0, 0);
777 else
778 rv = -EOPNOTSUPP;
779 return rv;
780}
781
782int
783vcom_socket_ioctl_va (int __fd, unsigned long int __cmd, va_list __ap)
784{
785 int rv = -EBADF;
786 vcom_socket_main_t *vsm = &vcom_socket_main;
787 uword *p;
788 vcom_socket_t *vsock;
789
790 p = hash_get (vsm->sockidx_by_fd, __fd);
791 if (!p)
792 return -EBADF;
793
794 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
795 if (!vsock)
796 return -ENOTSOCK;
797
798 if (vsock->type != SOCKET_TYPE_VPPCOM_BOUND)
799 return -EINVAL;
800
801 switch (vcom_socket_check_ioctl_cmd (__cmd))
802 {
803 /* Not supported cmd */
804 case 0:
805 rv = -EOPNOTSUPP;
806 break;
807
808 /* cmd not handled by vcom and vppcom */
809 case 1:
810 rv = libc_vioctl (vsock->fd, __cmd, __ap);
811 break;
812
813 /* cmd handled by vcom socket resource */
814 case 2:
815 rv = libc_vioctl (vsock->fd, __cmd, __ap);
816 break;
817
818 /* cmd handled by vppcom */
819 case 3:
820 rv = vppcom_session_ioctl_va (vsock->sid, __cmd, __ap);
821 break;
822
823 default:
824 rv = -EINVAL;
825 break;
826 }
827
828 return rv;
829}
830
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700831static inline int
832vcom_socket_fds_2_sid_fds (
833 /* dest */
834 int *vcom_nsid_fds,
835 fd_set * __restrict vcom_rd_sid_fds,
836 fd_set * __restrict vcom_wr_sid_fds,
837 fd_set * __restrict vcom_ex_sid_fds,
838 /* src */
839 int vcom_nfds,
840 fd_set * __restrict vcom_readfds,
841 fd_set * __restrict vcom_writefds,
842 fd_set * __restrict vcom_exceptfds)
843{
844 int rv = 0;
845 int fd;
846 int sid;
847 /* invalid max_sid is -1 */
848 int max_sid = -1;
849 int nsid = 0;
850
851 /*
852 * set sid in sid sets corresponding to fd's in fd sets
853 * compute nsid and vcom_nsid_fds from sid sets
854 */
855
856 for (fd = 0; fd < vcom_nfds; fd++)
857 {
858 /*
859 * F fd set, src
860 * S sid set, dest
861 */
862#define _(S,F) \
863 if ((F) && (S) && FD_ISSET (fd, (F))) \
864 { \
865 sid = vcom_socket_get_sid (fd); \
866 if (sid != INVALID_SESSION_ID) \
867 { \
868 FD_SET (sid, (S)); \
869 if (sid > max_sid) \
870 { \
871 max_sid = sid; \
872 } \
873 ++nsid; \
874 } \
875 else \
876 { \
877 rv = -EBADFD; \
878 goto done; \
879 } \
880 }
881
882
883 _(vcom_rd_sid_fds, vcom_readfds);
884 _(vcom_wr_sid_fds, vcom_writefds);
885 _(vcom_ex_sid_fds, vcom_exceptfds);
886#undef _
887 }
888
889 *vcom_nsid_fds = max_sid != -1 ? max_sid + 1 : 0;
890 rv = nsid;
891
892done:
893 return rv;
894}
895
896/*
897 * PRE: 00. sid sets were derived from fd sets
898 * 01. sid sets were updated with sids that actually changed
899 * status
900 * 02. fd sets still has watched fds
901 *
902 * This function will modify in place fd sets to indicate which fd's
903 * actually changed status(inferred from sid sets)
904 */
905static inline int
906vcom_socket_sid_fds_2_fds (
907 /* dest */
908 int *new_vcom_nfds,
909 int vcom_nfds,
910 fd_set * __restrict vcom_readfds,
911 fd_set * __restrict vcom_writefds,
912 fd_set * __restrict vcom_exceptfds,
913 /* src */
914 int vcom_nsid_fds,
915 fd_set * __restrict vcom_rd_sid_fds,
916 fd_set * __restrict vcom_wr_sid_fds,
917 fd_set * __restrict vcom_ex_sid_fds)
918{
919 int rv = 0;
920 int fd;
921 int sid;
922 /* invalid max_fd is -1 */
923 int max_fd = -1;
924 int nfd = 0;
925
926
927 /*
928 * modify in place fd sets to indicate which fd's
929 * actually changed status(inferred from sid sets)
930 */
931 for (fd = 0; fd < vcom_nfds; fd++)
932 {
933 /*
934 * F fd set, dest
935 * S sid set, src
936 */
937#define _(S,F) \
938 if ((F) && (S) && FD_ISSET (fd, (F))) \
939 { \
940 sid = vcom_socket_get_sid (fd); \
941 if (sid != INVALID_SESSION_ID) \
942 { \
943 if (!FD_ISSET (sid, (S))) \
944 { \
945 FD_CLR(fd, (F)); \
946 } \
947 } \
948 else \
949 { \
950 rv = -EBADFD; \
951 goto done; \
952 } \
953 }
954
955
956 _(vcom_rd_sid_fds, vcom_readfds);
957 _(vcom_wr_sid_fds, vcom_writefds);
958 _(vcom_ex_sid_fds, vcom_exceptfds);
959#undef _
960 }
961
962 /*
963 * compute nfd and new_vcom_nfds from fd sets
964 */
965 for (fd = 0; fd < vcom_nfds; fd++)
966 {
967
968#define _(F) \
969 if ((F) && FD_ISSET (fd, (F))) \
970 { \
971 if (fd > max_fd) \
972 { \
973 max_fd = fd; \
974 } \
975 ++nfd; \
976 }
977
978
979 _(vcom_readfds);
980 _(vcom_writefds);
981 _(vcom_exceptfds);
982#undef _
983
984 }
985
986 *new_vcom_nfds = max_fd != -1 ? max_fd + 1 : 0;
987 rv = nfd;
988
989done:
990 return rv;
991}
992
993/*
994 * PRE:
995 * vom_socket_select is always called with
996 * timeout->tv_sec and timeout->tv_usec set to zero.
997 * hence vppcom_select return immediately.
998 */
999/*
1000 * TBD: do{body;} while(timeout conditional); timeout loop
1001 */
1002int
1003vcom_socket_select (int vcom_nfds, fd_set * __restrict vcom_readfds,
1004 fd_set * __restrict vcom_writefds,
1005 fd_set * __restrict vcom_exceptfds,
1006 struct timeval *__restrict timeout)
1007{
1008 int rv = -EBADF;
1009 pid_t pid = getpid ();
1010
1011 int new_vcom_nfds = 0;
1012 int new_vcom_nfd = 0;
1013
1014 /* vcom sid fds */
1015 fd_set vcom_rd_sid_fds;
1016 fd_set vcom_wr_sid_fds;
1017 fd_set vcom_ex_sid_fds;
1018 unsigned long vcom_nsid_fds = 0;
1019 int vcom_nsid = 0;
1020
1021 /* in seconds eg. 3.123456789 seconds */
1022 double time_to_wait = (double) 0;
1023
1024 /* validate inputs */
1025 if (vcom_nfds < 0)
1026 {
1027 return -EINVAL;
1028 }
1029
1030 /* convert timeval timeout to double time_to_wait */
1031 if (timeout)
1032 {
1033 if (timeout->tv_sec == 0 && timeout->tv_usec == 0)
1034 {
1035 /* polling: vppcom_select returns immediately */
1036 time_to_wait = (double) 0;
1037 }
1038 else
1039 {
1040 /*TBD: use timeval api */
1041 time_to_wait = (double) timeout->tv_sec +
1042 (double) timeout->tv_usec / (double) 1000000 +
1043 (double) (timeout->tv_usec % 1000000) / (double) 1000000;
1044 }
1045 }
1046 else
1047 {
1048 /*
1049 * no timeout: vppcom_select can block indefinitely
1050 * waiting for a file descriptor to become ready
1051 * */
1052 /* set to a phantom value */
1053 time_to_wait = ~0;
1054 }
1055
1056 /* zero the sid_sets */
1057 /*
1058 * F fd set
1059 * S sid set
1060 */
1061#define _(S,F) \
1062 if ((F)) \
1063 { \
1064 FD_ZERO ((S)); \
1065 }
1066
1067
1068 _(&vcom_rd_sid_fds, vcom_readfds);
1069 _(&vcom_wr_sid_fds, vcom_writefds);
1070 _(&vcom_ex_sid_fds, vcom_exceptfds);
1071#undef _
1072
1073 /* populate read, write and except sid_sets */
1074 vcom_nsid = vcom_socket_fds_2_sid_fds (
1075 /* dest */
1076 vcom_readfds || vcom_writefds
1077 || vcom_exceptfds ? (int *)
1078 &vcom_nsid_fds : NULL,
1079 vcom_readfds ? &vcom_rd_sid_fds :
1080 NULL,
1081 vcom_writefds ? &vcom_wr_sid_fds :
1082 NULL,
1083 vcom_exceptfds ? &vcom_ex_sid_fds :
1084 NULL,
1085 /* src */
1086 vcom_nfds,
1087 vcom_readfds,
1088 vcom_writefds, vcom_exceptfds);
1089 if (vcom_nsid < 0)
1090 {
1091 return vcom_nsid;
1092 }
1093 if (vcom_nsid_fds < 0)
1094 {
1095 return -EINVAL;
1096 }
1097
1098 rv = vppcom_select (vcom_nsid_fds,
1099 vcom_readfds ? (unsigned long *) &vcom_rd_sid_fds :
1100 NULL,
1101 vcom_writefds ? (unsigned long *) &vcom_wr_sid_fds :
1102 NULL,
1103 vcom_exceptfds ? (unsigned long *) &vcom_ex_sid_fds :
1104 NULL, time_to_wait);
1105 if (VCOM_DEBUG > 0)
1106 fprintf (stderr, "[%d] vppcom_select: "
1107 "'%04d'='%04d'\n", pid, rv, (int) vcom_nsid_fds);
1108
1109 /* check if any file descriptors changed status */
1110 if (rv > 0)
1111 {
1112 /*
1113 * on exit, sets are modified in place to indicate which
1114 * file descriptors actually changed status
1115 * */
1116
1117 /*
1118 * comply with pre-condition
1119 * do not clear vcom fd sets befor calling
1120 * vcom_socket_sid_fds_2_fds
1121 */
1122 new_vcom_nfd = vcom_socket_sid_fds_2_fds (
1123 /* dest */
1124 &new_vcom_nfds,
1125 vcom_nfds,
1126 vcom_readfds,
1127 vcom_writefds,
1128 vcom_exceptfds,
1129 /* src */
1130 vcom_nsid_fds,
1131 vcom_readfds ?
1132 &vcom_rd_sid_fds : NULL,
1133 vcom_writefds ?
1134 &vcom_wr_sid_fds : NULL,
1135 vcom_exceptfds ?
1136 &vcom_ex_sid_fds : NULL);
1137 if (new_vcom_nfd < 0)
1138 {
1139 return new_vcom_nfd;
1140 }
1141 if (new_vcom_nfds < 0)
1142 {
1143 return -EINVAL;
1144 }
1145 rv = new_vcom_nfd;
1146 }
1147 return rv;
1148}
1149
1150
1151int
1152vcom_socket_socket (int __domain, int __type, int __protocol)
1153{
1154 int rv = -1;
1155 vcom_socket_main_t *vsm = &vcom_socket_main;
1156 vcom_socket_t *vsock;
1157
1158 i32 fd;
1159 i32 sid;
1160 i32 sockidx;
1161 u8 is_nonblocking = __type & SOCK_NONBLOCK ? 1 : 0;
1162 int type = __type & ~(SOCK_NONBLOCK | SOCK_CLOEXEC);
1163
1164 fd = vcom_socket_open_socket (__domain, __type, __protocol);
1165 if (fd < 0)
1166 {
1167 rv = fd;
1168 goto out;
1169 }
1170
1171 sid = vppcom_session_create (VPPCOM_VRF_DEFAULT,
1172 (type == SOCK_DGRAM) ?
1173 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP,
1174 is_nonblocking);
1175 if (sid < 0)
1176 {
1177 rv = sid;
1178 goto out_close_socket;
1179 }
1180
1181 pool_get (vsm->vsockets, vsock);
1182 vsocket_init (vsock);
1183
1184 sockidx = vsock - vsm->vsockets;
1185 hash_set (vsm->sockidx_by_fd, fd, sockidx);
1186
1187 vsocket_set (vsock, fd, sid, SOCKET_TYPE_VPPCOM_BOUND);
1188 return fd;
1189
1190out_close_socket:
1191 vcom_socket_close_socket (fd);
1192out:
1193 return rv;
1194}
1195
1196int
1197vcom_socket_socketpair (int __domain, int __type, int __protocol,
1198 int __fds[2])
1199{
1200/* TBD: */
1201 return 0;
1202}
1203
1204int
1205vcom_socket_bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
1206{
1207 int rv = -1;
1208 vcom_socket_main_t *vsm = &vcom_socket_main;
1209 uword *p;
1210 vcom_socket_t *vsock;
1211
1212 vppcom_endpt_t ep;
1213
1214 p = hash_get (vsm->sockidx_by_fd, __fd);
1215 if (!p)
1216 return -EBADF;
1217
1218 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
1219 if (!vsock)
1220 return -ENOTSOCK;
1221
1222 if (vsock->type != SOCKET_TYPE_VPPCOM_BOUND)
1223 return -EINVAL;
1224
1225 if (!__addr)
1226 {
1227 return -EINVAL;
1228 }
1229
1230 ep.vrf = VPPCOM_VRF_DEFAULT;
1231 switch (__addr->sa_family)
1232 {
1233 case AF_INET:
1234 if (__len != sizeof (struct sockaddr_in))
1235 {
1236 return -EINVAL;
1237 }
1238 ep.is_ip4 = VPPCOM_IS_IP4;
1239 ep.ip = (u8 *) & ((const struct sockaddr_in *) __addr)->sin_addr;
1240 ep.port = (u16) ((const struct sockaddr_in *) __addr)->sin_port;
1241 break;
1242
1243 case AF_INET6:
1244 if (__len != sizeof (struct sockaddr_in6))
1245 {
1246 return -EINVAL;
1247 }
1248 ep.is_ip4 = VPPCOM_IS_IP6;
1249 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) __addr)->sin6_addr;
1250 ep.port = (u16) ((const struct sockaddr_in6 *) __addr)->sin6_port;
1251 break;
1252
1253 default:
1254 return -1;
1255 break;
1256 }
1257
1258 rv = vppcom_session_bind (vsock->sid, &ep);
1259 /* TBD: remove libc_bind code snippet
1260 * once vppcom implements vppcom_session_getsockname */
1261 if (rv == 0)
1262 {
1263 rv = libc_bind (__fd, __addr, __len);
1264 if (rv != 0)
1265 {
1266 rv = -errno;
1267 }
1268 }
1269 return rv;
1270}
1271
1272int
1273vppcom_session_getsockname (int sid, vppcom_endpt_t * ep)
1274{
1275 /* TBD: move it to vppcom */
1276 return 0;
1277}
1278
1279int
1280vcom_socket_getsockname (int __fd, __SOCKADDR_ARG __addr,
1281 socklen_t * __restrict __len)
1282{
1283 int rv = -1;
1284 vcom_socket_main_t *vsm = &vcom_socket_main;
1285 uword *p;
1286 vcom_socket_t *vsock;
1287
1288
1289 p = hash_get (vsm->sockidx_by_fd, __fd);
1290 if (!p)
1291 return -EBADF;
1292
1293 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
1294 if (!vsock)
1295 return -ENOTSOCK;
1296
1297 if (vsock->type != SOCKET_TYPE_VPPCOM_BOUND)
1298 return -EINVAL;
1299
1300 if (!__addr || !__len)
1301 return -EFAULT;
1302
1303 if (*__len < 0)
1304 {
1305 return -EINVAL;
1306 }
1307
1308 /* TBD: remove libc_getsockname code snippet
1309 * once vppcom implements vppcom_session_getsockname */
1310 rv = libc_getsockname (__fd, __addr, __len);
1311 if (rv != 0)
1312 {
1313 rv = -errno;
1314 return rv;
1315 }
1316
1317 /* TBD: use the below code snippet when vppcom
1318 * implements vppcom_session_getsockname */
1319#if 0
1320 vppcom_endpt_t ep;
1321 ep.ip = (u8 *) & ((const struct sockaddr_in *) __addr)->sin_addr;
1322 rv = vppcom_session_getsockname (vsock->sid, &ep);
1323 if (rv == 0)
1324 {
1325 if (ep.vrf == VPPCOM_VRF_DEFAULT)
1326 {
1327 __addr->sa_family = ep.is_ip4 == VPPCOM_IS_IP4 ? AF_INET : AF_INET6;
1328 switch (__addr->sa_family)
1329 {
1330 case AF_INET:
1331 ((struct sockaddr_in *) __addr)->sin_port = ep.port;
1332 *__len = sizeof (struct sockaddr_in);
1333 break;
1334
1335 case AF_INET6:
1336 ((struct sockaddr_in6 *) __addr)->sin6_port = ep.port;
1337 *__len = sizeof (struct sockaddr_in6);
1338 break;
1339
1340 default:
1341 break;
1342 }
1343 }
1344 }
1345#endif
1346
1347 return rv;
1348}
1349
1350int
1351vcom_socket_connect (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
1352{
1353 int rv = -1;
1354 vcom_socket_main_t *vsm = &vcom_socket_main;
1355 uword *p;
1356 vcom_socket_t *vsock;
1357
1358 vppcom_endpt_t ep;
1359
1360 p = hash_get (vsm->sockidx_by_fd, __fd);
1361 if (p)
1362 {
1363 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
1364
1365 ep.vrf = VPPCOM_VRF_DEFAULT;
1366 switch (__addr->sa_family)
1367 {
1368 case AF_INET:
1369 ep.is_ip4 = VPPCOM_IS_IP4;
1370 ep.ip =
1371 (uint8_t *) & ((const struct sockaddr_in *) __addr)->sin_addr;
1372 ep.port =
1373 (uint16_t) ((const struct sockaddr_in *) __addr)->sin_port;
1374 break;
1375
1376 case AF_INET6:
1377 ep.is_ip4 = VPPCOM_IS_IP6;
1378 ep.ip =
1379 (uint8_t *) & ((const struct sockaddr_in6 *) __addr)->sin6_addr;
1380 ep.port =
1381 (uint16_t) ((const struct sockaddr_in6 *) __addr)->sin6_port;
1382 break;
1383
1384 default:
1385 return -1;
1386 break;
1387 }
1388
1389 rv = vppcom_session_connect (vsock->sid, &ep);
1390 }
1391 return rv;
1392}
1393
1394int
1395vppcom_session_getpeername (int sid, vppcom_endpt_t * ep)
1396{
1397 /* TBD: move it to vppcom */
1398 return 0;
1399}
1400
1401int
1402vcom_socket_getpeername (int __fd, __SOCKADDR_ARG __addr,
1403 socklen_t * __restrict __len)
1404{
1405 int rv = -1;
1406 vcom_socket_main_t *vsm = &vcom_socket_main;
1407 uword *p;
1408 vcom_socket_t *vsock;
1409
1410
1411 p = hash_get (vsm->sockidx_by_fd, __fd);
1412 if (!p)
1413 return -EBADF;
1414
1415 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
1416 if (!vsock)
1417 return -ENOTSOCK;
1418
1419 if (vsock->type != SOCKET_TYPE_VPPCOM_BOUND)
1420 return -EINVAL;
1421
1422 if (!__addr || !__len)
1423 return -EFAULT;
1424
1425 if (*__len < 0)
1426 {
1427 return -EINVAL;
1428 }
1429
1430 /* DAW: hack to allow iperf3 to be happy w/ getpeername output */
1431 {
1432 uint8_t *a;
1433 ((struct sockaddr_in *) __addr)->sin_family = AF_INET;
1434 ((struct sockaddr_in *) __addr)->sin_port = 0x1000;
1435 a = (uint8_t *) & ((struct sockaddr_in *) __addr)->sin_addr;
1436 a[0] = 0x7f;
1437 a[1] = 0x00;
1438 a[2] = 0x00;
1439 a[3] = 0x01;
1440 *__len = sizeof (struct sockaddr_in);
1441 return 0;
1442 }
1443
1444 /* TBD: remove libc_getpeername code snippet
1445 * once vppcom implements vppcom_session_getpeername */
1446 rv = libc_getpeername (__fd, __addr, __len);
1447 if (rv != 0)
1448 {
1449 rv = -errno;
1450 return rv;
1451 }
1452
1453 /* TBD: use the below code snippet when vppcom
1454 * implements vppcom_session_getpeername */
1455#if 0
1456 vppcom_endpt_t ep;
1457 ep.ip = (u8 *) & ((const struct sockaddr_in *) __addr)->sin_addr;
1458 rv = vppcom_session_getpeername (vsock->sid, &ep);
1459 if (rv == 0)
1460 {
1461 if (ep.vrf == VPPCOM_VRF_DEFAULT)
1462 {
1463 __addr->sa_family = ep.is_ip4 == VPPCOM_IS_IP4 ? AF_INET : AF_INET6;
1464 switch (__addr->sa_family)
1465 {
1466 case AF_INET:
1467 ((struct sockaddr_in *) __addr)->sin_port = ep.port;
1468 *__len = sizeof (struct sockaddr_in);
1469 break;
1470
1471 case AF_INET6:
1472 ((struct sockaddr_in6 *) __addr)->sin6_port = ep.port;
1473 *__len = sizeof (struct sockaddr_in6);
1474 break;
1475
1476 default:
1477 break;
1478 }
1479 }
1480 }
1481#endif
1482
1483 return rv;
1484}
1485
1486ssize_t
1487vcom_socket_send (int __fd, const void *__buf, size_t __n, int __flags)
1488{
1489 return vcom_socket_sendto (__fd, __buf, __n, __flags, NULL, 0);
1490}
1491
1492ssize_t
1493vcom_socket_recv (int __fd, void *__buf, size_t __n, int __flags)
1494{
1495 int rv = -1;
1496 rv = vcom_socket_recvfrom (__fd, __buf, __n, __flags, NULL, 0);
1497 return rv;
1498}
1499
1500/*
1501 * RETURN 1 if __fd is (SOCK_STREAM, SOCK_SEQPACKET),
1502 * 0 otherwise
1503 * */
1504int
1505vcom_socket_is_connection_mode_socket (int __fd)
1506{
1507 int rv = -1;
1508 /* TBD define new vppcom api */
1509 vcom_socket_main_t *vsm = &vcom_socket_main;
1510 uword *p;
1511 vcom_socket_t *vsock;
1512
1513 int type;
1514 socklen_t optlen;
1515
1516 p = hash_get (vsm->sockidx_by_fd, __fd);
1517
1518 if (p)
1519 {
1520 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
1521 if (vsock && vsock->type == SOCKET_TYPE_VPPCOM_BOUND)
1522 {
1523 optlen = sizeof (type);
1524 rv = libc_getsockopt (__fd, SOL_SOCKET, SO_TYPE, &type, &optlen);
1525 if (rv != 0)
1526 {
1527 return 0;
1528 }
1529 /* get socket type */
1530 switch (type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1531 {
1532 case SOCK_STREAM:
1533 case SOCK_SEQPACKET:
1534 return 1;
1535 break;
1536
1537 default:
1538 return 0;
1539 break;
1540 }
1541 }
1542 }
1543 return 0;
1544}
1545
1546ssize_t
1547vvppcom_session_sendto (int __sid, const void *__buf, size_t __n,
1548 int __flags, __CONST_SOCKADDR_ARG __addr,
1549 socklen_t __addr_len)
1550{
1551 int rv = -1;
1552 /* TBD add new vpp api */
1553 /* TBD add flags parameter */
1554 rv = vppcom_session_write (__sid, (void *) __buf, (int) __n);
1555 return rv;
1556}
1557
1558ssize_t
1559vcom_socket_sendto (int __fd, const void *__buf, size_t __n,
1560 int __flags, __CONST_SOCKADDR_ARG __addr,
1561 socklen_t __addr_len)
1562{
1563 int rv = -1;
1564 vcom_socket_main_t *vsm = &vcom_socket_main;
1565 uword *p;
1566 vcom_socket_t *vsock;
1567
1568 p = hash_get (vsm->sockidx_by_fd, __fd);
1569 if (!p)
1570 return -EBADF;
1571
1572 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
1573 if (!vsock)
1574 return -ENOTSOCK;
1575
1576 if (vsock->type != SOCKET_TYPE_VPPCOM_BOUND)
1577 return -EINVAL;
1578
1579 if (!__buf || __n < 0)
1580 {
1581 return -EINVAL;
1582 }
1583
1584 if (vcom_socket_is_connection_mode_socket (__fd))
1585 {
1586 /* ignore __addr and _addr_len */
1587 /* and EISCONN may be returned when they are not NULL and 0 */
1588 if ((__addr != NULL) || (__addr_len != 0))
1589 {
1590 return -EISCONN;
1591 }
1592 }
1593 else
1594 {
1595 if (!__addr || __addr_len < 0)
1596 {
1597 return -EDESTADDRREQ;
1598 }
1599 /* not a vppcom supported address family */
1600 if ((__addr->sa_family != AF_INET) || (__addr->sa_family != AF_INET6))
1601 {
1602 return -EINVAL;
1603 }
1604 }
1605
1606 rv = vvppcom_session_sendto (vsock->sid, (void *) __buf, (int) __n,
1607 __flags, __addr, __addr_len);
1608 return rv;
1609}
1610
1611/* TBD: move it to vppcom */
1612static ssize_t
1613vppcom_session_recvfrom (int __sid, void *__restrict __buf, size_t __n,
1614 int __flags, __SOCKADDR_ARG __addr,
1615 socklen_t * __restrict __addr_len)
1616{
1617 int rv = -1;
1618
1619 /* TBD add flags parameter */
1620 rv = vppcom_session_read (__sid, __buf, __n);
1621 return rv;
1622}
1623
1624ssize_t
1625vcom_socket_recvfrom (int __fd, void *__restrict __buf, size_t __n,
1626 int __flags, __SOCKADDR_ARG __addr,
1627 socklen_t * __restrict __addr_len)
1628{
1629 int rv = -1;
1630 vcom_socket_main_t *vsm = &vcom_socket_main;
1631 uword *p;
1632 vcom_socket_t *vsock;
1633
1634 p = hash_get (vsm->sockidx_by_fd, __fd);
1635 if (!p)
1636 return -EBADF;
1637
1638 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
1639 if (!vsock)
1640 return -ENOTSOCK;
1641
1642 if (vsock->type != SOCKET_TYPE_VPPCOM_BOUND)
1643 return -EINVAL;
1644
1645 if (!__buf || __n < 0)
1646 {
1647 return -EINVAL;
1648 }
1649
1650 if (__addr || __addr_len < 0)
1651 {
1652 return -EINVAL;
1653 }
1654
1655 rv = vppcom_session_recvfrom (vsock->sid, __buf, __n,
1656 __flags, __addr, __addr_len);
1657 return rv;
1658}
1659
1660/* TBD: move it to vppcom */
1661static ssize_t
1662vppcom_sendmsg (int __sid, const struct msghdr *__message, int __flags)
1663{
1664 int rv = -1;
1665 /* rv = vppcom_session_write (__sid, (void *) __message->__buf,
1666 (int)__n); */
1667 return rv;
1668}
1669
1670ssize_t
1671vcom_socket_sendmsg (int __fd, const struct msghdr * __message, int __flags)
1672{
1673 int rv = -1;
1674 vcom_socket_main_t *vsm = &vcom_socket_main;
1675 uword *p;
1676 vcom_socket_t *vsock;
1677
1678 p = hash_get (vsm->sockidx_by_fd, __fd);
1679 if (!p)
1680 return -EBADF;
1681
1682 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
1683 if (!vsock)
1684 return -ENOTSOCK;
1685
1686 if (vcom_socket_is_connection_mode_socket (__fd))
1687 {
1688 /* ignore __addr and _addr_len */
1689 /* and EISCONN may be returned when they are not NULL and 0 */
1690 if ((__message->msg_name != NULL) || (__message->msg_namelen != 0))
1691 {
1692 return -EISCONN;
1693 }
1694 }
1695 else
1696 {
1697 /* TBD: validate __message->msg_name and __message->msg_namelen
1698 * and return -EINVAL on validation error
1699 * */
1700 ;
1701 }
1702
1703 rv = vppcom_sendmsg (vsock->sid, __message, __flags);
1704
1705 return rv;
1706}
1707
1708#ifdef __USE_GNU
1709int
1710vcom_socket_sendmmsg (int __fd, struct mmsghdr *__vmessages,
1711 unsigned int __vlen, int __flags)
1712{
1713
1714 /* TBD: define a new vppcom api */
1715 return 0;
1716}
1717#endif
1718
1719/* TBD: move it to vppcom */
1720static ssize_t
1721vppcom_recvmsg (int __sid, struct msghdr *__message, int __flags)
1722{
1723 int rv = -1;
1724 /* rv = vppcom_session_read (__sid, (void *) __message->__buf,
1725 (int)__n); */
1726 rv = -EOPNOTSUPP;
1727 return rv;
1728}
1729
1730ssize_t
1731vcom_socket_recvmsg (int __fd, struct msghdr * __message, int __flags)
1732{
1733 int rv = -1;
1734 vcom_socket_main_t *vsm = &vcom_socket_main;
1735 uword *p;
1736 vcom_socket_t *vsock;
1737
1738 p = hash_get (vsm->sockidx_by_fd, __fd);
1739 if (!p)
1740 return -EBADF;
1741
1742 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
1743 if (!vsock)
1744 return -ENOTSOCK;
1745
1746 if (vsock->type != SOCKET_TYPE_VPPCOM_BOUND)
1747 return -EINVAL;
1748
1749 if (!__message)
1750 {
1751 return -EINVAL;
1752 }
1753
1754 /* validate __flags */
1755
1756 rv = vppcom_recvmsg (vsock->sid, __message, __flags);
1757 return rv;
1758}
1759
1760#ifdef __USE_GNU
1761int
1762vcom_socket_recvmmsg (int __fd, struct mmsghdr *__vmessages,
1763 unsigned int __vlen, int __flags,
1764 struct timespec *__tmo)
1765{
1766 /* TBD: define a new vppcom api */
1767 return 0;
1768}
1769#endif
1770
1771/* TBD: move it to vppcom */
1772static int
1773vppcom_getsockopt (int __sid, int __level, int __optname,
1774 void *__restrict __optval, socklen_t * __restrict __optlen)
1775{
1776 /* 1. for socket level options that are NOT socket attributes
1777 * and that has corresponding vpp options get from vppcom */
1778#if 0
1779 return 0;
1780#endif
1781
1782 /* 2. unhandled options */
1783 return -ENOPROTOOPT;
1784}
1785
1786int
1787vcom_socket_getsockopt (int __fd, int __level, int __optname,
1788 void *__restrict __optval,
1789 socklen_t * __restrict __optlen)
1790{
1791 int rv = -1;
1792 vcom_socket_main_t *vsm = &vcom_socket_main;
1793 uword *p;
1794 vcom_socket_t *vsock;
1795
1796 p = hash_get (vsm->sockidx_by_fd, __fd);
1797 if (!p)
1798 return -EBADF;
1799
1800 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
1801 if (!vsock)
1802 return -ENOTSOCK;
1803
1804 if (vsock->type != SOCKET_TYPE_VPPCOM_BOUND)
1805 return -EINVAL;
1806
1807 if (!__optval && !__optlen)
1808 return -EFAULT;
1809
1810 if (*__optlen < 0)
1811 {
1812 return -EINVAL;
1813 }
1814
1815 switch (__level)
1816 {
1817 /* handle options at socket level */
1818 case SOL_SOCKET:
1819 switch (__optname)
1820 {
1821/*
1822 * 1. for socket level options that are socket attributes,
1823 * get from libc_getsockopt.
1824 * 2. for socket level options that are NOT socket
1825 * attributes and that has corresponding vpp options
1826 * get from vppcom.
1827 * 3. for socket level options unimplemented
1828 * return -ENOPROTOOPT */
1829 case SO_DEBUG:
1830 case SO_DONTROUTE:
1831 case SO_BROADCAST:
1832 case SO_SNDBUF:
1833 case SO_RCVBUF:
1834 case SO_REUSEADDR:
1835 case SO_REUSEPORT:
1836 case SO_KEEPALIVE:
1837 case SO_TYPE:
1838 case SO_PROTOCOL:
1839 case SO_DOMAIN:
1840 case SO_ERROR:
1841 case SO_OOBINLINE:
1842 case SO_NO_CHECK:
1843 case SO_PRIORITY:
1844 case SO_LINGER:
1845 case SO_BSDCOMPAT:
1846 case SO_TIMESTAMP:
1847 case SO_TIMESTAMPNS:
1848 case SO_TIMESTAMPING:
1849 case SO_RCVTIMEO:
1850 case SO_SNDTIMEO:
1851 case SO_RCVLOWAT:
1852 case SO_SNDLOWAT:
1853 case SO_PASSCRED:
1854 case SO_PEERCRED:
1855 case SO_PEERNAME:
1856 case SO_ACCEPTCONN:
1857 case SO_PASSSEC:
1858 case SO_PEERSEC:
1859 case SO_MARK:
1860 case SO_RXQ_OVFL:
1861 case SO_WIFI_STATUS:
1862 case SO_PEEK_OFF:
1863 case SO_NOFCS:
1864 case SO_BINDTODEVICE:
1865 case SO_GET_FILTER:
1866 case SO_LOCK_FILTER:
1867 case SO_BPF_EXTENSIONS:
1868 case SO_SELECT_ERR_QUEUE:
1869#ifdef CONFIG_NET_RX_BUSY_POLL
1870 case SO_BUSY_POLL:
1871#endif
1872 case SO_MAX_PACING_RATE:
1873 case SO_INCOMING_CPU:
1874 rv = libc_getsockopt (__fd, __level, __optname, __optval, __optlen);
1875 if (rv != 0)
1876 {
1877 rv = -errno;
1878 return rv;
1879 }
1880 break;
1881
1882 default:
1883 /* We implement the SO_SNDLOWAT etc to not be settable
1884 * (1003.1g 7).
1885 */
1886 return -ENOPROTOOPT;
1887 }
1888
1889 break;
1890
1891 default:
1892 /* 1. handle options that are NOT socket level options,
1893 * but have corresponding vpp otions. */
1894 rv = vppcom_getsockopt (vsock->sid, __level, __optname,
1895 __optval, __optlen);
1896
1897 return rv;
1898#if 0
1899 /* 2. unhandled options */
1900 return -ENOPROTOOPT;
1901#endif
1902 }
1903
1904 return rv;
1905}
1906
1907/* TBD: move it to vppcom */
1908int
Stevenb59f2272017-10-12 17:10:33 -07001909vppcom_session_setsockopt (int __sid, int __level, int __optname,
1910 const void *__optval, socklen_t __optlen)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001911{
Stevenb59f2272017-10-12 17:10:33 -07001912 int rv = -EOPNOTSUPP;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001913
Stevenb59f2272017-10-12 17:10:33 -07001914 switch (__level)
1915 {
1916 case SOL_IPV6:
1917 switch (__optname)
1918 {
1919 case IPV6_V6ONLY:
1920 rv = vppcom_session_attr (__sid, VPPCOM_ATTR_SET_V6ONLY, 0, 0);
1921 return rv;
1922 default:
1923 return rv;
1924 }
1925 break;
1926 case SOL_SOCKET:
1927 switch (__optname)
1928 {
1929 case SO_REUSEADDR:
1930 rv = vppcom_session_attr (__sid, VPPCOM_ATTR_SET_REUSEADDR, 0, 0);
1931 return rv;
1932 case SO_BROADCAST:
1933 rv = vppcom_session_attr (__sid, VPPCOM_ATTR_SET_BROADCAST, 0, 0);
1934 return rv;
1935 default:
1936 return rv;
1937 }
1938 break;
1939 default:
1940 return rv;
1941 }
1942
1943 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001944}
1945
1946int
1947vcom_socket_setsockopt (int __fd, int __level, int __optname,
1948 const void *__optval, socklen_t __optlen)
1949{
1950 int rv = -1;
1951 vcom_socket_main_t *vsm = &vcom_socket_main;
1952 uword *p;
1953 vcom_socket_t *vsock;
1954
1955 p = hash_get (vsm->sockidx_by_fd, __fd);
1956 if (!p)
1957 return -EBADF;
1958
1959 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
1960 if (!vsock)
1961 return -ENOTSOCK;
1962
1963 if (vsock->type != SOCKET_TYPE_VPPCOM_BOUND)
1964 return -EINVAL;
1965
1966 /*
1967 * Options without arguments
1968 */
1969
1970 if (__optname == SO_BINDTODEVICE)
1971 {
1972 rv = libc_setsockopt (__fd, __level, __optname, __optval, __optlen);
1973 if (rv != 0)
1974 {
1975 rv = -errno;
1976 }
1977 return rv;
1978 }
1979
1980 if (!__optval)
1981 return -EFAULT;
1982
1983 if ((__optlen < 0) || (__optlen < sizeof (int)))
1984 return -EINVAL;
1985
1986 switch (__level)
1987 {
Stevenb59f2272017-10-12 17:10:33 -07001988 case SOL_IPV6:
1989 switch (__optname)
1990 {
1991 case IPV6_V6ONLY:
1992 rv = vppcom_session_setsockopt (vsock->sid, __level, __optname,
1993 __optval, __optlen);
1994 return rv;
1995 default:
1996 return -EOPNOTSUPP;
1997 }
1998 break;
1999 case SOL_TCP:
2000 switch (__optname)
2001 {
2002 case TCP_NODELAY:
2003 return 0;
2004 default:
2005 return -EOPNOTSUPP;
2006 }
2007 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002008 /* handle options at socket level */
2009 case SOL_SOCKET:
2010 switch (__optname)
2011 {
Stevenb59f2272017-10-12 17:10:33 -07002012 case SO_REUSEADDR:
2013 case SO_BROADCAST:
2014 rv = vppcom_session_setsockopt (vsock->sid, __level, __optname,
2015 __optval, __optlen);
2016 return rv;
2017
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002018 /*
2019 * 1. for socket level options that are socket attributes,
2020 * set it from libc_getsockopt
2021 * 2. for socket level options that are NOT socket
2022 * attributes and that has corresponding vpp options
2023 * set it from vppcom
2024 * 3. for socket level options unimplemented
2025 * return -ENOPROTOOPT */
2026 case SO_DEBUG:
2027 case SO_DONTROUTE:
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002028 case SO_SNDBUF:
2029 case SO_RCVBUF:
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002030 case SO_REUSEPORT:
2031 case SO_KEEPALIVE:
2032 case SO_TYPE:
2033 case SO_PROTOCOL:
2034 case SO_DOMAIN:
2035 case SO_ERROR:
2036 case SO_OOBINLINE:
2037 case SO_NO_CHECK:
2038 case SO_PRIORITY:
2039 case SO_LINGER:
2040 case SO_BSDCOMPAT:
2041 case SO_TIMESTAMP:
2042 case SO_TIMESTAMPNS:
2043 case SO_TIMESTAMPING:
2044 case SO_RCVTIMEO:
2045 case SO_SNDTIMEO:
2046 case SO_RCVLOWAT:
2047 case SO_SNDLOWAT:
2048 case SO_PASSCRED:
2049 case SO_PEERCRED:
2050 case SO_PEERNAME:
2051 case SO_ACCEPTCONN:
2052 case SO_PASSSEC:
2053 case SO_PEERSEC:
2054 case SO_MARK:
2055 case SO_RXQ_OVFL:
2056 case SO_WIFI_STATUS:
2057 case SO_PEEK_OFF:
2058 case SO_NOFCS:
2059 /*
2060 * SO_BINDTODEVICE already handled as
2061 * "Options without arguments" */
2062 /* case SO_BINDTODEVICE: */
2063 case SO_GET_FILTER:
2064 case SO_LOCK_FILTER:
2065 case SO_BPF_EXTENSIONS:
2066 case SO_SELECT_ERR_QUEUE:
2067#ifdef CONFIG_NET_RX_BUSY_POLL
2068 case SO_BUSY_POLL:
2069#endif
2070 case SO_MAX_PACING_RATE:
2071 case SO_INCOMING_CPU:
2072 rv = libc_setsockopt (__fd, __level, __optname, __optval, __optlen);
2073 if (rv != 0)
2074 {
2075 rv = -errno;
2076 return rv;
2077 }
2078 break;
2079
2080 default:
2081 /* We implement the SO_SNDLOWAT etc to not be settable
2082 * (1003.1g 7).
2083 */
2084 return -ENOPROTOOPT;
2085 }
2086
2087 break;
2088
2089 default:
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002090 return -ENOPROTOOPT;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002091 }
2092
2093 return rv;
2094}
2095
2096int
2097vcom_socket_listen (int __fd, int __n)
2098{
2099 int rv = -1;
2100 vcom_socket_main_t *vsm = &vcom_socket_main;
2101 uword *p;
2102 vcom_socket_t *vsock;
2103
2104 p = hash_get (vsm->sockidx_by_fd, __fd);
2105 if (p)
2106 {
2107 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
2108
2109 /* TBD vppcom to accept __n parameter */
2110 rv = vppcom_session_listen (vsock->sid, __n);
2111 }
2112
2113 return rv;
2114}
2115
2116static int
2117vcom_socket_connected_socket (int __fd, int __sid,
2118 int *__domain,
2119 int *__type, int *__protocol, int flags)
2120{
2121 int rv = -1;
2122 vcom_socket_main_t *vsm = &vcom_socket_main;
2123 vcom_socket_t *vsock;
2124
2125 i32 fd;
2126 i32 sockidx;
2127
2128 socklen_t optlen;
2129
2130 optlen = sizeof (*__domain);
2131 rv = libc_getsockopt (__fd, SOL_SOCKET, SO_DOMAIN, __domain, &optlen);
2132 if (rv != 0)
2133 {
2134 rv = -errno;
2135 goto out;
2136 }
2137
2138 optlen = sizeof (*__type);
2139 rv = libc_getsockopt (__fd, SOL_SOCKET, SO_TYPE, __type, &optlen);
2140 if (rv != 0)
2141 {
2142 rv = -errno;
2143 goto out;
2144 }
2145
2146 optlen = sizeof (*__protocol);
2147 rv = libc_getsockopt (__fd, SOL_SOCKET, SO_PROTOCOL, __protocol, &optlen);
2148 if (rv != 0)
2149 {
2150 rv = -errno;
2151 goto out;
2152 }
2153
2154 fd = vcom_socket_open_socket (*__domain, *__type | flags, *__protocol);
2155 if (fd < 0)
2156 {
2157 rv = fd;
2158 goto out;
2159 }
2160
2161 pool_get (vsm->vsockets, vsock);
2162 vsocket_init (vsock);
2163
2164 sockidx = vsock - vsm->vsockets;
2165 hash_set (vsm->sockidx_by_fd, fd, sockidx);
2166
2167 vsocket_set (vsock, fd, __sid, SOCKET_TYPE_VPPCOM_BOUND);
2168 return fd;
2169
2170out:
2171 return rv;
2172}
2173
2174/* If flag is 0, then accept4() is the same as accept().
2175 * SOCK_NONBLOCK and SOCK_CLOEXEC can be bitwise ORed in flags
2176 */
2177static int
2178vcom_socket_accept_flags (int __fd, __SOCKADDR_ARG __addr,
2179 socklen_t * __restrict __addr_len, int flags)
2180{
2181 int rv = -1;
2182 vcom_socket_main_t *vsm = &vcom_socket_main;
2183 uword *p;
2184 vcom_socket_t *vsock;
2185
2186 int fd;
2187 int sid;
2188 int domain;
2189 int type;
2190 int protocol;
2191
2192 uint8_t addr8[sizeof (struct in6_addr)];
2193 vppcom_endpt_t ep;
2194
2195 ep.ip = addr8;
2196
2197 /* validate flags */
2198
2199 /*
2200 * for documentation
2201 * switch (flags)
2202 * {
2203 * case 0:
2204 * case SOCK_NONBLOCK:
2205 * case SOCK_CLOEXEC:
2206 * case SOCK_NONBLOCK | SOCK_CLOEXEC:
2207 * break;
2208 *
2209 * default:
2210 * return -1;
2211 * }
2212 */
2213 /* flags can be 0 or can be bitwise OR
2214 * of any of SOCK_NONBLOCK and SOCK_CLOEXEC */
2215
2216 if (!(!flags || (flags & (SOCK_NONBLOCK | SOCK_CLOEXEC))))
2217 {
2218 /* TBD: return proper error code */
2219 return -1;
2220 }
2221
2222 /* TBD: return proper error code */
2223
2224 if (!vcom_socket_is_connection_mode_socket (__fd))
2225 {
2226 return -EOPNOTSUPP;
2227 }
2228
2229 p = hash_get (vsm->sockidx_by_fd, __fd);
2230 if (p)
2231 {
2232 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
2233
2234
2235 rv = vcom_fcntl (vsock->fd, F_GETFL, 0);
2236 if (rv < 0)
2237 {
2238 return rv;
2239 }
2240
2241 /* is blocking */
2242 if (!(rv & O_NONBLOCK))
2243 {
2244 /* socket is not marked as nonblocking
2245 * and no pending connections are present
2246 * on the queue, accept () blocks the caller
2247 * until a connection is present.
2248 */
2249 rv = vppcom_session_accept (vsock->sid, &ep,
2250 -1.0 /* wait forever */ );
2251 }
2252 else
2253 {
2254 /* The file descriptor refers to a socket and has been
2255 * marked nonblocking(O_NONBLOCK) and the accept would
2256 * block.
2257 * */
2258 /* is non blocking */
2259 rv = vppcom_session_accept (vsock->sid, &ep, 0);
2260 /* If the socket is marked nonblocking and
2261 * no pending connections are present on the
2262 * queue, accept fails with the error
2263 * EAGAIN or EWOULDBLOCK
2264 */
2265 if (rv == VPPCOM_ETIMEDOUT)
2266 {
2267 rv = VPPCOM_EAGAIN;
2268 }
2269 }
2270 if (rv < 0)
2271 {
2272 return rv;
2273 }
2274
2275 sid = rv;
2276
2277 /* create a new connected socket resource and set flags
2278 * on the new file descriptor.
2279 * update vsockets and sockidx_by_fd table
2280 * */
2281 fd = vcom_socket_connected_socket (__fd, sid,
2282 &domain, &type, &protocol, flags);
2283 if (fd < 0)
2284 {
2285 return fd;
2286 }
2287
2288 rv = fd;
2289
2290 /* TBD populate __addr and __addr_len */
2291 /* TBD: The returned address is truncated if the buffer
2292 * provided is too small, in this case, __addr_len will
2293 * return a value greater than was supplied to the call.*/
2294 if (__addr)
2295 {
2296 if (ep.is_cut_thru)
2297 {
2298 /* TBD populate __addr and __addr_len */
2299 switch (domain)
2300 {
2301 case AF_INET:
2302 ((struct sockaddr_in *) __addr)->sin_family = AF_INET;
2303 ((struct sockaddr_in *) __addr)->sin_port = ep.port;
2304 memcpy (&((struct sockaddr_in *) __addr)->sin_addr,
2305 addr8, sizeof (struct in_addr));
2306 /* TBD: populate __addr_len */
2307 if (__addr_len)
2308 {
2309 *__addr_len = sizeof (struct sockaddr_in);
2310 }
2311 break;
2312
2313 case AF_INET6:
2314 ((struct sockaddr_in6 *) __addr)->sin6_family = AF_INET6;
2315 ((struct sockaddr_in6 *) __addr)->sin6_port = ep.port;
2316 memcpy (((struct sockaddr_in6 *) __addr)->sin6_addr.
2317 __in6_u.__u6_addr8, addr8,
2318 sizeof (struct in6_addr));
2319 /* TBD: populate __addr_len */
2320 if (__addr_len)
2321 {
2322 *__addr_len = sizeof (struct sockaddr_in6);
2323 }
2324 break;
2325
2326 default:
2327 return -EAFNOSUPPORT;
2328 }
2329 }
2330 else
2331 {
2332 switch (ep.is_ip4)
2333 {
2334 case VPPCOM_IS_IP4:
2335 ((struct sockaddr_in *) __addr)->sin_family = AF_INET;
2336 ((struct sockaddr_in *) __addr)->sin_port = ep.port;
2337 memcpy (&((struct sockaddr_in *) __addr)->sin_addr,
2338 addr8, sizeof (struct in_addr));
2339 /* TBD: populate __addr_len */
2340 if (__addr_len)
2341 {
2342 *__addr_len = sizeof (struct sockaddr_in);
2343 }
2344 break;
2345
2346 case VPPCOM_IS_IP6:
2347 ((struct sockaddr_in6 *) __addr)->sin6_family = AF_INET6;
2348 ((struct sockaddr_in6 *) __addr)->sin6_port = ep.port;
2349 memcpy (((struct sockaddr_in6 *) __addr)->sin6_addr.
2350 __in6_u.__u6_addr8, addr8,
2351 sizeof (struct in6_addr));
2352 /* TBD: populate __addr_len */
2353 if (__addr_len)
2354 {
2355 *__addr_len = sizeof (struct sockaddr_in6);
2356 }
2357 break;
2358
2359 default:
2360 return -EAFNOSUPPORT;
2361 }
2362 }
2363 }
2364 else
2365 {
2366 /* when __addr is NULL, nothing is filled in,
2367 * in this case, __addr_len is not used,
2368 * and should also be null
2369 * */
2370 if (__addr_len)
2371 {
2372 /* TBD: return proper error code */
2373 return -1;
2374 }
2375 }
2376 }
2377
2378 return rv;
2379}
2380
2381int
2382vcom_socket_accept (int __fd, __SOCKADDR_ARG __addr,
2383 socklen_t * __restrict __addr_len)
2384{
2385 /* set flags to 0 for accept() */
2386 return vcom_socket_accept_flags (__fd, __addr, __addr_len, 0);
2387}
2388
2389#ifdef __USE_GNU
2390int
2391vcom_socket_accept4 (int __fd, __SOCKADDR_ARG __addr,
2392 socklen_t * __restrict __addr_len, int __flags)
2393{
2394 /* SOCK_NONBLOCK and SOCK_CLOEXEC can be bitwise ORed in flags */
2395 return vcom_socket_accept_flags (__fd, __addr, __addr_len, __flags);
2396}
2397#endif
2398
2399/* TBD: move it to vppcom */
2400int
2401vppcom_session_shutdown (int __fd, int __how)
2402{
2403 return 0;
2404}
2405
2406int
2407vcom_socket_shutdown (int __fd, int __how)
2408{
2409 int rv = -1;
2410 vcom_socket_main_t *vsm = &vcom_socket_main;
2411 uword *p;
2412 vcom_socket_t *vsock;
2413
2414 p = hash_get (vsm->sockidx_by_fd, __fd);
2415 if (p)
2416 {
2417 vsock = pool_elt_at_index (vsm->vsockets, p[0]);
2418 switch (__how)
2419 {
2420 case SHUT_RD:
2421 case SHUT_WR:
2422 case SHUT_RDWR:
2423 rv = vppcom_session_shutdown (vsock->sid, __how);
2424 return rv;
2425 break;
2426
2427 default:
2428 return -EINVAL;
2429 break;
2430 }
2431 }
2432
2433 return rv;
2434}
2435
2436int
2437vcom_socket_epoll_create1 (int __flags)
2438{
2439 int rv = -1;
2440 vcom_socket_main_t *vsm = &vcom_socket_main;
2441 vcom_epoll_t *vepoll;
2442
2443 i32 epfd;
2444 i32 vep_idx;
2445 i32 epollidx;
2446
2447 epfd = vcom_socket_open_epoll (__flags);
2448 if (epfd < 0)
2449 {
2450 rv = epfd;
2451 goto out;
2452 }
2453
2454 vep_idx = vppcom_epoll_create ();
2455 if (vep_idx < 0)
2456 {
2457 rv = vep_idx;
2458 goto out_close_epoll;
2459 }
2460
2461 pool_get (vsm->vepolls, vepoll);
2462 vepoll_init (vepoll);
2463
2464 epollidx = vepoll - vsm->vepolls;
2465 hash_set (vsm->epollidx_by_epfd, epfd, epollidx);
2466
2467 vepoll_set (vepoll, epfd, vep_idx, EPOLL_TYPE_VPPCOM_BOUND, __flags, 0, 0);
2468
2469 return epfd;
2470
2471out_close_epoll:
2472 vcom_socket_close_epoll (epfd);
2473out:
2474 return rv;
2475}
2476
2477/*
2478 * PRE: vppcom_epoll_ctl() is successful
2479 * free_vepitem_on_del : 0 - no_pool_put, 1 - pool_put
2480 */
2481int
2482vcom_socket_ctl_vepitem (int __epfd, int __op, int __fd,
2483 struct epoll_event *__event,
2484 i32 vep_idx, vcom_epoll_t * vepoll,
2485 i32 vfd_id, void *vfd, vcom_fd_type_t type,
2486 int free_vepitem_on_del)
2487{
2488 int rv = -1;
2489 vcom_socket_main_t *vsm = &vcom_socket_main;
2490 vcom_epitem_t *vepitem;
2491
2492 vcom_epitem_key_t epfdfd = {.epfd = __epfd,.fd = __fd };
2493 uword *p;
2494 i32 vepitemidx;
2495
2496 i32 *vepitemidxs = 0;
2497
2498 struct epoll_event revent = {.events = 0,.data.fd = INVALID_FD };
2499
2500 i32 vec_idx;
2501
2502 /* perform control operations on the epoll instance */
2503 switch (__op)
2504 {
2505 case EPOLL_CTL_ADD:
2506 /*
2507 * supplied file descriptor is already
2508 * registered with this epoll instance
2509 * */
2510 /* vepitem exists */
2511 p = hash_get (vsm->epitemidx_by_epfdfd, epfdfd.key);
2512 if (p)
2513 {
2514 rv = -EEXIST;
2515 goto out;
2516 }
2517
2518 /* add a new vepitem */
2519 pool_get (vsm->vepitems, vepitem);
2520 vepitem_init (vepitem);
2521
2522 vepitemidx = vepitem - vsm->vepitems;
2523 hash_set (vsm->epitemidx_by_epfdfd, epfdfd.key, vepitemidx);
2524 vepitem_set (vepitem, __epfd, __fd, __fd, __fd, type, *__event, revent);
2525
2526 /* update epitemidxs */
2527 /* by_epfd */
2528 p = hash_get (vsm->epitemidxs_by_epfd, __epfd);
2529 if (!p) /* not exist */
2530 {
2531 vepitemidxs = 0;
2532 vec_add1 (vepitemidxs, vepitemidx);
2533 hash_set (vsm->epitemidxs_by_epfd, __epfd, vepitemidxs);
2534 }
2535 else /* exists */
2536 {
2537 vepitemidxs = *(i32 **) p;
2538 vec_add1 (vepitemidxs, vepitemidx);
2539 hash_set3 (vsm->epitemidxs_by_epfd, __epfd, vepitemidxs, 0);
2540 }
2541 /* update epitemidxs */
2542 /* by_fd */
2543 p = hash_get (vsm->epitemidxs_by_fd, __fd);
2544 if (!p) /* not exist */
2545 {
2546 vepitemidxs = 0;
2547 vec_add1 (vepitemidxs, vepitemidx);
2548 hash_set (vsm->epitemidxs_by_fd, __fd, vepitemidxs);
2549 }
2550 else /* exists */
2551 {
2552 vepitemidxs = *(i32 **) p;
2553 vec_add1 (vepitemidxs, vepitemidx);
2554 hash_set3 (vsm->epitemidxs_by_fd, __fd, vepitemidxs, 0);
2555 }
2556
2557 /* increment vepoll fd count by 1 */
2558 vepoll->count += 1;
2559
2560 rv = 0;
2561 goto out;
2562 break;
2563
2564 case EPOLL_CTL_MOD:
2565 /*
2566 * supplied file descriptor is not
2567 * registered with this epoll instance
2568 * */
2569 /* vepitem not exist */
2570 p = hash_get (vsm->epitemidx_by_epfdfd, epfdfd.key);
2571 if (!p)
2572 {
2573 rv = -ENOENT;
2574 goto out;
2575 }
2576 vepitem = pool_elt_at_index (vsm->vepitems, p[0]);
2577 if (vepitem)
2578 {
2579 vepitem->event = *__event;
2580 vepitem->revent = revent;
2581 }
2582
2583 rv = 0;
2584 goto out;
2585 break;
2586
2587 case EPOLL_CTL_DEL:
2588 /*
2589 * supplied file descriptor is not
2590 * registered with this epoll instance
2591 * */
2592 /* vepitem not exist */
2593 p = hash_get (vsm->epitemidx_by_epfdfd, epfdfd.key);
2594 if (!p)
2595 {
2596 rv = -ENOENT;
2597 goto out;
2598 }
2599 vepitemidx = *(i32 *) p;
2600 hash_unset (vsm->epitemidx_by_epfdfd, epfdfd.key);
2601
2602 /* update epitemidxs */
2603 /* by_epfd */
2604 p = hash_get (vsm->epitemidxs_by_epfd, __epfd);
2605 if (!p) /* not exist */
2606 {
2607 rv = -ENOENT;
2608 goto out;
2609 }
2610 else /* exists */
2611 {
2612 vepitemidxs = *(i32 **) p;
2613 vec_idx = vec_search (vepitemidxs, vepitemidx);
2614 if (vec_idx != ~0)
2615 {
2616 vec_del1 (vepitemidxs, vec_idx);
2617 if (!vec_len (vepitemidxs))
2618 {
2619 vec_free (vepitemidxs);
2620 hash_unset (vsm->epitemidxs_by_epfd, __epfd);
2621 }
2622 }
2623 }
2624
2625 /* update epitemidxs */
2626 /* by_fd */
2627 p = hash_get (vsm->epitemidxs_by_fd, __fd);
2628 if (!p) /* not exist */
2629 {
2630 rv = -ENOENT;
2631 goto out;
2632 }
2633 else /* exists */
2634 {
2635 vepitemidxs = *(i32 **) p;
2636 vec_idx = vec_search (vepitemidxs, vepitemidx);
2637 if (vec_idx != ~0)
2638 {
2639 vec_del1 (vepitemidxs, vec_idx);
2640 if (!vec_len (vepitemidxs))
2641 {
2642 vec_free (vepitemidxs);
2643 hash_unset (vsm->epitemidxs_by_fd, __fd);
2644 }
2645 }
2646 }
2647
2648 /* pool put vepitem */
2649 vepitem = pool_elt_at_index (vsm->vepitems, vepitemidx);
2650 if (free_vepitem_on_del)
2651 {
2652 if (!vepitem)
2653 {
2654 rv = -ENOENT;
2655 goto out;
2656 }
2657 vepitem_init (vepitem);
2658 pool_put (vsm->vepitems, vepitem);
2659 }
2660 else
2661 {
2662 if (!vepitem)
2663 {
2664 vepitem_init (vepitem);
2665 }
2666 }
2667
2668 /* decrement vepoll fd count by 1 */
2669 vepoll->count -= 1;
2670
2671 rv = 0;
2672 goto out;
2673 break;
2674
2675 default:
2676 rv = -EINVAL;
2677 goto out;
2678 break;
2679 }
2680
2681out:
2682 return rv;
2683}
2684
2685/*
2686 * PRE: 00. null pointer check on __event
2687 * 01. all other parameters are validated
2688 */
2689
2690static int
2691vcom_socket_epoll_ctl_internal (int __epfd, int __op, int __fd,
2692 struct epoll_event *__event,
2693 int free_vepitem_on_del)
2694{
2695 int rv = -1;
2696
2697 /* vcom_socket_main_t *vsm = &vcom_socket_main; */
2698 vcom_epoll_t *vepoll;
2699
2700 /*__fd could could be vcom socket or vcom epoll or kernel fd */
2701 void *vfd;
2702 vcom_epoll_t *vfd_vepoll;
2703 vcom_socket_t *vfd_vsock;
2704
2705 i32 vep_idx;
2706 i32 vfd_id;
2707
2708 vcom_fd_type_t type = FD_TYPE_INVALID;
2709
2710 /* validate __event */
2711
2712 /* get vep_idx and vepoll */
2713 vep_idx = vcom_socket_get_vep_idx_and_vepoll (__epfd, &vepoll);
2714 if (vep_idx == INVALID_VEP_IDX)
2715 {
2716 return -EBADF;
2717 }
2718
2719 /* get vcom fd type, vfd_id and vfd */
2720 vfd_id = vcom_socket_get_sid_and_vsock (__fd, &vfd_vsock);
2721 if (vfd_id != INVALID_SESSION_ID)
2722 {
2723 type = FD_TYPE_VCOM_SOCKET;
2724 vfd = vfd_vsock;
2725 }
2726 else if ((vfd_id = vcom_socket_get_vep_idx_and_vepoll (__fd, &vfd_vepoll))
2727 != INVALID_VEP_IDX)
2728 {
2729 type = FD_TYPE_EPOLL;
2730 vfd = vfd_vepoll;
2731 }
2732 else
2733 {
2734 /* FD_TYPE_KERNEL not supported by epoll instance */
2735 type = FD_TYPE_INVALID;
2736 return -EBADF;
2737 }
2738
2739
2740 /* vepoll and vsock are now valid */
2741 rv = vppcom_epoll_ctl (vep_idx, __op, vfd_id, __event);
2742 if (rv < 0)
2743 {
2744 return rv;
2745 }
2746
2747 rv = vcom_socket_ctl_vepitem (__epfd, __op, __fd,
2748 __event,
2749 vep_idx, vepoll,
2750 vfd_id, vfd, type, free_vepitem_on_del);
2751 return rv;
2752}
2753
2754int
2755vcom_socket_epoll_ctl (int __epfd, int __op, int __fd,
2756 struct epoll_event *__event)
2757{
2758 int rv = -1;
2759
2760 rv = vcom_socket_epoll_ctl_internal (__epfd, __op, __fd, __event, 1);
2761 return rv;
2762}
2763
2764static int
2765vcom_socket_epoll_ctl1 (int __epfd, int __op, int __fd,
2766 struct epoll_event *__event)
2767{
2768 int rv = -1;
2769
2770 rv = vcom_socket_epoll_ctl_internal (__epfd, __op, __fd, __event, 0);
2771 return rv;
2772}
2773
2774int
2775vcom_socket_epoll_pwait (int __epfd, struct epoll_event *__events,
2776 int __maxevents, int __timeout,
2777 const __sigset_t * __ss)
2778{
2779 int rv = -EBADF;
2780
2781 /* in seconds eg. 3.123456789 seconds */
2782 double time_to_wait = (double) 0;
2783
2784 i32 vep_idx;
2785
2786 /* validate __event */
2787 if (!__events)
2788 {
2789 rv = -EFAULT;
2790 goto out;
2791 }
2792
2793 /* validate __timeout */
2794 if (__timeout > 0)
2795 {
2796 time_to_wait = (double) __timeout / (double) 1000;
2797 }
2798 else if (__timeout == 0)
2799 {
2800 time_to_wait = (double) 0;
2801 }
2802 else if (__timeout == -1)
2803 {
2804 time_to_wait = ~0;
2805 }
2806 else
2807 {
2808 rv = -EBADF;
2809 goto out;
2810 }
2811
2812 /* get vep_idx */
2813 vep_idx = vcom_socket_get_vep_idx (__epfd);
2814 if (vep_idx != INVALID_VEP_IDX)
2815 {
2816 rv = vppcom_epoll_wait (vep_idx, __events, __maxevents, time_to_wait);
2817 }
2818out:
2819 return rv;
2820}
2821
2822int
2823vcom_socket_main_init (void)
2824{
2825 vcom_socket_main_t *vsm = &vcom_socket_main;
2826
2827 if (VCOM_DEBUG > 0)
2828 printf ("vcom_socket_main_init\n");
2829
2830 if (!vsm->init)
2831 {
2832 /* TBD: define FD_MAXSIZE and use it here */
2833 pool_alloc (vsm->vsockets, FD_SETSIZE);
2834 vsm->sockidx_by_fd = hash_create (0, sizeof (i32));
2835
2836 pool_alloc (vsm->vepolls, FD_SETSIZE);
2837 vsm->epollidx_by_epfd = hash_create (0, sizeof (i32));
2838
2839 pool_alloc (vsm->vepitems, FD_SETSIZE);
2840 vsm->epitemidx_by_epfdfd = hash_create (0, sizeof (i32));
2841
2842 vsm->epitemidxs_by_epfd = hash_create (0, sizeof (i32 *));
2843 vsm->epitemidxs_by_fd = hash_create (0, sizeof (i32 *));
2844
2845 vsm->init = 1;
2846 }
2847
2848 return 0;
2849}
2850
2851
2852void
2853vcom_socket_main_show (void)
2854{
2855 vcom_socket_main_t *vsm = &vcom_socket_main;
2856 vcom_socket_t *vsock;
2857
2858 vcom_epoll_t *vepoll;
2859
2860 vcom_epitem_t *vepitem;
2861
2862 i32 epfd;
2863 i32 fd;
2864 i32 *vepitemidxs, *vepitemidxs_var;
2865
2866 if (vsm->init)
2867 {
2868 /* from active list of vsockets show vsock */
2869
2870 /* *INDENT-OFF* */
2871 pool_foreach (vsock, vsm->vsockets,
2872 ({
2873 printf(
2874 "fd='%04d', sid='%08x',type='%-30s'\n",
2875 vsock->fd, vsock->sid,
2876 vcom_socket_type_str (vsock->type));
2877 }));
2878 /* *INDENT-ON* */
2879
2880 /* from active list of vepolls, show vepoll */
2881
2882 /* *INDENT-OFF* */
2883 pool_foreach (vepoll, vsm->vepolls,
2884 ({
2885 printf(
2886 "epfd='%04d', vep_idx='%08x', "
2887 "type='%-30s', "
2888 "flags='%d', count='%d', close='%d'\n",
2889 vepoll->epfd, vepoll->vep_idx,
2890 vcom_socket_epoll_type_str (vepoll->type),
2891 vepoll->flags, vepoll->count, vepoll->close);
2892 }));
2893 /* *INDENT-ON* */
2894
2895 /* from active list of vepitems, show vepitem */
2896
2897 /* *INDENT-OFF* */
2898 pool_foreach (vepitem, vsm->vepitems,
2899 ({
2900 printf(
2901 "epfd='%04d', fd='%04d', "
2902 "next_fd='%04d', prev_fd='%04d', "
2903 "type='%-30s', "
2904 "events='%04x', revents='%04x'\n",
2905 vepitem->epfd, vepitem->fd,
2906 vepitem->next_fd, vepitem->prev_fd,
2907 vcom_socket_vcom_fd_type_str (vepitem->type),
2908 vepitem->event.events, vepitem->revent.events);
2909 }));
2910
2911 /* *INDENT-ON* */
2912
2913 /* show epitemidxs for epfd */
2914 /* *INDENT-OFF* */
2915 hash_foreach (epfd, vepitemidxs,
2916 vsm->epitemidxs_by_epfd,
2917 ({
2918 printf("\n[ '%04d': ", epfd);
2919 vec_foreach (vepitemidxs_var,vepitemidxs)
2920 {
2921 printf("'%04d' ", (int)vepitemidxs_var[0]);
2922 }
2923 printf("]\n");
2924 }));
2925 /* *INDENT-ON* */
2926
2927 /* show epitemidxs for fd */
2928 /* *INDENT-OFF* */
2929 hash_foreach (fd, vepitemidxs,
2930 vsm->epitemidxs_by_fd,
2931 ({
2932 printf("\n{ '%04d': ", fd);
2933 vec_foreach (vepitemidxs_var,vepitemidxs)
2934 {
2935 printf("'%04d' ", (int)vepitemidxs_var[0]);
2936 }
2937 printf("}\n");
2938 }));
2939 /* *INDENT-ON* */
2940
2941 }
2942}
2943
2944void
2945vcom_socket_main_destroy (void)
2946{
2947 vcom_socket_main_t *vsm = &vcom_socket_main;
2948 vcom_socket_t *vsock;
2949
2950 vcom_epoll_t *vepoll;
2951
2952 vcom_epitem_t *vepitem;
2953
2954 i32 epfd;
2955 i32 fd;
2956 i32 *vepitemidxs;
2957
2958
2959 if (VCOM_DEBUG > 0)
2960 printf ("vcom_socket_main_destroy\n");
2961
2962 if (vsm->init)
2963 {
2964
2965 /*
2966 * from active list of vepitems,
2967 * remove all "vepitem" elements from the pool in a safe way
2968 * */
2969
2970 /* *INDENT-OFF* */
2971 pool_flush (vepitem, vsm->vepitems,
2972 ({
2973 if (vepitem->type == FD_TYPE_EPOLL || FD_TYPE_VCOM_SOCKET)
2974 {
2975 vcom_socket_epoll_ctl1 (vepitem->epfd, EPOLL_CTL_DEL,
2976 vepitem->fd, NULL);
2977 vepitem_init (vepitem);
2978 }
2979 }));
2980 /* *INDENT-ON* */
2981
2982 pool_free (vsm->vepitems);
2983 hash_free (vsm->epitemidx_by_epfdfd);
2984
2985 /* free vepitemidxs for each epfd */
2986 /* *INDENT-OFF* */
2987 hash_foreach (epfd, vepitemidxs,
2988 vsm->epitemidxs_by_epfd,
2989 ({
2990 vec_free (vepitemidxs);
2991 }));
2992 /* *INDENT-ON* */
2993 hash_free (vsm->epitemidxs_by_epfd);
2994
2995 /* free vepitemidxs for each fd */
2996 /* *INDENT-OFF* */
2997 hash_foreach (fd, vepitemidxs,
2998 vsm->epitemidxs_by_fd,
2999 ({
3000 vec_free (vepitemidxs);
3001 }));
3002 /* *INDENT-ON* */
3003 hash_free (vsm->epitemidxs_by_fd);
3004
3005
3006 /*
3007 * from active list of vsockets,
3008 * close socket and vppcom session
3009 * */
3010
3011 /* *INDENT-OFF* */
3012 pool_foreach (vsock, vsm->vsockets,
3013 ({
3014 if (vsock->type == SOCKET_TYPE_VPPCOM_BOUND)
3015 {
3016 vppcom_session_close (vsock->sid);
3017 vcom_socket_close_socket (vsock->fd);
3018 vsocket_init (vsock);
3019 }
3020 }));
3021 /* *INDENT-ON* */
3022
3023 /*
3024 * return vsocket element to the pool
3025 * */
3026
3027 /* *INDENT-OFF* */
3028 pool_flush (vsock, vsm->vsockets,
3029 ({
3030 // vsocket_init(vsock);
3031 ;
3032 }));
3033 /* *INDENT-ON* */
3034
3035 pool_free (vsm->vsockets);
3036 hash_free (vsm->sockidx_by_fd);
3037
3038 /*
3039 * from active list of vepolls,
3040 * close epoll and vppcom_epoll
3041 * */
3042
3043 /* *INDENT-OFF* */
3044 pool_foreach (vepoll, vsm->vepolls,
3045 ({
3046 if (vepoll->type == EPOLL_TYPE_VPPCOM_BOUND)
3047 {
3048 vppcom_session_close (vepoll->vep_idx);
3049 vcom_socket_close_epoll (vepoll->epfd); /* TBD: */
3050 vepoll_init (vepoll);
3051 }
3052 }));
3053 /* *INDENT-ON* */
3054
3055 /*
3056 * return vepoll element to the pool
3057 * */
3058
3059 /* *INDENT-OFF* */
3060 pool_flush (vepoll, vsm->vepolls,
3061 ({
3062 // vepoll_init(vepoll);
3063 ;
3064 }));
3065 /* *INDENT-ON* */
3066
3067 pool_free (vsm->vepolls);
3068 hash_free (vsm->epollidx_by_epfd);
3069
3070 vsm->init = 0;
3071 }
3072}
3073
3074
3075/*
3076 * fd.io coding-style-patch-verification: ON
3077 *
3078 * Local Variables:
3079 * eval: (c-set-style "gnu")
3080 * End:
3081 */