ipvlan_setup.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package ipvlan
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "github.com/Sirupsen/logrus"
  7. "github.com/vishvananda/netlink"
  8. )
  9. const (
  10. dummyPrefix = "di-" // ipvlan prefix for dummy parent interface
  11. ipvlanKernelVer = 4 // minimum ipvlan kernel support
  12. ipvlanMajorVer = 2 // minimum ipvlan major kernel support
  13. )
  14. // createIPVlan Create the ipvlan slave specifying the source name
  15. func createIPVlan(containerIfName, parent, ipvlanMode string) (string, error) {
  16. // Set the ipvlan mode. Default is bridge mode
  17. mode, err := setIPVlanMode(ipvlanMode)
  18. if err != nil {
  19. return "", fmt.Errorf("Unsupported %s ipvlan mode: %v", ipvlanMode, err)
  20. }
  21. // verify the Docker host interface acting as the macvlan parent iface exists
  22. if !parentExists(parent) {
  23. return "", fmt.Errorf("the requested parent interface %s was not found on the Docker host", parent)
  24. }
  25. // Get the link for the master index (Example: the docker host eth iface)
  26. parentLink, err := netlink.LinkByName(parent)
  27. if err != nil {
  28. return "", fmt.Errorf("error occoured looking up the %s parent iface %s error: %s", ipvlanType, parent, err)
  29. }
  30. // Create an ipvlan link
  31. ipvlan := &netlink.IPVlan{
  32. LinkAttrs: netlink.LinkAttrs{
  33. Name: containerIfName,
  34. ParentIndex: parentLink.Attrs().Index,
  35. },
  36. Mode: mode,
  37. }
  38. if err := netlink.LinkAdd(ipvlan); err != nil {
  39. // If a user creates a macvlan and ipvlan on same parent, only one slave iface can be active at a time.
  40. return "", fmt.Errorf("failed to create the %s port: %v", ipvlanType, err)
  41. }
  42. return ipvlan.Attrs().Name, nil
  43. }
  44. // setIPVlanMode setter for one of the two ipvlan port types
  45. func setIPVlanMode(mode string) (netlink.IPVlanMode, error) {
  46. switch mode {
  47. case modeL2:
  48. return netlink.IPVLAN_MODE_L2, nil
  49. case modeL3:
  50. return netlink.IPVLAN_MODE_L3, nil
  51. default:
  52. return 0, fmt.Errorf("Unknown ipvlan mode: %s", mode)
  53. }
  54. }
  55. // parentExists check if the specified interface exists in the default namespace
  56. func parentExists(ifaceStr string) bool {
  57. _, err := netlink.LinkByName(ifaceStr)
  58. if err != nil {
  59. return false
  60. }
  61. return true
  62. }
  63. // createVlanLink parses sub-interfaces and vlan id for creation
  64. func createVlanLink(parentName string) error {
  65. if strings.Contains(parentName, ".") {
  66. parent, vidInt, err := parseVlan(parentName)
  67. if err != nil {
  68. return err
  69. }
  70. // VLAN identifier or VID is a 12-bit field specifying the VLAN to which the frame belongs
  71. if vidInt > 4094 || vidInt < 1 {
  72. return fmt.Errorf("vlan id must be between 1-4094, received: %d", vidInt)
  73. }
  74. // get the parent link to attach a vlan subinterface
  75. parentLink, err := netlink.LinkByName(parent)
  76. if err != nil {
  77. return fmt.Errorf("failed to find master interface %s on the Docker host: %v", parent, err)
  78. }
  79. vlanLink := &netlink.Vlan{
  80. LinkAttrs: netlink.LinkAttrs{
  81. Name: parentName,
  82. ParentIndex: parentLink.Attrs().Index,
  83. },
  84. VlanId: vidInt,
  85. }
  86. // create the subinterface
  87. if err := netlink.LinkAdd(vlanLink); err != nil {
  88. return fmt.Errorf("failed to create %s vlan link: %v", vlanLink.Name, err)
  89. }
  90. // Bring the new netlink iface up
  91. if err := netlink.LinkSetUp(vlanLink); err != nil {
  92. return fmt.Errorf("failed to enable %s the ipvlan parent link %v", vlanLink.Name, err)
  93. }
  94. logrus.Debugf("Added a vlan tagged netlink subinterface: %s with a vlan id: %d", parentName, vidInt)
  95. return nil
  96. }
  97. return fmt.Errorf("invalid subinterface vlan name %s, example formatting is eth0.10", parentName)
  98. }
  99. // delVlanLink verifies only sub-interfaces with a vlan id get deleted
  100. func delVlanLink(linkName string) error {
  101. if strings.Contains(linkName, ".") {
  102. _, _, err := parseVlan(linkName)
  103. if err != nil {
  104. return err
  105. }
  106. // delete the vlan subinterface
  107. vlanLink, err := netlink.LinkByName(linkName)
  108. if err != nil {
  109. return fmt.Errorf("failed to find interface %s on the Docker host : %v", linkName, err)
  110. }
  111. // verify a parent interface isn't being deleted
  112. if vlanLink.Attrs().ParentIndex == 0 {
  113. return fmt.Errorf("interface %s does not appear to be a slave device: %v", linkName, err)
  114. }
  115. // delete the ipvlan slave device
  116. if err := netlink.LinkDel(vlanLink); err != nil {
  117. return fmt.Errorf("failed to delete %s link: %v", linkName, err)
  118. }
  119. logrus.Debugf("Deleted a vlan tagged netlink subinterface: %s", linkName)
  120. }
  121. // if the subinterface doesn't parse to iface.vlan_id leave the interface in
  122. // place since it could be a user specified name not created by the driver.
  123. return nil
  124. }
  125. // parseVlan parses and verifies a slave interface name: -o parent=eth0.10
  126. func parseVlan(linkName string) (string, int, error) {
  127. // parse -o parent=eth0.10
  128. splitName := strings.Split(linkName, ".")
  129. if len(splitName) != 2 {
  130. return "", 0, fmt.Errorf("required interface name format is: name.vlan_id, ex. eth0.10 for vlan 10, instead received %s", linkName)
  131. }
  132. parent, vidStr := splitName[0], splitName[1]
  133. // validate type and convert vlan id to int
  134. vidInt, err := strconv.Atoi(vidStr)
  135. if err != nil {
  136. return "", 0, fmt.Errorf("unable to parse a valid vlan id from: %s (ex. eth0.10 for vlan 10)", vidStr)
  137. }
  138. // Check if the interface exists
  139. if !parentExists(parent) {
  140. return "", 0, fmt.Errorf("-o parent interface does was not found on the host: %s", parent)
  141. }
  142. return parent, vidInt, nil
  143. }
  144. // createDummyLink creates a dummy0 parent link
  145. func createDummyLink(dummyName, truncNetID string) error {
  146. // create a parent interface since one was not specified
  147. parent := &netlink.Dummy{
  148. LinkAttrs: netlink.LinkAttrs{
  149. Name: dummyName,
  150. },
  151. }
  152. if err := netlink.LinkAdd(parent); err != nil {
  153. return err
  154. }
  155. parentDummyLink, err := netlink.LinkByName(dummyName)
  156. if err != nil {
  157. return fmt.Errorf("error occoured looking up the %s parent iface %s error: %s", ipvlanType, dummyName, err)
  158. }
  159. // bring the new netlink iface up
  160. if err := netlink.LinkSetUp(parentDummyLink); err != nil {
  161. return fmt.Errorf("failed to enable %s the ipvlan parent link: %v", dummyName, err)
  162. }
  163. return nil
  164. }
  165. // delDummyLink deletes the link type dummy used when -o parent is not passed
  166. func delDummyLink(linkName string) error {
  167. // delete the vlan subinterface
  168. dummyLink, err := netlink.LinkByName(linkName)
  169. if err != nil {
  170. return fmt.Errorf("failed to find link %s on the Docker host : %v", linkName, err)
  171. }
  172. // verify a parent interface is being deleted
  173. if dummyLink.Attrs().ParentIndex != 0 {
  174. return fmt.Errorf("link %s is not a parent dummy interface", linkName)
  175. }
  176. // delete the ipvlan dummy device
  177. if err := netlink.LinkDel(dummyLink); err != nil {
  178. return fmt.Errorf("failed to delete the dummy %s link: %v", linkName, err)
  179. }
  180. logrus.Debugf("Deleted a dummy parent link: %s", linkName)
  181. return nil
  182. }
  183. // getDummyName returns the name of a dummy parent with truncated net ID and driver prefix
  184. func getDummyName(netID string) string {
  185. return fmt.Sprintf("%s%s", dummyPrefix, netID)
  186. }