alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 1 | package org.onlab.onos.fwd; |
| 2 | |
| 3 | import org.apache.felix.scr.annotations.Activate; |
| 4 | import org.apache.felix.scr.annotations.Component; |
| 5 | import org.apache.felix.scr.annotations.Deactivate; |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 6 | import org.apache.felix.scr.annotations.Modified; |
| 7 | import org.apache.felix.scr.annotations.Property; |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 8 | import org.apache.felix.scr.annotations.Reference; |
| 9 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 10 | import org.onlab.onos.ApplicationId; |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 11 | import org.onlab.onos.CoreService; |
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; |
alshabib | 8aef1ad | 2014-09-15 17:47:31 -0700 | [diff] [blame] | 23 | import org.onlab.onos.net.host.HostService; |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 24 | import org.onlab.onos.net.packet.InboundPacket; |
| 25 | import org.onlab.onos.net.packet.PacketContext; |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 26 | import org.onlab.onos.net.packet.PacketProcessor; |
| 27 | import org.onlab.onos.net.packet.PacketService; |
| 28 | import org.onlab.onos.net.topology.TopologyService; |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 29 | import org.onlab.packet.Ethernet; |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 30 | import org.osgi.service.component.ComponentContext; |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 31 | import org.slf4j.Logger; |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 32 | |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 33 | import java.util.Dictionary; |
| 34 | import java.util.Set; |
| 35 | |
| 36 | import static org.slf4j.LoggerFactory.getLogger; |
| 37 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Sample reactive forwarding application. |
| 40 | */ |
| 41 | @Component(immediate = true) |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 42 | public class ReactiveForwarding { |
| 43 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 44 | private static final int TIMEOUT = 10; |
alshabib | a0e0498 | 2014-10-03 13:03:19 -0700 | [diff] [blame] | 45 | private static final int PRIORITY = 10; |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 46 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 47 | private final Logger log = getLogger(getClass()); |
| 48 | |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 49 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 50 | protected TopologyService topologyService; |
| 51 | |
| 52 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 53 | protected PacketService packetService; |
| 54 | |
alshabib | 8aef1ad | 2014-09-15 17:47:31 -0700 | [diff] [blame] | 55 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 56 | protected HostService hostService; |
| 57 | |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 59 | protected FlowRuleService flowRuleService; |
| 60 | |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 62 | protected CoreService coreService; |
| 63 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 64 | private ReactivePacketProcessor processor = new ReactivePacketProcessor(); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 65 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 66 | private ApplicationId appId; |
| 67 | |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 68 | @Property(name = "enabled", boolValue = true, label = "Forwarding enabled") |
| 69 | private boolean isEnabled = true; |
| 70 | |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 71 | @Activate |
| 72 | public void activate() { |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 73 | appId = coreService.registerApplication("org.onlab.onos.fwd"); |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 74 | packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 2); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 75 | log.info("Started with Application ID {}", appId.id()); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Deactivate |
| 79 | public void deactivate() { |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 80 | flowRuleService.removeFlowRulesById(appId); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 81 | packetService.removeProcessor(processor); |
| 82 | processor = null; |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 83 | log.info("Stopped"); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 84 | } |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 85 | |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 86 | @Modified |
| 87 | public void modified(ComponentContext context) { |
| 88 | Dictionary properties = context.getProperties(); |
| 89 | String flag = (String) properties.get("enabled"); |
| 90 | if (flag != null) { |
| 91 | boolean enabled = flag.equals("true"); |
| 92 | if (isEnabled != enabled) { |
| 93 | isEnabled = enabled; |
| 94 | if (!isEnabled) { |
| 95 | flowRuleService.removeFlowRulesById(appId); |
| 96 | } |
| 97 | log.info("Reconfigured enabled = {}", isEnabled); |
| 98 | } |
| 99 | } |
| 100 | } |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 101 | |
| 102 | /** |
| 103 | * Packet processor responsible for forwarding packets along their paths. |
| 104 | */ |
| 105 | private class ReactivePacketProcessor implements PacketProcessor { |
| 106 | |
| 107 | @Override |
| 108 | public void process(PacketContext context) { |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 109 | // Stop processing if the packet has been handled, since we |
| 110 | // can't do any more to it. |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 111 | if (!isEnabled || context.isHandled()) { |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 112 | return; |
| 113 | } |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 114 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 115 | InboundPacket pkt = context.inPacket(); |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 116 | Ethernet ethPkt = pkt.parsed(); |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 117 | |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 118 | HostId id = HostId.hostId(ethPkt.getDestinationMAC()); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 119 | |
| 120 | // Do we know who this is for? If not, flood and bail. |
| 121 | Host dst = hostService.getHost(id); |
| 122 | if (dst == null) { |
| 123 | flood(context); |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | // Are we on an edge switch that our destination is on? If so, |
| 128 | // simply forward out to the destination and bail. |
| 129 | if (pkt.receivedFrom().deviceId().equals(dst.location().deviceId())) { |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 130 | if (!context.inPacket().receivedFrom().port().equals(dst.location().port())) { |
| 131 | installRule(context, dst.location().port()); |
| 132 | } |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 133 | return; |
| 134 | } |
| 135 | |
| 136 | // Otherwise, get a set of paths that lead from here to the |
| 137 | // destination edge switch. |
| 138 | Set<Path> paths = topologyService.getPaths(topologyService.currentTopology(), |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 139 | pkt.receivedFrom().deviceId(), |
| 140 | dst.location().deviceId()); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 141 | if (paths.isEmpty()) { |
| 142 | // If there are no paths, flood and bail. |
| 143 | flood(context); |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | // Otherwise, pick a path that does not lead back to where we |
| 148 | // came from; if no such path, flood and bail. |
| 149 | Path path = pickForwardPath(paths, pkt.receivedFrom().port()); |
| 150 | if (path == null) { |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 151 | log.warn("Doh... don't know where to go... {} -> {} received on {}", |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 152 | ethPkt.getSourceMAC(), ethPkt.getDestinationMAC(), |
| 153 | pkt.receivedFrom()); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 154 | flood(context); |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | // Otherwise forward and be done with it. |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 159 | installRule(context, path.src().port()); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| 163 | // Selects a path from the given set that does not lead back to the |
| 164 | // specified port. |
| 165 | private Path pickForwardPath(Set<Path> paths, PortNumber notToPort) { |
| 166 | for (Path path : paths) { |
| 167 | if (!path.src().port().equals(notToPort)) { |
| 168 | return path; |
| 169 | } |
| 170 | } |
| 171 | return null; |
| 172 | } |
| 173 | |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 174 | // Floods the specified packet if permissible. |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 175 | private void flood(PacketContext context) { |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 176 | if (topologyService.isBroadcastPoint(topologyService.currentTopology(), |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 177 | context.inPacket().receivedFrom())) { |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 178 | packetOut(context, PortNumber.FLOOD); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 179 | } else { |
| 180 | context.block(); |
| 181 | } |
| 182 | } |
| 183 | |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 184 | // Sends a packet out the specified port. |
| 185 | private void packetOut(PacketContext context, PortNumber portNumber) { |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 186 | context.treatmentBuilder().setOutput(portNumber); |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 187 | context.send(); |
| 188 | } |
| 189 | |
| 190 | // Install a rule forwarding the packet to the specified port. |
| 191 | private void installRule(PacketContext context, PortNumber portNumber) { |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 192 | // We don't yet support bufferids in the flowservice so packet out first. |
| 193 | packetOut(context, portNumber); |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 194 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 195 | // Install the flow rule to handle this type of message from now on. |
| 196 | Ethernet inPkt = context.inPacket().parsed(); |
| 197 | TrafficSelector.Builder builder = DefaultTrafficSelector.builder(); |
| 198 | builder.matchEthType(inPkt.getEtherType()) |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 199 | .matchEthSrc(inPkt.getSourceMAC()) |
| 200 | .matchEthDst(inPkt.getDestinationMAC()) |
| 201 | .matchInport(context.inPacket().receivedFrom().port()); |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 202 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 203 | TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder(); |
| 204 | treat.setOutput(portNumber); |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 205 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 206 | FlowRule f = new DefaultFlowRule(context.inPacket().receivedFrom().deviceId(), |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 207 | builder.build(), treat.build(), PRIORITY, appId, TIMEOUT); |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 208 | |
| 209 | flowRuleService.applyFlowRules(f); |
| 210 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 211 | } |
| 212 | |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 213 | } |
| 214 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 215 | |