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