netplan.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // Copyright 2019 Yunion
  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 netplan
  15. import "yunion.io/x/jsonutils"
  16. // Configuration examples reference from https://netplan.io/examples/
  17. // manpage: http://manpages.ubuntu.com/manpages/cosmic/man5/netplan.5.html
  18. type Configuration struct {
  19. Network *Network `json:"network"`
  20. }
  21. func NewConfiguration(network *Network) *Configuration {
  22. return &Configuration{
  23. Network: network,
  24. }
  25. }
  26. func (c *Configuration) YAMLString() string {
  27. return toYAMLString(c)
  28. }
  29. type NetworkRenderer string
  30. const (
  31. VERSION2 = 2
  32. NetworkRendererNetworkd NetworkRenderer = "networkd"
  33. )
  34. type Network struct {
  35. Version uint `json:"version"`
  36. Renderer NetworkRenderer `json:"renderer"`
  37. Ethernets map[string]*EthernetConfig `json:"ethernets"`
  38. Bonds map[string]*Bond `json:"bonds"`
  39. Vlans map[string]*VlanConfig `json:"vlans"`
  40. }
  41. func (n *Network) AddVlan(name string, vlan *VlanConfig) *Network {
  42. n.Vlans[name] = vlan
  43. return n
  44. }
  45. type EthernetConfigMatch struct {
  46. MacAddress string `json:"macaddress"`
  47. }
  48. func NewEthernetConfigMatchMac(macAddr string) *EthernetConfigMatch {
  49. return &EthernetConfigMatch{
  50. MacAddress: macAddr,
  51. }
  52. }
  53. type EthernetConfig struct {
  54. DHCP4 bool `json:"dhcp4,omitfalse"`
  55. DHCP6 bool `json:"dhcp6,omitfalse"`
  56. AcceptRa bool `json:"accept-ra,omitfalse"`
  57. Addresses []string `json:"addresses"`
  58. Match *EthernetConfigMatch `json:"match"`
  59. MacAddress string `json:"macaddress"`
  60. Gateway4 string `json:"gateway4"`
  61. Gateway6 string `json:"gateway6"`
  62. Routes []*Route `json:"routes"`
  63. Nameservers *Nameservers `json:"nameservers"`
  64. Mtu int16 `json:"mtu,omitzero"`
  65. }
  66. type VlanConfig struct {
  67. EthernetConfig
  68. Id int `json:"id"`
  69. Link string `json:"link"`
  70. }
  71. type Route struct {
  72. To string `json:"to"`
  73. Via string `json:"via"`
  74. Metric uint `json:"metric"`
  75. // OnLink bool `json:"on-link"`
  76. }
  77. type Nameservers struct {
  78. Search []string `json:"search"`
  79. Addresses []string `json:"addresses"`
  80. }
  81. type Bond struct {
  82. EthernetConfig
  83. Interfaces []string `json:"interfaces"`
  84. Parameters IBondModeParams `json:"parameters"`
  85. }
  86. func toYAMLString(obj interface{}) string {
  87. return jsonutils.Marshal(obj).YAMLString()
  88. }
  89. func (b *Bond) YAMLString() string {
  90. return toYAMLString(b)
  91. }
  92. type BondMode string
  93. const (
  94. // ref: https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/networking_guide/overview-of-bonding-modes-and-the-required-settings-on-the-switch
  95. // mode0
  96. bondModeBalanceRR = "balance-rr"
  97. // mode1
  98. bondModeActiveBackup = "active-backup"
  99. // mode4
  100. bondMode8023AD = "802.3ad"
  101. )
  102. type IBondModeParams interface {
  103. GetMode() string
  104. }
  105. type BondModeBaseParams struct {
  106. Mode string `json:"mode"`
  107. MiiMonitorInterval int `json:"mii-monitor-interval,omitzero"`
  108. GratuitiousArp int `json:"gratuitiousi-arp,omitzero"`
  109. }
  110. func (c BondModeBaseParams) GetMode() string {
  111. return c.Mode
  112. }
  113. func (c *BondModeBaseParams) SetMiiMonitorInterval(i int) {
  114. c.MiiMonitorInterval = i
  115. }
  116. func (c *BondModeBaseParams) SetGratutiousArp(g int) {
  117. c.GratuitiousArp = g
  118. }
  119. type BondModeActiveBackupParams struct {
  120. *BondModeBaseParams
  121. Primary string `json:"primary"`
  122. }
  123. func NewBondMode0Params() *BondModeBaseParams {
  124. return &BondModeBaseParams{
  125. Mode: bondModeBalanceRR,
  126. }
  127. }
  128. func NewBondModeActiveBackupParams(primary string) *BondModeActiveBackupParams {
  129. return &BondModeActiveBackupParams{
  130. BondModeBaseParams: &BondModeBaseParams{
  131. Mode: bondModeActiveBackup,
  132. },
  133. Primary: primary,
  134. }
  135. }
  136. type BondMode4Params struct {
  137. *BondModeBaseParams
  138. }
  139. func NewBondMode4Params() *BondMode4Params {
  140. return &BondMode4Params{
  141. BondModeBaseParams: &BondModeBaseParams{
  142. Mode: bondMode8023AD,
  143. },
  144. }
  145. }
  146. func NewNetwork() *Network {
  147. return &Network{
  148. Version: VERSION2,
  149. Renderer: NetworkRendererNetworkd,
  150. Ethernets: make(map[string]*EthernetConfig),
  151. Bonds: make(map[string]*Bond),
  152. Vlans: make(map[string]*VlanConfig),
  153. }
  154. }
  155. func (n *Network) AddEthernet(name string, ether *EthernetConfig) *Network {
  156. n.Ethernets[name] = ether
  157. return n
  158. }
  159. func (n *Network) AddBond(name string, bond *Bond) *Network {
  160. n.Bonds[name] = bond
  161. return n
  162. }
  163. func (n *Network) YAMLString() string {
  164. return toYAMLString(n)
  165. }
  166. func NewDHCPEthernetConfig() *EthernetConfig {
  167. return &EthernetConfig{}
  168. }
  169. func (c *EthernetConfig) EnableDHCP4() *EthernetConfig {
  170. c.DHCP4 = true
  171. return c
  172. }
  173. func (c *EthernetConfig) EnableDHCP6() *EthernetConfig {
  174. c.DHCP6 = true
  175. c.AcceptRa = true
  176. return c
  177. }
  178. func NewStaticEthernetConfig(
  179. addr string,
  180. addr6 string,
  181. gateway string,
  182. gateway6 string,
  183. search []string,
  184. nameservers []string,
  185. routes []*Route,
  186. ) *EthernetConfig {
  187. addrs := []string{
  188. addr,
  189. }
  190. if len(addr6) > 0 {
  191. addrs = append(addrs, addr6)
  192. }
  193. return &EthernetConfig{
  194. DHCP4: false,
  195. Addresses: addrs,
  196. Gateway4: gateway,
  197. Gateway6: gateway6,
  198. Routes: routes,
  199. Nameservers: &Nameservers{
  200. Search: search,
  201. Addresses: nameservers,
  202. },
  203. }
  204. }
  205. func (c *EthernetConfig) YAMLString() string {
  206. return toYAMLString(c)
  207. }
  208. func newBondModeByParams(conf *EthernetConfig, interfaces []string, params IBondModeParams) *Bond {
  209. return &Bond{
  210. EthernetConfig: *conf,
  211. Interfaces: interfaces,
  212. Parameters: params,
  213. }
  214. }
  215. func NewBondMode0(conf *EthernetConfig, interfaces []string) *Bond {
  216. params := NewBondMode0Params()
  217. params.SetMiiMonitorInterval(100)
  218. return newBondModeByParams(conf, interfaces, params)
  219. }
  220. func NewBondMode1(conf *EthernetConfig, interfaces []string) *Bond {
  221. params := NewBondModeActiveBackupParams(interfaces[0])
  222. params.SetMiiMonitorInterval(100)
  223. return newBondModeByParams(conf, interfaces, params)
  224. }
  225. func NewBondMode4(conf *EthernetConfig, interfaces []string) *Bond {
  226. params := NewBondMode4Params()
  227. // TODO: figure out what follows options related to netplan config
  228. // miimon: 1
  229. // lacp_rate: 1
  230. // xmit_hash_policy: 1
  231. params.SetMiiMonitorInterval(100)
  232. return newBondModeByParams(conf, interfaces, params)
  233. }