blob: 2c2f3bab8ead219a3449e0a9e8dcff3288780479 [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;
109 private Cache<GroupKey, List<OfdpaGroupHandlerUtility.OfdpaNextGroup>> pendingAddNextObjectives;
110 private Cache<NextObjective, List<GroupKey>> pendingRemoveNextObjectives;
111 private Cache<GroupKey, Set<OfdpaGroupHandlerUtility.GroupChainElem>> pendingGroups;
112 private ConcurrentHashMap<GroupKey, Set<NextObjective>> pendingUpdateNextObjectives;
113
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
122 public Cache<GroupKey, List<OfdpaNextGroup>> pendingAddNextObjectives() {
123 return pendingAddNextObjectives;
124 }
125
126 public Cache<GroupKey, Set<GroupChainElem>> pendingGroups() {
127 return pendingGroups;
128 }
129
Charles Chan40132b32017-01-22 00:19:37 -0800130 /**
131 * Determines whether this pipeline support copy ttl instructions or not.
132 *
133 * @return true if copy ttl instructions are supported
134 */
135 protected boolean supportCopyTtl() {
136 return true;
137 }
138
139 /**
140 * Determines whether this pipeline support set mpls bos instruction or not.
141 *
142 * @return true if set mpls bos instruction is supported
143 */
144 protected boolean supportSetMplsBos() {
145 return true;
146 }
147
Charles Chan0f43e472017-02-14 14:00:16 -0800148 /**
149 * Determines whether this pipeline requires popping VLAN before pushing MPLS.
150 * <p>
151 * If required, pop vlan before push mpls and add an arbitrary vlan back afterward.
152 * MPLS interface group will substitute the arbitrary VLAN with expected VLAN later on.
153 *
154 * @return true if this pipeline requires popping VLAN before pushing MPLS
155 */
156 protected boolean requireVlanPopBeforeMplsPush() {
157 return false;
158 }
159
Alex Yashchuk4caa8e82017-12-08 17:40:05 +0200160 public void init(DeviceId deviceId, PipelinerContext context) {
Yi Tsengef19de12017-04-24 11:33:05 -0700161 ServiceDirectory serviceDirectory = context.directory();
Charles Chan188ebf52015-12-23 00:15:11 -0800162 this.deviceId = deviceId;
163 this.flowObjectiveStore = context.store();
Charles Chan188ebf52015-12-23 00:15:11 -0800164 this.groupService = serviceDirectory.get(GroupService.class);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800165 this.storageService = serviceDirectory.get(StorageService.class);
Madan Jampanid5714e02016-04-19 14:15:20 -0700166 this.nextIndex = storageService.getAtomicCounter("group-id-index-counter");
Charles Chan188ebf52015-12-23 00:15:11 -0800167
Charles Chanfc5c7802016-05-17 13:13:55 -0700168 pendingAddNextObjectives = CacheBuilder.newBuilder()
Charles Chan188ebf52015-12-23 00:15:11 -0800169 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700170 .removalListener((RemovalNotification<GroupKey, List<OfdpaNextGroup>> notification) -> {
171 if (notification.getCause() == RemovalCause.EXPIRED &&
172 Objects.nonNull(notification.getValue())) {
173 notification.getValue()
174 .forEach(ofdpaNextGrp ->
175 fail(ofdpaNextGrp.nextObjective(),
176 ObjectiveError.GROUPINSTALLATIONFAILED));
Charles Chanfc5c7802016-05-17 13:13:55 -0700177 }
178 }).build();
Charles Chan188ebf52015-12-23 00:15:11 -0800179
Charles Chanfc5c7802016-05-17 13:13:55 -0700180 pendingRemoveNextObjectives = CacheBuilder.newBuilder()
181 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700182 .removalListener((RemovalNotification<NextObjective, List<GroupKey>> notification) -> {
Charles Chanfc5c7802016-05-17 13:13:55 -0700183 if (notification.getCause() == RemovalCause.EXPIRED) {
Yi Tsengef19de12017-04-24 11:33:05 -0700184 fail(notification.getKey(),
185 ObjectiveError.GROUPREMOVALFAILED);
Charles Chan188ebf52015-12-23 00:15:11 -0800186 }
187 }).build();
Saurav Das961beb22017-03-29 19:09:17 -0700188 pendingGroups = CacheBuilder.newBuilder()
189 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700190 .removalListener((RemovalNotification<GroupKey, Set<GroupChainElem>> notification) -> {
Saurav Das961beb22017-03-29 19:09:17 -0700191 if (notification.getCause() == RemovalCause.EXPIRED) {
192 log.error("Unable to install group with key {} and pending GCEs: {}",
193 notification.getKey(), notification.getValue());
194 }
195 }).build();
Yi Tseng78f51f42017-02-02 13:54:58 -0800196 pendingUpdateNextObjectives = new ConcurrentHashMap<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700197 GroupChecker groupChecker = new GroupChecker(this);
198 groupCheckerExecutor.scheduleAtFixedRate(groupChecker, 0, 500, TimeUnit.MILLISECONDS);
Charles Chan188ebf52015-12-23 00:15:11 -0800199 groupService.addListener(new InnerGroupListener());
200 }
201
Saurav Das8be4e3a2016-03-11 17:19:07 -0800202 //////////////////////////////////////
203 // Group Creation
204 //////////////////////////////////////
205
Yi Tsengef19de12017-04-24 11:33:05 -0700206 /**
207 * Adds a list of group chain by given NextObjective.
208 *
209 * @param nextObjective the NextObjective
210 */
Charles Chan188ebf52015-12-23 00:15:11 -0800211 protected void addGroup(NextObjective nextObjective) {
212 switch (nextObjective.type()) {
213 case SIMPLE:
214 Collection<TrafficTreatment> treatments = nextObjective.next();
215 if (treatments.size() != 1) {
216 log.error("Next Objectives of type Simple should only have a "
217 + "single Traffic Treatment. Next Objective Id:{}",
218 nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -0700219 fail(nextObjective, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800220 return;
221 }
222 processSimpleNextObjective(nextObjective);
223 break;
224 case BROADCAST:
225 processBroadcastNextObjective(nextObjective);
226 break;
227 case HASHED:
Pier Ventre140a8942016-11-02 07:26:38 -0700228 if (!verifyHashedNextObjective(nextObjective)) {
229 log.error("Next Objectives of type hashed not supported. Next Objective Id:{}",
230 nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -0700231 fail(nextObjective, ObjectiveError.BADPARAMS);
Pier Ventre140a8942016-11-02 07:26:38 -0700232 return;
233 }
Charles Chan188ebf52015-12-23 00:15:11 -0800234 processHashedNextObjective(nextObjective);
235 break;
236 case FAILOVER:
Yi Tsengef19de12017-04-24 11:33:05 -0700237 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -0800238 log.warn("Unsupported next objective type {}", nextObjective.type());
239 break;
240 default:
Yi Tsengef19de12017-04-24 11:33:05 -0700241 fail(nextObjective, ObjectiveError.UNKNOWN);
Charles Chan188ebf52015-12-23 00:15:11 -0800242 log.warn("Unknown next objective type {}", nextObjective.type());
243 }
244 }
245
246 /**
247 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
Saurav Dasa4020382018-02-14 14:14:54 -0800248 * a chain of groups. The simple Next Objective passed in by the application
249 * is broken up into a group chain. The following chains can be created
250 * depending on the parameters in the Next Objective.
251 * 1. L2 Interface group (no chaining)
252 * 2. L3 Unicast group -> L2 Interface group
253 * 3. MPLS Interface group -> L2 Interface group
254 * 4. MPLS Swap group -> MPLS Interface group -> L2 Interface group
255 * 5. PW initiation group chain
Charles Chan188ebf52015-12-23 00:15:11 -0800256 *
257 * @param nextObj the nextObjective of type SIMPLE
258 */
259 private void processSimpleNextObjective(NextObjective nextObj) {
260 TrafficTreatment treatment = nextObj.next().iterator().next();
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800261 // determine if plain L2 or L3->L2 or MPLS Swap -> MPLS Interface -> L2
Charles Chan188ebf52015-12-23 00:15:11 -0800262 boolean plainL2 = true;
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800263 boolean mplsSwap = false;
264 MplsLabel mplsLabel = null;
Charles Chan188ebf52015-12-23 00:15:11 -0800265 for (Instruction ins : treatment.allInstructions()) {
266 if (ins.type() == Instruction.Type.L2MODIFICATION) {
267 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
268 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_DST ||
269 l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_SRC) {
270 plainL2 = false;
Charles Chan188ebf52015-12-23 00:15:11 -0800271 }
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800272 // mpls label in simple next objectives is used only to indicate
273 // a MPLS Swap group before the MPLS Interface Group
274 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) {
275 mplsSwap = true;
276 mplsLabel = ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label();
277 }
Charles Chan188ebf52015-12-23 00:15:11 -0800278 }
279 }
280
281 if (plainL2) {
282 createL2InterfaceGroup(nextObj);
283 return;
284 }
285
Saurav Dasa4020382018-02-14 14:14:54 -0800286 // In order to understand if it is a pseudowire related
Pier Ventre42287df2016-11-09 14:17:26 -0800287 // next objective we look for the tunnel id in the meta.
288 boolean isPw = false;
Pier Ventre140a8942016-11-02 07:26:38 -0700289 if (nextObj.meta() != null) {
Pier Ventre42287df2016-11-09 14:17:26 -0800290 TunnelIdCriterion tunnelIdCriterion = (TunnelIdCriterion) nextObj
291 .meta()
292 .getCriterion(TUNNEL_ID);
293 if (tunnelIdCriterion != null) {
294 isPw = true;
295 }
Pier Ventre140a8942016-11-02 07:26:38 -0700296 }
297
Andreas Pantelopoulos25db42f2018-02-08 12:42:06 -0800298 if (mplsSwap && !isPw) {
Saurav Dasa4020382018-02-14 14:14:54 -0800299 log.debug("Creating a MPLS Swap -> MPLS Interface -> L2 Interface group chain.");
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800300
301 // break up simple next objective to GroupChain objects
302 GroupInfo groupInfo = createL2L3Chain(treatment, nextObj.id(),
303 nextObj.appId(), true,
304 nextObj.meta());
305 if (groupInfo == null) {
306 log.error("Could not process nextObj={} in dev:{}", nextObj.id(), deviceId);
307 fail(nextObj, ObjectiveError.BADPARAMS);
308 return;
309 }
310
311 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Saurav Dasa4020382018-02-14 14:14:54 -0800312 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie()); // l2 interface
313 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie()); // mpls interface
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800314
315 // creating the mpls swap group and adding it to the chain
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800316 int nextGid = groupInfo.nextGroupDesc().givenGroupId();
317 int index = getNextAvailableIndex();
318
Saurav Dasa4020382018-02-14 14:14:54 -0800319 GroupDescription swapGroupDescription = createMplsSwap(
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800320 nextGid,
321 OfdpaMplsGroupSubType.MPLS_SWAP_LABEL,
322 index,
323 mplsLabel,
324 nextObj.appId()
325 );
Saurav Dasa4020382018-02-14 14:14:54 -0800326 // ensure swap group is added after L2L3 chain
327 GroupKey swapGroupKey = swapGroupDescription.appCookie();
328 GroupChainElem swapChainElem = new GroupChainElem(swapGroupDescription,
329 1, false, deviceId);
330 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), swapChainElem);
331 gkeyChain.addFirst(swapGroupKey);
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800332
Saurav Dasa4020382018-02-14 14:14:54 -0800333 // ensure nextObjective waits on the outermost groupKey
334 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
335 allGroupKeys.add(gkeyChain);
336 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
337 updatePendingNextObjective(swapGroupKey, ofdpaGrp);
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800338
339 // now we are ready to send the l2 groupDescription (inner), as all the stores
340 // that will get async replies have been updated. By waiting to update
341 // the stores, we prevent nasty race conditions.
342 groupService.addGroup(groupInfo.innerMostGroupDesc());
343 } else if (!isPw) {
Saurav Dasa4020382018-02-14 14:14:54 -0800344 boolean isMpls = false;
345 if (nextObj.meta() != null) {
346 isMpls = isNotMplsBos(nextObj.meta());
347 }
348 log.debug("Creating a {} -> L2 Interface group chain.",
349 (isMpls) ? "MPLS Interface" : "L3 Unicast");
Pier Ventre42287df2016-11-09 14:17:26 -0800350 // break up simple next objective to GroupChain objects
351 GroupInfo groupInfo = createL2L3Chain(treatment, nextObj.id(),
352 nextObj.appId(), isMpls,
353 nextObj.meta());
354 if (groupInfo == null) {
355 log.error("Could not process nextObj={} in dev:{}", nextObj.id(), deviceId);
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800356 fail(nextObj, ObjectiveError.BADPARAMS);
Pier Ventre42287df2016-11-09 14:17:26 -0800357 return;
358 }
359 // create object for local and distributed storage
360 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700361 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
362 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Saurav Dasa4020382018-02-14 14:14:54 -0800363 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
364 allGroupKeys.add(gkeyChain);
365 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Pier Ventre42287df2016-11-09 14:17:26 -0800366
367 // store l3groupkey with the ofdpaNextGroup for the nextObjective that depends on it
Yi Tsengef19de12017-04-24 11:33:05 -0700368 updatePendingNextObjective(groupInfo.nextGroupDesc().appCookie(), ofdpaGrp);
Pier Ventre42287df2016-11-09 14:17:26 -0800369
370 // now we are ready to send the l2 groupDescription (inner), as all the stores
371 // that will get async replies have been updated. By waiting to update
372 // the stores, we prevent nasty race conditions.
Yi Tsengef19de12017-04-24 11:33:05 -0700373 groupService.addGroup(groupInfo.innerMostGroupDesc());
Pier Ventre42287df2016-11-09 14:17:26 -0800374 } else {
375 // We handle the pseudo wire with a different a procedure.
376 // This procedure is meant to handle both initiation and
377 // termination of the pseudo wire.
378 processPwNextObjective(nextObj);
Charles Chan188ebf52015-12-23 00:15:11 -0800379 }
Charles Chan188ebf52015-12-23 00:15:11 -0800380 }
381
Charles Chan188ebf52015-12-23 00:15:11 -0800382 /**
383 * Creates a simple L2 Interface Group.
384 *
385 * @param nextObj the next Objective
386 */
387 private void createL2InterfaceGroup(NextObjective nextObj) {
Yi Tsengef19de12017-04-24 11:33:05 -0700388 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700389 if (assignedVlan == null) {
390 log.warn("VLAN ID required by simple next obj is missing. Abort.");
Yi Tsengef19de12017-04-24 11:33:05 -0700391 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800392 return;
393 }
394
Charles Chan5b9df8d2016-03-28 22:21:40 -0700395 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Charles Chan188ebf52015-12-23 00:15:11 -0800396
Charles Chan5b9df8d2016-03-28 22:21:40 -0700397 // There is only one L2 interface group in this case
Yi Tsengef19de12017-04-24 11:33:05 -0700398 GroupDescription l2InterfaceGroupDesc = groupInfos.get(0).innerMostGroupDesc();
Charles Chan188ebf52015-12-23 00:15:11 -0800399
Charles Chan5b9df8d2016-03-28 22:21:40 -0700400 // Put all dependency information into allGroupKeys
401 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
402 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
403 gkeyChain.addFirst(l2InterfaceGroupDesc.appCookie());
404 allGroupKeys.add(gkeyChain);
Charles Chan188ebf52015-12-23 00:15:11 -0800405
Charles Chan5b9df8d2016-03-28 22:21:40 -0700406 // Point the next objective to this group
407 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
408 updatePendingNextObjective(l2InterfaceGroupDesc.appCookie(), ofdpaGrp);
409
410 // Start installing the inner-most group
411 groupService.addGroup(l2InterfaceGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800412 }
413
414 /**
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800415 * Creates an Mpls group of type swap.
416 *
417 * @param nextGroupId the next group in the chain
418 * @param subtype the mpls swap label group subtype
419 * @param index the index of the group
420 * @param mplsLabel the mpls label to swap
421 * @param applicationId the application id
422 * @return the group description
423 */
424 protected GroupDescription createMplsSwap(int nextGroupId,
425 OfdpaMplsGroupSubType subtype,
426 int index,
427 MplsLabel mplsLabel,
428 ApplicationId applicationId) {
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800429 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800430 treatment.setMpls(mplsLabel);
431
432 // We point the group to the next group.
433 treatment.group(new GroupId(nextGroupId));
434 GroupBucket groupBucket = DefaultGroupBucket
435 .createIndirectGroupBucket(treatment.build());
436 // Finally we build the group description.
437 int groupId = makeMplsLabelGroupId(subtype, index);
438 GroupKey groupKey = new DefaultGroupKey(
Saurav Dasa4020382018-02-14 14:14:54 -0800439 Ofdpa2Pipeline.appKryo.serialize(index));
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800440 return new DefaultGroupDescription(
441 deviceId,
442 INDIRECT,
443 new GroupBuckets(Collections.singletonList(groupBucket)),
444 groupKey,
445 groupId,
Saurav Dasa4020382018-02-14 14:14:54 -0800446 applicationId);
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800447 }
448
449 /**
Charles Chan188ebf52015-12-23 00:15:11 -0800450 * Creates one of two possible group-chains from the treatment
451 * passed in. Depending on the MPLS boolean, this method either creates
Charles Chan425854b2016-04-11 15:32:12 -0700452 * an L3Unicast Group --&gt; L2Interface Group, if mpls is false;
453 * or MPLSInterface Group --&gt; L2Interface Group, if mpls is true;
Charles Chan188ebf52015-12-23 00:15:11 -0800454 * The returned 'inner' group description is always the L2 Interface group.
455 *
456 * @param treatment that needs to be broken up to create the group chain
457 * @param nextId of the next objective that needs this group chain
458 * @param appId of the application that sent this next objective
459 * @param mpls determines if L3Unicast or MPLSInterface group is created
460 * @param meta metadata passed in by the application as part of the nextObjective
461 * @return GroupInfo containing the GroupDescription of the
462 * L2Interface group(inner) and the GroupDescription of the (outer)
463 * L3Unicast/MPLSInterface group. May return null if there is an
464 * error in processing the chain
465 */
Charles Chan425854b2016-04-11 15:32:12 -0700466 protected GroupInfo createL2L3Chain(TrafficTreatment treatment, int nextId,
Charles Chanf9e98652016-09-07 16:54:23 -0700467 ApplicationId appId, boolean mpls,
468 TrafficSelector meta) {
469 return createL2L3ChainInternal(treatment, nextId, appId, mpls, meta, true);
470 }
471
472 /**
473 * Internal implementation of createL2L3Chain.
474 * <p>
475 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
476 * Since it is non-OF spec, we need an extension treatment for that.
477 * The useSetVlanExtension must be set to false for OFDPA i12.
478 * </p>
479 *
480 * @param treatment that needs to be broken up to create the group chain
481 * @param nextId of the next objective that needs this group chain
482 * @param appId of the application that sent this next objective
483 * @param mpls determines if L3Unicast or MPLSInterface group is created
484 * @param meta metadata passed in by the application as part of the nextObjective
485 * @param useSetVlanExtension use the setVlanVid extension that has is_present bit set to 0.
486 * @return GroupInfo containing the GroupDescription of the
487 * L2Interface group(inner) and the GroupDescription of the (outer)
488 * L3Unicast/MPLSInterface group. May return null if there is an
489 * error in processing the chain
490 */
491 protected GroupInfo createL2L3ChainInternal(TrafficTreatment treatment, int nextId,
Pier Ventre42287df2016-11-09 14:17:26 -0800492 ApplicationId appId, boolean mpls,
493 TrafficSelector meta, boolean useSetVlanExtension) {
Charles Chan188ebf52015-12-23 00:15:11 -0800494 // for the l2interface group, get vlan and port info
495 // for the outer group, get the src/dst mac, and vlan info
496 TrafficTreatment.Builder outerTtb = DefaultTrafficTreatment.builder();
497 TrafficTreatment.Builder innerTtb = DefaultTrafficTreatment.builder();
498 VlanId vlanid = null;
499 long portNum = 0;
500 boolean setVlan = false, popVlan = false;
Yi Tseng78f51f42017-02-02 13:54:58 -0800501 MacAddress srcMac;
502 MacAddress dstMac;
Charles Chan188ebf52015-12-23 00:15:11 -0800503 for (Instruction ins : treatment.allInstructions()) {
504 if (ins.type() == Instruction.Type.L2MODIFICATION) {
505 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
506 switch (l2ins.subtype()) {
507 case ETH_DST:
Charles Chan5270ed02016-01-30 23:22:37 -0800508 dstMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
509 outerTtb.setEthDst(dstMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800510 break;
511 case ETH_SRC:
Charles Chand0fd5dc2016-02-16 23:14:49 -0800512 srcMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
513 outerTtb.setEthSrc(srcMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800514 break;
515 case VLAN_ID:
516 vlanid = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
Charles Chanf9e98652016-09-07 16:54:23 -0700517 if (useSetVlanExtension) {
518 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
519 outerTtb.extension(ofdpaSetVlanVid, deviceId);
520 } else {
521 outerTtb.setVlanId(vlanid);
522 }
Charles Chan188ebf52015-12-23 00:15:11 -0800523 setVlan = true;
524 break;
525 case VLAN_POP:
526 innerTtb.popVlan();
527 popVlan = true;
528 break;
529 case DEC_MPLS_TTL:
530 case MPLS_LABEL:
531 case MPLS_POP:
532 case MPLS_PUSH:
533 case VLAN_PCP:
534 case VLAN_PUSH:
535 default:
536 break;
537 }
538 } else if (ins.type() == Instruction.Type.OUTPUT) {
539 portNum = ((Instructions.OutputInstruction) ins).port().toLong();
540 innerTtb.add(ins);
541 } else {
Saurav Das7bcbe702017-06-13 15:35:54 -0700542 log.debug("Driver does not handle this type of TrafficTreatment"
Saurav Dasa4020382018-02-14 14:14:54 -0800543 + " instruction in l2l3chain: {} - {}", ins.type(),
544 ins);
Charles Chan188ebf52015-12-23 00:15:11 -0800545 }
546 }
547
548 if (vlanid == null && meta != null) {
549 // use metadata if available
Pier Ventre140a8942016-11-02 07:26:38 -0700550 Criterion vidCriterion = meta.getCriterion(VLAN_VID);
Charles Chan188ebf52015-12-23 00:15:11 -0800551 if (vidCriterion != null) {
552 vlanid = ((VlanIdCriterion) vidCriterion).vlanId();
553 }
554 // if vlan is not set, use the vlan in metadata for outerTtb
555 if (vlanid != null && !setVlan) {
Charles Chanf9e98652016-09-07 16:54:23 -0700556 if (useSetVlanExtension) {
557 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
558 outerTtb.extension(ofdpaSetVlanVid, deviceId);
559 } else {
560 outerTtb.setVlanId(vlanid);
561 }
Charles Chan188ebf52015-12-23 00:15:11 -0800562 }
563 }
564
565 if (vlanid == null) {
566 log.error("Driver cannot process an L2/L3 group chain without "
567 + "egress vlan information for dev: {} port:{}",
568 deviceId, portNum);
569 return null;
570 }
571
572 if (!setVlan && !popVlan) {
573 // untagged outgoing port
574 TrafficTreatment.Builder temp = DefaultTrafficTreatment.builder();
575 temp.popVlan();
Yi Tsengef19de12017-04-24 11:33:05 -0700576 innerTtb.build().allInstructions().forEach(temp::add);
Charles Chan188ebf52015-12-23 00:15:11 -0800577 innerTtb = temp;
578 }
579
580 // assemble information for ofdpa l2interface group
Yi Tsengef19de12017-04-24 11:33:05 -0700581 int l2groupId = l2GroupId(vlanid, portNum);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800582 // a globally unique groupkey that is different for ports in the same device,
Charles Chan188ebf52015-12-23 00:15:11 -0800583 // but different for the same portnumber on different devices. Also different
584 // for the various group-types created out of the same next objective.
Charles Chane849c192016-01-11 18:28:54 -0800585 int l2gk = l2InterfaceGroupKey(deviceId, vlanid, portNum);
Yi Tsengef19de12017-04-24 11:33:05 -0700586 final GroupKey l2groupkey = new DefaultGroupKey(appKryo.serialize(l2gk));
Charles Chan188ebf52015-12-23 00:15:11 -0800587
588 // assemble information for outer group
Yi Tsengef19de12017-04-24 11:33:05 -0700589 GroupDescription outerGrpDesc;
Charles Chan188ebf52015-12-23 00:15:11 -0800590 if (mpls) {
Yi Tsengef19de12017-04-24 11:33:05 -0700591 // outer group is MPLS Interface
Saurav Das8be4e3a2016-03-11 17:19:07 -0800592 int mplsInterfaceIndex = getNextAvailableIndex();
Yi Tsengef19de12017-04-24 11:33:05 -0700593 int mplsGroupId = MPLS_INTERFACE_TYPE | (SUBTYPE_MASK & mplsInterfaceIndex);
594 final GroupKey mplsGroupKey = new DefaultGroupKey(
595 appKryo.serialize(mplsInterfaceIndex));
Yi Tsengfa394de2017-02-01 11:26:40 -0800596 outerTtb.group(new GroupId(l2groupId));
Charles Chan188ebf52015-12-23 00:15:11 -0800597 // create the mpls-interface group description to wait for the
598 // l2 interface group to be processed
599 GroupBucket mplsinterfaceGroupBucket =
600 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
601 outerGrpDesc = new DefaultGroupDescription(
602 deviceId,
603 GroupDescription.Type.INDIRECT,
604 new GroupBuckets(Collections.singletonList(
605 mplsinterfaceGroupBucket)),
Yi Tsengef19de12017-04-24 11:33:05 -0700606 mplsGroupKey,
607 mplsGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800608 appId);
609 log.debug("Trying MPLS-Interface: device:{} gid:{} gkey:{} nextid:{}",
Yi Tsengef19de12017-04-24 11:33:05 -0700610 deviceId, Integer.toHexString(mplsGroupId),
611 mplsGroupKey, nextId);
Charles Chan188ebf52015-12-23 00:15:11 -0800612 } else {
613 // outer group is L3Unicast
Saurav Das8be4e3a2016-03-11 17:19:07 -0800614 int l3unicastIndex = getNextAvailableIndex();
615 int l3groupId = L3_UNICAST_TYPE | (TYPE_MASK & l3unicastIndex);
616 final GroupKey l3groupkey = new DefaultGroupKey(
Yi Tsengef19de12017-04-24 11:33:05 -0700617 appKryo.serialize(l3unicastIndex));
Yi Tsengfa394de2017-02-01 11:26:40 -0800618 outerTtb.group(new GroupId(l2groupId));
Charles Chan188ebf52015-12-23 00:15:11 -0800619 // create the l3unicast group description to wait for the
620 // l2 interface group to be processed
621 GroupBucket l3unicastGroupBucket =
622 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
623 outerGrpDesc = new DefaultGroupDescription(
624 deviceId,
625 GroupDescription.Type.INDIRECT,
Yi Tsengef19de12017-04-24 11:33:05 -0700626 new GroupBuckets(Collections.singletonList(l3unicastGroupBucket)),
Charles Chan188ebf52015-12-23 00:15:11 -0800627 l3groupkey,
628 l3groupId,
629 appId);
630 log.debug("Trying L3Unicast: device:{} gid:{} gkey:{} nextid:{}",
631 deviceId, Integer.toHexString(l3groupId),
632 l3groupkey, nextId);
633 }
634
635 // store l2groupkey with the groupChainElem for the outer-group that depends on it
Yi Tsengef19de12017-04-24 11:33:05 -0700636 GroupChainElem gce = new GroupChainElem(outerGrpDesc, 1, false, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800637 updatePendingGroups(l2groupkey, gce);
638
Yi Tsengef19de12017-04-24 11:33:05 -0700639 // create group description for the inner l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700640 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800641 DefaultGroupBucket.createIndirectGroupBucket(innerTtb.build());
642 GroupDescription l2groupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -0700643 new DefaultGroupDescription(deviceId,
644 GroupDescription.Type.INDIRECT,
645 new GroupBuckets(Collections.singletonList(l2InterfaceGroupBucket)),
646 l2groupkey,
647 l2groupId,
648 appId);
Charles Chan188ebf52015-12-23 00:15:11 -0800649 log.debug("Trying L2Interface: device:{} gid:{} gkey:{} nextId:{}",
650 deviceId, Integer.toHexString(l2groupId),
651 l2groupkey, nextId);
652 return new GroupInfo(l2groupDescription, outerGrpDesc);
653
654 }
655
656 /**
657 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
658 * a chain of groups. The broadcast Next Objective passed in by the application
659 * has to be broken up into a group chain comprising of an
Saurav Das1a129a02016-11-18 15:21:57 -0800660 * L2 Flood group or L3 Multicast group, whose buckets point to L2 Interface groups.
Charles Chan188ebf52015-12-23 00:15:11 -0800661 *
662 * @param nextObj the nextObjective of type BROADCAST
663 */
664 private void processBroadcastNextObjective(NextObjective nextObj) {
Yi Tsengef19de12017-04-24 11:33:05 -0700665 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700666 if (assignedVlan == null) {
667 log.warn("VLAN ID required by broadcast next obj is missing. Abort.");
Yi Tsengef19de12017-04-24 11:33:05 -0700668 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800669 return;
670 }
Charles Chan188ebf52015-12-23 00:15:11 -0800671
Charles Chan5b9df8d2016-03-28 22:21:40 -0700672 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
673
Yi Tsengef19de12017-04-24 11:33:05 -0700674 IpPrefix ipDst = readIpDstFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700675 if (ipDst != null) {
676 if (ipDst.isMulticast()) {
677 createL3MulticastGroup(nextObj, assignedVlan, groupInfos);
678 } else {
679 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -0700680 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700681 }
682 } else {
683 createL2FloodGroup(nextObj, assignedVlan, groupInfos);
684 }
685 }
686
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700687 private List<GroupInfo> prepareL2InterfaceGroup(NextObjective nextObj,
688 VlanId assignedVlan) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700689 ImmutableList.Builder<GroupInfo> groupInfoBuilder = ImmutableList.builder();
690
691 // break up broadcast next objective to multiple groups
692 Collection<TrafficTreatment> buckets = nextObj.next();
693
Charles Chan188ebf52015-12-23 00:15:11 -0800694 // each treatment is converted to an L2 interface group
Charles Chan188ebf52015-12-23 00:15:11 -0800695 for (TrafficTreatment treatment : buckets) {
696 TrafficTreatment.Builder newTreatment = DefaultTrafficTreatment.builder();
697 PortNumber portNum = null;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700698 VlanId egressVlan = null;
Charles Chan188ebf52015-12-23 00:15:11 -0800699 // ensure that the only allowed treatments are pop-vlan and output
700 for (Instruction ins : treatment.allInstructions()) {
701 if (ins.type() == Instruction.Type.L2MODIFICATION) {
702 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
703 switch (l2ins.subtype()) {
704 case VLAN_POP:
705 newTreatment.add(l2ins);
706 break;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700707 case VLAN_ID:
708 egressVlan = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
709 break;
Charles Chan188ebf52015-12-23 00:15:11 -0800710 default:
711 log.debug("action {} not permitted for broadcast nextObj",
712 l2ins.subtype());
713 break;
714 }
715 } else if (ins.type() == Instruction.Type.OUTPUT) {
716 portNum = ((Instructions.OutputInstruction) ins).port();
717 newTreatment.add(ins);
718 } else {
Charles Chane849c192016-01-11 18:28:54 -0800719 log.debug("TrafficTreatment of type {} not permitted in " +
720 " broadcast nextObjective", ins.type());
Charles Chan188ebf52015-12-23 00:15:11 -0800721 }
722 }
723
Yi Tsengef19de12017-04-24 11:33:05 -0700724 if (portNum == null) {
725 log.warn("Can't find output port for the bucket {}.", treatment);
726 continue;
727 }
728
Charles Chan188ebf52015-12-23 00:15:11 -0800729 // assemble info for l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700730 VlanId l2InterfaceGroupVlan =
731 (egressVlan != null && !assignedVlan.equals(egressVlan)) ?
732 egressVlan : assignedVlan;
733 int l2gk = l2InterfaceGroupKey(deviceId, l2InterfaceGroupVlan, portNum.toLong());
734 final GroupKey l2InterfaceGroupKey =
Yi Tsengef19de12017-04-24 11:33:05 -0700735 new DefaultGroupKey(appKryo.serialize(l2gk));
Charles Chan372b63e2017-02-07 12:10:53 -0800736 int l2InterfaceGroupId = L2_INTERFACE_TYPE |
737 ((l2InterfaceGroupVlan.toShort() & THREE_BIT_MASK) << PORT_LEN) |
738 ((int) portNum.toLong() & FOUR_BIT_MASK);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700739 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800740 DefaultGroupBucket.createIndirectGroupBucket(newTreatment.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700741 GroupDescription l2InterfaceGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -0700742 new DefaultGroupDescription(deviceId,
743 GroupDescription.Type.INDIRECT,
744 new GroupBuckets(Collections.singletonList(
745 l2InterfaceGroupBucket)),
746 l2InterfaceGroupKey,
747 l2InterfaceGroupId,
748 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800749 log.debug("Trying L2-Interface: device:{} gid:{} gkey:{} nextid:{}",
Charles Chan5b9df8d2016-03-28 22:21:40 -0700750 deviceId, Integer.toHexString(l2InterfaceGroupId),
751 l2InterfaceGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -0800752
Charles Chan5b9df8d2016-03-28 22:21:40 -0700753 groupInfoBuilder.add(new GroupInfo(l2InterfaceGroupDescription,
754 l2InterfaceGroupDescription));
Charles Chan188ebf52015-12-23 00:15:11 -0800755 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700756 return groupInfoBuilder.build();
757 }
Charles Chan188ebf52015-12-23 00:15:11 -0800758
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700759 private void createL2FloodGroup(NextObjective nextObj, VlanId vlanId,
760 List<GroupInfo> groupInfos) {
761 // assemble info for l2 flood group. Since there can be only one flood
762 // group for a vlan, its index is always the same - 0
Yi Tsengef19de12017-04-24 11:33:05 -0700763 Integer l2FloodGroupId = L2_FLOOD_TYPE | (vlanId.toShort() << 16);
764 final GroupKey l2FloodGroupKey = l2FloodGroupKey(vlanId, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700765
Charles Chan188ebf52015-12-23 00:15:11 -0800766 // collection of group buckets pointing to all the l2 interface groups
Yi Tsengef19de12017-04-24 11:33:05 -0700767 List<GroupBucket> l2floodBuckets = generateNextGroupBuckets(groupInfos, ALL);
Charles Chan188ebf52015-12-23 00:15:11 -0800768 // create the l2flood group-description to wait for all the
769 // l2interface groups to be processed
770 GroupDescription l2floodGroupDescription =
771 new DefaultGroupDescription(
772 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800773 ALL,
Charles Chan188ebf52015-12-23 00:15:11 -0800774 new GroupBuckets(l2floodBuckets),
Yi Tsengef19de12017-04-24 11:33:05 -0700775 l2FloodGroupKey,
776 l2FloodGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800777 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800778 log.debug("Trying L2-Flood: device:{} gid:{} gkey:{} nextid:{}",
Yi Tsengef19de12017-04-24 11:33:05 -0700779 deviceId, Integer.toHexString(l2FloodGroupId),
780 l2FloodGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -0800781
Charles Chan5b9df8d2016-03-28 22:21:40 -0700782 // Put all dependency information into allGroupKeys
783 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
784 groupInfos.forEach(groupInfo -> {
Yi Tsengef19de12017-04-24 11:33:05 -0700785 Deque<GroupKey> groupKeyChain = new ArrayDeque<>();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700786 // In this case we should have L2 interface group only
Yi Tsengef19de12017-04-24 11:33:05 -0700787 groupKeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
788 groupKeyChain.addFirst(l2FloodGroupKey);
789 allGroupKeys.add(groupKeyChain);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700790 });
Charles Chan188ebf52015-12-23 00:15:11 -0800791
Charles Chan5b9df8d2016-03-28 22:21:40 -0700792 // Point the next objective to this group
793 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -0700794 updatePendingNextObjective(l2FloodGroupKey, ofdpaGrp);
Charles Chan188ebf52015-12-23 00:15:11 -0800795
Charles Chan5b9df8d2016-03-28 22:21:40 -0700796 GroupChainElem gce = new GroupChainElem(l2floodGroupDescription,
Yi Tsengef19de12017-04-24 11:33:05 -0700797 groupInfos.size(), false, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700798 groupInfos.forEach(groupInfo -> {
799 // Point this group to the next group
Yi Tsengef19de12017-04-24 11:33:05 -0700800 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), gce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700801 // Start installing the inner-most group
Yi Tsengef19de12017-04-24 11:33:05 -0700802 groupService.addGroup(groupInfo.innerMostGroupDesc());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700803 });
804 }
805
Pier Luigi21fffd22018-01-19 10:24:53 +0100806 private void createL3MulticastGroup(NextObjective nextObj, VlanId vlanId,
807 List<GroupInfo> groupInfos) {
808 // Let's create a new list mcast buckets
809 List<GroupBucket> l3McastBuckets = createL3MulticastBucket(groupInfos);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700810
811 int l3MulticastIndex = getNextAvailableIndex();
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700812 int l3MulticastGroupId = L3_MULTICAST_TYPE |
813 vlanId.toShort() << 16 | (TYPE_VLAN_MASK & l3MulticastIndex);
814 final GroupKey l3MulticastGroupKey =
Yi Tsengef19de12017-04-24 11:33:05 -0700815 new DefaultGroupKey(appKryo.serialize(l3MulticastIndex));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700816
817 GroupDescription l3MulticastGroupDesc = new DefaultGroupDescription(deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800818 ALL,
Charles Chan5b9df8d2016-03-28 22:21:40 -0700819 new GroupBuckets(l3McastBuckets),
820 l3MulticastGroupKey,
821 l3MulticastGroupId,
822 nextObj.appId());
823
824 // Put all dependency information into allGroupKeys
825 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
826 groupInfos.forEach(groupInfo -> {
827 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700828 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700829 // Add L3 interface group to the chain if there is one.
Yi Tsengef19de12017-04-24 11:33:05 -0700830 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
831 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700832 }
833 gkeyChain.addFirst(l3MulticastGroupKey);
834 allGroupKeys.add(gkeyChain);
835 });
836
837 // Point the next objective to this group
838 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
839 updatePendingNextObjective(l3MulticastGroupKey, ofdpaGrp);
840
841 GroupChainElem outerGce = new GroupChainElem(l3MulticastGroupDesc,
Yi Tsengef19de12017-04-24 11:33:05 -0700842 groupInfos.size(), false, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700843 groupInfos.forEach(groupInfo -> {
844 // Point this group (L3 multicast) to the next group
Yi Tsengef19de12017-04-24 11:33:05 -0700845 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), outerGce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700846
847 // Point next group to inner-most group, if any
Yi Tsengef19de12017-04-24 11:33:05 -0700848 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
849 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(),
850 1, false, deviceId);
851 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700852 }
853
854 // Start installing the inner-most group
Yi Tsengef19de12017-04-24 11:33:05 -0700855 groupService.addGroup(groupInfo.innerMostGroupDesc());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700856 });
Charles Chan188ebf52015-12-23 00:15:11 -0800857 }
858
Charles Chan188ebf52015-12-23 00:15:11 -0800859 /**
860 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
861 * a chain of groups. The hashed Next Objective passed in by the application
862 * has to be broken up into a group chain comprising of an
863 * L3 ECMP group as the top level group. Buckets of this group can point
864 * to a variety of groups in a group chain, depending on the whether
865 * MPLS labels are being pushed or not.
866 * <p>
867 * NOTE: We do not create MPLS ECMP groups as they are unimplemented in
868 * OF-DPA 2.0 (even though it is in the spec). Therefore we do not
869 * check the nextObjective meta to see what is matching before being
870 * sent to this nextObjective.
871 *
872 * @param nextObj the nextObjective of type HASHED
873 */
Pier Ventre140a8942016-11-02 07:26:38 -0700874 protected void processHashedNextObjective(NextObjective nextObj) {
Charles Chan188ebf52015-12-23 00:15:11 -0800875 // storage for all group keys in the chain of groups created
876 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
877 List<GroupInfo> unsentGroups = new ArrayList<>();
878 createHashBucketChains(nextObj, allGroupKeys, unsentGroups);
879
880 // now we can create the outermost L3 ECMP group
881 List<GroupBucket> l3ecmpGroupBuckets = new ArrayList<>();
882 for (GroupInfo gi : unsentGroups) {
883 // create ECMP bucket to point to the outer group
884 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Yi Tsengef19de12017-04-24 11:33:05 -0700885 ttb.group(new GroupId(gi.nextGroupDesc().givenGroupId()));
Charles Chan188ebf52015-12-23 00:15:11 -0800886 GroupBucket sbucket = DefaultGroupBucket
887 .createSelectGroupBucket(ttb.build());
888 l3ecmpGroupBuckets.add(sbucket);
889 }
Saurav Das8be4e3a2016-03-11 17:19:07 -0800890 int l3ecmpIndex = getNextAvailableIndex();
891 int l3ecmpGroupId = L3_ECMP_TYPE | (TYPE_MASK & l3ecmpIndex);
892 GroupKey l3ecmpGroupKey = new DefaultGroupKey(
Yi Tsengef19de12017-04-24 11:33:05 -0700893 appKryo.serialize(l3ecmpIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800894 GroupDescription l3ecmpGroupDesc =
895 new DefaultGroupDescription(
896 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800897 SELECT,
Charles Chan188ebf52015-12-23 00:15:11 -0800898 new GroupBuckets(l3ecmpGroupBuckets),
899 l3ecmpGroupKey,
900 l3ecmpGroupId,
901 nextObj.appId());
902 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
903 l3ecmpGroupBuckets.size(),
Yi Tsengef19de12017-04-24 11:33:05 -0700904 false, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800905
906 // create objects for local and distributed storage
Yi Tsengef19de12017-04-24 11:33:05 -0700907 allGroupKeys.forEach(gKeyChain -> gKeyChain.addFirst(l3ecmpGroupKey));
Charles Chan188ebf52015-12-23 00:15:11 -0800908 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
909
910 // store l3ecmpGroupKey with the ofdpaGroupChain for the nextObjective
911 // that depends on it
912 updatePendingNextObjective(l3ecmpGroupKey, ofdpaGrp);
913
914 log.debug("Trying L3ECMP: device:{} gid:{} gkey:{} nextId:{}",
915 deviceId, Integer.toHexString(l3ecmpGroupId),
916 l3ecmpGroupKey, nextObj.id());
917 // finally we are ready to send the innermost groups
918 for (GroupInfo gi : unsentGroups) {
919 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -0700920 Integer.toHexString(gi.innerMostGroupDesc().givenGroupId()), deviceId);
921 updatePendingGroups(gi.nextGroupDesc().appCookie(), l3ecmpGce);
922 groupService.addGroup(gi.innerMostGroupDesc());
Charles Chan188ebf52015-12-23 00:15:11 -0800923 }
Charles Chan188ebf52015-12-23 00:15:11 -0800924 }
925
926 /**
927 * Creates group chains for all buckets in a hashed group, and stores the
928 * GroupInfos and GroupKeys for all the groups in the lists passed in, which
929 * should be empty.
930 * <p>
931 * Does not create the top level ECMP group. Does not actually send the
932 * groups to the groupService.
933 *
934 * @param nextObj the Next Objective with buckets that need to be converted
935 * to group chains
936 * @param allGroupKeys a list to store groupKey for each bucket-group-chain
937 * @param unsentGroups a list to store GroupInfo for each bucket-group-chain
938 */
Pier Ventre140a8942016-11-02 07:26:38 -0700939 protected void createHashBucketChains(NextObjective nextObj,
Yi Tsengef19de12017-04-24 11:33:05 -0700940 List<Deque<GroupKey>> allGroupKeys,
941 List<GroupInfo> unsentGroups) {
Charles Chan188ebf52015-12-23 00:15:11 -0800942 // break up hashed next objective to multiple groups
943 Collection<TrafficTreatment> buckets = nextObj.next();
944
945 for (TrafficTreatment bucket : buckets) {
946 //figure out how many labels are pushed in each bucket
947 int labelsPushed = 0;
948 MplsLabel innermostLabel = null;
949 for (Instruction ins : bucket.allInstructions()) {
950 if (ins.type() == Instruction.Type.L2MODIFICATION) {
951 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
952 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_PUSH) {
953 labelsPushed++;
954 }
955 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) {
956 if (innermostLabel == null) {
Yi Tsengef19de12017-04-24 11:33:05 -0700957 innermostLabel =
958 ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label();
Charles Chan188ebf52015-12-23 00:15:11 -0800959 }
960 }
961 }
962 }
963
Yi Tsengef19de12017-04-24 11:33:05 -0700964 Deque<GroupKey> gKeyChain = new ArrayDeque<>();
Saurav Dasa4020382018-02-14 14:14:54 -0800965 // here we only deal with 0 and 1 label push
Charles Chan188ebf52015-12-23 00:15:11 -0800966 if (labelsPushed == 0) {
Yi Tsengef19de12017-04-24 11:33:05 -0700967 GroupInfo noLabelGroupInfo;
Pier Ventre140a8942016-11-02 07:26:38 -0700968 TrafficSelector metaSelector = nextObj.meta();
969 if (metaSelector != null) {
970 if (isNotMplsBos(metaSelector)) {
Yi Tsengef19de12017-04-24 11:33:05 -0700971 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
972 nextObj.appId(), true,
973 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700974 } else {
Yi Tsengef19de12017-04-24 11:33:05 -0700975 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
976 nextObj.appId(), false,
977 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700978 }
979 } else {
Yi Tsengef19de12017-04-24 11:33:05 -0700980 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
981 nextObj.appId(), false,
982 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700983 }
Yi Tsengef19de12017-04-24 11:33:05 -0700984 if (noLabelGroupInfo == null) {
Charles Chan188ebf52015-12-23 00:15:11 -0800985 log.error("Could not process nextObj={} in dev:{}",
986 nextObj.id(), deviceId);
987 return;
988 }
Yi Tsengef19de12017-04-24 11:33:05 -0700989 gKeyChain.addFirst(noLabelGroupInfo.innerMostGroupDesc().appCookie());
990 gKeyChain.addFirst(noLabelGroupInfo.nextGroupDesc().appCookie());
Charles Chan188ebf52015-12-23 00:15:11 -0800991
992 // we can't send the inner group description yet, as we have to
993 // create the dependent ECMP group first. So we store..
Yi Tsengef19de12017-04-24 11:33:05 -0700994 unsentGroups.add(noLabelGroupInfo);
Charles Chan188ebf52015-12-23 00:15:11 -0800995
996 } else if (labelsPushed == 1) {
997 GroupInfo onelabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
998 nextObj.appId(), true,
999 nextObj.meta());
1000 if (onelabelGroupInfo == null) {
1001 log.error("Could not process nextObj={} in dev:{}",
1002 nextObj.id(), deviceId);
1003 return;
1004 }
1005 // we need to add another group to this chain - the L3VPN group
1006 TrafficTreatment.Builder l3vpnTtb = DefaultTrafficTreatment.builder();
Charles Chan0f43e472017-02-14 14:00:16 -08001007 if (requireVlanPopBeforeMplsPush()) {
1008 l3vpnTtb.popVlan();
1009 }
Charles Chan188ebf52015-12-23 00:15:11 -08001010 l3vpnTtb.pushMpls()
1011 .setMpls(innermostLabel)
Yi Tsengef19de12017-04-24 11:33:05 -07001012 .group(new GroupId(onelabelGroupInfo.nextGroupDesc().givenGroupId()));
Charles Chan40132b32017-01-22 00:19:37 -08001013 if (supportCopyTtl()) {
1014 l3vpnTtb.copyTtlOut();
1015 }
1016 if (supportSetMplsBos()) {
1017 l3vpnTtb.setMplsBos(true);
1018 }
Charles Chan0f43e472017-02-14 14:00:16 -08001019 if (requireVlanPopBeforeMplsPush()) {
1020 l3vpnTtb.pushVlan().setVlanId(VlanId.vlanId(VlanId.RESERVED));
1021 }
Charles Chan40132b32017-01-22 00:19:37 -08001022
Charles Chan188ebf52015-12-23 00:15:11 -08001023 GroupBucket l3vpnGrpBkt =
1024 DefaultGroupBucket.createIndirectGroupBucket(l3vpnTtb.build());
Saurav Das8be4e3a2016-03-11 17:19:07 -08001025 int l3vpnIndex = getNextAvailableIndex();
Yi Tsengef19de12017-04-24 11:33:05 -07001026 int l3vpnGroupId = MPLS_L3VPN_SUBTYPE | (SUBTYPE_MASK & l3vpnIndex);
1027 GroupKey l3vpnGroupKey = new DefaultGroupKey(
1028 appKryo.serialize(l3vpnIndex));
Charles Chan188ebf52015-12-23 00:15:11 -08001029 GroupDescription l3vpnGroupDesc =
1030 new DefaultGroupDescription(
1031 deviceId,
1032 GroupDescription.Type.INDIRECT,
Yi Tsengef19de12017-04-24 11:33:05 -07001033 new GroupBuckets(Collections.singletonList(l3vpnGrpBkt)),
1034 l3vpnGroupKey,
1035 l3vpnGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -08001036 nextObj.appId());
Yi Tsengef19de12017-04-24 11:33:05 -07001037 GroupChainElem l3vpnGce = new GroupChainElem(l3vpnGroupDesc,
1038 1,
1039 false,
1040 deviceId);
1041 updatePendingGroups(onelabelGroupInfo.nextGroupDesc().appCookie(), l3vpnGce);
Charles Chan188ebf52015-12-23 00:15:11 -08001042
Yi Tsengef19de12017-04-24 11:33:05 -07001043 gKeyChain.addFirst(onelabelGroupInfo.innerMostGroupDesc().appCookie());
1044 gKeyChain.addFirst(onelabelGroupInfo.nextGroupDesc().appCookie());
1045 gKeyChain.addFirst(l3vpnGroupKey);
Charles Chan188ebf52015-12-23 00:15:11 -08001046
1047 //now we can replace the outerGrpDesc with the one we just created
Yi Tsengef19de12017-04-24 11:33:05 -07001048 onelabelGroupInfo.nextGroupDesc(l3vpnGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -08001049
1050 // we can't send the innermost group yet, as we have to create
1051 // the dependent ECMP group first. So we store ...
1052 unsentGroups.add(onelabelGroupInfo);
1053
Yi Tsengef19de12017-04-24 11:33:05 -07001054 log.debug("Trying L3VPN: device:{} gid:{} group key:{} nextId:{}",
1055 deviceId, Integer.toHexString(l3vpnGroupId),
1056 l3vpnGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -08001057
1058 } else {
1059 log.warn("Driver currently does not handle more than 1 MPLS "
1060 + "labels. Not processing nextObjective {}", nextObj.id());
1061 return;
1062 }
1063
1064 // all groups in this chain
Yi Tsengef19de12017-04-24 11:33:05 -07001065 allGroupKeys.add(gKeyChain);
Charles Chan188ebf52015-12-23 00:15:11 -08001066 }
1067 }
1068
Pier Ventre42287df2016-11-09 14:17:26 -08001069 /**
1070 * Processes the pseudo wire related next objective.
1071 * This procedure try to reuse the mpls label groups,
1072 * the mpls interface group and the l2 interface group.
1073 *
1074 * @param nextObjective the objective to process.
1075 */
1076 protected void processPwNextObjective(NextObjective nextObjective) {
Saurav Das1547b3f2017-05-05 17:01:08 -07001077 log.warn("Pseudo wire extensions are not supported in OFDPA 2.0 {}",
1078 nextObjective.id());
Pier Ventre42287df2016-11-09 14:17:26 -08001079 }
1080
Saurav Das8be4e3a2016-03-11 17:19:07 -08001081 //////////////////////////////////////
1082 // Group Editing
1083 //////////////////////////////////////
Charles Chan188ebf52015-12-23 00:15:11 -08001084 /**
1085 * Adds a bucket to the top level group of a group-chain, and creates the chain.
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001086 * Ensures that bucket being added is not a duplicate, by checking existing
Yi Tsengef19de12017-04-24 11:33:05 -07001087 * buckets for the same output port.
Charles Chan188ebf52015-12-23 00:15:11 -08001088 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001089 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -08001090 * @param next the representation of the existing group-chain for this next objective
1091 */
1092 protected void addBucketToGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001093 if (nextObjective.type() != NextObjective.Type.HASHED &&
1094 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001095 log.warn("AddBuckets not applied to nextType:{} in dev:{} for next:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001096 nextObjective.type(), deviceId, nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -07001097 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001098 return;
1099 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001100
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001101 // first check to see if bucket being added is not a duplicate of an
Yi Tsengef19de12017-04-24 11:33:05 -07001102 // existing bucket. If it is for an existing output port, then its a
1103 // duplicate.
Yi Tseng78f51f42017-02-02 13:54:58 -08001104 Set<TrafficTreatment> duplicateBuckets = Sets.newHashSet();
Yi Tsengef19de12017-04-24 11:33:05 -07001105 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
1106 Set<PortNumber> existingPorts = getExistingOutputPorts(allActiveKeys,
1107 groupService,
1108 deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -08001109 Set<TrafficTreatment> nonDuplicateBuckets = Sets.newHashSet();
1110 NextObjective objectiveToAdd;
Yi Tseng78f51f42017-02-02 13:54:58 -08001111
1112 nextObjective.next().forEach(trafficTreatment -> {
1113 PortNumber portNumber = readOutPortFromTreatment(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001114 if (portNumber == null) {
1115 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001116 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001117 if (existingPorts.contains(portNumber)) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001118 // its possible that portnumbers are same but labels are different
1119 int label = readLabelFromTreatment(trafficTreatment);
1120 if (label == -1) {
1121 duplicateBuckets.add(trafficTreatment);
1122 } else {
Saurav Dasceccf242017-08-03 18:30:35 -07001123 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1124 groupService, deviceId,
1125 portNumber, label);
1126 if (!existing.isEmpty()) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001127 duplicateBuckets.add(trafficTreatment);
1128 } else {
1129 nonDuplicateBuckets.add(trafficTreatment);
1130 }
1131 }
Yi Tseng47f82dc2017-03-05 22:48:39 -08001132 } else {
1133 nonDuplicateBuckets.add(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001134 }
1135 });
1136
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001137 if (duplicateBuckets.isEmpty()) {
1138 // use the original objective
1139 objectiveToAdd = nextObjective;
1140 } else if (!nonDuplicateBuckets.isEmpty()) {
1141 // only use the non-duplicate buckets if there are any
1142 log.debug("Some buckets {} already exist in next id {}, duplicate "
1143 + "buckets will be ignored.", duplicateBuckets, nextObjective.id());
1144 // new next objective with non duplicate treatments
Yi Tseng47f82dc2017-03-05 22:48:39 -08001145 NextObjective.Builder builder = DefaultNextObjective.builder()
1146 .withType(nextObjective.type())
1147 .withId(nextObjective.id())
1148 .withMeta(nextObjective.meta())
1149 .fromApp(nextObjective.appId());
1150 nonDuplicateBuckets.forEach(builder::addTreatment);
1151
1152 ObjectiveContext context = nextObjective.context().orElse(null);
1153 objectiveToAdd = builder.addToExisting(context);
1154 } else {
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001155 // buckets to add are already there - nothing to do
Saurav Das1547b3f2017-05-05 17:01:08 -07001156 log.debug("buckets already exist {} in next: {} ..ignoring bucket add",
1157 duplicateBuckets, nextObjective.id());
1158 pass(nextObjective);
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001159 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001160 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001161
Saurav Das1a129a02016-11-18 15:21:57 -08001162 if (nextObjective.type() == NextObjective.Type.HASHED) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001163 addBucketToHashGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001164 } else if (nextObjective.type() == NextObjective.Type.BROADCAST) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001165 addBucketToBroadcastGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001166 }
1167 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001168
Saurav Das1a129a02016-11-18 15:21:57 -08001169 private void addBucketToHashGroup(NextObjective nextObjective,
Yi Tseng78f51f42017-02-02 13:54:58 -08001170 List<Deque<GroupKey>> allActiveKeys) {
Charles Chan188ebf52015-12-23 00:15:11 -08001171 // storage for all group keys in the chain of groups created
1172 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
1173 List<GroupInfo> unsentGroups = new ArrayList<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001174 List<GroupBucket> newBuckets;
Charles Chan188ebf52015-12-23 00:15:11 -08001175 createHashBucketChains(nextObjective, allGroupKeys, unsentGroups);
1176
Yi Tseng78f51f42017-02-02 13:54:58 -08001177 // now we can create the buckets to add to the outermost L3 ECMP group
1178 newBuckets = generateNextGroupBuckets(unsentGroups, SELECT);
Charles Chan188ebf52015-12-23 00:15:11 -08001179
Saurav Das1a129a02016-11-18 15:21:57 -08001180 // retrieve the original L3 ECMP group
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001181 Group l3ecmpGroup = retrieveTopLevelGroup(allActiveKeys, deviceId,
1182 groupService, nextObjective.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001183 if (l3ecmpGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001184 fail(nextObjective, ObjectiveError.GROUPMISSING);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001185 return;
1186 }
Saurav Das1a129a02016-11-18 15:21:57 -08001187 GroupKey l3ecmpGroupKey = l3ecmpGroup.appCookie();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001188 int l3ecmpGroupId = l3ecmpGroup.id().id();
Charles Chan188ebf52015-12-23 00:15:11 -08001189
1190 // Although GroupDescriptions are not necessary for adding buckets to
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001191 // existing groups, we still use one in the GroupChainElem. When the latter is
Charles Chan188ebf52015-12-23 00:15:11 -08001192 // processed, the info will be extracted for the bucketAdd call to groupService
1193 GroupDescription l3ecmpGroupDesc =
Yi Tsengef19de12017-04-24 11:33:05 -07001194 new DefaultGroupDescription(deviceId,
1195 SELECT,
1196 new GroupBuckets(newBuckets),
1197 l3ecmpGroupKey,
1198 l3ecmpGroupId,
1199 nextObjective.appId());
Yi Tseng78f51f42017-02-02 13:54:58 -08001200 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
1201 unsentGroups.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001202 true,
1203 deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001204
Saurav Dasc88d4662017-05-15 15:34:25 -07001205 // update new bucket-chains
1206 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
1207 for (Deque<GroupKey> newBucketChain : allGroupKeys) {
1208 newBucketChain.addFirst(l3ecmpGroupKey);
1209 addedKeys.add(newBucketChain);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001210 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001211 updatePendingNextObjective(l3ecmpGroupKey,
1212 new OfdpaNextGroup(addedKeys, nextObjective));
Yi Tsengef19de12017-04-24 11:33:05 -07001213 log.debug("Adding to L3ECMP: device:{} gid:{} group key:{} nextId:{}",
Charles Chan188ebf52015-12-23 00:15:11 -08001214 deviceId, Integer.toHexString(l3ecmpGroupId),
1215 l3ecmpGroupKey, nextObjective.id());
Yi Tseng78f51f42017-02-02 13:54:58 -08001216
1217 unsentGroups.forEach(groupInfo -> {
1218 // send the innermost group
1219 log.debug("Sending innermost group {} in group chain on device {} ",
Saurav Dasc88d4662017-05-15 15:34:25 -07001220 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
1221 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001222 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3ecmpGce);
1223 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001224 });
Saurav Das1a129a02016-11-18 15:21:57 -08001225 }
Charles Chan188ebf52015-12-23 00:15:11 -08001226
Saurav Das1a129a02016-11-18 15:21:57 -08001227 private void addBucketToBroadcastGroup(NextObjective nextObj,
Yi Tsengef19de12017-04-24 11:33:05 -07001228 List<Deque<GroupKey>> allActiveKeys) {
1229 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001230 if (assignedVlan == null) {
1231 log.warn("VLAN ID required by broadcast next obj is missing. "
1232 + "Aborting add bucket to broadcast group for next:{} in dev:{}",
1233 nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001234 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001235 return;
1236 }
Saurav Das1a129a02016-11-18 15:21:57 -08001237 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Yi Tsengef19de12017-04-24 11:33:05 -07001238 IpPrefix ipDst = readIpDstFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001239 if (ipDst != null) {
1240 if (ipDst.isMulticast()) {
Yi Tsengef19de12017-04-24 11:33:05 -07001241 addBucketToL3MulticastGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001242 } else {
1243 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001244 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001245 }
1246 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001247 addBucketToL2FloodGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001248 }
1249 }
1250
1251 private void addBucketToL2FloodGroup(NextObjective nextObj,
1252 List<Deque<GroupKey>> allActiveKeys,
1253 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001254 VlanId assignedVlan) {
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001255 Group l2FloodGroup = retrieveTopLevelGroup(allActiveKeys, deviceId,
1256 groupService, nextObj.id());
Yi Tseng78f51f42017-02-02 13:54:58 -08001257
1258 if (l2FloodGroup == null) {
1259 log.warn("Can't find L2 flood group while adding bucket to it. NextObj = {}",
1260 nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001261 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001262 return;
1263 }
Saurav Das1a129a02016-11-18 15:21:57 -08001264
Yi Tseng78f51f42017-02-02 13:54:58 -08001265 GroupKey l2floodGroupKey = l2FloodGroup.appCookie();
1266 int l2floodGroupId = l2FloodGroup.id().id();
1267 List<GroupBucket> newBuckets = generateNextGroupBuckets(groupInfos, ALL);
1268
1269 GroupDescription l2FloodGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001270 new DefaultGroupDescription(deviceId,
1271 ALL,
1272 new GroupBuckets(newBuckets),
1273 l2floodGroupKey,
1274 l2floodGroupId,
1275 nextObj.appId());
Saurav Das1a129a02016-11-18 15:21:57 -08001276
Yi Tseng78f51f42017-02-02 13:54:58 -08001277 GroupChainElem l2FloodGroupChainElement =
1278 new GroupChainElem(l2FloodGroupDescription,
1279 groupInfos.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001280 true,
1281 deviceId);
Yi Tseng78f51f42017-02-02 13:54:58 -08001282
Yi Tseng78f51f42017-02-02 13:54:58 -08001283
1284 //ensure assignedVlan applies to the chosen group
1285 VlanId floodGroupVlan = extractVlanIdFromGroupId(l2floodGroupId);
1286
1287 if (!floodGroupVlan.equals(assignedVlan)) {
1288 log.warn("VLAN ID {} does not match Flood group {} to which bucket is "
1289 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1290 Integer.toHexString(l2floodGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001291 fail(nextObj, ObjectiveError.BADPARAMS);
Yi Tseng78f51f42017-02-02 13:54:58 -08001292 return;
1293 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001294 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
Yi Tseng78f51f42017-02-02 13:54:58 -08001295 groupInfos.forEach(groupInfo -> {
1296 // update original NextGroup with new bucket-chain
Yi Tseng78f51f42017-02-02 13:54:58 -08001297 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001298 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001299 newBucketChain.addFirst(l2floodGroupKey);
Saurav Dasc88d4662017-05-15 15:34:25 -07001300 addedKeys.add(newBucketChain);
Yi Tseng78f51f42017-02-02 13:54:58 -08001301
Yi Tsengef19de12017-04-24 11:33:05 -07001302 log.debug("Adding to L2FLOOD: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001303 deviceId, Integer.toHexString(l2floodGroupId),
1304 l2floodGroupKey, nextObj.id());
1305 // send the innermost group
1306 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001307 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001308 deviceId);
1309
Yi Tsengef19de12017-04-24 11:33:05 -07001310 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l2FloodGroupChainElement);
Yi Tseng78f51f42017-02-02 13:54:58 -08001311
Yi Tsengef19de12017-04-24 11:33:05 -07001312 DeviceId innerMostGroupDevice = groupInfo.innerMostGroupDesc().deviceId();
1313 GroupKey innerMostGroupKey = groupInfo.innerMostGroupDesc().appCookie();
Yi Tseng78f51f42017-02-02 13:54:58 -08001314 Group existsL2IGroup = groupService.getGroup(innerMostGroupDevice, innerMostGroupKey);
1315
1316 if (existsL2IGroup != null) {
1317 // group already exist
1318 processPendingAddGroupsOrNextObjs(innerMostGroupKey, true);
1319 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001320 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001321 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001322 });
Saurav Dasc88d4662017-05-15 15:34:25 -07001323
1324 updatePendingNextObjective(l2floodGroupKey,
1325 new OfdpaNextGroup(addedKeys, nextObj));
Yi Tseng78f51f42017-02-02 13:54:58 -08001326 }
1327
Saurav Das1a129a02016-11-18 15:21:57 -08001328 private void addBucketToL3MulticastGroup(NextObjective nextObj,
1329 List<Deque<GroupKey>> allActiveKeys,
1330 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001331 VlanId assignedVlan) {
Pier Luigi21fffd22018-01-19 10:24:53 +01001332 // Create the buckets to add to the outermost L3 Multicast group
1333 List<GroupBucket> newBuckets = createL3MulticastBucket(groupInfos);
Saurav Das1a129a02016-11-18 15:21:57 -08001334
1335 // get the group being edited
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001336 Group l3mcastGroup = retrieveTopLevelGroup(allActiveKeys, deviceId,
1337 groupService, nextObj.id());
Saurav Das1a129a02016-11-18 15:21:57 -08001338 if (l3mcastGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001339 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001340 return;
1341 }
1342 GroupKey l3mcastGroupKey = l3mcastGroup.appCookie();
1343 int l3mcastGroupId = l3mcastGroup.id().id();
1344
1345 //ensure assignedVlan applies to the chosen group
Yi Tseng78f51f42017-02-02 13:54:58 -08001346 VlanId expectedVlan = extractVlanIdFromGroupId(l3mcastGroupId);
Saurav Das1a129a02016-11-18 15:21:57 -08001347 if (!expectedVlan.equals(assignedVlan)) {
1348 log.warn("VLAN ID {} does not match L3 Mcast group {} to which bucket is "
1349 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1350 Integer.toHexString(l3mcastGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001351 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001352 }
1353 GroupDescription l3mcastGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001354 new DefaultGroupDescription(deviceId,
1355 ALL,
1356 new GroupBuckets(newBuckets),
1357 l3mcastGroupKey,
1358 l3mcastGroupId,
1359 nextObj.appId());
Saurav Das1a129a02016-11-18 15:21:57 -08001360 GroupChainElem l3mcastGce = new GroupChainElem(l3mcastGroupDescription,
Yi Tsengef19de12017-04-24 11:33:05 -07001361 groupInfos.size(),
1362 true,
1363 deviceId);
Saurav Dasc88d4662017-05-15 15:34:25 -07001364
1365 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
Yi Tseng78f51f42017-02-02 13:54:58 -08001366 groupInfos.forEach(groupInfo -> {
1367 // update original NextGroup with new bucket-chain
1368 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001369 newBucketChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001370 // Add L3 interface group to the chain if there is one.
Yi Tsengef19de12017-04-24 11:33:05 -07001371 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1372 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001373 }
1374 newBucketChain.addFirst(l3mcastGroupKey);
Saurav Dasc88d4662017-05-15 15:34:25 -07001375 addedKeys.add(newBucketChain);
Yi Tseng78f51f42017-02-02 13:54:58 -08001376
Yi Tsengef19de12017-04-24 11:33:05 -07001377 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3mcastGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001378 // Point next group to inner-most group, if any
Yi Tsengef19de12017-04-24 11:33:05 -07001379 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1380 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(),
1381 1,
1382 false,
1383 deviceId);
1384 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001385 }
Yi Tsengef19de12017-04-24 11:33:05 -07001386 log.debug("Adding to L3MCAST: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001387 deviceId, Integer.toHexString(l3mcastGroupId),
1388 l3mcastGroupKey, nextObj.id());
1389 // send the innermost group
1390 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001391 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001392 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001393 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001394
1395 });
1396
Saurav Das1a129a02016-11-18 15:21:57 -08001397 updatePendingNextObjective(l3mcastGroupKey,
Saurav Dasc88d4662017-05-15 15:34:25 -07001398 new OfdpaNextGroup(addedKeys, nextObj));
Charles Chan188ebf52015-12-23 00:15:11 -08001399 }
1400
1401 /**
Saurav Dasceccf242017-08-03 18:30:35 -07001402 * Removes buckets in the top level group of a possible group-chain. Does
Saurav Das1a129a02016-11-18 15:21:57 -08001403 * not remove the groups in the group-chain pointed to by this bucket, as they
Charles Chan188ebf52015-12-23 00:15:11 -08001404 * may be in use (referenced by other groups) elsewhere.
1405 *
Saurav Dasceccf242017-08-03 18:30:35 -07001406 * @param nextObjective a next objective that contains information for the
1407 * buckets to be removed from the group
1408 * @param next the representation of the existing group-chains for this next
1409 * objective, from which the top-level buckets to remove are determined
Charles Chan188ebf52015-12-23 00:15:11 -08001410 */
1411 protected void removeBucketFromGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001412 if (nextObjective.type() != NextObjective.Type.HASHED &&
1413 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001414 log.warn("RemoveBuckets not applied to nextType:{} in dev:{} for next:{}",
1415 nextObjective.type(), deviceId, nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -07001416 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001417 return;
1418 }
Yi Tsengef19de12017-04-24 11:33:05 -07001419 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Saurav Dasceccf242017-08-03 18:30:35 -07001420 List<Integer> indicesToRemove = Lists.newArrayList();
1421 for (TrafficTreatment treatment : nextObjective.next()) {
1422 // find the top-level bucket in the group-chain by matching the
1423 // outport and label from different groups in the chain
1424 PortNumber portToRemove = readOutPortFromTreatment(treatment);
1425 int labelToRemove = readLabelFromTreatment(treatment);
1426 if (portToRemove == null) {
1427 log.warn("treatment {} of next objective {} has no outport.. "
1428 + "cannot remove bucket from group in dev: {}", treatment,
1429 nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001430 continue;
1431 }
Saurav Dasceccf242017-08-03 18:30:35 -07001432 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1433 groupService, deviceId,
1434 portToRemove, labelToRemove);
1435 indicesToRemove.addAll(existing);
1436
Charles Chan188ebf52015-12-23 00:15:11 -08001437 }
Saurav Dasceccf242017-08-03 18:30:35 -07001438
1439 List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList();
1440 indicesToRemove.forEach(index -> chainsToRemove
1441 .add(allActiveKeys.get(index)));
Yi Tseng78f51f42017-02-02 13:54:58 -08001442 if (chainsToRemove.isEmpty()) {
Charles Chan188ebf52015-12-23 00:15:11 -08001443 log.warn("Could not find appropriate group-chain for removing bucket"
1444 + " for next id {} in dev:{}", nextObjective.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001445 fail(nextObjective, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001446 return;
Charles Chan188ebf52015-12-23 00:15:11 -08001447 }
Saurav Dasceccf242017-08-03 18:30:35 -07001448 removeBucket(chainsToRemove, nextObjective);
1449 }
1450
1451 /**
1452 * Removes top-level buckets from a group that represents the given next objective.
1453 *
1454 * @param chainsToRemove a list of group bucket chains to remove
1455 * @param nextObjective the next objective that contains information for the
1456 * buckets to be removed from the group
1457 */
1458 protected void removeBucket(List<Deque<GroupKey>> chainsToRemove,
1459 NextObjective nextObjective) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001460 List<GroupBucket> bucketsToRemove = Lists.newArrayList();
1461 //first group key is the one we want to modify
1462 GroupKey modGroupKey = chainsToRemove.get(0).peekFirst();
Saurav Das1a129a02016-11-18 15:21:57 -08001463 Group modGroup = groupService.getGroup(deviceId, modGroupKey);
Yi Tseng78f51f42017-02-02 13:54:58 -08001464 for (Deque<GroupKey> foundChain : chainsToRemove) {
1465 //second group key is the one we wish to remove the reference to
1466 if (foundChain.size() < 2) {
Saurav Dasceccf242017-08-03 18:30:35 -07001467 // additional check to make sure second group key exists in
Yi Tseng78f51f42017-02-02 13:54:58 -08001468 // the chain.
1469 log.warn("Can't find second group key from chain {}",
1470 foundChain);
1471 continue;
Saurav Das1a129a02016-11-18 15:21:57 -08001472 }
Saurav Dasceccf242017-08-03 18:30:35 -07001473 GroupKey pointedGroupKey = foundChain.stream()
1474 .collect(Collectors.toList()).get(1);
Yi Tseng78f51f42017-02-02 13:54:58 -08001475 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
Yi Tseng78f51f42017-02-02 13:54:58 -08001476 if (pointedGroup == null) {
1477 continue;
1478 }
1479
1480 GroupBucket bucket;
1481 if (nextObjective.type() == NextObjective.Type.HASHED) {
1482 bucket = DefaultGroupBucket.createSelectGroupBucket(
1483 DefaultTrafficTreatment.builder()
1484 .group(pointedGroup.id())
1485 .build());
1486 } else {
1487 bucket = DefaultGroupBucket.createAllGroupBucket(
1488 DefaultTrafficTreatment.builder()
1489 .group(pointedGroup.id())
1490 .build());
1491 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001492 bucketsToRemove.add(bucket);
Saurav Das1a129a02016-11-18 15:21:57 -08001493 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001494
1495 GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove);
1496 List<String> pointedGroupIds; // for debug log
1497 pointedGroupIds = bucketsToRemove.stream()
1498 .map(GroupBucket::treatment)
1499 .map(TrafficTreatment::allInstructions)
1500 .flatMap(List::stream)
1501 .filter(inst -> inst instanceof Instructions.GroupInstruction)
1502 .map(inst -> (Instructions.GroupInstruction) inst)
1503 .map(Instructions.GroupInstruction::groupId)
1504 .map(GroupId::id)
1505 .map(Integer::toHexString)
1506 .map(id -> HEX_PREFIX + id)
1507 .collect(Collectors.toList());
1508
Yi Tseng78f51f42017-02-02 13:54:58 -08001509 log.debug("Removing buckets from group id 0x{} pointing to group id(s) {} "
Saurav Das1a129a02016-11-18 15:21:57 -08001510 + "for next id {} in device {}", Integer.toHexString(modGroup.id().id()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001511 pointedGroupIds, nextObjective.id(), deviceId);
1512 addPendingUpdateNextObjective(modGroupKey, nextObjective);
Saurav Das1a129a02016-11-18 15:21:57 -08001513 groupService.removeBucketsFromGroup(deviceId, modGroupKey,
1514 removeBuckets, modGroupKey,
1515 nextObjective.appId());
Saurav Dasceccf242017-08-03 18:30:35 -07001516 // update store - synchronize access as there may be multiple threads
1517 // trying to remove buckets from the same group, each with its own
1518 // potentially stale copy of allActiveKeys
Saurav Dasc88d4662017-05-15 15:34:25 -07001519 synchronized (flowObjectiveStore) {
Saurav Dasceccf242017-08-03 18:30:35 -07001520 // get a fresh copy of what the store holds
1521 NextGroup next = flowObjectiveStore.getNextGroup(nextObjective.id());
1522 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Pier Luigid008dbe2018-01-15 09:48:49 +01001523 allActiveKeys = Lists.newArrayList(allActiveKeys);
Saurav Dasc88d4662017-05-15 15:34:25 -07001524 // Note that since we got a new object, and ArrayDeque does not implement
Saurav Dasceccf242017-08-03 18:30:35 -07001525 // Object.equals(), we have to check the deque elems one by one
1526 allActiveKeys
1527 .removeIf(active ->
1528 chainsToRemove.stream().anyMatch(remove ->
1529 Arrays.equals(remove.toArray(new GroupKey[0]),
1530 active.toArray(new GroupKey[0]))));
Saurav Dasc88d4662017-05-15 15:34:25 -07001531 // If no buckets in the group, then retain an entry for the
1532 // top level group which still exists.
1533 if (allActiveKeys.isEmpty()) {
1534 ArrayDeque<GroupKey> top = new ArrayDeque<>();
1535 top.add(modGroupKey);
1536 allActiveKeys.add(top);
1537 }
1538 flowObjectiveStore.putNextGroup(nextObjective.id(),
Saurav Dasceccf242017-08-03 18:30:35 -07001539 new OfdpaNextGroup(allActiveKeys,
1540 nextObjective));
Saurav Das1a129a02016-11-18 15:21:57 -08001541 }
Charles Chan188ebf52015-12-23 00:15:11 -08001542 }
1543
1544 /**
Saurav Das961beb22017-03-29 19:09:17 -07001545 * Removes all groups in multiple possible group-chains that represent the next-obj.
Charles Chan188ebf52015-12-23 00:15:11 -08001546 *
1547 * @param nextObjective the next objective to remove
1548 * @param next the NextGroup that represents the existing group-chain for
1549 * this next objective
1550 */
1551 protected void removeGroup(NextObjective nextObjective, NextGroup next) {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -07001552
Yi Tsengef19de12017-04-24 11:33:05 -07001553 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Charles Chanfc5c7802016-05-17 13:13:55 -07001554
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001555 List<GroupKey> groupKeys = allActiveKeys.stream()
Charles Chanfc5c7802016-05-17 13:13:55 -07001556 .map(Deque::getFirst).collect(Collectors.toList());
Yi Tsengef19de12017-04-24 11:33:05 -07001557 addPendingRemoveNextObjective(nextObjective, groupKeys);
Charles Chanfc5c7802016-05-17 13:13:55 -07001558
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -07001559 allActiveKeys
1560 .forEach(groupChain -> groupChain.forEach(groupKey -> groupService
1561 .removeGroup(deviceId, groupKey, nextObjective.appId())));
Charles Chan188ebf52015-12-23 00:15:11 -08001562 flowObjectiveStore.removeNextGroup(nextObjective.id());
1563 }
1564
Saurav Dasceccf242017-08-03 18:30:35 -07001565 /**
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001566 * Modify buckets in the L2 interface group.
1567 *
1568 * @param nextObjective a next objective that contains information for the
1569 * buckets to be modified in the group
1570 * @param next the representation of the existing group-chains for this next
1571 * objective, from which the innermost group buckets to remove are determined
1572 */
1573 protected void modifyBucketFromGroup(NextObjective nextObjective, NextGroup next) {
1574 if (nextObjective.type() != NextObjective.Type.SIMPLE) {
1575 log.warn("ModifyBucketFromGroup cannot be applied to nextType:{} in dev:{} for next:{}",
1576 nextObjective.type(), deviceId, nextObjective.id());
1577 fail(nextObjective, ObjectiveError.UNSUPPORTED);
1578 return;
1579 }
1580
1581 VlanId assignedVlan = readVlanFromSelector(nextObjective.meta());
1582 if (assignedVlan == null) {
1583 log.warn("VLAN ID required by simple next obj is missing. Abort.");
1584 fail(nextObjective, ObjectiveError.BADPARAMS);
1585 return;
1586 }
1587
1588 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObjective, assignedVlan);
1589
1590 // There is only one L2 interface group in this case
1591 GroupDescription l2InterfaceGroupDesc = groupInfos.get(0).innerMostGroupDesc();
1592
1593 // Replace group bucket for L2 interface group
1594 groupService.setBucketsForGroup(deviceId,
1595 l2InterfaceGroupDesc.appCookie(),
1596 l2InterfaceGroupDesc.buckets(),
1597 l2InterfaceGroupDesc.appCookie(),
1598 l2InterfaceGroupDesc.appId());
1599
1600 // update store - synchronize access as there may be multiple threads
1601 // trying to remove buckets from the same group, each with its own
1602 // potentially stale copy of allActiveKeys
1603 synchronized (flowObjectiveStore) {
1604 List<Deque<GroupKey>> modifiedGroupKeys = Lists.newArrayList();
1605 ArrayDeque<GroupKey> top = new ArrayDeque<>();
1606 top.add(l2InterfaceGroupDesc.appCookie());
1607 modifiedGroupKeys.add(top);
1608
1609 flowObjectiveStore.putNextGroup(nextObjective.id(),
1610 new OfdpaNextGroup(modifiedGroupKeys,
1611 nextObjective));
1612 }
1613 }
1614
1615 /**
Saurav Dasceccf242017-08-03 18:30:35 -07001616 * Checks existing buckets in {@link NextGroup} to verify if they match
1617 * the buckets in the given {@link NextObjective}. Adds or removes buckets
1618 * to ensure that the buckets match up.
1619 *
1620 * @param nextObjective the next objective to verify
1621 * @param next the representation of the existing group which has to be
1622 * modified to match the given next objective
1623 */
1624 protected void verifyGroup(NextObjective nextObjective, NextGroup next) {
Pier Luigib72201b2018-01-25 16:16:02 +01001625 if (nextObjective.type() == NextObjective.Type.SIMPLE) {
1626 log.warn("verification not supported for indirect group");
Saurav Dasceccf242017-08-03 18:30:35 -07001627 fail(nextObjective, ObjectiveError.UNSUPPORTED);
1628 return;
1629 }
1630 log.debug("Call to verify device:{} nextId:{}", deviceId, nextObjective.id());
1631 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
1632 List<TrafficTreatment> bucketsToCreate = Lists.newArrayList();
1633 List<Integer> indicesToRemove = Lists.newArrayList();
Pier Luigiec6ac422018-01-29 10:30:59 +01001634
1635 // Iterating over the treatments of the next objective allows
1636 // to detect missing buckets and/or duplicate buckets (to be removed)
Saurav Dasceccf242017-08-03 18:30:35 -07001637 for (TrafficTreatment bkt : nextObjective.next()) {
1638 PortNumber portNumber = readOutPortFromTreatment(bkt);
1639 int label = readLabelFromTreatment(bkt);
1640 if (portNumber == null) {
1641 log.warn("treatment {} of next objective {} has no outport.. "
1642 + "cannot remove bucket from group in dev: {}", bkt,
1643 nextObjective.id(), deviceId);
1644 fail(nextObjective, ObjectiveError.BADPARAMS);
1645 return;
1646 }
1647 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1648 groupService, deviceId,
1649 portNumber, label);
1650 if (existing.isEmpty()) {
1651 // if it doesn't exist, mark this bucket for creation
1652 bucketsToCreate.add(bkt);
1653 }
1654 if (existing.size() > 1) {
1655 // if it exists but there are duplicates, mark the others for removal
1656 existing.remove(0);
1657 indicesToRemove.addAll(existing);
1658 }
1659 }
1660
Pier Luigiec6ac422018-01-29 10:30:59 +01001661 // Detect situation where the next data has more buckets
1662 // (not duplicates) respect to the next objective
Saurav Dasa4020382018-02-14 14:14:54 -08001663 if (allActiveKeys.size() > nextObjective.next().size() &&
1664 // ignore specific case of empty group
1665 !(nextObjective.next().size() == 0 && allActiveKeys.size() == 1
1666 && allActiveKeys.get(0).size() == 1)) {
Pier Luigiec6ac422018-01-29 10:30:59 +01001667 log.warn("Mismatch detected between next and flowobjstore for device {}: " +
Saurav Dasa4020382018-02-14 14:14:54 -08001668 "nextId:{}, nextObjective-size:{} next-size:{} .. correcting",
1669 deviceId, nextObjective.id(), nextObjective.next().size(),
1670 allActiveKeys.size());
1671 List<Integer> otherIndices =
1672 indicesToRemoveFromNextGroup(allActiveKeys, nextObjective,
1673 groupService, deviceId);
Pier Luigiec6ac422018-01-29 10:30:59 +01001674 // Filter out the indices not present
1675 otherIndices = otherIndices.stream()
1676 .filter(index -> !indicesToRemove.contains(index))
1677 .collect(Collectors.toList());
1678 // Add all to the final list
1679 indicesToRemove.addAll(otherIndices);
1680 }
1681
Pier Luigib72201b2018-01-25 16:16:02 +01001682 log.debug("Buckets to create {}", bucketsToCreate);
1683 log.debug("Indices to remove {}", indicesToRemove);
1684
Saurav Dasceccf242017-08-03 18:30:35 -07001685 if (!bucketsToCreate.isEmpty()) {
1686 log.info("creating {} buckets as part of nextId: {} verification",
1687 bucketsToCreate.size(), nextObjective.id());
1688 //create a nextObjective only with these buckets
1689 NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder()
1690 .withId(nextObjective.id())
Pier Luigib72201b2018-01-25 16:16:02 +01001691 .withType(nextObjective.type())
Saurav Dasceccf242017-08-03 18:30:35 -07001692 .withMeta(nextObjective.meta())
1693 .fromApp(nextObjective.appId());
Pier Luigib72201b2018-01-25 16:16:02 +01001694 bucketsToCreate.forEach(nextObjBuilder::addTreatment);
1695 // According to the next type we call the proper add function
1696 if (nextObjective.type() == NextObjective.Type.HASHED) {
1697 addBucketToHashGroup(nextObjBuilder.addToExisting(), allActiveKeys);
1698 } else {
1699 addBucketToBroadcastGroup(nextObjBuilder.addToExisting(), allActiveKeys);
1700 }
Saurav Dasceccf242017-08-03 18:30:35 -07001701 }
1702
1703 if (!indicesToRemove.isEmpty()) {
1704 log.info("removing {} buckets as part of nextId: {} verification",
1705 indicesToRemove.size(), nextObjective.id());
1706 List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList();
1707 indicesToRemove.forEach(index -> chainsToRemove
1708 .add(allActiveKeys.get(index)));
1709 removeBucket(chainsToRemove, nextObjective);
1710 }
1711
Saurav Das9df5b7c2017-08-14 16:44:43 -07001712 if (bucketsToCreate.isEmpty() && indicesToRemove.isEmpty()) {
1713 // flowObjective store record is in-sync with nextObjective passed-in
1714 // Nevertheless groupStore may not be in sync due to bug in the store
1715 // - see CORD-1844. XXX When this bug is fixed, the rest of this verify
1716 // method will not be required.
Pier Luigib72201b2018-01-25 16:16:02 +01001717 GroupKey topGroupKey = allActiveKeys.get(0).peekFirst();
1718 Group topGroup = groupService.getGroup(deviceId, topGroupKey);
1719 int actualGroupSize = topGroup.buckets().buckets().size();
Saurav Das9df5b7c2017-08-14 16:44:43 -07001720 int objGroupSize = nextObjective.next().size();
1721 if (actualGroupSize != objGroupSize) {
1722 log.warn("Mismatch detected in device:{}, nextId:{}, nextObjective-size"
1723 + ":{} group-size:{} .. correcting", deviceId, nextObjective.id(),
1724 objGroupSize, actualGroupSize);
1725 }
1726 if (actualGroupSize > objGroupSize) {
Pier Luigib72201b2018-01-25 16:16:02 +01001727 // Group in the device has more chains
Saurav Das9df5b7c2017-08-14 16:44:43 -07001728 List<GroupBucket> bucketsToRemove = Lists.newArrayList();
1729 //check every bucket in the actual group
Pier Luigib72201b2018-01-25 16:16:02 +01001730 for (GroupBucket bucket : topGroup.buckets().buckets()) {
Saurav Das9df5b7c2017-08-14 16:44:43 -07001731 GroupInstruction g = (GroupInstruction) bucket.treatment()
1732 .allInstructions().iterator().next();
1733 GroupId gidToCheck = g.groupId(); // the group pointed to
1734 boolean matches = false;
1735 for (Deque<GroupKey> validChain : allActiveKeys) {
1736 if (validChain.size() < 2) {
1737 continue;
1738 }
1739 GroupKey pointedGroupKey = validChain.stream()
1740 .collect(Collectors.toList()).get(1);
1741 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
1742 if (pointedGroup != null && gidToCheck.equals(pointedGroup.id())) {
1743 matches = true;
1744 break;
1745 }
1746 }
1747 if (!matches) {
1748 log.warn("Removing bucket pointing to groupId:{}", gidToCheck);
1749 bucketsToRemove.add(bucket);
1750 }
1751 }
1752 // remove buckets for which there was no record in the obj store
1753 if (bucketsToRemove.isEmpty()) {
1754 log.warn("Mismatch detected but could not determine which"
1755 + "buckets to remove");
1756 } else {
1757 GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove);
Pier Luigib72201b2018-01-25 16:16:02 +01001758 groupService.removeBucketsFromGroup(deviceId, topGroupKey,
1759 removeBuckets, topGroupKey,
Saurav Das9df5b7c2017-08-14 16:44:43 -07001760 nextObjective.appId());
1761 }
1762 } else if (actualGroupSize < objGroupSize) {
Pier Luigib72201b2018-01-25 16:16:02 +01001763 // Group in the device has less chains
Saurav Das9df5b7c2017-08-14 16:44:43 -07001764 // should also add buckets not in group-store but in obj-store
1765 List<GroupBucket> bucketsToAdd = Lists.newArrayList();
1766 //check every bucket in the obj
1767 for (Deque<GroupKey> validChain : allActiveKeys) {
1768 if (validChain.size() < 2) {
1769 continue;
1770 }
1771 GroupKey pointedGroupKey = validChain.stream()
1772 .collect(Collectors.toList()).get(1);
1773 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
1774 if (pointedGroup == null) {
1775 // group should exist, otherwise cannot be added as bucket
1776 continue;
1777 }
1778 boolean matches = false;
Pier Luigib72201b2018-01-25 16:16:02 +01001779 for (GroupBucket bucket : topGroup.buckets().buckets()) {
Saurav Das9df5b7c2017-08-14 16:44:43 -07001780 GroupInstruction g = (GroupInstruction) bucket.treatment()
1781 .allInstructions().iterator().next();
1782 GroupId gidToCheck = g.groupId(); // the group pointed to
1783 if (pointedGroup.id().equals(gidToCheck)) {
1784 matches = true;
1785 break;
1786 }
1787 }
1788 if (!matches) {
1789 log.warn("Adding bucket pointing to groupId:{}", pointedGroup);
1790 TrafficTreatment t = DefaultTrafficTreatment.builder()
1791 .group(pointedGroup.id())
1792 .build();
Pier Luigib72201b2018-01-25 16:16:02 +01001793 // Create the proper bucket according to the next type
1794 if (nextObjective.type() == NextObjective.Type.HASHED) {
1795 bucketsToAdd.add(DefaultGroupBucket.createSelectGroupBucket(t));
1796 } else {
1797 bucketsToAdd.add(DefaultGroupBucket.createAllGroupBucket(t));
1798 }
Saurav Das9df5b7c2017-08-14 16:44:43 -07001799 }
1800 }
1801 if (bucketsToAdd.isEmpty()) {
1802 log.warn("Mismatch detected but could not determine which "
1803 + "buckets to add");
1804 } else {
1805 GroupBuckets addBuckets = new GroupBuckets(bucketsToAdd);
Pier Luigib72201b2018-01-25 16:16:02 +01001806 groupService.addBucketsToGroup(deviceId, topGroupKey,
1807 addBuckets, topGroupKey,
Saurav Das9df5b7c2017-08-14 16:44:43 -07001808 nextObjective.appId());
1809 }
1810 }
1811 }
1812
Saurav Dasceccf242017-08-03 18:30:35 -07001813 pass(nextObjective);
1814 }
1815
1816 //////////////////////////////////////
1817 // Helper methods and classes
1818 //////////////////////////////////////
1819
Yi Tsengef19de12017-04-24 11:33:05 -07001820 protected void updatePendingNextObjective(GroupKey groupKey, OfdpaNextGroup nextGrp) {
1821 pendingAddNextObjectives.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07001822 if (val == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001823 val = new CopyOnWriteArrayList<>();
Saurav Das961beb22017-03-29 19:09:17 -07001824 }
Yi Tsengef19de12017-04-24 11:33:05 -07001825 val.add(nextGrp);
Saurav Das961beb22017-03-29 19:09:17 -07001826 return val;
1827 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08001828 }
1829
Yi Tsengef19de12017-04-24 11:33:05 -07001830 protected void updatePendingGroups(GroupKey groupKey, GroupChainElem gce) {
1831 pendingGroups.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07001832 if (val == null) {
1833 val = Sets.newConcurrentHashSet();
1834 }
1835 val.add(gce);
1836 return val;
1837 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08001838 }
1839
Yi Tsengef19de12017-04-24 11:33:05 -07001840 protected void addPendingUpdateNextObjective(GroupKey groupKey,
1841 NextObjective nextObjective) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001842 pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> {
1843 if (nextObjs != null) {
1844 nextObjs.add(nextObjective);
1845 } else {
1846 nextObjs = Sets.newHashSet(nextObjective);
1847 }
1848 return nextObjs;
1849 });
1850 }
1851
Yi Tsengef19de12017-04-24 11:33:05 -07001852 private void processPendingUpdateNextObjs(GroupKey groupKey) {
1853 pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> {
1854 if (nextObjs != null) {
1855
1856 nextObjs.forEach(nextObj -> {
1857 log.debug("Group {} updated, update pending next objective {}.",
1858 groupKey, nextObj);
1859
1860 pass(nextObj);
1861 });
1862 }
1863 return Sets.newHashSet();
1864 });
1865 }
1866
1867 private void processPendingRemoveNextObjs(GroupKey key) {
1868 pendingRemoveNextObjectives.asMap().forEach((nextObjective, groupKeys) -> {
1869 if (groupKeys.isEmpty()) {
1870 pendingRemoveNextObjectives.invalidate(nextObjective);
1871 pass(nextObjective);
1872 } else {
1873 groupKeys.remove(key);
1874 }
1875 });
1876 }
1877
1878 protected int getNextAvailableIndex() {
1879 return (int) nextIndex.incrementAndGet();
1880 }
1881
Yi Tsengef19de12017-04-24 11:33:05 -07001882 protected void processPendingAddGroupsOrNextObjs(GroupKey key, boolean added) {
1883 //first check for group chain
1884 Set<OfdpaGroupHandlerUtility.GroupChainElem> gceSet = pendingGroups.asMap().remove(key);
1885 if (gceSet != null) {
1886 for (GroupChainElem gce : gceSet) {
1887 log.debug("Group service {} group key {} in device {}. "
1888 + "Processing next group in group chain with group id 0x{}",
1889 (added) ? "ADDED" : "processed",
1890 key, deviceId,
1891 Integer.toHexString(gce.groupDescription().givenGroupId()));
1892 processGroupChain(gce);
1893 }
1894 } else {
1895 // otherwise chain complete - check for waiting nextObjectives
1896 List<OfdpaGroupHandlerUtility.OfdpaNextGroup> nextGrpList =
1897 pendingAddNextObjectives.getIfPresent(key);
1898 if (nextGrpList != null) {
1899 pendingAddNextObjectives.invalidate(key);
1900 nextGrpList.forEach(nextGrp -> {
1901 log.debug("Group service {} group key {} in device:{}. "
1902 + "Done implementing next objective: {} <<-->> gid:0x{}",
1903 (added) ? "ADDED" : "processed",
1904 key, deviceId, nextGrp.nextObjective().id(),
1905 Integer.toHexString(groupService.getGroup(deviceId, key)
1906 .givenGroupId()));
1907 pass(nextGrp.nextObjective());
Saurav Dasc88d4662017-05-15 15:34:25 -07001908 updateFlowObjectiveStore(nextGrp.nextObjective().id(), nextGrp);
Yi Tsengef19de12017-04-24 11:33:05 -07001909
1910 // check if addBuckets waiting for this completion
1911 pendingBuckets.compute(nextGrp.nextObjective().id(), (nextId, pendBkts) -> {
1912 if (pendBkts != null) {
1913 pendBkts.forEach(pendBkt -> addBucketToGroup(pendBkt, nextGrp));
1914 }
1915 return null;
1916 });
1917 });
1918 }
1919 }
1920 }
1921
Charles Chan188ebf52015-12-23 00:15:11 -08001922 /**
1923 * Processes next element of a group chain. Assumption is that if this
1924 * group points to another group, the latter has already been created
1925 * and this driver has received notification for it. A second assumption is
1926 * that if there is another group waiting for this group then the appropriate
1927 * stores already have the information to act upon the notification for the
1928 * creation of this group.
1929 * <p>
1930 * The processing of the GroupChainElement depends on the number of groups
1931 * this element is waiting on. For all group types other than SIMPLE, a
1932 * GroupChainElement could be waiting on multiple groups.
1933 *
1934 * @param gce the group chain element to be processed next
1935 */
1936 private void processGroupChain(GroupChainElem gce) {
1937 int waitOnGroups = gce.decrementAndGetGroupsWaitedOn();
1938 if (waitOnGroups != 0) {
1939 log.debug("GCE: {} not ready to be processed", gce);
1940 return;
1941 }
1942 log.debug("GCE: {} ready to be processed", gce);
Yi Tsengef19de12017-04-24 11:33:05 -07001943 if (gce.addBucketToGroup()) {
1944 groupService.addBucketsToGroup(gce.groupDescription().deviceId(),
1945 gce.groupDescription().appCookie(),
1946 gce.groupDescription().buckets(),
1947 gce.groupDescription().appCookie(),
1948 gce.groupDescription().appId());
Charles Chan188ebf52015-12-23 00:15:11 -08001949 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001950 groupService.addGroup(gce.groupDescription());
Charles Chan188ebf52015-12-23 00:15:11 -08001951 }
1952 }
1953
Yi Tsengef19de12017-04-24 11:33:05 -07001954 protected void addPendingRemoveNextObjective(NextObjective nextObjective,
1955 List<GroupKey> groupKeys) {
1956 pendingRemoveNextObjectives.put(nextObjective, groupKeys);
Charles Chan188ebf52015-12-23 00:15:11 -08001957 }
1958
Saurav Dasc88d4662017-05-15 15:34:25 -07001959 private void updateFlowObjectiveStore(Integer nextId, OfdpaNextGroup nextGrp) {
1960 synchronized (flowObjectiveStore) {
1961 // get fresh copy of what the store holds
1962 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1963 if (next == null || nextGrp.nextObjective().op() == Operation.ADD) {
1964 flowObjectiveStore.putNextGroup(nextId, nextGrp);
1965 return;
1966 }
1967 if (nextGrp.nextObjective().op() == Operation.ADD_TO_EXISTING) {
1968 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Pier Luigid008dbe2018-01-15 09:48:49 +01001969 allActiveKeys = Lists.newArrayList(allActiveKeys);
Saurav Dasc88d4662017-05-15 15:34:25 -07001970 // If active keys shows only the top-level group without a chain of groups,
1971 // then it represents an empty group. Update by replacing empty chain.
1972 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1973 allActiveKeys.clear();
1974 }
1975 allActiveKeys.addAll(nextGrp.allKeys());
1976 flowObjectiveStore.putNextGroup(nextId,
1977 new OfdpaNextGroup(allActiveKeys, nextGrp.nextObjective()));
1978 }
1979 }
1980 }
1981
Saurav Das8be4e3a2016-03-11 17:19:07 -08001982 private class InnerGroupListener implements GroupListener {
1983 @Override
1984 public void event(GroupEvent event) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001985 switch (event.type()) {
1986 case GROUP_ADDED:
1987 processPendingAddGroupsOrNextObjs(event.subject().appCookie(), true);
1988 break;
1989 case GROUP_REMOVED:
1990 processPendingRemoveNextObjs(event.subject().appCookie());
1991 break;
Yi Tseng78f51f42017-02-02 13:54:58 -08001992 case GROUP_UPDATED:
1993 processPendingUpdateNextObjs(event.subject().appCookie());
1994 break;
Charles Chanfc5c7802016-05-17 13:13:55 -07001995 default:
1996 break;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001997 }
1998 }
1999 }
Charles Chan188ebf52015-12-23 00:15:11 -08002000}