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