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