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