forked from submariner-io/submariner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhost_networking.go
More file actions
201 lines (157 loc) · 5.91 KB
/
Copy pathhost_networking.go
File metadata and controls
201 lines (157 loc) · 5.91 KB
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
/*
SPDX-License-Identifier: Apache-2.0
Copyright Contributors to the Submariner project.
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 ovn
import (
"fmt"
"io/fs"
"net"
"os"
"github.com/pkg/errors"
"github.com/submariner-io/admiral/pkg/log"
"github.com/submariner-io/admiral/pkg/resource"
"github.com/submariner-io/submariner/pkg/routeagent_driver/constants"
"github.com/vishvananda/netlink"
"k8s.io/utils/set"
)
const (
OVNK8sMgmntIntfName = "ovn-k8s-mp0"
)
func (ovn *Handler) updateHostNetworkDataplane() error {
ovn.mutex.Lock()
defer ovn.mutex.Unlock()
var (
endpointSubnets set.Set[string]
cleanup bool
)
if !ovn.State().IsOnGateway() && ovn.IntraRoutingDisabled {
// Set to empty so we delete all the rules.
endpointSubnets = set.New[string]()
cleanup = true
} else {
endpointSubnets = ovn.getRemoteSubnets()
}
currentRuleRemotes, err := ovn.getExistingHostNetworkRoutes()
if err != nil {
return errors.Wrapf(err, "error reading ip rule list for IPv4")
}
toAdd := endpointSubnets.Difference(currentRuleRemotes).UnsortedList()
err = ovn.programRulesForRemoteSubnets(toAdd, ovn.netLink.RuleAddIfNotPresent)
if err != nil {
return errors.Wrap(err, "error adding routing rule")
}
toRemove := currentRuleRemotes.Difference(endpointSubnets).UnsortedList()
err = ovn.programRulesForRemoteSubnets(toRemove, ovn.netLink.RuleDelIfPresent)
if err != nil {
return errors.Wrapf(err, "error removing routing rule")
}
nextHop, err := ovn.getNextHopOnK8sMgmtIntf()
if err != nil {
return errors.Wrapf(err, "getNextHopOnK8sMgmtIntf returned error")
}
route := &netlink.Route{
Gw: *nextHop,
Table: constants.RouteAgentHostNetworkTableID,
}
if cleanup {
err = ovn.netLink.RouteDel(route)
if os.IsNotExist(err) {
err = nil
}
return errors.Wrapf(err, "error deleting submariner default route with Gw %q", nextHop.String())
}
err = ovn.netLink.RouteAdd(route)
if errors.Is(err, fs.ErrExist) {
err = nil
}
return errors.Wrapf(err, "error adding submariner default route with Gw %q", nextHop.String())
}
func (ovn *Handler) getExistingHostNetworkRoutes() (set.Set[string], error) {
currentRuleRemotes := set.New[string]()
rules, err := ovn.netLink.RuleList(ovn.ipFamily)
if err != nil {
return nil, errors.Wrapf(err, "error listing rules")
}
for i := range rules {
if rules[i].Table == constants.RouteAgentHostNetworkTableID && rules[i].Dst != nil {
currentRuleRemotes.Insert(rules[i].Dst.String())
}
}
return currentRuleRemotes, nil
}
func (ovn *Handler) programRulesForRemoteSubnets(subnets []string, ruleFunc func(rule *netlink.Rule) error) error {
for _, remoteSubnet := range subnets {
rule, err := ovn.getRuleSpec(remoteSubnet, "", constants.RouteAgentHostNetworkTableID)
if err != nil {
return errors.Wrapf(err, "error creating rule %#v", rule)
}
err = ruleFunc(rule)
if err != nil {
return errors.Wrapf(err, "error handling rule: %s", resource.ToJSON(rule))
}
}
return nil
}
func (ovn *Handler) getNextHopOnK8sMgmtIntf() (*net.IP, error) {
link, err := ovn.netLink.LinkByName(OVNK8sMgmntIntfName)
if err != nil {
return nil, errors.Wrapf(err, "error retrieving link by name %q", OVNK8sMgmntIntfName)
}
routes, err := ovn.netLink.RouteList(link, ovn.ipFamily)
if err != nil {
return nil, errors.Wrapf(err, "error retrieving IPv%v routes on link %s", ovn.ipFamily, OVNK8sMgmntIntfName)
}
parsedClusterCIDRs := make([]*net.IPNet, 0, len(ovn.ClusterCIDR))
for _, subnet := range ovn.ClusterCIDR {
_, cidrNet, err := net.ParseCIDR(subnet)
if err != nil {
logger.Error(err, "Failed to parse CIDR", "subnet", subnet)
continue
}
parsedClusterCIDRs = append(parsedClusterCIDRs, cidrNet)
}
for i := range routes {
if routes[i].Dst == nil {
continue
}
// To support hostNetworking use-case the route-agent handler programs default route in table 150
// with nexthop matching the nexthop on the ovn-k8s-mp0 interface. Basically, we want the Submariner
// managed traffic to be forwarded to the ovn_cluster_router and pass through the CNI network so that
// it reaches the active gateway node in the cluster via the submariner pipeline.
logger.V(log.TRACE).Info("Processing route", "Dst", routes[i].Dst.String(), "Gw", routes[i].Gw.String())
for _, cidrNet := range parsedClusterCIDRs {
//nolint:gocritic // Pre-existing nesting, not introduced by this fix
if routes[i].Dst.String() == cidrNet.String() ||
cidrNet.Contains(routes[i].Dst.IP) ||
routes[i].Dst.Contains(cidrNet.IP) {
logger.V(log.TRACE).Info("Matched route", "Dst", routes[i].Dst.String(), "Gw", routes[i].Gw.String())
if routes[i].Gw != nil {
return &routes[i].Gw, nil
}
// If no explicit gateway, validate that Dst.IP is a valid host address, not a network address.
// A network address (e.g., 172.22.0.0/16) should not be used as a gateway.
// Skip routes where Dst.IP is the network address, but allow host routes (/32 or /128).
ones, bits := routes[i].Dst.Mask.Size()
isHostRoute := ones == bits // /32 for IPv4 or /128 for IPv6
if !isHostRoute && routes[i].Dst.IP.Equal(routes[i].Dst.IP.Mask(routes[i].Dst.Mask)) {
logger.V(log.DEBUG).Info("Skipping route with network address",
"Dst", routes[i].Dst.String(), "IP", routes[i].Dst.IP.String())
continue
}
localIP := routes[i].Dst.IP
return &localIP, nil
}
}
}
return nil, fmt.Errorf("could not find the route to any of %v via %q", ovn.ClusterCIDR, OVNK8sMgmntIntfName)
}