Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | cbc8e94 | 2016-09-06 03:17:45 +0000 | [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 | #include "generic-link-service.hpp" |
Junxiao Shi | cbc8e94 | 2016-09-06 03:17:45 +0000 | [diff] [blame^] | 27 | #include <ndn-cxx/lp/tags.hpp> |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 28 | |
| 29 | namespace nfd { |
| 30 | namespace face { |
| 31 | |
| 32 | NFD_LOG_INIT("GenericLinkService"); |
| 33 | |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 34 | GenericLinkServiceCounters::GenericLinkServiceCounters(const LpReassembler& reassembler) |
| 35 | : nReassembling(reassembler) |
| 36 | { |
| 37 | } |
| 38 | |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 39 | GenericLinkService::Options::Options() |
| 40 | : allowLocalFields(false) |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 41 | , allowFragmentation(false) |
| 42 | , allowReassembly(false) |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 43 | { |
| 44 | } |
| 45 | |
| 46 | GenericLinkService::GenericLinkService(const GenericLinkService::Options& options) |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 47 | : GenericLinkServiceCounters(m_reassembler) |
| 48 | , m_options(options) |
| 49 | , m_fragmenter(m_options.fragmenterOptions, this) |
| 50 | , m_reassembler(m_options.reassemblerOptions, this) |
| 51 | , m_lastSeqNo(-2) |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 52 | { |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 53 | m_reassembler.beforeTimeout.connect(bind([this] { ++this->nReassemblyTimeouts; })); |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 56 | void |
| 57 | GenericLinkService::doSendInterest(const Interest& interest) |
| 58 | { |
| 59 | lp::Packet lpPacket(interest.wireEncode()); |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 60 | |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 61 | if (m_options.allowLocalFields) { |
| 62 | encodeLocalFields(interest, lpPacket); |
| 63 | } |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 64 | |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 65 | this->sendNetPacket(std::move(lpPacket)); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void |
| 69 | GenericLinkService::doSendData(const Data& data) |
| 70 | { |
| 71 | lp::Packet lpPacket(data.wireEncode()); |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 72 | |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 73 | if (m_options.allowLocalFields) { |
| 74 | encodeLocalFields(data, lpPacket); |
| 75 | } |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 76 | |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 77 | this->sendNetPacket(std::move(lpPacket)); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void |
| 81 | GenericLinkService::doSendNack(const lp::Nack& nack) |
| 82 | { |
| 83 | lp::Packet lpPacket(nack.getInterest().wireEncode()); |
| 84 | lpPacket.add<lp::NackField>(nack.getHeader()); |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 85 | |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 86 | if (m_options.allowLocalFields) { |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 87 | encodeLocalFields(nack, lpPacket); |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 88 | } |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 89 | |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 90 | this->sendNetPacket(std::move(lpPacket)); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 93 | void |
| 94 | GenericLinkService::encodeLocalFields(const ndn::TagHost& netPkt, lp::Packet& lpPacket) |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 95 | { |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 96 | shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = netPkt.getTag<lp::IncomingFaceIdTag>(); |
| 97 | if (incomingFaceIdTag != nullptr) { |
| 98 | lpPacket.add<lp::IncomingFaceIdField>(*incomingFaceIdTag); |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 99 | } |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 102 | void |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 103 | GenericLinkService::sendNetPacket(lp::Packet&& pkt) |
| 104 | { |
| 105 | std::vector<lp::Packet> frags; |
| 106 | const ssize_t mtu = this->getTransport()->getMtu(); |
| 107 | if (m_options.allowFragmentation && mtu != MTU_UNLIMITED) { |
| 108 | bool isOk = false; |
| 109 | std::tie(isOk, frags) = m_fragmenter.fragmentPacket(pkt, mtu); |
| 110 | if (!isOk) { |
| 111 | // fragmentation failed (warning is logged by LpFragmenter) |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 112 | ++this->nFragmentationErrors; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 113 | return; |
| 114 | } |
| 115 | } |
| 116 | else { |
| 117 | frags.push_back(pkt); |
| 118 | } |
| 119 | |
| 120 | if (frags.size() > 1) { |
| 121 | // sequence is needed only if packet is fragmented |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 122 | this->assignSequences(frags); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 123 | } |
| 124 | else { |
| 125 | // even if indexed fragmentation is enabled, the fragmenter should not |
| 126 | // fragment the packet if it can fit in MTU |
| 127 | BOOST_ASSERT(frags.size() > 0); |
| 128 | BOOST_ASSERT(!frags.front().has<lp::FragIndexField>()); |
| 129 | BOOST_ASSERT(!frags.front().has<lp::FragCountField>()); |
| 130 | } |
| 131 | |
| 132 | for (const lp::Packet& frag : frags) { |
| 133 | Transport::Packet tp(frag.wireEncode()); |
| 134 | if (mtu != MTU_UNLIMITED && tp.packet.size() > static_cast<size_t>(mtu)) { |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 135 | ++this->nOutOverMtu; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 136 | NFD_LOG_FACE_WARN("attempt to send packet over MTU limit"); |
| 137 | continue; |
| 138 | } |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 139 | this->sendPacket(std::move(tp)); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | void |
| 144 | GenericLinkService::assignSequence(lp::Packet& pkt) |
| 145 | { |
| 146 | pkt.set<lp::SequenceField>(++m_lastSeqNo); |
| 147 | } |
| 148 | |
| 149 | void |
| 150 | GenericLinkService::assignSequences(std::vector<lp::Packet>& pkts) |
| 151 | { |
| 152 | std::for_each(pkts.begin(), pkts.end(), bind(&GenericLinkService::assignSequence, this, _1)); |
| 153 | } |
| 154 | |
| 155 | void |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 156 | GenericLinkService::doReceivePacket(Transport::Packet&& packet) |
| 157 | { |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 158 | try { |
Eric Newberry | a1939ba | 2015-10-09 12:35:03 -0700 | [diff] [blame] | 159 | lp::Packet pkt(packet.packet); |
| 160 | |
| 161 | if (!pkt.has<lp::FragmentField>()) { |
| 162 | NFD_LOG_FACE_TRACE("received IDLE packet: DROP"); |
| 163 | return; |
| 164 | } |
| 165 | |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 166 | if ((pkt.has<lp::FragIndexField>() || pkt.has<lp::FragCountField>()) && |
| 167 | !m_options.allowReassembly) { |
| 168 | NFD_LOG_FACE_WARN("received fragment, but reassembly disabled: DROP"); |
Eric Newberry | a1939ba | 2015-10-09 12:35:03 -0700 | [diff] [blame] | 169 | return; |
| 170 | } |
| 171 | |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 172 | bool isReassembled = false; |
| 173 | Block netPkt; |
| 174 | lp::Packet firstPkt; |
| 175 | std::tie(isReassembled, netPkt, firstPkt) = m_reassembler.receiveFragment(packet.remoteEndpoint, |
| 176 | pkt); |
| 177 | if (isReassembled) { |
| 178 | this->decodeNetPacket(netPkt, firstPkt); |
| 179 | } |
| 180 | } |
| 181 | catch (const tlv::Error& e) { |
| 182 | ++this->nInLpInvalid; |
| 183 | NFD_LOG_FACE_WARN("packet parse error (" << e.what() << "): DROP"); |
| 184 | } |
| 185 | } |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 186 | |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 187 | void |
| 188 | GenericLinkService::decodeNetPacket(const Block& netPkt, const lp::Packet& firstPkt) |
| 189 | { |
| 190 | try { |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 191 | switch (netPkt.type()) { |
| 192 | case tlv::Interest: |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 193 | if (firstPkt.has<lp::NackField>()) { |
| 194 | this->decodeNack(netPkt, firstPkt); |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 195 | } |
| 196 | else { |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 197 | this->decodeInterest(netPkt, firstPkt); |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 198 | } |
| 199 | break; |
| 200 | case tlv::Data: |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 201 | this->decodeData(netPkt, firstPkt); |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 202 | break; |
| 203 | default: |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 204 | ++this->nInNetInvalid; |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 205 | NFD_LOG_FACE_WARN("unrecognized network-layer packet TLV-TYPE " << netPkt.type() << ": DROP"); |
| 206 | return; |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 207 | } |
| 208 | } |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 209 | catch (const tlv::Error& e) { |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 210 | ++this->nInNetInvalid; |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 211 | NFD_LOG_FACE_WARN("packet parse error (" << e.what() << "): DROP"); |
| 212 | } |
| 213 | } |
| 214 | |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 215 | void |
| 216 | GenericLinkService::decodeInterest(const Block& netPkt, const lp::Packet& firstPkt) |
| 217 | { |
| 218 | BOOST_ASSERT(netPkt.type() == tlv::Interest); |
| 219 | BOOST_ASSERT(!firstPkt.has<lp::NackField>()); |
| 220 | |
| 221 | // forwarding expects Interest to be created with make_shared |
| 222 | auto interest = make_shared<Interest>(netPkt); |
| 223 | |
| 224 | if (firstPkt.has<lp::NextHopFaceIdField>()) { |
| 225 | if (m_options.allowLocalFields) { |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 226 | interest->setTag(make_shared<lp::NextHopFaceIdTag>(firstPkt.get<lp::NextHopFaceIdField>())); |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 227 | } |
| 228 | else { |
| 229 | NFD_LOG_FACE_WARN("received NextHopFaceId, but local fields disabled: DROP"); |
| 230 | return; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | if (firstPkt.has<lp::CachePolicyField>()) { |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 235 | ++this->nInNetInvalid; |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 236 | NFD_LOG_FACE_WARN("received CachePolicy with Interest: DROP"); |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | if (firstPkt.has<lp::IncomingFaceIdField>()) { |
| 241 | NFD_LOG_FACE_WARN("received IncomingFaceId: IGNORE"); |
| 242 | } |
| 243 | |
| 244 | this->receiveInterest(*interest); |
| 245 | } |
| 246 | |
| 247 | void |
| 248 | GenericLinkService::decodeData(const Block& netPkt, const lp::Packet& firstPkt) |
| 249 | { |
| 250 | BOOST_ASSERT(netPkt.type() == tlv::Data); |
| 251 | |
| 252 | // forwarding expects Data to be created with make_shared |
| 253 | auto data = make_shared<Data>(netPkt); |
| 254 | |
| 255 | if (firstPkt.has<lp::NackField>()) { |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 256 | ++this->nInNetInvalid; |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 257 | NFD_LOG_FACE_WARN("received Nack with Data: DROP"); |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | if (firstPkt.has<lp::NextHopFaceIdField>()) { |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 262 | ++this->nInNetInvalid; |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 263 | NFD_LOG_FACE_WARN("received NextHopFaceId with Data: DROP"); |
| 264 | return; |
| 265 | } |
| 266 | |
| 267 | if (firstPkt.has<lp::CachePolicyField>()) { |
| 268 | if (m_options.allowLocalFields) { |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 269 | // In case of an invalid CachePolicyType, get<lp::CachePolicyField> will throw, |
| 270 | // so it's unnecessary to check here. |
| 271 | data->setTag(make_shared<lp::CachePolicyTag>(firstPkt.get<lp::CachePolicyField>())); |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 272 | } |
| 273 | else { |
| 274 | NFD_LOG_FACE_WARN("received CachePolicy, but local fields disabled: IGNORE"); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | if (firstPkt.has<lp::IncomingFaceIdField>()) { |
| 279 | NFD_LOG_FACE_WARN("received IncomingFaceId: IGNORE"); |
| 280 | } |
| 281 | |
| 282 | this->receiveData(*data); |
| 283 | } |
| 284 | |
| 285 | void |
| 286 | GenericLinkService::decodeNack(const Block& netPkt, const lp::Packet& firstPkt) |
| 287 | { |
| 288 | BOOST_ASSERT(netPkt.type() == tlv::Interest); |
| 289 | BOOST_ASSERT(firstPkt.has<lp::NackField>()); |
| 290 | |
| 291 | lp::Nack nack((Interest(netPkt))); |
| 292 | nack.setHeader(firstPkt.get<lp::NackField>()); |
| 293 | |
| 294 | if (firstPkt.has<lp::NextHopFaceIdField>()) { |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 295 | ++this->nInNetInvalid; |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 296 | NFD_LOG_FACE_WARN("received NextHopFaceId with Nack: DROP"); |
| 297 | return; |
| 298 | } |
| 299 | |
| 300 | if (firstPkt.has<lp::CachePolicyField>()) { |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 301 | ++this->nInNetInvalid; |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 302 | NFD_LOG_FACE_WARN("received CachePolicy with Nack: DROP"); |
| 303 | return; |
| 304 | } |
| 305 | |
| 306 | if (firstPkt.has<lp::IncomingFaceIdField>()) { |
| 307 | NFD_LOG_FACE_WARN("received IncomingFaceId: IGNORE"); |
| 308 | } |
| 309 | |
| 310 | this->receiveNack(nack); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | } // namespace face |
| 314 | } // namespace nfd |