Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | a22a217 | 2015-01-05 11:14:02 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014, 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 |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 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/>. |
Junxiao Shi | a22a217 | 2015-01-05 11:14:02 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 25 | |
| 26 | #include "ndnlp-slicer.hpp" |
| 27 | |
Alexander Afanasyev | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame] | 28 | #include <ndn-cxx/encoding/encoding-buffer.hpp> |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 29 | |
| 30 | namespace nfd { |
| 31 | namespace ndnlp { |
| 32 | |
| 33 | Slicer::Slicer(size_t mtu) |
| 34 | : m_mtu(mtu) |
| 35 | { |
| 36 | this->estimateOverhead(); |
| 37 | } |
| 38 | |
| 39 | Slicer::~Slicer() |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | template<bool T> |
Junxiao Shi | a22a217 | 2015-01-05 11:14:02 -0700 | [diff] [blame] | 44 | size_t |
| 45 | Slicer::encodeFragment(ndn::EncodingImpl<T>& blk, |
| 46 | uint64_t seq, uint16_t fragIndex, uint16_t fragCount, |
| 47 | const uint8_t* payload, size_t payloadSize) |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 48 | { |
| 49 | size_t totalLength = 0; |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 50 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 51 | // NdnlpPayload |
| 52 | size_t payloadLength = blk.prependByteArray(payload, payloadSize); |
| 53 | totalLength += payloadLength; |
| 54 | totalLength += blk.prependVarNumber(payloadLength); |
| 55 | totalLength += blk.prependVarNumber(tlv::NdnlpPayload); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 56 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 57 | bool needFragIndexAndCount = fragCount > 1; |
| 58 | if (needFragIndexAndCount) { |
| 59 | // NdnlpFragCount |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 60 | size_t fragCountLength = blk.prependNonNegativeInteger(fragCount); |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 61 | totalLength += fragCountLength; |
| 62 | totalLength += blk.prependVarNumber(fragCountLength); |
| 63 | totalLength += blk.prependVarNumber(tlv::NdnlpFragCount); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 64 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 65 | // NdnlpFragIndex |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 66 | size_t fragIndexLength = blk.prependNonNegativeInteger(fragIndex); |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 67 | totalLength += fragIndexLength; |
| 68 | totalLength += blk.prependVarNumber(fragIndexLength); |
| 69 | totalLength += blk.prependVarNumber(tlv::NdnlpFragIndex); |
| 70 | } |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 71 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 72 | // NdnlpSequence |
| 73 | uint64_t sequenceBE = htobe64(seq); |
| 74 | size_t sequenceLength = blk.prependByteArray( |
| 75 | reinterpret_cast<uint8_t*>(&sequenceBE), sizeof(sequenceBE)); |
| 76 | totalLength += sequenceLength; |
| 77 | totalLength += blk.prependVarNumber(sequenceLength); |
| 78 | totalLength += blk.prependVarNumber(tlv::NdnlpSequence); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 79 | |
| 80 | // NdnlpData |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 81 | totalLength += blk.prependVarNumber(totalLength); |
| 82 | totalLength += blk.prependVarNumber(tlv::NdnlpData); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 83 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 84 | return totalLength; |
| 85 | } |
| 86 | |
| 87 | void |
| 88 | Slicer::estimateOverhead() |
| 89 | { |
Junxiao Shi | a22a217 | 2015-01-05 11:14:02 -0700 | [diff] [blame] | 90 | // estimate fragment size with all header fields at largest possible size |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 91 | ndn::EncodingEstimator estimator; |
Junxiao Shi | a22a217 | 2015-01-05 11:14:02 -0700 | [diff] [blame] | 92 | size_t estimatedSize = this->encodeFragment(estimator, |
| 93 | std::numeric_limits<uint64_t>::max(), |
| 94 | std::numeric_limits<uint16_t>::max() - 1, |
| 95 | std::numeric_limits<uint16_t>::max(), |
| 96 | nullptr, m_mtu); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 97 | |
Junxiao Shi | a22a217 | 2015-01-05 11:14:02 -0700 | [diff] [blame] | 98 | size_t overhead = estimatedSize - m_mtu; // minus payload length in estimation |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 99 | m_maxPayload = m_mtu - overhead; |
| 100 | } |
| 101 | |
| 102 | PacketArray |
| 103 | Slicer::slice(const Block& block) |
| 104 | { |
| 105 | BOOST_ASSERT(block.hasWire()); |
| 106 | const uint8_t* networkPacket = block.wire(); |
| 107 | size_t networkPacketSize = block.size(); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 108 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 109 | uint16_t fragCount = static_cast<uint16_t>( |
| 110 | (networkPacketSize / m_maxPayload) + |
| 111 | (networkPacketSize % m_maxPayload == 0 ? 0 : 1) |
| 112 | ); |
Junxiao Shi | a22a217 | 2015-01-05 11:14:02 -0700 | [diff] [blame] | 113 | PacketArray pa = make_shared<std::vector<Block>>(); |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 114 | pa->reserve(fragCount); |
| 115 | SequenceBlock seqBlock = m_seqgen.nextBlock(fragCount); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 116 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 117 | for (uint16_t fragIndex = 0; fragIndex < fragCount; ++fragIndex) { |
| 118 | size_t payloadOffset = fragIndex * m_maxPayload; |
| 119 | const uint8_t* payload = networkPacket + payloadOffset; |
| 120 | size_t payloadSize = std::min(m_maxPayload, networkPacketSize - payloadOffset); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 121 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 122 | ndn::EncodingBuffer buffer(m_mtu, 0); |
Junxiao Shi | a22a217 | 2015-01-05 11:14:02 -0700 | [diff] [blame] | 123 | size_t pktSize = this->encodeFragment(buffer, |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 124 | seqBlock[fragIndex], fragIndex, fragCount, payload, payloadSize); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 125 | |
Junxiao Shi | ac7b437 | 2014-12-13 21:47:04 -0700 | [diff] [blame] | 126 | BOOST_VERIFY(pktSize <= m_mtu); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 127 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 128 | pa->push_back(buffer.block()); |
| 129 | } |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 130 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 131 | return pa; |
| 132 | } |
| 133 | |
| 134 | } // namespace ndnlp |
| 135 | } // namespace nfd |