Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
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 | */ |
| 16 | package org.onosproject.driver.pipeline; |
| 17 | |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 20 | import org.onlab.osgi.ServiceDirectory; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 21 | import org.onlab.packet.Ethernet; |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 22 | import org.onlab.packet.IpPrefix; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 23 | import org.onlab.packet.MacAddress; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 24 | import org.onlab.packet.VlanId; |
| 25 | import org.onlab.util.KryoNamespace; |
| 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.core.CoreService; |
Charles Chan | cad338a | 2016-09-16 18:03:11 -0700 | [diff] [blame] | 28 | import org.onosproject.driver.extensions.Ofdpa3MplsType; |
| 29 | import org.onosproject.driver.extensions.Ofdpa3SetMplsType; |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 30 | import org.onosproject.driver.extensions.OfdpaMatchVlanVid; |
| 31 | import org.onosproject.driver.extensions.OfdpaSetVlanVid; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 32 | import org.onosproject.net.DeviceId; |
| 33 | import org.onosproject.net.Port; |
| 34 | import org.onosproject.net.PortNumber; |
| 35 | import org.onosproject.net.behaviour.NextGroup; |
| 36 | import org.onosproject.net.behaviour.Pipeliner; |
| 37 | import org.onosproject.net.behaviour.PipelinerContext; |
| 38 | import org.onosproject.net.device.DeviceService; |
| 39 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 40 | import org.onosproject.net.flow.DefaultFlowRule; |
| 41 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 42 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 43 | import org.onosproject.net.flow.FlowRule; |
| 44 | import org.onosproject.net.flow.FlowRuleOperations; |
| 45 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 46 | import org.onosproject.net.flow.FlowRuleService; |
| 47 | import org.onosproject.net.flow.TrafficSelector; |
| 48 | import org.onosproject.net.flow.TrafficTreatment; |
| 49 | import org.onosproject.net.flow.criteria.Criteria; |
| 50 | import org.onosproject.net.flow.criteria.Criterion; |
| 51 | import org.onosproject.net.flow.criteria.EthCriterion; |
| 52 | import org.onosproject.net.flow.criteria.EthTypeCriterion; |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 53 | import org.onosproject.net.flow.criteria.ExtensionCriterion; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 54 | import org.onosproject.net.flow.criteria.IPCriterion; |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 55 | import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion; |
| 56 | import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 57 | import org.onosproject.net.flow.criteria.MplsBosCriterion; |
| 58 | import org.onosproject.net.flow.criteria.MplsCriterion; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 59 | import org.onosproject.net.flow.criteria.PortCriterion; |
| 60 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
| 61 | import org.onosproject.net.flow.instructions.Instruction; |
| 62 | import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
| 63 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 64 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 65 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction; |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 66 | import org.onosproject.net.flow.instructions.L3ModificationInstruction; |
| 67 | import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 68 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 69 | import org.onosproject.net.flowobjective.FlowObjectiveStore; |
| 70 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 71 | import org.onosproject.net.flowobjective.NextObjective; |
| 72 | import org.onosproject.net.flowobjective.Objective; |
| 73 | import org.onosproject.net.flowobjective.ObjectiveError; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 74 | import org.onosproject.net.group.DefaultGroupKey; |
| 75 | import org.onosproject.net.group.Group; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 76 | import org.onosproject.net.group.GroupKey; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 77 | import org.onosproject.net.group.GroupService; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 78 | import org.onosproject.store.serializers.KryoNamespaces; |
| 79 | import org.slf4j.Logger; |
| 80 | |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 81 | import java.util.ArrayDeque; |
| 82 | import java.util.ArrayList; |
| 83 | import java.util.Collection; |
| 84 | import java.util.Collections; |
| 85 | import java.util.Deque; |
| 86 | import java.util.List; |
| 87 | import java.util.Objects; |
| 88 | import java.util.Set; |
| 89 | import java.util.concurrent.ConcurrentHashMap; |
| 90 | import java.util.concurrent.ScheduledExecutorService; |
| 91 | import java.util.concurrent.TimeUnit; |
| 92 | |
| 93 | import static java.util.concurrent.Executors.newScheduledThreadPool; |
| 94 | import static org.onlab.util.Tools.groupedThreads; |
| 95 | import static org.slf4j.LoggerFactory.getLogger; |
| 96 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 97 | /** |
| 98 | * Driver for Broadcom's OF-DPA v2.0 TTP. |
| 99 | * |
| 100 | */ |
Charles Chan | 361154b | 2016-03-24 10:23:39 -0700 | [diff] [blame] | 101 | public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner { |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 102 | protected static final int PORT_TABLE = 0; |
| 103 | protected static final int VLAN_TABLE = 10; |
| 104 | protected static final int TMAC_TABLE = 20; |
| 105 | protected static final int UNICAST_ROUTING_TABLE = 30; |
| 106 | protected static final int MULTICAST_ROUTING_TABLE = 40; |
| 107 | protected static final int MPLS_TABLE_0 = 23; |
| 108 | protected static final int MPLS_TABLE_1 = 24; |
Charles Chan | cad338a | 2016-09-16 18:03:11 -0700 | [diff] [blame] | 109 | protected static final int MPLS_L3_TYPE = 27; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 110 | protected static final int BRIDGING_TABLE = 50; |
| 111 | protected static final int ACL_TABLE = 60; |
| 112 | protected static final int MAC_LEARNING_TABLE = 254; |
| 113 | protected static final long OFPP_MAX = 0xffffff00L; |
| 114 | |
Saurav Das | 5202596 | 2016-01-28 22:30:01 -0800 | [diff] [blame] | 115 | protected static final int HIGHEST_PRIORITY = 0xffff; |
Saurav Das | 2857f38 | 2015-11-03 14:39:27 -0800 | [diff] [blame] | 116 | protected static final int DEFAULT_PRIORITY = 0x8000; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 117 | protected static final int LOWEST_PRIORITY = 0x0; |
| 118 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 119 | private final Logger log = getLogger(getClass()); |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 120 | protected ServiceDirectory serviceDirectory; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 121 | protected FlowRuleService flowRuleService; |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 122 | protected CoreService coreService; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 123 | protected GroupService groupService; |
| 124 | protected FlowObjectiveStore flowObjectiveStore; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 125 | protected DeviceId deviceId; |
| 126 | protected ApplicationId driverId; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 127 | protected DeviceService deviceService; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 128 | protected static KryoNamespace appKryo = new KryoNamespace.Builder() |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 129 | .register(KryoNamespaces.API) |
| 130 | .register(GroupKey.class) |
| 131 | .register(DefaultGroupKey.class) |
Charles Chan | 361154b | 2016-03-24 10:23:39 -0700 | [diff] [blame] | 132 | .register(Ofdpa2GroupHandler.OfdpaNextGroup.class) |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 133 | .register(ArrayDeque.class) |
Charles Chan | eefdedf | 2016-05-23 16:45:45 -0700 | [diff] [blame] | 134 | .build("Ofdpa2Pipeline"); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 135 | |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 136 | protected Ofdpa2GroupHandler groupHandler; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 137 | |
Saurav Das | 5202596 | 2016-01-28 22:30:01 -0800 | [diff] [blame] | 138 | protected Set<IPCriterion> sentIpFilters = Collections.newSetFromMap( |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 139 | new ConcurrentHashMap<>()); |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 140 | // flows installations to be retried |
| 141 | protected ScheduledExecutorService executorService |
| 142 | = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log)); |
| 143 | protected static final int MAX_RETRY_ATTEMPTS = 10; |
| 144 | protected static final int RETRY_MS = 1000; |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 145 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 146 | @Override |
| 147 | public void init(DeviceId deviceId, PipelinerContext context) { |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 148 | this.deviceId = deviceId; |
| 149 | |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 150 | serviceDirectory = context.directory(); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 151 | coreService = serviceDirectory.get(CoreService.class); |
| 152 | flowRuleService = serviceDirectory.get(FlowRuleService.class); |
| 153 | groupService = serviceDirectory.get(GroupService.class); |
| 154 | flowObjectiveStore = context.store(); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 155 | deviceService = serviceDirectory.get(DeviceService.class); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 156 | |
Charles Chan | 40132b3 | 2017-01-22 00:19:37 -0800 | [diff] [blame] | 157 | initDriverId(); |
| 158 | initGroupHander(context); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 159 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 160 | initializePipeline(); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Charles Chan | 40132b3 | 2017-01-22 00:19:37 -0800 | [diff] [blame] | 163 | protected void initDriverId() { |
| 164 | driverId = coreService.registerApplication( |
| 165 | "org.onosproject.driver.Ofdpa2Pipeline"); |
| 166 | } |
| 167 | |
| 168 | protected void initGroupHander(PipelinerContext context) { |
| 169 | groupHandler = new Ofdpa2GroupHandler(); |
| 170 | groupHandler.init(deviceId, context); |
| 171 | } |
| 172 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 173 | protected void initializePipeline() { |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 174 | // OF-DPA does not require initializing the pipeline as it puts default |
| 175 | // rules automatically in the hardware. However emulation of OFDPA in |
| 176 | // software switches does require table-miss-entries. |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | ////////////////////////////////////// |
| 180 | // Flow Objectives |
| 181 | ////////////////////////////////////// |
| 182 | |
| 183 | @Override |
| 184 | public void filter(FilteringObjective filteringObjective) { |
| 185 | if (filteringObjective.type() == FilteringObjective.Type.PERMIT) { |
| 186 | processFilter(filteringObjective, |
| 187 | filteringObjective.op() == Objective.Operation.ADD, |
| 188 | filteringObjective.appId()); |
| 189 | } else { |
| 190 | // Note that packets that don't match the PERMIT filter are |
| 191 | // automatically denied. The DENY filter is used to deny packets |
| 192 | // that are otherwise permitted by the PERMIT filter. |
| 193 | // Use ACL table flow rules here for DENY filtering objectives |
| 194 | log.debug("filter objective other than PERMIT currently not supported"); |
| 195 | fail(filteringObjective, ObjectiveError.UNSUPPORTED); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public void forward(ForwardingObjective fwd) { |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 201 | Collection<FlowRule> rules = processForward(fwd); |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 202 | if (rules == null || rules.isEmpty()) { |
| 203 | // Assumes fail message has already been generated to the objective |
| 204 | // context. Returning here prevents spurious pass message to be |
| 205 | // generated by FlowRule service for empty flowOps. |
| 206 | return; |
| 207 | } |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 208 | sendForward(fwd, rules); |
| 209 | } |
| 210 | |
| 211 | protected void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) { |
| 212 | FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder(); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 213 | switch (fwd.op()) { |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 214 | case ADD: |
| 215 | rules.stream() |
| 216 | .filter(Objects::nonNull) |
| 217 | .forEach(flowOpsBuilder::add); |
Saurav Das | d2fded0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 218 | log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId); |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 219 | break; |
| 220 | case REMOVE: |
| 221 | rules.stream() |
| 222 | .filter(Objects::nonNull) |
| 223 | .forEach(flowOpsBuilder::remove); |
| 224 | break; |
| 225 | default: |
| 226 | fail(fwd, ObjectiveError.UNKNOWN); |
| 227 | log.warn("Unknown forwarding type {}", fwd.op()); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 230 | flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() { |
| 231 | @Override |
| 232 | public void onSuccess(FlowRuleOperations ops) { |
| 233 | pass(fwd); |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public void onError(FlowRuleOperations ops) { |
| 238 | fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 239 | } |
| 240 | })); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | @Override |
| 244 | public void next(NextObjective nextObjective) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 245 | NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id()); |
| 246 | switch (nextObjective.op()) { |
| 247 | case ADD: |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 248 | if (nextGroup != null) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 249 | log.warn("Cannot add next {} that already exists in device {}", |
| 250 | nextObjective.id(), deviceId); |
| 251 | return; |
| 252 | } |
| 253 | log.debug("Processing NextObjective id{} in dev{} - add group", |
| 254 | nextObjective.id(), deviceId); |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 255 | groupHandler.addGroup(nextObjective); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 256 | break; |
| 257 | case ADD_TO_EXISTING: |
| 258 | if (nextGroup != null) { |
| 259 | log.debug("Processing NextObjective id{} in dev{} - add bucket", |
| 260 | nextObjective.id(), deviceId); |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 261 | groupHandler.addBucketToGroup(nextObjective, nextGroup); |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 262 | } else { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 263 | // it is possible that group-chain has not been fully created yet |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 264 | log.debug("Waiting to add bucket to group for next-id:{} in dev:{}", |
| 265 | nextObjective.id(), deviceId); |
| 266 | // by design only one pending bucket is allowed for the group |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 267 | groupHandler.pendingBuckets.put(nextObjective.id(), nextObjective); |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 268 | } |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 269 | break; |
| 270 | case REMOVE: |
| 271 | if (nextGroup == null) { |
| 272 | log.warn("Cannot remove next {} that does not exist in device {}", |
| 273 | nextObjective.id(), deviceId); |
| 274 | return; |
| 275 | } |
| 276 | log.debug("Processing NextObjective id{} in dev{} - remove group", |
| 277 | nextObjective.id(), deviceId); |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 278 | groupHandler.removeGroup(nextObjective, nextGroup); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 279 | break; |
| 280 | case REMOVE_FROM_EXISTING: |
| 281 | if (nextGroup == null) { |
| 282 | log.warn("Cannot remove from next {} that does not exist in device {}", |
| 283 | nextObjective.id(), deviceId); |
| 284 | return; |
| 285 | } |
| 286 | log.debug("Processing NextObjective id{} in dev{} - remove bucket", |
| 287 | nextObjective.id(), deviceId); |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 288 | groupHandler.removeBucketFromGroup(nextObjective, nextGroup); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 289 | break; |
| 290 | default: |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 291 | log.warn("Unsupported operation {}", nextObjective.op()); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
| 295 | ////////////////////////////////////// |
| 296 | // Flow handling |
| 297 | ////////////////////////////////////// |
| 298 | |
| 299 | /** |
| 300 | * As per OFDPA 2.0 TTP, filtering of VLAN ids, MAC addresses (for routing) |
| 301 | * and IP addresses configured on switch ports happen in different tables. |
| 302 | * Note that IP filtering rules need to be added to the ACL table, as there |
| 303 | * is no mechanism to send to controller via IP table. |
| 304 | * |
| 305 | * @param filt the filtering objective |
| 306 | * @param install indicates whether to add or remove the objective |
| 307 | * @param applicationId the application that sent this objective |
| 308 | */ |
Saurav Das | 5202596 | 2016-01-28 22:30:01 -0800 | [diff] [blame] | 309 | protected void processFilter(FilteringObjective filt, |
| 310 | boolean install, ApplicationId applicationId) { |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 311 | // This driver only processes filtering criteria defined with switch |
| 312 | // ports as the key |
| 313 | PortCriterion portCriterion = null; |
| 314 | EthCriterion ethCriterion = null; |
| 315 | VlanIdCriterion vidCriterion = null; |
| 316 | Collection<IPCriterion> ips = new ArrayList<IPCriterion>(); |
| 317 | if (!filt.key().equals(Criteria.dummy()) && |
| 318 | filt.key().type() == Criterion.Type.IN_PORT) { |
| 319 | portCriterion = (PortCriterion) filt.key(); |
| 320 | } else { |
| 321 | log.warn("No key defined in filtering objective from app: {}. Not" |
| 322 | + "processing filtering objective", applicationId); |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 323 | fail(filt, ObjectiveError.BADPARAMS); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 324 | return; |
| 325 | } |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 326 | log.debug("Received filtering objective for dev/port: {}/{}", deviceId, |
| 327 | portCriterion.port()); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 328 | // convert filtering conditions for switch-intfs into flowrules |
| 329 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 330 | for (Criterion criterion : filt.conditions()) { |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 331 | if (criterion.type() == Criterion.Type.ETH_DST || |
| 332 | criterion.type() == Criterion.Type.ETH_DST_MASKED) { |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 333 | ethCriterion = (EthCriterion) criterion; |
| 334 | } else if (criterion.type() == Criterion.Type.VLAN_VID) { |
| 335 | vidCriterion = (VlanIdCriterion) criterion; |
| 336 | } else if (criterion.type() == Criterion.Type.IPV4_DST) { |
| 337 | ips.add((IPCriterion) criterion); |
| 338 | } else { |
| 339 | log.error("Unsupported filter {}", criterion); |
| 340 | fail(filt, ObjectiveError.UNSUPPORTED); |
| 341 | return; |
| 342 | } |
| 343 | } |
| 344 | |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 345 | VlanId assignedVlan = null; |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 346 | if (vidCriterion != null) { |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 347 | // Use the VLAN in metadata whenever a metadata is provided |
| 348 | if (filt.meta() != null) { |
| 349 | assignedVlan = readVlanFromTreatment(filt.meta()); |
| 350 | // Use the VLAN in criterion if metadata is not present and the traffic is tagged |
| 351 | } else if (!vidCriterion.vlanId().equals(VlanId.NONE)) { |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 352 | assignedVlan = vidCriterion.vlanId(); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 353 | } |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 354 | |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 355 | if (assignedVlan == null) { |
| 356 | log.error("Driver fails to extract VLAN information. " |
| 357 | + "Not proccessing VLAN filters on device {}.", deviceId); |
| 358 | log.debug("VLAN ID in criterion={}, metadata={}", |
| 359 | readVlanFromTreatment(filt.meta()), vidCriterion.vlanId()); |
| 360 | fail(filt, ObjectiveError.BADPARAMS); |
| 361 | return; |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 365 | if (ethCriterion == null || ethCriterion.mac().equals(MacAddress.NONE)) { |
Charles Chan | 985b12e | 2016-05-11 19:47:22 -0700 | [diff] [blame] | 366 | // NOTE: it is possible that a filtering objective only has vidCriterion |
| 367 | log.debug("filtering objective missing dstMac, cannot program TMAC table"); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 368 | } else { |
| 369 | for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion, |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 370 | vidCriterion, assignedVlan, |
| 371 | applicationId)) { |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 372 | log.debug("adding MAC filtering rules in TMAC table: {} for dev: {}", |
| 373 | tmacRule, deviceId); |
| 374 | ops = install ? ops.add(tmacRule) : ops.remove(tmacRule); |
| 375 | } |
| 376 | } |
| 377 | |
Charles Chan | 985b12e | 2016-05-11 19:47:22 -0700 | [diff] [blame] | 378 | if (vidCriterion == null) { |
| 379 | // NOTE: it is possible that a filtering objective only has ethCriterion |
| 380 | log.debug("filtering objective missing dstMac or VLAN, " |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 381 | + "cannot program VLAN Table"); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 382 | } else { |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 383 | /* |
| 384 | * NOTE: Separate vlan filtering rules and assignment rules |
| 385 | * into different stage in order to guarantee that filtering rules |
Saurav Das | 5202596 | 2016-01-28 22:30:01 -0800 | [diff] [blame] | 386 | * always go first, as required by ofdpa. |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 387 | */ |
| 388 | List<FlowRule> allRules = processVlanIdFilter( |
| 389 | portCriterion, vidCriterion, assignedVlan, applicationId); |
| 390 | List<FlowRule> filteringRules = new ArrayList<>(); |
| 391 | List<FlowRule> assignmentRules = new ArrayList<>(); |
| 392 | |
| 393 | allRules.forEach(flowRule -> { |
| 394 | ExtensionCriterion extCriterion = |
| 395 | (ExtensionCriterion) flowRule.selector().getCriterion(Criterion.Type.EXTENSION); |
| 396 | VlanId vlanId = ((OfdpaMatchVlanVid) extCriterion.extensionSelector()).vlanId(); |
Charles Chan | be8aea4 | 2016-02-24 12:04:47 -0800 | [diff] [blame] | 397 | if (!vlanId.equals(VlanId.NONE)) { |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 398 | filteringRules.add(flowRule); |
| 399 | } else { |
| 400 | assignmentRules.add(flowRule); |
| 401 | } |
| 402 | }); |
| 403 | |
| 404 | for (FlowRule filteringRule : filteringRules) { |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 405 | log.debug("adding VLAN filtering rule in VLAN table: {} for dev: {}", |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 406 | filteringRule, deviceId); |
| 407 | ops = install ? ops.add(filteringRule) : ops.remove(filteringRule); |
| 408 | } |
| 409 | |
| 410 | ops.newStage(); |
| 411 | |
| 412 | for (FlowRule assignmentRule : assignmentRules) { |
| 413 | log.debug("adding VLAN assignment rule in VLAN table: {} for dev: {}", |
| 414 | assignmentRule, deviceId); |
| 415 | ops = install ? ops.add(assignmentRule) : ops.remove(assignmentRule); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
| 419 | for (IPCriterion ipaddr : ips) { |
| 420 | // since we ignore port information for IP rules, and the same (gateway) IP |
| 421 | // can be configured on multiple ports, we make sure that we send |
| 422 | // only a single rule to the switch. |
| 423 | if (!sentIpFilters.contains(ipaddr)) { |
| 424 | sentIpFilters.add(ipaddr); |
| 425 | log.debug("adding IP filtering rules in ACL table {} for dev: {}", |
| 426 | ipaddr, deviceId); |
| 427 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 428 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 429 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 430 | selector.matchIPDst(ipaddr.ip()); |
| 431 | treatment.setOutput(PortNumber.CONTROLLER); |
| 432 | FlowRule rule = DefaultFlowRule.builder() |
| 433 | .forDevice(deviceId) |
| 434 | .withSelector(selector.build()) |
| 435 | .withTreatment(treatment.build()) |
| 436 | .withPriority(HIGHEST_PRIORITY) |
| 437 | .fromApp(applicationId) |
| 438 | .makePermanent() |
| 439 | .forTable(ACL_TABLE).build(); |
| 440 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | // apply filtering flow rules |
| 445 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 446 | @Override |
| 447 | public void onSuccess(FlowRuleOperations ops) { |
| 448 | log.info("Applied {} filtering rules in device {}", |
| 449 | ops.stages().get(0).size(), deviceId); |
| 450 | pass(filt); |
| 451 | } |
| 452 | |
| 453 | @Override |
| 454 | public void onError(FlowRuleOperations ops) { |
| 455 | log.info("Failed to apply all filtering rules in dev {}", deviceId); |
| 456 | fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 457 | } |
| 458 | })); |
| 459 | |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * Allows untagged packets into pipeline by assigning a vlan id. |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 464 | * Vlan assignment is done by the application. |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 465 | * Allows tagged packets into pipeline as per configured port-vlan info. |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 466 | * |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 467 | * @param portCriterion port on device for which this filter is programmed |
| 468 | * @param vidCriterion vlan assigned to port, or NONE for untagged |
| 469 | * @param assignedVlan assigned vlan-id for untagged packets |
| 470 | * @param applicationId for application programming this filter |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 471 | * @return list of FlowRule for port-vlan filters |
| 472 | */ |
| 473 | protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion, |
| 474 | VlanIdCriterion vidCriterion, |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 475 | VlanId assignedVlan, |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 476 | ApplicationId applicationId) { |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 477 | return processVlanIdFilterInternal(portCriterion, vidCriterion, assignedVlan, |
| 478 | applicationId, true); |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * Internal implementation of processVlanIdFilter. |
| 483 | * <p> |
| 484 | * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12. |
| 485 | * Since it is non-OF spec, we need an extension treatment for that. |
| 486 | * The useSetVlanExtension must be set to false for OFDPA i12. |
| 487 | * </p> |
| 488 | * |
| 489 | * @param portCriterion port on device for which this filter is programmed |
| 490 | * @param vidCriterion vlan assigned to port, or NONE for untagged |
| 491 | * @param assignedVlan assigned vlan-id for untagged packets |
| 492 | * @param applicationId for application programming this filter |
| 493 | * @param useSetVlanExtension use the setVlanVid extension that has is_present bit set to 0. |
| 494 | * @return list of FlowRule for port-vlan filters |
| 495 | */ |
| 496 | protected List<FlowRule> processVlanIdFilterInternal(PortCriterion portCriterion, |
| 497 | VlanIdCriterion vidCriterion, |
| 498 | VlanId assignedVlan, |
| 499 | ApplicationId applicationId, |
| 500 | boolean useSetVlanExtension) { |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 501 | List<FlowRule> rules = new ArrayList<>(); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 502 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 503 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 504 | TrafficSelector.Builder preSelector = null; |
| 505 | TrafficTreatment.Builder preTreatment = null; |
| 506 | |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 507 | treatment.transition(TMAC_TABLE); |
| 508 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 509 | if (vidCriterion.vlanId() == VlanId.NONE) { |
| 510 | // untagged packets are assigned vlans |
Charles Chan | be8aea4 | 2016-02-24 12:04:47 -0800 | [diff] [blame] | 511 | OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE); |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 512 | selector.extension(ofdpaMatchVlanVid, deviceId); |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 513 | if (useSetVlanExtension) { |
| 514 | OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan); |
| 515 | treatment.extension(ofdpaSetVlanVid, deviceId); |
| 516 | } else { |
| 517 | treatment.setVlanId(assignedVlan); |
| 518 | } |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 519 | |
| 520 | preSelector = DefaultTrafficSelector.builder(); |
| 521 | OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan); |
| 522 | preSelector.extension(preOfdpaMatchVlanVid, deviceId); |
| 523 | preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE); |
| 524 | |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 525 | } else { |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 526 | OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId()); |
| 527 | selector.extension(ofdpaMatchVlanVid, deviceId); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 528 | |
| 529 | if (!assignedVlan.equals(vidCriterion.vlanId())) { |
Charles Chan | c03782d | 2017-01-31 13:57:55 -0800 | [diff] [blame^] | 530 | if (useSetVlanExtension) { |
| 531 | OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan); |
| 532 | treatment.extension(ofdpaSetVlanVid, deviceId); |
| 533 | } else { |
| 534 | treatment.setVlanId(assignedVlan); |
| 535 | } |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 536 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 537 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 538 | |
| 539 | // ofdpa cannot match on ALL portnumber, so we need to use separate |
| 540 | // rules for each port. |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 541 | List<PortNumber> portnums = new ArrayList<>(); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 542 | if (portCriterion.port() == PortNumber.ALL) { |
| 543 | for (Port port : deviceService.getPorts(deviceId)) { |
| 544 | if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) { |
| 545 | portnums.add(port.number()); |
| 546 | } |
| 547 | } |
| 548 | } else { |
| 549 | portnums.add(portCriterion.port()); |
| 550 | } |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 551 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 552 | for (PortNumber pnum : portnums) { |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 553 | // create rest of flowrule |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 554 | selector.matchInPort(pnum); |
| 555 | FlowRule rule = DefaultFlowRule.builder() |
| 556 | .forDevice(deviceId) |
| 557 | .withSelector(selector.build()) |
| 558 | .withTreatment(treatment.build()) |
| 559 | .withPriority(DEFAULT_PRIORITY) |
| 560 | .fromApp(applicationId) |
| 561 | .makePermanent() |
| 562 | .forTable(VLAN_TABLE).build(); |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 563 | |
| 564 | if (preSelector != null) { |
| 565 | preSelector.matchInPort(pnum); |
| 566 | FlowRule preRule = DefaultFlowRule.builder() |
| 567 | .forDevice(deviceId) |
| 568 | .withSelector(preSelector.build()) |
| 569 | .withTreatment(preTreatment.build()) |
| 570 | .withPriority(DEFAULT_PRIORITY) |
| 571 | .fromApp(applicationId) |
| 572 | .makePermanent() |
| 573 | .forTable(VLAN_TABLE).build(); |
| 574 | rules.add(preRule); |
| 575 | } |
| 576 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 577 | rules.add(rule); |
| 578 | } |
| 579 | return rules; |
| 580 | } |
| 581 | |
| 582 | /** |
| 583 | * Allows routed packets with correct destination MAC to be directed |
| 584 | * to unicast-IP routing table or MPLS forwarding table. |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 585 | * |
| 586 | * @param portCriterion port on device for which this filter is programmed |
| 587 | * @param ethCriterion dstMac of device for which is filter is programmed |
| 588 | * @param vidCriterion vlan assigned to port, or NONE for untagged |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 589 | * @param assignedVlan assigned vlan-id for untagged packets |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 590 | * @param applicationId for application programming this filter |
| 591 | * @return list of FlowRule for port-vlan filters |
| 592 | |
| 593 | */ |
| 594 | protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion, |
| 595 | EthCriterion ethCriterion, |
| 596 | VlanIdCriterion vidCriterion, |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 597 | VlanId assignedVlan, |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 598 | ApplicationId applicationId) { |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 599 | // Consider PortNumber.ANY as wildcard. Match ETH_DST only |
| 600 | if (portCriterion != null && portCriterion.port() == PortNumber.ANY) { |
| 601 | return processEthDstOnlyFilter(ethCriterion, applicationId); |
| 602 | } |
| 603 | |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 604 | // Multicast MAC |
| 605 | if (ethCriterion.mask() != null) { |
| 606 | return processMcastEthDstFilter(ethCriterion, applicationId); |
| 607 | } |
| 608 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 609 | //handling untagged packets via assigned VLAN |
| 610 | if (vidCriterion.vlanId() == VlanId.NONE) { |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 611 | vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 612 | } |
| 613 | // ofdpa cannot match on ALL portnumber, so we need to use separate |
| 614 | // rules for each port. |
| 615 | List<PortNumber> portnums = new ArrayList<PortNumber>(); |
| 616 | if (portCriterion.port() == PortNumber.ALL) { |
| 617 | for (Port port : deviceService.getPorts(deviceId)) { |
| 618 | if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) { |
| 619 | portnums.add(port.number()); |
| 620 | } |
| 621 | } |
| 622 | } else { |
| 623 | portnums.add(portCriterion.port()); |
| 624 | } |
| 625 | |
| 626 | List<FlowRule> rules = new ArrayList<FlowRule>(); |
| 627 | for (PortNumber pnum : portnums) { |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 628 | OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId()); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 629 | // for unicast IP packets |
| 630 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 631 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 632 | selector.matchInPort(pnum); |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 633 | selector.extension(ofdpaMatchVlanVid, deviceId); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 634 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 635 | selector.matchEthDst(ethCriterion.mac()); |
| 636 | treatment.transition(UNICAST_ROUTING_TABLE); |
| 637 | FlowRule rule = DefaultFlowRule.builder() |
| 638 | .forDevice(deviceId) |
| 639 | .withSelector(selector.build()) |
| 640 | .withTreatment(treatment.build()) |
| 641 | .withPriority(DEFAULT_PRIORITY) |
| 642 | .fromApp(applicationId) |
| 643 | .makePermanent() |
| 644 | .forTable(TMAC_TABLE).build(); |
| 645 | rules.add(rule); |
| 646 | //for MPLS packets |
| 647 | selector = DefaultTrafficSelector.builder(); |
| 648 | treatment = DefaultTrafficTreatment.builder(); |
| 649 | selector.matchInPort(pnum); |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 650 | selector.extension(ofdpaMatchVlanVid, deviceId); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 651 | selector.matchEthType(Ethernet.MPLS_UNICAST); |
| 652 | selector.matchEthDst(ethCriterion.mac()); |
| 653 | treatment.transition(MPLS_TABLE_0); |
| 654 | rule = DefaultFlowRule.builder() |
| 655 | .forDevice(deviceId) |
| 656 | .withSelector(selector.build()) |
| 657 | .withTreatment(treatment.build()) |
| 658 | .withPriority(DEFAULT_PRIORITY) |
| 659 | .fromApp(applicationId) |
| 660 | .makePermanent() |
| 661 | .forTable(TMAC_TABLE).build(); |
| 662 | rules.add(rule); |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 663 | /* |
| 664 | * TMAC rules for IPv6 packets |
| 665 | */ |
| 666 | selector = DefaultTrafficSelector.builder(); |
| 667 | treatment = DefaultTrafficTreatment.builder(); |
| 668 | selector.matchInPort(pnum); |
| 669 | selector.extension(ofdpaMatchVlanVid, deviceId); |
| 670 | selector.matchEthType(Ethernet.TYPE_IPV6); |
| 671 | selector.matchEthDst(ethCriterion.mac()); |
| 672 | treatment.transition(UNICAST_ROUTING_TABLE); |
| 673 | rule = DefaultFlowRule.builder() |
| 674 | .forDevice(deviceId) |
| 675 | .withSelector(selector.build()) |
| 676 | .withTreatment(treatment.build()) |
| 677 | .withPriority(DEFAULT_PRIORITY) |
| 678 | .fromApp(applicationId) |
| 679 | .makePermanent() |
| 680 | .forTable(TMAC_TABLE).build(); |
| 681 | rules.add(rule); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 682 | } |
| 683 | return rules; |
| 684 | } |
| 685 | |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 686 | protected List<FlowRule> processEthDstOnlyFilter(EthCriterion ethCriterion, |
| 687 | ApplicationId applicationId) { |
| 688 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 689 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 690 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 691 | selector.matchEthDst(ethCriterion.mac()); |
| 692 | treatment.transition(UNICAST_ROUTING_TABLE); |
| 693 | FlowRule rule = DefaultFlowRule.builder() |
| 694 | .forDevice(deviceId) |
| 695 | .withSelector(selector.build()) |
| 696 | .withTreatment(treatment.build()) |
| 697 | .withPriority(DEFAULT_PRIORITY) |
| 698 | .fromApp(applicationId) |
| 699 | .makePermanent() |
| 700 | .forTable(TMAC_TABLE).build(); |
| 701 | return ImmutableList.<FlowRule>builder().add(rule).build(); |
| 702 | } |
| 703 | |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 704 | protected List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion, |
| 705 | ApplicationId applicationId) { |
| 706 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 707 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 708 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 709 | selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask()); |
| 710 | treatment.transition(MULTICAST_ROUTING_TABLE); |
| 711 | FlowRule rule = DefaultFlowRule.builder() |
| 712 | .forDevice(deviceId) |
| 713 | .withSelector(selector.build()) |
| 714 | .withTreatment(treatment.build()) |
| 715 | .withPriority(DEFAULT_PRIORITY) |
| 716 | .fromApp(applicationId) |
| 717 | .makePermanent() |
| 718 | .forTable(TMAC_TABLE).build(); |
| 719 | return ImmutableList.<FlowRule>builder().add(rule).build(); |
| 720 | } |
| 721 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 722 | private Collection<FlowRule> processForward(ForwardingObjective fwd) { |
| 723 | switch (fwd.flag()) { |
| 724 | case SPECIFIC: |
| 725 | return processSpecific(fwd); |
| 726 | case VERSATILE: |
| 727 | return processVersatile(fwd); |
| 728 | default: |
| 729 | fail(fwd, ObjectiveError.UNKNOWN); |
| 730 | log.warn("Unknown forwarding flag {}", fwd.flag()); |
| 731 | } |
| 732 | return Collections.emptySet(); |
| 733 | } |
| 734 | |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 735 | |
| 736 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 737 | /** |
| 738 | * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the |
| 739 | * ACL table. |
| 740 | * @param fwd the forwarding objective of type 'versatile' |
| 741 | * @return a collection of flow rules to be sent to the switch. An empty |
| 742 | * collection may be returned if there is a problem in processing |
| 743 | * the flow rule |
| 744 | */ |
Saurav Das | 5202596 | 2016-01-28 22:30:01 -0800 | [diff] [blame] | 745 | protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) { |
Saurav Das | d2fded0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 746 | log.info("Processing versatile forwarding objective:{} in dev:{}", |
| 747 | fwd.id(), deviceId); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 748 | |
| 749 | EthTypeCriterion ethType = |
Saurav Das | 77b5e90 | 2016-01-27 17:01:59 -0800 | [diff] [blame] | 750 | (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 751 | if (ethType == null) { |
Saurav Das | d2fded0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 752 | log.error("Versatile forwarding objective:{} must include ethType", |
| 753 | fwd.id()); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 754 | fail(fwd, ObjectiveError.BADPARAMS); |
| 755 | return Collections.emptySet(); |
| 756 | } |
| 757 | if (fwd.nextId() == null && fwd.treatment() == null) { |
| 758 | log.error("Forwarding objective {} from {} must contain " |
| 759 | + "nextId or Treatment", fwd.selector(), fwd.appId()); |
Zsolt Haraszti | 9faab75 | 2016-02-17 15:55:20 -0800 | [diff] [blame] | 760 | fail(fwd, ObjectiveError.BADPARAMS); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 761 | return Collections.emptySet(); |
| 762 | } |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 763 | |
Saurav Das | 77b5e90 | 2016-01-27 17:01:59 -0800 | [diff] [blame] | 764 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 765 | fwd.selector().criteria().forEach(criterion -> { |
| 766 | if (criterion instanceof VlanIdCriterion) { |
| 767 | VlanId vlanId = ((VlanIdCriterion) criterion).vlanId(); |
| 768 | // ensure that match does not include vlan = NONE as OF-DPA does not |
| 769 | // match untagged packets this way in the ACL table. |
| 770 | if (vlanId.equals(VlanId.NONE)) { |
| 771 | return; |
| 772 | } |
| 773 | OfdpaMatchVlanVid ofdpaMatchVlanVid = |
| 774 | new OfdpaMatchVlanVid(vlanId); |
| 775 | sbuilder.extension(ofdpaMatchVlanVid, deviceId); |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 776 | } else if (criterion instanceof Icmpv6TypeCriterion || |
| 777 | criterion instanceof Icmpv6CodeCriterion) { |
| 778 | /* |
| 779 | * We silenty discard these criterions, our current |
| 780 | * OFDPA platform does not support these matches on |
| 781 | * the ACL table. |
| 782 | */ |
| 783 | log.warn("ICMPv6 Type and ICMPv6 Code are not supported"); |
Saurav Das | 77b5e90 | 2016-01-27 17:01:59 -0800 | [diff] [blame] | 784 | } else { |
| 785 | sbuilder.add(criterion); |
| 786 | } |
| 787 | }); |
| 788 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 789 | // XXX driver does not currently do type checking as per Tables 65-67 in |
| 790 | // 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] | 791 | TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder(); |
| 792 | if (fwd.treatment() != null) { |
| 793 | for (Instruction ins : fwd.treatment().allInstructions()) { |
| 794 | if (ins instanceof OutputInstruction) { |
| 795 | OutputInstruction o = (OutputInstruction) ins; |
| 796 | if (o.port() == PortNumber.CONTROLLER) { |
| 797 | ttBuilder.add(o); |
| 798 | } else { |
| 799 | log.warn("Only allowed treatments in versatile forwarding " |
| 800 | + "objectives are punts to the controller"); |
| 801 | } |
| 802 | } else { |
| 803 | log.warn("Cannot process instruction in versatile fwd {}", ins); |
| 804 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 805 | } |
Charles Chan | 2df0e8a | 2017-01-09 11:45:08 -0800 | [diff] [blame] | 806 | if (fwd.treatment().clearedDeferred()) { |
| 807 | ttBuilder.wipeDeferred(); |
| 808 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 809 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 810 | if (fwd.nextId() != null) { |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 811 | // overide case |
| 812 | NextGroup next = getGroupForNextObjective(fwd.nextId()); |
| 813 | List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data()); |
| 814 | // we only need the top level group's key to point the flow to it |
| 815 | Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst()); |
| 816 | if (group == null) { |
| 817 | log.warn("Group with key:{} for next-id:{} not found in dev:{}", |
| 818 | gkeys.get(0).peekFirst(), fwd.nextId(), deviceId); |
| 819 | fail(fwd, ObjectiveError.GROUPMISSING); |
| 820 | return Collections.emptySet(); |
| 821 | } |
| 822 | ttBuilder.deferred().group(group.id()); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 823 | } |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 824 | |
| 825 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 826 | .fromApp(fwd.appId()) |
| 827 | .withPriority(fwd.priority()) |
| 828 | .forDevice(deviceId) |
Saurav Das | 77b5e90 | 2016-01-27 17:01:59 -0800 | [diff] [blame] | 829 | .withSelector(sbuilder.build()) |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 830 | .withTreatment(ttBuilder.build()) |
| 831 | .makePermanent() |
| 832 | .forTable(ACL_TABLE); |
| 833 | return Collections.singletonList(ruleBuilder.build()); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | /** |
| 837 | * 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] | 838 | * (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] | 839 | * |
| 840 | * @param fwd the forwarding objective of type 'specific' |
| 841 | * @return a collection of flow rules. Typically there will be only one |
| 842 | * for this type of forwarding objective. An empty set may be |
| 843 | * returned if there is an issue in processing the objective. |
| 844 | */ |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 845 | protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) { |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 846 | log.debug("Processing specific fwd objective:{} in dev:{} with next:{}", |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 847 | fwd.id(), deviceId, fwd.nextId()); |
| 848 | boolean isEthTypeObj = isSupportedEthTypeObjective(fwd); |
| 849 | boolean isEthDstObj = isSupportedEthDstObjective(fwd); |
| 850 | |
| 851 | if (isEthTypeObj) { |
| 852 | return processEthTypeSpecific(fwd); |
| 853 | } else if (isEthDstObj) { |
| 854 | return processEthDstSpecific(fwd); |
| 855 | } else { |
| 856 | log.warn("processSpecific: Unsupported forwarding objective " |
| 857 | + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 858 | fail(fwd, ObjectiveError.UNSUPPORTED); |
| 859 | return Collections.emptySet(); |
| 860 | } |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 861 | } |
| 862 | |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 863 | /** |
| 864 | * Handles forwarding rules to the IP and MPLS tables. |
| 865 | * |
| 866 | * @param fwd the forwarding objective |
| 867 | * @return A collection of flow rules, or an empty set |
| 868 | */ |
| 869 | protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) { |
Charles Chan | cad338a | 2016-09-16 18:03:11 -0700 | [diff] [blame] | 870 | return processEthTypeSpecificInternal(fwd, false, ACL_TABLE); |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | /** |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 874 | * Helper method to build Ipv6 selector using the selector provided by |
| 875 | * a forwarding objective. |
| 876 | * |
| 877 | * @param builderToUpdate the builder to update |
| 878 | * @param fwd the selector to read |
| 879 | * @return 0 if the update ends correctly. -1 if the matches |
| 880 | * are not yet supported |
| 881 | */ |
| 882 | protected int buildIpv6Selector(TrafficSelector.Builder builderToUpdate, |
| 883 | ForwardingObjective fwd) { |
| 884 | |
| 885 | TrafficSelector selector = fwd.selector(); |
| 886 | |
| 887 | IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip(); |
| 888 | if (ipv6Dst.isMulticast()) { |
| 889 | log.warn("IPv6 Multicast is currently not supported"); |
| 890 | fail(fwd, ObjectiveError.BADPARAMS); |
| 891 | return -1; |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 892 | } |
Pier Luigi | 69fd431 | 2017-01-28 17:58:58 -0800 | [diff] [blame] | 893 | if (ipv6Dst.prefixLength() != 0) { |
| 894 | builderToUpdate.matchIPv6Dst(ipv6Dst); |
| 895 | } |
| 896 | builderToUpdate.matchEthType(Ethernet.TYPE_IPV6); |
| 897 | log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}" |
| 898 | + " in dev:{}", fwd.id(), fwd.nextId(), deviceId); |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 899 | return 0; |
| 900 | } |
| 901 | |
| 902 | /** |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 903 | * Internal implementation of processEthTypeSpecific. |
| 904 | * <p> |
| 905 | * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break |
| 906 | * the rule into 0.0.0.0/1 and 128.0.0.0/1. |
| 907 | * The allowDefaultRoute must be set to false for OFDPA i12. |
| 908 | * </p> |
| 909 | * |
| 910 | * @param fwd the forwarding objective |
| 911 | * @param allowDefaultRoute allow wildcarded IPv4_DST or not |
Ray Milkey | 0bb1e10 | 2016-11-10 14:51:27 -0800 | [diff] [blame] | 912 | * @param mplsNextTable next MPLS table |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 913 | * @return A collection of flow rules, or an empty set |
| 914 | */ |
| 915 | protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd, |
Charles Chan | cad338a | 2016-09-16 18:03:11 -0700 | [diff] [blame] | 916 | boolean allowDefaultRoute, |
| 917 | int mplsNextTable) { |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 918 | TrafficSelector selector = fwd.selector(); |
| 919 | EthTypeCriterion ethType = |
| 920 | (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE); |
Flavio Castro | e10fa24 | 2016-01-15 12:43:51 -0800 | [diff] [blame] | 921 | boolean defaultRule = false; |
| 922 | boolean popMpls = false; |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 923 | boolean emptyGroup = false; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 924 | int forTableId; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 925 | TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder(); |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 926 | TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder(); |
Flavio Castro | e10fa24 | 2016-01-15 12:43:51 -0800 | [diff] [blame] | 927 | TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder(); |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 928 | |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 929 | if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) { |
Flavio Castro | e10fa24 | 2016-01-15 12:43:51 -0800 | [diff] [blame] | 930 | IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip(); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 931 | if (ipv4Dst.isMulticast()) { |
| 932 | if (ipv4Dst.prefixLength() != 32) { |
| 933 | log.warn("Multicast specific forwarding objective can only be /32"); |
| 934 | fail(fwd, ObjectiveError.BADPARAMS); |
| 935 | return ImmutableSet.of(); |
| 936 | } |
| 937 | VlanId assignedVlan = readVlanFromSelector(fwd.meta()); |
| 938 | if (assignedVlan == null) { |
| 939 | log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort."); |
| 940 | fail(fwd, ObjectiveError.BADPARAMS); |
| 941 | return ImmutableSet.of(); |
| 942 | } |
| 943 | OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan); |
| 944 | filteredSelector.extension(ofdpaMatchVlanVid, deviceId); |
| 945 | filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst); |
| 946 | forTableId = MULTICAST_ROUTING_TABLE; |
| 947 | log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}" |
| 948 | + " in dev:{}", fwd.id(), fwd.nextId(), deviceId); |
Flavio Castro | e10fa24 | 2016-01-15 12:43:51 -0800 | [diff] [blame] | 949 | } else { |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 950 | if (ipv4Dst.prefixLength() == 0) { |
| 951 | if (allowDefaultRoute) { |
| 952 | // The entire IPV4_DST field is wildcarded intentionally |
| 953 | filteredSelector.matchEthType(Ethernet.TYPE_IPV4); |
| 954 | } else { |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 955 | // NOTE: The switch does not support matching 0.0.0.0/0 |
| 956 | // Split it into 0.0.0.0/1 and 128.0.0.0/1 |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 957 | filteredSelector.matchEthType(Ethernet.TYPE_IPV4) |
| 958 | .matchIPDst(IpPrefix.valueOf("0.0.0.0/1")); |
| 959 | complementarySelector.matchEthType(Ethernet.TYPE_IPV4) |
| 960 | .matchIPDst(IpPrefix.valueOf("128.0.0.0/1")); |
| 961 | defaultRule = true; |
| 962 | } |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 963 | } else { |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 964 | filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 965 | } |
| 966 | forTableId = UNICAST_ROUTING_TABLE; |
| 967 | log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}" |
| 968 | + " in dev:{}", fwd.id(), fwd.nextId(), deviceId); |
Flavio Castro | e10fa24 | 2016-01-15 12:43:51 -0800 | [diff] [blame] | 969 | } |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 970 | |
| 971 | if (fwd.treatment() != null) { |
| 972 | for (Instruction instr : fwd.treatment().allInstructions()) { |
| 973 | if (instr instanceof L3ModificationInstruction && |
| 974 | ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) { |
Saurav Das | 9c70534 | 2017-01-06 11:36:01 -0800 | [diff] [blame] | 975 | // XXX decrementing IP ttl is done automatically for routing, this |
| 976 | // action is ignored or rejected in ofdpa as it is not fully implemented |
| 977 | //tb.deferred().add(instr); |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 978 | } |
| 979 | } |
| 980 | } |
| 981 | |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 982 | } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) { |
| 983 | if (buildIpv6Selector(filteredSelector, fwd) < 0) { |
| 984 | return Collections.emptyList(); |
| 985 | } |
| 986 | forTableId = UNICAST_ROUTING_TABLE; |
| 987 | if (fwd.treatment() != null) { |
| 988 | for (Instruction instr : fwd.treatment().allInstructions()) { |
| 989 | if (instr instanceof L3ModificationInstruction && |
| 990 | ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) { |
| 991 | // XXX decrementing IP ttl is done automatically for routing, this |
| 992 | // action is ignored or rejected in ofdpa as it is not fully implemented |
| 993 | //tb.deferred().add(instr); |
| 994 | } |
| 995 | } |
| 996 | } |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 997 | } else { |
| 998 | filteredSelector |
| 999 | .matchEthType(Ethernet.MPLS_UNICAST) |
| 1000 | .matchMplsLabel(((MplsCriterion) |
| 1001 | selector.getCriterion(Criterion.Type.MPLS_LABEL)).label()); |
| 1002 | MplsBosCriterion bos = (MplsBosCriterion) selector |
| 1003 | .getCriterion(Criterion.Type.MPLS_BOS); |
| 1004 | if (bos != null) { |
| 1005 | filteredSelector.matchMplsBos(bos.mplsBos()); |
| 1006 | } |
| 1007 | forTableId = MPLS_TABLE_1; |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1008 | log.debug("processing MPLS specific forwarding objective {} -> next:{}" |
| 1009 | + " in dev {}", fwd.id(), fwd.nextId(), deviceId); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1010 | |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 1011 | if (fwd.treatment() != null) { |
| 1012 | for (Instruction instr : fwd.treatment().allInstructions()) { |
| 1013 | if (instr instanceof L2ModificationInstruction && |
| 1014 | ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1015 | popMpls = true; |
Saurav Das | 9c70534 | 2017-01-06 11:36:01 -0800 | [diff] [blame] | 1016 | // OF-DPA does not pop in MPLS table. Instead it requires |
| 1017 | // setting the MPLS_TYPE so pop can happen down the pipeline |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 1018 | } |
| 1019 | if (instr instanceof L3ModificationInstruction && |
| 1020 | ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) { |
| 1021 | // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction |
| 1022 | tb.immediate().decMplsTtl(); |
| 1023 | } |
| 1024 | if (instr instanceof L3ModificationInstruction && |
| 1025 | ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) { |
| 1026 | tb.immediate().add(instr); |
| 1027 | } |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1028 | } |
| 1029 | } |
| 1030 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1031 | |
| 1032 | if (fwd.nextId() != null) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1033 | if (forTableId == MPLS_TABLE_1 && !popMpls) { |
| 1034 | log.warn("SR CONTINUE case cannot be handled as MPLS ECMP " |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 1035 | + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}" |
| 1036 | + "in this device {}", fwd.id(), fwd.nextId(), deviceId); |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 1037 | // XXX We could convert to forwarding to a single-port, via a MPLS interface, |
| 1038 | // or a MPLS SWAP (with-same) but that would have to be handled in the next-objective. |
| 1039 | // Also the pop-mpls logic used here won't work in non-BoS case. |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1040 | fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1041 | return Collections.emptySet(); |
| 1042 | } |
| 1043 | |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 1044 | NextGroup next = getGroupForNextObjective(fwd.nextId()); |
| 1045 | if (next != null) { |
| 1046 | List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data()); |
| 1047 | // we only need the top level group's key to point the flow to it |
| 1048 | Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst()); |
| 1049 | if (group == null) { |
| 1050 | log.warn("Group with key:{} for next-id:{} not found in dev:{}", |
| 1051 | gkeys.get(0).peekFirst(), fwd.nextId(), deviceId); |
| 1052 | fail(fwd, ObjectiveError.GROUPMISSING); |
| 1053 | return Collections.emptySet(); |
| 1054 | } |
| 1055 | tb.deferred().group(group.id()); |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1056 | // check if group is empty |
| 1057 | if (gkeys.size() == 1 && gkeys.get(0).size() == 1) { |
| 1058 | log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}", |
| 1059 | Integer.toHexString(group.id().id()), deviceId, fwd.id()); |
| 1060 | emptyGroup = true; |
| 1061 | } |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 1062 | } else { |
| 1063 | log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}", |
| 1064 | fwd.nextId(), deviceId, fwd.id()); |
| 1065 | fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 1066 | return Collections.emptySet(); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1067 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1068 | } |
Charles Chan | cad338a | 2016-09-16 18:03:11 -0700 | [diff] [blame] | 1069 | |
| 1070 | if (forTableId == MPLS_TABLE_1) { |
| 1071 | if (mplsNextTable == MPLS_L3_TYPE) { |
| 1072 | Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP); |
Saurav Das | 9c70534 | 2017-01-06 11:36:01 -0800 | [diff] [blame] | 1073 | // set mpls type as apply_action |
| 1074 | tb.immediate().extension(setMplsType, deviceId); |
Charles Chan | cad338a | 2016-09-16 18:03:11 -0700 | [diff] [blame] | 1075 | } |
| 1076 | tb.transition(mplsNextTable); |
| 1077 | } else { |
| 1078 | tb.transition(ACL_TABLE); |
| 1079 | } |
| 1080 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1081 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 1082 | .fromApp(fwd.appId()) |
| 1083 | .withPriority(fwd.priority()) |
| 1084 | .forDevice(deviceId) |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1085 | .withSelector(filteredSelector.build()) |
| 1086 | .withTreatment(tb.build()) |
| 1087 | .forTable(forTableId); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1088 | |
| 1089 | if (fwd.permanent()) { |
| 1090 | ruleBuilder.makePermanent(); |
| 1091 | } else { |
| 1092 | ruleBuilder.makeTemporary(fwd.timeout()); |
| 1093 | } |
Flavio Castro | e10fa24 | 2016-01-15 12:43:51 -0800 | [diff] [blame] | 1094 | Collection<FlowRule> flowRuleCollection = new ArrayList<>(); |
| 1095 | flowRuleCollection.add(ruleBuilder.build()); |
| 1096 | if (defaultRule) { |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 1097 | flowRuleCollection.add( |
| 1098 | defaultRoute(fwd, complementarySelector, forTableId, tb) |
| 1099 | ); |
Flavio Castro | e10fa24 | 2016-01-15 12:43:51 -0800 | [diff] [blame] | 1100 | log.debug("Default rule 0.0.0.0/0 is being installed two rules"); |
| 1101 | } |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1102 | // XXX retrying flows may be necessary due to bug CORD-554 |
| 1103 | if (emptyGroup) { |
| 1104 | executorService.schedule(new RetryFlows(fwd, flowRuleCollection), |
| 1105 | RETRY_MS, TimeUnit.MILLISECONDS); |
| 1106 | } |
Flavio Castro | e10fa24 | 2016-01-15 12:43:51 -0800 | [diff] [blame] | 1107 | return flowRuleCollection; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1108 | } |
| 1109 | |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 1110 | protected FlowRule defaultRoute(ForwardingObjective fwd, |
| 1111 | TrafficSelector.Builder complementarySelector, |
| 1112 | int forTableId, |
| 1113 | TrafficTreatment.Builder tb) { |
| 1114 | FlowRule.Builder rule = DefaultFlowRule.builder() |
| 1115 | .fromApp(fwd.appId()) |
| 1116 | .withPriority(fwd.priority()) |
| 1117 | .forDevice(deviceId) |
| 1118 | .withSelector(complementarySelector.build()) |
| 1119 | .withTreatment(tb.build()) |
| 1120 | .forTable(forTableId); |
| 1121 | if (fwd.permanent()) { |
| 1122 | rule.makePermanent(); |
| 1123 | } else { |
| 1124 | rule.makeTemporary(fwd.timeout()); |
| 1125 | } |
| 1126 | return rule.build(); |
| 1127 | } |
| 1128 | |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1129 | /** |
| 1130 | * Handles forwarding rules to the L2 bridging table. Flow actions are not |
| 1131 | * allowed in the bridging table - instead we use L2 Interface group or |
| 1132 | * L2 flood group |
| 1133 | * |
| 1134 | * @param fwd the forwarding objective |
| 1135 | * @return A collection of flow rules, or an empty set |
| 1136 | */ |
| 1137 | protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) { |
| 1138 | List<FlowRule> rules = new ArrayList<>(); |
| 1139 | |
| 1140 | // Build filtered selector |
| 1141 | TrafficSelector selector = fwd.selector(); |
| 1142 | EthCriterion ethCriterion = (EthCriterion) selector |
| 1143 | .getCriterion(Criterion.Type.ETH_DST); |
| 1144 | VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector |
| 1145 | .getCriterion(Criterion.Type.VLAN_VID); |
| 1146 | |
| 1147 | if (vlanIdCriterion == null) { |
| 1148 | log.warn("Forwarding objective for bridging requires vlan. Not " |
| 1149 | + "installing fwd:{} in dev:{}", fwd.id(), deviceId); |
| 1150 | fail(fwd, ObjectiveError.BADPARAMS); |
| 1151 | return Collections.emptySet(); |
| 1152 | } |
| 1153 | |
| 1154 | TrafficSelector.Builder filteredSelectorBuilder = |
| 1155 | DefaultTrafficSelector.builder(); |
| 1156 | // Do not match MacAddress for subnet broadcast entry |
| 1157 | if (!ethCriterion.mac().equals(MacAddress.NONE)) { |
| 1158 | filteredSelectorBuilder.matchEthDst(ethCriterion.mac()); |
| 1159 | log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}", |
| 1160 | fwd.id(), fwd.nextId(), deviceId); |
| 1161 | } else { |
| 1162 | log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} " |
| 1163 | + "in dev:{} for vlan:{}", |
| 1164 | fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId()); |
| 1165 | } |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 1166 | OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId()); |
| 1167 | filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId); |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1168 | TrafficSelector filteredSelector = filteredSelectorBuilder.build(); |
| 1169 | |
| 1170 | if (fwd.treatment() != null) { |
| 1171 | log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table" |
| 1172 | + "for dev:{}. Expecting only nextId", fwd.id(), deviceId); |
| 1173 | } |
| 1174 | |
| 1175 | TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder(); |
| 1176 | if (fwd.nextId() != null) { |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 1177 | NextGroup next = getGroupForNextObjective(fwd.nextId()); |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1178 | if (next != null) { |
| 1179 | List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data()); |
| 1180 | // we only need the top level group's key to point the flow to it |
| 1181 | Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst()); |
| 1182 | if (group != null) { |
| 1183 | treatmentBuilder.deferred().group(group.id()); |
| 1184 | } else { |
| 1185 | log.warn("Group with key:{} for next-id:{} not found in dev:{}", |
| 1186 | gkeys.get(0).peekFirst(), fwd.nextId(), deviceId); |
| 1187 | fail(fwd, ObjectiveError.GROUPMISSING); |
| 1188 | return Collections.emptySet(); |
| 1189 | } |
| 1190 | } |
| 1191 | } |
| 1192 | treatmentBuilder.immediate().transition(ACL_TABLE); |
| 1193 | TrafficTreatment filteredTreatment = treatmentBuilder.build(); |
| 1194 | |
| 1195 | // Build bridging table entries |
| 1196 | FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder(); |
| 1197 | flowRuleBuilder.fromApp(fwd.appId()) |
| 1198 | .withPriority(fwd.priority()) |
| 1199 | .forDevice(deviceId) |
| 1200 | .withSelector(filteredSelector) |
| 1201 | .withTreatment(filteredTreatment) |
| 1202 | .forTable(BRIDGING_TABLE); |
| 1203 | if (fwd.permanent()) { |
| 1204 | flowRuleBuilder.makePermanent(); |
| 1205 | } else { |
| 1206 | flowRuleBuilder.makeTemporary(fwd.timeout()); |
| 1207 | } |
| 1208 | rules.add(flowRuleBuilder.build()); |
| 1209 | return rules; |
| 1210 | } |
| 1211 | |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1212 | ////////////////////////////////////// |
| 1213 | // Helper Methods and Classes |
| 1214 | ////////////////////////////////////// |
| 1215 | |
| 1216 | private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) { |
| 1217 | TrafficSelector selector = fwd.selector(); |
| 1218 | EthTypeCriterion ethType = (EthTypeCriterion) selector |
| 1219 | .getCriterion(Criterion.Type.ETH_TYPE); |
| 1220 | return !((ethType == null) || |
| 1221 | ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) && |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 1222 | (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) && |
| 1223 | (ethType.ethType().toShort() != Ethernet.TYPE_IPV6)); |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1224 | } |
| 1225 | |
| 1226 | private boolean isSupportedEthDstObjective(ForwardingObjective fwd) { |
| 1227 | TrafficSelector selector = fwd.selector(); |
| 1228 | EthCriterion ethDst = (EthCriterion) selector |
| 1229 | .getCriterion(Criterion.Type.ETH_DST); |
| 1230 | VlanIdCriterion vlanId = (VlanIdCriterion) selector |
| 1231 | .getCriterion(Criterion.Type.VLAN_VID); |
| 1232 | return !(ethDst == null && vlanId == null); |
| 1233 | } |
| 1234 | |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 1235 | protected NextGroup getGroupForNextObjective(Integer nextId) { |
| 1236 | NextGroup next = flowObjectiveStore.getNextGroup(nextId); |
| 1237 | if (next != null) { |
| 1238 | List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data()); |
| 1239 | if (gkeys != null && !gkeys.isEmpty()) { |
| 1240 | return next; |
| 1241 | } else { |
| 1242 | log.warn("Empty next group found in FlowObjective store for " |
| 1243 | + "next-id:{} in dev:{}", nextId, deviceId); |
| 1244 | } |
| 1245 | } else { |
| 1246 | log.warn("next-id {} not found in Flow objective store for dev:{}", |
| 1247 | nextId, deviceId); |
| 1248 | } |
| 1249 | return null; |
| 1250 | } |
| 1251 | |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1252 | protected static void pass(Objective obj) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 1253 | obj.context().ifPresent(context -> context.onSuccess(obj)); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1254 | } |
| 1255 | |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1256 | protected static void fail(Objective obj, ObjectiveError error) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 1257 | obj.context().ifPresent(context -> context.onError(obj, error)); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1258 | } |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 1259 | |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 1260 | @Override |
| 1261 | public List<String> getNextMappings(NextGroup nextGroup) { |
| 1262 | List<String> mappings = new ArrayList<>(); |
| 1263 | List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data()); |
| 1264 | for (Deque<GroupKey> gkd : gkeys) { |
| 1265 | Group lastGroup = null; |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1266 | StringBuffer gchain = new StringBuffer(); |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 1267 | for (GroupKey gk : gkd) { |
| 1268 | Group g = groupService.getGroup(deviceId, gk); |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1269 | if (g == null) { |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 1270 | gchain.append(" NoGrp").append(" -->"); |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1271 | continue; |
| 1272 | } |
| 1273 | gchain.append(" 0x").append(Integer.toHexString(g.id().id())) |
| 1274 | .append(" -->"); |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 1275 | lastGroup = g; |
| 1276 | } |
| 1277 | // add port information for last group in group-chain |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 1278 | List<Instruction> lastGroupIns = new ArrayList<Instruction>(); |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1279 | if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) { |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 1280 | lastGroupIns = lastGroup.buckets().buckets().get(0) |
| 1281 | .treatment().allInstructions(); |
| 1282 | } |
| 1283 | for (Instruction i: lastGroupIns) { |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 1284 | if (i instanceof OutputInstruction) { |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1285 | gchain.append(" port:").append(((OutputInstruction) i).port()); |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 1286 | } |
| 1287 | } |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1288 | mappings.add(gchain.toString()); |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 1289 | } |
| 1290 | return mappings; |
| 1291 | } |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1292 | |
| 1293 | protected static VlanId readVlanFromSelector(TrafficSelector selector) { |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 1294 | if (selector == null) { |
| 1295 | return null; |
| 1296 | } |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1297 | Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID); |
| 1298 | return (criterion == null) |
| 1299 | ? null : ((VlanIdCriterion) criterion).vlanId(); |
| 1300 | } |
| 1301 | |
| 1302 | protected static IpPrefix readIpDstFromSelector(TrafficSelector selector) { |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 1303 | if (selector == null) { |
| 1304 | return null; |
| 1305 | } |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1306 | Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST); |
| 1307 | return (criterion == null) ? null : ((IPCriterion) criterion).ip(); |
| 1308 | } |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1309 | |
| 1310 | private static VlanId readVlanFromTreatment(TrafficTreatment treatment) { |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 1311 | if (treatment == null) { |
| 1312 | return null; |
| 1313 | } |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1314 | for (Instruction i : treatment.allInstructions()) { |
| 1315 | if (i instanceof ModVlanIdInstruction) { |
| 1316 | return ((ModVlanIdInstruction) i).vlanId(); |
| 1317 | } |
| 1318 | } |
| 1319 | return null; |
| 1320 | } |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1321 | |
| 1322 | /** |
| 1323 | * Utility class that retries sending flows a fixed number of times, even if |
| 1324 | * some of the attempts are successful. Used only for forwarding objectives. |
| 1325 | */ |
| 1326 | protected final class RetryFlows implements Runnable { |
| 1327 | int attempts = MAX_RETRY_ATTEMPTS; |
| 1328 | private Collection<FlowRule> retryFlows; |
| 1329 | private ForwardingObjective fwd; |
| 1330 | |
| 1331 | RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) { |
| 1332 | this.fwd = fwd; |
| 1333 | this.retryFlows = retryFlows; |
| 1334 | } |
| 1335 | |
| 1336 | @Override |
| 1337 | public void run() { |
| 1338 | log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}", |
| 1339 | MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size()); |
| 1340 | sendForward(fwd, retryFlows); |
| 1341 | if (--attempts > 0) { |
| 1342 | executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS); |
| 1343 | } |
| 1344 | } |
| 1345 | } |
| 1346 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1347 | } |