yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -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 | */ |
| 16 | |
| 17 | package org.onosproject.incubator.net.virtual.impl.provider; |
| 18 | |
| 19 | import com.google.common.collect.Maps; |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 20 | import com.google.common.collect.Sets; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Modified; |
| 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 27 | import org.onlab.packet.Ethernet; |
| 28 | import org.onosproject.core.ApplicationId; |
| 29 | import org.onosproject.core.CoreService; |
| 30 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 31 | import org.onosproject.incubator.net.virtual.TenantId; |
| 32 | import org.onosproject.incubator.net.virtual.VirtualDevice; |
| 33 | import org.onosproject.incubator.net.virtual.VirtualNetwork; |
| 34 | import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService; |
| 35 | import org.onosproject.incubator.net.virtual.VirtualPort; |
| 36 | import org.onosproject.incubator.net.virtual.provider.AbstractVirtualProvider; |
| 37 | import org.onosproject.incubator.net.virtual.provider.VirtualPacketProvider; |
| 38 | import org.onosproject.incubator.net.virtual.provider.VirtualPacketProviderService; |
yoonseon | 736a73b | 2017-01-26 14:27:48 -0800 | [diff] [blame] | 39 | import org.onosproject.incubator.net.virtual.provider.VirtualProviderRegistryService; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 40 | import org.onosproject.net.ConnectPoint; |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 41 | import org.onosproject.net.DeviceId; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 42 | import org.onosproject.net.PortNumber; |
| 43 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 44 | import org.onosproject.net.flow.TrafficTreatment; |
| 45 | import org.onosproject.net.flow.instructions.Instruction; |
| 46 | import org.onosproject.net.flow.instructions.Instructions; |
| 47 | import org.onosproject.net.packet.DefaultInboundPacket; |
| 48 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 49 | import org.onosproject.net.packet.InboundPacket; |
| 50 | import org.onosproject.net.packet.OutboundPacket; |
| 51 | import org.onosproject.net.packet.PacketContext; |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 52 | import org.onosproject.net.packet.PacketPriority; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 53 | import org.onosproject.net.packet.PacketProcessor; |
| 54 | import org.onosproject.net.packet.PacketService; |
| 55 | import org.onosproject.net.provider.ProviderId; |
| 56 | import org.osgi.service.component.ComponentContext; |
| 57 | import org.slf4j.Logger; |
| 58 | |
| 59 | import java.nio.ByteBuffer; |
| 60 | import java.util.Dictionary; |
| 61 | import java.util.HashSet; |
| 62 | import java.util.Map; |
| 63 | import java.util.Optional; |
| 64 | import java.util.Set; |
| 65 | import java.util.stream.Collectors; |
| 66 | |
| 67 | import static org.slf4j.LoggerFactory.getLogger; |
| 68 | |
| 69 | @Component(immediate = true) |
| 70 | public class DefaultVirtualPacketProvider extends AbstractVirtualProvider |
| 71 | implements VirtualPacketProvider { |
| 72 | |
| 73 | private static final int PACKET_PROCESSOR_PRIORITY = 1; |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 74 | private static final PacketPriority VIRTUAL_PACKET_PRIORITY = PacketPriority.REACTIVE; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 75 | |
| 76 | private final Logger log = getLogger(getClass()); |
| 77 | |
| 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 79 | protected PacketService packetService; |
| 80 | |
| 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 82 | protected CoreService coreService; |
| 83 | |
| 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 85 | protected VirtualNetworkAdminService vnaService; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 86 | |
yoonseon | 736a73b | 2017-01-26 14:27:48 -0800 | [diff] [blame] | 87 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 88 | protected VirtualProviderRegistryService providerRegistryService; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 89 | |
| 90 | ApplicationId appId; |
| 91 | InternalPacketProcessor processor; |
| 92 | |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 93 | private Map<VirtualPacketContext, PacketContext> contextMap; |
| 94 | |
| 95 | private Set<NetworkId> requestsSet = Sets.newHashSet(); |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 96 | |
| 97 | /** |
| 98 | * Creates a provider with the supplied identifier. |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 99 | */ |
| 100 | public DefaultVirtualPacketProvider() { |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame^] | 101 | super(new ProviderId("virtual-packet", "org.onosproject.virtual.virtual-packet")); |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Activate |
| 105 | public void activate() { |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame^] | 106 | appId = coreService.registerApplication("org.onosproject.virtual.virtual-packet"); |
yoonseon | 736a73b | 2017-01-26 14:27:48 -0800 | [diff] [blame] | 107 | providerRegistryService.registerProvider(this); |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 108 | |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 109 | contextMap = Maps.newConcurrentMap(); |
| 110 | |
| 111 | log.info("Started"); |
| 112 | } |
| 113 | |
| 114 | @Deactivate |
| 115 | public void deactivate() { |
Jonathan Hart | 6602867 | 2017-02-02 16:25:49 -0800 | [diff] [blame] | 116 | if (processor != null) { |
| 117 | packetService.removeProcessor(processor); |
| 118 | } |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame^] | 119 | providerRegistryService.unregisterProvider(this); |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 120 | log.info("Stopped"); |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Modified |
| 124 | protected void modified(ComponentContext context) { |
| 125 | Dictionary<?, ?> properties = context.getProperties(); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | @Override |
| 130 | public void emit(NetworkId networkId, OutboundPacket packet) { |
| 131 | packetService.emit(devirtualize(networkId, packet)); |
| 132 | } |
| 133 | |
yoonseon | fb4a1db | 2017-01-31 11:38:30 -0800 | [diff] [blame] | 134 | @Override |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 135 | public void startPacketHandling(NetworkId networkId) { |
| 136 | requestsSet.add(networkId); |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 137 | if (processor == null) { |
| 138 | processor = new InternalPacketProcessor(); |
| 139 | packetService.addProcessor(processor, PACKET_PROCESSOR_PRIORITY); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public void stopPacketHandling(NetworkId networkId) { |
| 145 | requestsSet.remove(networkId); |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 146 | if (requestsSet.isEmpty()) { |
| 147 | packetService.removeProcessor(processor); |
| 148 | processor = null; |
| 149 | } |
yoonseon | fb4a1db | 2017-01-31 11:38:30 -0800 | [diff] [blame] | 150 | } |
| 151 | |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 152 | /** |
| 153 | * Translate the requested physical PacketContext into a virtual PacketContext. |
| 154 | * See {@link org.onosproject.net.packet.OutboundPacket} |
| 155 | * |
| 156 | * @param context A physical PacketContext be translated |
| 157 | * @return A translated virtual PacketContext |
| 158 | */ |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 159 | private VirtualPacketContext virtualize(PacketContext context) { |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 160 | |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 161 | VirtualPort vPort = getMappedVirtualPort(context.inPacket().receivedFrom()); |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 162 | |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 163 | if (vPort != null) { |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 164 | ConnectPoint cp = new ConnectPoint(vPort.element().id(), |
| 165 | vPort.number()); |
| 166 | |
| 167 | Ethernet eth = context.inPacket().parsed(); |
| 168 | eth.setVlanID(Ethernet.VLAN_UNTAGGED); |
| 169 | |
| 170 | InboundPacket inPacket = |
| 171 | new DefaultInboundPacket(cp, eth, |
| 172 | ByteBuffer.wrap(eth.serialize())); |
| 173 | |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 174 | DefaultOutboundPacket outPkt = |
| 175 | new DefaultOutboundPacket(cp.deviceId(), |
| 176 | DefaultTrafficTreatment.builder().build(), |
| 177 | ByteBuffer.wrap(eth.serialize())); |
| 178 | |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 179 | VirtualPacketContext vContext = |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 180 | new VirtualPacketContext(context.time(), inPacket, outPkt, |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 181 | false, vPort.networkId(), |
| 182 | this); |
| 183 | |
| 184 | contextMap.put(vContext, context); |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 185 | |
| 186 | return vContext; |
| 187 | } else { |
| 188 | return null; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Find the corresponding virtual port with the physical port. |
| 195 | * |
| 196 | * @param cp the connect point for the physical network |
| 197 | * @return a virtual port |
| 198 | */ |
| 199 | private VirtualPort getMappedVirtualPort(ConnectPoint cp) { |
| 200 | Set<TenantId> tIds = vnaService.getTenantIds(); |
| 201 | |
| 202 | Set<VirtualNetwork> vNetworks = new HashSet<>(); |
| 203 | tIds.forEach(tid -> vNetworks.addAll(vnaService.getVirtualNetworks(tid))); |
| 204 | |
| 205 | for (VirtualNetwork vNet : vNetworks) { |
| 206 | Set<VirtualDevice> vDevices = vnaService.getVirtualDevices(vNet.id()); |
| 207 | |
| 208 | Set<VirtualPort> vPorts = new HashSet<>(); |
| 209 | vDevices.forEach(dev -> vPorts |
| 210 | .addAll(vnaService.getVirtualPorts(dev.networkId(), dev.id()))); |
| 211 | |
| 212 | VirtualPort vPort = vPorts.stream() |
| 213 | .filter(vp -> vp.realizedBy().equals(cp)) |
| 214 | .findFirst().orElse(null); |
| 215 | |
| 216 | if (vPort != null) { |
| 217 | return vPort; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | return null; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Translate the requested a virtual outbound packet into |
| 226 | * a physical OutboundPacket. |
| 227 | * See {@link org.onosproject.net.packet.PacketContext} |
| 228 | * |
| 229 | * @param packet A OutboundPacket to be translated |
| 230 | * @return de-virtualized (physical) OutboundPacket |
| 231 | */ |
| 232 | private OutboundPacket devirtualize(NetworkId networkId, OutboundPacket packet) { |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 233 | Set<VirtualPort> vPorts = vnaService |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 234 | .getVirtualPorts(networkId, packet.sendThrough()); |
| 235 | |
| 236 | PortNumber vOutPortNum = packet.treatment().allInstructions().stream() |
| 237 | .filter(i -> i.type() == Instruction.Type.OUTPUT) |
| 238 | .map(i -> ((Instructions.OutputInstruction) i).port()) |
| 239 | .findFirst().get(); |
| 240 | |
| 241 | Optional<ConnectPoint> optionalCpOut = vPorts.stream() |
| 242 | .filter(v -> v.number().equals(vOutPortNum)) |
| 243 | .map(v -> v.realizedBy()) |
| 244 | .findFirst(); |
| 245 | if (!optionalCpOut.isPresent()) { |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 246 | log.warn("Port {} is not realized yet, in Network {}, Device {}", |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 247 | vOutPortNum, networkId, packet.sendThrough()); |
| 248 | return null; |
| 249 | } |
| 250 | ConnectPoint egressPoint = optionalCpOut.get(); |
| 251 | |
| 252 | TrafficTreatment.Builder commonTreatmentBuilder |
| 253 | = DefaultTrafficTreatment.builder(); |
| 254 | packet.treatment().allInstructions().stream() |
| 255 | .filter(i -> i.type() != Instruction.Type.OUTPUT) |
| 256 | .forEach(i -> commonTreatmentBuilder.add(i)); |
| 257 | TrafficTreatment commonTreatment = commonTreatmentBuilder.build(); |
| 258 | |
| 259 | TrafficTreatment treatment = DefaultTrafficTreatment |
| 260 | .builder(commonTreatment) |
| 261 | .setOutput(egressPoint.port()).build(); |
| 262 | |
| 263 | OutboundPacket outboundPacket = new DefaultOutboundPacket( |
| 264 | egressPoint.deviceId(), treatment, packet.data()); |
| 265 | return outboundPacket; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Translate the requested a virtual Packet Context into |
| 270 | * a physical Packet Context. |
| 271 | * This method is designed to support Context's send() method that invoked |
| 272 | * by applications. |
| 273 | * See {@link org.onosproject.net.packet.PacketContext} |
| 274 | * |
| 275 | * @param context A handled packet context |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 276 | */ |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 277 | public void devirtualizeContext(VirtualPacketContext context) { |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 278 | NetworkId networkId = context.getNetworkId(); |
| 279 | |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 280 | TrafficTreatment vTreatment = context.treatmentBuilder().build(); |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 281 | |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 282 | DeviceId sendThrough = context.outPacket().sendThrough(); |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 283 | |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 284 | PortNumber vOutPortNum = vTreatment.allInstructions().stream() |
| 285 | .filter(i -> i.type() == Instruction.Type.OUTPUT) |
| 286 | .map(i -> ((Instructions.OutputInstruction) i).port()) |
| 287 | .findFirst().get(); |
| 288 | |
| 289 | TrafficTreatment.Builder commonTreatmentBuilder |
| 290 | = DefaultTrafficTreatment.builder(); |
| 291 | vTreatment.allInstructions().stream() |
| 292 | .filter(i -> i.type() != Instruction.Type.OUTPUT) |
| 293 | .forEach(i -> commonTreatmentBuilder.add(i)); |
| 294 | TrafficTreatment commonTreatment = commonTreatmentBuilder.build(); |
| 295 | |
| 296 | if (!vOutPortNum.isLogical()) { |
| 297 | TrafficTreatment treatment = DefaultTrafficTreatment |
| 298 | .builder() |
| 299 | .addTreatment(commonTreatment) |
| 300 | .setOutput(vOutPortNum) |
| 301 | .build(); |
| 302 | |
| 303 | emit(networkId, new DefaultOutboundPacket(sendThrough, |
| 304 | treatment, |
| 305 | context.outPacket().data())); |
| 306 | } else { |
| 307 | if (vOutPortNum == PortNumber.FLOOD) { |
| 308 | Set<VirtualPort> vPorts = vnaService |
| 309 | .getVirtualPorts(networkId, sendThrough); |
| 310 | |
| 311 | Set<VirtualPort> outPorts = vPorts.stream() |
Yoonseon Han | 5cf483a | 2017-04-19 23:14:00 -0700 | [diff] [blame] | 312 | .filter(vp -> !vp.number().isLogical()) |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 313 | .filter(vp -> vp.number() != |
| 314 | context.inPacket().receivedFrom().port()) |
| 315 | .collect(Collectors.toSet()); |
| 316 | |
| 317 | for (VirtualPort outPort : outPorts) { |
| 318 | TrafficTreatment treatment = DefaultTrafficTreatment |
| 319 | .builder() |
| 320 | .addTreatment(commonTreatment) |
| 321 | .setOutput(outPort.number()) |
| 322 | .build(); |
| 323 | |
| 324 | emit(networkId, new DefaultOutboundPacket(sendThrough, |
| 325 | treatment, |
| 326 | context.outPacket().data())); |
| 327 | } |
| 328 | } |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 329 | } |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | private final class InternalPacketProcessor implements PacketProcessor { |
| 333 | |
| 334 | @Override |
| 335 | public void process(PacketContext context) { |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 336 | VirtualPacketContext vContexts = virtualize(context); |
yoonseon | 736a73b | 2017-01-26 14:27:48 -0800 | [diff] [blame] | 337 | |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 338 | if (vContexts == null) { |
| 339 | return; |
| 340 | } |
| 341 | |
| 342 | VirtualPacketProviderService service = |
| 343 | (VirtualPacketProviderService) providerRegistryService |
| 344 | .getProviderService(vContexts.getNetworkId(), |
| 345 | VirtualPacketProvider.class); |
| 346 | if (service != null) { |
| 347 | service.processPacket(vContexts); |
| 348 | } |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | } |