blob: 4ecb129479b7517cbf0d58167d5179925a20fff9 [file] [log] [blame]
alshabib030111e2014-09-15 15:56:42 -07001package org.onlab.onos.fwd;
2
3import static org.slf4j.LoggerFactory.getLogger;
4
alshabib8aef1ad2014-09-15 17:47:31 -07005import java.util.Set;
6
7import org.onlab.onos.net.Host;
8import org.onlab.onos.net.HostId;
9import org.onlab.onos.net.Path;
alshabib030111e2014-09-15 15:56:42 -070010import org.onlab.onos.net.PortNumber;
11import org.onlab.onos.net.flow.Instructions;
alshabib8aef1ad2014-09-15 17:47:31 -070012import org.onlab.onos.net.host.HostService;
13import org.onlab.onos.net.packet.InboundPacket;
alshabib030111e2014-09-15 15:56:42 -070014import org.onlab.onos.net.packet.PacketContext;
15import org.onlab.onos.net.packet.PacketProcessor;
16import org.onlab.onos.net.topology.TopologyService;
alshabib8aef1ad2014-09-15 17:47:31 -070017import org.onlab.packet.VLANID;
alshabib030111e2014-09-15 15:56:42 -070018import org.slf4j.Logger;
19
20public class ReactivePacketProcessor implements PacketProcessor {
21
22 private final Logger log = getLogger(getClass());
23 private final TopologyService topologyService;
alshabib8aef1ad2014-09-15 17:47:31 -070024 private final HostService hostService;
alshabib030111e2014-09-15 15:56:42 -070025
26
alshabib8aef1ad2014-09-15 17:47:31 -070027 public ReactivePacketProcessor(TopologyService topologyService, HostService hostService) {
alshabib030111e2014-09-15 15:56:42 -070028 this.topologyService = topologyService;
alshabib8aef1ad2014-09-15 17:47:31 -070029 this.hostService = hostService;
alshabib030111e2014-09-15 15:56:42 -070030 }
31
32
33 @Override
34 public void process(PacketContext context) {
alshabib8aef1ad2014-09-15 17:47:31 -070035 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) {
alshabib030111e2014-09-15 15:56:42 -070064 boolean canBcast = topologyService.isBroadcastPoint(topologyService.currentTopology(),
65 context.inPacket().receivedFrom());
alshabib030111e2014-09-15 15:56:42 -070066 if (canBcast) {
67 context.treatmentBuilder().add(Instructions.createOutput(PortNumber.FLOOD));
68 context.send();
69 } else {
70 context.block();
71 }
alshabib030111e2014-09-15 15:56:42 -070072 }
73
74}