join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
interfacemanager.hpp
Go to the documentation of this file.
1
25#ifndef __JOIN_INTERFACE_MANAGER_HPP__
26#define __JOIN_INTERFACE_MANAGER_HPP__
27
28// libjoin.
29#include <join/macaddress.hpp>
30#include <join/ipaddress.hpp>
31#include <join/condition.hpp>
32#include <join/interface.hpp>
33#include <join/socket.hpp>
34
35// C++.
36#include <functional>
37#include <string>
38#include <memory>
39#include <atomic>
40#include <map>
41
42// C.
43#include <linux/rtnetlink.h>
44#include <linux/netlink.h>
45#include <cstdint>
46#include <cstddef>
47
48namespace join
49{
54 {
55 Added = 1L << 0,
56 Deleted = 1L << 1,
57 Modified = 1L << 2,
60 MacChanged = 1L << 5,
61 NameChanged = 1L << 6,
62 MtuChanged = 1L << 7,
63 KindChanged = 1L << 8,
64 MasterChanged = 1L << 9,
65 };
66
74 { return ChangeType (static_cast <int> (__a) & static_cast <int> (__b)); }
75
83 { return ChangeType (static_cast <int> (__a) | static_cast <int> (__b)); }
84
92 { return ChangeType (static_cast <int> (__a) ^ static_cast <int> (__b)); }
93
100 { return ChangeType (~static_cast <int> (__a)); }
101
108 __inline__ const ChangeType& operator&= (ChangeType& __a, ChangeType __b)
109 { return __a = __a & __b; }
110
117 __inline__ const ChangeType& operator|= (ChangeType& __a, ChangeType __b)
118 { return __a = __a | __b; }
119
126 __inline__ const ChangeType& operator^= (ChangeType& __a, ChangeType __b)
127 { return __a = __a ^ __b; }
128
132 struct LinkInfo
133 {
134 uint32_t index;
136 };
137
141 struct AddressInfo : public LinkInfo
142 {
144 };
145
149 struct RouteInfo : public LinkInfo
150 {
152 };
153
158 {
159 private:
164
165 public:
166 using LinkNotify = std::function <void (const LinkInfo& info)>;
167 using AddressNotify = std::function <void (const AddressInfo& info)>;
168 using RouteNotify = std::function <void (const RouteInfo& info)>;
169
174 InterfaceManager (const InterfaceManager& other) = delete;
175
181
188
195
200
205 static InterfaceManager* instance ();
206
212 Interface::Ptr findByIndex (uint32_t interfaceIndex);
213
219 Interface::Ptr findByName (const std::string& interfaceName);
220
226
232 int refresh (bool sync = true);
233
238 void addLinkListener (const LinkNotify& cb);
239
244 void removeLinkListener (const LinkNotify& cb);
245
250 void addAddressListener (const AddressNotify& cb);
251
256 void removeAddressListener (const AddressNotify& cb);
257
262 void addRouteListener (const RouteNotify& cb);
263
268 void removeRouteListener (const RouteNotify& cb);
269
276 int createDummyInterface (const std::string& interfaceName, bool sync = false);
277
284 int createBridgeInterface (const std::string& interfaceName, bool sync = false);
285
295 int createVlanInterface (const std::string& interfaceName, uint32_t parentIndex, uint16_t id, uint16_t proto = ETH_P_8021Q, bool sync = false);
296
306 int createVlanInterface (const std::string& interfaceName, const std::string& parentName, uint16_t id, uint16_t proto = ETH_P_8021Q, bool sync = false);
307
316 int createVethInterface (const std::string& hostName, const std::string& peerName, pid_t* pid = nullptr, bool sync = false);
317
330 int createGreInterface (const std::string& tunnelName, uint32_t parentIndex,
331 const IpAddress& localAddress, const IpAddress& remoteAddress,
332 const uint32_t* ikey = nullptr, const uint32_t* okey = nullptr, uint8_t ttl = 64, bool sync = false);
333
346 int createGreInterface (const std::string& tunnelName, const std::string& parentName,
347 const IpAddress& localAddress, const IpAddress& remoteAddress,
348 const uint32_t* ikey = nullptr, const uint32_t* okey = nullptr, uint8_t ttl = 64, bool sync = false);
349
356 int removeInterface (uint32_t interfaceIndex, bool sync = false);
357
364 int removeInterface (const std::string& interfaceName, bool sync = false);
365
366 private:
374 template <typename T>
375 ChangeType updateValue (T& oldVal, const T& newVal, ChangeType changed) const
376 {
377 if (oldVal != newVal)
378 {
379 oldVal = newVal;
380 return changed;
381 }
382 return static_cast <ChangeType> (0);
383 }
384
392 void addAttributes (struct nlmsghdr *nlh, int type, const void *data, int alen);
393
399 struct rtattr* startNestedAttributes (struct nlmsghdr *nlh, int type);
400
406 int stopNestedAttributes (struct nlmsghdr *nlh, struct rtattr *nested);
407
413 void addPeerInfoData (struct nlmsghdr *nlh, const std::string& peerName);
414
422 int mtu (uint32_t interfaceIndex, uint32_t mtuBytes, bool sync = false);
423
431 int mac (uint32_t interfaceIndex, const MacAddress& macAddress, bool sync = false);
432
440 int addToBridge (uint32_t interfaceIndex, uint32_t masterIndex, bool sync = false);
441
448 int removeFromBridge (uint32_t interfaceIndex, bool sync = false);
449
457 int enable (uint32_t interfaceIndex, bool enabled = true, bool sync = false);
458
468 int addAddress (uint32_t interfaceIndex, const IpAddress& ipAddress, uint32_t prefix, const IpAddress& broadcast = {}, bool sync = false);
469
479 int removeAddress (uint32_t interfaceIndex, const IpAddress& ipAddress, uint32_t prefix, const IpAddress& broadcast = {}, bool sync = false);
480
491 int addRoute (uint32_t interfaceIndex, const IpAddress& dest, uint32_t prefix, const IpAddress& gateway, uint32_t* metric = nullptr, bool sync = false);
492
503 int removeRoute (uint32_t interfaceIndex, const IpAddress& dest, uint32_t prefix, const IpAddress& gateway, uint32_t* metric = nullptr, bool sync = false);
504
510 int dumpLink (bool sync = false);
511
517 int dumpAddress (bool sync = false);
518
524 int dumpRoute (bool sync = false);
525
532 int sendRequest (struct nlmsghdr* nlh, bool sync);
533
541 int waitResponse (ScopedLock <Mutex>& lock, uint32_t seq, uint32_t timeout = 5000);
542
546 virtual void onReceive () override;
547
552 void onLinkMessage (struct nlmsghdr* nlh);
553
559 void onLinkInfoMessage (Interface::Ptr& iface, struct rtattr* rta, ChangeType& flags);
560
565 void onAddressMessage (struct nlmsghdr* nlh);
566
571 void onRouteMessage (struct nlmsghdr* nlh);
572
578 void notifyRequest (uint32_t seq, int error = 0);
579
584 void notifyLinkUpdate (const LinkInfo& info);
585
590 void notifyAddressUpdate (const AddressInfo& info);
591
596 void notifyRouteUpdate (const RouteInfo& info);
597
602 Interface::Ptr acquire (LinkInfo& info);
603
605 static constexpr size_t _bufferSize = 4096;
606
608 std::unique_ptr <char []> _buffer;
609
611 static constexpr uint16_t reservedVlanId = 0;
612
614 static constexpr uint16_t maxVlanId = 4094;
615
617 std::map <uint32_t, Interface::Ptr> _interfaces;
618
620 Mutex _ifMutex;
621
623 std::atomic<uint32_t> _seq;
624
628 struct PendingRequest
629 {
630 Condition cond;
631 int error = 0;
632 };
633
635 std::map <uint32_t, std::shared_ptr <PendingRequest>> _pending;
636
638 Mutex _syncMutex;
639
641 std::vector <LinkNotify> _linkListeners;
642
644 Mutex _linkMutex;
645
647 std::vector <AddressNotify> _addressListeners;
648
650 Mutex _addressMutex;
651
653 std::vector <RouteNotify> _routeListeners;
654
656 Mutex _routeMutex;
657
658 // friendship with interface.
659 friend class Interface;
660 };
661}
662
663#endif
basic datagram socket class.
Definition socket.hpp:645
int ttl() const
returns the Time-To-Live value.
Definition socket.hpp:1109
int mtu() const
get socket mtu.
Definition socket.hpp:1071
int type() const noexcept
get the protocol communication semantic.
Definition socket.hpp:516
interface manager class.
Definition interfacemanager.hpp:158
std::function< void(const LinkInfo &info)> LinkNotify
Definition interfacemanager.hpp:166
Interface::Ptr findByName(const std::string &interfaceName)
find interface by name.
Definition interfacemanager.cpp:103
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:356
int removeInterface(uint32_t interfaceIndex, bool sync=false)
deletes the specified network interface.
Definition interfacemanager.cpp:519
void addLinkListener(const LinkNotify &cb)
registers a callback to be invoked when a link update occurs.
Definition interfacemanager.cpp:139
Interface::Ptr findByIndex(uint32_t interfaceIndex)
find interface by index.
Definition interfacemanager.cpp:86
InterfaceList enumerate()
enumerate all interfaces.
Definition interfacemanager.cpp:112
int createBridgeInterface(const std::string &interfaceName, bool sync=false)
creates a bridge interface.
Definition interfacemanager.cpp:251
std::function< void(const RouteInfo &info)> RouteNotify
Definition interfacemanager.hpp:168
void addAddressListener(const AddressNotify &cb)
registers a callback to be invoked when a address update occurs.
Definition interfacemanager.cpp:164
void addRouteListener(const RouteNotify &cb)
registers a callback to be invoked when a route update occurs.
Definition interfacemanager.cpp:189
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:288
std::function< void(const AddressInfo &info)> AddressNotify
Definition interfacemanager.hpp:167
void removeAddressListener(const AddressNotify &cb)
unregisters a previously registered address update callback.
Definition interfacemanager.cpp:174
InterfaceManager(const InterfaceManager &other)=delete
create instance by copy.
void removeLinkListener(const LinkNotify &cb)
unregisters a previously registered link update callback.
Definition interfacemanager.cpp:149
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:408
static InterfaceManager * instance()
create the InterfaceManager instance.
Definition interfacemanager.cpp:70
int refresh(bool sync=true)
refresh all data by dumping link, address, and route data.
Definition interfacemanager.cpp:130
int createDummyInterface(const std::string &interfaceName, bool sync=false)
creates a dummy interface.
Definition interfacemanager.cpp:214
~InterfaceManager()
destroy instance.
Definition interfacemanager.cpp:60
InterfaceManager(InterfaceManager &&other)=delete
create instance by move.
InterfaceManager & operator=(const InterfaceManager &other)=delete
assign instance by copy.
void removeRouteListener(const RouteNotify &cb)
unregisters a previously registered route update callback.
Definition interfacemanager.cpp:199
interface class.
Definition interface.hpp:51
std::shared_ptr< Interface > Ptr
Definition interface.hpp:61
std::tuple< IpAddress, uint32_t, IpAddress > Address
Definition interface.hpp:62
std::tuple< IpAddress, uint32_t, IpAddress, uint32_t > Route
Definition interface.hpp:64
IPv6, IPv4 address class.
Definition ipaddress.hpp:51
Definition acceptor.hpp:32
IpAddress operator^(const IpAddress &a, const IpAddress &b)
perform XOR operation on IP address.
Definition ipaddress.cpp:1720
std::set< Interface::Ptr > InterfaceList
list of interfaces.
Definition interface.hpp:449
constexpr const JsonReadMode & operator&=(JsonReadMode &a, JsonReadMode b) noexcept
perform binary AND on JsonReadMode.
Definition json.hpp:902
ChangeType
enumeration of interface change type.
Definition interfacemanager.hpp:54
@ KindChanged
Definition interfacemanager.hpp:63
@ OperStateChanged
Definition interfacemanager.hpp:59
@ NameChanged
Definition interfacemanager.hpp:61
@ MtuChanged
Definition interfacemanager.hpp:62
@ Deleted
Definition interfacemanager.hpp:56
@ Modified
Definition interfacemanager.hpp:57
@ Added
Definition interfacemanager.hpp:55
@ MasterChanged
Definition interfacemanager.hpp:64
@ MacChanged
Definition interfacemanager.hpp:60
@ AdminStateChanged
Definition interfacemanager.hpp:58
IpAddress operator&(const IpAddress &a, const IpAddress &b)
perform AND operation on IP address.
Definition ipaddress.cpp:1666
constexpr const JsonReadMode & operator|=(JsonReadMode &a, JsonReadMode b) noexcept
perform binary OR on JsonReadMode.
Definition json.hpp:911
__inline__ ChangeType operator~(ChangeType __a)
perform binary NOT on ChangeType.
Definition interfacemanager.hpp:99
__inline__ const ChangeType & operator^=(ChangeType &__a, ChangeType __b)
perform binary XOR on ChangeType.
Definition interfacemanager.hpp:126
IpAddress operator|(const IpAddress &a, const IpAddress &b)
perform OR operation on IP address.
Definition ipaddress.cpp:1693
address information.
Definition interfacemanager.hpp:142
Interface::Address address
Definition interfacemanager.hpp:143
link information.
Definition interfacemanager.hpp:133
ChangeType flags
Definition interfacemanager.hpp:135
uint32_t index
Definition interfacemanager.hpp:134
route information.
Definition interfacemanager.hpp:150
Interface::Route route
Definition interfacemanager.hpp:151