Use splice/tee to connect vsys clients and servers.
[vsys.git] / splice_stub.c
1 /* This module allows data between vsys clients and servers to be copied in kernel -
2  * and some of these copies to be eliminated through page rewrites */
3
4 #define SPLICE_SYSCALL  313
5 #define TEE_SYSCALL     315
6 #define SPLICE_F_NONBLOCK 0x02
7
8 #include <string.h>
9 #include <stdio.h>
10 #include <unistd.h>
11 #include <errno.h>
12 #include <sys/syscall.h>
13 #include <caml/mlvalues.h>
14 #include <caml/memory.h>
15 #include <caml/alloc.h>
16 #include <caml/custom.h>
17 #include <caml/fail.h>
18 #include <caml/signals.h>
19
20 CAMLprim value stub_splice(value fd_in, value fd_out, value len)
21 {
22         CAMLparam3(fd_in, fd_out, len);
23         long ret;
24         ret = syscall(SPLICE_SYSCALL, Int_val(fd_in), NULL, Int_val(fd_out),NULL, Int_val(len), SPLICE_F_NONBLOCK);
25         if (ret == -1) {
26                 printf ("Splice error: %s\n", strerror(errno));
27                 caml_failwith("Splice system call returned -1");
28         }
29         CAMLreturn(Val_int(ret));
30 }
31
32 CAMLprim value stub_tee(value fd_in, value fd_out, value len)
33 {
34         CAMLparam3(fd_in, fd_out, len);
35         long ret;
36         ret = syscall(TEE_SYSCALL,Int_val(fd_in), Int_val(fd_out), Int_val(len), SPLICE_F_NONBLOCK);
37         if (ret == -1) {
38                 printf ("Sendfile error: %s\n", strerror(errno));
39                 caml_failwith("Splice system call returned -1");
40         }
41         CAMLreturn(Val_int(ret));
42 }