Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, Regents of the University of California, |
Chengyu Fan | 4381fb6 | 2015-01-14 11:37:04 -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. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 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/>. |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 24 | */ |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 25 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_FACE_UDP_FACTORY_HPP |
| 27 | #define NFD_DAEMON_FACE_UDP_FACTORY_HPP |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 28 | |
| 29 | #include "protocol-factory.hpp" |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 30 | #include "network-predicate.hpp" |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 31 | #include "udp-channel.hpp" |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 32 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 33 | namespace nfd::face { |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 34 | |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 35 | /** |
| 36 | * \brief Protocol factory for UDP over IPv4 and IPv6. |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 37 | */ |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 38 | class UdpFactory final : public ProtocolFactory |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 39 | { |
| 40 | public: |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 41 | class Error : public ProtocolFactory::Error |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 42 | { |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 43 | public: |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 44 | using ProtocolFactory::Error::Error; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 45 | }; |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 46 | |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 47 | static const std::string& |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 48 | getId() noexcept; |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 49 | |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 50 | explicit |
| 51 | UdpFactory(const CtorParams& params); |
| 52 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 53 | /** |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 54 | * \brief Create UDP-based channel using udp::Endpoint. |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 55 | * |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 56 | * udp::Endpoint is an alias for boost::asio::ip::udp::endpoint. |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 57 | * |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 58 | * If this method is called twice with the same endpoint, only one channel |
| 59 | * will be created. The second call will just return the existing channel. |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 60 | * |
| 61 | * If a multicast face is already active on the same local endpoint, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 62 | * the creation fails and an exception is thrown. |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 63 | * |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 64 | * \return Always a valid pointer to a UdpChannel object, an exception |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 65 | * is thrown if it cannot be created. |
| 66 | * \throw UdpFactory::Error |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 67 | */ |
| 68 | shared_ptr<UdpChannel> |
| 69 | createChannel(const udp::Endpoint& localEndpoint, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 70 | time::nanoseconds idleTimeout); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 71 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 72 | /** |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 73 | * \brief Create a multicast UDP face. |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 74 | * |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 75 | * udp::Endpoint is an alias for boost::asio::ip::udp::endpoint. |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 76 | * |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 77 | * The face will join the specified multicast group. |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 78 | * |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 79 | * If this method is called twice with the same set of arguments, only one |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 80 | * face will be created. The second call will just return the existing face. |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 81 | * |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 82 | * If a UDP channel, unicast face, or multicast face already exists on the |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 83 | * same combination of local address and multicast port, the creation fails |
| 84 | * and an exception is thrown. |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 85 | * |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 86 | * \param netif the network interface to which the face will be bound |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 87 | * \param localAddress the local IP address to which the face will be bound |
| 88 | * \param multicastEndpoint the multicast endpoint (multicast group and port number) |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 89 | * |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 90 | * \return Always a valid shared pointer to the created face; |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 91 | * an exception is thrown if the face cannot be created. |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 92 | * \throw std::runtime_error %Face creation failed |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 93 | */ |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 94 | shared_ptr<Face> |
Davide Pesavento | 279af1c | 2022-08-29 20:18:32 -0400 | [diff] [blame] | 95 | createMulticastFace(const ndn::net::NetworkInterface& netif, |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 96 | const boost::asio::ip::address& localAddress, |
| 97 | const udp::Endpoint& multicastEndpoint); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 98 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 99 | private: |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 100 | void |
| 101 | doProcessConfig(OptionalConfigSection configSection, |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 102 | FaceSystem::ConfigContext& context) final; |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 103 | |
| 104 | void |
| 105 | doCreateFace(const CreateFaceRequest& req, |
| 106 | const FaceCreatedCallback& onCreated, |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 107 | const FaceCreationFailedCallback& onFailure) final; |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 108 | |
| 109 | std::vector<shared_ptr<const Channel>> |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 110 | doGetChannels() const final; |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 111 | |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 112 | /** |
| 113 | * \brief Create UDP multicast faces on \p netif if needed by \p m_mcastConfig. |
| 114 | * \return List of faces (just created or already existing) on \p netif. |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 115 | */ |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 116 | std::vector<shared_ptr<Face>> |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 117 | applyMcastConfigToNetif(const shared_ptr<const ndn::net::NetworkInterface>& netif); |
| 118 | |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 119 | /** |
| 120 | * \brief Create and destroy UDP multicast faces according to \p m_mcastConfig. |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 121 | */ |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 122 | void |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 123 | applyMcastConfig(const FaceSystem::ConfigContext& context); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 124 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 125 | private: |
Davide Pesavento | d5e89e8 | 2018-02-07 21:40:21 -0500 | [diff] [blame] | 126 | bool m_wantCongestionMarking = false; |
Junxiao Shi | a6286a9 | 2021-02-23 06:43:52 -0700 | [diff] [blame] | 127 | size_t m_defaultUnicastMtu = ndn::MAX_NDN_PACKET_SIZE; |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 128 | std::map<udp::Endpoint, shared_ptr<UdpChannel>> m_channels; |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 129 | |
| 130 | struct MulticastConfig |
| 131 | { |
| 132 | bool isEnabled = false; |
| 133 | udp::Endpoint group = udp::getDefaultMulticastGroup(); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 134 | udp::Endpoint groupV6 = udp::getDefaultMulticastGroupV6(); |
Teng Liang | fe4fce3 | 2017-03-29 04:49:38 +0000 | [diff] [blame] | 135 | ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_MULTI_ACCESS; |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 136 | NetworkInterfacePredicate netifPredicate; |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 137 | }; |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 138 | MulticastConfig m_mcastConfig; |
| 139 | std::map<udp::Endpoint, shared_ptr<Face>> m_mcastFaces; |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 140 | |
| 141 | signal::ScopedConnection m_netifAddConn; |
| 142 | struct NetifConns |
| 143 | { |
| 144 | signal::ScopedConnection addrAddConn; |
| 145 | }; |
| 146 | std::map<int, NetifConns> m_netifConns; // ifindex => signal connections |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 147 | }; |
| 148 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 149 | } // namespace nfd::face |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 150 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 151 | #endif // NFD_DAEMON_FACE_UDP_FACTORY_HPP |