generic-link-service.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2017, 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 "generic-link-service.hpp"
27 #include <ndn-cxx/lp/tags.hpp>
28 
29 namespace nfd {
30 namespace face {
31 
32 NFD_LOG_INIT("GenericLinkService");
33 
35  : allowLocalFields(false)
36  , allowFragmentation(false)
37  , allowReassembly(false)
38 {
39 }
40 
42  : m_options(options)
43  , m_fragmenter(m_options.fragmenterOptions, this)
44  , m_reassembler(m_options.reassemblerOptions, this)
45  , m_reliability(m_options.reliabilityOptions, this)
46  , m_lastSeqNo(-2)
47 {
48  m_reassembler.beforeTimeout.connect(bind([this] { ++this->nReassemblyTimeouts; }));
49  nReassembling.observe(&m_reassembler);
50 }
51 
52 void
54 {
55  m_options = options;
56  m_fragmenter.setOptions(m_options.fragmenterOptions);
57  m_reassembler.setOptions(m_options.reassemblerOptions);
58  m_reliability.setOptions(m_options.reliabilityOptions);
59 }
60 
61 void
62 GenericLinkService::requestIdlePacket()
63 {
64  // No need to request Acks to attach to this packet from LpReliability, as they are already
65  // attached in sendLpPacket
66  this->sendLpPacket({});
67 }
68 
69 void
70 GenericLinkService::sendLpPacket(lp::Packet&& pkt)
71 {
72  const ssize_t mtu = this->getTransport()->getMtu();
73  if (m_options.reliabilityOptions.isEnabled) {
74  m_reliability.piggyback(pkt, mtu);
75  }
76 
77  Transport::Packet tp(pkt.wireEncode());
78  if (mtu != MTU_UNLIMITED && tp.packet.size() > static_cast<size_t>(mtu)) {
79  ++this->nOutOverMtu;
80  NFD_LOG_FACE_WARN("attempted to send packet over MTU limit");
81  return;
82  }
83  this->sendPacket(std::move(tp));
84 }
85 
86 void
87 GenericLinkService::doSendInterest(const Interest& interest)
88 {
89  lp::Packet lpPacket(interest.wireEncode());
90 
91  encodeLpFields(interest, lpPacket);
92 
93  this->sendNetPacket(std::move(lpPacket));
94 }
95 
96 void
97 GenericLinkService::doSendData(const Data& data)
98 {
99  lp::Packet lpPacket(data.wireEncode());
100 
101  encodeLpFields(data, lpPacket);
102 
103  this->sendNetPacket(std::move(lpPacket));
104 }
105 
106 void
107 GenericLinkService::doSendNack(const lp::Nack& nack)
108 {
109  lp::Packet lpPacket(nack.getInterest().wireEncode());
110  lpPacket.add<lp::NackField>(nack.getHeader());
111 
112  encodeLpFields(nack, lpPacket);
113 
114  this->sendNetPacket(std::move(lpPacket));
115 }
116 
117 void
118 GenericLinkService::encodeLpFields(const ndn::TagHost& netPkt, lp::Packet& lpPacket)
119 {
120  if (m_options.allowLocalFields) {
121  shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = netPkt.getTag<lp::IncomingFaceIdTag>();
122  if (incomingFaceIdTag != nullptr) {
123  lpPacket.add<lp::IncomingFaceIdField>(*incomingFaceIdTag);
124  }
125  }
126 
127  shared_ptr<lp::CongestionMarkTag> congestionMarkTag = netPkt.getTag<lp::CongestionMarkTag>();
128  if (congestionMarkTag != nullptr) {
129  lpPacket.add<lp::CongestionMarkField>(*congestionMarkTag);
130  }
131 }
132 
133 void
134 GenericLinkService::sendNetPacket(lp::Packet&& pkt)
135 {
136  std::vector<lp::Packet> frags;
137  ssize_t mtu = this->getTransport()->getMtu();
138 
139  // Make space for feature fields in fragments
140  if (m_options.reliabilityOptions.isEnabled && mtu != MTU_UNLIMITED) {
142  BOOST_ASSERT(mtu > 0);
143  }
144 
145  if (m_options.allowFragmentation && mtu != MTU_UNLIMITED) {
146  bool isOk = false;
147  std::tie(isOk, frags) = m_fragmenter.fragmentPacket(pkt, mtu);
148  if (!isOk) {
149  // fragmentation failed (warning is logged by LpFragmenter)
150  ++this->nFragmentationErrors;
151  return;
152  }
153  }
154  else {
155  frags.push_back(std::move(pkt));
156  }
157 
158  if (frags.size() == 1) {
159  // even if indexed fragmentation is enabled, the fragmenter should not
160  // fragment the packet if it can fit in MTU
161  BOOST_ASSERT(!frags.front().has<lp::FragIndexField>());
162  BOOST_ASSERT(!frags.front().has<lp::FragCountField>());
163  }
164 
165  // Only assign sequences to fragments if packet contains more than 1 fragment
166  if (frags.size() > 1) {
167  // Assign sequences to all fragments
168  this->assignSequences(frags);
169  }
170 
171  if (m_options.reliabilityOptions.isEnabled && frags.front().has<lp::FragmentField>()) {
172  m_reliability.handleOutgoing(frags);
173  }
174 
175  for (lp::Packet& frag : frags) {
176  this->sendLpPacket(std::move(frag));
177  }
178 }
179 
180 void
181 GenericLinkService::assignSequence(lp::Packet& pkt)
182 {
183  pkt.set<lp::SequenceField>(++m_lastSeqNo);
184 }
185 
186 void
187 GenericLinkService::assignSequences(std::vector<lp::Packet>& pkts)
188 {
189  std::for_each(pkts.begin(), pkts.end(), bind(&GenericLinkService::assignSequence, this, _1));
190 }
191 
192 void
193 GenericLinkService::doReceivePacket(Transport::Packet&& packet)
194 {
195  try {
196  lp::Packet pkt(packet.packet);
197 
198  if (m_options.reliabilityOptions.isEnabled) {
199  m_reliability.processIncomingPacket(pkt);
200  }
201 
202  if (!pkt.has<lp::FragmentField>()) {
203  NFD_LOG_FACE_TRACE("received IDLE packet: DROP");
204  return;
205  }
206 
207  if ((pkt.has<lp::FragIndexField>() || pkt.has<lp::FragCountField>()) &&
208  !m_options.allowReassembly) {
209  NFD_LOG_FACE_WARN("received fragment, but reassembly disabled: DROP");
210  return;
211  }
212 
213  bool isReassembled = false;
214  Block netPkt;
215  lp::Packet firstPkt;
216  std::tie(isReassembled, netPkt, firstPkt) = m_reassembler.receiveFragment(packet.remoteEndpoint,
217  pkt);
218  if (isReassembled) {
219  this->decodeNetPacket(netPkt, firstPkt);
220  }
221  }
222  catch (const tlv::Error& e) {
223  ++this->nInLpInvalid;
224  NFD_LOG_FACE_WARN("packet parse error (" << e.what() << "): DROP");
225  }
226 }
227 
228 void
229 GenericLinkService::decodeNetPacket(const Block& netPkt, const lp::Packet& firstPkt)
230 {
231  try {
232  switch (netPkt.type()) {
233  case tlv::Interest:
234  if (firstPkt.has<lp::NackField>()) {
235  this->decodeNack(netPkt, firstPkt);
236  }
237  else {
238  this->decodeInterest(netPkt, firstPkt);
239  }
240  break;
241  case tlv::Data:
242  this->decodeData(netPkt, firstPkt);
243  break;
244  default:
245  ++this->nInNetInvalid;
246  NFD_LOG_FACE_WARN("unrecognized network-layer packet TLV-TYPE " << netPkt.type() << ": DROP");
247  return;
248  }
249  }
250  catch (const tlv::Error& e) {
251  ++this->nInNetInvalid;
252  NFD_LOG_FACE_WARN("packet parse error (" << e.what() << "): DROP");
253  }
254 }
255 
256 void
257 GenericLinkService::decodeInterest(const Block& netPkt, const lp::Packet& firstPkt)
258 {
259  BOOST_ASSERT(netPkt.type() == tlv::Interest);
260  BOOST_ASSERT(!firstPkt.has<lp::NackField>());
261 
262  // forwarding expects Interest to be created with make_shared
263  auto interest = make_shared<Interest>(netPkt);
264 
265  if (firstPkt.has<lp::NextHopFaceIdField>()) {
266  if (m_options.allowLocalFields) {
267  interest->setTag(make_shared<lp::NextHopFaceIdTag>(firstPkt.get<lp::NextHopFaceIdField>()));
268  }
269  else {
270  NFD_LOG_FACE_WARN("received NextHopFaceId, but local fields disabled: DROP");
271  return;
272  }
273  }
274 
275  if (firstPkt.has<lp::CachePolicyField>()) {
276  ++this->nInNetInvalid;
277  NFD_LOG_FACE_WARN("received CachePolicy with Interest: DROP");
278  return;
279  }
280 
281  if (firstPkt.has<lp::IncomingFaceIdField>()) {
282  NFD_LOG_FACE_WARN("received IncomingFaceId: IGNORE");
283  }
284 
285  if (firstPkt.has<lp::CongestionMarkField>()) {
286  interest->setTag(make_shared<lp::CongestionMarkTag>(firstPkt.get<lp::CongestionMarkField>()));
287  }
288 
289  this->receiveInterest(*interest);
290 }
291 
292 void
293 GenericLinkService::decodeData(const Block& netPkt, const lp::Packet& firstPkt)
294 {
295  BOOST_ASSERT(netPkt.type() == tlv::Data);
296 
297  // forwarding expects Data to be created with make_shared
298  auto data = make_shared<Data>(netPkt);
299 
300  if (firstPkt.has<lp::NackField>()) {
301  ++this->nInNetInvalid;
302  NFD_LOG_FACE_WARN("received Nack with Data: DROP");
303  return;
304  }
305 
306  if (firstPkt.has<lp::NextHopFaceIdField>()) {
307  ++this->nInNetInvalid;
308  NFD_LOG_FACE_WARN("received NextHopFaceId with Data: DROP");
309  return;
310  }
311 
312  if (firstPkt.has<lp::CachePolicyField>()) {
313  // CachePolicy is unprivileged and does not require allowLocalFields option.
314  // In case of an invalid CachePolicyType, get<lp::CachePolicyField> will throw,
315  // so it's unnecessary to check here.
316  data->setTag(make_shared<lp::CachePolicyTag>(firstPkt.get<lp::CachePolicyField>()));
317  }
318 
319  if (firstPkt.has<lp::IncomingFaceIdField>()) {
320  NFD_LOG_FACE_WARN("received IncomingFaceId: IGNORE");
321  }
322 
323  if (firstPkt.has<lp::CongestionMarkField>()) {
324  data->setTag(make_shared<lp::CongestionMarkTag>(firstPkt.get<lp::CongestionMarkField>()));
325  }
326 
327  this->receiveData(*data);
328 }
329 
330 void
331 GenericLinkService::decodeNack(const Block& netPkt, const lp::Packet& firstPkt)
332 {
333  BOOST_ASSERT(netPkt.type() == tlv::Interest);
334  BOOST_ASSERT(firstPkt.has<lp::NackField>());
335 
336  lp::Nack nack((Interest(netPkt)));
337  nack.setHeader(firstPkt.get<lp::NackField>());
338 
339  if (firstPkt.has<lp::NextHopFaceIdField>()) {
340  ++this->nInNetInvalid;
341  NFD_LOG_FACE_WARN("received NextHopFaceId with Nack: DROP");
342  return;
343  }
344 
345  if (firstPkt.has<lp::CachePolicyField>()) {
346  ++this->nInNetInvalid;
347  NFD_LOG_FACE_WARN("received CachePolicy with Nack: DROP");
348  return;
349  }
350 
351  if (firstPkt.has<lp::IncomingFaceIdField>()) {
352  NFD_LOG_FACE_WARN("received IncomingFaceId: IGNORE");
353  }
354 
355  if (firstPkt.has<lp::CongestionMarkField>()) {
356  nack.setTag(make_shared<lp::CongestionMarkTag>(firstPkt.get<lp::CongestionMarkField>()));
357  }
358 
359  this->receiveNack(nack);
360 }
361 
362 } // namespace face
363 } // namespace nfd
void processIncomingPacket(const lp::Packet &pkt)
extract and parse all Acks and add Ack for contained Fragment (if any) to AckQueue ...
#define NFD_LOG_FACE_TRACE(msg)
Log a message at TRACE level.
Definition: face-log.hpp:79
const ssize_t MTU_UNLIMITED
indicates the transport has no limit on payload size
Definition: transport.hpp:95
ssize_t getMtu() const
Definition: transport.hpp:432
void piggyback(lp::Packet &pkt, ssize_t mtu)
called by GenericLinkService to attach Acks onto an outgoing LpPacket
bool isEnabled
enables link-layer reliability
void setOptions(const Options &options)
set options for fragmenter
stores a packet along with the remote endpoint
Definition: transport.hpp:113
#define NFD_LOG_FACE_WARN(msg)
Log a message at WARN level.
Definition: face-log.hpp:88
static constexpr size_t RESERVED_HEADER_SPACE
TxSequence TLV-TYPE (3 octets) + TxSequence TLV-LENGTH (1 octet) + sizeof(lp::Sequence) ...
void handleOutgoing(std::vector< lp::Packet > &frags)
observe outgoing fragment(s) of a network packet and store for potential retransmission ...
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
signal::Signal< LpReassembler, Transport::EndpointId, size_t > beforeTimeout
signals before a partial packet is dropped due to timeout
std::tuple< bool, std::vector< lp::Packet > > fragmentPacket(const lp::Packet &packet, size_t mtu)
fragments a network-layer packet into link-layer packets
void setOptions(const Options &options)
set options for reliability
#define NFD_LOG_INIT(name)
Definition: logger.hpp:122
std::tuple< bool, Block, lp::Packet > receiveFragment(Transport::EndpointId remoteEndpoint, const lp::Packet &packet)
adds received fragment to buffer
void setOptions(const Options &options)
set options for reassembler