Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 1 | /* |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 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 | */ |
| 16 | |
Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 17 | package org.onosproject.openstacknetworking.switching; |
| 18 | |
Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
jskim | aa85193 | 2016-10-27 17:45:30 +0900 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Service; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 25 | import org.onlab.packet.Ethernet; |
Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 26 | import org.onlab.packet.Ip4Address; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 27 | import org.onlab.packet.IpAddress; |
| 28 | import org.onosproject.core.ApplicationId; |
| 29 | import org.onosproject.net.DeviceId; |
Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 30 | import org.onosproject.net.Host; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 31 | import org.onosproject.net.PortNumber; |
Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 32 | import org.onosproject.net.device.DeviceService; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 33 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 34 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 35 | import org.onosproject.net.flow.TrafficSelector; |
| 36 | import org.onosproject.net.flow.TrafficTreatment; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 37 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 38 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 39 | import org.onosproject.openstacknetworking.AbstractVmHandler; |
sangho | 91b5003 | 2017-01-06 18:16:21 +0900 | [diff] [blame] | 40 | import org.onosproject.openstacknetworking.OpenstackSwitchingService; |
| 41 | import org.onosproject.openstacknetworking.RulePopulatorUtil; |
Hyunsun Moon | 05d9b26 | 2016-07-03 18:38:44 -0700 | [diff] [blame] | 42 | import org.onosproject.openstacknode.OpenstackNodeService; |
Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 43 | import org.slf4j.Logger; |
| 44 | import org.slf4j.LoggerFactory; |
| 45 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 46 | import java.util.Optional; |
Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 47 | |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 48 | import static org.onosproject.openstacknetworking.Constants.*; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 49 | import static org.onosproject.openstacknetworking.RulePopulatorUtil.buildExtension; |
Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 50 | |
jskim | aa85193 | 2016-10-27 17:45:30 +0900 | [diff] [blame] | 51 | |
Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 52 | /** |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 53 | * Populates switching flow rules. |
Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 54 | */ |
jskim | aa85193 | 2016-10-27 17:45:30 +0900 | [diff] [blame] | 55 | @Service |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 56 | @Component(immediate = true) |
jskim | aa85193 | 2016-10-27 17:45:30 +0900 | [diff] [blame] | 57 | public final class OpenstackSwitchingManager extends AbstractVmHandler |
| 58 | implements OpenstackSwitchingService { |
Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 59 | |
| 60 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 61 | |
| 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 63 | protected DeviceService deviceService; |
| 64 | |
| 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 66 | protected FlowObjectiveService flowObjectiveService; |
Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 67 | |
| 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 69 | protected OpenstackNodeService nodeService; |
Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 70 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 71 | private ApplicationId appId; |
Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 72 | |
| 73 | @Activate |
| 74 | protected void activate() { |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 75 | super.activate(); |
| 76 | appId = coreService.registerApplication(SWITCHING_APP_ID); |
Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Deactivate |
| 80 | protected void deactivate() { |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 81 | super.deactivate(); |
| 82 | } |
Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 83 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 84 | private void populateSwitchingRules(Host host) { |
sangho | 91b5003 | 2017-01-06 18:16:21 +0900 | [diff] [blame] | 85 | setFlowRulesForTunnelTag(host, true); |
| 86 | setFlowRulesForTrafficToSameCnode(host, true); |
| 87 | setFlowRulesForTrafficToDifferentCnode(host, true); |
Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 88 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 89 | log.debug("Populated switching rule for {}", host); |
| 90 | } |
| 91 | |
sangho | 91b5003 | 2017-01-06 18:16:21 +0900 | [diff] [blame] | 92 | private void removeSwitchingRules(Host host) { |
| 93 | setFlowRulesForTunnelTag(host, false); |
| 94 | setFlowRulesForTrafficToSameCnode(host, false); |
| 95 | setFlowRulesForTrafficToDifferentCnode(host, false); |
| 96 | |
| 97 | log.debug("Removed switching rule for {}", host); |
| 98 | } |
| 99 | |
| 100 | private void setFlowRulesForTunnelTag(Host host, boolean install) { |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 101 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 102 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 103 | |
| 104 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 105 | .matchInPort(host.location().port()); |
| 106 | |
| 107 | tBuilder.setTunnelId(Long.valueOf(getVni(host))); |
| 108 | |
sangho | 91b5003 | 2017-01-06 18:16:21 +0900 | [diff] [blame] | 109 | RulePopulatorUtil.setRule(flowObjectiveService, appId, host.location().deviceId(), |
| 110 | sBuilder.build(), tBuilder.build(), ForwardingObjective.Flag.SPECIFIC, |
| 111 | TUNNELTAG_RULE_PRIORITY, install); |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 112 | } |
| 113 | |
sangho | 91b5003 | 2017-01-06 18:16:21 +0900 | [diff] [blame] | 114 | private void setFlowRulesForTrafficToSameCnode(Host host, boolean install) { |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 115 | //For L2 Switching Case |
| 116 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 117 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 118 | |
| 119 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 120 | .matchIPDst(getIp(host).toIpPrefix()) |
| 121 | .matchTunnelId(Long.valueOf(getVni(host))); |
| 122 | |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 123 | // Destination setting is required for routing cases. |
| 124 | // We do not believe the rule would not degrade the forwarding performance. |
| 125 | // But, if it does, we need to move the rule in a separate routing table. |
| 126 | tBuilder.setEthDst(host.mac()) |
| 127 | .setOutput(host.location().port()); |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 128 | |
sangho | 91b5003 | 2017-01-06 18:16:21 +0900 | [diff] [blame] | 129 | RulePopulatorUtil.setRule(flowObjectiveService, appId, host.location().deviceId(), |
| 130 | sBuilder.build(), tBuilder.build(), ForwardingObjective.Flag.SPECIFIC, |
| 131 | SWITCHING_RULE_PRIORITY, install); |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 132 | } |
| 133 | |
sangho | 91b5003 | 2017-01-06 18:16:21 +0900 | [diff] [blame] | 134 | private void setFlowRulesForTrafficToDifferentCnode(Host host, boolean install) { |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 135 | Ip4Address localVmIp = getIp(host); |
| 136 | DeviceId localDeviceId = host.location().deviceId(); |
| 137 | Optional<IpAddress> localDataIp = nodeService.dataIp(localDeviceId); |
| 138 | |
| 139 | if (!localDataIp.isPresent()) { |
| 140 | log.debug("Failed to get data IP for device {}", |
| 141 | host.location().deviceId()); |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | String vni = getVni(host); |
| 146 | getVmsInDifferentCnode(host).forEach(remoteVm -> { |
| 147 | Optional<IpAddress> remoteDataIp = nodeService.dataIp(remoteVm.location().deviceId()); |
| 148 | if (remoteDataIp.isPresent()) { |
| 149 | setVxLanFlowRule(vni, |
| 150 | localDeviceId, |
| 151 | remoteDataIp.get().getIp4Address(), |
sangho | 91b5003 | 2017-01-06 18:16:21 +0900 | [diff] [blame] | 152 | getIp(remoteVm), install); |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 153 | |
| 154 | setVxLanFlowRule(vni, |
| 155 | remoteVm.location().deviceId(), |
| 156 | localDataIp.get().getIp4Address(), |
sangho | 91b5003 | 2017-01-06 18:16:21 +0900 | [diff] [blame] | 157 | localVmIp, install); |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 158 | } |
| 159 | }); |
| 160 | } |
| 161 | |
| 162 | private void setVxLanFlowRule(String vni, DeviceId deviceId, Ip4Address remoteIp, |
sangho | 91b5003 | 2017-01-06 18:16:21 +0900 | [diff] [blame] | 163 | Ip4Address vmIp, boolean install) { |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 164 | Optional<PortNumber> tunnelPort = nodeService.tunnelPort(deviceId); |
| 165 | if (!tunnelPort.isPresent()) { |
| 166 | log.warn("Failed to get tunnel port from {}", deviceId); |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 171 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 172 | |
| 173 | sBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 174 | .matchTunnelId(Long.parseLong(vni)) |
| 175 | .matchIPDst(vmIp.toIpPrefix()); |
| 176 | tBuilder.extension(buildExtension(deviceService, deviceId, remoteIp), deviceId) |
| 177 | .setOutput(tunnelPort.get()); |
| 178 | |
sangho | 91b5003 | 2017-01-06 18:16:21 +0900 | [diff] [blame] | 179 | RulePopulatorUtil.setRule(flowObjectiveService, appId, deviceId, |
| 180 | sBuilder.build(), tBuilder.build(), ForwardingObjective.Flag.SPECIFIC, |
| 181 | SWITCHING_RULE_PRIORITY, install); |
Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | @Override |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 185 | protected void hostDetected(Host host) { |
| 186 | populateSwitchingRules(host); |
| 187 | log.info("Added new virtual machine to switching service {}", host); |
Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 190 | @Override |
| 191 | protected void hostRemoved(Host host) { |
| 192 | removeSwitchingRules(host); |
| 193 | log.info("Removed virtual machine from switching service {}", host); |
Hyunsun Moon | 05d9b26 | 2016-07-03 18:38:44 -0700 | [diff] [blame] | 194 | } |
jskim | aa85193 | 2016-10-27 17:45:30 +0900 | [diff] [blame] | 195 | |
| 196 | @Override |
| 197 | public void reinstallVmFlow(Host host) { |
| 198 | if (host == null) { |
| 199 | hostService.getHosts().forEach(h -> { |
| 200 | populateSwitchingRules(h); |
| 201 | log.info("Re-Install data plane flow of virtual machine {}", h); |
| 202 | }); |
| 203 | } else { |
| 204 | populateSwitchingRules(host); |
| 205 | log.info("Re-Install data plane flow of virtual machine {}", host); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public void purgeVmFlow(Host host) { |
| 211 | if (host == null) { |
| 212 | hostService.getHosts().forEach(h -> { |
| 213 | removeSwitchingRules(h); |
| 214 | log.info("Purge data plane flow of virtual machine {}", h); |
| 215 | }); |
| 216 | } else { |
| 217 | removeSwitchingRules(host); |
| 218 | log.info("Purge data plane flow of virtual machine {}", host); |
| 219 | } |
| 220 | } |
Hyunsun Moon | b974fca | 2016-06-30 21:20:39 -0700 | [diff] [blame] | 221 | } |