blob: 4dd2b16b53c31bee8e6aa6250f507307c659353f [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 Pesavento9a63bf22023-11-11 17:12:51 -05003 * Copyright (c) 2014-2023, 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"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040027#include "common/global.hpp"
Davide Pesavento9a63bf22023-11-11 17:12:51 -050028#include "link-service.hpp"
29
30#include <ndn-cxx/lp/fields.hpp>
Davide Pesaventoa3148082018-04-12 18:21:54 -040031
Eric Newberry4c3e6b82015-11-10 16:48:42 -070032#include <numeric>
33
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040034namespace nfd::face {
Eric Newberry4c3e6b82015-11-10 16:48:42 -070035
Davide Pesaventoa3148082018-04-12 18:21:54 -040036NFD_LOG_INIT(LpReassembler);
Eric Newberry4c3e6b82015-11-10 16:48:42 -070037
Eric Newberry4c3e6b82015-11-10 16:48:42 -070038LpReassembler::LpReassembler(const LpReassembler::Options& options, const LinkService* linkService)
39 : m_options(options)
40 , m_linkService(linkService)
41{
42}
43
44std::tuple<bool, Block, lp::Packet>
Teng Liangd94b7b32022-07-10 21:29:37 +080045LpReassembler::receiveFragment(const EndpointId& remoteEndpoint, const lp::Packet& packet)
Eric Newberry4c3e6b82015-11-10 16:48:42 -070046{
47 BOOST_ASSERT(packet.has<lp::FragmentField>());
48
Eric Newberry4c3e6b82015-11-10 16:48:42 -070049 // 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 Pesaventoa3a7a4e2022-05-29 16:06:22 -040061 return {false, {}, {}};
Eric Newberry4c3e6b82015-11-10 16:48:42 -070062 }
63
64 if (fragCount > m_options.nMaxFragments) {
65 NFD_LOG_FACE_WARN("reassembly error, FragCount over limit: DROP");
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040066 return {false, {}, {}};
Eric Newberry4c3e6b82015-11-10 16:48:42 -070067 }
68
69 // check for fast path
70 if (fragIndex == 0 && fragCount == 1) {
Davide Pesaventoa599d2a2022-02-16 18:52:43 -050071 auto frag = packet.get<lp::FragmentField>();
72 Block netPkt({frag.first, frag.second});
73 return {true, netPkt, packet};
Eric Newberry4c3e6b82015-11-10 16:48:42 -070074 }
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 Pesaventoa3a7a4e2022-05-29 16:06:22 -040079 return {false, {}, {}};
Eric Newberry4c3e6b82015-11-10 16:48:42 -070080 }
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040081
Eric Newberry4c3e6b82015-11-10 16:48:42 -070082 lp::Sequence messageIdentifier = packet.get<lp::SequenceField>() - fragIndex;
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040083 Key key(remoteEndpoint, messageIdentifier);
Eric Newberry4c3e6b82015-11-10 16:48:42 -070084
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 Pesaventoa3a7a4e2022-05-29 16:06:22 -040095 return {false, {}, {}};
Eric Newberry4c3e6b82015-11-10 16:48:42 -070096 }
97 }
98
99 if (pp.fragments[fragIndex].has<lp::SequenceField>()) {
100 NFD_LOG_FACE_TRACE("fragment already received: DROP");
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400101 return {false, {}, {}};
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700102 }
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 Pesaventoa3a7a4e2022-05-29 16:06:22 -0400112 return {true, reassembled, firstFrag};
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700113 }
114
115 // set drop timer
Davide Pesavento3dade002019-03-19 11:29:56 -0600116 pp.dropTimer = getScheduler().schedule(m_options.reassemblyTimeout, [=] { timeoutPartialPacket(key); });
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700117
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400118 return {false, {}, {}};
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700119}
120
121Block
122LpReassembler::doReassembly(const Key& key)
123{
124 PartialPacket& pp = m_partialPackets[key];
125
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400126 size_t payloadSize = std::accumulate(pp.fragments.begin(), pp.fragments.end(), 0U,
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700127 [&] (size_t sum, const lp::Packet& pkt) -> size_t {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400128 auto [fragBegin, fragEnd] = pkt.get<lp::FragmentField>();
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700129 return sum + std::distance(fragBegin, fragEnd);
130 });
131
132 ndn::Buffer fragBuffer(payloadSize);
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400133 auto it = fragBuffer.begin();
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700134 for (const lp::Packet& frag : pp.fragments) {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400135 auto [fragBegin, fragEnd] = frag.get<lp::FragmentField>();
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700136 it = std::copy(fragBegin, fragEnd, it);
137 }
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500138 return Block(fragBuffer);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700139}
140
141void
142LpReassembler::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
153std::ostream&
154operator<<(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 Pesaventoe422f9e2022-06-03 01:30:23 -0400165} // namespace nfd::face