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-2018, 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 
29 #include <numeric>
30 
31 namespace nfd {
32 namespace face {
33 
35 
37  : m_options(options)
38  , m_linkService(linkService)
39 {
40 }
41 
42 std::tuple<bool, Block, lp::Packet>
44 {
45  BOOST_ASSERT(packet.has<lp::FragmentField>());
46 
47  static auto FALSE_RETURN = std::make_tuple(false, Block(), lp::Packet());
48 
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");
61  return FALSE_RETURN;
62  }
63 
64  if (fragCount > m_options.nMaxFragments) {
65  NFD_LOG_FACE_WARN("reassembly error, FragCount over limit: DROP");
66  return FALSE_RETURN;
67  }
68 
69  // check for fast path
70  if (fragIndex == 0 && fragCount == 1) {
71  ndn::Buffer::const_iterator fragBegin, fragEnd;
72  std::tie(fragBegin, fragEnd) = packet.get<lp::FragmentField>();
73  Block netPkt(&*fragBegin, std::distance(fragBegin, fragEnd));
74  return std::make_tuple(true, netPkt, packet);
75  }
76 
77  // check Sequence and compute message identifier
78  if (!packet.has<lp::SequenceField>()) {
79  NFD_LOG_FACE_WARN("reassembly error, Sequence missing: DROP");
80  return FALSE_RETURN;
81  }
82  lp::Sequence messageIdentifier = packet.get<lp::SequenceField>() - fragIndex;
83  Key key = std::make_tuple(remoteEndpoint, messageIdentifier);
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");
95  return FALSE_RETURN;
96  }
97  }
98 
99  if (pp.fragments[fragIndex].has<lp::SequenceField>()) {
100  NFD_LOG_FACE_TRACE("fragment already received: DROP");
101  return FALSE_RETURN;
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);
112  return std::make_tuple(true, reassembled, firstFrag);
113  }
114 
115  // set drop timer
116  pp.dropTimer = scheduler::schedule(m_options.reassemblyTimeout, [=] { timeoutPartialPacket(key); });
117 
118  return FALSE_RETURN;
119 }
120 
121 Block
122 LpReassembler::doReassembly(const Key& key)
123 {
124  PartialPacket& pp = m_partialPackets[key];
125 
126  size_t payloadSize = std::accumulate(pp.fragments.begin(), pp.fragments.end(), 0U,
127  [&] (size_t sum, const lp::Packet& pkt) -> size_t {
128  ndn::Buffer::const_iterator fragBegin, fragEnd;
129  std::tie(fragBegin, fragEnd) = pkt.get<lp::FragmentField>();
130  return sum + std::distance(fragBegin, fragEnd);
131  });
132 
133  ndn::Buffer fragBuffer(payloadSize);
134  auto it = fragBuffer.begin();
135 
136  for (const lp::Packet& frag : pp.fragments) {
137  ndn::Buffer::const_iterator fragBegin, fragEnd;
138  std::tie(fragBegin, fragEnd) = frag.get<lp::FragmentField>();
139  it = std::copy(fragBegin, fragEnd, it);
140  }
141 
142  return Block(&*(fragBuffer.cbegin()), std::distance(fragBuffer.cbegin(), fragBuffer.cend()));
143 }
144 
145 void
146 LpReassembler::timeoutPartialPacket(const Key& key)
147 {
148  auto it = m_partialPackets.find(key);
149  if (it == m_partialPackets.end()) {
150  return;
151  }
152 
153  this->beforeTimeout(std::get<0>(key), it->second.nReceivedFragments);
154  m_partialPackets.erase(it);
155 }
156 
157 std::ostream&
158 operator<<(std::ostream& os, const FaceLogHelper<LpReassembler>& flh)
159 {
160  if (flh.obj.getLinkService() == nullptr) {
161  os << "[id=0,local=unknown,remote=unknown] ";
162  }
163  else {
164  os << FaceLogHelper<LinkService>(*flh.obj.getLinkService());
165  }
166  return os;
167 }
168 
169 } // namespace face
170 } // namespace nfd
signal::Signal< LpReassembler, Transport::EndpointId, size_t > beforeTimeout
signals before a partial packet is dropped due to timeout
the upper part of a Face
Options that control the behavior of LpReassembler.
#define NFD_LOG_FACE_TRACE(msg)
Log a message at TRACE level.
Definition: face-log.hpp:79
std::ostream & operator<<(std::ostream &os, const Face &face)
Definition: ndn-common.hpp:89
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
bool has() const
Definition: packet.hpp:74
uint64_t Sequence
represents a sequence number
Definition: sequence.hpp:35
Declare a field.
Definition: field-decl.hpp:181
FIELD::ValueType get(size_t index=0) const
Definition: packet.hpp:97
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
reassembles fragmented network-layer packets
LpReassembler(const Options &options, const LinkService *linkService=nullptr)
EventId schedule(time::nanoseconds after, const EventCallback &event)
Schedule an event.
Definition: scheduler.cpp:48
std::tuple< bool, Block, lp::Packet > receiveFragment(Transport::EndpointId remoteEndpoint, const lp::Packet &packet)
adds received fragment to buffer
for internal use by FaceLogging macros
Definition: face-log.hpp:43
const LinkService * getLinkService() const
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
time::nanoseconds reassemblyTimeout
timeout before a partially reassembled packet is dropped
uint64_t EndpointId
identifies an endpoint on the link
Definition: transport.hpp:118
General-purpose automatically managed/resized buffer.
Definition: buffer.hpp:40
#define NFD_LOG_FACE_WARN(msg)
Log a message at WARN level.
Definition: face-log.hpp:88
size_t nMaxFragments
maximum number of fragments in a packet