blob: 851715d8f447c00ba83c2263a39ed37b8bc44ea0 [file] [log] [blame]
Brian O'Connor7cbbbb72016-04-09 02:13:23 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Brian O'Connor7cbbbb72016-04-09 02:13:23 -07003 *
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 */
Yi Tsengef19de12017-04-24 11:33:05 -070016package org.onosproject.driver.pipeline.ofdpa;
Charles Chan188ebf52015-12-23 00:15:11 -080017
18import com.google.common.cache.Cache;
19import com.google.common.cache.CacheBuilder;
20import com.google.common.cache.RemovalCause;
21import com.google.common.cache.RemovalNotification;
Charles Chan5b9df8d2016-03-28 22:21:40 -070022import com.google.common.collect.ImmutableList;
23import com.google.common.collect.Lists;
Yi Tseng78f51f42017-02-02 13:54:58 -080024import com.google.common.collect.Sets;
Charles Chan188ebf52015-12-23 00:15:11 -080025import org.onlab.osgi.ServiceDirectory;
Charles Chan5b9df8d2016-03-28 22:21:40 -070026import org.onlab.packet.IpPrefix;
Charles Chan5270ed02016-01-30 23:22:37 -080027import org.onlab.packet.MacAddress;
Charles Chan188ebf52015-12-23 00:15:11 -080028import org.onlab.packet.MplsLabel;
29import org.onlab.packet.VlanId;
30import org.onosproject.core.ApplicationId;
Yi Tseng78f51f42017-02-02 13:54:58 -080031import org.onosproject.core.GroupId;
Charles Chan367c1c12018-10-19 16:23:28 -070032import org.onosproject.driver.extensions.Ofdpa3AllowVlanTranslationType;
33import org.onosproject.driver.extensions.OfdpaSetAllowVlanTranslation;
Charles Chan32562522016-04-07 14:37:14 -070034import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Charles Chan188ebf52015-12-23 00:15:11 -080035import org.onosproject.net.DeviceId;
36import org.onosproject.net.PortNumber;
37import org.onosproject.net.behaviour.NextGroup;
38import org.onosproject.net.behaviour.PipelinerContext;
39import org.onosproject.net.flow.DefaultTrafficTreatment;
40import org.onosproject.net.flow.TrafficSelector;
41import org.onosproject.net.flow.TrafficTreatment;
42import org.onosproject.net.flow.criteria.Criterion;
Charles Chan367c1c12018-10-19 16:23:28 -070043import org.onosproject.net.flow.criteria.PortCriterion;
Pier Ventre42287df2016-11-09 14:17:26 -080044import org.onosproject.net.flow.criteria.TunnelIdCriterion;
Charles Chan188ebf52015-12-23 00:15:11 -080045import org.onosproject.net.flow.criteria.VlanIdCriterion;
46import org.onosproject.net.flow.instructions.Instruction;
47import org.onosproject.net.flow.instructions.Instructions;
Saurav Das9df5b7c2017-08-14 16:44:43 -070048import org.onosproject.net.flow.instructions.Instructions.GroupInstruction;
Charles Chan188ebf52015-12-23 00:15:11 -080049import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Yi Tseng47f82dc2017-03-05 22:48:39 -080050import org.onosproject.net.flowobjective.DefaultNextObjective;
Charles Chan367c1c12018-10-19 16:23:28 -070051import org.onosproject.net.flowobjective.DefaultNextTreatment;
Charles Chan188ebf52015-12-23 00:15:11 -080052import org.onosproject.net.flowobjective.FlowObjectiveStore;
Charles Chan367c1c12018-10-19 16:23:28 -070053import org.onosproject.net.flowobjective.IdNextTreatment;
Charles Chan188ebf52015-12-23 00:15:11 -080054import org.onosproject.net.flowobjective.NextObjective;
Charles Chan367c1c12018-10-19 16:23:28 -070055import org.onosproject.net.flowobjective.NextTreatment;
Saurav Dasc88d4662017-05-15 15:34:25 -070056import org.onosproject.net.flowobjective.Objective.Operation;
Yi Tseng47f82dc2017-03-05 22:48:39 -080057import org.onosproject.net.flowobjective.ObjectiveContext;
Charles Chan188ebf52015-12-23 00:15:11 -080058import org.onosproject.net.flowobjective.ObjectiveError;
59import org.onosproject.net.group.DefaultGroupBucket;
60import org.onosproject.net.group.DefaultGroupDescription;
61import org.onosproject.net.group.DefaultGroupKey;
62import org.onosproject.net.group.Group;
63import org.onosproject.net.group.GroupBucket;
64import org.onosproject.net.group.GroupBuckets;
65import org.onosproject.net.group.GroupDescription;
66import org.onosproject.net.group.GroupEvent;
67import org.onosproject.net.group.GroupKey;
68import org.onosproject.net.group.GroupListener;
69import org.onosproject.net.group.GroupService;
Saurav Das8be4e3a2016-03-11 17:19:07 -080070import org.onosproject.store.service.AtomicCounter;
71import org.onosproject.store.service.StorageService;
Charles Chan188ebf52015-12-23 00:15:11 -080072import org.slf4j.Logger;
73
74import java.util.ArrayDeque;
75import java.util.ArrayList;
Saurav Dasceccf242017-08-03 18:30:35 -070076import java.util.Arrays;
Charles Chan188ebf52015-12-23 00:15:11 -080077import java.util.Collection;
78import java.util.Collections;
79import java.util.Deque;
80import java.util.List;
Charles Chand0fd5dc2016-02-16 23:14:49 -080081import java.util.Objects;
Charles Chan367c1c12018-10-19 16:23:28 -070082import java.util.Optional;
Charles Chan188ebf52015-12-23 00:15:11 -080083import java.util.Set;
84import java.util.concurrent.ConcurrentHashMap;
85import java.util.concurrent.CopyOnWriteArrayList;
86import java.util.concurrent.Executors;
87import java.util.concurrent.ScheduledExecutorService;
88import java.util.concurrent.TimeUnit;
Charles Chan188ebf52015-12-23 00:15:11 -080089import java.util.stream.Collectors;
90
91import static org.onlab.util.Tools.groupedThreads;
Yi Tsengef19de12017-04-24 11:33:05 -070092import static org.onosproject.driver.pipeline.ofdpa.Ofdpa2Pipeline.*;
pier9469f3e2019-04-17 17:05:08 +020093import static org.onosproject.driver.pipeline.ofdpa.OfdpaPipelineUtility.*;
Yi Tsengef19de12017-04-24 11:33:05 -070094import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
jayakumarthazhath655b9a82018-10-01 00:51:54 +053095import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.L2_MULTICAST_TYPE;
96import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.l2MulticastGroupKey;
Charles Chan367c1c12018-10-19 16:23:28 -070097import static org.onosproject.net.flow.criteria.Criterion.Type.IN_PORT;
Pier Ventre42287df2016-11-09 14:17:26 -080098import static org.onosproject.net.flow.criteria.Criterion.Type.TUNNEL_ID;
Pier Ventre140a8942016-11-02 07:26:38 -070099import static org.onosproject.net.flow.criteria.Criterion.Type.VLAN_VID;
Yi Tseng78f51f42017-02-02 13:54:58 -0800100import static org.onosproject.net.group.GroupDescription.Type.ALL;
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800101import static org.onosproject.net.group.GroupDescription.Type.INDIRECT;
Yi Tseng78f51f42017-02-02 13:54:58 -0800102import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Charles Chan188ebf52015-12-23 00:15:11 -0800103import static org.slf4j.LoggerFactory.getLogger;
104
105/**
Saurav Das961beb22017-03-29 19:09:17 -0700106 * Group handler that emulates Broadcom OF-DPA TTP.
Charles Chan188ebf52015-12-23 00:15:11 -0800107 */
Charles Chan361154b2016-03-24 10:23:39 -0700108public class Ofdpa2GroupHandler {
Yi Tsengef19de12017-04-24 11:33:05 -0700109 protected final Logger log = getLogger(getClass());
Yi Tsengef19de12017-04-24 11:33:05 -0700110 // Services, Stores
Charles Chan188ebf52015-12-23 00:15:11 -0800111 protected GroupService groupService;
Saurav Das8be4e3a2016-03-11 17:19:07 -0800112 protected StorageService storageService;
Yi Tsengef19de12017-04-24 11:33:05 -0700113 protected FlowObjectiveStore flowObjectiveStore;
Charles Chan188ebf52015-12-23 00:15:11 -0800114 // index number for group creation
Saurav Das8be4e3a2016-03-11 17:19:07 -0800115 private AtomicCounter nextIndex;
Yi Tsengef19de12017-04-24 11:33:05 -0700116 protected DeviceId deviceId;
Saurav Das2f2c9d02018-04-07 16:51:09 -0700117 Cache<GroupKey, List<OfdpaGroupHandlerUtility.OfdpaNextGroup>> pendingAddNextObjectives;
118 Cache<NextObjective, List<GroupKey>> pendingRemoveNextObjectives;
119 Cache<GroupKey, Set<OfdpaGroupHandlerUtility.GroupChainElem>> pendingGroups;
120 ConcurrentHashMap<GroupKey, Set<NextObjective>> pendingUpdateNextObjectives;
Yi Tseng47f82dc2017-03-05 22:48:39 -0800121 // local store for pending bucketAdds - by design there can be multiple
Charles Chan188ebf52015-12-23 00:15:11 -0800122 // pending bucket for a group
Yi Tseng47f82dc2017-03-05 22:48:39 -0800123 protected ConcurrentHashMap<Integer, Set<NextObjective>> pendingBuckets =
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700124 new ConcurrentHashMap<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700125 private ScheduledExecutorService groupCheckerExecutor =
126 Executors.newScheduledThreadPool(2, groupedThreads("onos/pipeliner", "ofdpa-%d", log));
pierf37ce522020-03-20 11:00:38 +0100127 private InnerGroupListener innerGroupListener = new InnerGroupListener();
Charles Chan40132b32017-01-22 00:19:37 -0800128 /**
129 * Determines whether this pipeline support copy ttl instructions or not.
130 *
131 * @return true if copy ttl instructions are supported
132 */
133 protected boolean supportCopyTtl() {
134 return true;
135 }
136
137 /**
138 * Determines whether this pipeline support set mpls bos instruction or not.
139 *
140 * @return true if set mpls bos instruction is supported
141 */
142 protected boolean supportSetMplsBos() {
143 return true;
144 }
145
Charles Chan0f43e472017-02-14 14:00:16 -0800146 /**
147 * Determines whether this pipeline requires popping VLAN before pushing MPLS.
148 * <p>
149 * If required, pop vlan before push mpls and add an arbitrary vlan back afterward.
150 * MPLS interface group will substitute the arbitrary VLAN with expected VLAN later on.
151 *
152 * @return true if this pipeline requires popping VLAN before pushing MPLS
153 */
154 protected boolean requireVlanPopBeforeMplsPush() {
155 return false;
156 }
157
Charles Chan367c1c12018-10-19 16:23:28 -0700158 /**
159 * Determines whether this pipeline requires AllowVlanTransition in L2 unfiltered group.
160 *
161 * @return true if the AllowVlanTransition action is required
162 */
163 protected boolean requireAllowVlanTransition() {
164 return true;
165 }
166
Alex Yashchuk4caa8e82017-12-08 17:40:05 +0200167 public void init(DeviceId deviceId, PipelinerContext context) {
Yi Tsengef19de12017-04-24 11:33:05 -0700168 ServiceDirectory serviceDirectory = context.directory();
Charles Chan188ebf52015-12-23 00:15:11 -0800169 this.deviceId = deviceId;
170 this.flowObjectiveStore = context.store();
Charles Chan188ebf52015-12-23 00:15:11 -0800171 this.groupService = serviceDirectory.get(GroupService.class);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800172 this.storageService = serviceDirectory.get(StorageService.class);
Madan Jampanid5714e02016-04-19 14:15:20 -0700173 this.nextIndex = storageService.getAtomicCounter("group-id-index-counter");
Charles Chan188ebf52015-12-23 00:15:11 -0800174
Charles Chanfc5c7802016-05-17 13:13:55 -0700175 pendingAddNextObjectives = CacheBuilder.newBuilder()
Charles Chan188ebf52015-12-23 00:15:11 -0800176 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700177 .removalListener((RemovalNotification<GroupKey, List<OfdpaNextGroup>> notification) -> {
178 if (notification.getCause() == RemovalCause.EXPIRED &&
179 Objects.nonNull(notification.getValue())) {
180 notification.getValue()
181 .forEach(ofdpaNextGrp ->
182 fail(ofdpaNextGrp.nextObjective(),
183 ObjectiveError.GROUPINSTALLATIONFAILED));
Charles Chanfc5c7802016-05-17 13:13:55 -0700184 }
185 }).build();
Charles Chan188ebf52015-12-23 00:15:11 -0800186
Charles Chanfc5c7802016-05-17 13:13:55 -0700187 pendingRemoveNextObjectives = CacheBuilder.newBuilder()
188 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700189 .removalListener((RemovalNotification<NextObjective, List<GroupKey>> notification) -> {
Charles Chanfc5c7802016-05-17 13:13:55 -0700190 if (notification.getCause() == RemovalCause.EXPIRED) {
Yi Tsengef19de12017-04-24 11:33:05 -0700191 fail(notification.getKey(),
192 ObjectiveError.GROUPREMOVALFAILED);
Charles Chan188ebf52015-12-23 00:15:11 -0800193 }
194 }).build();
Saurav Das961beb22017-03-29 19:09:17 -0700195 pendingGroups = CacheBuilder.newBuilder()
196 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700197 .removalListener((RemovalNotification<GroupKey, Set<GroupChainElem>> notification) -> {
Saurav Das961beb22017-03-29 19:09:17 -0700198 if (notification.getCause() == RemovalCause.EXPIRED) {
199 log.error("Unable to install group with key {} and pending GCEs: {}",
200 notification.getKey(), notification.getValue());
201 }
202 }).build();
Yi Tseng78f51f42017-02-02 13:54:58 -0800203 pendingUpdateNextObjectives = new ConcurrentHashMap<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700204 GroupChecker groupChecker = new GroupChecker(this);
205 groupCheckerExecutor.scheduleAtFixedRate(groupChecker, 0, 500, TimeUnit.MILLISECONDS);
pierf37ce522020-03-20 11:00:38 +0100206 groupService.addListener(innerGroupListener);
207 }
208
209 // Terminate internal references
210 public void terminate() {
211 if (nextIndex != null) {
212 nextIndex.destroy();
213 }
214 nextIndex = null;
215 if (pendingAddNextObjectives != null) {
216 pendingAddNextObjectives.cleanUp();
217 }
218 pendingAddNextObjectives = null;
219 if (pendingRemoveNextObjectives != null) {
220 pendingRemoveNextObjectives.cleanUp();
221 }
222 pendingRemoveNextObjectives = null;
223 if (pendingGroups != null) {
224 pendingGroups.cleanUp();
225 }
226 pendingGroups = null;
227 if (groupCheckerExecutor != null) {
228 groupCheckerExecutor.shutdown();
229 }
230 groupCheckerExecutor = null;
231 if (groupService != null) {
232 groupService.removeListener(innerGroupListener);
233 }
Charles Chan188ebf52015-12-23 00:15:11 -0800234 }
235
Saurav Das8be4e3a2016-03-11 17:19:07 -0800236 //////////////////////////////////////
237 // Group Creation
238 //////////////////////////////////////
239
Yi Tsengef19de12017-04-24 11:33:05 -0700240 /**
241 * Adds a list of group chain by given NextObjective.
242 *
243 * @param nextObjective the NextObjective
244 */
Charles Chan188ebf52015-12-23 00:15:11 -0800245 protected void addGroup(NextObjective nextObjective) {
246 switch (nextObjective.type()) {
247 case SIMPLE:
248 Collection<TrafficTreatment> treatments = nextObjective.next();
249 if (treatments.size() != 1) {
250 log.error("Next Objectives of type Simple should only have a "
251 + "single Traffic Treatment. Next Objective Id:{}",
252 nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -0700253 fail(nextObjective, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800254 return;
255 }
256 processSimpleNextObjective(nextObjective);
257 break;
258 case BROADCAST:
259 processBroadcastNextObjective(nextObjective);
260 break;
261 case HASHED:
Pier Ventre140a8942016-11-02 07:26:38 -0700262 if (!verifyHashedNextObjective(nextObjective)) {
263 log.error("Next Objectives of type hashed not supported. Next Objective Id:{}",
264 nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -0700265 fail(nextObjective, ObjectiveError.BADPARAMS);
Pier Ventre140a8942016-11-02 07:26:38 -0700266 return;
267 }
Charles Chan367c1c12018-10-19 16:23:28 -0700268 if (isL2Hash(nextObjective)) {
269 processL2HashedNextObjective(nextObjective);
270 return;
271 }
272 processEcmpHashedNextObjective(nextObjective);
Charles Chan188ebf52015-12-23 00:15:11 -0800273 break;
274 case FAILOVER:
Yi Tsengef19de12017-04-24 11:33:05 -0700275 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -0800276 log.warn("Unsupported next objective type {}", nextObjective.type());
277 break;
278 default:
Yi Tsengef19de12017-04-24 11:33:05 -0700279 fail(nextObjective, ObjectiveError.UNKNOWN);
Charles Chan188ebf52015-12-23 00:15:11 -0800280 log.warn("Unknown next objective type {}", nextObjective.type());
281 }
282 }
283
284 /**
jayakumarthazhath655b9a82018-10-01 00:51:54 +0530285 * Similar to processBroadcastNextObjective but handles L2 Multicast Next Objectives.
286 *
287 * @param nextObj NextObjective of L2_MULTICAST with chained NextObjectives for single homed access ports
288 */
289 private void processL2MulticastNextObjective(NextObjective nextObj) {
290
291 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
292 if (assignedVlan == null) {
293 log.warn("VLAN ID required by L2 multicast next objective is missing. Aborting group creation.");
294 fail(nextObj, ObjectiveError.BADPARAMS);
295 return;
296 }
297
298 // Group info should contain only single homed hosts for a given vlanId
299 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
300 createL2MulticastGroup(nextObj, assignedVlan, groupInfos);
301 }
302
303 private void createL2MulticastGroup(NextObjective nextObj, VlanId vlanId, List<GroupInfo> groupInfos) {
304 // Realize & represent L2 multicast group in OFDPA driver layer
305 // TODO : Need to identify significance of OfdpaNextGroup.
306 Integer l2MulticastGroupId = L2_MULTICAST_TYPE | (vlanId.toShort() << 16);
307 final GroupKey l2MulticastGroupKey = l2MulticastGroupKey(vlanId, deviceId);
308 List<Deque<GroupKey>> l2MulticastAllGroup = Lists.newArrayList();
309 groupInfos.forEach(groupInfo -> {
310 Deque<GroupKey> groupKeyChain = new ArrayDeque<>();
311 groupKeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
312 groupKeyChain.addFirst(l2MulticastGroupKey);
313 l2MulticastAllGroup.add(groupKeyChain);
314 });
315 OfdpaNextGroup ofdpaL2MulticastGroup = new OfdpaNextGroup(l2MulticastAllGroup, nextObj);
316 updatePendingNextObjective(l2MulticastGroupKey, ofdpaL2MulticastGroup);
317 // Group Chain Hierarchy creation using group service and thus in device level
318 List<GroupBucket> l2McastBuckets = new ArrayList<>();
319 groupInfos.forEach(groupInfo -> {
320 // Points to L2 interface group directly.
321 TrafficTreatment.Builder trafficTreatment = DefaultTrafficTreatment.builder();
322 trafficTreatment.group(new GroupId(groupInfo.innerMostGroupDesc().givenGroupId()));
323 GroupBucket bucket = DefaultGroupBucket.createAllGroupBucket(trafficTreatment.build());
324 l2McastBuckets.add(bucket);
325 });
326 GroupDescription l2MulticastGroupDescription =
327 new DefaultGroupDescription(
328 deviceId,
329 ALL,
330 new GroupBuckets(l2McastBuckets),
331 l2MulticastGroupKey,
332 l2MulticastGroupId,
333 nextObj.appId());
334 GroupChainElem l2MulticastGce = new GroupChainElem(l2MulticastGroupDescription,
335 groupInfos.size(), false, deviceId);
336 groupInfos.forEach(groupInfo -> {
337 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), l2MulticastGce);
338 groupService.addGroup(groupInfo.innerMostGroupDesc());
339 });
340 }
341
342 /**
Charles Chan188ebf52015-12-23 00:15:11 -0800343 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
Saurav Dasa4020382018-02-14 14:14:54 -0800344 * a chain of groups. The simple Next Objective passed in by the application
345 * is broken up into a group chain. The following chains can be created
346 * depending on the parameters in the Next Objective.
347 * 1. L2 Interface group (no chaining)
348 * 2. L3 Unicast group -> L2 Interface group
349 * 3. MPLS Interface group -> L2 Interface group
350 * 4. MPLS Swap group -> MPLS Interface group -> L2 Interface group
351 * 5. PW initiation group chain
Charles Chan188ebf52015-12-23 00:15:11 -0800352 *
353 * @param nextObj the nextObjective of type SIMPLE
354 */
355 private void processSimpleNextObjective(NextObjective nextObj) {
356 TrafficTreatment treatment = nextObj.next().iterator().next();
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800357 // determine if plain L2 or L3->L2 or MPLS Swap -> MPLS Interface -> L2
Charles Chan188ebf52015-12-23 00:15:11 -0800358 boolean plainL2 = true;
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800359 boolean mplsSwap = false;
360 MplsLabel mplsLabel = null;
Charles Chan188ebf52015-12-23 00:15:11 -0800361 for (Instruction ins : treatment.allInstructions()) {
362 if (ins.type() == Instruction.Type.L2MODIFICATION) {
363 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
364 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_DST ||
365 l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_SRC) {
366 plainL2 = false;
Charles Chan188ebf52015-12-23 00:15:11 -0800367 }
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800368 // mpls label in simple next objectives is used only to indicate
369 // a MPLS Swap group before the MPLS Interface Group
370 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) {
371 mplsSwap = true;
372 mplsLabel = ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label();
373 }
Charles Chan188ebf52015-12-23 00:15:11 -0800374 }
375 }
Charles Chan188ebf52015-12-23 00:15:11 -0800376 if (plainL2) {
377 createL2InterfaceGroup(nextObj);
378 return;
379 }
Saurav Dasa4020382018-02-14 14:14:54 -0800380 // In order to understand if it is a pseudowire related
Pier Ventre42287df2016-11-09 14:17:26 -0800381 // next objective we look for the tunnel id in the meta.
382 boolean isPw = false;
Pier Ventre140a8942016-11-02 07:26:38 -0700383 if (nextObj.meta() != null) {
Pier Ventre42287df2016-11-09 14:17:26 -0800384 TunnelIdCriterion tunnelIdCriterion = (TunnelIdCriterion) nextObj
385 .meta()
386 .getCriterion(TUNNEL_ID);
387 if (tunnelIdCriterion != null) {
388 isPw = true;
389 }
Pier Ventre140a8942016-11-02 07:26:38 -0700390 }
Andreas Pantelopoulos25db42f2018-02-08 12:42:06 -0800391 if (mplsSwap && !isPw) {
Saurav Dasa4020382018-02-14 14:14:54 -0800392 log.debug("Creating a MPLS Swap -> MPLS Interface -> L2 Interface group chain.");
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800393 // break up simple next objective to GroupChain objects
394 GroupInfo groupInfo = createL2L3Chain(treatment, nextObj.id(),
395 nextObj.appId(), true,
396 nextObj.meta());
397 if (groupInfo == null) {
398 log.error("Could not process nextObj={} in dev:{}", nextObj.id(), deviceId);
399 fail(nextObj, ObjectiveError.BADPARAMS);
400 return;
401 }
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800402 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Saurav Dasa4020382018-02-14 14:14:54 -0800403 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie()); // l2 interface
404 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie()); // mpls interface
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800405 // creating the mpls swap group and adding it to the chain
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800406 int nextGid = groupInfo.nextGroupDesc().givenGroupId();
407 int index = getNextAvailableIndex();
Saurav Dasa4020382018-02-14 14:14:54 -0800408 GroupDescription swapGroupDescription = createMplsSwap(
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800409 nextGid,
410 OfdpaMplsGroupSubType.MPLS_SWAP_LABEL,
411 index,
412 mplsLabel,
413 nextObj.appId()
414 );
Saurav Dasa4020382018-02-14 14:14:54 -0800415 // ensure swap group is added after L2L3 chain
416 GroupKey swapGroupKey = swapGroupDescription.appCookie();
417 GroupChainElem swapChainElem = new GroupChainElem(swapGroupDescription,
418 1, false, deviceId);
419 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), swapChainElem);
420 gkeyChain.addFirst(swapGroupKey);
Saurav Dasa4020382018-02-14 14:14:54 -0800421 // ensure nextObjective waits on the outermost groupKey
422 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
423 allGroupKeys.add(gkeyChain);
424 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
425 updatePendingNextObjective(swapGroupKey, ofdpaGrp);
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800426 // now we are ready to send the l2 groupDescription (inner), as all the stores
427 // that will get async replies have been updated. By waiting to update
428 // the stores, we prevent nasty race conditions.
429 groupService.addGroup(groupInfo.innerMostGroupDesc());
430 } else if (!isPw) {
Saurav Dasa4020382018-02-14 14:14:54 -0800431 boolean isMpls = false;
432 if (nextObj.meta() != null) {
433 isMpls = isNotMplsBos(nextObj.meta());
434 }
435 log.debug("Creating a {} -> L2 Interface group chain.",
436 (isMpls) ? "MPLS Interface" : "L3 Unicast");
Pier Ventre42287df2016-11-09 14:17:26 -0800437 // break up simple next objective to GroupChain objects
438 GroupInfo groupInfo = createL2L3Chain(treatment, nextObj.id(),
439 nextObj.appId(), isMpls,
440 nextObj.meta());
441 if (groupInfo == null) {
442 log.error("Could not process nextObj={} in dev:{}", nextObj.id(), deviceId);
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800443 fail(nextObj, ObjectiveError.BADPARAMS);
Pier Ventre42287df2016-11-09 14:17:26 -0800444 return;
445 }
446 // create object for local and distributed storage
447 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700448 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
449 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Saurav Dasa4020382018-02-14 14:14:54 -0800450 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
451 allGroupKeys.add(gkeyChain);
452 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Pier Ventre42287df2016-11-09 14:17:26 -0800453 // store l3groupkey with the ofdpaNextGroup for the nextObjective that depends on it
Yi Tsengef19de12017-04-24 11:33:05 -0700454 updatePendingNextObjective(groupInfo.nextGroupDesc().appCookie(), ofdpaGrp);
Pier Ventre42287df2016-11-09 14:17:26 -0800455 // now we are ready to send the l2 groupDescription (inner), as all the stores
456 // that will get async replies have been updated. By waiting to update
457 // the stores, we prevent nasty race conditions.
Yi Tsengef19de12017-04-24 11:33:05 -0700458 groupService.addGroup(groupInfo.innerMostGroupDesc());
Pier Ventre42287df2016-11-09 14:17:26 -0800459 } else {
460 // We handle the pseudo wire with a different a procedure.
461 // This procedure is meant to handle both initiation and
462 // termination of the pseudo wire.
463 processPwNextObjective(nextObj);
Charles Chan188ebf52015-12-23 00:15:11 -0800464 }
Charles Chan188ebf52015-12-23 00:15:11 -0800465 }
466
Charles Chan188ebf52015-12-23 00:15:11 -0800467 /**
468 * Creates a simple L2 Interface Group.
Charles Chan188ebf52015-12-23 00:15:11 -0800469 * @param nextObj the next Objective
470 */
471 private void createL2InterfaceGroup(NextObjective nextObj) {
Yi Tsengef19de12017-04-24 11:33:05 -0700472 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700473 if (assignedVlan == null) {
474 log.warn("VLAN ID required by simple next obj is missing. Abort.");
Yi Tsengef19de12017-04-24 11:33:05 -0700475 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800476 return;
477 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700478 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700479 // There is only one L2 interface group in this case
Yi Tsengef19de12017-04-24 11:33:05 -0700480 GroupDescription l2InterfaceGroupDesc = groupInfos.get(0).innerMostGroupDesc();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700481 // Put all dependency information into allGroupKeys
482 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
483 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
484 gkeyChain.addFirst(l2InterfaceGroupDesc.appCookie());
485 allGroupKeys.add(gkeyChain);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700486 // Point the next objective to this group
487 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
488 updatePendingNextObjective(l2InterfaceGroupDesc.appCookie(), ofdpaGrp);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700489 // Start installing the inner-most group
jayakumarthazhath655b9a82018-10-01 00:51:54 +0530490 groupService.addGroup(l2InterfaceGroupDesc); }
Charles Chan188ebf52015-12-23 00:15:11 -0800491
492 /**
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800493 * Creates an Mpls group of type swap.
494 *
495 * @param nextGroupId the next group in the chain
496 * @param subtype the mpls swap label group subtype
497 * @param index the index of the group
498 * @param mplsLabel the mpls label to swap
499 * @param applicationId the application id
500 * @return the group description
501 */
502 protected GroupDescription createMplsSwap(int nextGroupId,
503 OfdpaMplsGroupSubType subtype,
504 int index,
505 MplsLabel mplsLabel,
506 ApplicationId applicationId) {
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800507 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800508 treatment.setMpls(mplsLabel);
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800509 // We point the group to the next group.
510 treatment.group(new GroupId(nextGroupId));
511 GroupBucket groupBucket = DefaultGroupBucket
512 .createIndirectGroupBucket(treatment.build());
513 // Finally we build the group description.
514 int groupId = makeMplsLabelGroupId(subtype, index);
515 GroupKey groupKey = new DefaultGroupKey(
Saurav Dasa4020382018-02-14 14:14:54 -0800516 Ofdpa2Pipeline.appKryo.serialize(index));
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800517 return new DefaultGroupDescription(
518 deviceId,
519 INDIRECT,
520 new GroupBuckets(Collections.singletonList(groupBucket)),
521 groupKey,
522 groupId,
Saurav Dasa4020382018-02-14 14:14:54 -0800523 applicationId);
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800524 }
525
526 /**
Charles Chan188ebf52015-12-23 00:15:11 -0800527 * Creates one of two possible group-chains from the treatment
528 * passed in. Depending on the MPLS boolean, this method either creates
Charles Chan425854b2016-04-11 15:32:12 -0700529 * an L3Unicast Group --&gt; L2Interface Group, if mpls is false;
530 * or MPLSInterface Group --&gt; L2Interface Group, if mpls is true;
Charles Chan188ebf52015-12-23 00:15:11 -0800531 * The returned 'inner' group description is always the L2 Interface group.
532 *
533 * @param treatment that needs to be broken up to create the group chain
534 * @param nextId of the next objective that needs this group chain
535 * @param appId of the application that sent this next objective
536 * @param mpls determines if L3Unicast or MPLSInterface group is created
537 * @param meta metadata passed in by the application as part of the nextObjective
538 * @return GroupInfo containing the GroupDescription of the
539 * L2Interface group(inner) and the GroupDescription of the (outer)
540 * L3Unicast/MPLSInterface group. May return null if there is an
541 * error in processing the chain
542 */
Charles Chan425854b2016-04-11 15:32:12 -0700543 protected GroupInfo createL2L3Chain(TrafficTreatment treatment, int nextId,
Charles Chanf9e98652016-09-07 16:54:23 -0700544 ApplicationId appId, boolean mpls,
545 TrafficSelector meta) {
546 return createL2L3ChainInternal(treatment, nextId, appId, mpls, meta, true);
547 }
548
549 /**
550 * Internal implementation of createL2L3Chain.
551 * <p>
552 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
553 * Since it is non-OF spec, we need an extension treatment for that.
554 * The useSetVlanExtension must be set to false for OFDPA i12.
555 * </p>
556 *
557 * @param treatment that needs to be broken up to create the group chain
558 * @param nextId of the next objective that needs this group chain
559 * @param appId of the application that sent this next objective
560 * @param mpls determines if L3Unicast or MPLSInterface group is created
561 * @param meta metadata passed in by the application as part of the nextObjective
562 * @param useSetVlanExtension use the setVlanVid extension that has is_present bit set to 0.
563 * @return GroupInfo containing the GroupDescription of the
564 * L2Interface group(inner) and the GroupDescription of the (outer)
565 * L3Unicast/MPLSInterface group. May return null if there is an
566 * error in processing the chain
567 */
568 protected GroupInfo createL2L3ChainInternal(TrafficTreatment treatment, int nextId,
Pier Ventre42287df2016-11-09 14:17:26 -0800569 ApplicationId appId, boolean mpls,
570 TrafficSelector meta, boolean useSetVlanExtension) {
Charles Chan188ebf52015-12-23 00:15:11 -0800571 // for the l2interface group, get vlan and port info
572 // for the outer group, get the src/dst mac, and vlan info
573 TrafficTreatment.Builder outerTtb = DefaultTrafficTreatment.builder();
574 TrafficTreatment.Builder innerTtb = DefaultTrafficTreatment.builder();
575 VlanId vlanid = null;
576 long portNum = 0;
577 boolean setVlan = false, popVlan = false;
Yi Tseng78f51f42017-02-02 13:54:58 -0800578 MacAddress srcMac;
579 MacAddress dstMac;
Charles Chan188ebf52015-12-23 00:15:11 -0800580 for (Instruction ins : treatment.allInstructions()) {
581 if (ins.type() == Instruction.Type.L2MODIFICATION) {
582 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
583 switch (l2ins.subtype()) {
584 case ETH_DST:
Charles Chan5270ed02016-01-30 23:22:37 -0800585 dstMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
586 outerTtb.setEthDst(dstMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800587 break;
588 case ETH_SRC:
Charles Chand0fd5dc2016-02-16 23:14:49 -0800589 srcMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
590 outerTtb.setEthSrc(srcMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800591 break;
592 case VLAN_ID:
593 vlanid = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
Charles Chanf9e98652016-09-07 16:54:23 -0700594 if (useSetVlanExtension) {
595 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
596 outerTtb.extension(ofdpaSetVlanVid, deviceId);
597 } else {
598 outerTtb.setVlanId(vlanid);
599 }
Charles Chan188ebf52015-12-23 00:15:11 -0800600 setVlan = true;
601 break;
602 case VLAN_POP:
603 innerTtb.popVlan();
604 popVlan = true;
605 break;
606 case DEC_MPLS_TTL:
607 case MPLS_LABEL:
608 case MPLS_POP:
609 case MPLS_PUSH:
610 case VLAN_PCP:
611 case VLAN_PUSH:
612 default:
613 break;
614 }
615 } else if (ins.type() == Instruction.Type.OUTPUT) {
616 portNum = ((Instructions.OutputInstruction) ins).port().toLong();
617 innerTtb.add(ins);
618 } else {
Saurav Das7bcbe702017-06-13 15:35:54 -0700619 log.debug("Driver does not handle this type of TrafficTreatment"
Saurav Dasa4020382018-02-14 14:14:54 -0800620 + " instruction in l2l3chain: {} - {}", ins.type(),
621 ins);
Charles Chan188ebf52015-12-23 00:15:11 -0800622 }
623 }
Charles Chan188ebf52015-12-23 00:15:11 -0800624 if (vlanid == null && meta != null) {
625 // use metadata if available
Pier Ventre140a8942016-11-02 07:26:38 -0700626 Criterion vidCriterion = meta.getCriterion(VLAN_VID);
Charles Chan188ebf52015-12-23 00:15:11 -0800627 if (vidCriterion != null) {
628 vlanid = ((VlanIdCriterion) vidCriterion).vlanId();
629 }
630 // if vlan is not set, use the vlan in metadata for outerTtb
631 if (vlanid != null && !setVlan) {
Charles Chanf9e98652016-09-07 16:54:23 -0700632 if (useSetVlanExtension) {
633 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
634 outerTtb.extension(ofdpaSetVlanVid, deviceId);
635 } else {
636 outerTtb.setVlanId(vlanid);
637 }
Charles Chan188ebf52015-12-23 00:15:11 -0800638 }
639 }
Charles Chan188ebf52015-12-23 00:15:11 -0800640 if (vlanid == null) {
641 log.error("Driver cannot process an L2/L3 group chain without "
642 + "egress vlan information for dev: {} port:{}",
643 deviceId, portNum);
644 return null;
645 }
Charles Chan188ebf52015-12-23 00:15:11 -0800646 if (!setVlan && !popVlan) {
647 // untagged outgoing port
648 TrafficTreatment.Builder temp = DefaultTrafficTreatment.builder();
649 temp.popVlan();
Yi Tsengef19de12017-04-24 11:33:05 -0700650 innerTtb.build().allInstructions().forEach(temp::add);
Charles Chan188ebf52015-12-23 00:15:11 -0800651 innerTtb = temp;
652 }
Charles Chan188ebf52015-12-23 00:15:11 -0800653 // assemble information for ofdpa l2interface group
Yi Tsengef19de12017-04-24 11:33:05 -0700654 int l2groupId = l2GroupId(vlanid, portNum);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800655 // a globally unique groupkey that is different for ports in the same device,
Charles Chan188ebf52015-12-23 00:15:11 -0800656 // but different for the same portnumber on different devices. Also different
657 // for the various group-types created out of the same next objective.
Charles Chane849c192016-01-11 18:28:54 -0800658 int l2gk = l2InterfaceGroupKey(deviceId, vlanid, portNum);
Yi Tsengef19de12017-04-24 11:33:05 -0700659 final GroupKey l2groupkey = new DefaultGroupKey(appKryo.serialize(l2gk));
Charles Chan188ebf52015-12-23 00:15:11 -0800660 // assemble information for outer group
Yi Tsengef19de12017-04-24 11:33:05 -0700661 GroupDescription outerGrpDesc;
Charles Chan188ebf52015-12-23 00:15:11 -0800662 if (mpls) {
Yi Tsengef19de12017-04-24 11:33:05 -0700663 // outer group is MPLS Interface
Saurav Das8be4e3a2016-03-11 17:19:07 -0800664 int mplsInterfaceIndex = getNextAvailableIndex();
Yi Tsengef19de12017-04-24 11:33:05 -0700665 int mplsGroupId = MPLS_INTERFACE_TYPE | (SUBTYPE_MASK & mplsInterfaceIndex);
666 final GroupKey mplsGroupKey = new DefaultGroupKey(
667 appKryo.serialize(mplsInterfaceIndex));
Yi Tsengfa394de2017-02-01 11:26:40 -0800668 outerTtb.group(new GroupId(l2groupId));
Charles Chan188ebf52015-12-23 00:15:11 -0800669 // create the mpls-interface group description to wait for the
670 // l2 interface group to be processed
671 GroupBucket mplsinterfaceGroupBucket =
672 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
673 outerGrpDesc = new DefaultGroupDescription(
674 deviceId,
675 GroupDescription.Type.INDIRECT,
676 new GroupBuckets(Collections.singletonList(
677 mplsinterfaceGroupBucket)),
Yi Tsengef19de12017-04-24 11:33:05 -0700678 mplsGroupKey,
679 mplsGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800680 appId);
681 log.debug("Trying MPLS-Interface: device:{} gid:{} gkey:{} nextid:{}",
Yi Tsengef19de12017-04-24 11:33:05 -0700682 deviceId, Integer.toHexString(mplsGroupId),
683 mplsGroupKey, nextId);
Charles Chan188ebf52015-12-23 00:15:11 -0800684 } else {
685 // outer group is L3Unicast
Saurav Das8be4e3a2016-03-11 17:19:07 -0800686 int l3unicastIndex = getNextAvailableIndex();
687 int l3groupId = L3_UNICAST_TYPE | (TYPE_MASK & l3unicastIndex);
688 final GroupKey l3groupkey = new DefaultGroupKey(
Yi Tsengef19de12017-04-24 11:33:05 -0700689 appKryo.serialize(l3unicastIndex));
Yi Tsengfa394de2017-02-01 11:26:40 -0800690 outerTtb.group(new GroupId(l2groupId));
Charles Chan188ebf52015-12-23 00:15:11 -0800691 // create the l3unicast group description to wait for the
692 // l2 interface group to be processed
693 GroupBucket l3unicastGroupBucket =
694 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
695 outerGrpDesc = new DefaultGroupDescription(
696 deviceId,
697 GroupDescription.Type.INDIRECT,
Yi Tsengef19de12017-04-24 11:33:05 -0700698 new GroupBuckets(Collections.singletonList(l3unicastGroupBucket)),
Charles Chan188ebf52015-12-23 00:15:11 -0800699 l3groupkey,
700 l3groupId,
701 appId);
702 log.debug("Trying L3Unicast: device:{} gid:{} gkey:{} nextid:{}",
703 deviceId, Integer.toHexString(l3groupId),
704 l3groupkey, nextId);
705 }
Charles Chan188ebf52015-12-23 00:15:11 -0800706 // store l2groupkey with the groupChainElem for the outer-group that depends on it
Yi Tsengef19de12017-04-24 11:33:05 -0700707 GroupChainElem gce = new GroupChainElem(outerGrpDesc, 1, false, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800708 updatePendingGroups(l2groupkey, gce);
Yi Tsengef19de12017-04-24 11:33:05 -0700709 // create group description for the inner l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700710 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800711 DefaultGroupBucket.createIndirectGroupBucket(innerTtb.build());
712 GroupDescription l2groupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -0700713 new DefaultGroupDescription(deviceId,
714 GroupDescription.Type.INDIRECT,
715 new GroupBuckets(Collections.singletonList(l2InterfaceGroupBucket)),
716 l2groupkey,
717 l2groupId,
718 appId);
Charles Chan188ebf52015-12-23 00:15:11 -0800719 log.debug("Trying L2Interface: device:{} gid:{} gkey:{} nextId:{}",
720 deviceId, Integer.toHexString(l2groupId),
721 l2groupkey, nextId);
722 return new GroupInfo(l2groupDescription, outerGrpDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800723 }
724
725 /**
726 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
727 * a chain of groups. The broadcast Next Objective passed in by the application
728 * has to be broken up into a group chain comprising of an
Saurav Das1a129a02016-11-18 15:21:57 -0800729 * L2 Flood group or L3 Multicast group, whose buckets point to L2 Interface groups.
Charles Chan188ebf52015-12-23 00:15:11 -0800730 *
731 * @param nextObj the nextObjective of type BROADCAST
732 */
733 private void processBroadcastNextObjective(NextObjective nextObj) {
Yi Tsengef19de12017-04-24 11:33:05 -0700734 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700735 if (assignedVlan == null) {
736 log.warn("VLAN ID required by broadcast next obj is missing. Abort.");
Yi Tsengef19de12017-04-24 11:33:05 -0700737 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800738 return;
739 }
Charles Chan188ebf52015-12-23 00:15:11 -0800740
jayakumarthazhath655b9a82018-10-01 00:51:54 +0530741 // Handling L2 multicast cases.
742 MacAddress dstMac = readEthDstFromSelector(nextObj.meta());
743 if (dstMac != null && dstMac.isMulticast()) {
744 processL2MulticastNextObjective(nextObj);
Jayakumar Thazhath812aa762018-10-24 01:22:04 -0400745 return;
jayakumarthazhath655b9a82018-10-01 00:51:54 +0530746 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700747
Charles Chan367c1c12018-10-19 16:23:28 -0700748 // FIXME Improve the logic
749 // If L2 load balancer is not involved, use L2IG. Otherwise, use L2UG.
750 // The purpose is to make sure existing XConnect logic can still work on a configured port.
751 List<GroupInfo> groupInfos;
752 if (nextObj.nextTreatments().stream().allMatch(n -> n.type() == NextTreatment.Type.TREATMENT)) {
753 groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
754 log.debug("prepareL2InterfaceGroup");
755 } else {
756 groupInfos = prepareL2UnfilteredGroup(nextObj);
757 log.debug("prepareL2UnfilteredGroup");
758 }
759
pier0023ca92018-11-29 10:32:40 -0800760 if (groupInfos == null || groupInfos.isEmpty()) {
761 log.warn("No buckets for Broadcast NextObj {}", nextObj);
762 fail(nextObj, ObjectiveError.GROUPMISSING);
763 return;
764 }
765
Yi Tsengef19de12017-04-24 11:33:05 -0700766 IpPrefix ipDst = readIpDstFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700767 if (ipDst != null) {
768 if (ipDst.isMulticast()) {
769 createL3MulticastGroup(nextObj, assignedVlan, groupInfos);
770 } else {
771 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -0700772 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700773 }
774 } else {
775 createL2FloodGroup(nextObj, assignedVlan, groupInfos);
776 }
777 }
778
Charles Chan367c1c12018-10-19 16:23:28 -0700779 private List<GroupInfo> prepareL2UnfilteredGroup(NextObjective nextObj) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700780 ImmutableList.Builder<GroupInfo> groupInfoBuilder = ImmutableList.builder();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700781 // break up broadcast next objective to multiple groups
Charles Chan367c1c12018-10-19 16:23:28 -0700782 Collection<TrafficTreatment> treatments = nextObj.nextTreatments().stream()
783 .filter(nt -> nt.type() == NextTreatment.Type.TREATMENT)
784 .map(nt -> ((DefaultNextTreatment) nt).treatment())
785 .collect(Collectors.toSet());
786 Collection<Integer> nextIds = nextObj.nextTreatments().stream()
787 .filter(nt -> nt.type() == NextTreatment.Type.ID)
788 .map(nt -> ((IdNextTreatment) nt).nextId())
789 .collect(Collectors.toSet());
790
791 // Each treatment is converted to an L2 unfiltered group
792 treatments.forEach(treatment -> {
793 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
794 // Extract port information
795 PortNumber port = treatment.allInstructions().stream()
796 .map(instr -> (Instructions.OutputInstruction) instr)
797 .map(instr -> instr.port())
798 .findFirst().orElse(null);
799 if (port == null) {
800 log.debug("Skip bucket without output instruction");
801 return;
802 }
803 tBuilder.setOutput(port);
804 if (requireAllowVlanTransition()) {
805 tBuilder.extension(new OfdpaSetAllowVlanTranslation(Ofdpa3AllowVlanTranslationType.ALLOW), deviceId);
806 }
807
808 // Build L2UG
809 int l2ugk = l2UnfilteredGroupKey(deviceId, port.toLong());
810 final GroupKey l2UnfilterGroupKey = new DefaultGroupKey(appKryo.serialize(l2ugk));
811 int l2UnfilteredGroupId = L2_UNFILTERED_TYPE | ((int) port.toLong() & FOUR_NIBBLE_MASK);
812 GroupBucket l2UnfilteredGroupBucket = DefaultGroupBucket.createIndirectGroupBucket(tBuilder.build());
813 GroupDescription l2UnfilteredGroupDesc = new DefaultGroupDescription(deviceId,
814 GroupDescription.Type.INDIRECT,
815 new GroupBuckets(Collections.singletonList(l2UnfilteredGroupBucket)),
816 l2UnfilterGroupKey,
817 l2UnfilteredGroupId,
818 nextObj.appId());
819 log.debug("Trying L2-Unfiltered: device:{} gid:{} gkey:{} nextid:{}",
820 deviceId, Integer.toHexString(l2UnfilteredGroupId), l2UnfilterGroupKey, nextObj.id());
821 groupInfoBuilder.add(new GroupInfo(l2UnfilteredGroupDesc, l2UnfilteredGroupDesc));
822 });
pier0023ca92018-11-29 10:32:40 -0800823 // Save the current count
824 int counts = groupInfoBuilder.build().size();
Charles Chan367c1c12018-10-19 16:23:28 -0700825 // Lookup each nextId in the store and obtain the group information
826 nextIds.forEach(nextId -> {
pier0023ca92018-11-29 10:32:40 -0800827 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextId);
828 if (nextGroup != null) {
829 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(nextGroup.data());
830 GroupKey topGroupKey = allActiveKeys.get(0).getFirst();
831 GroupDescription groupDesc = groupService.getGroup(deviceId, topGroupKey);
832 if (groupDesc != null) {
833 log.debug("Trying L2-Hash device:{} gid:{}, gkey:{}, nextid:{}",
834 deviceId, Integer.toHexString(((Group) groupDesc).id().id()), topGroupKey, nextId);
835 groupInfoBuilder.add(new GroupInfo(groupDesc, groupDesc));
836 } else {
837 log.error("Not found L2-Hash device:{}, gkey:{}, nextid:{}", deviceId, topGroupKey, nextId);
838 }
839 } else {
840 log.error("Not found NextGroup device:{}, nextid:{}", deviceId, nextId);
841 }
Charles Chan367c1c12018-10-19 16:23:28 -0700842 });
pier0023ca92018-11-29 10:32:40 -0800843 // Compare the size before and after to detect problems during the creation
844 ImmutableList<GroupInfo> groupInfos = groupInfoBuilder.build();
845 return (counts + nextIds.size()) == groupInfos.size() ? groupInfos : ImmutableList.of();
Charles Chan367c1c12018-10-19 16:23:28 -0700846 }
847
848 private List<GroupInfo> prepareL2InterfaceGroup(NextObjective nextObj, VlanId assignedVlan) {
849 ImmutableList.Builder<GroupInfo> groupInfoBuilder = ImmutableList.builder();
850 // break up broadcast next objective to multiple groups
851 Collection<TrafficTreatment> buckets = nextObj.nextTreatments().stream()
852 .filter(nt -> nt.type() == NextTreatment.Type.TREATMENT)
853 .map(nt -> ((DefaultNextTreatment) nt).treatment())
854 .collect(Collectors.toSet());
855
856 // Each treatment is converted to an L2 interface group
Charles Chan188ebf52015-12-23 00:15:11 -0800857 for (TrafficTreatment treatment : buckets) {
858 TrafficTreatment.Builder newTreatment = DefaultTrafficTreatment.builder();
859 PortNumber portNum = null;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700860 VlanId egressVlan = null;
Charles Chan188ebf52015-12-23 00:15:11 -0800861 // ensure that the only allowed treatments are pop-vlan and output
862 for (Instruction ins : treatment.allInstructions()) {
863 if (ins.type() == Instruction.Type.L2MODIFICATION) {
864 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
865 switch (l2ins.subtype()) {
866 case VLAN_POP:
867 newTreatment.add(l2ins);
868 break;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700869 case VLAN_ID:
870 egressVlan = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
871 break;
Charles Chan188ebf52015-12-23 00:15:11 -0800872 default:
873 log.debug("action {} not permitted for broadcast nextObj",
874 l2ins.subtype());
875 break;
876 }
877 } else if (ins.type() == Instruction.Type.OUTPUT) {
878 portNum = ((Instructions.OutputInstruction) ins).port();
879 newTreatment.add(ins);
880 } else {
Charles Chane849c192016-01-11 18:28:54 -0800881 log.debug("TrafficTreatment of type {} not permitted in " +
882 " broadcast nextObjective", ins.type());
Charles Chan188ebf52015-12-23 00:15:11 -0800883 }
884 }
Yi Tsengef19de12017-04-24 11:33:05 -0700885 if (portNum == null) {
Ruchi Sahotaef0761c2019-01-28 01:08:18 +0000886 log.debug("Can't find output port for the bucket {}.", treatment);
Yi Tsengef19de12017-04-24 11:33:05 -0700887 continue;
888 }
Charles Chan188ebf52015-12-23 00:15:11 -0800889 // assemble info for l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700890 VlanId l2InterfaceGroupVlan =
891 (egressVlan != null && !assignedVlan.equals(egressVlan)) ?
892 egressVlan : assignedVlan;
893 int l2gk = l2InterfaceGroupKey(deviceId, l2InterfaceGroupVlan, portNum.toLong());
894 final GroupKey l2InterfaceGroupKey =
Yi Tsengef19de12017-04-24 11:33:05 -0700895 new DefaultGroupKey(appKryo.serialize(l2gk));
Charles Chan372b63e2017-02-07 12:10:53 -0800896 int l2InterfaceGroupId = L2_INTERFACE_TYPE |
Charles Chan367c1c12018-10-19 16:23:28 -0700897 ((l2InterfaceGroupVlan.toShort() & THREE_NIBBLE_MASK) << PORT_LEN) |
898 ((int) portNum.toLong() & FOUR_NIBBLE_MASK);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700899 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800900 DefaultGroupBucket.createIndirectGroupBucket(newTreatment.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700901 GroupDescription l2InterfaceGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -0700902 new DefaultGroupDescription(deviceId,
903 GroupDescription.Type.INDIRECT,
904 new GroupBuckets(Collections.singletonList(
905 l2InterfaceGroupBucket)),
906 l2InterfaceGroupKey,
907 l2InterfaceGroupId,
908 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800909 log.debug("Trying L2-Interface: device:{} gid:{} gkey:{} nextid:{}",
Charles Chan5b9df8d2016-03-28 22:21:40 -0700910 deviceId, Integer.toHexString(l2InterfaceGroupId),
911 l2InterfaceGroupKey, nextObj.id());
Ruchi Sahotaef0761c2019-01-28 01:08:18 +0000912
Charles Chan5b9df8d2016-03-28 22:21:40 -0700913 groupInfoBuilder.add(new GroupInfo(l2InterfaceGroupDescription,
914 l2InterfaceGroupDescription));
Charles Chan188ebf52015-12-23 00:15:11 -0800915 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700916 return groupInfoBuilder.build();
917 }
Charles Chan188ebf52015-12-23 00:15:11 -0800918
Ruchi Sahotaef0761c2019-01-28 01:08:18 +0000919 private GroupInfo prepareL3UnicastGroup(NextObjective nextObj, NextGroup next) {
920
921 ImmutableList.Builder<GroupInfo> groupInfoBuilder = ImmutableList.builder();
922 TrafficTreatment treatment = nextObj.next().iterator().next();
923
924 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
925 if (assignedVlan == null) {
926 log.warn("VLAN ID required by next obj is missing. Abort.");
927 return null;
928 }
929
930 List<GroupInfo> l2GroupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
931 GroupDescription l2InterfaceGroupDesc = l2GroupInfos.get(0).innerMostGroupDesc();
932 GroupKey l2groupkey = l2InterfaceGroupDesc.appCookie();
933
934 TrafficTreatment.Builder outerTtb = DefaultTrafficTreatment.builder();
935 VlanId vlanid = null;
936 MacAddress srcMac;
937 MacAddress dstMac;
938 for (Instruction ins : treatment.allInstructions()) {
939 if (ins.type() == Instruction.Type.L2MODIFICATION) {
940 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
941 switch (l2ins.subtype()) {
942 case ETH_DST:
943 dstMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
944 outerTtb.setEthDst(dstMac);
945 break;
946 case ETH_SRC:
947 srcMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
948 outerTtb.setEthSrc(srcMac);
949 break;
950 case VLAN_ID:
951 vlanid = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
952 outerTtb.setVlanId(vlanid);
953 break;
954 default:
955 break;
956 }
957 } else {
958 log.debug("Driver does not handle this type of TrafficTreatment"
959 + " instruction in l2l3chain: {} - {}", ins.type(), ins);
960 }
961 }
962
963 GroupId l2groupId = new GroupId(l2InterfaceGroupDesc.givenGroupId());
964 outerTtb.group(l2groupId);
965
966 // we need the top level group's key to point the flow to it
967 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
968 GroupKey l3groupkey = gkeys.get(0).peekFirst();
969 GroupId grpId = groupService.getGroup(deviceId, l3groupkey).id();
970 int l3groupId = grpId.id();
971
972 // create the l3unicast group description to wait for the
973 // l2 interface group to be processed
974 GroupBucket l3UnicastGroupBucket = DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
975
976 GroupDescription l3UnicastGroupDescription = new DefaultGroupDescription(deviceId,
977 GroupDescription.Type.INDIRECT,
978 new GroupBuckets(Collections.singletonList(
979 l3UnicastGroupBucket)), l3groupkey,
980 l3groupId, nextObj.appId());
981
982 // store l2groupkey with the groupChainElem for the outer-group that depends on it
983 GroupChainElem gce = new GroupChainElem(l3UnicastGroupDescription, 1, false, deviceId);
984 updatePendingGroups(l2groupkey, gce);
985
986 log.debug("Trying L3-Interface: device:{} gid:{} gkey:{} nextid:{}",
987 deviceId, Integer.toHexString(l3groupId), l3groupkey, nextObj.id());
988
989 groupInfoBuilder.add(new GroupInfo(l2InterfaceGroupDesc,
990 l3UnicastGroupDescription));
991
992 return groupInfoBuilder.build().iterator().next();
993 }
994
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700995 private void createL2FloodGroup(NextObjective nextObj, VlanId vlanId,
996 List<GroupInfo> groupInfos) {
997 // assemble info for l2 flood group. Since there can be only one flood
998 // group for a vlan, its index is always the same - 0
Yi Tsengef19de12017-04-24 11:33:05 -0700999 Integer l2FloodGroupId = L2_FLOOD_TYPE | (vlanId.toShort() << 16);
1000 final GroupKey l2FloodGroupKey = l2FloodGroupKey(vlanId, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001001 // collection of group buckets pointing to all the l2 interface groups
Yi Tsengef19de12017-04-24 11:33:05 -07001002 List<GroupBucket> l2floodBuckets = generateNextGroupBuckets(groupInfos, ALL);
Charles Chan188ebf52015-12-23 00:15:11 -08001003 // create the l2flood group-description to wait for all the
1004 // l2interface groups to be processed
1005 GroupDescription l2floodGroupDescription =
1006 new DefaultGroupDescription(
1007 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -08001008 ALL,
Charles Chan188ebf52015-12-23 00:15:11 -08001009 new GroupBuckets(l2floodBuckets),
Yi Tsengef19de12017-04-24 11:33:05 -07001010 l2FloodGroupKey,
1011 l2FloodGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -08001012 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -08001013 log.debug("Trying L2-Flood: device:{} gid:{} gkey:{} nextid:{}",
Yi Tsengef19de12017-04-24 11:33:05 -07001014 deviceId, Integer.toHexString(l2FloodGroupId),
1015 l2FloodGroupKey, nextObj.id());
Charles Chan5b9df8d2016-03-28 22:21:40 -07001016 // Put all dependency information into allGroupKeys
1017 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
1018 groupInfos.forEach(groupInfo -> {
Yi Tsengef19de12017-04-24 11:33:05 -07001019 Deque<GroupKey> groupKeyChain = new ArrayDeque<>();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001020 // In this case we should have L2 interface group only
Yi Tsengef19de12017-04-24 11:33:05 -07001021 groupKeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
1022 groupKeyChain.addFirst(l2FloodGroupKey);
1023 allGroupKeys.add(groupKeyChain);
Charles Chan5b9df8d2016-03-28 22:21:40 -07001024 });
Charles Chan5b9df8d2016-03-28 22:21:40 -07001025 // Point the next objective to this group
1026 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001027 updatePendingNextObjective(l2FloodGroupKey, ofdpaGrp);
Charles Chan5b9df8d2016-03-28 22:21:40 -07001028 GroupChainElem gce = new GroupChainElem(l2floodGroupDescription,
Yi Tsengef19de12017-04-24 11:33:05 -07001029 groupInfos.size(), false, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -07001030 groupInfos.forEach(groupInfo -> {
1031 // Point this group to the next group
Yi Tsengef19de12017-04-24 11:33:05 -07001032 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), gce);
Charles Chan5b9df8d2016-03-28 22:21:40 -07001033 // Start installing the inner-most group
Yi Tsengef19de12017-04-24 11:33:05 -07001034 groupService.addGroup(groupInfo.innerMostGroupDesc());
Charles Chan5b9df8d2016-03-28 22:21:40 -07001035 });
1036 }
1037
Pier Luigi21fffd22018-01-19 10:24:53 +01001038 private void createL3MulticastGroup(NextObjective nextObj, VlanId vlanId,
1039 List<GroupInfo> groupInfos) {
1040 // Let's create a new list mcast buckets
1041 List<GroupBucket> l3McastBuckets = createL3MulticastBucket(groupInfos);
Charles Chan5b9df8d2016-03-28 22:21:40 -07001042
1043 int l3MulticastIndex = getNextAvailableIndex();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001044 int l3MulticastGroupId = L3_MULTICAST_TYPE |
1045 vlanId.toShort() << 16 | (TYPE_VLAN_MASK & l3MulticastIndex);
1046 final GroupKey l3MulticastGroupKey =
Yi Tsengef19de12017-04-24 11:33:05 -07001047 new DefaultGroupKey(appKryo.serialize(l3MulticastIndex));
Charles Chan5b9df8d2016-03-28 22:21:40 -07001048
1049 GroupDescription l3MulticastGroupDesc = new DefaultGroupDescription(deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -08001050 ALL,
Charles Chan5b9df8d2016-03-28 22:21:40 -07001051 new GroupBuckets(l3McastBuckets),
1052 l3MulticastGroupKey,
1053 l3MulticastGroupId,
1054 nextObj.appId());
1055
1056 // Put all dependency information into allGroupKeys
1057 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
1058 groupInfos.forEach(groupInfo -> {
1059 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001060 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
Charles Chan5b9df8d2016-03-28 22:21:40 -07001061 // Add L3 interface group to the chain if there is one.
Yi Tsengef19de12017-04-24 11:33:05 -07001062 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1063 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Charles Chan5b9df8d2016-03-28 22:21:40 -07001064 }
1065 gkeyChain.addFirst(l3MulticastGroupKey);
1066 allGroupKeys.add(gkeyChain);
1067 });
Charles Chan5b9df8d2016-03-28 22:21:40 -07001068 // Point the next objective to this group
1069 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
1070 updatePendingNextObjective(l3MulticastGroupKey, ofdpaGrp);
Charles Chan5b9df8d2016-03-28 22:21:40 -07001071 GroupChainElem outerGce = new GroupChainElem(l3MulticastGroupDesc,
Yi Tsengef19de12017-04-24 11:33:05 -07001072 groupInfos.size(), false, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -07001073 groupInfos.forEach(groupInfo -> {
1074 // Point this group (L3 multicast) to the next group
Yi Tsengef19de12017-04-24 11:33:05 -07001075 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), outerGce);
Charles Chan5b9df8d2016-03-28 22:21:40 -07001076 // Point next group to inner-most group, if any
Yi Tsengef19de12017-04-24 11:33:05 -07001077 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1078 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(),
1079 1, false, deviceId);
1080 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce);
Charles Chan5b9df8d2016-03-28 22:21:40 -07001081 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001082 // Start installing the inner-most group
Yi Tsengef19de12017-04-24 11:33:05 -07001083 groupService.addGroup(groupInfo.innerMostGroupDesc());
Charles Chan5b9df8d2016-03-28 22:21:40 -07001084 });
Charles Chan188ebf52015-12-23 00:15:11 -08001085 }
1086
Charles Chan188ebf52015-12-23 00:15:11 -08001087 /**
1088 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
1089 * a chain of groups. The hashed Next Objective passed in by the application
1090 * has to be broken up into a group chain comprising of an
1091 * L3 ECMP group as the top level group. Buckets of this group can point
1092 * to a variety of groups in a group chain, depending on the whether
1093 * MPLS labels are being pushed or not.
1094 * <p>
1095 * NOTE: We do not create MPLS ECMP groups as they are unimplemented in
1096 * OF-DPA 2.0 (even though it is in the spec). Therefore we do not
1097 * check the nextObjective meta to see what is matching before being
1098 * sent to this nextObjective.
1099 *
1100 * @param nextObj the nextObjective of type HASHED
1101 */
Charles Chan367c1c12018-10-19 16:23:28 -07001102 protected void processEcmpHashedNextObjective(NextObjective nextObj) {
Charles Chan188ebf52015-12-23 00:15:11 -08001103 // storage for all group keys in the chain of groups created
1104 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
1105 List<GroupInfo> unsentGroups = new ArrayList<>();
Charles Chan367c1c12018-10-19 16:23:28 -07001106 createEcmpHashBucketChains(nextObj, allGroupKeys, unsentGroups);
Charles Chan188ebf52015-12-23 00:15:11 -08001107 // now we can create the outermost L3 ECMP group
1108 List<GroupBucket> l3ecmpGroupBuckets = new ArrayList<>();
1109 for (GroupInfo gi : unsentGroups) {
1110 // create ECMP bucket to point to the outer group
1111 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Yi Tsengef19de12017-04-24 11:33:05 -07001112 ttb.group(new GroupId(gi.nextGroupDesc().givenGroupId()));
Charles Chan188ebf52015-12-23 00:15:11 -08001113 GroupBucket sbucket = DefaultGroupBucket
1114 .createSelectGroupBucket(ttb.build());
1115 l3ecmpGroupBuckets.add(sbucket);
1116 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001117 int l3ecmpIndex = getNextAvailableIndex();
1118 int l3ecmpGroupId = L3_ECMP_TYPE | (TYPE_MASK & l3ecmpIndex);
1119 GroupKey l3ecmpGroupKey = new DefaultGroupKey(
Yi Tsengef19de12017-04-24 11:33:05 -07001120 appKryo.serialize(l3ecmpIndex));
Charles Chan188ebf52015-12-23 00:15:11 -08001121 GroupDescription l3ecmpGroupDesc =
1122 new DefaultGroupDescription(
1123 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -08001124 SELECT,
Charles Chan188ebf52015-12-23 00:15:11 -08001125 new GroupBuckets(l3ecmpGroupBuckets),
1126 l3ecmpGroupKey,
1127 l3ecmpGroupId,
1128 nextObj.appId());
1129 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
1130 l3ecmpGroupBuckets.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001131 false, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001132
1133 // create objects for local and distributed storage
Yi Tsengef19de12017-04-24 11:33:05 -07001134 allGroupKeys.forEach(gKeyChain -> gKeyChain.addFirst(l3ecmpGroupKey));
Charles Chan188ebf52015-12-23 00:15:11 -08001135 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Charles Chan188ebf52015-12-23 00:15:11 -08001136 // store l3ecmpGroupKey with the ofdpaGroupChain for the nextObjective
1137 // that depends on it
1138 updatePendingNextObjective(l3ecmpGroupKey, ofdpaGrp);
Charles Chan188ebf52015-12-23 00:15:11 -08001139 log.debug("Trying L3ECMP: device:{} gid:{} gkey:{} nextId:{}",
1140 deviceId, Integer.toHexString(l3ecmpGroupId),
1141 l3ecmpGroupKey, nextObj.id());
1142 // finally we are ready to send the innermost groups
1143 for (GroupInfo gi : unsentGroups) {
1144 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001145 Integer.toHexString(gi.innerMostGroupDesc().givenGroupId()), deviceId);
1146 updatePendingGroups(gi.nextGroupDesc().appCookie(), l3ecmpGce);
1147 groupService.addGroup(gi.innerMostGroupDesc());
Charles Chan188ebf52015-12-23 00:15:11 -08001148 }
Charles Chan188ebf52015-12-23 00:15:11 -08001149 }
1150
1151 /**
1152 * Creates group chains for all buckets in a hashed group, and stores the
1153 * GroupInfos and GroupKeys for all the groups in the lists passed in, which
1154 * should be empty.
1155 * <p>
1156 * Does not create the top level ECMP group. Does not actually send the
1157 * groups to the groupService.
1158 *
1159 * @param nextObj the Next Objective with buckets that need to be converted
1160 * to group chains
1161 * @param allGroupKeys a list to store groupKey for each bucket-group-chain
1162 * @param unsentGroups a list to store GroupInfo for each bucket-group-chain
1163 */
Charles Chan367c1c12018-10-19 16:23:28 -07001164 protected void createEcmpHashBucketChains(NextObjective nextObj,
1165 List<Deque<GroupKey>> allGroupKeys,
1166 List<GroupInfo> unsentGroups) {
Charles Chan188ebf52015-12-23 00:15:11 -08001167 // break up hashed next objective to multiple groups
1168 Collection<TrafficTreatment> buckets = nextObj.next();
1169
1170 for (TrafficTreatment bucket : buckets) {
1171 //figure out how many labels are pushed in each bucket
1172 int labelsPushed = 0;
1173 MplsLabel innermostLabel = null;
1174 for (Instruction ins : bucket.allInstructions()) {
1175 if (ins.type() == Instruction.Type.L2MODIFICATION) {
1176 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
1177 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_PUSH) {
1178 labelsPushed++;
1179 }
1180 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) {
1181 if (innermostLabel == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001182 innermostLabel =
1183 ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label();
Charles Chan188ebf52015-12-23 00:15:11 -08001184 }
1185 }
1186 }
1187 }
Yi Tsengef19de12017-04-24 11:33:05 -07001188 Deque<GroupKey> gKeyChain = new ArrayDeque<>();
Saurav Dasa4020382018-02-14 14:14:54 -08001189 // here we only deal with 0 and 1 label push
Charles Chan188ebf52015-12-23 00:15:11 -08001190 if (labelsPushed == 0) {
Yi Tsengef19de12017-04-24 11:33:05 -07001191 GroupInfo noLabelGroupInfo;
Pier Ventre140a8942016-11-02 07:26:38 -07001192 TrafficSelector metaSelector = nextObj.meta();
1193 if (metaSelector != null) {
1194 if (isNotMplsBos(metaSelector)) {
Yi Tsengef19de12017-04-24 11:33:05 -07001195 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
1196 nextObj.appId(), true,
1197 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -07001198 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001199 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
1200 nextObj.appId(), false,
1201 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -07001202 }
1203 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001204 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
1205 nextObj.appId(), false,
1206 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -07001207 }
Yi Tsengef19de12017-04-24 11:33:05 -07001208 if (noLabelGroupInfo == null) {
Charles Chan188ebf52015-12-23 00:15:11 -08001209 log.error("Could not process nextObj={} in dev:{}",
1210 nextObj.id(), deviceId);
1211 return;
1212 }
Yi Tsengef19de12017-04-24 11:33:05 -07001213 gKeyChain.addFirst(noLabelGroupInfo.innerMostGroupDesc().appCookie());
1214 gKeyChain.addFirst(noLabelGroupInfo.nextGroupDesc().appCookie());
Charles Chan188ebf52015-12-23 00:15:11 -08001215 // we can't send the inner group description yet, as we have to
1216 // create the dependent ECMP group first. So we store..
Yi Tsengef19de12017-04-24 11:33:05 -07001217 unsentGroups.add(noLabelGroupInfo);
Charles Chan188ebf52015-12-23 00:15:11 -08001218 } else if (labelsPushed == 1) {
1219 GroupInfo onelabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
1220 nextObj.appId(), true,
1221 nextObj.meta());
1222 if (onelabelGroupInfo == null) {
1223 log.error("Could not process nextObj={} in dev:{}",
1224 nextObj.id(), deviceId);
1225 return;
1226 }
1227 // we need to add another group to this chain - the L3VPN group
1228 TrafficTreatment.Builder l3vpnTtb = DefaultTrafficTreatment.builder();
Charles Chan0f43e472017-02-14 14:00:16 -08001229 if (requireVlanPopBeforeMplsPush()) {
1230 l3vpnTtb.popVlan();
1231 }
Charles Chan188ebf52015-12-23 00:15:11 -08001232 l3vpnTtb.pushMpls()
1233 .setMpls(innermostLabel)
Yi Tsengef19de12017-04-24 11:33:05 -07001234 .group(new GroupId(onelabelGroupInfo.nextGroupDesc().givenGroupId()));
Charles Chan40132b32017-01-22 00:19:37 -08001235 if (supportCopyTtl()) {
1236 l3vpnTtb.copyTtlOut();
1237 }
1238 if (supportSetMplsBos()) {
1239 l3vpnTtb.setMplsBos(true);
1240 }
Charles Chan0f43e472017-02-14 14:00:16 -08001241 if (requireVlanPopBeforeMplsPush()) {
1242 l3vpnTtb.pushVlan().setVlanId(VlanId.vlanId(VlanId.RESERVED));
1243 }
Charles Chan188ebf52015-12-23 00:15:11 -08001244 GroupBucket l3vpnGrpBkt =
1245 DefaultGroupBucket.createIndirectGroupBucket(l3vpnTtb.build());
Saurav Das8be4e3a2016-03-11 17:19:07 -08001246 int l3vpnIndex = getNextAvailableIndex();
Yi Tsengef19de12017-04-24 11:33:05 -07001247 int l3vpnGroupId = MPLS_L3VPN_SUBTYPE | (SUBTYPE_MASK & l3vpnIndex);
1248 GroupKey l3vpnGroupKey = new DefaultGroupKey(
1249 appKryo.serialize(l3vpnIndex));
Charles Chan188ebf52015-12-23 00:15:11 -08001250 GroupDescription l3vpnGroupDesc =
1251 new DefaultGroupDescription(
1252 deviceId,
1253 GroupDescription.Type.INDIRECT,
Yi Tsengef19de12017-04-24 11:33:05 -07001254 new GroupBuckets(Collections.singletonList(l3vpnGrpBkt)),
1255 l3vpnGroupKey,
1256 l3vpnGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -08001257 nextObj.appId());
Yi Tsengef19de12017-04-24 11:33:05 -07001258 GroupChainElem l3vpnGce = new GroupChainElem(l3vpnGroupDesc,
1259 1,
1260 false,
1261 deviceId);
1262 updatePendingGroups(onelabelGroupInfo.nextGroupDesc().appCookie(), l3vpnGce);
Charles Chan188ebf52015-12-23 00:15:11 -08001263
Yi Tsengef19de12017-04-24 11:33:05 -07001264 gKeyChain.addFirst(onelabelGroupInfo.innerMostGroupDesc().appCookie());
1265 gKeyChain.addFirst(onelabelGroupInfo.nextGroupDesc().appCookie());
1266 gKeyChain.addFirst(l3vpnGroupKey);
Charles Chan188ebf52015-12-23 00:15:11 -08001267 //now we can replace the outerGrpDesc with the one we just created
Yi Tsengef19de12017-04-24 11:33:05 -07001268 onelabelGroupInfo.nextGroupDesc(l3vpnGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -08001269 // we can't send the innermost group yet, as we have to create
1270 // the dependent ECMP group first. So we store ...
1271 unsentGroups.add(onelabelGroupInfo);
Yi Tsengef19de12017-04-24 11:33:05 -07001272 log.debug("Trying L3VPN: device:{} gid:{} group key:{} nextId:{}",
1273 deviceId, Integer.toHexString(l3vpnGroupId),
1274 l3vpnGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -08001275 } else {
1276 log.warn("Driver currently does not handle more than 1 MPLS "
1277 + "labels. Not processing nextObjective {}", nextObj.id());
1278 return;
1279 }
Charles Chan188ebf52015-12-23 00:15:11 -08001280 // all groups in this chain
Yi Tsengef19de12017-04-24 11:33:05 -07001281 allGroupKeys.add(gKeyChain);
Charles Chan188ebf52015-12-23 00:15:11 -08001282 }
1283 }
1284
Pier Ventre42287df2016-11-09 14:17:26 -08001285 /**
Charles Chan367c1c12018-10-19 16:23:28 -07001286 * Create L2 hash group.
1287 *
1288 * @param nextObj next objective
1289 */
1290 private void processL2HashedNextObjective(NextObjective nextObj) {
1291 int l2LbIndex = Optional.ofNullable(nextObj.meta().getCriterion(IN_PORT))
1292 .map(c -> (PortCriterion) c).map(PortCriterion::port).map(PortNumber::toLong).map(Long::intValue)
1293 .orElse(-1);
1294 if (l2LbIndex == -1) {
1295 log.warn("l2LbIndex is not found in the meta of L2 hash objective. Abort");
1296 return;
1297 }
1298
1299 // Storage for all group keys in the chain of groups created
1300 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
1301 List<GroupInfo> unsentGroups = new ArrayList<>();
1302 createL2HashBuckets(nextObj, allGroupKeys, unsentGroups);
1303
1304 // Create L2 load balancing group
1305 List<GroupBucket> l2LbGroupBuckets = new ArrayList<>();
1306 for (GroupInfo gi : unsentGroups) {
1307 // create load balancing bucket to point to the outer group
1308 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
1309 ttb.group(new GroupId(gi.nextGroupDesc().givenGroupId()));
1310 GroupBucket sbucket = DefaultGroupBucket.createSelectGroupBucket(ttb.build());
1311 l2LbGroupBuckets.add(sbucket);
1312 }
1313
1314 int l2LbGroupId = L2_LB_TYPE | (TYPE_MASK & l2LbIndex);
1315 int l2lbgk = l2HashGroupKey(deviceId, l2LbIndex);
1316 GroupKey l2LbGroupKey = new DefaultGroupKey(appKryo.serialize(l2lbgk));
1317 GroupDescription l2LbGroupDesc =
1318 new DefaultGroupDescription(
1319 deviceId,
1320 SELECT,
1321 new GroupBuckets(l2LbGroupBuckets),
1322 l2LbGroupKey,
1323 l2LbGroupId,
1324 nextObj.appId());
1325 GroupChainElem l2LbGce = new GroupChainElem(l2LbGroupDesc, l2LbGroupBuckets.size(), false, deviceId);
1326
1327 // Create objects for local and distributed storage
1328 allGroupKeys.forEach(gKeyChain -> gKeyChain.addFirst(l2LbGroupKey));
1329 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
1330 // Store l2LbGroupKey with the ofdpaGroupChain for the nextObjective that depends on it
1331 updatePendingNextObjective(l2LbGroupKey, ofdpaGrp);
1332 log.debug("Trying L2-LB: device:{} gid:{} gkey:{} nextId:{}",
1333 deviceId, Integer.toHexString(l2LbGroupId), l2LbGroupKey, nextObj.id());
1334 // finally we are ready to send the innermost groups
1335 for (GroupInfo gi : unsentGroups) {
1336 log.debug("Sending innermost group {} in group chain on device {} ",
1337 Integer.toHexString(gi.innerMostGroupDesc().givenGroupId()), deviceId);
1338 updatePendingGroups(gi.nextGroupDesc().appCookie(), l2LbGce);
1339 groupService.addGroup(gi.innerMostGroupDesc());
1340 }
1341 }
1342
1343 /**
1344 * Create L2 hash group buckets.
1345 *
1346 * @param nextObj next objective
1347 */
1348 private void createL2HashBuckets(NextObjective nextObj,
1349 List<Deque<GroupKey>> allGroupKeys, List<GroupInfo> unsentGroups) {
1350 List<GroupInfo> groupInfos = prepareL2UnfilteredGroup(nextObj);
1351 groupInfos.forEach(groupInfo -> {
1352 // Update allGroupKeys
1353 Deque<GroupKey> gKeyChain = new ArrayDeque<>();
1354 gKeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
1355 allGroupKeys.add(gKeyChain);
1356
1357 // Update unsent group list
1358 unsentGroups.add(groupInfo);
1359 });
1360 }
1361
1362 /**
Pier Ventre42287df2016-11-09 14:17:26 -08001363 * Processes the pseudo wire related next objective.
1364 * This procedure try to reuse the mpls label groups,
1365 * the mpls interface group and the l2 interface group.
1366 *
1367 * @param nextObjective the objective to process.
1368 */
1369 protected void processPwNextObjective(NextObjective nextObjective) {
Saurav Das1547b3f2017-05-05 17:01:08 -07001370 log.warn("Pseudo wire extensions are not supported in OFDPA 2.0 {}",
1371 nextObjective.id());
Pier Ventre42287df2016-11-09 14:17:26 -08001372 }
1373
Saurav Das8be4e3a2016-03-11 17:19:07 -08001374 //////////////////////////////////////
1375 // Group Editing
1376 //////////////////////////////////////
Charles Chan188ebf52015-12-23 00:15:11 -08001377 /**
1378 * Adds a bucket to the top level group of a group-chain, and creates the chain.
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001379 * Ensures that bucket being added is not a duplicate, by checking existing
Yi Tsengef19de12017-04-24 11:33:05 -07001380 * buckets for the same output port.
Charles Chan188ebf52015-12-23 00:15:11 -08001381 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001382 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -08001383 * @param next the representation of the existing group-chain for this next objective
1384 */
1385 protected void addBucketToGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001386 if (nextObjective.type() != NextObjective.Type.HASHED &&
1387 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001388 log.warn("AddBuckets not applied to nextType:{} in dev:{} for next:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001389 nextObjective.type(), deviceId, nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -07001390 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001391 return;
1392 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001393 // first check to see if bucket being added is not a duplicate of an
Yi Tsengef19de12017-04-24 11:33:05 -07001394 // existing bucket. If it is for an existing output port, then its a
1395 // duplicate.
Yi Tseng78f51f42017-02-02 13:54:58 -08001396 Set<TrafficTreatment> duplicateBuckets = Sets.newHashSet();
Yi Tsengef19de12017-04-24 11:33:05 -07001397 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
1398 Set<PortNumber> existingPorts = getExistingOutputPorts(allActiveKeys,
1399 groupService,
1400 deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -08001401 Set<TrafficTreatment> nonDuplicateBuckets = Sets.newHashSet();
1402 NextObjective objectiveToAdd;
Yi Tseng78f51f42017-02-02 13:54:58 -08001403 nextObjective.next().forEach(trafficTreatment -> {
1404 PortNumber portNumber = readOutPortFromTreatment(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001405 if (portNumber == null) {
1406 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001407 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001408 if (existingPorts.contains(portNumber)) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001409 // its possible that portnumbers are same but labels are different
1410 int label = readLabelFromTreatment(trafficTreatment);
1411 if (label == -1) {
1412 duplicateBuckets.add(trafficTreatment);
1413 } else {
Saurav Dasceccf242017-08-03 18:30:35 -07001414 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1415 groupService, deviceId,
1416 portNumber, label);
1417 if (!existing.isEmpty()) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001418 duplicateBuckets.add(trafficTreatment);
1419 } else {
1420 nonDuplicateBuckets.add(trafficTreatment);
1421 }
1422 }
Yi Tseng47f82dc2017-03-05 22:48:39 -08001423 } else {
1424 nonDuplicateBuckets.add(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001425 }
1426 });
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001427 if (duplicateBuckets.isEmpty()) {
1428 // use the original objective
1429 objectiveToAdd = nextObjective;
1430 } else if (!nonDuplicateBuckets.isEmpty()) {
1431 // only use the non-duplicate buckets if there are any
1432 log.debug("Some buckets {} already exist in next id {}, duplicate "
1433 + "buckets will be ignored.", duplicateBuckets, nextObjective.id());
1434 // new next objective with non duplicate treatments
Yi Tseng47f82dc2017-03-05 22:48:39 -08001435 NextObjective.Builder builder = DefaultNextObjective.builder()
1436 .withType(nextObjective.type())
1437 .withId(nextObjective.id())
1438 .withMeta(nextObjective.meta())
1439 .fromApp(nextObjective.appId());
1440 nonDuplicateBuckets.forEach(builder::addTreatment);
Yi Tseng47f82dc2017-03-05 22:48:39 -08001441 ObjectiveContext context = nextObjective.context().orElse(null);
1442 objectiveToAdd = builder.addToExisting(context);
1443 } else {
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001444 // buckets to add are already there - nothing to do
Saurav Das1547b3f2017-05-05 17:01:08 -07001445 log.debug("buckets already exist {} in next: {} ..ignoring bucket add",
1446 duplicateBuckets, nextObjective.id());
1447 pass(nextObjective);
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001448 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001449 }
Saurav Das1a129a02016-11-18 15:21:57 -08001450 if (nextObjective.type() == NextObjective.Type.HASHED) {
piercd2a3ef2018-11-22 15:44:57 +01001451 if (isL2Hash(nextObjective)) {
1452 addBucketToL2HashGroup(objectiveToAdd, allActiveKeys);
1453 return;
1454 }
1455 addBucketToEcmpHashGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001456 } else if (nextObjective.type() == NextObjective.Type.BROADCAST) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001457 addBucketToBroadcastGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001458 }
1459 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001460
piercd2a3ef2018-11-22 15:44:57 +01001461 private void addBucketToL2HashGroup(NextObjective nextObjective,
1462 List<Deque<GroupKey>> allActiveKeys) {
1463 // storage for all group keys in the chain of groups created
1464 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
1465 List<GroupInfo> unsentGroups = new ArrayList<>();
1466 List<GroupBucket> newBuckets;
1467 // Prepare the l2 unfiltered groups
1468 createL2HashBuckets(nextObjective, allGroupKeys, unsentGroups);
1469 // now we can create the buckets to add to the outermost L2 hash group
1470 newBuckets = generateNextGroupBuckets(unsentGroups, SELECT);
1471 // retrieve the original l2 load balance group
1472 Group l2hashGroup = retrieveTopLevelGroup(allActiveKeys, deviceId,
1473 groupService, nextObjective.id());
1474 if (l2hashGroup == null) {
1475 fail(nextObjective, ObjectiveError.GROUPMISSING);
1476 return;
1477 }
1478 GroupKey l2hashGroupKey = l2hashGroup.appCookie();
1479 int l2hashGroupId = l2hashGroup.id().id();
1480 GroupDescription l2hashGroupDesc = new DefaultGroupDescription(deviceId,
1481 SELECT,
1482 new GroupBuckets(newBuckets),
1483 l2hashGroupKey,
1484 l2hashGroupId,
1485 nextObjective.appId());
1486 GroupChainElem l2hashGce = new GroupChainElem(l2hashGroupDesc,
1487 unsentGroups.size(),
1488 true,
1489 deviceId);
1490 // update new bucket-chains
1491 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
1492 for (Deque<GroupKey> newBucketChain : allGroupKeys) {
1493 newBucketChain.addFirst(l2hashGroupKey);
1494 addedKeys.add(newBucketChain);
1495 }
1496 updatePendingNextObjective(l2hashGroupKey,
1497 new OfdpaNextGroup(addedKeys, nextObjective));
1498 log.debug("Adding to L2HASH: device:{} gid:{} group key:{} nextId:{}",
1499 deviceId, Integer.toHexString(l2hashGroupId),
1500 l2hashGroupKey, nextObjective.id());
1501 unsentGroups.forEach(groupInfo -> {
1502 // send the innermost group
1503 log.debug("Sending innermost group {} in group chain on device {} ",
1504 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
1505 deviceId);
1506 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l2hashGce);
1507 groupService.addGroup(groupInfo.innerMostGroupDesc());
1508 });
1509 }
1510
1511 private void addBucketToEcmpHashGroup(NextObjective nextObjective,
Yi Tseng78f51f42017-02-02 13:54:58 -08001512 List<Deque<GroupKey>> allActiveKeys) {
Charles Chan188ebf52015-12-23 00:15:11 -08001513 // storage for all group keys in the chain of groups created
1514 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
1515 List<GroupInfo> unsentGroups = new ArrayList<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001516 List<GroupBucket> newBuckets;
Charles Chan367c1c12018-10-19 16:23:28 -07001517 createEcmpHashBucketChains(nextObjective, allGroupKeys, unsentGroups);
Yi Tseng78f51f42017-02-02 13:54:58 -08001518 // now we can create the buckets to add to the outermost L3 ECMP group
1519 newBuckets = generateNextGroupBuckets(unsentGroups, SELECT);
Saurav Das1a129a02016-11-18 15:21:57 -08001520 // retrieve the original L3 ECMP group
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001521 Group l3ecmpGroup = retrieveTopLevelGroup(allActiveKeys, deviceId,
1522 groupService, nextObjective.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001523 if (l3ecmpGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001524 fail(nextObjective, ObjectiveError.GROUPMISSING);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001525 return;
1526 }
Saurav Das1a129a02016-11-18 15:21:57 -08001527 GroupKey l3ecmpGroupKey = l3ecmpGroup.appCookie();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001528 int l3ecmpGroupId = l3ecmpGroup.id().id();
Charles Chan188ebf52015-12-23 00:15:11 -08001529 // Although GroupDescriptions are not necessary for adding buckets to
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001530 // existing groups, we still use one in the GroupChainElem. When the latter is
Charles Chan188ebf52015-12-23 00:15:11 -08001531 // processed, the info will be extracted for the bucketAdd call to groupService
1532 GroupDescription l3ecmpGroupDesc =
Yi Tsengef19de12017-04-24 11:33:05 -07001533 new DefaultGroupDescription(deviceId,
1534 SELECT,
1535 new GroupBuckets(newBuckets),
1536 l3ecmpGroupKey,
1537 l3ecmpGroupId,
1538 nextObjective.appId());
Yi Tseng78f51f42017-02-02 13:54:58 -08001539 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
1540 unsentGroups.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001541 true,
1542 deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001543
Saurav Dasc88d4662017-05-15 15:34:25 -07001544 // update new bucket-chains
1545 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
1546 for (Deque<GroupKey> newBucketChain : allGroupKeys) {
1547 newBucketChain.addFirst(l3ecmpGroupKey);
1548 addedKeys.add(newBucketChain);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001549 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001550 updatePendingNextObjective(l3ecmpGroupKey,
1551 new OfdpaNextGroup(addedKeys, nextObjective));
Yi Tsengef19de12017-04-24 11:33:05 -07001552 log.debug("Adding to L3ECMP: device:{} gid:{} group key:{} nextId:{}",
Charles Chan188ebf52015-12-23 00:15:11 -08001553 deviceId, Integer.toHexString(l3ecmpGroupId),
1554 l3ecmpGroupKey, nextObjective.id());
Yi Tseng78f51f42017-02-02 13:54:58 -08001555 unsentGroups.forEach(groupInfo -> {
1556 // send the innermost group
1557 log.debug("Sending innermost group {} in group chain on device {} ",
Saurav Dasc88d4662017-05-15 15:34:25 -07001558 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
1559 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001560 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3ecmpGce);
1561 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001562 });
Saurav Das1a129a02016-11-18 15:21:57 -08001563 }
Charles Chan188ebf52015-12-23 00:15:11 -08001564
Saurav Das1a129a02016-11-18 15:21:57 -08001565 private void addBucketToBroadcastGroup(NextObjective nextObj,
Yi Tsengef19de12017-04-24 11:33:05 -07001566 List<Deque<GroupKey>> allActiveKeys) {
1567 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001568 if (assignedVlan == null) {
1569 log.warn("VLAN ID required by broadcast next obj is missing. "
1570 + "Aborting add bucket to broadcast group for next:{} in dev:{}",
1571 nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001572 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001573 return;
1574 }
Saurav Das1a129a02016-11-18 15:21:57 -08001575 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Yi Tsengef19de12017-04-24 11:33:05 -07001576 IpPrefix ipDst = readIpDstFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001577 if (ipDst != null) {
1578 if (ipDst.isMulticast()) {
Yi Tsengef19de12017-04-24 11:33:05 -07001579 addBucketToL3MulticastGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001580 } else {
1581 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001582 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001583 }
1584 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001585 addBucketToL2FloodGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001586 }
1587 }
1588
1589 private void addBucketToL2FloodGroup(NextObjective nextObj,
1590 List<Deque<GroupKey>> allActiveKeys,
1591 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001592 VlanId assignedVlan) {
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001593 Group l2FloodGroup = retrieveTopLevelGroup(allActiveKeys, deviceId,
1594 groupService, nextObj.id());
Yi Tseng78f51f42017-02-02 13:54:58 -08001595
1596 if (l2FloodGroup == null) {
1597 log.warn("Can't find L2 flood group while adding bucket to it. NextObj = {}",
1598 nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001599 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001600 return;
1601 }
Saurav Das1a129a02016-11-18 15:21:57 -08001602
Yi Tseng78f51f42017-02-02 13:54:58 -08001603 GroupKey l2floodGroupKey = l2FloodGroup.appCookie();
1604 int l2floodGroupId = l2FloodGroup.id().id();
1605 List<GroupBucket> newBuckets = generateNextGroupBuckets(groupInfos, ALL);
Yi Tseng78f51f42017-02-02 13:54:58 -08001606 GroupDescription l2FloodGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001607 new DefaultGroupDescription(deviceId,
1608 ALL,
1609 new GroupBuckets(newBuckets),
1610 l2floodGroupKey,
1611 l2floodGroupId,
1612 nextObj.appId());
Yi Tseng78f51f42017-02-02 13:54:58 -08001613 GroupChainElem l2FloodGroupChainElement =
1614 new GroupChainElem(l2FloodGroupDescription,
1615 groupInfos.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001616 true,
1617 deviceId);
Yi Tseng78f51f42017-02-02 13:54:58 -08001618
Yi Tseng78f51f42017-02-02 13:54:58 -08001619
1620 //ensure assignedVlan applies to the chosen group
1621 VlanId floodGroupVlan = extractVlanIdFromGroupId(l2floodGroupId);
Yi Tseng78f51f42017-02-02 13:54:58 -08001622 if (!floodGroupVlan.equals(assignedVlan)) {
1623 log.warn("VLAN ID {} does not match Flood group {} to which bucket is "
1624 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1625 Integer.toHexString(l2floodGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001626 fail(nextObj, ObjectiveError.BADPARAMS);
Yi Tseng78f51f42017-02-02 13:54:58 -08001627 return;
1628 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001629 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
Yi Tseng78f51f42017-02-02 13:54:58 -08001630 groupInfos.forEach(groupInfo -> {
1631 // update original NextGroup with new bucket-chain
Yi Tseng78f51f42017-02-02 13:54:58 -08001632 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001633 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001634 newBucketChain.addFirst(l2floodGroupKey);
Saurav Dasc88d4662017-05-15 15:34:25 -07001635 addedKeys.add(newBucketChain);
Yi Tsengef19de12017-04-24 11:33:05 -07001636 log.debug("Adding to L2FLOOD: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001637 deviceId, Integer.toHexString(l2floodGroupId),
1638 l2floodGroupKey, nextObj.id());
1639 // send the innermost group
1640 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001641 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001642 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001643 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l2FloodGroupChainElement);
Yi Tseng78f51f42017-02-02 13:54:58 -08001644
Yi Tsengef19de12017-04-24 11:33:05 -07001645 DeviceId innerMostGroupDevice = groupInfo.innerMostGroupDesc().deviceId();
1646 GroupKey innerMostGroupKey = groupInfo.innerMostGroupDesc().appCookie();
Yi Tseng78f51f42017-02-02 13:54:58 -08001647 Group existsL2IGroup = groupService.getGroup(innerMostGroupDevice, innerMostGroupKey);
1648
1649 if (existsL2IGroup != null) {
1650 // group already exist
1651 processPendingAddGroupsOrNextObjs(innerMostGroupKey, true);
1652 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001653 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001654 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001655 });
Saurav Dasc88d4662017-05-15 15:34:25 -07001656
1657 updatePendingNextObjective(l2floodGroupKey,
1658 new OfdpaNextGroup(addedKeys, nextObj));
Yi Tseng78f51f42017-02-02 13:54:58 -08001659 }
1660
Saurav Das1a129a02016-11-18 15:21:57 -08001661 private void addBucketToL3MulticastGroup(NextObjective nextObj,
1662 List<Deque<GroupKey>> allActiveKeys,
1663 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001664 VlanId assignedVlan) {
Pier Luigi21fffd22018-01-19 10:24:53 +01001665 // Create the buckets to add to the outermost L3 Multicast group
1666 List<GroupBucket> newBuckets = createL3MulticastBucket(groupInfos);
Saurav Das1a129a02016-11-18 15:21:57 -08001667
1668 // get the group being edited
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001669 Group l3mcastGroup = retrieveTopLevelGroup(allActiveKeys, deviceId,
1670 groupService, nextObj.id());
Saurav Das1a129a02016-11-18 15:21:57 -08001671 if (l3mcastGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001672 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001673 return;
1674 }
1675 GroupKey l3mcastGroupKey = l3mcastGroup.appCookie();
1676 int l3mcastGroupId = l3mcastGroup.id().id();
1677
1678 //ensure assignedVlan applies to the chosen group
Yi Tseng78f51f42017-02-02 13:54:58 -08001679 VlanId expectedVlan = extractVlanIdFromGroupId(l3mcastGroupId);
Saurav Das1a129a02016-11-18 15:21:57 -08001680 if (!expectedVlan.equals(assignedVlan)) {
1681 log.warn("VLAN ID {} does not match L3 Mcast group {} to which bucket is "
1682 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1683 Integer.toHexString(l3mcastGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001684 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001685 }
1686 GroupDescription l3mcastGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001687 new DefaultGroupDescription(deviceId,
1688 ALL,
1689 new GroupBuckets(newBuckets),
1690 l3mcastGroupKey,
1691 l3mcastGroupId,
1692 nextObj.appId());
Saurav Das1a129a02016-11-18 15:21:57 -08001693 GroupChainElem l3mcastGce = new GroupChainElem(l3mcastGroupDescription,
Yi Tsengef19de12017-04-24 11:33:05 -07001694 groupInfos.size(),
1695 true,
1696 deviceId);
Saurav Dasc88d4662017-05-15 15:34:25 -07001697
1698 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
Yi Tseng78f51f42017-02-02 13:54:58 -08001699 groupInfos.forEach(groupInfo -> {
1700 // update original NextGroup with new bucket-chain
1701 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001702 newBucketChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001703 // Add L3 interface group to the chain if there is one.
Yi Tsengef19de12017-04-24 11:33:05 -07001704 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1705 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001706 }
1707 newBucketChain.addFirst(l3mcastGroupKey);
Saurav Dasc88d4662017-05-15 15:34:25 -07001708 addedKeys.add(newBucketChain);
Yi Tseng78f51f42017-02-02 13:54:58 -08001709
Yi Tsengef19de12017-04-24 11:33:05 -07001710 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3mcastGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001711 // Point next group to inner-most group, if any
Yi Tsengef19de12017-04-24 11:33:05 -07001712 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1713 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(),
1714 1,
1715 false,
1716 deviceId);
1717 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001718 }
Yi Tsengef19de12017-04-24 11:33:05 -07001719 log.debug("Adding to L3MCAST: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001720 deviceId, Integer.toHexString(l3mcastGroupId),
1721 l3mcastGroupKey, nextObj.id());
1722 // send the innermost group
1723 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001724 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001725 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001726 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001727
1728 });
1729
Saurav Das1a129a02016-11-18 15:21:57 -08001730 updatePendingNextObjective(l3mcastGroupKey,
Saurav Dasc88d4662017-05-15 15:34:25 -07001731 new OfdpaNextGroup(addedKeys, nextObj));
Charles Chan188ebf52015-12-23 00:15:11 -08001732 }
1733
1734 /**
Saurav Dasceccf242017-08-03 18:30:35 -07001735 * Removes buckets in the top level group of a possible group-chain. Does
Saurav Das1a129a02016-11-18 15:21:57 -08001736 * not remove the groups in the group-chain pointed to by this bucket, as they
Charles Chan188ebf52015-12-23 00:15:11 -08001737 * may be in use (referenced by other groups) elsewhere.
1738 *
Saurav Dasceccf242017-08-03 18:30:35 -07001739 * @param nextObjective a next objective that contains information for the
1740 * buckets to be removed from the group
1741 * @param next the representation of the existing group-chains for this next
1742 * objective, from which the top-level buckets to remove are determined
Charles Chan188ebf52015-12-23 00:15:11 -08001743 */
1744 protected void removeBucketFromGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001745 if (nextObjective.type() != NextObjective.Type.HASHED &&
1746 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001747 log.warn("RemoveBuckets not applied to nextType:{} in dev:{} for next:{}",
1748 nextObjective.type(), deviceId, nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -07001749 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001750 return;
1751 }
Yi Tsengef19de12017-04-24 11:33:05 -07001752 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Saurav Dasceccf242017-08-03 18:30:35 -07001753 List<Integer> indicesToRemove = Lists.newArrayList();
1754 for (TrafficTreatment treatment : nextObjective.next()) {
1755 // find the top-level bucket in the group-chain by matching the
1756 // outport and label from different groups in the chain
1757 PortNumber portToRemove = readOutPortFromTreatment(treatment);
1758 int labelToRemove = readLabelFromTreatment(treatment);
1759 if (portToRemove == null) {
1760 log.warn("treatment {} of next objective {} has no outport.. "
1761 + "cannot remove bucket from group in dev: {}", treatment,
1762 nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001763 continue;
1764 }
Saurav Dasceccf242017-08-03 18:30:35 -07001765 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1766 groupService, deviceId,
1767 portToRemove, labelToRemove);
1768 indicesToRemove.addAll(existing);
1769
Charles Chan188ebf52015-12-23 00:15:11 -08001770 }
Saurav Dasceccf242017-08-03 18:30:35 -07001771
1772 List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList();
1773 indicesToRemove.forEach(index -> chainsToRemove
1774 .add(allActiveKeys.get(index)));
Yi Tseng78f51f42017-02-02 13:54:58 -08001775 if (chainsToRemove.isEmpty()) {
Charles Chan188ebf52015-12-23 00:15:11 -08001776 log.warn("Could not find appropriate group-chain for removing bucket"
1777 + " for next id {} in dev:{}", nextObjective.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001778 fail(nextObjective, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001779 return;
Charles Chan188ebf52015-12-23 00:15:11 -08001780 }
Saurav Dasceccf242017-08-03 18:30:35 -07001781 removeBucket(chainsToRemove, nextObjective);
1782 }
1783
1784 /**
1785 * Removes top-level buckets from a group that represents the given next objective.
1786 *
1787 * @param chainsToRemove a list of group bucket chains to remove
1788 * @param nextObjective the next objective that contains information for the
1789 * buckets to be removed from the group
1790 */
1791 protected void removeBucket(List<Deque<GroupKey>> chainsToRemove,
1792 NextObjective nextObjective) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001793 List<GroupBucket> bucketsToRemove = Lists.newArrayList();
1794 //first group key is the one we want to modify
1795 GroupKey modGroupKey = chainsToRemove.get(0).peekFirst();
Saurav Das1a129a02016-11-18 15:21:57 -08001796 Group modGroup = groupService.getGroup(deviceId, modGroupKey);
Ray Milkey43869812018-05-04 13:00:39 -07001797 if (modGroup == null) {
1798 log.warn("removeBucket(): Attempt to modify non-existent group {} for device {}", modGroupKey, deviceId);
1799 return;
1800 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001801 for (Deque<GroupKey> foundChain : chainsToRemove) {
1802 //second group key is the one we wish to remove the reference to
1803 if (foundChain.size() < 2) {
Saurav Dasceccf242017-08-03 18:30:35 -07001804 // additional check to make sure second group key exists in
Yi Tseng78f51f42017-02-02 13:54:58 -08001805 // the chain.
1806 log.warn("Can't find second group key from chain {}",
1807 foundChain);
1808 continue;
Saurav Das1a129a02016-11-18 15:21:57 -08001809 }
Saurav Dasceccf242017-08-03 18:30:35 -07001810 GroupKey pointedGroupKey = foundChain.stream()
1811 .collect(Collectors.toList()).get(1);
Yi Tseng78f51f42017-02-02 13:54:58 -08001812 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
Yi Tseng78f51f42017-02-02 13:54:58 -08001813 if (pointedGroup == null) {
1814 continue;
1815 }
1816
1817 GroupBucket bucket;
1818 if (nextObjective.type() == NextObjective.Type.HASHED) {
1819 bucket = DefaultGroupBucket.createSelectGroupBucket(
1820 DefaultTrafficTreatment.builder()
1821 .group(pointedGroup.id())
1822 .build());
1823 } else {
1824 bucket = DefaultGroupBucket.createAllGroupBucket(
1825 DefaultTrafficTreatment.builder()
1826 .group(pointedGroup.id())
1827 .build());
1828 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001829 bucketsToRemove.add(bucket);
Saurav Das1a129a02016-11-18 15:21:57 -08001830 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001831
1832 GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove);
1833 List<String> pointedGroupIds; // for debug log
1834 pointedGroupIds = bucketsToRemove.stream()
1835 .map(GroupBucket::treatment)
1836 .map(TrafficTreatment::allInstructions)
1837 .flatMap(List::stream)
1838 .filter(inst -> inst instanceof Instructions.GroupInstruction)
1839 .map(inst -> (Instructions.GroupInstruction) inst)
1840 .map(Instructions.GroupInstruction::groupId)
1841 .map(GroupId::id)
1842 .map(Integer::toHexString)
1843 .map(id -> HEX_PREFIX + id)
1844 .collect(Collectors.toList());
1845
Yi Tseng78f51f42017-02-02 13:54:58 -08001846 log.debug("Removing buckets from group id 0x{} pointing to group id(s) {} "
Ray Milkey43869812018-05-04 13:00:39 -07001847 + "for next id {} in device {}",
1848 Integer.toHexString(modGroup.id().id()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001849 pointedGroupIds, nextObjective.id(), deviceId);
1850 addPendingUpdateNextObjective(modGroupKey, nextObjective);
Saurav Das1a129a02016-11-18 15:21:57 -08001851 groupService.removeBucketsFromGroup(deviceId, modGroupKey,
1852 removeBuckets, modGroupKey,
1853 nextObjective.appId());
Saurav Dasceccf242017-08-03 18:30:35 -07001854 // update store - synchronize access as there may be multiple threads
1855 // trying to remove buckets from the same group, each with its own
1856 // potentially stale copy of allActiveKeys
Saurav Dasc88d4662017-05-15 15:34:25 -07001857 synchronized (flowObjectiveStore) {
Saurav Dasceccf242017-08-03 18:30:35 -07001858 // get a fresh copy of what the store holds
1859 NextGroup next = flowObjectiveStore.getNextGroup(nextObjective.id());
1860 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Pier Luigid008dbe2018-01-15 09:48:49 +01001861 allActiveKeys = Lists.newArrayList(allActiveKeys);
Saurav Dasc88d4662017-05-15 15:34:25 -07001862 // Note that since we got a new object, and ArrayDeque does not implement
Saurav Dasceccf242017-08-03 18:30:35 -07001863 // Object.equals(), we have to check the deque elems one by one
1864 allActiveKeys
1865 .removeIf(active ->
1866 chainsToRemove.stream().anyMatch(remove ->
1867 Arrays.equals(remove.toArray(new GroupKey[0]),
1868 active.toArray(new GroupKey[0]))));
Saurav Dasc88d4662017-05-15 15:34:25 -07001869 // If no buckets in the group, then retain an entry for the
1870 // top level group which still exists.
1871 if (allActiveKeys.isEmpty()) {
1872 ArrayDeque<GroupKey> top = new ArrayDeque<>();
1873 top.add(modGroupKey);
1874 allActiveKeys.add(top);
1875 }
1876 flowObjectiveStore.putNextGroup(nextObjective.id(),
Saurav Dasceccf242017-08-03 18:30:35 -07001877 new OfdpaNextGroup(allActiveKeys,
1878 nextObjective));
Saurav Das1a129a02016-11-18 15:21:57 -08001879 }
Charles Chan188ebf52015-12-23 00:15:11 -08001880 }
1881
1882 /**
Saurav Das961beb22017-03-29 19:09:17 -07001883 * Removes all groups in multiple possible group-chains that represent the next-obj.
Charles Chan188ebf52015-12-23 00:15:11 -08001884 *
1885 * @param nextObjective the next objective to remove
1886 * @param next the NextGroup that represents the existing group-chain for
1887 * this next objective
1888 */
1889 protected void removeGroup(NextObjective nextObjective, NextGroup next) {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -07001890
Yi Tsengef19de12017-04-24 11:33:05 -07001891 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Charles Chanfc5c7802016-05-17 13:13:55 -07001892
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001893 List<GroupKey> groupKeys = allActiveKeys.stream()
Charles Chanfc5c7802016-05-17 13:13:55 -07001894 .map(Deque::getFirst).collect(Collectors.toList());
Yi Tsengef19de12017-04-24 11:33:05 -07001895 addPendingRemoveNextObjective(nextObjective, groupKeys);
Charles Chanfc5c7802016-05-17 13:13:55 -07001896
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -07001897 allActiveKeys
1898 .forEach(groupChain -> groupChain.forEach(groupKey -> groupService
1899 .removeGroup(deviceId, groupKey, nextObjective.appId())));
Charles Chan188ebf52015-12-23 00:15:11 -08001900 flowObjectiveStore.removeNextGroup(nextObjective.id());
1901 }
1902
Saurav Dasceccf242017-08-03 18:30:35 -07001903 /**
Ruchi Sahotaef0761c2019-01-28 01:08:18 +00001904 * modifies group with next objective.
1905 *
1906 * @param nextObjective the NextObjective
1907 * @param nextGroup the NextGroup
1908 */
1909 protected void modifyBucketFromGroup(NextObjective nextObjective, NextGroup nextGroup) {
1910 switch (nextObjective.type()) {
1911 case SIMPLE:
1912 Collection<TrafficTreatment> treatments = nextObjective.next();
1913 if (treatments.size() != 1) {
1914 log.error("Next Objectives of type Simple should only have a "
1915 + "single Traffic Treatment. Next Objective Id:{}",
1916 nextObjective.id());
1917 fail(nextObjective, ObjectiveError.BADPARAMS);
1918 return;
1919 }
1920 modifySimpleNextObjective(nextObjective, nextGroup);
1921 break;
1922 default:
1923 fail(nextObjective, ObjectiveError.UNKNOWN);
1924 log.warn("Unknown next objective type {}", nextObjective.type());
1925 }
1926 }
1927
1928 /**
1929 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
1930 * a chain of groups. The simple Next Objective passed in by the application
1931 * is broken up into a group chain. The following chains can be modified
1932 * depending on the parameters in the Next Objective.
1933 * 1. L2 Interface group (no chaining)
1934 * 2. L3 Unicast group -> L2 Interface group
1935 *
1936 * @param nextObj the nextObjective of type SIMPLE
1937 */
1938 private void modifySimpleNextObjective(NextObjective nextObj, NextGroup nextGroup) {
1939 TrafficTreatment treatment = nextObj.next().iterator().next();
1940 // determine if plain L2 or L3->L2 chain
1941 boolean plainL2 = true;
1942 for (Instruction ins : treatment.allInstructions()) {
1943 if (ins.type() == Instruction.Type.L2MODIFICATION) {
1944 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
1945 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_DST ||
1946 l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_SRC ||
1947 l2ins.subtype() == L2ModificationInstruction.L2SubType.VLAN_ID) {
1948 plainL2 = false;
1949 }
1950 }
1951 }
1952 if (plainL2) {
1953 modifyBucketInL2Group(nextObj, nextGroup);
1954 } else {
1955 modifyBucketInL3Group(nextObj, nextGroup);
1956 }
1957 return;
1958 }
1959
1960
1961 /**
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001962 * Modify buckets in the L2 interface group.
1963 *
1964 * @param nextObjective a next objective that contains information for the
1965 * buckets to be modified in the group
1966 * @param next the representation of the existing group-chains for this next
1967 * objective, from which the innermost group buckets to remove are determined
1968 */
Ruchi Sahotaef0761c2019-01-28 01:08:18 +00001969
1970 protected void modifyBucketInL2Group(NextObjective nextObjective, NextGroup next) {
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001971
1972 VlanId assignedVlan = readVlanFromSelector(nextObjective.meta());
1973 if (assignedVlan == null) {
1974 log.warn("VLAN ID required by simple next obj is missing. Abort.");
1975 fail(nextObjective, ObjectiveError.BADPARAMS);
1976 return;
1977 }
1978
1979 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObjective, assignedVlan);
1980
1981 // There is only one L2 interface group in this case
1982 GroupDescription l2InterfaceGroupDesc = groupInfos.get(0).innerMostGroupDesc();
1983
1984 // Replace group bucket for L2 interface group
1985 groupService.setBucketsForGroup(deviceId,
1986 l2InterfaceGroupDesc.appCookie(),
1987 l2InterfaceGroupDesc.buckets(),
1988 l2InterfaceGroupDesc.appCookie(),
1989 l2InterfaceGroupDesc.appId());
1990
1991 // update store - synchronize access as there may be multiple threads
1992 // trying to remove buckets from the same group, each with its own
1993 // potentially stale copy of allActiveKeys
1994 synchronized (flowObjectiveStore) {
1995 List<Deque<GroupKey>> modifiedGroupKeys = Lists.newArrayList();
1996 ArrayDeque<GroupKey> top = new ArrayDeque<>();
1997 top.add(l2InterfaceGroupDesc.appCookie());
1998 modifiedGroupKeys.add(top);
1999
2000 flowObjectiveStore.putNextGroup(nextObjective.id(),
2001 new OfdpaNextGroup(modifiedGroupKeys,
2002 nextObjective));
2003 }
Ruchi Sahotaef0761c2019-01-28 01:08:18 +00002004
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002005 }
2006
Ruchi Sahotaef0761c2019-01-28 01:08:18 +00002007 protected void modifyBucketInL3Group(NextObjective nextObjective, NextGroup next) {
2008
2009 //get l3 group
2010 GroupInfo groupInfo = prepareL3UnicastGroup(nextObjective, next);
2011 if (groupInfo == null) {
2012 log.warn("Null groupInfo retrieved for next obj. Abort.");
2013 fail(nextObjective, ObjectiveError.BADPARAMS);
2014 return;
2015 }
2016
2017 GroupDescription l3UnicastGroupDesc = groupInfo.nextGroupDesc();
2018
2019 // Replace group bucket for L3 UC interface group
2020 groupService.setBucketsForGroup(deviceId, l3UnicastGroupDesc.appCookie(),
2021 l3UnicastGroupDesc.buckets(), l3UnicastGroupDesc.appCookie(),
2022 l3UnicastGroupDesc.appId());
2023
2024 // create object for local and distributed storage
2025 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
2026 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
2027 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
2028 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
2029 allGroupKeys.add(gkeyChain);
2030 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObjective);
2031 // store l3groupkey with the ofdpaNextGroup for the nextObjective that depends on it
2032 updatePendingNextObjective(groupInfo.nextGroupDesc().appCookie(), ofdpaGrp);
2033
2034 // update store - synchronize access as there may be multiple threads
2035 // trying to update bucket from the same group, each with its own
2036 // potentially stale copy of allActiveKeys
2037 synchronized (flowObjectiveStore) {
2038
2039 List<Deque<GroupKey>> modifiedGroupKeys = Lists.newArrayList();
2040 ArrayDeque<GroupKey> top = new ArrayDeque<>();
2041 top.add(l3UnicastGroupDesc.appCookie());
2042 top.add(groupInfo.innerMostGroupDesc().appCookie()); //l2 group key
2043 modifiedGroupKeys.add(top);
2044
2045 flowObjectiveStore.putNextGroup(nextObjective.id(),
2046 new OfdpaNextGroup(modifiedGroupKeys,
2047 nextObjective));
2048 }
2049 }
2050
2051
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002052 /**
Saurav Dasceccf242017-08-03 18:30:35 -07002053 * Checks existing buckets in {@link NextGroup} to verify if they match
2054 * the buckets in the given {@link NextObjective}. Adds or removes buckets
2055 * to ensure that the buckets match up.
2056 *
2057 * @param nextObjective the next objective to verify
2058 * @param next the representation of the existing group which has to be
2059 * modified to match the given next objective
2060 */
2061 protected void verifyGroup(NextObjective nextObjective, NextGroup next) {
Pier Luigib72201b2018-01-25 16:16:02 +01002062 if (nextObjective.type() == NextObjective.Type.SIMPLE) {
2063 log.warn("verification not supported for indirect group");
Saurav Dasceccf242017-08-03 18:30:35 -07002064 fail(nextObjective, ObjectiveError.UNSUPPORTED);
2065 return;
2066 }
Charles Chan1dd75572018-05-08 11:49:05 -07002067 log.trace("Call to verify device:{} nextId:{}", deviceId, nextObjective.id());
Saurav Dasceccf242017-08-03 18:30:35 -07002068 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
2069 List<TrafficTreatment> bucketsToCreate = Lists.newArrayList();
2070 List<Integer> indicesToRemove = Lists.newArrayList();
Pier Luigiec6ac422018-01-29 10:30:59 +01002071
2072 // Iterating over the treatments of the next objective allows
2073 // to detect missing buckets and/or duplicate buckets (to be removed)
Saurav Dasceccf242017-08-03 18:30:35 -07002074 for (TrafficTreatment bkt : nextObjective.next()) {
2075 PortNumber portNumber = readOutPortFromTreatment(bkt);
2076 int label = readLabelFromTreatment(bkt);
2077 if (portNumber == null) {
2078 log.warn("treatment {} of next objective {} has no outport.. "
Ravi Dewanganf59ca9e2019-05-28 22:24:18 +00002079 + "cannot remove bucket from group in dev: {}", bkt, nextObjective.id(), deviceId);
Saurav Dasceccf242017-08-03 18:30:35 -07002080 fail(nextObjective, ObjectiveError.BADPARAMS);
2081 return;
2082 }
Ravi Dewanganf59ca9e2019-05-28 22:24:18 +00002083 List<Integer> existing = existingPortAndLabel(allActiveKeys, groupService, deviceId, portNumber, label);
Saurav Dasceccf242017-08-03 18:30:35 -07002084 if (existing.isEmpty()) {
2085 // if it doesn't exist, mark this bucket for creation
2086 bucketsToCreate.add(bkt);
2087 }
2088 if (existing.size() > 1) {
2089 // if it exists but there are duplicates, mark the others for removal
2090 existing.remove(0);
2091 indicesToRemove.addAll(existing);
2092 }
2093 }
2094
Pier Luigiec6ac422018-01-29 10:30:59 +01002095 // Detect situation where the next data has more buckets
2096 // (not duplicates) respect to the next objective
Saurav Dasa4020382018-02-14 14:14:54 -08002097 if (allActiveKeys.size() > nextObjective.next().size() &&
2098 // ignore specific case of empty group
2099 !(nextObjective.next().size() == 0 && allActiveKeys.size() == 1
2100 && allActiveKeys.get(0).size() == 1)) {
Pier Luigiec6ac422018-01-29 10:30:59 +01002101 log.warn("Mismatch detected between next and flowobjstore for device {}: " +
Saurav Dasa4020382018-02-14 14:14:54 -08002102 "nextId:{}, nextObjective-size:{} next-size:{} .. correcting",
2103 deviceId, nextObjective.id(), nextObjective.next().size(),
2104 allActiveKeys.size());
2105 List<Integer> otherIndices =
2106 indicesToRemoveFromNextGroup(allActiveKeys, nextObjective,
2107 groupService, deviceId);
Pier Luigiec6ac422018-01-29 10:30:59 +01002108 // Filter out the indices not present
2109 otherIndices = otherIndices.stream()
2110 .filter(index -> !indicesToRemove.contains(index))
2111 .collect(Collectors.toList());
2112 // Add all to the final list
2113 indicesToRemove.addAll(otherIndices);
2114 }
2115
Charles Chan1dd75572018-05-08 11:49:05 -07002116 log.trace("Buckets to create {}", bucketsToCreate);
2117 log.trace("Indices to remove {}", indicesToRemove);
Pier Luigib72201b2018-01-25 16:16:02 +01002118
Saurav Dasceccf242017-08-03 18:30:35 -07002119 if (!bucketsToCreate.isEmpty()) {
2120 log.info("creating {} buckets as part of nextId: {} verification",
2121 bucketsToCreate.size(), nextObjective.id());
2122 //create a nextObjective only with these buckets
2123 NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder()
2124 .withId(nextObjective.id())
Pier Luigib72201b2018-01-25 16:16:02 +01002125 .withType(nextObjective.type())
Saurav Dasceccf242017-08-03 18:30:35 -07002126 .withMeta(nextObjective.meta())
2127 .fromApp(nextObjective.appId());
Pier Luigib72201b2018-01-25 16:16:02 +01002128 bucketsToCreate.forEach(nextObjBuilder::addTreatment);
2129 // According to the next type we call the proper add function
2130 if (nextObjective.type() == NextObjective.Type.HASHED) {
piercd2a3ef2018-11-22 15:44:57 +01002131 if (isL2Hash(nextObjective)) {
2132 addBucketToL2HashGroup(nextObjBuilder.addToExisting(), allActiveKeys);
2133 } else {
2134 addBucketToEcmpHashGroup(nextObjBuilder.addToExisting(), allActiveKeys);
2135 }
Pier Luigib72201b2018-01-25 16:16:02 +01002136 } else {
2137 addBucketToBroadcastGroup(nextObjBuilder.addToExisting(), allActiveKeys);
2138 }
Saurav Dasceccf242017-08-03 18:30:35 -07002139 }
2140
2141 if (!indicesToRemove.isEmpty()) {
2142 log.info("removing {} buckets as part of nextId: {} verification",
2143 indicesToRemove.size(), nextObjective.id());
2144 List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList();
2145 indicesToRemove.forEach(index -> chainsToRemove
2146 .add(allActiveKeys.get(index)));
2147 removeBucket(chainsToRemove, nextObjective);
2148 }
2149
Saurav Das9df5b7c2017-08-14 16:44:43 -07002150 if (bucketsToCreate.isEmpty() && indicesToRemove.isEmpty()) {
2151 // flowObjective store record is in-sync with nextObjective passed-in
2152 // Nevertheless groupStore may not be in sync due to bug in the store
2153 // - see CORD-1844. XXX When this bug is fixed, the rest of this verify
2154 // method will not be required.
Pier Luigib72201b2018-01-25 16:16:02 +01002155 GroupKey topGroupKey = allActiveKeys.get(0).peekFirst();
2156 Group topGroup = groupService.getGroup(deviceId, topGroupKey);
Ravi Dewanganf59ca9e2019-05-28 22:24:18 +00002157 // topGroup should not be null - adding guard
2158 if (topGroup == null) {
2159 log.warn("topGroup {} not found in GroupStore device:{}, nextId:{}",
2160 topGroupKey, deviceId, nextObjective.id());
pier94a5d1772019-12-10 18:09:00 +01002161 fail(nextObjective, ObjectiveError.GROUPMISSING);
Ravi Dewanganf59ca9e2019-05-28 22:24:18 +00002162 return;
2163 }
Pier Luigib72201b2018-01-25 16:16:02 +01002164 int actualGroupSize = topGroup.buckets().buckets().size();
Saurav Das9df5b7c2017-08-14 16:44:43 -07002165 int objGroupSize = nextObjective.next().size();
2166 if (actualGroupSize != objGroupSize) {
2167 log.warn("Mismatch detected in device:{}, nextId:{}, nextObjective-size"
2168 + ":{} group-size:{} .. correcting", deviceId, nextObjective.id(),
2169 objGroupSize, actualGroupSize);
2170 }
2171 if (actualGroupSize > objGroupSize) {
Pier Luigib72201b2018-01-25 16:16:02 +01002172 // Group in the device has more chains
Saurav Das9df5b7c2017-08-14 16:44:43 -07002173 List<GroupBucket> bucketsToRemove = Lists.newArrayList();
2174 //check every bucket in the actual group
Pier Luigib72201b2018-01-25 16:16:02 +01002175 for (GroupBucket bucket : topGroup.buckets().buckets()) {
Saurav Das9df5b7c2017-08-14 16:44:43 -07002176 GroupInstruction g = (GroupInstruction) bucket.treatment()
2177 .allInstructions().iterator().next();
2178 GroupId gidToCheck = g.groupId(); // the group pointed to
2179 boolean matches = false;
2180 for (Deque<GroupKey> validChain : allActiveKeys) {
2181 if (validChain.size() < 2) {
2182 continue;
2183 }
2184 GroupKey pointedGroupKey = validChain.stream()
2185 .collect(Collectors.toList()).get(1);
2186 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
2187 if (pointedGroup != null && gidToCheck.equals(pointedGroup.id())) {
2188 matches = true;
2189 break;
2190 }
2191 }
2192 if (!matches) {
2193 log.warn("Removing bucket pointing to groupId:{}", gidToCheck);
2194 bucketsToRemove.add(bucket);
2195 }
2196 }
2197 // remove buckets for which there was no record in the obj store
2198 if (bucketsToRemove.isEmpty()) {
2199 log.warn("Mismatch detected but could not determine which"
2200 + "buckets to remove");
2201 } else {
2202 GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove);
Pier Luigib72201b2018-01-25 16:16:02 +01002203 groupService.removeBucketsFromGroup(deviceId, topGroupKey,
2204 removeBuckets, topGroupKey,
Saurav Das9df5b7c2017-08-14 16:44:43 -07002205 nextObjective.appId());
2206 }
2207 } else if (actualGroupSize < objGroupSize) {
Pier Luigib72201b2018-01-25 16:16:02 +01002208 // Group in the device has less chains
Saurav Das9df5b7c2017-08-14 16:44:43 -07002209 // should also add buckets not in group-store but in obj-store
2210 List<GroupBucket> bucketsToAdd = Lists.newArrayList();
2211 //check every bucket in the obj
2212 for (Deque<GroupKey> validChain : allActiveKeys) {
2213 if (validChain.size() < 2) {
2214 continue;
2215 }
2216 GroupKey pointedGroupKey = validChain.stream()
2217 .collect(Collectors.toList()).get(1);
2218 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
2219 if (pointedGroup == null) {
2220 // group should exist, otherwise cannot be added as bucket
2221 continue;
2222 }
2223 boolean matches = false;
Pier Luigib72201b2018-01-25 16:16:02 +01002224 for (GroupBucket bucket : topGroup.buckets().buckets()) {
Saurav Das9df5b7c2017-08-14 16:44:43 -07002225 GroupInstruction g = (GroupInstruction) bucket.treatment()
2226 .allInstructions().iterator().next();
2227 GroupId gidToCheck = g.groupId(); // the group pointed to
2228 if (pointedGroup.id().equals(gidToCheck)) {
2229 matches = true;
2230 break;
2231 }
2232 }
2233 if (!matches) {
2234 log.warn("Adding bucket pointing to groupId:{}", pointedGroup);
2235 TrafficTreatment t = DefaultTrafficTreatment.builder()
2236 .group(pointedGroup.id())
2237 .build();
Pier Luigib72201b2018-01-25 16:16:02 +01002238 // Create the proper bucket according to the next type
2239 if (nextObjective.type() == NextObjective.Type.HASHED) {
2240 bucketsToAdd.add(DefaultGroupBucket.createSelectGroupBucket(t));
2241 } else {
2242 bucketsToAdd.add(DefaultGroupBucket.createAllGroupBucket(t));
2243 }
Saurav Das9df5b7c2017-08-14 16:44:43 -07002244 }
2245 }
2246 if (bucketsToAdd.isEmpty()) {
2247 log.warn("Mismatch detected but could not determine which "
2248 + "buckets to add");
2249 } else {
2250 GroupBuckets addBuckets = new GroupBuckets(bucketsToAdd);
Pier Luigib72201b2018-01-25 16:16:02 +01002251 groupService.addBucketsToGroup(deviceId, topGroupKey,
2252 addBuckets, topGroupKey,
Saurav Das9df5b7c2017-08-14 16:44:43 -07002253 nextObjective.appId());
2254 }
2255 }
2256 }
2257
Saurav Dasceccf242017-08-03 18:30:35 -07002258 pass(nextObjective);
2259 }
2260
2261 //////////////////////////////////////
2262 // Helper methods and classes
2263 //////////////////////////////////////
2264
Yi Tsengef19de12017-04-24 11:33:05 -07002265 protected void updatePendingNextObjective(GroupKey groupKey, OfdpaNextGroup nextGrp) {
2266 pendingAddNextObjectives.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07002267 if (val == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07002268 val = new CopyOnWriteArrayList<>();
Saurav Das961beb22017-03-29 19:09:17 -07002269 }
Yi Tsengef19de12017-04-24 11:33:05 -07002270 val.add(nextGrp);
Saurav Das961beb22017-03-29 19:09:17 -07002271 return val;
2272 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08002273 }
2274
Yi Tsengef19de12017-04-24 11:33:05 -07002275 protected void updatePendingGroups(GroupKey groupKey, GroupChainElem gce) {
2276 pendingGroups.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07002277 if (val == null) {
2278 val = Sets.newConcurrentHashSet();
2279 }
2280 val.add(gce);
2281 return val;
2282 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08002283 }
2284
Yi Tsengef19de12017-04-24 11:33:05 -07002285 protected void addPendingUpdateNextObjective(GroupKey groupKey,
2286 NextObjective nextObjective) {
Yi Tseng78f51f42017-02-02 13:54:58 -08002287 pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> {
2288 if (nextObjs != null) {
2289 nextObjs.add(nextObjective);
2290 } else {
2291 nextObjs = Sets.newHashSet(nextObjective);
2292 }
2293 return nextObjs;
2294 });
2295 }
2296
Saurav Das2f2c9d02018-04-07 16:51:09 -07002297 protected void addPendingRemoveNextObjective(NextObjective nextObjective,
2298 List<GroupKey> groupKeys) {
2299 pendingRemoveNextObjectives.put(nextObjective, groupKeys);
Yi Tsengef19de12017-04-24 11:33:05 -07002300 }
2301
Saurav Das2f2c9d02018-04-07 16:51:09 -07002302 protected int getNextAvailableIndex() {
2303 return (int) nextIndex.incrementAndGet();
2304 }
2305
2306 protected void processPendingUpdateNextObjs(GroupKey groupKey) {
2307 Set<NextObjective> nextObjs = pendingUpdateNextObjectives.remove(groupKey);
2308 if (nextObjs != null) {
2309 nextObjs.forEach(nextObj -> {
2310 log.debug("Group {} updated, update pending next objective {}.",
2311 groupKey, nextObj);
2312 pass(nextObj);
2313 });
2314 }
2315 }
2316
2317 protected void processPendingRemoveNextObjs(GroupKey key) {
Yi Tsengef19de12017-04-24 11:33:05 -07002318 pendingRemoveNextObjectives.asMap().forEach((nextObjective, groupKeys) -> {
jaegonkim2e4d99e2018-07-09 23:23:10 +09002319 groupKeys.remove(key);
Yi Tsengef19de12017-04-24 11:33:05 -07002320 if (groupKeys.isEmpty()) {
2321 pendingRemoveNextObjectives.invalidate(nextObjective);
2322 pass(nextObjective);
Yi Tsengef19de12017-04-24 11:33:05 -07002323 }
2324 });
2325 }
2326
Yi Tsengef19de12017-04-24 11:33:05 -07002327 protected void processPendingAddGroupsOrNextObjs(GroupKey key, boolean added) {
2328 //first check for group chain
2329 Set<OfdpaGroupHandlerUtility.GroupChainElem> gceSet = pendingGroups.asMap().remove(key);
2330 if (gceSet != null) {
2331 for (GroupChainElem gce : gceSet) {
2332 log.debug("Group service {} group key {} in device {}. "
2333 + "Processing next group in group chain with group id 0x{}",
2334 (added) ? "ADDED" : "processed",
2335 key, deviceId,
2336 Integer.toHexString(gce.groupDescription().givenGroupId()));
2337 processGroupChain(gce);
2338 }
2339 } else {
2340 // otherwise chain complete - check for waiting nextObjectives
2341 List<OfdpaGroupHandlerUtility.OfdpaNextGroup> nextGrpList =
2342 pendingAddNextObjectives.getIfPresent(key);
2343 if (nextGrpList != null) {
2344 pendingAddNextObjectives.invalidate(key);
2345 nextGrpList.forEach(nextGrp -> {
2346 log.debug("Group service {} group key {} in device:{}. "
2347 + "Done implementing next objective: {} <<-->> gid:0x{}",
2348 (added) ? "ADDED" : "processed",
2349 key, deviceId, nextGrp.nextObjective().id(),
2350 Integer.toHexString(groupService.getGroup(deviceId, key)
2351 .givenGroupId()));
2352 pass(nextGrp.nextObjective());
Saurav Dasc88d4662017-05-15 15:34:25 -07002353 updateFlowObjectiveStore(nextGrp.nextObjective().id(), nextGrp);
Yi Tsengef19de12017-04-24 11:33:05 -07002354
2355 // check if addBuckets waiting for this completion
2356 pendingBuckets.compute(nextGrp.nextObjective().id(), (nextId, pendBkts) -> {
2357 if (pendBkts != null) {
2358 pendBkts.forEach(pendBkt -> addBucketToGroup(pendBkt, nextGrp));
2359 }
2360 return null;
2361 });
2362 });
2363 }
2364 }
2365 }
2366
Charles Chan188ebf52015-12-23 00:15:11 -08002367 /**
2368 * Processes next element of a group chain. Assumption is that if this
2369 * group points to another group, the latter has already been created
2370 * and this driver has received notification for it. A second assumption is
2371 * that if there is another group waiting for this group then the appropriate
2372 * stores already have the information to act upon the notification for the
2373 * creation of this group.
2374 * <p>
2375 * The processing of the GroupChainElement depends on the number of groups
2376 * this element is waiting on. For all group types other than SIMPLE, a
2377 * GroupChainElement could be waiting on multiple groups.
2378 *
2379 * @param gce the group chain element to be processed next
2380 */
2381 private void processGroupChain(GroupChainElem gce) {
2382 int waitOnGroups = gce.decrementAndGetGroupsWaitedOn();
2383 if (waitOnGroups != 0) {
2384 log.debug("GCE: {} not ready to be processed", gce);
2385 return;
2386 }
2387 log.debug("GCE: {} ready to be processed", gce);
Yi Tsengef19de12017-04-24 11:33:05 -07002388 if (gce.addBucketToGroup()) {
2389 groupService.addBucketsToGroup(gce.groupDescription().deviceId(),
2390 gce.groupDescription().appCookie(),
2391 gce.groupDescription().buckets(),
2392 gce.groupDescription().appCookie(),
2393 gce.groupDescription().appId());
Charles Chan188ebf52015-12-23 00:15:11 -08002394 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07002395 groupService.addGroup(gce.groupDescription());
Charles Chan188ebf52015-12-23 00:15:11 -08002396 }
2397 }
2398
Saurav Dasc88d4662017-05-15 15:34:25 -07002399 private void updateFlowObjectiveStore(Integer nextId, OfdpaNextGroup nextGrp) {
2400 synchronized (flowObjectiveStore) {
2401 // get fresh copy of what the store holds
2402 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
2403 if (next == null || nextGrp.nextObjective().op() == Operation.ADD) {
2404 flowObjectiveStore.putNextGroup(nextId, nextGrp);
2405 return;
2406 }
2407 if (nextGrp.nextObjective().op() == Operation.ADD_TO_EXISTING) {
2408 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Pier Luigid008dbe2018-01-15 09:48:49 +01002409 allActiveKeys = Lists.newArrayList(allActiveKeys);
Saurav Dasc88d4662017-05-15 15:34:25 -07002410 // If active keys shows only the top-level group without a chain of groups,
2411 // then it represents an empty group. Update by replacing empty chain.
2412 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
2413 allActiveKeys.clear();
2414 }
2415 allActiveKeys.addAll(nextGrp.allKeys());
2416 flowObjectiveStore.putNextGroup(nextId,
2417 new OfdpaNextGroup(allActiveKeys, nextGrp.nextObjective()));
2418 }
2419 }
2420 }
2421
Saurav Das8be4e3a2016-03-11 17:19:07 -08002422 private class InnerGroupListener implements GroupListener {
2423 @Override
2424 public void event(GroupEvent event) {
Charles Chanfc5c7802016-05-17 13:13:55 -07002425 switch (event.type()) {
2426 case GROUP_ADDED:
2427 processPendingAddGroupsOrNextObjs(event.subject().appCookie(), true);
2428 break;
2429 case GROUP_REMOVED:
2430 processPendingRemoveNextObjs(event.subject().appCookie());
2431 break;
Yi Tseng78f51f42017-02-02 13:54:58 -08002432 case GROUP_UPDATED:
2433 processPendingUpdateNextObjs(event.subject().appCookie());
2434 break;
Charles Chanfc5c7802016-05-17 13:13:55 -07002435 default:
2436 break;
Saurav Das8be4e3a2016-03-11 17:19:07 -08002437 }
2438 }
2439 }
Charles Chan188ebf52015-12-23 00:15:11 -08002440}