network_linux.go 2.8 KB

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