38 , m_linkService(linkService)
39 , m_firstUnackedFrag(m_unackedFrags.begin())
42 BOOST_ASSERT(m_linkService !=
nullptr);
52 m_idleAckTimer.cancel();
69 auto unackedFragsIt = m_unackedFrags.begin();
70 auto sendTime = time::steady_clock::now();
72 auto netPkt = make_shared<NetPkt>(std::move(pkt), isInterest);
73 netPkt->unackedFrags.reserve(frags.size());
75 for (lp::Packet& frag : frags) {
77 BOOST_ASSERT(frag.has<lp::SequenceField>());
80 lp::Sequence txSeq = assignTxSequence(frag);
83 unackedFragsIt = m_unackedFrags.emplace_hint(unackedFragsIt,
84 std::piecewise_construct,
85 std::forward_as_tuple(txSeq),
86 std::forward_as_tuple(frag));
87 unackedFragsIt->second.sendTime = sendTime;
88 auto rto = m_rttEst.getEstimatedRto();
89 lp::Sequence seq = frag.get<lp::SequenceField>();
91 time::duration_cast<time::milliseconds>(rto).count() <<
"ms");
92 unackedFragsIt->second.rtoTimer =
getScheduler().schedule(rto, [=] {
93 onLpPacketLost(txSeq,
true);
95 unackedFragsIt->second.netPkt = netPkt;
97 if (m_unackedFrags.size() == 1) {
98 m_firstUnackedFrag = m_unackedFrags.begin();
102 netPkt->unackedFrags.push_back(unackedFragsIt);
111 bool isDuplicate =
false;
112 auto now = time::steady_clock::now();
115 for (lp::Sequence ackTxSeq : pkt.list<lp::AckField>()) {
116 auto fragIt = m_unackedFrags.find(ackTxSeq);
117 if (fragIt == m_unackedFrags.end()) {
122 auto& frag = fragIt->second;
125 frag.rtoTimer.cancel();
127 if (frag.retxCount == 0) {
128 NFD_LOG_FACE_TRACE(
"received ack for seq=" << frag.pkt.get<lp::SequenceField>() <<
", txseq=" <<
129 ackTxSeq <<
", retx=0, rtt=" <<
130 time::duration_cast<time::milliseconds>(now - frag.sendTime).count() <<
"ms");
132 m_rttEst.addMeasurement(now - frag.sendTime);
135 NFD_LOG_FACE_TRACE(
"received ack for seq=" << frag.pkt.get<lp::SequenceField>() <<
", txseq=" <<
136 ackTxSeq <<
", retx=" << frag.retxCount);
142 auto lostLpPackets = findLostLpPackets(fragIt);
146 onLpPacketAcknowledged(fragIt);
152 std::set<lp::Sequence> removedLpPackets;
155 for (lp::Sequence txSeq : lostLpPackets) {
156 if (removedLpPackets.find(txSeq) == removedLpPackets.end()) {
157 auto removedTxSeqs = onLpPacketLost(txSeq,
false);
158 for (
auto removedTxSeq : removedTxSeqs) {
159 removedLpPackets.insert(removedTxSeq);
166 if (pkt.has<lp::FragmentField>() && pkt.has<lp::TxSequenceField>()) {
167 NFD_LOG_FACE_TRACE(
"queueing ack for remote txseq=" << pkt.get<lp::TxSequenceField>());
168 m_ackQueue.push(pkt.get<lp::TxSequenceField>());
171 if (pkt.has<lp::SequenceField>()) {
172 lp::Sequence pktSequence = pkt.get<lp::SequenceField>();
173 isDuplicate = m_recentRecvSeqs.count(pktSequence) > 0;
175 auto now = time::steady_clock::now();
176 auto rto = m_rttEst.getEstimatedRto();
177 while (m_recentRecvSeqsQueue.size() > 0 &&
178 now > m_recentRecvSeqs[m_recentRecvSeqsQueue.front()] + rto) {
179 m_recentRecvSeqs.erase(m_recentRecvSeqsQueue.front());
180 m_recentRecvSeqsQueue.pop();
182 m_recentRecvSeqs.emplace(pktSequence, now);
183 m_recentRecvSeqsQueue.push(pktSequence);
196 BOOST_ASSERT(pkt.wireEncode().type() == lp::tlv::LpPacket);
199 ssize_t pktSize = pkt.wireEncode().size();
200 ssize_t reservedSpace = tlv::sizeOfVarNumber(ndn::MAX_NDN_PACKET_SIZE) -
201 tlv::sizeOfVarNumber(pktSize);
202 ssize_t remainingSpace = (mtu ==
MTU_UNLIMITED ? ndn::MAX_NDN_PACKET_SIZE : mtu) - reservedSpace;
203 remainingSpace -= pktSize;
205 while (!m_ackQueue.empty()) {
206 lp::Sequence ackTxSeq = m_ackQueue.front();
208 const ssize_t ackSize = tlv::sizeOfVarNumber(lp::tlv::Ack) +
209 tlv::sizeOfVarNumber(
sizeof(lp::Sequence)) +
210 sizeof(lp::Sequence);
212 if (ackSize > remainingSpace) {
218 pkt.add<lp::AckField>(ackTxSeq);
220 remainingSpace -= ackSize;
225 LpReliability::assignTxSequence(lp::Packet& frag)
227 lp::Sequence txSeq = ++m_lastTxSeqNo;
228 frag.set<lp::TxSequenceField>(txSeq);
229 if (m_unackedFrags.size() > 0 && m_lastTxSeqNo == m_firstUnackedFrag->first) {
230 NDN_THROW(std::length_error(
"TxSequence range exceeded"));
232 return m_lastTxSeqNo;
236 LpReliability::startIdleAckTimer()
238 if (m_idleAckTimer) {
244 while (!m_ackQueue.empty()) {
245 m_linkService->requestIdlePacket();
250 std::vector<lp::Sequence>
251 LpReliability::findLostLpPackets(LpReliability::UnackedFrags::iterator ackIt)
253 std::vector<lp::Sequence> lostLpPackets;
255 for (
auto it = m_firstUnackedFrag; ; ++it) {
256 if (it == m_unackedFrags.end()) {
257 it = m_unackedFrags.begin();
260 if (it->first == ackIt->first) {
264 auto& unackedFrag = it->second;
265 unackedFrag.nGreaterSeqAcks++;
267 ", before count=" << unackedFrag.nGreaterSeqAcks);
270 lostLpPackets.push_back(it->first);
274 return lostLpPackets;
277 std::vector<lp::Sequence>
278 LpReliability::onLpPacketLost(lp::Sequence txSeq,
bool isTimeout)
280 BOOST_ASSERT(m_unackedFrags.count(txSeq) > 0);
281 auto txSeqIt = m_unackedFrags.find(txSeq);
283 auto& txFrag = txSeqIt->second;
284 txFrag.rtoTimer.cancel();
285 auto netPkt = txFrag.netPkt;
286 std::vector<lp::Sequence> removedThisTxSeq;
287 lp::Sequence seq = txFrag.pkt.get<lp::SequenceField>();
294 " considered lost from acks for more recent txseqs");
298 if (txFrag.retxCount >= m_options.
maxRetx) {
301 for (
size_t i = 0; i < netPkt->unackedFrags.size(); i++) {
302 if (netPkt->unackedFrags[i] != txSeqIt) {
303 removedThisTxSeq.push_back(netPkt->unackedFrags[i]->first);
304 deleteUnackedFrag(netPkt->unackedFrags[i]);
311 if (netPkt->isInterest) {
312 BOOST_ASSERT(netPkt->pkt.has<lp::FragmentField>());
313 ndn::Buffer::const_iterator fragBegin, fragEnd;
314 std::tie(fragBegin, fragEnd) = netPkt->pkt.get<lp::FragmentField>();
315 Block frag(&*fragBegin, std::distance(fragBegin, fragEnd));
320 removedThisTxSeq.push_back(txSeqIt->first);
321 deleteUnackedFrag(txSeqIt);
325 lp::Sequence newTxSeq = assignTxSequence(txFrag.pkt);
326 netPkt->didRetx =
true;
329 auto newTxFragIt = m_unackedFrags.emplace_hint(
330 m_firstUnackedFrag != m_unackedFrags.end() && m_firstUnackedFrag->first > newTxSeq
332 : m_unackedFrags.end(),
333 std::piecewise_construct,
334 std::forward_as_tuple(newTxSeq),
335 std::forward_as_tuple(txFrag.pkt));
336 auto& newTxFrag = newTxFragIt->second;
337 newTxFrag.retxCount = txFrag.retxCount + 1;
338 newTxFrag.netPkt = netPkt;
341 auto fragInNetPkt = std::find(netPkt->unackedFrags.begin(), netPkt->unackedFrags.end(), txSeqIt);
342 BOOST_ASSERT(fragInNetPkt != netPkt->unackedFrags.end());
343 *fragInNetPkt = newTxFragIt;
345 removedThisTxSeq.push_back(txSeqIt->first);
346 deleteUnackedFrag(txSeqIt);
349 m_linkService->sendLpPacket(lp::Packet(newTxFrag.pkt));
351 auto rto = m_rttEst.getEstimatedRto();
352 NFD_LOG_FACE_TRACE(
"retransmitting seq=" << seq <<
", txseq=" << newTxSeq <<
", retx=" <<
353 txFrag.retxCount <<
", rto=" <<
354 time::duration_cast<time::milliseconds>(rto).count() <<
"ms");
358 onLpPacketLost(newTxSeq,
true);
362 return removedThisTxSeq;
366 LpReliability::onLpPacketAcknowledged(UnackedFrags::iterator fragIt)
368 auto netPkt = fragIt->second.netPkt;
371 auto fragInNetPkt = std::find(netPkt->unackedFrags.begin(), netPkt->unackedFrags.end(), fragIt);
372 BOOST_ASSERT(fragInNetPkt != netPkt->unackedFrags.end());
373 *fragInNetPkt = netPkt->unackedFrags.back();
374 netPkt->unackedFrags.pop_back();
377 if (netPkt->unackedFrags.empty()) {
378 if (netPkt->didRetx) {
386 deleteUnackedFrag(fragIt);
390 LpReliability::deleteUnackedFrag(UnackedFrags::iterator fragIt)
392 lp::Sequence firstUnackedTxSeq = m_firstUnackedFrag->first;
393 lp::Sequence currentTxSeq = fragIt->first;
394 auto nextFragIt = m_unackedFrags.erase(fragIt);
396 if (!m_unackedFrags.empty() && firstUnackedTxSeq == currentTxSeq) {
398 if (nextFragIt == m_unackedFrags.end()) {
399 m_firstUnackedFrag = m_unackedFrags.begin();
402 m_firstUnackedFrag = nextFragIt;
405 else if (m_unackedFrags.empty()) {
406 m_firstUnackedFrag = m_unackedFrags.end();
410 LpReliability::UnackedFrag::UnackedFrag(lp::Packet pkt)
411 : pkt(std::move(pkt))
412 , sendTime(time::steady_clock::now())
418 LpReliability::NetPkt::NetPkt(lp::Packet&& pkt,
bool isInterest)
419 : pkt(std::move(pkt))
420 , isInterest(isInterest)
426 operator<<(std::ostream& os, const FaceLogHelper<LpReliability>& flh)
428 if (flh.obj.getLinkService() ==
nullptr) {
429 os <<
"[id=0,local=unknown,remote=unknown] ";
432 os << FaceLogHelper<LinkService>(*flh.obj.getLinkService());
#define NFD_LOG_FACE_TRACE(msg)
Log a message at TRACE level.
const ssize_t MTU_UNLIMITED
indicates the transport has no limit on payload size
#define NFD_LOG_FACE_DEBUG(msg)
Log a message at DEBUG level.
void piggyback(lp::Packet &pkt, ssize_t mtu)
called by GenericLinkService to attach Acks onto an outgoing LpPacket
bool isEnabled
enables link-layer reliability
PacketCounter nAcknowledged
count of network-layer packets that did not require retransmission of a fragment
GenericLinkService is a LinkService that implements the NDNLPv2 protocol.
size_t maxRetx
maximum number of retransmissions for an LpPacket
Scheduler & getScheduler()
Returns the global Scheduler instance for the calling thread.
const GenericLinkService * getLinkService() const
size_t seqNumLossThreshold
a fragment is considered lost if this number of fragments with greater sequence numbers are acknowled...
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
LpReliability(const Options &options, GenericLinkService *linkService)
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 ...
time::nanoseconds idleAckTimerPeriod
period between sending pending Acks in an IDLE packet
void setOptions(const Options &options)
set options for reliability
#define NFD_LOG_INIT(name)
signal::Signal< LpReliability, Interest > onDroppedInterest
signals on Interest dropped by reliability system for exceeding allowed number of retx ...
bool processIncomingPacket(const lp::Packet &pkt)
extract and parse all Acks and add Ack for contained Fragment (if any) to AckQueue ...
PacketCounter nRetxExhausted
count of network-layer packets dropped because a fragment reached the maximum number of retransmissio...
PacketCounter nRetransmitted
count of network-layer packets that had at least one fragment retransmitted, but were eventually rece...