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