Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2017, 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 "generic-link-service.hpp" |
| 28 | #include "transport.hpp" |
| 29 | |
| 30 | namespace nfd { |
| 31 | namespace face { |
| 32 | |
| 33 | LpReliability::LpReliability(const LpReliability::Options& options, GenericLinkService* linkService) |
| 34 | : m_options(options) |
| 35 | , m_linkService(linkService) |
| 36 | , m_firstUnackedFrag(m_unackedFrags.begin()) |
| 37 | , m_isIdleAckTimerRunning(false) |
| 38 | { |
| 39 | BOOST_ASSERT(m_linkService != nullptr); |
| 40 | |
| 41 | BOOST_ASSERT(m_options.idleAckTimerPeriod > time::nanoseconds::zero()); |
| 42 | } |
| 43 | |
| 44 | void |
| 45 | LpReliability::setOptions(const Options& options) |
| 46 | { |
| 47 | BOOST_ASSERT(options.idleAckTimerPeriod > time::nanoseconds::zero()); |
| 48 | |
| 49 | if (m_options.isEnabled && !options.isEnabled) { |
| 50 | this->stopIdleAckTimer(); |
| 51 | } |
| 52 | |
| 53 | m_options = options; |
| 54 | } |
| 55 | |
| 56 | const GenericLinkService* |
| 57 | LpReliability::getLinkService() const |
| 58 | { |
| 59 | return m_linkService; |
| 60 | } |
| 61 | |
| 62 | void |
| 63 | LpReliability::observeOutgoing(const std::vector<lp::Packet>& frags) |
| 64 | { |
| 65 | BOOST_ASSERT(m_options.isEnabled); |
| 66 | |
| 67 | // The sequence number of the first fragment is used to identify the NetPkt. |
| 68 | lp::Sequence netPktIdentifier = frags.at(0).get<lp::SequenceField>(); |
| 69 | auto& netPkt = m_netPkts.emplace(netPktIdentifier, NetPkt{}).first->second; |
| 70 | auto unackedFragsIt = m_unackedFrags.begin(); |
| 71 | auto netPktUnackedFragsIt = netPkt.unackedFrags.begin(); |
| 72 | |
| 73 | for (const lp::Packet& frag : frags) { |
| 74 | // Store LpPacket for future retransmissions |
| 75 | lp::Sequence seq = frag.get<lp::SequenceField>(); |
| 76 | unackedFragsIt = m_unackedFrags.emplace_hint(unackedFragsIt, seq, frag); |
| 77 | unackedFragsIt->second.rtoTimer = |
| 78 | scheduler::schedule(m_rto.computeRto(), bind(&LpReliability::onLpPacketLost, this, seq)); |
| 79 | unackedFragsIt->second.sendTime = time::steady_clock::now(); |
| 80 | netPktUnackedFragsIt = netPkt.unackedFrags.insert(netPktUnackedFragsIt, seq); |
| 81 | if (m_unackedFrags.size() == 1) { |
| 82 | m_firstUnackedFrag = unackedFragsIt; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | void |
| 88 | LpReliability::processIncomingPacket(const lp::Packet& pkt) |
| 89 | { |
| 90 | BOOST_ASSERT(m_options.isEnabled); |
| 91 | |
| 92 | auto now = time::steady_clock::now(); |
| 93 | |
| 94 | // Extract and parse Acks |
| 95 | for (lp::Sequence ackSeq : pkt.list<lp::AckField>()) { |
| 96 | auto txFrag = m_unackedFrags.find(ackSeq); |
| 97 | if (txFrag == m_unackedFrags.end()) { |
| 98 | // Ignore an Ack for an unknown sequence number |
| 99 | continue; |
| 100 | } |
| 101 | |
| 102 | // Cancel the RTO timer for the acknowledged fragment |
| 103 | txFrag->second.rtoTimer.cancel(); |
| 104 | |
| 105 | if (txFrag->second.retxCount == 0) { |
| 106 | // This sequence had no retransmissions, so use it to calculate the RTO |
| 107 | m_rto.addMeasurement(time::duration_cast<RttEstimator::Duration>(now - txFrag->second.sendTime)); |
| 108 | } |
| 109 | |
| 110 | // Look for Acks with sequence numbers < ackSeq (allowing for wraparound) and consider them lost |
| 111 | // if a configurable number of Acks containing greater sequence numbers have been received. |
| 112 | auto lostLpPackets = findLostLpPackets(ackSeq); |
| 113 | |
| 114 | // Remove the fragment from the map of unacknowledged sequences and from its associated network |
| 115 | // packet (removing the network packet if it has been received in whole by remote host). |
| 116 | // Potentially increment the start of the window. |
| 117 | onLpPacketAcknowledged(txFrag, getNetPktByFrag(ackSeq)); |
| 118 | |
| 119 | // Resend or fail fragments considered lost. This must be done separately from the above |
| 120 | // enhanced for loop because onLpPacketLost may delete the fragment from m_unackedFrags. |
| 121 | for (const lp::Sequence& seq : lostLpPackets) { |
| 122 | this->onLpPacketLost(seq); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // If has Fragment field, extract Sequence and add to AckQueue |
| 127 | if (pkt.has<lp::FragmentField>() && pkt.has<lp::SequenceField>()) { |
| 128 | m_ackQueue.push(pkt.get<lp::SequenceField>()); |
| 129 | if (!m_isIdleAckTimerRunning) { |
| 130 | this->startIdleAckTimer(); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | void |
| 136 | LpReliability::piggyback(lp::Packet& pkt, ssize_t mtu) |
| 137 | { |
| 138 | BOOST_ASSERT(m_options.isEnabled); |
| 139 | |
| 140 | int maxAcks = std::numeric_limits<int>::max(); |
| 141 | if (mtu > 0) { |
| 142 | // Ack Type (3 octets) + Ack Length (1 octet) + sizeof(lp::Sequence) |
| 143 | size_t ackSize = 3 + 1 + sizeof(lp::Sequence); |
Junxiao Shi | 83be1da | 2017-06-30 13:37:37 +0000 | [diff] [blame] | 144 | maxAcks = (mtu - pkt.wireEncode().size()) / ackSize; |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | ssize_t nAcksInPkt = 0; |
| 148 | while (!m_ackQueue.empty() && nAcksInPkt < maxAcks) { |
| 149 | pkt.add<lp::AckField>(m_ackQueue.front()); |
| 150 | m_ackQueue.pop(); |
| 151 | nAcksInPkt++; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | void |
| 156 | LpReliability::startIdleAckTimer() |
| 157 | { |
| 158 | BOOST_ASSERT(!m_isIdleAckTimerRunning); |
| 159 | m_isIdleAckTimerRunning = true; |
| 160 | |
| 161 | m_idleAckTimer = scheduler::schedule(m_options.idleAckTimerPeriod, [this] { |
| 162 | while (!m_ackQueue.empty()) { |
| 163 | m_linkService->requestIdlePacket(); |
| 164 | } |
| 165 | |
| 166 | m_isIdleAckTimerRunning = false; |
| 167 | }); |
| 168 | } |
| 169 | |
| 170 | void |
| 171 | LpReliability::stopIdleAckTimer() |
| 172 | { |
| 173 | m_idleAckTimer.cancel(); |
| 174 | m_isIdleAckTimerRunning = false; |
| 175 | } |
| 176 | |
| 177 | std::vector<lp::Sequence> |
| 178 | LpReliability::findLostLpPackets(lp::Sequence ackSeq) |
| 179 | { |
| 180 | std::vector<lp::Sequence> lostLpPackets; |
| 181 | |
| 182 | for (auto it = m_firstUnackedFrag; ; ++it) { |
| 183 | if (it == m_unackedFrags.end()) { |
| 184 | it = m_unackedFrags.begin(); |
| 185 | } |
| 186 | |
| 187 | if (it->first == ackSeq) { |
| 188 | break; |
| 189 | } |
| 190 | |
| 191 | auto& unackedFrag = it->second; |
| 192 | |
| 193 | unackedFrag.nGreaterSeqAcks++; |
| 194 | |
| 195 | if (unackedFrag.nGreaterSeqAcks >= m_options.seqNumLossThreshold && !unackedFrag.wasTimedOutBySeq) { |
| 196 | unackedFrag.wasTimedOutBySeq = true; |
| 197 | lostLpPackets.push_back(it->first); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | return lostLpPackets; |
| 202 | } |
| 203 | |
| 204 | void |
| 205 | LpReliability::onLpPacketLost(lp::Sequence seq) |
| 206 | { |
| 207 | auto& txFrag = m_unackedFrags.at(seq); |
| 208 | auto netPktIt = getNetPktByFrag(seq); |
| 209 | |
| 210 | // Check if maximum number of retransmissions exceeded |
| 211 | if (txFrag.retxCount >= m_options.maxRetx) { |
| 212 | // Delete all LpPackets of NetPkt from TransmitCache |
| 213 | lp::Sequence firstSeq = *(netPktIt->second.unackedFrags.begin()); |
| 214 | lp::Sequence lastSeq = *(std::prev(netPktIt->second.unackedFrags.end())); |
| 215 | if (lastSeq >= firstSeq) { // Normal case: no wraparound |
| 216 | m_unackedFrags.erase(m_unackedFrags.find(firstSeq), std::next(m_unackedFrags.find(lastSeq))); |
| 217 | } |
| 218 | else { // sequence number wraparound |
| 219 | m_unackedFrags.erase(m_unackedFrags.find(firstSeq), m_unackedFrags.end()); |
| 220 | m_unackedFrags.erase(m_unackedFrags.begin(), std::next(m_unackedFrags.find(lastSeq))); |
| 221 | } |
| 222 | |
| 223 | m_netPkts.erase(netPktIt); |
| 224 | |
| 225 | ++m_linkService->nRetxExhausted; |
| 226 | } |
| 227 | else { |
| 228 | txFrag.retxCount++; |
| 229 | |
| 230 | // Start RTO timer for this sequence |
| 231 | txFrag.rtoTimer = scheduler::schedule(m_rto.computeRto(), |
| 232 | bind(&LpReliability::onLpPacketLost, this, seq)); |
| 233 | |
| 234 | // Retransmit fragment |
| 235 | m_linkService->sendLpPacket(lp::Packet(txFrag.pkt)); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | void |
| 240 | LpReliability::onLpPacketAcknowledged(std::map<lp::Sequence, LpReliability::UnackedFrag>::iterator fragIt, |
| 241 | std::map<lp::Sequence, LpReliability::NetPkt>::iterator netPktIt) |
| 242 | { |
| 243 | lp::Sequence seq = fragIt->first; |
| 244 | // We need to store the sequence of the window begin in case we are erasing it from m_unackedFrags |
| 245 | lp::Sequence firstUnackedSeq = m_firstUnackedFrag->first; |
| 246 | auto nextSeqIt = m_unackedFrags.erase(fragIt); |
| 247 | netPktIt->second.unackedFrags.erase(seq); |
| 248 | |
| 249 | if (!m_unackedFrags.empty() && firstUnackedSeq == seq) { |
| 250 | // If "first" fragment in send window (allowing for wraparound), increment window begin |
| 251 | if (nextSeqIt == m_unackedFrags.end()) { |
| 252 | m_firstUnackedFrag = m_unackedFrags.begin(); |
| 253 | } |
| 254 | else { |
| 255 | m_firstUnackedFrag = nextSeqIt; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | // Check if network-layer packet completely received. If so, delete network packet mapping |
| 260 | // and increment counter |
| 261 | if (netPktIt->second.unackedFrags.empty()) { |
| 262 | if (netPktIt->second.didRetx) { |
| 263 | ++m_linkService->nRetransmitted; |
| 264 | } |
| 265 | else { |
| 266 | ++m_linkService->nAcknowledged; |
| 267 | } |
| 268 | m_netPkts.erase(netPktIt); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | std::map<lp::Sequence, LpReliability::NetPkt>::iterator |
| 273 | LpReliability::getNetPktByFrag(lp::Sequence seq) |
| 274 | { |
| 275 | BOOST_ASSERT(!m_netPkts.empty()); |
| 276 | auto it = m_netPkts.lower_bound(seq); |
| 277 | if (it == m_netPkts.end()) { |
| 278 | // This can happen because of sequence number wraparound in the middle of a network packet. |
| 279 | // In this case, the network packet will be at the end of m_netPkts and we will need to |
| 280 | // decrement the iterator to m_netPkts.end() to the one before it. |
| 281 | --it; |
| 282 | } |
| 283 | return it; |
| 284 | } |
| 285 | |
| 286 | LpReliability::UnackedFrag::UnackedFrag(lp::Packet pkt) |
| 287 | : pkt(std::move(pkt)) |
| 288 | , sendTime(time::steady_clock::now()) |
| 289 | , retxCount(0) |
| 290 | , nGreaterSeqAcks(0) |
| 291 | , wasTimedOutBySeq(false) |
| 292 | { |
| 293 | } |
| 294 | |
| 295 | LpReliability::NetPkt::NetPkt() |
| 296 | : didRetx(false) |
| 297 | { |
| 298 | } |
| 299 | |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 300 | } // namespace face |
| 301 | } // namespace nfd |