31 #include <ndn-cxx/lp/fields.hpp>
41 , m_linkService(linkService)
42 , m_firstUnackedFrag(m_unackedFrags.begin())
45 BOOST_ASSERT(m_linkService !=
nullptr);
55 m_idleAckTimer.cancel();
66 auto unackedFragsIt = m_unackedFrags.begin();
67 auto sendTime = time::steady_clock::now();
69 auto netPkt = make_shared<NetPkt>(std::move(pkt), isInterest);
70 netPkt->unackedFrags.reserve(frags.size());
72 for (lp::Packet& frag : frags) {
74 BOOST_ASSERT(frag.has<lp::SequenceField>());
77 lp::Sequence txSeq = assignTxSequence(frag);
80 unackedFragsIt = m_unackedFrags.try_emplace(unackedFragsIt, txSeq, frag);
81 unackedFragsIt->second.sendTime = sendTime;
82 auto rto = m_rttEst.getEstimatedRto();
83 lp::Sequence seq = frag.get<lp::SequenceField>();
85 time::duration_cast<time::milliseconds>(rto).count() <<
"ms");
86 unackedFragsIt->second.rtoTimer =
getScheduler().schedule(rto, [=] {
87 onLpPacketLost(txSeq,
true);
89 unackedFragsIt->second.netPkt = netPkt;
91 if (m_unackedFrags.size() == 1) {
92 m_firstUnackedFrag = m_unackedFrags.begin();
96 netPkt->unackedFrags.push_back(unackedFragsIt);
105 bool isDuplicate =
false;
106 auto now = time::steady_clock::now();
109 for (lp::Sequence ackTxSeq : pkt.list<lp::AckField>()) {
110 auto fragIt = m_unackedFrags.find(ackTxSeq);
111 if (fragIt == m_unackedFrags.end()) {
116 auto& frag = fragIt->second;
119 frag.rtoTimer.cancel();
121 if (frag.retxCount == 0) {
122 NFD_LOG_FACE_TRACE(
"received ack for seq=" << frag.pkt.get<lp::SequenceField>() <<
", txseq=" <<
123 ackTxSeq <<
", retx=0, rtt=" <<
124 time::duration_cast<time::milliseconds>(now - frag.sendTime).count() <<
"ms");
126 m_rttEst.addMeasurement(now - frag.sendTime);
129 NFD_LOG_FACE_TRACE(
"received ack for seq=" << frag.pkt.get<lp::SequenceField>() <<
", txseq=" <<
130 ackTxSeq <<
", retx=" << frag.retxCount);
136 auto lostLpPackets = findLostLpPackets(fragIt);
140 onLpPacketAcknowledged(fragIt);
146 std::set<lp::Sequence> removedLpPackets;
149 for (lp::Sequence txSeq : lostLpPackets) {
150 if (removedLpPackets.find(txSeq) == removedLpPackets.end()) {
151 auto removedTxSeqs = onLpPacketLost(txSeq,
false);
152 for (
auto removedTxSeq : removedTxSeqs) {
153 removedLpPackets.insert(removedTxSeq);
160 if (pkt.has<lp::FragmentField>() && pkt.has<lp::TxSequenceField>()) {
161 NFD_LOG_FACE_TRACE(
"queueing ack for remote txseq=" << pkt.get<lp::TxSequenceField>());
162 m_ackQueue.push(pkt.get<lp::TxSequenceField>());
165 if (pkt.has<lp::SequenceField>()) {
166 lp::Sequence pktSequence = pkt.get<lp::SequenceField>();
167 isDuplicate = m_recentRecvSeqs.count(pktSequence) > 0;
169 auto now = time::steady_clock::now();
170 auto rto = m_rttEst.getEstimatedRto();
171 while (!m_recentRecvSeqsQueue.empty() &&
172 now > m_recentRecvSeqs[m_recentRecvSeqsQueue.front()] + rto) {
173 m_recentRecvSeqs.erase(m_recentRecvSeqsQueue.front());
174 m_recentRecvSeqsQueue.pop();
176 m_recentRecvSeqs.try_emplace(pktSequence, now);
177 m_recentRecvSeqsQueue.push(pktSequence);
190 BOOST_ASSERT(pkt.wireEncode().type() == lp::tlv::LpPacket);
193 ssize_t pktSize = pkt.wireEncode().size();
194 ssize_t reservedSpace = tlv::sizeOfVarNumber(ndn::MAX_NDN_PACKET_SIZE) -
195 tlv::sizeOfVarNumber(pktSize);
196 ssize_t remainingSpace = (mtu ==
MTU_UNLIMITED ? ndn::MAX_NDN_PACKET_SIZE : mtu) - reservedSpace;
197 remainingSpace -= pktSize;
199 while (!m_ackQueue.empty()) {
200 lp::Sequence ackTxSeq = m_ackQueue.front();
202 const ssize_t ackSize = tlv::sizeOfVarNumber(lp::tlv::Ack) +
203 tlv::sizeOfVarNumber(
sizeof(lp::Sequence)) +
204 sizeof(lp::Sequence);
206 if (ackSize > remainingSpace) {
212 pkt.add<lp::AckField>(ackTxSeq);
214 remainingSpace -= ackSize;
219 LpReliability::assignTxSequence(lp::Packet& frag)
221 lp::Sequence txSeq = ++m_lastTxSeqNo;
222 frag.set<lp::TxSequenceField>(txSeq);
223 if (!m_unackedFrags.empty() && m_lastTxSeqNo == m_firstUnackedFrag->first) {
224 NDN_THROW(std::length_error(
"TxSequence range exceeded"));
226 return m_lastTxSeqNo;
230 LpReliability::startIdleAckTimer()
232 if (m_idleAckTimer) {
238 while (!m_ackQueue.empty()) {
239 m_linkService->requestIdlePacket();
244 std::vector<lp::Sequence>
245 LpReliability::findLostLpPackets(LpReliability::UnackedFrags::iterator ackIt)
247 std::vector<lp::Sequence> lostLpPackets;
249 for (
auto it = m_firstUnackedFrag; ; ++it) {
250 if (it == m_unackedFrags.end()) {
251 it = m_unackedFrags.begin();
254 if (it->first == ackIt->first) {
258 auto& unackedFrag = it->second;
259 unackedFrag.nGreaterSeqAcks++;
261 ", before count=" << unackedFrag.nGreaterSeqAcks);
263 if (unackedFrag.nGreaterSeqAcks >= m_options.seqNumLossThreshold) {
264 lostLpPackets.push_back(it->first);
268 return lostLpPackets;
271 std::vector<lp::Sequence>
272 LpReliability::onLpPacketLost(lp::Sequence txSeq,
bool isTimeout)
274 BOOST_ASSERT(m_unackedFrags.count(txSeq) > 0);
275 auto txSeqIt = m_unackedFrags.find(txSeq);
277 auto& txFrag = txSeqIt->second;
278 txFrag.rtoTimer.cancel();
279 auto netPkt = txFrag.netPkt;
280 std::vector<lp::Sequence> removedThisTxSeq;
281 lp::Sequence seq = txFrag.pkt.get<lp::SequenceField>();
288 " considered lost from acks for more recent txseqs");
292 if (txFrag.retxCount >= m_options.maxRetx) {
295 for (
size_t i = 0; i < netPkt->unackedFrags.size(); i++) {
296 if (netPkt->unackedFrags[i] != txSeqIt) {
297 removedThisTxSeq.push_back(netPkt->unackedFrags[i]->first);
298 deleteUnackedFrag(netPkt->unackedFrags[i]);
302 ++m_linkService->nRetxExhausted;
305 if (netPkt->isInterest) {
306 BOOST_ASSERT(netPkt->pkt.has<lp::FragmentField>());
307 auto frag = netPkt->pkt.get<lp::FragmentField>();
308 onDroppedInterest(Interest(Block({frag.first, frag.second})));
312 removedThisTxSeq.push_back(txSeqIt->first);
313 deleteUnackedFrag(txSeqIt);
317 lp::Sequence newTxSeq = assignTxSequence(txFrag.pkt);
318 netPkt->didRetx =
true;
321 auto hint = m_firstUnackedFrag != m_unackedFrags.end() && m_firstUnackedFrag->first > newTxSeq
323 : m_unackedFrags.end();
324 auto newTxFragIt = m_unackedFrags.try_emplace(hint, newTxSeq, txFrag.pkt);
325 auto& newTxFrag = newTxFragIt->second;
326 newTxFrag.retxCount = txFrag.retxCount + 1;
327 newTxFrag.netPkt = netPkt;
330 auto fragInNetPkt = std::find(netPkt->unackedFrags.begin(), netPkt->unackedFrags.end(), txSeqIt);
331 BOOST_ASSERT(fragInNetPkt != netPkt->unackedFrags.end());
332 *fragInNetPkt = newTxFragIt;
334 removedThisTxSeq.push_back(txSeqIt->first);
335 deleteUnackedFrag(txSeqIt);
338 m_linkService->sendLpPacket(lp::Packet(newTxFrag.pkt));
340 auto rto = m_rttEst.getEstimatedRto();
341 NFD_LOG_FACE_TRACE(
"retransmitting seq=" << seq <<
", txseq=" << newTxSeq <<
", retx=" <<
342 txFrag.retxCount <<
", rto=" <<
343 time::duration_cast<time::milliseconds>(rto).count() <<
"ms");
347 onLpPacketLost(newTxSeq,
true);
351 return removedThisTxSeq;
355 LpReliability::onLpPacketAcknowledged(UnackedFrags::iterator fragIt)
357 auto netPkt = fragIt->second.netPkt;
360 auto fragInNetPkt = std::find(netPkt->unackedFrags.begin(), netPkt->unackedFrags.end(), fragIt);
361 BOOST_ASSERT(fragInNetPkt != netPkt->unackedFrags.end());
362 *fragInNetPkt = netPkt->unackedFrags.back();
363 netPkt->unackedFrags.pop_back();
366 if (netPkt->unackedFrags.empty()) {
367 if (netPkt->didRetx) {
368 ++m_linkService->nRetransmitted;
371 ++m_linkService->nAcknowledged;
375 deleteUnackedFrag(fragIt);
379 LpReliability::deleteUnackedFrag(UnackedFrags::iterator fragIt)
381 lp::Sequence firstUnackedTxSeq = m_firstUnackedFrag->first;
382 lp::Sequence currentTxSeq = fragIt->first;
383 auto nextFragIt = m_unackedFrags.erase(fragIt);
385 if (!m_unackedFrags.empty() && firstUnackedTxSeq == currentTxSeq) {
387 if (nextFragIt == m_unackedFrags.end()) {
388 m_firstUnackedFrag = m_unackedFrags.begin();
391 m_firstUnackedFrag = nextFragIt;
394 else if (m_unackedFrags.empty()) {
395 m_firstUnackedFrag = m_unackedFrags.end();
402 if (flh.
obj.getLinkService() ==
nullptr) {
403 os <<
"[id=0,local=unknown,remote=unknown] ";
406 os << FaceLogHelper<LinkService>(*flh.
obj.getLinkService());
For internal use by FaceLogging macros.
GenericLinkService is a LinkService that implements the NDNLPv2 protocol.
LpReliability(const Options &options, GenericLinkService *linkService)
void piggyback(lp::Packet &pkt, ssize_t mtu)
Called by GenericLinkService to attach Acks onto an outgoing LpPacket.
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 processIncomingPacket(const lp::Packet &pkt)
Extract and parse all Acks and add Ack for contained Fragment (if any) to AckQueue.
void setOptions(const Options &options)
Set options for reliability.
#define NFD_LOG_FACE_DEBUG(msg)
Log a message at DEBUG level.
#define NFD_LOG_FACE_TRACE(msg)
Log a message at TRACE level.
#define NFD_LOG_INIT(name)
constexpr ssize_t MTU_UNLIMITED
Indicates that the transport has no limit on payload size.
ndn::Scheduler & getScheduler()
Returns the global Scheduler instance for the calling thread.
std::ostream & operator<<(std::ostream &os, const Network &network)
time::nanoseconds idleAckTimerPeriod
Period between sending pending Acks in an IDLE packet.
bool isEnabled
Enables link-layer reliability.