25#ifndef __JOIN_INTERFACE_MANAGER_HPP__
26#define __JOIN_INTERFACE_MANAGER_HPP__
43#include <linux/rtnetlink.h>
44#include <linux/netlink.h>
74 {
return ChangeType (
static_cast <int> (__a) &
static_cast <int> (__b)); }
83 {
return ChangeType (
static_cast <int> (__a) |
static_cast <int> (__b)); }
92 {
return ChangeType (
static_cast <int> (__a) ^
static_cast <int> (__b)); }
100 {
return ChangeType (~
static_cast <int> (__a)); }
109 {
return __a = __a & __b; }
118 {
return __a = __a | __b; }
127 {
return __a = __a ^ __b; }
232 int refresh (
bool sync =
true);
295 int createVlanInterface (
const std::string& interfaceName, uint32_t parentIndex, uint16_t
id, uint16_t proto = ETH_P_8021Q,
bool sync =
false);
306 int createVlanInterface (
const std::string& interfaceName,
const std::string& parentName, uint16_t
id, uint16_t proto = ETH_P_8021Q,
bool sync =
false);
316 int createVethInterface (
const std::string& hostName,
const std::string& peerName, pid_t* pid =
nullptr,
bool sync =
false);
332 const uint32_t* ikey =
nullptr,
const uint32_t* okey =
nullptr, uint8_t
ttl = 64,
bool sync =
false);
348 const uint32_t* ikey =
nullptr,
const uint32_t* okey =
nullptr, uint8_t
ttl = 64,
bool sync =
false);
364 int removeInterface (
const std::string& interfaceName,
bool sync =
false);
374 template <
typename T>
377 if (oldVal != newVal)
392 void addAttributes (
struct nlmsghdr *nlh,
int type,
const void *data,
int alen);
399 struct rtattr* startNestedAttributes (
struct nlmsghdr *nlh,
int type);
406 int stopNestedAttributes (
struct nlmsghdr *nlh,
struct rtattr *nested);
413 void addPeerInfoData (
struct nlmsghdr *nlh,
const std::string& peerName);
422 int mtu (uint32_t interfaceIndex, uint32_t mtuBytes,
bool sync =
false);
431 int mac (uint32_t interfaceIndex,
const MacAddress& macAddress,
bool sync =
false);
440 int addToBridge (uint32_t interfaceIndex, uint32_t masterIndex,
bool sync =
false);
448 int removeFromBridge (uint32_t interfaceIndex,
bool sync =
false);
457 int enable (uint32_t interfaceIndex,
bool enabled =
true,
bool sync =
false);
468 int addAddress (uint32_t interfaceIndex,
const IpAddress& ipAddress, uint32_t prefix,
const IpAddress& broadcast = {},
bool sync =
false);
479 int removeAddress (uint32_t interfaceIndex,
const IpAddress& ipAddress, uint32_t prefix,
const IpAddress& broadcast = {},
bool sync =
false);
491 int addRoute (uint32_t interfaceIndex,
const IpAddress& dest, uint32_t prefix,
const IpAddress& gateway, uint32_t* metric =
nullptr,
bool sync =
false);
503 int removeRoute (uint32_t interfaceIndex,
const IpAddress& dest, uint32_t prefix,
const IpAddress& gateway, uint32_t* metric =
nullptr,
bool sync =
false);
510 int dumpLink (
bool sync =
false);
517 int dumpAddress (
bool sync =
false);
524 int dumpRoute (
bool sync =
false);
532 int sendRequest (
struct nlmsghdr* nlh,
bool sync);
541 int waitResponse (ScopedLock <Mutex>& lock, uint32_t seq, uint32_t timeout = 5000);
546 virtual void onReceive ()
override;
552 void onLinkMessage (
struct nlmsghdr* nlh);
565 void onAddressMessage (
struct nlmsghdr* nlh);
571 void onRouteMessage (
struct nlmsghdr* nlh);
578 void notifyRequest (uint32_t seq,
int error = 0);
584 void notifyLinkUpdate (
const LinkInfo& info);
590 void notifyAddressUpdate (
const AddressInfo& info);
596 void notifyRouteUpdate (
const RouteInfo& info);
605 static constexpr size_t _bufferSize = 4096;
608 std::unique_ptr <char []> _buffer;
611 static constexpr uint16_t reservedVlanId = 0;
614 static constexpr uint16_t maxVlanId = 4094;
617 std::map <uint32_t, Interface::Ptr> _interfaces;
623 std::atomic<uint32_t> _seq;
628 struct PendingRequest
635 std::map <uint32_t, std::shared_ptr <PendingRequest>> _pending;
641 std::vector <LinkNotify> _linkListeners;
647 std::vector <AddressNotify> _addressListeners;
653 std::vector <RouteNotify> _routeListeners;
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