Debian Codegraph @

pkg/hvsock/hvsock_linux.go

raw
1 package hvsock
2
3 // On Linux we have to deal with two different implementations. The
4 // "legacy" implementation never made it into the kernel, but several
5 // kernels, including the LinuxKit one carried patches for it for
6 // quite a while. The legacy version defined a new address family
7 // while the new version sits on top of the existing VMware/virtio
8 // socket implementation.
9 //
10 // We try to determine at init if we are on a kernel with the legacy
11 // implementation or the new version and set "legacyMode" accordingly.
12 //
13 // We can't just reuse the vsock implementation as we still need to
14 // emulated CloseRead()/CloseWrite() as not all Windows builds support
15 // it.
16
17 /*
18 #include <sys/socket.h>
19
20 struct sockaddr_hv {
21 unsigned short shv_family;
22 unsigned short reserved;
23 unsigned char shv_vm_id[16];
24 unsigned char shv_service_id[16];
25 };
26 int bind_sockaddr_hv(int fd, const struct sockaddr_hv *sa_hv) {
27 return bind(fd, (const struct sockaddr*)sa_hv, sizeof(*sa_hv));
28 }
29 int connect_sockaddr_hv(int fd, const struct sockaddr_hv *sa_hv) {
30 return connect(fd, (const struct sockaddr*)sa_hv, sizeof(*sa_hv));
31 }
32 int accept_hv(int fd, struct sockaddr_hv *sa_hv, socklen_t *sa_hv_len) {
33 return accept(fd, (struct sockaddr *)sa_hv, sa_hv_len);
34 }
35 int getsockname_hv(int fd, struct sockaddr_hv *sa_hv, socklen_t *sa_hv_len) {
36 return getsockname(fd, (struct sockaddr *)sa_hv, sa_hv_len);
37 }
38 */
39 import "C"
40
41 import (
42 "fmt"
43 "net"
44 "os"
45 "syscall"
46 "time"
47
48 "github.com/pkg/errors"
49 "golang.org/x/sys/unix"
50 )
51
52 const (
53 hvsockAF = 43 //SHV_PROTO_RAW
54 hvsockRaw = 1 // SHV_PROTO_RAW
55 )
56
57 // Supported returns if hvsocks are supported on your platform
58 func Supported() bool {
59 var sa C.struct_sockaddr_hv
60 var sa_len C.socklen_t
61
62 // Try opening a hvsockAF socket. If it works we are on older, i.e. 4.9.x kernels.
63 // 4.11 defines AF_SMC as 43 but it doesn't support protocol 1 so the
64 // socket() call should fail.
65 fd, err := syscall.Socket(hvsockAF, syscall.SOCK_STREAM, hvsockRaw)
66 if err != nil {
67 return false
68 }
69
70 // 4.16 defines SMCPROTO_SMC6 as 1 but its socket name size doesn't match
71 // size of sockaddr_hv so corresponding check should fail.
72 sa_len = C.sizeof_struct_sockaddr_hv
73 ret, _ := C.getsockname_hv(C.int(fd), &sa, &sa_len)
74 syscall.Close(fd)
75 if ret < 0 || sa_len != C.sizeof_struct_sockaddr_hv {
76 return false
77 }
78
79 return true
80 }
81
82 // Dial a Hyper-V socket address
83 func Dial(raddr Addr) (Conn, error) {
84 fd, err := syscall.Socket(hvsockAF, syscall.SOCK_STREAM, hvsockRaw)
85 if err != nil {
86 return nil, err
87 }
88
89 sa := C.struct_sockaddr_hv{}
90 sa.shv_family = hvsockAF
91 sa.reserved = 0
92
93 for i := 0; i < 16; i++ {
94 sa.shv_vm_id[i] = C.uchar(raddr.VMID[i])
95 }
96 for i := 0; i < 16; i++ {
97 sa.shv_service_id[i] = C.uchar(raddr.ServiceID[i])
98 }
99
100 // Retry connect in a loop if EINTR is encountered.
101 for {
102 if ret, errno := C.connect_sockaddr_hv(C.int(fd), &sa); ret != 0 {
103 if errno == syscall.EINTR {
104 continue
105 }
106 return nil, fmt.Errorf("connect(%s) failed with %d, errno=%d", raddr, ret, errno)
107 }
108 break
109 }
110
111 return newHVsockConn(uintptr(fd), &Addr{VMID: GUIDZero, ServiceID: GUIDZero}, &raddr), nil
112 }
113
114 // Listen returns a net.Listener which can accept connections on the given port
115 func Listen(addr Addr) (net.Listener, error) {
116 fd, err := syscall.Socket(hvsockAF, syscall.SOCK_STREAM, hvsockRaw)
117 if err != nil {
118 return nil, err
119 }
120
121 sa := C.struct_sockaddr_hv{}
122 sa.shv_family = hvsockAF
123 sa.reserved = 0
124
125 for i := 0; i < 16; i++ {
126 sa.shv_vm_id[i] = C.uchar(addr.VMID[i])
127 }
128 for i := 0; i < 16; i++ {
129 sa.shv_service_id[i] = C.uchar(addr.ServiceID[i])
130 }
131
132 if ret, errno := C.bind_sockaddr_hv(C.int(fd), &sa); ret != 0 {
133 return nil, fmt.Errorf("listen(%s) failed with %d, errno=%d", addr, ret, errno)
134 }
135
136 err = syscall.Listen(fd, syscall.SOMAXCONN)
137 if err != nil {
138 return nil, errors.Wrapf(err, "listen(%s) failed", addr)
139 }
140 return &hvsockListener{fd, addr}, nil
141 }
142
143 //
144 // Hyper-v sockets Listener implementation
145 //
146
147 type hvsockListener struct {
148 fd int
149 local Addr
150 }
151
152 // Accept accepts an incoming call and returns the new connection.
153 func (v *hvsockListener) Accept() (net.Conn, error) {
154 var acceptSA C.struct_sockaddr_hv
155 var acceptSALen C.socklen_t
156
157 acceptSALen = C.sizeof_struct_sockaddr_hv
158 fd, err := C.accept_hv(C.int(v.fd), &acceptSA, &acceptSALen)
159 if err != nil {
160 return nil, errors.Wrapf(err, "accept(%s) failed", v.local)
161 }
162
163 remote := &Addr{VMID: guidFromC(acceptSA.shv_vm_id), ServiceID: guidFromC(acceptSA.shv_service_id)}
164 return newHVsockConn(uintptr(fd), &v.local, remote), nil
165 }
166
167 // Close closes the listening connection
168 func (v *hvsockListener) Close() error {
169 // Note this won't cause the Accept to unblock.
170 return unix.Close(v.fd)
171 }
172
173 // Addr returns the address the Listener is listening on
174 func (v *hvsockListener) Addr() net.Addr {
175 return v.local
176 }
177
178 //
179 // Hyper-V socket connection implementation
180 //
181
182 // hvsockConn represents a connection over a Hyper-V socket
183 type hvsockConn struct {
184 hvsock *os.File
185 fd uintptr
186 local *Addr
187 remote *Addr
188 }
189
190 func newHVsockConn(fd uintptr, local, remote *Addr) *hvsockConn {
191 hvsock := os.NewFile(fd, fmt.Sprintf("hvsock:%d", fd))
192 return &hvsockConn{hvsock: hvsock, fd: fd, local: local, remote: remote}
193 }
194
195 // LocalAddr returns the local address of a connection
196 func (v *hvsockConn) LocalAddr() net.Addr {
197 return v.local
198 }
199
200 // RemoteAddr returns the remote address of a connection
201 func (v *hvsockConn) RemoteAddr() net.Addr {
202 return v.remote
203 }
204
205 // Close closes the connection
206 func (v *hvsockConn) Close() error {
207 return v.hvsock.Close()
208 }
209
210 // CloseRead shuts down the reading side of a hvsock connection
211 func (v *hvsockConn) CloseRead() error {
212 return syscall.Shutdown(int(v.fd), syscall.SHUT_RD)
213 }
214
215 // CloseWrite shuts down the writing side of a hvsock connection
216 func (v *hvsockConn) CloseWrite() error {
217 return syscall.Shutdown(int(v.fd), syscall.SHUT_WR)
218 }
219
220 // Read reads data from the connection
221 func (v *hvsockConn) Read(buf []byte) (int, error) {
222 return v.hvsock.Read(buf)
223 }
224
225 // Write writes data over the connection
226 // TODO(rn): replace with a straight call to v.hvsock.Write() once 4.9.x support is deprecated
227 func (v *hvsockConn) Write(buf []byte) (int, error) {
228 written := 0
229 toWrite := len(buf)
230 for toWrite > 0 {
231 thisBatch := min(toWrite, maxMsgSize)
232 n, err := v.hvsock.Write(buf[written : written+thisBatch])
233 if err != nil {
234 return written, err
235 }
236 if n != thisBatch {
237 return written, fmt.Errorf("short write %d != %d", n, thisBatch)
238 }
239 toWrite -= n
240 written += n
241 }
242
243 return written, nil
244 }
245
246 // SetDeadline sets the read and write deadlines associated with the connection
247 func (v *hvsockConn) SetDeadline(t time.Time) error {
248 return nil // FIXME
249 }
250
251 // SetReadDeadline sets the deadline for future Read calls.
252 func (v *hvsockConn) SetReadDeadline(t time.Time) error {
253 return nil // FIXME
254 }
255
256 // SetWriteDeadline sets the deadline for future Write calls
257 func (v *hvsockConn) SetWriteDeadline(t time.Time) error {
258 return nil // FIXME
259 }
260
261 // File duplicates the underlying socket descriptor and returns it.
262 func (v *hvsockConn) File() (*os.File, error) {
263 // This is equivalent to dup(2) but creates the new fd with CLOEXEC already set.
264 r0, _, e1 := syscall.Syscall(syscall.SYS_FCNTL, uintptr(v.hvsock.Fd()), syscall.F_DUPFD_CLOEXEC, 0)
265 if e1 != 0 {
266 return nil, os.NewSyscallError("fcntl", e1)
267 }
268 return os.NewFile(r0, v.hvsock.Name()), nil
269 }
270
271 func guidFromC(cg [16]C.uchar) GUID {
272 var g GUID
273 for i := 0; i < 16; i++ {
274 g[i] = byte(cg[i])
275 }
276 return g
277 }