blob: 9f3a0eaeaa34cdb49eacf1e1a147b26f238a1906 [file] [log] [blame]
Eric Newberry4c3e6b82015-11-10 16:48:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoe4b22382018-06-10 14:37:24 -04002/*
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Eric Newberry4c3e6b82015-11-10 16:48:42 -07004 * 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#ifndef NFD_DAEMON_FACE_LP_REASSEMBLER_HPP
27#define NFD_DAEMON_FACE_LP_REASSEMBLER_HPP
28
Davide Pesaventocb425e82019-07-14 21:48:22 -040029#include "face-common.hpp"
Eric Newberry4c3e6b82015-11-10 16:48:42 -070030
31#include <ndn-cxx/lp/packet.hpp>
Davide Pesavento9a63bf22023-11-11 17:12:51 -050032#include <ndn-cxx/lp/sequence.hpp>
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -050033#include <ndn-cxx/util/scheduler.hpp>
34
35#include <map>
Eric Newberry4c3e6b82015-11-10 16:48:42 -070036
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040037namespace nfd::face {
Eric Newberry4c3e6b82015-11-10 16:48:42 -070038
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040039/**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040040 * \brief Reassembles fragmented network-layer packets.
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040041 * \sa https://redmine.named-data.net/projects/nfd/wiki/NDNLPv2
Eric Newberry4c3e6b82015-11-10 16:48:42 -070042 */
43class LpReassembler : noncopyable
44{
45public:
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050046 /**
47 * \brief %Options that control the behavior of LpReassembler.
Eric Newberry4c3e6b82015-11-10 16:48:42 -070048 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -040049 struct Options
Eric Newberry4c3e6b82015-11-10 16:48:42 -070050 {
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050051 /**
52 * \brief Maximum number of fragments in a packet.
Eric Newberry4c3e6b82015-11-10 16:48:42 -070053 *
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050054 * LpPackets with FragCount over this limit are dropped.
Eric Newberry4c3e6b82015-11-10 16:48:42 -070055 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -040056 size_t nMaxFragments = 400;
Eric Newberry4c3e6b82015-11-10 16:48:42 -070057
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050058 /**
59 * \brief Timeout before a partially reassembled packet is dropped.
Eric Newberry4c3e6b82015-11-10 16:48:42 -070060 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -040061 time::nanoseconds reassemblyTimeout = 500_ms;
Eric Newberry4c3e6b82015-11-10 16:48:42 -070062 };
63
64 explicit
Davide Pesaventoe4b22382018-06-10 14:37:24 -040065 LpReassembler(const Options& options, const LinkService* linkService = nullptr);
Eric Newberry4c3e6b82015-11-10 16:48:42 -070066
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050067 /**
68 * \brief Set options for reassembler.
Eric Newberry4c3e6b82015-11-10 16:48:42 -070069 */
70 void
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050071 setOptions(const Options& options)
72 {
73 m_options = options;
74 }
Eric Newberry4c3e6b82015-11-10 16:48:42 -070075
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050076 /**
77 * \brief Returns the LinkService that owns this instance.
Eric Newberry4c3e6b82015-11-10 16:48:42 -070078 *
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050079 * This is only used for logging, and may be nullptr.
Eric Newberry4c3e6b82015-11-10 16:48:42 -070080 */
81 const LinkService*
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050082 getLinkService() const noexcept
83 {
84 return m_linkService;
85 }
Eric Newberry4c3e6b82015-11-10 16:48:42 -070086
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050087 /**
88 * \brief Adds received fragment to the buffer.
89 * \param remoteEndpoint endpoint that sent the packet
90 * \param packet received fragment; must have Fragment field
91 * \return a tuple containing:
92 * whether a network-layer packet has been completely received,
93 * the reassembled network-layer packet,
94 * the first fragment for inspecting other NDNLPv2 headers
95 * \throw tlv::Error packet is malformed
Eric Newberry4c3e6b82015-11-10 16:48:42 -070096 */
97 std::tuple<bool, Block, lp::Packet>
Teng Liangd94b7b32022-07-10 21:29:37 +080098 receiveFragment(const EndpointId& remoteEndpoint, const lp::Packet& packet);
Eric Newberry4c3e6b82015-11-10 16:48:42 -070099
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500100 /**
101 * \brief Count of partial packets.
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700102 */
103 size_t
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500104 size() const noexcept
105 {
106 return m_partialPackets.size();
107 }
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700108
Teng Liangd94b7b32022-07-10 21:29:37 +0800109 /**
110 * \brief Notifies before a partial packet is dropped due to timeout.
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700111 *
Teng Liangd94b7b32022-07-10 21:29:37 +0800112 * If a partial packet is incomplete and no new fragments are received within
113 * Options::reassemblyTimeout, the partial packet is dropped due to timeout.
114 * Before dropping the packet, this signal is emitted with the remote endpoint
115 * and the number of fragments being dropped.
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700116 */
ashiqopu77d0bfd2019-02-20 20:37:31 +0000117 signal::Signal<LpReassembler, EndpointId, size_t> beforeTimeout;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700118
119private:
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400120 /**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400121 * \brief Holds all fragments of a packet until reassembled.
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700122 */
123 struct PartialPacket
124 {
125 std::vector<lp::Packet> fragments;
126 size_t fragCount; ///< total fragments
127 size_t nReceivedFragments; ///< number of received fragments
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -0500128 ndn::scheduler::ScopedEventId dropTimer;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700129 };
130
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400131 /**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400132 * \brief Index key for PartialPackets.
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700133 */
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400134 using Key = std::tuple<
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400135 EndpointId, // remote endpoint
136 lp::Sequence // message identifier (sequence number of the first fragment)
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400137 >;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700138
139 Block
140 doReassembly(const Key& key);
141
142 void
143 timeoutPartialPacket(const Key& key);
144
145private:
146 Options m_options;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700147 const LinkService* m_linkService;
Davide Pesavento3dade002019-03-19 11:29:56 -0600148 std::map<Key, PartialPacket> m_partialPackets;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700149};
150
151std::ostream&
152operator<<(std::ostream& os, const FaceLogHelper<LpReassembler>& flh);
153
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400154} // namespace nfd::face
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700155
156#endif // NFD_DAEMON_FACE_LP_REASSEMBLER_HPP