blob: a4b255ba774aaf22fe554e43d2b50bb1bdf5a1db [file] [log] [blame]
Eric Newberry4c3e6b82015-11-10 16:48:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoa3148082018-04-12 18:21:54 -04002/*
Davide Pesaventoa599d2a2022-02-16 18:52:43 -05003 * Copyright (c) 2014-2022, Regents of the University of California,
Eric Newberry4c3e6b82015-11-10 16:48:42 -07004 * 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"
27#include "link-service.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040028#include "common/global.hpp"
Davide Pesaventoa3148082018-04-12 18:21:54 -040029
Eric Newberry4c3e6b82015-11-10 16:48:42 -070030#include <numeric>
31
32namespace nfd {
33namespace face {
34
Davide Pesaventoa3148082018-04-12 18:21:54 -040035NFD_LOG_INIT(LpReassembler);
Eric Newberry4c3e6b82015-11-10 16:48:42 -070036
Eric Newberry4c3e6b82015-11-10 16:48:42 -070037LpReassembler::LpReassembler(const LpReassembler::Options& options, const LinkService* linkService)
38 : m_options(options)
39 , m_linkService(linkService)
40{
41}
42
43std::tuple<bool, Block, lp::Packet>
ashiqopu77d0bfd2019-02-20 20:37:31 +000044LpReassembler::receiveFragment(EndpointId remoteEndpoint, const lp::Packet& packet)
Eric Newberry4c3e6b82015-11-10 16:48:42 -070045{
46 BOOST_ASSERT(packet.has<lp::FragmentField>());
47
Eric Newberry4c3e6b82015-11-10 16:48:42 -070048 // read and check FragIndex and FragCount
49 uint64_t fragIndex = 0;
50 uint64_t fragCount = 1;
51 if (packet.has<lp::FragIndexField>()) {
52 fragIndex = packet.get<lp::FragIndexField>();
53 }
54 if (packet.has<lp::FragCountField>()) {
55 fragCount = packet.get<lp::FragCountField>();
56 }
57
58 if (fragIndex >= fragCount) {
59 NFD_LOG_FACE_WARN("reassembly error, FragIndex>=FragCount: DROP");
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040060 return {false, {}, {}};
Eric Newberry4c3e6b82015-11-10 16:48:42 -070061 }
62
63 if (fragCount > m_options.nMaxFragments) {
64 NFD_LOG_FACE_WARN("reassembly error, FragCount over limit: DROP");
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040065 return {false, {}, {}};
Eric Newberry4c3e6b82015-11-10 16:48:42 -070066 }
67
68 // check for fast path
69 if (fragIndex == 0 && fragCount == 1) {
Davide Pesaventoa599d2a2022-02-16 18:52:43 -050070 auto frag = packet.get<lp::FragmentField>();
71 Block netPkt({frag.first, frag.second});
72 return {true, netPkt, packet};
Eric Newberry4c3e6b82015-11-10 16:48:42 -070073 }
74
75 // check Sequence and compute message identifier
76 if (!packet.has<lp::SequenceField>()) {
77 NFD_LOG_FACE_WARN("reassembly error, Sequence missing: DROP");
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040078 return {false, {}, {}};
Eric Newberry4c3e6b82015-11-10 16:48:42 -070079 }
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040080
Eric Newberry4c3e6b82015-11-10 16:48:42 -070081 lp::Sequence messageIdentifier = packet.get<lp::SequenceField>() - fragIndex;
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040082 Key key(remoteEndpoint, messageIdentifier);
Eric Newberry4c3e6b82015-11-10 16:48:42 -070083
84 // add to PartialPacket
85 PartialPacket& pp = m_partialPackets[key];
86 if (pp.fragCount == 0) { // new PartialPacket
87 pp.fragCount = fragCount;
88 pp.nReceivedFragments = 0;
89 pp.fragments.resize(fragCount);
90 }
91 else {
92 if (fragCount != pp.fragCount) {
93 NFD_LOG_FACE_WARN("reassembly error, FragCount changed: DROP");
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040094 return {false, {}, {}};
Eric Newberry4c3e6b82015-11-10 16:48:42 -070095 }
96 }
97
98 if (pp.fragments[fragIndex].has<lp::SequenceField>()) {
99 NFD_LOG_FACE_TRACE("fragment already received: DROP");
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400100 return {false, {}, {}};
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700101 }
102
103 pp.fragments[fragIndex] = packet;
104 ++pp.nReceivedFragments;
105
106 // check complete condition
107 if (pp.nReceivedFragments == pp.fragCount) {
108 Block reassembled = doReassembly(key);
109 lp::Packet firstFrag(std::move(pp.fragments[0]));
110 m_partialPackets.erase(key);
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400111 return {true, reassembled, firstFrag};
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700112 }
113
114 // set drop timer
Davide Pesavento3dade002019-03-19 11:29:56 -0600115 pp.dropTimer = getScheduler().schedule(m_options.reassemblyTimeout, [=] { timeoutPartialPacket(key); });
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700116
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400117 return {false, {}, {}};
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700118}
119
120Block
121LpReassembler::doReassembly(const Key& key)
122{
123 PartialPacket& pp = m_partialPackets[key];
124
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400125 size_t payloadSize = std::accumulate(pp.fragments.begin(), pp.fragments.end(), 0U,
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700126 [&] (size_t sum, const lp::Packet& pkt) -> size_t {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400127 auto [fragBegin, fragEnd] = pkt.get<lp::FragmentField>();
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700128 return sum + std::distance(fragBegin, fragEnd);
129 });
130
131 ndn::Buffer fragBuffer(payloadSize);
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400132 auto it = fragBuffer.begin();
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700133 for (const lp::Packet& frag : pp.fragments) {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400134 auto [fragBegin, fragEnd] = frag.get<lp::FragmentField>();
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700135 it = std::copy(fragBegin, fragEnd, it);
136 }
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500137 return Block(fragBuffer);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700138}
139
140void
141LpReassembler::timeoutPartialPacket(const Key& key)
142{
143 auto it = m_partialPackets.find(key);
144 if (it == m_partialPackets.end()) {
145 return;
146 }
147
148 this->beforeTimeout(std::get<0>(key), it->second.nReceivedFragments);
149 m_partialPackets.erase(it);
150}
151
152std::ostream&
153operator<<(std::ostream& os, const FaceLogHelper<LpReassembler>& flh)
154{
155 if (flh.obj.getLinkService() == nullptr) {
156 os << "[id=0,local=unknown,remote=unknown] ";
157 }
158 else {
159 os << FaceLogHelper<LinkService>(*flh.obj.getLinkService());
160 }
161 return os;
162}
163
164} // namespace face
165} // namespace nfd