Dobby 3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
|
Basic wrapper around the libnl netlink library. More...
#include <Netlink.h>
Classes | |
struct | BridgePortDetails |
Public Member Functions | |
bool | isValid () const |
bool | ifaceUp (const std::string &ifaceName) |
Brings an interface up. | |
bool | ifaceDown (const std::string &ifaceName) |
Takes an interface down. | |
bool | ifaceIsUp (const std::string &ifaceName) const |
Queries the interface to determine if it's up or not. | |
bool | ifaceExists (const std::string &ifaceName) const |
Checks if an interface with a given name exists (interface could be either up or down) | |
bool | setIfaceAddress (const std::string &ifaceName, const in_addr_t address, const in_addr_t netmask) |
Sets the ip address and netmask of an interface (IPv4) | |
bool | setIfaceAddress (const std::string &ifaceName, const struct in6_addr address, const int netmask) |
Sets the ip address and netmask of an interface (IPv6) | |
bool | setIfaceForwarding (const std::string &ifaceName, bool enable) |
Enables or disables IPv4 forwarding on the given interface. | |
bool | setIfaceForwarding6 (const std::shared_ptr< DobbyRdkPluginUtils > &utils, const std::string &ifaceName, bool enable) |
Enables or disables IPv6 forwarding on the given interface. | |
bool | setIfaceRouteLocalNet (const std::string &ifaceName, bool enable) |
Sets the route_localnet flag on the interface. | |
bool | setIfaceAcceptRa (const std::shared_ptr< DobbyRdkPluginUtils > &utils, const std::string &ifaceName, int value) |
Sets the accept_ra flag on the interface. | |
bool | setIfaceMAC (const std::string &ifaceName, const std::array< uint8_t, 6 > &address) |
Sets the MAC address of the given interface. | |
std::array< uint8_t, 6 > | getIfaceMAC (const std::string &ifaceName) |
Gets the MAC address of the given interface. | |
std::list< BridgePortDetails > | getAttachedIfaces (const std::string &bridgeName) |
Gets the set of interfaces currently enslaved to a given bridge device. | |
bool | createBridge (const std::string &bridgeName) |
Creates a new bridge device. | |
bool | destroyBridge (const std::string &bridgeName) |
Deletes an existing bridge device. | |
bool | addIfaceToBridge (const std::string &bridgeName, const std::string &ifaceName) |
Adds an interface to the bridge device. | |
bool | delIfaceFromBridge (const std::string &bridgeName, const std::string &ifaceName) |
Removes an interface from a bridge. | |
std::string | createVeth (const std::string &peerVethName, pid_t peerPid, std::vector< std::string > &takenVeths) |
Creates a veth pair for the netns attached to the given pid. | |
bool | checkVeth (const std::string &vethName) |
bool | addRoute (const std::string &iface, const in_addr_t destination, const in_addr_t netmask, const in_addr_t gateway) |
Adds a new route to the routing table (IPv4) | |
bool | addRoute (const std::string &iface, const struct in6_addr destination, const int netmask, const struct in6_addr gateway) |
Adds a new route to the routing table (IPv6) | |
bool | addArpEntry (const std::string &iface, in_addr_t address, const std::array< uint8_t, 6 > &mac) |
Sets an entry in the ARP table. | |
bool | delArpEntry (const std::string &iface, in_addr_t address) |
Removes (invalidates) an entry in the ARP table. | |
Private Member Functions | |
bool | applyChangesToLink (const std::string &ifaceName, const NlLink &changes) |
bool | setLinkAddress (const NlLink &link, const in_addr_t address, const in_addr_t netmask) |
Sets the ip address and netmask of an interface (IPv4) | |
bool | setLinkAddress (const NlLink &link, const struct in6_addr address, const int netmask) |
Sets the ip address and netmask of an interface (IPv6) | |
bool | setIfaceConfig (const std::string &ifaceName, const unsigned int configId, const uint32_t value) |
Sets the config on a given interface. | |
std::string | getAvailableVethName (const int startIndex) const |
Returns the number of the next free veth device. | |
Private Attributes | |
struct nl_sock * | mSocket |
int | mSysClassNetDirFd |
std::mutex | mLock |
Basic wrapper around the libnl netlink library.
There is only expected to be one of these objects (i.e. a shared_ptr is passed around). The object represents a single netlink socket.
At construction time a new netlink socket is opened, on destruction it is closed.
bool Netlink::addArpEntry | ( | const std::string & | iface, |
in_addr_t | address, | ||
const std::array< uint8_t, 6 > & | mac | ||
) |
Sets an entry in the ARP table.
This is equivalent of the performing the following on the command line
arp -i <iface> -s <address> <mac>
[in] | iface | The name of the iface containing the ARP cache |
[in] | address | The destination ip address |
[in] | mac | The hardware address of the destination |
bool Netlink::addIfaceToBridge | ( | const std::string & | bridgeName, |
const std::string & | ifaceName | ||
) |
Adds an interface to the bridge device.
This is equivalent of the performing the following on the command line
brctl addif <bridgeName> <ifaceName>
bool Netlink::addRoute | ( | const std::string & | iface, |
const in_addr_t | destination, | ||
const in_addr_t | netmask, | ||
const in_addr_t | gateway | ||
) |
Adds a new route to the routing table (IPv4)
This is equivalent of the performing the following on the command line
ip route add <destination>/<netmask> via <gateway> dev <ifname>
[in] | iface | The name of the iface to route to |
[in] | destination | The destination ip address |
[in] | netmask | The netmask for the destination ip address |
[in] | gateway | The ip address of the gateway |
bool Netlink::addRoute | ( | const std::string & | iface, |
const struct in6_addr | destination, | ||
const int | netmask, | ||
const struct in6_addr | gateway | ||
) |
Adds a new route to the routing table (IPv6)
This is equivalent of the performing the following on the command line
ip -6 route add <destination>/<netmask> via <gateway> dev <ifname>
[in] | iface | The name of the iface to route to |
[in] | destination | The destination ip address |
[in] | netmask | The netmask for the destination ip address |
[in] | gateway | The ip address of the gateway, if null, we don't set a gateway for the route. |
bool Netlink::createBridge | ( | const std::string & | bridgeName | ) |
Creates a new bridge device.
Note that it is not an error if a bridge device already exists with the same name, this method will return true in that case.
This is equivalent of the performing the following on the command line
brctl addbr <bridgeName>
[in] | bridgeName | The name of the new bridge device. |
std::string Netlink::createVeth | ( | const std::string & | peerVethName, |
pid_t | peerPid, | ||
std::vector< std::string > & | takenVeths | ||
) |
Creates a veth pair for the netns attached to the given pid.
[in] | peerVethName | The name of the veth interface inside the net namespace (container), typically this will be "eth0". |
[in] | peerPid | The pid of the process which has the netns we want to create the veth in (i.e. the pid of init process within the container). |
[in] | takenVeths | Veth devices reserved by other containers. We want to check that in case of races. |
bool Netlink::delArpEntry | ( | const std::string & | iface, |
in_addr_t | address | ||
) |
Removes (invalidates) an entry in the ARP table.
This is equivalent of the performing the following on the command line
arp -i <iface> -d <address>
[in] | iface | The name of the iface containing the ARP cache |
[in] | address | The destination ip address |
bool Netlink::delIfaceFromBridge | ( | const std::string & | bridgeName, |
const std::string & | ifaceName | ||
) |
Removes an interface from a bridge.
This is equivalent of the performing the following on the command line
brctl delif <bridgeName> <ifaceName>
bool Netlink::destroyBridge | ( | const std::string & | bridgeName | ) |
Deletes an existing bridge device.
This is equivalent of the performing the following on the command line
brctl delbr <bridgeName>
[in] | bridgeName | The name of the new bridge to delete. |
std::list< Netlink::BridgePortDetails > Netlink::getAttachedIfaces | ( | const std::string & | bridgeName | ) |
Gets the set of interfaces currently enslaved to a given bridge device.
[in] | bridgeName | The name of the bridge device |
|
private |
Returns the number of the next free veth device.
This works by scanning /sys/class/net/ for devices with names "veth%d", the first one not found is returned.
[in] | startIndex | Starting index for veth names |
std::array< uint8_t, 6 > Netlink::getIfaceMAC | ( | const std::string & | ifaceName | ) |
Gets the MAC address of the given interface.
[in] | ifaceName | The name of the interface to get. |
bool Netlink::ifaceDown | ( | const std::string & | ifaceName | ) |
Takes an interface down.
[in] | ifaceName | The name of the interface to take down. |
bool Netlink::ifaceExists | ( | const std::string & | ifaceName | ) | const |
Checks if an interface with a given name exists (interface could be either up or down)
[in] | ifaceName | The name of the interface to check |
bool Netlink::ifaceIsUp | ( | const std::string & | ifaceName | ) | const |
Queries the interface to determine if it's up or not.
[in] | ifaceName | The name of the interface to check |
bool Netlink::ifaceUp | ( | const std::string & | ifaceName | ) |
Brings an interface up.
[in] | ifaceName | The name of the interface to bring up. |
bool Netlink::setIfaceAcceptRa | ( | const std::shared_ptr< DobbyRdkPluginUtils > & | utils, |
const std::string & | ifaceName, | ||
int | value | ||
) |
Sets the accept_ra flag on the interface.
This is the equivalent of the following on the command line
echo "2" > /proc/sys/net/ipv6/conf/<ifaceName>/accept_ra
Or echo "1" > /proc/sys/net/ipv6/conf/<ifaceName>/accept_ra Or echo "0" > /proc/sys/net/ipv6/conf/<ifaceName>/accept_ra
This is used to set accept_ra to "2" so that router advertisements are accepted on the interface even with forwarding enabled.
[in] | utils | Instance of the DobbyRdkPluginUtils class. |
[in] | ifaceName | The name of the interface to set the config on. |
[in] | enable | true to enable, false to disable. |
bool Netlink::setIfaceAddress | ( | const std::string & | ifaceName, |
const in_addr_t | address, | ||
const in_addr_t | netmask | ||
) |
Sets the ip address and netmask of an interface (IPv4)
This is the equivalent of the following on the command line
ifconfig <ifaceName> <address> netmask <netmask>
[in] | ifaceName | The name of the interface to set the address on |
[in] | address | The address to set, the netmask will be applied to this before setting on the iface |
[in] | netmask | The netmask to apply. |
bool Netlink::setIfaceAddress | ( | const std::string & | ifaceName, |
const struct in6_addr | address, | ||
const int | netmask | ||
) |
Sets the ip address and netmask of an interface (IPv6)
This is the equivalent of the following on the command line
ifconfig <ifaceName> inet6 add <address>/<netmask>
[in] | ifaceName | The name of the interface to set the address on |
[in] | address | The IPv6 address to set, the netmask will be applied to this before setting on the iface |
[in] | netmask | The netmask to apply. |
|
private |
Sets the config on a given interface.
Used by setIfaceForwarding() and setIfaceRouteLocalNet().
[in] | ifaceName | The name of the interface to set the address on |
[in] | configId | The config to set. |
[in] | value | The value to set. |
bool Netlink::setIfaceForwarding | ( | const std::string & | ifaceName, |
bool | enable | ||
) |
Enables or disables IPv4 forwarding on the given interface.
This is the equivalent of the following on the command line
echo "1" > /proc/sys/net/ipv4/conf/<ifaceName>/forwarding
Or echo "0" > /proc/sys/net/ipv4/conf/<ifaceName>/forwarding
[in] | ifaceName | The name of the interface to set the config on |
[in] | enable | true to enable, false to disable. |
bool Netlink::setIfaceForwarding6 | ( | const std::shared_ptr< DobbyRdkPluginUtils > & | utils, |
const std::string & | ifaceName, | ||
bool | enable | ||
) |
Enables or disables IPv6 forwarding on the given interface.
This is the equivalent of the following on the command line
echo "1" > /proc/sys/net/ipv6/conf/<ifaceName>/forwarding
Or echo "0" > /proc/sys/net/ipv6/conf/<ifaceName>/forwarding
[in] | utils | Instance of the DobbyRdkPluginUtils class. |
[in] | ifaceName | The name of the interface to set the config on. |
[in] | enable | true to enable, false to disable. |
bool Netlink::setIfaceMAC | ( | const std::string & | ifaceName, |
const std::array< uint8_t, 6 > & | address | ||
) |
Sets the MAC address of the given interface.
This is primarily used to set a fixed MAC address for the bridge device.
[in] | ifaceName | The name of the interface to set. |
[in] | address | The MAC address to set. |
bool Netlink::setIfaceRouteLocalNet | ( | const std::string & | ifaceName, |
bool | enable | ||
) |
Sets the route_localnet flag on the interface.
Which means: "Do not consider loopback addresses as martian source or destination while routing. This enables the use of 127/8 for local routing purposes. default FALSE"
This is the equivalent of the following on the command line
echo "1" > /proc/sys/net/ipv4/conf/<ifaceName>/route_localnet
Or echo "0" > /proc/sys/net/ipv4/conf/<ifaceName>/route_localnet
This is used so we can use iptables to route packets on the bridge interface to local host. The main usage is for connecting specific ports like dns, as to the localhost interface.
[in] | ifaceName | The name of the interface to set the config on. |
[in] | enable | true to enable, false to disable. |
|
private |
Sets the ip address and netmask of an interface (IPv4)
This is the equivalent of the following on the command line
ifconfig <ifaceName> <address> netmask <netmask>
[in] | link | Instance of NlLink (rtnl_link wrapper) |
[in] | address | The address to set, the netmask will be applied to this before setting on the iface |
[in] | netmask | The netmask to apply. |
|
private |
Sets the ip address and netmask of an interface (IPv6)
This is the equivalent of the following on the command line
ifconfig <ifaceName> inet6 add <address>/<netmask>
[in] | link | Instance of NlLink (rtnl_link wrapper) |
[in] | address | The address to set, the netmask will be applied to this before setting on the iface |
[in] | netmask | The netmask to apply. |