2015-02-10 07:16:27 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package libcontainer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/docker/libcontainer/netlink"
|
2015-02-11 03:51:45 +08:00
|
|
|
"github.com/docker/libcontainer/utils"
|
2015-02-10 07:16:27 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var strategies = map[string]networkStrategy{
|
|
|
|
"veth": &veth{},
|
|
|
|
"loopback": &loopback{},
|
|
|
|
}
|
|
|
|
|
|
|
|
// networkStrategy represents a specific network configuration for
|
|
|
|
// a container's networking stack
|
|
|
|
type networkStrategy interface {
|
2015-02-11 03:51:45 +08:00
|
|
|
create(*network, int) error
|
|
|
|
initialize(*network) error
|
2015-02-10 07:16:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// getStrategy returns the specific network strategy for the
|
2015-02-12 09:12:03 +08:00
|
|
|
// provided type.
|
2015-02-10 07:16:27 +08:00
|
|
|
func getStrategy(tpe string) (networkStrategy, error) {
|
|
|
|
s, exists := strategies[tpe]
|
|
|
|
if !exists {
|
2015-02-12 09:12:03 +08:00
|
|
|
return nil, fmt.Errorf("unknown strategy type %q", tpe)
|
2015-02-10 07:16:27 +08:00
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the network statistics for the network interfaces represented by the NetworkRuntimeInfo.
|
2015-02-12 09:12:03 +08:00
|
|
|
func getNetworkInterfaceStats(interfaceName string) (*networkInterface, error) {
|
|
|
|
out := &networkInterface{Name: interfaceName}
|
2015-02-10 07:16:27 +08:00
|
|
|
// This can happen if the network runtime information is missing - possible if the
|
|
|
|
// container was created by an old version of libcontainer.
|
|
|
|
if interfaceName == "" {
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
type netStatsPair struct {
|
|
|
|
// Where to write the output.
|
|
|
|
Out *uint64
|
|
|
|
// The network stats file to read.
|
|
|
|
File string
|
|
|
|
}
|
|
|
|
// Ingress for host veth is from the container. Hence tx_bytes stat on the host veth is actually number of bytes received by the container.
|
|
|
|
netStats := []netStatsPair{
|
|
|
|
{Out: &out.RxBytes, File: "tx_bytes"},
|
|
|
|
{Out: &out.RxPackets, File: "tx_packets"},
|
|
|
|
{Out: &out.RxErrors, File: "tx_errors"},
|
|
|
|
{Out: &out.RxDropped, File: "tx_dropped"},
|
|
|
|
|
|
|
|
{Out: &out.TxBytes, File: "rx_bytes"},
|
|
|
|
{Out: &out.TxPackets, File: "rx_packets"},
|
|
|
|
{Out: &out.TxErrors, File: "rx_errors"},
|
|
|
|
{Out: &out.TxDropped, File: "rx_dropped"},
|
|
|
|
}
|
|
|
|
for _, netStat := range netStats {
|
|
|
|
data, err := readSysfsNetworkStats(interfaceName, netStat.File)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
*(netStat.Out) = data
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reads the specified statistics available under /sys/class/net/<EthInterface>/statistics
|
|
|
|
func readSysfsNetworkStats(ethInterface, statsFile string) (uint64, error) {
|
|
|
|
data, err := ioutil.ReadFile(filepath.Join("/sys/class/net", ethInterface, "statistics", statsFile))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64)
|
|
|
|
}
|
|
|
|
|
|
|
|
// loopback is a network strategy that provides a basic loopback device
|
|
|
|
type loopback struct {
|
|
|
|
}
|
|
|
|
|
2015-02-11 03:51:45 +08:00
|
|
|
func (l *loopback) create(n *network, nspid int) error {
|
2015-02-10 07:16:27 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-11 03:51:45 +08:00
|
|
|
func (l *loopback) initialize(config *network) error {
|
2015-02-10 07:16:27 +08:00
|
|
|
iface, err := net.InterfaceByName("lo")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return netlink.NetworkLinkUp(iface)
|
|
|
|
}
|
|
|
|
|
|
|
|
// veth is a network strategy that uses a bridge and creates
|
2015-02-14 08:06:17 +08:00
|
|
|
// a veth pair, one that is attached to the bridge on the host and the other
|
2015-02-10 07:16:27 +08:00
|
|
|
// is placed inside the container's namespace
|
|
|
|
type veth struct {
|
|
|
|
}
|
|
|
|
|
2015-02-11 03:51:45 +08:00
|
|
|
func (v *veth) create(n *network, nspid int) (err error) {
|
|
|
|
tmpName, err := v.generateTempPeerName()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
n.TempVethPeerName = tmpName
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
netlink.NetworkLinkDel(n.HostInterfaceName)
|
|
|
|
netlink.NetworkLinkDel(n.TempVethPeerName)
|
|
|
|
}
|
|
|
|
}()
|
2015-02-10 07:16:27 +08:00
|
|
|
if n.Bridge == "" {
|
|
|
|
return fmt.Errorf("bridge is not specified")
|
|
|
|
}
|
|
|
|
bridge, err := net.InterfaceByName(n.Bridge)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-11 03:51:45 +08:00
|
|
|
if err := netlink.NetworkCreateVethPair(n.HostInterfaceName, n.TempVethPeerName, n.TxQueueLen); err != nil {
|
2015-02-10 07:16:27 +08:00
|
|
|
return err
|
|
|
|
}
|
2015-02-11 03:51:45 +08:00
|
|
|
host, err := net.InterfaceByName(n.HostInterfaceName)
|
2015-02-10 07:16:27 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := netlink.AddToBridge(host, bridge); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := netlink.NetworkSetMTU(host, n.Mtu); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := netlink.NetworkLinkUp(host); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-11 03:51:45 +08:00
|
|
|
child, err := net.InterfaceByName(n.TempVethPeerName)
|
2015-02-10 07:16:27 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return netlink.NetworkSetNsPid(child, nspid)
|
|
|
|
}
|
|
|
|
|
2015-02-11 03:51:45 +08:00
|
|
|
func (v *veth) generateTempPeerName() (string, error) {
|
|
|
|
return utils.GenerateRandomName("veth", 7)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *veth) initialize(config *network) error {
|
|
|
|
peer := config.TempVethPeerName
|
|
|
|
if peer == "" {
|
|
|
|
return fmt.Errorf("peer is not specified")
|
2015-02-10 07:16:27 +08:00
|
|
|
}
|
2015-02-11 03:51:45 +08:00
|
|
|
child, err := net.InterfaceByName(peer)
|
2015-02-10 07:16:27 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := netlink.NetworkLinkDown(child); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-11 03:51:45 +08:00
|
|
|
if err := netlink.NetworkChangeName(child, config.Name); err != nil {
|
2015-02-10 07:16:27 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// get the interface again after we changed the name as the index also changes.
|
2015-02-11 03:51:45 +08:00
|
|
|
if child, err = net.InterfaceByName(config.Name); err != nil {
|
2015-02-10 07:16:27 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if config.MacAddress != "" {
|
|
|
|
if err := netlink.NetworkSetMacAddress(child, config.MacAddress); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ip, ipNet, err := net.ParseCIDR(config.Address)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := netlink.NetworkLinkAddIp(child, ip, ipNet); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if config.IPv6Address != "" {
|
|
|
|
if ip, ipNet, err = net.ParseCIDR(config.IPv6Address); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := netlink.NetworkLinkAddIp(child, ip, ipNet); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := netlink.NetworkSetMTU(child, config.Mtu); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := netlink.NetworkLinkUp(child); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if config.Gateway != "" {
|
2015-02-11 03:51:45 +08:00
|
|
|
if err := netlink.AddDefaultGw(config.Gateway, config.Name); err != nil {
|
2015-02-10 07:16:27 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if config.IPv6Gateway != "" {
|
2015-02-11 03:51:45 +08:00
|
|
|
if err := netlink.AddDefaultGw(config.IPv6Gateway, config.Name); err != nil {
|
2015-02-10 07:16:27 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|