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