Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -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 | * |
| 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 24 | |
| 25 | #include "ndnlp-slicer.hpp" |
| 26 | |
Alexander Afanasyev | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame] | 27 | #include <ndn-cxx/encoding/encoding-buffer.hpp> |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 28 | |
| 29 | namespace nfd { |
| 30 | namespace ndnlp { |
| 31 | |
| 32 | Slicer::Slicer(size_t mtu) |
| 33 | : m_mtu(mtu) |
| 34 | { |
| 35 | this->estimateOverhead(); |
| 36 | } |
| 37 | |
| 38 | Slicer::~Slicer() |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | template<bool T> |
| 43 | static inline size_t |
| 44 | Slicer_encodeFragment(ndn::EncodingImpl<T>& blk, |
| 45 | uint64_t seq, uint16_t fragIndex, uint16_t fragCount, |
| 46 | const uint8_t* payload, size_t payloadSize) |
| 47 | { |
| 48 | size_t totalLength = 0; |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 49 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 50 | // NdnlpPayload |
| 51 | size_t payloadLength = blk.prependByteArray(payload, payloadSize); |
| 52 | totalLength += payloadLength; |
| 53 | totalLength += blk.prependVarNumber(payloadLength); |
| 54 | totalLength += blk.prependVarNumber(tlv::NdnlpPayload); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 55 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 56 | bool needFragIndexAndCount = fragCount > 1; |
| 57 | if (needFragIndexAndCount) { |
| 58 | // NdnlpFragCount |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 59 | size_t fragCountLength = blk.prependNonNegativeInteger(fragCount); |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 60 | totalLength += fragCountLength; |
| 61 | totalLength += blk.prependVarNumber(fragCountLength); |
| 62 | totalLength += blk.prependVarNumber(tlv::NdnlpFragCount); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 63 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 64 | // NdnlpFragIndex |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 65 | size_t fragIndexLength = blk.prependNonNegativeInteger(fragIndex); |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 66 | totalLength += fragIndexLength; |
| 67 | totalLength += blk.prependVarNumber(fragIndexLength); |
| 68 | totalLength += blk.prependVarNumber(tlv::NdnlpFragIndex); |
| 69 | } |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 70 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 71 | // NdnlpSequence |
| 72 | uint64_t sequenceBE = htobe64(seq); |
| 73 | size_t sequenceLength = blk.prependByteArray( |
| 74 | reinterpret_cast<uint8_t*>(&sequenceBE), sizeof(sequenceBE)); |
| 75 | totalLength += sequenceLength; |
| 76 | totalLength += blk.prependVarNumber(sequenceLength); |
| 77 | totalLength += blk.prependVarNumber(tlv::NdnlpSequence); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 78 | |
| 79 | // NdnlpData |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 80 | totalLength += blk.prependVarNumber(totalLength); |
| 81 | totalLength += blk.prependVarNumber(tlv::NdnlpData); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 82 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 83 | return totalLength; |
| 84 | } |
| 85 | |
| 86 | void |
| 87 | Slicer::estimateOverhead() |
| 88 | { |
| 89 | ndn::EncodingEstimator estimator; |
| 90 | size_t estimatedSize = Slicer_encodeFragment(estimator, |
| 91 | 0, 0, 2, 0, m_mtu); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 92 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 93 | size_t overhead = estimatedSize - m_mtu; |
| 94 | m_maxPayload = m_mtu - overhead; |
| 95 | } |
| 96 | |
| 97 | PacketArray |
| 98 | Slicer::slice(const Block& block) |
| 99 | { |
| 100 | BOOST_ASSERT(block.hasWire()); |
| 101 | const uint8_t* networkPacket = block.wire(); |
| 102 | size_t networkPacketSize = block.size(); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 103 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 104 | uint16_t fragCount = static_cast<uint16_t>( |
| 105 | (networkPacketSize / m_maxPayload) + |
| 106 | (networkPacketSize % m_maxPayload == 0 ? 0 : 1) |
| 107 | ); |
| 108 | PacketArray pa = make_shared<std::vector<Block> >(); |
| 109 | pa->reserve(fragCount); |
| 110 | SequenceBlock seqBlock = m_seqgen.nextBlock(fragCount); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 111 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 112 | for (uint16_t fragIndex = 0; fragIndex < fragCount; ++fragIndex) { |
| 113 | size_t payloadOffset = fragIndex * m_maxPayload; |
| 114 | const uint8_t* payload = networkPacket + payloadOffset; |
| 115 | size_t payloadSize = std::min(m_maxPayload, networkPacketSize - payloadOffset); |
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 | ndn::EncodingBuffer buffer(m_mtu, 0); |
| 118 | size_t pktSize = Slicer_encodeFragment(buffer, |
| 119 | seqBlock[fragIndex], fragIndex, fragCount, payload, payloadSize); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 120 | |
Junxiao Shi | ac7b437 | 2014-12-13 21:47:04 -0700 | [diff] [blame] | 121 | BOOST_VERIFY(pktSize <= m_mtu); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 122 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 123 | pa->push_back(buffer.block()); |
| 124 | } |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 125 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 126 | return pa; |
| 127 | } |
| 128 | |
| 129 | } // namespace ndnlp |
| 130 | } // namespace nfd |