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