Jian Li | 66f6e3c | 2019-01-25 10:24:44 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.k8snetworking.impl; |
| 17 | |
Jian Li | 4aa1764 | 2019-01-30 00:01:11 +0900 | [diff] [blame] | 18 | import org.onlab.packet.Ethernet; |
| 19 | import org.onlab.packet.IpPrefix; |
Jian Li | 7d111d7 | 2019-04-12 13:58:44 +0900 | [diff] [blame] | 20 | import org.onlab.packet.MacAddress; |
Jian Li | 66f6e3c | 2019-01-25 10:24:44 +0900 | [diff] [blame] | 21 | import org.onosproject.cluster.ClusterService; |
| 22 | import org.onosproject.cluster.LeadershipService; |
| 23 | import org.onosproject.cluster.NodeId; |
| 24 | import org.onosproject.core.ApplicationId; |
| 25 | import org.onosproject.core.CoreService; |
| 26 | import org.onosproject.k8snetworking.api.K8sFlowRuleService; |
Jian Li | 4aa1764 | 2019-01-30 00:01:11 +0900 | [diff] [blame] | 27 | import org.onosproject.k8snetworking.api.K8sNetwork; |
| 28 | import org.onosproject.k8snetworking.api.K8sNetworkEvent; |
| 29 | import org.onosproject.k8snetworking.api.K8sNetworkListener; |
| 30 | import org.onosproject.k8snetworking.api.K8sNetworkService; |
Jian Li | 66f6e3c | 2019-01-25 10:24:44 +0900 | [diff] [blame] | 31 | import org.onosproject.k8snode.api.K8sNode; |
| 32 | import org.onosproject.k8snode.api.K8sNodeEvent; |
| 33 | import org.onosproject.k8snode.api.K8sNodeListener; |
| 34 | import org.onosproject.k8snode.api.K8sNodeService; |
| 35 | import org.onosproject.net.DeviceId; |
Jian Li | 4aa1764 | 2019-01-30 00:01:11 +0900 | [diff] [blame] | 36 | import org.onosproject.net.PortNumber; |
Jian Li | 7d111d7 | 2019-04-12 13:58:44 +0900 | [diff] [blame] | 37 | import org.onosproject.net.device.DeviceService; |
Jian Li | 66f6e3c | 2019-01-25 10:24:44 +0900 | [diff] [blame] | 38 | import org.onosproject.net.flow.DefaultFlowRule; |
| 39 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 40 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 41 | import org.onosproject.net.flow.FlowRule; |
| 42 | import org.onosproject.net.flow.FlowRuleOperations; |
| 43 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 44 | import org.onosproject.net.flow.FlowRuleService; |
| 45 | import org.onosproject.net.flow.TrafficSelector; |
| 46 | import org.onosproject.net.flow.TrafficTreatment; |
| 47 | import org.osgi.service.component.annotations.Activate; |
| 48 | import org.osgi.service.component.annotations.Component; |
| 49 | import org.osgi.service.component.annotations.Deactivate; |
| 50 | import org.osgi.service.component.annotations.Reference; |
| 51 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 52 | import org.slf4j.Logger; |
| 53 | |
| 54 | import java.util.Objects; |
| 55 | import java.util.concurrent.ExecutorService; |
| 56 | import java.util.concurrent.Executors; |
| 57 | |
| 58 | import static org.onlab.util.Tools.groupedThreads; |
Jian Li | 73d3b6a | 2019-07-08 18:07:53 +0900 | [diff] [blame] | 59 | import static org.onosproject.k8snetworking.api.Constants.ACL_TABLE; |
Jian Li | 66f6e3c | 2019-01-25 10:24:44 +0900 | [diff] [blame] | 60 | import static org.onosproject.k8snetworking.api.Constants.ARP_TABLE; |
| 61 | import static org.onosproject.k8snetworking.api.Constants.DEFAULT_GATEWAY_MAC; |
Jian Li | 66f6e3c | 2019-01-25 10:24:44 +0900 | [diff] [blame] | 62 | import static org.onosproject.k8snetworking.api.Constants.FORWARDING_TABLE; |
Jian Li | 73d3b6a | 2019-07-08 18:07:53 +0900 | [diff] [blame] | 63 | import static org.onosproject.k8snetworking.api.Constants.GROUPING_TABLE; |
Jian Li | 1a2eb5d | 2019-08-27 02:07:05 +0900 | [diff] [blame] | 64 | import static org.onosproject.k8snetworking.api.Constants.HOST_PREFIX; |
Jian Li | e1a5b8f | 2019-07-23 17:13:19 +0900 | [diff] [blame] | 65 | import static org.onosproject.k8snetworking.api.Constants.JUMP_TABLE; |
Jian Li | 66f6e3c | 2019-01-25 10:24:44 +0900 | [diff] [blame] | 66 | import static org.onosproject.k8snetworking.api.Constants.K8S_NETWORKING_APP_ID; |
Jian Li | e1a5b8f | 2019-07-23 17:13:19 +0900 | [diff] [blame] | 67 | import static org.onosproject.k8snetworking.api.Constants.NAMESPACE_TABLE; |
Jian Li | 7d111d7 | 2019-04-12 13:58:44 +0900 | [diff] [blame] | 68 | import static org.onosproject.k8snetworking.api.Constants.PRIORITY_CIDR_RULE; |
Jian Li | 66f6e3c | 2019-01-25 10:24:44 +0900 | [diff] [blame] | 69 | import static org.onosproject.k8snetworking.api.Constants.PRIORITY_SNAT_RULE; |
| 70 | import static org.onosproject.k8snetworking.api.Constants.ROUTING_TABLE; |
Jian Li | 73d3b6a | 2019-07-08 18:07:53 +0900 | [diff] [blame] | 71 | import static org.onosproject.k8snetworking.api.Constants.STAT_EGRESS_TABLE; |
Jian Li | e1a5b8f | 2019-07-23 17:13:19 +0900 | [diff] [blame] | 72 | import static org.onosproject.k8snetworking.api.Constants.STAT_INGRESS_TABLE; |
Jian Li | 619fa28 | 2020-09-02 14:45:35 +0900 | [diff] [blame] | 73 | import static org.onosproject.k8snetworking.api.Constants.TUN_ENTRY_TABLE; |
Jian Li | 66f6e3c | 2019-01-25 10:24:44 +0900 | [diff] [blame] | 74 | import static org.onosproject.k8snetworking.api.Constants.VTAG_TABLE; |
Jian Li | 73d3b6a | 2019-07-08 18:07:53 +0900 | [diff] [blame] | 75 | import static org.onosproject.k8snetworking.api.Constants.VTAP_EGRESS_TABLE; |
Jian Li | e1a5b8f | 2019-07-23 17:13:19 +0900 | [diff] [blame] | 76 | import static org.onosproject.k8snetworking.api.Constants.VTAP_INGRESS_TABLE; |
Jian Li | 7d111d7 | 2019-04-12 13:58:44 +0900 | [diff] [blame] | 77 | import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.tunnelPortNumByNetId; |
| 78 | import static org.onosproject.k8snetworking.util.RulePopulatorUtil.buildExtension; |
Jian Li | 66f6e3c | 2019-01-25 10:24:44 +0900 | [diff] [blame] | 79 | import static org.slf4j.LoggerFactory.getLogger; |
| 80 | |
| 81 | /** |
| 82 | * Sets flow rules directly using FlowRuleService. |
| 83 | */ |
| 84 | @Component(immediate = true, service = K8sFlowRuleService.class) |
| 85 | public class K8sFlowRuleManager implements K8sFlowRuleService { |
| 86 | |
| 87 | private final Logger log = getLogger(getClass()); |
| 88 | |
| 89 | private static final int DROP_PRIORITY = 0; |
| 90 | private static final int HIGH_PRIORITY = 30000; |
| 91 | private static final int TIMEOUT_SNAT_RULE = 60; |
| 92 | |
| 93 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 94 | protected FlowRuleService flowRuleService; |
| 95 | |
| 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 97 | protected CoreService coreService; |
| 98 | |
| 99 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | 7d111d7 | 2019-04-12 13:58:44 +0900 | [diff] [blame] | 100 | protected DeviceService deviceService; |
| 101 | |
| 102 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | 66f6e3c | 2019-01-25 10:24:44 +0900 | [diff] [blame] | 103 | protected ClusterService clusterService; |
| 104 | |
| 105 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 106 | protected LeadershipService leadershipService; |
| 107 | |
| 108 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | 4aa1764 | 2019-01-30 00:01:11 +0900 | [diff] [blame] | 109 | protected K8sNetworkService k8sNetworkService; |
| 110 | |
| 111 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | 66f6e3c | 2019-01-25 10:24:44 +0900 | [diff] [blame] | 112 | protected K8sNodeService k8sNodeService; |
| 113 | |
| 114 | private final ExecutorService deviceEventExecutor = |
| 115 | Executors.newSingleThreadExecutor(groupedThreads( |
| 116 | getClass().getSimpleName(), "device-event")); |
Jian Li | 4aa1764 | 2019-01-30 00:01:11 +0900 | [diff] [blame] | 117 | private final K8sNetworkListener internalNetworkListener = new InternalK8sNetworkListener(); |
Jian Li | 66f6e3c | 2019-01-25 10:24:44 +0900 | [diff] [blame] | 118 | private final K8sNodeListener internalNodeListener = new InternalK8sNodeListener(); |
| 119 | |
| 120 | private ApplicationId appId; |
| 121 | private NodeId localNodeId; |
| 122 | |
| 123 | @Activate |
| 124 | protected void activate() { |
| 125 | appId = coreService.registerApplication(K8S_NETWORKING_APP_ID); |
| 126 | coreService.registerApplication(K8S_NETWORKING_APP_ID); |
| 127 | k8sNodeService.addListener(internalNodeListener); |
Jian Li | 4aa1764 | 2019-01-30 00:01:11 +0900 | [diff] [blame] | 128 | k8sNetworkService.addListener(internalNetworkListener); |
Jian Li | 66f6e3c | 2019-01-25 10:24:44 +0900 | [diff] [blame] | 129 | localNodeId = clusterService.getLocalNode().id(); |
| 130 | leadershipService.runForLeadership(appId.name()); |
Jian Li | 4aa1764 | 2019-01-30 00:01:11 +0900 | [diff] [blame] | 131 | k8sNodeService.completeNodes().forEach(this::initializePipeline); |
Jian Li | 66f6e3c | 2019-01-25 10:24:44 +0900 | [diff] [blame] | 132 | |
| 133 | log.info("Started"); |
| 134 | } |
| 135 | |
| 136 | @Deactivate |
| 137 | protected void deactivate() { |
| 138 | k8sNodeService.removeListener(internalNodeListener); |
Jian Li | 4aa1764 | 2019-01-30 00:01:11 +0900 | [diff] [blame] | 139 | k8sNetworkService.removeListener(internalNetworkListener); |
Jian Li | 66f6e3c | 2019-01-25 10:24:44 +0900 | [diff] [blame] | 140 | leadershipService.withdraw(appId.name()); |
| 141 | deviceEventExecutor.shutdown(); |
| 142 | |
| 143 | log.info("Stopped"); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public void setRule(ApplicationId appId, DeviceId deviceId, |
| 148 | TrafficSelector selector, TrafficTreatment treatment, |
| 149 | int priority, int tableType, boolean install) { |
| 150 | FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder() |
| 151 | .forDevice(deviceId) |
| 152 | .withSelector(selector) |
| 153 | .withTreatment(treatment) |
| 154 | .withPriority(priority) |
| 155 | .fromApp(appId) |
| 156 | .forTable(tableType); |
| 157 | |
| 158 | if (priority == PRIORITY_SNAT_RULE) { |
| 159 | flowRuleBuilder.makeTemporary(TIMEOUT_SNAT_RULE); |
| 160 | } else { |
| 161 | flowRuleBuilder.makePermanent(); |
| 162 | } |
| 163 | |
| 164 | applyRule(flowRuleBuilder.build(), install); |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public void setUpTableMissEntry(DeviceId deviceId, int table) { |
| 169 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 170 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 171 | |
| 172 | treatment.drop(); |
| 173 | |
| 174 | FlowRule flowRule = DefaultFlowRule.builder() |
| 175 | .forDevice(deviceId) |
| 176 | .withSelector(selector.build()) |
| 177 | .withTreatment(treatment.build()) |
| 178 | .withPriority(DROP_PRIORITY) |
| 179 | .fromApp(appId) |
| 180 | .makePermanent() |
| 181 | .forTable(table) |
| 182 | .build(); |
| 183 | |
| 184 | applyRule(flowRule, true); |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public void connectTables(DeviceId deviceId, int fromTable, int toTable) { |
| 189 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 190 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 191 | |
| 192 | treatment.transition(toTable); |
| 193 | |
| 194 | FlowRule flowRule = DefaultFlowRule.builder() |
| 195 | .forDevice(deviceId) |
| 196 | .withSelector(selector.build()) |
| 197 | .withTreatment(treatment.build()) |
| 198 | .withPriority(DROP_PRIORITY) |
| 199 | .fromApp(appId) |
| 200 | .makePermanent() |
| 201 | .forTable(fromTable) |
| 202 | .build(); |
| 203 | |
| 204 | applyRule(flowRule, true); |
| 205 | } |
| 206 | |
| 207 | private void applyRule(FlowRule flowRule, boolean install) { |
| 208 | FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder(); |
| 209 | |
| 210 | flowOpsBuilder = install ? flowOpsBuilder.add(flowRule) : flowOpsBuilder.remove(flowRule); |
| 211 | |
| 212 | flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() { |
| 213 | @Override |
| 214 | public void onSuccess(FlowRuleOperations ops) { |
| 215 | log.debug("Provisioned vni or forwarding table"); |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public void onError(FlowRuleOperations ops) { |
| 220 | log.debug("Failed to provision vni or forwarding table"); |
| 221 | } |
| 222 | })); |
| 223 | } |
| 224 | |
Jian Li | 4aa1764 | 2019-01-30 00:01:11 +0900 | [diff] [blame] | 225 | protected void initializePipeline(K8sNode k8sNode) { |
| 226 | |
| 227 | DeviceId deviceId = k8sNode.intgBridge(); |
| 228 | |
Jian Li | 66f6e3c | 2019-01-25 10:24:44 +0900 | [diff] [blame] | 229 | // for inbound table transition |
Jian Li | 73d3b6a | 2019-07-08 18:07:53 +0900 | [diff] [blame] | 230 | connectTables(deviceId, STAT_INGRESS_TABLE, VTAP_INGRESS_TABLE); |
| 231 | connectTables(deviceId, VTAP_INGRESS_TABLE, VTAG_TABLE); |
Jian Li | 66f6e3c | 2019-01-25 10:24:44 +0900 | [diff] [blame] | 232 | |
| 233 | // for vTag and ARP table transition |
| 234 | connectTables(deviceId, VTAG_TABLE, ARP_TABLE); |
| 235 | |
Jian Li | e1a5b8f | 2019-07-23 17:13:19 +0900 | [diff] [blame] | 236 | // for jump and namespace table transition |
| 237 | connectTables(deviceId, JUMP_TABLE, NAMESPACE_TABLE); |
Jian Li | 66f6e3c | 2019-01-25 10:24:44 +0900 | [diff] [blame] | 238 | |
Jian Li | 4aa1764 | 2019-01-30 00:01:11 +0900 | [diff] [blame] | 239 | // for ARP and ACL table transition |
Jian Li | e1a5b8f | 2019-07-23 17:13:19 +0900 | [diff] [blame] | 240 | connectTables(deviceId, ARP_TABLE, NAMESPACE_TABLE); |
| 241 | |
| 242 | // for namespace table transition to grouping table |
| 243 | connectTables(deviceId, NAMESPACE_TABLE, GROUPING_TABLE); |
Jian Li | 4aa1764 | 2019-01-30 00:01:11 +0900 | [diff] [blame] | 244 | |
Jian Li | 73d3b6a | 2019-07-08 18:07:53 +0900 | [diff] [blame] | 245 | // for grouping table transition to ACL table |
| 246 | connectTables(deviceId, GROUPING_TABLE, ACL_TABLE); |
Jian Li | 7d111d7 | 2019-04-12 13:58:44 +0900 | [diff] [blame] | 247 | |
Jian Li | 73d3b6a | 2019-07-08 18:07:53 +0900 | [diff] [blame] | 248 | // for ACL table transition to routing table |
| 249 | connectTables(deviceId, ACL_TABLE, ROUTING_TABLE); |
| 250 | |
| 251 | // for grouping table transition |
| 252 | // we need grouping table for bypassing routing table which contains large |
Jian Li | 66f6e3c | 2019-01-25 10:24:44 +0900 | [diff] [blame] | 253 | // amount of flow rules which might cause performance degradation during |
| 254 | // table lookup |
Jian Li | 2cc2b63 | 2019-02-18 00:56:40 +0900 | [diff] [blame] | 255 | // setupJumpTable(k8sNode); |
| 256 | |
| 257 | // for routing and outbound table transition |
Jian Li | 73d3b6a | 2019-07-08 18:07:53 +0900 | [diff] [blame] | 258 | connectTables(deviceId, ROUTING_TABLE, STAT_EGRESS_TABLE); |
Jian Li | 66f6e3c | 2019-01-25 10:24:44 +0900 | [diff] [blame] | 259 | |
| 260 | // for outbound table transition |
Jian Li | 73d3b6a | 2019-07-08 18:07:53 +0900 | [diff] [blame] | 261 | connectTables(deviceId, STAT_EGRESS_TABLE, VTAP_EGRESS_TABLE); |
| 262 | connectTables(deviceId, VTAP_EGRESS_TABLE, FORWARDING_TABLE); |
Jian Li | 66f6e3c | 2019-01-25 10:24:44 +0900 | [diff] [blame] | 263 | } |
| 264 | |
Jian Li | 4aa1764 | 2019-01-30 00:01:11 +0900 | [diff] [blame] | 265 | private void setupJumpTable(K8sNode k8sNode) { |
| 266 | DeviceId deviceId = k8sNode.intgBridge(); |
| 267 | |
Jian Li | 66f6e3c | 2019-01-25 10:24:44 +0900 | [diff] [blame] | 268 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 269 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 270 | |
| 271 | selector.matchEthDst(DEFAULT_GATEWAY_MAC); |
| 272 | treatment.transition(ROUTING_TABLE); |
| 273 | |
| 274 | FlowRule flowRule = DefaultFlowRule.builder() |
| 275 | .forDevice(deviceId) |
| 276 | .withSelector(selector.build()) |
| 277 | .withTreatment(treatment.build()) |
| 278 | .withPriority(HIGH_PRIORITY) |
| 279 | .fromApp(appId) |
| 280 | .makePermanent() |
Jian Li | 73d3b6a | 2019-07-08 18:07:53 +0900 | [diff] [blame] | 281 | .forTable(GROUPING_TABLE) |
Jian Li | 66f6e3c | 2019-01-25 10:24:44 +0900 | [diff] [blame] | 282 | .build(); |
| 283 | |
| 284 | applyRule(flowRule, true); |
| 285 | |
| 286 | selector = DefaultTrafficSelector.builder(); |
| 287 | treatment = DefaultTrafficTreatment.builder(); |
| 288 | |
Jian Li | 73d3b6a | 2019-07-08 18:07:53 +0900 | [diff] [blame] | 289 | treatment.transition(STAT_EGRESS_TABLE); |
Jian Li | 66f6e3c | 2019-01-25 10:24:44 +0900 | [diff] [blame] | 290 | |
| 291 | flowRule = DefaultFlowRule.builder() |
| 292 | .forDevice(deviceId) |
| 293 | .withSelector(selector.build()) |
| 294 | .withTreatment(treatment.build()) |
| 295 | .withPriority(DROP_PRIORITY) |
| 296 | .fromApp(appId) |
| 297 | .makePermanent() |
Jian Li | 73d3b6a | 2019-07-08 18:07:53 +0900 | [diff] [blame] | 298 | .forTable(GROUPING_TABLE) |
Jian Li | 66f6e3c | 2019-01-25 10:24:44 +0900 | [diff] [blame] | 299 | .build(); |
| 300 | |
| 301 | applyRule(flowRule, true); |
| 302 | } |
| 303 | |
Jian Li | 7d111d7 | 2019-04-12 13:58:44 +0900 | [diff] [blame] | 304 | private void setAnyRoutingRule(IpPrefix srcIpPrefix, MacAddress mac, |
| 305 | K8sNetwork k8sNetwork) { |
Jian Li | 2cc2b63 | 2019-02-18 00:56:40 +0900 | [diff] [blame] | 306 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder() |
| 307 | .matchEthType(Ethernet.TYPE_IPV4) |
Jian Li | 004526d | 2019-02-25 16:26:27 +0900 | [diff] [blame] | 308 | .matchIPSrc(srcIpPrefix) |
Jian Li | 4aa1764 | 2019-01-30 00:01:11 +0900 | [diff] [blame] | 309 | .matchIPDst(IpPrefix.valueOf(k8sNetwork.cidr())); |
| 310 | |
Jian Li | 4aa1764 | 2019-01-30 00:01:11 +0900 | [diff] [blame] | 311 | for (K8sNode node : k8sNodeService.completeNodes()) { |
Jian Li | 7d111d7 | 2019-04-12 13:58:44 +0900 | [diff] [blame] | 312 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder() |
| 313 | .setTunnelId(Long.valueOf(k8sNetwork.segmentId())); |
| 314 | |
| 315 | if (node.hostname().equals(k8sNetwork.name())) { |
| 316 | if (mac != null) { |
| 317 | tBuilder.setEthSrc(mac); |
| 318 | } |
Jian Li | 73d3b6a | 2019-07-08 18:07:53 +0900 | [diff] [blame] | 319 | tBuilder.transition(STAT_EGRESS_TABLE); |
Jian Li | 7d111d7 | 2019-04-12 13:58:44 +0900 | [diff] [blame] | 320 | } else { |
Jian Li | 619fa28 | 2020-09-02 14:45:35 +0900 | [diff] [blame] | 321 | K8sNode localNode = k8sNodeService.node(k8sNetwork.name()); |
| 322 | tBuilder.setOutput(node.intgToTunPortNum()); |
| 323 | |
| 324 | // install flows into tunnel bridge |
Jian Li | 7d111d7 | 2019-04-12 13:58:44 +0900 | [diff] [blame] | 325 | PortNumber portNum = tunnelPortNumByNetId(k8sNetwork.networkId(), |
| 326 | k8sNetworkService, node); |
Jian Li | 619fa28 | 2020-09-02 14:45:35 +0900 | [diff] [blame] | 327 | TrafficTreatment treatmentToRemote = DefaultTrafficTreatment.builder() |
| 328 | .extension(buildExtension( |
| 329 | deviceService, |
| 330 | node.tunBridge(), |
| 331 | localNode.dataIp().getIp4Address()), |
| 332 | node.tunBridge()) |
| 333 | .setTunnelId(Long.valueOf(k8sNetwork.segmentId())) |
| 334 | .setOutput(portNum) |
| 335 | .build(); |
Jian Li | 7d111d7 | 2019-04-12 13:58:44 +0900 | [diff] [blame] | 336 | |
Jian Li | 619fa28 | 2020-09-02 14:45:35 +0900 | [diff] [blame] | 337 | FlowRule remoteFlowRule = DefaultFlowRule.builder() |
| 338 | .forDevice(node.tunBridge()) |
| 339 | .withSelector(sBuilder.build()) |
| 340 | .withTreatment(treatmentToRemote) |
| 341 | .withPriority(PRIORITY_CIDR_RULE) |
| 342 | .fromApp(appId) |
| 343 | .makePermanent() |
| 344 | .forTable(TUN_ENTRY_TABLE) |
| 345 | .build(); |
| 346 | applyRule(remoteFlowRule, true); |
Jian Li | 7d111d7 | 2019-04-12 13:58:44 +0900 | [diff] [blame] | 347 | } |
| 348 | |
Jian Li | 4aa1764 | 2019-01-30 00:01:11 +0900 | [diff] [blame] | 349 | FlowRule flowRule = DefaultFlowRule.builder() |
| 350 | .forDevice(node.intgBridge()) |
| 351 | .withSelector(sBuilder.build()) |
| 352 | .withTreatment(tBuilder.build()) |
Jian Li | 7d111d7 | 2019-04-12 13:58:44 +0900 | [diff] [blame] | 353 | .withPriority(PRIORITY_CIDR_RULE) |
Jian Li | 4aa1764 | 2019-01-30 00:01:11 +0900 | [diff] [blame] | 354 | .fromApp(appId) |
| 355 | .makePermanent() |
Jian Li | 2cc2b63 | 2019-02-18 00:56:40 +0900 | [diff] [blame] | 356 | .forTable(ROUTING_TABLE) |
| 357 | .build(); |
| 358 | applyRule(flowRule, true); |
| 359 | } |
| 360 | } |
| 361 | |
Jian Li | 004526d | 2019-02-25 16:26:27 +0900 | [diff] [blame] | 362 | private void setupHostRoutingRule(K8sNetwork k8sNetwork) { |
Jian Li | 7d111d7 | 2019-04-12 13:58:44 +0900 | [diff] [blame] | 363 | setAnyRoutingRule(IpPrefix.valueOf( |
Jian Li | 1a2eb5d | 2019-08-27 02:07:05 +0900 | [diff] [blame] | 364 | k8sNetwork.gatewayIp(), HOST_PREFIX), null, k8sNetwork); |
Jian Li | 4aa1764 | 2019-01-30 00:01:11 +0900 | [diff] [blame] | 365 | } |
| 366 | |
Jian Li | 66f6e3c | 2019-01-25 10:24:44 +0900 | [diff] [blame] | 367 | private class InternalK8sNodeListener implements K8sNodeListener { |
| 368 | private boolean isRelevantHelper() { |
| 369 | return Objects.equals(localNodeId, leadershipService.getLeader(appId.name())); |
| 370 | } |
| 371 | |
| 372 | @Override |
| 373 | public void event(K8sNodeEvent event) { |
Jian Li | 66f6e3c | 2019-01-25 10:24:44 +0900 | [diff] [blame] | 374 | switch (event.type()) { |
| 375 | case K8S_NODE_COMPLETE: |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 376 | deviceEventExecutor.execute(() -> processNodeCompletion(event.subject())); |
Jian Li | 66f6e3c | 2019-01-25 10:24:44 +0900 | [diff] [blame] | 377 | break; |
| 378 | case K8S_NODE_CREATED: |
| 379 | default: |
| 380 | // do nothing |
| 381 | break; |
| 382 | } |
| 383 | } |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 384 | |
| 385 | private void processNodeCompletion(K8sNode node) { |
| 386 | log.info("COMPLETE node {} is detected", node.hostname()); |
| 387 | |
| 388 | if (!isRelevantHelper()) { |
| 389 | return; |
| 390 | } |
| 391 | |
| 392 | initializePipeline(node); |
Jian Li | 7d111d7 | 2019-04-12 13:58:44 +0900 | [diff] [blame] | 393 | |
Jian Li | f5da78a | 2019-04-15 01:52:23 +0900 | [diff] [blame] | 394 | k8sNetworkService.networks().forEach(K8sFlowRuleManager.this::setupHostRoutingRule); |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 395 | } |
Jian Li | 66f6e3c | 2019-01-25 10:24:44 +0900 | [diff] [blame] | 396 | } |
Jian Li | 4aa1764 | 2019-01-30 00:01:11 +0900 | [diff] [blame] | 397 | |
| 398 | private class InternalK8sNetworkListener implements K8sNetworkListener { |
| 399 | |
| 400 | private boolean isRelevantHelper() { |
| 401 | return Objects.equals(localNodeId, leadershipService.getLeader(appId.name())); |
| 402 | } |
| 403 | |
| 404 | @Override |
| 405 | public void event(K8sNetworkEvent event) { |
| 406 | |
| 407 | switch (event.type()) { |
| 408 | case K8S_NETWORK_CREATED: |
| 409 | deviceEventExecutor.execute(() -> processNetworkCreation(event.subject())); |
| 410 | break; |
| 411 | case K8S_NETWORK_REMOVED: |
Jian Li | 4aa1764 | 2019-01-30 00:01:11 +0900 | [diff] [blame] | 412 | default: |
Jian Li | 1a2eb5d | 2019-08-27 02:07:05 +0900 | [diff] [blame] | 413 | // do nothing |
Jian Li | 4aa1764 | 2019-01-30 00:01:11 +0900 | [diff] [blame] | 414 | break; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | private void processNetworkCreation(K8sNetwork network) { |
| 419 | if (!isRelevantHelper()) { |
| 420 | return; |
| 421 | } |
| 422 | |
Jian Li | 2cc2b63 | 2019-02-18 00:56:40 +0900 | [diff] [blame] | 423 | setupHostRoutingRule(network); |
Jian Li | 4aa1764 | 2019-01-30 00:01:11 +0900 | [diff] [blame] | 424 | } |
| 425 | } |
Jian Li | 66f6e3c | 2019-01-25 10:24:44 +0900 | [diff] [blame] | 426 | } |