gitee.com/leisunstar/runtime@v0.0.0-20200521203717-5cef3e7b53f9/virtcontainers/ipvlan_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  
    11  	"github.com/containernetworking/plugins/pkg/ns"
    12  	persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api"
    13  )
    14  
    15  // IPVlanEndpoint represents a ipvlan endpoint that is bridged to the VM
    16  type IPVlanEndpoint struct {
    17  	NetPair            NetworkInterfacePair
    18  	EndpointProperties NetworkInfo
    19  	EndpointType       EndpointType
    20  	PCIAddr            string
    21  }
    22  
    23  func createIPVlanNetworkEndpoint(idx int, ifName string) (*IPVlanEndpoint, error) {
    24  	if idx < 0 {
    25  		return &IPVlanEndpoint{}, fmt.Errorf("invalid network endpoint index: %d", idx)
    26  	}
    27  
    28  	// Use tc filtering for ipvlan, since the other inter networking models will
    29  	// not work for ipvlan.
    30  	interworkingModel := NetXConnectTCFilterModel
    31  	netPair, err := createNetworkInterfacePair(idx, ifName, interworkingModel)
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  
    36  	endpoint := &IPVlanEndpoint{
    37  		NetPair:      netPair,
    38  		EndpointType: IPVlanEndpointType,
    39  	}
    40  	if ifName != "" {
    41  		endpoint.NetPair.VirtIface.Name = ifName
    42  	}
    43  
    44  	return endpoint, nil
    45  }
    46  
    47  // Properties returns properties of the interface.
    48  func (endpoint *IPVlanEndpoint) Properties() NetworkInfo {
    49  	return endpoint.EndpointProperties
    50  }
    51  
    52  // Name returns name of the veth interface in the network pair.
    53  func (endpoint *IPVlanEndpoint) Name() string {
    54  	return endpoint.NetPair.VirtIface.Name
    55  }
    56  
    57  // HardwareAddr returns the mac address that is assigned to the tap interface
    58  // in th network pair.
    59  func (endpoint *IPVlanEndpoint) HardwareAddr() string {
    60  	return endpoint.NetPair.TAPIface.HardAddr
    61  }
    62  
    63  // Type identifies the endpoint as a virtual endpoint.
    64  func (endpoint *IPVlanEndpoint) Type() EndpointType {
    65  	return endpoint.EndpointType
    66  }
    67  
    68  // SetProperties sets the properties for the endpoint.
    69  func (endpoint *IPVlanEndpoint) SetProperties(properties NetworkInfo) {
    70  	endpoint.EndpointProperties = properties
    71  }
    72  
    73  // PciAddr returns the PCI address of the endpoint.
    74  func (endpoint *IPVlanEndpoint) PciAddr() string {
    75  	return endpoint.PCIAddr
    76  }
    77  
    78  // SetPciAddr sets the PCI address of the endpoint.
    79  func (endpoint *IPVlanEndpoint) SetPciAddr(pciAddr string) {
    80  	endpoint.PCIAddr = pciAddr
    81  }
    82  
    83  // NetworkPair returns the network pair of the endpoint.
    84  func (endpoint *IPVlanEndpoint) NetworkPair() *NetworkInterfacePair {
    85  	return &endpoint.NetPair
    86  }
    87  
    88  // Attach for virtual endpoint bridges the network pair and adds the
    89  // tap interface of the network pair to the hypervisor.
    90  func (endpoint *IPVlanEndpoint) Attach(h hypervisor) error {
    91  	if err := xConnectVMNetwork(endpoint, h); err != nil {
    92  		networkLogger().WithError(err).Error("Error bridging virtual ep")
    93  		return err
    94  	}
    95  
    96  	return h.addDevice(endpoint, netDev)
    97  }
    98  
    99  // Detach for the virtual endpoint tears down the tap and bridge
   100  // created for the veth interface.
   101  func (endpoint *IPVlanEndpoint) Detach(netNsCreated bool, netNsPath string) error {
   102  	// The network namespace would have been deleted at this point
   103  	// if it has not been created by virtcontainers.
   104  	if !netNsCreated {
   105  		return nil
   106  	}
   107  
   108  	return doNetNS(netNsPath, func(_ ns.NetNS) error {
   109  		return xDisconnectVMNetwork(endpoint)
   110  	})
   111  }
   112  
   113  // HotAttach for physical endpoint not supported yet
   114  func (endpoint *IPVlanEndpoint) HotAttach(h hypervisor) error {
   115  	return fmt.Errorf("IPVlanEndpoint does not support Hot attach")
   116  }
   117  
   118  // HotDetach for physical endpoint not supported yet
   119  func (endpoint *IPVlanEndpoint) HotDetach(h hypervisor, netNsCreated bool, netNsPath string) error {
   120  	return fmt.Errorf("IPVlanEndpoint does not support Hot detach")
   121  }
   122  
   123  func (endpoint *IPVlanEndpoint) save() persistapi.NetworkEndpoint {
   124  	netpair := saveNetIfPair(&endpoint.NetPair)
   125  
   126  	return persistapi.NetworkEndpoint{
   127  		Type: string(endpoint.Type()),
   128  		IPVlan: &persistapi.IPVlanEndpoint{
   129  			NetPair: *netpair,
   130  		},
   131  	}
   132  }
   133  
   134  func (endpoint *IPVlanEndpoint) load(s persistapi.NetworkEndpoint) {
   135  	endpoint.EndpointType = IPVlanEndpointType
   136  
   137  	if s.IPVlan != nil {
   138  		netpair := loadNetIfPair(&s.IPVlan.NetPair)
   139  		endpoint.NetPair = *netpair
   140  	}
   141  }