alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 1 | package org.onlab.onos.fwd; |
| 2 | |
| 3 | import static org.slf4j.LoggerFactory.getLogger; |
| 4 | |
alshabib | 8aef1ad | 2014-09-15 17:47:31 -0700 | [diff] [blame^] | 5 | import java.util.Set; |
| 6 | |
| 7 | import org.onlab.onos.net.Host; |
| 8 | import org.onlab.onos.net.HostId; |
| 9 | import org.onlab.onos.net.Path; |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 10 | import org.onlab.onos.net.PortNumber; |
| 11 | import org.onlab.onos.net.flow.Instructions; |
alshabib | 8aef1ad | 2014-09-15 17:47:31 -0700 | [diff] [blame^] | 12 | import org.onlab.onos.net.host.HostService; |
| 13 | import org.onlab.onos.net.packet.InboundPacket; |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 14 | import org.onlab.onos.net.packet.PacketContext; |
| 15 | import org.onlab.onos.net.packet.PacketProcessor; |
| 16 | import org.onlab.onos.net.topology.TopologyService; |
alshabib | 8aef1ad | 2014-09-15 17:47:31 -0700 | [diff] [blame^] | 17 | import org.onlab.packet.VLANID; |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 18 | import org.slf4j.Logger; |
| 19 | |
| 20 | public class ReactivePacketProcessor implements PacketProcessor { |
| 21 | |
| 22 | private final Logger log = getLogger(getClass()); |
| 23 | private final TopologyService topologyService; |
alshabib | 8aef1ad | 2014-09-15 17:47:31 -0700 | [diff] [blame^] | 24 | private final HostService hostService; |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 25 | |
| 26 | |
alshabib | 8aef1ad | 2014-09-15 17:47:31 -0700 | [diff] [blame^] | 27 | public ReactivePacketProcessor(TopologyService topologyService, HostService hostService) { |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 28 | this.topologyService = topologyService; |
alshabib | 8aef1ad | 2014-09-15 17:47:31 -0700 | [diff] [blame^] | 29 | this.hostService = hostService; |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | |
| 33 | @Override |
| 34 | public void process(PacketContext context) { |
alshabib | 8aef1ad | 2014-09-15 17:47:31 -0700 | [diff] [blame^] | 35 | InboundPacket pkt = context.inPacket(); |
| 36 | HostId id = HostId.hostId(pkt.parsed().getDestinationMAC(), VLANID.vlanId((short) -1)); |
| 37 | Host dst = hostService.getHost(id); |
| 38 | if (dst == null) { |
| 39 | flood(context); |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | Set<Path> p = null; |
| 44 | if (pkt.receivedFrom().deviceId().equals(dst.location().deviceId())) { |
| 45 | context.treatmentBuilder().add(Instructions.createOutput(dst.location().port())); |
| 46 | context.send(); |
| 47 | return; |
| 48 | } else { |
| 49 | p = topologyService.getPaths(topologyService.currentTopology(), |
| 50 | context.inPacket().receivedFrom().deviceId(), dst.location().deviceId()); |
| 51 | } |
| 52 | |
| 53 | if (p.isEmpty()) { |
| 54 | flood(context); |
| 55 | } else { |
| 56 | Path p1 = p.iterator().next(); |
| 57 | context.treatmentBuilder().add(Instructions.createOutput(p1.src().port())); |
| 58 | context.send(); |
| 59 | } |
| 60 | |
| 61 | } |
| 62 | |
| 63 | private void flood(PacketContext context) { |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 64 | boolean canBcast = topologyService.isBroadcastPoint(topologyService.currentTopology(), |
| 65 | context.inPacket().receivedFrom()); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 66 | if (canBcast) { |
| 67 | context.treatmentBuilder().add(Instructions.createOutput(PortNumber.FLOOD)); |
| 68 | context.send(); |
| 69 | } else { |
| 70 | context.block(); |
| 71 | } |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | } |