gitee.com/leisunstar/runtime@v0.0.0-20200521203717-5cef3e7b53f9/virtcontainers/macvtap_endpoint.go (about)

     1  // Copyright (c) 2018 Intel Corporation
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  
     6  package virtcontainers
     7  
     8  import (
     9  	"fmt"
    10  	"os"
    11  
    12  	persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api"
    13  )
    14  
    15  // MacvtapEndpoint represents a macvtap endpoint
    16  type MacvtapEndpoint struct {
    17  	EndpointProperties NetworkInfo
    18  	EndpointType       EndpointType
    19  	VMFds              []*os.File
    20  	VhostFds           []*os.File
    21  	PCIAddr            string
    22  }
    23  
    24  func createMacvtapNetworkEndpoint(netInfo NetworkInfo) (*MacvtapEndpoint, error) {
    25  	endpoint := &MacvtapEndpoint{
    26  		EndpointType:       MacvtapEndpointType,
    27  		EndpointProperties: netInfo,
    28  	}
    29  
    30  	return endpoint, nil
    31  }
    32  
    33  // Properties returns the properties of the macvtap interface.
    34  func (endpoint *MacvtapEndpoint) Properties() NetworkInfo {
    35  	return endpoint.EndpointProperties
    36  }
    37  
    38  // HardwareAddr returns the mac address of the macvtap network interface.
    39  func (endpoint *MacvtapEndpoint) HardwareAddr() string {
    40  	return endpoint.EndpointProperties.Iface.HardwareAddr.String()
    41  }
    42  
    43  // Name returns name of the macvtap interface.
    44  func (endpoint *MacvtapEndpoint) Name() string {
    45  	return endpoint.EndpointProperties.Iface.Name
    46  }
    47  
    48  // Type indentifies the endpoint as a macvtap endpoint.
    49  func (endpoint *MacvtapEndpoint) Type() EndpointType {
    50  	return endpoint.EndpointType
    51  }
    52  
    53  // SetProperties sets the properties of the macvtap endpoint.
    54  func (endpoint *MacvtapEndpoint) SetProperties(properties NetworkInfo) {
    55  	endpoint.EndpointProperties = properties
    56  }
    57  
    58  // Attach for macvtap endpoint passes macvtap device to the hypervisor.
    59  func (endpoint *MacvtapEndpoint) Attach(h hypervisor) error {
    60  	var err error
    61  
    62  	endpoint.VMFds, err = createMacvtapFds(endpoint.EndpointProperties.Iface.Index, int(h.hypervisorConfig().NumVCPUs))
    63  	if err != nil {
    64  		return fmt.Errorf("Could not setup macvtap fds %s: %s", endpoint.EndpointProperties.Iface.Name, err)
    65  	}
    66  
    67  	if !h.hypervisorConfig().DisableVhostNet {
    68  		vhostFds, err := createVhostFds(int(h.hypervisorConfig().NumVCPUs))
    69  		if err != nil {
    70  			return fmt.Errorf("Could not setup vhost fds %s : %s", endpoint.EndpointProperties.Iface.Name, err)
    71  		}
    72  		endpoint.VhostFds = vhostFds
    73  	}
    74  
    75  	return h.addDevice(endpoint, netDev)
    76  }
    77  
    78  // Detach for macvtap endpoint does nothing.
    79  func (endpoint *MacvtapEndpoint) Detach(netNsCreated bool, netNsPath string) error {
    80  	return nil
    81  }
    82  
    83  // HotAttach for macvtap endpoint not supported yet
    84  func (endpoint *MacvtapEndpoint) HotAttach(h hypervisor) error {
    85  	return fmt.Errorf("MacvtapEndpoint does not support Hot attach")
    86  }
    87  
    88  // HotDetach for macvtap endpoint not supported yet
    89  func (endpoint *MacvtapEndpoint) HotDetach(h hypervisor, netNsCreated bool, netNsPath string) error {
    90  	return fmt.Errorf("MacvtapEndpoint does not support Hot detach")
    91  }
    92  
    93  // PciAddr returns the PCI address of the endpoint.
    94  func (endpoint *MacvtapEndpoint) PciAddr() string {
    95  	return endpoint.PCIAddr
    96  }
    97  
    98  // SetPciAddr sets the PCI address of the endpoint.
    99  func (endpoint *MacvtapEndpoint) SetPciAddr(pciAddr string) {
   100  	endpoint.PCIAddr = pciAddr
   101  }
   102  
   103  // NetworkPair returns the network pair of the endpoint.
   104  func (endpoint *MacvtapEndpoint) NetworkPair() *NetworkInterfacePair {
   105  	return nil
   106  }
   107  
   108  func (endpoint *MacvtapEndpoint) save() persistapi.NetworkEndpoint {
   109  	return persistapi.NetworkEndpoint{
   110  		Type: string(endpoint.Type()),
   111  
   112  		Macvtap: &persistapi.MacvtapEndpoint{
   113  			PCIAddr: endpoint.PCIAddr,
   114  		},
   115  	}
   116  }
   117  func (endpoint *MacvtapEndpoint) load(s persistapi.NetworkEndpoint) {
   118  	endpoint.EndpointType = MacvtapEndpointType
   119  
   120  	if s.Macvtap != nil {
   121  		endpoint.PCIAddr = s.Macvtap.PCIAddr
   122  	}
   123  }