blob: 40aad0d6aa2e1244a577d960ca038e380b44c4ce [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 Chan32562522016-04-07 14:37:14 -070032import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Charles Chan188ebf52015-12-23 00:15:11 -080033import org.onosproject.net.DeviceId;
34import org.onosproject.net.PortNumber;
35import org.onosproject.net.behaviour.NextGroup;
36import org.onosproject.net.behaviour.PipelinerContext;
37import org.onosproject.net.flow.DefaultTrafficTreatment;
38import org.onosproject.net.flow.TrafficSelector;
39import org.onosproject.net.flow.TrafficTreatment;
40import org.onosproject.net.flow.criteria.Criterion;
Pier Ventre42287df2016-11-09 14:17:26 -080041import org.onosproject.net.flow.criteria.TunnelIdCriterion;
Charles Chan188ebf52015-12-23 00:15:11 -080042import org.onosproject.net.flow.criteria.VlanIdCriterion;
43import org.onosproject.net.flow.instructions.Instruction;
44import org.onosproject.net.flow.instructions.Instructions;
Saurav Das9df5b7c2017-08-14 16:44:43 -070045import org.onosproject.net.flow.instructions.Instructions.GroupInstruction;
Charles Chan188ebf52015-12-23 00:15:11 -080046import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Yi Tseng47f82dc2017-03-05 22:48:39 -080047import org.onosproject.net.flowobjective.DefaultNextObjective;
Charles Chan188ebf52015-12-23 00:15:11 -080048import org.onosproject.net.flowobjective.FlowObjectiveStore;
49import org.onosproject.net.flowobjective.NextObjective;
Saurav Dasc88d4662017-05-15 15:34:25 -070050import org.onosproject.net.flowobjective.Objective.Operation;
Yi Tseng47f82dc2017-03-05 22:48:39 -080051import org.onosproject.net.flowobjective.ObjectiveContext;
Charles Chan188ebf52015-12-23 00:15:11 -080052import org.onosproject.net.flowobjective.ObjectiveError;
53import org.onosproject.net.group.DefaultGroupBucket;
54import org.onosproject.net.group.DefaultGroupDescription;
55import org.onosproject.net.group.DefaultGroupKey;
56import org.onosproject.net.group.Group;
57import org.onosproject.net.group.GroupBucket;
58import org.onosproject.net.group.GroupBuckets;
59import org.onosproject.net.group.GroupDescription;
60import org.onosproject.net.group.GroupEvent;
61import org.onosproject.net.group.GroupKey;
62import org.onosproject.net.group.GroupListener;
63import org.onosproject.net.group.GroupService;
Saurav Das8be4e3a2016-03-11 17:19:07 -080064import org.onosproject.store.service.AtomicCounter;
65import org.onosproject.store.service.StorageService;
Charles Chan188ebf52015-12-23 00:15:11 -080066import org.slf4j.Logger;
67
68import java.util.ArrayDeque;
69import java.util.ArrayList;
Saurav Dasceccf242017-08-03 18:30:35 -070070import java.util.Arrays;
Charles Chan188ebf52015-12-23 00:15:11 -080071import java.util.Collection;
72import java.util.Collections;
73import java.util.Deque;
74import java.util.List;
Charles Chand0fd5dc2016-02-16 23:14:49 -080075import java.util.Objects;
Charles Chan188ebf52015-12-23 00:15:11 -080076import java.util.Set;
77import java.util.concurrent.ConcurrentHashMap;
78import java.util.concurrent.CopyOnWriteArrayList;
79import java.util.concurrent.Executors;
80import java.util.concurrent.ScheduledExecutorService;
81import java.util.concurrent.TimeUnit;
Charles Chan188ebf52015-12-23 00:15:11 -080082import java.util.stream.Collectors;
83
84import static org.onlab.util.Tools.groupedThreads;
Yi Tsengef19de12017-04-24 11:33:05 -070085import static org.onosproject.driver.pipeline.ofdpa.Ofdpa2Pipeline.*;
86import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
Pier Ventre42287df2016-11-09 14:17:26 -080087import static org.onosproject.net.flow.criteria.Criterion.Type.TUNNEL_ID;
Pier Ventre140a8942016-11-02 07:26:38 -070088import static org.onosproject.net.flow.criteria.Criterion.Type.VLAN_VID;
Yi Tseng78f51f42017-02-02 13:54:58 -080089import static org.onosproject.net.group.GroupDescription.Type.ALL;
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -080090import static org.onosproject.net.group.GroupDescription.Type.INDIRECT;
Yi Tseng78f51f42017-02-02 13:54:58 -080091import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Charles Chan188ebf52015-12-23 00:15:11 -080092import static org.slf4j.LoggerFactory.getLogger;
93
94/**
Saurav Das961beb22017-03-29 19:09:17 -070095 * Group handler that emulates Broadcom OF-DPA TTP.
Charles Chan188ebf52015-12-23 00:15:11 -080096 */
Charles Chan361154b2016-03-24 10:23:39 -070097public class Ofdpa2GroupHandler {
Yi Tsengef19de12017-04-24 11:33:05 -070098 protected final Logger log = getLogger(getClass());
Charles Chane849c192016-01-11 18:28:54 -080099
Yi Tsengef19de12017-04-24 11:33:05 -0700100 // Services, Stores
Charles Chan188ebf52015-12-23 00:15:11 -0800101 protected GroupService groupService;
Saurav Das8be4e3a2016-03-11 17:19:07 -0800102 protected StorageService storageService;
Yi Tsengef19de12017-04-24 11:33:05 -0700103 protected FlowObjectiveStore flowObjectiveStore;
Charles Chan188ebf52015-12-23 00:15:11 -0800104
105 // index number for group creation
Saurav Das8be4e3a2016-03-11 17:19:07 -0800106 private AtomicCounter nextIndex;
Charles Chan188ebf52015-12-23 00:15:11 -0800107
Yi Tsengef19de12017-04-24 11:33:05 -0700108 protected DeviceId deviceId;
Saurav Das2f2c9d02018-04-07 16:51:09 -0700109 Cache<GroupKey, List<OfdpaGroupHandlerUtility.OfdpaNextGroup>> pendingAddNextObjectives;
110 Cache<NextObjective, List<GroupKey>> pendingRemoveNextObjectives;
111 Cache<GroupKey, Set<OfdpaGroupHandlerUtility.GroupChainElem>> pendingGroups;
112 ConcurrentHashMap<GroupKey, Set<NextObjective>> pendingUpdateNextObjectives;
Yi Tsengef19de12017-04-24 11:33:05 -0700113
Yi Tseng47f82dc2017-03-05 22:48:39 -0800114 // local store for pending bucketAdds - by design there can be multiple
Charles Chan188ebf52015-12-23 00:15:11 -0800115 // pending bucket for a group
Yi Tseng47f82dc2017-03-05 22:48:39 -0800116 protected ConcurrentHashMap<Integer, Set<NextObjective>> pendingBuckets =
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700117 new ConcurrentHashMap<>();
Charles Chan188ebf52015-12-23 00:15:11 -0800118
Yi Tsengef19de12017-04-24 11:33:05 -0700119 private ScheduledExecutorService groupCheckerExecutor =
120 Executors.newScheduledThreadPool(2, groupedThreads("onos/pipeliner", "ofdpa-%d", log));
121
Charles Chan40132b32017-01-22 00:19:37 -0800122 /**
123 * Determines whether this pipeline support copy ttl instructions or not.
124 *
125 * @return true if copy ttl instructions are supported
126 */
127 protected boolean supportCopyTtl() {
128 return true;
129 }
130
131 /**
132 * Determines whether this pipeline support set mpls bos instruction or not.
133 *
134 * @return true if set mpls bos instruction is supported
135 */
136 protected boolean supportSetMplsBos() {
137 return true;
138 }
139
Charles Chan0f43e472017-02-14 14:00:16 -0800140 /**
141 * Determines whether this pipeline requires popping VLAN before pushing MPLS.
142 * <p>
143 * If required, pop vlan before push mpls and add an arbitrary vlan back afterward.
144 * MPLS interface group will substitute the arbitrary VLAN with expected VLAN later on.
145 *
146 * @return true if this pipeline requires popping VLAN before pushing MPLS
147 */
148 protected boolean requireVlanPopBeforeMplsPush() {
149 return false;
150 }
151
Alex Yashchuk4caa8e82017-12-08 17:40:05 +0200152 public void init(DeviceId deviceId, PipelinerContext context) {
Yi Tsengef19de12017-04-24 11:33:05 -0700153 ServiceDirectory serviceDirectory = context.directory();
Charles Chan188ebf52015-12-23 00:15:11 -0800154 this.deviceId = deviceId;
155 this.flowObjectiveStore = context.store();
Charles Chan188ebf52015-12-23 00:15:11 -0800156 this.groupService = serviceDirectory.get(GroupService.class);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800157 this.storageService = serviceDirectory.get(StorageService.class);
Madan Jampanid5714e02016-04-19 14:15:20 -0700158 this.nextIndex = storageService.getAtomicCounter("group-id-index-counter");
Charles Chan188ebf52015-12-23 00:15:11 -0800159
Charles Chanfc5c7802016-05-17 13:13:55 -0700160 pendingAddNextObjectives = CacheBuilder.newBuilder()
Charles Chan188ebf52015-12-23 00:15:11 -0800161 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700162 .removalListener((RemovalNotification<GroupKey, List<OfdpaNextGroup>> notification) -> {
163 if (notification.getCause() == RemovalCause.EXPIRED &&
164 Objects.nonNull(notification.getValue())) {
165 notification.getValue()
166 .forEach(ofdpaNextGrp ->
167 fail(ofdpaNextGrp.nextObjective(),
168 ObjectiveError.GROUPINSTALLATIONFAILED));
Charles Chanfc5c7802016-05-17 13:13:55 -0700169 }
170 }).build();
Charles Chan188ebf52015-12-23 00:15:11 -0800171
Charles Chanfc5c7802016-05-17 13:13:55 -0700172 pendingRemoveNextObjectives = CacheBuilder.newBuilder()
173 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700174 .removalListener((RemovalNotification<NextObjective, List<GroupKey>> notification) -> {
Charles Chanfc5c7802016-05-17 13:13:55 -0700175 if (notification.getCause() == RemovalCause.EXPIRED) {
Yi Tsengef19de12017-04-24 11:33:05 -0700176 fail(notification.getKey(),
177 ObjectiveError.GROUPREMOVALFAILED);
Charles Chan188ebf52015-12-23 00:15:11 -0800178 }
179 }).build();
Saurav Das961beb22017-03-29 19:09:17 -0700180 pendingGroups = CacheBuilder.newBuilder()
181 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700182 .removalListener((RemovalNotification<GroupKey, Set<GroupChainElem>> notification) -> {
Saurav Das961beb22017-03-29 19:09:17 -0700183 if (notification.getCause() == RemovalCause.EXPIRED) {
184 log.error("Unable to install group with key {} and pending GCEs: {}",
185 notification.getKey(), notification.getValue());
186 }
187 }).build();
Yi Tseng78f51f42017-02-02 13:54:58 -0800188 pendingUpdateNextObjectives = new ConcurrentHashMap<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700189 GroupChecker groupChecker = new GroupChecker(this);
190 groupCheckerExecutor.scheduleAtFixedRate(groupChecker, 0, 500, TimeUnit.MILLISECONDS);
Charles Chan188ebf52015-12-23 00:15:11 -0800191 groupService.addListener(new InnerGroupListener());
192 }
193
Saurav Das8be4e3a2016-03-11 17:19:07 -0800194 //////////////////////////////////////
195 // Group Creation
196 //////////////////////////////////////
197
Yi Tsengef19de12017-04-24 11:33:05 -0700198 /**
199 * Adds a list of group chain by given NextObjective.
200 *
201 * @param nextObjective the NextObjective
202 */
Charles Chan188ebf52015-12-23 00:15:11 -0800203 protected void addGroup(NextObjective nextObjective) {
204 switch (nextObjective.type()) {
205 case SIMPLE:
206 Collection<TrafficTreatment> treatments = nextObjective.next();
207 if (treatments.size() != 1) {
208 log.error("Next Objectives of type Simple should only have a "
209 + "single Traffic Treatment. Next Objective Id:{}",
210 nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -0700211 fail(nextObjective, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800212 return;
213 }
214 processSimpleNextObjective(nextObjective);
215 break;
216 case BROADCAST:
217 processBroadcastNextObjective(nextObjective);
218 break;
219 case HASHED:
Pier Ventre140a8942016-11-02 07:26:38 -0700220 if (!verifyHashedNextObjective(nextObjective)) {
221 log.error("Next Objectives of type hashed not supported. Next Objective Id:{}",
222 nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -0700223 fail(nextObjective, ObjectiveError.BADPARAMS);
Pier Ventre140a8942016-11-02 07:26:38 -0700224 return;
225 }
Charles Chan188ebf52015-12-23 00:15:11 -0800226 processHashedNextObjective(nextObjective);
227 break;
228 case FAILOVER:
Yi Tsengef19de12017-04-24 11:33:05 -0700229 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -0800230 log.warn("Unsupported next objective type {}", nextObjective.type());
231 break;
232 default:
Yi Tsengef19de12017-04-24 11:33:05 -0700233 fail(nextObjective, ObjectiveError.UNKNOWN);
Charles Chan188ebf52015-12-23 00:15:11 -0800234 log.warn("Unknown next objective type {}", nextObjective.type());
235 }
236 }
237
238 /**
239 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
Saurav Dasa4020382018-02-14 14:14:54 -0800240 * a chain of groups. The simple Next Objective passed in by the application
241 * is broken up into a group chain. The following chains can be created
242 * depending on the parameters in the Next Objective.
243 * 1. L2 Interface group (no chaining)
244 * 2. L3 Unicast group -> L2 Interface group
245 * 3. MPLS Interface group -> L2 Interface group
246 * 4. MPLS Swap group -> MPLS Interface group -> L2 Interface group
247 * 5. PW initiation group chain
Charles Chan188ebf52015-12-23 00:15:11 -0800248 *
249 * @param nextObj the nextObjective of type SIMPLE
250 */
251 private void processSimpleNextObjective(NextObjective nextObj) {
252 TrafficTreatment treatment = nextObj.next().iterator().next();
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800253 // determine if plain L2 or L3->L2 or MPLS Swap -> MPLS Interface -> L2
Charles Chan188ebf52015-12-23 00:15:11 -0800254 boolean plainL2 = true;
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800255 boolean mplsSwap = false;
256 MplsLabel mplsLabel = null;
Charles Chan188ebf52015-12-23 00:15:11 -0800257 for (Instruction ins : treatment.allInstructions()) {
258 if (ins.type() == Instruction.Type.L2MODIFICATION) {
259 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
260 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_DST ||
261 l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_SRC) {
262 plainL2 = false;
Charles Chan188ebf52015-12-23 00:15:11 -0800263 }
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800264 // mpls label in simple next objectives is used only to indicate
265 // a MPLS Swap group before the MPLS Interface Group
266 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) {
267 mplsSwap = true;
268 mplsLabel = ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label();
269 }
Charles Chan188ebf52015-12-23 00:15:11 -0800270 }
271 }
272
273 if (plainL2) {
274 createL2InterfaceGroup(nextObj);
275 return;
276 }
277
Saurav Dasa4020382018-02-14 14:14:54 -0800278 // In order to understand if it is a pseudowire related
Pier Ventre42287df2016-11-09 14:17:26 -0800279 // next objective we look for the tunnel id in the meta.
280 boolean isPw = false;
Pier Ventre140a8942016-11-02 07:26:38 -0700281 if (nextObj.meta() != null) {
Pier Ventre42287df2016-11-09 14:17:26 -0800282 TunnelIdCriterion tunnelIdCriterion = (TunnelIdCriterion) nextObj
283 .meta()
284 .getCriterion(TUNNEL_ID);
285 if (tunnelIdCriterion != null) {
286 isPw = true;
287 }
Pier Ventre140a8942016-11-02 07:26:38 -0700288 }
289
Andreas Pantelopoulos25db42f2018-02-08 12:42:06 -0800290 if (mplsSwap && !isPw) {
Saurav Dasa4020382018-02-14 14:14:54 -0800291 log.debug("Creating a MPLS Swap -> MPLS Interface -> L2 Interface group chain.");
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800292
293 // break up simple next objective to GroupChain objects
294 GroupInfo groupInfo = createL2L3Chain(treatment, nextObj.id(),
295 nextObj.appId(), true,
296 nextObj.meta());
297 if (groupInfo == null) {
298 log.error("Could not process nextObj={} in dev:{}", nextObj.id(), deviceId);
299 fail(nextObj, ObjectiveError.BADPARAMS);
300 return;
301 }
302
303 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Saurav Dasa4020382018-02-14 14:14:54 -0800304 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie()); // l2 interface
305 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie()); // mpls interface
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800306
307 // creating the mpls swap group and adding it to the chain
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800308 int nextGid = groupInfo.nextGroupDesc().givenGroupId();
309 int index = getNextAvailableIndex();
310
Saurav Dasa4020382018-02-14 14:14:54 -0800311 GroupDescription swapGroupDescription = createMplsSwap(
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800312 nextGid,
313 OfdpaMplsGroupSubType.MPLS_SWAP_LABEL,
314 index,
315 mplsLabel,
316 nextObj.appId()
317 );
Saurav Dasa4020382018-02-14 14:14:54 -0800318 // ensure swap group is added after L2L3 chain
319 GroupKey swapGroupKey = swapGroupDescription.appCookie();
320 GroupChainElem swapChainElem = new GroupChainElem(swapGroupDescription,
321 1, false, deviceId);
322 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), swapChainElem);
323 gkeyChain.addFirst(swapGroupKey);
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800324
Saurav Dasa4020382018-02-14 14:14:54 -0800325 // ensure nextObjective waits on the outermost groupKey
326 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
327 allGroupKeys.add(gkeyChain);
328 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
329 updatePendingNextObjective(swapGroupKey, ofdpaGrp);
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800330
331 // now we are ready to send the l2 groupDescription (inner), as all the stores
332 // that will get async replies have been updated. By waiting to update
333 // the stores, we prevent nasty race conditions.
334 groupService.addGroup(groupInfo.innerMostGroupDesc());
335 } else if (!isPw) {
Saurav Dasa4020382018-02-14 14:14:54 -0800336 boolean isMpls = false;
337 if (nextObj.meta() != null) {
338 isMpls = isNotMplsBos(nextObj.meta());
339 }
340 log.debug("Creating a {} -> L2 Interface group chain.",
341 (isMpls) ? "MPLS Interface" : "L3 Unicast");
Pier Ventre42287df2016-11-09 14:17:26 -0800342 // break up simple next objective to GroupChain objects
343 GroupInfo groupInfo = createL2L3Chain(treatment, nextObj.id(),
344 nextObj.appId(), isMpls,
345 nextObj.meta());
346 if (groupInfo == null) {
347 log.error("Could not process nextObj={} in dev:{}", nextObj.id(), deviceId);
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800348 fail(nextObj, ObjectiveError.BADPARAMS);
Pier Ventre42287df2016-11-09 14:17:26 -0800349 return;
350 }
351 // create object for local and distributed storage
352 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700353 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
354 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Saurav Dasa4020382018-02-14 14:14:54 -0800355 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
356 allGroupKeys.add(gkeyChain);
357 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Pier Ventre42287df2016-11-09 14:17:26 -0800358
359 // store l3groupkey with the ofdpaNextGroup for the nextObjective that depends on it
Yi Tsengef19de12017-04-24 11:33:05 -0700360 updatePendingNextObjective(groupInfo.nextGroupDesc().appCookie(), ofdpaGrp);
Pier Ventre42287df2016-11-09 14:17:26 -0800361
362 // now we are ready to send the l2 groupDescription (inner), as all the stores
363 // that will get async replies have been updated. By waiting to update
364 // the stores, we prevent nasty race conditions.
Yi Tsengef19de12017-04-24 11:33:05 -0700365 groupService.addGroup(groupInfo.innerMostGroupDesc());
Pier Ventre42287df2016-11-09 14:17:26 -0800366 } else {
367 // We handle the pseudo wire with a different a procedure.
368 // This procedure is meant to handle both initiation and
369 // termination of the pseudo wire.
370 processPwNextObjective(nextObj);
Charles Chan188ebf52015-12-23 00:15:11 -0800371 }
Charles Chan188ebf52015-12-23 00:15:11 -0800372 }
373
Charles Chan188ebf52015-12-23 00:15:11 -0800374 /**
375 * Creates a simple L2 Interface Group.
376 *
377 * @param nextObj the next Objective
378 */
379 private void createL2InterfaceGroup(NextObjective nextObj) {
Yi Tsengef19de12017-04-24 11:33:05 -0700380 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700381 if (assignedVlan == null) {
382 log.warn("VLAN ID required by simple next obj is missing. Abort.");
Yi Tsengef19de12017-04-24 11:33:05 -0700383 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800384 return;
385 }
386
Charles Chan5b9df8d2016-03-28 22:21:40 -0700387 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Charles Chan188ebf52015-12-23 00:15:11 -0800388
Charles Chan5b9df8d2016-03-28 22:21:40 -0700389 // There is only one L2 interface group in this case
Yi Tsengef19de12017-04-24 11:33:05 -0700390 GroupDescription l2InterfaceGroupDesc = groupInfos.get(0).innerMostGroupDesc();
Charles Chan188ebf52015-12-23 00:15:11 -0800391
Charles Chan5b9df8d2016-03-28 22:21:40 -0700392 // Put all dependency information into allGroupKeys
393 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
394 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
395 gkeyChain.addFirst(l2InterfaceGroupDesc.appCookie());
396 allGroupKeys.add(gkeyChain);
Charles Chan188ebf52015-12-23 00:15:11 -0800397
Charles Chan5b9df8d2016-03-28 22:21:40 -0700398 // Point the next objective to this group
399 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
400 updatePendingNextObjective(l2InterfaceGroupDesc.appCookie(), ofdpaGrp);
401
402 // Start installing the inner-most group
403 groupService.addGroup(l2InterfaceGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800404 }
405
406 /**
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800407 * Creates an Mpls group of type swap.
408 *
409 * @param nextGroupId the next group in the chain
410 * @param subtype the mpls swap label group subtype
411 * @param index the index of the group
412 * @param mplsLabel the mpls label to swap
413 * @param applicationId the application id
414 * @return the group description
415 */
416 protected GroupDescription createMplsSwap(int nextGroupId,
417 OfdpaMplsGroupSubType subtype,
418 int index,
419 MplsLabel mplsLabel,
420 ApplicationId applicationId) {
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800421 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800422 treatment.setMpls(mplsLabel);
423
424 // We point the group to the next group.
425 treatment.group(new GroupId(nextGroupId));
426 GroupBucket groupBucket = DefaultGroupBucket
427 .createIndirectGroupBucket(treatment.build());
428 // Finally we build the group description.
429 int groupId = makeMplsLabelGroupId(subtype, index);
430 GroupKey groupKey = new DefaultGroupKey(
Saurav Dasa4020382018-02-14 14:14:54 -0800431 Ofdpa2Pipeline.appKryo.serialize(index));
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800432 return new DefaultGroupDescription(
433 deviceId,
434 INDIRECT,
435 new GroupBuckets(Collections.singletonList(groupBucket)),
436 groupKey,
437 groupId,
Saurav Dasa4020382018-02-14 14:14:54 -0800438 applicationId);
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800439 }
440
441 /**
Charles Chan188ebf52015-12-23 00:15:11 -0800442 * Creates one of two possible group-chains from the treatment
443 * passed in. Depending on the MPLS boolean, this method either creates
Charles Chan425854b2016-04-11 15:32:12 -0700444 * an L3Unicast Group --&gt; L2Interface Group, if mpls is false;
445 * or MPLSInterface Group --&gt; L2Interface Group, if mpls is true;
Charles Chan188ebf52015-12-23 00:15:11 -0800446 * The returned 'inner' group description is always the L2 Interface group.
447 *
448 * @param treatment that needs to be broken up to create the group chain
449 * @param nextId of the next objective that needs this group chain
450 * @param appId of the application that sent this next objective
451 * @param mpls determines if L3Unicast or MPLSInterface group is created
452 * @param meta metadata passed in by the application as part of the nextObjective
453 * @return GroupInfo containing the GroupDescription of the
454 * L2Interface group(inner) and the GroupDescription of the (outer)
455 * L3Unicast/MPLSInterface group. May return null if there is an
456 * error in processing the chain
457 */
Charles Chan425854b2016-04-11 15:32:12 -0700458 protected GroupInfo createL2L3Chain(TrafficTreatment treatment, int nextId,
Charles Chanf9e98652016-09-07 16:54:23 -0700459 ApplicationId appId, boolean mpls,
460 TrafficSelector meta) {
461 return createL2L3ChainInternal(treatment, nextId, appId, mpls, meta, true);
462 }
463
464 /**
465 * Internal implementation of createL2L3Chain.
466 * <p>
467 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
468 * Since it is non-OF spec, we need an extension treatment for that.
469 * The useSetVlanExtension must be set to false for OFDPA i12.
470 * </p>
471 *
472 * @param treatment that needs to be broken up to create the group chain
473 * @param nextId of the next objective that needs this group chain
474 * @param appId of the application that sent this next objective
475 * @param mpls determines if L3Unicast or MPLSInterface group is created
476 * @param meta metadata passed in by the application as part of the nextObjective
477 * @param useSetVlanExtension use the setVlanVid extension that has is_present bit set to 0.
478 * @return GroupInfo containing the GroupDescription of the
479 * L2Interface group(inner) and the GroupDescription of the (outer)
480 * L3Unicast/MPLSInterface group. May return null if there is an
481 * error in processing the chain
482 */
483 protected GroupInfo createL2L3ChainInternal(TrafficTreatment treatment, int nextId,
Pier Ventre42287df2016-11-09 14:17:26 -0800484 ApplicationId appId, boolean mpls,
485 TrafficSelector meta, boolean useSetVlanExtension) {
Charles Chan188ebf52015-12-23 00:15:11 -0800486 // for the l2interface group, get vlan and port info
487 // for the outer group, get the src/dst mac, and vlan info
488 TrafficTreatment.Builder outerTtb = DefaultTrafficTreatment.builder();
489 TrafficTreatment.Builder innerTtb = DefaultTrafficTreatment.builder();
490 VlanId vlanid = null;
491 long portNum = 0;
492 boolean setVlan = false, popVlan = false;
Yi Tseng78f51f42017-02-02 13:54:58 -0800493 MacAddress srcMac;
494 MacAddress dstMac;
Charles Chan188ebf52015-12-23 00:15:11 -0800495 for (Instruction ins : treatment.allInstructions()) {
496 if (ins.type() == Instruction.Type.L2MODIFICATION) {
497 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
498 switch (l2ins.subtype()) {
499 case ETH_DST:
Charles Chan5270ed02016-01-30 23:22:37 -0800500 dstMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
501 outerTtb.setEthDst(dstMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800502 break;
503 case ETH_SRC:
Charles Chand0fd5dc2016-02-16 23:14:49 -0800504 srcMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
505 outerTtb.setEthSrc(srcMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800506 break;
507 case VLAN_ID:
508 vlanid = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
Charles Chanf9e98652016-09-07 16:54:23 -0700509 if (useSetVlanExtension) {
510 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
511 outerTtb.extension(ofdpaSetVlanVid, deviceId);
512 } else {
513 outerTtb.setVlanId(vlanid);
514 }
Charles Chan188ebf52015-12-23 00:15:11 -0800515 setVlan = true;
516 break;
517 case VLAN_POP:
518 innerTtb.popVlan();
519 popVlan = true;
520 break;
521 case DEC_MPLS_TTL:
522 case MPLS_LABEL:
523 case MPLS_POP:
524 case MPLS_PUSH:
525 case VLAN_PCP:
526 case VLAN_PUSH:
527 default:
528 break;
529 }
530 } else if (ins.type() == Instruction.Type.OUTPUT) {
531 portNum = ((Instructions.OutputInstruction) ins).port().toLong();
532 innerTtb.add(ins);
533 } else {
Saurav Das7bcbe702017-06-13 15:35:54 -0700534 log.debug("Driver does not handle this type of TrafficTreatment"
Saurav Dasa4020382018-02-14 14:14:54 -0800535 + " instruction in l2l3chain: {} - {}", ins.type(),
536 ins);
Charles Chan188ebf52015-12-23 00:15:11 -0800537 }
538 }
539
540 if (vlanid == null && meta != null) {
541 // use metadata if available
Pier Ventre140a8942016-11-02 07:26:38 -0700542 Criterion vidCriterion = meta.getCriterion(VLAN_VID);
Charles Chan188ebf52015-12-23 00:15:11 -0800543 if (vidCriterion != null) {
544 vlanid = ((VlanIdCriterion) vidCriterion).vlanId();
545 }
546 // if vlan is not set, use the vlan in metadata for outerTtb
547 if (vlanid != null && !setVlan) {
Charles Chanf9e98652016-09-07 16:54:23 -0700548 if (useSetVlanExtension) {
549 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
550 outerTtb.extension(ofdpaSetVlanVid, deviceId);
551 } else {
552 outerTtb.setVlanId(vlanid);
553 }
Charles Chan188ebf52015-12-23 00:15:11 -0800554 }
555 }
556
557 if (vlanid == null) {
558 log.error("Driver cannot process an L2/L3 group chain without "
559 + "egress vlan information for dev: {} port:{}",
560 deviceId, portNum);
561 return null;
562 }
563
564 if (!setVlan && !popVlan) {
565 // untagged outgoing port
566 TrafficTreatment.Builder temp = DefaultTrafficTreatment.builder();
567 temp.popVlan();
Yi Tsengef19de12017-04-24 11:33:05 -0700568 innerTtb.build().allInstructions().forEach(temp::add);
Charles Chan188ebf52015-12-23 00:15:11 -0800569 innerTtb = temp;
570 }
571
572 // assemble information for ofdpa l2interface group
Yi Tsengef19de12017-04-24 11:33:05 -0700573 int l2groupId = l2GroupId(vlanid, portNum);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800574 // a globally unique groupkey that is different for ports in the same device,
Charles Chan188ebf52015-12-23 00:15:11 -0800575 // but different for the same portnumber on different devices. Also different
576 // for the various group-types created out of the same next objective.
Charles Chane849c192016-01-11 18:28:54 -0800577 int l2gk = l2InterfaceGroupKey(deviceId, vlanid, portNum);
Yi Tsengef19de12017-04-24 11:33:05 -0700578 final GroupKey l2groupkey = new DefaultGroupKey(appKryo.serialize(l2gk));
Charles Chan188ebf52015-12-23 00:15:11 -0800579
580 // assemble information for outer group
Yi Tsengef19de12017-04-24 11:33:05 -0700581 GroupDescription outerGrpDesc;
Charles Chan188ebf52015-12-23 00:15:11 -0800582 if (mpls) {
Yi Tsengef19de12017-04-24 11:33:05 -0700583 // outer group is MPLS Interface
Saurav Das8be4e3a2016-03-11 17:19:07 -0800584 int mplsInterfaceIndex = getNextAvailableIndex();
Yi Tsengef19de12017-04-24 11:33:05 -0700585 int mplsGroupId = MPLS_INTERFACE_TYPE | (SUBTYPE_MASK & mplsInterfaceIndex);
586 final GroupKey mplsGroupKey = new DefaultGroupKey(
587 appKryo.serialize(mplsInterfaceIndex));
Yi Tsengfa394de2017-02-01 11:26:40 -0800588 outerTtb.group(new GroupId(l2groupId));
Charles Chan188ebf52015-12-23 00:15:11 -0800589 // create the mpls-interface group description to wait for the
590 // l2 interface group to be processed
591 GroupBucket mplsinterfaceGroupBucket =
592 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
593 outerGrpDesc = new DefaultGroupDescription(
594 deviceId,
595 GroupDescription.Type.INDIRECT,
596 new GroupBuckets(Collections.singletonList(
597 mplsinterfaceGroupBucket)),
Yi Tsengef19de12017-04-24 11:33:05 -0700598 mplsGroupKey,
599 mplsGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800600 appId);
601 log.debug("Trying MPLS-Interface: device:{} gid:{} gkey:{} nextid:{}",
Yi Tsengef19de12017-04-24 11:33:05 -0700602 deviceId, Integer.toHexString(mplsGroupId),
603 mplsGroupKey, nextId);
Charles Chan188ebf52015-12-23 00:15:11 -0800604 } else {
605 // outer group is L3Unicast
Saurav Das8be4e3a2016-03-11 17:19:07 -0800606 int l3unicastIndex = getNextAvailableIndex();
607 int l3groupId = L3_UNICAST_TYPE | (TYPE_MASK & l3unicastIndex);
608 final GroupKey l3groupkey = new DefaultGroupKey(
Yi Tsengef19de12017-04-24 11:33:05 -0700609 appKryo.serialize(l3unicastIndex));
Yi Tsengfa394de2017-02-01 11:26:40 -0800610 outerTtb.group(new GroupId(l2groupId));
Charles Chan188ebf52015-12-23 00:15:11 -0800611 // create the l3unicast group description to wait for the
612 // l2 interface group to be processed
613 GroupBucket l3unicastGroupBucket =
614 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
615 outerGrpDesc = new DefaultGroupDescription(
616 deviceId,
617 GroupDescription.Type.INDIRECT,
Yi Tsengef19de12017-04-24 11:33:05 -0700618 new GroupBuckets(Collections.singletonList(l3unicastGroupBucket)),
Charles Chan188ebf52015-12-23 00:15:11 -0800619 l3groupkey,
620 l3groupId,
621 appId);
622 log.debug("Trying L3Unicast: device:{} gid:{} gkey:{} nextid:{}",
623 deviceId, Integer.toHexString(l3groupId),
624 l3groupkey, nextId);
625 }
626
627 // store l2groupkey with the groupChainElem for the outer-group that depends on it
Yi Tsengef19de12017-04-24 11:33:05 -0700628 GroupChainElem gce = new GroupChainElem(outerGrpDesc, 1, false, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800629 updatePendingGroups(l2groupkey, gce);
630
Yi Tsengef19de12017-04-24 11:33:05 -0700631 // create group description for the inner l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700632 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800633 DefaultGroupBucket.createIndirectGroupBucket(innerTtb.build());
634 GroupDescription l2groupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -0700635 new DefaultGroupDescription(deviceId,
636 GroupDescription.Type.INDIRECT,
637 new GroupBuckets(Collections.singletonList(l2InterfaceGroupBucket)),
638 l2groupkey,
639 l2groupId,
640 appId);
Charles Chan188ebf52015-12-23 00:15:11 -0800641 log.debug("Trying L2Interface: device:{} gid:{} gkey:{} nextId:{}",
642 deviceId, Integer.toHexString(l2groupId),
643 l2groupkey, nextId);
644 return new GroupInfo(l2groupDescription, outerGrpDesc);
645
646 }
647
648 /**
649 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
650 * a chain of groups. The broadcast Next Objective passed in by the application
651 * has to be broken up into a group chain comprising of an
Saurav Das1a129a02016-11-18 15:21:57 -0800652 * L2 Flood group or L3 Multicast group, whose buckets point to L2 Interface groups.
Charles Chan188ebf52015-12-23 00:15:11 -0800653 *
654 * @param nextObj the nextObjective of type BROADCAST
655 */
656 private void processBroadcastNextObjective(NextObjective nextObj) {
Yi Tsengef19de12017-04-24 11:33:05 -0700657 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700658 if (assignedVlan == null) {
659 log.warn("VLAN ID required by broadcast next obj is missing. Abort.");
Yi Tsengef19de12017-04-24 11:33:05 -0700660 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800661 return;
662 }
Charles Chan188ebf52015-12-23 00:15:11 -0800663
Charles Chan5b9df8d2016-03-28 22:21:40 -0700664 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
665
Yi Tsengef19de12017-04-24 11:33:05 -0700666 IpPrefix ipDst = readIpDstFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700667 if (ipDst != null) {
668 if (ipDst.isMulticast()) {
669 createL3MulticastGroup(nextObj, assignedVlan, groupInfos);
670 } else {
671 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -0700672 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700673 }
674 } else {
675 createL2FloodGroup(nextObj, assignedVlan, groupInfos);
676 }
677 }
678
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700679 private List<GroupInfo> prepareL2InterfaceGroup(NextObjective nextObj,
680 VlanId assignedVlan) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700681 ImmutableList.Builder<GroupInfo> groupInfoBuilder = ImmutableList.builder();
682
683 // break up broadcast next objective to multiple groups
684 Collection<TrafficTreatment> buckets = nextObj.next();
685
Charles Chan188ebf52015-12-23 00:15:11 -0800686 // each treatment is converted to an L2 interface group
Charles Chan188ebf52015-12-23 00:15:11 -0800687 for (TrafficTreatment treatment : buckets) {
688 TrafficTreatment.Builder newTreatment = DefaultTrafficTreatment.builder();
689 PortNumber portNum = null;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700690 VlanId egressVlan = null;
Charles Chan188ebf52015-12-23 00:15:11 -0800691 // ensure that the only allowed treatments are pop-vlan and output
692 for (Instruction ins : treatment.allInstructions()) {
693 if (ins.type() == Instruction.Type.L2MODIFICATION) {
694 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
695 switch (l2ins.subtype()) {
696 case VLAN_POP:
697 newTreatment.add(l2ins);
698 break;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700699 case VLAN_ID:
700 egressVlan = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
701 break;
Charles Chan188ebf52015-12-23 00:15:11 -0800702 default:
703 log.debug("action {} not permitted for broadcast nextObj",
704 l2ins.subtype());
705 break;
706 }
707 } else if (ins.type() == Instruction.Type.OUTPUT) {
708 portNum = ((Instructions.OutputInstruction) ins).port();
709 newTreatment.add(ins);
710 } else {
Charles Chane849c192016-01-11 18:28:54 -0800711 log.debug("TrafficTreatment of type {} not permitted in " +
712 " broadcast nextObjective", ins.type());
Charles Chan188ebf52015-12-23 00:15:11 -0800713 }
714 }
715
Yi Tsengef19de12017-04-24 11:33:05 -0700716 if (portNum == null) {
717 log.warn("Can't find output port for the bucket {}.", treatment);
718 continue;
719 }
720
Charles Chan188ebf52015-12-23 00:15:11 -0800721 // assemble info for l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700722 VlanId l2InterfaceGroupVlan =
723 (egressVlan != null && !assignedVlan.equals(egressVlan)) ?
724 egressVlan : assignedVlan;
725 int l2gk = l2InterfaceGroupKey(deviceId, l2InterfaceGroupVlan, portNum.toLong());
726 final GroupKey l2InterfaceGroupKey =
Yi Tsengef19de12017-04-24 11:33:05 -0700727 new DefaultGroupKey(appKryo.serialize(l2gk));
Charles Chan372b63e2017-02-07 12:10:53 -0800728 int l2InterfaceGroupId = L2_INTERFACE_TYPE |
729 ((l2InterfaceGroupVlan.toShort() & THREE_BIT_MASK) << PORT_LEN) |
730 ((int) portNum.toLong() & FOUR_BIT_MASK);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700731 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800732 DefaultGroupBucket.createIndirectGroupBucket(newTreatment.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700733 GroupDescription l2InterfaceGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -0700734 new DefaultGroupDescription(deviceId,
735 GroupDescription.Type.INDIRECT,
736 new GroupBuckets(Collections.singletonList(
737 l2InterfaceGroupBucket)),
738 l2InterfaceGroupKey,
739 l2InterfaceGroupId,
740 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800741 log.debug("Trying L2-Interface: device:{} gid:{} gkey:{} nextid:{}",
Charles Chan5b9df8d2016-03-28 22:21:40 -0700742 deviceId, Integer.toHexString(l2InterfaceGroupId),
743 l2InterfaceGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -0800744
Charles Chan5b9df8d2016-03-28 22:21:40 -0700745 groupInfoBuilder.add(new GroupInfo(l2InterfaceGroupDescription,
746 l2InterfaceGroupDescription));
Charles Chan188ebf52015-12-23 00:15:11 -0800747 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700748 return groupInfoBuilder.build();
749 }
Charles Chan188ebf52015-12-23 00:15:11 -0800750
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700751 private void createL2FloodGroup(NextObjective nextObj, VlanId vlanId,
752 List<GroupInfo> groupInfos) {
753 // assemble info for l2 flood group. Since there can be only one flood
754 // group for a vlan, its index is always the same - 0
Yi Tsengef19de12017-04-24 11:33:05 -0700755 Integer l2FloodGroupId = L2_FLOOD_TYPE | (vlanId.toShort() << 16);
756 final GroupKey l2FloodGroupKey = l2FloodGroupKey(vlanId, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700757
Charles Chan188ebf52015-12-23 00:15:11 -0800758 // collection of group buckets pointing to all the l2 interface groups
Yi Tsengef19de12017-04-24 11:33:05 -0700759 List<GroupBucket> l2floodBuckets = generateNextGroupBuckets(groupInfos, ALL);
Charles Chan188ebf52015-12-23 00:15:11 -0800760 // create the l2flood group-description to wait for all the
761 // l2interface groups to be processed
762 GroupDescription l2floodGroupDescription =
763 new DefaultGroupDescription(
764 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800765 ALL,
Charles Chan188ebf52015-12-23 00:15:11 -0800766 new GroupBuckets(l2floodBuckets),
Yi Tsengef19de12017-04-24 11:33:05 -0700767 l2FloodGroupKey,
768 l2FloodGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800769 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800770 log.debug("Trying L2-Flood: device:{} gid:{} gkey:{} nextid:{}",
Yi Tsengef19de12017-04-24 11:33:05 -0700771 deviceId, Integer.toHexString(l2FloodGroupId),
772 l2FloodGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -0800773
Charles Chan5b9df8d2016-03-28 22:21:40 -0700774 // Put all dependency information into allGroupKeys
775 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
776 groupInfos.forEach(groupInfo -> {
Yi Tsengef19de12017-04-24 11:33:05 -0700777 Deque<GroupKey> groupKeyChain = new ArrayDeque<>();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700778 // In this case we should have L2 interface group only
Yi Tsengef19de12017-04-24 11:33:05 -0700779 groupKeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
780 groupKeyChain.addFirst(l2FloodGroupKey);
781 allGroupKeys.add(groupKeyChain);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700782 });
Charles Chan188ebf52015-12-23 00:15:11 -0800783
Charles Chan5b9df8d2016-03-28 22:21:40 -0700784 // Point the next objective to this group
785 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -0700786 updatePendingNextObjective(l2FloodGroupKey, ofdpaGrp);
Charles Chan188ebf52015-12-23 00:15:11 -0800787
Charles Chan5b9df8d2016-03-28 22:21:40 -0700788 GroupChainElem gce = new GroupChainElem(l2floodGroupDescription,
Yi Tsengef19de12017-04-24 11:33:05 -0700789 groupInfos.size(), false, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700790 groupInfos.forEach(groupInfo -> {
791 // Point this group to the next group
Yi Tsengef19de12017-04-24 11:33:05 -0700792 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), gce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700793 // Start installing the inner-most group
Yi Tsengef19de12017-04-24 11:33:05 -0700794 groupService.addGroup(groupInfo.innerMostGroupDesc());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700795 });
796 }
797
Pier Luigi21fffd22018-01-19 10:24:53 +0100798 private void createL3MulticastGroup(NextObjective nextObj, VlanId vlanId,
799 List<GroupInfo> groupInfos) {
800 // Let's create a new list mcast buckets
801 List<GroupBucket> l3McastBuckets = createL3MulticastBucket(groupInfos);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700802
803 int l3MulticastIndex = getNextAvailableIndex();
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700804 int l3MulticastGroupId = L3_MULTICAST_TYPE |
805 vlanId.toShort() << 16 | (TYPE_VLAN_MASK & l3MulticastIndex);
806 final GroupKey l3MulticastGroupKey =
Yi Tsengef19de12017-04-24 11:33:05 -0700807 new DefaultGroupKey(appKryo.serialize(l3MulticastIndex));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700808
809 GroupDescription l3MulticastGroupDesc = new DefaultGroupDescription(deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800810 ALL,
Charles Chan5b9df8d2016-03-28 22:21:40 -0700811 new GroupBuckets(l3McastBuckets),
812 l3MulticastGroupKey,
813 l3MulticastGroupId,
814 nextObj.appId());
815
816 // Put all dependency information into allGroupKeys
817 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
818 groupInfos.forEach(groupInfo -> {
819 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700820 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700821 // Add L3 interface group to the chain if there is one.
Yi Tsengef19de12017-04-24 11:33:05 -0700822 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
823 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700824 }
825 gkeyChain.addFirst(l3MulticastGroupKey);
826 allGroupKeys.add(gkeyChain);
827 });
828
829 // Point the next objective to this group
830 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
831 updatePendingNextObjective(l3MulticastGroupKey, ofdpaGrp);
832
833 GroupChainElem outerGce = new GroupChainElem(l3MulticastGroupDesc,
Yi Tsengef19de12017-04-24 11:33:05 -0700834 groupInfos.size(), false, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700835 groupInfos.forEach(groupInfo -> {
836 // Point this group (L3 multicast) to the next group
Yi Tsengef19de12017-04-24 11:33:05 -0700837 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), outerGce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700838
839 // Point next group to inner-most group, if any
Yi Tsengef19de12017-04-24 11:33:05 -0700840 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
841 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(),
842 1, false, deviceId);
843 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700844 }
845
846 // Start installing the inner-most group
Yi Tsengef19de12017-04-24 11:33:05 -0700847 groupService.addGroup(groupInfo.innerMostGroupDesc());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700848 });
Charles Chan188ebf52015-12-23 00:15:11 -0800849 }
850
Charles Chan188ebf52015-12-23 00:15:11 -0800851 /**
852 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
853 * a chain of groups. The hashed Next Objective passed in by the application
854 * has to be broken up into a group chain comprising of an
855 * L3 ECMP group as the top level group. Buckets of this group can point
856 * to a variety of groups in a group chain, depending on the whether
857 * MPLS labels are being pushed or not.
858 * <p>
859 * NOTE: We do not create MPLS ECMP groups as they are unimplemented in
860 * OF-DPA 2.0 (even though it is in the spec). Therefore we do not
861 * check the nextObjective meta to see what is matching before being
862 * sent to this nextObjective.
863 *
864 * @param nextObj the nextObjective of type HASHED
865 */
Pier Ventre140a8942016-11-02 07:26:38 -0700866 protected void processHashedNextObjective(NextObjective nextObj) {
Charles Chan188ebf52015-12-23 00:15:11 -0800867 // storage for all group keys in the chain of groups created
868 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
869 List<GroupInfo> unsentGroups = new ArrayList<>();
870 createHashBucketChains(nextObj, allGroupKeys, unsentGroups);
871
872 // now we can create the outermost L3 ECMP group
873 List<GroupBucket> l3ecmpGroupBuckets = new ArrayList<>();
874 for (GroupInfo gi : unsentGroups) {
875 // create ECMP bucket to point to the outer group
876 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Yi Tsengef19de12017-04-24 11:33:05 -0700877 ttb.group(new GroupId(gi.nextGroupDesc().givenGroupId()));
Charles Chan188ebf52015-12-23 00:15:11 -0800878 GroupBucket sbucket = DefaultGroupBucket
879 .createSelectGroupBucket(ttb.build());
880 l3ecmpGroupBuckets.add(sbucket);
881 }
Saurav Das8be4e3a2016-03-11 17:19:07 -0800882 int l3ecmpIndex = getNextAvailableIndex();
883 int l3ecmpGroupId = L3_ECMP_TYPE | (TYPE_MASK & l3ecmpIndex);
884 GroupKey l3ecmpGroupKey = new DefaultGroupKey(
Yi Tsengef19de12017-04-24 11:33:05 -0700885 appKryo.serialize(l3ecmpIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800886 GroupDescription l3ecmpGroupDesc =
887 new DefaultGroupDescription(
888 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800889 SELECT,
Charles Chan188ebf52015-12-23 00:15:11 -0800890 new GroupBuckets(l3ecmpGroupBuckets),
891 l3ecmpGroupKey,
892 l3ecmpGroupId,
893 nextObj.appId());
894 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
895 l3ecmpGroupBuckets.size(),
Yi Tsengef19de12017-04-24 11:33:05 -0700896 false, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800897
898 // create objects for local and distributed storage
Yi Tsengef19de12017-04-24 11:33:05 -0700899 allGroupKeys.forEach(gKeyChain -> gKeyChain.addFirst(l3ecmpGroupKey));
Charles Chan188ebf52015-12-23 00:15:11 -0800900 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
901
902 // store l3ecmpGroupKey with the ofdpaGroupChain for the nextObjective
903 // that depends on it
904 updatePendingNextObjective(l3ecmpGroupKey, ofdpaGrp);
905
906 log.debug("Trying L3ECMP: device:{} gid:{} gkey:{} nextId:{}",
907 deviceId, Integer.toHexString(l3ecmpGroupId),
908 l3ecmpGroupKey, nextObj.id());
909 // finally we are ready to send the innermost groups
910 for (GroupInfo gi : unsentGroups) {
911 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -0700912 Integer.toHexString(gi.innerMostGroupDesc().givenGroupId()), deviceId);
913 updatePendingGroups(gi.nextGroupDesc().appCookie(), l3ecmpGce);
914 groupService.addGroup(gi.innerMostGroupDesc());
Charles Chan188ebf52015-12-23 00:15:11 -0800915 }
Charles Chan188ebf52015-12-23 00:15:11 -0800916 }
917
918 /**
919 * Creates group chains for all buckets in a hashed group, and stores the
920 * GroupInfos and GroupKeys for all the groups in the lists passed in, which
921 * should be empty.
922 * <p>
923 * Does not create the top level ECMP group. Does not actually send the
924 * groups to the groupService.
925 *
926 * @param nextObj the Next Objective with buckets that need to be converted
927 * to group chains
928 * @param allGroupKeys a list to store groupKey for each bucket-group-chain
929 * @param unsentGroups a list to store GroupInfo for each bucket-group-chain
930 */
Pier Ventre140a8942016-11-02 07:26:38 -0700931 protected void createHashBucketChains(NextObjective nextObj,
Yi Tsengef19de12017-04-24 11:33:05 -0700932 List<Deque<GroupKey>> allGroupKeys,
933 List<GroupInfo> unsentGroups) {
Charles Chan188ebf52015-12-23 00:15:11 -0800934 // break up hashed next objective to multiple groups
935 Collection<TrafficTreatment> buckets = nextObj.next();
936
937 for (TrafficTreatment bucket : buckets) {
938 //figure out how many labels are pushed in each bucket
939 int labelsPushed = 0;
940 MplsLabel innermostLabel = null;
941 for (Instruction ins : bucket.allInstructions()) {
942 if (ins.type() == Instruction.Type.L2MODIFICATION) {
943 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
944 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_PUSH) {
945 labelsPushed++;
946 }
947 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) {
948 if (innermostLabel == null) {
Yi Tsengef19de12017-04-24 11:33:05 -0700949 innermostLabel =
950 ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label();
Charles Chan188ebf52015-12-23 00:15:11 -0800951 }
952 }
953 }
954 }
955
Yi Tsengef19de12017-04-24 11:33:05 -0700956 Deque<GroupKey> gKeyChain = new ArrayDeque<>();
Saurav Dasa4020382018-02-14 14:14:54 -0800957 // here we only deal with 0 and 1 label push
Charles Chan188ebf52015-12-23 00:15:11 -0800958 if (labelsPushed == 0) {
Yi Tsengef19de12017-04-24 11:33:05 -0700959 GroupInfo noLabelGroupInfo;
Pier Ventre140a8942016-11-02 07:26:38 -0700960 TrafficSelector metaSelector = nextObj.meta();
961 if (metaSelector != null) {
962 if (isNotMplsBos(metaSelector)) {
Yi Tsengef19de12017-04-24 11:33:05 -0700963 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
964 nextObj.appId(), true,
965 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700966 } else {
Yi Tsengef19de12017-04-24 11:33:05 -0700967 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
968 nextObj.appId(), false,
969 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700970 }
971 } else {
Yi Tsengef19de12017-04-24 11:33:05 -0700972 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
973 nextObj.appId(), false,
974 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700975 }
Yi Tsengef19de12017-04-24 11:33:05 -0700976 if (noLabelGroupInfo == null) {
Charles Chan188ebf52015-12-23 00:15:11 -0800977 log.error("Could not process nextObj={} in dev:{}",
978 nextObj.id(), deviceId);
979 return;
980 }
Yi Tsengef19de12017-04-24 11:33:05 -0700981 gKeyChain.addFirst(noLabelGroupInfo.innerMostGroupDesc().appCookie());
982 gKeyChain.addFirst(noLabelGroupInfo.nextGroupDesc().appCookie());
Charles Chan188ebf52015-12-23 00:15:11 -0800983
984 // we can't send the inner group description yet, as we have to
985 // create the dependent ECMP group first. So we store..
Yi Tsengef19de12017-04-24 11:33:05 -0700986 unsentGroups.add(noLabelGroupInfo);
Charles Chan188ebf52015-12-23 00:15:11 -0800987
988 } else if (labelsPushed == 1) {
989 GroupInfo onelabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
990 nextObj.appId(), true,
991 nextObj.meta());
992 if (onelabelGroupInfo == null) {
993 log.error("Could not process nextObj={} in dev:{}",
994 nextObj.id(), deviceId);
995 return;
996 }
997 // we need to add another group to this chain - the L3VPN group
998 TrafficTreatment.Builder l3vpnTtb = DefaultTrafficTreatment.builder();
Charles Chan0f43e472017-02-14 14:00:16 -0800999 if (requireVlanPopBeforeMplsPush()) {
1000 l3vpnTtb.popVlan();
1001 }
Charles Chan188ebf52015-12-23 00:15:11 -08001002 l3vpnTtb.pushMpls()
1003 .setMpls(innermostLabel)
Yi Tsengef19de12017-04-24 11:33:05 -07001004 .group(new GroupId(onelabelGroupInfo.nextGroupDesc().givenGroupId()));
Charles Chan40132b32017-01-22 00:19:37 -08001005 if (supportCopyTtl()) {
1006 l3vpnTtb.copyTtlOut();
1007 }
1008 if (supportSetMplsBos()) {
1009 l3vpnTtb.setMplsBos(true);
1010 }
Charles Chan0f43e472017-02-14 14:00:16 -08001011 if (requireVlanPopBeforeMplsPush()) {
1012 l3vpnTtb.pushVlan().setVlanId(VlanId.vlanId(VlanId.RESERVED));
1013 }
Charles Chan40132b32017-01-22 00:19:37 -08001014
Charles Chan188ebf52015-12-23 00:15:11 -08001015 GroupBucket l3vpnGrpBkt =
1016 DefaultGroupBucket.createIndirectGroupBucket(l3vpnTtb.build());
Saurav Das8be4e3a2016-03-11 17:19:07 -08001017 int l3vpnIndex = getNextAvailableIndex();
Yi Tsengef19de12017-04-24 11:33:05 -07001018 int l3vpnGroupId = MPLS_L3VPN_SUBTYPE | (SUBTYPE_MASK & l3vpnIndex);
1019 GroupKey l3vpnGroupKey = new DefaultGroupKey(
1020 appKryo.serialize(l3vpnIndex));
Charles Chan188ebf52015-12-23 00:15:11 -08001021 GroupDescription l3vpnGroupDesc =
1022 new DefaultGroupDescription(
1023 deviceId,
1024 GroupDescription.Type.INDIRECT,
Yi Tsengef19de12017-04-24 11:33:05 -07001025 new GroupBuckets(Collections.singletonList(l3vpnGrpBkt)),
1026 l3vpnGroupKey,
1027 l3vpnGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -08001028 nextObj.appId());
Yi Tsengef19de12017-04-24 11:33:05 -07001029 GroupChainElem l3vpnGce = new GroupChainElem(l3vpnGroupDesc,
1030 1,
1031 false,
1032 deviceId);
1033 updatePendingGroups(onelabelGroupInfo.nextGroupDesc().appCookie(), l3vpnGce);
Charles Chan188ebf52015-12-23 00:15:11 -08001034
Yi Tsengef19de12017-04-24 11:33:05 -07001035 gKeyChain.addFirst(onelabelGroupInfo.innerMostGroupDesc().appCookie());
1036 gKeyChain.addFirst(onelabelGroupInfo.nextGroupDesc().appCookie());
1037 gKeyChain.addFirst(l3vpnGroupKey);
Charles Chan188ebf52015-12-23 00:15:11 -08001038
1039 //now we can replace the outerGrpDesc with the one we just created
Yi Tsengef19de12017-04-24 11:33:05 -07001040 onelabelGroupInfo.nextGroupDesc(l3vpnGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -08001041
1042 // we can't send the innermost group yet, as we have to create
1043 // the dependent ECMP group first. So we store ...
1044 unsentGroups.add(onelabelGroupInfo);
1045
Yi Tsengef19de12017-04-24 11:33:05 -07001046 log.debug("Trying L3VPN: device:{} gid:{} group key:{} nextId:{}",
1047 deviceId, Integer.toHexString(l3vpnGroupId),
1048 l3vpnGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -08001049
1050 } else {
1051 log.warn("Driver currently does not handle more than 1 MPLS "
1052 + "labels. Not processing nextObjective {}", nextObj.id());
1053 return;
1054 }
1055
1056 // all groups in this chain
Yi Tsengef19de12017-04-24 11:33:05 -07001057 allGroupKeys.add(gKeyChain);
Charles Chan188ebf52015-12-23 00:15:11 -08001058 }
1059 }
1060
Pier Ventre42287df2016-11-09 14:17:26 -08001061 /**
1062 * Processes the pseudo wire related next objective.
1063 * This procedure try to reuse the mpls label groups,
1064 * the mpls interface group and the l2 interface group.
1065 *
1066 * @param nextObjective the objective to process.
1067 */
1068 protected void processPwNextObjective(NextObjective nextObjective) {
Saurav Das1547b3f2017-05-05 17:01:08 -07001069 log.warn("Pseudo wire extensions are not supported in OFDPA 2.0 {}",
1070 nextObjective.id());
Pier Ventre42287df2016-11-09 14:17:26 -08001071 }
1072
Saurav Das8be4e3a2016-03-11 17:19:07 -08001073 //////////////////////////////////////
1074 // Group Editing
1075 //////////////////////////////////////
Charles Chan188ebf52015-12-23 00:15:11 -08001076 /**
1077 * Adds a bucket to the top level group of a group-chain, and creates the chain.
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001078 * Ensures that bucket being added is not a duplicate, by checking existing
Yi Tsengef19de12017-04-24 11:33:05 -07001079 * buckets for the same output port.
Charles Chan188ebf52015-12-23 00:15:11 -08001080 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001081 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -08001082 * @param next the representation of the existing group-chain for this next objective
1083 */
1084 protected void addBucketToGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001085 if (nextObjective.type() != NextObjective.Type.HASHED &&
1086 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001087 log.warn("AddBuckets not applied to nextType:{} in dev:{} for next:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001088 nextObjective.type(), deviceId, nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -07001089 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001090 return;
1091 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001092
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001093 // first check to see if bucket being added is not a duplicate of an
Yi Tsengef19de12017-04-24 11:33:05 -07001094 // existing bucket. If it is for an existing output port, then its a
1095 // duplicate.
Yi Tseng78f51f42017-02-02 13:54:58 -08001096 Set<TrafficTreatment> duplicateBuckets = Sets.newHashSet();
Yi Tsengef19de12017-04-24 11:33:05 -07001097 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
1098 Set<PortNumber> existingPorts = getExistingOutputPorts(allActiveKeys,
1099 groupService,
1100 deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -08001101 Set<TrafficTreatment> nonDuplicateBuckets = Sets.newHashSet();
1102 NextObjective objectiveToAdd;
Yi Tseng78f51f42017-02-02 13:54:58 -08001103
1104 nextObjective.next().forEach(trafficTreatment -> {
1105 PortNumber portNumber = readOutPortFromTreatment(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001106 if (portNumber == null) {
1107 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001108 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001109 if (existingPorts.contains(portNumber)) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001110 // its possible that portnumbers are same but labels are different
1111 int label = readLabelFromTreatment(trafficTreatment);
1112 if (label == -1) {
1113 duplicateBuckets.add(trafficTreatment);
1114 } else {
Saurav Dasceccf242017-08-03 18:30:35 -07001115 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1116 groupService, deviceId,
1117 portNumber, label);
1118 if (!existing.isEmpty()) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001119 duplicateBuckets.add(trafficTreatment);
1120 } else {
1121 nonDuplicateBuckets.add(trafficTreatment);
1122 }
1123 }
Yi Tseng47f82dc2017-03-05 22:48:39 -08001124 } else {
1125 nonDuplicateBuckets.add(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001126 }
1127 });
1128
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001129 if (duplicateBuckets.isEmpty()) {
1130 // use the original objective
1131 objectiveToAdd = nextObjective;
1132 } else if (!nonDuplicateBuckets.isEmpty()) {
1133 // only use the non-duplicate buckets if there are any
1134 log.debug("Some buckets {} already exist in next id {}, duplicate "
1135 + "buckets will be ignored.", duplicateBuckets, nextObjective.id());
1136 // new next objective with non duplicate treatments
Yi Tseng47f82dc2017-03-05 22:48:39 -08001137 NextObjective.Builder builder = DefaultNextObjective.builder()
1138 .withType(nextObjective.type())
1139 .withId(nextObjective.id())
1140 .withMeta(nextObjective.meta())
1141 .fromApp(nextObjective.appId());
1142 nonDuplicateBuckets.forEach(builder::addTreatment);
1143
1144 ObjectiveContext context = nextObjective.context().orElse(null);
1145 objectiveToAdd = builder.addToExisting(context);
1146 } else {
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001147 // buckets to add are already there - nothing to do
Saurav Das1547b3f2017-05-05 17:01:08 -07001148 log.debug("buckets already exist {} in next: {} ..ignoring bucket add",
1149 duplicateBuckets, nextObjective.id());
1150 pass(nextObjective);
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001151 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001152 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001153
Saurav Das1a129a02016-11-18 15:21:57 -08001154 if (nextObjective.type() == NextObjective.Type.HASHED) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001155 addBucketToHashGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001156 } else if (nextObjective.type() == NextObjective.Type.BROADCAST) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001157 addBucketToBroadcastGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001158 }
1159 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001160
Saurav Das1a129a02016-11-18 15:21:57 -08001161 private void addBucketToHashGroup(NextObjective nextObjective,
Yi Tseng78f51f42017-02-02 13:54:58 -08001162 List<Deque<GroupKey>> allActiveKeys) {
Charles Chan188ebf52015-12-23 00:15:11 -08001163 // storage for all group keys in the chain of groups created
1164 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
1165 List<GroupInfo> unsentGroups = new ArrayList<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001166 List<GroupBucket> newBuckets;
Charles Chan188ebf52015-12-23 00:15:11 -08001167 createHashBucketChains(nextObjective, allGroupKeys, unsentGroups);
1168
Yi Tseng78f51f42017-02-02 13:54:58 -08001169 // now we can create the buckets to add to the outermost L3 ECMP group
1170 newBuckets = generateNextGroupBuckets(unsentGroups, SELECT);
Charles Chan188ebf52015-12-23 00:15:11 -08001171
Saurav Das1a129a02016-11-18 15:21:57 -08001172 // retrieve the original L3 ECMP group
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001173 Group l3ecmpGroup = retrieveTopLevelGroup(allActiveKeys, deviceId,
1174 groupService, nextObjective.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001175 if (l3ecmpGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001176 fail(nextObjective, ObjectiveError.GROUPMISSING);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001177 return;
1178 }
Saurav Das1a129a02016-11-18 15:21:57 -08001179 GroupKey l3ecmpGroupKey = l3ecmpGroup.appCookie();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001180 int l3ecmpGroupId = l3ecmpGroup.id().id();
Charles Chan188ebf52015-12-23 00:15:11 -08001181
1182 // Although GroupDescriptions are not necessary for adding buckets to
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001183 // existing groups, we still use one in the GroupChainElem. When the latter is
Charles Chan188ebf52015-12-23 00:15:11 -08001184 // processed, the info will be extracted for the bucketAdd call to groupService
1185 GroupDescription l3ecmpGroupDesc =
Yi Tsengef19de12017-04-24 11:33:05 -07001186 new DefaultGroupDescription(deviceId,
1187 SELECT,
1188 new GroupBuckets(newBuckets),
1189 l3ecmpGroupKey,
1190 l3ecmpGroupId,
1191 nextObjective.appId());
Yi Tseng78f51f42017-02-02 13:54:58 -08001192 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
1193 unsentGroups.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001194 true,
1195 deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001196
Saurav Dasc88d4662017-05-15 15:34:25 -07001197 // update new bucket-chains
1198 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
1199 for (Deque<GroupKey> newBucketChain : allGroupKeys) {
1200 newBucketChain.addFirst(l3ecmpGroupKey);
1201 addedKeys.add(newBucketChain);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001202 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001203 updatePendingNextObjective(l3ecmpGroupKey,
1204 new OfdpaNextGroup(addedKeys, nextObjective));
Yi Tsengef19de12017-04-24 11:33:05 -07001205 log.debug("Adding to L3ECMP: device:{} gid:{} group key:{} nextId:{}",
Charles Chan188ebf52015-12-23 00:15:11 -08001206 deviceId, Integer.toHexString(l3ecmpGroupId),
1207 l3ecmpGroupKey, nextObjective.id());
Yi Tseng78f51f42017-02-02 13:54:58 -08001208
1209 unsentGroups.forEach(groupInfo -> {
1210 // send the innermost group
1211 log.debug("Sending innermost group {} in group chain on device {} ",
Saurav Dasc88d4662017-05-15 15:34:25 -07001212 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
1213 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001214 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3ecmpGce);
1215 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001216 });
Saurav Das1a129a02016-11-18 15:21:57 -08001217 }
Charles Chan188ebf52015-12-23 00:15:11 -08001218
Saurav Das1a129a02016-11-18 15:21:57 -08001219 private void addBucketToBroadcastGroup(NextObjective nextObj,
Yi Tsengef19de12017-04-24 11:33:05 -07001220 List<Deque<GroupKey>> allActiveKeys) {
1221 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001222 if (assignedVlan == null) {
1223 log.warn("VLAN ID required by broadcast next obj is missing. "
1224 + "Aborting add bucket to broadcast group for next:{} in dev:{}",
1225 nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001226 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001227 return;
1228 }
Saurav Das1a129a02016-11-18 15:21:57 -08001229 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Yi Tsengef19de12017-04-24 11:33:05 -07001230 IpPrefix ipDst = readIpDstFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001231 if (ipDst != null) {
1232 if (ipDst.isMulticast()) {
Yi Tsengef19de12017-04-24 11:33:05 -07001233 addBucketToL3MulticastGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001234 } else {
1235 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001236 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001237 }
1238 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001239 addBucketToL2FloodGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001240 }
1241 }
1242
1243 private void addBucketToL2FloodGroup(NextObjective nextObj,
1244 List<Deque<GroupKey>> allActiveKeys,
1245 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001246 VlanId assignedVlan) {
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001247 Group l2FloodGroup = retrieveTopLevelGroup(allActiveKeys, deviceId,
1248 groupService, nextObj.id());
Yi Tseng78f51f42017-02-02 13:54:58 -08001249
1250 if (l2FloodGroup == null) {
1251 log.warn("Can't find L2 flood group while adding bucket to it. NextObj = {}",
1252 nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001253 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001254 return;
1255 }
Saurav Das1a129a02016-11-18 15:21:57 -08001256
Yi Tseng78f51f42017-02-02 13:54:58 -08001257 GroupKey l2floodGroupKey = l2FloodGroup.appCookie();
1258 int l2floodGroupId = l2FloodGroup.id().id();
1259 List<GroupBucket> newBuckets = generateNextGroupBuckets(groupInfos, ALL);
1260
1261 GroupDescription l2FloodGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001262 new DefaultGroupDescription(deviceId,
1263 ALL,
1264 new GroupBuckets(newBuckets),
1265 l2floodGroupKey,
1266 l2floodGroupId,
1267 nextObj.appId());
Saurav Das1a129a02016-11-18 15:21:57 -08001268
Yi Tseng78f51f42017-02-02 13:54:58 -08001269 GroupChainElem l2FloodGroupChainElement =
1270 new GroupChainElem(l2FloodGroupDescription,
1271 groupInfos.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001272 true,
1273 deviceId);
Yi Tseng78f51f42017-02-02 13:54:58 -08001274
Yi Tseng78f51f42017-02-02 13:54:58 -08001275
1276 //ensure assignedVlan applies to the chosen group
1277 VlanId floodGroupVlan = extractVlanIdFromGroupId(l2floodGroupId);
1278
1279 if (!floodGroupVlan.equals(assignedVlan)) {
1280 log.warn("VLAN ID {} does not match Flood group {} to which bucket is "
1281 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1282 Integer.toHexString(l2floodGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001283 fail(nextObj, ObjectiveError.BADPARAMS);
Yi Tseng78f51f42017-02-02 13:54:58 -08001284 return;
1285 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001286 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
Yi Tseng78f51f42017-02-02 13:54:58 -08001287 groupInfos.forEach(groupInfo -> {
1288 // update original NextGroup with new bucket-chain
Yi Tseng78f51f42017-02-02 13:54:58 -08001289 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001290 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001291 newBucketChain.addFirst(l2floodGroupKey);
Saurav Dasc88d4662017-05-15 15:34:25 -07001292 addedKeys.add(newBucketChain);
Yi Tseng78f51f42017-02-02 13:54:58 -08001293
Yi Tsengef19de12017-04-24 11:33:05 -07001294 log.debug("Adding to L2FLOOD: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001295 deviceId, Integer.toHexString(l2floodGroupId),
1296 l2floodGroupKey, nextObj.id());
1297 // send the innermost group
1298 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001299 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001300 deviceId);
1301
Yi Tsengef19de12017-04-24 11:33:05 -07001302 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l2FloodGroupChainElement);
Yi Tseng78f51f42017-02-02 13:54:58 -08001303
Yi Tsengef19de12017-04-24 11:33:05 -07001304 DeviceId innerMostGroupDevice = groupInfo.innerMostGroupDesc().deviceId();
1305 GroupKey innerMostGroupKey = groupInfo.innerMostGroupDesc().appCookie();
Yi Tseng78f51f42017-02-02 13:54:58 -08001306 Group existsL2IGroup = groupService.getGroup(innerMostGroupDevice, innerMostGroupKey);
1307
1308 if (existsL2IGroup != null) {
1309 // group already exist
1310 processPendingAddGroupsOrNextObjs(innerMostGroupKey, true);
1311 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001312 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001313 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001314 });
Saurav Dasc88d4662017-05-15 15:34:25 -07001315
1316 updatePendingNextObjective(l2floodGroupKey,
1317 new OfdpaNextGroup(addedKeys, nextObj));
Yi Tseng78f51f42017-02-02 13:54:58 -08001318 }
1319
Saurav Das1a129a02016-11-18 15:21:57 -08001320 private void addBucketToL3MulticastGroup(NextObjective nextObj,
1321 List<Deque<GroupKey>> allActiveKeys,
1322 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001323 VlanId assignedVlan) {
Pier Luigi21fffd22018-01-19 10:24:53 +01001324 // Create the buckets to add to the outermost L3 Multicast group
1325 List<GroupBucket> newBuckets = createL3MulticastBucket(groupInfos);
Saurav Das1a129a02016-11-18 15:21:57 -08001326
1327 // get the group being edited
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001328 Group l3mcastGroup = retrieveTopLevelGroup(allActiveKeys, deviceId,
1329 groupService, nextObj.id());
Saurav Das1a129a02016-11-18 15:21:57 -08001330 if (l3mcastGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001331 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001332 return;
1333 }
1334 GroupKey l3mcastGroupKey = l3mcastGroup.appCookie();
1335 int l3mcastGroupId = l3mcastGroup.id().id();
1336
1337 //ensure assignedVlan applies to the chosen group
Yi Tseng78f51f42017-02-02 13:54:58 -08001338 VlanId expectedVlan = extractVlanIdFromGroupId(l3mcastGroupId);
Saurav Das1a129a02016-11-18 15:21:57 -08001339 if (!expectedVlan.equals(assignedVlan)) {
1340 log.warn("VLAN ID {} does not match L3 Mcast group {} to which bucket is "
1341 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1342 Integer.toHexString(l3mcastGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001343 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001344 }
1345 GroupDescription l3mcastGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001346 new DefaultGroupDescription(deviceId,
1347 ALL,
1348 new GroupBuckets(newBuckets),
1349 l3mcastGroupKey,
1350 l3mcastGroupId,
1351 nextObj.appId());
Saurav Das1a129a02016-11-18 15:21:57 -08001352 GroupChainElem l3mcastGce = new GroupChainElem(l3mcastGroupDescription,
Yi Tsengef19de12017-04-24 11:33:05 -07001353 groupInfos.size(),
1354 true,
1355 deviceId);
Saurav Dasc88d4662017-05-15 15:34:25 -07001356
1357 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
Yi Tseng78f51f42017-02-02 13:54:58 -08001358 groupInfos.forEach(groupInfo -> {
1359 // update original NextGroup with new bucket-chain
1360 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001361 newBucketChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001362 // Add L3 interface group to the chain if there is one.
Yi Tsengef19de12017-04-24 11:33:05 -07001363 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1364 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001365 }
1366 newBucketChain.addFirst(l3mcastGroupKey);
Saurav Dasc88d4662017-05-15 15:34:25 -07001367 addedKeys.add(newBucketChain);
Yi Tseng78f51f42017-02-02 13:54:58 -08001368
Yi Tsengef19de12017-04-24 11:33:05 -07001369 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3mcastGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001370 // Point next group to inner-most group, if any
Yi Tsengef19de12017-04-24 11:33:05 -07001371 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1372 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(),
1373 1,
1374 false,
1375 deviceId);
1376 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001377 }
Yi Tsengef19de12017-04-24 11:33:05 -07001378 log.debug("Adding to L3MCAST: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001379 deviceId, Integer.toHexString(l3mcastGroupId),
1380 l3mcastGroupKey, nextObj.id());
1381 // send the innermost group
1382 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001383 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001384 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001385 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001386
1387 });
1388
Saurav Das1a129a02016-11-18 15:21:57 -08001389 updatePendingNextObjective(l3mcastGroupKey,
Saurav Dasc88d4662017-05-15 15:34:25 -07001390 new OfdpaNextGroup(addedKeys, nextObj));
Charles Chan188ebf52015-12-23 00:15:11 -08001391 }
1392
1393 /**
Saurav Dasceccf242017-08-03 18:30:35 -07001394 * Removes buckets in the top level group of a possible group-chain. Does
Saurav Das1a129a02016-11-18 15:21:57 -08001395 * not remove the groups in the group-chain pointed to by this bucket, as they
Charles Chan188ebf52015-12-23 00:15:11 -08001396 * may be in use (referenced by other groups) elsewhere.
1397 *
Saurav Dasceccf242017-08-03 18:30:35 -07001398 * @param nextObjective a next objective that contains information for the
1399 * buckets to be removed from the group
1400 * @param next the representation of the existing group-chains for this next
1401 * objective, from which the top-level buckets to remove are determined
Charles Chan188ebf52015-12-23 00:15:11 -08001402 */
1403 protected void removeBucketFromGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001404 if (nextObjective.type() != NextObjective.Type.HASHED &&
1405 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001406 log.warn("RemoveBuckets not applied to nextType:{} in dev:{} for next:{}",
1407 nextObjective.type(), deviceId, nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -07001408 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001409 return;
1410 }
Yi Tsengef19de12017-04-24 11:33:05 -07001411 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Saurav Dasceccf242017-08-03 18:30:35 -07001412 List<Integer> indicesToRemove = Lists.newArrayList();
1413 for (TrafficTreatment treatment : nextObjective.next()) {
1414 // find the top-level bucket in the group-chain by matching the
1415 // outport and label from different groups in the chain
1416 PortNumber portToRemove = readOutPortFromTreatment(treatment);
1417 int labelToRemove = readLabelFromTreatment(treatment);
1418 if (portToRemove == null) {
1419 log.warn("treatment {} of next objective {} has no outport.. "
1420 + "cannot remove bucket from group in dev: {}", treatment,
1421 nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001422 continue;
1423 }
Saurav Dasceccf242017-08-03 18:30:35 -07001424 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1425 groupService, deviceId,
1426 portToRemove, labelToRemove);
1427 indicesToRemove.addAll(existing);
1428
Charles Chan188ebf52015-12-23 00:15:11 -08001429 }
Saurav Dasceccf242017-08-03 18:30:35 -07001430
1431 List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList();
1432 indicesToRemove.forEach(index -> chainsToRemove
1433 .add(allActiveKeys.get(index)));
Yi Tseng78f51f42017-02-02 13:54:58 -08001434 if (chainsToRemove.isEmpty()) {
Charles Chan188ebf52015-12-23 00:15:11 -08001435 log.warn("Could not find appropriate group-chain for removing bucket"
1436 + " for next id {} in dev:{}", nextObjective.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001437 fail(nextObjective, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001438 return;
Charles Chan188ebf52015-12-23 00:15:11 -08001439 }
Saurav Dasceccf242017-08-03 18:30:35 -07001440 removeBucket(chainsToRemove, nextObjective);
1441 }
1442
1443 /**
1444 * Removes top-level buckets from a group that represents the given next objective.
1445 *
1446 * @param chainsToRemove a list of group bucket chains to remove
1447 * @param nextObjective the next objective that contains information for the
1448 * buckets to be removed from the group
1449 */
1450 protected void removeBucket(List<Deque<GroupKey>> chainsToRemove,
1451 NextObjective nextObjective) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001452 List<GroupBucket> bucketsToRemove = Lists.newArrayList();
1453 //first group key is the one we want to modify
1454 GroupKey modGroupKey = chainsToRemove.get(0).peekFirst();
Saurav Das1a129a02016-11-18 15:21:57 -08001455 Group modGroup = groupService.getGroup(deviceId, modGroupKey);
Yi Tseng78f51f42017-02-02 13:54:58 -08001456 for (Deque<GroupKey> foundChain : chainsToRemove) {
1457 //second group key is the one we wish to remove the reference to
1458 if (foundChain.size() < 2) {
Saurav Dasceccf242017-08-03 18:30:35 -07001459 // additional check to make sure second group key exists in
Yi Tseng78f51f42017-02-02 13:54:58 -08001460 // the chain.
1461 log.warn("Can't find second group key from chain {}",
1462 foundChain);
1463 continue;
Saurav Das1a129a02016-11-18 15:21:57 -08001464 }
Saurav Dasceccf242017-08-03 18:30:35 -07001465 GroupKey pointedGroupKey = foundChain.stream()
1466 .collect(Collectors.toList()).get(1);
Yi Tseng78f51f42017-02-02 13:54:58 -08001467 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
Yi Tseng78f51f42017-02-02 13:54:58 -08001468 if (pointedGroup == null) {
1469 continue;
1470 }
1471
1472 GroupBucket bucket;
1473 if (nextObjective.type() == NextObjective.Type.HASHED) {
1474 bucket = DefaultGroupBucket.createSelectGroupBucket(
1475 DefaultTrafficTreatment.builder()
1476 .group(pointedGroup.id())
1477 .build());
1478 } else {
1479 bucket = DefaultGroupBucket.createAllGroupBucket(
1480 DefaultTrafficTreatment.builder()
1481 .group(pointedGroup.id())
1482 .build());
1483 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001484 bucketsToRemove.add(bucket);
Saurav Das1a129a02016-11-18 15:21:57 -08001485 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001486
1487 GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove);
1488 List<String> pointedGroupIds; // for debug log
1489 pointedGroupIds = bucketsToRemove.stream()
1490 .map(GroupBucket::treatment)
1491 .map(TrafficTreatment::allInstructions)
1492 .flatMap(List::stream)
1493 .filter(inst -> inst instanceof Instructions.GroupInstruction)
1494 .map(inst -> (Instructions.GroupInstruction) inst)
1495 .map(Instructions.GroupInstruction::groupId)
1496 .map(GroupId::id)
1497 .map(Integer::toHexString)
1498 .map(id -> HEX_PREFIX + id)
1499 .collect(Collectors.toList());
1500
Yi Tseng78f51f42017-02-02 13:54:58 -08001501 log.debug("Removing buckets from group id 0x{} pointing to group id(s) {} "
Saurav Das1a129a02016-11-18 15:21:57 -08001502 + "for next id {} in device {}", Integer.toHexString(modGroup.id().id()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001503 pointedGroupIds, nextObjective.id(), deviceId);
1504 addPendingUpdateNextObjective(modGroupKey, nextObjective);
Saurav Das1a129a02016-11-18 15:21:57 -08001505 groupService.removeBucketsFromGroup(deviceId, modGroupKey,
1506 removeBuckets, modGroupKey,
1507 nextObjective.appId());
Saurav Dasceccf242017-08-03 18:30:35 -07001508 // update store - synchronize access as there may be multiple threads
1509 // trying to remove buckets from the same group, each with its own
1510 // potentially stale copy of allActiveKeys
Saurav Dasc88d4662017-05-15 15:34:25 -07001511 synchronized (flowObjectiveStore) {
Saurav Dasceccf242017-08-03 18:30:35 -07001512 // get a fresh copy of what the store holds
1513 NextGroup next = flowObjectiveStore.getNextGroup(nextObjective.id());
1514 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Pier Luigid008dbe2018-01-15 09:48:49 +01001515 allActiveKeys = Lists.newArrayList(allActiveKeys);
Saurav Dasc88d4662017-05-15 15:34:25 -07001516 // Note that since we got a new object, and ArrayDeque does not implement
Saurav Dasceccf242017-08-03 18:30:35 -07001517 // Object.equals(), we have to check the deque elems one by one
1518 allActiveKeys
1519 .removeIf(active ->
1520 chainsToRemove.stream().anyMatch(remove ->
1521 Arrays.equals(remove.toArray(new GroupKey[0]),
1522 active.toArray(new GroupKey[0]))));
Saurav Dasc88d4662017-05-15 15:34:25 -07001523 // If no buckets in the group, then retain an entry for the
1524 // top level group which still exists.
1525 if (allActiveKeys.isEmpty()) {
1526 ArrayDeque<GroupKey> top = new ArrayDeque<>();
1527 top.add(modGroupKey);
1528 allActiveKeys.add(top);
1529 }
1530 flowObjectiveStore.putNextGroup(nextObjective.id(),
Saurav Dasceccf242017-08-03 18:30:35 -07001531 new OfdpaNextGroup(allActiveKeys,
1532 nextObjective));
Saurav Das1a129a02016-11-18 15:21:57 -08001533 }
Charles Chan188ebf52015-12-23 00:15:11 -08001534 }
1535
1536 /**
Saurav Das961beb22017-03-29 19:09:17 -07001537 * Removes all groups in multiple possible group-chains that represent the next-obj.
Charles Chan188ebf52015-12-23 00:15:11 -08001538 *
1539 * @param nextObjective the next objective to remove
1540 * @param next the NextGroup that represents the existing group-chain for
1541 * this next objective
1542 */
1543 protected void removeGroup(NextObjective nextObjective, NextGroup next) {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -07001544
Yi Tsengef19de12017-04-24 11:33:05 -07001545 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Charles Chanfc5c7802016-05-17 13:13:55 -07001546
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001547 List<GroupKey> groupKeys = allActiveKeys.stream()
Charles Chanfc5c7802016-05-17 13:13:55 -07001548 .map(Deque::getFirst).collect(Collectors.toList());
Yi Tsengef19de12017-04-24 11:33:05 -07001549 addPendingRemoveNextObjective(nextObjective, groupKeys);
Charles Chanfc5c7802016-05-17 13:13:55 -07001550
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -07001551 allActiveKeys
1552 .forEach(groupChain -> groupChain.forEach(groupKey -> groupService
1553 .removeGroup(deviceId, groupKey, nextObjective.appId())));
Charles Chan188ebf52015-12-23 00:15:11 -08001554 flowObjectiveStore.removeNextGroup(nextObjective.id());
1555 }
1556
Saurav Dasceccf242017-08-03 18:30:35 -07001557 /**
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001558 * Modify buckets in the L2 interface group.
1559 *
1560 * @param nextObjective a next objective that contains information for the
1561 * buckets to be modified in the group
1562 * @param next the representation of the existing group-chains for this next
1563 * objective, from which the innermost group buckets to remove are determined
1564 */
1565 protected void modifyBucketFromGroup(NextObjective nextObjective, NextGroup next) {
1566 if (nextObjective.type() != NextObjective.Type.SIMPLE) {
1567 log.warn("ModifyBucketFromGroup cannot be applied to nextType:{} in dev:{} for next:{}",
1568 nextObjective.type(), deviceId, nextObjective.id());
1569 fail(nextObjective, ObjectiveError.UNSUPPORTED);
1570 return;
1571 }
1572
1573 VlanId assignedVlan = readVlanFromSelector(nextObjective.meta());
1574 if (assignedVlan == null) {
1575 log.warn("VLAN ID required by simple next obj is missing. Abort.");
1576 fail(nextObjective, ObjectiveError.BADPARAMS);
1577 return;
1578 }
1579
1580 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObjective, assignedVlan);
1581
1582 // There is only one L2 interface group in this case
1583 GroupDescription l2InterfaceGroupDesc = groupInfos.get(0).innerMostGroupDesc();
1584
1585 // Replace group bucket for L2 interface group
1586 groupService.setBucketsForGroup(deviceId,
1587 l2InterfaceGroupDesc.appCookie(),
1588 l2InterfaceGroupDesc.buckets(),
1589 l2InterfaceGroupDesc.appCookie(),
1590 l2InterfaceGroupDesc.appId());
1591
1592 // update store - synchronize access as there may be multiple threads
1593 // trying to remove buckets from the same group, each with its own
1594 // potentially stale copy of allActiveKeys
1595 synchronized (flowObjectiveStore) {
1596 List<Deque<GroupKey>> modifiedGroupKeys = Lists.newArrayList();
1597 ArrayDeque<GroupKey> top = new ArrayDeque<>();
1598 top.add(l2InterfaceGroupDesc.appCookie());
1599 modifiedGroupKeys.add(top);
1600
1601 flowObjectiveStore.putNextGroup(nextObjective.id(),
1602 new OfdpaNextGroup(modifiedGroupKeys,
1603 nextObjective));
1604 }
1605 }
1606
1607 /**
Saurav Dasceccf242017-08-03 18:30:35 -07001608 * Checks existing buckets in {@link NextGroup} to verify if they match
1609 * the buckets in the given {@link NextObjective}. Adds or removes buckets
1610 * to ensure that the buckets match up.
1611 *
1612 * @param nextObjective the next objective to verify
1613 * @param next the representation of the existing group which has to be
1614 * modified to match the given next objective
1615 */
1616 protected void verifyGroup(NextObjective nextObjective, NextGroup next) {
Pier Luigib72201b2018-01-25 16:16:02 +01001617 if (nextObjective.type() == NextObjective.Type.SIMPLE) {
1618 log.warn("verification not supported for indirect group");
Saurav Dasceccf242017-08-03 18:30:35 -07001619 fail(nextObjective, ObjectiveError.UNSUPPORTED);
1620 return;
1621 }
1622 log.debug("Call to verify device:{} nextId:{}", deviceId, nextObjective.id());
1623 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
1624 List<TrafficTreatment> bucketsToCreate = Lists.newArrayList();
1625 List<Integer> indicesToRemove = Lists.newArrayList();
Pier Luigiec6ac422018-01-29 10:30:59 +01001626
1627 // Iterating over the treatments of the next objective allows
1628 // to detect missing buckets and/or duplicate buckets (to be removed)
Saurav Dasceccf242017-08-03 18:30:35 -07001629 for (TrafficTreatment bkt : nextObjective.next()) {
1630 PortNumber portNumber = readOutPortFromTreatment(bkt);
1631 int label = readLabelFromTreatment(bkt);
1632 if (portNumber == null) {
1633 log.warn("treatment {} of next objective {} has no outport.. "
1634 + "cannot remove bucket from group in dev: {}", bkt,
1635 nextObjective.id(), deviceId);
1636 fail(nextObjective, ObjectiveError.BADPARAMS);
1637 return;
1638 }
1639 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1640 groupService, deviceId,
1641 portNumber, label);
1642 if (existing.isEmpty()) {
1643 // if it doesn't exist, mark this bucket for creation
1644 bucketsToCreate.add(bkt);
1645 }
1646 if (existing.size() > 1) {
1647 // if it exists but there are duplicates, mark the others for removal
1648 existing.remove(0);
1649 indicesToRemove.addAll(existing);
1650 }
1651 }
1652
Pier Luigiec6ac422018-01-29 10:30:59 +01001653 // Detect situation where the next data has more buckets
1654 // (not duplicates) respect to the next objective
Saurav Dasa4020382018-02-14 14:14:54 -08001655 if (allActiveKeys.size() > nextObjective.next().size() &&
1656 // ignore specific case of empty group
1657 !(nextObjective.next().size() == 0 && allActiveKeys.size() == 1
1658 && allActiveKeys.get(0).size() == 1)) {
Pier Luigiec6ac422018-01-29 10:30:59 +01001659 log.warn("Mismatch detected between next and flowobjstore for device {}: " +
Saurav Dasa4020382018-02-14 14:14:54 -08001660 "nextId:{}, nextObjective-size:{} next-size:{} .. correcting",
1661 deviceId, nextObjective.id(), nextObjective.next().size(),
1662 allActiveKeys.size());
1663 List<Integer> otherIndices =
1664 indicesToRemoveFromNextGroup(allActiveKeys, nextObjective,
1665 groupService, deviceId);
Pier Luigiec6ac422018-01-29 10:30:59 +01001666 // Filter out the indices not present
1667 otherIndices = otherIndices.stream()
1668 .filter(index -> !indicesToRemove.contains(index))
1669 .collect(Collectors.toList());
1670 // Add all to the final list
1671 indicesToRemove.addAll(otherIndices);
1672 }
1673
Pier Luigib72201b2018-01-25 16:16:02 +01001674 log.debug("Buckets to create {}", bucketsToCreate);
1675 log.debug("Indices to remove {}", indicesToRemove);
1676
Saurav Dasceccf242017-08-03 18:30:35 -07001677 if (!bucketsToCreate.isEmpty()) {
1678 log.info("creating {} buckets as part of nextId: {} verification",
1679 bucketsToCreate.size(), nextObjective.id());
1680 //create a nextObjective only with these buckets
1681 NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder()
1682 .withId(nextObjective.id())
Pier Luigib72201b2018-01-25 16:16:02 +01001683 .withType(nextObjective.type())
Saurav Dasceccf242017-08-03 18:30:35 -07001684 .withMeta(nextObjective.meta())
1685 .fromApp(nextObjective.appId());
Pier Luigib72201b2018-01-25 16:16:02 +01001686 bucketsToCreate.forEach(nextObjBuilder::addTreatment);
1687 // According to the next type we call the proper add function
1688 if (nextObjective.type() == NextObjective.Type.HASHED) {
1689 addBucketToHashGroup(nextObjBuilder.addToExisting(), allActiveKeys);
1690 } else {
1691 addBucketToBroadcastGroup(nextObjBuilder.addToExisting(), allActiveKeys);
1692 }
Saurav Dasceccf242017-08-03 18:30:35 -07001693 }
1694
1695 if (!indicesToRemove.isEmpty()) {
1696 log.info("removing {} buckets as part of nextId: {} verification",
1697 indicesToRemove.size(), nextObjective.id());
1698 List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList();
1699 indicesToRemove.forEach(index -> chainsToRemove
1700 .add(allActiveKeys.get(index)));
1701 removeBucket(chainsToRemove, nextObjective);
1702 }
1703
Saurav Das9df5b7c2017-08-14 16:44:43 -07001704 if (bucketsToCreate.isEmpty() && indicesToRemove.isEmpty()) {
1705 // flowObjective store record is in-sync with nextObjective passed-in
1706 // Nevertheless groupStore may not be in sync due to bug in the store
1707 // - see CORD-1844. XXX When this bug is fixed, the rest of this verify
1708 // method will not be required.
Pier Luigib72201b2018-01-25 16:16:02 +01001709 GroupKey topGroupKey = allActiveKeys.get(0).peekFirst();
1710 Group topGroup = groupService.getGroup(deviceId, topGroupKey);
1711 int actualGroupSize = topGroup.buckets().buckets().size();
Saurav Das9df5b7c2017-08-14 16:44:43 -07001712 int objGroupSize = nextObjective.next().size();
1713 if (actualGroupSize != objGroupSize) {
1714 log.warn("Mismatch detected in device:{}, nextId:{}, nextObjective-size"
1715 + ":{} group-size:{} .. correcting", deviceId, nextObjective.id(),
1716 objGroupSize, actualGroupSize);
1717 }
1718 if (actualGroupSize > objGroupSize) {
Pier Luigib72201b2018-01-25 16:16:02 +01001719 // Group in the device has more chains
Saurav Das9df5b7c2017-08-14 16:44:43 -07001720 List<GroupBucket> bucketsToRemove = Lists.newArrayList();
1721 //check every bucket in the actual group
Pier Luigib72201b2018-01-25 16:16:02 +01001722 for (GroupBucket bucket : topGroup.buckets().buckets()) {
Saurav Das9df5b7c2017-08-14 16:44:43 -07001723 GroupInstruction g = (GroupInstruction) bucket.treatment()
1724 .allInstructions().iterator().next();
1725 GroupId gidToCheck = g.groupId(); // the group pointed to
1726 boolean matches = false;
1727 for (Deque<GroupKey> validChain : allActiveKeys) {
1728 if (validChain.size() < 2) {
1729 continue;
1730 }
1731 GroupKey pointedGroupKey = validChain.stream()
1732 .collect(Collectors.toList()).get(1);
1733 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
1734 if (pointedGroup != null && gidToCheck.equals(pointedGroup.id())) {
1735 matches = true;
1736 break;
1737 }
1738 }
1739 if (!matches) {
1740 log.warn("Removing bucket pointing to groupId:{}", gidToCheck);
1741 bucketsToRemove.add(bucket);
1742 }
1743 }
1744 // remove buckets for which there was no record in the obj store
1745 if (bucketsToRemove.isEmpty()) {
1746 log.warn("Mismatch detected but could not determine which"
1747 + "buckets to remove");
1748 } else {
1749 GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove);
Pier Luigib72201b2018-01-25 16:16:02 +01001750 groupService.removeBucketsFromGroup(deviceId, topGroupKey,
1751 removeBuckets, topGroupKey,
Saurav Das9df5b7c2017-08-14 16:44:43 -07001752 nextObjective.appId());
1753 }
1754 } else if (actualGroupSize < objGroupSize) {
Pier Luigib72201b2018-01-25 16:16:02 +01001755 // Group in the device has less chains
Saurav Das9df5b7c2017-08-14 16:44:43 -07001756 // should also add buckets not in group-store but in obj-store
1757 List<GroupBucket> bucketsToAdd = Lists.newArrayList();
1758 //check every bucket in the obj
1759 for (Deque<GroupKey> validChain : allActiveKeys) {
1760 if (validChain.size() < 2) {
1761 continue;
1762 }
1763 GroupKey pointedGroupKey = validChain.stream()
1764 .collect(Collectors.toList()).get(1);
1765 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
1766 if (pointedGroup == null) {
1767 // group should exist, otherwise cannot be added as bucket
1768 continue;
1769 }
1770 boolean matches = false;
Pier Luigib72201b2018-01-25 16:16:02 +01001771 for (GroupBucket bucket : topGroup.buckets().buckets()) {
Saurav Das9df5b7c2017-08-14 16:44:43 -07001772 GroupInstruction g = (GroupInstruction) bucket.treatment()
1773 .allInstructions().iterator().next();
1774 GroupId gidToCheck = g.groupId(); // the group pointed to
1775 if (pointedGroup.id().equals(gidToCheck)) {
1776 matches = true;
1777 break;
1778 }
1779 }
1780 if (!matches) {
1781 log.warn("Adding bucket pointing to groupId:{}", pointedGroup);
1782 TrafficTreatment t = DefaultTrafficTreatment.builder()
1783 .group(pointedGroup.id())
1784 .build();
Pier Luigib72201b2018-01-25 16:16:02 +01001785 // Create the proper bucket according to the next type
1786 if (nextObjective.type() == NextObjective.Type.HASHED) {
1787 bucketsToAdd.add(DefaultGroupBucket.createSelectGroupBucket(t));
1788 } else {
1789 bucketsToAdd.add(DefaultGroupBucket.createAllGroupBucket(t));
1790 }
Saurav Das9df5b7c2017-08-14 16:44:43 -07001791 }
1792 }
1793 if (bucketsToAdd.isEmpty()) {
1794 log.warn("Mismatch detected but could not determine which "
1795 + "buckets to add");
1796 } else {
1797 GroupBuckets addBuckets = new GroupBuckets(bucketsToAdd);
Pier Luigib72201b2018-01-25 16:16:02 +01001798 groupService.addBucketsToGroup(deviceId, topGroupKey,
1799 addBuckets, topGroupKey,
Saurav Das9df5b7c2017-08-14 16:44:43 -07001800 nextObjective.appId());
1801 }
1802 }
1803 }
1804
Saurav Dasceccf242017-08-03 18:30:35 -07001805 pass(nextObjective);
1806 }
1807
1808 //////////////////////////////////////
1809 // Helper methods and classes
1810 //////////////////////////////////////
1811
Yi Tsengef19de12017-04-24 11:33:05 -07001812 protected void updatePendingNextObjective(GroupKey groupKey, OfdpaNextGroup nextGrp) {
1813 pendingAddNextObjectives.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07001814 if (val == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001815 val = new CopyOnWriteArrayList<>();
Saurav Das961beb22017-03-29 19:09:17 -07001816 }
Yi Tsengef19de12017-04-24 11:33:05 -07001817 val.add(nextGrp);
Saurav Das961beb22017-03-29 19:09:17 -07001818 return val;
1819 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08001820 }
1821
Yi Tsengef19de12017-04-24 11:33:05 -07001822 protected void updatePendingGroups(GroupKey groupKey, GroupChainElem gce) {
1823 pendingGroups.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07001824 if (val == null) {
1825 val = Sets.newConcurrentHashSet();
1826 }
1827 val.add(gce);
1828 return val;
1829 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08001830 }
1831
Yi Tsengef19de12017-04-24 11:33:05 -07001832 protected void addPendingUpdateNextObjective(GroupKey groupKey,
1833 NextObjective nextObjective) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001834 pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> {
1835 if (nextObjs != null) {
1836 nextObjs.add(nextObjective);
1837 } else {
1838 nextObjs = Sets.newHashSet(nextObjective);
1839 }
1840 return nextObjs;
1841 });
1842 }
1843
Saurav Das2f2c9d02018-04-07 16:51:09 -07001844 protected void addPendingRemoveNextObjective(NextObjective nextObjective,
1845 List<GroupKey> groupKeys) {
1846 pendingRemoveNextObjectives.put(nextObjective, groupKeys);
Yi Tsengef19de12017-04-24 11:33:05 -07001847 }
1848
Saurav Das2f2c9d02018-04-07 16:51:09 -07001849 protected int getNextAvailableIndex() {
1850 return (int) nextIndex.incrementAndGet();
1851 }
1852
1853 protected void processPendingUpdateNextObjs(GroupKey groupKey) {
1854 Set<NextObjective> nextObjs = pendingUpdateNextObjectives.remove(groupKey);
1855 if (nextObjs != null) {
1856 nextObjs.forEach(nextObj -> {
1857 log.debug("Group {} updated, update pending next objective {}.",
1858 groupKey, nextObj);
1859 pass(nextObj);
1860 });
1861 }
1862 }
1863
1864 protected void processPendingRemoveNextObjs(GroupKey key) {
Yi Tsengef19de12017-04-24 11:33:05 -07001865 pendingRemoveNextObjectives.asMap().forEach((nextObjective, groupKeys) -> {
1866 if (groupKeys.isEmpty()) {
1867 pendingRemoveNextObjectives.invalidate(nextObjective);
1868 pass(nextObjective);
1869 } else {
1870 groupKeys.remove(key);
1871 }
1872 });
1873 }
1874
Yi Tsengef19de12017-04-24 11:33:05 -07001875 protected void processPendingAddGroupsOrNextObjs(GroupKey key, boolean added) {
1876 //first check for group chain
1877 Set<OfdpaGroupHandlerUtility.GroupChainElem> gceSet = pendingGroups.asMap().remove(key);
1878 if (gceSet != null) {
1879 for (GroupChainElem gce : gceSet) {
1880 log.debug("Group service {} group key {} in device {}. "
1881 + "Processing next group in group chain with group id 0x{}",
1882 (added) ? "ADDED" : "processed",
1883 key, deviceId,
1884 Integer.toHexString(gce.groupDescription().givenGroupId()));
1885 processGroupChain(gce);
1886 }
1887 } else {
1888 // otherwise chain complete - check for waiting nextObjectives
1889 List<OfdpaGroupHandlerUtility.OfdpaNextGroup> nextGrpList =
1890 pendingAddNextObjectives.getIfPresent(key);
1891 if (nextGrpList != null) {
1892 pendingAddNextObjectives.invalidate(key);
1893 nextGrpList.forEach(nextGrp -> {
1894 log.debug("Group service {} group key {} in device:{}. "
1895 + "Done implementing next objective: {} <<-->> gid:0x{}",
1896 (added) ? "ADDED" : "processed",
1897 key, deviceId, nextGrp.nextObjective().id(),
1898 Integer.toHexString(groupService.getGroup(deviceId, key)
1899 .givenGroupId()));
1900 pass(nextGrp.nextObjective());
Saurav Dasc88d4662017-05-15 15:34:25 -07001901 updateFlowObjectiveStore(nextGrp.nextObjective().id(), nextGrp);
Yi Tsengef19de12017-04-24 11:33:05 -07001902
1903 // check if addBuckets waiting for this completion
1904 pendingBuckets.compute(nextGrp.nextObjective().id(), (nextId, pendBkts) -> {
1905 if (pendBkts != null) {
1906 pendBkts.forEach(pendBkt -> addBucketToGroup(pendBkt, nextGrp));
1907 }
1908 return null;
1909 });
1910 });
1911 }
1912 }
1913 }
1914
Charles Chan188ebf52015-12-23 00:15:11 -08001915 /**
1916 * Processes next element of a group chain. Assumption is that if this
1917 * group points to another group, the latter has already been created
1918 * and this driver has received notification for it. A second assumption is
1919 * that if there is another group waiting for this group then the appropriate
1920 * stores already have the information to act upon the notification for the
1921 * creation of this group.
1922 * <p>
1923 * The processing of the GroupChainElement depends on the number of groups
1924 * this element is waiting on. For all group types other than SIMPLE, a
1925 * GroupChainElement could be waiting on multiple groups.
1926 *
1927 * @param gce the group chain element to be processed next
1928 */
1929 private void processGroupChain(GroupChainElem gce) {
1930 int waitOnGroups = gce.decrementAndGetGroupsWaitedOn();
1931 if (waitOnGroups != 0) {
1932 log.debug("GCE: {} not ready to be processed", gce);
1933 return;
1934 }
1935 log.debug("GCE: {} ready to be processed", gce);
Yi Tsengef19de12017-04-24 11:33:05 -07001936 if (gce.addBucketToGroup()) {
1937 groupService.addBucketsToGroup(gce.groupDescription().deviceId(),
1938 gce.groupDescription().appCookie(),
1939 gce.groupDescription().buckets(),
1940 gce.groupDescription().appCookie(),
1941 gce.groupDescription().appId());
Charles Chan188ebf52015-12-23 00:15:11 -08001942 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001943 groupService.addGroup(gce.groupDescription());
Charles Chan188ebf52015-12-23 00:15:11 -08001944 }
1945 }
1946
Saurav Dasc88d4662017-05-15 15:34:25 -07001947 private void updateFlowObjectiveStore(Integer nextId, OfdpaNextGroup nextGrp) {
1948 synchronized (flowObjectiveStore) {
1949 // get fresh copy of what the store holds
1950 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1951 if (next == null || nextGrp.nextObjective().op() == Operation.ADD) {
1952 flowObjectiveStore.putNextGroup(nextId, nextGrp);
1953 return;
1954 }
1955 if (nextGrp.nextObjective().op() == Operation.ADD_TO_EXISTING) {
1956 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Pier Luigid008dbe2018-01-15 09:48:49 +01001957 allActiveKeys = Lists.newArrayList(allActiveKeys);
Saurav Dasc88d4662017-05-15 15:34:25 -07001958 // If active keys shows only the top-level group without a chain of groups,
1959 // then it represents an empty group. Update by replacing empty chain.
1960 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1961 allActiveKeys.clear();
1962 }
1963 allActiveKeys.addAll(nextGrp.allKeys());
1964 flowObjectiveStore.putNextGroup(nextId,
1965 new OfdpaNextGroup(allActiveKeys, nextGrp.nextObjective()));
1966 }
1967 }
1968 }
1969
Saurav Das8be4e3a2016-03-11 17:19:07 -08001970 private class InnerGroupListener implements GroupListener {
1971 @Override
1972 public void event(GroupEvent event) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001973 switch (event.type()) {
1974 case GROUP_ADDED:
1975 processPendingAddGroupsOrNextObjs(event.subject().appCookie(), true);
1976 break;
1977 case GROUP_REMOVED:
1978 processPendingRemoveNextObjs(event.subject().appCookie());
1979 break;
Yi Tseng78f51f42017-02-02 13:54:58 -08001980 case GROUP_UPDATED:
1981 processPendingUpdateNextObjs(event.subject().appCookie());
1982 break;
Charles Chanfc5c7802016-05-17 13:13:55 -07001983 default:
1984 break;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001985 }
1986 }
1987 }
Charles Chan188ebf52015-12-23 00:15:11 -08001988}