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