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