Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 16 | package org.onosproject.driver.pipeline.ofdpa; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 17 | |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
pier | 5b9b9e3 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Yi Tseng | 47f82dc | 2017-03-05 22:48:39 -0800 | [diff] [blame] | 20 | import com.google.common.collect.Sets; |
pier | 5b9b9e3 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 21 | import org.apache.commons.lang3.tuple.Pair; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 22 | import org.onlab.osgi.ServiceDirectory; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 23 | import org.onlab.packet.Ethernet; |
Alex Yashchuk | a367953 | 2017-12-08 17:40:05 +0200 | [diff] [blame] | 24 | import org.onlab.packet.EthType.EtherType; |
Julia Ferguson | 65428c3 | 2017-08-10 18:15:24 +0000 | [diff] [blame] | 25 | import org.onlab.packet.IpAddress; |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 26 | import org.onlab.packet.IpPrefix; |
Charles Chan | d57552d | 2018-03-02 15:41:41 -0800 | [diff] [blame] | 27 | import org.onlab.packet.MacAddress; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 28 | import org.onlab.packet.VlanId; |
pier | 5b9b9e3 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 29 | import org.onlab.util.AbstractAccumulator; |
| 30 | import org.onlab.util.Accumulator; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 31 | import org.onlab.util.KryoNamespace; |
| 32 | import org.onosproject.core.ApplicationId; |
| 33 | import org.onosproject.core.CoreService; |
Charles Chan | cad338a | 2016-09-16 18:03:11 -0700 | [diff] [blame] | 34 | import org.onosproject.driver.extensions.Ofdpa3MplsType; |
| 35 | import org.onosproject.driver.extensions.Ofdpa3SetMplsType; |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 36 | import org.onosproject.driver.extensions.OfdpaMatchVlanVid; |
| 37 | import org.onosproject.driver.extensions.OfdpaSetVlanVid; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 38 | import org.onosproject.net.DeviceId; |
| 39 | import org.onosproject.net.Port; |
| 40 | import org.onosproject.net.PortNumber; |
| 41 | import org.onosproject.net.behaviour.NextGroup; |
| 42 | import org.onosproject.net.behaviour.Pipeliner; |
| 43 | import org.onosproject.net.behaviour.PipelinerContext; |
| 44 | import org.onosproject.net.device.DeviceService; |
| 45 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 46 | import org.onosproject.net.flow.DefaultFlowRule; |
| 47 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 48 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 49 | import org.onosproject.net.flow.FlowRule; |
| 50 | import org.onosproject.net.flow.FlowRuleOperations; |
| 51 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 52 | import org.onosproject.net.flow.FlowRuleService; |
| 53 | import org.onosproject.net.flow.TrafficSelector; |
| 54 | import org.onosproject.net.flow.TrafficTreatment; |
| 55 | import org.onosproject.net.flow.criteria.Criteria; |
| 56 | import org.onosproject.net.flow.criteria.Criterion; |
| 57 | import org.onosproject.net.flow.criteria.EthCriterion; |
| 58 | import org.onosproject.net.flow.criteria.EthTypeCriterion; |
| 59 | import org.onosproject.net.flow.criteria.IPCriterion; |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 60 | import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion; |
| 61 | import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 62 | import org.onosproject.net.flow.criteria.MplsBosCriterion; |
| 63 | import org.onosproject.net.flow.criteria.MplsCriterion; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 64 | import org.onosproject.net.flow.criteria.PortCriterion; |
Charles Chan | d9e47c6 | 2017-10-05 15:17:15 -0700 | [diff] [blame] | 65 | import org.onosproject.net.flow.criteria.TcpPortCriterion; |
| 66 | import org.onosproject.net.flow.criteria.UdpPortCriterion; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 67 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
| 68 | import org.onosproject.net.flow.instructions.Instruction; |
| 69 | import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
Charles Chan | 3fe7171 | 2018-06-15 18:54:18 -0700 | [diff] [blame] | 70 | import org.onosproject.net.flow.instructions.Instructions.NoActionInstruction; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 71 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 72 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType; |
Alex Yashchuk | a367953 | 2017-12-08 17:40:05 +0200 | [diff] [blame] | 73 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsHeaderInstruction; |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 74 | import org.onosproject.net.flow.instructions.L3ModificationInstruction; |
| 75 | import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 76 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 77 | import org.onosproject.net.flowobjective.FlowObjectiveStore; |
| 78 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 79 | import org.onosproject.net.flowobjective.NextObjective; |
| 80 | import org.onosproject.net.flowobjective.Objective; |
| 81 | import org.onosproject.net.flowobjective.ObjectiveError; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 82 | import org.onosproject.net.group.DefaultGroupKey; |
| 83 | import org.onosproject.net.group.Group; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 84 | import org.onosproject.net.group.GroupKey; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 85 | import org.onosproject.net.group.GroupService; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 86 | import org.onosproject.store.serializers.KryoNamespaces; |
| 87 | import org.slf4j.Logger; |
| 88 | |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 89 | import java.util.ArrayDeque; |
| 90 | import java.util.ArrayList; |
| 91 | import java.util.Collection; |
| 92 | import java.util.Collections; |
| 93 | import java.util.Deque; |
| 94 | import java.util.List; |
| 95 | import java.util.Objects; |
pier | 5b9b9e3 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 96 | import java.util.Timer; |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 97 | import java.util.concurrent.ScheduledExecutorService; |
| 98 | import java.util.concurrent.TimeUnit; |
| 99 | |
| 100 | import static java.util.concurrent.Executors.newScheduledThreadPool; |
pier | 5b9b9e3 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 101 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
Yi Tseng | 3a77b4f | 2017-02-06 15:02:17 -0800 | [diff] [blame] | 102 | import static org.onlab.packet.MacAddress.BROADCAST; |
Charles Chan | b4879a5 | 2017-10-20 19:09:16 -0700 | [diff] [blame] | 103 | import static org.onlab.packet.MacAddress.IPV4_MULTICAST; |
| 104 | import static org.onlab.packet.MacAddress.IPV6_MULTICAST; |
Yi Tseng | 3a77b4f | 2017-02-06 15:02:17 -0800 | [diff] [blame] | 105 | import static org.onlab.packet.MacAddress.NONE; |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 106 | import static org.onlab.util.Tools.groupedThreads; |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 107 | import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*; |
pier | 5c81758 | 2019-04-17 17:05:08 +0200 | [diff] [blame] | 108 | import static org.onosproject.driver.pipeline.ofdpa.OfdpaPipelineUtility.*; |
pier | 4baa8cc | 2019-05-10 13:19:15 +0200 | [diff] [blame] | 109 | import static org.onosproject.net.flowobjective.ForwardingObjective.Flag.SPECIFIC; |
Pier Luigi | 5af88fe | 2018-02-01 10:23:12 +0100 | [diff] [blame] | 110 | import static org.onosproject.net.group.GroupDescription.Type.SELECT; |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 111 | import static org.slf4j.LoggerFactory.getLogger; |
Pier Ventre | 140a894 | 2016-11-02 07:26:38 -0700 | [diff] [blame] | 112 | import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS; |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 113 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 114 | /** |
| 115 | * Driver for Broadcom's OF-DPA v2.0 TTP. |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 116 | */ |
Charles Chan | 361154b | 2016-03-24 10:23:39 -0700 | [diff] [blame] | 117 | public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner { |
pier | 5b9b9e3 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 118 | // Timer for the accumulator |
| 119 | private static final Timer TIMER = new Timer("fwdobj-batching"); |
| 120 | private Accumulator<Pair<ForwardingObjective, Collection<FlowRule>>> accumulator; |
pier | 5c81758 | 2019-04-17 17:05:08 +0200 | [diff] [blame] | 121 | // Internal objects |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 122 | private final Logger log = getLogger(getClass()); |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 123 | protected ServiceDirectory serviceDirectory; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 124 | protected FlowRuleService flowRuleService; |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 125 | protected CoreService coreService; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 126 | protected GroupService groupService; |
| 127 | protected FlowObjectiveStore flowObjectiveStore; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 128 | protected DeviceId deviceId; |
| 129 | protected ApplicationId driverId; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 130 | protected DeviceService deviceService; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 131 | protected static KryoNamespace appKryo = new KryoNamespace.Builder() |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 132 | .register(KryoNamespaces.API) |
| 133 | .register(GroupKey.class) |
| 134 | .register(DefaultGroupKey.class) |
| 135 | .register(OfdpaNextGroup.class) |
| 136 | .register(ArrayDeque.class) |
| 137 | .build("Ofdpa2Pipeline"); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 138 | |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 139 | protected Ofdpa2GroupHandler groupHandler; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 140 | |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 141 | // flows installations to be retried |
pier | 5b9b9e3 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 142 | private ScheduledExecutorService retryExecutorService |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 143 | = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log)); |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 144 | |
pier | 5b9b9e3 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 145 | // accumulator executor service |
| 146 | private ScheduledExecutorService accumulatorExecutorService |
| 147 | = newSingleThreadScheduledExecutor(groupedThreads("OfdpaPipeliner", "acc-%d", log)); |
| 148 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 149 | @Override |
| 150 | public void init(DeviceId deviceId, PipelinerContext context) { |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 151 | this.deviceId = deviceId; |
| 152 | |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 153 | serviceDirectory = context.directory(); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 154 | coreService = serviceDirectory.get(CoreService.class); |
| 155 | flowRuleService = serviceDirectory.get(FlowRuleService.class); |
| 156 | groupService = serviceDirectory.get(GroupService.class); |
| 157 | flowObjectiveStore = context.store(); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 158 | deviceService = serviceDirectory.get(DeviceService.class); |
pier | 5b9b9e3 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 159 | // Init the accumulator, if enabled |
pier | 5c81758 | 2019-04-17 17:05:08 +0200 | [diff] [blame] | 160 | if (isAccumulatorEnabled(this)) { |
pier | 5b9b9e3 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 161 | accumulator = new ForwardingObjectiveAccumulator(context.accumulatorMaxObjectives(), |
| 162 | context.accumulatorMaxBatchMillis(), |
| 163 | context.accumulatorMaxIdleMillis()); |
| 164 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 165 | |
Charles Chan | 40132b3 | 2017-01-22 00:19:37 -0800 | [diff] [blame] | 166 | initDriverId(); |
| 167 | initGroupHander(context); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 168 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 169 | initializePipeline(); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 170 | } |
| 171 | |
pier | 4baa8cc | 2019-05-10 13:19:15 +0200 | [diff] [blame] | 172 | void setupAccumulatorForTests(int maxFwd, int maxBatchMS, int maxIdleMS) { |
| 173 | if (accumulator == null) { |
| 174 | accumulator = new ForwardingObjectiveAccumulator(maxFwd, |
| 175 | maxBatchMS, |
| 176 | maxIdleMS); |
| 177 | } |
| 178 | } |
| 179 | |
Charles Chan | 40132b3 | 2017-01-22 00:19:37 -0800 | [diff] [blame] | 180 | protected void initDriverId() { |
| 181 | driverId = coreService.registerApplication( |
| 182 | "org.onosproject.driver.Ofdpa2Pipeline"); |
| 183 | } |
| 184 | |
| 185 | protected void initGroupHander(PipelinerContext context) { |
| 186 | groupHandler = new Ofdpa2GroupHandler(); |
| 187 | groupHandler.init(deviceId, context); |
| 188 | } |
| 189 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 190 | protected void initializePipeline() { |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 191 | // OF-DPA does not require initializing the pipeline as it puts default |
| 192 | // rules automatically in the hardware. However emulation of OFDPA in |
| 193 | // software switches does require table-miss-entries. |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Charles Chan | d117263 | 2017-03-15 17:33:09 -0700 | [diff] [blame] | 196 | /** |
Alex Yashchuk | a367953 | 2017-12-08 17:40:05 +0200 | [diff] [blame] | 197 | * Determines whether this pipeline requires MPLS POP instruction. |
| 198 | * |
| 199 | * @return true to use MPLS POP instruction |
| 200 | */ |
| 201 | public boolean requireMplsPop() { |
| 202 | return true; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Determines whether this pipeline requires MPLS BOS match. |
| 207 | * |
| 208 | * @return true to use MPLS BOS match |
| 209 | */ |
| 210 | public boolean requireMplsBosMatch() { |
| 211 | return true; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Determines whether this pipeline requires MPLS TTL decrement and copy. |
| 216 | * |
| 217 | * @return true to use MPLS TTL decrement and copy |
| 218 | */ |
| 219 | public boolean requireMplsTtlModification() { |
| 220 | return true; |
| 221 | } |
| 222 | |
| 223 | /** |
Charles Chan | d117263 | 2017-03-15 17:33:09 -0700 | [diff] [blame] | 224 | * Determines whether this pipeline requires OFDPA match and set VLAN extensions. |
| 225 | * |
| 226 | * @return true to use the extensions |
| 227 | */ |
| 228 | protected boolean requireVlanExtensions() { |
| 229 | return true; |
| 230 | } |
| 231 | |
Saurav Das | 86d13e8 | 2017-04-28 17:03:48 -0700 | [diff] [blame] | 232 | /** |
| 233 | * Determines whether in-port should be matched on in TMAC table rules. |
| 234 | * |
| 235 | * @return true if match on in-port should be programmed |
| 236 | */ |
| 237 | protected boolean matchInPortTmacTable() { |
| 238 | return true; |
| 239 | } |
| 240 | |
Charles Chan | d9e47c6 | 2017-10-05 15:17:15 -0700 | [diff] [blame] | 241 | /** |
| 242 | * Determines whether matching L4 destination port on IPv6 packets is supported in ACL table. |
| 243 | * |
| 244 | * @return true if matching L4 destination port on IPv6 packets is supported in ACL table. |
| 245 | */ |
| 246 | protected boolean supportIpv6L4Dst() { |
| 247 | return true; |
| 248 | } |
| 249 | |
Saurav Das | f3f7594 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 250 | /** |
| 251 | * Determines whether this driver should continue to retry flows that point |
| 252 | * to empty groups. See CORD-554. |
| 253 | * |
| 254 | * @return true if the driver should retry flows |
| 255 | */ |
| 256 | protected boolean shouldRetry() { |
| 257 | return true; |
| 258 | } |
| 259 | |
Charles Chan | d57552d | 2018-03-02 15:41:41 -0800 | [diff] [blame] | 260 | /** |
| 261 | * Determines whether this driver requires unicast flow to be installed before multicast flow |
| 262 | * in TMAC table. |
| 263 | * |
| 264 | * @return true if required |
| 265 | */ |
| 266 | protected boolean requireUnicastBeforeMulticast() { |
| 267 | return false; |
| 268 | } |
| 269 | |
Andrea Campanella | 84ac4df | 2018-04-30 11:48:55 +0200 | [diff] [blame] | 270 | /** |
| 271 | * Determines whether this driver supports installing a clearDeferred action on table 30. |
| 272 | * |
| 273 | * @return true if required |
| 274 | */ |
| 275 | protected boolean supportsUnicastBlackHole() { |
| 276 | return true; |
| 277 | } |
| 278 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 279 | ////////////////////////////////////// |
| 280 | // Flow Objectives |
| 281 | ////////////////////////////////////// |
| 282 | |
| 283 | @Override |
| 284 | public void filter(FilteringObjective filteringObjective) { |
| 285 | if (filteringObjective.type() == FilteringObjective.Type.PERMIT) { |
| 286 | processFilter(filteringObjective, |
| 287 | filteringObjective.op() == Objective.Operation.ADD, |
| 288 | filteringObjective.appId()); |
| 289 | } else { |
| 290 | // Note that packets that don't match the PERMIT filter are |
| 291 | // automatically denied. The DENY filter is used to deny packets |
| 292 | // that are otherwise permitted by the PERMIT filter. |
| 293 | // Use ACL table flow rules here for DENY filtering objectives |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 294 | log.warn("filter objective other than PERMIT currently not supported"); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 295 | fail(filteringObjective, ObjectiveError.UNSUPPORTED); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | @Override |
| 300 | public void forward(ForwardingObjective fwd) { |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 301 | Collection<FlowRule> rules = processForward(fwd); |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 302 | if (rules == null || rules.isEmpty()) { |
| 303 | // Assumes fail message has already been generated to the objective |
| 304 | // context. Returning here prevents spurious pass message to be |
| 305 | // generated by FlowRule service for empty flowOps. |
| 306 | return; |
| 307 | } |
pier | 4baa8cc | 2019-05-10 13:19:15 +0200 | [diff] [blame] | 308 | // Let's accumulate flow rules if accumulator is active and fwd objective is not versatile. |
| 309 | // Otherwise send directly, without adding futher delay |
| 310 | if (accumulator != null && Objects.equals(fwd.flag(), SPECIFIC)) { |
pier | 5b9b9e3 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 311 | accumulator.add(Pair.of(fwd, rules)); |
| 312 | } else { |
| 313 | sendForwards(Collections.singletonList(Pair.of(fwd, rules))); |
| 314 | } |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 315 | } |
| 316 | |
pier | 5b9b9e3 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 317 | // Builds the batch using the accumulated flow rules |
| 318 | private void sendForwards(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) { |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 319 | FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder(); |
pier | 5b9b9e3 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 320 | log.debug("Sending {} fwd-objs", pairs.size()); |
| 321 | List<Objective> fwdObjs = Lists.newArrayList(); |
| 322 | // Iterates over all accumulated flow rules and then build an unique batch |
| 323 | pairs.forEach(pair -> { |
| 324 | ForwardingObjective fwd = pair.getLeft(); |
| 325 | Collection<FlowRule> rules = pair.getRight(); |
| 326 | switch (fwd.op()) { |
| 327 | case ADD: |
| 328 | rules.stream() |
| 329 | .filter(Objects::nonNull) |
| 330 | .forEach(flowOpsBuilder::add); |
| 331 | log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId); |
| 332 | fwdObjs.add(fwd); |
| 333 | break; |
| 334 | case REMOVE: |
| 335 | rules.stream() |
| 336 | .filter(Objects::nonNull) |
| 337 | .forEach(flowOpsBuilder::remove); |
| 338 | log.debug("Deleting a flow rule to sw:{}", deviceId); |
| 339 | fwdObjs.add(fwd); |
| 340 | break; |
| 341 | default: |
| 342 | fail(fwd, ObjectiveError.UNKNOWN); |
| 343 | log.warn("Unknown forwarding type {}", fwd.op()); |
| 344 | } |
| 345 | }); |
| 346 | // Finally applies the operations |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 347 | flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() { |
pier | 5b9b9e3 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 348 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 349 | @Override |
| 350 | public void onSuccess(FlowRuleOperations ops) { |
pier | 5b9b9e3 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 351 | log.trace("Flow rule operations onSuccess {}", ops); |
pier | 5c81758 | 2019-04-17 17:05:08 +0200 | [diff] [blame] | 352 | fwdObjs.forEach(OfdpaPipelineUtility::pass); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | @Override |
| 356 | public void onError(FlowRuleOperations ops) { |
pier | 5b9b9e3 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 357 | ObjectiveError error = ObjectiveError.FLOWINSTALLATIONFAILED; |
| 358 | log.warn("Flow rule operations onError {}. Reason = {}", ops, error); |
| 359 | fwdObjs.forEach(fwdObj -> fail(fwdObj, error)); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 360 | } |
| 361 | })); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | @Override |
| 365 | public void next(NextObjective nextObjective) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 366 | NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id()); |
| 367 | switch (nextObjective.op()) { |
| 368 | case ADD: |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 369 | if (nextGroup != null) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 370 | log.warn("Cannot add next {} that already exists in device {}", |
| 371 | nextObjective.id(), deviceId); |
| 372 | return; |
| 373 | } |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 374 | log.debug("Processing NextObjective id {} in dev {} - add group", |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 375 | nextObjective.id(), deviceId); |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 376 | groupHandler.addGroup(nextObjective); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 377 | break; |
| 378 | case ADD_TO_EXISTING: |
| 379 | if (nextGroup != null) { |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 380 | log.debug("Processing NextObjective id {} in dev {} - add bucket", |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 381 | nextObjective.id(), deviceId); |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 382 | groupHandler.addBucketToGroup(nextObjective, nextGroup); |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 383 | } else { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 384 | // it is possible that group-chain has not been fully created yet |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 385 | log.debug("Waiting to add bucket to group for next-id:{} in dev:{}", |
| 386 | nextObjective.id(), deviceId); |
Yi Tseng | 47f82dc | 2017-03-05 22:48:39 -0800 | [diff] [blame] | 387 | |
| 388 | // by design multiple pending bucket is allowed for the group |
| 389 | groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> { |
| 390 | if (pendBkts == null) { |
| 391 | pendBkts = Sets.newHashSet(); |
| 392 | } |
| 393 | pendBkts.add(nextObjective); |
| 394 | return pendBkts; |
| 395 | }); |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 396 | } |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 397 | break; |
| 398 | case REMOVE: |
| 399 | if (nextGroup == null) { |
| 400 | log.warn("Cannot remove next {} that does not exist in device {}", |
| 401 | nextObjective.id(), deviceId); |
| 402 | return; |
| 403 | } |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 404 | log.debug("Processing NextObjective id {} in dev {} - remove group", |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 405 | nextObjective.id(), deviceId); |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 406 | groupHandler.removeGroup(nextObjective, nextGroup); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 407 | break; |
| 408 | case REMOVE_FROM_EXISTING: |
| 409 | if (nextGroup == null) { |
| 410 | log.warn("Cannot remove from next {} that does not exist in device {}", |
| 411 | nextObjective.id(), deviceId); |
| 412 | return; |
| 413 | } |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 414 | log.debug("Processing NextObjective id {} in dev {} - remove bucket", |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 415 | nextObjective.id(), deviceId); |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 416 | groupHandler.removeBucketFromGroup(nextObjective, nextGroup); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 417 | break; |
Jonghwan Hyun | 25199f2 | 2018-02-12 16:43:45 +0900 | [diff] [blame] | 418 | case MODIFY: |
| 419 | if (nextGroup == null) { |
| 420 | log.warn("Cannot modify next {} that does not exist in device {}", |
| 421 | nextObjective.id(), deviceId); |
| 422 | return; |
| 423 | } |
Ruchi Sahota | f77dc0f | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 424 | log.debug("Processing NextObjective id {} in dev {} group {} - modify bucket", |
| 425 | nextObjective.id(), deviceId, nextGroup); |
Jonghwan Hyun | 25199f2 | 2018-02-12 16:43:45 +0900 | [diff] [blame] | 426 | groupHandler.modifyBucketFromGroup(nextObjective, nextGroup); |
| 427 | break; |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 428 | case VERIFY: |
| 429 | if (nextGroup == null) { |
| 430 | log.warn("Cannot verify next {} that does not exist in device {}", |
| 431 | nextObjective.id(), deviceId); |
| 432 | return; |
| 433 | } |
| 434 | log.debug("Processing NextObjective id {} in dev {} - verify", |
| 435 | nextObjective.id(), deviceId); |
| 436 | groupHandler.verifyGroup(nextObjective, nextGroup); |
| 437 | break; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 438 | default: |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 439 | log.warn("Unsupported operation {}", nextObjective.op()); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | |
| 443 | ////////////////////////////////////// |
| 444 | // Flow handling |
| 445 | ////////////////////////////////////// |
| 446 | |
| 447 | /** |
Yi Tseng | 0fdc07e | 2017-04-24 19:17:08 -0700 | [diff] [blame] | 448 | * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing) |
| 449 | * configured on switch ports happen in different tables. |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 450 | * |
| 451 | * @param filt the filtering objective |
| 452 | * @param install indicates whether to add or remove the objective |
| 453 | * @param applicationId the application that sent this objective |
| 454 | */ |
Saurav Das | 5202596 | 2016-01-28 22:30:01 -0800 | [diff] [blame] | 455 | protected void processFilter(FilteringObjective filt, |
| 456 | boolean install, ApplicationId applicationId) { |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 457 | // This driver only processes filtering criteria defined with switch |
| 458 | // ports as the key |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 459 | PortCriterion portCriterion = null; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 460 | EthCriterion ethCriterion = null; |
| 461 | VlanIdCriterion vidCriterion = null; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 462 | if (!filt.key().equals(Criteria.dummy()) && |
| 463 | filt.key().type() == Criterion.Type.IN_PORT) { |
| 464 | portCriterion = (PortCriterion) filt.key(); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 465 | } |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 466 | if (portCriterion == null) { |
| 467 | log.debug("No IN_PORT defined in filtering objective from app: {}", |
| 468 | applicationId); |
| 469 | } else { |
| 470 | log.debug("Received filtering objective for dev/port: {}/{}", deviceId, |
| 471 | portCriterion.port()); |
| 472 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 473 | // convert filtering conditions for switch-intfs into flowrules |
| 474 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 475 | for (Criterion criterion : filt.conditions()) { |
Yi Tseng | 0fdc07e | 2017-04-24 19:17:08 -0700 | [diff] [blame] | 476 | switch (criterion.type()) { |
| 477 | case ETH_DST: |
| 478 | case ETH_DST_MASKED: |
| 479 | ethCriterion = (EthCriterion) criterion; |
| 480 | break; |
| 481 | case VLAN_VID: |
| 482 | vidCriterion = (VlanIdCriterion) criterion; |
| 483 | break; |
| 484 | default: |
| 485 | log.warn("Unsupported filter {}", criterion); |
| 486 | fail(filt, ObjectiveError.UNSUPPORTED); |
| 487 | return; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 491 | VlanId assignedVlan = null; |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 492 | if (vidCriterion != null) { |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 493 | // Use the VLAN in criterion if metadata is not present and the traffic is tagged |
Charles Chan | 9174e6b | 2017-12-19 19:55:57 -0800 | [diff] [blame] | 494 | if (!vidCriterion.vlanId().equals(VlanId.NONE)) { |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 495 | assignedVlan = vidCriterion.vlanId(); |
Pier | 3cc7e66 | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 496 | } |
| 497 | // If the meta VLAN is present let's update the assigned vlan |
| 498 | if (filt.meta() != null) { |
| 499 | VlanId metaVlan = readVlanFromTreatment(filt.meta()); |
| 500 | if (metaVlan != null) { |
| 501 | assignedVlan = metaVlan; |
| 502 | } |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 503 | } |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 504 | |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 505 | if (assignedVlan == null) { |
| 506 | log.error("Driver fails to extract VLAN information. " |
Frank Wang | d8ab096 | 2017-08-11 11:09:30 +0800 | [diff] [blame] | 507 | + "Not processing VLAN filters on device {}.", deviceId); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 508 | log.debug("VLAN ID in criterion={}, metadata={}", |
| 509 | readVlanFromTreatment(filt.meta()), vidCriterion.vlanId()); |
| 510 | fail(filt, ObjectiveError.BADPARAMS); |
| 511 | return; |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | |
Yi Tseng | 3a77b4f | 2017-02-06 15:02:17 -0800 | [diff] [blame] | 515 | if (ethCriterion == null || ethCriterion.mac().equals(NONE)) { |
Charles Chan | 985b12e | 2016-05-11 19:47:22 -0700 | [diff] [blame] | 516 | // NOTE: it is possible that a filtering objective only has vidCriterion |
Daniel Ginsburg | 16e5ed4 | 2018-04-30 19:27:19 -0400 | [diff] [blame] | 517 | log.debug("filtering objective missing dstMac, won't program TMAC table"); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 518 | } else { |
Charles Chan | d57552d | 2018-03-02 15:41:41 -0800 | [diff] [blame] | 519 | MacAddress unicastMac = readEthDstFromTreatment(filt.meta()); |
Charles Chan | 206506b | 2018-03-02 16:43:28 -0800 | [diff] [blame] | 520 | List<List<FlowRule>> allStages = processEthDstFilter(portCriterion, ethCriterion, |
Charles Chan | d57552d | 2018-03-02 15:41:41 -0800 | [diff] [blame] | 521 | vidCriterion, assignedVlan, unicastMac, applicationId); |
Charles Chan | 206506b | 2018-03-02 16:43:28 -0800 | [diff] [blame] | 522 | for (List<FlowRule> flowRules : allStages) { |
Charles Chan | d57552d | 2018-03-02 15:41:41 -0800 | [diff] [blame] | 523 | log.trace("Starting a new flow rule stage for TMAC table flow"); |
Charles Chan | 206506b | 2018-03-02 16:43:28 -0800 | [diff] [blame] | 524 | ops.newStage(); |
Charles Chan | 9174e6b | 2017-12-19 19:55:57 -0800 | [diff] [blame] | 525 | |
Charles Chan | 206506b | 2018-03-02 16:43:28 -0800 | [diff] [blame] | 526 | for (FlowRule flowRule : flowRules) { |
Charles Chan | 893a1d7 | 2018-05-10 22:19:25 +0000 | [diff] [blame] | 527 | log.trace("{} flow rules in TMAC table: {} for dev: {}", |
Charles Chan | 206506b | 2018-03-02 16:43:28 -0800 | [diff] [blame] | 528 | (install) ? "adding" : "removing", flowRules, deviceId); |
Charles Chan | 206506b | 2018-03-02 16:43:28 -0800 | [diff] [blame] | 529 | if (install) { |
| 530 | ops = ops.add(flowRule); |
Charles Chan | 9174e6b | 2017-12-19 19:55:57 -0800 | [diff] [blame] | 531 | } else { |
Charles Chan | 206506b | 2018-03-02 16:43:28 -0800 | [diff] [blame] | 532 | // NOTE: Only remove TMAC flow when there is no more enabled port within the |
| 533 | // same VLAN on this device if TMAC doesn't support matching on in_port. |
| 534 | if (matchInPortTmacTable() || (filt.meta() != null && filt.meta().clearedDeferred())) { |
| 535 | ops = ops.remove(flowRule); |
| 536 | } else { |
| 537 | log.debug("Abort TMAC flow removal on {}. Some other ports still share this TMAC flow"); |
| 538 | } |
Charles Chan | 9174e6b | 2017-12-19 19:55:57 -0800 | [diff] [blame] | 539 | } |
| 540 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
Charles Chan | 985b12e | 2016-05-11 19:47:22 -0700 | [diff] [blame] | 544 | if (vidCriterion == null) { |
| 545 | // NOTE: it is possible that a filtering objective only has ethCriterion |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 546 | log.info("filtering objective missing VLAN, cannot program VLAN Table"); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 547 | } else { |
Charles Chan | 206506b | 2018-03-02 16:43:28 -0800 | [diff] [blame] | 548 | List<List<FlowRule>> allStages = processVlanIdFilter( |
pier | b2f7114 | 2019-06-10 17:10:26 +0200 | [diff] [blame] | 549 | portCriterion, vidCriterion, assignedVlan, applicationId, install); |
Charles Chan | 206506b | 2018-03-02 16:43:28 -0800 | [diff] [blame] | 550 | for (List<FlowRule> flowRules : allStages) { |
Charles Chan | d57552d | 2018-03-02 15:41:41 -0800 | [diff] [blame] | 551 | log.trace("Starting a new flow rule stage for VLAN table flow"); |
Charles Chan | 206506b | 2018-03-02 16:43:28 -0800 | [diff] [blame] | 552 | ops.newStage(); |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 553 | |
Charles Chan | 206506b | 2018-03-02 16:43:28 -0800 | [diff] [blame] | 554 | for (FlowRule flowRule : flowRules) { |
| 555 | log.trace("{} flow rules in VLAN table: {} for dev: {}", |
| 556 | (install) ? "adding" : "removing", flowRule, deviceId); |
| 557 | ops = install ? ops.add(flowRule) : ops.remove(flowRule); |
Charles Chan | d117263 | 2017-03-15 17:33:09 -0700 | [diff] [blame] | 558 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 562 | // apply filtering flow rules |
| 563 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 564 | @Override |
| 565 | public void onSuccess(FlowRuleOperations ops) { |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 566 | log.debug("Applied {} filtering rules in device {}", |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 567 | ops.stages().get(0).size(), deviceId); |
| 568 | pass(filt); |
| 569 | } |
| 570 | |
| 571 | @Override |
| 572 | public void onError(FlowRuleOperations ops) { |
| 573 | log.info("Failed to apply all filtering rules in dev {}", deviceId); |
| 574 | fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 575 | } |
| 576 | })); |
| 577 | |
| 578 | } |
| 579 | |
| 580 | /** |
Charles Chan | d117263 | 2017-03-15 17:33:09 -0700 | [diff] [blame] | 581 | * Internal implementation of processVlanIdFilter. |
| 582 | * <p> |
| 583 | * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12. |
| 584 | * Since it is non-OF spec, we need an extension treatment for that. |
| 585 | * The useVlanExtension must be set to false for OFDPA i12. |
| 586 | * </p> |
Charles Chan | 206506b | 2018-03-02 16:43:28 -0800 | [diff] [blame] | 587 | * <p> |
| 588 | * NOTE: Separate VLAN filtering rules and assignment rules |
| 589 | * into different stages in order to guarantee that filtering rules |
| 590 | * always go first, as required by OFDPA. |
| 591 | * </p> |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 592 | * |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 593 | * @param portCriterion port on device for which this filter is programmed |
| 594 | * @param vidCriterion vlan assigned to port, or NONE for untagged |
| 595 | * @param assignedVlan assigned vlan-id for untagged packets |
| 596 | * @param applicationId for application programming this filter |
pier | b2f7114 | 2019-06-10 17:10:26 +0200 | [diff] [blame] | 597 | * @param install indicates whether to add or remove the objective |
Charles Chan | 206506b | 2018-03-02 16:43:28 -0800 | [diff] [blame] | 598 | * @return stages of flow rules for port-vlan filters |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 599 | */ |
Charles Chan | 206506b | 2018-03-02 16:43:28 -0800 | [diff] [blame] | 600 | protected List<List<FlowRule>> processVlanIdFilter(PortCriterion portCriterion, |
pier | b2f7114 | 2019-06-10 17:10:26 +0200 | [diff] [blame] | 601 | VlanIdCriterion vidCriterion, |
| 602 | VlanId assignedVlan, |
| 603 | ApplicationId applicationId, |
| 604 | boolean install) { |
Charles Chan | 206506b | 2018-03-02 16:43:28 -0800 | [diff] [blame] | 605 | List<FlowRule> filteringRules = new ArrayList<>(); |
| 606 | List<FlowRule> assignmentRules = new ArrayList<>(); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 607 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 608 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 609 | TrafficSelector.Builder preSelector = null; |
| 610 | TrafficTreatment.Builder preTreatment = null; |
| 611 | |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 612 | treatment.transition(TMAC_TABLE); |
| 613 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 614 | if (vidCriterion.vlanId() == VlanId.NONE) { |
| 615 | // untagged packets are assigned vlans |
Charles Chan | d117263 | 2017-03-15 17:33:09 -0700 | [diff] [blame] | 616 | preSelector = DefaultTrafficSelector.builder(); |
| 617 | if (requireVlanExtensions()) { |
| 618 | OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE); |
| 619 | selector.extension(ofdpaMatchVlanVid, deviceId); |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 620 | OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan); |
| 621 | treatment.extension(ofdpaSetVlanVid, deviceId); |
Charles Chan | d117263 | 2017-03-15 17:33:09 -0700 | [diff] [blame] | 622 | |
| 623 | OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan); |
| 624 | preSelector.extension(preOfdpaMatchVlanVid, deviceId); |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 625 | } else { |
Charles Chan | d117263 | 2017-03-15 17:33:09 -0700 | [diff] [blame] | 626 | selector.matchVlanId(VlanId.NONE); |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 627 | treatment.setVlanId(assignedVlan); |
Charles Chan | d117263 | 2017-03-15 17:33:09 -0700 | [diff] [blame] | 628 | |
| 629 | preSelector.matchVlanId(assignedVlan); |
| 630 | } |
| 631 | preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE); |
| 632 | } else { |
| 633 | if (requireVlanExtensions()) { |
| 634 | OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId()); |
| 635 | selector.extension(ofdpaMatchVlanVid, deviceId); |
| 636 | } else { |
| 637 | selector.matchVlanId(vidCriterion.vlanId()); |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 638 | } |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 639 | |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 640 | if (!assignedVlan.equals(vidCriterion.vlanId())) { |
Charles Chan | d117263 | 2017-03-15 17:33:09 -0700 | [diff] [blame] | 641 | if (requireVlanExtensions()) { |
Charles Chan | c03782d | 2017-01-31 13:57:55 -0800 | [diff] [blame] | 642 | OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan); |
| 643 | treatment.extension(ofdpaSetVlanVid, deviceId); |
| 644 | } else { |
| 645 | treatment.setVlanId(assignedVlan); |
| 646 | } |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 647 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 648 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 649 | |
| 650 | // ofdpa cannot match on ALL portnumber, so we need to use separate |
| 651 | // rules for each port. |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 652 | List<PortNumber> portnums = new ArrayList<>(); |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 653 | if (portCriterion != null) { |
| 654 | if (PortNumber.ALL.equals(portCriterion.port())) { |
| 655 | for (Port port : deviceService.getPorts(deviceId)) { |
| 656 | if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) { |
| 657 | portnums.add(port.number()); |
| 658 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 659 | } |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 660 | } else { |
| 661 | portnums.add(portCriterion.port()); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 662 | } |
| 663 | } else { |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 664 | log.warn("Filtering Objective missing Port Criterion . " + |
Charles Chan | 206506b | 2018-03-02 16:43:28 -0800 | [diff] [blame] | 665 | "VLAN Table cannot be programmed for {}", deviceId); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 666 | } |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 667 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 668 | for (PortNumber pnum : portnums) { |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 669 | // create rest of flowrule |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 670 | selector.matchInPort(pnum); |
| 671 | FlowRule rule = DefaultFlowRule.builder() |
| 672 | .forDevice(deviceId) |
| 673 | .withSelector(selector.build()) |
| 674 | .withTreatment(treatment.build()) |
| 675 | .withPriority(DEFAULT_PRIORITY) |
| 676 | .fromApp(applicationId) |
| 677 | .makePermanent() |
| 678 | .forTable(VLAN_TABLE).build(); |
Charles Chan | 206506b | 2018-03-02 16:43:28 -0800 | [diff] [blame] | 679 | assignmentRules.add(rule); |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 680 | |
| 681 | if (preSelector != null) { |
| 682 | preSelector.matchInPort(pnum); |
| 683 | FlowRule preRule = DefaultFlowRule.builder() |
| 684 | .forDevice(deviceId) |
| 685 | .withSelector(preSelector.build()) |
| 686 | .withTreatment(preTreatment.build()) |
| 687 | .withPriority(DEFAULT_PRIORITY) |
| 688 | .fromApp(applicationId) |
| 689 | .makePermanent() |
| 690 | .forTable(VLAN_TABLE).build(); |
Charles Chan | 206506b | 2018-03-02 16:43:28 -0800 | [diff] [blame] | 691 | filteringRules.add(preRule); |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 692 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 693 | } |
pier | b2f7114 | 2019-06-10 17:10:26 +0200 | [diff] [blame] | 694 | return install ? ImmutableList.of(filteringRules, assignmentRules) : |
| 695 | ImmutableList.of(assignmentRules, filteringRules); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | /** |
| 699 | * Allows routed packets with correct destination MAC to be directed |
Charles Chan | d57552d | 2018-03-02 15:41:41 -0800 | [diff] [blame] | 700 | * to unicast routing table, multicast routing table or MPLS forwarding table. |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 701 | * |
| 702 | * @param portCriterion port on device for which this filter is programmed |
| 703 | * @param ethCriterion dstMac of device for which is filter is programmed |
| 704 | * @param vidCriterion vlan assigned to port, or NONE for untagged |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 705 | * @param assignedVlan assigned vlan-id for untagged packets |
Charles Chan | d57552d | 2018-03-02 15:41:41 -0800 | [diff] [blame] | 706 | * @param unicastMac some switches require a unicast TMAC flow to be programmed before multicast |
| 707 | * TMAC flow. This MAC address will be used for the unicast TMAC flow. |
| 708 | * This is unused if the filtering objective is a unicast. |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 709 | * @param applicationId for application programming this filter |
Charles Chan | 206506b | 2018-03-02 16:43:28 -0800 | [diff] [blame] | 710 | * @return stages of flow rules for port-vlan filters |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 711 | |
| 712 | */ |
Charles Chan | 206506b | 2018-03-02 16:43:28 -0800 | [diff] [blame] | 713 | protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion, |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 714 | EthCriterion ethCriterion, |
| 715 | VlanIdCriterion vidCriterion, |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 716 | VlanId assignedVlan, |
Charles Chan | d57552d | 2018-03-02 15:41:41 -0800 | [diff] [blame] | 717 | MacAddress unicastMac, |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 718 | ApplicationId applicationId) { |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 719 | // Consider PortNumber.ANY as wildcard. Match ETH_DST only |
Sivachidambaram Subramanian | c598d06 | 2017-05-16 22:42:06 +0530 | [diff] [blame] | 720 | if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) { |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 721 | return processEthDstOnlyFilter(ethCriterion, applicationId); |
| 722 | } |
| 723 | |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 724 | // Multicast MAC |
| 725 | if (ethCriterion.mask() != null) { |
Charles Chan | d57552d | 2018-03-02 15:41:41 -0800 | [diff] [blame] | 726 | return processMcastEthDstFilter(ethCriterion, assignedVlan, unicastMac, applicationId); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 727 | } |
| 728 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 729 | //handling untagged packets via assigned VLAN |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 730 | if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) { |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 731 | vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 732 | } |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 733 | List<FlowRule> rules = new ArrayList<>(); |
| 734 | OfdpaMatchVlanVid ofdpaMatchVlanVid = null; |
| 735 | if (vidCriterion != null && requireVlanExtensions()) { |
| 736 | ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId()); |
| 737 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 738 | // ofdpa cannot match on ALL portnumber, so we need to use separate |
| 739 | // rules for each port. |
Yuta HIGUCHI | 2dce08a | 2017-04-20 21:57:48 -0700 | [diff] [blame] | 740 | List<PortNumber> portnums = new ArrayList<>(); |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 741 | if (portCriterion != null) { |
| 742 | if (PortNumber.ALL.equals(portCriterion.port())) { |
| 743 | for (Port port : deviceService.getPorts(deviceId)) { |
| 744 | if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) { |
| 745 | portnums.add(port.number()); |
| 746 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 747 | } |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 748 | } else { |
| 749 | portnums.add(portCriterion.port()); |
| 750 | } |
| 751 | for (PortNumber pnum : portnums) { |
| 752 | rules.add(buildTmacRuleForIpv4(ethCriterion, |
| 753 | vidCriterion, |
| 754 | ofdpaMatchVlanVid, |
| 755 | applicationId, |
| 756 | pnum)); |
| 757 | rules.add(buildTmacRuleForMpls(ethCriterion, |
| 758 | vidCriterion, |
| 759 | ofdpaMatchVlanVid, |
| 760 | applicationId, |
| 761 | pnum)); |
| 762 | rules.add(buildTmacRuleForIpv6(ethCriterion, |
| 763 | vidCriterion, |
| 764 | ofdpaMatchVlanVid, |
| 765 | applicationId, |
| 766 | pnum)); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 767 | } |
| 768 | } else { |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 769 | rules.add(buildTmacRuleForIpv4(ethCriterion, |
| 770 | vidCriterion, |
| 771 | ofdpaMatchVlanVid, |
| 772 | applicationId, |
| 773 | null)); |
| 774 | rules.add(buildTmacRuleForMpls(ethCriterion, |
| 775 | vidCriterion, |
| 776 | ofdpaMatchVlanVid, |
| 777 | applicationId, |
| 778 | null)); |
| 779 | rules.add(buildTmacRuleForIpv6(ethCriterion, |
| 780 | vidCriterion, |
| 781 | ofdpaMatchVlanVid, |
| 782 | applicationId, |
| 783 | null)); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 784 | } |
Charles Chan | 206506b | 2018-03-02 16:43:28 -0800 | [diff] [blame] | 785 | return ImmutableList.of(rules); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 786 | } |
| 787 | |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 788 | /** |
| 789 | * Builds TMAC rules for IPv4 packets. |
| 790 | * |
Charles Chan | 9174e6b | 2017-12-19 19:55:57 -0800 | [diff] [blame] | 791 | * @param ethCriterion dst mac matching |
| 792 | * @param vidCriterion vlan id assigned to the port |
| 793 | * @param ofdpaMatchVlanVid OFDPA vlan id matching |
| 794 | * @param applicationId application id |
| 795 | * @param pnum port number |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 796 | * @return TMAC rule for IPV4 packets |
| 797 | */ |
| 798 | private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion, |
| 799 | VlanIdCriterion vidCriterion, |
| 800 | OfdpaMatchVlanVid ofdpaMatchVlanVid, |
| 801 | ApplicationId applicationId, |
| 802 | PortNumber pnum) { |
| 803 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 804 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 805 | if (pnum != null) { |
| 806 | if (matchInPortTmacTable()) { |
| 807 | selector.matchInPort(pnum); |
| 808 | } else { |
Saurav Das | 73a6cc1 | 2017-12-05 15:00:23 -0800 | [diff] [blame] | 809 | log.debug("Pipeline does not support IN_PORT matching in TMAC table, " + |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 810 | "ignoring the IN_PORT criteria"); |
| 811 | } |
| 812 | } |
| 813 | if (vidCriterion != null) { |
| 814 | if (requireVlanExtensions()) { |
| 815 | selector.extension(ofdpaMatchVlanVid, deviceId); |
| 816 | } else { |
| 817 | selector.matchVlanId(vidCriterion.vlanId()); |
| 818 | } |
| 819 | } |
| 820 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 821 | selector.matchEthDst(ethCriterion.mac()); |
| 822 | treatment.transition(UNICAST_ROUTING_TABLE); |
| 823 | return DefaultFlowRule.builder() |
| 824 | .forDevice(deviceId) |
| 825 | .withSelector(selector.build()) |
| 826 | .withTreatment(treatment.build()) |
| 827 | .withPriority(DEFAULT_PRIORITY) |
| 828 | .fromApp(applicationId) |
| 829 | .makePermanent() |
| 830 | .forTable(TMAC_TABLE).build(); |
| 831 | } |
| 832 | |
| 833 | /** |
| 834 | * Builds TMAC rule for MPLS packets. |
| 835 | * |
Charles Chan | 9174e6b | 2017-12-19 19:55:57 -0800 | [diff] [blame] | 836 | * @param ethCriterion dst mac matching |
| 837 | * @param vidCriterion vlan id assigned to the port |
| 838 | * @param ofdpaMatchVlanVid OFDPA vlan id matching |
| 839 | * @param applicationId application id |
| 840 | * @param pnum port number |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 841 | * @return TMAC rule for MPLS packets |
| 842 | */ |
| 843 | private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion, |
| 844 | VlanIdCriterion vidCriterion, |
| 845 | OfdpaMatchVlanVid ofdpaMatchVlanVid, |
| 846 | ApplicationId applicationId, |
| 847 | PortNumber pnum) { |
| 848 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 849 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 850 | if (pnum != null) { |
| 851 | if (matchInPortTmacTable()) { |
| 852 | selector.matchInPort(pnum); |
| 853 | } else { |
Saurav Das | 73a6cc1 | 2017-12-05 15:00:23 -0800 | [diff] [blame] | 854 | log.debug("Pipeline does not support IN_PORT matching in TMAC table, " + |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 855 | "ignoring the IN_PORT criteria"); |
| 856 | } |
| 857 | } |
| 858 | if (vidCriterion != null) { |
| 859 | if (requireVlanExtensions()) { |
| 860 | selector.extension(ofdpaMatchVlanVid, deviceId); |
| 861 | } else { |
| 862 | selector.matchVlanId(vidCriterion.vlanId()); |
| 863 | } |
| 864 | } |
| 865 | selector.matchEthType(Ethernet.MPLS_UNICAST); |
| 866 | selector.matchEthDst(ethCriterion.mac()); |
| 867 | treatment.transition(MPLS_TABLE_0); |
| 868 | return DefaultFlowRule.builder() |
| 869 | .forDevice(deviceId) |
| 870 | .withSelector(selector.build()) |
| 871 | .withTreatment(treatment.build()) |
| 872 | .withPriority(DEFAULT_PRIORITY) |
| 873 | .fromApp(applicationId) |
| 874 | .makePermanent() |
| 875 | .forTable(TMAC_TABLE).build(); |
| 876 | } |
| 877 | |
| 878 | /** |
| 879 | * Builds TMAC rules for IPv6 packets. |
| 880 | * |
Charles Chan | 9174e6b | 2017-12-19 19:55:57 -0800 | [diff] [blame] | 881 | * @param ethCriterion dst mac matching |
| 882 | * @param vidCriterion vlan id assigned to the port |
| 883 | * @param ofdpaMatchVlanVid OFDPA vlan id matching |
| 884 | * @param applicationId application id |
| 885 | * @param pnum port number |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 886 | * @return TMAC rule for IPV6 packets |
| 887 | */ |
| 888 | private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion, |
| 889 | VlanIdCriterion vidCriterion, |
| 890 | OfdpaMatchVlanVid ofdpaMatchVlanVid, |
| 891 | ApplicationId applicationId, |
| 892 | PortNumber pnum) { |
| 893 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 894 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 895 | if (pnum != null) { |
| 896 | if (matchInPortTmacTable()) { |
| 897 | selector.matchInPort(pnum); |
| 898 | } else { |
Saurav Das | 73a6cc1 | 2017-12-05 15:00:23 -0800 | [diff] [blame] | 899 | log.debug("Pipeline does not support IN_PORT matching in TMAC table, " + |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 900 | "ignoring the IN_PORT criteria"); |
| 901 | } |
| 902 | } |
| 903 | if (vidCriterion != null) { |
| 904 | if (requireVlanExtensions()) { |
| 905 | selector.extension(ofdpaMatchVlanVid, deviceId); |
| 906 | } else { |
| 907 | selector.matchVlanId(vidCriterion.vlanId()); |
| 908 | } |
| 909 | } |
| 910 | selector.matchEthType(Ethernet.TYPE_IPV6); |
| 911 | selector.matchEthDst(ethCriterion.mac()); |
| 912 | treatment.transition(UNICAST_ROUTING_TABLE); |
| 913 | return DefaultFlowRule.builder() |
| 914 | .forDevice(deviceId) |
| 915 | .withSelector(selector.build()) |
| 916 | .withTreatment(treatment.build()) |
| 917 | .withPriority(DEFAULT_PRIORITY) |
| 918 | .fromApp(applicationId) |
| 919 | .makePermanent() |
| 920 | .forTable(TMAC_TABLE).build(); |
| 921 | } |
| 922 | |
Charles Chan | 206506b | 2018-03-02 16:43:28 -0800 | [diff] [blame] | 923 | protected List<List<FlowRule>> processEthDstOnlyFilter(EthCriterion ethCriterion, |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 924 | ApplicationId applicationId) { |
Pier Luigi | 0e35863 | 2017-01-31 09:35:05 -0800 | [diff] [blame] | 925 | ImmutableList.Builder<FlowRule> builder = ImmutableList.builder(); |
| 926 | |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 927 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 928 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 929 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 930 | selector.matchEthDst(ethCriterion.mac()); |
| 931 | treatment.transition(UNICAST_ROUTING_TABLE); |
| 932 | FlowRule rule = DefaultFlowRule.builder() |
| 933 | .forDevice(deviceId) |
| 934 | .withSelector(selector.build()) |
| 935 | .withTreatment(treatment.build()) |
| 936 | .withPriority(DEFAULT_PRIORITY) |
| 937 | .fromApp(applicationId) |
| 938 | .makePermanent() |
| 939 | .forTable(TMAC_TABLE).build(); |
Pier Luigi | 0e35863 | 2017-01-31 09:35:05 -0800 | [diff] [blame] | 940 | builder.add(rule); |
| 941 | |
| 942 | selector = DefaultTrafficSelector.builder(); |
| 943 | treatment = DefaultTrafficTreatment.builder(); |
| 944 | selector.matchEthType(Ethernet.TYPE_IPV6); |
| 945 | selector.matchEthDst(ethCriterion.mac()); |
| 946 | treatment.transition(UNICAST_ROUTING_TABLE); |
| 947 | rule = DefaultFlowRule.builder() |
| 948 | .forDevice(deviceId) |
| 949 | .withSelector(selector.build()) |
| 950 | .withTreatment(treatment.build()) |
| 951 | .withPriority(DEFAULT_PRIORITY) |
| 952 | .fromApp(applicationId) |
| 953 | .makePermanent() |
| 954 | .forTable(TMAC_TABLE).build(); |
Charles Chan | 206506b | 2018-03-02 16:43:28 -0800 | [diff] [blame] | 955 | return ImmutableList.of(builder.add(rule).build()); |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 956 | } |
| 957 | |
Charles Chan | 206506b | 2018-03-02 16:43:28 -0800 | [diff] [blame] | 958 | List<List<FlowRule>> processMcastEthDstFilter(EthCriterion ethCriterion, |
Charles Chan | b4879a5 | 2017-10-20 19:09:16 -0700 | [diff] [blame] | 959 | VlanId assignedVlan, |
Charles Chan | d57552d | 2018-03-02 15:41:41 -0800 | [diff] [blame] | 960 | MacAddress unicastMac, |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 961 | ApplicationId applicationId) { |
Charles Chan | d57552d | 2018-03-02 15:41:41 -0800 | [diff] [blame] | 962 | ImmutableList.Builder<FlowRule> unicastFlows = ImmutableList.builder(); |
| 963 | ImmutableList.Builder<FlowRule> multicastFlows = ImmutableList.builder(); |
| 964 | TrafficSelector.Builder selector; |
| 965 | TrafficTreatment.Builder treatment; |
Charles Chan | b4879a5 | 2017-10-20 19:09:16 -0700 | [diff] [blame] | 966 | FlowRule rule; |
Julia Ferguson | 65428c3 | 2017-08-10 18:15:24 +0000 | [diff] [blame] | 967 | |
Charles Chan | b4879a5 | 2017-10-20 19:09:16 -0700 | [diff] [blame] | 968 | if (IPV4_MULTICAST.equals(ethCriterion.mac())) { |
Charles Chan | d57552d | 2018-03-02 15:41:41 -0800 | [diff] [blame] | 969 | if (requireUnicastBeforeMulticast()) { |
| 970 | selector = DefaultTrafficSelector.builder(); |
| 971 | treatment = DefaultTrafficTreatment.builder(); |
| 972 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 973 | selector.matchEthDst(unicastMac); |
Charles Chan | 893a1d7 | 2018-05-10 22:19:25 +0000 | [diff] [blame] | 974 | selector.matchVlanId(assignedVlan); |
Charles Chan | d57552d | 2018-03-02 15:41:41 -0800 | [diff] [blame] | 975 | treatment.transition(UNICAST_ROUTING_TABLE); |
| 976 | rule = DefaultFlowRule.builder() |
| 977 | .forDevice(deviceId) |
| 978 | .withSelector(selector.build()) |
| 979 | .withTreatment(treatment.build()) |
Charles Chan | 893a1d7 | 2018-05-10 22:19:25 +0000 | [diff] [blame] | 980 | .withPriority(DEFAULT_PRIORITY) |
Charles Chan | d57552d | 2018-03-02 15:41:41 -0800 | [diff] [blame] | 981 | .fromApp(applicationId) |
| 982 | .makePermanent() |
| 983 | .forTable(TMAC_TABLE).build(); |
| 984 | unicastFlows.add(rule); |
| 985 | } |
| 986 | |
| 987 | selector = DefaultTrafficSelector.builder(); |
| 988 | treatment = DefaultTrafficTreatment.builder(); |
Charles Chan | b4879a5 | 2017-10-20 19:09:16 -0700 | [diff] [blame] | 989 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 990 | selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask()); |
Charles Chan | 893a1d7 | 2018-05-10 22:19:25 +0000 | [diff] [blame] | 991 | selector.matchVlanId(assignedVlan); |
Charles Chan | b4879a5 | 2017-10-20 19:09:16 -0700 | [diff] [blame] | 992 | treatment.transition(MULTICAST_ROUTING_TABLE); |
| 993 | rule = DefaultFlowRule.builder() |
| 994 | .forDevice(deviceId) |
| 995 | .withSelector(selector.build()) |
| 996 | .withTreatment(treatment.build()) |
Charles Chan | 893a1d7 | 2018-05-10 22:19:25 +0000 | [diff] [blame] | 997 | .withPriority(DEFAULT_PRIORITY) |
Charles Chan | b4879a5 | 2017-10-20 19:09:16 -0700 | [diff] [blame] | 998 | .fromApp(applicationId) |
| 999 | .makePermanent() |
| 1000 | .forTable(TMAC_TABLE).build(); |
Charles Chan | d57552d | 2018-03-02 15:41:41 -0800 | [diff] [blame] | 1001 | multicastFlows.add(rule); |
Charles Chan | b4879a5 | 2017-10-20 19:09:16 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | if (IPV6_MULTICAST.equals(ethCriterion.mac())) { |
Charles Chan | d57552d | 2018-03-02 15:41:41 -0800 | [diff] [blame] | 1005 | if (requireUnicastBeforeMulticast()) { |
| 1006 | selector = DefaultTrafficSelector.builder(); |
| 1007 | treatment = DefaultTrafficTreatment.builder(); |
| 1008 | selector.matchEthType(Ethernet.TYPE_IPV6); |
| 1009 | selector.matchEthDst(unicastMac); |
Charles Chan | 893a1d7 | 2018-05-10 22:19:25 +0000 | [diff] [blame] | 1010 | selector.matchVlanId(assignedVlan); |
Charles Chan | d57552d | 2018-03-02 15:41:41 -0800 | [diff] [blame] | 1011 | treatment.transition(UNICAST_ROUTING_TABLE); |
| 1012 | rule = DefaultFlowRule.builder() |
| 1013 | .forDevice(deviceId) |
| 1014 | .withSelector(selector.build()) |
| 1015 | .withTreatment(treatment.build()) |
Charles Chan | 893a1d7 | 2018-05-10 22:19:25 +0000 | [diff] [blame] | 1016 | .withPriority(DEFAULT_PRIORITY) |
Charles Chan | d57552d | 2018-03-02 15:41:41 -0800 | [diff] [blame] | 1017 | .fromApp(applicationId) |
| 1018 | .makePermanent() |
| 1019 | .forTable(TMAC_TABLE).build(); |
| 1020 | unicastFlows.add(rule); |
| 1021 | } |
| 1022 | |
Charles Chan | b4879a5 | 2017-10-20 19:09:16 -0700 | [diff] [blame] | 1023 | selector = DefaultTrafficSelector.builder(); |
| 1024 | treatment = DefaultTrafficTreatment.builder(); |
| 1025 | selector.matchEthType(Ethernet.TYPE_IPV6); |
| 1026 | selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask()); |
Charles Chan | 893a1d7 | 2018-05-10 22:19:25 +0000 | [diff] [blame] | 1027 | selector.matchVlanId(assignedVlan); |
Charles Chan | b4879a5 | 2017-10-20 19:09:16 -0700 | [diff] [blame] | 1028 | treatment.transition(MULTICAST_ROUTING_TABLE); |
| 1029 | rule = DefaultFlowRule.builder() |
| 1030 | .forDevice(deviceId) |
| 1031 | .withSelector(selector.build()) |
| 1032 | .withTreatment(treatment.build()) |
Charles Chan | 893a1d7 | 2018-05-10 22:19:25 +0000 | [diff] [blame] | 1033 | .withPriority(DEFAULT_PRIORITY) |
Charles Chan | b4879a5 | 2017-10-20 19:09:16 -0700 | [diff] [blame] | 1034 | .fromApp(applicationId) |
| 1035 | .makePermanent() |
| 1036 | .forTable(TMAC_TABLE).build(); |
Charles Chan | d57552d | 2018-03-02 15:41:41 -0800 | [diff] [blame] | 1037 | multicastFlows.add(rule); |
Charles Chan | b4879a5 | 2017-10-20 19:09:16 -0700 | [diff] [blame] | 1038 | } |
Charles Chan | d57552d | 2018-03-02 15:41:41 -0800 | [diff] [blame] | 1039 | return ImmutableList.of(unicastFlows.build(), multicastFlows.build()); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1040 | } |
| 1041 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1042 | private Collection<FlowRule> processForward(ForwardingObjective fwd) { |
| 1043 | switch (fwd.flag()) { |
| 1044 | case SPECIFIC: |
| 1045 | return processSpecific(fwd); |
| 1046 | case VERSATILE: |
| 1047 | return processVersatile(fwd); |
| 1048 | default: |
| 1049 | fail(fwd, ObjectiveError.UNKNOWN); |
| 1050 | log.warn("Unknown forwarding flag {}", fwd.flag()); |
| 1051 | } |
| 1052 | return Collections.emptySet(); |
| 1053 | } |
| 1054 | |
| 1055 | /** |
| 1056 | * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the |
| 1057 | * ACL table. |
| 1058 | * @param fwd the forwarding objective of type 'versatile' |
| 1059 | * @return a collection of flow rules to be sent to the switch. An empty |
| 1060 | * collection may be returned if there is a problem in processing |
| 1061 | * the flow rule |
| 1062 | */ |
Saurav Das | 5202596 | 2016-01-28 22:30:01 -0800 | [diff] [blame] | 1063 | protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) { |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 1064 | log.debug("Processing versatile forwarding objective:{} in dev:{}", |
Saurav Das | d2fded0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 1065 | fwd.id(), deviceId); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1066 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1067 | if (fwd.nextId() == null && fwd.treatment() == null) { |
| 1068 | log.error("Forwarding objective {} from {} must contain " |
| 1069 | + "nextId or Treatment", fwd.selector(), fwd.appId()); |
Zsolt Haraszti | 9faab75 | 2016-02-17 15:55:20 -0800 | [diff] [blame] | 1070 | fail(fwd, ObjectiveError.BADPARAMS); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1071 | return Collections.emptySet(); |
| 1072 | } |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 1073 | |
Saurav Das | 77b5e90 | 2016-01-27 17:01:59 -0800 | [diff] [blame] | 1074 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 1075 | fwd.selector().criteria().forEach(criterion -> { |
| 1076 | if (criterion instanceof VlanIdCriterion) { |
| 1077 | VlanId vlanId = ((VlanIdCriterion) criterion).vlanId(); |
| 1078 | // ensure that match does not include vlan = NONE as OF-DPA does not |
| 1079 | // match untagged packets this way in the ACL table. |
| 1080 | if (vlanId.equals(VlanId.NONE)) { |
| 1081 | return; |
| 1082 | } |
Charles Chan | d117263 | 2017-03-15 17:33:09 -0700 | [diff] [blame] | 1083 | if (requireVlanExtensions()) { |
| 1084 | OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId); |
| 1085 | sbuilder.extension(ofdpaMatchVlanVid, deviceId); |
| 1086 | } else { |
| 1087 | sbuilder.matchVlanId(vlanId); |
| 1088 | } |
Charles Chan | 393339f | 2018-01-11 11:48:18 -0800 | [diff] [blame] | 1089 | } else if (criterion instanceof Icmpv6TypeCriterion) { |
| 1090 | byte icmpv6Type = (byte) ((Icmpv6TypeCriterion) criterion).icmpv6Type(); |
| 1091 | sbuilder.matchIcmpv6Type(icmpv6Type); |
| 1092 | } else if (criterion instanceof Icmpv6CodeCriterion) { |
| 1093 | byte icmpv6Code = (byte) ((Icmpv6CodeCriterion) criterion).icmpv6Code(); |
| 1094 | sbuilder.matchIcmpv6Type(icmpv6Code); |
Charles Chan | d9e47c6 | 2017-10-05 15:17:15 -0700 | [diff] [blame] | 1095 | } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) { |
| 1096 | // FIXME: QMX switches do not support L4 dst port matching in ACL table. |
| 1097 | // Currently L4 dst port matching is only used by DHCP relay feature |
| 1098 | // and therefore is safe to be replaced with L4 src port matching. |
| 1099 | // We need to revisit this if L4 dst port is used for other purpose in the future. |
| 1100 | if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) { |
Charles Chan | d9e47c6 | 2017-10-05 15:17:15 -0700 | [diff] [blame] | 1101 | switch (criterion.type()) { |
| 1102 | case UDP_DST: |
Charles Chan | d9e47c6 | 2017-10-05 15:17:15 -0700 | [diff] [blame] | 1103 | case UDP_DST_MASKED: |
Charles Chan | d9e47c6 | 2017-10-05 15:17:15 -0700 | [diff] [blame] | 1104 | case TCP_DST: |
Charles Chan | d9e47c6 | 2017-10-05 15:17:15 -0700 | [diff] [blame] | 1105 | case TCP_DST_MASKED: |
Charles Chan | d9e47c6 | 2017-10-05 15:17:15 -0700 | [diff] [blame] | 1106 | break; |
| 1107 | default: |
| 1108 | sbuilder.add(criterion); |
| 1109 | } |
| 1110 | } else { |
| 1111 | sbuilder.add(criterion); |
| 1112 | } |
Saurav Das | 77b5e90 | 2016-01-27 17:01:59 -0800 | [diff] [blame] | 1113 | } else { |
| 1114 | sbuilder.add(criterion); |
| 1115 | } |
| 1116 | }); |
| 1117 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1118 | // XXX driver does not currently do type checking as per Tables 65-67 in |
| 1119 | // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller. |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 1120 | TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder(); |
| 1121 | if (fwd.treatment() != null) { |
| 1122 | for (Instruction ins : fwd.treatment().allInstructions()) { |
| 1123 | if (ins instanceof OutputInstruction) { |
| 1124 | OutputInstruction o = (OutputInstruction) ins; |
Charles Chan | 9309035 | 2018-03-02 13:26:22 -0800 | [diff] [blame] | 1125 | if (PortNumber.CONTROLLER.equals(o.port())) { |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 1126 | ttBuilder.add(o); |
| 1127 | } else { |
| 1128 | log.warn("Only allowed treatments in versatile forwarding " |
| 1129 | + "objectives are punts to the controller"); |
| 1130 | } |
Charles Chan | 3fe7171 | 2018-06-15 18:54:18 -0700 | [diff] [blame] | 1131 | } else if (ins instanceof NoActionInstruction) { |
| 1132 | // No action is allowed and nothing needs to be done |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 1133 | } else { |
| 1134 | log.warn("Cannot process instruction in versatile fwd {}", ins); |
| 1135 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1136 | } |
Charles Chan | 2df0e8a | 2017-01-09 11:45:08 -0800 | [diff] [blame] | 1137 | if (fwd.treatment().clearedDeferred()) { |
| 1138 | ttBuilder.wipeDeferred(); |
| 1139 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1140 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1141 | if (fwd.nextId() != null) { |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 1142 | // overide case |
| 1143 | NextGroup next = getGroupForNextObjective(fwd.nextId()); |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 1144 | if (next == null) { |
| 1145 | fail(fwd, ObjectiveError.BADPARAMS); |
| 1146 | return Collections.emptySet(); |
| 1147 | } |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 1148 | List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data()); |
| 1149 | // we only need the top level group's key to point the flow to it |
| 1150 | Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst()); |
| 1151 | if (group == null) { |
| 1152 | log.warn("Group with key:{} for next-id:{} not found in dev:{}", |
| 1153 | gkeys.get(0).peekFirst(), fwd.nextId(), deviceId); |
| 1154 | fail(fwd, ObjectiveError.GROUPMISSING); |
| 1155 | return Collections.emptySet(); |
| 1156 | } |
| 1157 | ttBuilder.deferred().group(group.id()); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1158 | } |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 1159 | |
| 1160 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 1161 | .fromApp(fwd.appId()) |
| 1162 | .withPriority(fwd.priority()) |
| 1163 | .forDevice(deviceId) |
Saurav Das | 77b5e90 | 2016-01-27 17:01:59 -0800 | [diff] [blame] | 1164 | .withSelector(sbuilder.build()) |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 1165 | .withTreatment(ttBuilder.build()) |
| 1166 | .makePermanent() |
| 1167 | .forTable(ACL_TABLE); |
| 1168 | return Collections.singletonList(ruleBuilder.build()); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | /** |
| 1172 | * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1173 | * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table. |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1174 | * |
| 1175 | * @param fwd the forwarding objective of type 'specific' |
| 1176 | * @return a collection of flow rules. Typically there will be only one |
| 1177 | * for this type of forwarding objective. An empty set may be |
| 1178 | * returned if there is an issue in processing the objective. |
| 1179 | */ |
Charles Chan | 9309035 | 2018-03-02 13:26:22 -0800 | [diff] [blame] | 1180 | private Collection<FlowRule> processSpecific(ForwardingObjective fwd) { |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 1181 | log.debug("Processing specific fwd objective:{} in dev:{} with next:{}", |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1182 | fwd.id(), deviceId, fwd.nextId()); |
| 1183 | boolean isEthTypeObj = isSupportedEthTypeObjective(fwd); |
| 1184 | boolean isEthDstObj = isSupportedEthDstObjective(fwd); |
| 1185 | |
| 1186 | if (isEthTypeObj) { |
| 1187 | return processEthTypeSpecific(fwd); |
| 1188 | } else if (isEthDstObj) { |
| 1189 | return processEthDstSpecific(fwd); |
| 1190 | } else { |
| 1191 | log.warn("processSpecific: Unsupported forwarding objective " |
| 1192 | + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1193 | fail(fwd, ObjectiveError.UNSUPPORTED); |
| 1194 | return Collections.emptySet(); |
| 1195 | } |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1196 | } |
| 1197 | |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1198 | /** |
| 1199 | * Handles forwarding rules to the IP and MPLS tables. |
| 1200 | * |
| 1201 | * @param fwd the forwarding objective |
| 1202 | * @return A collection of flow rules, or an empty set |
| 1203 | */ |
| 1204 | protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) { |
Charles Chan | cad338a | 2016-09-16 18:03:11 -0700 | [diff] [blame] | 1205 | return processEthTypeSpecificInternal(fwd, false, ACL_TABLE); |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | /** |
| 1209 | * Internal implementation of processEthTypeSpecific. |
| 1210 | * <p> |
| 1211 | * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break |
| 1212 | * the rule into 0.0.0.0/1 and 128.0.0.0/1. |
| 1213 | * The allowDefaultRoute must be set to false for OFDPA i12. |
| 1214 | * </p> |
| 1215 | * |
| 1216 | * @param fwd the forwarding objective |
| 1217 | * @param allowDefaultRoute allow wildcarded IPv4_DST or not |
Ray Milkey | 0bb1e10 | 2016-11-10 14:51:27 -0800 | [diff] [blame] | 1218 | * @param mplsNextTable next MPLS table |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 1219 | * @return A collection of flow rules, or an empty set |
| 1220 | */ |
| 1221 | protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd, |
Charles Chan | cad338a | 2016-09-16 18:03:11 -0700 | [diff] [blame] | 1222 | boolean allowDefaultRoute, |
| 1223 | int mplsNextTable) { |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1224 | TrafficSelector selector = fwd.selector(); |
| 1225 | EthTypeCriterion ethType = |
| 1226 | (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE); |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1227 | boolean emptyGroup = false; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1228 | int forTableId; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1229 | TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder(); |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 1230 | TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder(); |
Flavio Castro | e10fa24 | 2016-01-15 12:43:51 -0800 | [diff] [blame] | 1231 | TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder(); |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 1232 | |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1233 | if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) { |
Pier Luigi | 3bfe32c | 2017-01-30 09:47:36 -0800 | [diff] [blame] | 1234 | if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) { |
| 1235 | return Collections.emptyList(); |
| 1236 | } |
| 1237 | // We need to set properly the next table |
Flavio Castro | e10fa24 | 2016-01-15 12:43:51 -0800 | [diff] [blame] | 1238 | IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip(); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1239 | if (ipv4Dst.isMulticast()) { |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1240 | forTableId = MULTICAST_ROUTING_TABLE; |
Flavio Castro | e10fa24 | 2016-01-15 12:43:51 -0800 | [diff] [blame] | 1241 | } else { |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1242 | forTableId = UNICAST_ROUTING_TABLE; |
Flavio Castro | e10fa24 | 2016-01-15 12:43:51 -0800 | [diff] [blame] | 1243 | } |
Charles Chan | 9309035 | 2018-03-02 13:26:22 -0800 | [diff] [blame] | 1244 | |
Charles Chan | 7feb6c8 | 2018-03-05 11:28:23 -0800 | [diff] [blame] | 1245 | // TODO decrementing IP ttl is done automatically for routing, this |
Charles Chan | 9309035 | 2018-03-02 13:26:22 -0800 | [diff] [blame] | 1246 | // action is ignored or rejected in ofdpa as it is not fully implemented |
Charles Chan | 7feb6c8 | 2018-03-05 11:28:23 -0800 | [diff] [blame] | 1247 | // if (fwd.treatment() != null) { |
| 1248 | // for (Instruction instr : fwd.treatment().allInstructions()) { |
| 1249 | // if (instr instanceof L3ModificationInstruction && |
| 1250 | // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) { |
| 1251 | // tb.deferred().add(instr); |
| 1252 | // } |
| 1253 | // } |
| 1254 | // } |
| 1255 | |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 1256 | } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) { |
| 1257 | if (buildIpv6Selector(filteredSelector, fwd) < 0) { |
| 1258 | return Collections.emptyList(); |
| 1259 | } |
Kalhee Kim | 3d26bf0 | 2017-10-04 14:59:11 +0000 | [diff] [blame] | 1260 | //We need to set the proper next table |
| 1261 | IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip(); |
| 1262 | if (ipv6Dst.isMulticast()) { |
| 1263 | forTableId = MULTICAST_ROUTING_TABLE; |
| 1264 | } else { |
| 1265 | forTableId = UNICAST_ROUTING_TABLE; |
| 1266 | } |
| 1267 | |
Charles Chan | 7feb6c8 | 2018-03-05 11:28:23 -0800 | [diff] [blame] | 1268 | // TODO decrementing IP ttl is done automatically for routing, this |
Charles Chan | 9309035 | 2018-03-02 13:26:22 -0800 | [diff] [blame] | 1269 | // action is ignored or rejected in ofdpa as it is not fully implemented |
Charles Chan | 7feb6c8 | 2018-03-05 11:28:23 -0800 | [diff] [blame] | 1270 | // if (fwd.treatment() != null) { |
| 1271 | // for (Instruction instr : fwd.treatment().allInstructions()) { |
| 1272 | // if (instr instanceof L3ModificationInstruction && |
| 1273 | // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) { |
| 1274 | // tb.deferred().add(instr); |
| 1275 | // } |
| 1276 | // } |
| 1277 | // } |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1278 | } else { |
| 1279 | filteredSelector |
| 1280 | .matchEthType(Ethernet.MPLS_UNICAST) |
| 1281 | .matchMplsLabel(((MplsCriterion) |
| 1282 | selector.getCriterion(Criterion.Type.MPLS_LABEL)).label()); |
| 1283 | MplsBosCriterion bos = (MplsBosCriterion) selector |
Pier Ventre | 140a894 | 2016-11-02 07:26:38 -0700 | [diff] [blame] | 1284 | .getCriterion(MPLS_BOS); |
Alex Yashchuk | a367953 | 2017-12-08 17:40:05 +0200 | [diff] [blame] | 1285 | if (bos != null && requireMplsBosMatch()) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1286 | filteredSelector.matchMplsBos(bos.mplsBos()); |
| 1287 | } |
| 1288 | forTableId = MPLS_TABLE_1; |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1289 | log.debug("processing MPLS specific forwarding objective {} -> next:{}" |
| 1290 | + " in dev {}", fwd.id(), fwd.nextId(), deviceId); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1291 | |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 1292 | if (fwd.treatment() != null) { |
| 1293 | for (Instruction instr : fwd.treatment().allInstructions()) { |
| 1294 | if (instr instanceof L2ModificationInstruction && |
| 1295 | ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) { |
Pier Luigi | 3bfe32c | 2017-01-30 09:47:36 -0800 | [diff] [blame] | 1296 | // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires |
Saurav Das | 9c70534 | 2017-01-06 11:36:01 -0800 | [diff] [blame] | 1297 | // setting the MPLS_TYPE so pop can happen down the pipeline |
Alex Yashchuk | a367953 | 2017-12-08 17:40:05 +0200 | [diff] [blame] | 1298 | if (requireMplsPop()) { |
| 1299 | if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) { |
| 1300 | tb.immediate().popMpls(); |
| 1301 | } |
| 1302 | } else { |
| 1303 | // Skip mpls pop action for mpls_unicast label |
| 1304 | if (instr instanceof ModMplsHeaderInstruction && |
| 1305 | !((ModMplsHeaderInstruction) instr).ethernetType() |
| 1306 | .equals(EtherType.MPLS_UNICAST.ethType())) { |
| 1307 | tb.immediate().add(instr); |
| 1308 | } |
Pier Luigi | 3bfe32c | 2017-01-30 09:47:36 -0800 | [diff] [blame] | 1309 | } |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 1310 | } |
Alex Yashchuk | a367953 | 2017-12-08 17:40:05 +0200 | [diff] [blame] | 1311 | |
| 1312 | if (requireMplsTtlModification()) { |
| 1313 | if (instr instanceof L3ModificationInstruction && |
| 1314 | ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) { |
| 1315 | // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction |
| 1316 | tb.immediate().decMplsTtl(); |
| 1317 | } |
| 1318 | if (instr instanceof L3ModificationInstruction && |
| 1319 | ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) { |
| 1320 | tb.immediate().add(instr); |
| 1321 | } |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 1322 | } |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1323 | } |
| 1324 | } |
| 1325 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1326 | |
| 1327 | if (fwd.nextId() != null) { |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 1328 | NextGroup next = getGroupForNextObjective(fwd.nextId()); |
| 1329 | if (next != null) { |
| 1330 | List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data()); |
| 1331 | // we only need the top level group's key to point the flow to it |
| 1332 | Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst()); |
| 1333 | if (group == null) { |
| 1334 | log.warn("Group with key:{} for next-id:{} not found in dev:{}", |
| 1335 | gkeys.get(0).peekFirst(), fwd.nextId(), deviceId); |
| 1336 | fail(fwd, ObjectiveError.GROUPMISSING); |
| 1337 | return Collections.emptySet(); |
| 1338 | } |
Saurav Das | a89b95a | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 1339 | if (isNotMplsBos(selector) && group.type().equals(SELECT)) { |
| 1340 | log.warn("SR CONTINUE case cannot be handled as MPLS ECMP " |
| 1341 | + "is not implemented in OF-DPA yet. Aborting flow {} -> next:{} " |
| 1342 | + "in this device {}", fwd.id(), fwd.nextId(), deviceId); |
| 1343 | fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 1344 | return Collections.emptySet(); |
| 1345 | } |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 1346 | tb.deferred().group(group.id()); |
Saurav Das | f3f7594 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 1347 | // retrying flows may be necessary due to bug CORD-554 |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1348 | if (gkeys.size() == 1 && gkeys.get(0).size() == 1) { |
Saurav Das | f3f7594 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 1349 | if (shouldRetry()) { |
| 1350 | log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}", |
| 1351 | Integer.toHexString(group.id().id()), deviceId, |
| 1352 | fwd.id()); |
| 1353 | emptyGroup = true; |
| 1354 | } |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1355 | } |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 1356 | } else { |
| 1357 | log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}", |
| 1358 | fwd.nextId(), deviceId, fwd.id()); |
| 1359 | fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 1360 | return Collections.emptySet(); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1361 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1362 | } |
Charles Chan | cad338a | 2016-09-16 18:03:11 -0700 | [diff] [blame] | 1363 | |
| 1364 | if (forTableId == MPLS_TABLE_1) { |
Pier Ventre | 140a894 | 2016-11-02 07:26:38 -0700 | [diff] [blame] | 1365 | if (mplsNextTable == MPLS_L3_TYPE_TABLE) { |
Charles Chan | cad338a | 2016-09-16 18:03:11 -0700 | [diff] [blame] | 1366 | Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP); |
Saurav Das | 9c70534 | 2017-01-06 11:36:01 -0800 | [diff] [blame] | 1367 | // set mpls type as apply_action |
| 1368 | tb.immediate().extension(setMplsType, deviceId); |
Charles Chan | cad338a | 2016-09-16 18:03:11 -0700 | [diff] [blame] | 1369 | } |
| 1370 | tb.transition(mplsNextTable); |
| 1371 | } else { |
| 1372 | tb.transition(ACL_TABLE); |
| 1373 | } |
| 1374 | |
Andrea Campanella | 84ac4df | 2018-04-30 11:48:55 +0200 | [diff] [blame] | 1375 | if (fwd.treatment() != null && fwd.treatment().clearedDeferred()) { |
| 1376 | if (supportsUnicastBlackHole()) { |
| 1377 | tb.wipeDeferred(); |
| 1378 | } else { |
| 1379 | log.warn("Clear Deferred is not supported Unicast Routing Table on device {}", deviceId); |
| 1380 | return Collections.emptySet(); |
| 1381 | } |
| 1382 | } |
| 1383 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1384 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 1385 | .fromApp(fwd.appId()) |
| 1386 | .withPriority(fwd.priority()) |
| 1387 | .forDevice(deviceId) |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1388 | .withSelector(filteredSelector.build()) |
| 1389 | .withTreatment(tb.build()) |
| 1390 | .forTable(forTableId); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1391 | |
| 1392 | if (fwd.permanent()) { |
| 1393 | ruleBuilder.makePermanent(); |
| 1394 | } else { |
| 1395 | ruleBuilder.makeTemporary(fwd.timeout()); |
| 1396 | } |
Flavio Castro | e10fa24 | 2016-01-15 12:43:51 -0800 | [diff] [blame] | 1397 | Collection<FlowRule> flowRuleCollection = new ArrayList<>(); |
| 1398 | flowRuleCollection.add(ruleBuilder.build()); |
Pier Luigi | 3bfe32c | 2017-01-30 09:47:36 -0800 | [diff] [blame] | 1399 | if (!allowDefaultRoute) { |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 1400 | flowRuleCollection.add( |
| 1401 | defaultRoute(fwd, complementarySelector, forTableId, tb) |
| 1402 | ); |
Flavio Castro | e10fa24 | 2016-01-15 12:43:51 -0800 | [diff] [blame] | 1403 | log.debug("Default rule 0.0.0.0/0 is being installed two rules"); |
| 1404 | } |
Saurav Das | f3f7594 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 1405 | |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1406 | if (emptyGroup) { |
pier | 5b9b9e3 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 1407 | retryExecutorService.schedule(new RetryFlows(fwd, flowRuleCollection), |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1408 | RETRY_MS, TimeUnit.MILLISECONDS); |
| 1409 | } |
Flavio Castro | e10fa24 | 2016-01-15 12:43:51 -0800 | [diff] [blame] | 1410 | return flowRuleCollection; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1411 | } |
| 1412 | |
Charles Chan | 9309035 | 2018-03-02 13:26:22 -0800 | [diff] [blame] | 1413 | private int buildIpv4Selector(TrafficSelector.Builder builderToUpdate, |
Pier Luigi | 3bfe32c | 2017-01-30 09:47:36 -0800 | [diff] [blame] | 1414 | TrafficSelector.Builder extBuilder, |
| 1415 | ForwardingObjective fwd, |
| 1416 | boolean allowDefaultRoute) { |
| 1417 | TrafficSelector selector = fwd.selector(); |
| 1418 | |
| 1419 | IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip(); |
| 1420 | if (ipv4Dst.isMulticast()) { |
| 1421 | if (ipv4Dst.prefixLength() != 32) { |
| 1422 | log.warn("Multicast specific forwarding objective can only be /32"); |
| 1423 | fail(fwd, ObjectiveError.BADPARAMS); |
| 1424 | return -1; |
| 1425 | } |
| 1426 | VlanId assignedVlan = readVlanFromSelector(fwd.meta()); |
| 1427 | if (assignedVlan == null) { |
| 1428 | log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort."); |
| 1429 | fail(fwd, ObjectiveError.BADPARAMS); |
| 1430 | return -1; |
| 1431 | } |
Charles Chan | d117263 | 2017-03-15 17:33:09 -0700 | [diff] [blame] | 1432 | if (requireVlanExtensions()) { |
| 1433 | OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan); |
| 1434 | builderToUpdate.extension(ofdpaMatchVlanVid, deviceId); |
| 1435 | } else { |
| 1436 | builderToUpdate.matchVlanId(assignedVlan); |
| 1437 | } |
Pier Luigi | 3bfe32c | 2017-01-30 09:47:36 -0800 | [diff] [blame] | 1438 | builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst); |
| 1439 | log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}" |
| 1440 | + " in dev:{}", fwd.id(), fwd.nextId(), deviceId); |
| 1441 | } else { |
| 1442 | if (ipv4Dst.prefixLength() == 0) { |
| 1443 | if (allowDefaultRoute) { |
| 1444 | // The entire IPV4_DST field is wildcarded intentionally |
| 1445 | builderToUpdate.matchEthType(Ethernet.TYPE_IPV4); |
| 1446 | } else { |
| 1447 | // NOTE: The switch does not support matching 0.0.0.0/0 |
| 1448 | // Split it into 0.0.0.0/1 and 128.0.0.0/1 |
| 1449 | builderToUpdate.matchEthType(Ethernet.TYPE_IPV4) |
| 1450 | .matchIPDst(IpPrefix.valueOf("0.0.0.0/1")); |
| 1451 | extBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 1452 | .matchIPDst(IpPrefix.valueOf("128.0.0.0/1")); |
| 1453 | } |
| 1454 | } else { |
| 1455 | builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst); |
| 1456 | } |
| 1457 | log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}" |
| 1458 | + " in dev:{}", fwd.id(), fwd.nextId(), deviceId); |
| 1459 | } |
| 1460 | return 0; |
| 1461 | } |
| 1462 | |
| 1463 | /** |
| 1464 | * Helper method to build Ipv6 selector using the selector provided by |
| 1465 | * a forwarding objective. |
| 1466 | * |
| 1467 | * @param builderToUpdate the builder to update |
| 1468 | * @param fwd the selector to read |
| 1469 | * @return 0 if the update ends correctly. -1 if the matches |
| 1470 | * are not yet supported |
| 1471 | */ |
Charles Chan | 9309035 | 2018-03-02 13:26:22 -0800 | [diff] [blame] | 1472 | int buildIpv6Selector(TrafficSelector.Builder builderToUpdate, |
Pier Luigi | 3bfe32c | 2017-01-30 09:47:36 -0800 | [diff] [blame] | 1473 | ForwardingObjective fwd) { |
| 1474 | |
| 1475 | TrafficSelector selector = fwd.selector(); |
| 1476 | |
| 1477 | IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip(); |
| 1478 | if (ipv6Dst.isMulticast()) { |
Julia Ferguson | 65428c3 | 2017-08-10 18:15:24 +0000 | [diff] [blame] | 1479 | if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) { |
| 1480 | log.warn("Multicast specific forwarding objective can only be /128"); |
| 1481 | fail(fwd, ObjectiveError.BADPARAMS); |
| 1482 | return -1; |
| 1483 | } |
| 1484 | VlanId assignedVlan = readVlanFromSelector(fwd.meta()); |
| 1485 | if (assignedVlan == null) { |
| 1486 | log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort."); |
| 1487 | fail(fwd, ObjectiveError.BADPARAMS); |
| 1488 | return -1; |
| 1489 | } |
| 1490 | if (requireVlanExtensions()) { |
| 1491 | OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan); |
| 1492 | builderToUpdate.extension(ofdpaMatchVlanVid, deviceId); |
| 1493 | } else { |
| 1494 | builderToUpdate.matchVlanId(assignedVlan); |
| 1495 | } |
| 1496 | builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst); |
| 1497 | log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}" |
| 1498 | + " in dev:{}", fwd.id(), fwd.nextId(), deviceId); |
| 1499 | } else { |
| 1500 | if (ipv6Dst.prefixLength() != 0) { |
| 1501 | builderToUpdate.matchIPv6Dst(ipv6Dst); |
| 1502 | } |
Pier Luigi | 3bfe32c | 2017-01-30 09:47:36 -0800 | [diff] [blame] | 1503 | builderToUpdate.matchEthType(Ethernet.TYPE_IPV6); |
| 1504 | log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}" |
| 1505 | + " in dev:{}", fwd.id(), fwd.nextId(), deviceId); |
Julia Ferguson | 65428c3 | 2017-08-10 18:15:24 +0000 | [diff] [blame] | 1506 | } |
Pier Luigi | 3bfe32c | 2017-01-30 09:47:36 -0800 | [diff] [blame] | 1507 | return 0; |
| 1508 | } |
| 1509 | |
Charles Chan | 9309035 | 2018-03-02 13:26:22 -0800 | [diff] [blame] | 1510 | FlowRule defaultRoute(ForwardingObjective fwd, |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 1511 | TrafficSelector.Builder complementarySelector, |
| 1512 | int forTableId, |
| 1513 | TrafficTreatment.Builder tb) { |
| 1514 | FlowRule.Builder rule = DefaultFlowRule.builder() |
| 1515 | .fromApp(fwd.appId()) |
| 1516 | .withPriority(fwd.priority()) |
| 1517 | .forDevice(deviceId) |
| 1518 | .withSelector(complementarySelector.build()) |
| 1519 | .withTreatment(tb.build()) |
| 1520 | .forTable(forTableId); |
| 1521 | if (fwd.permanent()) { |
| 1522 | rule.makePermanent(); |
| 1523 | } else { |
| 1524 | rule.makeTemporary(fwd.timeout()); |
| 1525 | } |
| 1526 | return rule.build(); |
| 1527 | } |
| 1528 | |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1529 | /** |
| 1530 | * Handles forwarding rules to the L2 bridging table. Flow actions are not |
| 1531 | * allowed in the bridging table - instead we use L2 Interface group or |
| 1532 | * L2 flood group |
| 1533 | * |
| 1534 | * @param fwd the forwarding objective |
| 1535 | * @return A collection of flow rules, or an empty set |
| 1536 | */ |
| 1537 | protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) { |
| 1538 | List<FlowRule> rules = new ArrayList<>(); |
| 1539 | |
| 1540 | // Build filtered selector |
| 1541 | TrafficSelector selector = fwd.selector(); |
| 1542 | EthCriterion ethCriterion = (EthCriterion) selector |
| 1543 | .getCriterion(Criterion.Type.ETH_DST); |
| 1544 | VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector |
| 1545 | .getCriterion(Criterion.Type.VLAN_VID); |
| 1546 | |
| 1547 | if (vlanIdCriterion == null) { |
| 1548 | log.warn("Forwarding objective for bridging requires vlan. Not " |
| 1549 | + "installing fwd:{} in dev:{}", fwd.id(), deviceId); |
| 1550 | fail(fwd, ObjectiveError.BADPARAMS); |
| 1551 | return Collections.emptySet(); |
| 1552 | } |
| 1553 | |
| 1554 | TrafficSelector.Builder filteredSelectorBuilder = |
| 1555 | DefaultTrafficSelector.builder(); |
Yi Tseng | 3a77b4f | 2017-02-06 15:02:17 -0800 | [diff] [blame] | 1556 | |
| 1557 | if (!ethCriterion.mac().equals(NONE) && |
| 1558 | !ethCriterion.mac().equals(BROADCAST)) { |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1559 | filteredSelectorBuilder.matchEthDst(ethCriterion.mac()); |
| 1560 | log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}", |
| 1561 | fwd.id(), fwd.nextId(), deviceId); |
| 1562 | } else { |
Yi Tseng | 3a77b4f | 2017-02-06 15:02:17 -0800 | [diff] [blame] | 1563 | // Use wildcard DST_MAC if the MacAddress is None or Broadcast |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1564 | log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} " |
| 1565 | + "in dev:{} for vlan:{}", |
| 1566 | fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId()); |
| 1567 | } |
Charles Chan | d117263 | 2017-03-15 17:33:09 -0700 | [diff] [blame] | 1568 | if (requireVlanExtensions()) { |
| 1569 | OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId()); |
| 1570 | filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId); |
| 1571 | } else { |
| 1572 | filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId()); |
| 1573 | } |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1574 | TrafficSelector filteredSelector = filteredSelectorBuilder.build(); |
| 1575 | |
| 1576 | if (fwd.treatment() != null) { |
| 1577 | log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table" |
| 1578 | + "for dev:{}. Expecting only nextId", fwd.id(), deviceId); |
| 1579 | } |
| 1580 | |
| 1581 | TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder(); |
| 1582 | if (fwd.nextId() != null) { |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 1583 | NextGroup next = getGroupForNextObjective(fwd.nextId()); |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1584 | if (next != null) { |
| 1585 | List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data()); |
| 1586 | // we only need the top level group's key to point the flow to it |
| 1587 | Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst()); |
| 1588 | if (group != null) { |
| 1589 | treatmentBuilder.deferred().group(group.id()); |
| 1590 | } else { |
| 1591 | log.warn("Group with key:{} for next-id:{} not found in dev:{}", |
| 1592 | gkeys.get(0).peekFirst(), fwd.nextId(), deviceId); |
| 1593 | fail(fwd, ObjectiveError.GROUPMISSING); |
| 1594 | return Collections.emptySet(); |
| 1595 | } |
| 1596 | } |
| 1597 | } |
| 1598 | treatmentBuilder.immediate().transition(ACL_TABLE); |
| 1599 | TrafficTreatment filteredTreatment = treatmentBuilder.build(); |
| 1600 | |
| 1601 | // Build bridging table entries |
| 1602 | FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder(); |
| 1603 | flowRuleBuilder.fromApp(fwd.appId()) |
| 1604 | .withPriority(fwd.priority()) |
| 1605 | .forDevice(deviceId) |
| 1606 | .withSelector(filteredSelector) |
| 1607 | .withTreatment(filteredTreatment) |
| 1608 | .forTable(BRIDGING_TABLE); |
| 1609 | if (fwd.permanent()) { |
| 1610 | flowRuleBuilder.makePermanent(); |
| 1611 | } else { |
| 1612 | flowRuleBuilder.makeTemporary(fwd.timeout()); |
| 1613 | } |
| 1614 | rules.add(flowRuleBuilder.build()); |
| 1615 | return rules; |
| 1616 | } |
| 1617 | |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1618 | ////////////////////////////////////// |
| 1619 | // Helper Methods and Classes |
| 1620 | ////////////////////////////////////// |
| 1621 | |
| 1622 | private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) { |
| 1623 | TrafficSelector selector = fwd.selector(); |
| 1624 | EthTypeCriterion ethType = (EthTypeCriterion) selector |
| 1625 | .getCriterion(Criterion.Type.ETH_TYPE); |
| 1626 | return !((ethType == null) || |
| 1627 | ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) && |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 1628 | (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) && |
| 1629 | (ethType.ethType().toShort() != Ethernet.TYPE_IPV6)); |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | private boolean isSupportedEthDstObjective(ForwardingObjective fwd) { |
| 1633 | TrafficSelector selector = fwd.selector(); |
| 1634 | EthCriterion ethDst = (EthCriterion) selector |
| 1635 | .getCriterion(Criterion.Type.ETH_DST); |
| 1636 | VlanIdCriterion vlanId = (VlanIdCriterion) selector |
| 1637 | .getCriterion(Criterion.Type.VLAN_VID); |
| 1638 | return !(ethDst == null && vlanId == null); |
| 1639 | } |
| 1640 | |
Charles Chan | 9309035 | 2018-03-02 13:26:22 -0800 | [diff] [blame] | 1641 | NextGroup getGroupForNextObjective(Integer nextId) { |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 1642 | NextGroup next = flowObjectiveStore.getNextGroup(nextId); |
| 1643 | if (next != null) { |
| 1644 | List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data()); |
| 1645 | if (gkeys != null && !gkeys.isEmpty()) { |
| 1646 | return next; |
| 1647 | } else { |
| 1648 | log.warn("Empty next group found in FlowObjective store for " |
| 1649 | + "next-id:{} in dev:{}", nextId, deviceId); |
| 1650 | } |
| 1651 | } else { |
| 1652 | log.warn("next-id {} not found in Flow objective store for dev:{}", |
| 1653 | nextId, deviceId); |
| 1654 | } |
| 1655 | return null; |
| 1656 | } |
| 1657 | |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 1658 | @Override |
| 1659 | public List<String> getNextMappings(NextGroup nextGroup) { |
| 1660 | List<String> mappings = new ArrayList<>(); |
| 1661 | List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data()); |
| 1662 | for (Deque<GroupKey> gkd : gkeys) { |
| 1663 | Group lastGroup = null; |
Yuta HIGUCHI | 2dce08a | 2017-04-20 21:57:48 -0700 | [diff] [blame] | 1664 | StringBuilder gchain = new StringBuilder(); |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 1665 | for (GroupKey gk : gkd) { |
| 1666 | Group g = groupService.getGroup(deviceId, gk); |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1667 | if (g == null) { |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 1668 | gchain.append(" NoGrp").append(" -->"); |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1669 | continue; |
| 1670 | } |
| 1671 | gchain.append(" 0x").append(Integer.toHexString(g.id().id())) |
| 1672 | .append(" -->"); |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 1673 | lastGroup = g; |
| 1674 | } |
| 1675 | // add port information for last group in group-chain |
Yuta HIGUCHI | 2dce08a | 2017-04-20 21:57:48 -0700 | [diff] [blame] | 1676 | List<Instruction> lastGroupIns = new ArrayList<>(); |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1677 | if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) { |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 1678 | lastGroupIns = lastGroup.buckets().buckets().get(0) |
| 1679 | .treatment().allInstructions(); |
| 1680 | } |
| 1681 | for (Instruction i: lastGroupIns) { |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 1682 | if (i instanceof OutputInstruction) { |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1683 | gchain.append(" port:").append(((OutputInstruction) i).port()); |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 1684 | } |
| 1685 | } |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1686 | mappings.add(gchain.toString()); |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 1687 | } |
| 1688 | return mappings; |
| 1689 | } |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1690 | |
Saurav Das | a89b95a | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 1691 | /** |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1692 | * Utility class that retries sending flows a fixed number of times, even if |
| 1693 | * some of the attempts are successful. Used only for forwarding objectives. |
| 1694 | */ |
Alex Yashchuk | a367953 | 2017-12-08 17:40:05 +0200 | [diff] [blame] | 1695 | public final class RetryFlows implements Runnable { |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1696 | int attempts = MAX_RETRY_ATTEMPTS; |
| 1697 | private Collection<FlowRule> retryFlows; |
| 1698 | private ForwardingObjective fwd; |
| 1699 | |
Charles Chan | 9309035 | 2018-03-02 13:26:22 -0800 | [diff] [blame] | 1700 | RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) { |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1701 | this.fwd = fwd; |
| 1702 | this.retryFlows = retryFlows; |
| 1703 | } |
| 1704 | |
| 1705 | @Override |
| 1706 | public void run() { |
| 1707 | log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}", |
| 1708 | MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size()); |
pier | 5b9b9e3 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 1709 | sendForwards(Collections.singletonList(Pair.of(fwd, retryFlows))); |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1710 | if (--attempts > 0) { |
pier | 5b9b9e3 | 2019-03-11 15:14:02 -0700 | [diff] [blame] | 1711 | retryExecutorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS); |
| 1712 | } |
| 1713 | } |
| 1714 | } |
| 1715 | |
| 1716 | // Flow rules accumulator for reducing the number of transactions required to the devices. |
| 1717 | private final class ForwardingObjectiveAccumulator |
| 1718 | extends AbstractAccumulator<Pair<ForwardingObjective, Collection<FlowRule>>> { |
| 1719 | |
| 1720 | ForwardingObjectiveAccumulator(int maxFwd, int maxBatchMS, int maxIdleMS) { |
| 1721 | super(TIMER, maxFwd, maxBatchMS, maxIdleMS); |
| 1722 | } |
| 1723 | |
| 1724 | @Override |
| 1725 | public void processItems(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) { |
| 1726 | // Triggers creation of a batch using the list of flowrules generated from fwdobjs. |
| 1727 | accumulatorExecutorService.execute(new FlowRulesBuilderTask(pairs)); |
| 1728 | } |
| 1729 | } |
| 1730 | |
| 1731 | // Task for building batch of flow rules in a separate thread. |
| 1732 | private final class FlowRulesBuilderTask implements Runnable { |
| 1733 | private final List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs; |
| 1734 | |
| 1735 | FlowRulesBuilderTask(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) { |
| 1736 | this.pairs = pairs; |
| 1737 | } |
| 1738 | |
| 1739 | @Override |
| 1740 | public void run() { |
| 1741 | try { |
| 1742 | sendForwards(pairs); |
| 1743 | } catch (Exception e) { |
| 1744 | log.warn("Unable to send forwards", e); |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1745 | } |
| 1746 | } |
| 1747 | } |
| 1748 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1749 | } |