ipvlan_setup.go 7.3 KB

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