join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
interfacemanager.hpp
Go to the documentation of this file.
1
25#ifndef JOIN_FABRIC_INTERFACEMANAGER_HPP
26#define JOIN_FABRIC_INTERFACEMANAGER_HPP
27
28// libjoin.
30#include <join/macaddress.hpp>
31#include <join/ipaddress.hpp>
32#include <join/interface.hpp>
33
34// C++.
35#include <functional>
36#include <string>
37
38namespace join
39{
43 enum class InterfaceChangeType : uint32_t
44 {
45 Added = 1L << 0,
46 Deleted = 1L << 1,
47 Modified = 1L << 2,
48 AdminStateChanged = 1L << 3,
49 OperStateChanged = 1L << 4,
50 MacChanged = 1L << 5,
51 NameChanged = 1L << 6,
52 MtuChanged = 1L << 7,
53 KindChanged = 1L << 8,
54 MasterChanged = 1L << 9,
55 };
56
64 {
65 return InterfaceChangeType (static_cast<uint32_t> (a) & static_cast<uint32_t> (b));
66 }
67
75 {
76 return InterfaceChangeType (static_cast<uint32_t> (a) | static_cast<uint32_t> (b));
77 }
78
86 {
87 return InterfaceChangeType (static_cast<uint32_t> (a) ^ static_cast<uint32_t> (b));
88 }
89
96 {
97 return InterfaceChangeType (~static_cast<uint32_t> (a));
98 }
99
107 {
108 return a = a & b;
109 }
110
118 {
119 return a = a | b;
120 }
121
129 {
130 return a = a ^ b;
131 }
132
141
145 struct AddressInfo : public LinkInfo
146 {
148 };
149
153 struct RouteInfo : public LinkInfo
154 {
156 };
157
162 {
163 public:
164 using LinkNotify = std::function<void (const LinkInfo& info)>;
165 using AddressNotify = std::function<void (const AddressInfo& info)>;
166 using RouteNotify = std::function<void (const RouteInfo& info)>;
167
172 InterfaceManager (Reactor* reactor = nullptr);
173
178 InterfaceManager (const InterfaceManager& other) = delete;
179
185
192
199
204
209 static InterfaceManager& instance ();
210
216 Interface::Ptr findByIndex (uint32_t interfaceIndex);
217
223 Interface::Ptr findByName (const std::string& interfaceName);
224
230
235 int refresh ();
236
242 uint64_t addLinkListener (const LinkNotify& cb);
243
248 void removeLinkListener (uint64_t id);
249
255 uint64_t addAddressListener (const AddressNotify& cb);
256
261 void removeAddressListener (uint64_t id);
262
268 uint64_t addRouteListener (const RouteNotify& cb);
269
274 void removeRouteListener (uint64_t id);
275
282 int createDummyInterface (const std::string& interfaceName, bool sync = false);
283
290 int createBridgeInterface (const std::string& interfaceName, bool sync = false);
291
301 int createVlanInterface (const std::string& interfaceName, uint32_t parentIndex, uint16_t id,
302 uint16_t proto = ETH_P_8021Q, bool sync = false);
303
313 int createVlanInterface (const std::string& interfaceName, const std::string& parentName, uint16_t id,
314 uint16_t proto = ETH_P_8021Q, bool sync = false);
315
324 int createVethInterface (const std::string& hostName, const std::string& peerName, pid_t* pid = nullptr,
325 bool sync = false);
326
339 int createGreInterface (const std::string& tunnelName, uint32_t parentIndex, const IpAddress& localAddress,
340 const IpAddress& remoteAddress, const uint32_t* ikey = nullptr,
341 const uint32_t* okey = nullptr, uint8_t ttl = 64, bool sync = false);
342
355 int createGreInterface (const std::string& tunnelName, const std::string& parentName,
356 const IpAddress& localAddress, const IpAddress& remoteAddress,
357 const uint32_t* ikey = nullptr, const uint32_t* okey = nullptr, uint8_t ttl = 64,
358 bool sync = false);
359
366 int removeInterface (uint32_t interfaceIndex, bool sync = false);
367
374 int removeInterface (const std::string& interfaceName, bool sync = false);
375
376 private:
382 static void addPeerInfoData (struct nlmsghdr* nlh, const std::string& peerName);
383
391 int mtu (uint32_t interfaceIndex, uint32_t mtuBytes, bool sync = false);
392
400 int mac (uint32_t interfaceIndex, const MacAddress& macAddress, bool sync = false);
401
409 int addToBridge (uint32_t interfaceIndex, uint32_t masterIndex, bool sync = false);
410
417 int removeFromBridge (uint32_t interfaceIndex, bool sync = false);
418
426 int enable (uint32_t interfaceIndex, bool enabled = true, bool sync = false);
427
437 int addAddress (uint32_t interfaceIndex, const IpAddress& ipAddress, uint32_t prefix,
438 const IpAddress& broadcast = {}, bool sync = false);
439
449 int removeAddress (uint32_t interfaceIndex, const IpAddress& ipAddress, uint32_t prefix,
450 const IpAddress& broadcast = {}, bool sync = false);
451
462 int addRoute (uint32_t interfaceIndex, const IpAddress& dest, uint32_t prefix, const IpAddress& gateway,
463 uint32_t* metric = nullptr, bool sync = false);
464
475 int removeRoute (uint32_t interfaceIndex, const IpAddress& dest, uint32_t prefix, const IpAddress& gateway,
476 uint32_t* metric = nullptr, bool sync = false);
477
483 int dumpLink (bool sync = false);
484
490 int dumpAddress (bool sync = false);
491
497 int dumpRoute (bool sync = false);
498
503 void onMessage (struct nlmsghdr* nlh) override;
504
509 void onLinkMessage (struct nlmsghdr* nlh);
510
516 void onLinkInfoMessage (Interface::Ptr& iface, struct rtattr* rta, InterfaceChangeType& flags);
517
522 void onAddressMessage (struct nlmsghdr* nlh);
523
528 void onRouteMessage (struct nlmsghdr* nlh);
529
534 void notifyLinkUpdate (const LinkInfo& info);
535
540 void notifyAddressUpdate (const AddressInfo& info);
541
546 void notifyRouteUpdate (const RouteInfo& info);
547
553 Interface::Ptr acquire (uint32_t index, LinkInfo& info);
554
556 static constexpr uint16_t reservedVlanId = 0;
557
559 static constexpr uint16_t maxVlanId = 4094;
560
562 std::unordered_map<uint32_t, Interface::Ptr> _interfaces;
563
565 Mutex _ifMutex;
566
568 std::unordered_map<uint64_t, LinkNotify> _linkListeners;
569
571 std::unordered_map<uint64_t, AddressNotify> _addressListeners;
572
574 std::unordered_map<uint64_t, RouteNotify> _routeListeners;
575
577 uint64_t _listenerCounter = 0;
578
580 Mutex _listenerMutex;
581
582 // friendship with interface.
583 friend class Interface;
584 };
585}
586
587#endif
int ttl() const
returns the Time-To-Live value.
Definition socket.hpp:1112
int mtu() const
get socket mtu.
Definition socket.hpp:1074
interface manager class.
Definition interfacemanager.hpp:162
InterfaceManager(Reactor *reactor=nullptr)
create instance.
Definition interfacemanager.cpp:41
int refresh()
refresh all data.
Definition interfacemanager.cpp:117
Interface::Ptr findByName(const std::string &interfaceName)
find interface by name.
Definition interfacemanager.cpp:89
int createVethInterface(const std::string &hostName, const std::string &peerName, pid_t *pid=nullptr, bool sync=false)
creates a Virtual Ethernet nterface pair.
Definition interfacemanager.cpp:332
std::function< void(const AddressInfo &info)> AddressNotify
Definition interfacemanager.hpp:165
int removeInterface(uint32_t interfaceIndex, bool sync=false)
deletes the specified network interface.
Definition interfacemanager.cpp:495
uint64_t addAddressListener(const AddressNotify &cb)
registers a callback to be invoked when a address update occurs.
Definition interfacemanager.cpp:151
void removeLinkListener(uint64_t id)
unregisters a previously registered link update callback.
Definition interfacemanager.cpp:141
Interface::Ptr findByIndex(uint32_t interfaceIndex)
find interface by index.
Definition interfacemanager.cpp:72
std::function< void(const LinkInfo &info)> LinkNotify
Definition interfacemanager.hpp:164
InterfaceList enumerate()
enumerate all interfaces.
Definition interfacemanager.cpp:98
int createBridgeInterface(const std::string &interfaceName, bool sync=false)
creates a bridge interface.
Definition interfacemanager.cpp:227
void removeAddressListener(uint64_t id)
unregisters a previously registered address update callback.
Definition interfacemanager.cpp:161
uint64_t addRouteListener(const RouteNotify &cb)
registers a callback to be invoked when a route update occurs.
Definition interfacemanager.cpp:171
static InterfaceManager & instance()
get the a singleton instance.
Definition interfacemanager.cpp:62
void removeRouteListener(uint64_t id)
unregisters a previously registered route update callback.
Definition interfacemanager.cpp:181
int createVlanInterface(const std::string &interfaceName, uint32_t parentIndex, uint16_t id, uint16_t proto=ETH_P_8021Q, bool sync=false)
creates a VLAN interface.
Definition interfacemanager.cpp:263
InterfaceManager(const InterfaceManager &other)=delete
create instance by copy.
uint64_t addLinkListener(const LinkNotify &cb)
registers a callback to be invoked when a link update occurs.
Definition interfacemanager.cpp:131
int createGreInterface(const std::string &tunnelName, uint32_t parentIndex, const IpAddress &localAddress, const IpAddress &remoteAddress, const uint32_t *ikey=nullptr, const uint32_t *okey=nullptr, uint8_t ttl=64, bool sync=false)
creates a a GRE tunnel interface.
Definition interfacemanager.cpp:384
std::function< void(const RouteInfo &info)> RouteNotify
Definition interfacemanager.hpp:166
int createDummyInterface(const std::string &interfaceName, bool sync=false)
creates a dummy interface.
Definition interfacemanager.cpp:191
~InterfaceManager()
destroy instance.
Definition interfacemanager.cpp:53
InterfaceManager(InterfaceManager &&other)=delete
create instance by move.
InterfaceManager & operator=(const InterfaceManager &other)=delete
assign instance by copy.
interface class.
Definition interface.hpp:50
std::shared_ptr< Interface > Ptr
Definition interface.hpp:60
std::tuple< IpAddress, uint32_t, IpAddress, uint32_t > Route
Definition interface.hpp:63
std::tuple< IpAddress, uint32_t, IpAddress > Address
Definition interface.hpp:61
IPv6, IPv4 address class.
Definition ipaddress.hpp:51
MAC address class.
Definition macaddress.hpp:46
base class for netlink-based managers.
Definition netlinkmanager.hpp:49
Reactor * reactor() const noexcept
get the event loop reactor.
Definition netlinkmanager.cpp:52
Reactor class.
Definition reactor.hpp:120
Definition acceptor.hpp:32
std::vector< Interface::Ptr > InterfaceList
list of interfaces.
Definition interface.hpp:452
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
enumeration of interface change type.
Definition interfacemanager.hpp:44
InterfaceChangeType operator~(InterfaceChangeType a)
perform binary NOT on InterfaceChangeType.
Definition interfacemanager.hpp:95
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 on InterfaceChangeType.
Definition interfacemanager.hpp:128
IpAddress operator|(const IpAddress &a, const IpAddress &b)
perform OR operation on IP address.
Definition ipaddress.cpp:1692
address information.
Definition interfacemanager.hpp:146
Interface::Address address
Definition interfacemanager.hpp:147
link information.
Definition interfacemanager.hpp:137
InterfaceChangeType flags
Definition interfacemanager.hpp:139
Interface::Ptr interface
Definition interfacemanager.hpp:138
route information.
Definition interfacemanager.hpp:154
Interface::Route route
Definition interfacemanager.hpp:155