25#ifndef JOIN_FABRIC_NETLINK_MANAGER_HPP
26#define JOIN_FABRIC_NETLINK_MANAGER_HPP
36#include <unordered_map>
37#include <system_error>
109 int sendRequest (struct nlmsghdr* nlh,
bool sync,
std::chrono::milliseconds timeout =
std::chrono::seconds (5));
115 virtual
void onReadable (
int fd) override final;
143 static
void addAttributes (struct nlmsghdr* nlh,
int type, const
void* data,
int alen);
167 template <typename T, typename Flag>
170 if (oldVal != newVal)
175 return static_cast<Flag
> (0);
202 std::unordered_map<uint32_t, std::unique_ptr<PendingRequest>>
_pending;
condition variable class.
Definition condition.hpp:42
Event handler interface class.
Definition reactor.hpp:48
class used to protect shared data from being simultaneously accessed by multiple threads.
Definition mutex.hpp:37
base class for netlink-based managers.
Definition netlink_manager.hpp:51
Reactor & reactor() const noexcept
get the event loop reactor.
Definition netlink_manager.cpp:52
static void addAttributes(struct nlmsghdr *nlh, int type, const void *data, int alen)
add an attribute to a netlink message.
Definition netlink_manager.cpp:234
std::atomic< uint32_t > _seq
sequence number.
Definition netlink_manager.hpp:191
NetlinkManager(uint32_t groups, Reactor &reactor=ReactorThread::reactor())
create instance.
Definition netlink_manager.cpp:38
static constexpr size_t _bufferSize
internal buffer size.
Definition netlink_manager.hpp:185
virtual void onReadable(int fd) override final
method called when data are ready to be read on handle.
Definition netlink_manager.cpp:163
int sendRequest(struct nlmsghdr *nlh, bool sync, std::chrono::milliseconds timeout=std::chrono::seconds(5))
send a netlink request, optionally waiting for the ack.
Definition netlink_manager.cpp:79
Mutex _syncMutex
protection mutex.
Definition netlink_manager.hpp:205
static Flag updateValue(T &oldVal, const T &newVal, Flag changed)
update a value in place and report whether it changed.
Definition netlink_manager.hpp:168
NetlinkManager(NetlinkManager &&)=delete
create instance by move.
void notifyRequest(uint32_t seq, const std::error_code &error=std::error_code())
notify a pending synchronous request.
Definition netlink_manager.cpp:199
Reactor & _reactor
event loop reactor.
Definition netlink_manager.hpp:208
NetlinkManager & operator=(const NetlinkManager &)=delete
assign instance by copy.
void stop()
stop listening for netlink events.
Definition netlink_manager.cpp:70
NetlinkManager(const NetlinkManager &)=delete
create instance by copy.
static struct rtattr * startNestedAttributes(struct nlmsghdr *nlh, int type)
open a nested attribute block.
Definition netlink_manager.cpp:249
void notifyAllRequests(const std::error_code &error)
notify every pending synchronous requests with an error.
Definition netlink_manager.cpp:216
void start()
start listening for netlink events.
Definition netlink_manager.cpp:61
std::unique_ptr< char[]> _buffer
internal read buffer.
Definition netlink_manager.hpp:188
virtual ~NetlinkManager()=default
destroy instance.
virtual void onMessage(struct nlmsghdr *nlh)=0
dispatch a single RTM_* message to the derived class.
Netlink::Socket _socket
underlying socket.
Definition netlink_manager.hpp:179
static constexpr int _rcvBufferSize
socket receive buffer size.
Definition netlink_manager.hpp:182
std::unordered_map< uint32_t, std::unique_ptr< PendingRequest > > _pending
synchronous requests indexed by sequence number.
Definition netlink_manager.hpp:202
static int stopNestedAttributes(struct nlmsghdr *nlh, struct rtattr *nested)
close a nested attribute block.
Definition netlink_manager.cpp:261
static Reactor & reactor()
get the global Reactor instance.
Definition reactor.cpp:584
Reactor class.
Definition reactor.hpp:156
bool changed(InterfaceChangeType flags, InterfaceChangeType type)
Definition ifplugd.cpp:90
Definition acceptor.hpp:32
pending synchronous request.
Definition netlink_manager.hpp:195
bool done
Definition netlink_manager.hpp:198
std::error_code error
Definition netlink_manager.hpp:197
Condition cond
Definition netlink_manager.hpp:196