blob: 655f3132d0ca2bee66daf7c0b9d35f734b36b888 [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 Pesavento1e0c88e2023-10-05 03:52:52 -04003 * Copyright (c) 2014-2023, 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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040036namespace nfd::face {
Davide Pesavento43ff2a92017-05-18 19:50:57 -040037
Davide Pesaventoa3148082018-04-12 18:21:54 -040038NFD_LOG_INIT(EthernetChannel);
Davide Pesavento43ff2a92017-05-18 19:50:57 -040039
Junxiao Shi0d82d042017-07-07 06:15:27 +000040EthernetChannel::EthernetChannel(shared_ptr<const ndn::net::NetworkInterface> localEndpoint,
Davide Pesavento43ff2a92017-05-18 19:50:57 -040041 time::nanoseconds idleTimeout)
Junxiao Shi0d82d042017-07-07 06:15:27 +000042 : m_localEndpoint(std::move(localEndpoint))
Davide Pesavento43ff2a92017-05-18 19:50:57 -040043 , m_isListening(false)
44 , m_socket(getGlobalIoService())
Junxiao Shi0d82d042017-07-07 06:15:27 +000045 , m_pcap(m_localEndpoint->getName())
Davide Pesavento43ff2a92017-05-18 19:50:57 -040046 , m_idleFaceTimeout(idleTimeout)
47#ifdef _DEBUG
48 , m_nDropped(0)
49#endif
50{
Junxiao Shi0d82d042017-07-07 06:15:27 +000051 setUri(FaceUri::fromDev(m_localEndpoint->getName()));
Davide Pesavento43ff2a92017-05-18 19:50:57 -040052 NFD_LOG_CHAN_INFO("Creating channel");
53}
54
55void
56EthernetChannel::connect(const ethernet::Address& remoteEndpoint,
Davide Pesavento15b55052018-01-27 19:09:28 -050057 const FaceParams& params,
Davide Pesavento43ff2a92017-05-18 19:50:57 -040058 const FaceCreatedCallback& onFaceCreated,
59 const FaceCreationFailedCallback& onConnectFailed)
60{
61 shared_ptr<Face> face;
62 try {
Davide Pesavento15b55052018-01-27 19:09:28 -050063 face = createFace(remoteEndpoint, params).second;
Davide Pesavento43ff2a92017-05-18 19:50:57 -040064 }
65 catch (const boost::system::system_error& e) {
66 NFD_LOG_CHAN_DEBUG("Face creation for " << remoteEndpoint << " failed: " << e.what());
67 if (onConnectFailed)
Davide Pesaventoe4b22382018-06-10 14:37:24 -040068 onConnectFailed(504, "Face creation failed: "s + e.what());
Davide Pesavento43ff2a92017-05-18 19:50:57 -040069 return;
70 }
71
72 // Need to invoke the callback regardless of whether or not we had already
73 // created the face so that control responses and such can be sent
74 onFaceCreated(face);
75}
76
77void
78EthernetChannel::listen(const FaceCreatedCallback& onFaceCreated,
79 const FaceCreationFailedCallback& onFaceCreationFailed)
80{
81 if (isListening()) {
82 NFD_LOG_CHAN_WARN("Already listening");
83 return;
84 }
85 m_isListening = true;
86
87 try {
88 m_pcap.activate(DLT_EN10MB);
89 m_socket.assign(m_pcap.getFd());
90 }
91 catch (const PcapHelper::Error& e) {
Davide Pesavento19779d82019-02-14 13:40:04 -050092 NDN_THROW_NESTED(Error(e.what()));
Davide Pesavento43ff2a92017-05-18 19:50:57 -040093 }
94 updateFilter();
95
96 asyncRead(onFaceCreated, onFaceCreationFailed);
97 NFD_LOG_CHAN_DEBUG("Started listening");
98}
99
100void
101EthernetChannel::asyncRead(const FaceCreatedCallback& onFaceCreated,
102 const FaceCreationFailedCallback& onReceiveFailed)
103{
Davide Pesaventoc5a9f812023-10-17 18:01:52 -0400104 m_socket.async_wait(boost::asio::posix::stream_descriptor::wait_read, [=] (const auto& e) {
105 handleRead(e, onFaceCreated, onReceiveFailed);
106 });
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 Pesaventoa3a7a4e2022-05-29 16:06:22 -0400123 auto [pkt, readErr] = m_pcap.readNextPacket();
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500124 if (pkt.empty()) {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400125 NFD_LOG_CHAN_WARN("Read error: " << readErr);
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400126 }
127 else {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400128 auto [eh, frameErr] = ethernet::checkFrameHeader(pkt, m_localEndpoint->getEthernetAddress(),
129 m_localEndpoint->getEthernetAddress());
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400130 if (eh == nullptr) {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400131 NFD_LOG_CHAN_DEBUG(frameErr);
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400132 }
133 else {
134 ethernet::Address sender(eh->ether_shost);
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500135 pkt = pkt.subspan(ethernet::HDR_LEN);
136 processIncomingPacket(pkt, sender, onFaceCreated, onReceiveFailed);
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400137 }
138 }
139
140#ifdef _DEBUG
141 size_t nDropped = m_pcap.getNDropped();
142 if (nDropped - m_nDropped > 0)
143 NFD_LOG_CHAN_DEBUG("Detected " << nDropped - m_nDropped << " dropped frame(s)");
144 m_nDropped = nDropped;
145#endif
146
147 asyncRead(onFaceCreated, onReceiveFailed);
148}
149
150void
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500151EthernetChannel::processIncomingPacket(span<const uint8_t> packet,
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400152 const ethernet::Address& sender,
153 const FaceCreatedCallback& onFaceCreated,
154 const FaceCreationFailedCallback& onReceiveFailed)
155{
156 NFD_LOG_CHAN_TRACE("New peer " << sender);
157
158 bool isCreated = false;
159 shared_ptr<Face> face;
160 try {
Davide Pesavento15b55052018-01-27 19:09:28 -0500161 FaceParams params;
162 params.persistency = ndn::nfd::FACE_PERSISTENCY_ON_DEMAND;
163 std::tie(isCreated, face) = createFace(sender, params);
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400164 }
165 catch (const EthernetTransport::Error& e) {
166 NFD_LOG_CHAN_DEBUG("Face creation for " << sender << " failed: " << e.what());
167 if (onReceiveFailed)
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400168 onReceiveFailed(504, "Face creation failed: "s + e.what());
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400169 return;
170 }
171
172 if (isCreated)
173 onFaceCreated(face);
174 else
175 NFD_LOG_CHAN_DEBUG("Received frame for existing face");
176
177 // dispatch the packet to the face for processing
178 auto* transport = static_cast<UnicastEthernetTransport*>(face->getTransport());
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500179 transport->receivePayload(packet, sender);
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400180}
181
182std::pair<bool, shared_ptr<Face>>
183EthernetChannel::createFace(const ethernet::Address& remoteEndpoint,
Davide Pesavento15b55052018-01-27 19:09:28 -0500184 const FaceParams& params)
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400185{
186 auto it = m_channelFaces.find(remoteEndpoint);
187 if (it != m_channelFaces.end()) {
188 // we already have a face for this endpoint, so reuse it
189 NFD_LOG_CHAN_TRACE("Reusing existing face for " << remoteEndpoint);
190 return {false, it->second};
191 }
192
193 // else, create a new face
Eric Newberry2642cd22017-07-13 21:34:53 -0400194 GenericLinkService::Options options;
Davide Pesavento00a3c9d2017-08-10 22:40:16 -0400195 options.allowFragmentation = true;
196 options.allowReassembly = true;
Davide Pesavento15b55052018-01-27 19:09:28 -0500197 options.reliabilityOptions.isEnabled = params.wantLpReliability;
Eric Newberrycb6551e2020-03-02 14:12:16 -0800198 if (params.mtu) {
199 options.overrideMtu = *params.mtu;
200 }
Davide Pesavento00a3c9d2017-08-10 22:40:16 -0400201
Eric Newberry2642cd22017-07-13 21:34:53 -0400202 auto linkService = make_unique<GenericLinkService>(options);
Junxiao Shi0d82d042017-07-07 06:15:27 +0000203 auto transport = make_unique<UnicastEthernetTransport>(*m_localEndpoint, remoteEndpoint,
Eric Newberrycb6551e2020-03-02 14:12:16 -0800204 params.persistency, m_idleFaceTimeout);
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400205 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400206 face->setChannel(weak_from_this());
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400207
208 m_channelFaces[remoteEndpoint] = face;
209 connectFaceClosedSignal(*face, [this, remoteEndpoint] {
210 m_channelFaces.erase(remoteEndpoint);
211 updateFilter();
212 });
213 updateFilter();
214
215 return {true, face};
216}
217
218void
219EthernetChannel::updateFilter()
220{
221 if (!isListening())
222 return;
223
224 std::string filter = "(ether proto " + to_string(ethernet::ETHERTYPE_NDN) +
Junxiao Shi0d82d042017-07-07 06:15:27 +0000225 ") && (ether dst " + m_localEndpoint->getEthernetAddress().toString() + ")";
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400226 for (const auto& addr : m_channelFaces | boost::adaptors::map_keys) {
227 filter += " && (not ether src " + addr.toString() + ")";
228 }
229 // "not vlan" must appear last in the filter expression, or the
230 // rest of the filter won't work as intended, see pcap-filter(7)
231 filter += " && (not vlan)";
232
233 NFD_LOG_CHAN_TRACE("Updating filter: " << filter);
234 m_pcap.setPacketFilter(filter.data());
235}
236
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400237} // namespace nfd::face