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