diff options
author | Ghanan Gowripalan <ghanan@google.com> | 2020-11-23 22:45:42 -0800 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2020-11-23 22:47:55 -0800 |
commit | ba2d5cb7e1f3ac69a65d0e790f1319082ee01de2 (patch) | |
tree | 8bace42d0e2ee03d4f62178e811ab49b40caf76e /pkg/tcpip/network | |
parent | d4951e05a00a9ec84b8065311836aa9c844f63f6 (diff) |
Use time.Duration for IGMP Max Response Time field
Bug #4682
PiperOrigin-RevId: 343993297
Diffstat (limited to 'pkg/tcpip/network')
-rw-r--r-- | pkg/tcpip/network/ipv4/igmp.go | 40 | ||||
-rw-r--r-- | pkg/tcpip/network/ipv4/igmp_test.go | 41 |
2 files changed, 42 insertions, 39 deletions
diff --git a/pkg/tcpip/network/ipv4/igmp.go b/pkg/tcpip/network/ipv4/igmp.go index e1de58f73..18fe2fd2f 100644 --- a/pkg/tcpip/network/ipv4/igmp.go +++ b/pkg/tcpip/network/ipv4/igmp.go @@ -35,15 +35,18 @@ const ( // See note on igmpState.igmpV1Present for more detail. v1RouterPresentTimeout = 400 * time.Second - // v1MaxRespTimeTenthSec from RFC 2236 Section 4, Page 5. "The IGMPv1 router + // v1MaxRespTime from RFC 2236 Section 4, Page 5. "The IGMPv1 router // will send General Queries with the Max Response Time set to 0. This MUST // be interpreted as a value of 100 (10 seconds)." - v1MaxRespTimeTenthSec = 100 - - // UnsolicitedReportIntervalMaxTenthSec from RFC 2236 Section 8.10, Page 19. - // As all IGMP delay timers are set to a random value between 0 and the - // interval, this is technically a maximum. - UnsolicitedReportIntervalMaxTenthSec = 100 + // + // Note that the Max Response Time field is a value in units of deciseconds. + v1MaxRespTime = 10 * time.Second + + // UnsolicitedReportIntervalMax is the maximum delay between sending + // unsolicited IGMP reports. + // + // Obtained from RFC 2236 Section 8.10, Page 19. + UnsolicitedReportIntervalMax = 10 * time.Second ) // igmpState is the per-interface IGMP state. @@ -185,7 +188,7 @@ func (igmp *igmpState) handleIGMP(pkt *stack.PacketBuffer) { } } -func (igmp *igmpState) handleMembershipQuery(groupAddress tcpip.Address, maxRespTime byte) { +func (igmp *igmpState) handleMembershipQuery(groupAddress tcpip.Address, maxRespTime time.Duration) { igmp.mu.Lock() defer igmp.mu.Unlock() @@ -196,7 +199,7 @@ func (igmp *igmpState) handleMembershipQuery(groupAddress tcpip.Address, maxResp igmp.mu.igmpV1Job.Cancel() igmp.mu.igmpV1Job.Schedule(v1RouterPresentTimeout) igmp.mu.igmpV1Present = true - maxRespTime = v1MaxRespTimeTenthSec + maxRespTime = v1MaxRespTime } // IPv4Any is the General Query Address. @@ -215,7 +218,7 @@ func (igmp *igmpState) handleMembershipQuery(groupAddress tcpip.Address, maxResp // modify IGMP state directly. // // Precondition: igmp.mu MUST be read locked. -func (igmp *igmpState) setDelayTimerForAddressRLocked(groupAddress tcpip.Address, info *membershipInfo, maxRespTime byte) { +func (igmp *igmpState) setDelayTimerForAddressRLocked(groupAddress tcpip.Address, info *membershipInfo, maxRespTime time.Duration) { if info.state == delayingMember { // As per RFC 2236 Section 3, page 3: "If a timer for the group is already // running, it is reset to the random value only if the requested Max @@ -352,7 +355,7 @@ func (igmp *igmpState) joinGroup(groupAddress tcpip.Address) *tcpip.Error { // it should immediately transmit an unsolicited Version 2 Membership Report // for that group" ... "it is recommended that it be repeated" igmp.sendReportLocked(groupAddress) - igmp.setDelayTimerForAddressRLocked(groupAddress, &info, UnsolicitedReportIntervalMaxTenthSec) + igmp.setDelayTimerForAddressRLocked(groupAddress, &info, UnsolicitedReportIntervalMax) igmp.mu.memberships[groupAddress] = info return nil @@ -383,16 +386,7 @@ func (igmp *igmpState) leaveGroup(groupAddress tcpip.Address) { } // RFC 2236 Section 3, Page 3: The response time is set to a "random value... -// selected from the range (0, Max Response Time]" where Max Resp Time is given -// in units of 1/10 of a second. -func (igmp *igmpState) calculateDelayTimerDuration(maxRespTime byte) time.Duration { - maxRespTimeDuration := DecisecondToSecond(maxRespTime) - return time.Duration(igmp.ep.protocol.stack.Rand().Int63n(int64(maxRespTimeDuration))) -} - -// DecisecondToSecond converts a byte representing deci-seconds to a Duration -// type. This helper function exists because the IGMP stack sends and receives -// Max Response Times in deci-seconds. -func DecisecondToSecond(ds byte) time.Duration { - return time.Duration(ds) * time.Second / 10 +// selected from the range (0, Max Response Time]". +func (igmp *igmpState) calculateDelayTimerDuration(maxRespTime time.Duration) time.Duration { + return time.Duration(igmp.ep.protocol.stack.Rand().Int63n(int64(maxRespTime))) } diff --git a/pkg/tcpip/network/ipv4/igmp_test.go b/pkg/tcpip/network/ipv4/igmp_test.go index a0f37885a..4873a336f 100644 --- a/pkg/tcpip/network/ipv4/igmp_test.go +++ b/pkg/tcpip/network/ipv4/igmp_test.go @@ -15,7 +15,9 @@ package ipv4_test import ( + "fmt" "testing" + "time" "gvisor.dev/gvisor/pkg/tcpip" "gvisor.dev/gvisor/pkg/tcpip/buffer" @@ -35,9 +37,16 @@ const ( ) var ( - // unsolicitedReportIntervalMax is the maximum amount of time the NIC will - // wait before sending an unsolicited report after joining a multicast group. - unsolicitedReportIntervalMax = ipv4.DecisecondToSecond(ipv4.UnsolicitedReportIntervalMaxTenthSec) + // unsolicitedReportIntervalMaxTenthSec is the maximum amount of time the NIC + // will wait before sending an unsolicited report after joining a multicast + // group, in deciseconds. + unsolicitedReportIntervalMaxTenthSec = func() uint8 { + const decisecond = time.Second / 10 + if ipv4.UnsolicitedReportIntervalMax%decisecond != 0 { + panic(fmt.Sprintf("UnsolicitedReportIntervalMax of %d is a lossy conversion to deciseconds", ipv4.UnsolicitedReportIntervalMax)) + } + return uint8(ipv4.UnsolicitedReportIntervalMax / decisecond) + }() ) // validateIgmpPacket checks that a passed PacketInfo is an IPv4 IGMP packet @@ -51,7 +60,7 @@ func validateIgmpPacket(t *testing.T, p channel.PacketInfo, remoteAddress tcpip. checker.DstAddr(remoteAddress), checker.IGMP( checker.IGMPType(igmpType), - checker.IGMPMaxRespTime(maxRespTime), + checker.IGMPMaxRespTime(header.DecisecondToDuration(maxRespTime)), checker.IGMPGroupAddress(groupAddress), ), ) @@ -134,7 +143,7 @@ func TestIgmpDisabled(t *testing.T) { // Inject a General Membership Query, which is an IGMP Membership Query with // a zeroed Group Address (IPv4Any) to verify that it does not reach the // handler. - createAndInjectIGMPPacket(e, header.IGMPMembershipQuery, ipv4.UnsolicitedReportIntervalMaxTenthSec, header.IPv4Any) + createAndInjectIGMPPacket(e, header.IGMPMembershipQuery, unsolicitedReportIntervalMaxTenthSec, header.IPv4Any) if got := s.Stats().IGMP.PacketsReceived.MembershipQuery.Value(); got != 0 { t.Fatalf("got Membership Queries received = %d, want = 0", got) @@ -161,7 +170,7 @@ func TestIgmpReceivesIGMPMessages(t *testing.T) { { name: "General Membership Query", headerType: header.IGMPMembershipQuery, - maxRespTime: ipv4.UnsolicitedReportIntervalMaxTenthSec, + maxRespTime: unsolicitedReportIntervalMaxTenthSec, groupAddress: header.IPv4Any, statCounter: func(stats tcpip.IGMPReceivedPacketStats) *tcpip.StatCounter { return stats.MembershipQuery @@ -234,12 +243,12 @@ func TestIgmpJoinGroup(t *testing.T) { } // Verify the second Membership Report is sent after a random interval up to - // the unsolicitedReportIntervalMax. + // the maximum unsolicited report interval. p, ok = e.Read() if ok { t.Fatalf("sent unexpected packet, expected V2MembershipReport only after advancing the clock = %+v", p.Pkt) } - clock.Advance(unsolicitedReportIntervalMax) + clock.Advance(ipv4.UnsolicitedReportIntervalMax) p, ok = e.Read() if !ok { t.Fatal("unable to Read IGMP packet, expected V2MembershipReport") @@ -273,13 +282,13 @@ func TestIgmpLeaveGroup(t *testing.T) { } // Verify the second Membership Report is sent after a random interval up to - // the unsolicitedReportIntervalMax, and is sent to the multicast address - // being joined. + // the maximum unsolicited report interval, and is sent to the multicast + // address being joined. p, ok = e.Read() if ok { t.Fatalf("sent unexpected packet, expected V2MembershipReport only after advancing the clock = %+v", p.Pkt) } - clock.Advance(unsolicitedReportIntervalMax) + clock.Advance(ipv4.UnsolicitedReportIntervalMax) p, ok = e.Read() if !ok { t.Fatal("unable to Read IGMP packet, expected V2MembershipReport") @@ -334,7 +343,7 @@ func TestIgmpJoinLeaveGroup(t *testing.T) { // Wait for the standard IGMP Unsolicited Report Interval duration before // verifying that the unsolicited Membership Report was sent after leaving // the group. - clock.Advance(unsolicitedReportIntervalMax) + clock.Advance(ipv4.UnsolicitedReportIntervalMax) if got := s.Stats().IGMP.PacketsSent.V2MembershipReport.Value(); got != 1 { t.Fatalf("got V2MembershipReport messages sent = %d, want = 1", got) } @@ -365,7 +374,7 @@ func TestIgmpMembershipQueryReport(t *testing.T) { if ok { t.Fatalf("sent unexpected packet, expected V2MembershipReport only after advancing the clock = %+v", p.Pkt) } - clock.Advance(unsolicitedReportIntervalMax) + clock.Advance(ipv4.UnsolicitedReportIntervalMax) p, ok = e.Read() if !ok { t.Fatal("unable to Read IGMP packet, expected V2MembershipReport") @@ -384,7 +393,7 @@ func TestIgmpMembershipQueryReport(t *testing.T) { if ok { t.Fatalf("sent unexpected packet, expected V2MembershipReport only after advancing the clock = %+v", p.Pkt) } - clock.Advance(ipv4.DecisecondToSecond(maxRespTimeDS)) + clock.Advance(header.DecisecondToDuration(maxRespTimeDS)) p, ok = e.Read() if !ok { t.Fatal("unable to Read IGMP packet, expected V2MembershipReport") @@ -428,7 +437,7 @@ func TestIgmpMultipleHosts(t *testing.T) { // Wait to be sure that no Leave Group messages were sent up to the max // unsolicited report interval since it was not the last host to join this // group. - clock.Advance(unsolicitedReportIntervalMax) + clock.Advance(ipv4.UnsolicitedReportIntervalMax) if got := s.Stats().IGMP.PacketsSent.LeaveGroup.Value(); got != 0 { t.Fatalf("got LeaveGroup messages sent = %d, want = 0", got) } @@ -479,7 +488,7 @@ func TestIgmpV1Present(t *testing.T) { if ok { t.Fatalf("sent unexpected packet, expected V1MembershipReport only after advancing the clock = %+v", p.Pkt) } - clock.Advance(unsolicitedReportIntervalMax) + clock.Advance(ipv4.UnsolicitedReportIntervalMax) p, ok = e.Read() if !ok { t.Fatal("unable to Read IGMP packet, expected V1MembershipReport") |