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