macvlan_setup.go 7.4 KB

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