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