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
88 void start ();
89
93 void stop ();
94
99 Reactor& reactor () const noexcept;
100
101 protected:
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 std::unique_ptr<char[]> _buffer;
186
188 std::atomic<uint32_t> _seq;
189
192 {
194 std::error_code error;
195 bool done = false;
196 };
197
199 std::unordered_map<uint32_t, std::unique_ptr<PendingRequest>> _pending;
200
203
206 };
207}
208
209#endif
condition variable class.
Definition condition.hpp:42
Event handler interface class.
Definition reactor.hpp:51
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:240
std::atomic< uint32_t > _seq
sequence number.
Definition netlink_manager.hpp:188
NetlinkManager(uint32_t groups, Reactor &reactor=ReactorThread::reactor())
create instance.
Definition netlink_manager.cpp:38
virtual void onReadable(int fd) override final
method called when data are ready to be read on handle.
Definition netlink_manager.cpp:169
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:202
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:205
Reactor & _reactor
event loop reactor.
Definition netlink_manager.hpp:205
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:258
void notifyAllRequests(const std::error_code &error)
notify every pending synchronous requests with an error.
Definition netlink_manager.cpp:222
void start()
start listening for netlink events.
Definition netlink_manager.cpp:61
std::unique_ptr< char[]> _buffer
internal read buffer.
Definition netlink_manager.hpp:185
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:199
static int stopNestedAttributes(struct nlmsghdr *nlh, struct rtattr *nested)
close a nested attribute block.
Definition netlink_manager.cpp:270
static Reactor & reactor()
get the global Reactor instance.
Definition reactor.cpp:612
Reactor class.
Definition reactor.hpp:159
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:192
bool done
Definition netlink_manager.hpp:195
std::error_code error
Definition netlink_manager.hpp:194
Condition cond
Definition netlink_manager.hpp:193