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