/
gateway.go
503 lines (464 loc) · 18 KB
/
gateway.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
package daemon
import (
"context"
"fmt"
"net"
"os"
"os/exec"
"strings"
"github.com/alauda/felix/ipsets"
kubeovnv1 "github.com/alauda/kube-ovn/pkg/apis/kubeovn/v1"
"github.com/alauda/kube-ovn/pkg/ovs"
"github.com/alauda/kube-ovn/pkg/util"
"github.com/vishvananda/netlink"
v1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/klog"
)
const (
SubnetSet = "subnets"
SubnetNatSet = "subnets-nat"
LocalPodSet = "local-pod-ip-nat"
OtherNodeSet = "other-node"
IPSetPrefix = "ovn"
)
func (c *Controller) runGateway() {
if err := c.setIPSet(); err != nil {
klog.Errorf("failed to set gw ipsets")
}
if err := c.setIptables(); err != nil {
klog.Errorf("failed to set gw iptables")
}
if err := c.setGatewayBandwidth(); err != nil {
klog.Errorf("failed to set gw bandwidth, %v", err)
}
if err := c.setICGateway(); err != nil {
klog.Errorf("failed to set ic gateway, %v", err)
}
if err := c.setExGateway(); err != nil {
klog.Errorf("failed to set ex gateway, %v", err)
}
c.appendMssRule()
}
func (c *Controller) setIPSet() error {
protocols := make([]string, 2)
if c.protocol == kubeovnv1.ProtocolDual {
protocols[0] = kubeovnv1.ProtocolIPv4
protocols[1] = kubeovnv1.ProtocolIPv6
} else {
protocols[0] = c.protocol
}
for _, protocol := range protocols {
if c.ipset[protocol] == nil {
continue
}
subnets, err := c.getSubnetsCIDR(protocol)
if err != nil {
klog.Errorf("get subnets failed, %+v", err)
return err
}
localPodIPs, err := c.getLocalPodIPsNeedNAT(protocol)
if err != nil {
klog.Errorf("get local pod ips failed, %+v", err)
return err
}
subnetsNeedNat, err := c.getSubnetsNeedNAT(protocol)
if err != nil {
klog.Errorf("get need nat subnets failed, %+v", err)
return err
}
otherNode, err := c.getOtherNodes(protocol)
if err != nil {
klog.Errorf("failed to get node, %+v", err)
return err
}
c.ipset[protocol].AddOrReplaceIPSet(ipsets.IPSetMetadata{
MaxSize: 1048576,
SetID: SubnetSet,
Type: ipsets.IPSetTypeHashNet,
}, subnets)
c.ipset[protocol].AddOrReplaceIPSet(ipsets.IPSetMetadata{
MaxSize: 1048576,
SetID: LocalPodSet,
Type: ipsets.IPSetTypeHashIP,
}, localPodIPs)
c.ipset[protocol].AddOrReplaceIPSet(ipsets.IPSetMetadata{
MaxSize: 1048576,
SetID: SubnetNatSet,
Type: ipsets.IPSetTypeHashNet,
}, subnetsNeedNat)
c.ipset[protocol].AddOrReplaceIPSet(ipsets.IPSetMetadata{
MaxSize: 1048576,
SetID: OtherNodeSet,
Type: ipsets.IPSetTypeHashNet,
}, otherNode)
c.ipset[protocol].ApplyUpdates()
}
return nil
}
func (c *Controller) addIPSetMembers(setID, subnet, ip string) error {
podSubnet, err := c.subnetsLister.Get(subnet)
if err != nil {
klog.Errorf("get subnet %s failed, %+v", subnet, err)
return err
}
if !podSubnet.Spec.NatOutgoing ||
podSubnet.Spec.Vpc != util.DefaultVpc ||
podSubnet.Spec.GatewayType != kubeovnv1.GWDistributedType {
return nil
}
podIPs := strings.Split(ip, ",")
if protocol := util.CheckProtocol(ip); protocol == kubeovnv1.ProtocolDual {
c.ipset[kubeovnv1.ProtocolIPv4].AddMembers(setID, []string{podIPs[0]})
c.ipset[kubeovnv1.ProtocolIPv6].AddMembers(setID, []string{podIPs[1]})
c.ipset[kubeovnv1.ProtocolIPv4].ApplyUpdates()
c.ipset[kubeovnv1.ProtocolIPv6].ApplyUpdates()
} else {
c.ipset[protocol].AddMembers(setID, []string{podIPs[0]})
c.ipset[protocol].ApplyUpdates()
}
return nil
}
func (c *Controller) removeIPSetMembers(setID, subnet, ip string) error {
if subnet == "" || ip == "" {
return nil
}
podSubnet, err := c.subnetsLister.Get(subnet)
if err != nil {
klog.Errorf("get subnet %s failed, %+v", subnet, err)
return err
}
if !podSubnet.Spec.NatOutgoing ||
podSubnet.Spec.Vpc != util.DefaultVpc ||
podSubnet.Spec.GatewayType != kubeovnv1.GWDistributedType {
return nil
}
podIPs := strings.Split(ip, ",")
if protocol := util.CheckProtocol(ip); protocol == kubeovnv1.ProtocolDual {
c.ipset[kubeovnv1.ProtocolIPv4].RemoveMembers(setID, []string{podIPs[0]})
c.ipset[kubeovnv1.ProtocolIPv6].RemoveMembers(setID, []string{podIPs[1]})
c.ipset[kubeovnv1.ProtocolIPv4].ApplyUpdates()
c.ipset[kubeovnv1.ProtocolIPv6].ApplyUpdates()
} else {
c.ipset[protocol].RemoveMembers(setID, []string{podIPs[0]})
c.ipset[protocol].ApplyUpdates()
}
return nil
}
func (c *Controller) setIptables() error {
klog.V(3).Infoln("start to set up iptables")
node, err := c.nodesLister.Get(c.config.NodeName)
if err != nil {
klog.Errorf("failed to get node %s, %v", c.config.NodeName, err)
return err
}
hostIP := util.GetNodeInternalIP(node)
var (
v4Rules = []util.IPTableRule{
// Prevent performing Masquerade on external traffic which arrives from a Node that owns the Pod/Subnet IP
{Table: "nat", Chain: "POSTROUTING", Rule: strings.Split(`-m set ! --match-set ovn40subnets src -m set ! --match-set ovn40other-node src -m set --match-set ovn40local-pod-ip-nat dst -j RETURN`, " ")},
{Table: "nat", Chain: "POSTROUTING", Rule: strings.Split(`-m set ! --match-set ovn40subnets src -m set ! --match-set ovn40other-node src -m set --match-set ovn40subnets-nat dst -j RETURN`, " ")},
// NAT if pod/subnet to external address
{Table: "nat", Chain: "POSTROUTING", Rule: strings.Split(`-m set --match-set ovn40local-pod-ip-nat src -m set ! --match-set ovn40subnets dst -j MASQUERADE`, " ")},
{Table: "nat", Chain: "POSTROUTING", Rule: strings.Split(`-m set --match-set ovn40subnets-nat src -m set ! --match-set ovn40subnets dst -j MASQUERADE`, " ")},
// masq traffic from hostport/nodeport
{Table: "nat", Chain: "POSTROUTING", Rule: strings.Split(fmt.Sprintf(`-o ovn0 ! -s %s -m mark --mark 0x4000/0x4000 -j MASQUERADE`, hostIP), " ")},
// Input Accept
{Table: "filter", Chain: "FORWARD", Rule: strings.Split(`-m set --match-set ovn40subnets src -j ACCEPT`, " ")},
{Table: "filter", Chain: "FORWARD", Rule: strings.Split(`-m set --match-set ovn40subnets dst -j ACCEPT`, " ")},
// Forward Accept
{Table: "filter", Chain: "INPUT", Rule: strings.Split(`-m set --match-set ovn40subnets src -j ACCEPT`, " ")},
{Table: "filter", Chain: "INPUT", Rule: strings.Split(`-m set --match-set ovn40subnets dst -j ACCEPT`, " ")},
}
v6Rules = []util.IPTableRule{
// Prevent performing Masquerade on external traffic which arrives from a Node that owns the Pod/Subnet IP
{Table: "nat", Chain: "POSTROUTING", Rule: strings.Split(`-m set ! --match-set ovn60subnets src -m set ! --match-set ovn60other-node src -m set --match-set ovn60local-pod-ip-nat dst -j RETURN`, " ")},
{Table: "nat", Chain: "POSTROUTING", Rule: strings.Split(`-m set ! --match-set ovn60subnets src -m set ! --match-set ovn60other-node src -m set --match-set ovn60subnets-nat dst -j RETURN`, " ")},
// NAT if pod/subnet to external address
{Table: "nat", Chain: "POSTROUTING", Rule: strings.Split(`-m set --match-set ovn60local-pod-ip-nat src -m set ! --match-set ovn60subnets dst -j MASQUERADE`, " ")},
{Table: "nat", Chain: "POSTROUTING", Rule: strings.Split(`-m set --match-set ovn60subnets-nat src -m set ! --match-set ovn60subnets dst -j MASQUERADE`, " ")},
// masq traffic from hostport/nodeport
{Table: "nat", Chain: "POSTROUTING", Rule: strings.Split(fmt.Sprintf(`-o ovn0 ! -s %s -m mark --mark 0x4000/0x4000 -j MASQUERADE`, hostIP), " ")},
// Input Accept
{Table: "filter", Chain: "FORWARD", Rule: strings.Split(`-m set --match-set ovn60subnets src -j ACCEPT`, " ")},
{Table: "filter", Chain: "FORWARD", Rule: strings.Split(`-m set --match-set ovn60subnets dst -j ACCEPT`, " ")},
// Forward Accept
{Table: "filter", Chain: "INPUT", Rule: strings.Split(`-m set --match-set ovn60subnets src -j ACCEPT`, " ")},
{Table: "filter", Chain: "INPUT", Rule: strings.Split(`-m set --match-set ovn60subnets dst -j ACCEPT`, " ")},
}
)
protocols := make([]string, 2)
if c.protocol == kubeovnv1.ProtocolDual {
protocols[0] = kubeovnv1.ProtocolIPv4
protocols[1] = kubeovnv1.ProtocolIPv6
} else {
protocols[0] = c.protocol
}
for _, protocol := range protocols {
if c.iptable[protocol] == nil {
continue
}
iptableRules := make([]util.IPTableRule, len(v4Rules))
if protocol == kubeovnv1.ProtocolIPv4 {
iptableRules = v4Rules
} else {
iptableRules = v6Rules
}
iptableRules[0], iptableRules[1], iptableRules[3], iptableRules[4] =
iptableRules[4], iptableRules[3], iptableRules[1], iptableRules[0]
for _, iptRule := range iptableRules {
if strings.Contains(strings.Join(iptRule.Rule, " "), "ovn0") && protocol != util.CheckProtocol(hostIP) {
klog.V(3).Infof("ignore check iptable rule, protocol %v, hostIP %v", protocol, hostIP)
continue
}
exists, err := c.iptable[protocol].Exists(iptRule.Table, iptRule.Chain, iptRule.Rule...)
if err != nil {
klog.Errorf("check iptable rule exist failed, %+v", err)
return err
}
if !exists {
klog.Infof("iptables rules %s not exist, recreate iptables rules", strings.Join(iptRule.Rule, " "))
if err := c.iptable[protocol].Insert(iptRule.Table, iptRule.Chain, 1, iptRule.Rule...); err != nil {
klog.Errorf("insert iptable rule %s failed, %+v", strings.Join(iptRule.Rule, " "), err)
return err
}
}
}
}
return nil
}
func (c *Controller) setGatewayBandwidth() error {
node, err := c.config.KubeClient.CoreV1().Nodes().Get(context.Background(), c.config.NodeName, metav1.GetOptions{})
if err != nil {
klog.Errorf("failed to get node, %v", err)
return err
}
ingress, egress := node.Annotations[util.IngressRateAnnotation], node.Annotations[util.EgressRateAnnotation]
ifaceId := fmt.Sprintf("node-%s", c.config.NodeName)
return ovs.SetInterfaceBandwidth(ifaceId, egress, ingress)
}
func (c *Controller) setICGateway() error {
node, err := c.config.KubeClient.CoreV1().Nodes().Get(context.Background(), c.config.NodeName, metav1.GetOptions{})
if err != nil {
klog.Errorf("failed to get node, %v", err)
return err
}
enable := node.Labels[util.ICGatewayLabel]
if enable == "true" {
icEnabled, err := ovs.Exec(ovs.IfExists, "get", "open", ".", "external_ids:ovn-is-interconn")
if err != nil {
return fmt.Errorf("failed to get if ic enabled, %v", err)
}
if strings.Trim(icEnabled, "\"") != "true" {
if _, err := ovs.Exec("set", "open", ".", "external_ids:ovn-is-interconn=true"); err != nil {
return fmt.Errorf("failed to enable ic gateway, %v", err)
}
output, err := exec.Command("/usr/share/ovn/scripts/ovn-ctl", "restart_controller").CombinedOutput()
if err != nil {
return fmt.Errorf("failed to restart ovn-controller, %v, %q", err, output)
}
}
} else {
if _, err := ovs.Exec("set", "open", ".", "external_ids:ovn-is-interconn=false"); err != nil {
return fmt.Errorf("failed to disable ic gateway, %v", err)
}
}
return nil
}
func (c *Controller) setExGateway() error {
node, err := c.config.KubeClient.CoreV1().Nodes().Get(context.Background(), c.config.NodeName, metav1.GetOptions{})
if err != nil {
klog.Errorf("failed to get node, %v", err)
return err
}
enable := node.Labels[util.ExGatewayLabel]
if enable == "true" {
cm, err := c.config.KubeClient.CoreV1().ConfigMaps("kube-system").Get(context.Background(), util.ExternalGatewayConfig, metav1.GetOptions{})
if err != nil && !k8serrors.IsNotFound(err) {
klog.Errorf("failed to get ovn-external-gw-config, %v", err)
return err
}
link, err := netlink.LinkByName(cm.Data["external-gw-nic"])
if err != nil {
klog.Errorf("failed to get nic %s, %v", cm.Data["external-gw-nic"], err)
return err
}
if err := netlink.LinkSetUp(link); err != nil {
klog.Errorf("failed to set gateway nic %s up, %v", cm.Data["external-gw-nic"], err)
return err
}
if _, err := ovs.Exec(
ovs.MayExist, "add-br", "br-external", "--",
ovs.MayExist, "add-port", "br-external", cm.Data["external-gw-nic"],
); err != nil {
return fmt.Errorf("failed to enable external gateway, %v", err)
}
output, err := ovs.Exec(ovs.IfExists, "get", "open", ".", "external-ids:ovn-bridge-mappings")
if err != nil {
return fmt.Errorf("failed to get external-ids, %v", err)
}
bridgeMappings := "external:br-external"
if output != "" && !util.IsStringIn(bridgeMappings, strings.Split(output, ",")) {
bridgeMappings = fmt.Sprintf("%s,%s", output, bridgeMappings)
}
output, err = ovs.Exec("set", "open", ".", fmt.Sprintf("external-ids:ovn-bridge-mappings=%s", bridgeMappings))
if err != nil {
return fmt.Errorf("failed to set bridg-mappings, %v: %q", err, output)
}
} else {
if _, err := ovs.Exec(
ovs.IfExists, "del-br", "br-external"); err != nil {
return fmt.Errorf("failed to disable external gateway, %v", err)
}
}
return nil
}
func (c *Controller) getLocalPodIPsNeedNAT(protocol string) ([]string, error) {
var localPodIPs []string
hostname := os.Getenv("KUBE_NODE_NAME")
allPods, err := c.podsLister.List(labels.Everything())
if err != nil {
klog.Errorf("list pods failed, %+v", err)
return nil, err
}
for _, pod := range allPods {
if pod.Spec.HostNetwork == true ||
pod.Status.PodIP == "" ||
pod.Annotations[util.LogicalSwitchAnnotation] == "" {
continue
}
subnet, err := c.subnetsLister.Get(pod.Annotations[util.LogicalSwitchAnnotation])
if err != nil {
klog.Errorf("get subnet %s failed, %+v", pod.Annotations[util.LogicalSwitchAnnotation], err)
continue
}
nsGWType := subnet.Spec.GatewayType
nsGWNat := subnet.Spec.NatOutgoing
if nsGWNat &&
subnet.Spec.Vpc == util.DefaultVpc &&
nsGWType == kubeovnv1.GWDistributedType &&
pod.Spec.NodeName == hostname {
if len(pod.Status.PodIPs) == 2 && protocol == kubeovnv1.ProtocolIPv6 {
localPodIPs = append(localPodIPs, pod.Status.PodIPs[1].IP)
} else if util.CheckProtocol(pod.Status.PodIP) == protocol {
localPodIPs = append(localPodIPs, pod.Status.PodIP)
}
}
}
klog.V(3).Infof("local pod ips %v", localPodIPs)
return localPodIPs, nil
}
func (c *Controller) getSubnetsNeedNAT(protocol string) ([]string, error) {
var subnetsNeedNat []string
subnets, err := c.subnetsLister.List(labels.Everything())
if err != nil {
klog.Errorf("list subnets failed, %v", err)
return nil, err
}
for _, subnet := range subnets {
if subnet.Spec.Vpc == util.DefaultVpc &&
subnet.Spec.GatewayType == kubeovnv1.GWCentralizedType &&
subnet.Status.ActivateGateway == c.config.NodeName &&
(subnet.Spec.Protocol == kubeovnv1.ProtocolDual || subnet.Spec.Protocol == protocol) &&
subnet.Spec.NatOutgoing {
cidrBlock := getCidrByProtocol(subnet.Spec.CIDRBlock, protocol)
subnetsNeedNat = append(subnetsNeedNat, cidrBlock)
}
}
return subnetsNeedNat, nil
}
func (c *Controller) getSubnetsCIDR(protocol string) ([]string, error) {
subnets, err := c.subnetsLister.List(labels.Everything())
if err != nil {
klog.Error("failed to list subnets")
return nil, err
}
ret := make([]string, 0, len(subnets)+3)
if c.config.NodeLocalDNSIP != "" && net.ParseIP(c.config.NodeLocalDNSIP) != nil && util.CheckProtocol(c.config.NodeLocalDNSIP) == protocol {
ret = append(ret, c.config.NodeLocalDNSIP)
}
for _, sip := range strings.Split(c.config.ServiceClusterIPRange, ",") {
if util.CheckProtocol(sip) == protocol {
ret = append(ret, sip)
}
}
for _, subnet := range subnets {
if subnet.Spec.Vpc == util.DefaultVpc {
cidrBlock := getCidrByProtocol(subnet.Spec.CIDRBlock, protocol)
ret = append(ret, cidrBlock)
}
}
return ret, nil
}
func (c *Controller) getOtherNodes(protocol string) ([]string, error) {
nodes, err := c.nodesLister.List(labels.Everything())
if err != nil {
klog.Error("failed to list nodes")
return nil, err
}
ret := make([]string, 0, len(nodes)-1)
for _, node := range nodes {
if node.Name == c.config.NodeName {
continue
}
for _, addr := range node.Status.Addresses {
if addr.Type == v1.NodeInternalIP {
if util.CheckProtocol(addr.Address) == protocol {
ret = append(ret, addr.Address)
}
}
}
}
return ret, nil
}
//Generally, the MTU of the interface is set to 1400. But in special cases, a special pod (docker indocker) will introduce the docker0 interface to the pod. The MTU of docker0 is 1500.
//The network application in pod will calculate the TCP MSS according to the MTU of docker0, and then initiate communication with others. After the other party sends a response, the kernel protocol stack of Linux host will send ICMP unreachable message to the other party, indicating that IP fragmentation is needed, which is not supported by the other party, resulting in communication failure.
func (c *Controller) appendMssRule() {
if c.config.Iface != "" && c.config.MSS > 0 {
rule := fmt.Sprintf("-p tcp --tcp-flags SYN,RST SYN -o %s -j TCPMSS --set-mss %d", c.config.Iface, c.config.MSS)
MssMangleRule := util.IPTableRule{
Table: "mangle",
Chain: "POSTROUTING",
Rule: strings.Split(rule, " "),
}
switch c.protocol {
case kubeovnv1.ProtocolIPv4:
c.updateMssRuleByProtocol(c.protocol, MssMangleRule)
case kubeovnv1.ProtocolIPv6:
c.updateMssRuleByProtocol(c.protocol, MssMangleRule)
case kubeovnv1.ProtocolDual:
c.updateMssRuleByProtocol(kubeovnv1.ProtocolIPv4, MssMangleRule)
c.updateMssRuleByProtocol(kubeovnv1.ProtocolIPv6, MssMangleRule)
}
}
}
func (c *Controller) updateMssRuleByProtocol(protocol string, MssMangleRule util.IPTableRule) {
exists, err := c.iptable[protocol].Exists(MssMangleRule.Table, MssMangleRule.Chain, MssMangleRule.Rule...)
if err != nil {
klog.Errorf("check iptable rule %v failed, %+v", MssMangleRule.Rule, err)
return
}
if !exists {
klog.Infof("iptables rules %s not exist, append iptables rules", strings.Join(MssMangleRule.Rule, " "))
if err := c.iptable[protocol].Append(MssMangleRule.Table, MssMangleRule.Chain, MssMangleRule.Rule...); err != nil {
klog.Errorf("append iptable rule %v failed, %+v", MssMangleRule.Rule, err)
return
}
}
}
func getCidrByProtocol(cidr, protocol string) string {
var cidrStr string
if util.CheckProtocol(cidr) == kubeovnv1.ProtocolDual {
cidrBlocks := strings.Split(cidr, ",")
if protocol == kubeovnv1.ProtocolIPv4 {
cidrStr = cidrBlocks[0]
} else if protocol == kubeovnv1.ProtocolIPv6 {
cidrStr = cidrBlocks[1]
}
} else {
cidrStr = cidr
}
return cidrStr
}