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