summaryrefslogtreecommitdiffhomepage
path: root/pkg/abi
diff options
context:
space:
mode:
authorAyush Ranjan <ayushranjan@google.com>2021-02-08 18:03:29 -0800
committergVisor bot <gvisor-bot@google.com>2021-02-08 18:08:29 -0800
commitcfa4633c3d206aa2f9abdaac60d053162244ee6d (patch)
treecddf8d20bde9b55bee4d510876b77af92dc635a3 /pkg/abi
parente51f775cbb8db89d1dac6dcf584be5eef1f82d3c (diff)
[go-marshal] Add dynamic tag in go_marshal.
This makes it easier to implement dynamically sized types in go-marshal. You really only need to implement MarshalBytes, UnmarshalBytes and SizeBytes to implement the entire interface. By using the `dynamic` tag, the autogenerator will generate the rest of the methods for us. This change also simplifies how KernelIPTGetEntries implements Marshallable using the newly added utility. PiperOrigin-RevId: 356397114
Diffstat (limited to 'pkg/abi')
-rw-r--r--pkg/abi/linux/BUILD1
-rw-r--r--pkg/abi/linux/netfilter.go73
-rw-r--r--pkg/abi/linux/netfilter_ipv6.go67
3 files changed, 10 insertions, 131 deletions
diff --git a/pkg/abi/linux/BUILD b/pkg/abi/linux/BUILD
index 8fa61d6f7..ecaeb11ac 100644
--- a/pkg/abi/linux/BUILD
+++ b/pkg/abi/linux/BUILD
@@ -80,7 +80,6 @@ go_library(
"//pkg/bits",
"//pkg/marshal",
"//pkg/marshal/primitive",
- "//pkg/usermem",
],
)
diff --git a/pkg/abi/linux/netfilter.go b/pkg/abi/linux/netfilter.go
index b521144d9..378f1baf3 100644
--- a/pkg/abi/linux/netfilter.go
+++ b/pkg/abi/linux/netfilter.go
@@ -15,11 +15,8 @@
package linux
import (
- "io"
-
"gvisor.dev/gvisor/pkg/marshal"
"gvisor.dev/gvisor/pkg/marshal/primitive"
- "gvisor.dev/gvisor/pkg/usermem"
)
// This file contains structures required to support netfilter, specifically
@@ -129,8 +126,8 @@ type IPTEntry struct {
const SizeOfIPTEntry = 112
// KernelIPTEntry is identical to IPTEntry, but includes the Elems field.
-// KernelIPTEntry itself is not Marshallable but it implements some methods of
-// marshal.Marshallable that help in other implementations of Marshallable.
+//
+// +marshal dynamic
type KernelIPTEntry struct {
Entry IPTEntry
@@ -158,6 +155,8 @@ func (ke *KernelIPTEntry) UnmarshalBytes(src []byte) {
ke.Elems.UnmarshalBytes(src[ke.Entry.SizeBytes():])
}
+var _ marshal.Marshallable = (*KernelIPTEntry)(nil)
+
// IPTIP contains information for matching a packet's IP header.
// It corresponds to struct ipt_ip in
// include/uapi/linux/netfilter_ipv4/ip_tables.h.
@@ -411,8 +410,9 @@ type IPTGetEntries struct {
const SizeOfIPTGetEntries = 40
// KernelIPTGetEntries is identical to IPTGetEntries, but includes the
-// Entrytable field. This has been manually made marshal.Marshallable since it
-// is dynamically sized.
+// Entrytable field.
+//
+// +marshal dynamic
type KernelIPTGetEntries struct {
IPTGetEntries
Entrytable []KernelIPTEntry
@@ -447,65 +447,6 @@ func (ke *KernelIPTGetEntries) UnmarshalBytes(src []byte) {
}
}
-// Packed implements marshal.Marshallable.Packed.
-func (ke *KernelIPTGetEntries) Packed() bool {
- // KernelIPTGetEntries isn't packed because the ke.Entrytable contains an
- // indirection to the actual data we want to marshal (the slice data
- // pointer), and the memory for KernelIPTGetEntries contains the slice
- // header which we don't want to marshal.
- return false
-}
-
-// MarshalUnsafe implements marshal.Marshallable.MarshalUnsafe.
-func (ke *KernelIPTGetEntries) MarshalUnsafe(dst []byte) {
- // Fall back to safe Marshal because the type in not packed.
- ke.MarshalBytes(dst)
-}
-
-// UnmarshalUnsafe implements marshal.Marshallable.UnmarshalUnsafe.
-func (ke *KernelIPTGetEntries) UnmarshalUnsafe(src []byte) {
- // Fall back to safe Unmarshal because the type in not packed.
- ke.UnmarshalBytes(src)
-}
-
-// CopyIn implements marshal.Marshallable.CopyIn.
-func (ke *KernelIPTGetEntries) CopyIn(cc marshal.CopyContext, addr usermem.Addr) (int, error) {
- buf := cc.CopyScratchBuffer(ke.SizeBytes()) // escapes: okay.
- length, err := cc.CopyInBytes(addr, buf) // escapes: okay.
- // Unmarshal unconditionally. If we had a short copy-in, this results in a
- // partially unmarshalled struct.
- ke.UnmarshalBytes(buf) // escapes: fallback.
- return length, err
-}
-
-// CopyOut implements marshal.Marshallable.CopyOut.
-func (ke *KernelIPTGetEntries) CopyOut(cc marshal.CopyContext, addr usermem.Addr) (int, error) {
- // Type KernelIPTGetEntries doesn't have a packed layout in memory, fall
- // back to MarshalBytes.
- return cc.CopyOutBytes(addr, ke.marshalAll(cc))
-}
-
-// CopyOutN implements marshal.Marshallable.CopyOutN.
-func (ke *KernelIPTGetEntries) CopyOutN(cc marshal.CopyContext, addr usermem.Addr, limit int) (int, error) {
- // Type KernelIPTGetEntries doesn't have a packed layout in memory, fall
- // back to MarshalBytes.
- return cc.CopyOutBytes(addr, ke.marshalAll(cc)[:limit])
-}
-
-func (ke *KernelIPTGetEntries) marshalAll(cc marshal.CopyContext) []byte {
- buf := cc.CopyScratchBuffer(ke.SizeBytes())
- ke.MarshalBytes(buf)
- return buf
-}
-
-// WriteTo implements io.WriterTo.WriteTo.
-func (ke *KernelIPTGetEntries) WriteTo(w io.Writer) (int64, error) {
- buf := make([]byte, ke.SizeBytes())
- ke.MarshalBytes(buf)
- length, err := w.Write(buf)
- return int64(length), err
-}
-
var _ marshal.Marshallable = (*KernelIPTGetEntries)(nil)
// IPTReplace is the argument for the IPT_SO_SET_REPLACE sockopt. It
diff --git a/pkg/abi/linux/netfilter_ipv6.go b/pkg/abi/linux/netfilter_ipv6.go
index bcb57642e..b953e62dc 100644
--- a/pkg/abi/linux/netfilter_ipv6.go
+++ b/pkg/abi/linux/netfilter_ipv6.go
@@ -15,11 +15,8 @@
package linux
import (
- "io"
-
"gvisor.dev/gvisor/pkg/marshal"
"gvisor.dev/gvisor/pkg/marshal/primitive"
- "gvisor.dev/gvisor/pkg/usermem"
)
// This file contains structures required to support IPv6 netfilter and
@@ -70,8 +67,9 @@ type IP6TReplace struct {
const SizeOfIP6TReplace = 96
// KernelIP6TGetEntries is identical to IP6TGetEntries, but includes the
-// Entrytable field. This has been manually made marshal.Marshallable since it
-// is dynamically sized.
+// Entrytable field.
+//
+// +marshal dynamic
type KernelIP6TGetEntries struct {
IPTGetEntries
Entrytable []KernelIP6TEntry
@@ -106,65 +104,6 @@ func (ke *KernelIP6TGetEntries) UnmarshalBytes(src []byte) {
}
}
-// Packed implements marshal.Marshallable.Packed.
-func (ke *KernelIP6TGetEntries) Packed() bool {
- // KernelIP6TGetEntries isn't packed because the ke.Entrytable contains
- // an indirection to the actual data we want to marshal (the slice data
- // pointer), and the memory for KernelIP6TGetEntries contains the slice
- // header which we don't want to marshal.
- return false
-}
-
-// MarshalUnsafe implements marshal.Marshallable.MarshalUnsafe.
-func (ke *KernelIP6TGetEntries) MarshalUnsafe(dst []byte) {
- // Fall back to safe Marshal because the type in not packed.
- ke.MarshalBytes(dst)
-}
-
-// UnmarshalUnsafe implements marshal.Marshallable.UnmarshalUnsafe.
-func (ke *KernelIP6TGetEntries) UnmarshalUnsafe(src []byte) {
- // Fall back to safe Unmarshal because the type in not packed.
- ke.UnmarshalBytes(src)
-}
-
-// CopyIn implements marshal.Marshallable.CopyIn.
-func (ke *KernelIP6TGetEntries) CopyIn(cc marshal.CopyContext, addr usermem.Addr) (int, error) {
- buf := cc.CopyScratchBuffer(ke.SizeBytes()) // escapes: okay.
- length, err := cc.CopyInBytes(addr, buf) // escapes: okay.
- // Unmarshal unconditionally. If we had a short copy-in, this results
- // in a partially unmarshalled struct.
- ke.UnmarshalBytes(buf) // escapes: fallback.
- return length, err
-}
-
-// CopyOut implements marshal.Marshallable.CopyOut.
-func (ke *KernelIP6TGetEntries) CopyOut(cc marshal.CopyContext, addr usermem.Addr) (int, error) {
- // Type KernelIP6TGetEntries doesn't have a packed layout in memory,
- // fall back to MarshalBytes.
- return cc.CopyOutBytes(addr, ke.marshalAll(cc))
-}
-
-// CopyOutN implements marshal.Marshallable.CopyOutN.
-func (ke *KernelIP6TGetEntries) CopyOutN(cc marshal.CopyContext, addr usermem.Addr, limit int) (int, error) {
- // Type KernelIP6TGetEntries doesn't have a packed layout in memory, fall
- // back to MarshalBytes.
- return cc.CopyOutBytes(addr, ke.marshalAll(cc)[:limit])
-}
-
-func (ke *KernelIP6TGetEntries) marshalAll(cc marshal.CopyContext) []byte {
- buf := cc.CopyScratchBuffer(ke.SizeBytes())
- ke.MarshalBytes(buf)
- return buf
-}
-
-// WriteTo implements io.WriterTo.WriteTo.
-func (ke *KernelIP6TGetEntries) WriteTo(w io.Writer) (int64, error) {
- buf := make([]byte, ke.SizeBytes())
- ke.MarshalBytes(buf)
- length, err := w.Write(buf)
- return int64(length), err
-}
-
var _ marshal.Marshallable = (*KernelIP6TGetEntries)(nil)
// IP6TEntry is an iptables rule. It corresponds to struct ip6t_entry in