• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef GRPC_SRC_CORE_EXT_TRANSPORT_CHAOTIC_GOOD_FRAME_HEADER_H
16 #define GRPC_SRC_CORE_EXT_TRANSPORT_CHAOTIC_GOOD_FRAME_HEADER_H
17 
18 #include <grpc/support/port_platform.h>
19 #include <stddef.h>
20 
21 #include <cstdint>
22 
23 #include "absl/status/statusor.h"
24 #include "src/core/util/bitset.h"
25 
26 namespace grpc_core {
27 namespace chaotic_good {
28 
29 // Remember to add new frame types to frame_fuzzer.cc
30 enum class FrameType : uint8_t {
31   kSettings = 0x00,
32   kClientInitialMetadata = 0x80,
33   kClientEndOfStream = 0x81,
34   kServerInitialMetadata = 0x91,
35   kServerTrailingMetadata = 0x92,
36   kMessage = 0xa0,
37   kBeginMessage = 0xa1,
38   kMessageChunk = 0xa2,
39   kCancel = 0xff,
40 };
41 
42 std::string FrameTypeString(FrameType type);
43 
44 inline std::ostream& operator<<(std::ostream& out, FrameType type) {
45   return out << FrameTypeString(type);
46 }
47 
48 template <typename Sink>
AbslStringify(Sink & sink,FrameType type)49 void AbslStringify(Sink& sink, FrameType type) {
50   sink.Append(FrameTypeString(type));
51 }
52 
53 struct FrameHeader {
54   FrameType type = FrameType::kCancel;
55   uint16_t payload_connection_id = 0;
56   uint32_t stream_id = 0;
57   uint32_t payload_length = 0;
58 
59   // Parses a frame header from a buffer of 12 bytes. All 12 bytes are consumed.
60   static absl::StatusOr<FrameHeader> Parse(const uint8_t* data);
61   // Serializes a frame header into a buffer of 12 bytes.
62   void Serialize(uint8_t* data) const;
63   // Report contents as a string
64   std::string ToString() const;
65   // Required padding to maintain alignment.
PaddingFrameHeader66   uint32_t Padding(uint32_t alignment) const {
67     if (payload_connection_id == 0) {
68       return 0;
69     }
70     if (payload_length % alignment == 0) {
71       return 0;
72     }
73     return alignment - (payload_length % alignment);
74   }
75 
76   bool operator==(const FrameHeader& h) const {
77     return type == h.type && stream_id == h.stream_id &&
78            payload_connection_id == h.payload_connection_id &&
79            payload_length == h.payload_length;
80   }
81   // Frame header size is fixed to 12 bytes.
82   enum { kFrameHeaderSize = 12 };
83 };
84 
85 inline std::ostream& operator<<(std::ostream& out, const FrameHeader& h) {
86   return out << h.ToString();
87 }
88 
89 }  // namespace chaotic_good
90 }  // namespace grpc_core
91 
92 #endif  // GRPC_SRC_CORE_EXT_TRANSPORT_CHAOTIC_GOOD_FRAME_HEADER_H
93