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