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