join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
routemanager.hpp
Go to the documentation of this file.
1
25#ifndef JOIN_FABRIC_ROUTEMANAGER_HPP
26#define JOIN_FABRIC_ROUTEMANAGER_HPP
27
28// libjoin.
30#include <join/route.hpp>
31
32// C++.
33#include <functional>
34
35namespace join
36{
40 enum class RouteChangeType : uint32_t
41 {
42 Added = 1L << 0,
43 Deleted = 1L << 1,
44 Modified = 1L << 2,
45 GatewayChanged = 1L << 3,
46 MetricChanged = 1L << 4,
47 TypeChanged = 1L << 5,
48 ScopeChanged = 1L << 6,
49 ProtocolChanged = 1L << 7,
50 };
51
59 {
60 return RouteChangeType (static_cast<uint32_t> (a) & static_cast<uint32_t> (b));
61 }
62
70 {
71 return RouteChangeType (static_cast<uint32_t> (a) | static_cast<uint32_t> (b));
72 }
73
81 {
82 return RouteChangeType (static_cast<uint32_t> (a) ^ static_cast<uint32_t> (b));
83 }
84
91 {
92 return RouteChangeType (~static_cast<uint32_t> (a));
93 }
94
102 {
103 return a = a & b;
104 }
105
113 {
114 return a = a | b;
115 }
116
124 {
125 return a = a ^ b;
126 }
127
136
141 {
142 public:
143 using RouteNotify = std::function<void (const RouteInfo& info)>;
144
149 explicit RouteManager (Reactor* reactor = nullptr);
150
154 RouteManager (const RouteManager&) = delete;
155
160
165
170
174 ~RouteManager ();
175
180 static RouteManager& instance ();
181
189 Route::Ptr findByIndex (uint32_t index, const IpAddress& dest, uint32_t prefix);
190
198 Route::Ptr findByName (const std::string& interfaceName, const IpAddress& dest, uint32_t prefix);
199
205
211 RouteList enumerate (uint32_t index);
212
218 RouteList enumerate (const std::string& interfaceName);
219
224 int refresh ();
225
231 uint64_t addRouteListener (const RouteNotify& cb);
232
237 void removeRouteListener (uint64_t id);
238
249 int addRoute (uint32_t index, const IpAddress& dest, uint32_t prefix, const IpAddress& gateway = {},
250 uint32_t metric = 0, bool sync = false);
251
262 int addRoute (const std::string& interfaceName, const IpAddress& dest, uint32_t prefix,
263 const IpAddress& gateway = {}, uint32_t metric = 0, bool sync = false);
264
273 int removeRoute (uint32_t index, const IpAddress& dest, uint32_t prefix, bool sync = false);
274
283 int removeRoute (const std::string& interfaceName, const IpAddress& dest, uint32_t prefix, bool sync = false);
284
291 int flushRoutes (uint32_t index, bool sync = false);
292
299 int flushRoutes (const std::string& interfaceName, bool sync = false);
300
301 private:
312 int setRoute (uint32_t index, const IpAddress& dest, uint32_t prefix, const IpAddress& gateway, uint32_t metric,
313 bool sync);
314
320 int dumpRoutes (bool sync = false);
321
326 void onMessage (struct nlmsghdr* nlh) override;
327
332 void onRouteMessage (struct nlmsghdr* nlh);
333
338 void notifyRouteUpdate (const RouteInfo& info);
339
348 Route::Ptr acquire (uint32_t index, const IpAddress& dest, uint32_t prefix, RouteInfo& info);
349
351 std::unordered_map<RouteKey, Route::Ptr> _routes;
352
354 Mutex _routeMutex;
355
357 std::unordered_map<uint64_t, RouteNotify> _routeListeners;
358
360 std::atomic<uint64_t> _listenerCounter{0};
361
362 // friendship with route.
363 friend class Route;
364 };
365}
366
367#endif
IPv6, IPv4 address class.
Definition ipaddress.hpp:51
base class for netlink-based managers.
Definition netlinkmanager.hpp:51
Reactor * reactor() const noexcept
get the event loop reactor.
Definition netlinkmanager.cpp:66
Reactor class.
Definition reactor.hpp:120
kernel routing table manager class.
Definition routemanager.hpp:141
uint64_t addRouteListener(const RouteNotify &cb)
register a callback invoked on any route table change.
Definition routemanager.cpp:159
int removeRoute(uint32_t index, const IpAddress &dest, uint32_t prefix, bool sync=false)
remove a route entry from the kernel table.
Definition routemanager.cpp:241
std::function< void(const RouteInfo &info)> RouteNotify
Definition routemanager.hpp:143
RouteManager & operator=(const RouteManager &)=delete
assign instance by copy.
int flushRoutes(uint32_t index, bool sync=false)
flush all route entries for a given interface.
Definition routemanager.cpp:297
RouteList enumerate()
enumerate all cached route entries.
Definition routemanager.cpp:96
static RouteManager & instance()
get the singleton instance.
Definition routemanager.cpp:60
RouteManager(Reactor *reactor=nullptr)
create instance.
Definition routemanager.cpp:40
int refresh()
refresh all data.
Definition routemanager.cpp:145
Route::Ptr findByIndex(uint32_t index, const IpAddress &dest, uint32_t prefix)
find a route entry by interface index, destination and prefix.
Definition routemanager.cpp:70
void removeRouteListener(uint64_t id)
unregister a previously registered callback.
Definition routemanager.cpp:174
RouteManager(RouteManager &&)=delete
create instance by move.
RouteManager(const RouteManager &)=delete
create instance by copy.
int addRoute(uint32_t index, const IpAddress &dest, uint32_t prefix, const IpAddress &gateway={}, uint32_t metric=0, bool sync=false)
add a route entry in the kernel table.
Definition routemanager.cpp:185
Route::Ptr findByName(const std::string &interfaceName, const IpAddress &dest, uint32_t prefix)
find a route entry by interface name, destination and prefix.
Definition routemanager.cpp:87
~RouteManager()
destroy instance.
Definition routemanager.cpp:51
class representing a single kernel routing table entry.
Definition route.hpp:94
std::shared_ptr< Route > Ptr
Definition route.hpp:104
Definition acceptor.hpp:32
IpAddress operator^(const IpAddress &a, const IpAddress &b)
perform XOR operation on IP address.
Definition ipaddress.cpp:1719
constexpr const JsonReadMode & operator&=(JsonReadMode &a, JsonReadMode b) noexcept
perform binary AND on JsonReadMode.
Definition json.hpp:916
IpAddress operator&(const IpAddress &a, const IpAddress &b)
perform AND operation on IP address.
Definition ipaddress.cpp:1665
InterfaceChangeType operator~(InterfaceChangeType a)
perform binary NOT on InterfaceChangeType.
Definition interfacemanager.hpp:95
RouteChangeType
enumeration of route change types.
Definition routemanager.hpp:41
std::vector< Route::Ptr > RouteList
list of routes.
Definition route.hpp:341
constexpr const JsonReadMode & operator|=(JsonReadMode &a, JsonReadMode b) noexcept
perform binary OR on JsonReadMode.
Definition json.hpp:927
const InterfaceChangeType & operator^=(InterfaceChangeType &a, InterfaceChangeType b)
perform binary XOR assignment on InterfaceChangeType.
Definition interfacemanager.hpp:128
IpAddress operator|(const IpAddress &a, const IpAddress &b)
perform OR operation on IP address.
Definition ipaddress.cpp:1692
route change notification payload.
Definition routemanager.hpp:132
Route::Ptr route
Definition routemanager.hpp:133
RouteChangeType flags
Definition routemanager.hpp:134