25#ifndef JOIN_FABRIC_NETLINKMANAGER_HPP
26#define JOIN_FABRIC_NETLINKMANAGER_HPP
35#include <unordered_map>
41#include <linux/rtnetlink.h>
42#include <linux/netlink.h>
110 int sendRequest (struct nlmsghdr* nlh,
bool sync,
std::chrono::milliseconds timeout =
std::chrono::seconds (5));
117 template <typename Func, typename... Args>
118 void pushJob (Func&& func, Args&&... args) noexcept
121 job.
func = std::bind (std::forward<Func> (func), std::forward<Args> (args)...);
132 [[maybe_unused]] ssize_t bytes =
::write (
_wakeup, &v,
sizeof (v));
135 while (!job.
done.load (std::memory_order_acquire))
145 virtual void onReadable (
int fd)
override final;
167 static void addAttributes (
struct nlmsghdr* nlh,
int type,
const void* data,
int alen);
191 template <
typename T,
typename Flag>
192 static Flag
updateValue (T& oldVal,
const T& newVal, Flag changed)
194 if (oldVal != newVal)
199 return static_cast<Flag
> (0);
219 std::unordered_map<uint32_t, std::unique_ptr<PendingRequest>>
_pending;
adaptive backoff strategy for busy-wait loops.
Definition backoff.hpp:45
basic datagram socket class.
Definition socket.hpp:644
virtual int write(const char *data, unsigned long maxSize) noexcept override
write data.
Definition socket.hpp:925
queue base class.
Definition queue.hpp:127
int type() const noexcept
get the protocol communication semantic.
Definition socket.hpp:515
condition variable class.
Definition condition.hpp:42
Event handler interface class.
Definition reactor.hpp:46
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:52
Reactor & reactor() const noexcept
get the event loop reactor.
Definition netlinkmanager.cpp:62
static void addAttributes(struct nlmsghdr *nlh, int type, const void *data, int alen)
add an attribute to a netlink message.
Definition netlinkmanager.cpp:209
std::atomic< uint32_t > _seq
sequence number.
Definition netlinkmanager.hpp:209
NetlinkManager(uint32_t groups, Reactor &reactor=ReactorThread::reactor())
create instance.
Definition netlinkmanager.cpp:38
static constexpr size_t _bufferSize
internal buffer size.
Definition netlinkmanager.hpp:203
virtual void onReadable(int fd) override final
method called when data are ready to be read on handle.
Definition netlinkmanager.cpp:146
int _wakeup
eventfd used to wake the reactor thread for pending jobs.
Definition netlinkmanager.hpp:243
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:91
Mutex _syncMutex
protection mutex.
Definition netlinkmanager.hpp:222
void pushJob(Func &&func, Args &&... args) noexcept
push a job to be executed on the reactor thread.
Definition netlinkmanager.hpp:118
static Flag updateValue(T &oldVal, const T &newVal, Flag changed)
update a value in place and report whether it changed.
Definition netlinkmanager.hpp:192
void notifyRequest(uint32_t seq, int error=0)
notify a pending synchronous request.
Definition netlinkmanager.cpp:193
NetlinkManager(NetlinkManager &&)=delete
create instance by move.
Reactor & _reactor
event loop reactor.
Definition netlinkmanager.hpp:246
NetlinkManager & operator=(const NetlinkManager &)=delete
assign instance by copy.
void stop()
stop listening for netlink events.
Definition netlinkmanager.cpp:81
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:224
void start()
start listening for netlink events.
Definition netlinkmanager.cpp:71
std::unique_ptr< char[]> _buffer
internal read buffer.
Definition netlinkmanager.hpp:206
LocalMem::Mpsc::Queue< Job * > _jobs
job queue.
Definition netlinkmanager.hpp:240
virtual void onMessage(struct nlmsghdr *nlh)=0
dispatch a single RTM_* message to the derived class.
static constexpr size_t _jobQueueSize
job queue size.
Definition netlinkmanager.hpp:237
virtual ~NetlinkManager()
destroy instance.
Definition netlinkmanager.cpp:53
std::unordered_map< uint32_t, std::unique_ptr< PendingRequest > > _pending
synchronous requests indexed by sequence number.
Definition netlinkmanager.hpp:219
static int stopNestedAttributes(struct nlmsghdr *nlh, struct rtattr *nested)
close a nested attribute block.
Definition netlinkmanager.cpp:236
static Reactor & reactor()
get the global Reactor instance.
Definition reactor.cpp:514
Reactor class.
Definition reactor.hpp:129
bool isReactorThread() const noexcept
check if the calling thread is the reactor thread.
Definition reactor.cpp:265
Definition acceptor.hpp:32
job to be executed on the reactor thread.
Definition netlinkmanager.hpp:228
std::function< void()> func
function to execute.
Definition netlinkmanager.hpp:230
std::atomic< bool > done
set to true when the job has been executed.
Definition netlinkmanager.hpp:233
pending synchronous request.
Definition netlinkmanager.hpp:213
int error
Definition netlinkmanager.hpp:215
Condition cond
Definition netlinkmanager.hpp:214