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
154 {
155 public:
156 using LinkNotify = std::function<void (const LinkInfo& info)>;
157 using AddressNotify = std::function<void (const AddressInfo& info)>;
158
163 InterfaceManager (Reactor* reactor = nullptr);
164
169 InterfaceManager (const InterfaceManager& other) = delete;
170
176
183
190
195
200 static InterfaceManager& instance ();
201
207 Interface::Ptr findByIndex (uint32_t interfaceIndex);
208
214 Interface::Ptr findByName (const std::string& interfaceName);
215
221
226 int refresh ();
227
233 uint64_t addLinkListener (const LinkNotify& cb);
234
239 void removeLinkListener (uint64_t id);
240
246 uint64_t addAddressListener (const AddressNotify& cb);
247
252 void removeAddressListener (uint64_t id);
253
260 int createDummyInterface (const std::string& interfaceName, bool sync = false);
261
268 int createBridgeInterface (const std::string& interfaceName, bool sync = false);
269
279 int createVlanInterface (const std::string& interfaceName, uint32_t parentIndex, uint16_t id,
280 uint16_t proto = ETH_P_8021Q, bool sync = false);
281
291 int createVlanInterface (const std::string& interfaceName, const std::string& parentName, uint16_t id,
292 uint16_t proto = ETH_P_8021Q, bool sync = false);
293
302 int createVethInterface (const std::string& hostName, const std::string& peerName, pid_t* pid = nullptr,
303 bool sync = false);
304
317 int createGreInterface (const std::string& tunnelName, uint32_t parentIndex, const IpAddress& localAddress,
318 const IpAddress& remoteAddress, const uint32_t* ikey = nullptr,
319 const uint32_t* okey = nullptr, uint8_t ttl = 64, bool sync = false);
320
333 int createGreInterface (const std::string& tunnelName, const std::string& parentName,
334 const IpAddress& localAddress, const IpAddress& remoteAddress,
335 const uint32_t* ikey = nullptr, const uint32_t* okey = nullptr, uint8_t ttl = 64,
336 bool sync = false);
337
344 int removeInterface (uint32_t interfaceIndex, bool sync = false);
345
352 int removeInterface (const std::string& interfaceName, bool sync = false);
353
354 private:
360 static void addPeerInfoData (struct nlmsghdr* nlh, const std::string& peerName);
361
369 int mtu (uint32_t interfaceIndex, uint32_t mtuBytes, bool sync = false);
370
378 int mac (uint32_t interfaceIndex, const MacAddress& macAddress, bool sync = false);
379
387 int addToBridge (uint32_t interfaceIndex, uint32_t masterIndex, bool sync = false);
388
395 int removeFromBridge (uint32_t interfaceIndex, bool sync = false);
396
404 int enable (uint32_t interfaceIndex, bool enabled = true, bool sync = false);
405
415 int addAddress (uint32_t interfaceIndex, const IpAddress& ipAddress, uint32_t prefix,
416 const IpAddress& broadcast = {}, bool sync = false);
417
427 int removeAddress (uint32_t interfaceIndex, const IpAddress& ipAddress, uint32_t prefix,
428 const IpAddress& broadcast = {}, bool sync = false);
429
435 int dumpLink (bool sync = false);
436
442 int dumpAddress (bool sync = false);
443
448 void onMessage (struct nlmsghdr* nlh) override;
449
454 void onLinkMessage (struct nlmsghdr* nlh);
455
462 void onLinkInfoMessage (Interface::Ptr& iface, struct rtattr* rta, InterfaceChangeType& flags);
463
468 void onAddressMessage (struct nlmsghdr* nlh);
469
474 void notifyLinkUpdate (const LinkInfo& info);
475
480 void notifyAddressUpdate (const AddressInfo& info);
481
488 Interface::Ptr acquire (uint32_t index, LinkInfo& info);
489
491 static constexpr uint16_t reservedVlanId = 0;
492
494 static constexpr uint16_t maxVlanId = 4094;
495
497 std::unordered_map<uint32_t, Interface::Ptr> _interfaces;
498
500 Mutex _ifMutex;
501
503 std::unordered_map<uint64_t, LinkNotify> _linkListeners;
504
506 std::unordered_map<uint64_t, AddressNotify> _addressListeners;
507
509 std::atomic<uint64_t> _listenerCounter{0};
510
511 // friendship with interface.
512 friend class Interface;
513 };
514}
515
516#endif
int mtu() const
get socket mtu.
Definition socket.hpp:1074
int ttl() const noexcept
returns the Time-To-Live value.
Definition socket.hpp:1112
interface manager class.
Definition interfacemanager.hpp:154
InterfaceManager(Reactor *reactor=nullptr)
create instance.
Definition interfacemanager.cpp:41
int refresh()
refresh all data.
Definition interfacemanager.cpp:116
Interface::Ptr findByName(const std::string &interfaceName)
find interface by name.
Definition interfacemanager.cpp:88
int createVethInterface(const std::string &hostName, const std::string &peerName, pid_t *pid=nullptr, bool sync=false)
creates a Virtual Ethernet interface pair.
Definition interfacemanager.cpp:323
std::function< void(const AddressInfo &info)> AddressNotify
Definition interfacemanager.hpp:157
int removeInterface(uint32_t interfaceIndex, bool sync=false)
deletes the specified network interface.
Definition interfacemanager.cpp:486
uint64_t addAddressListener(const AddressNotify &cb)
register a callback to be invoked when an address update occurs.
Definition interfacemanager.cpp:156
void removeLinkListener(uint64_t id)
unregister a previously registered link update callback.
Definition interfacemanager.cpp:145
Interface::Ptr findByIndex(uint32_t interfaceIndex)
find interface by index.
Definition interfacemanager.cpp:71
std::function< void(const LinkInfo &info)> LinkNotify
Definition interfacemanager.hpp:156
InterfaceList enumerate()
enumerate all interfaces.
Definition interfacemanager.cpp:97
int createBridgeInterface(const std::string &interfaceName, bool sync=false)
creates a bridge interface.
Definition interfacemanager.cpp:218
void removeAddressListener(uint64_t id)
unregister a previously registered address update callback.
Definition interfacemanager.cpp:171
static InterfaceManager & instance()
get the singleton instance.
Definition interfacemanager.cpp:61
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:254
InterfaceManager(const InterfaceManager &other)=delete
create instance by copy.
uint64_t addLinkListener(const LinkNotify &cb)
register a callback to be invoked when a link update occurs.
Definition interfacemanager.cpp:130
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 GRE tunnel interface.
Definition interfacemanager.cpp:375
int createDummyInterface(const std::string &interfaceName, bool sync=false)
creates a dummy interface.
Definition interfacemanager.cpp:182
~InterfaceManager()
destroy instance.
Definition interfacemanager.cpp:52
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
IPv6, IPv4 address class.
Definition ipaddress.hpp:51
MAC address class.
Definition macaddress.hpp:46
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
Definition acceptor.hpp:32
std::vector< Interface::Ptr > InterfaceList
list of interfaces.
Definition interface.hpp:394
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 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
address change notification payload.
Definition interfacemanager.hpp:146
Interface::Address address
Definition interfacemanager.hpp:147
IP address entry associated with an interface.
Definition interface.hpp:66
link change notification payload.
Definition interfacemanager.hpp:137
InterfaceChangeType flags
Definition interfacemanager.hpp:139
Interface::Ptr interface
Definition interfacemanager.hpp:138