| /* |
| * Copyright 2014 Open Networking Laboratory |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| package org.onosproject.fwd; |
| |
| import static org.slf4j.LoggerFactory.getLogger; |
| |
| import java.util.Dictionary; |
| 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.Modified; |
| import org.apache.felix.scr.annotations.Property; |
| import org.apache.felix.scr.annotations.Reference; |
| import org.apache.felix.scr.annotations.ReferenceCardinality; |
| import org.onlab.packet.Ethernet; |
| import org.onosproject.core.ApplicationId; |
| import org.onosproject.core.CoreService; |
| import org.onosproject.net.Device; |
| import org.onosproject.net.Host; |
| import org.onosproject.net.HostId; |
| import org.onosproject.net.Path; |
| import org.onosproject.net.PortNumber; |
| import org.onosproject.net.device.DeviceEvent; |
| import org.onosproject.net.device.DeviceListener; |
| import org.onosproject.net.device.DeviceService; |
| import org.onosproject.net.flow.DefaultFlowRule; |
| import org.onosproject.net.flow.DefaultTrafficSelector; |
| import org.onosproject.net.flow.DefaultTrafficTreatment; |
| import org.onosproject.net.flow.FlowRule; |
| import org.onosproject.net.flow.FlowRuleService; |
| import org.onosproject.net.flow.TrafficSelector; |
| import org.onosproject.net.flow.TrafficTreatment; |
| import org.onosproject.net.host.HostService; |
| import org.onosproject.net.packet.InboundPacket; |
| import org.onosproject.net.packet.PacketContext; |
| import org.onosproject.net.packet.PacketProcessor; |
| import org.onosproject.net.packet.PacketService; |
| import org.onosproject.net.topology.TopologyService; |
| import org.osgi.service.component.ComponentContext; |
| import org.slf4j.Logger; |
| |
| /** |
| * Sample reactive forwarding application. |
| */ |
| @Component(immediate = true) |
| public class ReactiveForwarding { |
| |
| private static final int TIMEOUT = 10; |
| private static final int PRIORITY = 10; |
| private static final int PUNT_RULE_PRIORITY = 5; |
| |
| 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 DeviceService deviceService; |
| |
| @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| protected CoreService coreService; |
| |
| private ReactivePacketProcessor processor = new ReactivePacketProcessor(); |
| |
| private ApplicationId appId; |
| |
| @Property(name = "packetOutOnly", boolValue = false, |
| label = "Enable packet-out only forwarding; default is false") |
| private boolean packetOutOnly = false; |
| |
| @Property(name = "ipv6Forwarding", boolValue = false, |
| label = "Enable IPv6 forwarding; default is false") |
| private boolean ipv6Forwarding = false; |
| |
| @Activate |
| public void activate(ComponentContext context) { |
| appId = coreService.registerApplication("org.onosproject.fwd"); |
| deviceService.addListener(new InternalDeviceListener()); |
| pushRules(); |
| packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 2); |
| readComponentConfiguration(context); |
| log.info("Started with Application ID {}", appId.id()); |
| } |
| |
| @Deactivate |
| public void deactivate() { |
| flowRuleService.removeFlowRulesById(appId); |
| packetService.removeProcessor(processor); |
| processor = null; |
| log.info("Stopped"); |
| } |
| |
| @Modified |
| public void modified(ComponentContext context) { |
| readComponentConfiguration(context); |
| } |
| |
| /** |
| * Extracts properties from the component configuration context. |
| * |
| * @param context the component context |
| */ |
| private void readComponentConfiguration(ComponentContext context) { |
| Dictionary<?, ?> properties = context.getProperties(); |
| boolean packetOutOnlyEnabled = isPropertyEnabled(properties, "packetOutOnly"); |
| if (packetOutOnly != packetOutOnlyEnabled) { |
| packetOutOnly = packetOutOnlyEnabled; |
| log.info("Configured. Packet-out only forwarding is {}", |
| packetOutOnly ? "enabled" : "disabled"); |
| } |
| boolean ipv6ForwardingEnabled = isPropertyEnabled(properties, "ipv6Forwarding"); |
| if (ipv6Forwarding != ipv6ForwardingEnabled) { |
| ipv6Forwarding = ipv6ForwardingEnabled; |
| log.info("Configured. IPv6 forwarding is {}", |
| ipv6Forwarding ? "enabled" : "disabled"); |
| } |
| } |
| |
| /** |
| * Check property name is defined and set to true. |
| * |
| * @param properties properties to be looked up |
| * @param propertyName the name of the property to look up |
| * @return true when the propertyName is defined and set to true |
| */ |
| private static boolean isPropertyEnabled(Dictionary<?, ?> properties, String propertyName) { |
| boolean enabled = false; |
| try { |
| String flag = (String) properties.get(propertyName); |
| if (flag != null) { |
| enabled = flag.equals("true"); |
| } |
| } catch (ClassCastException e) { |
| // No propertyName defined. |
| enabled = false; |
| } |
| return enabled; |
| } |
| |
| /** |
| * 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 == null) { |
| return; |
| } |
| |
| // Bail if this is deemed to be a control packet. |
| if (isControlPacket(ethPkt)) { |
| return; |
| } |
| |
| // Skip IPv6 multicast packet when IPv6 forward is disabled. |
| if (!ipv6Forwarding && isIpv6Multicast(ethPkt)) { |
| return; |
| } |
| |
| HostId id = HostId.hostId(ethPkt.getDestinationMAC()); |
| |
| // Do not process link-local addresses in any way. |
| if (id.mac().isLinkLocal()) { |
| return; |
| } |
| |
| // 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()); |
| } |
| |
| } |
| |
| // Indicates whether this is a control packet, e.g. LLDP, BDDP |
| private boolean isControlPacket(Ethernet eth) { |
| short type = eth.getEtherType(); |
| return type == Ethernet.TYPE_LLDP || type == Ethernet.TYPE_BSN; |
| } |
| |
| // Indicated whether this is an IPv6 multicast packet. |
| private boolean isIpv6Multicast(Ethernet eth) { |
| return eth.getEtherType() == Ethernet.TYPE_IPV6 && eth.isMulticast(); |
| } |
| |
| // 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 (!packetOutOnly) { |
| // Install the flow rule to handle this type of message from now on. |
| Ethernet inPkt = context.inPacket().parsed(); |
| TrafficSelector.Builder builder = DefaultTrafficSelector.builder(); |
| builder.matchEthType(inPkt.getEtherType()) |
| .matchEthSrc(inPkt.getSourceMAC()) |
| .matchEthDst(inPkt.getDestinationMAC()) |
| .matchInport(context.inPacket().receivedFrom().port()); |
| |
| TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder(); |
| treat.setOutput(portNumber); |
| |
| FlowRule f = new DefaultFlowRule(context.inPacket().receivedFrom().deviceId(), |
| builder.build(), treat.build(), PRIORITY, appId, TIMEOUT, false); |
| |
| flowRuleService.applyFlowRules(f); |
| } |
| } |
| |
| /** |
| * Pushes flow rules to all devices. |
| */ |
| private void pushRules() { |
| for (Device device : deviceService.getDevices()) { |
| pushRules(device); |
| } |
| } |
| |
| /** |
| * Pushes flow rules to the device to receive packets that need |
| * to be processed. |
| * |
| * @param device the device to push the rules to |
| */ |
| private synchronized void pushRules(Device device) { |
| TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| |
| // Get all IPv4 packets |
| sbuilder.matchEthType(Ethernet.TYPE_IPV4); |
| tbuilder.punt(); |
| FlowRule flowArp = |
| new DefaultFlowRule(device.id(), |
| sbuilder.build(), tbuilder.build(), |
| PUNT_RULE_PRIORITY, appId, 0, true); |
| |
| flowRuleService.applyFlowRules(flowArp); |
| } |
| |
| public class InternalDeviceListener implements DeviceListener { |
| |
| @Override |
| public void event(DeviceEvent event) { |
| Device device = event.subject(); |
| switch (event.type()) { |
| case DEVICE_ADDED: |
| pushRules(device); |
| break; |
| case DEVICE_AVAILABILITY_CHANGED: |
| case DEVICE_SUSPENDED: |
| case DEVICE_UPDATED: |
| case DEVICE_REMOVED: |
| case PORT_ADDED: |
| case PORT_UPDATED: |
| case PORT_REMOVED: |
| default: |
| break; |
| } |
| } |
| } |
| |
| } |
| |
| |