1 //
2 // Copyright 2017 Ettus Research, National Instruments Company
3 // Copyright 2019 Ettus Research, National Instruments Brand
4 //
5 // SPDX-License-Identifier: GPL-3.0-or-later
6 //
7
8 #include "mpmd_link_if_ctrl_udp.hpp"
9 #include "mpmd_impl.hpp"
10 #include "mpmd_link_if_mgr.hpp"
11 #include <uhd/rfnoc/constants.hpp>
12 #include <uhd/transport/udp_constants.hpp>
13 #include <uhd/transport/udp_simple.hpp>
14 #include <uhd/transport/udp_zero_copy.hpp>
15 #include <uhdlib/rfnoc/rfnoc_common.hpp>
16 #include <uhdlib/transport/udp_boost_asio_link.hpp>
17 #include <uhdlib/transport/udp_common.hpp>
18 #include <uhdlib/utils/narrow.hpp>
19 #include <string>
20 #ifdef HAVE_DPDK
21 # include <uhdlib/transport/dpdk_simple.hpp>
22 # include <uhdlib/transport/udp_dpdk_link.hpp>
23 #endif
24
25 using namespace uhd;
26 using namespace uhd::transport;
27 using namespace uhd::mpmd::xport;
28
29 namespace {
30
31 //! Maximum CHDR packet size in bytes
32 const size_t MPMD_10GE_DATA_FRAME_MAX_SIZE = 8000;
33 const size_t MPMD_1GE_DATA_FRAME_MAX_SIZE = 1472;
34 const size_t MPMD_1GE_ASYNCMSG_FRAME_MAX_SIZE = 1472;
35
36 //! Number of send/recv frames
37 const size_t MPMD_ETH_NUM_FRAMES = 32;
38
39 //!
40 const double MPMD_BUFFER_DEPTH = 20.0e-3; // s
41 //! For MTU discovery, the time we wait for a packet before calling it
42 // oversized (seconds).
43 const double MPMD_MTU_DISCOVERY_TIMEOUT = 0.02;
44
45 // TODO: move these to appropriate header file for all other devices
46 const size_t MAX_RATE_1GIGE = 1e9 / 8; // byte/s
47 const size_t MAX_RATE_10GIGE = 10e9 / 8; // byte/s
48
49
get_udp_info_from_xport_info(const mpmd_link_if_mgr::xport_info_list_t & link_info_list)50 mpmd_link_if_ctrl_udp::udp_link_info_map get_udp_info_from_xport_info(
51 const mpmd_link_if_mgr::xport_info_list_t& link_info_list)
52 {
53 mpmd_link_if_ctrl_udp::udp_link_info_map result;
54 for (const auto& link_info : link_info_list) {
55 if (!link_info.count("ipv4")) {
56 UHD_LOG_ERROR("MPMD::XPORT::UDP",
57 "Invalid response from get_chdr_link_options()! No `ipv4' key!");
58 throw uhd::runtime_error(
59 "Invalid response from get_chdr_link_options()! No `ipv4' key!");
60 }
61 if (!link_info.count("port")) {
62 UHD_LOG_ERROR("MPMD::XPORT::UDP",
63 "Invalid response from get_chdr_link_options()! No `port' key!");
64 throw uhd::runtime_error(
65 "Invalid response from get_chdr_link_options()! No `port' key!");
66 }
67 const std::string udp_port = link_info.at("port");
68 const size_t link_rate = link_info.count("link_rate")
69 ? std::stoul(link_info.at("link_rate"))
70 : MAX_RATE_1GIGE;
71 const std::string link_type = link_info.at("type");
72 const size_t if_mtu = std::stoul(link_info.at("mtu"));
73 result.emplace(link_info.at("ipv4"),
74 mpmd_link_if_ctrl_udp::udp_link_info_t{
75 udp_port, link_rate, link_type, if_mtu});
76 }
77
78 return result;
79 }
80
get_addrs_from_mb_args(const uhd::device_addr_t & mb_args,const mpmd_link_if_ctrl_udp::udp_link_info_map & link_info_list)81 std::vector<std::string> get_addrs_from_mb_args(const uhd::device_addr_t& mb_args,
82 const mpmd_link_if_ctrl_udp::udp_link_info_map& link_info_list)
83 {
84 std::vector<std::string> addrs;
85 if(link_info_list.size() > 0 &&
86 link_info_list.begin()->second.link_type == "internal") {
87 // If link_type is "internal" we are local. In this case
88 // use this address always. MPM knows better than us.
89 addrs.push_back(link_info_list.begin()->first);
90 }
91 else {
92 if (mb_args.has_key(FIRST_ADDR_KEY)) {
93 addrs.push_back(mb_args[FIRST_ADDR_KEY]);
94 }
95 if (mb_args.has_key(SECOND_ADDR_KEY)) {
96 addrs.push_back(mb_args[SECOND_ADDR_KEY]);
97 }
98 }
99 if(addrs.empty()) {
100 if(link_info_list.size() > 0) {
101 addrs.push_back(link_info_list.begin()->first);
102 }
103 else {
104 UHD_LOG_WARNING("MPMD::XPORT::UDP",
105 "The `" << FIRST_ADDR_KEY
106 << "' key must be specified in "
107 "device args to create an Ethernet transport to an RFNoC block");
108 return {};
109 }
110 }
111
112 // This is where in UHD we encode the knowledge about what
113 // get_chdr_link_options() returns to us.
114 for (const auto& ip_addr : addrs) {
115 if (link_info_list.count(ip_addr)) {
116 continue;
117 }
118 UHD_LOG_WARNING("MPMD::XPORT::UDP",
119 "Cannot create UDP link to device: The IP address `"
120 << ip_addr << "' is requested, but not reachable.");
121 return {};
122 }
123
124 return addrs;
125 }
126
127 /*! Do a binary search to discover MTU
128 *
129 * Uses the MPM echo service to figure out MTU. We simply send a bunch of
130 * packets and see if they come back until we converged on the path MTU.
131 * The end result must lie between \p min_frame_size and \p max_frame_size.
132 *
133 * \param address IP address
134 * \param port UDP port (yeah it's a string!)
135 * \param min_frame_size Minimum frame size, initialize algorithm to start
136 * with this value
137 * \param max_frame_size Maximum frame size, initialize algorithm to start
138 * with this value
139 * \param echo_timeout Timeout value in seconds. For frame sizes that
140 * exceed the MTU, we don't expect a response, and this
141 * is the amount of time we'll wait before we assume
142 * the frame size exceeds the MTU.
143 */
discover_mtu(const std::string & address,const std::string & port,size_t min_frame_size,size_t max_frame_size,const double echo_timeout,const bool use_dpdk)144 size_t discover_mtu(const std::string& address,
145 const std::string& port,
146 size_t min_frame_size,
147 size_t max_frame_size,
148 const double echo_timeout,
149 const bool use_dpdk)
150 {
151 //! Function to create a udp_simple::sptr (kernel-based or DPDK-based)
152 using udp_simple_factory_t = std::function<uhd::transport::udp_simple::sptr(
153 const std::string&, const std::string&)>;
154
155 udp_simple_factory_t udp_make_broadcast = udp_simple::make_broadcast;
156 if (use_dpdk) {
157 #ifdef HAVE_DPDK
158 udp_make_broadcast = [](const std::string& addr, const std::string& port) {
159 return dpdk_simple::make_broadcast(addr, port);
160 };
161 #else
162 UHD_LOG_WARNING("MPMD",
163 "DPDK was requested but is not available, falling back to regular UDP");
164 #endif
165 }
166 const size_t echo_prefix_offset = uhd::mpmd::mpmd_impl::MPM_ECHO_CMD.size();
167 const size_t mtu_hdr_len = echo_prefix_offset + 10;
168 UHD_ASSERT_THROW(min_frame_size < max_frame_size);
169 UHD_ASSERT_THROW(min_frame_size % 4 == 0);
170 UHD_ASSERT_THROW(max_frame_size % 4 == 0);
171 UHD_ASSERT_THROW(min_frame_size >= echo_prefix_offset + mtu_hdr_len);
172 using namespace uhd::transport;
173 // The return port will probably differ from the discovery port, so we
174 // need a "broadcast" UDP connection; using make_connected() would
175 // drop packets
176 udp_simple::sptr udp = udp_make_broadcast(address, port);
177 std::string send_buf(uhd::mpmd::mpmd_impl::MPM_ECHO_CMD);
178 send_buf.resize(max_frame_size, '#');
179 UHD_ASSERT_THROW(send_buf.size() == max_frame_size);
180 std::vector<uint8_t> recv_buf;
181 recv_buf.resize(max_frame_size, ' ');
182
183 // Little helper to check returned packets match the sent ones
184 auto require_bufs_match = [&recv_buf, &send_buf, mtu_hdr_len](const size_t len) {
185 if (len < mtu_hdr_len
186 or std::memcmp((void*)&recv_buf[0], (void*)&send_buf[0], mtu_hdr_len) != 0) {
187 throw uhd::runtime_error("Unexpected content of MTU "
188 "discovery return packet!");
189 }
190 };
191 UHD_LOG_TRACE("MPMD", "Determining UDP MTU... ");
192 size_t seq_no = 0;
193 while (min_frame_size < max_frame_size) {
194 // Only test multiples of 4 bytes!
195 const size_t test_frame_size = (max_frame_size / 2 + min_frame_size / 2 + 3)
196 & ~size_t(3);
197 // Encode sequence number and current size in the string, makes it
198 // easy to debug in code or Wireshark. Is also used for identifying
199 // response packets.
200 std::sprintf(
201 &send_buf[echo_prefix_offset], ";%04lu,%04lu", seq_no++, test_frame_size);
202 UHD_LOG_TRACE("MPMD", "Testing frame size " << test_frame_size);
203 udp->send(boost::asio::buffer(&send_buf[0], test_frame_size));
204
205 const size_t len = udp->recv(boost::asio::buffer(recv_buf), echo_timeout);
206 if (len == 0) {
207 // Nothing received, so this is probably too big
208 max_frame_size = test_frame_size - 4;
209 } else if (len >= test_frame_size) {
210 // Size went through, so bump the minimum
211 require_bufs_match(len);
212 min_frame_size = test_frame_size;
213 } else if (len < test_frame_size) {
214 // This is an odd case. Something must have snipped the packet
215 // on the way back. Still, we'll just back off and try
216 // something smaller.
217 UHD_LOG_DEBUG("MPMD", "Unexpected packet truncation during MTU discovery.");
218 require_bufs_match(len);
219 max_frame_size = len;
220 }
221 }
222 UHD_LOG_DEBUG("MPMD", "Path MTU for address " << address << ": " << min_frame_size);
223 return min_frame_size;
224 }
225
226 } // namespace
227
228
229 /******************************************************************************
230 * Structors
231 *****************************************************************************/
mpmd_link_if_ctrl_udp(const uhd::device_addr_t & mb_args,const mpmd_link_if_mgr::xport_info_list_t & xport_info,const uhd::rfnoc::chdr_w_t chdr_w)232 mpmd_link_if_ctrl_udp::mpmd_link_if_ctrl_udp(const uhd::device_addr_t& mb_args,
233 const mpmd_link_if_mgr::xport_info_list_t& xport_info,
234 const uhd::rfnoc::chdr_w_t chdr_w)
235 : _mb_args(mb_args)
236 , _udp_info(get_udp_info_from_xport_info(xport_info))
237 , _mtu(MPMD_10GE_DATA_FRAME_MAX_SIZE)
238 , _pkt_factory(chdr_w, ENDIANNESS_LITTLE)
239 {
240 const bool use_dpdk =
241 mb_args.has_key("use_dpdk"); // FIXME use constrained_device_args
242 const std::string mpm_discovery_port = _mb_args.get(
243 mpmd_impl::MPM_DISCOVERY_PORT_KEY, std::to_string(mpmd_impl::MPM_DISCOVERY_PORT));
244 auto discover_mtu_for_ip = [mpm_discovery_port, use_dpdk](
245 const std::string& ip_addr) {
246 return discover_mtu(ip_addr,
247 mpm_discovery_port,
248 IP_PROTOCOL_MIN_MTU_SIZE - IP_PROTOCOL_UDP_PLUS_IP_HEADER,
249 MPMD_10GE_DATA_FRAME_MAX_SIZE,
250 MPMD_MTU_DISCOVERY_TIMEOUT,
251 use_dpdk);
252 };
253
254 const std::vector<std::string> requested_addrs(
255 get_addrs_from_mb_args(mb_args, _udp_info));
256 for (const auto& ip_addr : requested_addrs) {
257 try {
258 // If MTU discovery fails, we gracefully recover, but declare that
259 // link invalid.
260 auto& info = _udp_info.at(ip_addr);
261 if (info.link_type == "internal") {
262 UHD_LOG_TRACE("MPMD::XPORT::UDP",
263 "MTU for internal interface " << ip_addr << " is "
264 << std::to_string(info.if_mtu));
265 _mtu = std::min(_mtu, info.if_mtu);
266 } else {
267 _mtu = std::min(_mtu, discover_mtu_for_ip(ip_addr));
268 }
269 _available_addrs.push_back(ip_addr);
270 } catch (const uhd::exception& ex) {
271 UHD_LOG_WARNING("MPMD::XPORT::UDP",
272 "Error during MTU discovery on address " << ip_addr << ": " << ex.what());
273 }
274 }
275 }
276
277 /******************************************************************************
278 * API
279 *****************************************************************************/
get_link(const size_t link_idx,const uhd::transport::link_type_t link_type,const uhd::device_addr_t & link_args)280 uhd::transport::both_links_t mpmd_link_if_ctrl_udp::get_link(const size_t link_idx,
281 const uhd::transport::link_type_t link_type,
282 const uhd::device_addr_t& link_args)
283 {
284 UHD_ASSERT_THROW(link_idx < _available_addrs.size());
285 const std::string ip_addr = _available_addrs.at(link_idx);
286 const std::string udp_port = _udp_info.at(ip_addr).udp_port;
287
288 const size_t link_rate = get_link_rate(link_idx);
289 const bool use_dpdk = _mb_args.has_key("use_dpdk"); // FIXME use constrained device args
290 link_params_t default_link_params;
291 default_link_params.num_send_frames = MPMD_ETH_NUM_FRAMES;
292 default_link_params.num_recv_frames = MPMD_ETH_NUM_FRAMES;
293 default_link_params.send_frame_size = (link_rate == MAX_RATE_10GIGE)
294 ? MPMD_10GE_DATA_FRAME_MAX_SIZE
295 : (link_rate == MAX_RATE_1GIGE)
296 ? MPMD_1GE_DATA_FRAME_MAX_SIZE
297 : get_mtu(uhd::TX_DIRECTION);
298 default_link_params.recv_frame_size = (link_rate == MAX_RATE_10GIGE)
299 ? MPMD_10GE_DATA_FRAME_MAX_SIZE
300 : (link_rate == MAX_RATE_1GIGE)
301 ? MPMD_1GE_DATA_FRAME_MAX_SIZE
302 : get_mtu(uhd::RX_DIRECTION);
303 default_link_params.send_buff_size = get_link_rate(link_idx) * MPMD_BUFFER_DEPTH;
304 default_link_params.recv_buff_size = get_link_rate(link_idx) * MPMD_BUFFER_DEPTH;
305
306 #ifdef HAVE_DPDK
307 if(use_dpdk) {
308 default_link_params.num_recv_frames = default_link_params.recv_buff_size /
309 default_link_params.recv_frame_size;
310 }
311 #endif
312
313 link_params_t link_params = calculate_udp_link_params(link_type,
314 get_mtu(uhd::TX_DIRECTION),
315 get_mtu(uhd::RX_DIRECTION),
316 default_link_params,
317 _mb_args,
318 link_args);
319
320 // Enforce a minimum bound of the number of receive and send frames.
321 link_params.num_send_frames =
322 std::max(uhd::rfnoc::MIN_NUM_FRAMES, link_params.num_send_frames);
323 link_params.num_recv_frames =
324 std::max(uhd::rfnoc::MIN_NUM_FRAMES, link_params.num_recv_frames);
325
326 if (use_dpdk) {
327 #ifdef HAVE_DPDK
328 auto link = uhd::transport::udp_dpdk_link::make(ip_addr, udp_port, link_params);
329 return std::make_tuple(link,
330 link_params.send_buff_size,
331 link,
332 link_params.recv_buff_size,
333 true,
334 true);
335 #else
336 UHD_LOG_WARNING("MPMD", "Cannot create DPDK transport, falling back to UDP");
337 #endif
338 }
339 auto link = uhd::transport::udp_boost_asio_link::make(ip_addr,
340 udp_port,
341 link_params,
342 link_params.recv_buff_size,
343 link_params.send_buff_size);
344 return std::make_tuple(
345 link, link_params.send_buff_size, link, link_params.recv_buff_size, true, false);
346 }
347
get_num_links() const348 size_t mpmd_link_if_ctrl_udp::get_num_links() const
349 {
350 return _available_addrs.size();
351 }
352
353 //! Return the rate of the underlying link in bytes/sec
get_link_rate(const size_t link_idx) const354 double mpmd_link_if_ctrl_udp::get_link_rate(const size_t link_idx) const
355 {
356 UHD_ASSERT_THROW(link_idx < get_num_links());
357 return _udp_info.at(_available_addrs.at(link_idx)).link_rate;
358 }
359
360 const uhd::rfnoc::chdr::chdr_packet_factory&
get_packet_factory() const361 mpmd_link_if_ctrl_udp::get_packet_factory() const
362 {
363 return _pkt_factory;
364 }
365