35 , m_linkService(linkService)
36 , m_firstUnackedFrag(m_unackedFrags.begin())
38 , m_isIdleAckTimerRunning(false)
40 BOOST_ASSERT(m_linkService !=
nullptr);
51 this->stopIdleAckTimer();
68 auto unackedFragsIt = m_unackedFrags.begin();
71 auto netPkt = make_shared<NetPkt>(std::move(pkt), isInterest);
72 netPkt->unackedFrags.reserve(frags.size());
79 unackedFragsIt = m_unackedFrags.emplace_hint(unackedFragsIt,
80 std::piecewise_construct,
81 std::forward_as_tuple(txSeq),
82 std::forward_as_tuple(frag));
83 unackedFragsIt->second.sendTime = sendTime;
84 unackedFragsIt->second.rtoTimer =
86 unackedFragsIt->second.netPkt = netPkt;
88 if (m_unackedFrags.size() == 1) {
89 m_firstUnackedFrag = m_unackedFrags.begin();
93 netPkt->unackedFrags.push_back(unackedFragsIt);
106 auto fragIt = m_unackedFrags.find(ackSeq);
107 if (fragIt == m_unackedFrags.end()) {
111 auto& frag = fragIt->second;
114 frag.rtoTimer.cancel();
116 if (frag.retxCount == 0) {
118 m_rto.
addMeasurement(time::duration_cast<RttEstimator::Duration>(now - frag.sendTime));
124 auto lostLpPackets = findLostLpPackets(fragIt);
128 onLpPacketAcknowledged(fragIt);
132 this->onLpPacketLost(txSeqIt);
139 if (!m_isIdleAckTimerRunning) {
140 this->startIdleAckTimer();
156 remainingSpace -= pktSize;
158 while (!m_ackQueue.empty()) {
166 if (ackSize > remainingSpace) {
172 remainingSpace -= ackSize;
177 LpReliability::assignTxSequence(
lp::Packet& frag)
181 if (m_unackedFrags.size() > 0 && m_lastTxSeqNo == m_firstUnackedFrag->first) {
182 BOOST_THROW_EXCEPTION(std::length_error(
"TxSequence range exceeded"));
184 return m_lastTxSeqNo;
188 LpReliability::startIdleAckTimer()
190 BOOST_ASSERT(!m_isIdleAckTimerRunning);
191 m_isIdleAckTimerRunning =
true;
194 while (!m_ackQueue.empty()) {
195 m_linkService->requestIdlePacket();
198 m_isIdleAckTimerRunning =
false;
203 LpReliability::stopIdleAckTimer()
205 m_idleAckTimer.cancel();
206 m_isIdleAckTimerRunning =
false;
209 std::vector<LpReliability::UnackedFrags::iterator>
212 std::vector<UnackedFrags::iterator> lostLpPackets;
214 for (
auto it = m_firstUnackedFrag; ; ++it) {
215 if (it == m_unackedFrags.end()) {
216 it = m_unackedFrags.begin();
219 if (it->first == ackIt->first) {
223 auto& unackedFrag = it->second;
224 unackedFrag.nGreaterSeqAcks++;
226 if (unackedFrag.nGreaterSeqAcks >= m_options.seqNumLossThreshold) {
227 lostLpPackets.push_back(it);
231 return lostLpPackets;
237 BOOST_ASSERT(m_unackedFrags.count(txSeqIt->first) > 0);
239 auto& txFrag = txSeqIt->second;
240 txFrag.rtoTimer.cancel();
241 auto netPkt = txFrag.netPkt;
244 if (txFrag.retxCount >= m_options.maxRetx) {
246 for (
size_t i = 0; i < netPkt->unackedFrags.size(); i++) {
247 if (netPkt->unackedFrags[i] != txSeqIt) {
248 deleteUnackedFrag(netPkt->unackedFrags[i]);
252 ++m_linkService->nRetxExhausted;
255 if (netPkt->isInterest) {
257 ndn::Buffer::const_iterator fragBegin, fragEnd;
259 Block frag(&*fragBegin, std::distance(fragBegin, fragEnd));
263 deleteUnackedFrag(txSeqIt);
268 netPkt->didRetx =
true;
271 auto newTxFragIt = m_unackedFrags.emplace_hint(
272 m_firstUnackedFrag != m_unackedFrags.end() && m_firstUnackedFrag->first > newTxSeq
274 : m_unackedFrags.end(),
275 std::piecewise_construct,
276 std::forward_as_tuple(newTxSeq),
277 std::forward_as_tuple(txFrag.pkt));
278 auto& newTxFrag = newTxFragIt->second;
279 newTxFrag.retxCount = txFrag.retxCount + 1;
280 newTxFrag.netPkt = netPkt;
283 auto fragInNetPkt = std::find(netPkt->unackedFrags.begin(), netPkt->unackedFrags.end(), txSeqIt);
284 BOOST_ASSERT(fragInNetPkt != netPkt->unackedFrags.end());
285 *fragInNetPkt = newTxFragIt;
287 deleteUnackedFrag(txSeqIt);
290 m_linkService->sendLpPacket(
lp::Packet(newTxFrag.pkt));
294 bind(&LpReliability::onLpPacketLost,
this, newTxFragIt));
301 auto netPkt = fragIt->second.netPkt;
304 auto fragInNetPkt = std::find(netPkt->unackedFrags.begin(), netPkt->unackedFrags.end(), fragIt);
305 BOOST_ASSERT(fragInNetPkt != netPkt->unackedFrags.end());
306 *fragInNetPkt = netPkt->unackedFrags.back();
307 netPkt->unackedFrags.pop_back();
310 if (netPkt->unackedFrags.empty()) {
311 if (netPkt->didRetx) {
312 ++m_linkService->nRetransmitted;
315 ++m_linkService->nAcknowledged;
319 deleteUnackedFrag(fragIt);
325 lp::Sequence firstUnackedTxSeq = m_firstUnackedFrag->first;
327 auto nextFragIt = m_unackedFrags.erase(fragIt);
329 if (!m_unackedFrags.empty() && firstUnackedTxSeq == currentTxSeq) {
331 if (nextFragIt == m_unackedFrags.end()) {
332 m_firstUnackedFrag = m_unackedFrags.begin();
335 m_firstUnackedFrag = nextFragIt;
338 else if (m_unackedFrags.empty()) {
339 m_firstUnackedFrag = m_unackedFrags.end();
343 LpReliability::UnackedFrag::UnackedFrag(
lp::Packet pkt)
344 : pkt(
std::move(pkt))
345 , sendTime(
time::steady_clock::now())
351 LpReliability::NetPkt::NetPkt(
lp::Packet&& pkt,
bool isInterest)
352 : pkt(
std::move(pkt))
353 , isInterest(isInterest)
Duration computeRto() const
std::vector< typename FIELD::ValueType > list() const
void setOptions(const Options &options)
set options for reliability
GenericLinkService is a LinkService that implements the NDNLPv2 protocol.
void processIncomingPacket(const lp::Packet &pkt)
extract and parse all Acks and add Ack for contained Fragment (if any) to AckQueue ...
Packet & set(const typename FIELD::ValueType &value)
remove all occurrences of FIELD, and add a FIELD with value
const ssize_t MTU_UNLIMITED
indicates the transport has no limit on payload size
static time_point now() noexcept
void piggyback(lp::Packet &pkt, ssize_t mtu)
called by GenericLinkService to attach Acks onto an outgoing LpPacket
Packet & add(const typename FIELD::ValueType &value)
add a FIELD with value
void handleOutgoing(std::vector< lp::Packet > &frags, lp::Packet &&pkt, bool isInterest)
observe outgoing fragment(s) of a network packet and store for potential retransmission ...
bool isEnabled
enables link-layer reliability
uint64_t Sequence
represents a sequence number
Table::const_iterator iterator
void addMeasurement(Duration measure)
size_t size() const
Get size of encoded wire, including Type-Length-Value.
FIELD::ValueType get(size_t index=0) const
Copyright (c) 2011-2015 Regents of the University of California.
constexpr size_t sizeOfNonNegativeInteger(uint64_t integer)
Get number of bytes necessary to hold value of nonNegativeInteger.
const GenericLinkService * getLinkService() const
LpReliability(const Options &options, GenericLinkService *linkService)
EventId schedule(time::nanoseconds after, const EventCallback &event)
schedule an event
Block wireEncode() const
encode packet into wire format
time::nanoseconds idleAckTimerPeriod
period between sending pending Acks in an IDLE packet
constexpr size_t sizeOfVarNumber(uint64_t number)
Get number of bytes necessary to hold value of VAR-NUMBER.
uint32_t type() const
Get TLV-TYPE.
const size_t MAX_NDN_PACKET_SIZE
practical limit of network layer packet size