| package org.onlab.onos.fwd; |
| |
| import static org.slf4j.LoggerFactory.getLogger; |
| |
| import java.util.Set; |
| |
| import org.apache.felix.scr.annotations.Activate; |
| import org.apache.felix.scr.annotations.Component; |
| import org.apache.felix.scr.annotations.Deactivate; |
| import org.apache.felix.scr.annotations.Reference; |
| import org.apache.felix.scr.annotations.ReferenceCardinality; |
| import org.onlab.onos.ApplicationId; |
| import org.onlab.onos.net.Host; |
| import org.onlab.onos.net.HostId; |
| import org.onlab.onos.net.Path; |
| import org.onlab.onos.net.PortNumber; |
| import org.onlab.onos.net.flow.DefaultFlowRule; |
| import org.onlab.onos.net.flow.DefaultTrafficSelector; |
| import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
| import org.onlab.onos.net.flow.FlowRule; |
| import org.onlab.onos.net.flow.FlowRuleService; |
| import org.onlab.onos.net.flow.TrafficSelector; |
| import org.onlab.onos.net.flow.TrafficTreatment; |
| import org.onlab.onos.net.host.HostService; |
| import org.onlab.onos.net.packet.InboundPacket; |
| import org.onlab.onos.net.packet.PacketContext; |
| import org.onlab.onos.net.packet.PacketProcessor; |
| import org.onlab.onos.net.packet.PacketService; |
| import org.onlab.onos.net.proxyarp.ProxyArpService; |
| import org.onlab.onos.net.topology.TopologyService; |
| import org.onlab.packet.ARP; |
| import org.onlab.packet.Ethernet; |
| import org.slf4j.Logger; |
| |
| /** |
| * Sample reactive forwarding application. |
| */ |
| @Component(immediate = true) |
| public class ReactiveForwarding { |
| |
| private static final int TIMEOUT = 10; |
| |
| private final Logger log = getLogger(getClass()); |
| |
| @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| protected TopologyService topologyService; |
| |
| @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| protected PacketService packetService; |
| |
| @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| protected HostService hostService; |
| |
| @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| protected FlowRuleService flowRuleService; |
| |
| @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| protected ProxyArpService proxyArpService; |
| |
| private ReactivePacketProcessor processor = new ReactivePacketProcessor(); |
| |
| private ApplicationId appId; |
| |
| @Activate |
| public void activate() { |
| appId = ApplicationId.getAppId(); |
| packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 1); |
| log.info("Started with Application ID {}", appId.id()); |
| } |
| |
| @Deactivate |
| public void deactivate() { |
| flowRuleService.removeFlowRulesById(appId); |
| packetService.removeProcessor(processor); |
| processor = null; |
| log.info("Stopped"); |
| } |
| |
| |
| /** |
| * Packet processor responsible for forwarding packets along their paths. |
| */ |
| private class ReactivePacketProcessor implements PacketProcessor { |
| |
| @Override |
| public void process(PacketContext context) { |
| // Stop processing if the packet has been handled, since we |
| // can't do any more to it. |
| if (context.isHandled()) { |
| return; |
| } |
| |
| InboundPacket pkt = context.inPacket(); |
| Ethernet ethPkt = pkt.parsed(); |
| if (ethPkt.getEtherType() == Ethernet.TYPE_ARP) { |
| ARP arp = (ARP) ethPkt.getPayload(); |
| if (arp.getOpCode() == ARP.OP_REPLY) { |
| proxyArpService.forward(ethPkt); |
| } else if (arp.getOpCode() == ARP.OP_REQUEST) { |
| proxyArpService.reply(ethPkt); |
| } |
| context.block(); |
| return; |
| } |
| |
| HostId id = HostId.hostId(ethPkt.getDestinationMAC()); |
| |
| // Do we know who this is for? If not, flood and bail. |
| Host dst = hostService.getHost(id); |
| if (dst == null) { |
| flood(context); |
| return; |
| } |
| |
| // Are we on an edge switch that our destination is on? If so, |
| // simply forward out to the destination and bail. |
| if (pkt.receivedFrom().deviceId().equals(dst.location().deviceId())) { |
| if (!context.inPacket().receivedFrom().port().equals(dst.location().port())) { |
| installRule(context, dst.location().port()); |
| } |
| return; |
| } |
| |
| // Otherwise, get a set of paths that lead from here to the |
| // destination edge switch. |
| Set<Path> paths = topologyService.getPaths(topologyService.currentTopology(), |
| pkt.receivedFrom().deviceId(), |
| dst.location().deviceId()); |
| if (paths.isEmpty()) { |
| // If there are no paths, flood and bail. |
| flood(context); |
| return; |
| } |
| |
| // Otherwise, pick a path that does not lead back to where we |
| // came from; if no such path, flood and bail. |
| Path path = pickForwardPath(paths, pkt.receivedFrom().port()); |
| if (path == null) { |
| log.warn("Doh... don't know where to go... {} -> {} received on {}", |
| ethPkt.getSourceMAC(), ethPkt.getDestinationMAC(), |
| pkt.receivedFrom()); |
| flood(context); |
| return; |
| } |
| |
| // Otherwise forward and be done with it. |
| installRule(context, path.src().port()); |
| } |
| } |
| |
| // Selects a path from the given set that does not lead back to the |
| // specified port. |
| private Path pickForwardPath(Set<Path> paths, PortNumber notToPort) { |
| for (Path path : paths) { |
| if (!path.src().port().equals(notToPort)) { |
| return path; |
| } |
| } |
| return null; |
| } |
| |
| // Floods the specified packet if permissible. |
| private void flood(PacketContext context) { |
| if (topologyService.isBroadcastPoint(topologyService.currentTopology(), |
| context.inPacket().receivedFrom())) { |
| packetOut(context, PortNumber.FLOOD); |
| } else { |
| context.block(); |
| } |
| } |
| |
| // Sends a packet out the specified port. |
| private void packetOut(PacketContext context, PortNumber portNumber) { |
| context.treatmentBuilder().setOutput(portNumber); |
| context.send(); |
| } |
| |
| // Install a rule forwarding the packet to the specified port. |
| private void installRule(PacketContext context, PortNumber portNumber) { |
| // We don't yet support bufferids in the flowservice so packet out first. |
| packetOut(context, portNumber); |
| |
| if (context.inPacket().parsed().getEtherType() == Ethernet.TYPE_IPV4) { |
| |
| // Install the flow rule to handle this type of message from now on. |
| Ethernet inPkt = context.inPacket().parsed(); |
| TrafficSelector.Builder builder = new DefaultTrafficSelector.Builder(); |
| builder.matchEthType(inPkt.getEtherType()) |
| .matchEthSrc(inPkt.getSourceMAC()) |
| .matchEthDst(inPkt.getDestinationMAC()) |
| .matchInport(context.inPacket().receivedFrom().port()); |
| |
| TrafficTreatment.Builder treat = new DefaultTrafficTreatment.Builder(); |
| treat.setOutput(portNumber); |
| |
| FlowRule f = new DefaultFlowRule(context.inPacket().receivedFrom().deviceId(), |
| builder.build(), treat.build(), 0, appId, TIMEOUT); |
| |
| flowRuleService.applyFlowRules(f); |
| } |
| } |
| |
| } |
| |
| |