macvlan_setup.go 7.0 KB

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