blob: 6b1755aa36b18d6a80faf1d528fc4feb2574e42a [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
31namespace nfd {
32namespace face {
33
Davide Pesaventoa3148082018-04-12 18:21:54 -040034NFD_LOG_INIT(LpFragmenter);
Eric Newberry4c3e6b82015-11-10 16:48:42 -070035
36static_assert(lp::tlv::LpPacket < 253, "LpPacket TLV-TYPE must fit in 1 octet");
37static_assert(lp::tlv::Sequence < 253, "Sequence TLV-TYPE must fit in 1 octet");
38static_assert(lp::tlv::FragIndex < 253, "FragIndex TLV-TYPE must fit in 1 octet");
39static_assert(lp::tlv::FragCount < 253, "FragCount TLV-TYPE must fit in 1 octet");
40static_assert(lp::tlv::Fragment < 253, "Fragment TLV-TYPE must fit in 1 octet");
41
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040042/**
43 * \brief Maximum overhead on a single fragment, not counting other NDNLPv2 headers.
Eric Newberry4c3e6b82015-11-10 16:48:42 -070044 */
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040045const size_t MAX_SINGLE_FRAG_OVERHEAD =
Eric Newberry4c3e6b82015-11-10 16:48:42 -070046 1 + 9 + // LpPacket TLV-TYPE and TLV-LENGTH
47 1 + 1 + 8 + // Sequence TLV
48 1 + 9; // Fragment TLV-TYPE and TLV-LENGTH
49
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040050/**
51 * \brief Maximum overhead of adding fragmentation to payload, not counting other NDNLPv2 headers.
Eric Newberry4c3e6b82015-11-10 16:48:42 -070052 */
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040053const size_t MAX_FRAG_OVERHEAD =
Eric Newberry4c3e6b82015-11-10 16:48:42 -070054 1 + 9 + // LpPacket TLV-TYPE and TLV-LENGTH
55 1 + 1 + 8 + // Sequence TLV
56 1 + 1 + 8 + // FragIndex TLV
57 1 + 1 + 8 + // FragCount TLV
58 1 + 9; // Fragment TLV-TYPE and TLV-LENGTH
59
Eric Newberry4c3e6b82015-11-10 16:48:42 -070060LpFragmenter::LpFragmenter(const LpFragmenter::Options& options, const LinkService* linkService)
61 : m_options(options)
62 , m_linkService(linkService)
63{
64}
65
66void
67LpFragmenter::setOptions(const Options& options)
68{
69 m_options = options;
70}
71
72const LinkService*
73LpFragmenter::getLinkService() const
74{
75 return m_linkService;
76}
77
78std::tuple<bool, std::vector<lp::Packet>>
79LpFragmenter::fragmentPacket(const lp::Packet& packet, size_t mtu)
80{
81 BOOST_ASSERT(packet.has<lp::FragmentField>());
82 BOOST_ASSERT(!packet.has<lp::FragIndexField>());
83 BOOST_ASSERT(!packet.has<lp::FragCountField>());
84
85 if (MAX_SINGLE_FRAG_OVERHEAD + packet.wireEncode().size() <= mtu) {
86 // fast path: fragmentation not needed
87 // To qualify for fast path, the packet must have space for adding a sequence number,
88 // because another NDNLPv2 feature may require the sequence number.
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040089 return {true, {packet}};
Eric Newberry4c3e6b82015-11-10 16:48:42 -070090 }
91
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040092 auto [netPktBegin, netPktEnd] = packet.get<lp::FragmentField>();
Eric Newberry4c3e6b82015-11-10 16:48:42 -070093 size_t netPktSize = std::distance(netPktBegin, netPktEnd);
94
95 // compute size of other NDNLPv2 headers to be placed on the first fragment
96 size_t firstHeaderSize = 0;
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040097 const auto& packetWire = packet.wireEncode();
Eric Newberry4c3e6b82015-11-10 16:48:42 -070098 if (packetWire.type() == lp::tlv::LpPacket) {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040099 for (const auto& element : packetWire.elements()) {
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700100 if (element.type() != lp::tlv::Fragment) {
101 firstHeaderSize += element.size();
102 }
103 }
104 }
105
106 // compute payload size
107 if (MAX_FRAG_OVERHEAD + firstHeaderSize + 1 > mtu) { // 1-octet fragment
108 NFD_LOG_FACE_WARN("fragmentation error, MTU too small for first fragment: DROP");
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400109 return {false, {}};
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700110 }
111 size_t firstPayloadSize = std::min(netPktSize, mtu - firstHeaderSize - MAX_FRAG_OVERHEAD);
112 size_t payloadSize = mtu - MAX_FRAG_OVERHEAD;
113 size_t fragCount = 1 + ((netPktSize - firstPayloadSize) / payloadSize) +
114 ((netPktSize - firstPayloadSize) % payloadSize != 0);
115
116 // compute FragCount
117 if (fragCount > m_options.nMaxFragments) {
118 NFD_LOG_FACE_WARN("fragmentation error, FragCount over limit: DROP");
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400119 return {false, {}};
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700120 }
121
122 // populate fragments
123 std::vector<lp::Packet> frags(fragCount);
124 frags.front() = packet; // copy input packet to preserve other NDNLPv2 fields
125 size_t fragIndex = 0;
126 auto fragBegin = netPktBegin,
127 fragEnd = fragBegin + firstPayloadSize;
128 while (fragBegin < netPktEnd) {
129 lp::Packet& frag = frags[fragIndex];
130 frag.add<lp::FragIndexField>(fragIndex);
131 frag.add<lp::FragCountField>(fragCount);
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400132 frag.set<lp::FragmentField>({fragBegin, fragEnd});
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700133 BOOST_ASSERT(frag.wireEncode().size() <= mtu);
134
135 ++fragIndex;
136 fragBegin = fragEnd;
137 fragEnd = std::min(netPktEnd, fragBegin + payloadSize);
138 }
139 BOOST_ASSERT(fragIndex == fragCount);
140
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400141 return {true, frags};
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700142}
143
144std::ostream&
145operator<<(std::ostream& os, const FaceLogHelper<LpFragmenter>& flh)
146{
147 if (flh.obj.getLinkService() == nullptr) {
148 os << "[id=0,local=unknown,remote=unknown] ";
149 }
150 else {
151 os << FaceLogHelper<LinkService>(*flh.obj.getLinkService());
152 }
153 return os;
154}
155
156} // namespace face
157} // namespace nfd