2015-09-02 19:42:01 -04:00
|
|
|
package osl
|
2015-06-12 16:51:57 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2015-06-30 17:16:16 -04:00
|
|
|
"regexp"
|
2015-06-12 16:51:57 -04:00
|
|
|
"sync"
|
2016-01-08 16:56:01 -05:00
|
|
|
"syscall"
|
2016-06-22 12:40:20 -04:00
|
|
|
"time"
|
2015-06-12 16:51:57 -04:00
|
|
|
|
2016-11-09 21:01:07 -05:00
|
|
|
"github.com/Sirupsen/logrus"
|
2016-06-07 17:28:28 -04:00
|
|
|
"github.com/docker/libnetwork/ns"
|
2015-06-12 16:51:57 -04:00
|
|
|
"github.com/docker/libnetwork/types"
|
|
|
|
"github.com/vishvananda/netlink"
|
2016-06-07 17:28:28 -04:00
|
|
|
"github.com/vishvananda/netns"
|
2015-06-12 16:51:57 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// IfaceOption is a function option type to set interface options
|
|
|
|
type IfaceOption func(i *nwIface)
|
|
|
|
|
|
|
|
type nwIface struct {
|
|
|
|
srcName string
|
|
|
|
dstName string
|
|
|
|
master string
|
|
|
|
dstMaster string
|
2016-03-08 21:47:02 -05:00
|
|
|
mac net.HardwareAddr
|
2015-06-12 16:51:57 -04:00
|
|
|
address *net.IPNet
|
|
|
|
addressIPv6 *net.IPNet
|
2016-11-21 14:44:58 -05:00
|
|
|
ipAliases []*net.IPNet
|
2016-06-07 17:28:28 -04:00
|
|
|
llAddrs []*net.IPNet
|
2015-06-12 16:51:57 -04:00
|
|
|
routes []*net.IPNet
|
|
|
|
bridge bool
|
|
|
|
ns *networkNamespace
|
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *nwIface) SrcName() string {
|
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
|
|
|
|
|
|
|
return i.srcName
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *nwIface) DstName() string {
|
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
|
|
|
|
|
|
|
return i.dstName
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *nwIface) DstMaster() string {
|
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
|
|
|
|
|
|
|
return i.dstMaster
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *nwIface) Bridge() bool {
|
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
|
|
|
|
|
|
|
return i.bridge
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *nwIface) Master() string {
|
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
|
|
|
|
|
|
|
return i.master
|
|
|
|
}
|
|
|
|
|
2016-03-08 21:47:02 -05:00
|
|
|
func (i *nwIface) MacAddress() net.HardwareAddr {
|
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
|
|
|
|
|
|
|
return types.GetMacCopy(i.mac)
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:51:57 -04:00
|
|
|
func (i *nwIface) Address() *net.IPNet {
|
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
|
|
|
|
|
|
|
return types.GetIPNetCopy(i.address)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *nwIface) AddressIPv6() *net.IPNet {
|
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
|
|
|
|
|
|
|
return types.GetIPNetCopy(i.addressIPv6)
|
|
|
|
}
|
|
|
|
|
2016-06-07 17:28:28 -04:00
|
|
|
func (i *nwIface) LinkLocalAddresses() []*net.IPNet {
|
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
|
|
|
|
|
|
|
return i.llAddrs
|
|
|
|
}
|
|
|
|
|
2016-11-21 14:44:58 -05:00
|
|
|
func (i *nwIface) IPAliases() []*net.IPNet {
|
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
|
|
|
|
|
|
|
return i.ipAliases
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:51:57 -04:00
|
|
|
func (i *nwIface) Routes() []*net.IPNet {
|
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
|
|
|
|
|
|
|
routes := make([]*net.IPNet, len(i.routes))
|
|
|
|
for index, route := range i.routes {
|
|
|
|
r := types.GetIPNetCopy(route)
|
|
|
|
routes[index] = r
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *networkNamespace) Interfaces() []Interface {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
ifaces := make([]Interface, len(n.iFaces))
|
|
|
|
|
|
|
|
for i, iface := range n.iFaces {
|
|
|
|
ifaces[i] = iface
|
|
|
|
}
|
|
|
|
|
|
|
|
return ifaces
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *nwIface) Remove() error {
|
|
|
|
i.Lock()
|
|
|
|
n := i.ns
|
|
|
|
i.Unlock()
|
|
|
|
|
|
|
|
n.Lock()
|
2015-12-18 14:03:57 -05:00
|
|
|
isDefault := n.isDefault
|
2016-06-07 17:28:28 -04:00
|
|
|
nlh := n.nlHandle
|
2015-06-12 16:51:57 -04:00
|
|
|
n.Unlock()
|
|
|
|
|
2016-08-11 02:31:01 -04:00
|
|
|
// Find the network interface identified by the DstName attribute.
|
2016-06-07 17:28:28 -04:00
|
|
|
iface, err := nlh.LinkByName(i.DstName())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-12 16:51:57 -04:00
|
|
|
|
2016-06-07 17:28:28 -04:00
|
|
|
// Down the interface before configuring
|
|
|
|
if err := nlh.LinkSetDown(iface); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-12 16:51:57 -04:00
|
|
|
|
2016-06-07 17:28:28 -04:00
|
|
|
err = nlh.LinkSetName(iface, i.SrcName())
|
|
|
|
if err != nil {
|
2016-11-09 21:01:07 -05:00
|
|
|
logrus.Debugf("LinkSetName failed for interface %s: %v", i.SrcName(), err)
|
2016-06-07 17:28:28 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-06-12 16:51:57 -04:00
|
|
|
|
2016-06-07 17:28:28 -04:00
|
|
|
// if it is a bridge just delete it.
|
|
|
|
if i.Bridge() {
|
|
|
|
if err := nlh.LinkDel(iface); err != nil {
|
|
|
|
return fmt.Errorf("failed deleting bridge %q: %v", i.SrcName(), err)
|
2015-06-12 16:51:57 -04:00
|
|
|
}
|
2016-06-07 17:28:28 -04:00
|
|
|
} else if !isDefault {
|
|
|
|
// Move the network interface to caller namespace.
|
|
|
|
if err := nlh.LinkSetNsFd(iface, ns.ParseHandlerInt()); err != nil {
|
2016-11-09 21:01:07 -05:00
|
|
|
logrus.Debugf("LinkSetNsPid failed for interface %s: %v", i.SrcName(), err)
|
2016-06-07 17:28:28 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-06-12 16:51:57 -04:00
|
|
|
|
2016-06-07 17:28:28 -04:00
|
|
|
n.Lock()
|
|
|
|
for index, intf := range n.iFaces {
|
|
|
|
if intf == i {
|
|
|
|
n.iFaces = append(n.iFaces[:index], n.iFaces[index+1:]...)
|
|
|
|
break
|
2015-06-12 16:51:57 -04:00
|
|
|
}
|
2016-06-07 17:28:28 -04:00
|
|
|
}
|
|
|
|
n.Unlock()
|
2015-06-12 16:51:57 -04:00
|
|
|
|
2016-06-07 17:28:28 -04:00
|
|
|
return nil
|
2015-06-12 16:51:57 -04:00
|
|
|
}
|
|
|
|
|
2015-06-30 17:16:16 -04:00
|
|
|
// Returns the sandbox's side veth interface statistics
|
2015-10-04 23:02:02 -04:00
|
|
|
func (i *nwIface) Statistics() (*types.InterfaceStatistics, error) {
|
2015-06-30 17:16:16 -04:00
|
|
|
i.Lock()
|
|
|
|
n := i.ns
|
|
|
|
i.Unlock()
|
|
|
|
|
2016-06-07 17:28:28 -04:00
|
|
|
l, err := n.nlHandle.LinkByName(i.DstName())
|
2015-06-30 17:16:16 -04:00
|
|
|
if err != nil {
|
2016-06-07 17:28:28 -04:00
|
|
|
return nil, fmt.Errorf("failed to retrieve the statistics for %s in netns %s: %v", i.DstName(), n.path, err)
|
2015-06-30 17:16:16 -04:00
|
|
|
}
|
|
|
|
|
2016-06-07 17:28:28 -04:00
|
|
|
stats := l.Attrs().Statistics
|
|
|
|
if stats == nil {
|
|
|
|
return nil, fmt.Errorf("no statistics were returned")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &types.InterfaceStatistics{
|
|
|
|
RxBytes: uint64(stats.RxBytes),
|
|
|
|
TxBytes: uint64(stats.TxBytes),
|
|
|
|
RxPackets: uint64(stats.RxPackets),
|
|
|
|
TxPackets: uint64(stats.TxPackets),
|
|
|
|
RxDropped: uint64(stats.RxDropped),
|
|
|
|
TxDropped: uint64(stats.TxDropped),
|
|
|
|
}, nil
|
2015-06-30 17:16:16 -04:00
|
|
|
}
|
|
|
|
|
2015-06-20 20:08:36 -04:00
|
|
|
func (n *networkNamespace) findDst(srcName string, isBridge bool) string {
|
2015-06-12 16:51:57 -04:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
for _, i := range n.iFaces {
|
|
|
|
// The master should match the srcname of the interface and the
|
2015-06-20 20:08:36 -04:00
|
|
|
// master interface should be of type bridge, if searching for a bridge type
|
|
|
|
if i.SrcName() == srcName && (!isBridge || i.Bridge()) {
|
2015-06-12 16:51:57 -04:00
|
|
|
return i.DstName()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *networkNamespace) AddInterface(srcName, dstPrefix string, options ...IfaceOption) error {
|
|
|
|
i := &nwIface{srcName: srcName, dstName: dstPrefix, ns: n}
|
|
|
|
i.processInterfaceOptions(options...)
|
|
|
|
|
|
|
|
if i.master != "" {
|
2015-06-20 20:08:36 -04:00
|
|
|
i.dstMaster = n.findDst(i.master, true)
|
2015-06-12 16:51:57 -04:00
|
|
|
if i.dstMaster == "" {
|
|
|
|
return fmt.Errorf("could not find an appropriate master %q for %q",
|
|
|
|
i.master, i.srcName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
n.Lock()
|
2015-12-18 14:03:57 -05:00
|
|
|
if n.isDefault {
|
|
|
|
i.dstName = i.srcName
|
|
|
|
} else {
|
|
|
|
i.dstName = fmt.Sprintf("%s%d", i.dstName, n.nextIfIndex)
|
|
|
|
n.nextIfIndex++
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:51:57 -04:00
|
|
|
path := n.path
|
2015-12-18 14:03:57 -05:00
|
|
|
isDefault := n.isDefault
|
2016-06-07 17:28:28 -04:00
|
|
|
nlh := n.nlHandle
|
|
|
|
nlhHost := ns.NlHandle()
|
2015-06-12 16:51:57 -04:00
|
|
|
n.Unlock()
|
|
|
|
|
2016-06-07 17:28:28 -04:00
|
|
|
// If it is a bridge interface we have to create the bridge inside
|
|
|
|
// the namespace so don't try to lookup the interface using srcName
|
|
|
|
if i.bridge {
|
|
|
|
link := &netlink.Bridge{
|
|
|
|
LinkAttrs: netlink.LinkAttrs{
|
|
|
|
Name: i.srcName,
|
|
|
|
},
|
2015-06-12 16:51:57 -04:00
|
|
|
}
|
2016-06-07 17:28:28 -04:00
|
|
|
if err := nlh.LinkAdd(link); err != nil {
|
|
|
|
return fmt.Errorf("failed to create bridge %q: %v", i.srcName, err)
|
|
|
|
}
|
|
|
|
} else {
|
2015-06-12 16:51:57 -04:00
|
|
|
// Find the network interface identified by the SrcName attribute.
|
2016-06-07 17:28:28 -04:00
|
|
|
iface, err := nlhHost.LinkByName(i.srcName)
|
2015-06-12 16:51:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get link by name %q: %v", i.srcName, err)
|
|
|
|
}
|
|
|
|
|
2015-12-18 14:03:57 -05:00
|
|
|
// Move the network interface to the destination
|
|
|
|
// namespace only if the namespace is not a default
|
|
|
|
// type
|
|
|
|
if !isDefault {
|
2016-06-07 17:28:28 -04:00
|
|
|
newNs, err := netns.GetFromPath(path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed get network namespace %q: %v", path, err)
|
2015-06-12 16:51:57 -04:00
|
|
|
}
|
2016-06-07 17:28:28 -04:00
|
|
|
defer newNs.Close()
|
|
|
|
if err := nlhHost.LinkSetNsFd(iface, int(newNs)); err != nil {
|
|
|
|
return fmt.Errorf("failed to set namespace on link %q: %v", i.srcName, err)
|
2015-06-12 16:51:57 -04:00
|
|
|
}
|
|
|
|
}
|
2016-06-07 17:28:28 -04:00
|
|
|
}
|
2015-06-12 16:51:57 -04:00
|
|
|
|
2016-06-07 17:28:28 -04:00
|
|
|
// Find the network interface identified by the SrcName attribute.
|
|
|
|
iface, err := nlh.LinkByName(i.srcName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get link by name %q: %v", i.srcName, err)
|
|
|
|
}
|
2015-06-12 16:51:57 -04:00
|
|
|
|
2016-06-07 17:28:28 -04:00
|
|
|
// Down the interface before configuring
|
|
|
|
if err := nlh.LinkSetDown(iface); err != nil {
|
|
|
|
return fmt.Errorf("failed to set link down: %v", err)
|
|
|
|
}
|
2015-06-12 16:51:57 -04:00
|
|
|
|
2016-06-07 17:28:28 -04:00
|
|
|
// Configure the interface now this is moved in the proper namespace.
|
|
|
|
if err := configureInterface(nlh, iface, i); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-12 16:51:57 -04:00
|
|
|
|
2016-06-07 17:28:28 -04:00
|
|
|
// Up the interface.
|
2016-06-22 12:40:20 -04:00
|
|
|
cnt := 0
|
|
|
|
for err = nlh.LinkSetUp(iface); err != nil && cnt < 3; cnt++ {
|
2016-11-09 21:01:07 -05:00
|
|
|
logrus.Debugf("retrying link setup because of: %v", err)
|
2016-06-22 12:40:20 -04:00
|
|
|
time.Sleep(10 * time.Millisecond)
|
2016-07-21 17:27:47 -04:00
|
|
|
err = nlh.LinkSetUp(iface)
|
2016-06-22 12:40:20 -04:00
|
|
|
}
|
|
|
|
if err != nil {
|
2016-06-07 17:28:28 -04:00
|
|
|
return fmt.Errorf("failed to set link up: %v", err)
|
|
|
|
}
|
2015-06-12 16:51:57 -04:00
|
|
|
|
2016-06-07 17:28:28 -04:00
|
|
|
// Set the routes on the interface. This can only be done when the interface is up.
|
|
|
|
if err := setInterfaceRoutes(nlh, iface, i); err != nil {
|
|
|
|
return fmt.Errorf("error setting interface %q routes to %q: %v", iface.Attrs().Name, i.Routes(), err)
|
|
|
|
}
|
2015-09-21 14:13:14 -04:00
|
|
|
|
2016-06-07 17:28:28 -04:00
|
|
|
n.Lock()
|
|
|
|
n.iFaces = append(n.iFaces, i)
|
|
|
|
n.Unlock()
|
2015-06-12 16:51:57 -04:00
|
|
|
|
2016-06-07 17:28:28 -04:00
|
|
|
return nil
|
2015-06-12 16:51:57 -04:00
|
|
|
}
|
|
|
|
|
2016-06-07 17:28:28 -04:00
|
|
|
func configureInterface(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
|
2015-06-12 16:51:57 -04:00
|
|
|
ifaceName := iface.Attrs().Name
|
|
|
|
ifaceConfigurators := []struct {
|
2016-06-07 17:28:28 -04:00
|
|
|
Fn func(*netlink.Handle, netlink.Link, *nwIface) error
|
2015-06-12 16:51:57 -04:00
|
|
|
ErrMessage string
|
|
|
|
}{
|
|
|
|
{setInterfaceName, fmt.Sprintf("error renaming interface %q to %q", ifaceName, i.DstName())},
|
2016-03-08 21:47:02 -05:00
|
|
|
{setInterfaceMAC, fmt.Sprintf("error setting interface %q MAC to %q", ifaceName, i.MacAddress())},
|
2016-03-16 17:52:34 -04:00
|
|
|
{setInterfaceIP, fmt.Sprintf("error setting interface %q IP to %v", ifaceName, i.Address())},
|
|
|
|
{setInterfaceIPv6, fmt.Sprintf("error setting interface %q IPv6 to %v", ifaceName, i.AddressIPv6())},
|
2015-06-12 16:51:57 -04:00
|
|
|
{setInterfaceMaster, fmt.Sprintf("error setting interface %q master to %q", ifaceName, i.DstMaster())},
|
2016-06-07 17:28:28 -04:00
|
|
|
{setInterfaceLinkLocalIPs, fmt.Sprintf("error setting interface %q link local IPs to %v", ifaceName, i.LinkLocalAddresses())},
|
2016-11-21 14:44:58 -05:00
|
|
|
{setInterfaceIPAliases, fmt.Sprintf("error setting interface %q IP Aliases to %v", ifaceName, i.IPAliases())},
|
2015-06-12 16:51:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, config := range ifaceConfigurators {
|
2016-06-07 17:28:28 -04:00
|
|
|
if err := config.Fn(nlh, iface, i); err != nil {
|
2015-06-12 16:51:57 -04:00
|
|
|
return fmt.Errorf("%s: %v", config.ErrMessage, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-07 17:28:28 -04:00
|
|
|
func setInterfaceMaster(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
|
2015-06-12 16:51:57 -04:00
|
|
|
if i.DstMaster() == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-07 17:28:28 -04:00
|
|
|
return nlh.LinkSetMaster(iface, &netlink.Bridge{
|
2015-06-12 16:51:57 -04:00
|
|
|
LinkAttrs: netlink.LinkAttrs{Name: i.DstMaster()}})
|
|
|
|
}
|
|
|
|
|
2016-06-07 17:28:28 -04:00
|
|
|
func setInterfaceMAC(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
|
2016-03-08 21:47:02 -05:00
|
|
|
if i.MacAddress() == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2016-06-07 17:28:28 -04:00
|
|
|
return nlh.LinkSetHardwareAddr(iface, i.MacAddress())
|
2016-03-08 21:47:02 -05:00
|
|
|
}
|
|
|
|
|
2016-06-07 17:28:28 -04:00
|
|
|
func setInterfaceIP(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
|
2015-06-12 16:51:57 -04:00
|
|
|
if i.Address() == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2016-11-09 21:01:07 -05:00
|
|
|
if err := checkRouteConflict(nlh, i.Address(), netlink.FAMILY_V4); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-12 16:51:57 -04:00
|
|
|
ipAddr := &netlink.Addr{IPNet: i.Address(), Label: ""}
|
2016-06-07 17:28:28 -04:00
|
|
|
return nlh.AddrAdd(iface, ipAddr)
|
2015-06-12 16:51:57 -04:00
|
|
|
}
|
|
|
|
|
2016-06-07 17:28:28 -04:00
|
|
|
func setInterfaceIPv6(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
|
2015-06-12 16:51:57 -04:00
|
|
|
if i.AddressIPv6() == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2016-11-09 21:01:07 -05:00
|
|
|
if err := checkRouteConflict(nlh, i.AddressIPv6(), netlink.FAMILY_V6); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-08 16:56:01 -05:00
|
|
|
ipAddr := &netlink.Addr{IPNet: i.AddressIPv6(), Label: "", Flags: syscall.IFA_F_NODAD}
|
2016-06-07 17:28:28 -04:00
|
|
|
return nlh.AddrAdd(iface, ipAddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setInterfaceLinkLocalIPs(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
|
|
|
|
for _, llIP := range i.LinkLocalAddresses() {
|
|
|
|
ipAddr := &netlink.Addr{IPNet: llIP}
|
|
|
|
if err := nlh.AddrAdd(iface, ipAddr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2015-06-12 16:51:57 -04:00
|
|
|
}
|
|
|
|
|
2016-11-21 14:44:58 -05:00
|
|
|
func setInterfaceIPAliases(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
|
|
|
|
for _, si := range i.IPAliases() {
|
|
|
|
ipAddr := &netlink.Addr{IPNet: si}
|
|
|
|
if err := nlh.AddrAdd(iface, ipAddr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-07 17:28:28 -04:00
|
|
|
func setInterfaceName(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
|
|
|
|
return nlh.LinkSetName(iface, i.DstName())
|
2015-06-12 16:51:57 -04:00
|
|
|
}
|
|
|
|
|
2016-06-07 17:28:28 -04:00
|
|
|
func setInterfaceRoutes(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
|
2015-06-12 16:51:57 -04:00
|
|
|
for _, route := range i.Routes() {
|
2016-06-07 17:28:28 -04:00
|
|
|
err := nlh.RouteAdd(&netlink.Route{
|
2015-06-12 16:51:57 -04:00
|
|
|
Scope: netlink.SCOPE_LINK,
|
|
|
|
LinkIndex: iface.Attrs().Index,
|
|
|
|
Dst: route,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-06-30 17:16:16 -04:00
|
|
|
|
|
|
|
// In older kernels (like the one in Centos 6.6 distro) sysctl does not have netns support. Therefore
|
|
|
|
// we cannot gather the statistics from /sys/class/net/<dev>/statistics/<counter> files. Per-netns stats
|
2015-09-21 14:13:14 -04:00
|
|
|
// are naturally found in /proc/net/dev in kernels which support netns (ifconfig relies on that).
|
2015-06-30 17:16:16 -04:00
|
|
|
const (
|
|
|
|
netStatsFile = "/proc/net/dev"
|
|
|
|
base = "[ ]*%s:([ ]+[0-9]+){16}"
|
|
|
|
)
|
|
|
|
|
2015-10-04 23:02:02 -04:00
|
|
|
func scanInterfaceStats(data, ifName string, i *types.InterfaceStatistics) error {
|
2015-06-30 17:16:16 -04:00
|
|
|
var (
|
|
|
|
bktStr string
|
|
|
|
bkt uint64
|
|
|
|
)
|
|
|
|
|
|
|
|
regex := fmt.Sprintf(base, ifName)
|
|
|
|
re := regexp.MustCompile(regex)
|
|
|
|
line := re.FindString(data)
|
|
|
|
|
|
|
|
_, err := fmt.Sscanf(line, "%s %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d",
|
|
|
|
&bktStr, &i.RxBytes, &i.RxPackets, &i.RxErrors, &i.RxDropped, &bkt, &bkt, &bkt,
|
|
|
|
&bkt, &i.TxBytes, &i.TxPackets, &i.TxErrors, &i.TxDropped, &bkt, &bkt, &bkt, &bkt)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2016-11-09 21:01:07 -05:00
|
|
|
|
|
|
|
func checkRouteConflict(nlh *netlink.Handle, address *net.IPNet, family int) error {
|
|
|
|
routes, err := nlh.RouteList(nil, family)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, route := range routes {
|
|
|
|
if route.Dst != nil {
|
|
|
|
if route.Dst.Contains(address.IP) || address.Contains(route.Dst.IP) {
|
|
|
|
return fmt.Errorf("cannot program address %v in sandbox interface because it conflicts with existing route %s",
|
|
|
|
address, route)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|