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