Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.driver.pipeline; |
| 17 | |
| 18 | import static org.onlab.util.Tools.groupedThreads; |
| 19 | import static org.slf4j.LoggerFactory.getLogger; |
| 20 | |
| 21 | import com.google.common.cache.Cache; |
| 22 | import com.google.common.cache.CacheBuilder; |
| 23 | import com.google.common.cache.RemovalCause; |
| 24 | import com.google.common.cache.RemovalNotification; |
| 25 | |
| 26 | import org.onlab.osgi.ServiceDirectory; |
| 27 | import org.onlab.packet.Ethernet; |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 28 | import org.onlab.packet.MacAddress; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 29 | import org.onlab.packet.VlanId; |
| 30 | import org.onlab.util.KryoNamespace; |
| 31 | import org.onosproject.core.ApplicationId; |
| 32 | import org.onosproject.core.CoreService; |
| 33 | import org.onosproject.net.DeviceId; |
| 34 | import org.onosproject.net.PortNumber; |
| 35 | import org.onosproject.net.behaviour.NextGroup; |
| 36 | import org.onosproject.net.behaviour.Pipeliner; |
| 37 | import org.onosproject.net.behaviour.PipelinerContext; |
| 38 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 39 | import org.onosproject.net.flow.DefaultFlowRule; |
| 40 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 41 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 42 | import org.onosproject.net.flow.FlowRule; |
| 43 | import org.onosproject.net.flow.FlowRuleOperations; |
| 44 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 45 | import org.onosproject.net.flow.FlowRuleService; |
| 46 | import org.onosproject.net.flow.TrafficSelector; |
| 47 | import org.onosproject.net.flow.TrafficTreatment; |
| 48 | import org.onosproject.net.flow.criteria.Criteria; |
| 49 | import org.onosproject.net.flow.criteria.Criterion; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 50 | import org.onosproject.net.flow.criteria.Criterion.Type; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 51 | import org.onosproject.net.flow.criteria.EthCriterion; |
| 52 | import org.onosproject.net.flow.criteria.EthTypeCriterion; |
| 53 | import org.onosproject.net.flow.criteria.IPCriterion; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 54 | import org.onosproject.net.flow.criteria.MplsBosCriterion; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 55 | import org.onosproject.net.flow.criteria.MplsCriterion; |
| 56 | import org.onosproject.net.flow.criteria.PortCriterion; |
| 57 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
| 58 | import org.onosproject.net.flow.instructions.Instruction; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 59 | import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 60 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 61 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 62 | import org.onosproject.net.flowobjective.FlowObjectiveStore; |
| 63 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 64 | import org.onosproject.net.flowobjective.NextObjective; |
| 65 | import org.onosproject.net.flowobjective.Objective; |
| 66 | import org.onosproject.net.flowobjective.ObjectiveError; |
| 67 | import org.onosproject.net.group.DefaultGroupBucket; |
| 68 | import org.onosproject.net.group.DefaultGroupDescription; |
| 69 | import org.onosproject.net.group.DefaultGroupKey; |
| 70 | import org.onosproject.net.group.Group; |
| 71 | import org.onosproject.net.group.GroupBucket; |
| 72 | import org.onosproject.net.group.GroupBuckets; |
| 73 | import org.onosproject.net.group.GroupDescription; |
| 74 | import org.onosproject.net.group.GroupEvent; |
| 75 | import org.onosproject.net.group.GroupKey; |
| 76 | import org.onosproject.net.group.GroupListener; |
| 77 | import org.onosproject.net.group.GroupService; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 78 | import org.onosproject.store.serializers.KryoNamespaces; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 79 | import org.slf4j.Logger; |
| 80 | |
| 81 | import java.util.ArrayList; |
| 82 | import java.util.Collection; |
| 83 | import java.util.Collections; |
| 84 | import java.util.List; |
Sho SHIMIZU | 4590604 | 2016-01-13 23:05:54 -0800 | [diff] [blame] | 85 | import java.util.Objects; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 86 | import java.util.Set; |
| 87 | import java.util.concurrent.Executors; |
| 88 | import java.util.concurrent.ScheduledExecutorService; |
| 89 | import java.util.concurrent.TimeUnit; |
| 90 | import java.util.stream.Collectors; |
| 91 | |
| 92 | /** |
| 93 | * Driver for SPRING-OPEN pipeline. |
| 94 | */ |
| 95 | public class SpringOpenTTP extends AbstractHandlerBehaviour |
| 96 | implements Pipeliner { |
| 97 | |
| 98 | // Default table ID - compatible with CpqD switch |
| 99 | private static final int TABLE_VLAN = 0; |
| 100 | private static final int TABLE_TMAC = 1; |
| 101 | private static final int TABLE_IPV4_UNICAST = 2; |
| 102 | private static final int TABLE_MPLS = 3; |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 103 | private static final int TABLE_DMAC = 4; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 104 | private static final int TABLE_ACL = 5; |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 105 | private static final int TABLE_SMAC = 6; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 106 | |
| 107 | /** |
| 108 | * Set the default values. These variables will get overwritten based on the |
| 109 | * switch vendor type |
| 110 | */ |
| 111 | protected int vlanTableId = TABLE_VLAN; |
| 112 | protected int tmacTableId = TABLE_TMAC; |
| 113 | protected int ipv4UnicastTableId = TABLE_IPV4_UNICAST; |
| 114 | protected int mplsTableId = TABLE_MPLS; |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 115 | protected int dstMacTableId = TABLE_DMAC; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 116 | protected int aclTableId = TABLE_ACL; |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 117 | protected int srcMacTableId = TABLE_SMAC; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 118 | |
| 119 | protected final Logger log = getLogger(getClass()); |
| 120 | |
| 121 | private ServiceDirectory serviceDirectory; |
| 122 | private FlowRuleService flowRuleService; |
| 123 | private CoreService coreService; |
| 124 | protected GroupService groupService; |
| 125 | protected FlowObjectiveStore flowObjectiveStore; |
| 126 | protected DeviceId deviceId; |
| 127 | private ApplicationId appId; |
| 128 | |
| 129 | private Cache<GroupKey, NextObjective> pendingGroups; |
| 130 | |
| 131 | private ScheduledExecutorService groupChecker = Executors |
| 132 | .newScheduledThreadPool(2, |
| 133 | groupedThreads("onos/pipeliner", |
| 134 | "spring-open-%d")); |
| 135 | protected KryoNamespace appKryo = new KryoNamespace.Builder() |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 136 | .register(KryoNamespaces.API) |
| 137 | .register(GroupKey.class) |
| 138 | .register(DefaultGroupKey.class) |
| 139 | .register(TrafficTreatment.class) |
| 140 | .register(SpringOpenGroup.class) |
| 141 | .register(byte[].class) |
| 142 | .build(); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 143 | |
| 144 | @Override |
| 145 | public void init(DeviceId deviceId, PipelinerContext context) { |
| 146 | this.serviceDirectory = context.directory(); |
| 147 | this.deviceId = deviceId; |
| 148 | |
| 149 | pendingGroups = CacheBuilder |
| 150 | .newBuilder() |
| 151 | .expireAfterWrite(20, TimeUnit.SECONDS) |
| 152 | .removalListener((RemovalNotification<GroupKey, NextObjective> notification) -> { |
| 153 | if (notification.getCause() == RemovalCause.EXPIRED) { |
| 154 | fail(notification.getValue(), |
| 155 | ObjectiveError.GROUPINSTALLATIONFAILED); |
| 156 | } |
| 157 | }).build(); |
| 158 | |
| 159 | groupChecker.scheduleAtFixedRate(new GroupChecker(), 0, 500, |
| 160 | TimeUnit.MILLISECONDS); |
| 161 | |
| 162 | coreService = serviceDirectory.get(CoreService.class); |
| 163 | flowRuleService = serviceDirectory.get(FlowRuleService.class); |
| 164 | groupService = serviceDirectory.get(GroupService.class); |
| 165 | flowObjectiveStore = context.store(); |
| 166 | |
| 167 | groupService.addListener(new InnerGroupListener()); |
| 168 | |
| 169 | appId = coreService |
| 170 | .registerApplication("org.onosproject.driver.SpringOpenTTP"); |
| 171 | |
| 172 | setTableMissEntries(); |
| 173 | log.info("Spring Open TTP driver initialized"); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public void filter(FilteringObjective filteringObjective) { |
| 178 | if (filteringObjective.type() == FilteringObjective.Type.PERMIT) { |
| 179 | log.debug("processing PERMIT filter objective"); |
| 180 | processFilter(filteringObjective, |
| 181 | filteringObjective.op() == Objective.Operation.ADD, |
| 182 | filteringObjective.appId()); |
| 183 | } else { |
| 184 | log.debug("filter objective other than PERMIT not supported"); |
| 185 | fail(filteringObjective, ObjectiveError.UNSUPPORTED); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public void forward(ForwardingObjective fwd) { |
| 191 | Collection<FlowRule> rules; |
| 192 | FlowRuleOperations.Builder flowBuilder = FlowRuleOperations.builder(); |
| 193 | |
| 194 | rules = processForward(fwd); |
| 195 | switch (fwd.op()) { |
| 196 | case ADD: |
Sho SHIMIZU | 4590604 | 2016-01-13 23:05:54 -0800 | [diff] [blame] | 197 | rules.stream().filter(Objects::nonNull) |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 198 | .forEach(flowBuilder::add); |
| 199 | break; |
| 200 | case REMOVE: |
Sho SHIMIZU | 4590604 | 2016-01-13 23:05:54 -0800 | [diff] [blame] | 201 | rules.stream().filter(Objects::nonNull) |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 202 | .forEach(flowBuilder::remove); |
| 203 | break; |
| 204 | default: |
| 205 | fail(fwd, ObjectiveError.UNKNOWN); |
| 206 | log.warn("Unknown forwarding type {}", fwd.op()); |
| 207 | } |
| 208 | |
| 209 | flowRuleService.apply(flowBuilder |
| 210 | .build(new FlowRuleOperationsContext() { |
| 211 | @Override |
| 212 | public void onSuccess(FlowRuleOperations ops) { |
| 213 | pass(fwd); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 214 | log.debug("Provisioned tables in {} successfully with " |
| 215 | + "forwarding rules", deviceId); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public void onError(FlowRuleOperations ops) { |
| 220 | fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 221 | log.warn("Failed to provision tables in {} with " |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 222 | + "forwarding rules", deviceId); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 223 | } |
| 224 | })); |
| 225 | |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public void next(NextObjective nextObjective) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 230 | NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id()); |
| 231 | switch (nextObjective.op()) { |
| 232 | case ADD: |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 233 | if (nextGroup != null) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 234 | log.warn("Cannot add next {} that already exists in device {}", |
| 235 | nextObjective.id(), deviceId); |
| 236 | return; |
| 237 | } |
| 238 | log.debug("Processing NextObjective id{} in dev{} - add group", |
| 239 | nextObjective.id(), deviceId); |
| 240 | addGroup(nextObjective); |
| 241 | break; |
| 242 | case ADD_TO_EXISTING: |
| 243 | if (nextGroup != null) { |
| 244 | log.debug("Processing NextObjective id{} in dev{} - add bucket", |
| 245 | nextObjective.id(), deviceId); |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 246 | addBucketToGroup(nextObjective); |
| 247 | } else { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 248 | log.warn("Cannot add to group that does not exist"); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 249 | } |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 250 | break; |
| 251 | case REMOVE: |
| 252 | if (nextGroup == null) { |
| 253 | log.warn("Cannot remove next {} that does not exist in device {}", |
| 254 | nextObjective.id(), deviceId); |
| 255 | return; |
| 256 | } |
| 257 | log.debug("Processing NextObjective id{} in dev{} - remove group", |
| 258 | nextObjective.id(), deviceId); |
| 259 | removeGroup(nextObjective); |
| 260 | break; |
| 261 | case REMOVE_FROM_EXISTING: |
| 262 | if (nextGroup == null) { |
| 263 | log.warn("Cannot remove from next {} that does not exist in device {}", |
| 264 | nextObjective.id(), deviceId); |
| 265 | return; |
| 266 | } |
| 267 | log.debug("Processing NextObjective id{} in dev{} - remove bucket", |
| 268 | nextObjective.id(), deviceId); |
| 269 | removeBucketFromGroup(nextObjective); |
| 270 | break; |
| 271 | default: |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 272 | log.warn("Unsupported operation {}", nextObjective.op()); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 273 | } |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 274 | } |
| 275 | |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 276 | private void removeGroup(NextObjective nextObjective) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 277 | log.debug("removeGroup in {}: for next objective id {}", |
| 278 | deviceId, nextObjective.id()); |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 279 | final GroupKey key = new DefaultGroupKey( |
| 280 | appKryo.serialize(nextObjective.id())); |
| 281 | groupService.removeGroup(deviceId, key, appId); |
| 282 | } |
| 283 | |
| 284 | private void addGroup(NextObjective nextObjective) { |
Srikanth Vavilapalli | 5428b6c | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 285 | log.debug("addGroup with type{} for nextObjective id {}", |
| 286 | nextObjective.type(), nextObjective.id()); |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 287 | List<GroupBucket> buckets; |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 288 | switch (nextObjective.type()) { |
| 289 | case SIMPLE: |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 290 | Collection<TrafficTreatment> treatments = nextObjective.next(); |
| 291 | if (treatments.size() == 1) { |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 292 | // Spring Open TTP converts simple nextObjective to flow-actions |
| 293 | // in a dummy group |
| 294 | TrafficTreatment treatment = nextObjective.next().iterator().next(); |
| 295 | log.debug("Converting SIMPLE group for next objective id {} " + |
| 296 | "to {} flow-actions in device:{}", nextObjective.id(), |
| 297 | treatment.allInstructions().size(), deviceId); |
| 298 | flowObjectiveStore.putNextGroup(nextObjective.id(), |
| 299 | new SpringOpenGroup(null, treatment)); |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 300 | } |
| 301 | break; |
| 302 | case HASHED: |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 303 | // we convert MPLS ECMP groups to flow-actions for a single |
| 304 | // bucket(output port). |
| 305 | boolean mplsEcmp = false; |
| 306 | if (nextObjective.meta() != null) { |
| 307 | for (Criterion c : nextObjective.meta().criteria()) { |
| 308 | if (c.type() == Type.MPLS_LABEL) { |
| 309 | mplsEcmp = true; |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | if (mplsEcmp) { |
| 314 | // covert to flow-actions in a dummy group by choosing the first bucket |
| 315 | log.debug("Converting HASHED group for next objective id {} " + |
| 316 | "to flow-actions in device:{}", nextObjective.id(), |
| 317 | deviceId); |
| 318 | TrafficTreatment treatment = nextObjective.next().iterator().next(); |
| 319 | flowObjectiveStore.putNextGroup(nextObjective.id(), |
| 320 | new SpringOpenGroup(null, treatment)); |
| 321 | } else { |
| 322 | // process as ECMP group |
| 323 | buckets = nextObjective |
| 324 | .next() |
| 325 | .stream() |
| 326 | .map((treatment) -> DefaultGroupBucket |
| 327 | .createSelectGroupBucket(treatment)) |
| 328 | .collect(Collectors.toList()); |
| 329 | if (!buckets.isEmpty()) { |
| 330 | final GroupKey key = new DefaultGroupKey( |
| 331 | appKryo.serialize(nextObjective.id())); |
| 332 | GroupDescription groupDescription = new DefaultGroupDescription( |
| 333 | deviceId, |
| 334 | GroupDescription.Type.SELECT, |
| 335 | new GroupBuckets(buckets), |
| 336 | key, |
| 337 | null, |
| 338 | nextObjective.appId()); |
| 339 | log.debug("Creating HASHED group for next objective id {}" |
| 340 | + " in dev:{}", nextObjective.id(), deviceId); |
| 341 | pendingGroups.put(key, nextObjective); |
| 342 | groupService.addGroup(groupDescription); |
| 343 | } |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 344 | } |
| 345 | break; |
| 346 | case BROADCAST: |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 347 | buckets = nextObjective |
| 348 | .next() |
| 349 | .stream() |
| 350 | .map((treatment) -> DefaultGroupBucket |
| 351 | .createAllGroupBucket(treatment)) |
| 352 | .collect(Collectors.toList()); |
| 353 | if (!buckets.isEmpty()) { |
| 354 | final GroupKey key = new DefaultGroupKey( |
| 355 | appKryo.serialize(nextObjective |
| 356 | .id())); |
| 357 | GroupDescription groupDescription = new DefaultGroupDescription( |
| 358 | deviceId, |
| 359 | GroupDescription.Type.ALL, |
| 360 | new GroupBuckets(buckets), |
| 361 | key, |
| 362 | null, |
| 363 | nextObjective.appId()); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 364 | log.debug("Creating BROADCAST group for next objective id {} " |
| 365 | + "in device {}", nextObjective.id(), deviceId); |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 366 | pendingGroups.put(key, nextObjective); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 367 | groupService.addGroup(groupDescription); |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 368 | } |
| 369 | break; |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 370 | case FAILOVER: |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 371 | log.debug("FAILOVER next objectives not supported"); |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 372 | fail(nextObjective, ObjectiveError.UNSUPPORTED); |
| 373 | log.warn("Unsupported next objective type {}", nextObjective.type()); |
| 374 | break; |
| 375 | default: |
| 376 | fail(nextObjective, ObjectiveError.UNKNOWN); |
| 377 | log.warn("Unknown next objective type {}", nextObjective.type()); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | private void addBucketToGroup(NextObjective nextObjective) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 382 | log.debug("addBucketToGroup in {}: for next objective id {}", |
| 383 | deviceId, nextObjective.id()); |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 384 | Collection<TrafficTreatment> treatments = nextObjective.next(); |
| 385 | TrafficTreatment treatment = treatments.iterator().next(); |
| 386 | final GroupKey key = new DefaultGroupKey( |
| 387 | appKryo.serialize(nextObjective |
| 388 | .id())); |
| 389 | Group group = groupService.getGroup(deviceId, key); |
| 390 | if (group == null) { |
| 391 | log.warn("Group is not found in {} for {}", deviceId, key); |
| 392 | return; |
| 393 | } |
| 394 | GroupBucket bucket; |
| 395 | if (group.type() == GroupDescription.Type.INDIRECT) { |
| 396 | bucket = DefaultGroupBucket.createIndirectGroupBucket(treatment); |
| 397 | } else if (group.type() == GroupDescription.Type.SELECT) { |
| 398 | bucket = DefaultGroupBucket.createSelectGroupBucket(treatment); |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 399 | } else if (group.type() == GroupDescription.Type.ALL) { |
| 400 | bucket = DefaultGroupBucket.createAllGroupBucket(treatment); |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 401 | } else { |
| 402 | log.warn("Unsupported Group type {}", group.type()); |
| 403 | return; |
| 404 | } |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 405 | GroupBuckets bucketsToAdd = new GroupBuckets(Collections.singletonList(bucket)); |
Srikanth Vavilapalli | 5428b6c | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 406 | log.debug("Adding buckets to group id {} of next objective id {} in device {}", |
| 407 | group.id(), nextObjective.id(), deviceId); |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 408 | groupService.addBucketsToGroup(deviceId, key, bucketsToAdd, key, appId); |
| 409 | } |
| 410 | |
| 411 | private void removeBucketFromGroup(NextObjective nextObjective) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 412 | log.debug("removeBucketFromGroup in {}: for next objective id {}", |
| 413 | deviceId, nextObjective.id()); |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 414 | NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id()); |
| 415 | if (nextGroup != null) { |
| 416 | Collection<TrafficTreatment> treatments = nextObjective.next(); |
| 417 | TrafficTreatment treatment = treatments.iterator().next(); |
| 418 | final GroupKey key = new DefaultGroupKey( |
| 419 | appKryo.serialize(nextObjective |
| 420 | .id())); |
| 421 | Group group = groupService.getGroup(deviceId, key); |
| 422 | if (group == null) { |
| 423 | log.warn("Group is not found in {} for {}", deviceId, key); |
| 424 | return; |
| 425 | } |
| 426 | GroupBucket bucket; |
| 427 | if (group.type() == GroupDescription.Type.INDIRECT) { |
| 428 | bucket = DefaultGroupBucket.createIndirectGroupBucket(treatment); |
| 429 | } else if (group.type() == GroupDescription.Type.SELECT) { |
| 430 | bucket = DefaultGroupBucket.createSelectGroupBucket(treatment); |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 431 | } else if (group.type() == GroupDescription.Type.ALL) { |
| 432 | bucket = DefaultGroupBucket.createAllGroupBucket(treatment); |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 433 | } else { |
| 434 | log.warn("Unsupported Group type {}", group.type()); |
| 435 | return; |
| 436 | } |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 437 | GroupBuckets removeBuckets = new GroupBuckets(Collections.singletonList(bucket)); |
Srikanth Vavilapalli | 5428b6c | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 438 | log.debug("Removing buckets from group id {} of next objective id {} in device {}", |
| 439 | group.id(), nextObjective.id(), deviceId); |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 440 | groupService.removeBucketsFromGroup(deviceId, key, removeBuckets, key, appId); |
| 441 | } |
| 442 | } |
| 443 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 444 | private Collection<FlowRule> processForward(ForwardingObjective fwd) { |
| 445 | switch (fwd.flag()) { |
| 446 | case SPECIFIC: |
| 447 | return processSpecific(fwd); |
| 448 | case VERSATILE: |
| 449 | return processVersatile(fwd); |
| 450 | default: |
| 451 | fail(fwd, ObjectiveError.UNKNOWN); |
| 452 | log.warn("Unknown forwarding flag {}", fwd.flag()); |
| 453 | } |
| 454 | return Collections.emptySet(); |
| 455 | } |
| 456 | |
| 457 | private Collection<FlowRule> processVersatile(ForwardingObjective fwd) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 458 | log.debug("Processing versatile forwarding objective in dev:{}", deviceId); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 459 | TrafficSelector selector = fwd.selector(); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 460 | EthTypeCriterion ethType = |
| 461 | (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE); |
| 462 | if (ethType == null) { |
| 463 | log.error("Versatile forwarding objective must include ethType"); |
| 464 | fail(fwd, ObjectiveError.UNKNOWN); |
| 465 | return Collections.emptySet(); |
| 466 | } |
| 467 | |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 468 | if (fwd.treatment() == null && fwd.nextId() == null) { |
| 469 | log.error("VERSATILE forwarding objective needs next objective ID " |
| 470 | + "or treatment."); |
| 471 | return Collections.emptySet(); |
| 472 | } |
| 473 | // emulation of ACL table (for versatile fwd objective) requires |
| 474 | // overriding any previous instructions |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 475 | TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment |
| 476 | .builder(); |
| 477 | treatmentBuilder.wipeDeferred(); |
| 478 | |
| 479 | if (fwd.nextId() != null) { |
| 480 | NextGroup next = flowObjectiveStore.getNextGroup(fwd.nextId()); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 481 | if (next != null) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 482 | SpringOpenGroup soGroup = appKryo.deserialize(next.data()); |
| 483 | if (soGroup.dummy) { |
| 484 | // need to convert to flow-actions |
| 485 | for (Instruction ins : soGroup.treatment.allInstructions()) { |
| 486 | treatmentBuilder.add(ins); |
| 487 | } |
| 488 | } else { |
| 489 | GroupKey key = soGroup.key; |
| 490 | Group group = groupService.getGroup(deviceId, key); |
| 491 | if (group == null) { |
| 492 | log.warn("The group left!"); |
| 493 | fail(fwd, ObjectiveError.GROUPMISSING); |
| 494 | return Collections.emptySet(); |
| 495 | } |
| 496 | treatmentBuilder.deferred().group(group.id()); |
| 497 | log.debug("Adding OUTGROUP action"); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 498 | } |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 499 | } |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | if (fwd.treatment() != null) { |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 503 | if (fwd.treatment().allInstructions().size() == 1 && |
| 504 | fwd.treatment().allInstructions().get(0).type() == Instruction.Type.OUTPUT) { |
| 505 | OutputInstruction o = (OutputInstruction) fwd.treatment().allInstructions().get(0); |
| 506 | if (o.port() == PortNumber.CONTROLLER) { |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 507 | treatmentBuilder.punt(); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 508 | } else { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 509 | treatmentBuilder.add(o); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 510 | } |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 511 | } else { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 512 | for (Instruction ins : fwd.treatment().allInstructions()) { |
| 513 | treatmentBuilder.add(ins); |
| 514 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 515 | } |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 516 | } |
| 517 | |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 518 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 519 | .fromApp(fwd.appId()).withPriority(fwd.priority()) |
sangho | f9d0bf1 | 2015-05-19 11:57:42 -0700 | [diff] [blame] | 520 | .forDevice(deviceId).withSelector(fwd.selector()) |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 521 | .withTreatment(treatmentBuilder.build()); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 522 | |
| 523 | if (fwd.permanent()) { |
| 524 | ruleBuilder.makePermanent(); |
| 525 | } else { |
| 526 | ruleBuilder.makeTemporary(fwd.timeout()); |
| 527 | } |
| 528 | |
| 529 | ruleBuilder.forTable(aclTableId); |
| 530 | return Collections.singletonList(ruleBuilder.build()); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 531 | } |
| 532 | |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 533 | private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) { |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 534 | TrafficSelector selector = fwd.selector(); |
| 535 | EthTypeCriterion ethType = (EthTypeCriterion) selector |
| 536 | .getCriterion(Criterion.Type.ETH_TYPE); |
| 537 | if ((ethType == null) || |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 538 | ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) && |
| 539 | (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST))) { |
| 540 | return false; |
| 541 | } |
| 542 | return true; |
| 543 | } |
| 544 | |
| 545 | private boolean isSupportedEthDstObjective(ForwardingObjective fwd) { |
| 546 | TrafficSelector selector = fwd.selector(); |
| 547 | EthCriterion ethDst = (EthCriterion) selector |
| 548 | .getCriterion(Criterion.Type.ETH_DST); |
| 549 | VlanIdCriterion vlanId = (VlanIdCriterion) selector |
| 550 | .getCriterion(Criterion.Type.VLAN_VID); |
| 551 | if (ethDst == null && vlanId == null) { |
| 552 | return false; |
| 553 | } |
| 554 | return true; |
| 555 | } |
| 556 | |
| 557 | protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 558 | log.debug("Processing specific fwd objective:{} in dev:{} with next:{}", |
| 559 | fwd.id(), deviceId, fwd.nextId()); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 560 | boolean isEthTypeObj = isSupportedEthTypeObjective(fwd); |
| 561 | boolean isEthDstObj = isSupportedEthDstObjective(fwd); |
| 562 | |
| 563 | if (isEthTypeObj) { |
| 564 | return processEthTypeSpecificObjective(fwd); |
| 565 | } else if (isEthDstObj) { |
| 566 | return processEthDstSpecificObjective(fwd); |
| 567 | } else { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 568 | log.warn("processSpecific: Unsupported " |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 569 | + "forwarding objective criteria"); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 570 | fail(fwd, ObjectiveError.UNSUPPORTED); |
| 571 | return Collections.emptySet(); |
| 572 | } |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | protected Collection<FlowRule> |
| 576 | processEthTypeSpecificObjective(ForwardingObjective fwd) { |
| 577 | TrafficSelector selector = fwd.selector(); |
| 578 | EthTypeCriterion ethType = (EthTypeCriterion) selector |
| 579 | .getCriterion(Criterion.Type.ETH_TYPE); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 580 | |
| 581 | TrafficSelector.Builder filteredSelectorBuilder = |
| 582 | DefaultTrafficSelector.builder(); |
| 583 | int forTableId = -1; |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 584 | if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) { |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 585 | filteredSelectorBuilder = filteredSelectorBuilder |
| 586 | .matchEthType(Ethernet.TYPE_IPV4) |
| 587 | .matchIPDst(((IPCriterion) selector |
| 588 | .getCriterion(Criterion.Type.IPV4_DST)) |
| 589 | .ip()); |
| 590 | forTableId = ipv4UnicastTableId; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 591 | log.debug("processing IPv4 specific forwarding objective:{} in dev:{}", |
| 592 | fwd.id(), deviceId); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 593 | } else { |
| 594 | filteredSelectorBuilder = filteredSelectorBuilder |
| 595 | .matchEthType(Ethernet.MPLS_UNICAST) |
| 596 | .matchMplsLabel(((MplsCriterion) |
| 597 | selector.getCriterion(Criterion.Type.MPLS_LABEL)).label()); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 598 | if (selector.getCriterion(Criterion.Type.MPLS_BOS) != null) { |
| 599 | filteredSelectorBuilder.matchMplsBos(((MplsBosCriterion) |
| 600 | selector.getCriterion(Type.MPLS_BOS)).mplsBos()); |
| 601 | } |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 602 | forTableId = mplsTableId; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 603 | log.debug("processing MPLS specific forwarding objective:{} in dev:{}", |
| 604 | fwd.id(), deviceId); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment |
| 608 | .builder(); |
| 609 | if (fwd.treatment() != null) { |
| 610 | for (Instruction i : fwd.treatment().allInstructions()) { |
| 611 | treatmentBuilder.add(i); |
| 612 | } |
| 613 | } |
| 614 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 615 | if (fwd.nextId() != null) { |
| 616 | NextGroup next = flowObjectiveStore.getNextGroup(fwd.nextId()); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 617 | if (next != null) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 618 | SpringOpenGroup soGroup = appKryo.deserialize(next.data()); |
| 619 | if (soGroup.dummy) { |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 620 | log.debug("Adding {} flow-actions for fwd. obj. {} -> next:{} " |
| 621 | + "in dev: {}", soGroup.treatment.allInstructions().size(), |
| 622 | fwd.id(), fwd.nextId(), deviceId); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 623 | for (Instruction ins : soGroup.treatment.allInstructions()) { |
| 624 | treatmentBuilder.add(ins); |
| 625 | } |
| 626 | } else { |
| 627 | GroupKey key = soGroup.key; |
| 628 | Group group = groupService.getGroup(deviceId, key); |
| 629 | if (group == null) { |
| 630 | log.warn("The group left!"); |
| 631 | fail(fwd, ObjectiveError.GROUPMISSING); |
| 632 | return Collections.emptySet(); |
| 633 | } |
| 634 | treatmentBuilder.deferred().group(group.id()); |
| 635 | log.debug("Adding OUTGROUP action to group:{} for fwd. obj. {} " |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 636 | + "for next:{} in dev: {}", group.id(), fwd.id(), |
| 637 | fwd.nextId(), deviceId); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 638 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 639 | } else { |
| 640 | log.warn("processSpecific: No associated next objective object"); |
| 641 | fail(fwd, ObjectiveError.GROUPMISSING); |
| 642 | return Collections.emptySet(); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 643 | } |
| 644 | } |
| 645 | |
| 646 | TrafficSelector filteredSelector = filteredSelectorBuilder.build(); |
| 647 | TrafficTreatment treatment = treatmentBuilder.transition(aclTableId) |
| 648 | .build(); |
| 649 | |
| 650 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 651 | .fromApp(fwd.appId()).withPriority(fwd.priority()) |
| 652 | .forDevice(deviceId).withSelector(filteredSelector) |
| 653 | .withTreatment(treatment); |
| 654 | |
| 655 | if (fwd.permanent()) { |
| 656 | ruleBuilder.makePermanent(); |
| 657 | } else { |
| 658 | ruleBuilder.makeTemporary(fwd.timeout()); |
| 659 | } |
| 660 | |
| 661 | ruleBuilder.forTable(forTableId); |
| 662 | return Collections.singletonList(ruleBuilder.build()); |
| 663 | |
| 664 | } |
| 665 | |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 666 | protected Collection<FlowRule> |
| 667 | processEthDstSpecificObjective(ForwardingObjective fwd) { |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 668 | List<FlowRule> rules = new ArrayList<>(); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 669 | |
| 670 | // Build filtered selector |
| 671 | TrafficSelector selector = fwd.selector(); |
| 672 | EthCriterion ethCriterion = (EthCriterion) selector |
| 673 | .getCriterion(Criterion.Type.ETH_DST); |
| 674 | VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector |
| 675 | .getCriterion(Criterion.Type.VLAN_VID); |
| 676 | TrafficSelector.Builder filteredSelectorBuilder = |
| 677 | DefaultTrafficSelector.builder(); |
| 678 | // Do not match MacAddress for subnet broadcast entry |
| 679 | if (!ethCriterion.mac().equals(MacAddress.NONE)) { |
| 680 | filteredSelectorBuilder.matchEthDst(ethCriterion.mac()); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 681 | log.debug("processing L2 forwarding objective:{} in dev:{}", |
| 682 | fwd.id(), deviceId); |
| 683 | } else { |
| 684 | log.debug("processing L2 Broadcast forwarding objective:{} " |
| 685 | + "in dev:{} for vlan:{}", |
| 686 | fwd.id(), deviceId, vlanIdCriterion.vlanId()); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 687 | } |
| 688 | filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId()); |
| 689 | TrafficSelector filteredSelector = filteredSelectorBuilder.build(); |
| 690 | |
| 691 | // Build filtered treatment |
| 692 | TrafficTreatment.Builder treatmentBuilder = |
| 693 | DefaultTrafficTreatment.builder(); |
| 694 | if (fwd.treatment() != null) { |
| 695 | treatmentBuilder.deferred(); |
| 696 | fwd.treatment().allInstructions().forEach(treatmentBuilder::add); |
| 697 | } |
| 698 | if (fwd.nextId() != null) { |
| 699 | NextGroup next = flowObjectiveStore.getNextGroup(fwd.nextId()); |
| 700 | if (next != null) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 701 | SpringOpenGroup soGrp = appKryo.deserialize(next.data()); |
| 702 | if (soGrp.dummy) { |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 703 | log.debug("Adding {} flow-actions for fwd. obj. {} " |
| 704 | + "in dev: {}", soGrp.treatment.allInstructions().size(), |
| 705 | fwd.id(), deviceId); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 706 | for (Instruction ins : soGrp.treatment.allInstructions()) { |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 707 | treatmentBuilder.deferred().add(ins); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 708 | } |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 709 | } else { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 710 | GroupKey key = soGrp.key; |
| 711 | Group group = groupService.getGroup(deviceId, key); |
| 712 | if (group == null) { |
| 713 | log.warn("The group left!"); |
| 714 | fail(fwd, ObjectiveError.GROUPMISSING); |
| 715 | return Collections.emptySet(); |
| 716 | } |
| 717 | treatmentBuilder.deferred().group(group.id()); |
| 718 | log.debug("Adding OUTGROUP action to group:{} for fwd. obj. {} " |
| 719 | + "in dev: {}", group.id(), fwd.id(), deviceId); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 720 | } |
| 721 | } |
| 722 | } |
| 723 | treatmentBuilder.immediate().transition(aclTableId); |
| 724 | TrafficTreatment filteredTreatment = treatmentBuilder.build(); |
| 725 | |
| 726 | // Build bridging table entries |
| 727 | FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder(); |
| 728 | flowRuleBuilder.fromApp(fwd.appId()) |
| 729 | .withPriority(fwd.priority()) |
| 730 | .forDevice(deviceId) |
| 731 | .withSelector(filteredSelector) |
| 732 | .withTreatment(filteredTreatment) |
| 733 | .forTable(dstMacTableId); |
| 734 | if (fwd.permanent()) { |
| 735 | flowRuleBuilder.makePermanent(); |
| 736 | } else { |
| 737 | flowRuleBuilder.makeTemporary(fwd.timeout()); |
| 738 | } |
| 739 | rules.add(flowRuleBuilder.build()); |
| 740 | |
| 741 | /* |
| 742 | // TODO Emulate source MAC table behavior |
| 743 | // Do not install source MAC table entry for subnet broadcast |
| 744 | if (!ethCriterion.mac().equals(MacAddress.NONE)) { |
| 745 | // Build filtered selector |
| 746 | selector = fwd.selector(); |
| 747 | ethCriterion = (EthCriterion) selector.getCriterion(Criterion.Type.ETH_DST); |
| 748 | filteredSelectorBuilder = DefaultTrafficSelector.builder(); |
| 749 | filteredSelectorBuilder.matchEthSrc(ethCriterion.mac()); |
| 750 | filteredSelector = filteredSelectorBuilder.build(); |
| 751 | |
| 752 | // Build empty treatment. Apply existing instruction if match. |
| 753 | treatmentBuilder = DefaultTrafficTreatment.builder(); |
| 754 | filteredTreatment = treatmentBuilder.build(); |
| 755 | |
| 756 | // Build bridging table entries |
| 757 | flowRuleBuilder = DefaultFlowRule.builder(); |
| 758 | flowRuleBuilder.fromApp(fwd.appId()) |
| 759 | .withPriority(fwd.priority()) |
| 760 | .forDevice(deviceId) |
| 761 | .withSelector(filteredSelector) |
| 762 | .withTreatment(filteredTreatment) |
| 763 | .forTable(srcMacTableId) |
| 764 | .makePermanent(); |
| 765 | rules.add(flowRuleBuilder.build()); |
| 766 | } |
| 767 | */ |
| 768 | |
| 769 | return rules; |
| 770 | } |
| 771 | |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 772 | /* |
| 773 | * Note: CpqD switches do not handle MPLS-related operation properly |
| 774 | * for a packet with VLAN tag. We pop VLAN here as a workaround. |
| 775 | * Side effect: HostService learns redundant hosts with same MAC but |
| 776 | * different VLAN. No known side effect on the network reachability. |
| 777 | */ |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 778 | protected List<FlowRule> processEthDstFilter(EthCriterion ethCriterion, |
| 779 | VlanIdCriterion vlanIdCriterion, |
| 780 | FilteringObjective filt, |
| 781 | VlanId assignedVlan, |
| 782 | ApplicationId applicationId) { |
| 783 | //handling untagged packets via assigned VLAN |
| 784 | if (vlanIdCriterion.vlanId() == VlanId.NONE) { |
| 785 | vlanIdCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan); |
| 786 | } |
| 787 | |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 788 | List<FlowRule> rules = new ArrayList<>(); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 789 | TrafficSelector.Builder selectorIp = DefaultTrafficSelector |
| 790 | .builder(); |
| 791 | TrafficTreatment.Builder treatmentIp = DefaultTrafficTreatment |
| 792 | .builder(); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 793 | selectorIp.matchEthDst(ethCriterion.mac()); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 794 | selectorIp.matchEthType(Ethernet.TYPE_IPV4); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 795 | selectorIp.matchVlanId(vlanIdCriterion.vlanId()); |
| 796 | treatmentIp.popVlan(); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 797 | treatmentIp.transition(ipv4UnicastTableId); |
| 798 | FlowRule ruleIp = DefaultFlowRule.builder().forDevice(deviceId) |
| 799 | .withSelector(selectorIp.build()) |
| 800 | .withTreatment(treatmentIp.build()) |
| 801 | .withPriority(filt.priority()).fromApp(applicationId) |
| 802 | .makePermanent().forTable(tmacTableId).build(); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 803 | log.debug("adding IP ETH rule for MAC: {}", ethCriterion.mac()); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 804 | rules.add(ruleIp); |
| 805 | |
| 806 | TrafficSelector.Builder selectorMpls = DefaultTrafficSelector |
| 807 | .builder(); |
| 808 | TrafficTreatment.Builder treatmentMpls = DefaultTrafficTreatment |
| 809 | .builder(); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 810 | selectorMpls.matchEthDst(ethCriterion.mac()); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 811 | selectorMpls.matchEthType(Ethernet.MPLS_UNICAST); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 812 | selectorMpls.matchVlanId(vlanIdCriterion.vlanId()); |
| 813 | treatmentMpls.popVlan(); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 814 | treatmentMpls.transition(mplsTableId); |
| 815 | FlowRule ruleMpls = DefaultFlowRule.builder() |
| 816 | .forDevice(deviceId).withSelector(selectorMpls.build()) |
| 817 | .withTreatment(treatmentMpls.build()) |
| 818 | .withPriority(filt.priority()).fromApp(applicationId) |
| 819 | .makePermanent().forTable(tmacTableId).build(); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 820 | log.debug("adding MPLS ETH rule for MAC: {}", ethCriterion.mac()); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 821 | rules.add(ruleMpls); |
| 822 | |
| 823 | return rules; |
| 824 | } |
| 825 | |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 826 | protected List<FlowRule> processVlanIdFilter(VlanIdCriterion vlanIdCriterion, |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 827 | FilteringObjective filt, |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 828 | VlanId assignedVlan, |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 829 | ApplicationId applicationId) { |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 830 | List<FlowRule> rules = new ArrayList<>(); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 831 | log.debug("adding rule for VLAN: {}", vlanIdCriterion.vlanId()); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 832 | TrafficSelector.Builder selector = DefaultTrafficSelector |
| 833 | .builder(); |
| 834 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment |
| 835 | .builder(); |
| 836 | PortCriterion p = (PortCriterion) filt.key(); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 837 | if (vlanIdCriterion.vlanId() != VlanId.NONE) { |
| 838 | selector.matchVlanId(vlanIdCriterion.vlanId()); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 839 | selector.matchInPort(p.port()); |
| 840 | treatment.deferred().popVlan(); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 841 | } else { |
| 842 | selector.matchInPort(p.port()); |
| 843 | treatment.immediate().pushVlan().setVlanId(assignedVlan); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 844 | } |
| 845 | treatment.transition(tmacTableId); |
| 846 | FlowRule rule = DefaultFlowRule.builder().forDevice(deviceId) |
| 847 | .withSelector(selector.build()) |
| 848 | .withTreatment(treatment.build()) |
| 849 | .withPriority(filt.priority()).fromApp(applicationId) |
| 850 | .makePermanent().forTable(vlanTableId).build(); |
| 851 | rules.add(rule); |
| 852 | |
| 853 | return rules; |
| 854 | } |
| 855 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 856 | private void processFilter(FilteringObjective filt, boolean install, |
| 857 | ApplicationId applicationId) { |
| 858 | // This driver only processes filtering criteria defined with switch |
| 859 | // ports as the key |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 860 | if (filt.key().equals(Criteria.dummy()) |
| 861 | || filt.key().type() != Criterion.Type.IN_PORT) { |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 862 | log.warn("No key defined in filtering objective from app: {}. Not" |
| 863 | + "processing filtering objective", applicationId); |
| 864 | fail(filt, ObjectiveError.UNKNOWN); |
| 865 | return; |
| 866 | } |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 867 | |
| 868 | EthCriterion ethCriterion = null; |
| 869 | VlanIdCriterion vlanIdCriterion = null; |
| 870 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 871 | // convert filtering conditions for switch-intfs into flowrules |
| 872 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 873 | |
| 874 | for (Criterion criterion : filt.conditions()) { |
| 875 | if (criterion.type() == Criterion.Type.ETH_DST) { |
| 876 | ethCriterion = (EthCriterion) criterion; |
| 877 | } else if (criterion.type() == Criterion.Type.VLAN_VID) { |
| 878 | vlanIdCriterion = (VlanIdCriterion) criterion; |
| 879 | } else if (criterion.type() == Criterion.Type.IPV4_DST) { |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 880 | log.debug("driver does not process IP filtering rules as it " + |
| 881 | "sends all misses in the IP table to the controller"); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 882 | } else { |
| 883 | log.warn("Driver does not currently process filtering condition" |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 884 | + " of type: {}", criterion.type()); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 885 | fail(filt, ObjectiveError.UNSUPPORTED); |
| 886 | } |
| 887 | } |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 888 | |
| 889 | VlanId assignedVlan = null; |
| 890 | if (vlanIdCriterion != null && vlanIdCriterion.vlanId() == VlanId.NONE) { |
| 891 | // Assign a VLAN ID to untagged packets |
| 892 | if (filt.meta() == null) { |
| 893 | log.error("Missing metadata in filtering objective required " |
| 894 | + "for vlan assignment in dev {}", deviceId); |
| 895 | fail(filt, ObjectiveError.BADPARAMS); |
| 896 | return; |
| 897 | } |
| 898 | for (Instruction i : filt.meta().allInstructions()) { |
| 899 | if (i instanceof ModVlanIdInstruction) { |
| 900 | assignedVlan = ((ModVlanIdInstruction) i).vlanId(); |
| 901 | } |
| 902 | } |
| 903 | if (assignedVlan == null) { |
| 904 | log.error("Driver requires an assigned vlan-id to tag incoming " |
| 905 | + "untagged packets. Not processing vlan filters on " |
| 906 | + "device {}", deviceId); |
| 907 | fail(filt, ObjectiveError.BADPARAMS); |
| 908 | return; |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | if (ethCriterion == null) { |
| 913 | log.debug("filtering objective missing dstMac, cannot program TMAC table"); |
| 914 | } else { |
| 915 | for (FlowRule tmacRule : processEthDstFilter(ethCriterion, |
| 916 | vlanIdCriterion, |
| 917 | filt, |
| 918 | assignedVlan, |
| 919 | applicationId)) { |
| 920 | log.debug("adding MAC filtering rules in TMAC table: {} for dev: {}", |
| 921 | tmacRule, deviceId); |
| 922 | ops = install ? ops.add(tmacRule) : ops.remove(tmacRule); |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | if (ethCriterion == null || vlanIdCriterion == null) { |
| 927 | log.debug("filtering objective missing dstMac or vlan, cannot program" |
| 928 | + "Vlan Table"); |
| 929 | } else { |
| 930 | for (FlowRule vlanRule : processVlanIdFilter(vlanIdCriterion, |
| 931 | filt, |
| 932 | assignedVlan, |
| 933 | applicationId)) { |
| 934 | log.debug("adding VLAN filtering rule in VLAN table: {} for dev: {}", |
| 935 | vlanRule, deviceId); |
| 936 | ops = install ? ops.add(vlanRule) : ops.remove(vlanRule); |
| 937 | } |
| 938 | } |
| 939 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 940 | // apply filtering flow rules |
| 941 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 942 | @Override |
| 943 | public void onSuccess(FlowRuleOperations ops) { |
| 944 | pass(filt); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 945 | log.debug("Provisioned tables in {} with fitering " |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 946 | + "rules", deviceId); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | @Override |
| 950 | public void onError(FlowRuleOperations ops) { |
| 951 | fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 952 | log.warn("Failed to provision tables in {} with " |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 953 | + "fitering rules", deviceId); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 954 | } |
| 955 | })); |
| 956 | } |
| 957 | |
| 958 | protected void setTableMissEntries() { |
| 959 | // set all table-miss-entries |
| 960 | populateTableMissEntry(vlanTableId, true, false, false, -1); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 961 | populateTableMissEntry(tmacTableId, false, false, true, dstMacTableId); |
| 962 | populateTableMissEntry(ipv4UnicastTableId, false, true, true, aclTableId); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 963 | populateTableMissEntry(mplsTableId, false, true, true, aclTableId); |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 964 | populateTableMissEntry(dstMacTableId, false, false, true, aclTableId); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 965 | populateTableMissEntry(aclTableId, false, false, false, -1); |
| 966 | } |
| 967 | |
| 968 | protected void populateTableMissEntry(int tableToAdd, |
| 969 | boolean toControllerNow, |
| 970 | boolean toControllerWrite, |
| 971 | boolean toTable, int tableToSend) { |
| 972 | TrafficSelector selector = DefaultTrafficSelector.builder().build(); |
| 973 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 974 | |
| 975 | if (toControllerNow) { |
| 976 | tBuilder.setOutput(PortNumber.CONTROLLER); |
| 977 | } |
| 978 | |
| 979 | if (toControllerWrite) { |
| 980 | tBuilder.deferred().setOutput(PortNumber.CONTROLLER); |
| 981 | } |
| 982 | |
| 983 | if (toTable) { |
| 984 | tBuilder.transition(tableToSend); |
| 985 | } |
| 986 | |
| 987 | FlowRule flow = DefaultFlowRule.builder().forDevice(deviceId) |
| 988 | .withSelector(selector).withTreatment(tBuilder.build()) |
| 989 | .withPriority(0).fromApp(appId).makePermanent() |
| 990 | .forTable(tableToAdd).build(); |
| 991 | |
| 992 | flowRuleService.applyFlowRules(flow); |
| 993 | } |
| 994 | |
| 995 | private void pass(Objective obj) { |
| 996 | if (obj.context().isPresent()) { |
| 997 | obj.context().get().onSuccess(obj); |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | protected void fail(Objective obj, ObjectiveError error) { |
| 1002 | if (obj.context().isPresent()) { |
| 1003 | obj.context().get().onError(obj, error); |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | private class InnerGroupListener implements GroupListener { |
| 1008 | @Override |
| 1009 | public void event(GroupEvent event) { |
| 1010 | if (event.type() == GroupEvent.Type.GROUP_ADDED) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1011 | log.trace("InnerGroupListener: Group ADDED " |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1012 | + "event received in device {}", deviceId); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 1013 | GroupKey key = event.subject().appCookie(); |
| 1014 | |
| 1015 | NextObjective obj = pendingGroups.getIfPresent(key); |
| 1016 | if (obj != null) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1017 | log.debug("Group verified: dev:{} gid:{} <<->> nextId:{}", |
| 1018 | deviceId, event.subject().id(), obj.id()); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 1019 | flowObjectiveStore |
| 1020 | .putNextGroup(obj.id(), |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1021 | new SpringOpenGroup(key, null)); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 1022 | pass(obj); |
| 1023 | pendingGroups.invalidate(key); |
| 1024 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1025 | } else if (event.type() == GroupEvent.Type.GROUP_ADD_FAILED) { |
| 1026 | log.warn("InnerGroupListener: Group ADD " |
| 1027 | + "failed event received in device {}", deviceId); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 1028 | } |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | private class GroupChecker implements Runnable { |
| 1033 | |
| 1034 | @Override |
| 1035 | public void run() { |
| 1036 | Set<GroupKey> keys = pendingGroups |
| 1037 | .asMap() |
| 1038 | .keySet() |
| 1039 | .stream() |
| 1040 | .filter(key -> groupService.getGroup(deviceId, key) != null) |
| 1041 | .collect(Collectors.toSet()); |
| 1042 | |
| 1043 | keys.stream() |
| 1044 | .forEach(key -> { |
| 1045 | NextObjective obj = pendingGroups |
| 1046 | .getIfPresent(key); |
| 1047 | if (obj == null) { |
| 1048 | return; |
| 1049 | } |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1050 | log.debug("Group verified: dev:{} gid:{} <<->> nextId:{}", |
| 1051 | deviceId, |
| 1052 | groupService.getGroup(deviceId, key).id(), |
| 1053 | obj.id()); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 1054 | pass(obj); |
| 1055 | pendingGroups.invalidate(key); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1056 | flowObjectiveStore.putNextGroup( |
| 1057 | obj.id(), |
| 1058 | new SpringOpenGroup(key, null)); |
| 1059 | }); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 1060 | } |
| 1061 | } |
| 1062 | |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1063 | /** |
| 1064 | * SpringOpenGroup can either serve as storage for a GroupKey which can be |
| 1065 | * used to fetch the group from the Group Service, or it can be serve as storage |
| 1066 | * for Traffic Treatments which can be used as flow actions. In the latter |
| 1067 | * case, we refer to this as a dummy group. |
| 1068 | * |
| 1069 | */ |
| 1070 | private class SpringOpenGroup implements NextGroup { |
| 1071 | private final boolean dummy; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 1072 | private final GroupKey key; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1073 | private final TrafficTreatment treatment; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 1074 | |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1075 | /** |
| 1076 | * Storage for a GroupKey or a TrafficTreatment. One of the params |
| 1077 | * to this constructor must be null. |
| 1078 | * @param key represents a GroupKey |
| 1079 | * @param treatment represents flow actions in a dummy group |
| 1080 | */ |
| 1081 | public SpringOpenGroup(GroupKey key, TrafficTreatment treatment) { |
| 1082 | if (key == null) { |
| 1083 | this.key = new DefaultGroupKey(new byte[]{0}); |
| 1084 | this.treatment = treatment; |
| 1085 | this.dummy = true; |
| 1086 | } else { |
| 1087 | this.key = key; |
| 1088 | this.treatment = DefaultTrafficTreatment.builder().build(); |
| 1089 | this.dummy = false; |
| 1090 | } |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 1091 | } |
| 1092 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1093 | @SuppressWarnings("unused") |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 1094 | public GroupKey key() { |
| 1095 | return key; |
| 1096 | } |
| 1097 | |
| 1098 | @Override |
| 1099 | public byte[] data() { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1100 | return appKryo.serialize(this); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | } |
| 1104 | } |