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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.fwd; |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 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; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 30 | import org.onlab.packet.Ethernet; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 31 | import org.onosproject.core.ApplicationId; |
| 32 | import org.onosproject.core.CoreService; |
| 33 | import org.onosproject.net.Host; |
| 34 | import org.onosproject.net.HostId; |
| 35 | import org.onosproject.net.Path; |
| 36 | import org.onosproject.net.PortNumber; |
| 37 | import org.onosproject.net.flow.DefaultFlowRule; |
| 38 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 39 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 40 | import org.onosproject.net.flow.FlowRule; |
| 41 | import org.onosproject.net.flow.FlowRuleService; |
| 42 | import org.onosproject.net.flow.TrafficSelector; |
| 43 | import org.onosproject.net.flow.TrafficTreatment; |
| 44 | import org.onosproject.net.host.HostService; |
| 45 | import org.onosproject.net.packet.InboundPacket; |
| 46 | import org.onosproject.net.packet.PacketContext; |
| 47 | import org.onosproject.net.packet.PacketProcessor; |
| 48 | import org.onosproject.net.packet.PacketService; |
| 49 | import org.onosproject.net.topology.TopologyService; |
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 | bd7f4b3 | 2014-12-04 20:54:55 -0800 | [diff] [blame] | 83 | @Property(name = "packetOutOnly", boolValue = false, |
| 84 | label = "Enable packet-out only forwarding; default is false") |
| 85 | private boolean packetOutOnly = false; |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 86 | |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 87 | @Property(name = "ipv6Forwarding", boolValue = false, |
| 88 | label = "Enable IPv6 forwarding; default is false") |
| 89 | private boolean ipv6Forwarding = false; |
| 90 | |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 91 | @Activate |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 92 | public void activate(ComponentContext context) { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 93 | appId = coreService.registerApplication("org.onosproject.fwd"); |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 94 | packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 2); |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 95 | readComponentConfiguration(context); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 96 | log.info("Started with Application ID {}", appId.id()); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Deactivate |
| 100 | public void deactivate() { |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 101 | flowRuleService.removeFlowRulesById(appId); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 102 | packetService.removeProcessor(processor); |
| 103 | processor = null; |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 104 | log.info("Stopped"); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 105 | } |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 106 | |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 107 | @Modified |
| 108 | public void modified(ComponentContext context) { |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 109 | readComponentConfiguration(context); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Extracts properties from the component configuration context. |
| 114 | * |
| 115 | * @param context the component context |
| 116 | */ |
| 117 | private void readComponentConfiguration(ComponentContext context) { |
| 118 | Dictionary<?, ?> properties = context.getProperties(); |
| 119 | boolean packetOutOnlyEnabled = isPropertyEnabled(properties, "packetOutOnly"); |
| 120 | if (packetOutOnly != packetOutOnlyEnabled) { |
| 121 | packetOutOnly = packetOutOnlyEnabled; |
| 122 | log.info("Configured. Packet-out only forwarding is {}", |
| 123 | packetOutOnly ? "enabled" : "disabled"); |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 124 | } |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 125 | boolean ipv6ForwardingEnabled = isPropertyEnabled(properties, "ipv6Forwarding"); |
| 126 | if (ipv6Forwarding != ipv6ForwardingEnabled) { |
| 127 | ipv6Forwarding = ipv6ForwardingEnabled; |
| 128 | log.info("Configured. IPv6 forwarding is {}", |
| 129 | ipv6Forwarding ? "enabled" : "disabled"); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Check property name is defined and set to true. |
| 135 | * |
| 136 | * @param properties properties to be looked up |
| 137 | * @param propertyName the name of the property to look up |
| 138 | * @return true when the propertyName is defined and set to true |
| 139 | */ |
| 140 | private static boolean isPropertyEnabled(Dictionary<?, ?> properties, String propertyName) { |
| 141 | boolean enabled = false; |
| 142 | try { |
| 143 | String flag = (String) properties.get(propertyName); |
| 144 | if (flag != null) { |
| 145 | enabled = flag.equals("true"); |
| 146 | } |
| 147 | } catch (ClassCastException e) { |
| 148 | // No propertyName defined. |
| 149 | enabled = false; |
| 150 | } |
| 151 | return enabled; |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 152 | } |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 153 | |
| 154 | /** |
| 155 | * Packet processor responsible for forwarding packets along their paths. |
| 156 | */ |
| 157 | private class ReactivePacketProcessor implements PacketProcessor { |
| 158 | |
| 159 | @Override |
| 160 | public void process(PacketContext context) { |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 161 | // Stop processing if the packet has been handled, since we |
| 162 | // can't do any more to it. |
Thomas Vachuska | bd7f4b3 | 2014-12-04 20:54:55 -0800 | [diff] [blame] | 163 | if (context.isHandled()) { |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 164 | return; |
| 165 | } |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 166 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 167 | InboundPacket pkt = context.inPacket(); |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 168 | Ethernet ethPkt = pkt.parsed(); |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 169 | |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 170 | if (ethPkt == null) { |
| 171 | return; |
| 172 | } |
| 173 | |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 174 | // Bail if this is deemed to be a control packet. |
| 175 | if (isControlPacket(ethPkt)) { |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | // Skip IPv6 multicast packet when IPv6 forward is disabled. |
| 180 | if (!ipv6Forwarding && isIpv6Multicast(ethPkt)) { |
Thomas Vachuska | 01a6ec0 | 2014-11-05 09:54:09 -0800 | [diff] [blame] | 181 | return; |
| 182 | } |
| 183 | |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 184 | HostId id = HostId.hostId(ethPkt.getDestinationMAC()); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 185 | |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 186 | // Do not process link-local addresses in any way. |
| 187 | if (id.mac().isLinkLocal()) { |
| 188 | return; |
| 189 | } |
| 190 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 191 | // Do we know who this is for? If not, flood and bail. |
| 192 | Host dst = hostService.getHost(id); |
| 193 | if (dst == null) { |
| 194 | flood(context); |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | // Are we on an edge switch that our destination is on? If so, |
| 199 | // simply forward out to the destination and bail. |
| 200 | if (pkt.receivedFrom().deviceId().equals(dst.location().deviceId())) { |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 201 | if (!context.inPacket().receivedFrom().port().equals(dst.location().port())) { |
| 202 | installRule(context, dst.location().port()); |
| 203 | } |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 204 | return; |
| 205 | } |
| 206 | |
| 207 | // Otherwise, get a set of paths that lead from here to the |
| 208 | // destination edge switch. |
| 209 | Set<Path> paths = topologyService.getPaths(topologyService.currentTopology(), |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 210 | pkt.receivedFrom().deviceId(), |
| 211 | dst.location().deviceId()); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 212 | if (paths.isEmpty()) { |
| 213 | // If there are no paths, flood and bail. |
| 214 | flood(context); |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | // Otherwise, pick a path that does not lead back to where we |
| 219 | // came from; if no such path, flood and bail. |
| 220 | Path path = pickForwardPath(paths, pkt.receivedFrom().port()); |
| 221 | if (path == null) { |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 222 | log.warn("Doh... don't know where to go... {} -> {} received on {}", |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 223 | ethPkt.getSourceMAC(), ethPkt.getDestinationMAC(), |
| 224 | pkt.receivedFrom()); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 225 | flood(context); |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | // Otherwise forward and be done with it. |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 230 | installRule(context, path.src().port()); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 231 | } |
Thomas Vachuska | 01a6ec0 | 2014-11-05 09:54:09 -0800 | [diff] [blame] | 232 | |
| 233 | } |
| 234 | |
| 235 | // Indicates whether this is a control packet, e.g. LLDP, BDDP |
| 236 | private boolean isControlPacket(Ethernet eth) { |
| 237 | short type = eth.getEtherType(); |
| 238 | return type == Ethernet.TYPE_LLDP || type == Ethernet.TYPE_BSN; |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Thomas Vachuska | 5dd52f7 | 2014-11-28 19:27:45 -0800 | [diff] [blame] | 241 | // Indicated whether this is an IPv6 multicast packet. |
| 242 | private boolean isIpv6Multicast(Ethernet eth) { |
| 243 | return eth.getEtherType() == Ethernet.TYPE_IPV6 && eth.isMulticast(); |
| 244 | } |
| 245 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 246 | // Selects a path from the given set that does not lead back to the |
| 247 | // specified port. |
| 248 | private Path pickForwardPath(Set<Path> paths, PortNumber notToPort) { |
| 249 | for (Path path : paths) { |
| 250 | if (!path.src().port().equals(notToPort)) { |
| 251 | return path; |
| 252 | } |
| 253 | } |
| 254 | return null; |
| 255 | } |
| 256 | |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 257 | // Floods the specified packet if permissible. |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 258 | private void flood(PacketContext context) { |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 259 | if (topologyService.isBroadcastPoint(topologyService.currentTopology(), |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 260 | context.inPacket().receivedFrom())) { |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 261 | packetOut(context, PortNumber.FLOOD); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 262 | } else { |
| 263 | context.block(); |
| 264 | } |
| 265 | } |
| 266 | |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 267 | // Sends a packet out the specified port. |
| 268 | private void packetOut(PacketContext context, PortNumber portNumber) { |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 269 | context.treatmentBuilder().setOutput(portNumber); |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 270 | context.send(); |
| 271 | } |
| 272 | |
| 273 | // Install a rule forwarding the packet to the specified port. |
| 274 | private void installRule(PacketContext context, PortNumber portNumber) { |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 275 | // We don't yet support bufferids in the flowservice so packet out first. |
| 276 | packetOut(context, portNumber); |
Thomas Vachuska | bd7f4b3 | 2014-12-04 20:54:55 -0800 | [diff] [blame] | 277 | if (!packetOutOnly) { |
| 278 | // Install the flow rule to handle this type of message from now on. |
| 279 | Ethernet inPkt = context.inPacket().parsed(); |
| 280 | TrafficSelector.Builder builder = DefaultTrafficSelector.builder(); |
| 281 | builder.matchEthType(inPkt.getEtherType()) |
| 282 | .matchEthSrc(inPkt.getSourceMAC()) |
| 283 | .matchEthDst(inPkt.getDestinationMAC()) |
| 284 | .matchInport(context.inPacket().receivedFrom().port()); |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 285 | |
Thomas Vachuska | bd7f4b3 | 2014-12-04 20:54:55 -0800 | [diff] [blame] | 286 | TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder(); |
| 287 | treat.setOutput(portNumber); |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 288 | |
Thomas Vachuska | bd7f4b3 | 2014-12-04 20:54:55 -0800 | [diff] [blame] | 289 | FlowRule f = new DefaultFlowRule(context.inPacket().receivedFrom().deviceId(), |
| 290 | builder.build(), treat.build(), PRIORITY, appId, TIMEOUT, false); |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 291 | |
Thomas Vachuska | bd7f4b3 | 2014-12-04 20:54:55 -0800 | [diff] [blame] | 292 | flowRuleService.applyFlowRules(f); |
| 293 | } |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 294 | } |
| 295 | |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 296 | } |
| 297 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 298 | |