29 #include <ndn-cxx/encoding/tlv.hpp> 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");
62 , m_linkService(linkService)
78 std::tuple<bool, std::vector<lp::Packet>>
81 BOOST_ASSERT(packet.has<lp::FragmentField>());
82 BOOST_ASSERT(!packet.has<lp::FragIndexField>());
83 BOOST_ASSERT(!packet.has<lp::FragCountField>());
85 if (MAX_SINGLE_FRAG_OVERHEAD + packet.wireEncode().size() <= mtu) {
89 return std::make_tuple(
true, std::vector<lp::Packet>{packet});
92 ndn::Buffer::const_iterator netPktBegin, netPktEnd;
93 std::tie(netPktBegin, netPktEnd) = packet.get<lp::FragmentField>();
94 size_t netPktSize = std::distance(netPktBegin, netPktEnd);
97 size_t firstHeaderSize = 0;
98 const Block& packetWire = packet.wireEncode();
99 if (packetWire.type() == lp::tlv::LpPacket) {
100 for (
const Block& element : packetWire.elements()) {
101 if (element.type() != lp::tlv::Fragment) {
102 firstHeaderSize += element.size();
108 if (MAX_FRAG_OVERHEAD + firstHeaderSize + 1 > mtu) {
109 NFD_LOG_FACE_WARN(
"fragmentation error, MTU too small for first fragment: DROP");
110 return std::make_tuple(
false, std::vector<lp::Packet>{});
112 size_t firstPayloadSize = std::min(netPktSize, mtu - firstHeaderSize - MAX_FRAG_OVERHEAD);
114 size_t fragCount = 1 + ((netPktSize - firstPayloadSize) / payloadSize) +
115 ((netPktSize - firstPayloadSize) % payloadSize != 0);
120 return std::make_tuple(
false, std::vector<lp::Packet>{});
124 std::vector<lp::Packet> frags(fragCount);
125 frags.front() = packet;
126 size_t fragIndex = 0;
127 auto fragBegin = netPktBegin,
128 fragEnd = fragBegin + firstPayloadSize;
129 while (fragBegin < netPktEnd) {
130 lp::Packet& frag = frags[fragIndex];
131 frag.add<lp::FragIndexField>(fragIndex);
132 frag.add<lp::FragCountField>(fragCount);
133 frag.set<lp::FragmentField>({fragBegin, fragEnd});
134 BOOST_ASSERT(frag.wireEncode().size() <= mtu);
138 fragEnd = std::min(netPktEnd, fragBegin + payloadSize);
140 BOOST_ASSERT(fragIndex == fragCount);
142 return std::make_tuple(
true, frags);
146 operator<<(std::ostream& os, const FaceLogHelper<LpFragmenter>& flh)
148 if (flh.obj.getLinkService() ==
nullptr) {
149 os <<
"[id=0,local=unknown,remote=unknown] ";
152 os << FaceLogHelper<LinkService>(*flh.obj.getLinkService());
LpFragmenter(const Options &options, const LinkService *linkService=nullptr)
size_t nMaxFragments
maximum number of fragments in a packet
void setOptions(const Options &options)
set options for fragmenter
#define NFD_LOG_FACE_WARN(msg)
Log a message at WARN level.
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
std::tuple< bool, std::vector< lp::Packet > > fragmentPacket(const lp::Packet &packet, size_t mtu)
fragments a network-layer packet into link-layer packets
#define NFD_LOG_INIT(name)
Options that control the behavior of LpFragmenter.
static const size_t MAX_SINGLE_FRAG_OVERHEAD
maximum overhead on a single fragment, not counting other NDNLPv2 headers
static const size_t MAX_FRAG_OVERHEAD
maximum overhead of adding fragmentation to payload, not counting other NDNLPv2 headers ...
const LinkService * getLinkService() const