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