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