gitee.com/zeroway/libvirt-go-xml@v7.4.0+incompatible/network_port.go (about)

     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) 2019 Red Hat, Inc.
    23   *
    24   */
    25  
    26  package libvirtxml
    27  
    28  import (
    29  	"encoding/xml"
    30  	"fmt"
    31  )
    32  
    33  type NetworkPort struct {
    34  	XMLName     xml.Name                `xml:"networkport"`
    35  	UUID        string                  `xml:"uuid,omitempty"`
    36  	Owner       *NetworkPortOwner       `xml:"owner",`
    37  	MAC         *NetworkPortMAC         `xml:"mac"`
    38  	Group       string                  `xml:"group,omitempty"`
    39  	Bandwidth   *NetworkBandwidth       `xml:"bandwidth"`
    40  	VLAN        *NetworkPortVLAN        `xml:"vlan"`
    41  	PortOptions *NetworkPortPortOptions `xml:"port"`
    42  	VirtualPort *NetworkVirtualPort     `xml:"virtualport"`
    43  	RXFilters   *NetworkPortRXFilters   `xml:"rxfilters"`
    44  	Plug        *NetworkPortPlug        `xml:"plug"`
    45  }
    46  
    47  type NetworkPortPortOptions struct {
    48  	Isolated string `xml:"isolated,attr,omitempty"`
    49  }
    50  
    51  type NetworkPortVLAN struct {
    52  	Trunk string               `xml:"trunk,attr,omitempty"`
    53  	Tags  []NetworkPortVLANTag `xml:"tag"`
    54  }
    55  
    56  type NetworkPortVLANTag struct {
    57  	ID         uint   `xml:"id,attr"`
    58  	NativeMode string `xml:"nativeMode,attr,omitempty"`
    59  }
    60  
    61  type NetworkPortOwner struct {
    62  	UUID string `xml:"uuid,omitempty"`
    63  	Name string `xml:"name,omitempty"`
    64  }
    65  
    66  type NetworkPortMAC struct {
    67  	Address string `xml:"address,attr"`
    68  }
    69  
    70  type NetworkPortRXFilters struct {
    71  	TrustGuest string `xml:"trustGuest,attr"`
    72  }
    73  
    74  type NetworkPortPlug struct {
    75  	Bridge     *NetworkPortPlugBridge     `xml:"-"`
    76  	Network    *NetworkPortPlugNetwork    `xml:"-"`
    77  	Direct     *NetworkPortPlugDirect     `xml:"-"`
    78  	HostDevPCI *NetworkPortPlugHostDevPCI `xml:"-"`
    79  }
    80  
    81  type NetworkPortPlugBridge struct {
    82  	Bridge          string `xml:"bridge,attr"`
    83  	MacTableManager string `xml:"macTableManager,attr,omitempty"`
    84  }
    85  
    86  type NetworkPortPlugNetwork struct {
    87  	Bridge          string `xml:"bridge,attr"`
    88  	MacTableManager string `xml:"macTableManager,attr,omitempty"`
    89  }
    90  
    91  type NetworkPortPlugDirect struct {
    92  	Dev  string `xml:"dev,attr"`
    93  	Mode string `xml:"mode,attr"`
    94  }
    95  
    96  type NetworkPortPlugHostDevPCI struct {
    97  	Managed string                            `xml:"managed,attr,omitempty"`
    98  	Driver  *NetworkPortPlugHostDevPCIDriver  `xml:"driver"`
    99  	Address *NetworkPortPlugHostDevPCIAddress `xml:"address"`
   100  }
   101  
   102  type NetworkPortPlugHostDevPCIDriver struct {
   103  	Name string `xml:"name,attr"`
   104  }
   105  
   106  type NetworkPortPlugHostDevPCIAddress struct {
   107  	Domain   *uint `xml:"domain,attr"`
   108  	Bus      *uint `xml:"bus,attr"`
   109  	Slot     *uint `xml:"slot,attr"`
   110  	Function *uint `xml:"function,attr"`
   111  }
   112  
   113  func (a *NetworkPortPlugHostDevPCIAddress) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
   114  	marshalUintAttr(&start, "domain", a.Domain, "0x%04x")
   115  	marshalUintAttr(&start, "bus", a.Bus, "0x%02x")
   116  	marshalUintAttr(&start, "slot", a.Slot, "0x%02x")
   117  	marshalUintAttr(&start, "function", a.Function, "0x%x")
   118  	e.EncodeToken(start)
   119  	e.EncodeToken(start.End())
   120  	return nil
   121  }
   122  
   123  func (a *NetworkPortPlugHostDevPCIAddress) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
   124  	for _, attr := range start.Attr {
   125  		if attr.Name.Local == "domain" {
   126  			if err := unmarshalUintAttr(attr.Value, &a.Domain, 0); err != nil {
   127  				return err
   128  			}
   129  		} else if attr.Name.Local == "bus" {
   130  			if err := unmarshalUintAttr(attr.Value, &a.Bus, 0); err != nil {
   131  				return err
   132  			}
   133  		} else if attr.Name.Local == "slot" {
   134  			if err := unmarshalUintAttr(attr.Value, &a.Slot, 0); err != nil {
   135  				return err
   136  			}
   137  		} else if attr.Name.Local == "function" {
   138  			if err := unmarshalUintAttr(attr.Value, &a.Function, 0); err != nil {
   139  				return err
   140  			}
   141  		}
   142  	}
   143  	d.Skip()
   144  	return nil
   145  }
   146  
   147  func (p *NetworkPortPlug) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
   148  	start.Name.Local = "plug"
   149  	if p.Bridge != nil {
   150  		start.Attr = append(start.Attr, xml.Attr{
   151  			xml.Name{Local: "type"}, "bridge",
   152  		})
   153  		return e.EncodeElement(p.Bridge, start)
   154  	} else if p.Network != nil {
   155  		start.Attr = append(start.Attr, xml.Attr{
   156  			xml.Name{Local: "type"}, "network",
   157  		})
   158  		return e.EncodeElement(p.Network, start)
   159  	} else if p.Direct != nil {
   160  		start.Attr = append(start.Attr, xml.Attr{
   161  			xml.Name{Local: "type"}, "direct",
   162  		})
   163  		return e.EncodeElement(p.Direct, start)
   164  	} else if p.HostDevPCI != nil {
   165  		start.Attr = append(start.Attr, xml.Attr{
   166  			xml.Name{Local: "type"}, "hostdev-pci",
   167  		})
   168  		return e.EncodeElement(p.HostDevPCI, start)
   169  	}
   170  	return nil
   171  }
   172  
   173  func (p *NetworkPortPlug) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
   174  	typ, ok := getAttr(start.Attr, "type")
   175  	if !ok {
   176  		return fmt.Errorf("Missing type attribute on plug")
   177  	} else if typ == "bridge" {
   178  		var pb NetworkPortPlugBridge
   179  		if err := d.DecodeElement(&pb, &start); err != nil {
   180  			return err
   181  		}
   182  		p.Bridge = &pb
   183  	} else if typ == "network" {
   184  		var pn NetworkPortPlugNetwork
   185  		if err := d.DecodeElement(&pn, &start); err != nil {
   186  			return err
   187  		}
   188  		p.Network = &pn
   189  	} else if typ == "direct" {
   190  		var pd NetworkPortPlugDirect
   191  		if err := d.DecodeElement(&pd, &start); err != nil {
   192  			return err
   193  		}
   194  		p.Direct = &pd
   195  	} else if typ == "hostdev-pci" {
   196  		var ph NetworkPortPlugHostDevPCI
   197  		if err := d.DecodeElement(&ph, &start); err != nil {
   198  			return err
   199  		}
   200  		p.HostDevPCI = &ph
   201  	}
   202  	d.Skip()
   203  	return nil
   204  }
   205  
   206  func (s *NetworkPort) Unmarshal(doc string) error {
   207  	return xml.Unmarshal([]byte(doc), s)
   208  }
   209  
   210  func (s *NetworkPort) Marshal() (string, error) {
   211  	doc, err := xml.MarshalIndent(s, "", "  ")
   212  	if err != nil {
   213  		return "", err
   214  	}
   215  	return string(doc), nil
   216  }