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