From 93e387314131c3d511ced623af4ca186cde02561 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Tue, 6 Oct 2020 15:05:26 +0700 Subject: [PATCH 1/2] qlog the key phase bit --- logging/interface.go | 9 +++++++++ qlog/packet_header.go | 15 +++++++++++++++ qlog/packet_header_test.go | 3 +++ 3 files changed, 27 insertions(+) diff --git a/logging/interface.go b/logging/interface.go index 24398d3ae..b0c41fb81 100644 --- a/logging/interface.go +++ b/logging/interface.go @@ -22,6 +22,8 @@ type ( EncryptionLevel = protocol.EncryptionLevel // The KeyPhase is the key phase of the 1-RTT keys. KeyPhase = protocol.KeyPhase + // The KeyPhaseBit is the value of the key phase bit of the 1-RTT packets. + KeyPhaseBit = protocol.KeyPhaseBit // The PacketNumber is the packet number of a packet. PacketNumber = protocol.PacketNumber // The Perspective is the role of a QUIC endpoint (client or server). @@ -53,6 +55,13 @@ type ( RTTStats = utils.RTTStats ) +const ( + // KeyPhaseZero is key phase bit 0 + KeyPhaseZero KeyPhaseBit = protocol.KeyPhaseZero + // KeyPhaseOne is key phase bit 1 + KeyPhaseOne KeyPhaseBit = protocol.KeyPhaseOne +) + const ( // PerspectiveServer is used for a QUIC server PerspectiveServer Perspective = protocol.PerspectiveServer diff --git a/qlog/packet_header.go b/qlog/packet_header.go index 8d324f029..0369bbfad 100644 --- a/qlog/packet_header.go +++ b/qlog/packet_header.go @@ -37,6 +37,9 @@ type packetHeader struct { Version logging.VersionNumber SrcConnectionID logging.ConnectionID DestConnectionID logging.ConnectionID + + hasKeyPhase bool + KeyPhaseBit logging.KeyPhaseBit } func transformHeader(hdr *wire.Header) *packetHeader { @@ -52,6 +55,10 @@ func transformHeader(hdr *wire.Header) *packetHeader { func transformExtendedHeader(hdr *wire.ExtendedHeader) *packetHeader { h := transformHeader(&hdr.Header) h.PacketNumber = hdr.PacketNumber + if !hdr.IsLongHeader { + h.hasKeyPhase = true + h.KeyPhaseBit = hdr.KeyPhase + } return h } @@ -74,4 +81,12 @@ func (h packetHeader) MarshalJSONObject(enc *gojay.Encoder) { if h.DestConnectionID.Len() > 0 { enc.StringKey("dcid", connectionID(h.DestConnectionID).String()) } + if h.hasKeyPhase { + switch h.KeyPhaseBit { + case logging.KeyPhaseZero: + enc.StringKey("key_phase_bit", "0") + case logging.KeyPhaseOne: + enc.StringKey("key_phase_bit", "1") + } + } } diff --git a/qlog/packet_header_test.go b/qlog/packet_header_test.go index c6846e86a..fd7893eb5 100644 --- a/qlog/packet_header_test.go +++ b/qlog/packet_header_test.go @@ -38,6 +38,7 @@ var _ = Describe("Packet Header", func() { map[string]interface{}{ "packet_number": 42, "dcil": 0, + "key_phase_bit": "0", }, ) }) @@ -108,11 +109,13 @@ var _ = Describe("Packet Header", func() { &wire.ExtendedHeader{ PacketNumber: 42, Header: wire.Header{DestConnectionID: protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef}}, + KeyPhase: protocol.KeyPhaseOne, }, map[string]interface{}{ "packet_number": 42, "dcil": 4, "dcid": "deadbeef", + "key_phase_bit": "1", }, ) }) From d5a30225ce0fd4a2c6ccc5980dccaa3f4997375c Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Tue, 6 Oct 2020 15:25:31 +0700 Subject: [PATCH 2/2] use a uint8 to represent the key phase bit --- internal/protocol/key_phase.go | 23 +++++++++++++++++------ internal/protocol/key_phase_test.go | 5 +++++ qlog/packet_header.go | 15 +++------------ qlog/packet_header_test.go | 5 ++++- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/internal/protocol/key_phase.go b/internal/protocol/key_phase.go index 2ebc3f926..edd740cf6 100644 --- a/internal/protocol/key_phase.go +++ b/internal/protocol/key_phase.go @@ -3,23 +3,34 @@ package protocol // KeyPhase is the key phase type KeyPhase uint64 +// Bit determines the key phase bit func (p KeyPhase) Bit() KeyPhaseBit { - return p%2 == 1 + if p%2 == 0 { + return KeyPhaseZero + } + return KeyPhaseOne } // KeyPhaseBit is the key phase bit -type KeyPhaseBit bool +type KeyPhaseBit uint8 const ( + // KeyPhaseUndefined is an undefined key phase + KeyPhaseUndefined KeyPhaseBit = iota // KeyPhaseZero is key phase 0 - KeyPhaseZero KeyPhaseBit = false + KeyPhaseZero // KeyPhaseOne is key phase 1 - KeyPhaseOne KeyPhaseBit = true + KeyPhaseOne ) func (p KeyPhaseBit) String() string { - if p == KeyPhaseZero { + //nolint:exhaustive + switch p { + case KeyPhaseZero: return "0" + case KeyPhaseOne: + return "1" + default: + return "undefined" } - return "1" } diff --git a/internal/protocol/key_phase_test.go b/internal/protocol/key_phase_test.go index da06513e6..92f404a54 100644 --- a/internal/protocol/key_phase_test.go +++ b/internal/protocol/key_phase_test.go @@ -6,6 +6,11 @@ import ( ) var _ = Describe("Key Phases", func() { + It("has undefined as its default value", func() { + var k KeyPhaseBit + Expect(k).To(Equal(KeyPhaseUndefined)) + }) + It("has the correct string representation", func() { Expect(KeyPhaseZero.String()).To(Equal("0")) Expect(KeyPhaseOne.String()).To(Equal("1")) diff --git a/qlog/packet_header.go b/qlog/packet_header.go index 0369bbfad..069ef224d 100644 --- a/qlog/packet_header.go +++ b/qlog/packet_header.go @@ -38,7 +38,6 @@ type packetHeader struct { SrcConnectionID logging.ConnectionID DestConnectionID logging.ConnectionID - hasKeyPhase bool KeyPhaseBit logging.KeyPhaseBit } @@ -55,10 +54,7 @@ func transformHeader(hdr *wire.Header) *packetHeader { func transformExtendedHeader(hdr *wire.ExtendedHeader) *packetHeader { h := transformHeader(&hdr.Header) h.PacketNumber = hdr.PacketNumber - if !hdr.IsLongHeader { - h.hasKeyPhase = true - h.KeyPhaseBit = hdr.KeyPhase - } + h.KeyPhaseBit = hdr.KeyPhase return h } @@ -81,12 +77,7 @@ func (h packetHeader) MarshalJSONObject(enc *gojay.Encoder) { if h.DestConnectionID.Len() > 0 { enc.StringKey("dcid", connectionID(h.DestConnectionID).String()) } - if h.hasKeyPhase { - switch h.KeyPhaseBit { - case logging.KeyPhaseZero: - enc.StringKey("key_phase_bit", "0") - case logging.KeyPhaseOne: - enc.StringKey("key_phase_bit", "1") - } + if h.KeyPhaseBit == logging.KeyPhaseZero || h.KeyPhaseBit == logging.KeyPhaseOne { + enc.StringKey("key_phase_bit", h.KeyPhaseBit.String()) } } diff --git a/qlog/packet_header_test.go b/qlog/packet_header_test.go index fd7893eb5..4d3af3e19 100644 --- a/qlog/packet_header_test.go +++ b/qlog/packet_header_test.go @@ -34,7 +34,10 @@ var _ = Describe("Packet Header", func() { It("marshals a header for a 1-RTT packet", func() { check( - &wire.ExtendedHeader{PacketNumber: 42}, + &wire.ExtendedHeader{ + PacketNumber: 42, + KeyPhase: protocol.KeyPhaseZero, + }, map[string]interface{}{ "packet_number": 42, "dcil": 0,