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