Hybrid ICN (hICN) plugin  v21.06-rc0-4-g18fa668
rely.h
1 /*
2  * Copyright (c) 2021 Cisco and/or its affiliates.
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 
16 #pragma once
17 
18 #include <hicn/transport/utils/chrono_typedefs.h>
19 #include <hicn/transport/utils/membuf.h>
20 #include <protocols/fec/fec_info.h>
21 #include <protocols/fec_base.h>
22 
23 #include <rely/decoder.hpp>
24 #include <rely/encoder.hpp>
25 
26 #define RELY_DEBUG 0
27 
28 namespace transport {
29 namespace protocol {
30 namespace fec {
31 
35 #define foreach_rely_fec_type \
36  _(Rely, 1, 3) \
37  _(Rely, 2, 3) \
38  _(Rely, 4, 5) \
39  _(Rely, 4, 6) \
40  _(Rely, 4, 7) \
41  _(Rely, 6, 10) \
42  _(Rely, 8, 10) \
43  _(Rely, 8, 11) \
44  _(Rely, 8, 12) \
45  _(Rely, 8, 14) \
46  _(Rely, 8, 16) \
47  _(Rely, 8, 32) \
48  _(Rely, 10, 30) \
49  _(Rely, 10, 40) \
50  _(Rely, 10, 90) \
51  _(Rely, 16, 21) \
52  _(Rely, 16, 23) \
53  _(Rely, 16, 24) \
54  _(Rely, 16, 27) \
55  _(Rely, 17, 21) \
56  _(Rely, 17, 34) \
57  _(Rely, 32, 41) \
58  _(Rely, 32, 46) \
59  _(Rely, 32, 54) \
60  _(Rely, 34, 42) \
61  _(Rely, 35, 70) \
62  _(Rely, 52, 62)
63 
67 class RelyBase : public virtual FECBase {
68  protected:
69  static const constexpr size_t kmax_stream_size = 125U;
70  static const constexpr size_t kmtu = 1500U;
71  static const constexpr size_t ktimeout = 100U;
77  struct fec_header {
78  uint32_t seq_number;
79 
80  void setSeqNumberBase(uint32_t suffix) { seq_number = htonl(suffix); }
81  uint32_t getSeqNumberBase() { return ntohl(seq_number); }
82  };
83 
93  RelyBase(uint32_t k, uint32_t n, uint32_t seq_offset = 0)
94  : k_(k),
95  n_(n),
96  seq_offset_(seq_offset % n_),
97  current_index_(seq_offset)
98 #if RELY_DEBUG
99  ,
100  time_(0)
101 #endif
102  {
103  }
104 
110  int64_t getCurrentTime() {
111  // Get the current time
112 #if RELY_DEBUG
113  return time_++;
114 #else
115  auto _time = utils::SteadyClock::now().time_since_epoch();
116  auto time = std::chrono::duration_cast<utils::Milliseconds>(_time).count();
117  return time;
118 #endif
119  }
120 
121  protected:
122  uint32_t k_;
123  uint32_t n_;
124  std::uint32_t seq_offset_;
129  std::vector<std::pair<uint32_t, buffer>> packets_;
130 
135  uint32_t current_index_;
136 #if RELY_DEBUG
137  uint32_t time_;
138 #endif
139 };
140 
145 class RelyEncoder : private RelyBase,
146  private rely::encoder,
147  public ProducerFEC {
148  public:
149  RelyEncoder(uint32_t k, uint32_t n, uint32_t seq_offset = 0);
153  void onPacketProduced(core::ContentObject &content_object,
154  uint32_t offset) override;
155 
159  std::size_t getFecHeaderSize() override {
160  return header_bytes() + sizeof(fec_header) + 4;
161  }
162 
163  void reset() override {}
164 };
165 
166 class RelyDecoder : private RelyBase,
167  private rely::decoder,
168  public ConsumerFEC {
169  public:
170  RelyDecoder(uint32_t k, uint32_t n, uint32_t seq_offset = 0);
171 
175  void onDataPacket(core::ContentObject &content_object,
176  uint32_t offset) override;
177 
181  std::size_t getFecHeaderSize() override {
182  return header_bytes() + sizeof(fec_header);
183  }
184 
185  void reset() override {}
186 };
187 
188 } // namespace fec
189 
190 } // namespace protocol
191 } // namespace transport
transport::protocol::fec::RelyEncoder::onPacketProduced
void onPacketProduced(core::ContentObject &content_object, uint32_t offset) override
transport::protocol::fec::RelyBase::RelyBase
RelyBase(uint32_t k, uint32_t n, uint32_t seq_offset=0)
Construct a new Rely Base object.
Definition: rely.h:93
transport::protocol::fec::RelyBase::current_index_
uint32_t current_index_
Current index to be used for local packet count.
Definition: rely.h:135
transport::protocol::fec::ProducerFEC
Definition: fec_base.h:77
transport::core::ContentObject
Definition: content_object.h:29
transport::protocol::fec::RelyBase::packets_
std::vector< std::pair< uint32_t, buffer > > packets_
Vector of packets to be passed to caller callbacks. For encoder it will contain the repair packets,...
Definition: rely.h:129
transport::protocol::fec::RelyEncoder::getFecHeaderSize
std::size_t getFecHeaderSize() override
Get the fec header size, if added to source packets.
Definition: rely.h:159
transport::protocol::fec::ConsumerFEC
Definition: fec_base.h:90
transport::protocol::fec::FECBase
Definition: fec_base.h:31
transport
Definition: forwarder_config.h:32
transport::protocol::fec::RelyDecoder::getFecHeaderSize
std::size_t getFecHeaderSize() override
Get the fec header size, if added to source packets.
Definition: rely.h:181
transport::protocol::fec::RelyDecoder
Definition: rely.h:166
transport::protocol::fec::RelyBase
Base class to store common fields.
Definition: rely.h:67
transport::protocol::fec::RelyBase::fec_header
FEC Header, added to each packet to get sequence number upon decoding operations. It may be removed o...
Definition: rely.h:77
transport::protocol::fec::RelyDecoder::onDataPacket
void onDataPacket(core::ContentObject &content_object, uint32_t offset) override
transport::protocol::fec::RelyEncoder
The Rely Encoder implementation.
Definition: rely.h:145
transport::protocol::fec::RelyBase::getCurrentTime
int64_t getCurrentTime()
Get the current time in milliseconds.
Definition: rely.h:110