packet.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package network
  15. import (
  16. "net"
  17. "github.com/rancher/os/pkg/netconf"
  18. )
  19. func ProcessPacketNetconf(netdata netconf.NetworkConfig) ([]InterfaceGenerator, error) {
  20. var nameservers []net.IP
  21. for _, v := range netdata.DNS.Nameservers {
  22. nameservers = append(nameservers, net.ParseIP(v))
  23. }
  24. if len(nameservers) == 0 {
  25. nameservers = append(nameservers, net.ParseIP("8.8.8.8"), net.ParseIP("8.8.4.4"))
  26. }
  27. generators, err := parseNetwork(netdata, nameservers)
  28. if err != nil {
  29. return nil, err
  30. }
  31. return generators, nil
  32. }
  33. func parseNetwork(netdata netconf.NetworkConfig, nameservers []net.IP) ([]InterfaceGenerator, error) {
  34. var interfaces []InterfaceGenerator
  35. var addresses []net.IPNet
  36. var routes []route
  37. // TODO: commented out because we don't use it - should combine with the code we do use...
  38. /* for _, netblock := range netdata.Netblocks {
  39. addresses = append(addresses, net.IPNet{
  40. IP: netblock.Address,
  41. Mask: net.IPMask(netblock.Netmask),
  42. })
  43. if netblock.Public == false {
  44. routes = append(routes, route{
  45. destination: net.IPNet{
  46. IP: net.IPv4(10, 0, 0, 0),
  47. Mask: net.IPv4Mask(255, 0, 0, 0),
  48. },
  49. gateway: netblock.Gateway,
  50. })
  51. } else {
  52. if netblock.AddressFamily == 4 {
  53. routes = append(routes, route{
  54. destination: net.IPNet{
  55. IP: net.IPv4zero,
  56. Mask: net.IPMask(net.IPv4zero),
  57. },
  58. gateway: netblock.Gateway,
  59. })
  60. } else {
  61. routes = append(routes, route{
  62. destination: net.IPNet{
  63. IP: net.IPv6zero,
  64. Mask: net.IPMask(net.IPv6zero),
  65. },
  66. gateway: netblock.Gateway,
  67. })
  68. }
  69. }
  70. }
  71. */
  72. bond := bondInterface{
  73. logicalInterface: logicalInterface{
  74. name: "bond0",
  75. config: configMethodStatic{
  76. addresses: addresses,
  77. nameservers: nameservers,
  78. routes: routes,
  79. },
  80. },
  81. options: map[string]string{
  82. "Mode": "802.3ad",
  83. "LACPTransmitRate": "fast",
  84. "MIIMonitorSec": ".2",
  85. "UpDelaySec": ".2",
  86. "DownDelaySec": ".2",
  87. },
  88. }
  89. //bond.hwaddr, _ = net.ParseMAC(netdata.Interfaces[0].Mac)
  90. index := 0
  91. for name := range netdata.Interfaces {
  92. bond.slaves = append(bond.slaves, name)
  93. interfaces = append(interfaces, &physicalInterface{
  94. logicalInterface: logicalInterface{
  95. name: name,
  96. config: configMethodStatic{
  97. nameservers: nameservers,
  98. },
  99. children: []networkInterface{&bond},
  100. configDepth: index,
  101. },
  102. })
  103. index = index + 1
  104. }
  105. interfaces = append(interfaces, &bond)
  106. return interfaces, nil
  107. }