Network operations to be performed on given container and interface operates on netlink device. More...
#include <network.hpp>
Public Member Functions | |
NetworkInterface (const std::string &ifname, pid_t pid=0) | |
Create network interface object for the ifname in the container (network namespace) More... | |
const std::string & | getName () const |
bool | exists () const noexcept |
NetStatus | status () const |
Retrieve network interface status (UP or DOWN) More... | |
void | create (InterfaceType type, const std::string &peerif="", MacVLanMode mode=MacVLanMode::PRIVATE) |
Create network interface in container identified by mContainerPid. More... | |
void | destroy () |
Delete interface. More... | |
void | moveToContainer (pid_t pid) |
Move interface to container. More... | |
void | renameFrom (const std::string &oldif) |
Rename interface name. More... | |
void | addToBridge (const std::string &bridge) |
Add interface to the bridge. More... | |
void | delFromBridge () |
Remove insterface from the bridge. More... | |
void | setAttrs (const Attrs &attrs) |
Set or get interface attributes in one netlink call. More... | |
Attrs | getAttrs () const |
void | addInetAddr (const InetAddr &addr) |
Add inet address to the interface. More... | |
void | delInetAddr (const InetAddr &addr) |
Remove inet address from the interface. More... | |
std::vector< InetAddr > | getInetAddressList () const |
Retrieve all inet addresses for the interface. More... | |
void | addRoute (const Route &route, const RoutingTable rt=RoutingTable::MAIN) |
Add route to specified routing table. More... | |
void | delRoute (const Route &route, const RoutingTable rt=RoutingTable::MAIN) |
Remove route from specified routing table. More... | |
std::vector< Route > | getRoutes (const RoutingTable rt=RoutingTable::MAIN) const |
Retrieve routing table for the interface. More... | |
void | up () |
Set interface up. More... | |
void | down () |
Set interface down. More... | |
void | setMACAddress (const std::string &macaddr) |
Set MAC address attribute. More... | |
void | setMTU (int mtu) |
Set MTU attribute. More... | |
void | setTxLength (int txlen) |
Set TxQ attribute. More... | |
Static Public Member Functions | |
static std::vector< std::string > | getInterfaces (pid_t initpid) |
Get list of network interafece names. More... | |
static std::vector< Route > | getRoutes (pid_t initpid, const RoutingTable rt=RoutingTable::MAIN) |
Get list of routes (specified routing table). More... | |
Private Member Functions | |
void | createVeth (const std::string &peerif) |
void | createBridge () |
void | createMacVLan (const std::string &masterif, MacVLanMode mode) |
void | modifyRoute (int cmd, const InetAddr &src, const InetAddr &dst) |
Private Attributes | |
const std::string | mIfname |
network interface name inside zone More... | |
pid_t | mContainerPid |
Container pid to operate on (0 means kernel) More... | |
Network operations to be performed on given container and interface operates on netlink device.
|
inline |
Create network interface object for the ifname in the container (network namespace)
void lxcpp::NetworkInterface::addInetAddr | ( | const InetAddr & | addr | ) |
Add inet address to the interface.
Equivalent to: ip addr add addr dev mIfname
void lxcpp::NetworkInterface::addRoute | ( | const Route & | route, |
const RoutingTable | rt = RoutingTable::MAIN |
||
) |
Add route to specified routing table.
Equivalent to: ip route add route.dst.addr/route.dst.prefix dev mIfname (if route.src.prefix=0)
void lxcpp::NetworkInterface::addToBridge | ( | const std::string & | bridge | ) |
Add interface to the bridge.
Equivalent to: ip link set mIfname master bridge
void lxcpp::NetworkInterface::create | ( | InterfaceType | type, |
const std::string & | peerif = "" , |
||
MacVLanMode | mode = MacVLanMode::PRIVATE |
||
) |
Create network interface in container identified by mContainerPid.
Equivalent to: ip link add mIfname type type [...]
Create pair of virtual ethernet interfaces:
Create bridge interface:
Create pseudo-ethernet interface on existing one:
|
private |
|
private |
|
private |
void lxcpp::NetworkInterface::delFromBridge | ( | ) |
Remove insterface from the bridge.
Equivalent to: ip link set mIfname nomaster
void lxcpp::NetworkInterface::delInetAddr | ( | const InetAddr & | addr | ) |
Remove inet address from the interface.
Equivalent to: ip addr del addr dev mIfname
void lxcpp::NetworkInterface::delRoute | ( | const Route & | route, |
const RoutingTable | rt = RoutingTable::MAIN |
||
) |
Remove route from specified routing table.
Equivalent to: ip route del route.dst.addr dev mIfname
void lxcpp::NetworkInterface::destroy | ( | ) |
Delete interface.
Equivalent to: ip link delete mIfname
void lxcpp::NetworkInterface::down | ( | ) |
Set interface down.
Equivalent to: ip link set mIfname down
|
noexcept |
Attrs lxcpp::NetworkInterface::getAttrs | ( | ) | const |
std::vector< InetAddr > lxcpp::NetworkInterface::getInetAddressList | ( | ) | const |
Retrieve all inet addresses for the interface.
Equivalent to: ip addr show, ip -6 addr show
|
static |
Get list of network interafece names.
Equivalent to: ip link show
|
inline |
std::vector< Route > lxcpp::NetworkInterface::getRoutes | ( | const RoutingTable | rt = RoutingTable::MAIN | ) | const |
Retrieve routing table for the interface.
Equivalent to: ip route show dev mIfname table rt
|
static |
Get list of routes (specified routing table).
Equivalent to: ip route show table rt
|
private |
void lxcpp::NetworkInterface::moveToContainer | ( | pid_t | pid | ) |
Move interface to container.
Equivalent to: ip link set dev mIfname netns pid
void lxcpp::NetworkInterface::renameFrom | ( | const std::string & | oldif | ) |
Rename interface name.
Equivalent to: ip link set dev oldif name mIfname
void lxcpp::NetworkInterface::setAttrs | ( | const Attrs & | attrs | ) |
Set or get interface attributes in one netlink call.
Supported attributes: see AttrName
void lxcpp::NetworkInterface::setMACAddress | ( | const std::string & | macaddr | ) |
Set MAC address attribute.
Equivalent to: ip link set mIfname address macaddr (macaddr in format AA:BB:CC:DD:FF:GG)
Note: two lower bits of first byte (leftmost) specifies MAC address class:
In most cases should be b2=0, b1=1, (see: https://en.wikipedia.org/wiki/MAC_address)
void lxcpp::NetworkInterface::setMTU | ( | int | mtu | ) |
Set MTU attribute.
Equivalent to: ip link set mIfname mtu mtu
void lxcpp::NetworkInterface::setTxLength | ( | int | txlen | ) |
Set TxQ attribute.
Equivalent to: ip link set mIfname txqueue txlen
NetStatus lxcpp::NetworkInterface::status | ( | ) | const |
Retrieve network interface status (UP or DOWN)
void lxcpp::NetworkInterface::up | ( | ) |
Set interface up.
Equivalent to: ip link set mIfname up
|
private |
Container pid to operate on (0 means kernel)
|
private |
network interface name inside zone