mirror of
https://github.com/netbirdio/gvisor.git
synced 2026-05-22 17:12:49 -07:00
Change AllocationSize to SizeWithPadding as requested
RELNOTES: n/a PiperOrigin-RevId: 342176296
This commit is contained in:
committed by
gVisor bot
parent
62db1fad2c
commit
638d64c633
@@ -275,12 +275,12 @@ func (b IPv4) DestinationAddress() tcpip.Address {
|
||||
// IPv4Options is a buffer that holds all the raw IP options.
|
||||
type IPv4Options []byte
|
||||
|
||||
// AllocationSize implements stack.NetOptions.
|
||||
// SizeWithPadding implements stack.NetOptions.
|
||||
// It reports the size to allocate for the Options. RFC 791 page 23 (end of
|
||||
// section 3.1) says of the padding at the end of the options:
|
||||
// The internet header padding is used to ensure that the internet
|
||||
// header ends on a 32 bit boundary.
|
||||
func (o IPv4Options) AllocationSize() int {
|
||||
func (o IPv4Options) SizeWithPadding() int {
|
||||
return (len(o) + IPv4IHLStride - 1) & ^(IPv4IHLStride - 1)
|
||||
}
|
||||
|
||||
@@ -368,8 +368,8 @@ func (b IPv4) Encode(i *IPv4Fields) {
|
||||
// worth a bit of optimisation here to keep the copy out of the fast path.
|
||||
hdrLen := IPv4MinimumSize
|
||||
if len(i.Options) != 0 {
|
||||
// AllocationSize is always >= len(i.Options).
|
||||
aLen := i.Options.AllocationSize()
|
||||
// SizeWithPadding is always >= len(i.Options).
|
||||
aLen := i.Options.SizeWithPadding()
|
||||
hdrLen += aLen
|
||||
if hdrLen > len(b) {
|
||||
panic(fmt.Sprintf("encode received %d bytes, wanted >= %d", len(b), hdrLen))
|
||||
|
||||
@@ -1227,7 +1227,7 @@ func TestWriteHeaderIncludedPacket(t *testing.T) {
|
||||
nicAddr: localIPv4Addr,
|
||||
remoteAddr: remoteIPv4Addr,
|
||||
pktGen: func(t *testing.T, src tcpip.Address) buffer.VectorisedView {
|
||||
ipHdrLen := header.IPv4MinimumSize + ipv4Options.AllocationSize()
|
||||
ipHdrLen := header.IPv4MinimumSize + ipv4Options.SizeWithPadding()
|
||||
totalLen := ipHdrLen + len(data)
|
||||
hdr := buffer.NewPrependable(totalLen)
|
||||
if n := copy(hdr.Prepend(len(data)), data); n != len(data) {
|
||||
@@ -1272,7 +1272,7 @@ func TestWriteHeaderIncludedPacket(t *testing.T) {
|
||||
nicAddr: localIPv4Addr,
|
||||
remoteAddr: remoteIPv4Addr,
|
||||
pktGen: func(t *testing.T, src tcpip.Address) buffer.VectorisedView {
|
||||
ip := header.IPv4(make([]byte, header.IPv4MinimumSize+ipv4Options.AllocationSize()))
|
||||
ip := header.IPv4(make([]byte, header.IPv4MinimumSize+ipv4Options.SizeWithPadding()))
|
||||
ip.Encode(&header.IPv4Fields{
|
||||
Protocol: transportProto,
|
||||
TTL: ipv4.DefaultTTL,
|
||||
|
||||
@@ -206,12 +206,12 @@ func (e *endpoint) addIPHeader(r *stack.Route, pkt *stack.PacketBuffer, params s
|
||||
if opts, ok = params.Options.(header.IPv4Options); !ok {
|
||||
panic(fmt.Sprintf("want IPv4Options, got %T", params.Options))
|
||||
}
|
||||
hdrLen += opts.AllocationSize()
|
||||
hdrLen += opts.SizeWithPadding()
|
||||
if hdrLen > header.IPv4MaximumHeaderSize {
|
||||
// Since we have no way to report an error we must either panic or create
|
||||
// a packet which is different to what was requested. Choose panic as this
|
||||
// would be a programming error that should be caught in testing.
|
||||
panic(fmt.Sprintf("IPv4 Options %d bytes, Max %d", params.Options.AllocationSize(), header.IPv4MaximumOptionsSize))
|
||||
panic(fmt.Sprintf("IPv4 Options %d bytes, Max %d", params.Options.SizeWithPadding(), header.IPv4MaximumOptionsSize))
|
||||
}
|
||||
}
|
||||
ip := header.IPv4(pkt.NetworkHeader().Push(hdrLen))
|
||||
|
||||
@@ -167,7 +167,7 @@ func TestIPv4EncodeOptions(t *testing.T) {
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
paddedOptionLength := test.options.AllocationSize()
|
||||
paddedOptionLength := test.options.SizeWithPadding()
|
||||
ipHeaderLength := header.IPv4MinimumSize + paddedOptionLength
|
||||
if ipHeaderLength > header.IPv4MaximumHeaderSize {
|
||||
t.Fatalf("IP header length too large: got = %d, want <= %d ", ipHeaderLength, header.IPv4MaximumHeaderSize)
|
||||
@@ -706,7 +706,7 @@ func TestIPv4Sanity(t *testing.T) {
|
||||
},
|
||||
})
|
||||
|
||||
paddedOptionLength := test.options.AllocationSize()
|
||||
paddedOptionLength := test.options.SizeWithPadding()
|
||||
ipHeaderLength := header.IPv4MinimumSize + paddedOptionLength
|
||||
if ipHeaderLength > header.IPv4MaximumHeaderSize {
|
||||
t.Fatalf("IP header length too large: got = %d, want <= %d ", ipHeaderLength, header.IPv4MaximumHeaderSize)
|
||||
|
||||
@@ -262,10 +262,10 @@ const (
|
||||
// NetOptions is an interface that allows us to pass network protocol specific
|
||||
// options through the Stack layer code.
|
||||
type NetOptions interface {
|
||||
// AllocationSize returns the amount of memory that must be allocated to
|
||||
// SizeWithPadding returns the amount of memory that must be allocated to
|
||||
// hold the options given that the value must be rounded up to the next
|
||||
// multiple of 4 bytes.
|
||||
AllocationSize() int
|
||||
SizeWithPadding() int
|
||||
}
|
||||
|
||||
// NetworkHeaderParams are the header parameters given as input by the
|
||||
|
||||
@@ -298,7 +298,7 @@ func (l *IPv4) ToBytes() ([]byte, error) {
|
||||
// An IPv4 header is variable length depending on the size of the Options.
|
||||
hdrLen := header.IPv4MinimumSize
|
||||
if l.Options != nil {
|
||||
hdrLen += l.Options.AllocationSize()
|
||||
hdrLen += l.Options.SizeWithPadding()
|
||||
if hdrLen > header.IPv4MaximumHeaderSize {
|
||||
// While ToBytes can be called on packets that were received as well
|
||||
// as packets locally generated, it is physically impossible for a
|
||||
|
||||
Reference in New Issue
Block a user