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; |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 22 | import static com.google.common.base.Strings.isNullOrEmpty; |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 23 | |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Activate; |
| 25 | import org.apache.felix.scr.annotations.Component; |
| 26 | import org.apache.felix.scr.annotations.Deactivate; |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.Modified; |
| 28 | import org.apache.felix.scr.annotations.Property; |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 29 | import org.apache.felix.scr.annotations.Reference; |
| 30 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 31 | import org.onlab.packet.Ethernet; |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 32 | import org.onlab.packet.IPv4; |
| 33 | import org.onlab.packet.IPv6; |
| 34 | import org.onlab.packet.TCP; |
| 35 | import org.onlab.packet.UDP; |
| 36 | import org.onlab.packet.ICMP; |
| 37 | import org.onlab.packet.ICMP6; |
| 38 | import org.onlab.packet.Ip4Prefix; |
| 39 | import org.onlab.packet.Ip6Prefix; |
| 40 | import org.onlab.packet.VlanId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 41 | import org.onosproject.core.ApplicationId; |
| 42 | import org.onosproject.core.CoreService; |
| 43 | import org.onosproject.net.Host; |
| 44 | import org.onosproject.net.HostId; |
| 45 | import org.onosproject.net.Path; |
| 46 | import org.onosproject.net.PortNumber; |
| 47 | import org.onosproject.net.flow.DefaultFlowRule; |
| 48 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 49 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 50 | import org.onosproject.net.flow.FlowRule; |
| 51 | import org.onosproject.net.flow.FlowRuleService; |
| 52 | import org.onosproject.net.flow.TrafficSelector; |
| 53 | import org.onosproject.net.flow.TrafficTreatment; |
| 54 | import org.onosproject.net.host.HostService; |
| 55 | import org.onosproject.net.packet.InboundPacket; |
| 56 | import org.onosproject.net.packet.PacketContext; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 57 | import org.onosproject.net.packet.PacketPriority; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 58 | import org.onosproject.net.packet.PacketProcessor; |
| 59 | import org.onosproject.net.packet.PacketService; |
| 60 | import org.onosproject.net.topology.TopologyService; |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 61 | import org.osgi.service.component.ComponentContext; |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 62 | import org.slf4j.Logger; |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 63 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 64 | /** |
| 65 | * Sample reactive forwarding application. |
| 66 | */ |
| 67 | @Component(immediate = true) |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 68 | public class ReactiveForwarding { |
| 69 | |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 70 | private static final int DEFAULT_TIMEOUT = 10; |
| 71 | private static final int DEFAULT_PRIORITY = 10; |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 72 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 73 | private final Logger log = getLogger(getClass()); |
| 74 | |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 76 | protected TopologyService topologyService; |
| 77 | |
| 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 79 | protected PacketService packetService; |
| 80 | |
alshabib | 8aef1ad | 2014-09-15 17:47:31 -0700 | [diff] [blame] | 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 82 | protected HostService hostService; |
| 83 | |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 85 | protected FlowRuleService flowRuleService; |
| 86 | |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 87 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 88 | protected CoreService coreService; |
| 89 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 90 | private ReactivePacketProcessor processor = new ReactivePacketProcessor(); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 91 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 92 | private ApplicationId appId; |
| 93 | |
Thomas Vachuska | bd7f4b3 | 2014-12-04 20:54:55 -0800 | [diff] [blame] | 94 | @Property(name = "packetOutOnly", boolValue = false, |
| 95 | label = "Enable packet-out only forwarding; default is false") |
| 96 | private boolean packetOutOnly = false; |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 97 | |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 98 | @Property(name = "packetOutOfppTable", boolValue = false, |
| 99 | label = "Enable first packet forwarding using OFPP_TABLE port " + |
| 100 | "instead of PacketOut with actual port; default is false") |
| 101 | private boolean packetOutOfppTable = false; |
| 102 | |
| 103 | @Property(name = "flowTimeout", intValue = DEFAULT_TIMEOUT, |
| 104 | label = "Configure Flow Timeout for installed flow rules; " + |
| 105 | "default is 10 sec") |
| 106 | private int flowTimeout = DEFAULT_TIMEOUT; |
| 107 | |
| 108 | @Property(name = "flowPriority", intValue = DEFAULT_PRIORITY, |
| 109 | label = "Configure Flow Priority for installed flow rules; " + |
| 110 | "default is 10") |
| 111 | private int flowPriority = DEFAULT_PRIORITY; |
| 112 | |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 113 | @Property(name = "ipv6Forwarding", boolValue = false, |
| 114 | label = "Enable IPv6 forwarding; default is false") |
| 115 | private boolean ipv6Forwarding = false; |
| 116 | |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 117 | @Property(name = "matchDstMacOnly", boolValue = false, |
| 118 | label = "Enable matching Dst Mac Only; default is false") |
| 119 | private boolean matchDstMacOnly = false; |
| 120 | |
| 121 | @Property(name = "matchVlanId", boolValue = false, |
| 122 | label = "Enable matching Vlan ID; default is false") |
| 123 | private boolean matchVlanId = false; |
| 124 | |
| 125 | @Property(name = "matchIpv4Address", boolValue = false, |
| 126 | label = "Enable matching IPv4 Addresses; default is false") |
| 127 | private boolean matchIpv4Address = false; |
| 128 | |
| 129 | @Property(name = "matchIpv4Dscp", boolValue = false, |
| 130 | label = "Enable matching IPv4 DSCP and ECN; default is false") |
| 131 | private boolean matchIpv4Dscp = false; |
| 132 | |
| 133 | @Property(name = "matchIpv6Address", boolValue = false, |
| 134 | label = "Enable matching IPv6 Addresses; default is false") |
| 135 | private boolean matchIpv6Address = false; |
| 136 | |
| 137 | @Property(name = "matchIpv6FlowLabel", boolValue = false, |
| 138 | label = "Enable matching IPv6 FlowLabel; default is false") |
| 139 | private boolean matchIpv6FlowLabel = false; |
| 140 | |
| 141 | @Property(name = "matchTcpUdpPorts", boolValue = false, |
| 142 | label = "Enable matching TCP/UDP ports; default is false") |
| 143 | private boolean matchTcpUdpPorts = false; |
| 144 | |
| 145 | @Property(name = "matchIcmpFields", boolValue = false, |
| 146 | label = "Enable matching ICMPv4 and ICMPv6 fields; " + |
| 147 | "default is false") |
| 148 | private boolean matchIcmpFields = false; |
| 149 | |
| 150 | |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 151 | @Activate |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 152 | public void activate(ComponentContext context) { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 153 | appId = coreService.registerApplication("org.onosproject.fwd"); |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 154 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 155 | packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 2); |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 156 | readComponentConfiguration(context); |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 157 | |
| 158 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 159 | selector.matchEthType(Ethernet.TYPE_IPV4); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 160 | packetService.requestPackets(selector.build(), PacketPriority.REACTIVE, |
| 161 | appId); |
| 162 | selector.matchEthType(Ethernet.TYPE_ARP); |
| 163 | packetService.requestPackets(selector.build(), PacketPriority.REACTIVE, |
| 164 | appId); |
| 165 | |
| 166 | if (ipv6Forwarding) { |
| 167 | selector.matchEthType(Ethernet.TYPE_IPV6); |
| 168 | packetService.requestPackets(selector.build(), |
| 169 | PacketPriority.REACTIVE, appId); |
| 170 | } |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 171 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 172 | log.info("Started with Application ID {}", appId.id()); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | @Deactivate |
| 176 | public void deactivate() { |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 177 | flowRuleService.removeFlowRulesById(appId); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 178 | packetService.removeProcessor(processor); |
| 179 | processor = null; |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 180 | log.info("Stopped"); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 181 | } |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 182 | |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 183 | @Modified |
| 184 | public void modified(ComponentContext context) { |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 185 | readComponentConfiguration(context); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Extracts properties from the component configuration context. |
| 190 | * |
| 191 | * @param context the component context |
| 192 | */ |
| 193 | private void readComponentConfiguration(ComponentContext context) { |
| 194 | Dictionary<?, ?> properties = context.getProperties(); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 195 | boolean packetOutOnlyEnabled = |
| 196 | isPropertyEnabled(properties, "packetOutOnly"); |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 197 | if (packetOutOnly != packetOutOnlyEnabled) { |
| 198 | packetOutOnly = packetOutOnlyEnabled; |
| 199 | log.info("Configured. Packet-out only forwarding is {}", |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 200 | packetOutOnly ? "enabled" : "disabled"); |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 201 | } |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 202 | boolean packetOutOfppTableEnabled = |
| 203 | isPropertyEnabled(properties, "packetOutOfppTable"); |
| 204 | if (packetOutOfppTable != packetOutOfppTableEnabled) { |
| 205 | packetOutOfppTable = packetOutOfppTableEnabled; |
| 206 | log.info("Configured. Forwarding using OFPP_TABLE port is {}", |
| 207 | packetOutOfppTable ? "enabled" : "disabled"); |
| 208 | } |
| 209 | boolean ipv6ForwardingEnabled = |
| 210 | isPropertyEnabled(properties, "ipv6Forwarding"); |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 211 | if (ipv6Forwarding != ipv6ForwardingEnabled) { |
| 212 | ipv6Forwarding = ipv6ForwardingEnabled; |
| 213 | log.info("Configured. IPv6 forwarding is {}", |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 214 | ipv6Forwarding ? "enabled" : "disabled"); |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 215 | } |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 216 | boolean matchDstMacOnlyEnabled = |
| 217 | isPropertyEnabled(properties, "matchDstMacOnly"); |
| 218 | if (matchDstMacOnly != matchDstMacOnlyEnabled) { |
| 219 | matchDstMacOnly = matchDstMacOnlyEnabled; |
| 220 | log.info("Configured. Match Dst MAC Only is {}", |
| 221 | matchDstMacOnly ? "enabled" : "disabled"); |
| 222 | } |
| 223 | boolean matchVlanIdEnabled = |
| 224 | isPropertyEnabled(properties, "matchVlanId"); |
| 225 | if (matchVlanId != matchVlanIdEnabled) { |
| 226 | matchVlanId = matchVlanIdEnabled; |
| 227 | log.info("Configured. Matching Vlan ID is {}", |
| 228 | matchVlanId ? "enabled" : "disabled"); |
| 229 | } |
| 230 | boolean matchIpv4AddressEnabled = |
| 231 | isPropertyEnabled(properties, "matchIpv4Address"); |
| 232 | if (matchIpv4Address != matchIpv4AddressEnabled) { |
| 233 | matchIpv4Address = matchIpv4AddressEnabled; |
| 234 | log.info("Configured. Matching IPv4 Addresses is {}", |
| 235 | matchIpv4Address ? "enabled" : "disabled"); |
| 236 | } |
| 237 | boolean matchIpv4DscpEnabled = |
| 238 | isPropertyEnabled(properties, "matchIpv4Dscp"); |
| 239 | if (matchIpv4Dscp != matchIpv4DscpEnabled) { |
| 240 | matchIpv4Dscp = matchIpv4DscpEnabled; |
| 241 | log.info("Configured. Matching IPv4 DSCP and ECN is {}", |
| 242 | matchIpv4Dscp ? "enabled" : "disabled"); |
| 243 | } |
| 244 | boolean matchIpv6AddressEnabled = |
| 245 | isPropertyEnabled(properties, "matchIpv6Address"); |
| 246 | if (matchIpv6Address != matchIpv6AddressEnabled) { |
| 247 | matchIpv6Address = matchIpv6AddressEnabled; |
| 248 | log.info("Configured. Matching IPv6 Addresses is {}", |
| 249 | matchIpv6Address ? "enabled" : "disabled"); |
| 250 | } |
| 251 | boolean matchIpv6FlowLabelEnabled = |
| 252 | isPropertyEnabled(properties, "matchIpv6FlowLabel"); |
| 253 | if (matchIpv6FlowLabel != matchIpv6FlowLabelEnabled) { |
| 254 | matchIpv6FlowLabel = matchIpv6FlowLabelEnabled; |
| 255 | log.info("Configured. Matching IPv6 FlowLabel is {}", |
| 256 | matchIpv6FlowLabel ? "enabled" : "disabled"); |
| 257 | } |
| 258 | boolean matchTcpUdpPortsEnabled = |
| 259 | isPropertyEnabled(properties, "matchTcpUdpPorts"); |
| 260 | if (matchTcpUdpPorts != matchTcpUdpPortsEnabled) { |
| 261 | matchTcpUdpPorts = matchTcpUdpPortsEnabled; |
| 262 | log.info("Configured. Matching TCP/UDP fields is {}", |
| 263 | matchTcpUdpPorts ? "enabled" : "disabled"); |
| 264 | } |
| 265 | boolean matchIcmpFieldsEnabled = |
| 266 | isPropertyEnabled(properties, "matchIcmpFields"); |
| 267 | if (matchIcmpFields != matchIcmpFieldsEnabled) { |
| 268 | matchIcmpFields = matchIcmpFieldsEnabled; |
| 269 | log.info("Configured. Matching ICMP (v4 and v6) fields is {}", |
| 270 | matchIcmpFields ? "enabled" : "disabled"); |
| 271 | } |
| 272 | Integer flowTimeoutConfigured = |
| 273 | getIntegerProperty(properties, "flowTimeout"); |
| 274 | if (flowTimeoutConfigured == null) { |
| 275 | log.info("Flow Timeout is not configured, default value is {}", |
| 276 | flowTimeout); |
| 277 | } else { |
| 278 | flowTimeout = flowTimeoutConfigured; |
| 279 | log.info("Configured. Flow Timeout is configured to {}", |
| 280 | flowTimeout, " seconds"); |
| 281 | } |
| 282 | Integer flowPriorityConfigured = |
| 283 | getIntegerProperty(properties, "flowPriority"); |
| 284 | if (flowPriorityConfigured == null) { |
| 285 | log.info("Flow Priority is not configured, default value is {}", |
| 286 | flowPriority); |
| 287 | } else { |
| 288 | flowPriority = flowPriorityConfigured; |
| 289 | log.info("Configured. Flow Priority is configured to {}", |
| 290 | flowPriority); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Get Integer property from the propertyName |
| 296 | * Return null if propertyName is not found. |
| 297 | * |
| 298 | * @param properties properties to be looked up |
| 299 | * @param propertyName the name of the property to look up |
| 300 | * @return value when the propertyName is defined or return null |
| 301 | */ |
| 302 | private static Integer getIntegerProperty(Dictionary<?, ?> properties, |
| 303 | String propertyName) { |
| 304 | Integer value = null; |
| 305 | try { |
| 306 | String s = (String) properties.get(propertyName); |
| 307 | value = isNullOrEmpty(s) ? value : Integer.parseInt(s.trim()); |
Pavlin Radoslavov | b9e50df | 2015-02-20 20:01:26 -0800 | [diff] [blame] | 308 | } catch (NumberFormatException | ClassCastException e) { |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 309 | value = null; |
| 310 | } |
| 311 | return value; |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Check property name is defined and set to true. |
| 316 | * |
| 317 | * @param properties properties to be looked up |
| 318 | * @param propertyName the name of the property to look up |
| 319 | * @return true when the propertyName is defined and set to true |
| 320 | */ |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 321 | private static boolean isPropertyEnabled(Dictionary<?, ?> properties, |
| 322 | String propertyName) { |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 323 | boolean enabled = false; |
| 324 | try { |
| 325 | String flag = (String) properties.get(propertyName); |
| 326 | if (flag != null) { |
Ayaka Koshibe | 8851ed9 | 2015-01-22 12:07:24 -0800 | [diff] [blame] | 327 | enabled = flag.trim().equals("true"); |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 328 | } |
| 329 | } catch (ClassCastException e) { |
| 330 | // No propertyName defined. |
| 331 | enabled = false; |
| 332 | } |
| 333 | return enabled; |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 334 | } |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 335 | |
| 336 | /** |
| 337 | * Packet processor responsible for forwarding packets along their paths. |
| 338 | */ |
| 339 | private class ReactivePacketProcessor implements PacketProcessor { |
| 340 | |
| 341 | @Override |
| 342 | public void process(PacketContext context) { |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 343 | // Stop processing if the packet has been handled, since we |
| 344 | // can't do any more to it. |
Thomas Vachuska | bd7f4b3 | 2014-12-04 20:54:55 -0800 | [diff] [blame] | 345 | if (context.isHandled()) { |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 346 | return; |
| 347 | } |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 348 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 349 | InboundPacket pkt = context.inPacket(); |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 350 | Ethernet ethPkt = pkt.parsed(); |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 351 | |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 352 | if (ethPkt == null) { |
| 353 | return; |
| 354 | } |
| 355 | |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 356 | // Bail if this is deemed to be a control packet. |
| 357 | if (isControlPacket(ethPkt)) { |
| 358 | return; |
| 359 | } |
| 360 | |
| 361 | // Skip IPv6 multicast packet when IPv6 forward is disabled. |
| 362 | if (!ipv6Forwarding && isIpv6Multicast(ethPkt)) { |
Thomas Vachuska | 01a6ec0 | 2014-11-05 09:54:09 -0800 | [diff] [blame] | 363 | return; |
| 364 | } |
| 365 | |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 366 | HostId id = HostId.hostId(ethPkt.getDestinationMAC()); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 367 | |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 368 | // Do not process link-local addresses in any way. |
| 369 | if (id.mac().isLinkLocal()) { |
| 370 | return; |
| 371 | } |
| 372 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 373 | // Do we know who this is for? If not, flood and bail. |
| 374 | Host dst = hostService.getHost(id); |
| 375 | if (dst == null) { |
| 376 | flood(context); |
| 377 | return; |
| 378 | } |
| 379 | |
| 380 | // Are we on an edge switch that our destination is on? If so, |
| 381 | // simply forward out to the destination and bail. |
| 382 | if (pkt.receivedFrom().deviceId().equals(dst.location().deviceId())) { |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 383 | if (!context.inPacket().receivedFrom().port().equals(dst.location().port())) { |
| 384 | installRule(context, dst.location().port()); |
| 385 | } |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 386 | return; |
| 387 | } |
| 388 | |
| 389 | // Otherwise, get a set of paths that lead from here to the |
| 390 | // destination edge switch. |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 391 | Set<Path> paths = |
| 392 | topologyService.getPaths(topologyService.currentTopology(), |
| 393 | pkt.receivedFrom().deviceId(), |
| 394 | dst.location().deviceId()); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 395 | if (paths.isEmpty()) { |
| 396 | // If there are no paths, flood and bail. |
| 397 | flood(context); |
| 398 | return; |
| 399 | } |
| 400 | |
| 401 | // Otherwise, pick a path that does not lead back to where we |
| 402 | // came from; if no such path, flood and bail. |
| 403 | Path path = pickForwardPath(paths, pkt.receivedFrom().port()); |
| 404 | if (path == null) { |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 405 | log.warn("Doh... don't know where to go... {} -> {} received on {}", |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 406 | ethPkt.getSourceMAC(), ethPkt.getDestinationMAC(), |
| 407 | pkt.receivedFrom()); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 408 | flood(context); |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | // Otherwise forward and be done with it. |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 413 | installRule(context, path.src().port()); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 414 | } |
Thomas Vachuska | 01a6ec0 | 2014-11-05 09:54:09 -0800 | [diff] [blame] | 415 | |
| 416 | } |
| 417 | |
| 418 | // Indicates whether this is a control packet, e.g. LLDP, BDDP |
| 419 | private boolean isControlPacket(Ethernet eth) { |
| 420 | short type = eth.getEtherType(); |
| 421 | return type == Ethernet.TYPE_LLDP || type == Ethernet.TYPE_BSN; |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Thomas Vachuska | 5dd52f7 | 2014-11-28 19:27:45 -0800 | [diff] [blame] | 424 | // Indicated whether this is an IPv6 multicast packet. |
| 425 | private boolean isIpv6Multicast(Ethernet eth) { |
| 426 | return eth.getEtherType() == Ethernet.TYPE_IPV6 && eth.isMulticast(); |
| 427 | } |
| 428 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 429 | // Selects a path from the given set that does not lead back to the |
| 430 | // specified port. |
| 431 | private Path pickForwardPath(Set<Path> paths, PortNumber notToPort) { |
| 432 | for (Path path : paths) { |
| 433 | if (!path.src().port().equals(notToPort)) { |
| 434 | return path; |
| 435 | } |
| 436 | } |
| 437 | return null; |
| 438 | } |
| 439 | |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 440 | // Floods the specified packet if permissible. |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 441 | private void flood(PacketContext context) { |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 442 | if (topologyService.isBroadcastPoint(topologyService.currentTopology(), |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 443 | context.inPacket().receivedFrom())) { |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 444 | packetOut(context, PortNumber.FLOOD); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 445 | } else { |
| 446 | context.block(); |
| 447 | } |
| 448 | } |
| 449 | |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 450 | // Sends a packet out the specified port. |
| 451 | private void packetOut(PacketContext context, PortNumber portNumber) { |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 452 | context.treatmentBuilder().setOutput(portNumber); |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 453 | context.send(); |
| 454 | } |
| 455 | |
| 456 | // Install a rule forwarding the packet to the specified port. |
| 457 | private void installRule(PacketContext context, PortNumber portNumber) { |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 458 | // |
| 459 | // We don't support (yet) buffer IDs in the Flow Service so |
| 460 | // packet out first. |
| 461 | // |
| 462 | Ethernet inPkt = context.inPacket().parsed(); |
| 463 | TrafficSelector.Builder builder = DefaultTrafficSelector.builder(); |
| 464 | |
| 465 | // If PacketOutOnly or ARP packet than forward directly to output port |
| 466 | if (packetOutOnly || inPkt.getEtherType() == Ethernet.TYPE_ARP) { |
| 467 | packetOut(context, portNumber); |
| 468 | return; |
| 469 | } |
| 470 | |
| 471 | // |
| 472 | // If matchDstMacOnly |
| 473 | // Create flows matching dstMac only |
| 474 | // Else |
| 475 | // Create flows with default matching and include configured fields |
| 476 | // |
| 477 | if (matchDstMacOnly) { |
| 478 | builder.matchEthDst(inPkt.getDestinationMAC()); |
| 479 | } else { |
| 480 | builder.matchInPort(context.inPacket().receivedFrom().port()) |
Thomas Vachuska | bd7f4b3 | 2014-12-04 20:54:55 -0800 | [diff] [blame] | 481 | .matchEthSrc(inPkt.getSourceMAC()) |
| 482 | .matchEthDst(inPkt.getDestinationMAC()) |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 483 | .matchEthType(inPkt.getEtherType()); |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 484 | |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 485 | // If configured Match Vlan ID |
| 486 | if (matchVlanId && inPkt.getVlanID() != Ethernet.VLAN_UNTAGGED) { |
| 487 | builder.matchVlanId(VlanId.vlanId(inPkt.getVlanID())); |
| 488 | } |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 489 | |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 490 | // |
| 491 | // If configured and EtherType is IPv4 - Match IPv4 and |
| 492 | // TCP/UDP/ICMP fields |
| 493 | // |
| 494 | if (matchIpv4Address && inPkt.getEtherType() == Ethernet.TYPE_IPV4) { |
| 495 | IPv4 ipv4Packet = (IPv4) inPkt.getPayload(); |
| 496 | byte ipv4Protocol = ipv4Packet.getProtocol(); |
| 497 | Ip4Prefix matchIp4SrcPrefix = |
| 498 | Ip4Prefix.valueOf(ipv4Packet.getSourceAddress(), |
| 499 | Ip4Prefix.MAX_MASK_LENGTH); |
| 500 | Ip4Prefix matchIp4DstPrefix = |
| 501 | Ip4Prefix.valueOf(ipv4Packet.getDestinationAddress(), |
| 502 | Ip4Prefix.MAX_MASK_LENGTH); |
| 503 | builder.matchIPSrc(matchIp4SrcPrefix) |
| 504 | .matchIPDst(matchIp4DstPrefix) |
| 505 | .matchIPProtocol(ipv4Protocol); |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 506 | |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 507 | if (matchIpv4Dscp) { |
Pavlin Radoslavov | bf23c55 | 2015-02-20 14:20:30 -0800 | [diff] [blame] | 508 | byte dscp = ipv4Packet.getDscp(); |
| 509 | byte ecn = ipv4Packet.getEcn(); |
| 510 | builder.matchIPDscp(dscp).matchIPEcn(ecn); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | if (matchTcpUdpPorts && ipv4Protocol == IPv4.PROTOCOL_TCP) { |
| 514 | TCP tcpPacket = (TCP) ipv4Packet.getPayload(); |
| 515 | builder.matchTcpSrc(tcpPacket.getSourcePort()) |
| 516 | .matchTcpDst(tcpPacket.getDestinationPort()); |
| 517 | } |
| 518 | if (matchTcpUdpPorts && ipv4Protocol == IPv4.PROTOCOL_UDP) { |
| 519 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 520 | builder.matchUdpSrc(udpPacket.getSourcePort()) |
| 521 | .matchUdpDst(udpPacket.getDestinationPort()); |
| 522 | } |
| 523 | if (matchIcmpFields && ipv4Protocol == IPv4.PROTOCOL_ICMP) { |
| 524 | ICMP icmpPacket = (ICMP) ipv4Packet.getPayload(); |
| 525 | builder.matchIcmpType(icmpPacket.getIcmpType()) |
| 526 | .matchIcmpCode(icmpPacket.getIcmpCode()); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | // |
| 531 | // If configured and EtherType is IPv6 - Match IPv6 and |
| 532 | // TCP/UDP/ICMP fields |
| 533 | // |
| 534 | if (matchIpv6Address && inPkt.getEtherType() == Ethernet.TYPE_IPV6) { |
| 535 | IPv6 ipv6Packet = (IPv6) inPkt.getPayload(); |
| 536 | byte ipv6NextHeader = ipv6Packet.getNextHeader(); |
| 537 | Ip6Prefix matchIp6SrcPrefix = |
| 538 | Ip6Prefix.valueOf(ipv6Packet.getSourceAddress(), |
| 539 | Ip6Prefix.MAX_MASK_LENGTH); |
| 540 | Ip6Prefix matchIp6DstPrefix = |
| 541 | Ip6Prefix.valueOf(ipv6Packet.getDestinationAddress(), |
| 542 | Ip6Prefix.MAX_MASK_LENGTH); |
| 543 | builder.matchIPv6Src(matchIp6SrcPrefix) |
| 544 | .matchIPv6Dst(matchIp6DstPrefix) |
| 545 | .matchIPProtocol(ipv6NextHeader); |
| 546 | |
| 547 | if (matchIpv6FlowLabel) { |
| 548 | builder.matchIPv6FlowLabel(ipv6Packet.getFlowLabel()); |
| 549 | } |
| 550 | |
| 551 | if (matchTcpUdpPorts && ipv6NextHeader == IPv6.PROTOCOL_TCP) { |
| 552 | TCP tcpPacket = (TCP) ipv6Packet.getPayload(); |
| 553 | builder.matchTcpSrc(tcpPacket.getSourcePort()) |
| 554 | .matchTcpDst(tcpPacket.getDestinationPort()); |
| 555 | } |
| 556 | if (matchTcpUdpPorts && ipv6NextHeader == IPv6.PROTOCOL_UDP) { |
| 557 | UDP udpPacket = (UDP) ipv6Packet.getPayload(); |
| 558 | builder.matchUdpSrc(udpPacket.getSourcePort()) |
| 559 | .matchUdpDst(udpPacket.getDestinationPort()); |
| 560 | } |
| 561 | if (matchIcmpFields && ipv6NextHeader == IPv6.PROTOCOL_ICMP6) { |
| 562 | ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload(); |
| 563 | builder.matchIcmpv6Type(icmp6Packet.getIcmpType()) |
| 564 | .matchIcmpv6Code(icmp6Packet.getIcmpCode()); |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder(); |
| 569 | treat.setOutput(portNumber); |
| 570 | |
| 571 | FlowRule f = |
| 572 | new DefaultFlowRule(context.inPacket().receivedFrom().deviceId(), |
| 573 | builder.build(), treat.build(), flowPriority, |
| 574 | appId, flowTimeout, false); |
| 575 | |
| 576 | flowRuleService.applyFlowRules(f); |
| 577 | |
| 578 | // |
| 579 | // If packetOutOfppTable |
| 580 | // Send packet back to the OpenFlow pipeline to match installed flow |
| 581 | // Else |
| 582 | // Send packet direction on the appropriate port |
| 583 | // |
| 584 | if (packetOutOfppTable) { |
| 585 | packetOut(context, PortNumber.TABLE); |
| 586 | } else { |
| 587 | packetOut(context, portNumber); |
Thomas Vachuska | bd7f4b3 | 2014-12-04 20:54:55 -0800 | [diff] [blame] | 588 | } |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 589 | } |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 590 | } |