-
Notifications
You must be signed in to change notification settings - Fork 346
/
types.go
187 lines (166 loc) · 6.63 KB
/
types.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright 2019 Antrea Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package interfacestore
import (
"net"
"strconv"
"antrea.io/antrea/pkg/agent/util"
"antrea.io/antrea/pkg/ovs/ovsconfig"
)
const (
// ContainerInterface is used to mark current interface is for container
ContainerInterface InterfaceType = iota
// GatewayInterface is used to mark current interface is for host gateway
GatewayInterface
// TunnelInterface is used to mark current interface is for tunnel port
TunnelInterface
// UplinkInterface is used to mark current interface is for uplink port
UplinkInterface
// HostInterface is used to mark current interface is for host
HostInterface
// TrafficControlInterface is used to mark current interface is for traffic control port
TrafficControlInterface
// IPSecTunnelInterface is used to mark current interface is for IPSec tunnel port
IPSecTunnelInterface
AntreaInterfaceTypeKey = "antrea-type"
AntreaGateway = "gateway"
AntreaContainer = "container"
AntreaTunnel = "tunnel"
AntreaUplink = "uplink"
AntreaHost = "host"
AntreaTrafficControl = "traffic-control"
AntreaIPsecTunnel = "ipsec-tunnel"
AntreaUnset = ""
)
type InterfaceType uint8
func (t InterfaceType) String() string {
return strconv.Itoa(int(t))
}
type OVSPortConfig struct {
PortUUID string
OFPort int32
}
type ContainerInterfaceConfig struct {
ContainerID string
PodName string
PodNamespace string
}
type TunnelInterfaceConfig struct {
Type ovsconfig.TunnelType
// Name of the remote Node.
NodeName string
// IP address of the local Node.
LocalIP net.IP
// IP address of the remote Node.
RemoteIP net.IP
// Destination port of the remote Node.
DestinationPort int32
// CommonName of the remote Name for certificate based authentication.
RemoteName string
// Pre-shard key for authentication.
PSK string
// Whether options:csum is set for this tunnel interface.
// If true, encapsulation header UDP checksums will be computed on outgoing packets.
Csum bool
}
type InterfaceConfig struct {
Type InterfaceType
// Unique name of the interface, also used for the OVS port name.
InterfaceName string
IPs []net.IP
MAC net.HardwareAddr
// VLAN ID of the interface
VLANID uint16
*OVSPortConfig
*ContainerInterfaceConfig
*TunnelInterfaceConfig
}
// InterfaceStore is a service interface to create local interfaces for container, host gateway, and tunnel port.
// Support add/delete/get operations
type InterfaceStore interface {
Initialize(interfaces []*InterfaceConfig)
AddInterface(interfaceConfig *InterfaceConfig)
ListInterfaces() []*InterfaceConfig
DeleteInterface(interfaceConfig *InterfaceConfig)
GetInterface(interfaceKey string) (*InterfaceConfig, bool)
GetInterfaceByName(interfaceName string) (*InterfaceConfig, bool)
GetContainerInterface(containerID string) (*InterfaceConfig, bool)
GetInterfacesByEntity(name string, namespace string) []*InterfaceConfig
GetContainerInterfacesByPod(podName string, podNamespace string) []*InterfaceConfig
GetInterfaceByIP(interfaceIP string) (*InterfaceConfig, bool)
GetNodeTunnelInterface(nodeName string) (*InterfaceConfig, bool)
GetInterfaceByOFPort(ofPort uint32) (*InterfaceConfig, bool)
GetContainerInterfaceNum() int
GetInterfacesByType(interfaceType InterfaceType) []*InterfaceConfig
Len() int
GetInterfaceKeysByType(interfaceType InterfaceType) []string
}
// NewContainerInterface creates InterfaceConfig for a Pod.
func NewContainerInterface(
interfaceName string,
containerID string,
podName string,
podNamespace string,
mac net.HardwareAddr,
ips []net.IP,
vlanID uint16) *InterfaceConfig {
containerConfig := &ContainerInterfaceConfig{
ContainerID: containerID,
PodName: podName,
PodNamespace: podNamespace}
return &InterfaceConfig{
InterfaceName: interfaceName,
Type: ContainerInterface,
IPs: ips,
MAC: mac,
VLANID: vlanID,
ContainerInterfaceConfig: containerConfig}
}
// NewGatewayInterface creates InterfaceConfig for the host gateway interface.
func NewGatewayInterface(gatewayName string, gatewayMAC net.HardwareAddr) *InterfaceConfig {
gatewayConfig := &InterfaceConfig{InterfaceName: gatewayName, Type: GatewayInterface, MAC: gatewayMAC}
return gatewayConfig
}
// NewTunnelInterface creates InterfaceConfig for the default tunnel port
// interface.
func NewTunnelInterface(tunnelName string, tunnelType ovsconfig.TunnelType, localIP net.IP, csum bool) *InterfaceConfig {
tunnelConfig := &TunnelInterfaceConfig{Type: tunnelType, LocalIP: localIP, Csum: csum}
return &InterfaceConfig{InterfaceName: tunnelName, Type: TunnelInterface, TunnelInterfaceConfig: tunnelConfig}
}
// NewIPSecTunnelInterface creates InterfaceConfig for the IPsec tunnel to the
// Node.
func NewIPSecTunnelInterface(interfaceName string, tunnelType ovsconfig.TunnelType, nodeName string, nodeIP net.IP, psk, remoteName string) *InterfaceConfig {
tunnelConfig := &TunnelInterfaceConfig{Type: tunnelType, NodeName: nodeName, RemoteIP: nodeIP, PSK: psk, RemoteName: remoteName}
return &InterfaceConfig{InterfaceName: interfaceName, Type: IPSecTunnelInterface, TunnelInterfaceConfig: tunnelConfig}
}
// NewUplinkInterface creates InterfaceConfig for the uplink interface.
func NewUplinkInterface(uplinkName string) *InterfaceConfig {
uplinkConfig := &InterfaceConfig{InterfaceName: uplinkName, Type: UplinkInterface}
return uplinkConfig
}
func NewHostInterface(hostInterfaceName string) *InterfaceConfig {
return &InterfaceConfig{InterfaceName: hostInterfaceName, Type: HostInterface}
}
func NewTrafficControlInterface(interfaceName string, ovsPortConfig *OVSPortConfig) *InterfaceConfig {
trafficControlConfig := &InterfaceConfig{InterfaceName: interfaceName, Type: TrafficControlInterface, OVSPortConfig: ovsPortConfig}
return trafficControlConfig
}
// TODO: remove this method after IPv4/IPv6 dual-stack is supported completely.
func (c *InterfaceConfig) GetIPv4Addr() net.IP {
return util.GetIPv4Addr(c.IPs)
}
func (c *InterfaceConfig) GetIPv6Addr() net.IP {
ipv6, _ := util.GetIPWithFamily(c.IPs, util.FamilyIPv6)
return ipv6
}