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 | |
Sahil Lele | a69534f | 2015-07-16 15:14:57 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Modified; |
| 23 | import org.apache.felix.scr.annotations.Property; |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 26 | import org.onlab.packet.Ethernet; |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 27 | import org.onlab.packet.ICMP; |
| 28 | import org.onlab.packet.ICMP6; |
Jonathan Hart | 430223a | 2015-04-22 17:39:02 -0700 | [diff] [blame] | 29 | import org.onlab.packet.IPv4; |
| 30 | import org.onlab.packet.IPv6; |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 31 | import org.onlab.packet.Ip4Prefix; |
| 32 | import org.onlab.packet.Ip6Prefix; |
Sahil Lele | a69534f | 2015-07-16 15:14:57 -0700 | [diff] [blame] | 33 | import org.onlab.packet.MacAddress; |
Jonathan Hart | 430223a | 2015-04-22 17:39:02 -0700 | [diff] [blame] | 34 | import org.onlab.packet.TCP; |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 35 | import org.onlab.packet.TpPort; |
Jonathan Hart | 430223a | 2015-04-22 17:39:02 -0700 | [diff] [blame] | 36 | import org.onlab.packet.UDP; |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 37 | import org.onlab.packet.VlanId; |
maojianwei | 8bf77b7 | 2016-02-15 15:18:24 +0800 | [diff] [blame] | 38 | import org.onlab.util.Tools; |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 39 | import org.onosproject.cfg.ComponentConfigService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 40 | import org.onosproject.core.ApplicationId; |
| 41 | import org.onosproject.core.CoreService; |
Sahil Lele | a69534f | 2015-07-16 15:14:57 -0700 | [diff] [blame] | 42 | import org.onosproject.event.Event; |
| 43 | import org.onosproject.net.ConnectPoint; |
| 44 | import org.onosproject.net.DeviceId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 45 | import org.onosproject.net.Host; |
| 46 | import org.onosproject.net.HostId; |
Sahil Lele | a69534f | 2015-07-16 15:14:57 -0700 | [diff] [blame] | 47 | import org.onosproject.net.Link; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 48 | import org.onosproject.net.Path; |
| 49 | import org.onosproject.net.PortNumber; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 50 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 51 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Sahil Lele | a69534f | 2015-07-16 15:14:57 -0700 | [diff] [blame] | 52 | import org.onosproject.net.flow.FlowEntry; |
| 53 | import org.onosproject.net.flow.FlowRule; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 54 | import org.onosproject.net.flow.FlowRuleService; |
| 55 | import org.onosproject.net.flow.TrafficSelector; |
| 56 | import org.onosproject.net.flow.TrafficTreatment; |
Sahil Lele | a69534f | 2015-07-16 15:14:57 -0700 | [diff] [blame] | 57 | import org.onosproject.net.flow.criteria.Criterion; |
| 58 | import org.onosproject.net.flow.criteria.EthCriterion; |
| 59 | import org.onosproject.net.flow.instructions.Instruction; |
| 60 | import org.onosproject.net.flow.instructions.Instructions; |
Jonathan Hart | 3b881aa | 2015-04-22 18:03:50 -0700 | [diff] [blame] | 61 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 62 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 63 | import org.onosproject.net.flowobjective.ForwardingObjective; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 64 | import org.onosproject.net.host.HostService; |
Sahil Lele | a69534f | 2015-07-16 15:14:57 -0700 | [diff] [blame] | 65 | import org.onosproject.net.link.LinkEvent; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 66 | import org.onosproject.net.packet.InboundPacket; |
| 67 | import org.onosproject.net.packet.PacketContext; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 68 | import org.onosproject.net.packet.PacketPriority; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 69 | import org.onosproject.net.packet.PacketProcessor; |
| 70 | import org.onosproject.net.packet.PacketService; |
Sahil Lele | a69534f | 2015-07-16 15:14:57 -0700 | [diff] [blame] | 71 | import org.onosproject.net.topology.TopologyEvent; |
| 72 | import org.onosproject.net.topology.TopologyListener; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 73 | import org.onosproject.net.topology.TopologyService; |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 74 | import org.osgi.service.component.ComponentContext; |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 75 | import org.slf4j.Logger; |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 76 | |
Jonathan Hart | 430223a | 2015-04-22 17:39:02 -0700 | [diff] [blame] | 77 | import java.util.Dictionary; |
Sahil Lele | 7b96166 | 2015-07-20 15:06:04 -0700 | [diff] [blame] | 78 | import java.util.HashMap; |
Sahil Lele | a69534f | 2015-07-16 15:14:57 -0700 | [diff] [blame] | 79 | import java.util.List; |
Sahil Lele | 7b96166 | 2015-07-20 15:06:04 -0700 | [diff] [blame] | 80 | import java.util.Map; |
Sahil Lele | a69534f | 2015-07-16 15:14:57 -0700 | [diff] [blame] | 81 | import java.util.Objects; |
Jonathan Hart | 430223a | 2015-04-22 17:39:02 -0700 | [diff] [blame] | 82 | import java.util.Set; |
| 83 | |
| 84 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 85 | import static org.slf4j.LoggerFactory.getLogger; |
| 86 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 87 | /** |
| 88 | * Sample reactive forwarding application. |
| 89 | */ |
| 90 | @Component(immediate = true) |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 91 | public class ReactiveForwarding { |
| 92 | |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 93 | private static final int DEFAULT_TIMEOUT = 10; |
| 94 | private static final int DEFAULT_PRIORITY = 10; |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 95 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 96 | private final Logger log = getLogger(getClass()); |
| 97 | |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 98 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 99 | protected TopologyService topologyService; |
| 100 | |
| 101 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 102 | protected PacketService packetService; |
| 103 | |
alshabib | 8aef1ad | 2014-09-15 17:47:31 -0700 | [diff] [blame] | 104 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 105 | protected HostService hostService; |
| 106 | |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 107 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 108 | protected FlowRuleService flowRuleService; |
| 109 | |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 110 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jonathan Hart | 3b881aa | 2015-04-22 18:03:50 -0700 | [diff] [blame] | 111 | protected FlowObjectiveService flowObjectiveService; |
| 112 | |
| 113 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 114 | protected CoreService coreService; |
| 115 | |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 116 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 117 | protected ComponentConfigService cfgService; |
| 118 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 119 | private ReactivePacketProcessor processor = new ReactivePacketProcessor(); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 120 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 121 | private ApplicationId appId; |
| 122 | |
Thomas Vachuska | bd7f4b3 | 2014-12-04 20:54:55 -0800 | [diff] [blame] | 123 | @Property(name = "packetOutOnly", boolValue = false, |
| 124 | label = "Enable packet-out only forwarding; default is false") |
| 125 | private boolean packetOutOnly = false; |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 126 | |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 127 | @Property(name = "packetOutOfppTable", boolValue = false, |
| 128 | label = "Enable first packet forwarding using OFPP_TABLE port " + |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 129 | "instead of PacketOut with actual port; default is false") |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 130 | private boolean packetOutOfppTable = false; |
| 131 | |
| 132 | @Property(name = "flowTimeout", intValue = DEFAULT_TIMEOUT, |
| 133 | label = "Configure Flow Timeout for installed flow rules; " + |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 134 | "default is 10 sec") |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 135 | private int flowTimeout = DEFAULT_TIMEOUT; |
| 136 | |
| 137 | @Property(name = "flowPriority", intValue = DEFAULT_PRIORITY, |
| 138 | label = "Configure Flow Priority for installed flow rules; " + |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 139 | "default is 10") |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 140 | private int flowPriority = DEFAULT_PRIORITY; |
| 141 | |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 142 | @Property(name = "ipv6Forwarding", boolValue = false, |
| 143 | label = "Enable IPv6 forwarding; default is false") |
| 144 | private boolean ipv6Forwarding = false; |
| 145 | |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 146 | @Property(name = "matchDstMacOnly", boolValue = false, |
| 147 | label = "Enable matching Dst Mac Only; default is false") |
| 148 | private boolean matchDstMacOnly = false; |
| 149 | |
| 150 | @Property(name = "matchVlanId", boolValue = false, |
| 151 | label = "Enable matching Vlan ID; default is false") |
| 152 | private boolean matchVlanId = false; |
| 153 | |
| 154 | @Property(name = "matchIpv4Address", boolValue = false, |
| 155 | label = "Enable matching IPv4 Addresses; default is false") |
| 156 | private boolean matchIpv4Address = false; |
| 157 | |
| 158 | @Property(name = "matchIpv4Dscp", boolValue = false, |
| 159 | label = "Enable matching IPv4 DSCP and ECN; default is false") |
| 160 | private boolean matchIpv4Dscp = false; |
| 161 | |
| 162 | @Property(name = "matchIpv6Address", boolValue = false, |
| 163 | label = "Enable matching IPv6 Addresses; default is false") |
| 164 | private boolean matchIpv6Address = false; |
| 165 | |
| 166 | @Property(name = "matchIpv6FlowLabel", boolValue = false, |
| 167 | label = "Enable matching IPv6 FlowLabel; default is false") |
| 168 | private boolean matchIpv6FlowLabel = false; |
| 169 | |
| 170 | @Property(name = "matchTcpUdpPorts", boolValue = false, |
| 171 | label = "Enable matching TCP/UDP ports; default is false") |
| 172 | private boolean matchTcpUdpPorts = false; |
| 173 | |
| 174 | @Property(name = "matchIcmpFields", boolValue = false, |
| 175 | label = "Enable matching ICMPv4 and ICMPv6 fields; " + |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 176 | "default is false") |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 177 | private boolean matchIcmpFields = false; |
| 178 | |
Sahil Lele | a69534f | 2015-07-16 15:14:57 -0700 | [diff] [blame] | 179 | |
Rusty Eddy | 29acad6 | 2015-07-07 19:33:47 -0700 | [diff] [blame] | 180 | @Property(name = "ignoreIPv4Multicast", boolValue = false, |
| 181 | label = "Ignore (do not forward) IPv4 multicast packets; default is false") |
| 182 | private boolean ignoreIpv4McastPackets = false; |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 183 | |
Sahil Lele | a69534f | 2015-07-16 15:14:57 -0700 | [diff] [blame] | 184 | private final TopologyListener topologyListener = new InternalTopologyListener(); |
| 185 | |
| 186 | |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 187 | @Activate |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 188 | public void activate(ComponentContext context) { |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 189 | cfgService.registerProperties(getClass()); |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 190 | appId = coreService.registerApplication("org.onosproject.fwd"); |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 191 | |
Brian O'Connor | 3b78326 | 2015-07-29 17:49:24 -0700 | [diff] [blame] | 192 | packetService.addProcessor(processor, PacketProcessor.director(2)); |
Sahil Lele | a69534f | 2015-07-16 15:14:57 -0700 | [diff] [blame] | 193 | topologyService.addListener(topologyListener); |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 194 | readComponentConfiguration(context); |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 195 | requestIntercepts(); |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 196 | |
Thomas Vachuska | 8d03367 | 2015-07-21 16:15:04 -0700 | [diff] [blame] | 197 | log.info("Started", appId.id()); |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | @Deactivate |
| 201 | public void deactivate() { |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 202 | cfgService.unregisterProperties(getClass(), false); |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 203 | withdrawIntercepts(); |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 204 | flowRuleService.removeFlowRulesById(appId); |
| 205 | packetService.removeProcessor(processor); |
Sahil Lele | a69534f | 2015-07-16 15:14:57 -0700 | [diff] [blame] | 206 | topologyService.removeListener(topologyListener); |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 207 | processor = null; |
| 208 | log.info("Stopped"); |
| 209 | } |
| 210 | |
| 211 | @Modified |
| 212 | public void modified(ComponentContext context) { |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 213 | readComponentConfiguration(context); |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 214 | requestIntercepts(); |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /** |
Thomas Vachuska | 8d03367 | 2015-07-21 16:15:04 -0700 | [diff] [blame] | 218 | * Request packet in via packet service. |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 219 | */ |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 220 | private void requestIntercepts() { |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 221 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 222 | selector.matchEthType(Ethernet.TYPE_IPV4); |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 223 | packetService.requestPackets(selector.build(), PacketPriority.REACTIVE, appId); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 224 | selector.matchEthType(Ethernet.TYPE_ARP); |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 225 | packetService.requestPackets(selector.build(), PacketPriority.REACTIVE, appId); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 226 | |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 227 | selector.matchEthType(Ethernet.TYPE_IPV6); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 228 | if (ipv6Forwarding) { |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 229 | packetService.requestPackets(selector.build(), PacketPriority.REACTIVE, appId); |
| 230 | } else { |
| 231 | packetService.cancelPackets(selector.build(), PacketPriority.REACTIVE, appId); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 232 | } |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | /** |
Thomas Vachuska | 8d03367 | 2015-07-21 16:15:04 -0700 | [diff] [blame] | 236 | * Cancel request for packet in via packet service. |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 237 | */ |
| 238 | private void withdrawIntercepts() { |
| 239 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 240 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 241 | packetService.cancelPackets(selector.build(), PacketPriority.REACTIVE, appId); |
| 242 | selector.matchEthType(Ethernet.TYPE_ARP); |
| 243 | packetService.cancelPackets(selector.build(), PacketPriority.REACTIVE, appId); |
| 244 | selector.matchEthType(Ethernet.TYPE_IPV6); |
| 245 | packetService.cancelPackets(selector.build(), PacketPriority.REACTIVE, appId); |
| 246 | } |
| 247 | |
| 248 | /** |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 249 | * Extracts properties from the component configuration context. |
| 250 | * |
| 251 | * @param context the component context |
| 252 | */ |
| 253 | private void readComponentConfiguration(ComponentContext context) { |
| 254 | Dictionary<?, ?> properties = context.getProperties(); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 255 | boolean packetOutOnlyEnabled = |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 256 | isPropertyEnabled(properties, "packetOutOnly"); |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 257 | if (packetOutOnly != packetOutOnlyEnabled) { |
| 258 | packetOutOnly = packetOutOnlyEnabled; |
| 259 | log.info("Configured. Packet-out only forwarding is {}", |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 260 | packetOutOnly ? "enabled" : "disabled"); |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 261 | } |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 262 | boolean packetOutOfppTableEnabled = |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 263 | isPropertyEnabled(properties, "packetOutOfppTable"); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 264 | if (packetOutOfppTable != packetOutOfppTableEnabled) { |
| 265 | packetOutOfppTable = packetOutOfppTableEnabled; |
| 266 | log.info("Configured. Forwarding using OFPP_TABLE port is {}", |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 267 | packetOutOfppTable ? "enabled" : "disabled"); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 268 | } |
| 269 | boolean ipv6ForwardingEnabled = |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 270 | isPropertyEnabled(properties, "ipv6Forwarding"); |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 271 | if (ipv6Forwarding != ipv6ForwardingEnabled) { |
| 272 | ipv6Forwarding = ipv6ForwardingEnabled; |
| 273 | log.info("Configured. IPv6 forwarding is {}", |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 274 | ipv6Forwarding ? "enabled" : "disabled"); |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 275 | } |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 276 | boolean matchDstMacOnlyEnabled = |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 277 | isPropertyEnabled(properties, "matchDstMacOnly"); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 278 | if (matchDstMacOnly != matchDstMacOnlyEnabled) { |
| 279 | matchDstMacOnly = matchDstMacOnlyEnabled; |
| 280 | log.info("Configured. Match Dst MAC Only is {}", |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 281 | matchDstMacOnly ? "enabled" : "disabled"); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 282 | } |
| 283 | boolean matchVlanIdEnabled = |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 284 | isPropertyEnabled(properties, "matchVlanId"); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 285 | if (matchVlanId != matchVlanIdEnabled) { |
| 286 | matchVlanId = matchVlanIdEnabled; |
| 287 | log.info("Configured. Matching Vlan ID is {}", |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 288 | matchVlanId ? "enabled" : "disabled"); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 289 | } |
| 290 | boolean matchIpv4AddressEnabled = |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 291 | isPropertyEnabled(properties, "matchIpv4Address"); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 292 | if (matchIpv4Address != matchIpv4AddressEnabled) { |
| 293 | matchIpv4Address = matchIpv4AddressEnabled; |
| 294 | log.info("Configured. Matching IPv4 Addresses is {}", |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 295 | matchIpv4Address ? "enabled" : "disabled"); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 296 | } |
| 297 | boolean matchIpv4DscpEnabled = |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 298 | isPropertyEnabled(properties, "matchIpv4Dscp"); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 299 | if (matchIpv4Dscp != matchIpv4DscpEnabled) { |
| 300 | matchIpv4Dscp = matchIpv4DscpEnabled; |
| 301 | log.info("Configured. Matching IPv4 DSCP and ECN is {}", |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 302 | matchIpv4Dscp ? "enabled" : "disabled"); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 303 | } |
| 304 | boolean matchIpv6AddressEnabled = |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 305 | isPropertyEnabled(properties, "matchIpv6Address"); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 306 | if (matchIpv6Address != matchIpv6AddressEnabled) { |
| 307 | matchIpv6Address = matchIpv6AddressEnabled; |
| 308 | log.info("Configured. Matching IPv6 Addresses is {}", |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 309 | matchIpv6Address ? "enabled" : "disabled"); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 310 | } |
| 311 | boolean matchIpv6FlowLabelEnabled = |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 312 | isPropertyEnabled(properties, "matchIpv6FlowLabel"); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 313 | if (matchIpv6FlowLabel != matchIpv6FlowLabelEnabled) { |
| 314 | matchIpv6FlowLabel = matchIpv6FlowLabelEnabled; |
| 315 | log.info("Configured. Matching IPv6 FlowLabel is {}", |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 316 | matchIpv6FlowLabel ? "enabled" : "disabled"); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 317 | } |
| 318 | boolean matchTcpUdpPortsEnabled = |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 319 | isPropertyEnabled(properties, "matchTcpUdpPorts"); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 320 | if (matchTcpUdpPorts != matchTcpUdpPortsEnabled) { |
| 321 | matchTcpUdpPorts = matchTcpUdpPortsEnabled; |
| 322 | log.info("Configured. Matching TCP/UDP fields is {}", |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 323 | matchTcpUdpPorts ? "enabled" : "disabled"); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 324 | } |
| 325 | boolean matchIcmpFieldsEnabled = |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 326 | isPropertyEnabled(properties, "matchIcmpFields"); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 327 | if (matchIcmpFields != matchIcmpFieldsEnabled) { |
| 328 | matchIcmpFields = matchIcmpFieldsEnabled; |
| 329 | log.info("Configured. Matching ICMP (v4 and v6) fields is {}", |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 330 | matchIcmpFields ? "enabled" : "disabled"); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 331 | } |
| 332 | Integer flowTimeoutConfigured = |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 333 | getIntegerProperty(properties, "flowTimeout"); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 334 | if (flowTimeoutConfigured == null) { |
Antonio Marsico | b8b8d75 | 2015-12-17 16:28:34 +0100 | [diff] [blame] | 335 | flowTimeout = DEFAULT_TIMEOUT; |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 336 | log.info("Flow Timeout is not configured, default value is {}", |
| 337 | flowTimeout); |
| 338 | } else { |
| 339 | flowTimeout = flowTimeoutConfigured; |
| 340 | log.info("Configured. Flow Timeout is configured to {}", |
| 341 | flowTimeout, " seconds"); |
| 342 | } |
| 343 | Integer flowPriorityConfigured = |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 344 | getIntegerProperty(properties, "flowPriority"); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 345 | if (flowPriorityConfigured == null) { |
Antonio Marsico | b8b8d75 | 2015-12-17 16:28:34 +0100 | [diff] [blame] | 346 | flowPriority = DEFAULT_PRIORITY; |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 347 | log.info("Flow Priority is not configured, default value is {}", |
| 348 | flowPriority); |
| 349 | } else { |
| 350 | flowPriority = flowPriorityConfigured; |
| 351 | log.info("Configured. Flow Priority is configured to {}", |
| 352 | flowPriority); |
| 353 | } |
Rusty Eddy | 29acad6 | 2015-07-07 19:33:47 -0700 | [diff] [blame] | 354 | |
| 355 | boolean ignoreIpv4McastPacketsEnabled = |
| 356 | isPropertyEnabled(properties, "ignoreIpv4McastPackets"); |
| 357 | if (ignoreIpv4McastPackets != ignoreIpv4McastPacketsEnabled) { |
| 358 | ignoreIpv4McastPackets = ignoreIpv4McastPacketsEnabled; |
| 359 | log.info("Configured. Ignore IPv4 multicast packets is {}", |
Thomas Vachuska | 8d03367 | 2015-07-21 16:15:04 -0700 | [diff] [blame] | 360 | ignoreIpv4McastPackets ? "enabled" : "disabled"); |
Rusty Eddy | 29acad6 | 2015-07-07 19:33:47 -0700 | [diff] [blame] | 361 | } |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Get Integer property from the propertyName |
| 366 | * Return null if propertyName is not found. |
| 367 | * |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 368 | * @param properties properties to be looked up |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 369 | * @param propertyName the name of the property to look up |
| 370 | * @return value when the propertyName is defined or return null |
| 371 | */ |
| 372 | private static Integer getIntegerProperty(Dictionary<?, ?> properties, |
| 373 | String propertyName) { |
| 374 | Integer value = null; |
| 375 | try { |
maojianwei | 8bf77b7 | 2016-02-15 15:18:24 +0800 | [diff] [blame] | 376 | String s = Tools.get(properties, propertyName); |
| 377 | value = isNullOrEmpty(s) ? value : Integer.parseInt(s); |
Pavlin Radoslavov | b9e50df | 2015-02-20 20:01:26 -0800 | [diff] [blame] | 378 | } catch (NumberFormatException | ClassCastException e) { |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 379 | value = null; |
| 380 | } |
| 381 | return value; |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Check property name is defined and set to true. |
| 386 | * |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 387 | * @param properties properties to be looked up |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 388 | * @param propertyName the name of the property to look up |
| 389 | * @return true when the propertyName is defined and set to true |
| 390 | */ |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 391 | private static boolean isPropertyEnabled(Dictionary<?, ?> properties, |
| 392 | String propertyName) { |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 393 | boolean enabled = false; |
| 394 | try { |
maojianwei | 8bf77b7 | 2016-02-15 15:18:24 +0800 | [diff] [blame] | 395 | String flag = Tools.get(properties, propertyName); |
| 396 | enabled = isNullOrEmpty(flag) ? enabled : flag.equals("true"); |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 397 | } catch (ClassCastException e) { |
| 398 | // No propertyName defined. |
| 399 | enabled = false; |
| 400 | } |
| 401 | return enabled; |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 402 | } |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 403 | |
| 404 | /** |
| 405 | * Packet processor responsible for forwarding packets along their paths. |
| 406 | */ |
| 407 | private class ReactivePacketProcessor implements PacketProcessor { |
| 408 | |
| 409 | @Override |
| 410 | public void process(PacketContext context) { |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 411 | // Stop processing if the packet has been handled, since we |
| 412 | // can't do any more to it. |
Sahil Lele | a69534f | 2015-07-16 15:14:57 -0700 | [diff] [blame] | 413 | |
Thomas Vachuska | bd7f4b3 | 2014-12-04 20:54:55 -0800 | [diff] [blame] | 414 | if (context.isHandled()) { |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 415 | return; |
| 416 | } |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 417 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 418 | InboundPacket pkt = context.inPacket(); |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 419 | Ethernet ethPkt = pkt.parsed(); |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 420 | |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 421 | if (ethPkt == null) { |
| 422 | return; |
| 423 | } |
| 424 | |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 425 | // Bail if this is deemed to be a control packet. |
| 426 | if (isControlPacket(ethPkt)) { |
| 427 | return; |
| 428 | } |
| 429 | |
| 430 | // Skip IPv6 multicast packet when IPv6 forward is disabled. |
| 431 | if (!ipv6Forwarding && isIpv6Multicast(ethPkt)) { |
Thomas Vachuska | 01a6ec0 | 2014-11-05 09:54:09 -0800 | [diff] [blame] | 432 | return; |
| 433 | } |
| 434 | |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 435 | HostId id = HostId.hostId(ethPkt.getDestinationMAC()); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 436 | |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 437 | // Do not process link-local addresses in any way. |
| 438 | if (id.mac().isLinkLocal()) { |
| 439 | return; |
| 440 | } |
| 441 | |
Rusty Eddy | 29acad6 | 2015-07-07 19:33:47 -0700 | [diff] [blame] | 442 | // Do not process IPv4 multicast packets, let mfwd handle them |
| 443 | if (ignoreIpv4McastPackets && ethPkt.getEtherType() == Ethernet.TYPE_IPV4) { |
| 444 | if (id.mac().isMulticast()) { |
| 445 | return; |
| 446 | } |
| 447 | } |
| 448 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 449 | // Do we know who this is for? If not, flood and bail. |
| 450 | Host dst = hostService.getHost(id); |
| 451 | if (dst == null) { |
| 452 | flood(context); |
| 453 | return; |
| 454 | } |
| 455 | |
| 456 | // Are we on an edge switch that our destination is on? If so, |
| 457 | // simply forward out to the destination and bail. |
| 458 | if (pkt.receivedFrom().deviceId().equals(dst.location().deviceId())) { |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 459 | if (!context.inPacket().receivedFrom().port().equals(dst.location().port())) { |
| 460 | installRule(context, dst.location().port()); |
| 461 | } |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 462 | return; |
| 463 | } |
| 464 | |
| 465 | // Otherwise, get a set of paths that lead from here to the |
| 466 | // destination edge switch. |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 467 | Set<Path> paths = |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 468 | topologyService.getPaths(topologyService.currentTopology(), |
| 469 | pkt.receivedFrom().deviceId(), |
| 470 | dst.location().deviceId()); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 471 | if (paths.isEmpty()) { |
| 472 | // If there are no paths, flood and bail. |
| 473 | flood(context); |
| 474 | return; |
| 475 | } |
| 476 | |
| 477 | // Otherwise, pick a path that does not lead back to where we |
| 478 | // came from; if no such path, flood and bail. |
Thomas Vachuska | 320c58f | 2015-08-05 10:42:32 -0700 | [diff] [blame] | 479 | Path path = pickForwardPathIfPossible(paths, pkt.receivedFrom().port()); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 480 | if (path == null) { |
Thomas Vachuska | 320c58f | 2015-08-05 10:42:32 -0700 | [diff] [blame] | 481 | log.warn("Don't know where to go from here {} for {} -> {}", |
| 482 | pkt.receivedFrom(), ethPkt.getSourceMAC(), ethPkt.getDestinationMAC()); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 483 | flood(context); |
| 484 | return; |
| 485 | } |
| 486 | |
| 487 | // Otherwise forward and be done with it. |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 488 | installRule(context, path.src().port()); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 489 | } |
Thomas Vachuska | 01a6ec0 | 2014-11-05 09:54:09 -0800 | [diff] [blame] | 490 | |
| 491 | } |
| 492 | |
| 493 | // Indicates whether this is a control packet, e.g. LLDP, BDDP |
| 494 | private boolean isControlPacket(Ethernet eth) { |
| 495 | short type = eth.getEtherType(); |
| 496 | return type == Ethernet.TYPE_LLDP || type == Ethernet.TYPE_BSN; |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Thomas Vachuska | 5dd52f7 | 2014-11-28 19:27:45 -0800 | [diff] [blame] | 499 | // Indicated whether this is an IPv6 multicast packet. |
| 500 | private boolean isIpv6Multicast(Ethernet eth) { |
| 501 | return eth.getEtherType() == Ethernet.TYPE_IPV6 && eth.isMulticast(); |
| 502 | } |
| 503 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 504 | // Selects a path from the given set that does not lead back to the |
Thomas Vachuska | 320c58f | 2015-08-05 10:42:32 -0700 | [diff] [blame] | 505 | // specified port if possible. |
| 506 | private Path pickForwardPathIfPossible(Set<Path> paths, PortNumber notToPort) { |
| 507 | Path lastPath = null; |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 508 | for (Path path : paths) { |
Thomas Vachuska | 320c58f | 2015-08-05 10:42:32 -0700 | [diff] [blame] | 509 | lastPath = path; |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 510 | if (!path.src().port().equals(notToPort)) { |
| 511 | return path; |
| 512 | } |
| 513 | } |
Thomas Vachuska | 320c58f | 2015-08-05 10:42:32 -0700 | [diff] [blame] | 514 | return lastPath; |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 515 | } |
| 516 | |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 517 | // Floods the specified packet if permissible. |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 518 | private void flood(PacketContext context) { |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 519 | if (topologyService.isBroadcastPoint(topologyService.currentTopology(), |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 520 | context.inPacket().receivedFrom())) { |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 521 | packetOut(context, PortNumber.FLOOD); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 522 | } else { |
| 523 | context.block(); |
| 524 | } |
| 525 | } |
| 526 | |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 527 | // Sends a packet out the specified port. |
| 528 | private void packetOut(PacketContext context, PortNumber portNumber) { |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 529 | context.treatmentBuilder().setOutput(portNumber); |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 530 | context.send(); |
| 531 | } |
| 532 | |
| 533 | // Install a rule forwarding the packet to the specified port. |
| 534 | private void installRule(PacketContext context, PortNumber portNumber) { |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 535 | // |
| 536 | // We don't support (yet) buffer IDs in the Flow Service so |
| 537 | // packet out first. |
| 538 | // |
| 539 | Ethernet inPkt = context.inPacket().parsed(); |
Jonathan Hart | 3b881aa | 2015-04-22 18:03:50 -0700 | [diff] [blame] | 540 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 541 | |
| 542 | // If PacketOutOnly or ARP packet than forward directly to output port |
| 543 | if (packetOutOnly || inPkt.getEtherType() == Ethernet.TYPE_ARP) { |
| 544 | packetOut(context, portNumber); |
| 545 | return; |
| 546 | } |
| 547 | |
| 548 | // |
| 549 | // If matchDstMacOnly |
| 550 | // Create flows matching dstMac only |
| 551 | // Else |
| 552 | // Create flows with default matching and include configured fields |
| 553 | // |
| 554 | if (matchDstMacOnly) { |
Jonathan Hart | 3b881aa | 2015-04-22 18:03:50 -0700 | [diff] [blame] | 555 | selectorBuilder.matchEthDst(inPkt.getDestinationMAC()); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 556 | } else { |
Jonathan Hart | 3b881aa | 2015-04-22 18:03:50 -0700 | [diff] [blame] | 557 | selectorBuilder.matchInPort(context.inPacket().receivedFrom().port()) |
Thomas Vachuska | bd7f4b3 | 2014-12-04 20:54:55 -0800 | [diff] [blame] | 558 | .matchEthSrc(inPkt.getSourceMAC()) |
Jonathan Hart | 430223a | 2015-04-22 17:39:02 -0700 | [diff] [blame] | 559 | .matchEthDst(inPkt.getDestinationMAC()); |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 560 | |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 561 | // If configured Match Vlan ID |
| 562 | if (matchVlanId && inPkt.getVlanID() != Ethernet.VLAN_UNTAGGED) { |
Jonathan Hart | 3b881aa | 2015-04-22 18:03:50 -0700 | [diff] [blame] | 563 | selectorBuilder.matchVlanId(VlanId.vlanId(inPkt.getVlanID())); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 564 | } |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 565 | |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 566 | // |
| 567 | // If configured and EtherType is IPv4 - Match IPv4 and |
| 568 | // TCP/UDP/ICMP fields |
| 569 | // |
| 570 | if (matchIpv4Address && inPkt.getEtherType() == Ethernet.TYPE_IPV4) { |
| 571 | IPv4 ipv4Packet = (IPv4) inPkt.getPayload(); |
| 572 | byte ipv4Protocol = ipv4Packet.getProtocol(); |
| 573 | Ip4Prefix matchIp4SrcPrefix = |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 574 | Ip4Prefix.valueOf(ipv4Packet.getSourceAddress(), |
| 575 | Ip4Prefix.MAX_MASK_LENGTH); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 576 | Ip4Prefix matchIp4DstPrefix = |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 577 | Ip4Prefix.valueOf(ipv4Packet.getDestinationAddress(), |
| 578 | Ip4Prefix.MAX_MASK_LENGTH); |
Charles M.C. Chan | 7b8a921 | 2015-04-26 01:25:53 +0800 | [diff] [blame] | 579 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV4) |
Jonathan Hart | 430223a | 2015-04-22 17:39:02 -0700 | [diff] [blame] | 580 | .matchIPSrc(matchIp4SrcPrefix) |
| 581 | .matchIPDst(matchIp4DstPrefix); |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 582 | |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 583 | if (matchIpv4Dscp) { |
Pavlin Radoslavov | bf23c55 | 2015-02-20 14:20:30 -0800 | [diff] [blame] | 584 | byte dscp = ipv4Packet.getDscp(); |
| 585 | byte ecn = ipv4Packet.getEcn(); |
Jonathan Hart | 3b881aa | 2015-04-22 18:03:50 -0700 | [diff] [blame] | 586 | selectorBuilder.matchIPDscp(dscp).matchIPEcn(ecn); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | if (matchTcpUdpPorts && ipv4Protocol == IPv4.PROTOCOL_TCP) { |
| 590 | TCP tcpPacket = (TCP) ipv4Packet.getPayload(); |
Jonathan Hart | 3b881aa | 2015-04-22 18:03:50 -0700 | [diff] [blame] | 591 | selectorBuilder.matchIPProtocol(ipv4Protocol) |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 592 | .matchTcpSrc(TpPort.tpPort(tcpPacket.getSourcePort())) |
| 593 | .matchTcpDst(TpPort.tpPort(tcpPacket.getDestinationPort())); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 594 | } |
| 595 | if (matchTcpUdpPorts && ipv4Protocol == IPv4.PROTOCOL_UDP) { |
| 596 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
Jonathan Hart | 3b881aa | 2015-04-22 18:03:50 -0700 | [diff] [blame] | 597 | selectorBuilder.matchIPProtocol(ipv4Protocol) |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 598 | .matchUdpSrc(TpPort.tpPort(udpPacket.getSourcePort())) |
| 599 | .matchUdpDst(TpPort.tpPort(udpPacket.getDestinationPort())); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 600 | } |
| 601 | if (matchIcmpFields && ipv4Protocol == IPv4.PROTOCOL_ICMP) { |
| 602 | ICMP icmpPacket = (ICMP) ipv4Packet.getPayload(); |
Jonathan Hart | 3b881aa | 2015-04-22 18:03:50 -0700 | [diff] [blame] | 603 | selectorBuilder.matchIPProtocol(ipv4Protocol) |
Jonathan Hart | 430223a | 2015-04-22 17:39:02 -0700 | [diff] [blame] | 604 | .matchIcmpType(icmpPacket.getIcmpType()) |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 605 | .matchIcmpCode(icmpPacket.getIcmpCode()); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | // |
| 610 | // If configured and EtherType is IPv6 - Match IPv6 and |
| 611 | // TCP/UDP/ICMP fields |
| 612 | // |
| 613 | if (matchIpv6Address && inPkt.getEtherType() == Ethernet.TYPE_IPV6) { |
| 614 | IPv6 ipv6Packet = (IPv6) inPkt.getPayload(); |
| 615 | byte ipv6NextHeader = ipv6Packet.getNextHeader(); |
| 616 | Ip6Prefix matchIp6SrcPrefix = |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 617 | Ip6Prefix.valueOf(ipv6Packet.getSourceAddress(), |
| 618 | Ip6Prefix.MAX_MASK_LENGTH); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 619 | Ip6Prefix matchIp6DstPrefix = |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 620 | Ip6Prefix.valueOf(ipv6Packet.getDestinationAddress(), |
| 621 | Ip6Prefix.MAX_MASK_LENGTH); |
Charles M.C. Chan | 7b8a921 | 2015-04-26 01:25:53 +0800 | [diff] [blame] | 622 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV6) |
| 623 | .matchIPv6Src(matchIp6SrcPrefix) |
Jonathan Hart | 430223a | 2015-04-22 17:39:02 -0700 | [diff] [blame] | 624 | .matchIPv6Dst(matchIp6DstPrefix); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 625 | |
| 626 | if (matchIpv6FlowLabel) { |
Jonathan Hart | 3b881aa | 2015-04-22 18:03:50 -0700 | [diff] [blame] | 627 | selectorBuilder.matchIPv6FlowLabel(ipv6Packet.getFlowLabel()); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | if (matchTcpUdpPorts && ipv6NextHeader == IPv6.PROTOCOL_TCP) { |
| 631 | TCP tcpPacket = (TCP) ipv6Packet.getPayload(); |
Jonathan Hart | 3b881aa | 2015-04-22 18:03:50 -0700 | [diff] [blame] | 632 | selectorBuilder.matchIPProtocol(ipv6NextHeader) |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 633 | .matchTcpSrc(TpPort.tpPort(tcpPacket.getSourcePort())) |
| 634 | .matchTcpDst(TpPort.tpPort(tcpPacket.getDestinationPort())); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 635 | } |
| 636 | if (matchTcpUdpPorts && ipv6NextHeader == IPv6.PROTOCOL_UDP) { |
| 637 | UDP udpPacket = (UDP) ipv6Packet.getPayload(); |
Jonathan Hart | 3b881aa | 2015-04-22 18:03:50 -0700 | [diff] [blame] | 638 | selectorBuilder.matchIPProtocol(ipv6NextHeader) |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 639 | .matchUdpSrc(TpPort.tpPort(udpPacket.getSourcePort())) |
| 640 | .matchUdpDst(TpPort.tpPort(udpPacket.getDestinationPort())); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 641 | } |
| 642 | if (matchIcmpFields && ipv6NextHeader == IPv6.PROTOCOL_ICMP6) { |
| 643 | ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload(); |
Jonathan Hart | 3b881aa | 2015-04-22 18:03:50 -0700 | [diff] [blame] | 644 | selectorBuilder.matchIPProtocol(ipv6NextHeader) |
Jonathan Hart | 430223a | 2015-04-22 17:39:02 -0700 | [diff] [blame] | 645 | .matchIcmpv6Type(icmp6Packet.getIcmpType()) |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 646 | .matchIcmpv6Code(icmp6Packet.getIcmpCode()); |
| 647 | } |
| 648 | } |
| 649 | } |
Jonathan Hart | 3b881aa | 2015-04-22 18:03:50 -0700 | [diff] [blame] | 650 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 651 | .setOutput(portNumber) |
| 652 | .build(); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 653 | |
Jonathan Hart | 3b881aa | 2015-04-22 18:03:50 -0700 | [diff] [blame] | 654 | ForwardingObjective forwardingObjective = DefaultForwardingObjective.builder() |
| 655 | .withSelector(selectorBuilder.build()) |
| 656 | .withTreatment(treatment) |
| 657 | .withPriority(flowPriority) |
| 658 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 659 | .fromApp(appId) |
| 660 | .makeTemporary(flowTimeout) |
| 661 | .add(); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 662 | |
Jonathan Hart | 3b881aa | 2015-04-22 18:03:50 -0700 | [diff] [blame] | 663 | flowObjectiveService.forward(context.inPacket().receivedFrom().deviceId(), |
| 664 | forwardingObjective); |
Dusan Pajin | 0d1d48f | 2015-02-20 16:05:11 +0100 | [diff] [blame] | 665 | |
| 666 | // |
| 667 | // If packetOutOfppTable |
| 668 | // Send packet back to the OpenFlow pipeline to match installed flow |
| 669 | // Else |
| 670 | // Send packet direction on the appropriate port |
| 671 | // |
| 672 | if (packetOutOfppTable) { |
| 673 | packetOut(context, PortNumber.TABLE); |
| 674 | } else { |
| 675 | packetOut(context, portNumber); |
Thomas Vachuska | bd7f4b3 | 2014-12-04 20:54:55 -0800 | [diff] [blame] | 676 | } |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 677 | } |
Sahil Lele | a69534f | 2015-07-16 15:14:57 -0700 | [diff] [blame] | 678 | |
| 679 | private class InternalTopologyListener implements TopologyListener { |
| 680 | @Override |
| 681 | public void event(TopologyEvent event) { |
| 682 | List<Event> reasons = event.reasons(); |
| 683 | if (reasons != null) { |
| 684 | reasons.forEach(re -> { |
| 685 | if (re instanceof LinkEvent) { |
| 686 | LinkEvent le = (LinkEvent) re; |
| 687 | if (le.type() == LinkEvent.Type.LINK_REMOVED) { |
| 688 | fixBlackhole(le.subject().src()); |
| 689 | } |
| 690 | } |
| 691 | }); |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | private void fixBlackhole(ConnectPoint egress) { |
Thomas Vachuska | 8d03367 | 2015-07-21 16:15:04 -0700 | [diff] [blame] | 697 | Set<FlowEntry> rules = getFlowRulesFrom(egress); |
Sahil Lele | a69534f | 2015-07-16 15:14:57 -0700 | [diff] [blame] | 698 | Set<SrcDstPair> pairs = findSrcDstPairs(rules); |
| 699 | |
Sahil Lele | 7b96166 | 2015-07-20 15:06:04 -0700 | [diff] [blame] | 700 | Map<DeviceId, Set<Path>> srcPaths = new HashMap<>(); |
| 701 | |
Thomas Vachuska | 8d03367 | 2015-07-21 16:15:04 -0700 | [diff] [blame] | 702 | for (SrcDstPair sd : pairs) { |
Sahil Lele | a69534f | 2015-07-16 15:14:57 -0700 | [diff] [blame] | 703 | // get the edge deviceID for the src host |
Sahil Lele | 0f8d00e | 2015-07-24 11:05:43 -0700 | [diff] [blame] | 704 | Host srcHost = hostService.getHost(HostId.hostId(sd.src)); |
| 705 | Host dstHost = hostService.getHost(HostId.hostId(sd.dst)); |
| 706 | if (srcHost != null && dstHost != null) { |
| 707 | DeviceId srcId = srcHost.location().deviceId(); |
| 708 | DeviceId dstId = dstHost.location().deviceId(); |
| 709 | log.trace("SRC ID is " + srcId + ", DST ID is " + dstId); |
Sahil Lele | a69534f | 2015-07-16 15:14:57 -0700 | [diff] [blame] | 710 | |
Sahil Lele | 0f8d00e | 2015-07-24 11:05:43 -0700 | [diff] [blame] | 711 | cleanFlowRules(sd, egress.deviceId()); |
Sahil Lele | a69534f | 2015-07-16 15:14:57 -0700 | [diff] [blame] | 712 | |
Sahil Lele | 0f8d00e | 2015-07-24 11:05:43 -0700 | [diff] [blame] | 713 | Set<Path> shortestPaths = srcPaths.get(srcId); |
| 714 | if (shortestPaths == null) { |
| 715 | shortestPaths = topologyService.getPaths(topologyService.currentTopology(), |
| 716 | egress.deviceId(), srcId); |
| 717 | srcPaths.put(srcId, shortestPaths); |
| 718 | } |
| 719 | backTrackBadNodes(shortestPaths, dstId, sd); |
Sahil Lele | 7b96166 | 2015-07-20 15:06:04 -0700 | [diff] [blame] | 720 | } |
Sahil Lele | a69534f | 2015-07-16 15:14:57 -0700 | [diff] [blame] | 721 | } |
| 722 | } |
| 723 | |
| 724 | // Backtracks from link down event to remove flows that lead to blackhole |
| 725 | private void backTrackBadNodes(Set<Path> shortestPaths, DeviceId dstId, SrcDstPair sd) { |
Thomas Vachuska | 8d03367 | 2015-07-21 16:15:04 -0700 | [diff] [blame] | 726 | for (Path p : shortestPaths) { |
Sahil Lele | a69534f | 2015-07-16 15:14:57 -0700 | [diff] [blame] | 727 | List<Link> pathLinks = p.links(); |
| 728 | for (int i = 0; i < pathLinks.size(); i = i + 1) { |
| 729 | Link curLink = pathLinks.get(i); |
| 730 | DeviceId curDevice = curLink.src().deviceId(); |
Sahil Lele | a69534f | 2015-07-16 15:14:57 -0700 | [diff] [blame] | 731 | |
| 732 | // skipping the first link because this link's src has already been pruned beforehand |
| 733 | if (i != 0) { |
| 734 | cleanFlowRules(sd, curDevice); |
| 735 | } |
| 736 | |
Thomas Vachuska | 8d03367 | 2015-07-21 16:15:04 -0700 | [diff] [blame] | 737 | Set<Path> pathsFromCurDevice = |
| 738 | topologyService.getPaths(topologyService.currentTopology(), |
| 739 | curDevice, dstId); |
Thomas Vachuska | 320c58f | 2015-08-05 10:42:32 -0700 | [diff] [blame] | 740 | if (pickForwardPathIfPossible(pathsFromCurDevice, curLink.src().port()) != null) { |
Sahil Lele | a69534f | 2015-07-16 15:14:57 -0700 | [diff] [blame] | 741 | break; |
| 742 | } else { |
| 743 | if (i + 1 == pathLinks.size()) { |
| 744 | cleanFlowRules(sd, curLink.dst().deviceId()); |
| 745 | } |
| 746 | } |
| 747 | } |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | // Removes flow rules off specified device with specific SrcDstPair |
| 752 | private void cleanFlowRules(SrcDstPair pair, DeviceId id) { |
| 753 | log.trace("Searching for flow rules to remove from: " + id); |
| 754 | log.trace("Removing flows w/ SRC=" + pair.src + ", DST=" + pair.dst); |
| 755 | for (FlowEntry r : flowRuleService.getFlowEntries(id)) { |
| 756 | boolean matchesSrc = false, matchesDst = false; |
| 757 | for (Instruction i : r.treatment().allInstructions()) { |
| 758 | if (i.type() == Instruction.Type.OUTPUT) { |
Thomas Vachuska | 8d03367 | 2015-07-21 16:15:04 -0700 | [diff] [blame] | 759 | // if the flow has matching src and dst |
Sahil Lele | a69534f | 2015-07-16 15:14:57 -0700 | [diff] [blame] | 760 | for (Criterion cr : r.selector().criteria()) { |
| 761 | if (cr.type() == Criterion.Type.ETH_DST) { |
| 762 | if (((EthCriterion) cr).mac().equals(pair.dst)) { |
| 763 | matchesDst = true; |
| 764 | } |
| 765 | } else if (cr.type() == Criterion.Type.ETH_SRC) { |
| 766 | if (((EthCriterion) cr).mac().equals(pair.src)) { |
| 767 | matchesSrc = true; |
| 768 | } |
| 769 | } |
| 770 | } |
| 771 | } |
| 772 | } |
| 773 | if (matchesDst && matchesSrc) { |
| 774 | log.trace("Removed flow rule from device: " + id); |
| 775 | flowRuleService.removeFlowRules((FlowRule) r); |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | } |
| 780 | |
| 781 | // Returns a set of src/dst MAC pairs extracted from the specified set of flow entries |
| 782 | private Set<SrcDstPair> findSrcDstPairs(Set<FlowEntry> rules) { |
| 783 | ImmutableSet.Builder<SrcDstPair> builder = ImmutableSet.builder(); |
Thomas Vachuska | 8d03367 | 2015-07-21 16:15:04 -0700 | [diff] [blame] | 784 | for (FlowEntry r : rules) { |
Sahil Lele | a69534f | 2015-07-16 15:14:57 -0700 | [diff] [blame] | 785 | MacAddress src = null, dst = null; |
Thomas Vachuska | 8d03367 | 2015-07-21 16:15:04 -0700 | [diff] [blame] | 786 | for (Criterion cr : r.selector().criteria()) { |
Sahil Lele | a69534f | 2015-07-16 15:14:57 -0700 | [diff] [blame] | 787 | if (cr.type() == Criterion.Type.ETH_DST) { |
| 788 | dst = ((EthCriterion) cr).mac(); |
| 789 | } else if (cr.type() == Criterion.Type.ETH_SRC) { |
| 790 | src = ((EthCriterion) cr).mac(); |
| 791 | } |
| 792 | } |
| 793 | builder.add(new SrcDstPair(src, dst)); |
| 794 | } |
| 795 | return builder.build(); |
| 796 | } |
| 797 | |
Thomas Vachuska | 320c58f | 2015-08-05 10:42:32 -0700 | [diff] [blame] | 798 | // Returns set of flow entries which were created by this application and |
| 799 | // which egress from the specified connection port |
Sahil Lele | a69534f | 2015-07-16 15:14:57 -0700 | [diff] [blame] | 800 | private Set<FlowEntry> getFlowRulesFrom(ConnectPoint egress) { |
| 801 | ImmutableSet.Builder<FlowEntry> builder = ImmutableSet.builder(); |
| 802 | flowRuleService.getFlowEntries(egress.deviceId()).forEach(r -> { |
| 803 | if (r.appId() == appId.id()) { |
| 804 | r.treatment().allInstructions().forEach(i -> { |
| 805 | if (i.type() == Instruction.Type.OUTPUT) { |
| 806 | if (((Instructions.OutputInstruction) i).port().equals(egress.port())) { |
| 807 | builder.add(r); |
| 808 | } |
| 809 | } |
| 810 | }); |
| 811 | } |
| 812 | }); |
| 813 | |
| 814 | return builder.build(); |
| 815 | } |
| 816 | |
| 817 | // Wrapper class for a source and destination pair of MAC addresses |
| 818 | private final class SrcDstPair { |
| 819 | final MacAddress src; |
| 820 | final MacAddress dst; |
| 821 | |
| 822 | private SrcDstPair(MacAddress src, MacAddress dst) { |
| 823 | this.src = src; |
| 824 | this.dst = dst; |
| 825 | } |
| 826 | |
| 827 | @Override |
| 828 | public boolean equals(Object o) { |
| 829 | if (this == o) { |
| 830 | return true; |
| 831 | } |
| 832 | if (o == null || getClass() != o.getClass()) { |
| 833 | return false; |
| 834 | } |
| 835 | SrcDstPair that = (SrcDstPair) o; |
| 836 | return Objects.equals(src, that.src) && |
| 837 | Objects.equals(dst, that.dst); |
| 838 | } |
| 839 | |
| 840 | @Override |
| 841 | public int hashCode() { |
| 842 | return Objects.hash(src, dst); |
| 843 | } |
| 844 | } |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 845 | } |