NFD: Named Data Networking Forwarding Daemon 24.07-28-gdcc0e6e0
Loading...
Searching...
No Matches
lp-reliability.cpp
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014-2024, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#include "lp-reliability.hpp"
27#include "common/global.hpp"
29#include "transport.hpp"
30
31#include <ndn-cxx/lp/fields.hpp>
32
33#include <set>
34
35namespace nfd::face {
36
37NFD_LOG_INIT(LpReliability);
38
40 : m_options(options)
41 , m_linkService(linkService)
42 , m_firstUnackedFrag(m_unackedFrags.begin())
43 , m_lastTxSeqNo(-1) // set to "-1" to start TxSequence numbers at 0
44{
45 BOOST_ASSERT(m_linkService != nullptr);
46 BOOST_ASSERT(m_options.idleAckTimerPeriod > 0_ns);
47}
48
49void
51{
52 BOOST_ASSERT(options.idleAckTimerPeriod > 0_ns);
53
54 if (m_options.isEnabled && !options.isEnabled) {
55 m_idleAckTimer.cancel();
56 }
57
58 m_options = options;
59}
60
61void
62LpReliability::handleOutgoing(std::vector<lp::Packet>& frags, lp::Packet&& pkt, bool isInterest)
63{
64 BOOST_ASSERT(m_options.isEnabled);
65
66 auto unackedFragsIt = m_unackedFrags.begin();
67 auto sendTime = time::steady_clock::now();
68
69 auto netPkt = make_shared<NetPkt>(std::move(pkt), isInterest);
70 netPkt->unackedFrags.reserve(frags.size());
71
72 for (lp::Packet& frag : frags) {
73 // Non-IDLE packets are required to have assigned Sequence numbers with LpReliability enabled
74 BOOST_ASSERT(frag.has<lp::SequenceField>());
75
76 // Assign TxSequence number
77 lp::Sequence txSeq = assignTxSequence(frag);
78
79 // Store LpPacket for future retransmissions
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>();
84 NFD_LOG_FACE_TRACE("transmitting seq=" << seq << ", txseq=" << txSeq << ", rto=" <<
85 time::duration_cast<time::milliseconds>(rto).count() << "ms");
86 unackedFragsIt->second.rtoTimer = getScheduler().schedule(rto, [=] {
87 onLpPacketLost(txSeq, true);
88 });
89 unackedFragsIt->second.netPkt = netPkt;
90
91 if (m_unackedFrags.size() == 1) {
92 m_firstUnackedFrag = m_unackedFrags.begin();
93 }
94
95 // Add to associated NetPkt
96 netPkt->unackedFrags.push_back(unackedFragsIt);
97 }
98}
99
100bool
102{
103 BOOST_ASSERT(m_options.isEnabled);
104
105 bool isDuplicate = false;
106 auto now = time::steady_clock::now();
107
108 // Extract and parse Acks
109 for (lp::Sequence ackTxSeq : pkt.list<lp::AckField>()) {
110 auto fragIt = m_unackedFrags.find(ackTxSeq);
111 if (fragIt == m_unackedFrags.end()) {
112 // Ignore an Ack for an unknown TxSequence number
113 NFD_LOG_FACE_DEBUG("received ack for unknown txseq=" << ackTxSeq);
114 continue;
115 }
116 auto& frag = fragIt->second;
117
118 // Cancel the RTO timer for the acknowledged fragment
119 frag.rtoTimer.cancel();
120
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");
125 // This sequence had no retransmissions, so use it to estimate the RTO
126 m_rttEst.addMeasurement(now - frag.sendTime);
127 }
128 else {
129 NFD_LOG_FACE_TRACE("received ack for seq=" << frag.pkt.get<lp::SequenceField>() << ", txseq=" <<
130 ackTxSeq << ", retx=" << frag.retxCount);
131 }
132
133 // Look for frags with TxSequence numbers < ackTxSeq (allowing for wraparound) and consider
134 // them lost if a configurable number of Acks containing greater TxSequence numbers have been
135 // received.
136 auto lostLpPackets = findLostLpPackets(fragIt);
137
138 // Remove the fragment from the map of unacknowledged fragments and from its associated network
139 // packet. Potentially increment the start of the window.
140 onLpPacketAcknowledged(fragIt);
141
142 // This set contains TxSequences that have been removed by onLpPacketLost below because they
143 // were part of a network packet that was removed due to a fragment exceeding retx, as well as
144 // any other TxSequences removed by onLpPacketLost. This prevents onLpPacketLost from being
145 // called later for an invalid iterator.
146 std::set<lp::Sequence> removedLpPackets;
147
148 // Resend or fail fragments considered lost. Potentially increment the start of the window.
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);
154 }
155 }
156 }
157 }
158
159 // If packet has Fragment and TxSequence fields, extract TxSequence and add to AckQueue
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>());
163
164 // Check for received frames with duplicate Sequences
165 if (pkt.has<lp::SequenceField>()) {
166 lp::Sequence pktSequence = pkt.get<lp::SequenceField>();
167 isDuplicate = m_recentRecvSeqs.count(pktSequence) > 0;
168 // Check for recent received Sequences to remove
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();
175 }
176 m_recentRecvSeqs.try_emplace(pktSequence, now);
177 m_recentRecvSeqsQueue.push(pktSequence);
178 }
179
180 startIdleAckTimer();
181 }
182
183 return !isDuplicate;
184}
185
186void
187LpReliability::piggyback(lp::Packet& pkt, ssize_t mtu)
188{
189 BOOST_ASSERT(m_options.isEnabled);
190 BOOST_ASSERT(pkt.wireEncode().type() == lp::tlv::LpPacket);
191
192 // up to 2 extra octets reserved for potential TLV-LENGTH size increases
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;
198
199 while (!m_ackQueue.empty()) {
200 lp::Sequence ackTxSeq = m_ackQueue.front();
201 // Ack size = Ack TLV-TYPE (3 octets) + TLV-LENGTH (1 octet) + lp::Sequence (8 octets)
202 const ssize_t ackSize = tlv::sizeOfVarNumber(lp::tlv::Ack) +
203 tlv::sizeOfVarNumber(sizeof(lp::Sequence)) +
204 sizeof(lp::Sequence);
205
206 if (ackSize > remainingSpace) {
207 break;
208 }
209
210 NFD_LOG_FACE_TRACE("piggybacking ack for remote txseq=" << ackTxSeq);
211
212 pkt.add<lp::AckField>(ackTxSeq);
213 m_ackQueue.pop();
214 remainingSpace -= ackSize;
215 }
216}
217
218lp::Sequence
219LpReliability::assignTxSequence(lp::Packet& frag)
220{
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"));
225 }
226 return m_lastTxSeqNo;
227}
228
229void
230LpReliability::startIdleAckTimer()
231{
232 if (m_idleAckTimer) {
233 // timer is already running, do nothing
234 return;
235 }
236
237 m_idleAckTimer = getScheduler().schedule(m_options.idleAckTimerPeriod, [this] {
238 while (!m_ackQueue.empty()) {
239 m_linkService->requestIdlePacket();
240 }
241 });
242}
243
244std::vector<lp::Sequence>
245LpReliability::findLostLpPackets(LpReliability::UnackedFrags::iterator ackIt)
246{
247 std::vector<lp::Sequence> lostLpPackets;
248
249 for (auto it = m_firstUnackedFrag; ; ++it) {
250 if (it == m_unackedFrags.end()) {
251 it = m_unackedFrags.begin();
252 }
253
254 if (it->first == ackIt->first) {
255 break;
256 }
257
258 auto& unackedFrag = it->second;
259 unackedFrag.nGreaterSeqAcks++;
260 NFD_LOG_FACE_TRACE("received ack=" << ackIt->first << " before=" << it->first <<
261 ", before count=" << unackedFrag.nGreaterSeqAcks);
262
263 if (unackedFrag.nGreaterSeqAcks >= m_options.seqNumLossThreshold) {
264 lostLpPackets.push_back(it->first);
265 }
266 }
267
268 return lostLpPackets;
269}
270
271std::vector<lp::Sequence>
272LpReliability::onLpPacketLost(lp::Sequence txSeq, bool isTimeout)
273{
274 BOOST_ASSERT(m_unackedFrags.count(txSeq) > 0);
275 auto txSeqIt = m_unackedFrags.find(txSeq);
276
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>();
282
283 if (isTimeout) {
284 NFD_LOG_FACE_TRACE("rto timer expired for seq=" << seq << ", txseq=" << txSeq);
285 }
286 else { // lost due to out-of-order TxSeqs
287 NFD_LOG_FACE_TRACE("seq=" << seq << ", txseq=" << txSeq <<
288 " considered lost from acks for more recent txseqs");
289 }
290
291 // Check if maximum number of retransmissions exceeded
292 if (txFrag.retxCount >= m_options.maxRetx) {
293 NFD_LOG_FACE_DEBUG("seq=" << seq << " exceeded allowed retransmissions: DROP");
294 // Delete all LpPackets of NetPkt from m_unackedFrags (except this one)
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]);
299 }
300 }
301
302 ++m_linkService->nRetxExhausted;
303
304 // Notify strategy of dropped Interest (if any)
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})));
309 }
310
311 // Delete this LpPacket from m_unackedFrags
312 removedThisTxSeq.push_back(txSeqIt->first);
313 deleteUnackedFrag(txSeqIt);
314 }
315 else {
316 // Assign new TxSequence
317 lp::Sequence newTxSeq = assignTxSequence(txFrag.pkt);
318 netPkt->didRetx = true;
319
320 // Move fragment to new TxSequence mapping
321 auto hint = m_firstUnackedFrag != m_unackedFrags.end() && m_firstUnackedFrag->first > newTxSeq
322 ? m_firstUnackedFrag
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;
328
329 // Update associated NetPkt
330 auto fragInNetPkt = std::find(netPkt->unackedFrags.begin(), netPkt->unackedFrags.end(), txSeqIt);
331 BOOST_ASSERT(fragInNetPkt != netPkt->unackedFrags.end());
332 *fragInNetPkt = newTxFragIt;
333
334 removedThisTxSeq.push_back(txSeqIt->first);
335 deleteUnackedFrag(txSeqIt);
336
337 // Retransmit fragment
338 m_linkService->sendLpPacket(lp::Packet(newTxFrag.pkt));
339
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");
344
345 // Start RTO timer for this sequence
346 newTxFrag.rtoTimer = getScheduler().schedule(rto, [=] {
347 onLpPacketLost(newTxSeq, true);
348 });
349 }
350
351 return removedThisTxSeq;
352}
353
354void
355LpReliability::onLpPacketAcknowledged(UnackedFrags::iterator fragIt)
356{
357 auto netPkt = fragIt->second.netPkt;
358
359 // Remove from NetPkt unacked fragment list
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();
364
365 // Check if network-layer packet completely received. If so, increment counters
366 if (netPkt->unackedFrags.empty()) {
367 if (netPkt->didRetx) {
368 ++m_linkService->nRetransmitted;
369 }
370 else {
371 ++m_linkService->nAcknowledged;
372 }
373 }
374
375 deleteUnackedFrag(fragIt);
376}
377
378void
379LpReliability::deleteUnackedFrag(UnackedFrags::iterator fragIt)
380{
381 lp::Sequence firstUnackedTxSeq = m_firstUnackedFrag->first;
382 lp::Sequence currentTxSeq = fragIt->first;
383 auto nextFragIt = m_unackedFrags.erase(fragIt);
384
385 if (!m_unackedFrags.empty() && firstUnackedTxSeq == currentTxSeq) {
386 // If "first" fragment in send window (allowing for wraparound), increment window begin
387 if (nextFragIt == m_unackedFrags.end()) {
388 m_firstUnackedFrag = m_unackedFrags.begin();
389 }
390 else {
391 m_firstUnackedFrag = nextFragIt;
392 }
393 }
394 else if (m_unackedFrags.empty()) {
395 m_firstUnackedFrag = m_unackedFrags.end();
396 }
397}
398
399std::ostream&
400operator<<(std::ostream& os, const FaceLogHelper<LpReliability>& flh)
401{
402 if (flh.obj.getLinkService() == nullptr) {
403 os << "[id=0,local=unknown,remote=unknown] ";
404 }
405 else {
406 os << FaceLogHelper<LinkService>(*flh.obj.getLinkService());
407 }
408 return os;
409}
410
411} // namespace nfd::face
For internal use by FaceLogging macros.
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)
Definition logger.hpp:31
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.
Definition global.cpp:45
std::ostream & operator<<(std::ostream &os, const Network &network)
Definition network.cpp:83
time::nanoseconds idleAckTimerPeriod
Period between sending pending Acks in an IDLE packet.
bool isEnabled
Enables link-layer reliability.