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); |
| 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 | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 226 | groupHandler.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 | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 232 | groupHandler.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 | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 238 | groupHandler.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 | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 249 | groupHandler.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 | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 259 | groupHandler.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); |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 294 | fail(filt, ObjectiveError.BADPARAMS); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 295 | return; |
| 296 | } |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 297 | log.debug("Received filtering objective for dev/port: {}/{}", deviceId, |
| 298 | portCriterion.port()); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 299 | // convert filtering conditions for switch-intfs into flowrules |
| 300 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 301 | for (Criterion criterion : filt.conditions()) { |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 302 | if (criterion.type() == Criterion.Type.ETH_DST || |
| 303 | criterion.type() == Criterion.Type.ETH_DST_MASKED) { |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 304 | ethCriterion = (EthCriterion) criterion; |
| 305 | } else if (criterion.type() == Criterion.Type.VLAN_VID) { |
| 306 | vidCriterion = (VlanIdCriterion) criterion; |
| 307 | } else if (criterion.type() == Criterion.Type.IPV4_DST) { |
| 308 | ips.add((IPCriterion) criterion); |
| 309 | } else { |
| 310 | log.error("Unsupported filter {}", criterion); |
| 311 | fail(filt, ObjectiveError.UNSUPPORTED); |
| 312 | return; |
| 313 | } |
| 314 | } |
| 315 | |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 316 | VlanId assignedVlan = null; |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 317 | if (vidCriterion != null) { |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 318 | // Use the VLAN in metadata whenever a metadata is provided |
| 319 | if (filt.meta() != null) { |
| 320 | assignedVlan = readVlanFromTreatment(filt.meta()); |
| 321 | // Use the VLAN in criterion if metadata is not present and the traffic is tagged |
| 322 | } else if (!vidCriterion.vlanId().equals(VlanId.NONE)) { |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 323 | assignedVlan = vidCriterion.vlanId(); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 324 | } |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 325 | |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 326 | if (assignedVlan == null) { |
| 327 | log.error("Driver fails to extract VLAN information. " |
| 328 | + "Not proccessing VLAN filters on device {}.", deviceId); |
| 329 | log.debug("VLAN ID in criterion={}, metadata={}", |
| 330 | readVlanFromTreatment(filt.meta()), vidCriterion.vlanId()); |
| 331 | fail(filt, ObjectiveError.BADPARAMS); |
| 332 | return; |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 336 | if (ethCriterion == null || ethCriterion.mac().equals(MacAddress.NONE)) { |
Charles Chan | 985b12e | 2016-05-11 19:47:22 -0700 | [diff] [blame] | 337 | // NOTE: it is possible that a filtering objective only has vidCriterion |
| 338 | log.debug("filtering objective missing dstMac, cannot program TMAC table"); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 339 | } else { |
| 340 | for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion, |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 341 | vidCriterion, assignedVlan, |
| 342 | applicationId)) { |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 343 | log.debug("adding MAC filtering rules in TMAC table: {} for dev: {}", |
| 344 | tmacRule, deviceId); |
| 345 | ops = install ? ops.add(tmacRule) : ops.remove(tmacRule); |
| 346 | } |
| 347 | } |
| 348 | |
Charles Chan | 985b12e | 2016-05-11 19:47:22 -0700 | [diff] [blame] | 349 | if (vidCriterion == null) { |
| 350 | // NOTE: it is possible that a filtering objective only has ethCriterion |
| 351 | log.debug("filtering objective missing dstMac or VLAN, " |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 352 | + "cannot program VLAN Table"); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 353 | } else { |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 354 | /* |
| 355 | * NOTE: Separate vlan filtering rules and assignment rules |
| 356 | * into different stage in order to guarantee that filtering rules |
Saurav Das | 5202596 | 2016-01-28 22:30:01 -0800 | [diff] [blame] | 357 | * always go first, as required by ofdpa. |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 358 | */ |
| 359 | List<FlowRule> allRules = processVlanIdFilter( |
| 360 | portCriterion, vidCriterion, assignedVlan, applicationId); |
| 361 | List<FlowRule> filteringRules = new ArrayList<>(); |
| 362 | List<FlowRule> assignmentRules = new ArrayList<>(); |
| 363 | |
| 364 | allRules.forEach(flowRule -> { |
| 365 | ExtensionCriterion extCriterion = |
| 366 | (ExtensionCriterion) flowRule.selector().getCriterion(Criterion.Type.EXTENSION); |
| 367 | VlanId vlanId = ((OfdpaMatchVlanVid) extCriterion.extensionSelector()).vlanId(); |
Charles Chan | be8aea4 | 2016-02-24 12:04:47 -0800 | [diff] [blame] | 368 | if (!vlanId.equals(VlanId.NONE)) { |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 369 | filteringRules.add(flowRule); |
| 370 | } else { |
| 371 | assignmentRules.add(flowRule); |
| 372 | } |
| 373 | }); |
| 374 | |
| 375 | for (FlowRule filteringRule : filteringRules) { |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 376 | log.debug("adding VLAN filtering rule in VLAN table: {} for dev: {}", |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 377 | filteringRule, deviceId); |
| 378 | ops = install ? ops.add(filteringRule) : ops.remove(filteringRule); |
| 379 | } |
| 380 | |
| 381 | ops.newStage(); |
| 382 | |
| 383 | for (FlowRule assignmentRule : assignmentRules) { |
| 384 | log.debug("adding VLAN assignment rule in VLAN table: {} for dev: {}", |
| 385 | assignmentRule, deviceId); |
| 386 | ops = install ? ops.add(assignmentRule) : ops.remove(assignmentRule); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
| 390 | for (IPCriterion ipaddr : ips) { |
| 391 | // since we ignore port information for IP rules, and the same (gateway) IP |
| 392 | // can be configured on multiple ports, we make sure that we send |
| 393 | // only a single rule to the switch. |
| 394 | if (!sentIpFilters.contains(ipaddr)) { |
| 395 | sentIpFilters.add(ipaddr); |
| 396 | log.debug("adding IP filtering rules in ACL table {} for dev: {}", |
| 397 | ipaddr, deviceId); |
| 398 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 399 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 400 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 401 | selector.matchIPDst(ipaddr.ip()); |
| 402 | treatment.setOutput(PortNumber.CONTROLLER); |
| 403 | FlowRule rule = DefaultFlowRule.builder() |
| 404 | .forDevice(deviceId) |
| 405 | .withSelector(selector.build()) |
| 406 | .withTreatment(treatment.build()) |
| 407 | .withPriority(HIGHEST_PRIORITY) |
| 408 | .fromApp(applicationId) |
| 409 | .makePermanent() |
| 410 | .forTable(ACL_TABLE).build(); |
| 411 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | // apply filtering flow rules |
| 416 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 417 | @Override |
| 418 | public void onSuccess(FlowRuleOperations ops) { |
| 419 | log.info("Applied {} filtering rules in device {}", |
| 420 | ops.stages().get(0).size(), deviceId); |
| 421 | pass(filt); |
| 422 | } |
| 423 | |
| 424 | @Override |
| 425 | public void onError(FlowRuleOperations ops) { |
| 426 | log.info("Failed to apply all filtering rules in dev {}", deviceId); |
| 427 | fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 428 | } |
| 429 | })); |
| 430 | |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * Allows untagged packets into pipeline by assigning a vlan id. |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 435 | * Vlan assignment is done by the application. |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 436 | * Allows tagged packets into pipeline as per configured port-vlan info. |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 437 | * |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 438 | * @param portCriterion port on device for which this filter is programmed |
| 439 | * @param vidCriterion vlan assigned to port, or NONE for untagged |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 440 | * @param assignedVlan assigned vlan-id for untagged packets |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 441 | * @param applicationId for application programming this filter |
| 442 | * @return list of FlowRule for port-vlan filters |
| 443 | */ |
| 444 | protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion, |
| 445 | VlanIdCriterion vidCriterion, |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 446 | VlanId assignedVlan, |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 447 | ApplicationId applicationId) { |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 448 | List<FlowRule> rules = new ArrayList<>(); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 449 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 450 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 451 | TrafficSelector.Builder preSelector = null; |
| 452 | TrafficTreatment.Builder preTreatment = null; |
| 453 | |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 454 | treatment.transition(TMAC_TABLE); |
| 455 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 456 | if (vidCriterion.vlanId() == VlanId.NONE) { |
| 457 | // untagged packets are assigned vlans |
Charles Chan | be8aea4 | 2016-02-24 12:04:47 -0800 | [diff] [blame] | 458 | OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE); |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 459 | selector.extension(ofdpaMatchVlanVid, deviceId); |
| 460 | OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan); |
| 461 | treatment.extension(ofdpaSetVlanVid, deviceId); |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 462 | |
| 463 | preSelector = DefaultTrafficSelector.builder(); |
| 464 | OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan); |
| 465 | preSelector.extension(preOfdpaMatchVlanVid, deviceId); |
| 466 | preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE); |
| 467 | |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 468 | } else { |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 469 | OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId()); |
| 470 | selector.extension(ofdpaMatchVlanVid, deviceId); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 471 | |
| 472 | if (!assignedVlan.equals(vidCriterion.vlanId())) { |
| 473 | OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan); |
| 474 | treatment.extension(ofdpaSetVlanVid, deviceId); |
| 475 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 476 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 477 | |
| 478 | // ofdpa cannot match on ALL portnumber, so we need to use separate |
| 479 | // rules for each port. |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 480 | List<PortNumber> portnums = new ArrayList<>(); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 481 | if (portCriterion.port() == PortNumber.ALL) { |
| 482 | for (Port port : deviceService.getPorts(deviceId)) { |
| 483 | if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) { |
| 484 | portnums.add(port.number()); |
| 485 | } |
| 486 | } |
| 487 | } else { |
| 488 | portnums.add(portCriterion.port()); |
| 489 | } |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 490 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 491 | for (PortNumber pnum : portnums) { |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 492 | // create rest of flowrule |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 493 | selector.matchInPort(pnum); |
| 494 | FlowRule rule = DefaultFlowRule.builder() |
| 495 | .forDevice(deviceId) |
| 496 | .withSelector(selector.build()) |
| 497 | .withTreatment(treatment.build()) |
| 498 | .withPriority(DEFAULT_PRIORITY) |
| 499 | .fromApp(applicationId) |
| 500 | .makePermanent() |
| 501 | .forTable(VLAN_TABLE).build(); |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 502 | |
| 503 | if (preSelector != null) { |
| 504 | preSelector.matchInPort(pnum); |
| 505 | FlowRule preRule = DefaultFlowRule.builder() |
| 506 | .forDevice(deviceId) |
| 507 | .withSelector(preSelector.build()) |
| 508 | .withTreatment(preTreatment.build()) |
| 509 | .withPriority(DEFAULT_PRIORITY) |
| 510 | .fromApp(applicationId) |
| 511 | .makePermanent() |
| 512 | .forTable(VLAN_TABLE).build(); |
| 513 | rules.add(preRule); |
| 514 | } |
| 515 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 516 | rules.add(rule); |
| 517 | } |
| 518 | return rules; |
| 519 | } |
| 520 | |
| 521 | /** |
| 522 | * Allows routed packets with correct destination MAC to be directed |
| 523 | * to unicast-IP routing table or MPLS forwarding table. |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 524 | * |
| 525 | * @param portCriterion port on device for which this filter is programmed |
| 526 | * @param ethCriterion dstMac of device for which is filter is programmed |
| 527 | * @param vidCriterion vlan assigned to port, or NONE for untagged |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 528 | * @param assignedVlan assigned vlan-id for untagged packets |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 529 | * @param applicationId for application programming this filter |
| 530 | * @return list of FlowRule for port-vlan filters |
| 531 | |
| 532 | */ |
| 533 | protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion, |
| 534 | EthCriterion ethCriterion, |
| 535 | VlanIdCriterion vidCriterion, |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 536 | VlanId assignedVlan, |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 537 | ApplicationId applicationId) { |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 538 | // Consider PortNumber.ANY as wildcard. Match ETH_DST only |
| 539 | if (portCriterion != null && portCriterion.port() == PortNumber.ANY) { |
| 540 | return processEthDstOnlyFilter(ethCriterion, applicationId); |
| 541 | } |
| 542 | |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 543 | // Multicast MAC |
| 544 | if (ethCriterion.mask() != null) { |
| 545 | return processMcastEthDstFilter(ethCriterion, applicationId); |
| 546 | } |
| 547 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 548 | //handling untagged packets via assigned VLAN |
| 549 | if (vidCriterion.vlanId() == VlanId.NONE) { |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 550 | vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 551 | } |
| 552 | // ofdpa cannot match on ALL portnumber, so we need to use separate |
| 553 | // rules for each port. |
| 554 | List<PortNumber> portnums = new ArrayList<PortNumber>(); |
| 555 | if (portCriterion.port() == PortNumber.ALL) { |
| 556 | for (Port port : deviceService.getPorts(deviceId)) { |
| 557 | if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) { |
| 558 | portnums.add(port.number()); |
| 559 | } |
| 560 | } |
| 561 | } else { |
| 562 | portnums.add(portCriterion.port()); |
| 563 | } |
| 564 | |
| 565 | List<FlowRule> rules = new ArrayList<FlowRule>(); |
| 566 | for (PortNumber pnum : portnums) { |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 567 | OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId()); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 568 | // for unicast IP packets |
| 569 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 570 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 571 | selector.matchInPort(pnum); |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 572 | selector.extension(ofdpaMatchVlanVid, deviceId); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 573 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 574 | selector.matchEthDst(ethCriterion.mac()); |
| 575 | treatment.transition(UNICAST_ROUTING_TABLE); |
| 576 | FlowRule rule = DefaultFlowRule.builder() |
| 577 | .forDevice(deviceId) |
| 578 | .withSelector(selector.build()) |
| 579 | .withTreatment(treatment.build()) |
| 580 | .withPriority(DEFAULT_PRIORITY) |
| 581 | .fromApp(applicationId) |
| 582 | .makePermanent() |
| 583 | .forTable(TMAC_TABLE).build(); |
| 584 | rules.add(rule); |
| 585 | //for MPLS packets |
| 586 | selector = DefaultTrafficSelector.builder(); |
| 587 | treatment = DefaultTrafficTreatment.builder(); |
| 588 | selector.matchInPort(pnum); |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 589 | selector.extension(ofdpaMatchVlanVid, deviceId); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 590 | selector.matchEthType(Ethernet.MPLS_UNICAST); |
| 591 | selector.matchEthDst(ethCriterion.mac()); |
| 592 | treatment.transition(MPLS_TABLE_0); |
| 593 | rule = DefaultFlowRule.builder() |
| 594 | .forDevice(deviceId) |
| 595 | .withSelector(selector.build()) |
| 596 | .withTreatment(treatment.build()) |
| 597 | .withPriority(DEFAULT_PRIORITY) |
| 598 | .fromApp(applicationId) |
| 599 | .makePermanent() |
| 600 | .forTable(TMAC_TABLE).build(); |
| 601 | rules.add(rule); |
| 602 | } |
| 603 | return rules; |
| 604 | } |
| 605 | |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 606 | protected List<FlowRule> processEthDstOnlyFilter(EthCriterion ethCriterion, |
| 607 | ApplicationId applicationId) { |
| 608 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 609 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 610 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 611 | selector.matchEthDst(ethCriterion.mac()); |
| 612 | treatment.transition(UNICAST_ROUTING_TABLE); |
| 613 | FlowRule rule = DefaultFlowRule.builder() |
| 614 | .forDevice(deviceId) |
| 615 | .withSelector(selector.build()) |
| 616 | .withTreatment(treatment.build()) |
| 617 | .withPriority(DEFAULT_PRIORITY) |
| 618 | .fromApp(applicationId) |
| 619 | .makePermanent() |
| 620 | .forTable(TMAC_TABLE).build(); |
| 621 | return ImmutableList.<FlowRule>builder().add(rule).build(); |
| 622 | } |
| 623 | |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 624 | protected List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion, |
| 625 | ApplicationId applicationId) { |
| 626 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 627 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 628 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 629 | selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask()); |
| 630 | treatment.transition(MULTICAST_ROUTING_TABLE); |
| 631 | FlowRule rule = DefaultFlowRule.builder() |
| 632 | .forDevice(deviceId) |
| 633 | .withSelector(selector.build()) |
| 634 | .withTreatment(treatment.build()) |
| 635 | .withPriority(DEFAULT_PRIORITY) |
| 636 | .fromApp(applicationId) |
| 637 | .makePermanent() |
| 638 | .forTable(TMAC_TABLE).build(); |
| 639 | return ImmutableList.<FlowRule>builder().add(rule).build(); |
| 640 | } |
| 641 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 642 | private Collection<FlowRule> processForward(ForwardingObjective fwd) { |
| 643 | switch (fwd.flag()) { |
| 644 | case SPECIFIC: |
| 645 | return processSpecific(fwd); |
| 646 | case VERSATILE: |
| 647 | return processVersatile(fwd); |
| 648 | default: |
| 649 | fail(fwd, ObjectiveError.UNKNOWN); |
| 650 | log.warn("Unknown forwarding flag {}", fwd.flag()); |
| 651 | } |
| 652 | return Collections.emptySet(); |
| 653 | } |
| 654 | |
| 655 | /** |
| 656 | * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the |
| 657 | * ACL table. |
| 658 | * @param fwd the forwarding objective of type 'versatile' |
| 659 | * @return a collection of flow rules to be sent to the switch. An empty |
| 660 | * collection may be returned if there is a problem in processing |
| 661 | * the flow rule |
| 662 | */ |
Saurav Das | 5202596 | 2016-01-28 22:30:01 -0800 | [diff] [blame] | 663 | protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) { |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 664 | log.info("Processing versatile forwarding objective"); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 665 | |
| 666 | EthTypeCriterion ethType = |
Saurav Das | 77b5e90 | 2016-01-27 17:01:59 -0800 | [diff] [blame] | 667 | (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 668 | if (ethType == null) { |
| 669 | log.error("Versatile forwarding objective must include ethType"); |
| 670 | fail(fwd, ObjectiveError.BADPARAMS); |
| 671 | return Collections.emptySet(); |
| 672 | } |
| 673 | if (fwd.nextId() == null && fwd.treatment() == null) { |
| 674 | log.error("Forwarding objective {} from {} must contain " |
| 675 | + "nextId or Treatment", fwd.selector(), fwd.appId()); |
Zsolt Haraszti | 9faab75 | 2016-02-17 15:55:20 -0800 | [diff] [blame] | 676 | fail(fwd, ObjectiveError.BADPARAMS); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 677 | return Collections.emptySet(); |
| 678 | } |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 679 | |
Saurav Das | 77b5e90 | 2016-01-27 17:01:59 -0800 | [diff] [blame] | 680 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 681 | fwd.selector().criteria().forEach(criterion -> { |
| 682 | if (criterion instanceof VlanIdCriterion) { |
| 683 | VlanId vlanId = ((VlanIdCriterion) criterion).vlanId(); |
| 684 | // ensure that match does not include vlan = NONE as OF-DPA does not |
| 685 | // match untagged packets this way in the ACL table. |
| 686 | if (vlanId.equals(VlanId.NONE)) { |
| 687 | return; |
| 688 | } |
| 689 | OfdpaMatchVlanVid ofdpaMatchVlanVid = |
| 690 | new OfdpaMatchVlanVid(vlanId); |
| 691 | sbuilder.extension(ofdpaMatchVlanVid, deviceId); |
| 692 | } else { |
| 693 | sbuilder.add(criterion); |
| 694 | } |
| 695 | }); |
| 696 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 697 | // XXX driver does not currently do type checking as per Tables 65-67 in |
| 698 | // 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] | 699 | TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder(); |
| 700 | if (fwd.treatment() != null) { |
| 701 | for (Instruction ins : fwd.treatment().allInstructions()) { |
| 702 | if (ins instanceof OutputInstruction) { |
| 703 | OutputInstruction o = (OutputInstruction) ins; |
| 704 | if (o.port() == PortNumber.CONTROLLER) { |
| 705 | ttBuilder.add(o); |
| 706 | } else { |
| 707 | log.warn("Only allowed treatments in versatile forwarding " |
| 708 | + "objectives are punts to the controller"); |
| 709 | } |
| 710 | } else { |
| 711 | log.warn("Cannot process instruction in versatile fwd {}", ins); |
| 712 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 713 | } |
| 714 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 715 | if (fwd.nextId() != null) { |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 716 | // overide case |
| 717 | NextGroup next = getGroupForNextObjective(fwd.nextId()); |
| 718 | List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data()); |
| 719 | // we only need the top level group's key to point the flow to it |
| 720 | Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst()); |
| 721 | if (group == null) { |
| 722 | log.warn("Group with key:{} for next-id:{} not found in dev:{}", |
| 723 | gkeys.get(0).peekFirst(), fwd.nextId(), deviceId); |
| 724 | fail(fwd, ObjectiveError.GROUPMISSING); |
| 725 | return Collections.emptySet(); |
| 726 | } |
| 727 | ttBuilder.deferred().group(group.id()); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 728 | } |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 729 | |
| 730 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 731 | .fromApp(fwd.appId()) |
| 732 | .withPriority(fwd.priority()) |
| 733 | .forDevice(deviceId) |
Saurav Das | 77b5e90 | 2016-01-27 17:01:59 -0800 | [diff] [blame] | 734 | .withSelector(sbuilder.build()) |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 735 | .withTreatment(ttBuilder.build()) |
| 736 | .makePermanent() |
| 737 | .forTable(ACL_TABLE); |
| 738 | return Collections.singletonList(ruleBuilder.build()); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | /** |
| 742 | * 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] | 743 | * (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] | 744 | * |
| 745 | * @param fwd the forwarding objective of type 'specific' |
| 746 | * @return a collection of flow rules. Typically there will be only one |
| 747 | * for this type of forwarding objective. An empty set may be |
| 748 | * returned if there is an issue in processing the objective. |
| 749 | */ |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 750 | protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) { |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 751 | log.trace("Processing specific fwd objective:{} in dev:{} with next:{}", |
| 752 | fwd.id(), deviceId, fwd.nextId()); |
| 753 | boolean isEthTypeObj = isSupportedEthTypeObjective(fwd); |
| 754 | boolean isEthDstObj = isSupportedEthDstObjective(fwd); |
| 755 | |
| 756 | if (isEthTypeObj) { |
| 757 | return processEthTypeSpecific(fwd); |
| 758 | } else if (isEthDstObj) { |
| 759 | return processEthDstSpecific(fwd); |
| 760 | } else { |
| 761 | log.warn("processSpecific: Unsupported forwarding objective " |
| 762 | + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 763 | fail(fwd, ObjectiveError.UNSUPPORTED); |
| 764 | return Collections.emptySet(); |
| 765 | } |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) { |
| 769 | TrafficSelector selector = fwd.selector(); |
| 770 | EthTypeCriterion ethType = (EthTypeCriterion) selector |
| 771 | .getCriterion(Criterion.Type.ETH_TYPE); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 772 | return !((ethType == null) || |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 773 | ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) && |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 774 | (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST))); |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | private boolean isSupportedEthDstObjective(ForwardingObjective fwd) { |
| 778 | TrafficSelector selector = fwd.selector(); |
| 779 | EthCriterion ethDst = (EthCriterion) selector |
| 780 | .getCriterion(Criterion.Type.ETH_DST); |
| 781 | VlanIdCriterion vlanId = (VlanIdCriterion) selector |
| 782 | .getCriterion(Criterion.Type.VLAN_VID); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 783 | return !(ethDst == null && vlanId == null); |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | /** |
| 787 | * Handles forwarding rules to the IP and MPLS tables. |
| 788 | * |
| 789 | * @param fwd the forwarding objective |
| 790 | * @return A collection of flow rules, or an empty set |
| 791 | */ |
| 792 | protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) { |
| 793 | TrafficSelector selector = fwd.selector(); |
| 794 | EthTypeCriterion ethType = |
| 795 | (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE); |
Flavio Castro | e10fa24 | 2016-01-15 12:43:51 -0800 | [diff] [blame] | 796 | boolean defaultRule = false; |
| 797 | boolean popMpls = false; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 798 | int forTableId; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 799 | TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder(); |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 800 | TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder(); |
Flavio Castro | e10fa24 | 2016-01-15 12:43:51 -0800 | [diff] [blame] | 801 | TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder(); |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 802 | |
Flavio Castro | e10fa24 | 2016-01-15 12:43:51 -0800 | [diff] [blame] | 803 | /* |
| 804 | * NOTE: The switch does not support matching 0.0.0.0/0. |
| 805 | * Split it into 0.0.0.0/1 and 128.0.0.0/1 |
| 806 | */ |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 807 | if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) { |
Flavio Castro | e10fa24 | 2016-01-15 12:43:51 -0800 | [diff] [blame] | 808 | IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip(); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 809 | if (ipv4Dst.isMulticast()) { |
| 810 | if (ipv4Dst.prefixLength() != 32) { |
| 811 | log.warn("Multicast specific forwarding objective can only be /32"); |
| 812 | fail(fwd, ObjectiveError.BADPARAMS); |
| 813 | return ImmutableSet.of(); |
| 814 | } |
| 815 | VlanId assignedVlan = readVlanFromSelector(fwd.meta()); |
| 816 | if (assignedVlan == null) { |
| 817 | log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort."); |
| 818 | fail(fwd, ObjectiveError.BADPARAMS); |
| 819 | return ImmutableSet.of(); |
| 820 | } |
| 821 | OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan); |
| 822 | filteredSelector.extension(ofdpaMatchVlanVid, deviceId); |
| 823 | filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst); |
| 824 | forTableId = MULTICAST_ROUTING_TABLE; |
| 825 | log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}" |
| 826 | + " in dev:{}", fwd.id(), fwd.nextId(), deviceId); |
Flavio Castro | e10fa24 | 2016-01-15 12:43:51 -0800 | [diff] [blame] | 827 | } else { |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 828 | if (ipv4Dst.prefixLength() > 0) { |
| 829 | filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst); |
| 830 | } else { |
| 831 | filteredSelector.matchEthType(Ethernet.TYPE_IPV4) |
| 832 | .matchIPDst(IpPrefix.valueOf("0.0.0.0/1")); |
| 833 | complementarySelector.matchEthType(Ethernet.TYPE_IPV4) |
| 834 | .matchIPDst(IpPrefix.valueOf("128.0.0.0/1")); |
| 835 | defaultRule = true; |
| 836 | } |
| 837 | forTableId = UNICAST_ROUTING_TABLE; |
| 838 | log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}" |
| 839 | + " in dev:{}", fwd.id(), fwd.nextId(), deviceId); |
Flavio Castro | e10fa24 | 2016-01-15 12:43:51 -0800 | [diff] [blame] | 840 | } |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 841 | |
| 842 | if (fwd.treatment() != null) { |
| 843 | for (Instruction instr : fwd.treatment().allInstructions()) { |
| 844 | if (instr instanceof L3ModificationInstruction && |
| 845 | ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) { |
| 846 | tb.deferred().add(instr); |
| 847 | } |
| 848 | } |
| 849 | } |
| 850 | |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 851 | } else { |
| 852 | filteredSelector |
| 853 | .matchEthType(Ethernet.MPLS_UNICAST) |
| 854 | .matchMplsLabel(((MplsCriterion) |
| 855 | selector.getCriterion(Criterion.Type.MPLS_LABEL)).label()); |
| 856 | MplsBosCriterion bos = (MplsBosCriterion) selector |
| 857 | .getCriterion(Criterion.Type.MPLS_BOS); |
| 858 | if (bos != null) { |
| 859 | filteredSelector.matchMplsBos(bos.mplsBos()); |
| 860 | } |
| 861 | forTableId = MPLS_TABLE_1; |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 862 | log.debug("processing MPLS specific forwarding objective {} -> next:{}" |
| 863 | + " in dev {}", fwd.id(), fwd.nextId(), deviceId); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 864 | |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 865 | if (fwd.treatment() != null) { |
| 866 | for (Instruction instr : fwd.treatment().allInstructions()) { |
| 867 | if (instr instanceof L2ModificationInstruction && |
| 868 | ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 869 | popMpls = true; |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 870 | tb.immediate().add(instr); |
| 871 | } |
| 872 | if (instr instanceof L3ModificationInstruction && |
| 873 | ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) { |
| 874 | // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction |
| 875 | tb.immediate().decMplsTtl(); |
| 876 | } |
| 877 | if (instr instanceof L3ModificationInstruction && |
| 878 | ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) { |
| 879 | tb.immediate().add(instr); |
| 880 | } |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 881 | } |
| 882 | } |
| 883 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 884 | |
| 885 | if (fwd.nextId() != null) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 886 | if (forTableId == MPLS_TABLE_1 && !popMpls) { |
| 887 | log.warn("SR CONTINUE case cannot be handled as MPLS ECMP " |
| 888 | + "is not implemented in OF-DPA yet. Aborting this flow " |
| 889 | + "in this device {}", deviceId); |
| 890 | // XXX We could convert to forwarding to a single-port, via a |
| 891 | // MPLS interface, or a MPLS SWAP (with-same) but that would |
| 892 | // have to be handled in the next-objective. Also the pop-mpls |
| 893 | // logic used here won't work in non-BoS case. |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 894 | fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 895 | return Collections.emptySet(); |
| 896 | } |
| 897 | |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 898 | NextGroup next = getGroupForNextObjective(fwd.nextId()); |
| 899 | if (next != null) { |
| 900 | List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data()); |
| 901 | // we only need the top level group's key to point the flow to it |
| 902 | Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst()); |
| 903 | if (group == null) { |
| 904 | log.warn("Group with key:{} for next-id:{} not found in dev:{}", |
| 905 | gkeys.get(0).peekFirst(), fwd.nextId(), deviceId); |
| 906 | fail(fwd, ObjectiveError.GROUPMISSING); |
| 907 | return Collections.emptySet(); |
| 908 | } |
| 909 | tb.deferred().group(group.id()); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 910 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 911 | } |
| 912 | tb.transition(ACL_TABLE); |
| 913 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 914 | .fromApp(fwd.appId()) |
| 915 | .withPriority(fwd.priority()) |
| 916 | .forDevice(deviceId) |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 917 | .withSelector(filteredSelector.build()) |
| 918 | .withTreatment(tb.build()) |
| 919 | .forTable(forTableId); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 920 | |
| 921 | if (fwd.permanent()) { |
| 922 | ruleBuilder.makePermanent(); |
| 923 | } else { |
| 924 | ruleBuilder.makeTemporary(fwd.timeout()); |
| 925 | } |
Flavio Castro | e10fa24 | 2016-01-15 12:43:51 -0800 | [diff] [blame] | 926 | Collection<FlowRule> flowRuleCollection = new ArrayList<>(); |
| 927 | flowRuleCollection.add(ruleBuilder.build()); |
| 928 | if (defaultRule) { |
| 929 | FlowRule.Builder rule = DefaultFlowRule.builder() |
| 930 | .fromApp(fwd.appId()) |
| 931 | .withPriority(fwd.priority()) |
| 932 | .forDevice(deviceId) |
| 933 | .withSelector(complementarySelector.build()) |
| 934 | .withTreatment(tb.build()) |
| 935 | .forTable(forTableId); |
| 936 | if (fwd.permanent()) { |
| 937 | rule.makePermanent(); |
| 938 | } else { |
| 939 | rule.makeTemporary(fwd.timeout()); |
| 940 | } |
| 941 | flowRuleCollection.add(rule.build()); |
| 942 | log.debug("Default rule 0.0.0.0/0 is being installed two rules"); |
| 943 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 944 | |
Flavio Castro | e10fa24 | 2016-01-15 12:43:51 -0800 | [diff] [blame] | 945 | return flowRuleCollection; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 946 | } |
| 947 | |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 948 | /** |
| 949 | * Handles forwarding rules to the L2 bridging table. Flow actions are not |
| 950 | * allowed in the bridging table - instead we use L2 Interface group or |
| 951 | * L2 flood group |
| 952 | * |
| 953 | * @param fwd the forwarding objective |
| 954 | * @return A collection of flow rules, or an empty set |
| 955 | */ |
| 956 | protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) { |
| 957 | List<FlowRule> rules = new ArrayList<>(); |
| 958 | |
| 959 | // Build filtered selector |
| 960 | TrafficSelector selector = fwd.selector(); |
| 961 | EthCriterion ethCriterion = (EthCriterion) selector |
| 962 | .getCriterion(Criterion.Type.ETH_DST); |
| 963 | VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector |
| 964 | .getCriterion(Criterion.Type.VLAN_VID); |
| 965 | |
| 966 | if (vlanIdCriterion == null) { |
| 967 | log.warn("Forwarding objective for bridging requires vlan. Not " |
| 968 | + "installing fwd:{} in dev:{}", fwd.id(), deviceId); |
| 969 | fail(fwd, ObjectiveError.BADPARAMS); |
| 970 | return Collections.emptySet(); |
| 971 | } |
| 972 | |
| 973 | TrafficSelector.Builder filteredSelectorBuilder = |
| 974 | DefaultTrafficSelector.builder(); |
| 975 | // Do not match MacAddress for subnet broadcast entry |
| 976 | if (!ethCriterion.mac().equals(MacAddress.NONE)) { |
| 977 | filteredSelectorBuilder.matchEthDst(ethCriterion.mac()); |
| 978 | log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}", |
| 979 | fwd.id(), fwd.nextId(), deviceId); |
| 980 | } else { |
| 981 | log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} " |
| 982 | + "in dev:{} for vlan:{}", |
| 983 | fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId()); |
| 984 | } |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 985 | OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId()); |
| 986 | filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId); |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 987 | TrafficSelector filteredSelector = filteredSelectorBuilder.build(); |
| 988 | |
| 989 | if (fwd.treatment() != null) { |
| 990 | log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table" |
| 991 | + "for dev:{}. Expecting only nextId", fwd.id(), deviceId); |
| 992 | } |
| 993 | |
| 994 | TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder(); |
| 995 | if (fwd.nextId() != null) { |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 996 | NextGroup next = getGroupForNextObjective(fwd.nextId()); |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 997 | if (next != null) { |
| 998 | List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data()); |
| 999 | // we only need the top level group's key to point the flow to it |
| 1000 | Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst()); |
| 1001 | if (group != null) { |
| 1002 | treatmentBuilder.deferred().group(group.id()); |
| 1003 | } else { |
| 1004 | log.warn("Group with key:{} for next-id:{} not found in dev:{}", |
| 1005 | gkeys.get(0).peekFirst(), fwd.nextId(), deviceId); |
| 1006 | fail(fwd, ObjectiveError.GROUPMISSING); |
| 1007 | return Collections.emptySet(); |
| 1008 | } |
| 1009 | } |
| 1010 | } |
| 1011 | treatmentBuilder.immediate().transition(ACL_TABLE); |
| 1012 | TrafficTreatment filteredTreatment = treatmentBuilder.build(); |
| 1013 | |
| 1014 | // Build bridging table entries |
| 1015 | FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder(); |
| 1016 | flowRuleBuilder.fromApp(fwd.appId()) |
| 1017 | .withPriority(fwd.priority()) |
| 1018 | .forDevice(deviceId) |
| 1019 | .withSelector(filteredSelector) |
| 1020 | .withTreatment(filteredTreatment) |
| 1021 | .forTable(BRIDGING_TABLE); |
| 1022 | if (fwd.permanent()) { |
| 1023 | flowRuleBuilder.makePermanent(); |
| 1024 | } else { |
| 1025 | flowRuleBuilder.makeTemporary(fwd.timeout()); |
| 1026 | } |
| 1027 | rules.add(flowRuleBuilder.build()); |
| 1028 | return rules; |
| 1029 | } |
| 1030 | |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 1031 | protected NextGroup getGroupForNextObjective(Integer nextId) { |
| 1032 | NextGroup next = flowObjectiveStore.getNextGroup(nextId); |
| 1033 | if (next != null) { |
| 1034 | List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data()); |
| 1035 | if (gkeys != null && !gkeys.isEmpty()) { |
| 1036 | return next; |
| 1037 | } else { |
| 1038 | log.warn("Empty next group found in FlowObjective store for " |
| 1039 | + "next-id:{} in dev:{}", nextId, deviceId); |
| 1040 | } |
| 1041 | } else { |
| 1042 | log.warn("next-id {} not found in Flow objective store for dev:{}", |
| 1043 | nextId, deviceId); |
| 1044 | } |
| 1045 | return null; |
| 1046 | } |
| 1047 | |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1048 | protected static void pass(Objective obj) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 1049 | obj.context().ifPresent(context -> context.onSuccess(obj)); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1050 | } |
| 1051 | |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1052 | protected static void fail(Objective obj, ObjectiveError error) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 1053 | obj.context().ifPresent(context -> context.onError(obj, error)); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1054 | } |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 1055 | |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 1056 | @Override |
| 1057 | public List<String> getNextMappings(NextGroup nextGroup) { |
| 1058 | List<String> mappings = new ArrayList<>(); |
| 1059 | List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data()); |
| 1060 | for (Deque<GroupKey> gkd : gkeys) { |
| 1061 | Group lastGroup = null; |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1062 | StringBuffer gchain = new StringBuffer(); |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 1063 | for (GroupKey gk : gkd) { |
| 1064 | Group g = groupService.getGroup(deviceId, gk); |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1065 | if (g == null) { |
| 1066 | gchain.append(" ERROR").append(" -->"); |
| 1067 | continue; |
| 1068 | } |
| 1069 | gchain.append(" 0x").append(Integer.toHexString(g.id().id())) |
| 1070 | .append(" -->"); |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 1071 | lastGroup = g; |
| 1072 | } |
| 1073 | // add port information for last group in group-chain |
| 1074 | for (Instruction i: lastGroup.buckets().buckets().get(0).treatment().allInstructions()) { |
| 1075 | if (i instanceof OutputInstruction) { |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1076 | gchain.append(" port:").append(((OutputInstruction) i).port()); |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 1077 | } |
| 1078 | } |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1079 | mappings.add(gchain.toString()); |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 1080 | } |
| 1081 | return mappings; |
| 1082 | } |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1083 | |
| 1084 | protected static VlanId readVlanFromSelector(TrafficSelector selector) { |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 1085 | if (selector == null) { |
| 1086 | return null; |
| 1087 | } |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1088 | Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID); |
| 1089 | return (criterion == null) |
| 1090 | ? null : ((VlanIdCriterion) criterion).vlanId(); |
| 1091 | } |
| 1092 | |
| 1093 | protected static IpPrefix readIpDstFromSelector(TrafficSelector selector) { |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 1094 | if (selector == null) { |
| 1095 | return null; |
| 1096 | } |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1097 | Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST); |
| 1098 | return (criterion == null) ? null : ((IPCriterion) criterion).ip(); |
| 1099 | } |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1100 | |
| 1101 | private static VlanId readVlanFromTreatment(TrafficTreatment treatment) { |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 1102 | if (treatment == null) { |
| 1103 | return null; |
| 1104 | } |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1105 | for (Instruction i : treatment.allInstructions()) { |
| 1106 | if (i instanceof ModVlanIdInstruction) { |
| 1107 | return ((ModVlanIdInstruction) i).vlanId(); |
| 1108 | } |
| 1109 | } |
| 1110 | return null; |
| 1111 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1112 | } |