Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 9a63bf2 | 2023-11-11 17:12:51 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, Regents of the University of California, |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 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-reassembler.hpp" |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 27 | #include "common/global.hpp" |
Davide Pesavento | 9a63bf2 | 2023-11-11 17:12:51 -0500 | [diff] [blame] | 28 | #include "link-service.hpp" |
| 29 | |
| 30 | #include <ndn-cxx/lp/fields.hpp> |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 31 | |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 32 | #include <numeric> |
| 33 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 34 | namespace nfd::face { |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 35 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 36 | NFD_LOG_INIT(LpReassembler); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 37 | |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 38 | LpReassembler::LpReassembler(const LpReassembler::Options& options, const LinkService* linkService) |
| 39 | : m_options(options) |
| 40 | , m_linkService(linkService) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | std::tuple<bool, Block, lp::Packet> |
Teng Liang | d94b7b3 | 2022-07-10 21:29:37 +0800 | [diff] [blame] | 45 | LpReassembler::receiveFragment(const EndpointId& remoteEndpoint, const lp::Packet& packet) |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 46 | { |
| 47 | BOOST_ASSERT(packet.has<lp::FragmentField>()); |
| 48 | |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 49 | // read and check FragIndex and FragCount |
| 50 | uint64_t fragIndex = 0; |
| 51 | uint64_t fragCount = 1; |
| 52 | if (packet.has<lp::FragIndexField>()) { |
| 53 | fragIndex = packet.get<lp::FragIndexField>(); |
| 54 | } |
| 55 | if (packet.has<lp::FragCountField>()) { |
| 56 | fragCount = packet.get<lp::FragCountField>(); |
| 57 | } |
| 58 | |
| 59 | if (fragIndex >= fragCount) { |
| 60 | NFD_LOG_FACE_WARN("reassembly error, FragIndex>=FragCount: DROP"); |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 61 | return {false, {}, {}}; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | if (fragCount > m_options.nMaxFragments) { |
| 65 | NFD_LOG_FACE_WARN("reassembly error, FragCount over limit: DROP"); |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 66 | return {false, {}, {}}; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // check for fast path |
| 70 | if (fragIndex == 0 && fragCount == 1) { |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 71 | auto frag = packet.get<lp::FragmentField>(); |
| 72 | Block netPkt({frag.first, frag.second}); |
| 73 | return {true, netPkt, packet}; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | // check Sequence and compute message identifier |
| 77 | if (!packet.has<lp::SequenceField>()) { |
| 78 | NFD_LOG_FACE_WARN("reassembly error, Sequence missing: DROP"); |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 79 | return {false, {}, {}}; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 80 | } |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 81 | |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 82 | lp::Sequence messageIdentifier = packet.get<lp::SequenceField>() - fragIndex; |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 83 | Key key(remoteEndpoint, messageIdentifier); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 84 | |
| 85 | // add to PartialPacket |
| 86 | PartialPacket& pp = m_partialPackets[key]; |
| 87 | if (pp.fragCount == 0) { // new PartialPacket |
| 88 | pp.fragCount = fragCount; |
| 89 | pp.nReceivedFragments = 0; |
| 90 | pp.fragments.resize(fragCount); |
| 91 | } |
| 92 | else { |
| 93 | if (fragCount != pp.fragCount) { |
| 94 | NFD_LOG_FACE_WARN("reassembly error, FragCount changed: DROP"); |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 95 | return {false, {}, {}}; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | if (pp.fragments[fragIndex].has<lp::SequenceField>()) { |
| 100 | NFD_LOG_FACE_TRACE("fragment already received: DROP"); |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 101 | return {false, {}, {}}; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | pp.fragments[fragIndex] = packet; |
| 105 | ++pp.nReceivedFragments; |
| 106 | |
| 107 | // check complete condition |
| 108 | if (pp.nReceivedFragments == pp.fragCount) { |
| 109 | Block reassembled = doReassembly(key); |
| 110 | lp::Packet firstFrag(std::move(pp.fragments[0])); |
| 111 | m_partialPackets.erase(key); |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 112 | return {true, reassembled, firstFrag}; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // set drop timer |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 116 | pp.dropTimer = getScheduler().schedule(m_options.reassemblyTimeout, [=] { timeoutPartialPacket(key); }); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 117 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 118 | return {false, {}, {}}; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | Block |
| 122 | LpReassembler::doReassembly(const Key& key) |
| 123 | { |
| 124 | PartialPacket& pp = m_partialPackets[key]; |
| 125 | |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 126 | size_t payloadSize = std::accumulate(pp.fragments.begin(), pp.fragments.end(), 0U, |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 127 | [&] (size_t sum, const lp::Packet& pkt) -> size_t { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 128 | auto [fragBegin, fragEnd] = pkt.get<lp::FragmentField>(); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 129 | return sum + std::distance(fragBegin, fragEnd); |
| 130 | }); |
| 131 | |
| 132 | ndn::Buffer fragBuffer(payloadSize); |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 133 | auto it = fragBuffer.begin(); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 134 | for (const lp::Packet& frag : pp.fragments) { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 135 | auto [fragBegin, fragEnd] = frag.get<lp::FragmentField>(); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 136 | it = std::copy(fragBegin, fragEnd, it); |
| 137 | } |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 138 | return Block(fragBuffer); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void |
| 142 | LpReassembler::timeoutPartialPacket(const Key& key) |
| 143 | { |
| 144 | auto it = m_partialPackets.find(key); |
| 145 | if (it == m_partialPackets.end()) { |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | this->beforeTimeout(std::get<0>(key), it->second.nReceivedFragments); |
| 150 | m_partialPackets.erase(it); |
| 151 | } |
| 152 | |
| 153 | std::ostream& |
| 154 | operator<<(std::ostream& os, const FaceLogHelper<LpReassembler>& flh) |
| 155 | { |
| 156 | if (flh.obj.getLinkService() == nullptr) { |
| 157 | os << "[id=0,local=unknown,remote=unknown] "; |
| 158 | } |
| 159 | else { |
| 160 | os << FaceLogHelper<LinkService>(*flh.obj.getLinkService()); |
| 161 | } |
| 162 | return os; |
| 163 | } |
| 164 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 165 | } // namespace nfd::face |