interface.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * This file is part of the libvirt-go-xml project
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. *
  22. * Copyright (C) 2017 Lian Duan <blazeblue@gmail.com>
  23. *
  24. */
  25. package libvirtxml
  26. import (
  27. "encoding/xml"
  28. )
  29. type Interface struct {
  30. XMLName xml.Name `xml:"interface"`
  31. Name string `xml:"name,attr,omitempty"`
  32. Start *InterfaceStart `xml:"start"`
  33. MTU *InterfaceMTU `xml:"mtu"`
  34. Protocol []InterfaceProtocol `xml:"protocol"`
  35. Link *InterfaceLink `xml:"link"`
  36. MAC *InterfaceMAC `xml:"mac"`
  37. Bond *InterfaceBond `xml:"bond"`
  38. Bridge *InterfaceBridge `xml:"bridge"`
  39. VLAN *InterfaceVLAN `xml:"vlan"`
  40. }
  41. type InterfaceStart struct {
  42. Mode string `xml:"mode,attr"`
  43. }
  44. type InterfaceMTU struct {
  45. Size uint `xml:"size,attr"`
  46. }
  47. type InterfaceProtocol struct {
  48. Family string `xml:"family,attr,omitempty"`
  49. AutoConf *InterfaceAutoConf `xml:"autoconf"`
  50. DHCP *InterfaceDHCP `xml:"dhcp"`
  51. IPs []InterfaceIP `xml:"ip"`
  52. Route []InterfaceRoute `xml:"route"`
  53. }
  54. type InterfaceAutoConf struct {
  55. }
  56. type InterfaceDHCP struct {
  57. PeerDNS string `xml:"peerdns,attr,omitempty"`
  58. }
  59. type InterfaceIP struct {
  60. Address string `xml:"address,attr"`
  61. Prefix uint `xml:"prefix,attr,omitempty"`
  62. }
  63. type InterfaceRoute struct {
  64. Gateway string `xml:"gateway,attr"`
  65. }
  66. type InterfaceLink struct {
  67. Speed uint `xml:"speed,attr,omitempty"`
  68. State string `xml:"state,attr,omitempty"`
  69. }
  70. type InterfaceMAC struct {
  71. Address string `xml:"address,attr"`
  72. }
  73. type InterfaceBond struct {
  74. Mode string `xml:"mode,attr,omitempty"`
  75. ARPMon *InterfaceBondARPMon `xml:"arpmon"`
  76. MIIMon *InterfaceBondMIIMon `xml:"miimon"`
  77. Interfaces []Interface `xml:"interface"`
  78. }
  79. type InterfaceBondARPMon struct {
  80. Interval uint `xml:"interval,attr,omitempty"`
  81. Target string `xml:"target,attr,omitempty"`
  82. Validate string `xml:"validate,attr,omitempty"`
  83. }
  84. type InterfaceBondMIIMon struct {
  85. Freq uint `xml:"freq,attr,omitempty"`
  86. UpDelay uint `xml:"updelay,attr,omitempty"`
  87. Carrier string `xml:"carrier,attr,omitempty"`
  88. }
  89. type InterfaceBridge struct {
  90. STP string `xml:"stp,attr,omitempty"`
  91. Delay *float64 `xml:"delay,attr"`
  92. Interfaces []Interface `xml:"interface"`
  93. }
  94. type InterfaceVLAN struct {
  95. Tag *uint `xml:"tag,attr"`
  96. Interface *Interface `xml:"interface"`
  97. }
  98. type interfaceDup Interface
  99. func (s *Interface) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  100. start.Name.Local = "interface"
  101. typ := "ethernet"
  102. if s.Bond != nil {
  103. typ = "bond"
  104. } else if s.Bridge != nil {
  105. typ = "bridge"
  106. } else if s.VLAN != nil {
  107. typ = "vlan"
  108. }
  109. start.Attr = append(start.Attr, xml.Attr{
  110. Name: xml.Name{Local: "type"},
  111. Value: typ,
  112. })
  113. i := interfaceDup(*s)
  114. return e.EncodeElement(i, start)
  115. }
  116. func (s *Interface) Unmarshal(doc string) error {
  117. return xml.Unmarshal([]byte(doc), s)
  118. }
  119. func (s *Interface) Marshal() (string, error) {
  120. doc, err := xml.MarshalIndent(s, "", " ")
  121. if err != nil {
  122. return "", err
  123. }
  124. return string(doc), nil
  125. }