NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2019, 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 {
33 namespace face {
34 
36 
38  : m_options(options)
39  , m_linkService(linkService)
40 {
41 }
42 
43 std::tuple<bool, Block, lp::Packet>
45 {
46  BOOST_ASSERT(packet.has<lp::FragmentField>());
47 
48  static auto FALSE_RETURN = std::make_tuple(false, Block(), lp::Packet());
49 
50  // read and check FragIndex and FragCount
51  uint64_t fragIndex = 0;
52  uint64_t fragCount = 1;
53  if (packet.has<lp::FragIndexField>()) {
54  fragIndex = packet.get<lp::FragIndexField>();
55  }
56  if (packet.has<lp::FragCountField>()) {
57  fragCount = packet.get<lp::FragCountField>();
58  }
59 
60  if (fragIndex >= fragCount) {
61  NFD_LOG_FACE_WARN("reassembly error, FragIndex>=FragCount: DROP");
62  return FALSE_RETURN;
63  }
64 
65  if (fragCount > m_options.nMaxFragments) {
66  NFD_LOG_FACE_WARN("reassembly error, FragCount over limit: DROP");
67  return FALSE_RETURN;
68  }
69 
70  // check for fast path
71  if (fragIndex == 0 && fragCount == 1) {
72  ndn::Buffer::const_iterator fragBegin, fragEnd;
73  std::tie(fragBegin, fragEnd) = packet.get<lp::FragmentField>();
74  Block netPkt(&*fragBegin, std::distance(fragBegin, fragEnd));
75  return std::make_tuple(true, netPkt, packet);
76  }
77 
78  // check Sequence and compute message identifier
79  if (!packet.has<lp::SequenceField>()) {
80  NFD_LOG_FACE_WARN("reassembly error, Sequence missing: DROP");
81  return FALSE_RETURN;
82  }
83  lp::Sequence messageIdentifier = packet.get<lp::SequenceField>() - fragIndex;
84  Key key = std::make_tuple(remoteEndpoint, messageIdentifier);
85 
86  // add to PartialPacket
87  PartialPacket& pp = m_partialPackets[key];
88  if (pp.fragCount == 0) { // new PartialPacket
89  pp.fragCount = fragCount;
90  pp.nReceivedFragments = 0;
91  pp.fragments.resize(fragCount);
92  }
93  else {
94  if (fragCount != pp.fragCount) {
95  NFD_LOG_FACE_WARN("reassembly error, FragCount changed: DROP");
96  return FALSE_RETURN;
97  }
98  }
99 
100  if (pp.fragments[fragIndex].has<lp::SequenceField>()) {
101  NFD_LOG_FACE_TRACE("fragment already received: DROP");
102  return FALSE_RETURN;
103  }
104 
105  pp.fragments[fragIndex] = packet;
106  ++pp.nReceivedFragments;
107 
108  // check complete condition
109  if (pp.nReceivedFragments == pp.fragCount) {
110  Block reassembled = doReassembly(key);
111  lp::Packet firstFrag(std::move(pp.fragments[0]));
112  m_partialPackets.erase(key);
113  return std::make_tuple(true, reassembled, firstFrag);
114  }
115 
116  // set drop timer
117  pp.dropTimer = getScheduler().schedule(m_options.reassemblyTimeout, [=] { timeoutPartialPacket(key); });
118 
119  return FALSE_RETURN;
120 }
121 
122 Block
123 LpReassembler::doReassembly(const Key& key)
124 {
125  PartialPacket& pp = m_partialPackets[key];
126 
127  size_t payloadSize = std::accumulate(pp.fragments.begin(), pp.fragments.end(), 0U,
128  [&] (size_t sum, const lp::Packet& pkt) -> size_t {
129  ndn::Buffer::const_iterator fragBegin, fragEnd;
130  std::tie(fragBegin, fragEnd) = pkt.get<lp::FragmentField>();
131  return sum + std::distance(fragBegin, fragEnd);
132  });
133 
134  ndn::Buffer fragBuffer(payloadSize);
135  auto it = fragBuffer.begin();
136 
137  for (const lp::Packet& frag : pp.fragments) {
138  ndn::Buffer::const_iterator fragBegin, fragEnd;
139  std::tie(fragBegin, fragEnd) = frag.get<lp::FragmentField>();
140  it = std::copy(fragBegin, fragEnd, it);
141  }
142 
143  return Block(&*(fragBuffer.cbegin()), std::distance(fragBuffer.cbegin(), fragBuffer.cend()));
144 }
145 
146 void
147 LpReassembler::timeoutPartialPacket(const Key& key)
148 {
149  auto it = m_partialPackets.find(key);
150  if (it == m_partialPackets.end()) {
151  return;
152  }
153 
154  this->beforeTimeout(std::get<0>(key), it->second.nReceivedFragments);
155  m_partialPackets.erase(it);
156 }
157 
158 std::ostream&
159 operator<<(std::ostream& os, const FaceLogHelper<LpReassembler>& flh)
160 {
161  if (flh.obj.getLinkService() == nullptr) {
162  os << "[id=0,local=unknown,remote=unknown] ";
163  }
164  else {
165  os << FaceLogHelper<LinkService>(*flh.obj.getLinkService());
166  }
167  return os;
168 }
169 
170 } // namespace face
171 } // namespace nfd
ndn::lp::Packet
Definition: packet.hpp:31
global.hpp
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:421
ndn::lp::Packet::has
NDN_CXX_NODISCARD bool has() const
Definition: packet.hpp:74
ndn::Buffer
General-purpose automatically managed/resized buffer.
Definition: buffer.hpp:41
nfd::face::LpReassembler::Options
Options that control the behavior of LpReassembler.
Definition: lp-reassembler.hpp:45
nfd::face::LpReassembler::LpReassembler
LpReassembler(const Options &options, const LinkService *linkService=nullptr)
Definition: lp-reassembler.cpp:37
nfd::face::operator<<
std::ostream & operator<<(std::ostream &os, const Face &face)
Definition: ndn-common.hpp:87
nfd
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
nfd::face::LinkService
the upper part of a Face
Definition: link-service.hpp:76
NFD_LOG_FACE_TRACE
#define NFD_LOG_FACE_TRACE(msg)
Log a message at TRACE level.
Definition: face-common.hpp:133
ndn::lp::FieldDecl
Declare a field.
Definition: field-decl.hpp:180
nfd::getScheduler
Scheduler & getScheduler()
Returns the global Scheduler instance for the calling thread.
Definition: global.cpp:70
nfd::face::LpReassembler::Options::reassemblyTimeout
time::nanoseconds reassemblyTimeout
timeout before a partially reassembled packet is dropped
Definition: lp-reassembler.hpp:54
nfd::face::LpReassembler
reassembles fragmented network-layer packets
Definition: lp-reassembler.hpp:40
nfd::face::LpReassembler::Options::nMaxFragments
size_t nMaxFragments
maximum number of fragments in a packet
Definition: lp-reassembler.hpp:50
lp-reassembler.hpp
ndn::Block
Represents a TLV element of NDN packet format.
Definition: block.hpp:43
nfd::face::LpReassembler::receiveFragment
std::tuple< bool, Block, lp::Packet > receiveFragment(EndpointId remoteEndpoint, const lp::Packet &packet)
adds received fragment to the buffer
Definition: lp-reassembler.cpp:44
nfd::face::LpReassembler::getLinkService
const LinkService * getLinkService() const
Definition: lp-reassembler.hpp:138
nfd::face::EndpointId
uint64_t EndpointId
Identifies a remote endpoint on the link.
Definition: face-common.hpp:65
nfd::face::FaceLogHelper
For internal use by FaceLogging macros.
Definition: face-common.hpp:93
NFD_LOG_FACE_WARN
#define NFD_LOG_FACE_WARN(msg)
Log a message at WARN level.
Definition: face-common.hpp:142
ndn::lp::Sequence
uint64_t Sequence
represents a sequence number
Definition: sequence.hpp:35
ndn::lp::Packet::get
FIELD::ValueType get(size_t index=0) const
Definition: packet.hpp:96
NFD_LOG_INIT
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
nfd::face::LpReassembler::beforeTimeout
signal::Signal< LpReassembler, EndpointId, size_t > beforeTimeout
signals before a partial packet is dropped due to timeout
Definition: lp-reassembler.hpp:96
nfd::face::FaceLogHelper::obj
const T & obj
Definition: face-common.hpp:102