Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nebula_hw_interfaces): better UDP socket #231

Open
wants to merge 30 commits into
base: main
Choose a base branch
from

Conversation

mojomex
Copy link
Collaborator

@mojomex mojomex commented Nov 21, 2024

PR Type

  • Improvement

Description

The current Boost.ASIO/transport_drivers implementation is bloated and does not offer all the features we need for accurate timing and packet loss measurement.
Specifically, a good equivalent to recvmsg (see man recvmsg for details) is not supported.

This PR introduces a new, minimal and robust UDP socket implementation with the following features:

*: Depending on the network interface hardware, the timestamp will be measured in hardware on packet arrival, or by the kernel in software as soon as possible after. In any case, the timing is much more accurate than doing it in user space, where scheduling has a huge impact on accuracy.

Usage

I aimed to document the class as well as possible, but still, here is a quick rundown of how to use the socket:

#include <nebula_hw_interfaces/nebula_hw_interfaces_common/connections/udp.hpp>

void my_func {
  using nebula::drivers::connections::UdpSocket;
  using nebula::drivers::connections::SocketError;
  using nebula::drivers::connections::UsageError;

  // Creates the underlying socket and sets timestamping, overflow reporting, etc. as options.
  UdpSocket sock{};

  // Sets the host IP and port. No actual socket operations happen at this point.
  sock.init("192.168.1.10", 1234);

  // Binds (= activates) the socket on the given host IP/port.
  sock.bind();

  // Forwards all received packets, with metadata (timestamp, packet drops, etc.) to `my_function`.
  sock.subscribe(my_funcion);

  // Stops forwarding packets. May have to be called manually if a lambda with reference-type captures
  // has been used as `my_function`, and its lifetime is shorter than that of `sock`.
  sock.unsubscribe();
}

Functions can also be chained like this:

auto sock = UdpSocket().init(...).bind().subscribe(...);

Pre-Review Checklist for the PR Author

PR Author should check the checkboxes below when creating the PR.

  • Assign PR to reviewer

Checklist for the PR Reviewer

Reviewers should check the checkboxes below before approval.

  • Commits are properly organized and messages are according to the guideline
  • (Optional) Unit tests have been written for new behavior
  • PR title describes the changes

Post-Review Checklist for the PR Author

PR Author should check the checkboxes below before merging.

  • All open points are addressed and tracked via issues or tickets

CI Checks

  • Build and test for PR: Required to pass before the merge.

Copy link

codecov bot commented Nov 21, 2024

Codecov Report

Attention: Patch coverage is 66.22222% with 76 lines in your changes missing coverage. Please review.

Project coverage is 27.07%. Comparing base (3284357) to head (3124948).
Report is 10 commits behind head on main.

Files with missing lines Patch % Lines
...es/nebula_hw_interfaces_common/connections/udp.hpp 63.49% 23 Missing and 23 partials ⚠️
nebula_hw_interfaces/test/common/test_udp.cpp 64.86% 7 Missing and 19 partials ⚠️
...ebula_hw_interfaces/test/common/test_udp/utils.hpp 84.00% 0 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #231      +/-   ##
==========================================
+ Coverage   26.10%   27.07%   +0.97%     
==========================================
  Files         100      104       +4     
  Lines        9218     9455     +237     
  Branches     2215     2319     +104     
==========================================
+ Hits         2406     2560     +154     
- Misses       6423     6458      +35     
- Partials      389      437      +48     
Flag Coverage Δ
differential 27.07% <66.22%> (?)
total ?

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@mojomex
Copy link
Collaborator Author

mojomex commented Nov 22, 2024

Unit tests have been written, but things like querying net.core.rmem_max, or simulating packet loss, is not easily possible in Docker in CI, so test coverage is currently at 75%.

@mojomex mojomex self-assigned this Nov 22, 2024
@mojomex mojomex requested a review from drwnz November 22, 2024 05:20
mojomex added a commit that referenced this pull request Nov 26, 2024
Signed-off-by: Max SCHMELLER <[email protected]>
mojomex added a commit that referenced this pull request Nov 26, 2024
Signed-off-by: Max SCHMELLER <[email protected]>
Copy link
Collaborator

@drwnz drwnz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left some comments! Many are questions and there are no must-do changes but please have a look and I will approve once confirmed.


// Enable kernel-space receive time measurement
result = setsockopt(sock_fd, SOL_SOCKET, SO_TIMESTAMP, &enable, sizeof(enable));
if (result < 0) throw SocketError(errno);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very very nit: sock_fd is checked for explicitly -1, where setsockopt for < 0. Both seem to only return -1 when a problem has occurred.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 193bf6e.

* @brief Gracefully stops the active receiver thread (if any) but keeps the socket alive. The
* same socket can later be subscribed again.
*/
UdpSocket & unsubscribe()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the socket is kept alive, should we consider shutting it down to prevent reading from it?
shutdown(sock_fd_, SHUT_RD);
and associated error handling.


~UdpSocket()
{
unsubscribe();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to leave any subscribed multicast groups on destruction? (or in unsubscribe)

// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: are we moving to header blocks?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean

#ifndef ...
#define ...

versus

#pragma once

?

I strongly prefer pragma once for style, is there any discussion on this I have missed?

// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: are we moving to header blocks?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(see my other comment on udp.hpp)

int sock_fd_;
pollfd poll_fd_;

size_t buffer_size_ = 1500;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uniform initialization and const/constexpr for the 1500?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be changed during runtime via set_mtu, but I agree with uniform initialization.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 9ea3a90.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants