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