Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | e0b67df | 2024-02-17 19:14:24 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, Regents of the University of California, |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 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-fragmenter.hpp" |
| 27 | #include "link-service.hpp" |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 28 | |
Davide Pesavento | 9a63bf2 | 2023-11-11 17:12:51 -0500 | [diff] [blame] | 29 | #include <ndn-cxx/lp/fields.hpp> |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 30 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 31 | namespace nfd::face { |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 32 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 33 | NFD_LOG_INIT(LpFragmenter); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 34 | |
| 35 | static_assert(lp::tlv::LpPacket < 253, "LpPacket TLV-TYPE must fit in 1 octet"); |
| 36 | static_assert(lp::tlv::Sequence < 253, "Sequence TLV-TYPE must fit in 1 octet"); |
| 37 | static_assert(lp::tlv::FragIndex < 253, "FragIndex TLV-TYPE must fit in 1 octet"); |
| 38 | static_assert(lp::tlv::FragCount < 253, "FragCount TLV-TYPE must fit in 1 octet"); |
| 39 | static_assert(lp::tlv::Fragment < 253, "Fragment TLV-TYPE must fit in 1 octet"); |
| 40 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 41 | /** |
| 42 | * \brief Maximum overhead on a single fragment, not counting other NDNLPv2 headers. |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 43 | */ |
Davide Pesavento | 20d94f6 | 2022-09-26 03:30:53 -0400 | [diff] [blame] | 44 | constexpr size_t MAX_SINGLE_FRAG_OVERHEAD = |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 45 | 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 Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 49 | /** |
| 50 | * \brief Maximum overhead of adding fragmentation to payload, not counting other NDNLPv2 headers. |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 51 | */ |
Davide Pesavento | 20d94f6 | 2022-09-26 03:30:53 -0400 | [diff] [blame] | 52 | constexpr size_t MAX_FRAG_OVERHEAD = |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 53 | 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 Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 59 | LpFragmenter::LpFragmenter(const LpFragmenter::Options& options, const LinkService* linkService) |
| 60 | : m_options(options) |
| 61 | , m_linkService(linkService) |
| 62 | { |
| 63 | } |
| 64 | |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 65 | std::tuple<bool, std::vector<lp::Packet>> |
| 66 | LpFragmenter::fragmentPacket(const lp::Packet& packet, size_t mtu) |
| 67 | { |
| 68 | BOOST_ASSERT(packet.has<lp::FragmentField>()); |
| 69 | BOOST_ASSERT(!packet.has<lp::FragIndexField>()); |
| 70 | BOOST_ASSERT(!packet.has<lp::FragCountField>()); |
| 71 | |
| 72 | if (MAX_SINGLE_FRAG_OVERHEAD + packet.wireEncode().size() <= mtu) { |
| 73 | // fast path: fragmentation not needed |
| 74 | // To qualify for fast path, the packet must have space for adding a sequence number, |
| 75 | // because another NDNLPv2 feature may require the sequence number. |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 76 | return {true, {packet}}; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 79 | auto [netPktBegin, netPktEnd] = packet.get<lp::FragmentField>(); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 80 | size_t netPktSize = std::distance(netPktBegin, netPktEnd); |
| 81 | |
| 82 | // compute size of other NDNLPv2 headers to be placed on the first fragment |
| 83 | size_t firstHeaderSize = 0; |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 84 | const auto& packetWire = packet.wireEncode(); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 85 | if (packetWire.type() == lp::tlv::LpPacket) { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 86 | for (const auto& element : packetWire.elements()) { |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 87 | if (element.type() != lp::tlv::Fragment) { |
| 88 | firstHeaderSize += element.size(); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // compute payload size |
| 94 | if (MAX_FRAG_OVERHEAD + firstHeaderSize + 1 > mtu) { // 1-octet fragment |
| 95 | NFD_LOG_FACE_WARN("fragmentation error, MTU too small for first fragment: DROP"); |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 96 | return {false, {}}; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 97 | } |
| 98 | size_t firstPayloadSize = std::min(netPktSize, mtu - firstHeaderSize - MAX_FRAG_OVERHEAD); |
| 99 | size_t payloadSize = mtu - MAX_FRAG_OVERHEAD; |
| 100 | size_t fragCount = 1 + ((netPktSize - firstPayloadSize) / payloadSize) + |
| 101 | ((netPktSize - firstPayloadSize) % payloadSize != 0); |
| 102 | |
| 103 | // compute FragCount |
| 104 | if (fragCount > m_options.nMaxFragments) { |
| 105 | NFD_LOG_FACE_WARN("fragmentation error, FragCount over limit: DROP"); |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 106 | return {false, {}}; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // populate fragments |
| 110 | std::vector<lp::Packet> frags(fragCount); |
| 111 | frags.front() = packet; // copy input packet to preserve other NDNLPv2 fields |
| 112 | size_t fragIndex = 0; |
| 113 | auto fragBegin = netPktBegin, |
| 114 | fragEnd = fragBegin + firstPayloadSize; |
| 115 | while (fragBegin < netPktEnd) { |
| 116 | lp::Packet& frag = frags[fragIndex]; |
| 117 | frag.add<lp::FragIndexField>(fragIndex); |
| 118 | frag.add<lp::FragCountField>(fragCount); |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 119 | frag.set<lp::FragmentField>({fragBegin, fragEnd}); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 120 | BOOST_ASSERT(frag.wireEncode().size() <= mtu); |
| 121 | |
| 122 | ++fragIndex; |
| 123 | fragBegin = fragEnd; |
| 124 | fragEnd = std::min(netPktEnd, fragBegin + payloadSize); |
| 125 | } |
| 126 | BOOST_ASSERT(fragIndex == fragCount); |
| 127 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 128 | return {true, frags}; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | std::ostream& |
| 132 | operator<<(std::ostream& os, const FaceLogHelper<LpFragmenter>& flh) |
| 133 | { |
| 134 | if (flh.obj.getLinkService() == nullptr) { |
| 135 | os << "[id=0,local=unknown,remote=unknown] "; |
| 136 | } |
| 137 | else { |
| 138 | os << FaceLogHelper<LinkService>(*flh.obj.getLinkService()); |
| 139 | } |
| 140 | return os; |
| 141 | } |
| 142 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 143 | } // namespace nfd::face |