Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 | */ |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 16 | package org.opencord.cordvtn.impl.handler; |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 17 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame^] | 18 | import com.google.common.collect.ImmutableSet; |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 25 | import org.onlab.packet.Ethernet; |
| 26 | import org.onlab.packet.Ip4Address; |
| 27 | import org.onlab.packet.Ip4Prefix; |
| 28 | import org.onosproject.net.flow.DefaultFlowRule; |
| 29 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 30 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 31 | import org.onosproject.net.flow.FlowRule; |
| 32 | import org.onosproject.net.flow.TrafficSelector; |
| 33 | import org.onosproject.net.flow.TrafficTreatment; |
| 34 | import org.onosproject.net.flow.instructions.ExtensionTreatment; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame^] | 35 | import org.opencord.cordvtn.api.VtnNetwork; |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 36 | import org.opencord.cordvtn.api.CordVtnNode; |
| 37 | import org.opencord.cordvtn.api.Instance; |
| 38 | import org.opencord.cordvtn.api.InstanceHandler; |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 39 | import org.opencord.cordvtn.impl.CordVtnNodeManager; |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 40 | import org.opencord.cordvtn.impl.CordVtnPipeline; |
| 41 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame^] | 42 | import static org.opencord.cordvtn.api.ServiceNetwork.ServiceNetworkType.PRIVATE; |
| 43 | import static org.opencord.cordvtn.api.ServiceNetwork.ServiceNetworkType.PUBLIC; |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Provides network connectivity for default service instances. |
| 47 | */ |
| 48 | @Component(immediate = true) |
| 49 | public class DefaultInstanceHandler extends AbstractInstanceHandler implements InstanceHandler { |
| 50 | |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 51 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 52 | protected CordVtnPipeline pipeline; |
| 53 | |
| 54 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 55 | protected CordVtnNodeManager nodeManager; |
| 56 | |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 57 | @Activate |
| 58 | protected void activate() { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame^] | 59 | netTypes = ImmutableSet.of(PRIVATE, PUBLIC); |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 60 | super.activate(); |
| 61 | } |
| 62 | |
| 63 | @Deactivate |
| 64 | protected void deactivate() { |
| 65 | super.deactivate(); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public void instanceDetected(Instance instance) { |
| 70 | log.info("Instance is detected {}", instance); |
| 71 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame^] | 72 | VtnNetwork vtnNet = getVtnNetwork(instance); |
| 73 | populateDefaultRules(instance, vtnNet, true); |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void instanceRemoved(Instance instance) { |
| 78 | log.info("Instance is removed {}", instance); |
| 79 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame^] | 80 | VtnNetwork vtnNet = getVtnNetwork(instance); |
| 81 | populateDefaultRules(instance, vtnNet, false); |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame^] | 84 | private void populateDefaultRules(Instance instance, VtnNetwork vtnNet, boolean install) { |
| 85 | long vni = vtnNet.segmentId().id(); |
| 86 | Ip4Prefix serviceIpRange = vtnNet.subnet().getIp4Prefix(); |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 87 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame^] | 88 | populateInPortRule(instance, install); |
| 89 | populateDstIpRule(instance, vni, install); |
| 90 | populateTunnelInRule(instance, vni, install); |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 91 | |
| 92 | if (install) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame^] | 93 | populateDirectAccessRule(serviceIpRange, serviceIpRange, true); |
| 94 | populateServiceIsolationRule(serviceIpRange, true); |
| 95 | } else if (getInstances(vtnNet.id()).isEmpty()) { |
| 96 | populateDirectAccessRule(serviceIpRange, serviceIpRange, false); |
| 97 | populateServiceIsolationRule(serviceIpRange, false); |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame^] | 101 | private void populateInPortRule(Instance instance, boolean install) { |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 102 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 103 | .matchInPort(instance.portNumber()) |
| 104 | .matchEthType(Ethernet.TYPE_IPV4) |
| 105 | .matchIPSrc(instance.ipAddress().toIpPrefix()) |
| 106 | .build(); |
| 107 | |
| 108 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 109 | .transition(CordVtnPipeline.TABLE_ACCESS) |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 110 | .build(); |
| 111 | |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 112 | FlowRule flowRule = DefaultFlowRule.builder() |
| 113 | .fromApp(appId) |
| 114 | .withSelector(selector) |
| 115 | .withTreatment(treatment) |
| 116 | .withPriority(CordVtnPipeline.PRIORITY_DEFAULT) |
| 117 | .forDevice(instance.deviceId()) |
| 118 | .forTable(CordVtnPipeline.TABLE_IN_PORT) |
| 119 | .makePermanent() |
| 120 | .build(); |
| 121 | |
| 122 | pipeline.processFlowRule(install, flowRule); |
| 123 | |
| 124 | selector = DefaultTrafficSelector.builder() |
| 125 | .matchInPort(instance.portNumber()) |
| 126 | .build(); |
| 127 | |
| 128 | treatment = DefaultTrafficTreatment.builder() |
| 129 | .transition(CordVtnPipeline.TABLE_IN_SERVICE) |
| 130 | .build(); |
| 131 | |
| 132 | flowRule = DefaultFlowRule.builder() |
| 133 | .fromApp(appId) |
| 134 | .withSelector(selector) |
| 135 | .withTreatment(treatment) |
| 136 | .withPriority(CordVtnPipeline.PRIORITY_LOW) |
| 137 | .forDevice(instance.deviceId()) |
| 138 | .forTable(CordVtnPipeline.TABLE_IN_PORT) |
| 139 | .makePermanent() |
| 140 | .build(); |
| 141 | |
| 142 | pipeline.processFlowRule(install, flowRule); |
| 143 | } |
| 144 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame^] | 145 | private void populateDstIpRule(Instance instance, long vni, boolean install) { |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 146 | Ip4Address tunnelIp = nodeManager.dataIp(instance.deviceId()).getIp4Address(); |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 147 | |
| 148 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 149 | .matchEthType(Ethernet.TYPE_IPV4) |
| 150 | .matchIPDst(instance.ipAddress().toIpPrefix()) |
| 151 | .build(); |
| 152 | |
| 153 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 154 | .setEthDst(instance.mac()) |
| 155 | .setOutput(instance.portNumber()) |
| 156 | .build(); |
| 157 | |
| 158 | FlowRule flowRule = DefaultFlowRule.builder() |
| 159 | .fromApp(appId) |
| 160 | .withSelector(selector) |
| 161 | .withTreatment(treatment) |
| 162 | .withPriority(CordVtnPipeline.PRIORITY_DEFAULT) |
| 163 | .forDevice(instance.deviceId()) |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 164 | .forTable(CordVtnPipeline.TABLE_DST) |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 165 | .makePermanent() |
| 166 | .build(); |
| 167 | |
| 168 | pipeline.processFlowRule(install, flowRule); |
| 169 | |
| 170 | for (CordVtnNode node : nodeManager.completeNodes()) { |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 171 | if (node.integrationBridgeId().equals(instance.deviceId())) { |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 172 | continue; |
| 173 | } |
| 174 | |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 175 | ExtensionTreatment tunnelDst = |
| 176 | pipeline.tunnelDstTreatment(node.integrationBridgeId(), tunnelIp); |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 177 | if (tunnelDst == null) { |
| 178 | continue; |
| 179 | } |
| 180 | |
| 181 | treatment = DefaultTrafficTreatment.builder() |
| 182 | .setEthDst(instance.mac()) |
| 183 | .setTunnelId(vni) |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 184 | .extension(tunnelDst, node.integrationBridgeId()) |
| 185 | .setOutput(nodeManager.tunnelPort(node.integrationBridgeId())) |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 186 | .build(); |
| 187 | |
| 188 | flowRule = DefaultFlowRule.builder() |
| 189 | .fromApp(appId) |
| 190 | .withSelector(selector) |
| 191 | .withTreatment(treatment) |
| 192 | .withPriority(CordVtnPipeline.PRIORITY_DEFAULT) |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 193 | .forDevice(node.integrationBridgeId()) |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 194 | .forTable(CordVtnPipeline.TABLE_DST) |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 195 | .makePermanent() |
| 196 | .build(); |
| 197 | |
| 198 | pipeline.processFlowRule(install, flowRule); |
| 199 | } |
| 200 | } |
| 201 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame^] | 202 | private void populateTunnelInRule(Instance instance, long vni, boolean install) { |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 203 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 204 | .matchTunnelId(vni) |
| 205 | .matchEthDst(instance.mac()) |
| 206 | .build(); |
| 207 | |
| 208 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 209 | .setOutput(instance.portNumber()) |
| 210 | .build(); |
| 211 | |
| 212 | FlowRule flowRule = DefaultFlowRule.builder() |
| 213 | .fromApp(appId) |
| 214 | .withSelector(selector) |
| 215 | .withTreatment(treatment) |
| 216 | .withPriority(CordVtnPipeline.PRIORITY_DEFAULT) |
| 217 | .forDevice(instance.deviceId()) |
| 218 | .forTable(CordVtnPipeline.TABLE_TUNNEL_IN) |
| 219 | .makePermanent() |
| 220 | .build(); |
| 221 | |
| 222 | pipeline.processFlowRule(install, flowRule); |
| 223 | } |
| 224 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame^] | 225 | private void populateDirectAccessRule(Ip4Prefix srcRange, Ip4Prefix dstRange, boolean install) { |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 226 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 227 | .matchEthType(Ethernet.TYPE_IPV4) |
| 228 | .matchIPSrc(srcRange) |
| 229 | .matchIPDst(dstRange) |
| 230 | .build(); |
| 231 | |
| 232 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 233 | .transition(CordVtnPipeline.TABLE_DST) |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 234 | .build(); |
| 235 | |
| 236 | |
| 237 | nodeManager.completeNodes().stream().forEach(node -> { |
| 238 | FlowRule flowRuleDirect = DefaultFlowRule.builder() |
| 239 | .fromApp(appId) |
| 240 | .withSelector(selector) |
| 241 | .withTreatment(treatment) |
| 242 | .withPriority(CordVtnPipeline.PRIORITY_DEFAULT) |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 243 | .forDevice(node.integrationBridgeId()) |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 244 | .forTable(CordVtnPipeline.TABLE_ACCESS) |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 245 | .makePermanent() |
| 246 | .build(); |
| 247 | |
| 248 | pipeline.processFlowRule(install, flowRuleDirect); |
| 249 | }); |
| 250 | } |
| 251 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame^] | 252 | private void populateServiceIsolationRule(Ip4Prefix dstRange, boolean install) { |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 253 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 254 | .matchEthType(Ethernet.TYPE_IPV4) |
| 255 | .matchIPDst(dstRange) |
| 256 | .build(); |
| 257 | |
| 258 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 259 | .drop() |
| 260 | .build(); |
| 261 | |
| 262 | nodeManager.completeNodes().stream().forEach(node -> { |
| 263 | FlowRule flowRuleDirect = DefaultFlowRule.builder() |
| 264 | .fromApp(appId) |
| 265 | .withSelector(selector) |
| 266 | .withTreatment(treatment) |
| 267 | .withPriority(CordVtnPipeline.PRIORITY_LOW) |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 268 | .forDevice(node.integrationBridgeId()) |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 269 | .forTable(CordVtnPipeline.TABLE_ACCESS) |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 270 | .makePermanent() |
| 271 | .build(); |
| 272 | |
| 273 | pipeline.processFlowRule(install, flowRuleDirect); |
| 274 | }); |
| 275 | } |
| 276 | } |