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