network_linux.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // +build linux
  2. package libcontainer
  3. import (
  4. "fmt"
  5. "io/ioutil"
  6. "path/filepath"
  7. "strconv"
  8. "strings"
  9. "github.com/opencontainers/runc/libcontainer/configs"
  10. "github.com/opencontainers/runc/types"
  11. "github.com/vishvananda/netlink"
  12. )
  13. var strategies = map[string]networkStrategy{
  14. "loopback": &loopback{},
  15. }
  16. // networkStrategy represents a specific network configuration for
  17. // a container's networking stack
  18. type networkStrategy interface {
  19. create(*network, int) error
  20. initialize(*network) error
  21. detach(*configs.Network) error
  22. attach(*configs.Network) error
  23. }
  24. // getStrategy returns the specific network strategy for the
  25. // provided type.
  26. func getStrategy(tpe string) (networkStrategy, error) {
  27. s, exists := strategies[tpe]
  28. if !exists {
  29. return nil, fmt.Errorf("unknown strategy type %q", tpe)
  30. }
  31. return s, nil
  32. }
  33. // Returns the network statistics for the network interfaces represented by the NetworkRuntimeInfo.
  34. func getNetworkInterfaceStats(interfaceName string) (*types.NetworkInterface, error) {
  35. out := &types.NetworkInterface{Name: interfaceName}
  36. // This can happen if the network runtime information is missing - possible if the
  37. // container was created by an old version of libcontainer.
  38. if interfaceName == "" {
  39. return out, nil
  40. }
  41. type netStatsPair struct {
  42. // Where to write the output.
  43. Out *uint64
  44. // The network stats file to read.
  45. File string
  46. }
  47. // 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.
  48. netStats := []netStatsPair{
  49. {Out: &out.RxBytes, File: "tx_bytes"},
  50. {Out: &out.RxPackets, File: "tx_packets"},
  51. {Out: &out.RxErrors, File: "tx_errors"},
  52. {Out: &out.RxDropped, File: "tx_dropped"},
  53. {Out: &out.TxBytes, File: "rx_bytes"},
  54. {Out: &out.TxPackets, File: "rx_packets"},
  55. {Out: &out.TxErrors, File: "rx_errors"},
  56. {Out: &out.TxDropped, File: "rx_dropped"},
  57. }
  58. for _, netStat := range netStats {
  59. data, err := readSysfsNetworkStats(interfaceName, netStat.File)
  60. if err != nil {
  61. return nil, err
  62. }
  63. *(netStat.Out) = data
  64. }
  65. return out, nil
  66. }
  67. // Reads the specified statistics available under /sys/class/net/<EthInterface>/statistics
  68. func readSysfsNetworkStats(ethInterface, statsFile string) (uint64, error) {
  69. data, err := ioutil.ReadFile(filepath.Join("/sys/class/net", ethInterface, "statistics", statsFile))
  70. if err != nil {
  71. return 0, err
  72. }
  73. return strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64)
  74. }
  75. // loopback is a network strategy that provides a basic loopback device
  76. type loopback struct {
  77. }
  78. func (l *loopback) create(n *network, nspid int) error {
  79. return nil
  80. }
  81. func (l *loopback) initialize(config *network) error {
  82. return netlink.LinkSetUp(&netlink.Device{LinkAttrs: netlink.LinkAttrs{Name: "lo"}})
  83. }
  84. func (l *loopback) attach(n *configs.Network) (err error) {
  85. return nil
  86. }
  87. func (l *loopback) detach(n *configs.Network) (err error) {
  88. return nil
  89. }