blob: 5f3f29508f1fa80b33af209fc55fddc1ef67a324 [file] [log] [blame]
Davide Pesavento43ff2a92017-05-18 19:50:57 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi0d82d042017-07-07 06:15:27 +00002/*
Davide Pesaventoa599d2a2022-02-16 18:52:43 -05003 * Copyright (c) 2014-2022, Regents of the University of California,
Davide Pesavento43ff2a92017-05-18 19:50:57 -04004 * 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 "ethernet-channel.hpp"
27#include "ethernet-protocol.hpp"
Davide Pesaventocb425e82019-07-14 21:48:22 -040028#include "face.hpp"
Davide Pesavento43ff2a92017-05-18 19:50:57 -040029#include "generic-link-service.hpp"
30#include "unicast-ethernet-transport.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040031#include "common/global.hpp"
Davide Pesavento43ff2a92017-05-18 19:50:57 -040032
33#include <boost/range/adaptor/map.hpp>
34#include <pcap/pcap.h>
35
36namespace nfd {
37namespace face {
38
Davide Pesaventoa3148082018-04-12 18:21:54 -040039NFD_LOG_INIT(EthernetChannel);
Davide Pesavento43ff2a92017-05-18 19:50:57 -040040
Junxiao Shi0d82d042017-07-07 06:15:27 +000041EthernetChannel::EthernetChannel(shared_ptr<const ndn::net::NetworkInterface> localEndpoint,
Davide Pesavento43ff2a92017-05-18 19:50:57 -040042 time::nanoseconds idleTimeout)
Junxiao Shi0d82d042017-07-07 06:15:27 +000043 : m_localEndpoint(std::move(localEndpoint))
Davide Pesavento43ff2a92017-05-18 19:50:57 -040044 , m_isListening(false)
45 , m_socket(getGlobalIoService())
Junxiao Shi0d82d042017-07-07 06:15:27 +000046 , m_pcap(m_localEndpoint->getName())
Davide Pesavento43ff2a92017-05-18 19:50:57 -040047 , m_idleFaceTimeout(idleTimeout)
48#ifdef _DEBUG
49 , m_nDropped(0)
50#endif
51{
Junxiao Shi0d82d042017-07-07 06:15:27 +000052 setUri(FaceUri::fromDev(m_localEndpoint->getName()));
Davide Pesavento43ff2a92017-05-18 19:50:57 -040053 NFD_LOG_CHAN_INFO("Creating channel");
54}
55
56void
57EthernetChannel::connect(const ethernet::Address& remoteEndpoint,
Davide Pesavento15b55052018-01-27 19:09:28 -050058 const FaceParams& params,
Davide Pesavento43ff2a92017-05-18 19:50:57 -040059 const FaceCreatedCallback& onFaceCreated,
60 const FaceCreationFailedCallback& onConnectFailed)
61{
62 shared_ptr<Face> face;
63 try {
Davide Pesavento15b55052018-01-27 19:09:28 -050064 face = createFace(remoteEndpoint, params).second;
Davide Pesavento43ff2a92017-05-18 19:50:57 -040065 }
66 catch (const boost::system::system_error& e) {
67 NFD_LOG_CHAN_DEBUG("Face creation for " << remoteEndpoint << " failed: " << e.what());
68 if (onConnectFailed)
Davide Pesaventoe4b22382018-06-10 14:37:24 -040069 onConnectFailed(504, "Face creation failed: "s + e.what());
Davide Pesavento43ff2a92017-05-18 19:50:57 -040070 return;
71 }
72
73 // Need to invoke the callback regardless of whether or not we had already
74 // created the face so that control responses and such can be sent
75 onFaceCreated(face);
76}
77
78void
79EthernetChannel::listen(const FaceCreatedCallback& onFaceCreated,
80 const FaceCreationFailedCallback& onFaceCreationFailed)
81{
82 if (isListening()) {
83 NFD_LOG_CHAN_WARN("Already listening");
84 return;
85 }
86 m_isListening = true;
87
88 try {
89 m_pcap.activate(DLT_EN10MB);
90 m_socket.assign(m_pcap.getFd());
91 }
92 catch (const PcapHelper::Error& e) {
Davide Pesavento19779d82019-02-14 13:40:04 -050093 NDN_THROW_NESTED(Error(e.what()));
Davide Pesavento43ff2a92017-05-18 19:50:57 -040094 }
95 updateFilter();
96
97 asyncRead(onFaceCreated, onFaceCreationFailed);
98 NFD_LOG_CHAN_DEBUG("Started listening");
99}
100
101void
102EthernetChannel::asyncRead(const FaceCreatedCallback& onFaceCreated,
103 const FaceCreationFailedCallback& onReceiveFailed)
104{
105 m_socket.async_read_some(boost::asio::null_buffers(),
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400106 [=] (const auto& e, auto) { this->handleRead(e, onFaceCreated, onReceiveFailed); });
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400107}
108
109void
110EthernetChannel::handleRead(const boost::system::error_code& error,
111 const FaceCreatedCallback& onFaceCreated,
112 const FaceCreationFailedCallback& onReceiveFailed)
113{
114 if (error) {
115 if (error != boost::asio::error::operation_aborted) {
116 NFD_LOG_CHAN_DEBUG("Receive failed: " << error.message());
117 if (onReceiveFailed)
118 onReceiveFailed(500, "Receive failed: " + error.message());
119 }
120 return;
121 }
122
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500123 span<const uint8_t> pkt;
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400124 std::string err;
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500125 std::tie(pkt, err) = m_pcap.readNextPacket();
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400126
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500127 if (pkt.empty()) {
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400128 NFD_LOG_CHAN_WARN("Read error: " << err);
129 }
130 else {
131 const ether_header* eh;
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500132 std::tie(eh, err) = ethernet::checkFrameHeader(pkt, m_localEndpoint->getEthernetAddress(),
Junxiao Shi0d82d042017-07-07 06:15:27 +0000133 m_localEndpoint->getEthernetAddress());
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400134 if (eh == nullptr) {
135 NFD_LOG_CHAN_DEBUG(err);
136 }
137 else {
138 ethernet::Address sender(eh->ether_shost);
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500139 pkt = pkt.subspan(ethernet::HDR_LEN);
140 processIncomingPacket(pkt, sender, onFaceCreated, onReceiveFailed);
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400141 }
142 }
143
144#ifdef _DEBUG
145 size_t nDropped = m_pcap.getNDropped();
146 if (nDropped - m_nDropped > 0)
147 NFD_LOG_CHAN_DEBUG("Detected " << nDropped - m_nDropped << " dropped frame(s)");
148 m_nDropped = nDropped;
149#endif
150
151 asyncRead(onFaceCreated, onReceiveFailed);
152}
153
154void
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500155EthernetChannel::processIncomingPacket(span<const uint8_t> packet,
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400156 const ethernet::Address& sender,
157 const FaceCreatedCallback& onFaceCreated,
158 const FaceCreationFailedCallback& onReceiveFailed)
159{
160 NFD_LOG_CHAN_TRACE("New peer " << sender);
161
162 bool isCreated = false;
163 shared_ptr<Face> face;
164 try {
Davide Pesavento15b55052018-01-27 19:09:28 -0500165 FaceParams params;
166 params.persistency = ndn::nfd::FACE_PERSISTENCY_ON_DEMAND;
167 std::tie(isCreated, face) = createFace(sender, params);
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400168 }
169 catch (const EthernetTransport::Error& e) {
170 NFD_LOG_CHAN_DEBUG("Face creation for " << sender << " failed: " << e.what());
171 if (onReceiveFailed)
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400172 onReceiveFailed(504, "Face creation failed: "s + e.what());
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400173 return;
174 }
175
176 if (isCreated)
177 onFaceCreated(face);
178 else
179 NFD_LOG_CHAN_DEBUG("Received frame for existing face");
180
181 // dispatch the packet to the face for processing
182 auto* transport = static_cast<UnicastEthernetTransport*>(face->getTransport());
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500183 transport->receivePayload(packet, sender);
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400184}
185
186std::pair<bool, shared_ptr<Face>>
187EthernetChannel::createFace(const ethernet::Address& remoteEndpoint,
Davide Pesavento15b55052018-01-27 19:09:28 -0500188 const FaceParams& params)
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400189{
190 auto it = m_channelFaces.find(remoteEndpoint);
191 if (it != m_channelFaces.end()) {
192 // we already have a face for this endpoint, so reuse it
193 NFD_LOG_CHAN_TRACE("Reusing existing face for " << remoteEndpoint);
194 return {false, it->second};
195 }
196
197 // else, create a new face
Eric Newberry2642cd22017-07-13 21:34:53 -0400198 GenericLinkService::Options options;
Davide Pesavento00a3c9d2017-08-10 22:40:16 -0400199 options.allowFragmentation = true;
200 options.allowReassembly = true;
Davide Pesavento15b55052018-01-27 19:09:28 -0500201 options.reliabilityOptions.isEnabled = params.wantLpReliability;
Eric Newberrycb6551e2020-03-02 14:12:16 -0800202 if (params.mtu) {
203 options.overrideMtu = *params.mtu;
204 }
Davide Pesavento00a3c9d2017-08-10 22:40:16 -0400205
Eric Newberry2642cd22017-07-13 21:34:53 -0400206 auto linkService = make_unique<GenericLinkService>(options);
Junxiao Shi0d82d042017-07-07 06:15:27 +0000207 auto transport = make_unique<UnicastEthernetTransport>(*m_localEndpoint, remoteEndpoint,
Eric Newberrycb6551e2020-03-02 14:12:16 -0800208 params.persistency, m_idleFaceTimeout);
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400209 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -0400210 face->setChannel(shared_from_this()); // use weak_from_this() in C++17
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400211
212 m_channelFaces[remoteEndpoint] = face;
213 connectFaceClosedSignal(*face, [this, remoteEndpoint] {
214 m_channelFaces.erase(remoteEndpoint);
215 updateFilter();
216 });
217 updateFilter();
218
219 return {true, face};
220}
221
222void
223EthernetChannel::updateFilter()
224{
225 if (!isListening())
226 return;
227
228 std::string filter = "(ether proto " + to_string(ethernet::ETHERTYPE_NDN) +
Junxiao Shi0d82d042017-07-07 06:15:27 +0000229 ") && (ether dst " + m_localEndpoint->getEthernetAddress().toString() + ")";
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400230 for (const auto& addr : m_channelFaces | boost::adaptors::map_keys) {
231 filter += " && (not ether src " + addr.toString() + ")";
232 }
233 // "not vlan" must appear last in the filter expression, or the
234 // rest of the filter won't work as intended, see pcap-filter(7)
235 filter += " && (not vlan)";
236
237 NFD_LOG_CHAN_TRACE("Updating filter: " << filter);
238 m_pcap.setPacketFilter(filter.data());
239}
240
241} // namespace face
242} // namespace nfd