lp-reassembler.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2022, 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-reassembler.hpp"
27 #include "link-service.hpp"
28 #include "common/global.hpp"
29 
30 #include <numeric>
31 
32 namespace nfd::face {
33 
34 NFD_LOG_INIT(LpReassembler);
35 
37  : m_options(options)
38  , m_linkService(linkService)
39 {
40 }
41 
42 std::tuple<bool, Block, lp::Packet>
43 LpReassembler::receiveFragment(const EndpointId& remoteEndpoint, const lp::Packet& packet)
44 {
45  BOOST_ASSERT(packet.has<lp::FragmentField>());
46 
47  // read and check FragIndex and FragCount
48  uint64_t fragIndex = 0;
49  uint64_t fragCount = 1;
50  if (packet.has<lp::FragIndexField>()) {
51  fragIndex = packet.get<lp::FragIndexField>();
52  }
53  if (packet.has<lp::FragCountField>()) {
54  fragCount = packet.get<lp::FragCountField>();
55  }
56 
57  if (fragIndex >= fragCount) {
58  NFD_LOG_FACE_WARN("reassembly error, FragIndex>=FragCount: DROP");
59  return {false, {}, {}};
60  }
61 
62  if (fragCount > m_options.nMaxFragments) {
63  NFD_LOG_FACE_WARN("reassembly error, FragCount over limit: DROP");
64  return {false, {}, {}};
65  }
66 
67  // check for fast path
68  if (fragIndex == 0 && fragCount == 1) {
69  auto frag = packet.get<lp::FragmentField>();
70  Block netPkt({frag.first, frag.second});
71  return {true, netPkt, packet};
72  }
73 
74  // check Sequence and compute message identifier
75  if (!packet.has<lp::SequenceField>()) {
76  NFD_LOG_FACE_WARN("reassembly error, Sequence missing: DROP");
77  return {false, {}, {}};
78  }
79 
80  lp::Sequence messageIdentifier = packet.get<lp::SequenceField>() - fragIndex;
81  Key key(remoteEndpoint, messageIdentifier);
82 
83  // add to PartialPacket
84  PartialPacket& pp = m_partialPackets[key];
85  if (pp.fragCount == 0) { // new PartialPacket
86  pp.fragCount = fragCount;
87  pp.nReceivedFragments = 0;
88  pp.fragments.resize(fragCount);
89  }
90  else {
91  if (fragCount != pp.fragCount) {
92  NFD_LOG_FACE_WARN("reassembly error, FragCount changed: DROP");
93  return {false, {}, {}};
94  }
95  }
96 
97  if (pp.fragments[fragIndex].has<lp::SequenceField>()) {
98  NFD_LOG_FACE_TRACE("fragment already received: DROP");
99  return {false, {}, {}};
100  }
101 
102  pp.fragments[fragIndex] = packet;
103  ++pp.nReceivedFragments;
104 
105  // check complete condition
106  if (pp.nReceivedFragments == pp.fragCount) {
107  Block reassembled = doReassembly(key);
108  lp::Packet firstFrag(std::move(pp.fragments[0]));
109  m_partialPackets.erase(key);
110  return {true, reassembled, firstFrag};
111  }
112 
113  // set drop timer
114  pp.dropTimer = getScheduler().schedule(m_options.reassemblyTimeout, [=] { timeoutPartialPacket(key); });
115 
116  return {false, {}, {}};
117 }
118 
119 Block
120 LpReassembler::doReassembly(const Key& key)
121 {
122  PartialPacket& pp = m_partialPackets[key];
123 
124  size_t payloadSize = std::accumulate(pp.fragments.begin(), pp.fragments.end(), 0U,
125  [&] (size_t sum, const lp::Packet& pkt) -> size_t {
126  auto [fragBegin, fragEnd] = pkt.get<lp::FragmentField>();
127  return sum + std::distance(fragBegin, fragEnd);
128  });
129 
130  ndn::Buffer fragBuffer(payloadSize);
131  auto it = fragBuffer.begin();
132  for (const lp::Packet& frag : pp.fragments) {
133  auto [fragBegin, fragEnd] = frag.get<lp::FragmentField>();
134  it = std::copy(fragBegin, fragEnd, it);
135  }
136  return Block(fragBuffer);
137 }
138 
139 void
140 LpReassembler::timeoutPartialPacket(const Key& key)
141 {
142  auto it = m_partialPackets.find(key);
143  if (it == m_partialPackets.end()) {
144  return;
145  }
146 
147  this->beforeTimeout(std::get<0>(key), it->second.nReceivedFragments);
148  m_partialPackets.erase(it);
149 }
150 
151 std::ostream&
152 operator<<(std::ostream& os, const FaceLogHelper<LpReassembler>& flh)
153 {
154  if (flh.obj.getLinkService() == nullptr) {
155  os << "[id=0,local=unknown,remote=unknown] ";
156  }
157  else {
158  os << FaceLogHelper<LinkService>(*flh.obj.getLinkService());
159  }
160  return os;
161 }
162 
163 } // namespace nfd::face
For internal use by FaceLogging macros.
std::tuple< bool, Block, lp::Packet > receiveFragment(const EndpointId &remoteEndpoint, const lp::Packet &packet)
Adds received fragment to the buffer.
signal::Signal< LpReassembler, EndpointId, size_t > beforeTimeout
Notifies before a partial packet is dropped due to timeout.
LpReassembler(const Options &options, const LinkService *linkService=nullptr)
#define NFD_LOG_FACE_WARN(msg)
Log a message at WARN level.
#define NFD_LOG_FACE_TRACE(msg)
Log a message at TRACE level.
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
std::ostream & operator<<(std::ostream &os, const FaceLogHelper< Face > &flh)
Definition: face.cpp:45
std::variant< std::monostate, ethernet::Address, udp::Endpoint > EndpointId
Identifies a remote endpoint on the link.
Definition: face-common.hpp:77
Scheduler & getScheduler()
Returns the global Scheduler instance for the calling thread.
Definition: global.cpp:45
Options that control the behavior of LpReassembler.
time::nanoseconds reassemblyTimeout
Timeout before a partially reassembled packet is dropped.
size_t nMaxFragments
Maximum number of fragments in a packet.