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) { |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame^] | 77 | if (context.isHandled()) { |
| 78 | return; |
| 79 | } |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 80 | InboundPacket pkt = context.inPacket(); |
| 81 | HostId id = HostId.hostId(pkt.parsed().getDestinationMAC()); |
| 82 | |
| 83 | // Do we know who this is for? If not, flood and bail. |
| 84 | Host dst = hostService.getHost(id); |
| 85 | if (dst == null) { |
| 86 | flood(context); |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | // Are we on an edge switch that our destination is on? If so, |
| 91 | // simply forward out to the destination and bail. |
| 92 | if (pkt.receivedFrom().deviceId().equals(dst.location().deviceId())) { |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame^] | 93 | installRule(context, dst.location().port()); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 94 | return; |
| 95 | } |
| 96 | |
| 97 | // Otherwise, get a set of paths that lead from here to the |
| 98 | // destination edge switch. |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame^] | 99 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 100 | Set<Path> paths = topologyService.getPaths(topologyService.currentTopology(), |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame^] | 101 | context.inPacket().receivedFrom().deviceId(), |
| 102 | dst.location().deviceId()); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 103 | if (paths.isEmpty()) { |
| 104 | // If there are no paths, flood and bail. |
| 105 | flood(context); |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | // Otherwise, pick a path that does not lead back to where we |
| 110 | // came from; if no such path, flood and bail. |
| 111 | Path path = pickForwardPath(paths, pkt.receivedFrom().port()); |
| 112 | if (path == null) { |
| 113 | log.warn("Doh... don't know where to go..."); |
| 114 | flood(context); |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | // Otherwise forward and be done with it. |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame^] | 119 | installRule(context, path.src().port()); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
| 123 | // Selects a path from the given set that does not lead back to the |
| 124 | // specified port. |
| 125 | private Path pickForwardPath(Set<Path> paths, PortNumber notToPort) { |
| 126 | for (Path path : paths) { |
| 127 | if (!path.src().port().equals(notToPort)) { |
| 128 | return path; |
| 129 | } |
| 130 | } |
| 131 | return null; |
| 132 | } |
| 133 | |
| 134 | // Floods the specified packet. |
| 135 | private void flood(PacketContext context) { |
| 136 | boolean canBcast = topologyService.isBroadcastPoint(topologyService.currentTopology(), |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame^] | 137 | context.inPacket().receivedFrom()); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 138 | if (canBcast) { |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame^] | 139 | packetOutFlood(context); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 140 | } else { |
| 141 | context.block(); |
| 142 | } |
| 143 | } |
| 144 | |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame^] | 145 | //Floods a packet out |
| 146 | private void packetOutFlood(PacketContext context) { |
| 147 | context.treatmentBuilder().add(Instructions.createOutput(PortNumber.FLOOD)); |
| 148 | context.send(); |
| 149 | } |
| 150 | |
| 151 | // Install a rule forwarding the packet to the specified port. |
| 152 | private void installRule(PacketContext context, PortNumber portNumber) { |
| 153 | // we don't yet support bufferids in the flowservice so packet out and |
| 154 | // then install a flowmod. |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 155 | context.treatmentBuilder().add(Instructions.createOutput(portNumber)); |
| 156 | context.send(); |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame^] | 157 | |
| 158 | |
| 159 | Ethernet inPkt = context.inPacket().parsed(); |
| 160 | TrafficSelector.Builder builder = new DefaultTrafficSelector.Builder(); |
| 161 | builder.add(Criteria.matchEthType(inPkt.getEtherType())) |
| 162 | .add(Criteria.matchEthSrc(inPkt.getSourceMAC())) |
| 163 | .add(Criteria.matchEthDst(inPkt.getDestinationMAC())) |
| 164 | .add(Criteria.matchInPort(context.inPacket().receivedFrom().port())); |
| 165 | |
| 166 | TrafficTreatment.Builder treat = new DefaultTrafficTreatment.Builder(); |
| 167 | treat.add(Instructions.createOutput(portNumber)); |
| 168 | |
| 169 | FlowRule f = new DefaultFlowRule(context.inPacket().receivedFrom().deviceId(), |
| 170 | builder.build(), treat.build()); |
| 171 | |
| 172 | flowRuleService.applyFlowRules(f); |
| 173 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 174 | } |
| 175 | |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 176 | } |
| 177 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 178 | |