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 | /* |
| 3 | * Copyright (c) 2014-2018, 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 | |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 29 | #include <ndn-cxx/encoding/tlv.hpp> |
| 30 | |
| 31 | namespace nfd { |
| 32 | namespace face { |
| 33 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 34 | NFD_LOG_INIT(LpFragmenter); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 35 | |
| 36 | static_assert(lp::tlv::LpPacket < 253, "LpPacket TLV-TYPE must fit in 1 octet"); |
| 37 | static_assert(lp::tlv::Sequence < 253, "Sequence TLV-TYPE must fit in 1 octet"); |
| 38 | static_assert(lp::tlv::FragIndex < 253, "FragIndex TLV-TYPE must fit in 1 octet"); |
| 39 | static_assert(lp::tlv::FragCount < 253, "FragCount TLV-TYPE must fit in 1 octet"); |
| 40 | static_assert(lp::tlv::Fragment < 253, "Fragment TLV-TYPE must fit in 1 octet"); |
| 41 | |
| 42 | /** \brief maximum overhead on a single fragment, |
| 43 | * not counting other NDNLPv2 headers |
| 44 | */ |
| 45 | static const size_t MAX_SINGLE_FRAG_OVERHEAD = |
| 46 | 1 + 9 + // LpPacket TLV-TYPE and TLV-LENGTH |
| 47 | 1 + 1 + 8 + // Sequence TLV |
| 48 | 1 + 9; // Fragment TLV-TYPE and TLV-LENGTH |
| 49 | |
| 50 | /** \brief maximum overhead of adding fragmentation to payload, |
| 51 | * not counting other NDNLPv2 headers |
| 52 | */ |
| 53 | static const size_t MAX_FRAG_OVERHEAD = |
| 54 | 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 | |
| 60 | LpFragmenter::Options::Options() |
| 61 | : nMaxFragments(400) |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | LpFragmenter::LpFragmenter(const LpFragmenter::Options& options, const LinkService* linkService) |
| 66 | : m_options(options) |
| 67 | , m_linkService(linkService) |
| 68 | { |
| 69 | } |
| 70 | |
| 71 | void |
| 72 | LpFragmenter::setOptions(const Options& options) |
| 73 | { |
| 74 | m_options = options; |
| 75 | } |
| 76 | |
| 77 | const LinkService* |
| 78 | LpFragmenter::getLinkService() const |
| 79 | { |
| 80 | return m_linkService; |
| 81 | } |
| 82 | |
| 83 | std::tuple<bool, std::vector<lp::Packet>> |
| 84 | LpFragmenter::fragmentPacket(const lp::Packet& packet, size_t mtu) |
| 85 | { |
| 86 | BOOST_ASSERT(packet.has<lp::FragmentField>()); |
| 87 | BOOST_ASSERT(!packet.has<lp::FragIndexField>()); |
| 88 | BOOST_ASSERT(!packet.has<lp::FragCountField>()); |
| 89 | |
| 90 | if (MAX_SINGLE_FRAG_OVERHEAD + packet.wireEncode().size() <= mtu) { |
| 91 | // fast path: fragmentation not needed |
| 92 | // To qualify for fast path, the packet must have space for adding a sequence number, |
| 93 | // because another NDNLPv2 feature may require the sequence number. |
| 94 | return std::make_tuple(true, std::vector<lp::Packet>{packet}); |
| 95 | } |
| 96 | |
| 97 | ndn::Buffer::const_iterator netPktBegin, netPktEnd; |
| 98 | std::tie(netPktBegin, netPktEnd) = packet.get<lp::FragmentField>(); |
| 99 | size_t netPktSize = std::distance(netPktBegin, netPktEnd); |
| 100 | |
| 101 | // compute size of other NDNLPv2 headers to be placed on the first fragment |
| 102 | size_t firstHeaderSize = 0; |
| 103 | const Block& packetWire = packet.wireEncode(); |
| 104 | if (packetWire.type() == lp::tlv::LpPacket) { |
| 105 | for (const Block& element : packetWire.elements()) { |
| 106 | if (element.type() != lp::tlv::Fragment) { |
| 107 | firstHeaderSize += element.size(); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // compute payload size |
| 113 | if (MAX_FRAG_OVERHEAD + firstHeaderSize + 1 > mtu) { // 1-octet fragment |
| 114 | NFD_LOG_FACE_WARN("fragmentation error, MTU too small for first fragment: DROP"); |
| 115 | return std::make_tuple(false, std::vector<lp::Packet>{}); |
| 116 | } |
| 117 | size_t firstPayloadSize = std::min(netPktSize, mtu - firstHeaderSize - MAX_FRAG_OVERHEAD); |
| 118 | size_t payloadSize = mtu - MAX_FRAG_OVERHEAD; |
| 119 | size_t fragCount = 1 + ((netPktSize - firstPayloadSize) / payloadSize) + |
| 120 | ((netPktSize - firstPayloadSize) % payloadSize != 0); |
| 121 | |
| 122 | // compute FragCount |
| 123 | if (fragCount > m_options.nMaxFragments) { |
| 124 | NFD_LOG_FACE_WARN("fragmentation error, FragCount over limit: DROP"); |
| 125 | return std::make_pair(false, std::vector<lp::Packet>{}); |
| 126 | } |
| 127 | |
| 128 | // populate fragments |
| 129 | std::vector<lp::Packet> frags(fragCount); |
| 130 | frags.front() = packet; // copy input packet to preserve other NDNLPv2 fields |
| 131 | size_t fragIndex = 0; |
| 132 | auto fragBegin = netPktBegin, |
| 133 | fragEnd = fragBegin + firstPayloadSize; |
| 134 | while (fragBegin < netPktEnd) { |
| 135 | lp::Packet& frag = frags[fragIndex]; |
| 136 | frag.add<lp::FragIndexField>(fragIndex); |
| 137 | frag.add<lp::FragCountField>(fragCount); |
| 138 | frag.set<lp::FragmentField>(std::make_pair(fragBegin, fragEnd)); |
| 139 | BOOST_ASSERT(frag.wireEncode().size() <= mtu); |
| 140 | |
| 141 | ++fragIndex; |
| 142 | fragBegin = fragEnd; |
| 143 | fragEnd = std::min(netPktEnd, fragBegin + payloadSize); |
| 144 | } |
| 145 | BOOST_ASSERT(fragIndex == fragCount); |
| 146 | |
| 147 | return std::make_pair(true, frags); |
| 148 | } |
| 149 | |
| 150 | std::ostream& |
| 151 | operator<<(std::ostream& os, const FaceLogHelper<LpFragmenter>& flh) |
| 152 | { |
| 153 | if (flh.obj.getLinkService() == nullptr) { |
| 154 | os << "[id=0,local=unknown,remote=unknown] "; |
| 155 | } |
| 156 | else { |
| 157 | os << FaceLogHelper<LinkService>(*flh.obj.getLinkService()); |
| 158 | } |
| 159 | return os; |
| 160 | } |
| 161 | |
| 162 | } // namespace face |
| 163 | } // namespace nfd |