alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 1 | package org.onlab.onos.fwd; |
| 2 | |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 3 | import static org.slf4j.LoggerFactory.getLogger; |
| 4 | |
| 5 | import java.util.Set; |
| 6 | |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 7 | import org.apache.felix.scr.annotations.Activate; |
| 8 | import org.apache.felix.scr.annotations.Component; |
| 9 | import org.apache.felix.scr.annotations.Deactivate; |
| 10 | import org.apache.felix.scr.annotations.Reference; |
| 11 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 12 | import org.onlab.onos.net.Host; |
| 13 | import org.onlab.onos.net.HostId; |
| 14 | import org.onlab.onos.net.Path; |
| 15 | import org.onlab.onos.net.PortNumber; |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 16 | import org.onlab.onos.net.flow.DefaultFlowRule; |
| 17 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
| 18 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
| 19 | import org.onlab.onos.net.flow.FlowRule; |
| 20 | import org.onlab.onos.net.flow.FlowRuleService; |
| 21 | import org.onlab.onos.net.flow.TrafficSelector; |
| 22 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 23 | import org.onlab.onos.net.flow.criteria.Criteria; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 24 | import org.onlab.onos.net.flow.instructions.Instructions; |
alshabib | 8aef1ad | 2014-09-15 17:47:31 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.host.HostService; |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 26 | import org.onlab.onos.net.packet.InboundPacket; |
| 27 | import org.onlab.onos.net.packet.PacketContext; |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.packet.PacketProcessor; |
| 29 | import org.onlab.onos.net.packet.PacketService; |
| 30 | import org.onlab.onos.net.topology.TopologyService; |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 31 | import org.onlab.packet.Ethernet; |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 32 | import org.slf4j.Logger; |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 33 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 34 | /** |
| 35 | * Sample reactive forwarding application. |
| 36 | */ |
| 37 | @Component(immediate = true) |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 38 | public class ReactiveForwarding { |
| 39 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 40 | private final Logger log = getLogger(getClass()); |
| 41 | |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 42 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 43 | protected TopologyService topologyService; |
| 44 | |
| 45 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 46 | protected PacketService packetService; |
| 47 | |
alshabib | 8aef1ad | 2014-09-15 17:47:31 -0700 | [diff] [blame] | 48 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 49 | protected HostService hostService; |
| 50 | |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 51 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 52 | protected FlowRuleService flowRuleService; |
| 53 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 54 | private ReactivePacketProcessor processor = new ReactivePacketProcessor(); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 55 | |
| 56 | @Activate |
| 57 | public void activate() { |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 58 | packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 1); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 59 | log.info("Started"); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Deactivate |
| 63 | public void deactivate() { |
| 64 | packetService.removeProcessor(processor); |
| 65 | processor = null; |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 66 | log.info("Stopped"); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 67 | } |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 68 | |
| 69 | |
| 70 | /** |
| 71 | * Packet processor responsible for forwarding packets along their paths. |
| 72 | */ |
| 73 | private class ReactivePacketProcessor implements PacketProcessor { |
| 74 | |
| 75 | @Override |
| 76 | public void process(PacketContext context) { |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame^] | 77 | // Stop processing if the packet has been handled, since we |
| 78 | // can't do any more to it. |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 79 | if (context.isHandled()) { |
| 80 | return; |
| 81 | } |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame^] | 82 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 83 | InboundPacket pkt = context.inPacket(); |
| 84 | HostId id = HostId.hostId(pkt.parsed().getDestinationMAC()); |
| 85 | |
| 86 | // Do we know who this is for? If not, flood and bail. |
| 87 | Host dst = hostService.getHost(id); |
| 88 | if (dst == null) { |
| 89 | flood(context); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | // Are we on an edge switch that our destination is on? If so, |
| 94 | // simply forward out to the destination and bail. |
| 95 | if (pkt.receivedFrom().deviceId().equals(dst.location().deviceId())) { |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 96 | installRule(context, dst.location().port()); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 97 | return; |
| 98 | } |
| 99 | |
| 100 | // Otherwise, get a set of paths that lead from here to the |
| 101 | // destination edge switch. |
| 102 | Set<Path> paths = topologyService.getPaths(topologyService.currentTopology(), |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame^] | 103 | context.inPacket().receivedFrom().deviceId(), |
| 104 | dst.location().deviceId()); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 105 | if (paths.isEmpty()) { |
| 106 | // If there are no paths, flood and bail. |
| 107 | flood(context); |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | // Otherwise, pick a path that does not lead back to where we |
| 112 | // came from; if no such path, flood and bail. |
| 113 | Path path = pickForwardPath(paths, pkt.receivedFrom().port()); |
| 114 | if (path == null) { |
| 115 | log.warn("Doh... don't know where to go..."); |
| 116 | flood(context); |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | // Otherwise forward and be done with it. |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 121 | installRule(context, path.src().port()); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | // Selects a path from the given set that does not lead back to the |
| 126 | // specified port. |
| 127 | private Path pickForwardPath(Set<Path> paths, PortNumber notToPort) { |
| 128 | for (Path path : paths) { |
| 129 | if (!path.src().port().equals(notToPort)) { |
| 130 | return path; |
| 131 | } |
| 132 | } |
| 133 | return null; |
| 134 | } |
| 135 | |
| 136 | // Floods the specified packet. |
| 137 | private void flood(PacketContext context) { |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame^] | 138 | if (topologyService.isBroadcastPoint(topologyService.currentTopology(), |
| 139 | context.inPacket().receivedFrom())) { |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 140 | packetOutFlood(context); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 141 | } else { |
| 142 | context.block(); |
| 143 | } |
| 144 | } |
| 145 | |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 146 | //Floods a packet out |
| 147 | private void packetOutFlood(PacketContext context) { |
| 148 | context.treatmentBuilder().add(Instructions.createOutput(PortNumber.FLOOD)); |
| 149 | context.send(); |
| 150 | } |
| 151 | |
| 152 | // Install a rule forwarding the packet to the specified port. |
| 153 | private void installRule(PacketContext context, PortNumber portNumber) { |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 154 | Ethernet inPkt = context.inPacket().parsed(); |
| 155 | TrafficSelector.Builder builder = new DefaultTrafficSelector.Builder(); |
| 156 | builder.add(Criteria.matchEthType(inPkt.getEtherType())) |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame^] | 157 | .add(Criteria.matchEthSrc(inPkt.getSourceMAC())) |
| 158 | .add(Criteria.matchEthDst(inPkt.getDestinationMAC())) |
| 159 | .add(Criteria.matchInPort(context.inPacket().receivedFrom().port())); |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 160 | |
| 161 | TrafficTreatment.Builder treat = new DefaultTrafficTreatment.Builder(); |
| 162 | treat.add(Instructions.createOutput(portNumber)); |
| 163 | |
| 164 | FlowRule f = new DefaultFlowRule(context.inPacket().receivedFrom().deviceId(), |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame^] | 165 | builder.build(), treat.build()); |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 166 | |
| 167 | flowRuleService.applyFlowRules(f); |
| 168 | |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame^] | 169 | // we don't yet support bufferids in the flowservice so packet out and |
| 170 | // then install a flowmod. |
| 171 | context.treatmentBuilder().add(Instructions.createOutput(portNumber)); |
| 172 | context.send(); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 173 | } |
| 174 | |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 175 | } |
| 176 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 177 | |