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. |
| 99 | * |
| 100 | */ |
| 101 | public DefaultVirtualPacketProvider() { |
| 102 | super(new ProviderId("virtual-packet", |
| 103 | "org.onosproject.virtual.virtual-packet")); |
| 104 | } |
| 105 | |
| 106 | @Activate |
| 107 | public void activate() { |
| 108 | appId = coreService.registerApplication( |
yoonseon | 4994c3d | 2017-01-27 13:07:18 -0800 | [diff] [blame] | 109 | "org.onosproject.virtual.virtual-packet"); |
yoonseon | 736a73b | 2017-01-26 14:27:48 -0800 | [diff] [blame] | 110 | providerRegistryService.registerProvider(this); |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 111 | |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 112 | contextMap = Maps.newConcurrentMap(); |
| 113 | |
| 114 | log.info("Started"); |
| 115 | } |
| 116 | |
| 117 | @Deactivate |
| 118 | public void deactivate() { |
Jonathan Hart | 6602867 | 2017-02-02 16:25:49 -0800 | [diff] [blame] | 119 | if (processor != null) { |
| 120 | packetService.removeProcessor(processor); |
| 121 | } |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 122 | log.info("Stopped"); |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Modified |
| 126 | protected void modified(ComponentContext context) { |
| 127 | Dictionary<?, ?> properties = context.getProperties(); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | @Override |
| 132 | public void emit(NetworkId networkId, OutboundPacket packet) { |
| 133 | packetService.emit(devirtualize(networkId, packet)); |
| 134 | } |
| 135 | |
yoonseon | fb4a1db | 2017-01-31 11:38:30 -0800 | [diff] [blame] | 136 | @Override |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 137 | public void startPacketHandling(NetworkId networkId) { |
| 138 | requestsSet.add(networkId); |
| 139 | |
| 140 | if (processor == null) { |
| 141 | processor = new InternalPacketProcessor(); |
| 142 | packetService.addProcessor(processor, PACKET_PROCESSOR_PRIORITY); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public void stopPacketHandling(NetworkId networkId) { |
| 148 | requestsSet.remove(networkId); |
| 149 | |
| 150 | if (requestsSet.isEmpty()) { |
| 151 | packetService.removeProcessor(processor); |
| 152 | processor = null; |
| 153 | } |
yoonseon | fb4a1db | 2017-01-31 11:38:30 -0800 | [diff] [blame] | 154 | } |
| 155 | |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 156 | /** |
| 157 | * Translate the requested physical PacketContext into a virtual PacketContext. |
| 158 | * See {@link org.onosproject.net.packet.OutboundPacket} |
| 159 | * |
| 160 | * @param context A physical PacketContext be translated |
| 161 | * @return A translated virtual PacketContext |
| 162 | */ |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 163 | private VirtualPacketContext virtualize(PacketContext context) { |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 164 | |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 165 | VirtualPort vPort = getMappedVirtualPort(context.inPacket().receivedFrom()); |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 166 | |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 167 | if (vPort != null) { |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 168 | ConnectPoint cp = new ConnectPoint(vPort.element().id(), |
| 169 | vPort.number()); |
| 170 | |
| 171 | Ethernet eth = context.inPacket().parsed(); |
| 172 | eth.setVlanID(Ethernet.VLAN_UNTAGGED); |
| 173 | |
| 174 | InboundPacket inPacket = |
| 175 | new DefaultInboundPacket(cp, eth, |
| 176 | ByteBuffer.wrap(eth.serialize())); |
| 177 | |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 178 | DefaultOutboundPacket outPkt = |
| 179 | new DefaultOutboundPacket(cp.deviceId(), |
| 180 | DefaultTrafficTreatment.builder().build(), |
| 181 | ByteBuffer.wrap(eth.serialize())); |
| 182 | |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 183 | VirtualPacketContext vContext = |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 184 | new VirtualPacketContext(context.time(), inPacket, outPkt, |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 185 | false, vPort.networkId(), |
| 186 | this); |
| 187 | |
| 188 | contextMap.put(vContext, context); |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 189 | |
| 190 | return vContext; |
| 191 | } else { |
| 192 | return null; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Find the corresponding virtual port with the physical port. |
| 199 | * |
| 200 | * @param cp the connect point for the physical network |
| 201 | * @return a virtual port |
| 202 | */ |
| 203 | private VirtualPort getMappedVirtualPort(ConnectPoint cp) { |
| 204 | Set<TenantId> tIds = vnaService.getTenantIds(); |
| 205 | |
| 206 | Set<VirtualNetwork> vNetworks = new HashSet<>(); |
| 207 | tIds.forEach(tid -> vNetworks.addAll(vnaService.getVirtualNetworks(tid))); |
| 208 | |
| 209 | for (VirtualNetwork vNet : vNetworks) { |
| 210 | Set<VirtualDevice> vDevices = vnaService.getVirtualDevices(vNet.id()); |
| 211 | |
| 212 | Set<VirtualPort> vPorts = new HashSet<>(); |
| 213 | vDevices.forEach(dev -> vPorts |
| 214 | .addAll(vnaService.getVirtualPorts(dev.networkId(), dev.id()))); |
| 215 | |
| 216 | VirtualPort vPort = vPorts.stream() |
| 217 | .filter(vp -> vp.realizedBy().equals(cp)) |
| 218 | .findFirst().orElse(null); |
| 219 | |
| 220 | if (vPort != null) { |
| 221 | return vPort; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | return null; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Translate the requested a virtual outbound packet into |
| 230 | * a physical OutboundPacket. |
| 231 | * See {@link org.onosproject.net.packet.PacketContext} |
| 232 | * |
| 233 | * @param packet A OutboundPacket to be translated |
| 234 | * @return de-virtualized (physical) OutboundPacket |
| 235 | */ |
| 236 | private OutboundPacket devirtualize(NetworkId networkId, OutboundPacket packet) { |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 237 | Set<VirtualPort> vPorts = vnaService |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 238 | .getVirtualPorts(networkId, packet.sendThrough()); |
| 239 | |
| 240 | PortNumber vOutPortNum = packet.treatment().allInstructions().stream() |
| 241 | .filter(i -> i.type() == Instruction.Type.OUTPUT) |
| 242 | .map(i -> ((Instructions.OutputInstruction) i).port()) |
| 243 | .findFirst().get(); |
| 244 | |
| 245 | Optional<ConnectPoint> optionalCpOut = vPorts.stream() |
| 246 | .filter(v -> v.number().equals(vOutPortNum)) |
| 247 | .map(v -> v.realizedBy()) |
| 248 | .findFirst(); |
| 249 | if (!optionalCpOut.isPresent()) { |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 250 | log.warn("Port {} is not realized yet, in Network {}, Device {}", |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 251 | vOutPortNum, networkId, packet.sendThrough()); |
| 252 | return null; |
| 253 | } |
| 254 | ConnectPoint egressPoint = optionalCpOut.get(); |
| 255 | |
| 256 | TrafficTreatment.Builder commonTreatmentBuilder |
| 257 | = DefaultTrafficTreatment.builder(); |
| 258 | packet.treatment().allInstructions().stream() |
| 259 | .filter(i -> i.type() != Instruction.Type.OUTPUT) |
| 260 | .forEach(i -> commonTreatmentBuilder.add(i)); |
| 261 | TrafficTreatment commonTreatment = commonTreatmentBuilder.build(); |
| 262 | |
| 263 | TrafficTreatment treatment = DefaultTrafficTreatment |
| 264 | .builder(commonTreatment) |
| 265 | .setOutput(egressPoint.port()).build(); |
| 266 | |
| 267 | OutboundPacket outboundPacket = new DefaultOutboundPacket( |
| 268 | egressPoint.deviceId(), treatment, packet.data()); |
| 269 | return outboundPacket; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Translate the requested a virtual Packet Context into |
| 274 | * a physical Packet Context. |
| 275 | * This method is designed to support Context's send() method that invoked |
| 276 | * by applications. |
| 277 | * See {@link org.onosproject.net.packet.PacketContext} |
| 278 | * |
| 279 | * @param context A handled packet context |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 280 | */ |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 281 | public void devirtualizeContext(VirtualPacketContext context) { |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 282 | NetworkId networkId = context.getNetworkId(); |
| 283 | |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 284 | TrafficTreatment vTreatment = context.treatmentBuilder().build(); |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 285 | |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 286 | DeviceId sendThrough = context.outPacket().sendThrough(); |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 287 | |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 288 | PortNumber vOutPortNum = vTreatment.allInstructions().stream() |
| 289 | .filter(i -> i.type() == Instruction.Type.OUTPUT) |
| 290 | .map(i -> ((Instructions.OutputInstruction) i).port()) |
| 291 | .findFirst().get(); |
| 292 | |
| 293 | TrafficTreatment.Builder commonTreatmentBuilder |
| 294 | = DefaultTrafficTreatment.builder(); |
| 295 | vTreatment.allInstructions().stream() |
| 296 | .filter(i -> i.type() != Instruction.Type.OUTPUT) |
| 297 | .forEach(i -> commonTreatmentBuilder.add(i)); |
| 298 | TrafficTreatment commonTreatment = commonTreatmentBuilder.build(); |
| 299 | |
| 300 | if (!vOutPortNum.isLogical()) { |
| 301 | TrafficTreatment treatment = DefaultTrafficTreatment |
| 302 | .builder() |
| 303 | .addTreatment(commonTreatment) |
| 304 | .setOutput(vOutPortNum) |
| 305 | .build(); |
| 306 | |
| 307 | emit(networkId, new DefaultOutboundPacket(sendThrough, |
| 308 | treatment, |
| 309 | context.outPacket().data())); |
| 310 | } else { |
| 311 | if (vOutPortNum == PortNumber.FLOOD) { |
| 312 | Set<VirtualPort> vPorts = vnaService |
| 313 | .getVirtualPorts(networkId, sendThrough); |
| 314 | |
| 315 | Set<VirtualPort> outPorts = vPorts.stream() |
| 316 | .filter(vp -> vp.number() != |
| 317 | context.inPacket().receivedFrom().port()) |
| 318 | .collect(Collectors.toSet()); |
| 319 | |
| 320 | for (VirtualPort outPort : outPorts) { |
| 321 | TrafficTreatment treatment = DefaultTrafficTreatment |
| 322 | .builder() |
| 323 | .addTreatment(commonTreatment) |
| 324 | .setOutput(outPort.number()) |
| 325 | .build(); |
| 326 | |
| 327 | emit(networkId, new DefaultOutboundPacket(sendThrough, |
| 328 | treatment, |
| 329 | context.outPacket().data())); |
| 330 | } |
| 331 | } |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 332 | } |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | private final class InternalPacketProcessor implements PacketProcessor { |
| 336 | |
| 337 | @Override |
| 338 | public void process(PacketContext context) { |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 339 | VirtualPacketContext vContexts = virtualize(context); |
yoonseon | 736a73b | 2017-01-26 14:27:48 -0800 | [diff] [blame] | 340 | |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 341 | if (vContexts == null) { |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | VirtualPacketProviderService service = |
| 346 | (VirtualPacketProviderService) providerRegistryService |
| 347 | .getProviderService(vContexts.getNetworkId(), |
| 348 | VirtualPacketProvider.class); |
| 349 | if (service != null) { |
| 350 | service.processPacket(vContexts); |
| 351 | } |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | } |