25#ifndef JOIN_FABRIC_NETLINKMANAGER_HPP
26#define JOIN_FABRIC_NETLINKMANAGER_HPP
33#include <unordered_map>
38#include <linux/rtnetlink.h>
39#include <linux/netlink.h>
107 int sendRequest (struct nlmsghdr* nlh,
bool sync,
std::chrono::milliseconds timeout =
std::chrono::seconds (5));
116 template <class Rep, class Period>
119 auto inserted =
_pending.emplace (seq, std::make_unique<PendingRequest> ());
120 if (!inserted.second)
122 lastError = make_error_code (Errc::OperationFailed);
126 if (!inserted.first->second->cond.timedWait (lock, timeout))
128 _pending.erase (inserted.first);
129 lastError = make_error_code (Errc::TimedOut);
133 if (inserted.first->second->error != 0)
135 int err = inserted.first->second->error;
136 _pending.erase (inserted.first);
137 lastError = std::error_code (err, std::generic_category ());
149 virtual void onReceive (
int fd)
override final;
171 static void addAttributes (
struct nlmsghdr* nlh,
int type,
const void* data,
int alen);
195 template <
typename T,
typename Flag>
196 static Flag
updateValue (T& oldVal,
const T& newVal, Flag changed)
198 if (oldVal != newVal)
203 return static_cast<Flag
> (0);
223 std::unordered_map<uint32_t, std::unique_ptr<PendingRequest>>
_pending;
basic datagram socket class.
Definition socket.hpp:645
int type() const noexcept
get the protocol communication semantic.
Definition socket.hpp:516
condition variable class.
Definition condition.hpp:42
class used to protect shared data from being simultaneously accessed by multiple threads.
Definition mutex.hpp:37
base class for netlink-based managers.
Definition netlinkmanager.hpp:49
static void addAttributes(struct nlmsghdr *nlh, int type, const void *data, int alen)
add an attribute to a netlink message.
Definition netlinkmanager.cpp:154
std::atomic< uint32_t > _seq
sequence number.
Definition netlinkmanager.hpp:213
virtual void onReceive(int fd) override final
method called when data are ready to be read on handle.
Definition netlinkmanager.cpp:105
int waitResponse(ScopedLock< Mutex > &lock, uint32_t seq, std::chrono::duration< Rep, Period > timeout)
wait for specific netlink response.
Definition netlinkmanager.hpp:117
static constexpr size_t _bufferSize
internal buffer size.
Definition netlinkmanager.hpp:207
NetlinkManager(uint32_t groups, Reactor *reactor=nullptr)
create instance.
Definition netlinkmanager.cpp:35
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 netlinkmanager.cpp:79
Mutex _syncMutex
protection mutex.
Definition netlinkmanager.hpp:226
static Flag updateValue(T &oldVal, const T &newVal, Flag changed)
update a value in place and report whether it changed.
Definition netlinkmanager.hpp:196
void notifyRequest(uint32_t seq, int error=0)
notify a pending synchronous request.
Definition netlinkmanager.cpp:138
NetlinkManager(NetlinkManager &&)=delete
create instance by move.
Reactor * reactor() const noexcept
get the event loop reactor.
Definition netlinkmanager.cpp:52
Reactor * _reactor
event loop reactor.
Definition netlinkmanager.hpp:229
NetlinkManager & operator=(const NetlinkManager &)=delete
assign instance by copy.
void stop()
stop listening for netlink events.
Definition netlinkmanager.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 netlinkmanager.cpp:169
void start()
start listening for netlink events.
Definition netlinkmanager.cpp:61
std::unique_ptr< char[]> _buffer
internal read buffer.
Definition netlinkmanager.hpp:210
virtual ~NetlinkManager()=default
destroy instance.
virtual void onMessage(struct nlmsghdr *nlh)=0
dispatch a single RTM_* message to the derived class.
std::unordered_map< uint32_t, std::unique_ptr< PendingRequest > > _pending
synchronous requests indexed by sequence number.
Definition netlinkmanager.hpp:223
static int stopNestedAttributes(struct nlmsghdr *nlh, struct rtattr *nested)
close a nested attribute block.
Definition netlinkmanager.cpp:181
Reactor class.
Definition reactor.hpp:120
class owning a mutex for the duration of a scoped block.
Definition mutex.hpp:246
Definition acceptor.hpp:32
pending synchronous request.
Definition netlinkmanager.hpp:217
int error
Definition netlinkmanager.hpp:219
Condition cond
Definition netlinkmanager.hpp:218