blob: 22d8c3b67bcef2074e230155eb8ec79701618a12 [file] [log] [blame]
Junxiao Shi9b0d3e92014-02-15 12:27:12 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * 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 Shi9b0d3e92014-02-15 12:27:12 -070024
25#include "ndnlp-slicer.hpp"
26
27#include <ndn-cpp-dev/encoding/encoding-buffer.hpp>
28
29namespace nfd {
30namespace ndnlp {
31
32Slicer::Slicer(size_t mtu)
33 : m_mtu(mtu)
34{
35 this->estimateOverhead();
36}
37
38Slicer::~Slicer()
39{
40}
41
42template<bool T>
43static inline size_t
44Slicer_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 Shidf3b4382014-02-23 11:28:21 -070049
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070050 // NdnlpPayload
51 size_t payloadLength = blk.prependByteArray(payload, payloadSize);
52 totalLength += payloadLength;
53 totalLength += blk.prependVarNumber(payloadLength);
54 totalLength += blk.prependVarNumber(tlv::NdnlpPayload);
Junxiao Shidf3b4382014-02-23 11:28:21 -070055
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070056 bool needFragIndexAndCount = fragCount > 1;
57 if (needFragIndexAndCount) {
58 // NdnlpFragCount
Junxiao Shidf3b4382014-02-23 11:28:21 -070059 size_t fragCountLength = blk.prependNonNegativeInteger(fragCount);
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070060 totalLength += fragCountLength;
61 totalLength += blk.prependVarNumber(fragCountLength);
62 totalLength += blk.prependVarNumber(tlv::NdnlpFragCount);
Junxiao Shidf3b4382014-02-23 11:28:21 -070063
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070064 // NdnlpFragIndex
Junxiao Shidf3b4382014-02-23 11:28:21 -070065 size_t fragIndexLength = blk.prependNonNegativeInteger(fragIndex);
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070066 totalLength += fragIndexLength;
67 totalLength += blk.prependVarNumber(fragIndexLength);
68 totalLength += blk.prependVarNumber(tlv::NdnlpFragIndex);
69 }
Junxiao Shidf3b4382014-02-23 11:28:21 -070070
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070071 // 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 Shidf3b4382014-02-23 11:28:21 -070078
79 // NdnlpData
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070080 totalLength += blk.prependVarNumber(totalLength);
81 totalLength += blk.prependVarNumber(tlv::NdnlpData);
Junxiao Shidf3b4382014-02-23 11:28:21 -070082
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070083 return totalLength;
84}
85
86void
87Slicer::estimateOverhead()
88{
89 ndn::EncodingEstimator estimator;
90 size_t estimatedSize = Slicer_encodeFragment(estimator,
91 0, 0, 2, 0, m_mtu);
Junxiao Shidf3b4382014-02-23 11:28:21 -070092
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070093 size_t overhead = estimatedSize - m_mtu;
94 m_maxPayload = m_mtu - overhead;
95}
96
97PacketArray
98Slicer::slice(const Block& block)
99{
100 BOOST_ASSERT(block.hasWire());
101 const uint8_t* networkPacket = block.wire();
102 size_t networkPacketSize = block.size();
Junxiao Shidf3b4382014-02-23 11:28:21 -0700103
Junxiao Shi9b0d3e92014-02-15 12:27:12 -0700104 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 Shidf3b4382014-02-23 11:28:21 -0700111
Junxiao Shi9b0d3e92014-02-15 12:27:12 -0700112 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 Shidf3b4382014-02-23 11:28:21 -0700116
Junxiao Shi9b0d3e92014-02-15 12:27:12 -0700117 ndn::EncodingBuffer buffer(m_mtu, 0);
118 size_t pktSize = Slicer_encodeFragment(buffer,
119 seqBlock[fragIndex], fragIndex, fragCount, payload, payloadSize);
Junxiao Shidf3b4382014-02-23 11:28:21 -0700120
Junxiao Shi9b0d3e92014-02-15 12:27:12 -0700121 BOOST_ASSERT(pktSize <= m_mtu);
Junxiao Shidf3b4382014-02-23 11:28:21 -0700122
Junxiao Shi9b0d3e92014-02-15 12:27:12 -0700123 pa->push_back(buffer.block());
124 }
Junxiao Shidf3b4382014-02-23 11:28:21 -0700125
Junxiao Shi9b0d3e92014-02-15 12:27:12 -0700126 return pa;
127}
128
129} // namespace ndnlp
130} // namespace nfd