1 // Copyright 2022 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <grpc/support/port_platform.h>
16
17 #include "src/core/ext/transport/chaotic_good/frame_header.h"
18
19 #include <cstdint>
20
21 #include "absl/status/status.h"
22
23 namespace grpc_core {
24 namespace chaotic_good {
25
26 namespace {
WriteLittleEndianUint32(uint32_t value,uint8_t * data)27 void WriteLittleEndianUint32(uint32_t value, uint8_t* data) {
28 data[0] = static_cast<uint8_t>(value);
29 data[1] = static_cast<uint8_t>(value >> 8);
30 data[2] = static_cast<uint8_t>(value >> 16);
31 data[3] = static_cast<uint8_t>(value >> 24);
32 }
33
ReadLittleEndianUint32(const uint8_t * data)34 uint32_t ReadLittleEndianUint32(const uint8_t* data) {
35 return static_cast<uint32_t>(data[0]) |
36 (static_cast<uint32_t>(data[1]) << 8) |
37 (static_cast<uint32_t>(data[2]) << 16) |
38 (static_cast<uint32_t>(data[3]) << 24);
39 }
40 } // namespace
41
42 // Serializes a frame header into a buffer of 24 bytes.
Serialize(uint8_t * data) const43 void FrameHeader::Serialize(uint8_t* data) const {
44 WriteLittleEndianUint32(
45 static_cast<uint32_t>(type) | (flags.ToInt<uint32_t>() << 8), data);
46 WriteLittleEndianUint32(stream_id, data + 4);
47 WriteLittleEndianUint32(header_length, data + 8);
48 WriteLittleEndianUint32(message_length, data + 12);
49 WriteLittleEndianUint32(message_padding, data + 16);
50 WriteLittleEndianUint32(trailer_length, data + 20);
51 }
52
53 // Parses a frame header from a buffer of 24 bytes. All 24 bytes are consumed.
Parse(const uint8_t * data)54 absl::StatusOr<FrameHeader> FrameHeader::Parse(const uint8_t* data) {
55 FrameHeader header;
56 const uint32_t type_and_flags = ReadLittleEndianUint32(data);
57 header.type = static_cast<FrameType>(type_and_flags & 0xff);
58 const uint32_t flags = type_and_flags >> 8;
59 if (flags > 3) return absl::InvalidArgumentError("Invalid flags");
60 header.flags = BitSet<2>::FromInt(flags);
61 header.stream_id = ReadLittleEndianUint32(data + 4);
62 header.header_length = ReadLittleEndianUint32(data + 8);
63 header.message_length = ReadLittleEndianUint32(data + 12);
64 header.message_padding = ReadLittleEndianUint32(data + 16);
65 header.trailer_length = ReadLittleEndianUint32(data + 20);
66 return header;
67 }
68
GetFrameLength() const69 uint32_t FrameHeader::GetFrameLength() const {
70 // In chaotic-good transport design, message and message padding are sent
71 // through different channel. So not included in the frame length calculation.
72 uint32_t frame_length = header_length + trailer_length;
73 return frame_length;
74 }
75
76 } // namespace chaotic_good
77 } // namespace grpc_core
78