tvm
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
framing.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
25 #ifndef TVM_RUNTIME_CRT_RPC_COMMON_FRAMING_H_
26 #define TVM_RUNTIME_CRT_RPC_COMMON_FRAMING_H_
27 
28 #include <inttypes.h>
29 #include <stddef.h>
32 
33 namespace tvm {
34 namespace runtime {
35 namespace micro_rpc {
36 
37 uint16_t crc16_compute(const uint8_t* data, size_t data_size_bytes, uint16_t* previous_crc);
38 
39 enum class Escape : uint8_t { kEscapeStart = 0xff, kEscapeNop = 0xfe, kPacketStart = 0xfd };
40 
42  public:
43  static constexpr const size_t kPayloadLength = sizeof(uint32_t);
44  static constexpr const size_t kCrc = sizeof(uint16_t);
45 };
46 
47 class Unframer {
48  public:
49  explicit Unframer(WriteStream* stream)
50  : stream_{stream},
51  state_{State::kFindPacketStart},
52  saw_escape_start_{false},
53  num_buffer_bytes_valid_{0} {}
54 
71  tvm_crt_error_t Write(const uint8_t* data, size_t data_size_bytes, size_t* bytes_consumed);
72 
74  void Reset();
75 
77  size_t BytesNeeded();
78 
79  private:
80  tvm_crt_error_t FindPacketStart();
81  tvm_crt_error_t FindPacketLength();
82  tvm_crt_error_t FindPacketCrc();
83  tvm_crt_error_t FindCrcEnd();
84 
85  bool IsBufferFull(size_t buffer_full_bytes) {
86  return num_buffer_bytes_valid_ >= buffer_full_bytes;
87  }
88 
90  tvm_crt_error_t AddToBuffer(size_t buffer_full_bytes, bool update_crc);
91 
92  void ClearBuffer();
93 
113  tvm_crt_error_t ConsumeInput(uint8_t* buffer, size_t buffer_size_bytes, size_t* bytes_filled,
114  bool update_crc);
115 
116  WriteStream* stream_;
117 
118  enum class State : uint8_t {
119  kFindPacketStart = 0,
120  kFindPacketLength = 1,
121  kFindPacketCrc = 2,
122  kFindCrcEnd = 3,
123  };
124  State state_;
125 
126  const uint8_t* input_;
127  size_t input_size_bytes_;
128 
129  bool saw_escape_start_;
130 
132  uint8_t buffer_[128];
133 
135  size_t num_buffer_bytes_valid_;
136 
138  size_t num_payload_bytes_remaining_;
139 
141  uint16_t crc_;
142 };
143 
144 class Framer {
145  public:
146  typedef ssize_t (*WriteFunc)(const uint8_t* data, size_t data_size_bytes);
147 
148  explicit Framer(WriteStream* stream)
149  : stream_{stream}, state_{State::kReset}, num_payload_bytes_remaining_{0} {}
150 
163  tvm_crt_error_t Write(const uint8_t* payload, size_t payload_size_bytes);
164 
180  tvm_crt_error_t StartPacket(size_t payload_size_bytes);
181 
202  tvm_crt_error_t WritePayloadChunk(const uint8_t* payload_chunk, size_t payload_chunk_size_bytes);
203 
204  /* \brief Finish writing one packet by sending the CRC.
205  *
206  * When transmitting paylaods that are too large to be buffered, call this function after sending
207  * the entire payload using WritePayloadChunk.
208  *
209  * \return
210  * - kTvmErrorNoError when no error occurs
211  * - kTvmErrorFramingInvalidState when StartPacket() has not been called.
212  * - kTvmErrorFramingPayloadIncomplete when less bytes were written using WritePayloadChunk()
213  * than were declared in the payload_size_bytes parameter given to StartPacket().
214  * - kTvmErrorWriteStreamShortWrite if the WriteStream passed to constructor's Write()
215  * function returns 0.
216  * - kTvmErrorWriteStreamShortWrite if the WriteStream passed to constructor's Write()
217  * function returns an invalid positive number.
218  * - Any negative value (i.e. with bits in kTvmErrorSystemErrorMask set) returned by the
219  * WriteStream's Write() function.
220  */
222 
223  /* \brief Reset state of the Framer. */
224  void Reset();
225 
226  private:
228  static constexpr const size_t kMaxStackBufferSizeBytes = 128;
229 
230  enum class State : uint8_t {
232  kReset = 0,
233 
235  kIdle = 1,
236 
238  kTransmitPacketPayload = 2,
239  };
240 
250  tvm_crt_error_t WriteAndCrc(const uint8_t* data, size_t data_size_bytes, bool escape,
251  bool update_crc);
252 
254  WriteStream* stream_;
255 
257  State state_;
258 
260  size_t num_payload_bytes_remaining_;
261 
263  uint16_t crc_;
264 };
265 
266 } // namespace micro_rpc
267 } // namespace runtime
268 } // namespace tvm
269 
270 #endif // TVM_RUNTIME_CRT_RPC_COMMON_FRAMING_H_
Definition: framing.h:144
tvm_crt_error_t Write(const uint8_t *payload, size_t payload_size_bytes)
Frame and write a full packet.
ssize_t(* WriteFunc)(const uint8_t *data, size_t data_size_bytes)
Definition: framing.h:146
tvm_crt_error_t WritePayloadChunk(const uint8_t *payload_chunk, size_t payload_chunk_size_bytes)
Write payload data to the wire.
tvm_crt_error_t FinishPacket()
tvm_crt_error_t StartPacket(size_t payload_size_bytes)
Start framing and writing a new packet to the wire.
Framer(WriteStream *stream)
Definition: framing.h:148
static constexpr const size_t kCrc
Definition: framing.h:44
static constexpr const size_t kPayloadLength
Definition: framing.h:43
Definition: framing.h:47
Unframer(WriteStream *stream)
Definition: framing.h:49
tvm_crt_error_t Write(const uint8_t *data, size_t data_size_bytes, size_t *bytes_consumed)
Push data into unframer and try to decode one packet.
void Reset()
Reset unframer to initial state.
size_t BytesNeeded()
Return an underestimate of the number of bytes needed from the wire.
Definition: write_stream.h:39
Defines integral error codes returned by the CRT.
tvm_crt_error_t
Definition: error_codes.h:50
Escape
Definition: framing.h:39
uint16_t crc16_compute(const uint8_t *data, size_t data_size_bytes, uint16_t *previous_crc)
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36