summaryrefslogtreecommitdiffhomepage
path: root/test/packetimpact/testbench/dut.go
blob: 0cac0bf1b914a73c1df691e68c560bebd6a4823e (plain)
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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
// Copyright 2020 The gVisor Authors.
//
// 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 testbench

import (
	"context"
	"encoding/binary"
	"fmt"
	"net"
	"testing"
	"time"

	"golang.org/x/sys/unix"
	"google.golang.org/grpc"
	"google.golang.org/grpc/keepalive"
	"gvisor.dev/gvisor/pkg/abi/linux"
	bin "gvisor.dev/gvisor/pkg/binary"
	"gvisor.dev/gvisor/pkg/hostarch"
	pb "gvisor.dev/gvisor/test/packetimpact/proto/posix_server_go_proto"
)

// DUT communicates with the DUT to force it to make POSIX calls.
type DUT struct {
	conn        *grpc.ClientConn
	posixServer POSIXClient
	Net         *DUTTestNet
	Uname       *DUTUname
}

// NewDUT creates a new connection with the DUT over gRPC.
func NewDUT(t *testing.T) DUT {
	t.Helper()
	info := getDUTInfo()
	dut := info.ConnectToDUT(t)
	t.Cleanup(func() {
		dut.TearDownConnection()
		info.release()
	})
	return dut
}

// ConnectToDUT connects to DUT through gRPC.
func (info *DUTInfo) ConnectToDUT(t *testing.T) DUT {
	t.Helper()

	n := info.Net
	posixServerAddress := net.JoinHostPort(n.POSIXServerIP.String(), fmt.Sprintf("%d", n.POSIXServerPort))
	conn, err := grpc.Dial(posixServerAddress, grpc.WithInsecure(), grpc.WithKeepaliveParams(keepalive.ClientParameters{Timeout: RPCKeepalive}))
	if err != nil {
		t.Fatalf("failed to grpc.Dial(%s): %s", posixServerAddress, err)
	}
	posixServer := NewPOSIXClient(conn)
	return DUT{
		conn:        conn,
		posixServer: posixServer,
		Net:         n,
		Uname:       info.Uname,
	}
}

// TearDownConnection closes the underlying connection.
func (dut *DUT) TearDownConnection() {
	dut.conn.Close()
}

func (dut *DUT) sockaddrToProto(t *testing.T, sa unix.Sockaddr) *pb.Sockaddr {
	t.Helper()

	switch s := sa.(type) {
	case *unix.SockaddrInet4:
		return &pb.Sockaddr{
			Sockaddr: &pb.Sockaddr_In{
				In: &pb.SockaddrIn{
					Family: unix.AF_INET,
					Port:   uint32(s.Port),
					Addr:   s.Addr[:],
				},
			},
		}
	case *unix.SockaddrInet6:
		return &pb.Sockaddr{
			Sockaddr: &pb.Sockaddr_In6{
				In6: &pb.SockaddrIn6{
					Family:   unix.AF_INET6,
					Port:     uint32(s.Port),
					Flowinfo: 0,
					ScopeId:  s.ZoneId,
					Addr:     s.Addr[:],
				},
			},
		}
	}
	t.Fatalf("can't parse Sockaddr struct: %+v", sa)
	return nil
}

func (dut *DUT) protoToSockaddr(t *testing.T, sa *pb.Sockaddr) unix.Sockaddr {
	t.Helper()

	switch s := sa.Sockaddr.(type) {
	case *pb.Sockaddr_In:
		ret := unix.SockaddrInet4{
			Port: int(s.In.GetPort()),
		}
		copy(ret.Addr[:], s.In.GetAddr())
		return &ret
	case *pb.Sockaddr_In6:
		ret := unix.SockaddrInet6{
			Port:   int(s.In6.GetPort()),
			ZoneId: s.In6.GetScopeId(),
		}
		copy(ret.Addr[:], s.In6.GetAddr())
		return &ret
	}
	t.Fatalf("can't parse Sockaddr proto: %#v", sa)
	return nil
}

// CreateBoundSocket makes a new socket on the DUT, with type typ and protocol
// proto, and bound to the IP address addr. Returns the new file descriptor and
// the port that was selected on the DUT.
func (dut *DUT) CreateBoundSocket(t *testing.T, typ, proto int32, addr net.IP) (int32, uint16) {
	t.Helper()

	var fd int32
	if addr.To4() != nil {
		fd = dut.Socket(t, unix.AF_INET, typ, proto)
		sa := unix.SockaddrInet4{}
		copy(sa.Addr[:], addr.To4())
		dut.Bind(t, fd, &sa)
	} else if addr.To16() != nil {
		fd = dut.Socket(t, unix.AF_INET6, typ, proto)
		sa := unix.SockaddrInet6{}
		copy(sa.Addr[:], addr.To16())
		sa.ZoneId = dut.Net.RemoteDevID
		dut.Bind(t, fd, &sa)
	} else {
		t.Fatalf("invalid IP address: %s", addr)
	}
	sa := dut.GetSockName(t, fd)
	var port int
	switch s := sa.(type) {
	case *unix.SockaddrInet4:
		port = s.Port
	case *unix.SockaddrInet6:
		port = s.Port
	default:
		t.Fatalf("unknown sockaddr type from getsockname: %T", sa)
	}
	return fd, uint16(port)
}

// CreateListener makes a new TCP connection. If it fails, the test ends.
func (dut *DUT) CreateListener(t *testing.T, typ, proto, backlog int32) (int32, uint16) {
	t.Helper()

	fd, remotePort := dut.CreateBoundSocket(t, typ, proto, dut.Net.RemoteIPv4)
	dut.Listen(t, fd, backlog)
	return fd, remotePort
}

// All the functions that make gRPC calls to the POSIX service are below, sorted
// alphabetically.

// Accept calls accept on the DUT and causes a fatal test failure if it doesn't
// succeed. If more control over the timeout or error handling is needed, use
// AcceptWithErrno.
func (dut *DUT) Accept(t *testing.T, sockfd int32) (int32, unix.Sockaddr) {
	t.Helper()

	ctx, cancel := context.WithTimeout(context.Background(), RPCTimeout)
	defer cancel()
	fd, sa, err := dut.AcceptWithErrno(ctx, t, sockfd)
	if fd < 0 {
		t.Fatalf("failed to accept: %s", err)
	}
	return fd, sa
}

// AcceptWithErrno calls accept on the DUT.
func (dut *DUT) AcceptWithErrno(ctx context.Context, t *testing.T, sockfd int32) (int32, unix.Sockaddr, error) {
	t.Helper()

	req := &pb.AcceptRequest{
		Sockfd: sockfd,
	}
	resp, err := dut.posixServer.Accept(ctx, req)
	if err != nil {
		t.Fatalf("failed to call Accept: %s", err)
	}
	return resp.GetFd(), dut.protoToSockaddr(t, resp.GetAddr()), unix.Errno(resp.GetErrno_())
}

// Bind calls bind on the DUT and causes a fatal test failure if it doesn't
// succeed. If more control over the timeout or error handling is
// needed, use BindWithErrno.
func (dut *DUT) Bind(t *testing.T, fd int32, sa unix.Sockaddr) {
	t.Helper()

	ctx, cancel := context.WithTimeout(context.Background(), RPCTimeout)
	defer cancel()
	ret, err := dut.BindWithErrno(ctx, t, fd, sa)
	if ret != 0 {
		t.Fatalf("failed to bind socket: %s", err)
	}
}

// BindWithErrno calls bind on the DUT.
func (dut *DUT) BindWithErrno(ctx context.Context, t *testing.T, fd int32, sa unix.Sockaddr) (int32, error) {
	t.Helper()

	req := &pb.BindRequest{
		Sockfd: fd,
		Addr:   dut.sockaddrToProto(t, sa),
	}
	resp, err := dut.posixServer.Bind(ctx, req)
	if err != nil {
		t.Fatalf("failed to call Bind: %s", err)
	}
	return resp.GetRet(), unix.Errno(resp.GetErrno_())
}

// Close calls close on the DUT and causes a fatal test failure if it doesn't
// succeed. If more control over the timeout or error handling is needed, use
// CloseWithErrno.
func (dut *DUT) Close(t *testing.T, fd int32) {
	t.Helper()

	ctx, cancel := context.WithTimeout(context.Background(), RPCTimeout)
	defer cancel()
	ret, err := dut.CloseWithErrno(ctx, t, fd)
	if ret != 0 {
		t.Fatalf("failed to close: %s", err)
	}
}

// CloseWithErrno calls close on the DUT.
func (dut *DUT) CloseWithErrno(ctx context.Context, t *testing.T, fd int32) (int32, error) {
	t.Helper()

	req := &pb.CloseRequest{
		Fd: fd,
	}
	resp, err := dut.posixServer.Close(ctx, req)
	if err != nil {
		t.Fatalf("failed to call Close: %s", err)
	}
	return resp.GetRet(), unix.Errno(resp.GetErrno_())
}

// Connect calls connect on the DUT and causes a fatal test failure if it
// doesn't succeed. If more control over the timeout or error handling is
// needed, use ConnectWithErrno.
func (dut *DUT) Connect(t *testing.T, fd int32, sa unix.Sockaddr) {
	t.Helper()

	ctx, cancel := context.WithTimeout(context.Background(), RPCTimeout)
	defer cancel()
	ret, err := dut.ConnectWithErrno(ctx, t, fd, sa)
	// Ignore 'operation in progress' error that can be returned when the socket
	// is non-blocking.
	if err != unix.EINPROGRESS && ret != 0 {
		t.Fatalf("failed to connect socket: %s", err)
	}
}

// ConnectWithErrno calls bind on the DUT.
func (dut *DUT) ConnectWithErrno(ctx context.Context, t *testing.T, fd int32, sa unix.Sockaddr) (int32, error) {
	t.Helper()

	req := &pb.ConnectRequest{
		Sockfd: fd,
		Addr:   dut.sockaddrToProto(t, sa),
	}
	resp, err := dut.posixServer.Connect(ctx, req)
	if err != nil {
		t.Fatalf("failed to call Connect: %s", err)
	}
	return resp.GetRet(), unix.Errno(resp.GetErrno_())
}

// GetSockName calls getsockname on the DUT and causes a fatal test failure if
// it doesn't succeed. If more control over the timeout or error handling is
// needed, use GetSockNameWithErrno.
func (dut *DUT) GetSockName(t *testing.T, sockfd int32) unix.Sockaddr {
	t.Helper()

	ctx, cancel := context.WithTimeout(context.Background(), RPCTimeout)
	defer cancel()
	ret, sa, err := dut.GetSockNameWithErrno(ctx, t, sockfd)
	if ret != 0 {
		t.Fatalf("failed to getsockname: %s", err)
	}
	return sa
}

// GetSockNameWithErrno calls getsockname on the DUT.
func (dut *DUT) GetSockNameWithErrno(ctx context.Context, t *testing.T, sockfd int32) (int32, unix.Sockaddr, error) {
	t.Helper()

	req := &pb.GetSockNameRequest{
		Sockfd: sockfd,
	}
	resp, err := dut.posixServer.GetSockName(ctx, req)
	if err != nil {
		t.Fatalf("failed to call Bind: %s", err)
	}
	return resp.GetRet(), dut.protoToSockaddr(t, resp.GetAddr()), unix.Errno(resp.GetErrno_())
}

func (dut *DUT) getSockOpt(ctx context.Context, t *testing.T, sockfd, level, optname, optlen int32, typ pb.GetSockOptRequest_SockOptType) (int32, *pb.SockOptVal, error) {
	t.Helper()

	req := &pb.GetSockOptRequest{
		Sockfd:  sockfd,
		Level:   level,
		Optname: optname,
		Optlen:  optlen,
		Type:    typ,
	}
	resp, err := dut.posixServer.GetSockOpt(ctx, req)
	if err != nil {
		t.Fatalf("failed to call GetSockOpt: %s", err)
	}
	optval := resp.GetOptval()
	if optval == nil {
		t.Fatalf("GetSockOpt response does not contain a value")
	}
	return resp.GetRet(), optval, unix.Errno(resp.GetErrno_())
}

// GetSockOpt calls getsockopt on the DUT and causes a fatal test failure if it
// doesn't succeed. If more control over the timeout or error handling is
// needed, use GetSockOptWithErrno. Because endianess and the width of values
// might differ between the testbench and DUT architectures, prefer to use a
// more specific GetSockOptXxx function.
func (dut *DUT) GetSockOpt(t *testing.T, sockfd, level, optname, optlen int32) []byte {
	t.Helper()

	ctx, cancel := context.WithTimeout(context.Background(), RPCTimeout)
	defer cancel()
	ret, optval, err := dut.GetSockOptWithErrno(ctx, t, sockfd, level, optname, optlen)
	if ret != 0 {
		t.Fatalf("failed to GetSockOpt: %s", err)
	}
	return optval
}

// GetSockOptWithErrno calls getsockopt on the DUT. Because endianess and the
// width of values might differ between the testbench and DUT architectures,
// prefer to use a more specific GetSockOptXxxWithErrno function.
func (dut *DUT) GetSockOptWithErrno(ctx context.Context, t *testing.T, sockfd, level, optname, optlen int32) (int32, []byte, error) {
	t.Helper()

	ret, optval, errno := dut.getSockOpt(ctx, t, sockfd, level, optname, optlen, pb.GetSockOptRequest_BYTES)
	bytesval, ok := optval.Val.(*pb.SockOptVal_Bytesval)
	if !ok {
		t.Fatalf("GetSockOpt got value type: %T, want bytes", optval.Val)
	}
	return ret, bytesval.Bytesval, errno
}

// GetSockOptInt calls getsockopt on the DUT and causes a fatal test failure
// if it doesn't succeed. If more control over the int optval or error handling
// is needed, use GetSockOptIntWithErrno.
func (dut *DUT) GetSockOptInt(t *testing.T, sockfd, level, optname int32) int32 {
	t.Helper()

	ctx, cancel := context.WithTimeout(context.Background(), RPCTimeout)
	defer cancel()
	ret, intval, err := dut.GetSockOptIntWithErrno(ctx, t, sockfd, level, optname)
	if ret != 0 {
		t.Fatalf("failed to GetSockOptInt: %s", err)
	}
	return intval
}

// GetSockOptIntWithErrno calls getsockopt with an integer optval.
func (dut *DUT) GetSockOptIntWithErrno(ctx context.Context, t *testing.T, sockfd, level, optname int32) (int32, int32, error) {
	t.Helper()

	ret, optval, errno := dut.getSockOpt(ctx, t, sockfd, level, optname, 0, pb.GetSockOptRequest_INT)
	intval, ok := optval.Val.(*pb.SockOptVal_Intval)
	if !ok {
		t.Fatalf("GetSockOpt got value type: %T, want int", optval.Val)
	}
	return ret, intval.Intval, errno
}

// GetSockOptTimeval calls getsockopt on the DUT and causes a fatal test failure
// if it doesn't succeed. If more control over the timeout or error handling is
// needed, use GetSockOptTimevalWithErrno.
func (dut *DUT) GetSockOptTimeval(t *testing.T, sockfd, level, optname int32) unix.Timeval {
	t.Helper()

	ctx, cancel := context.WithTimeout(context.Background(), RPCTimeout)
	defer cancel()
	ret, timeval, err := dut.GetSockOptTimevalWithErrno(ctx, t, sockfd, level, optname)
	if ret != 0 {
		t.Fatalf("failed to GetSockOptTimeval: %s", err)
	}
	return timeval
}

// GetSockOptTimevalWithErrno calls getsockopt and returns a timeval.
func (dut *DUT) GetSockOptTimevalWithErrno(ctx context.Context, t *testing.T, sockfd, level, optname int32) (int32, unix.Timeval, error) {
	t.Helper()

	ret, optval, errno := dut.getSockOpt(ctx, t, sockfd, level, optname, 0, pb.GetSockOptRequest_TIME)
	tv, ok := optval.Val.(*pb.SockOptVal_Timeval)
	if !ok {
		t.Fatalf("GetSockOpt got value type: %T, want timeval", optval.Val)
	}
	timeval := unix.Timeval{
		Sec:  tv.Timeval.Seconds,
		Usec: tv.Timeval.Microseconds,
	}
	return ret, timeval, errno
}

// GetSockOptTCPInfo retreives TCPInfo for the given socket descriptor.
func (dut *DUT) GetSockOptTCPInfo(t *testing.T, sockfd int32) linux.TCPInfo {
	t.Helper()

	ctx, cancel := context.WithTimeout(context.Background(), RPCTimeout)
	defer cancel()
	ret, info, err := dut.GetSockOptTCPInfoWithErrno(ctx, t, sockfd)
	if ret != 0 || err != unix.Errno(0) {
		t.Fatalf("failed to GetSockOptTCPInfo: %s", err)
	}
	return info
}

// GetSockOptTCPInfoWithErrno retreives TCPInfo with any errno.
func (dut *DUT) GetSockOptTCPInfoWithErrno(ctx context.Context, t *testing.T, sockfd int32) (int32, linux.TCPInfo, error) {
	t.Helper()

	info := linux.TCPInfo{}
	ret, infoBytes, errno := dut.GetSockOptWithErrno(ctx, t, sockfd, unix.SOL_TCP, unix.TCP_INFO, int32(linux.SizeOfTCPInfo))
	if got, want := len(infoBytes), linux.SizeOfTCPInfo; got != want {
		t.Fatalf("expected %T, got %d bytes want %d bytes", info, got, want)
	}
	bin.Unmarshal(infoBytes, hostarch.ByteOrder, &info)

	return ret, info, errno
}

// Listen calls listen on the DUT and causes a fatal test failure if it doesn't
// succeed. If more control over the timeout or error handling is needed, use
// ListenWithErrno.
func (dut *DUT) Listen(t *testing.T, sockfd, backlog int32) {
	t.Helper()

	ctx, cancel := context.WithTimeout(context.Background(), RPCTimeout)
	defer cancel()
	ret, err := dut.ListenWithErrno(ctx, t, sockfd, backlog)
	if ret != 0 {
		t.Fatalf("failed to listen: %s", err)
	}
}

// ListenWithErrno calls listen on the DUT.
func (dut *DUT) ListenWithErrno(ctx context.Context, t *testing.T, sockfd, backlog int32) (int32, error) {
	t.Helper()

	req := &pb.ListenRequest{
		Sockfd:  sockfd,
		Backlog: backlog,
	}
	resp, err := dut.posixServer.Listen(ctx, req)
	if err != nil {
		t.Fatalf("failed to call Listen: %s", err)
	}
	return resp.GetRet(), unix.Errno(resp.GetErrno_())
}

// PollOne calls poll on the DUT and asserts that the expected event must be
// signaled on the given fd within the given timeout.
func (dut *DUT) PollOne(t *testing.T, fd int32, events int16, timeout time.Duration) {
	t.Helper()

	pfds := dut.Poll(t, []unix.PollFd{{Fd: fd, Events: events}}, timeout)
	if n := len(pfds); n != 1 {
		t.Fatalf("Poll returned %d ready file descriptors, expected 1", n)
	}
	if readyFd := pfds[0].Fd; readyFd != fd {
		t.Fatalf("Poll returned an fd %d that was not requested (%d)", readyFd, fd)
	}
	if got, want := pfds[0].Revents, int16(events); got&want != want {
		t.Fatalf("Poll returned events does not include all of the interested events, got: %#b, want: %#b", got, want)
	}
}

// Poll calls poll on the DUT and causes a fatal test failure if it doesn't
// succeed. If more control over error handling is needed, use PollWithErrno.
// Only pollfds with non-empty revents are returned, the only way to tie the
// response back to the original request is using the fd number.
func (dut *DUT) Poll(t *testing.T, pfds []unix.PollFd, timeout time.Duration) []unix.PollFd {
	t.Helper()

	ctx := context.Background()
	var cancel context.CancelFunc
	if timeout >= 0 {
		ctx, cancel = context.WithTimeout(ctx, timeout+RPCTimeout)
		defer cancel()
	}
	ret, result, err := dut.PollWithErrno(ctx, t, pfds, timeout)
	if ret < 0 {
		t.Fatalf("failed to poll: %s", err)
	}
	return result
}

// PollWithErrno calls poll on the DUT.
func (dut *DUT) PollWithErrno(ctx context.Context, t *testing.T, pfds []unix.PollFd, timeout time.Duration) (int32, []unix.PollFd, error) {
	t.Helper()

	req := &pb.PollRequest{
		TimeoutMillis: int32(timeout.Milliseconds()),
	}
	for _, pfd := range pfds {
		req.Pfds = append(req.Pfds, &pb.PollFd{
			Fd:     pfd.Fd,
			Events: uint32(pfd.Events),
		})
	}
	resp, err := dut.posixServer.Poll(ctx, req)
	if err != nil {
		t.Fatalf("failed to call Poll: %s", err)
	}
	if ret, npfds := resp.GetRet(), len(resp.GetPfds()); ret >= 0 && int(ret) != npfds {
		t.Fatalf("nonsensical poll response: ret(%d) != len(pfds)(%d)", ret, npfds)
	}
	var result []unix.PollFd
	for _, protoPfd := range resp.GetPfds() {
		result = append(result, unix.PollFd{
			Fd:      protoPfd.GetFd(),
			Revents: int16(protoPfd.GetEvents()),
		})
	}
	return resp.GetRet(), result, unix.Errno(resp.GetErrno_())
}

// Send calls send on the DUT and causes a fatal test failure if it doesn't
// succeed. If more control over the timeout or error handling is needed, use
// SendWithErrno.
func (dut *DUT) Send(t *testing.T, sockfd int32, buf []byte, flags int32) int32 {
	t.Helper()

	ctx, cancel := context.WithTimeout(context.Background(), RPCTimeout)
	defer cancel()
	ret, err := dut.SendWithErrno(ctx, t, sockfd, buf, flags)
	if ret == -1 {
		t.Fatalf("failed to send: %s", err)
	}
	return ret
}

// SendWithErrno calls send on the DUT.
func (dut *DUT) SendWithErrno(ctx context.Context, t *testing.T, sockfd int32, buf []byte, flags int32) (int32, error) {
	t.Helper()

	req := &pb.SendRequest{
		Sockfd: sockfd,
		Buf:    buf,
		Flags:  flags,
	}
	resp, err := dut.posixServer.Send(ctx, req)
	if err != nil {
		t.Fatalf("failed to call Send: %s", err)
	}
	return resp.GetRet(), unix.Errno(resp.GetErrno_())
}

// SendTo calls sendto on the DUT and causes a fatal test failure if it doesn't
// succeed. If more control over the timeout or error handling is needed, use
// SendToWithErrno.
func (dut *DUT) SendTo(t *testing.T, sockfd int32, buf []byte, flags int32, destAddr unix.Sockaddr) int32 {
	t.Helper()

	ctx, cancel := context.WithTimeout(context.Background(), RPCTimeout)
	defer cancel()
	ret, err := dut.SendToWithErrno(ctx, t, sockfd, buf, flags, destAddr)
	if ret == -1 {
		t.Fatalf("failed to sendto: %s", err)
	}
	return ret
}

// SendToWithErrno calls sendto on the DUT.
func (dut *DUT) SendToWithErrno(ctx context.Context, t *testing.T, sockfd int32, buf []byte, flags int32, destAddr unix.Sockaddr) (int32, error) {
	t.Helper()

	req := &pb.SendToRequest{
		Sockfd:   sockfd,
		Buf:      buf,
		Flags:    flags,
		DestAddr: dut.sockaddrToProto(t, destAddr),
	}
	resp, err := dut.posixServer.SendTo(ctx, req)
	if err != nil {
		t.Fatalf("failed to call SendTo: %s", err)
	}
	return resp.GetRet(), unix.Errno(resp.GetErrno_())
}

// SetNonBlocking will set O_NONBLOCK flag for fd if nonblocking
// is true, otherwise it will clear the flag.
func (dut *DUT) SetNonBlocking(t *testing.T, fd int32, nonblocking bool) {
	t.Helper()

	req := &pb.SetNonblockingRequest{
		Fd:          fd,
		Nonblocking: nonblocking,
	}
	ctx, cancel := context.WithTimeout(context.Background(), RPCTimeout)
	defer cancel()

	resp, err := dut.posixServer.SetNonblocking(ctx, req)
	if err != nil {
		t.Fatalf("failed to call SetNonblocking: %s", err)
	}
	if resp.GetRet() == -1 {
		t.Fatalf("fcntl(%d, %s) failed: %s", fd, resp.GetCmd(), unix.Errno(resp.GetErrno_()))
	}
}

func (dut *DUT) setSockOpt(ctx context.Context, t *testing.T, sockfd, level, optname int32, optval *pb.SockOptVal) (int32, error) {
	t.Helper()

	req := &pb.SetSockOptRequest{
		Sockfd:  sockfd,
		Level:   level,
		Optname: optname,
		Optval:  optval,
	}
	resp, err := dut.posixServer.SetSockOpt(ctx, req)
	if err != nil {
		t.Fatalf("failed to call SetSockOpt: %s", err)
	}
	return resp.GetRet(), unix.Errno(resp.GetErrno_())
}

// SetSockOpt calls setsockopt on the DUT and causes a fatal test failure if it
// doesn't succeed. If more control over the timeout or error handling is
// needed, use SetSockOptWithErrno. Because endianess and the width of values
// might differ between the testbench and DUT architectures, prefer to use a
// more specific SetSockOptXxx function.
func (dut *DUT) SetSockOpt(t *testing.T, sockfd, level, optname int32, optval []byte) {
	t.Helper()

	ctx, cancel := context.WithTimeout(context.Background(), RPCTimeout)
	defer cancel()
	ret, err := dut.SetSockOptWithErrno(ctx, t, sockfd, level, optname, optval)
	if ret != 0 {
		t.Fatalf("failed to SetSockOpt: %s", err)
	}
}

// SetSockOptWithErrno calls setsockopt on the DUT. Because endianess and the
// width of values might differ between the testbench and DUT architectures,
// prefer to use a more specific SetSockOptXxxWithErrno function.
func (dut *DUT) SetSockOptWithErrno(ctx context.Context, t *testing.T, sockfd, level, optname int32, optval []byte) (int32, error) {
	t.Helper()

	return dut.setSockOpt(ctx, t, sockfd, level, optname, &pb.SockOptVal{Val: &pb.SockOptVal_Bytesval{optval}})
}

// SetSockOptInt calls setsockopt on the DUT and causes a fatal test failure
// if it doesn't succeed. If more control over the int optval or error handling
// is needed, use SetSockOptIntWithErrno.
func (dut *DUT) SetSockOptInt(t *testing.T, sockfd, level, optname, optval int32) {
	t.Helper()

	ctx, cancel := context.WithTimeout(context.Background(), RPCTimeout)
	defer cancel()
	ret, err := dut.SetSockOptIntWithErrno(ctx, t, sockfd, level, optname, optval)
	if ret != 0 {
		t.Fatalf("failed to SetSockOptInt: %s", err)
	}
}

// SetSockOptIntWithErrno calls setsockopt with an integer optval.
func (dut *DUT) SetSockOptIntWithErrno(ctx context.Context, t *testing.T, sockfd, level, optname, optval int32) (int32, error) {
	t.Helper()

	return dut.setSockOpt(ctx, t, sockfd, level, optname, &pb.SockOptVal{Val: &pb.SockOptVal_Intval{optval}})
}

// SetSockOptTimeval calls setsockopt on the DUT and causes a fatal test failure
// if it doesn't succeed. If more control over the timeout or error handling is
// needed, use SetSockOptTimevalWithErrno.
func (dut *DUT) SetSockOptTimeval(t *testing.T, sockfd, level, optname int32, tv *unix.Timeval) {
	t.Helper()

	ctx, cancel := context.WithTimeout(context.Background(), RPCTimeout)
	defer cancel()
	ret, err := dut.SetSockOptTimevalWithErrno(ctx, t, sockfd, level, optname, tv)
	if ret != 0 {
		t.Fatalf("failed to SetSockOptTimeval: %s", err)
	}
}

// SetSockOptTimevalWithErrno calls setsockopt with the timeval converted to
// bytes.
func (dut *DUT) SetSockOptTimevalWithErrno(ctx context.Context, t *testing.T, sockfd, level, optname int32, tv *unix.Timeval) (int32, error) {
	t.Helper()

	timeval := pb.Timeval{
		Seconds:      int64(tv.Sec),
		Microseconds: int64(tv.Usec),
	}
	return dut.setSockOpt(ctx, t, sockfd, level, optname, &pb.SockOptVal{Val: &pb.SockOptVal_Timeval{&timeval}})
}

// Socket calls socket on the DUT and returns the file descriptor. If socket
// fails on the DUT, the test ends.
func (dut *DUT) Socket(t *testing.T, domain, typ, proto int32) int32 {
	t.Helper()

	fd, err := dut.SocketWithErrno(t, domain, typ, proto)
	if fd < 0 {
		t.Fatalf("failed to create socket: %s", err)
	}
	return fd
}

// SocketWithErrno calls socket on the DUT and returns the fd and errno.
func (dut *DUT) SocketWithErrno(t *testing.T, domain, typ, proto int32) (int32, error) {
	t.Helper()

	req := &pb.SocketRequest{
		Domain:   domain,
		Type:     typ,
		Protocol: proto,
	}
	ctx := context.Background()
	resp, err := dut.posixServer.Socket(ctx, req)
	if err != nil {
		t.Fatalf("failed to call Socket: %s", err)
	}
	return resp.GetFd(), unix.Errno(resp.GetErrno_())
}

// Recv calls recv on the DUT and causes a fatal test failure if it doesn't
// succeed. If more control over the timeout or error handling is needed, use
// RecvWithErrno.
func (dut *DUT) Recv(t *testing.T, sockfd, len, flags int32) []byte {
	t.Helper()

	ctx, cancel := context.WithTimeout(context.Background(), RPCTimeout)
	defer cancel()
	ret, buf, err := dut.RecvWithErrno(ctx, t, sockfd, len, flags)
	if ret == -1 {
		t.Fatalf("failed to recv: %s", err)
	}
	return buf
}

// RecvWithErrno calls recv on the DUT.
func (dut *DUT) RecvWithErrno(ctx context.Context, t *testing.T, sockfd, len, flags int32) (int32, []byte, error) {
	t.Helper()

	req := &pb.RecvRequest{
		Sockfd: sockfd,
		Len:    len,
		Flags:  flags,
	}
	resp, err := dut.posixServer.Recv(ctx, req)
	if err != nil {
		t.Fatalf("failed to call Recv: %s", err)
	}
	return resp.GetRet(), resp.GetBuf(), unix.Errno(resp.GetErrno_())
}

// SetSockLingerOption sets SO_LINGER socket option on the DUT.
func (dut *DUT) SetSockLingerOption(t *testing.T, sockfd int32, timeout time.Duration, enable bool) {
	var linger unix.Linger
	if enable {
		linger.Onoff = 1
	}
	linger.Linger = int32(timeout / time.Second)

	buf := make([]byte, 8)
	binary.LittleEndian.PutUint32(buf, uint32(linger.Onoff))
	binary.LittleEndian.PutUint32(buf[4:], uint32(linger.Linger))
	dut.SetSockOpt(t, sockfd, unix.SOL_SOCKET, unix.SO_LINGER, buf)
}

// Shutdown calls shutdown on the DUT and causes a fatal test failure if it
// doesn't succeed. If more control over the timeout or error handling is
// needed, use ShutdownWithErrno.
func (dut *DUT) Shutdown(t *testing.T, fd, how int32) {
	t.Helper()

	ctx, cancel := context.WithTimeout(context.Background(), RPCTimeout)
	defer cancel()
	ret, err := dut.ShutdownWithErrno(ctx, t, fd, how)
	if ret != 0 {
		t.Fatalf("failed to shutdown(%d, %d): %s", fd, how, err)
	}
}

// ShutdownWithErrno calls shutdown on the DUT.
func (dut *DUT) ShutdownWithErrno(ctx context.Context, t *testing.T, fd, how int32) (int32, error) {
	t.Helper()

	req := &pb.ShutdownRequest{
		Fd:  fd,
		How: how,
	}
	resp, err := dut.posixServer.Shutdown(ctx, req)
	if err != nil {
		t.Fatalf("failed to call Shutdown: %s", err)
	}
	if resp.GetErrno_() == 0 {
		return resp.GetRet(), nil
	}
	return resp.GetRet(), unix.Errno(resp.GetErrno_())
}