network_linux.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // +build linux
  2. package libcontainer
  3. import (
  4. "fmt"
  5. "io/ioutil"
  6. "net"
  7. "path/filepath"
  8. "strconv"
  9. "strings"
  10. "github.com/opencontainers/runc/libcontainer/configs"
  11. "github.com/opencontainers/runc/libcontainer/utils"
  12. "github.com/vishvananda/netlink"
  13. )
  14. var strategies = map[string]networkStrategy{
  15. "veth": &veth{},
  16. "loopback": &loopback{},
  17. }
  18. // networkStrategy represents a specific network configuration for
  19. // a container's networking stack
  20. type networkStrategy interface {
  21. create(*network, int) error
  22. initialize(*network) error
  23. detach(*configs.Network) error
  24. attach(*configs.Network) error
  25. }
  26. // getStrategy returns the specific network strategy for the
  27. // provided type.
  28. func getStrategy(tpe string) (networkStrategy, error) {
  29. s, exists := strategies[tpe]
  30. if !exists {
  31. return nil, fmt.Errorf("unknown strategy type %q", tpe)
  32. }
  33. return s, nil
  34. }
  35. // Returns the network statistics for the network interfaces represented by the NetworkRuntimeInfo.
  36. func getNetworkInterfaceStats(interfaceName string) (*NetworkInterface, error) {
  37. out := &NetworkInterface{Name: interfaceName}
  38. // This can happen if the network runtime information is missing - possible if the
  39. // container was created by an old version of libcontainer.
  40. if interfaceName == "" {
  41. return out, nil
  42. }
  43. type netStatsPair struct {
  44. // Where to write the output.
  45. Out *uint64
  46. // The network stats file to read.
  47. File string
  48. }
  49. // 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.
  50. netStats := []netStatsPair{
  51. {Out: &out.RxBytes, File: "tx_bytes"},
  52. {Out: &out.RxPackets, File: "tx_packets"},
  53. {Out: &out.RxErrors, File: "tx_errors"},
  54. {Out: &out.RxDropped, File: "tx_dropped"},
  55. {Out: &out.TxBytes, File: "rx_bytes"},
  56. {Out: &out.TxPackets, File: "rx_packets"},
  57. {Out: &out.TxErrors, File: "rx_errors"},
  58. {Out: &out.TxDropped, File: "rx_dropped"},
  59. }
  60. for _, netStat := range netStats {
  61. data, err := readSysfsNetworkStats(interfaceName, netStat.File)
  62. if err != nil {
  63. return nil, err
  64. }
  65. *(netStat.Out) = data
  66. }
  67. return out, nil
  68. }
  69. // Reads the specified statistics available under /sys/class/net/<EthInterface>/statistics
  70. func readSysfsNetworkStats(ethInterface, statsFile string) (uint64, error) {
  71. data, err := ioutil.ReadFile(filepath.Join("/sys/class/net", ethInterface, "statistics", statsFile))
  72. if err != nil {
  73. return 0, err
  74. }
  75. return strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64)
  76. }
  77. // loopback is a network strategy that provides a basic loopback device
  78. type loopback struct {
  79. }
  80. func (l *loopback) create(n *network, nspid int) error {
  81. return nil
  82. }
  83. func (l *loopback) initialize(config *network) error {
  84. return netlink.LinkSetUp(&netlink.Device{LinkAttrs: netlink.LinkAttrs{Name: "lo"}})
  85. }
  86. func (l *loopback) attach(n *configs.Network) (err error) {
  87. return nil
  88. }
  89. func (l *loopback) detach(n *configs.Network) (err error) {
  90. return nil
  91. }
  92. // veth is a network strategy that uses a bridge and creates
  93. // a veth pair, one that is attached to the bridge on the host and the other
  94. // is placed inside the container's namespace
  95. type veth struct {
  96. }
  97. func (v *veth) detach(n *configs.Network) (err error) {
  98. return netlink.LinkSetMaster(&netlink.Device{LinkAttrs: netlink.LinkAttrs{Name: n.HostInterfaceName}}, nil)
  99. }
  100. // attach a container network interface to an external network
  101. func (v *veth) attach(n *configs.Network) (err error) {
  102. brl, err := netlink.LinkByName(n.Bridge)
  103. if err != nil {
  104. return err
  105. }
  106. br, ok := brl.(*netlink.Bridge)
  107. if !ok {
  108. return fmt.Errorf("Wrong device type %T", brl)
  109. }
  110. host, err := netlink.LinkByName(n.HostInterfaceName)
  111. if err != nil {
  112. return err
  113. }
  114. if err := netlink.LinkSetMaster(host, br); err != nil {
  115. return err
  116. }
  117. if err := netlink.LinkSetMTU(host, n.Mtu); err != nil {
  118. return err
  119. }
  120. if n.HairpinMode {
  121. if err := netlink.LinkSetHairpin(host, true); err != nil {
  122. return err
  123. }
  124. }
  125. if err := netlink.LinkSetUp(host); err != nil {
  126. return err
  127. }
  128. return nil
  129. }
  130. func (v *veth) create(n *network, nspid int) (err error) {
  131. tmpName, err := v.generateTempPeerName()
  132. if err != nil {
  133. return err
  134. }
  135. n.TempVethPeerName = tmpName
  136. if n.Bridge == "" {
  137. return fmt.Errorf("bridge is not specified")
  138. }
  139. veth := &netlink.Veth{
  140. LinkAttrs: netlink.LinkAttrs{
  141. Name: n.HostInterfaceName,
  142. TxQLen: n.TxQueueLen,
  143. },
  144. PeerName: n.TempVethPeerName,
  145. }
  146. if err := netlink.LinkAdd(veth); err != nil {
  147. return err
  148. }
  149. defer func() {
  150. if err != nil {
  151. netlink.LinkDel(veth)
  152. }
  153. }()
  154. if err := v.attach(&n.Network); err != nil {
  155. return err
  156. }
  157. child, err := netlink.LinkByName(n.TempVethPeerName)
  158. if err != nil {
  159. return err
  160. }
  161. return netlink.LinkSetNsPid(child, nspid)
  162. }
  163. func (v *veth) generateTempPeerName() (string, error) {
  164. return utils.GenerateRandomName("veth", 7)
  165. }
  166. func (v *veth) initialize(config *network) error {
  167. peer := config.TempVethPeerName
  168. if peer == "" {
  169. return fmt.Errorf("peer is not specified")
  170. }
  171. child, err := netlink.LinkByName(peer)
  172. if err != nil {
  173. return err
  174. }
  175. if err := netlink.LinkSetDown(child); err != nil {
  176. return err
  177. }
  178. if err := netlink.LinkSetName(child, config.Name); err != nil {
  179. return err
  180. }
  181. // get the interface again after we changed the name as the index also changes.
  182. if child, err = netlink.LinkByName(config.Name); err != nil {
  183. return err
  184. }
  185. if config.MacAddress != "" {
  186. mac, err := net.ParseMAC(config.MacAddress)
  187. if err != nil {
  188. return err
  189. }
  190. if err := netlink.LinkSetHardwareAddr(child, mac); err != nil {
  191. return err
  192. }
  193. }
  194. ip, err := netlink.ParseAddr(config.Address)
  195. if err != nil {
  196. return err
  197. }
  198. if err := netlink.AddrAdd(child, ip); err != nil {
  199. return err
  200. }
  201. if config.IPv6Address != "" {
  202. ip6, err := netlink.ParseAddr(config.IPv6Address)
  203. if err != nil {
  204. return err
  205. }
  206. if err := netlink.AddrAdd(child, ip6); err != nil {
  207. return err
  208. }
  209. }
  210. if err := netlink.LinkSetMTU(child, config.Mtu); err != nil {
  211. return err
  212. }
  213. if err := netlink.LinkSetUp(child); err != nil {
  214. return err
  215. }
  216. if config.Gateway != "" {
  217. gw := net.ParseIP(config.Gateway)
  218. if err := netlink.RouteAdd(&netlink.Route{
  219. Scope: netlink.SCOPE_UNIVERSE,
  220. LinkIndex: child.Attrs().Index,
  221. Gw: gw,
  222. }); err != nil {
  223. return err
  224. }
  225. }
  226. if config.IPv6Gateway != "" {
  227. gw := net.ParseIP(config.IPv6Gateway)
  228. if err := netlink.RouteAdd(&netlink.Route{
  229. Scope: netlink.SCOPE_UNIVERSE,
  230. LinkIndex: child.Attrs().Index,
  231. Gw: gw,
  232. }); err != nil {
  233. return err
  234. }
  235. }
  236. return nil
  237. }