join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
netlink_manager.hpp
Go to the documentation of this file.
1
25#ifndef JOIN_FABRIC_NETLINK_MANAGER_HPP
26#define JOIN_FABRIC_NETLINK_MANAGER_HPP
27
28// libjoin.
31#include <join/condition.hpp>
32#include <join/function.hpp>
33#include <join/reactor.hpp>
34
35// C++.
36#include <unordered_map>
37#include <system_error>
38#include <memory>
39#include <atomic>
40
41// C.
42#include <cstdint>
43#include <cstddef>
44
45namespace join
46{
51 {
52 public:
59
63 NetlinkManager (const NetlinkManager&) = delete;
64
69
74
79
83 virtual ~NetlinkManager () = default;
84
89 Reactor& reactor () const noexcept;
90
91 protected:
95 void start ();
96
100 void stop ();
101
109 int sendRequest (struct nlmsghdr* nlh, bool sync, std::chrono::milliseconds timeout = std::chrono::seconds (5));
110
115 virtual void onReadable (int fd) override final;
116
121 virtual void onMessage (struct nlmsghdr* nlh) = 0;
122
128 void notifyRequest (uint32_t seq, const std::error_code& error = std::error_code ());
129
134 void notifyAllRequests (const std::error_code& error);
135
143 static void addAttributes (struct nlmsghdr* nlh, int type, const void* data, int alen);
144
151 static struct rtattr* startNestedAttributes (struct nlmsghdr* nlh, int type);
152
159 static int stopNestedAttributes (struct nlmsghdr* nlh, struct rtattr* nested);
160
167 template <typename T, typename Flag>
168 static Flag updateValue (T& oldVal, const T& newVal, Flag changed)
169 {
170 if (oldVal != newVal)
171 {
172 oldVal = newVal;
173 return changed;
174 }
175 return static_cast<Flag> (0);
176 }
177
180
182 static constexpr int _rcvBufferSize = 1 << 20;
183
185 static constexpr size_t _bufferSize = 16384;
186
188 std::unique_ptr<char[]> _buffer;
189
191 std::atomic<uint32_t> _seq;
192
195 {
197 std::error_code error;
198 bool done = false;
199 };
200
202 std::unordered_map<uint32_t, std::unique_ptr<PendingRequest>> _pending;
203
206
209 };
210}
211
212#endif
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
Definition error.hpp:144
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