link.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2015 CNI authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package ip
  15. import (
  16. "crypto/rand"
  17. "fmt"
  18. "net"
  19. "os"
  20. "github.com/containernetworking/cni/pkg/ns"
  21. "github.com/vishvananda/netlink"
  22. )
  23. func makeVethPair(name, peer string, mtu int) (netlink.Link, error) {
  24. veth := &netlink.Veth{
  25. LinkAttrs: netlink.LinkAttrs{
  26. Name: name,
  27. Flags: net.FlagUp,
  28. MTU: mtu,
  29. },
  30. PeerName: peer,
  31. }
  32. if err := netlink.LinkAdd(veth); err != nil {
  33. return nil, err
  34. }
  35. return veth, nil
  36. }
  37. func makeVeth(name string, mtu int) (peerName string, veth netlink.Link, err error) {
  38. for i := 0; i < 10; i++ {
  39. peerName, err = RandomVethName()
  40. if err != nil {
  41. return
  42. }
  43. veth, err = makeVethPair(name, peerName, mtu)
  44. switch {
  45. case err == nil:
  46. return
  47. case os.IsExist(err):
  48. continue
  49. default:
  50. err = fmt.Errorf("failed to make veth pair: %v", err)
  51. return
  52. }
  53. }
  54. // should really never be hit
  55. err = fmt.Errorf("failed to find a unique veth name")
  56. return
  57. }
  58. // RandomVethName returns string "veth" with random prefix (hashed from entropy)
  59. func RandomVethName() (string, error) {
  60. entropy := make([]byte, 4)
  61. _, err := rand.Reader.Read(entropy)
  62. if err != nil {
  63. return "", fmt.Errorf("failed to generate random veth name: %v", err)
  64. }
  65. // NetworkManager (recent versions) will ignore veth devices that start with "veth"
  66. return fmt.Sprintf("veth%x", entropy), nil
  67. }
  68. // SetupVeth sets up a virtual ethernet link.
  69. // Should be in container netns, and will switch back to hostNS to set the host
  70. // veth end up.
  71. func SetupVeth(contVethName string, mtu int, hostNS ns.NetNS) (hostVeth, contVeth netlink.Link, err error) {
  72. var hostVethName string
  73. hostVethName, contVeth, err = makeVeth(contVethName, mtu)
  74. if err != nil {
  75. return
  76. }
  77. if err = netlink.LinkSetUp(contVeth); err != nil {
  78. err = fmt.Errorf("failed to set %q up: %v", contVethName, err)
  79. return
  80. }
  81. hostVeth, err = netlink.LinkByName(hostVethName)
  82. if err != nil {
  83. err = fmt.Errorf("failed to lookup %q: %v", hostVethName, err)
  84. return
  85. }
  86. if err = netlink.LinkSetNsFd(hostVeth, int(hostNS.Fd())); err != nil {
  87. err = fmt.Errorf("failed to move veth to host netns: %v", err)
  88. return
  89. }
  90. err = hostNS.Do(func(_ ns.NetNS) error {
  91. hostVeth, err := netlink.LinkByName(hostVethName)
  92. if err != nil {
  93. return fmt.Errorf("failed to lookup %q in %q: %v", hostVethName, hostNS.Path(), err)
  94. }
  95. if err = netlink.LinkSetUp(hostVeth); err != nil {
  96. return fmt.Errorf("failed to set %q up: %v", hostVethName, err)
  97. }
  98. return nil
  99. })
  100. return
  101. }
  102. // DelLinkByName removes an interface link.
  103. func DelLinkByName(ifName string) error {
  104. iface, err := netlink.LinkByName(ifName)
  105. if err != nil {
  106. return fmt.Errorf("failed to lookup %q: %v", ifName, err)
  107. }
  108. if err = netlink.LinkDel(iface); err != nil {
  109. return fmt.Errorf("failed to delete %q: %v", ifName, err)
  110. }
  111. return nil
  112. }
  113. // DelLinkByNameAddr remove an interface returns its IP address
  114. // of the specified family
  115. func DelLinkByNameAddr(ifName string, family int) (*net.IPNet, error) {
  116. iface, err := netlink.LinkByName(ifName)
  117. if err != nil {
  118. return nil, fmt.Errorf("failed to lookup %q: %v", ifName, err)
  119. }
  120. addrs, err := netlink.AddrList(iface, family)
  121. if err != nil || len(addrs) == 0 {
  122. return nil, fmt.Errorf("failed to get IP addresses for %q: %v", ifName, err)
  123. }
  124. if err = netlink.LinkDel(iface); err != nil {
  125. return nil, fmt.Errorf("failed to delete %q: %v", ifName, err)
  126. }
  127. return addrs[0].IPNet, nil
  128. }