blob: a9fcf175a06e3474d41c5e9a58d161fe555b1431 [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 Pesaventoa3a7a4e2022-05-29 16:06:22 -04003 * Copyright (c) 2014-2022, 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-fragmenter.hpp"
27#include "link-service.hpp"
Davide Pesaventoa3148082018-04-12 18:21:54 -040028
Eric Newberry4c3e6b82015-11-10 16:48:42 -070029#include <ndn-cxx/encoding/tlv.hpp>
30
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040031namespace nfd::face {
Eric Newberry4c3e6b82015-11-10 16:48:42 -070032
Davide Pesaventoa3148082018-04-12 18:21:54 -040033NFD_LOG_INIT(LpFragmenter);
Eric Newberry4c3e6b82015-11-10 16:48:42 -070034
35static_assert(lp::tlv::LpPacket < 253, "LpPacket TLV-TYPE must fit in 1 octet");
36static_assert(lp::tlv::Sequence < 253, "Sequence TLV-TYPE must fit in 1 octet");
37static_assert(lp::tlv::FragIndex < 253, "FragIndex TLV-TYPE must fit in 1 octet");
38static_assert(lp::tlv::FragCount < 253, "FragCount TLV-TYPE must fit in 1 octet");
39static_assert(lp::tlv::Fragment < 253, "Fragment TLV-TYPE must fit in 1 octet");
40
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040041/**
42 * \brief Maximum overhead on a single fragment, not counting other NDNLPv2 headers.
Eric Newberry4c3e6b82015-11-10 16:48:42 -070043 */
Davide Pesavento20d94f62022-09-26 03:30:53 -040044constexpr size_t MAX_SINGLE_FRAG_OVERHEAD =
Eric Newberry4c3e6b82015-11-10 16:48:42 -070045 1 + 9 + // LpPacket TLV-TYPE and TLV-LENGTH
46 1 + 1 + 8 + // Sequence TLV
47 1 + 9; // Fragment TLV-TYPE and TLV-LENGTH
48
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040049/**
50 * \brief Maximum overhead of adding fragmentation to payload, not counting other NDNLPv2 headers.
Eric Newberry4c3e6b82015-11-10 16:48:42 -070051 */
Davide Pesavento20d94f62022-09-26 03:30:53 -040052constexpr size_t MAX_FRAG_OVERHEAD =
Eric Newberry4c3e6b82015-11-10 16:48:42 -070053 1 + 9 + // LpPacket TLV-TYPE and TLV-LENGTH
54 1 + 1 + 8 + // Sequence TLV
55 1 + 1 + 8 + // FragIndex TLV
56 1 + 1 + 8 + // FragCount TLV
57 1 + 9; // Fragment TLV-TYPE and TLV-LENGTH
58
Eric Newberry4c3e6b82015-11-10 16:48:42 -070059LpFragmenter::LpFragmenter(const LpFragmenter::Options& options, const LinkService* linkService)
60 : m_options(options)
61 , m_linkService(linkService)
62{
63}
64
65void
66LpFragmenter::setOptions(const Options& options)
67{
68 m_options = options;
69}
70
71const LinkService*
72LpFragmenter::getLinkService() const
73{
74 return m_linkService;
75}
76
77std::tuple<bool, std::vector<lp::Packet>>
78LpFragmenter::fragmentPacket(const lp::Packet& packet, size_t mtu)
79{
80 BOOST_ASSERT(packet.has<lp::FragmentField>());
81 BOOST_ASSERT(!packet.has<lp::FragIndexField>());
82 BOOST_ASSERT(!packet.has<lp::FragCountField>());
83
84 if (MAX_SINGLE_FRAG_OVERHEAD + packet.wireEncode().size() <= mtu) {
85 // fast path: fragmentation not needed
86 // To qualify for fast path, the packet must have space for adding a sequence number,
87 // because another NDNLPv2 feature may require the sequence number.
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040088 return {true, {packet}};
Eric Newberry4c3e6b82015-11-10 16:48:42 -070089 }
90
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040091 auto [netPktBegin, netPktEnd] = packet.get<lp::FragmentField>();
Eric Newberry4c3e6b82015-11-10 16:48:42 -070092 size_t netPktSize = std::distance(netPktBegin, netPktEnd);
93
94 // compute size of other NDNLPv2 headers to be placed on the first fragment
95 size_t firstHeaderSize = 0;
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040096 const auto& packetWire = packet.wireEncode();
Eric Newberry4c3e6b82015-11-10 16:48:42 -070097 if (packetWire.type() == lp::tlv::LpPacket) {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040098 for (const auto& element : packetWire.elements()) {
Eric Newberry4c3e6b82015-11-10 16:48:42 -070099 if (element.type() != lp::tlv::Fragment) {
100 firstHeaderSize += element.size();
101 }
102 }
103 }
104
105 // compute payload size
106 if (MAX_FRAG_OVERHEAD + firstHeaderSize + 1 > mtu) { // 1-octet fragment
107 NFD_LOG_FACE_WARN("fragmentation error, MTU too small for first fragment: DROP");
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400108 return {false, {}};
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700109 }
110 size_t firstPayloadSize = std::min(netPktSize, mtu - firstHeaderSize - MAX_FRAG_OVERHEAD);
111 size_t payloadSize = mtu - MAX_FRAG_OVERHEAD;
112 size_t fragCount = 1 + ((netPktSize - firstPayloadSize) / payloadSize) +
113 ((netPktSize - firstPayloadSize) % payloadSize != 0);
114
115 // compute FragCount
116 if (fragCount > m_options.nMaxFragments) {
117 NFD_LOG_FACE_WARN("fragmentation error, FragCount over limit: DROP");
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400118 return {false, {}};
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700119 }
120
121 // populate fragments
122 std::vector<lp::Packet> frags(fragCount);
123 frags.front() = packet; // copy input packet to preserve other NDNLPv2 fields
124 size_t fragIndex = 0;
125 auto fragBegin = netPktBegin,
126 fragEnd = fragBegin + firstPayloadSize;
127 while (fragBegin < netPktEnd) {
128 lp::Packet& frag = frags[fragIndex];
129 frag.add<lp::FragIndexField>(fragIndex);
130 frag.add<lp::FragCountField>(fragCount);
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400131 frag.set<lp::FragmentField>({fragBegin, fragEnd});
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700132 BOOST_ASSERT(frag.wireEncode().size() <= mtu);
133
134 ++fragIndex;
135 fragBegin = fragEnd;
136 fragEnd = std::min(netPktEnd, fragBegin + payloadSize);
137 }
138 BOOST_ASSERT(fragIndex == fragCount);
139
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400140 return {true, frags};
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700141}
142
143std::ostream&
144operator<<(std::ostream& os, const FaceLogHelper<LpFragmenter>& flh)
145{
146 if (flh.obj.getLinkService() == nullptr) {
147 os << "[id=0,local=unknown,remote=unknown] ";
148 }
149 else {
150 os << FaceLogHelper<LinkService>(*flh.obj.getLinkService());
151 }
152 return os;
153}
154
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400155} // namespace nfd::face