summaryrefslogtreecommitdiffhomepage
path: root/pkg/tcpip/link/sharedmem/sharedmem_unsafe.go
diff options
context:
space:
mode:
authorgVisor bot <gvisor-bot@google.com>2021-10-13 22:24:19 +0000
committergVisor bot <gvisor-bot@google.com>2021-10-13 22:24:19 +0000
commitcb091cfa76c99fea029ef56ddfaa89000ab7d174 (patch)
treeb4b8e4f35d17a782d8f9b804b994c612f2ae7cc7 /pkg/tcpip/link/sharedmem/sharedmem_unsafe.go
parentf29bd12489ad6463533f23dfa840b7c56047b25c (diff)
parent4e2cc2bef3220bb77a60b4a33fb02f491d5acf98 (diff)
Merge release-20210927.0-71-g4e2cc2bef (automated)
Diffstat (limited to 'pkg/tcpip/link/sharedmem/sharedmem_unsafe.go')
-rw-r--r--pkg/tcpip/link/sharedmem/sharedmem_unsafe.go33
1 files changed, 33 insertions, 0 deletions
diff --git a/pkg/tcpip/link/sharedmem/sharedmem_unsafe.go b/pkg/tcpip/link/sharedmem/sharedmem_unsafe.go
index f7e816a41..d974c266e 100644
--- a/pkg/tcpip/link/sharedmem/sharedmem_unsafe.go
+++ b/pkg/tcpip/link/sharedmem/sharedmem_unsafe.go
@@ -15,7 +15,12 @@
package sharedmem
import (
+ "fmt"
+ "reflect"
"unsafe"
+
+ "golang.org/x/sys/unix"
+ "gvisor.dev/gvisor/pkg/memutil"
)
// sharedDataPointer converts the shared data slice into a pointer so that it
@@ -23,3 +28,31 @@ import (
func sharedDataPointer(sharedData []byte) *uint32 {
return (*uint32)(unsafe.Pointer(&sharedData[0:4][0]))
}
+
+// getBuffer returns a memory region mapped to the full contents of the given
+// file descriptor.
+func getBuffer(fd int) ([]byte, error) {
+ var s unix.Stat_t
+ if err := unix.Fstat(fd, &s); err != nil {
+ return nil, err
+ }
+
+ // Check that size doesn't overflow an int.
+ if s.Size > int64(^uint(0)>>1) {
+ return nil, unix.EDOM
+ }
+
+ addr, err := memutil.MapFile(0 /* addr */, uintptr(s.Size), unix.PROT_READ|unix.PROT_WRITE, unix.MAP_SHARED|unix.MAP_FILE, uintptr(fd), 0 /*offset*/)
+ if err != nil {
+ return nil, fmt.Errorf("failed to map memory for buffer fd: %d, error: %s", fd, err)
+ }
+
+ // Use unsafe to conver addr into a []byte.
+ var b []byte
+ hdr := (*reflect.SliceHeader)(unsafe.Pointer(&b))
+ hdr.Data = addr
+ hdr.Len = int(s.Size)
+ hdr.Cap = int(s.Size)
+
+ return b, nil
+}