Skip to content

Commit

Permalink
netstack: remove PacketBuffer.IsNil()
Browse files Browse the repository at this point in the history
The change was originally motivated by ticket references (cl/450976957), which
were never implemented.

PiperOrigin-RevId: 617480960
  • Loading branch information
kevinGC authored and gvisor-bot committed Mar 20, 2024
1 parent 5a6aadd commit 3c75945
Show file tree
Hide file tree
Showing 40 changed files with 208 additions and 213 deletions.
2 changes: 1 addition & 1 deletion pkg/tcpip/link/channel/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (e *Endpoint) ReadContext(ctx context.Context) *stack.PacketBuffer {
// Drain removes all outbound packets from the channel and counts them.
func (e *Endpoint) Drain() int {
c := 0
for pkt := e.Read(); !pkt.IsNil(); pkt = e.Read() {
for pkt := e.Read(); pkt != nil; pkt = e.Read() {
pkt.DecRef()
c++
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/tcpip/link/ethernet/ethernet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func TestWritePacketToRemoteAddHeader(t *testing.T) {

{
pkt := c.Read()
if pkt.IsNil() {
if pkt == nil {
t.Fatal("expected to read a packet")
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/tcpip/link/fdbased/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func checkPacketInfoEqual(t *testing.T, got, want packetInfo) {
if diff := cmp.Diff(
want, got,
cmp.Transformer("ExtractPacketBuffer", func(pk *stack.PacketBuffer) *packetContents {
if pk.IsNil() {
if pk == nil {
return nil
}
return &packetContents{
Expand Down
4 changes: 2 additions & 2 deletions pkg/tcpip/link/packetsocket/packetsocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ type testNetworkDispatcher struct {
}

func (t *testNetworkDispatcher) reset() {
if pkt := t.linkPacket.pkt; !pkt.IsNil() {
if pkt := t.linkPacket.pkt; pkt != nil {
pkt.DecRef()
}
if pkt := t.networkPacket.pkt; !pkt.IsNil() {
if pkt := t.networkPacket.pkt; pkt != nil {
pkt.DecRef()
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/tcpip/link/qdisc/fifo/fifo.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (qd *queueDispatcher) dispatchLoop() {
case &qd.newPacketWaker:
case &qd.closeWaker:
qd.mu.Lock()
for p := qd.queue.removeFront(); !p.IsNil(); p = qd.queue.removeFront() {
for p := qd.queue.removeFront(); p != nil; p = qd.queue.removeFront() {
p.DecRef()
}
qd.queue.decRef()
Expand All @@ -107,7 +107,7 @@ func (qd *queueDispatcher) dispatchLoop() {
panic("unknown waker")
}
qd.mu.Lock()
for pkt := qd.queue.removeFront(); !pkt.IsNil(); pkt = qd.queue.removeFront() {
for pkt := qd.queue.removeFront(); pkt != nil; pkt = qd.queue.removeFront() {
batch.PushBack(pkt)
if batch.Len() < BatchSize && !qd.queue.isEmpty() {
continue
Expand Down
2 changes: 1 addition & 1 deletion pkg/tcpip/link/tun/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func (d *Device) Read() (*buffer.View, error) {
}

pkt := endpoint.Read()
if pkt.IsNil() {
if pkt == nil {
return nil, linuxerr.ErrWouldBlock
}
v := d.encodePkt(pkt)
Expand Down
8 changes: 4 additions & 4 deletions pkg/tcpip/network/arp/arp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func TestDirectRequest(t *testing.T) {
// No packets should be sent after receiving an invalid ARP request.
// There is no need to perform a blocking read here, since packets are
// sent in the same function that handles ARP requests.
if pkt := c.linkEP.Read(); !pkt.IsNil() {
if pkt := c.linkEP.Read(); pkt != nil {
t.Errorf("unexpected packet sent: %+v", pkt)
}
if got, want := c.s.Stats().ARP.RequestsReceivedUnknownTargetAddress.Value(), requestsRecvUnknownAddr+1; got != want {
Expand All @@ -338,7 +338,7 @@ func TestDirectRequest(t *testing.T) {

// Verify an ARP response was sent.
pi := c.linkEP.Read()
if pi.IsNil() {
if pi == nil {
t.Fatal("expected ARP response to be sent, got none")
}

Expand Down Expand Up @@ -714,7 +714,7 @@ func TestLinkAddressRequest(t *testing.T) {
}

pkt := linkEP.Read()
if pkt.IsNil() {
if pkt == nil {
t.Fatal("expected to send a link address request")
}

Expand Down Expand Up @@ -773,7 +773,7 @@ func TestDADARPRequestPacket(t *testing.T) {

clock.RunImmediatelyScheduledJobs()
pkt := e.Read()
if pkt.IsNil() {
if pkt == nil {
t.Fatal("expected to send an ARP request")
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/tcpip/network/internal/fragmentation/fragmentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,12 @@ func (f *Fragmentation) release(r *reassembler, timedOut bool) {
if h := f.timeoutHandler; timedOut && h != nil {
h.OnReassemblyTimeout(r.pkt)
}
if !r.pkt.IsNil() {
if r.pkt != nil {
r.pkt.DecRef()
r.pkt = nil
}
for _, h := range r.holes {
if !h.pkt.IsNil() {
if h.pkt != nil {
h.pkt.DecRef()
h.pkt = nil
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/tcpip/network/internal/fragmentation/fragmentation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func TestFragmentationProcess(t *testing.T) {
defer in.pkt.DecRef()
defer c.out[i].buf.Release()
resPkt, proto, done, err := f.Process(in.id, in.first, in.last, in.more, in.proto, in.pkt)
if !resPkt.IsNil() {
if resPkt != nil {
defer resPkt.DecRef()
}
if err != nil {
Expand Down Expand Up @@ -267,7 +267,7 @@ func TestReassemblingTimeout(t *testing.T) {
p := pkt(len(frag.data), frag.data)
defer p.DecRef()
pkt, _, done, err := f.Process(FragmentID{}, frag.first, frag.last, frag.more, protocol, p)
if !pkt.IsNil() {
if pkt != nil {
pkt.DecRef()
}
if err != nil {
Expand Down Expand Up @@ -450,7 +450,7 @@ func TestErrors(t *testing.T) {
f := NewFragmentation(test.blockSize, HighFragThreshold, LowFragThreshold, reassembleTimeout, c, nil)
resPkt, _, done, err := f.Process(FragmentID{}, test.first, test.last, test.more, 0, p0)

if !resPkt.IsNil() {
if resPkt != nil {
resPkt.DecRef()
}
if !errors.Is(err, test.err) {
Expand Down Expand Up @@ -689,11 +689,11 @@ func TestTimeoutHandler(t *testing.T) {
f.release(r, true)
}
switch {
case !handler.pkt.IsNil() && test.wantPkt.IsNil():
case handler.pkt != nil && test.wantPkt == nil:
t.Errorf("got handler.pkt = not nil (pkt.Data = %x), want = nil", handler.pkt.Data().AsRange().ToSlice())
case handler.pkt.IsNil() && !test.wantPkt.IsNil():
case handler.pkt == nil && test.wantPkt != nil:
t.Errorf("got handler.pkt = nil, want = not nil (pkt.Data = %x)", test.wantPkt.Data().AsRange().ToSlice())
case !handler.pkt.IsNil() && !test.wantPkt.IsNil():
case handler.pkt != nil && test.wantPkt != nil:
if diff := cmp.Diff(test.wantPkt.Data().AsRange().ToSlice(), handler.pkt.Data().AsRange().ToSlice()); diff != "" {
t.Errorf("pkt.Data mismatch (-want, +got):\n%s", diff)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/tcpip/network/internal/fragmentation/reassembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (r *reassembler) process(first, last uint16, more bool, proto uint8, pkt *s
// options received in the first fragment should be used - and they should
// override options from following fragments.
if first == 0 {
if !r.pkt.IsNil() {
if r.pkt != nil {
r.pkt.DecRef()
}
r.pkt = pkt.IncRef()
Expand Down
14 changes: 7 additions & 7 deletions pkg/tcpip/network/internal/fragmentation/reassembler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,19 +191,19 @@ func TestReassemblerProcess(t *testing.T) {
// reassembler will leak PacketBuffers.
defer func() {
for _, h := range r.holes {
if !h.pkt.IsNil() {
if h.pkt != nil {
h.pkt.DecRef()
}
}
if !r.pkt.IsNil() {
if r.pkt != nil {
r.pkt.DecRef()
}
}()
var resPkt *stack.PacketBuffer
var isDone bool
for _, param := range test.params {
pkt, _, done, _, err := r.process(param.first, param.last, param.more, proto, param.pkt)
if !pkt.IsNil() {
if pkt != nil {
defer pkt.DecRef()
}
if done != param.wantDone || err != param.wantError {
Expand All @@ -217,7 +217,7 @@ func TestReassemblerProcess(t *testing.T) {

ignorePkt := func(a, b *stack.PacketBuffer) bool { return true }
cmpPktData := func(a, b *stack.PacketBuffer) bool {
if a.IsNil() || b.IsNil() {
if a == nil || b == nil {
return a == b
}
return bytes.Equal(a.Data().AsRange().ToSlice(), b.Data().AsRange().ToSlice())
Expand Down Expand Up @@ -246,16 +246,16 @@ func TestReassemblerProcess(t *testing.T) {
}
})
for _, p := range test.params {
if !p.pkt.IsNil() {
if p.pkt != nil {
p.pkt.DecRef()
}
}
for _, w := range test.want {
if !w.pkt.IsNil() {
if w.pkt != nil {
w.pkt.DecRef()
}
}
if !test.wantPkt.IsNil() {
if test.wantPkt != nil {
test.wantPkt.DecRef()
}
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/tcpip/network/internal/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func ValidateIGMPv3RecordsAcrossReports(t *testing.T, e *channel.Endpoint, srcAd

for len(expectedRecords) != 0 {
p := e.Read()
if p.IsNil() {
if p == nil {
t.Fatalf("expected IGMP message with expectedRecords = %#v", expectedRecords)
}
v := stack.PayloadSince(p.NetworkHeader())
Expand Down Expand Up @@ -265,7 +265,7 @@ func ValidMultipleIGMPv2ReportLeaves(t *testing.T, e *channel.Endpoint, srcAddr

for len(expectedGroups) != 0 {
p := e.Read()
if p.IsNil() {
if p == nil {
t.Fatalf("expected IGMP message with expectedGroups = %#v", expectedGroups)
}
v := stack.PayloadSince(p.NetworkHeader())
Expand Down Expand Up @@ -334,7 +334,7 @@ func ValidateMLDv2RecordsAcrossReports(t *testing.T, e *channel.Endpoint, srcAdd

for len(expectedRecords) != 0 {
p := e.Read()
if p.IsNil() {
if p == nil {
t.Fatalf("expected MLD Message with expectedRecords = %#v", expectedRecords)
}
v := stack.PayloadSince(p.NetworkHeader())
Expand Down Expand Up @@ -365,7 +365,7 @@ func ValidMultipleMLDv1ReportLeaves(t *testing.T, e *channel.Endpoint, srcAddr t

for len(expectedGroups) != 0 {
p := e.Read()
if p.IsNil() {
if p == nil {
t.Fatalf("expected MLD Message with expectedGroups = %#v", expectedGroups)
}
v := stack.PayloadSince(p.NetworkHeader())
Expand Down
4 changes: 2 additions & 2 deletions pkg/tcpip/network/ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1799,7 +1799,7 @@ func TestWriteHeaderIncludedPacket(t *testing.T) {
}

pkt := e.Read()
if pkt.IsNil() {
if pkt == nil {
t.Fatal("expected a packet to be written")
}
test.checker(t, pkt, subTest.srcAddr)
Expand Down Expand Up @@ -2055,7 +2055,7 @@ func TestICMPInclusionSize(t *testing.T) {
})
v := test.injector(e, test.srcAddress, payload)
pkt := e.Read()
if pkt.IsNil() {
if pkt == nil {
t.Fatal("expected a packet to be written")
}
if got, want := pkt.Size(), test.replyLength; got != want {
Expand Down
2 changes: 1 addition & 1 deletion pkg/tcpip/network/ipv4/icmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ func (p *protocol) OnReassemblyTimeout(pkt *stack.PacketBuffer) {
//
// If fragment zero is not available then no time exceeded need be sent at
// all.
if !pkt.IsNil() {
if pkt != nil {
p.returnError(&icmpReasonReassemblyTimeout{}, pkt, true /* deliveredLocally */)
}
}
20 changes: 10 additions & 10 deletions pkg/tcpip/network/ipv4/igmp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func TestIGMPV1Present(t *testing.T) {
// the IGMPv1 General Membership Query in.
{
p := e.Read()
if p.IsNil() {
if p == nil {
t.Fatal("unable to Read IGMP packet, expected V3MembershipReport")
}
if got := s.Stats().IGMP.PacketsSent.V3MembershipReport.Value(); got != 1 {
Expand Down Expand Up @@ -203,13 +203,13 @@ func TestIGMPV1Present(t *testing.T) {

// Verify the solicited Membership Report is sent. Now that this NIC has seen
// an IGMPv1 query, it should send an IGMPv1 Membership Report.
if p := e.Read(); !p.IsNil() {
if p := e.Read(); p != nil {
t.Fatalf("sent unexpected packet, expected V1MembershipReport only after advancing the clock = %+v", p)
}
ctx.clock.Advance(ipv4.UnsolicitedReportIntervalMax)
{
p := e.Read()
if p.IsNil() {
if p == nil {
t.Fatal("unable to Read IGMP packet, expected V1MembershipReport")
}
if got := s.Stats().IGMP.PacketsSent.V1MembershipReport.Value(); got != 1 {
Expand All @@ -228,7 +228,7 @@ func TestIGMPV1Present(t *testing.T) {
}
{
p := e.Read()
if p.IsNil() {
if p == nil {
t.Fatal("unable to Read IGMP packet, expected V2MembershipReport")
}
if got := s.Stats().IGMP.PacketsSent.V3MembershipReport.Value(); got != 2 {
Expand Down Expand Up @@ -322,7 +322,7 @@ func TestSendQueuedIGMPReports(t *testing.T) {
}
}
test.checkStats(t, s, reportCounter, doneCounter, reportV2Counter)
if p := e.Read(); !p.IsNil() {
if p := e.Read(); p != nil {
t.Fatalf("got unexpected packet = %#v", p)
}

Expand Down Expand Up @@ -358,7 +358,7 @@ func TestSendQueuedIGMPReports(t *testing.T) {
// Should have no more packets to send after the initial set of unsolicited
// reports.
clock.Advance(time.Hour)
if p := e.Read(); !p.IsNil() {
if p := e.Read(); p != nil {
t.Fatalf("got unexpected packet = %#v", p)
}
})
Expand Down Expand Up @@ -540,7 +540,7 @@ func TestGetSetIGMPVersion(t *testing.T) {
if err := s.JoinGroup(ipv4.ProtocolNumber, nicID, multicastAddr1); err != nil {
t.Fatalf("JoinGroup(ipv4, nic, %s) = %s", multicastAddr1, err)
}
if p := e.Read(); p.IsNil() {
if p := e.Read(); p == nil {
t.Fatal("expected a report message to be sent")
} else {
validateIgmpv3ReportPacket(t, p, stackAddr, multicastAddr1)
Expand All @@ -556,7 +556,7 @@ func TestGetSetIGMPVersion(t *testing.T) {
if err := s.JoinGroup(ipv4.ProtocolNumber, nicID, multicastAddr2); err != nil {
t.Fatalf("JoinGroup(ipv4, nic, %s) = %s", multicastAddr2, err)
}
if p := e.Read(); p.IsNil() {
if p := e.Read(); p == nil {
t.Fatal("expected a report message to be sent")
} else {
validateIgmpPacket(t, p, header.IGMPv2MembershipReport, 0, stackAddr, multicastAddr2, multicastAddr2)
Expand All @@ -572,7 +572,7 @@ func TestGetSetIGMPVersion(t *testing.T) {
if err := s.JoinGroup(ipv4.ProtocolNumber, nicID, multicastAddr3); err != nil {
t.Fatalf("JoinGroup(ipv4, nic, %s) = %s", multicastAddr3, err)
}
if p := e.Read(); p.IsNil() {
if p := e.Read(); p == nil {
t.Fatal("expected a report message to be sent")
} else {
validateIgmpPacket(t, p, header.IGMPv1MembershipReport, 0, stackAddr, multicastAddr3, multicastAddr3)
Expand All @@ -588,7 +588,7 @@ func TestGetSetIGMPVersion(t *testing.T) {
if err := s.JoinGroup(ipv4.ProtocolNumber, nicID, multicastAddr4); err != nil {
t.Fatalf("JoinGroup(ipv4, nic, %s) = %s", multicastAddr4, err)
}
if p := e.Read(); p.IsNil() {
if p := e.Read(); p == nil {
t.Fatal("expected a report message to be sent")
} else {
validateIgmpv3ReportPacket(t, p, stackAddr, multicastAddr4)
Expand Down
Loading

0 comments on commit 3c75945

Please sign in to comment.