Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | b84bd3a | 2016-04-22 02:21:45 +0200 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 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. |
| 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_GENERIC_LINK_SERVICE_HPP |
| 27 | #define NFD_DAEMON_FACE_GENERIC_LINK_SERVICE_HPP |
| 28 | |
Junxiao Shi | 9f5b01d | 2016-08-05 03:54:28 +0000 | [diff] [blame] | 29 | #include "core/common.hpp" |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 30 | #include "core/logger.hpp" |
| 31 | |
| 32 | #include "link-service.hpp" |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 33 | #include "lp-fragmenter.hpp" |
| 34 | #include "lp-reassembler.hpp" |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 35 | |
| 36 | namespace nfd { |
| 37 | namespace face { |
| 38 | |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 39 | /** \brief counters provided by GenericLinkService |
| 40 | * \note The type name 'GenericLinkServiceCounters' is implementation detail. |
| 41 | * Use 'GenericLinkService::Counters' in public API. |
| 42 | */ |
| 43 | class GenericLinkServiceCounters : public virtual LinkService::Counters |
| 44 | { |
| 45 | public: |
| 46 | explicit |
| 47 | GenericLinkServiceCounters(const LpReassembler& reassembler); |
| 48 | |
| 49 | /** \brief count of failed fragmentations |
| 50 | */ |
| 51 | PacketCounter nFragmentationErrors; |
| 52 | |
| 53 | /** \brief count of outgoing LpPackets dropped due to exceeding MTU limit |
| 54 | * |
| 55 | * If this counter is non-zero, the operator should enable fragmentation. |
| 56 | */ |
| 57 | PacketCounter nOutOverMtu; |
| 58 | |
| 59 | /** \brief count of invalid LpPackets dropped before reassembly |
| 60 | */ |
| 61 | PacketCounter nInLpInvalid; |
| 62 | |
| 63 | /** \brief count of network-layer packets currently being reassembled |
| 64 | */ |
| 65 | SizeCounter<LpReassembler> nReassembling; |
| 66 | |
| 67 | /** \brief count of dropped partial network-layer packets due to reassembly timeout |
| 68 | */ |
| 69 | PacketCounter nReassemblyTimeouts; |
| 70 | |
| 71 | /** \brief count of invalid reassembled network-layer packets dropped |
| 72 | */ |
| 73 | PacketCounter nInNetInvalid; |
| 74 | }; |
| 75 | |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 76 | /** \brief GenericLinkService is a LinkService that implements the NDNLPv2 protocol |
| 77 | * \sa http://redmine.named-data.net/projects/nfd/wiki/NDNLPv2 |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 78 | */ |
| 79 | class GenericLinkService : public LinkService |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 80 | , protected virtual GenericLinkServiceCounters |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 81 | { |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 82 | public: |
| 83 | /** \brief Options that control the behavior of GenericLinkService |
| 84 | */ |
| 85 | class Options |
| 86 | { |
| 87 | public: |
| 88 | Options(); |
| 89 | |
| 90 | public: |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 91 | /** \brief enables encoding of IncomingFaceId, and decoding of NextHopFaceId and CachePolicy |
| 92 | */ |
| 93 | bool allowLocalFields; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 94 | |
| 95 | /** \brief enables fragmentation |
| 96 | */ |
| 97 | bool allowFragmentation; |
| 98 | |
| 99 | /** \brief options for fragmentation |
| 100 | */ |
| 101 | LpFragmenter::Options fragmenterOptions; |
| 102 | |
| 103 | /** \brief enables reassembly |
| 104 | */ |
| 105 | bool allowReassembly; |
| 106 | |
| 107 | /** \brief options for reassembly |
| 108 | */ |
| 109 | LpReassembler::Options reassemblerOptions; |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 110 | }; |
| 111 | |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 112 | /** \brief counters provided by GenericLinkService |
| 113 | */ |
| 114 | typedef GenericLinkServiceCounters Counters; |
| 115 | |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 116 | explicit |
| 117 | GenericLinkService(const Options& options = Options()); |
| 118 | |
| 119 | /** \brief get Options used by GenericLinkService |
| 120 | */ |
| 121 | const Options& |
| 122 | getOptions() const; |
| 123 | |
| 124 | /** \brief sets Options used by GenericLinkService |
| 125 | */ |
| 126 | void |
| 127 | setOptions(const Options& options); |
| 128 | |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 129 | virtual const Counters& |
Davide Pesavento | b84bd3a | 2016-04-22 02:21:45 +0200 | [diff] [blame] | 130 | getCounters() const override; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 131 | |
| 132 | private: // send path |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 133 | /** \brief send Interest |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 134 | */ |
| 135 | void |
Davide Pesavento | b84bd3a | 2016-04-22 02:21:45 +0200 | [diff] [blame] | 136 | doSendInterest(const Interest& interest) override; |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 137 | |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 138 | /** \brief send Data |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 139 | */ |
| 140 | void |
Davide Pesavento | b84bd3a | 2016-04-22 02:21:45 +0200 | [diff] [blame] | 141 | doSendData(const Data& data) override; |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 142 | |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 143 | /** \brief send Nack |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 144 | */ |
| 145 | void |
Davide Pesavento | b84bd3a | 2016-04-22 02:21:45 +0200 | [diff] [blame] | 146 | doSendNack(const ndn::lp::Nack& nack) override; |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 147 | |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 148 | /** \brief encode local fields from tags onto outgoing LpPacket |
| 149 | * \param pkt LpPacket containing a complete network layer packet |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 150 | */ |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 151 | static void |
| 152 | encodeLocalFields(const ndn::TagHost& netPkt, lp::Packet& lpPacket); |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 153 | |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 154 | /** \brief send a complete network layer packet |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 155 | * \param pkt LpPacket containing a complete network layer packet |
| 156 | */ |
| 157 | void |
| 158 | sendNetPacket(lp::Packet&& pkt); |
| 159 | |
| 160 | /** \brief assign a sequence number to an LpPacket |
| 161 | */ |
| 162 | void |
| 163 | assignSequence(lp::Packet& pkt); |
| 164 | |
| 165 | /** \brief assign consecutive sequence numbers to LpPackets |
| 166 | */ |
| 167 | void |
| 168 | assignSequences(std::vector<lp::Packet>& pkts); |
| 169 | |
| 170 | private: // receive path |
| 171 | /** \brief receive Packet from Transport |
| 172 | */ |
| 173 | void |
Davide Pesavento | b84bd3a | 2016-04-22 02:21:45 +0200 | [diff] [blame] | 174 | doReceivePacket(Transport::Packet&& packet) override; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 175 | |
| 176 | /** \brief decode incoming network-layer packet |
| 177 | * \param netPkt reassembled network-layer packet |
| 178 | * \param firstPkt LpPacket of first fragment |
| 179 | * |
| 180 | * If decoding is successful, a receive signal is emitted; |
| 181 | * otherwise, a warning is logged. |
| 182 | */ |
| 183 | void |
| 184 | decodeNetPacket(const Block& netPkt, const lp::Packet& firstPkt); |
| 185 | |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 186 | /** \brief decode incoming Interest |
| 187 | * \param netPkt reassembled network-layer packet; TLV-TYPE must be Interest |
| 188 | * \param firstPkt LpPacket of first fragment; must not have Nack field |
| 189 | * |
| 190 | * If decoding is successful, receiveInterest signal is emitted; |
| 191 | * otherwise, a warning is logged. |
| 192 | * |
| 193 | * \throw tlv::Error parse error in an LpHeader field |
| 194 | */ |
| 195 | void |
| 196 | decodeInterest(const Block& netPkt, const lp::Packet& firstPkt); |
| 197 | |
| 198 | /** \brief decode incoming Interest |
| 199 | * \param netPkt reassembled network-layer packet; TLV-TYPE must be Data |
| 200 | * \param firstPkt LpPacket of first fragment |
| 201 | * |
| 202 | * If decoding is successful, receiveData signal is emitted; |
| 203 | * otherwise, a warning is logged. |
| 204 | * |
| 205 | * \throw tlv::Error parse error in an LpHeader field |
| 206 | */ |
| 207 | void |
| 208 | decodeData(const Block& netPkt, const lp::Packet& firstPkt); |
| 209 | |
| 210 | /** \brief decode incoming Interest |
| 211 | * \param netPkt reassembled network-layer packet; TLV-TYPE must be Interest |
| 212 | * \param firstPkt LpPacket of first fragment; must have Nack field |
| 213 | * |
| 214 | * If decoding is successful, receiveNack signal is emitted; |
| 215 | * otherwise, a warning is logged. |
| 216 | * |
| 217 | * \throw tlv::Error parse error in an LpHeader field |
| 218 | */ |
| 219 | void |
| 220 | decodeNack(const Block& netPkt, const lp::Packet& firstPkt); |
| 221 | |
| 222 | private: |
| 223 | Options m_options; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 224 | LpFragmenter m_fragmenter; |
| 225 | LpReassembler m_reassembler; |
| 226 | lp::Sequence m_lastSeqNo; |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 227 | }; |
| 228 | |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 229 | inline const GenericLinkService::Options& |
| 230 | GenericLinkService::getOptions() const |
| 231 | { |
| 232 | return m_options; |
| 233 | } |
| 234 | |
| 235 | inline void |
| 236 | GenericLinkService::setOptions(const GenericLinkService::Options& options) |
| 237 | { |
| 238 | m_options = options; |
| 239 | } |
| 240 | |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 241 | inline const GenericLinkService::Counters& |
| 242 | GenericLinkService::getCounters() const |
| 243 | { |
| 244 | return *this; |
| 245 | } |
| 246 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 247 | } // namespace face |
| 248 | } // namespace nfd |
| 249 | |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 250 | #endif // NFD_DAEMON_FACE_GENERIC_LINK_SERVICE_HPP |