Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.driver.pipeline; |
| 17 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 20 | import java.util.ArrayDeque; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 21 | import java.util.ArrayList; |
| 22 | import java.util.Collection; |
| 23 | import java.util.Collections; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 24 | import java.util.Deque; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 25 | import java.util.List; |
| 26 | import java.util.Set; |
| 27 | import java.util.concurrent.ConcurrentHashMap; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 28 | |
| 29 | import org.onlab.osgi.ServiceDirectory; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 30 | import org.onlab.packet.Ethernet; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 31 | import org.onlab.packet.MacAddress; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 32 | import org.onlab.packet.VlanId; |
| 33 | import org.onlab.util.KryoNamespace; |
| 34 | import org.onosproject.core.ApplicationId; |
| 35 | import org.onosproject.core.CoreService; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 36 | import org.onosproject.driver.pipeline.OFDPA2GroupHandler.OfdpaNextGroup; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 37 | import org.onosproject.net.DeviceId; |
| 38 | import org.onosproject.net.Port; |
| 39 | import org.onosproject.net.PortNumber; |
| 40 | import org.onosproject.net.behaviour.NextGroup; |
| 41 | import org.onosproject.net.behaviour.Pipeliner; |
| 42 | import org.onosproject.net.behaviour.PipelinerContext; |
| 43 | import org.onosproject.net.device.DeviceService; |
| 44 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 45 | import org.onosproject.net.flow.DefaultFlowRule; |
| 46 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 47 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 48 | import org.onosproject.net.flow.FlowRule; |
| 49 | import org.onosproject.net.flow.FlowRuleOperations; |
| 50 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 51 | import org.onosproject.net.flow.FlowRuleService; |
| 52 | import org.onosproject.net.flow.TrafficSelector; |
| 53 | import org.onosproject.net.flow.TrafficTreatment; |
| 54 | import org.onosproject.net.flow.criteria.Criteria; |
| 55 | import org.onosproject.net.flow.criteria.Criterion; |
| 56 | import org.onosproject.net.flow.criteria.EthCriterion; |
| 57 | import org.onosproject.net.flow.criteria.EthTypeCriterion; |
| 58 | import org.onosproject.net.flow.criteria.IPCriterion; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 59 | import org.onosproject.net.flow.criteria.MplsBosCriterion; |
| 60 | import org.onosproject.net.flow.criteria.MplsCriterion; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 61 | import org.onosproject.net.flow.criteria.PortCriterion; |
| 62 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
| 63 | import org.onosproject.net.flow.instructions.Instruction; |
| 64 | import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
| 65 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 66 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 67 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction; |
| 68 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 69 | import org.onosproject.net.flowobjective.FlowObjectiveStore; |
| 70 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 71 | import org.onosproject.net.flowobjective.NextObjective; |
| 72 | import org.onosproject.net.flowobjective.Objective; |
| 73 | import org.onosproject.net.flowobjective.ObjectiveError; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 74 | import org.onosproject.net.group.DefaultGroupKey; |
| 75 | import org.onosproject.net.group.Group; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 76 | import org.onosproject.net.group.GroupKey; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 77 | import org.onosproject.net.group.GroupService; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 78 | import org.onosproject.store.serializers.KryoNamespaces; |
| 79 | import org.slf4j.Logger; |
| 80 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 81 | /** |
| 82 | * Driver for Broadcom's OF-DPA v2.0 TTP. |
| 83 | * |
| 84 | */ |
| 85 | public class OFDPA2Pipeline extends AbstractHandlerBehaviour implements Pipeliner { |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 86 | protected static final int PORT_TABLE = 0; |
| 87 | protected static final int VLAN_TABLE = 10; |
| 88 | protected static final int TMAC_TABLE = 20; |
| 89 | protected static final int UNICAST_ROUTING_TABLE = 30; |
| 90 | protected static final int MULTICAST_ROUTING_TABLE = 40; |
| 91 | protected static final int MPLS_TABLE_0 = 23; |
| 92 | protected static final int MPLS_TABLE_1 = 24; |
| 93 | protected static final int BRIDGING_TABLE = 50; |
| 94 | protected static final int ACL_TABLE = 60; |
| 95 | protected static final int MAC_LEARNING_TABLE = 254; |
| 96 | protected static final long OFPP_MAX = 0xffffff00L; |
| 97 | |
| 98 | private static final int HIGHEST_PRIORITY = 0xffff; |
Saurav Das | 2857f38 | 2015-11-03 14:39:27 -0800 | [diff] [blame] | 99 | protected static final int DEFAULT_PRIORITY = 0x8000; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 100 | protected static final int LOWEST_PRIORITY = 0x0; |
| 101 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 102 | private final Logger log = getLogger(getClass()); |
| 103 | private ServiceDirectory serviceDirectory; |
| 104 | protected FlowRuleService flowRuleService; |
| 105 | private CoreService coreService; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 106 | protected GroupService groupService; |
| 107 | protected FlowObjectiveStore flowObjectiveStore; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 108 | protected DeviceId deviceId; |
| 109 | protected ApplicationId driverId; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 110 | protected DeviceService deviceService; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 111 | protected static KryoNamespace appKryo = new KryoNamespace.Builder() |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 112 | .register(KryoNamespaces.API) |
| 113 | .register(GroupKey.class) |
| 114 | .register(DefaultGroupKey.class) |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 115 | .register(OfdpaNextGroup.class) |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 116 | .register(byte[].class) |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 117 | .register(ArrayDeque.class) |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 118 | .build(); |
| 119 | |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 120 | protected OFDPA2GroupHandler ofdpa2GroupHandler; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 121 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 122 | private Set<IPCriterion> sentIpFilters = Collections.newSetFromMap( |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 123 | new ConcurrentHashMap<>()); |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 124 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 125 | @Override |
| 126 | public void init(DeviceId deviceId, PipelinerContext context) { |
| 127 | this.serviceDirectory = context.directory(); |
| 128 | this.deviceId = deviceId; |
| 129 | |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 130 | // Initialize OFDPA group handler |
| 131 | ofdpa2GroupHandler = new OFDPA2GroupHandler(); |
| 132 | ofdpa2GroupHandler.init(deviceId, context); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 133 | |
| 134 | coreService = serviceDirectory.get(CoreService.class); |
| 135 | flowRuleService = serviceDirectory.get(FlowRuleService.class); |
| 136 | groupService = serviceDirectory.get(GroupService.class); |
| 137 | flowObjectiveStore = context.store(); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 138 | deviceService = serviceDirectory.get(DeviceService.class); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 139 | |
| 140 | driverId = coreService.registerApplication( |
| 141 | "org.onosproject.driver.OFDPA2Pipeline"); |
| 142 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 143 | initializePipeline(); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | protected void initializePipeline() { |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 147 | // OF-DPA does not require initializing the pipeline as it puts default |
| 148 | // rules automatically in the hardware. However emulation of OFDPA in |
| 149 | // software switches does require table-miss-entries. |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | ////////////////////////////////////// |
| 153 | // Flow Objectives |
| 154 | ////////////////////////////////////// |
| 155 | |
| 156 | @Override |
| 157 | public void filter(FilteringObjective filteringObjective) { |
| 158 | if (filteringObjective.type() == FilteringObjective.Type.PERMIT) { |
| 159 | processFilter(filteringObjective, |
| 160 | filteringObjective.op() == Objective.Operation.ADD, |
| 161 | filteringObjective.appId()); |
| 162 | } else { |
| 163 | // Note that packets that don't match the PERMIT filter are |
| 164 | // automatically denied. The DENY filter is used to deny packets |
| 165 | // that are otherwise permitted by the PERMIT filter. |
| 166 | // Use ACL table flow rules here for DENY filtering objectives |
| 167 | log.debug("filter objective other than PERMIT currently not supported"); |
| 168 | fail(filteringObjective, ObjectiveError.UNSUPPORTED); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public void forward(ForwardingObjective fwd) { |
| 174 | Collection<FlowRule> rules; |
| 175 | FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder(); |
| 176 | |
| 177 | rules = processForward(fwd); |
| 178 | switch (fwd.op()) { |
| 179 | case ADD: |
| 180 | rules.stream() |
| 181 | .filter(rule -> rule != null) |
| 182 | .forEach(flowOpsBuilder::add); |
| 183 | break; |
| 184 | case REMOVE: |
| 185 | rules.stream() |
| 186 | .filter(rule -> rule != null) |
| 187 | .forEach(flowOpsBuilder::remove); |
| 188 | break; |
| 189 | default: |
| 190 | fail(fwd, ObjectiveError.UNKNOWN); |
| 191 | log.warn("Unknown forwarding type {}", fwd.op()); |
| 192 | } |
| 193 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 194 | flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() { |
| 195 | @Override |
| 196 | public void onSuccess(FlowRuleOperations ops) { |
| 197 | pass(fwd); |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public void onError(FlowRuleOperations ops) { |
| 202 | fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 203 | } |
| 204 | })); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public void next(NextObjective nextObjective) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 209 | NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id()); |
| 210 | switch (nextObjective.op()) { |
| 211 | case ADD: |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 212 | if (nextGroup != null) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 213 | log.warn("Cannot add next {} that already exists in device {}", |
| 214 | nextObjective.id(), deviceId); |
| 215 | return; |
| 216 | } |
| 217 | log.debug("Processing NextObjective id{} in dev{} - add group", |
| 218 | nextObjective.id(), deviceId); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 219 | ofdpa2GroupHandler.addGroup(nextObjective); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 220 | break; |
| 221 | case ADD_TO_EXISTING: |
| 222 | if (nextGroup != null) { |
| 223 | log.debug("Processing NextObjective id{} in dev{} - add bucket", |
| 224 | nextObjective.id(), deviceId); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 225 | ofdpa2GroupHandler.addBucketToGroup(nextObjective, nextGroup); |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 226 | } else { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 227 | // it is possible that group-chain has not been fully created yet |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 228 | log.debug("Waiting to add bucket to group for next-id:{} in dev:{}", |
| 229 | nextObjective.id(), deviceId); |
| 230 | // by design only one pending bucket is allowed for the group |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 231 | ofdpa2GroupHandler.pendingBuckets.put(nextObjective.id(), nextObjective); |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 232 | } |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 233 | break; |
| 234 | case REMOVE: |
| 235 | if (nextGroup == null) { |
| 236 | log.warn("Cannot remove next {} that does not exist in device {}", |
| 237 | nextObjective.id(), deviceId); |
| 238 | return; |
| 239 | } |
| 240 | log.debug("Processing NextObjective id{} in dev{} - remove group", |
| 241 | nextObjective.id(), deviceId); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 242 | ofdpa2GroupHandler.removeGroup(nextObjective, nextGroup); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 243 | break; |
| 244 | case REMOVE_FROM_EXISTING: |
| 245 | if (nextGroup == null) { |
| 246 | log.warn("Cannot remove from next {} that does not exist in device {}", |
| 247 | nextObjective.id(), deviceId); |
| 248 | return; |
| 249 | } |
| 250 | log.debug("Processing NextObjective id{} in dev{} - remove bucket", |
| 251 | nextObjective.id(), deviceId); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 252 | ofdpa2GroupHandler.removeBucketFromGroup(nextObjective, nextGroup); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 253 | break; |
| 254 | default: |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 255 | log.warn("Unsupported operation {}", nextObjective.op()); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
| 259 | ////////////////////////////////////// |
| 260 | // Flow handling |
| 261 | ////////////////////////////////////// |
| 262 | |
| 263 | /** |
| 264 | * As per OFDPA 2.0 TTP, filtering of VLAN ids, MAC addresses (for routing) |
| 265 | * and IP addresses configured on switch ports happen in different tables. |
| 266 | * Note that IP filtering rules need to be added to the ACL table, as there |
| 267 | * is no mechanism to send to controller via IP table. |
| 268 | * |
| 269 | * @param filt the filtering objective |
| 270 | * @param install indicates whether to add or remove the objective |
| 271 | * @param applicationId the application that sent this objective |
| 272 | */ |
| 273 | private void processFilter(FilteringObjective filt, |
| 274 | boolean install, ApplicationId applicationId) { |
| 275 | // This driver only processes filtering criteria defined with switch |
| 276 | // ports as the key |
| 277 | PortCriterion portCriterion = null; |
| 278 | EthCriterion ethCriterion = null; |
| 279 | VlanIdCriterion vidCriterion = null; |
| 280 | Collection<IPCriterion> ips = new ArrayList<IPCriterion>(); |
| 281 | if (!filt.key().equals(Criteria.dummy()) && |
| 282 | filt.key().type() == Criterion.Type.IN_PORT) { |
| 283 | portCriterion = (PortCriterion) filt.key(); |
| 284 | } else { |
| 285 | log.warn("No key defined in filtering objective from app: {}. Not" |
| 286 | + "processing filtering objective", applicationId); |
| 287 | fail(filt, ObjectiveError.UNKNOWN); |
| 288 | return; |
| 289 | } |
| 290 | // convert filtering conditions for switch-intfs into flowrules |
| 291 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 292 | for (Criterion criterion : filt.conditions()) { |
| 293 | if (criterion.type() == Criterion.Type.ETH_DST) { |
| 294 | ethCriterion = (EthCriterion) criterion; |
| 295 | } else if (criterion.type() == Criterion.Type.VLAN_VID) { |
| 296 | vidCriterion = (VlanIdCriterion) criterion; |
| 297 | } else if (criterion.type() == Criterion.Type.IPV4_DST) { |
| 298 | ips.add((IPCriterion) criterion); |
| 299 | } else { |
| 300 | log.error("Unsupported filter {}", criterion); |
| 301 | fail(filt, ObjectiveError.UNSUPPORTED); |
| 302 | return; |
| 303 | } |
| 304 | } |
| 305 | |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 306 | VlanId assignedVlan = null; |
| 307 | if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) { |
| 308 | // untagged packets are assigned vlans in OF-DPA |
| 309 | if (filt.meta() == null) { |
| 310 | log.error("Missing metadata in filtering objective required " |
| 311 | + "for vlan assignment in dev {}", deviceId); |
| 312 | fail(filt, ObjectiveError.BADPARAMS); |
| 313 | return; |
| 314 | } |
| 315 | for (Instruction i : filt.meta().allInstructions()) { |
| 316 | if (i instanceof ModVlanIdInstruction) { |
| 317 | assignedVlan = ((ModVlanIdInstruction) i).vlanId(); |
| 318 | } |
| 319 | } |
| 320 | if (assignedVlan == null) { |
| 321 | log.error("Driver requires an assigned vlan-id to tag incoming " |
| 322 | + "untagged packets. Not processing vlan filters on " |
| 323 | + "device {}", deviceId); |
| 324 | fail(filt, ObjectiveError.BADPARAMS); |
| 325 | return; |
| 326 | } |
| 327 | } |
| 328 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 329 | if (ethCriterion == null) { |
| 330 | log.debug("filtering objective missing dstMac, cannot program TMAC table"); |
| 331 | } else { |
| 332 | for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion, |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 333 | vidCriterion, assignedVlan, |
| 334 | applicationId)) { |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 335 | log.debug("adding MAC filtering rules in TMAC table: {} for dev: {}", |
| 336 | tmacRule, deviceId); |
| 337 | ops = install ? ops.add(tmacRule) : ops.remove(tmacRule); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | if (ethCriterion == null || vidCriterion == null) { |
| 342 | log.debug("filtering objective missing dstMac or vlan, cannot program" |
| 343 | + "Vlan Table"); |
| 344 | } else { |
| 345 | for (FlowRule vlanRule : processVlanIdFilter(portCriterion, vidCriterion, |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 346 | assignedVlan, |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 347 | applicationId)) { |
| 348 | log.debug("adding VLAN filtering rule in VLAN table: {} for dev: {}", |
| 349 | vlanRule, deviceId); |
| 350 | ops = install ? ops.add(vlanRule) : ops.remove(vlanRule); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | for (IPCriterion ipaddr : ips) { |
| 355 | // since we ignore port information for IP rules, and the same (gateway) IP |
| 356 | // can be configured on multiple ports, we make sure that we send |
| 357 | // only a single rule to the switch. |
| 358 | if (!sentIpFilters.contains(ipaddr)) { |
| 359 | sentIpFilters.add(ipaddr); |
| 360 | log.debug("adding IP filtering rules in ACL table {} for dev: {}", |
| 361 | ipaddr, deviceId); |
| 362 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 363 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 364 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 365 | selector.matchIPDst(ipaddr.ip()); |
| 366 | treatment.setOutput(PortNumber.CONTROLLER); |
| 367 | FlowRule rule = DefaultFlowRule.builder() |
| 368 | .forDevice(deviceId) |
| 369 | .withSelector(selector.build()) |
| 370 | .withTreatment(treatment.build()) |
| 371 | .withPriority(HIGHEST_PRIORITY) |
| 372 | .fromApp(applicationId) |
| 373 | .makePermanent() |
| 374 | .forTable(ACL_TABLE).build(); |
| 375 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | // apply filtering flow rules |
| 380 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 381 | @Override |
| 382 | public void onSuccess(FlowRuleOperations ops) { |
| 383 | log.info("Applied {} filtering rules in device {}", |
| 384 | ops.stages().get(0).size(), deviceId); |
| 385 | pass(filt); |
| 386 | } |
| 387 | |
| 388 | @Override |
| 389 | public void onError(FlowRuleOperations ops) { |
| 390 | log.info("Failed to apply all filtering rules in dev {}", deviceId); |
| 391 | fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 392 | } |
| 393 | })); |
| 394 | |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Allows untagged packets into pipeline by assigning a vlan id. |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 399 | * Vlan assignment is done by the application. |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 400 | * Allows tagged packets into pipeline as per configured port-vlan info. |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 401 | * |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 402 | * @param portCriterion port on device for which this filter is programmed |
| 403 | * @param vidCriterion vlan assigned to port, or NONE for untagged |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 404 | * @param assignedVlan assigned vlan-id for untagged packets |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 405 | * @param applicationId for application programming this filter |
| 406 | * @return list of FlowRule for port-vlan filters |
| 407 | */ |
| 408 | protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion, |
| 409 | VlanIdCriterion vidCriterion, |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 410 | VlanId assignedVlan, |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 411 | ApplicationId applicationId) { |
| 412 | List<FlowRule> rules = new ArrayList<FlowRule>(); |
| 413 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 414 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 415 | selector.matchVlanId(vidCriterion.vlanId()); |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 416 | treatment.transition(TMAC_TABLE); |
| 417 | |
| 418 | VlanId storeVlan = null; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 419 | if (vidCriterion.vlanId() == VlanId.NONE) { |
| 420 | // untagged packets are assigned vlans |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 421 | treatment.pushVlan().setVlanId(assignedVlan); |
Saurav Das | 2857f38 | 2015-11-03 14:39:27 -0800 | [diff] [blame] | 422 | // XXX ofdpa will require an additional vlan match on the assigned vlan |
| 423 | // and it may not require the push. This is not in compliance with OF |
| 424 | // standard. Waiting on what the exact flows are going to look like. |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 425 | storeVlan = assignedVlan; |
| 426 | } else { |
| 427 | storeVlan = vidCriterion.vlanId(); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 428 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 429 | |
| 430 | // ofdpa cannot match on ALL portnumber, so we need to use separate |
| 431 | // rules for each port. |
| 432 | List<PortNumber> portnums = new ArrayList<PortNumber>(); |
| 433 | if (portCriterion.port() == PortNumber.ALL) { |
| 434 | for (Port port : deviceService.getPorts(deviceId)) { |
| 435 | if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) { |
| 436 | portnums.add(port.number()); |
| 437 | } |
| 438 | } |
| 439 | } else { |
| 440 | portnums.add(portCriterion.port()); |
| 441 | } |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 442 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 443 | for (PortNumber pnum : portnums) { |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 444 | // update storage |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 445 | ofdpa2GroupHandler.port2Vlan.put(pnum, storeVlan); |
| 446 | Set<PortNumber> vlanPorts = ofdpa2GroupHandler.vlan2Port.get(storeVlan); |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 447 | if (vlanPorts == null) { |
| 448 | vlanPorts = Collections.newSetFromMap( |
| 449 | new ConcurrentHashMap<PortNumber, Boolean>()); |
| 450 | vlanPorts.add(pnum); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 451 | ofdpa2GroupHandler.vlan2Port.put(storeVlan, vlanPorts); |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 452 | } else { |
| 453 | vlanPorts.add(pnum); |
| 454 | } |
| 455 | // create rest of flowrule |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 456 | selector.matchInPort(pnum); |
| 457 | FlowRule rule = DefaultFlowRule.builder() |
| 458 | .forDevice(deviceId) |
| 459 | .withSelector(selector.build()) |
| 460 | .withTreatment(treatment.build()) |
| 461 | .withPriority(DEFAULT_PRIORITY) |
| 462 | .fromApp(applicationId) |
| 463 | .makePermanent() |
| 464 | .forTable(VLAN_TABLE).build(); |
| 465 | rules.add(rule); |
| 466 | } |
| 467 | return rules; |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * Allows routed packets with correct destination MAC to be directed |
| 472 | * to unicast-IP routing table or MPLS forwarding table. |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 473 | * |
| 474 | * @param portCriterion port on device for which this filter is programmed |
| 475 | * @param ethCriterion dstMac of device for which is filter is programmed |
| 476 | * @param vidCriterion vlan assigned to port, or NONE for untagged |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 477 | * @param assignedVlan assigned vlan-id for untagged packets |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 478 | * @param applicationId for application programming this filter |
| 479 | * @return list of FlowRule for port-vlan filters |
| 480 | |
| 481 | */ |
| 482 | protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion, |
| 483 | EthCriterion ethCriterion, |
| 484 | VlanIdCriterion vidCriterion, |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 485 | VlanId assignedVlan, |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 486 | ApplicationId applicationId) { |
| 487 | //handling untagged packets via assigned VLAN |
| 488 | if (vidCriterion.vlanId() == VlanId.NONE) { |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 489 | vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 490 | } |
| 491 | // ofdpa cannot match on ALL portnumber, so we need to use separate |
| 492 | // rules for each port. |
| 493 | List<PortNumber> portnums = new ArrayList<PortNumber>(); |
| 494 | if (portCriterion.port() == PortNumber.ALL) { |
| 495 | for (Port port : deviceService.getPorts(deviceId)) { |
| 496 | if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) { |
| 497 | portnums.add(port.number()); |
| 498 | } |
| 499 | } |
| 500 | } else { |
| 501 | portnums.add(portCriterion.port()); |
| 502 | } |
| 503 | |
| 504 | List<FlowRule> rules = new ArrayList<FlowRule>(); |
| 505 | for (PortNumber pnum : portnums) { |
| 506 | // for unicast IP packets |
| 507 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 508 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 509 | selector.matchInPort(pnum); |
| 510 | selector.matchVlanId(vidCriterion.vlanId()); |
| 511 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 512 | selector.matchEthDst(ethCriterion.mac()); |
| 513 | treatment.transition(UNICAST_ROUTING_TABLE); |
| 514 | FlowRule rule = DefaultFlowRule.builder() |
| 515 | .forDevice(deviceId) |
| 516 | .withSelector(selector.build()) |
| 517 | .withTreatment(treatment.build()) |
| 518 | .withPriority(DEFAULT_PRIORITY) |
| 519 | .fromApp(applicationId) |
| 520 | .makePermanent() |
| 521 | .forTable(TMAC_TABLE).build(); |
| 522 | rules.add(rule); |
| 523 | //for MPLS packets |
| 524 | selector = DefaultTrafficSelector.builder(); |
| 525 | treatment = DefaultTrafficTreatment.builder(); |
| 526 | selector.matchInPort(pnum); |
| 527 | selector.matchVlanId(vidCriterion.vlanId()); |
| 528 | selector.matchEthType(Ethernet.MPLS_UNICAST); |
| 529 | selector.matchEthDst(ethCriterion.mac()); |
| 530 | treatment.transition(MPLS_TABLE_0); |
| 531 | rule = DefaultFlowRule.builder() |
| 532 | .forDevice(deviceId) |
| 533 | .withSelector(selector.build()) |
| 534 | .withTreatment(treatment.build()) |
| 535 | .withPriority(DEFAULT_PRIORITY) |
| 536 | .fromApp(applicationId) |
| 537 | .makePermanent() |
| 538 | .forTable(TMAC_TABLE).build(); |
| 539 | rules.add(rule); |
| 540 | } |
| 541 | return rules; |
| 542 | } |
| 543 | |
| 544 | private Collection<FlowRule> processForward(ForwardingObjective fwd) { |
| 545 | switch (fwd.flag()) { |
| 546 | case SPECIFIC: |
| 547 | return processSpecific(fwd); |
| 548 | case VERSATILE: |
| 549 | return processVersatile(fwd); |
| 550 | default: |
| 551 | fail(fwd, ObjectiveError.UNKNOWN); |
| 552 | log.warn("Unknown forwarding flag {}", fwd.flag()); |
| 553 | } |
| 554 | return Collections.emptySet(); |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the |
| 559 | * ACL table. |
| 560 | * @param fwd the forwarding objective of type 'versatile' |
| 561 | * @return a collection of flow rules to be sent to the switch. An empty |
| 562 | * collection may be returned if there is a problem in processing |
| 563 | * the flow rule |
| 564 | */ |
| 565 | private Collection<FlowRule> processVersatile(ForwardingObjective fwd) { |
| 566 | log.info("Processing versatile forwarding objective"); |
| 567 | TrafficSelector selector = fwd.selector(); |
| 568 | |
| 569 | EthTypeCriterion ethType = |
| 570 | (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE); |
| 571 | if (ethType == null) { |
| 572 | log.error("Versatile forwarding objective must include ethType"); |
| 573 | fail(fwd, ObjectiveError.BADPARAMS); |
| 574 | return Collections.emptySet(); |
| 575 | } |
| 576 | if (fwd.nextId() == null && fwd.treatment() == null) { |
| 577 | log.error("Forwarding objective {} from {} must contain " |
| 578 | + "nextId or Treatment", fwd.selector(), fwd.appId()); |
| 579 | return Collections.emptySet(); |
| 580 | } |
| 581 | // XXX driver does not currently do type checking as per Tables 65-67 in |
| 582 | // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller. |
| 583 | if (fwd.treatment() != null && |
| 584 | fwd.treatment().allInstructions().size() == 1 && |
| 585 | fwd.treatment().allInstructions().get(0).type() == Instruction.Type.OUTPUT) { |
| 586 | OutputInstruction o = (OutputInstruction) fwd.treatment().allInstructions().get(0); |
| 587 | if (o.port() == PortNumber.CONTROLLER) { |
| 588 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 589 | .fromApp(fwd.appId()) |
| 590 | .withPriority(fwd.priority()) |
| 591 | .forDevice(deviceId) |
| 592 | .withSelector(fwd.selector()) |
| 593 | .withTreatment(fwd.treatment()) |
| 594 | .makePermanent() |
| 595 | .forTable(ACL_TABLE); |
| 596 | return Collections.singletonList(ruleBuilder.build()); |
| 597 | } else { |
| 598 | log.warn("Only allowed treatments in versatile forwarding " |
| 599 | + "objectives are punts to the controller"); |
| 600 | return Collections.emptySet(); |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | if (fwd.nextId() != null) { |
| 605 | // XXX overide case |
| 606 | log.warn("versatile objective --> next Id not yet implemeted"); |
| 607 | } |
| 608 | return Collections.emptySet(); |
| 609 | } |
| 610 | |
| 611 | /** |
| 612 | * 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] | 613 | * (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] | 614 | * |
| 615 | * @param fwd the forwarding objective of type 'specific' |
| 616 | * @return a collection of flow rules. Typically there will be only one |
| 617 | * for this type of forwarding objective. An empty set may be |
| 618 | * returned if there is an issue in processing the objective. |
| 619 | */ |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 620 | protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) { |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 621 | log.trace("Processing specific fwd objective:{} in dev:{} with next:{}", |
| 622 | fwd.id(), deviceId, fwd.nextId()); |
| 623 | boolean isEthTypeObj = isSupportedEthTypeObjective(fwd); |
| 624 | boolean isEthDstObj = isSupportedEthDstObjective(fwd); |
| 625 | |
| 626 | if (isEthTypeObj) { |
| 627 | return processEthTypeSpecific(fwd); |
| 628 | } else if (isEthDstObj) { |
| 629 | return processEthDstSpecific(fwd); |
| 630 | } else { |
| 631 | log.warn("processSpecific: Unsupported forwarding objective " |
| 632 | + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 633 | fail(fwd, ObjectiveError.UNSUPPORTED); |
| 634 | return Collections.emptySet(); |
| 635 | } |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) { |
| 639 | TrafficSelector selector = fwd.selector(); |
| 640 | EthTypeCriterion ethType = (EthTypeCriterion) selector |
| 641 | .getCriterion(Criterion.Type.ETH_TYPE); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 642 | return !((ethType == null) || |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 643 | ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) && |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 644 | (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST))); |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | private boolean isSupportedEthDstObjective(ForwardingObjective fwd) { |
| 648 | TrafficSelector selector = fwd.selector(); |
| 649 | EthCriterion ethDst = (EthCriterion) selector |
| 650 | .getCriterion(Criterion.Type.ETH_DST); |
| 651 | VlanIdCriterion vlanId = (VlanIdCriterion) selector |
| 652 | .getCriterion(Criterion.Type.VLAN_VID); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 653 | return !(ethDst == null && vlanId == null); |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | /** |
| 657 | * Handles forwarding rules to the IP and MPLS tables. |
| 658 | * |
| 659 | * @param fwd the forwarding objective |
| 660 | * @return A collection of flow rules, or an empty set |
| 661 | */ |
| 662 | protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) { |
| 663 | TrafficSelector selector = fwd.selector(); |
| 664 | EthTypeCriterion ethType = |
| 665 | (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 666 | |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 667 | int forTableId; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 668 | TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder(); |
| 669 | if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) { |
| 670 | filteredSelector.matchEthType(Ethernet.TYPE_IPV4) |
| 671 | .matchIPDst(((IPCriterion) |
| 672 | selector.getCriterion(Criterion.Type.IPV4_DST)).ip()); |
| 673 | forTableId = UNICAST_ROUTING_TABLE; |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 674 | log.debug("processing IPv4 specific forwarding objective {} -> next:{}" |
| 675 | + " in dev:{}", fwd.id(), fwd.nextId(), deviceId); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 676 | } else { |
| 677 | filteredSelector |
| 678 | .matchEthType(Ethernet.MPLS_UNICAST) |
| 679 | .matchMplsLabel(((MplsCriterion) |
| 680 | selector.getCriterion(Criterion.Type.MPLS_LABEL)).label()); |
| 681 | MplsBosCriterion bos = (MplsBosCriterion) selector |
| 682 | .getCriterion(Criterion.Type.MPLS_BOS); |
| 683 | if (bos != null) { |
| 684 | filteredSelector.matchMplsBos(bos.mplsBos()); |
| 685 | } |
| 686 | forTableId = MPLS_TABLE_1; |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 687 | log.debug("processing MPLS specific forwarding objective {} -> next:{}" |
| 688 | + " in dev {}", fwd.id(), fwd.nextId(), deviceId); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 689 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 690 | |
| 691 | TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder(); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 692 | boolean popMpls = false; |
| 693 | if (fwd.treatment() != null) { |
| 694 | for (Instruction i : fwd.treatment().allInstructions()) { |
Charles Chan | 7d10b16 | 2015-12-07 18:54:45 -0800 | [diff] [blame] | 695 | /* |
| 696 | * NOTE: OF-DPA does not support immediate instruction in |
| 697 | * L3 unicast and MPLS table. |
| 698 | */ |
| 699 | tb.deferred().add(i); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 700 | if (i instanceof L2ModificationInstruction && |
| 701 | ((L2ModificationInstruction) i).subtype() == L2SubType.MPLS_POP) { |
| 702 | popMpls = true; |
| 703 | } |
| 704 | } |
| 705 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 706 | |
| 707 | if (fwd.nextId() != null) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 708 | if (forTableId == MPLS_TABLE_1 && !popMpls) { |
| 709 | log.warn("SR CONTINUE case cannot be handled as MPLS ECMP " |
| 710 | + "is not implemented in OF-DPA yet. Aborting this flow " |
| 711 | + "in this device {}", deviceId); |
| 712 | // XXX We could convert to forwarding to a single-port, via a |
| 713 | // MPLS interface, or a MPLS SWAP (with-same) but that would |
| 714 | // have to be handled in the next-objective. Also the pop-mpls |
| 715 | // logic used here won't work in non-BoS case. |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 716 | fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 717 | return Collections.emptySet(); |
| 718 | } |
| 719 | |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 720 | NextGroup next = getGroupForNextObjective(fwd.nextId()); |
| 721 | if (next != null) { |
| 722 | List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data()); |
| 723 | // we only need the top level group's key to point the flow to it |
| 724 | Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst()); |
| 725 | if (group == null) { |
| 726 | log.warn("Group with key:{} for next-id:{} not found in dev:{}", |
| 727 | gkeys.get(0).peekFirst(), fwd.nextId(), deviceId); |
| 728 | fail(fwd, ObjectiveError.GROUPMISSING); |
| 729 | return Collections.emptySet(); |
| 730 | } |
| 731 | tb.deferred().group(group.id()); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 732 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 733 | } |
| 734 | tb.transition(ACL_TABLE); |
| 735 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 736 | .fromApp(fwd.appId()) |
| 737 | .withPriority(fwd.priority()) |
| 738 | .forDevice(deviceId) |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 739 | .withSelector(filteredSelector.build()) |
| 740 | .withTreatment(tb.build()) |
| 741 | .forTable(forTableId); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 742 | |
| 743 | if (fwd.permanent()) { |
| 744 | ruleBuilder.makePermanent(); |
| 745 | } else { |
| 746 | ruleBuilder.makeTemporary(fwd.timeout()); |
| 747 | } |
| 748 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 749 | return Collections.singletonList(ruleBuilder.build()); |
| 750 | } |
| 751 | |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 752 | /** |
| 753 | * Handles forwarding rules to the L2 bridging table. Flow actions are not |
| 754 | * allowed in the bridging table - instead we use L2 Interface group or |
| 755 | * L2 flood group |
| 756 | * |
| 757 | * @param fwd the forwarding objective |
| 758 | * @return A collection of flow rules, or an empty set |
| 759 | */ |
| 760 | protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) { |
| 761 | List<FlowRule> rules = new ArrayList<>(); |
| 762 | |
| 763 | // Build filtered selector |
| 764 | TrafficSelector selector = fwd.selector(); |
| 765 | EthCriterion ethCriterion = (EthCriterion) selector |
| 766 | .getCriterion(Criterion.Type.ETH_DST); |
| 767 | VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector |
| 768 | .getCriterion(Criterion.Type.VLAN_VID); |
| 769 | |
| 770 | if (vlanIdCriterion == null) { |
| 771 | log.warn("Forwarding objective for bridging requires vlan. Not " |
| 772 | + "installing fwd:{} in dev:{}", fwd.id(), deviceId); |
| 773 | fail(fwd, ObjectiveError.BADPARAMS); |
| 774 | return Collections.emptySet(); |
| 775 | } |
| 776 | |
| 777 | TrafficSelector.Builder filteredSelectorBuilder = |
| 778 | DefaultTrafficSelector.builder(); |
| 779 | // Do not match MacAddress for subnet broadcast entry |
| 780 | if (!ethCriterion.mac().equals(MacAddress.NONE)) { |
| 781 | filteredSelectorBuilder.matchEthDst(ethCriterion.mac()); |
| 782 | log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}", |
| 783 | fwd.id(), fwd.nextId(), deviceId); |
| 784 | } else { |
| 785 | log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} " |
| 786 | + "in dev:{} for vlan:{}", |
| 787 | fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId()); |
| 788 | } |
| 789 | filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId()); |
| 790 | TrafficSelector filteredSelector = filteredSelectorBuilder.build(); |
| 791 | |
| 792 | if (fwd.treatment() != null) { |
| 793 | log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table" |
| 794 | + "for dev:{}. Expecting only nextId", fwd.id(), deviceId); |
| 795 | } |
| 796 | |
| 797 | TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder(); |
| 798 | if (fwd.nextId() != null) { |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 799 | NextGroup next = getGroupForNextObjective(fwd.nextId()); |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 800 | if (next != null) { |
| 801 | List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data()); |
| 802 | // we only need the top level group's key to point the flow to it |
| 803 | Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst()); |
| 804 | if (group != null) { |
| 805 | treatmentBuilder.deferred().group(group.id()); |
| 806 | } else { |
| 807 | log.warn("Group with key:{} for next-id:{} not found in dev:{}", |
| 808 | gkeys.get(0).peekFirst(), fwd.nextId(), deviceId); |
| 809 | fail(fwd, ObjectiveError.GROUPMISSING); |
| 810 | return Collections.emptySet(); |
| 811 | } |
| 812 | } |
| 813 | } |
| 814 | treatmentBuilder.immediate().transition(ACL_TABLE); |
| 815 | TrafficTreatment filteredTreatment = treatmentBuilder.build(); |
| 816 | |
| 817 | // Build bridging table entries |
| 818 | FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder(); |
| 819 | flowRuleBuilder.fromApp(fwd.appId()) |
| 820 | .withPriority(fwd.priority()) |
| 821 | .forDevice(deviceId) |
| 822 | .withSelector(filteredSelector) |
| 823 | .withTreatment(filteredTreatment) |
| 824 | .forTable(BRIDGING_TABLE); |
| 825 | if (fwd.permanent()) { |
| 826 | flowRuleBuilder.makePermanent(); |
| 827 | } else { |
| 828 | flowRuleBuilder.makeTemporary(fwd.timeout()); |
| 829 | } |
| 830 | rules.add(flowRuleBuilder.build()); |
| 831 | return rules; |
| 832 | } |
| 833 | |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 834 | protected NextGroup getGroupForNextObjective(Integer nextId) { |
| 835 | NextGroup next = flowObjectiveStore.getNextGroup(nextId); |
| 836 | if (next != null) { |
| 837 | List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data()); |
| 838 | if (gkeys != null && !gkeys.isEmpty()) { |
| 839 | return next; |
| 840 | } else { |
| 841 | log.warn("Empty next group found in FlowObjective store for " |
| 842 | + "next-id:{} in dev:{}", nextId, deviceId); |
| 843 | } |
| 844 | } else { |
| 845 | log.warn("next-id {} not found in Flow objective store for dev:{}", |
| 846 | nextId, deviceId); |
| 847 | } |
| 848 | return null; |
| 849 | } |
| 850 | |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 851 | protected static void pass(Objective obj) { |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 852 | if (obj.context().isPresent()) { |
| 853 | obj.context().get().onSuccess(obj); |
| 854 | } |
| 855 | } |
| 856 | |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 857 | protected static void fail(Objective obj, ObjectiveError error) { |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 858 | if (obj.context().isPresent()) { |
| 859 | obj.context().get().onError(obj, error); |
| 860 | } |
| 861 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 862 | } |