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