macvlan_setup.go 6.9 KB

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