network.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package network
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "net"
  7. "os"
  8. "os/exec"
  9. "strings"
  10. log "github.com/Sirupsen/logrus"
  11. "github.com/rancherio/os/config"
  12. "github.com/rancherio/os/docker"
  13. "github.com/ryanuber/go-glob"
  14. "github.com/vishvananda/netlink"
  15. )
  16. func Main() {
  17. args := os.Args
  18. if len(args) > 1 {
  19. fmt.Println("call " + args[0] + " to load network config from rancher.yml config file")
  20. return
  21. }
  22. cfg, err := config.LoadConfig()
  23. if err != nil {
  24. log.Fatal(err)
  25. }
  26. ApplyNetworkConfigs(&cfg.Network)
  27. }
  28. func createInterfaces(netCfg *config.NetworkConfig) error {
  29. for name, iface := range netCfg.Interfaces {
  30. if !iface.Bridge {
  31. continue
  32. }
  33. bridge := netlink.Bridge{}
  34. bridge.LinkAttrs.Name = name
  35. if err := netlink.LinkAdd(&bridge); err != nil {
  36. log.Errorf("Failed to create bridge %s: %v", name, err)
  37. }
  38. }
  39. return nil
  40. }
  41. func ApplyNetworkConfigs(netCfg *config.NetworkConfig) error {
  42. if err := createInterfaces(netCfg); err != nil {
  43. return err
  44. }
  45. links, err := netlink.LinkList()
  46. if err != nil {
  47. return err
  48. }
  49. //apply network config
  50. for _, link := range links {
  51. linkName := link.Attrs().Name
  52. var match config.InterfaceConfig
  53. for key, netConf := range netCfg.Interfaces {
  54. if netConf.Match == "" {
  55. netConf.Match = key
  56. }
  57. if netConf.Match == "" {
  58. continue
  59. }
  60. if len(netConf.Match) > 4 && strings.ToLower(netConf.Match[:3]) == "mac" {
  61. haAddr, err := net.ParseMAC(netConf.Match[4:])
  62. if err != nil {
  63. return err
  64. }
  65. if bytes.Compare(haAddr, link.Attrs().HardwareAddr) == 0 {
  66. // MAC address match is used over all other matches
  67. match = netConf
  68. break
  69. }
  70. }
  71. // "" means match has not been found
  72. if match.Match == "" && matches(linkName, netConf.Match) {
  73. match = netConf
  74. }
  75. if netConf.Match == linkName {
  76. // Found exact match, use it over wildcard match
  77. match = netConf
  78. }
  79. }
  80. if match.Match != "" {
  81. err = applyNetConf(link, match)
  82. if err != nil {
  83. log.Errorf("Failed to apply settings to %s : %v", linkName, err)
  84. }
  85. }
  86. }
  87. if err != nil {
  88. return err
  89. }
  90. //post run
  91. if netCfg.PostRun != nil {
  92. return docker.StartAndWait(config.DOCKER_SYSTEM_HOST, netCfg.PostRun)
  93. }
  94. return nil
  95. }
  96. func applyNetConf(link netlink.Link, netConf config.InterfaceConfig) error {
  97. if netConf.DHCP {
  98. log.Infof("Running DHCP on %s", link.Attrs().Name)
  99. cmd := exec.Command("dhcpcd", "-A4", "-e", "force_hostname=true", link.Attrs().Name)
  100. cmd.Stdout = os.Stdout
  101. cmd.Stderr = os.Stderr
  102. if err := cmd.Run(); err != nil {
  103. log.Error(err)
  104. }
  105. } else if netConf.IPV4LL {
  106. if err := AssignLinkLocalIP(link); err != nil {
  107. log.Error("IPV4LL set failed")
  108. return err
  109. }
  110. } else if netConf.Address == "" {
  111. return nil
  112. } else {
  113. addr, err := netlink.ParseAddr(netConf.Address)
  114. if err != nil {
  115. return err
  116. }
  117. if err := netlink.AddrAdd(link, addr); err != nil {
  118. log.Error("addr add failed")
  119. return err
  120. }
  121. log.Infof("Set %s on %s", netConf.Address, link.Attrs().Name)
  122. }
  123. if netConf.MTU > 0 {
  124. if err := netlink.LinkSetMTU(link, netConf.MTU); err != nil {
  125. log.Error("set MTU Failed")
  126. return err
  127. }
  128. }
  129. if err := netlink.LinkSetUp(link); err != nil {
  130. log.Error("failed to setup link")
  131. return err
  132. }
  133. if netConf.Gateway != "" {
  134. gatewayIp := net.ParseIP(netConf.Gateway)
  135. if gatewayIp == nil {
  136. return errors.New("Invalid gateway address " + netConf.Gateway)
  137. }
  138. route := netlink.Route{
  139. Scope: netlink.SCOPE_UNIVERSE,
  140. Gw: net.ParseIP(netConf.Gateway),
  141. }
  142. if err := netlink.RouteAdd(&route); err != nil {
  143. log.Error("gateway set failed")
  144. return err
  145. }
  146. log.Infof("Set default gateway %s", netConf.Gateway)
  147. }
  148. return nil
  149. }
  150. func matches(link, conf string) bool {
  151. return glob.Glob(conf, link)
  152. }