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