2014-06-24 07:23:51 +08:00
|
|
|
// +build linux
|
|
|
|
|
2014-02-19 08:56:11 +08:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2014-06-24 07:23:51 +08:00
|
|
|
|
|
|
|
"github.com/docker/libcontainer/netlink"
|
2014-02-19 08:56:11 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func InterfaceUp(name string) error {
|
|
|
|
iface, err := net.InterfaceByName(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return netlink.NetworkLinkUp(iface)
|
|
|
|
}
|
|
|
|
|
|
|
|
func InterfaceDown(name string) error {
|
|
|
|
iface, err := net.InterfaceByName(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return netlink.NetworkLinkDown(iface)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChangeInterfaceName(old, newName string) error {
|
|
|
|
iface, err := net.InterfaceByName(old)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return netlink.NetworkChangeName(iface, newName)
|
|
|
|
}
|
|
|
|
|
2014-09-17 17:02:27 +08:00
|
|
|
func CreateVethPair(name1, name2 string, txQueueLen int) error {
|
|
|
|
return netlink.NetworkCreateVethPair(name1, name2, txQueueLen)
|
2014-02-19 08:56:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func SetInterfaceInNamespacePid(name string, nsPid int) error {
|
|
|
|
iface, err := net.InterfaceByName(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return netlink.NetworkSetNsPid(iface, nsPid)
|
|
|
|
}
|
|
|
|
|
2014-08-12 09:56:58 +08:00
|
|
|
func SetInterfaceInNamespaceFd(name string, fd uintptr) error {
|
|
|
|
iface, err := net.InterfaceByName(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return netlink.NetworkSetNsFd(iface, int(fd))
|
|
|
|
}
|
|
|
|
|
2014-02-19 08:56:11 +08:00
|
|
|
func SetInterfaceMaster(name, master string) error {
|
|
|
|
iface, err := net.InterfaceByName(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
masterIface, err := net.InterfaceByName(master)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-04-09 21:41:14 +08:00
|
|
|
return netlink.AddToBridge(iface, masterIface)
|
2014-02-19 08:56:11 +08:00
|
|
|
}
|
|
|
|
|
2014-05-17 15:06:29 +08:00
|
|
|
func SetDefaultGateway(ip, ifaceName string) error {
|
|
|
|
return netlink.AddDefaultGw(ip, ifaceName)
|
2014-02-19 08:56:11 +08:00
|
|
|
}
|
|
|
|
|
2014-09-30 05:51:53 +08:00
|
|
|
func SetInterfaceMac(name string, macaddr string) error {
|
|
|
|
iface, err := net.InterfaceByName(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return netlink.NetworkSetMacAddress(iface, macaddr)
|
|
|
|
}
|
|
|
|
|
2014-02-19 08:56:11 +08:00
|
|
|
func SetInterfaceIp(name string, rawIp string) error {
|
|
|
|
iface, err := net.InterfaceByName(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ip, ipNet, err := net.ParseCIDR(rawIp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return netlink.NetworkLinkAddIp(iface, ip, ipNet)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetMtu(name string, mtu int) error {
|
|
|
|
iface, err := net.InterfaceByName(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return netlink.NetworkSetMTU(iface, mtu)
|
|
|
|
}
|
2014-07-02 07:56:00 +08:00
|
|
|
|
|
|
|
func SetHairpinMode(name string, enabled bool) error {
|
|
|
|
iface, err := net.InterfaceByName(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return netlink.SetHairpinMode(iface, enabled)
|
|
|
|
}
|