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