Skip to content

Commit 25b1d71

Browse files
manninglucasgvisor-bot
authored andcommitted
Add constants and types related to the implementation of PACKET_MMAP.
This is the first in a series of changes that implements PACKET_MMAP. PiperOrigin-RevId: 716014872
1 parent 689db80 commit 25b1d71

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

pkg/abi/linux/socket.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,72 @@ const (
144144
PACKET_OUTGOING = 4 // Outgoing of any type
145145
)
146146

147+
// Packet socket options from <linux/if_packet.h>
148+
const (
149+
PACKET_RX_RING = 5
150+
)
151+
152+
// Statuses for a frame in a packet_mmap ring buffer from <linux/if_packet.h>.
153+
const (
154+
TP_STATUS_KERNEL = 0
155+
TP_STATUS_USER = 0x1
156+
TP_STATUS_COPY = 0x2
157+
TP_STATUS_LOSING = 0x4
158+
TP_STATUS_CSUM_NOT_READY = 0x8
159+
TP_STATUS_VLAN_VALID = 0x10
160+
TP_STATUS_BLK_TMO = 0x20
161+
TP_STATUS_VLAN_TPID_VALID = 0x40
162+
TP_STATUS_CSUM_VALID = 0x80
163+
TP_STATUS_GSO_TCP = 0x100
164+
)
165+
166+
// TpacketReq is the request for a packet_mmap ring buffer from
167+
// <linux/if_packet.h>.
168+
//
169+
// +marshal
170+
type TpacketReq struct {
171+
TpBlockSize uint32
172+
TpBlockNr uint32
173+
TpFrameSize uint32
174+
TpFrameNr uint32
175+
}
176+
177+
// TpacketHdr is the header for a frame in a packet_mmap ring buffer from
178+
// <linux/if_packet.h>.
179+
//
180+
// +marshal
181+
type TpacketHdr struct {
182+
TpStatus uint64
183+
TpLen uint32
184+
TpSnaplen uint32
185+
TpMac uint16
186+
TpNet uint16
187+
TpSec uint32
188+
TpUsec uint32 `marshal:"unaligned"`
189+
}
190+
191+
// TpacketAlignment is the alignment of a frame in a packet_mmap ring buffer
192+
// from <linux/if_packet.h>.
193+
const (
194+
TPACKET_ALIGNMENT = 16
195+
)
196+
197+
// TPACKET_V1 is the version of a packet_mmap ring buffer from
198+
// <linux/if_packet.h> that is implemented in gVisor.
199+
const (
200+
TPACKET_V1 = iota
201+
)
202+
203+
// TPACKET_HDRLEN is the length of a TpacketHdr from <linux/if_packet.h>.
204+
var (
205+
TPACKET_HDRLEN = TPacketAlign(uint32((*TpacketHdr)(nil).SizeBytes()) + uint32((*SockAddrLink)(nil).SizeBytes()))
206+
)
207+
208+
// TPacketAlign aligns a value to the alignment of a TPacket.
209+
func TPacketAlign(x uint32) uint32 {
210+
return (x + TPACKET_ALIGNMENT - 1) &^ (TPACKET_ALIGNMENT - 1)
211+
}
212+
147213
// Socket options from socket.h.
148214
const (
149215
SO_DEBUG = 1

pkg/syserr/netstack.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ var (
5555
ErrMulticastInputCannotBeOutput = New((&tcpip.ErrMulticastInputCannotBeOutput{}).String(), errno.EINVAL)
5656
ErrMissingRequiredFields = New((&tcpip.ErrMissingRequiredFields{}).String(), errno.EINVAL)
5757
ErrNoNet = New((&tcpip.ErrNoNet{}).String(), errno.ENONET)
58+
ErrEndpointBusy = New((&tcpip.ErrEndpointBusy{}).String(), errno.EBUSY)
5859
)
5960

6061
// TranslateNetstackError converts an error from the tcpip package to a sentry
@@ -149,6 +150,8 @@ func TranslateNetstackError(err tcpip.Error) *Error {
149150
return ErrMulticastInputCannotBeOutput
150151
case *tcpip.ErrMissingRequiredFields:
151152
return ErrMissingRequiredFields
153+
case *tcpip.ErrEndpointBusy:
154+
return ErrEndpointBusy
152155
default:
153156
panic(fmt.Sprintf("unknown error %T", err))
154157
}

pkg/tcpip/errors.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,4 +620,22 @@ func (*ErrMulticastInputCannotBeOutput) IgnoreStats() bool {
620620
}
621621
func (*ErrMulticastInputCannotBeOutput) String() string { return "output cannot contain input" }
622622

623+
// ErrEndpointBusy indicates that the operation cannot be completed because the
624+
// endpoint is busy.
625+
//
626+
// +stateify savable
627+
type ErrEndpointBusy struct{}
628+
629+
// isError implements Error.
630+
func (*ErrEndpointBusy) isError() {}
631+
632+
// IgnoreStats implements Error.
633+
func (*ErrEndpointBusy) IgnoreStats() bool {
634+
return true
635+
}
636+
637+
func (*ErrEndpointBusy) String() string {
638+
return "operation cannot be completed because the endpoint is busy"
639+
}
640+
623641
// LINT.ThenChange(../syserr/netstack.go)

pkg/tcpip/tcpip.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,19 @@ func (*ICMPv6Filter) isGettableSocketOption() {}
11851185

11861186
func (*ICMPv6Filter) isSettableSocketOption() {}
11871187

1188+
// TpacketReq is the tpacket_req structure as described in
1189+
// https://www.kernel.org/doc/Documentation/networking/packet_mmap.txt
1190+
//
1191+
// +stateify savable
1192+
type TpacketReq struct {
1193+
TpBlockSize uint32
1194+
TpBlockNr uint32
1195+
TpFrameSize uint32
1196+
TpFrameNr uint32
1197+
}
1198+
1199+
func (*TpacketReq) isSettableSocketOption() {}
1200+
11881201
// EndpointState represents the state of an endpoint.
11891202
type EndpointState uint8
11901203

0 commit comments

Comments
 (0)