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