1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
|
// Copyright 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package rpcinet
import (
"syscall"
"gvisor.googlesource.com/gvisor/pkg/abi/linux"
"gvisor.googlesource.com/gvisor/pkg/sentry/arch"
"gvisor.googlesource.com/gvisor/pkg/sentry/context"
"gvisor.googlesource.com/gvisor/pkg/sentry/fs"
"gvisor.googlesource.com/gvisor/pkg/sentry/fs/fsutil"
"gvisor.googlesource.com/gvisor/pkg/sentry/kernel"
"gvisor.googlesource.com/gvisor/pkg/sentry/kernel/kdefs"
ktime "gvisor.googlesource.com/gvisor/pkg/sentry/kernel/time"
"gvisor.googlesource.com/gvisor/pkg/sentry/socket"
"gvisor.googlesource.com/gvisor/pkg/sentry/socket/rpcinet/conn"
"gvisor.googlesource.com/gvisor/pkg/sentry/socket/rpcinet/notifier"
pb "gvisor.googlesource.com/gvisor/pkg/sentry/socket/rpcinet/syscall_rpc_go_proto"
"gvisor.googlesource.com/gvisor/pkg/sentry/usermem"
"gvisor.googlesource.com/gvisor/pkg/syserr"
"gvisor.googlesource.com/gvisor/pkg/syserror"
"gvisor.googlesource.com/gvisor/pkg/tcpip/buffer"
"gvisor.googlesource.com/gvisor/pkg/tcpip/transport/unix"
"gvisor.googlesource.com/gvisor/pkg/waiter"
)
// socketOperations implements fs.FileOperations and socket.Socket for a socket
// implemented using a host socket.
type socketOperations struct {
socket.ReceiveTimeout
fsutil.PipeSeek `state:"nosave"`
fsutil.NotDirReaddir `state:"nosave"`
fsutil.NoFsync `state:"nosave"`
fsutil.NoopFlush `state:"nosave"`
fsutil.NoMMap `state:"nosave"`
fd uint32 // must be O_NONBLOCK
wq *waiter.Queue
rpcConn *conn.RPCConnection
notifier *notifier.Notifier
}
// Verify that we actually implement socket.Socket.
var _ = socket.Socket(&socketOperations{})
const (
sizeOfIfReq = 40
)
// New creates a new RPC socket.
func newSocketFile(ctx context.Context, stack *Stack, family int, skType int, protocol int) (*fs.File, *syserr.Error) {
id, c := stack.rpcConn.NewRequest(pb.SyscallRequest{Args: &pb.SyscallRequest_Socket{&pb.SocketRequest{Family: int64(family), Type: int64(skType | syscall.SOCK_NONBLOCK), Protocol: int64(protocol)}}}, false /* ignoreResult */)
<-c
res := stack.rpcConn.Request(id).Result.(*pb.SyscallResponse_Socket).Socket.Result
if e, ok := res.(*pb.SocketResponse_ErrorNumber); ok {
return nil, syserr.FromHost(syscall.Errno(e.ErrorNumber))
}
fd := res.(*pb.SocketResponse_Fd).Fd
var wq waiter.Queue
stack.notifier.AddFD(fd, &wq)
dirent := socket.NewDirent(ctx, socketDevice)
defer dirent.DecRef()
return fs.NewFile(ctx, dirent, fs.FileFlags{Read: true, Write: true}, &socketOperations{
wq: &wq,
fd: fd,
rpcConn: stack.rpcConn,
notifier: stack.notifier,
}), nil
}
func isBlockingErrno(err error) bool {
return err == syscall.EAGAIN || err == syscall.EWOULDBLOCK
}
func translateIOSyscallError(err error) error {
if isBlockingErrno(err) {
return syserror.ErrWouldBlock
}
return err
}
// Release implements fs.FileOperations.Release.
func (s *socketOperations) Release() {
s.notifier.RemoveFD(s.fd)
// We always need to close the FD.
_, _ = s.rpcConn.NewRequest(pb.SyscallRequest{Args: &pb.SyscallRequest_Close{&pb.CloseRequest{Fd: s.fd}}}, true /* ignoreResult */)
}
// Readiness implements waiter.Waitable.Readiness.
func (s *socketOperations) Readiness(mask waiter.EventMask) waiter.EventMask {
return s.notifier.NonBlockingPoll(s.fd, mask)
}
// EventRegister implements waiter.Waitable.EventRegister.
func (s *socketOperations) EventRegister(e *waiter.Entry, mask waiter.EventMask) {
s.wq.EventRegister(e, mask)
s.notifier.UpdateFD(s.fd)
}
// EventUnregister implements waiter.Waitable.EventUnregister.
func (s *socketOperations) EventUnregister(e *waiter.Entry) {
s.wq.EventUnregister(e)
s.notifier.UpdateFD(s.fd)
}
func rpcRead(t *kernel.Task, req *pb.SyscallRequest_Read) (*pb.ReadResponse_Data, *syserr.Error) {
s := t.NetworkContext().(*Stack)
id, c := s.rpcConn.NewRequest(pb.SyscallRequest{Args: req}, false /* ignoreResult */)
<-c
res := s.rpcConn.Request(id).Result.(*pb.SyscallResponse_Read).Read.Result
if e, ok := res.(*pb.ReadResponse_ErrorNumber); ok {
return nil, syserr.FromHost(syscall.Errno(e.ErrorNumber))
}
return res.(*pb.ReadResponse_Data), nil
}
// Read implements fs.FileOperations.Read.
func (s *socketOperations) Read(ctx context.Context, _ *fs.File, dst usermem.IOSequence, _ int64) (int64, error) {
req := &pb.SyscallRequest_Read{&pb.ReadRequest{
Fd: s.fd,
Length: uint32(dst.NumBytes()),
}}
res, se := rpcRead(ctx.(*kernel.Task), req)
if se == nil {
n, e := dst.CopyOut(ctx, res.Data)
return int64(n), e
}
return 0, se.ToError()
}
func rpcWrite(t *kernel.Task, req *pb.SyscallRequest_Write) (uint32, *syserr.Error) {
s := t.NetworkContext().(*Stack)
id, c := s.rpcConn.NewRequest(pb.SyscallRequest{Args: req}, false /* ignoreResult */)
<-c
res := s.rpcConn.Request(id).Result.(*pb.SyscallResponse_Write).Write.Result
if e, ok := res.(*pb.WriteResponse_ErrorNumber); ok {
return 0, syserr.FromHost(syscall.Errno(e.ErrorNumber))
}
return res.(*pb.WriteResponse_Length).Length, nil
}
// Write implements fs.FileOperations.Write.
func (s *socketOperations) Write(ctx context.Context, _ *fs.File, src usermem.IOSequence, _ int64) (int64, error) {
t := ctx.(*kernel.Task)
v := buffer.NewView(int(src.NumBytes()))
// Copy all the data into the buffer.
if _, err := src.CopyIn(t, v); err != nil {
return 0, err
}
n, err := rpcWrite(t, &pb.SyscallRequest_Write{&pb.WriteRequest{Fd: s.fd, Data: v}})
return int64(n), err.ToError()
}
func rpcConnect(t *kernel.Task, fd uint32, sockaddr []byte) *syserr.Error {
s := t.NetworkContext().(*Stack)
id, c := s.rpcConn.NewRequest(pb.SyscallRequest{Args: &pb.SyscallRequest_Connect{&pb.ConnectRequest{Fd: uint32(fd), Address: sockaddr}}}, false /* ignoreResult */)
<-c
if e := s.rpcConn.Request(id).Result.(*pb.SyscallResponse_Connect).Connect.ErrorNumber; e != 0 {
return syserr.FromHost(syscall.Errno(e))
}
return nil
}
// Connect implements socket.Socket.Connect.
func (s *socketOperations) Connect(t *kernel.Task, sockaddr []byte, blocking bool) *syserr.Error {
if !blocking {
return rpcConnect(t, s.fd, sockaddr)
}
// Register for notification when the endpoint becomes writable, then
// initiate the connection.
e, ch := waiter.NewChannelEntry(nil)
s.EventRegister(&e, waiter.EventOut|waiter.EventIn|waiter.EventHUp)
defer s.EventUnregister(&e)
for {
if err := rpcConnect(t, s.fd, sockaddr); err == nil || err != syserr.ErrInProgress && err != syserr.ErrAlreadyInProgress {
return err
}
// It's pending, so we have to wait for a notification, and fetch the
// result once the wait completes.
if err := t.Block(ch); err != nil {
return syserr.FromError(err)
}
}
}
func rpcAccept(t *kernel.Task, fd uint32, peer bool) (*pb.AcceptResponse_ResultPayload, *syserr.Error) {
stack := t.NetworkContext().(*Stack)
id, c := stack.rpcConn.NewRequest(pb.SyscallRequest{Args: &pb.SyscallRequest_Accept{&pb.AcceptRequest{Fd: fd, Peer: peer, Flags: syscall.SOCK_NONBLOCK}}}, false /* ignoreResult */)
<-c
res := stack.rpcConn.Request(id).Result.(*pb.SyscallResponse_Accept).Accept.Result
if e, ok := res.(*pb.AcceptResponse_ErrorNumber); ok {
return nil, syserr.FromHost(syscall.Errno(e.ErrorNumber))
}
return res.(*pb.AcceptResponse_Payload).Payload, nil
}
// Accept implements socket.Socket.Accept.
func (s *socketOperations) Accept(t *kernel.Task, peerRequested bool, flags int, blocking bool) (kdefs.FD, interface{}, uint32, *syserr.Error) {
payload, se := rpcAccept(t, s.fd, peerRequested)
// Check if we need to block.
if blocking && se == syserr.ErrTryAgain {
// Register for notifications.
e, ch := waiter.NewChannelEntry(nil)
s.EventRegister(&e, waiter.EventIn)
defer s.EventUnregister(&e)
// Try to accept the connection again; if it fails, then wait until we
// get a notification.
for {
if payload, se = rpcAccept(t, s.fd, peerRequested); se != syserr.ErrTryAgain {
break
}
if err := t.Block(ch); err != nil {
return 0, nil, 0, syserr.FromError(err)
}
}
}
// Handle any error from accept.
if se != nil {
return 0, nil, 0, se
}
var wq waiter.Queue
s.notifier.AddFD(payload.Fd, &wq)
dirent := socket.NewDirent(t, socketDevice)
defer dirent.DecRef()
file := fs.NewFile(t, dirent, fs.FileFlags{Read: true, Write: true, NonBlocking: flags&linux.SOCK_NONBLOCK != 0}, &socketOperations{
wq: &wq,
fd: payload.Fd,
rpcConn: s.rpcConn,
notifier: s.notifier,
})
defer file.DecRef()
fdFlags := kernel.FDFlags{
CloseOnExec: flags&linux.SOCK_CLOEXEC != 0,
}
fd, err := t.FDMap().NewFDFrom(0, file, fdFlags, t.ThreadGroup().Limits())
if err != nil {
return 0, nil, 0, syserr.FromError(err)
}
if peerRequested {
return fd, payload.Address.Address, payload.Address.Length, nil
}
return fd, nil, 0, nil
}
// Bind implements socket.Socket.Bind.
func (s *socketOperations) Bind(t *kernel.Task, sockaddr []byte) *syserr.Error {
stack := t.NetworkContext().(*Stack)
id, c := stack.rpcConn.NewRequest(pb.SyscallRequest{Args: &pb.SyscallRequest_Bind{&pb.BindRequest{Fd: s.fd, Address: sockaddr}}}, false /* ignoreResult */)
<-c
if e := stack.rpcConn.Request(id).Result.(*pb.SyscallResponse_Bind).Bind.ErrorNumber; e != 0 {
syserr.FromHost(syscall.Errno(e))
}
return nil
}
// Listen implements socket.Socket.Listen.
func (s *socketOperations) Listen(t *kernel.Task, backlog int) *syserr.Error {
stack := t.NetworkContext().(*Stack)
id, c := stack.rpcConn.NewRequest(pb.SyscallRequest{Args: &pb.SyscallRequest_Listen{&pb.ListenRequest{Fd: s.fd, Backlog: int64(backlog)}}}, false /* ignoreResult */)
<-c
if e := stack.rpcConn.Request(id).Result.(*pb.SyscallResponse_Listen).Listen.ErrorNumber; e != 0 {
syserr.FromHost(syscall.Errno(e))
}
return nil
}
// Shutdown implements socket.Socket.Shutdown.
func (s *socketOperations) Shutdown(t *kernel.Task, how int) *syserr.Error {
stack := t.NetworkContext().(*Stack)
id, c := stack.rpcConn.NewRequest(pb.SyscallRequest{Args: &pb.SyscallRequest_Shutdown{&pb.ShutdownRequest{Fd: s.fd, How: int64(how)}}}, false /* ignoreResult */)
<-c
if e := stack.rpcConn.Request(id).Result.(*pb.SyscallResponse_Shutdown).Shutdown.ErrorNumber; e != 0 {
return syserr.FromHost(syscall.Errno(e))
}
return nil
}
// GetSockOpt implements socket.Socket.GetSockOpt.
func (s *socketOperations) GetSockOpt(t *kernel.Task, level int, name int, outLen int) (interface{}, *syserr.Error) {
stack := t.NetworkContext().(*Stack)
id, c := stack.rpcConn.NewRequest(pb.SyscallRequest{Args: &pb.SyscallRequest_GetSockOpt{&pb.GetSockOptRequest{Fd: s.fd, Level: int64(level), Name: int64(name), Length: uint32(outLen)}}}, false /* ignoreResult */)
<-c
res := stack.rpcConn.Request(id).Result.(*pb.SyscallResponse_GetSockOpt).GetSockOpt.Result
if e, ok := res.(*pb.GetSockOptResponse_ErrorNumber); ok {
return nil, syserr.FromHost(syscall.Errno(e.ErrorNumber))
}
return res.(*pb.GetSockOptResponse_Opt).Opt, nil
}
// SetSockOpt implements socket.Socket.SetSockOpt.
func (s *socketOperations) SetSockOpt(t *kernel.Task, level int, name int, opt []byte) *syserr.Error {
stack := t.NetworkContext().(*Stack)
id, c := stack.rpcConn.NewRequest(pb.SyscallRequest{Args: &pb.SyscallRequest_SetSockOpt{&pb.SetSockOptRequest{Fd: s.fd, Level: int64(level), Name: int64(name), Opt: opt}}}, false /* ignoreResult */)
<-c
if e := stack.rpcConn.Request(id).Result.(*pb.SyscallResponse_SetSockOpt).SetSockOpt.ErrorNumber; e != 0 {
syserr.FromHost(syscall.Errno(e))
}
return nil
}
// GetPeerName implements socket.Socket.GetPeerName.
func (s *socketOperations) GetPeerName(t *kernel.Task) (interface{}, uint32, *syserr.Error) {
stack := t.NetworkContext().(*Stack)
id, c := stack.rpcConn.NewRequest(pb.SyscallRequest{Args: &pb.SyscallRequest_GetPeerName{&pb.GetPeerNameRequest{Fd: s.fd}}}, false /* ignoreResult */)
<-c
res := stack.rpcConn.Request(id).Result.(*pb.SyscallResponse_GetPeerName).GetPeerName.Result
if e, ok := res.(*pb.GetPeerNameResponse_ErrorNumber); ok {
return nil, 0, syserr.FromHost(syscall.Errno(e.ErrorNumber))
}
addr := res.(*pb.GetPeerNameResponse_Address).Address
return addr.Address, addr.Length, nil
}
// GetSockName implements socket.Socket.GetSockName.
func (s *socketOperations) GetSockName(t *kernel.Task) (interface{}, uint32, *syserr.Error) {
stack := t.NetworkContext().(*Stack)
id, c := stack.rpcConn.NewRequest(pb.SyscallRequest{Args: &pb.SyscallRequest_GetSockName{&pb.GetSockNameRequest{Fd: s.fd}}}, false /* ignoreResult */)
<-c
res := stack.rpcConn.Request(id).Result.(*pb.SyscallResponse_GetSockName).GetSockName.Result
if e, ok := res.(*pb.GetSockNameResponse_ErrorNumber); ok {
return nil, 0, syserr.FromHost(syscall.Errno(e.ErrorNumber))
}
addr := res.(*pb.GetSockNameResponse_Address).Address
return addr.Address, addr.Length, nil
}
func rpcIoctl(t *kernel.Task, fd, cmd uint32, arg []byte) ([]byte, error) {
stack := t.NetworkContext().(*Stack)
id, c := stack.rpcConn.NewRequest(pb.SyscallRequest{Args: &pb.SyscallRequest_Ioctl{&pb.IOCtlRequest{Fd: fd, Cmd: cmd, Arg: arg}}}, false /* ignoreResult */)
<-c
res := stack.rpcConn.Request(id).Result.(*pb.SyscallResponse_Ioctl).Ioctl.Result
if e, ok := res.(*pb.IOCtlResponse_ErrorNumber); ok {
return nil, syscall.Errno(e.ErrorNumber)
}
return res.(*pb.IOCtlResponse_Value).Value, nil
}
// Ioctl implements fs.FileOperations.Ioctl.
func (s *socketOperations) Ioctl(ctx context.Context, io usermem.IO, args arch.SyscallArguments) (uintptr, error) {
t := ctx.(*kernel.Task)
cmd := uint32(args[1].Int())
arg := args[2].Pointer()
var buf []byte
switch cmd {
// The following ioctls take 4 byte argument parameters.
case syscall.TIOCINQ,
syscall.TIOCOUTQ:
buf = make([]byte, 4)
// The following ioctls have args which are sizeof(struct ifreq).
case syscall.SIOCGIFADDR,
syscall.SIOCGIFBRDADDR,
syscall.SIOCGIFDSTADDR,
syscall.SIOCGIFFLAGS,
syscall.SIOCGIFHWADDR,
syscall.SIOCGIFINDEX,
syscall.SIOCGIFMAP,
syscall.SIOCGIFMETRIC,
syscall.SIOCGIFMTU,
syscall.SIOCGIFNAME,
syscall.SIOCGIFNETMASK,
syscall.SIOCGIFTXQLEN:
buf = make([]byte, sizeOfIfReq)
default:
return 0, syserror.ENOTTY
}
_, err := io.CopyIn(ctx, arg, buf, usermem.IOOpts{
AddressSpaceActive: true,
})
if err != nil {
return 0, err
}
v, err := rpcIoctl(t, s.fd, cmd, buf)
if err != nil {
return 0, err
}
if len(v) != len(buf) {
return 0, syserror.EINVAL
}
_, err = io.CopyOut(ctx, arg, v, usermem.IOOpts{
AddressSpaceActive: true,
})
return 0, err
}
func rpcRecvMsg(t *kernel.Task, req *pb.SyscallRequest_Recvmsg) (*pb.RecvmsgResponse_ResultPayload, *syserr.Error) {
s := t.NetworkContext().(*Stack)
id, c := s.rpcConn.NewRequest(pb.SyscallRequest{Args: req}, false /* ignoreResult */)
<-c
res := s.rpcConn.Request(id).Result.(*pb.SyscallResponse_Recvmsg).Recvmsg.Result
if e, ok := res.(*pb.RecvmsgResponse_ErrorNumber); ok {
return nil, syserr.FromHost(syscall.Errno(e.ErrorNumber))
}
return res.(*pb.RecvmsgResponse_Payload).Payload, nil
}
// RecvMsg implements socket.Socket.RecvMsg.
func (s *socketOperations) RecvMsg(t *kernel.Task, dst usermem.IOSequence, flags int, haveDeadline bool, deadline ktime.Time, senderRequested bool, controlDataLen uint64) (int, interface{}, uint32, socket.ControlMessages, *syserr.Error) {
req := &pb.SyscallRequest_Recvmsg{&pb.RecvmsgRequest{
Fd: s.fd,
Length: uint32(dst.NumBytes()),
Sender: senderRequested,
Trunc: flags&linux.MSG_TRUNC != 0,
Peek: flags&linux.MSG_PEEK != 0,
}}
res, err := rpcRecvMsg(t, req)
if err == nil {
var e error
var n int
if len(res.Data) > 0 {
n, e = dst.CopyOut(t, res.Data)
if e == nil && n != len(res.Data) {
panic("CopyOut failed to copy full buffer")
}
}
return int(res.Length), res.Address.GetAddress(), res.Address.GetLength(), socket.ControlMessages{}, syserr.FromError(e)
}
if err != syserr.ErrWouldBlock && err != syserr.ErrTryAgain || flags&linux.MSG_DONTWAIT != 0 {
return 0, nil, 0, socket.ControlMessages{}, err
}
// We'll have to block. Register for notifications and keep trying to
// send all the data.
e, ch := waiter.NewChannelEntry(nil)
s.EventRegister(&e, waiter.EventIn)
defer s.EventUnregister(&e)
for {
res, err := rpcRecvMsg(t, req)
if err == nil {
var e error
var n int
if len(res.Data) > 0 {
n, e = dst.CopyOut(t, res.Data)
if e == nil && n != len(res.Data) {
panic("CopyOut failed to copy full buffer")
}
}
return int(res.Length), res.Address.GetAddress(), res.Address.GetLength(), socket.ControlMessages{}, syserr.FromError(e)
}
if err != syserr.ErrWouldBlock && err != syserr.ErrTryAgain {
return 0, nil, 0, socket.ControlMessages{}, err
}
if err := t.BlockWithDeadline(ch, haveDeadline, deadline); err != nil {
if err == syserror.ETIMEDOUT {
return 0, nil, 0, socket.ControlMessages{}, syserr.ErrTryAgain
}
return 0, nil, 0, socket.ControlMessages{}, syserr.FromError(err)
}
}
}
func rpcSendMsg(t *kernel.Task, req *pb.SyscallRequest_Sendmsg) (uint32, *syserr.Error) {
s := t.NetworkContext().(*Stack)
id, c := s.rpcConn.NewRequest(pb.SyscallRequest{Args: req}, false /* ignoreResult */)
<-c
res := s.rpcConn.Request(id).Result.(*pb.SyscallResponse_Sendmsg).Sendmsg.Result
if e, ok := res.(*pb.SendmsgResponse_ErrorNumber); ok {
return 0, syserr.FromHost(syscall.Errno(e.ErrorNumber))
}
return res.(*pb.SendmsgResponse_Length).Length, nil
}
// SendMsg implements socket.Socket.SendMsg.
func (s *socketOperations) SendMsg(t *kernel.Task, src usermem.IOSequence, to []byte, flags int, controlMessages socket.ControlMessages) (int, *syserr.Error) {
// Whitelist flags.
if flags&^(syscall.MSG_DONTWAIT|syscall.MSG_EOR|syscall.MSG_FASTOPEN|syscall.MSG_MORE|syscall.MSG_NOSIGNAL) != 0 {
return 0, syserr.ErrInvalidArgument
}
// Reject Unix control messages.
if !controlMessages.Unix.Empty() {
return 0, syserr.ErrInvalidArgument
}
v := buffer.NewView(int(src.NumBytes()))
// Copy all the data into the buffer.
if _, err := src.CopyIn(t, v); err != nil {
return 0, syserr.FromError(err)
}
// TODO: this needs to change to map directly to a SendMsg syscall
// in the RPC.
req := &pb.SyscallRequest_Sendmsg{&pb.SendmsgRequest{
Fd: uint32(s.fd),
Data: v,
Address: to,
More: flags&linux.MSG_MORE != 0,
EndOfRecord: flags&linux.MSG_EOR != 0,
}}
n, err := rpcSendMsg(t, req)
if err != syserr.ErrWouldBlock && err != syserr.ErrTryAgain || flags&linux.MSG_DONTWAIT != 0 {
return int(n), err
}
// We'll have to block. Register for notification and keep trying to
// send all the data.
e, ch := waiter.NewChannelEntry(nil)
s.EventRegister(&e, waiter.EventOut)
defer s.EventUnregister(&e)
for {
n, err := rpcSendMsg(t, req)
if err != syserr.ErrWouldBlock && err != syserr.ErrTryAgain {
return int(n), err
}
if err := t.Block(ch); err != nil {
return 0, syserr.FromError(err)
}
}
}
type socketProvider struct {
family int
}
// Socket implements socket.Provider.Socket.
func (p *socketProvider) Socket(t *kernel.Task, stypeflags unix.SockType, protocol int) (*fs.File, *syserr.Error) {
// Check that we are using the RPC network stack.
stack := t.NetworkContext()
if stack == nil {
return nil, nil
}
s, ok := stack.(*Stack)
if !ok {
return nil, nil
}
// Only accept TCP and UDP.
//
// Try to restrict the flags we will accept to minimize backwards
// incompatibility with netstack.
stype := int(stypeflags) & linux.SOCK_TYPE_MASK
switch stype {
case syscall.SOCK_STREAM:
switch protocol {
case 0, syscall.IPPROTO_TCP:
// ok
default:
return nil, nil
}
case syscall.SOCK_DGRAM:
switch protocol {
case 0, syscall.IPPROTO_UDP:
// ok
default:
return nil, nil
}
default:
return nil, nil
}
return newSocketFile(t, s, p.family, stype, 0)
}
// Pair implements socket.Provider.Pair.
func (p *socketProvider) Pair(t *kernel.Task, stype unix.SockType, protocol int) (*fs.File, *fs.File, *syserr.Error) {
// Not supported by AF_INET/AF_INET6.
return nil, nil, nil
}
func init() {
for _, family := range []int{syscall.AF_INET, syscall.AF_INET6} {
socket.RegisterProvider(family, &socketProvider{family})
}
}
|