2015-02-10 07:16:27 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package libcontainer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2015-06-22 10:29:59 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
2015-06-27 02:38:23 +08:00
|
|
|
"github.com/vishvananda/netlink"
|
2015-02-10 07:16:27 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var strategies = map[string]networkStrategy{
|
|
|
|
"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-04-20 16:24:50 +08:00
|
|
|
detach(*configs.Network) error
|
|
|
|
attach(*configs.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-03-04 03:04:42 +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-11-30 19:30:57 +08:00
|
|
|
return netlink.LinkSetUp(&netlink.Device{LinkAttrs: netlink.LinkAttrs{Name: "lo"}})
|
2015-02-10 07:16:27 +08:00
|
|
|
}
|
|
|
|
|
2015-04-20 16:24:50 +08:00
|
|
|
func (l *loopback) attach(n *configs.Network) (err error) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *loopback) detach(n *configs.Network) (err error) {
|
|
|
|
return nil
|
|
|
|
}
|