network.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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 NetworkBridge struct {
  30. Name string `xml:"name,attr,omitempty"`
  31. STP string `xml:"stp,attr,omitempty"`
  32. Delay string `xml:"delay,attr,omitempty"`
  33. MACTableManager string `xml:"macTableManager,attr,omitempty"`
  34. Zone string `xml:"zone,attr,omitempty"`
  35. }
  36. type NetworkVirtualPort struct {
  37. Params *NetworkVirtualPortParams `xml:"parameters"`
  38. }
  39. type NetworkVirtualPortParams struct {
  40. Any *NetworkVirtualPortParamsAny `xml:"-"`
  41. VEPA8021QBG *NetworkVirtualPortParamsVEPA8021QBG `xml:"-"`
  42. VNTag8011QBH *NetworkVirtualPortParamsVNTag8021QBH `xml:"-"`
  43. OpenVSwitch *NetworkVirtualPortParamsOpenVSwitch `xml:"-"`
  44. MidoNet *NetworkVirtualPortParamsMidoNet `xml:"-"`
  45. }
  46. type NetworkVirtualPortParamsAny struct {
  47. ManagerID *uint `xml:"managerid,attr"`
  48. TypeID *uint `xml:"typeid,attr"`
  49. TypeIDVersion *uint `xml:"typeidversion,attr"`
  50. InstanceID string `xml:"instanceid,attr,omitempty"`
  51. ProfileID string `xml:"profileid,attr,omitempty"`
  52. InterfaceID string `xml:"interfaceid,attr,omitempty"`
  53. }
  54. type NetworkVirtualPortParamsVEPA8021QBG struct {
  55. ManagerID *uint `xml:"managerid,attr"`
  56. TypeID *uint `xml:"typeid,attr"`
  57. TypeIDVersion *uint `xml:"typeidversion,attr"`
  58. InstanceID string `xml:"instanceid,attr,omitempty"`
  59. }
  60. type NetworkVirtualPortParamsVNTag8021QBH struct {
  61. ProfileID string `xml:"profileid,attr,omitempty"`
  62. }
  63. type NetworkVirtualPortParamsOpenVSwitch struct {
  64. InterfaceID string `xml:"interfaceid,attr,omitempty"`
  65. ProfileID string `xml:"profileid,attr,omitempty"`
  66. }
  67. type NetworkVirtualPortParamsMidoNet struct {
  68. InterfaceID string `xml:"interfaceid,attr,omitempty"`
  69. }
  70. type NetworkDomain struct {
  71. Name string `xml:"name,attr,omitempty"`
  72. LocalOnly string `xml:"localOnly,attr,omitempty"`
  73. }
  74. type NetworkForwardNATAddress struct {
  75. Start string `xml:"start,attr"`
  76. End string `xml:"end,attr"`
  77. }
  78. type NetworkForwardNATPort struct {
  79. Start uint `xml:"start,attr"`
  80. End uint `xml:"end,attr"`
  81. }
  82. type NetworkForwardNAT struct {
  83. Addresses []NetworkForwardNATAddress `xml:"address"`
  84. Ports []NetworkForwardNATPort `xml:"port"`
  85. }
  86. type NetworkForward struct {
  87. Mode string `xml:"mode,attr,omitempty"`
  88. Dev string `xml:"dev,attr,omitempty"`
  89. Managed string `xml:"managed,attr,omitempty"`
  90. Driver *NetworkForwardDriver `xml:"driver"`
  91. PFs []NetworkForwardPF `xml:"pf"`
  92. NAT *NetworkForwardNAT `xml:"nat"`
  93. Interfaces []NetworkForwardInterface `xml:"interface"`
  94. Addresses []NetworkForwardAddress `xml:"address"`
  95. }
  96. type NetworkForwardDriver struct {
  97. Name string `xml:"name,attr"`
  98. }
  99. type NetworkForwardPF struct {
  100. Dev string `xml:"dev,attr"`
  101. }
  102. type NetworkForwardAddress struct {
  103. PCI *NetworkForwardAddressPCI `xml:"-"`
  104. }
  105. type NetworkForwardAddressPCI struct {
  106. Domain *uint `xml:"domain,attr"`
  107. Bus *uint `xml:"bus,attr"`
  108. Slot *uint `xml:"slot,attr"`
  109. Function *uint `xml:"function,attr"`
  110. }
  111. type NetworkForwardInterface struct {
  112. XMLName xml.Name `xml:"interface"`
  113. Dev string `xml:"dev,attr,omitempty"`
  114. }
  115. type NetworkMAC struct {
  116. Address string `xml:"address,attr,omitempty"`
  117. }
  118. type NetworkDHCPRange struct {
  119. XMLName xml.Name `xml:"range"`
  120. Start string `xml:"start,attr,omitempty"`
  121. End string `xml:"end,attr,omitempty"`
  122. }
  123. type NetworkDHCPHost struct {
  124. XMLName xml.Name `xml:"host"`
  125. ID string `xml:"id,attr,omitempty"`
  126. MAC string `xml:"mac,attr,omitempty"`
  127. Name string `xml:"name,attr,omitempty"`
  128. IP string `xml:"ip,attr,omitempty"`
  129. }
  130. type NetworkBootp struct {
  131. File string `xml:"file,attr,omitempty"`
  132. Server string `xml:"server,attr,omitempty"`
  133. }
  134. type NetworkDHCP struct {
  135. Ranges []NetworkDHCPRange `xml:"range"`
  136. Hosts []NetworkDHCPHost `xml:"host"`
  137. Bootp []NetworkBootp `xml:"bootp"`
  138. }
  139. type NetworkIP struct {
  140. Address string `xml:"address,attr,omitempty"`
  141. Family string `xml:"family,attr,omitempty"`
  142. Netmask string `xml:"netmask,attr,omitempty"`
  143. Prefix uint `xml:"prefix,attr,omitempty"`
  144. LocalPtr string `xml:"localPtr,attr,omitempty"`
  145. DHCP *NetworkDHCP `xml:"dhcp"`
  146. TFTP *NetworkTFTP `xml:"tftp"`
  147. }
  148. type NetworkTFTP struct {
  149. Root string `xml:"root,attr,omitempty"`
  150. }
  151. type NetworkRoute struct {
  152. Family string `xml:"family,attr,omitempty"`
  153. Address string `xml:"address,attr,omitempty"`
  154. Netmask string `xml:"netmask,attr,omitempty"`
  155. Prefix uint `xml:"prefix,attr,omitempty"`
  156. Gateway string `xml:"gateway,attr,omitempty"`
  157. Metric string `xml:"metric,attr,omitempty"`
  158. }
  159. type NetworkDNSForwarder struct {
  160. Domain string `xml:"domain,attr,omitempty"`
  161. Addr string `xml:"addr,attr,omitempty"`
  162. }
  163. type NetworkDNSTXT struct {
  164. XMLName xml.Name `xml:"txt"`
  165. Name string `xml:"name,attr"`
  166. Value string `xml:"value,attr"`
  167. }
  168. type NetworkDNSHostHostname struct {
  169. Hostname string `xml:",chardata"`
  170. }
  171. type NetworkDNSHost struct {
  172. XMLName xml.Name `xml:"host"`
  173. IP string `xml:"ip,attr"`
  174. Hostnames []NetworkDNSHostHostname `xml:"hostname"`
  175. }
  176. type NetworkDNSSRV struct {
  177. XMLName xml.Name `xml:"srv"`
  178. Service string `xml:"service,attr,omitempty"`
  179. Protocol string `xml:"protocol,attr,omitempty"`
  180. Target string `xml:"target,attr,omitempty"`
  181. Port uint `xml:"port,attr,omitempty"`
  182. Priority uint `xml:"priority,attr,omitempty"`
  183. Weight uint `xml:"weight,attr,omitempty"`
  184. Domain string `xml:"domain,attr,omitempty"`
  185. }
  186. type NetworkDNS struct {
  187. Enable string `xml:"enable,attr,omitempty"`
  188. ForwardPlainNames string `xml:"forwardPlainNames,attr,omitempty"`
  189. Forwarders []NetworkDNSForwarder `xml:"forwarder"`
  190. TXTs []NetworkDNSTXT `xml:"txt"`
  191. Host []NetworkDNSHost `xml:"host"`
  192. SRVs []NetworkDNSSRV `xml:"srv"`
  193. }
  194. type NetworkMetadata struct {
  195. XML string `xml:",innerxml"`
  196. }
  197. type NetworkMTU struct {
  198. Size uint `xml:"size,attr"`
  199. }
  200. type Network struct {
  201. XMLName xml.Name `xml:"network"`
  202. IPv6 string `xml:"ipv6,attr,omitempty"`
  203. TrustGuestRxFilters string `xml:"trustGuestRxFilters,attr,omitempty"`
  204. Name string `xml:"name,omitempty"`
  205. UUID string `xml:"uuid,omitempty"`
  206. Metadata *NetworkMetadata `xml:"metadata"`
  207. Forward *NetworkForward `xml:"forward"`
  208. Bridge *NetworkBridge `xml:"bridge"`
  209. MTU *NetworkMTU `xml:"mtu"`
  210. MAC *NetworkMAC `xml:"mac"`
  211. Domain *NetworkDomain `xml:"domain"`
  212. DNS *NetworkDNS `xml:"dns"`
  213. VLAN *NetworkVLAN `xml:"vlan"`
  214. Bandwidth *NetworkBandwidth `xml:"bandwidth"`
  215. IPs []NetworkIP `xml:"ip"`
  216. Routes []NetworkRoute `xml:"route"`
  217. VirtualPort *NetworkVirtualPort `xml:"virtualport"`
  218. PortGroups []NetworkPortGroup `xml:"portgroup"`
  219. }
  220. type NetworkPortGroup struct {
  221. XMLName xml.Name `xml:"portgroup"`
  222. Name string `xml:"name,attr,omitempty"`
  223. Default string `xml:"default,attr,omitempty"`
  224. TrustGuestRxFilters string `xml:"trustGuestRxFilters,attr,omitempty"`
  225. VLAN *NetworkVLAN `xml:"vlan"`
  226. VirtualPort *NetworkVirtualPort `xml:"virtualport"`
  227. }
  228. type NetworkVLAN struct {
  229. Trunk string `xml:"trunk,attr,omitempty"`
  230. Tags []NetworkVLANTag `xml:"tag"`
  231. }
  232. type NetworkVLANTag struct {
  233. ID uint `xml:"id,attr"`
  234. NativeMode string `xml:"nativeMode,attr,omitempty"`
  235. }
  236. type NetworkBandwidthParams struct {
  237. Average *uint `xml:"average,attr"`
  238. Peak *uint `xml:"peak,attr"`
  239. Burst *uint `xml:"burst,attr"`
  240. Floor *uint `xml:"floor,attr"`
  241. }
  242. type NetworkBandwidth struct {
  243. Inbound *NetworkBandwidthParams `xml:"inbound"`
  244. Outbound *NetworkBandwidthParams `xml:"outbound"`
  245. }
  246. func (a *NetworkVirtualPortParams) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  247. start.Name.Local = "parameters"
  248. if a.Any != nil {
  249. return e.EncodeElement(a.Any, start)
  250. } else if a.VEPA8021QBG != nil {
  251. return e.EncodeElement(a.VEPA8021QBG, start)
  252. } else if a.VNTag8011QBH != nil {
  253. return e.EncodeElement(a.VNTag8011QBH, start)
  254. } else if a.OpenVSwitch != nil {
  255. return e.EncodeElement(a.OpenVSwitch, start)
  256. } else if a.MidoNet != nil {
  257. return e.EncodeElement(a.MidoNet, start)
  258. }
  259. return nil
  260. }
  261. func (a *NetworkVirtualPortParams) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
  262. if a.Any != nil {
  263. return d.DecodeElement(a.Any, &start)
  264. } else if a.VEPA8021QBG != nil {
  265. return d.DecodeElement(a.VEPA8021QBG, &start)
  266. } else if a.VNTag8011QBH != nil {
  267. return d.DecodeElement(a.VNTag8011QBH, &start)
  268. } else if a.OpenVSwitch != nil {
  269. return d.DecodeElement(a.OpenVSwitch, &start)
  270. } else if a.MidoNet != nil {
  271. return d.DecodeElement(a.MidoNet, &start)
  272. }
  273. return nil
  274. }
  275. type networkVirtualPort NetworkVirtualPort
  276. func (a *NetworkVirtualPort) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  277. start.Name.Local = "virtualport"
  278. if a.Params != nil {
  279. if a.Params.Any != nil {
  280. /* no type attr wanted */
  281. } else if a.Params.VEPA8021QBG != nil {
  282. start.Attr = append(start.Attr, xml.Attr{
  283. xml.Name{Local: "type"}, "802.1Qbg",
  284. })
  285. } else if a.Params.VNTag8011QBH != nil {
  286. start.Attr = append(start.Attr, xml.Attr{
  287. xml.Name{Local: "type"}, "802.1Qbh",
  288. })
  289. } else if a.Params.OpenVSwitch != nil {
  290. start.Attr = append(start.Attr, xml.Attr{
  291. xml.Name{Local: "type"}, "openvswitch",
  292. })
  293. } else if a.Params.MidoNet != nil {
  294. start.Attr = append(start.Attr, xml.Attr{
  295. xml.Name{Local: "type"}, "midonet",
  296. })
  297. }
  298. }
  299. vp := networkVirtualPort(*a)
  300. return e.EncodeElement(&vp, start)
  301. }
  302. func (a *NetworkVirtualPort) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
  303. typ, ok := getAttr(start.Attr, "type")
  304. a.Params = &NetworkVirtualPortParams{}
  305. if !ok {
  306. var any NetworkVirtualPortParamsAny
  307. a.Params.Any = &any
  308. } else if typ == "802.1Qbg" {
  309. var vepa NetworkVirtualPortParamsVEPA8021QBG
  310. a.Params.VEPA8021QBG = &vepa
  311. } else if typ == "802.1Qbh" {
  312. var vntag NetworkVirtualPortParamsVNTag8021QBH
  313. a.Params.VNTag8011QBH = &vntag
  314. } else if typ == "openvswitch" {
  315. var ovs NetworkVirtualPortParamsOpenVSwitch
  316. a.Params.OpenVSwitch = &ovs
  317. } else if typ == "midonet" {
  318. var mido NetworkVirtualPortParamsMidoNet
  319. a.Params.MidoNet = &mido
  320. }
  321. vp := networkVirtualPort(*a)
  322. err := d.DecodeElement(&vp, &start)
  323. if err != nil {
  324. return err
  325. }
  326. *a = NetworkVirtualPort(vp)
  327. return nil
  328. }
  329. func (a *NetworkForwardAddressPCI) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  330. marshalUintAttr(&start, "domain", a.Domain, "0x%04x")
  331. marshalUintAttr(&start, "bus", a.Bus, "0x%02x")
  332. marshalUintAttr(&start, "slot", a.Slot, "0x%02x")
  333. marshalUintAttr(&start, "function", a.Function, "0x%x")
  334. e.EncodeToken(start)
  335. e.EncodeToken(start.End())
  336. return nil
  337. }
  338. func (a *NetworkForwardAddressPCI) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
  339. for _, attr := range start.Attr {
  340. if attr.Name.Local == "domain" {
  341. if err := unmarshalUintAttr(attr.Value, &a.Domain, 0); err != nil {
  342. return err
  343. }
  344. } else if attr.Name.Local == "bus" {
  345. if err := unmarshalUintAttr(attr.Value, &a.Bus, 0); err != nil {
  346. return err
  347. }
  348. } else if attr.Name.Local == "slot" {
  349. if err := unmarshalUintAttr(attr.Value, &a.Slot, 0); err != nil {
  350. return err
  351. }
  352. } else if attr.Name.Local == "function" {
  353. if err := unmarshalUintAttr(attr.Value, &a.Function, 0); err != nil {
  354. return err
  355. }
  356. }
  357. }
  358. d.Skip()
  359. return nil
  360. }
  361. func (a *NetworkForwardAddress) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  362. if a.PCI != nil {
  363. start.Attr = append(start.Attr, xml.Attr{
  364. xml.Name{Local: "type"}, "pci",
  365. })
  366. return e.EncodeElement(a.PCI, start)
  367. } else {
  368. return nil
  369. }
  370. }
  371. func (a *NetworkForwardAddress) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
  372. var typ string
  373. for _, attr := range start.Attr {
  374. if attr.Name.Local == "type" {
  375. typ = attr.Value
  376. break
  377. }
  378. }
  379. if typ == "" {
  380. d.Skip()
  381. return nil
  382. }
  383. if typ == "pci" {
  384. a.PCI = &NetworkForwardAddressPCI{}
  385. return d.DecodeElement(a.PCI, &start)
  386. }
  387. return nil
  388. }
  389. func (s *NetworkDHCPHost) Unmarshal(doc string) error {
  390. return xml.Unmarshal([]byte(doc), s)
  391. }
  392. func (s *NetworkDHCPHost) Marshal() (string, error) {
  393. doc, err := xml.MarshalIndent(s, "", " ")
  394. if err != nil {
  395. return "", err
  396. }
  397. return string(doc), nil
  398. }
  399. func (s *NetworkDNSHost) Unmarshal(doc string) error {
  400. return xml.Unmarshal([]byte(doc), s)
  401. }
  402. func (s *NetworkDNSHost) Marshal() (string, error) {
  403. doc, err := xml.MarshalIndent(s, "", " ")
  404. if err != nil {
  405. return "", err
  406. }
  407. return string(doc), nil
  408. }
  409. func (s *NetworkPortGroup) Unmarshal(doc string) error {
  410. return xml.Unmarshal([]byte(doc), s)
  411. }
  412. func (s *NetworkPortGroup) Marshal() (string, error) {
  413. doc, err := xml.MarshalIndent(s, "", " ")
  414. if err != nil {
  415. return "", err
  416. }
  417. return string(doc), nil
  418. }
  419. func (s *NetworkDNSTXT) Unmarshal(doc string) error {
  420. return xml.Unmarshal([]byte(doc), s)
  421. }
  422. func (s *NetworkDNSTXT) Marshal() (string, error) {
  423. doc, err := xml.MarshalIndent(s, "", " ")
  424. if err != nil {
  425. return "", err
  426. }
  427. return string(doc), nil
  428. }
  429. func (s *NetworkDNSSRV) Unmarshal(doc string) error {
  430. return xml.Unmarshal([]byte(doc), s)
  431. }
  432. func (s *NetworkDNSSRV) Marshal() (string, error) {
  433. doc, err := xml.MarshalIndent(s, "", " ")
  434. if err != nil {
  435. return "", err
  436. }
  437. return string(doc), nil
  438. }
  439. func (s *NetworkDHCPRange) Unmarshal(doc string) error {
  440. return xml.Unmarshal([]byte(doc), s)
  441. }
  442. func (s *NetworkDHCPRange) Marshal() (string, error) {
  443. doc, err := xml.MarshalIndent(s, "", " ")
  444. if err != nil {
  445. return "", err
  446. }
  447. return string(doc), nil
  448. }
  449. func (s *NetworkForwardInterface) Unmarshal(doc string) error {
  450. return xml.Unmarshal([]byte(doc), s)
  451. }
  452. func (s *NetworkForwardInterface) Marshal() (string, error) {
  453. doc, err := xml.MarshalIndent(s, "", " ")
  454. if err != nil {
  455. return "", err
  456. }
  457. return string(doc), nil
  458. }
  459. func (s *Network) Unmarshal(doc string) error {
  460. return xml.Unmarshal([]byte(doc), s)
  461. }
  462. func (s *Network) Marshal() (string, error) {
  463. doc, err := xml.MarshalIndent(s, "", " ")
  464. if err != nil {
  465. return "", err
  466. }
  467. return string(doc), nil
  468. }