blob: aaca49522c4af299315919948a954736978e7ad4 [file] [log] [blame]
Brian O'Connor7cbbbb72016-04-09 02:13:23 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
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;
45import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Yi Tseng47f82dc2017-03-05 22:48:39 -080046import org.onosproject.net.flowobjective.DefaultNextObjective;
Charles Chan188ebf52015-12-23 00:15:11 -080047import org.onosproject.net.flowobjective.FlowObjectiveStore;
48import org.onosproject.net.flowobjective.NextObjective;
Yi Tseng47f82dc2017-03-05 22:48:39 -080049import org.onosproject.net.flowobjective.ObjectiveContext;
Charles Chan188ebf52015-12-23 00:15:11 -080050import org.onosproject.net.flowobjective.ObjectiveError;
51import org.onosproject.net.group.DefaultGroupBucket;
52import org.onosproject.net.group.DefaultGroupDescription;
53import org.onosproject.net.group.DefaultGroupKey;
54import org.onosproject.net.group.Group;
55import org.onosproject.net.group.GroupBucket;
56import org.onosproject.net.group.GroupBuckets;
57import org.onosproject.net.group.GroupDescription;
58import org.onosproject.net.group.GroupEvent;
59import org.onosproject.net.group.GroupKey;
60import org.onosproject.net.group.GroupListener;
61import org.onosproject.net.group.GroupService;
Saurav Das8be4e3a2016-03-11 17:19:07 -080062import org.onosproject.store.service.AtomicCounter;
63import org.onosproject.store.service.StorageService;
Charles Chan188ebf52015-12-23 00:15:11 -080064import org.slf4j.Logger;
65
66import java.util.ArrayDeque;
67import java.util.ArrayList;
68import java.util.Collection;
69import java.util.Collections;
70import java.util.Deque;
71import java.util.List;
Charles Chand0fd5dc2016-02-16 23:14:49 -080072import java.util.Objects;
Charles Chan188ebf52015-12-23 00:15:11 -080073import java.util.Set;
74import java.util.concurrent.ConcurrentHashMap;
75import java.util.concurrent.CopyOnWriteArrayList;
76import java.util.concurrent.Executors;
77import java.util.concurrent.ScheduledExecutorService;
78import java.util.concurrent.TimeUnit;
Charles Chan188ebf52015-12-23 00:15:11 -080079import java.util.stream.Collectors;
80
81import static org.onlab.util.Tools.groupedThreads;
Yi Tsengef19de12017-04-24 11:33:05 -070082import static org.onosproject.driver.pipeline.ofdpa.Ofdpa2Pipeline.*;
83import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
Pier Ventre42287df2016-11-09 14:17:26 -080084import static org.onosproject.net.flow.criteria.Criterion.Type.TUNNEL_ID;
Pier Ventre140a8942016-11-02 07:26:38 -070085import static org.onosproject.net.flow.criteria.Criterion.Type.VLAN_VID;
Yi Tseng78f51f42017-02-02 13:54:58 -080086import static org.onosproject.net.group.GroupDescription.Type.ALL;
87import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Charles Chan188ebf52015-12-23 00:15:11 -080088import static org.slf4j.LoggerFactory.getLogger;
89
90/**
Saurav Das961beb22017-03-29 19:09:17 -070091 * Group handler that emulates Broadcom OF-DPA TTP.
Charles Chan188ebf52015-12-23 00:15:11 -080092 */
Charles Chan361154b2016-03-24 10:23:39 -070093public class Ofdpa2GroupHandler {
Yi Tsengef19de12017-04-24 11:33:05 -070094 protected final Logger log = getLogger(getClass());
Charles Chane849c192016-01-11 18:28:54 -080095
Yi Tsengef19de12017-04-24 11:33:05 -070096 // Services, Stores
Charles Chan188ebf52015-12-23 00:15:11 -080097 protected GroupService groupService;
Saurav Das8be4e3a2016-03-11 17:19:07 -080098 protected StorageService storageService;
Yi Tsengef19de12017-04-24 11:33:05 -070099 protected FlowObjectiveStore flowObjectiveStore;
Charles Chan188ebf52015-12-23 00:15:11 -0800100
101 // index number for group creation
Saurav Das8be4e3a2016-03-11 17:19:07 -0800102 private AtomicCounter nextIndex;
Charles Chan188ebf52015-12-23 00:15:11 -0800103
Yi Tsengef19de12017-04-24 11:33:05 -0700104 protected DeviceId deviceId;
105 private Cache<GroupKey, List<OfdpaGroupHandlerUtility.OfdpaNextGroup>> pendingAddNextObjectives;
106 private Cache<NextObjective, List<GroupKey>> pendingRemoveNextObjectives;
107 private Cache<GroupKey, Set<OfdpaGroupHandlerUtility.GroupChainElem>> pendingGroups;
108 private ConcurrentHashMap<GroupKey, Set<NextObjective>> pendingUpdateNextObjectives;
109
Yi Tseng47f82dc2017-03-05 22:48:39 -0800110 // local store for pending bucketAdds - by design there can be multiple
Charles Chan188ebf52015-12-23 00:15:11 -0800111 // pending bucket for a group
Yi Tseng47f82dc2017-03-05 22:48:39 -0800112 protected ConcurrentHashMap<Integer, Set<NextObjective>> pendingBuckets =
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700113 new ConcurrentHashMap<>();
Charles Chan188ebf52015-12-23 00:15:11 -0800114
Yi Tsengef19de12017-04-24 11:33:05 -0700115 private ScheduledExecutorService groupCheckerExecutor =
116 Executors.newScheduledThreadPool(2, groupedThreads("onos/pipeliner", "ofdpa-%d", log));
117
118 public Cache<GroupKey, List<OfdpaNextGroup>> pendingAddNextObjectives() {
119 return pendingAddNextObjectives;
120 }
121
122 public Cache<GroupKey, Set<GroupChainElem>> pendingGroups() {
123 return pendingGroups;
124 }
125
Charles Chan40132b32017-01-22 00:19:37 -0800126 /**
127 * Determines whether this pipeline support copy ttl instructions or not.
128 *
129 * @return true if copy ttl instructions are supported
130 */
131 protected boolean supportCopyTtl() {
132 return true;
133 }
134
135 /**
136 * Determines whether this pipeline support set mpls bos instruction or not.
137 *
138 * @return true if set mpls bos instruction is supported
139 */
140 protected boolean supportSetMplsBos() {
141 return true;
142 }
143
Charles Chan0f43e472017-02-14 14:00:16 -0800144 /**
145 * Determines whether this pipeline requires popping VLAN before pushing MPLS.
146 * <p>
147 * If required, pop vlan before push mpls and add an arbitrary vlan back afterward.
148 * MPLS interface group will substitute the arbitrary VLAN with expected VLAN later on.
149 *
150 * @return true if this pipeline requires popping VLAN before pushing MPLS
151 */
152 protected boolean requireVlanPopBeforeMplsPush() {
153 return false;
154 }
155
Charles Chan188ebf52015-12-23 00:15:11 -0800156 protected void init(DeviceId deviceId, PipelinerContext context) {
Yi Tsengef19de12017-04-24 11:33:05 -0700157 ServiceDirectory serviceDirectory = context.directory();
Charles Chan188ebf52015-12-23 00:15:11 -0800158 this.deviceId = deviceId;
159 this.flowObjectiveStore = context.store();
Charles Chan188ebf52015-12-23 00:15:11 -0800160 this.groupService = serviceDirectory.get(GroupService.class);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800161 this.storageService = serviceDirectory.get(StorageService.class);
Madan Jampanid5714e02016-04-19 14:15:20 -0700162 this.nextIndex = storageService.getAtomicCounter("group-id-index-counter");
Charles Chan188ebf52015-12-23 00:15:11 -0800163
Charles Chanfc5c7802016-05-17 13:13:55 -0700164 pendingAddNextObjectives = CacheBuilder.newBuilder()
Charles Chan188ebf52015-12-23 00:15:11 -0800165 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700166 .removalListener((RemovalNotification<GroupKey, List<OfdpaNextGroup>> notification) -> {
167 if (notification.getCause() == RemovalCause.EXPIRED &&
168 Objects.nonNull(notification.getValue())) {
169 notification.getValue()
170 .forEach(ofdpaNextGrp ->
171 fail(ofdpaNextGrp.nextObjective(),
172 ObjectiveError.GROUPINSTALLATIONFAILED));
Charles Chanfc5c7802016-05-17 13:13:55 -0700173 }
174 }).build();
Charles Chan188ebf52015-12-23 00:15:11 -0800175
Charles Chanfc5c7802016-05-17 13:13:55 -0700176 pendingRemoveNextObjectives = CacheBuilder.newBuilder()
177 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700178 .removalListener((RemovalNotification<NextObjective, List<GroupKey>> notification) -> {
Charles Chanfc5c7802016-05-17 13:13:55 -0700179 if (notification.getCause() == RemovalCause.EXPIRED) {
Yi Tsengef19de12017-04-24 11:33:05 -0700180 fail(notification.getKey(),
181 ObjectiveError.GROUPREMOVALFAILED);
Charles Chan188ebf52015-12-23 00:15:11 -0800182 }
183 }).build();
Saurav Das961beb22017-03-29 19:09:17 -0700184 pendingGroups = CacheBuilder.newBuilder()
185 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700186 .removalListener((RemovalNotification<GroupKey, Set<GroupChainElem>> notification) -> {
Saurav Das961beb22017-03-29 19:09:17 -0700187 if (notification.getCause() == RemovalCause.EXPIRED) {
188 log.error("Unable to install group with key {} and pending GCEs: {}",
189 notification.getKey(), notification.getValue());
190 }
191 }).build();
Yi Tseng78f51f42017-02-02 13:54:58 -0800192 pendingUpdateNextObjectives = new ConcurrentHashMap<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700193 GroupChecker groupChecker = new GroupChecker(this);
194 groupCheckerExecutor.scheduleAtFixedRate(groupChecker, 0, 500, TimeUnit.MILLISECONDS);
Charles Chan188ebf52015-12-23 00:15:11 -0800195 groupService.addListener(new InnerGroupListener());
196 }
197
Saurav Das8be4e3a2016-03-11 17:19:07 -0800198 //////////////////////////////////////
199 // Group Creation
200 //////////////////////////////////////
201
Yi Tsengef19de12017-04-24 11:33:05 -0700202 /**
203 * Adds a list of group chain by given NextObjective.
204 *
205 * @param nextObjective the NextObjective
206 */
Charles Chan188ebf52015-12-23 00:15:11 -0800207 protected void addGroup(NextObjective nextObjective) {
208 switch (nextObjective.type()) {
209 case SIMPLE:
210 Collection<TrafficTreatment> treatments = nextObjective.next();
211 if (treatments.size() != 1) {
212 log.error("Next Objectives of type Simple should only have a "
213 + "single Traffic Treatment. Next Objective Id:{}",
214 nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -0700215 fail(nextObjective, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800216 return;
217 }
218 processSimpleNextObjective(nextObjective);
219 break;
220 case BROADCAST:
221 processBroadcastNextObjective(nextObjective);
222 break;
223 case HASHED:
Pier Ventre140a8942016-11-02 07:26:38 -0700224 if (!verifyHashedNextObjective(nextObjective)) {
225 log.error("Next Objectives of type hashed not supported. Next Objective Id:{}",
226 nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -0700227 fail(nextObjective, ObjectiveError.BADPARAMS);
Pier Ventre140a8942016-11-02 07:26:38 -0700228 return;
229 }
Charles Chan188ebf52015-12-23 00:15:11 -0800230 processHashedNextObjective(nextObjective);
231 break;
232 case FAILOVER:
Yi Tsengef19de12017-04-24 11:33:05 -0700233 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -0800234 log.warn("Unsupported next objective type {}", nextObjective.type());
235 break;
236 default:
Yi Tsengef19de12017-04-24 11:33:05 -0700237 fail(nextObjective, ObjectiveError.UNKNOWN);
Charles Chan188ebf52015-12-23 00:15:11 -0800238 log.warn("Unknown next objective type {}", nextObjective.type());
239 }
240 }
241
242 /**
243 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
244 * a chain of groups. The simple Next Objective passed
245 * in by the application has to be broken up into a group chain
246 * comprising of an L3 Unicast Group that points to an L2 Interface
247 * Group which in-turn points to an output port. In some cases, the simple
248 * next Objective can just be an L2 interface without the need for chaining.
249 *
250 * @param nextObj the nextObjective of type SIMPLE
251 */
252 private void processSimpleNextObjective(NextObjective nextObj) {
253 TrafficTreatment treatment = nextObj.next().iterator().next();
254 // determine if plain L2 or L3->L2
255 boolean plainL2 = true;
256 for (Instruction ins : treatment.allInstructions()) {
257 if (ins.type() == Instruction.Type.L2MODIFICATION) {
258 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
259 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_DST ||
260 l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_SRC) {
261 plainL2 = false;
262 break;
263 }
264 }
265 }
266
267 if (plainL2) {
268 createL2InterfaceGroup(nextObj);
269 return;
270 }
271
Pier Ventre140a8942016-11-02 07:26:38 -0700272 boolean isMpls = false;
Pier Ventre42287df2016-11-09 14:17:26 -0800273 // In order to understand if it is a pseudo wire related
274 // next objective we look for the tunnel id in the meta.
275 boolean isPw = false;
Pier Ventre140a8942016-11-02 07:26:38 -0700276 if (nextObj.meta() != null) {
277 isMpls = isNotMplsBos(nextObj.meta());
Pier Ventre42287df2016-11-09 14:17:26 -0800278
279 TunnelIdCriterion tunnelIdCriterion = (TunnelIdCriterion) nextObj
280 .meta()
281 .getCriterion(TUNNEL_ID);
282 if (tunnelIdCriterion != null) {
283 isPw = true;
284 }
285
Pier Ventre140a8942016-11-02 07:26:38 -0700286 }
287
Pier Ventre42287df2016-11-09 14:17:26 -0800288 if (!isPw) {
289 // break up simple next objective to GroupChain objects
290 GroupInfo groupInfo = createL2L3Chain(treatment, nextObj.id(),
291 nextObj.appId(), isMpls,
292 nextObj.meta());
293 if (groupInfo == null) {
294 log.error("Could not process nextObj={} in dev:{}", nextObj.id(), deviceId);
295 return;
296 }
297 // create object for local and distributed storage
298 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700299 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
300 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Pier Ventre42287df2016-11-09 14:17:26 -0800301 OfdpaNextGroup ofdpaGrp =
302 new OfdpaNextGroup(Collections.singletonList(gkeyChain), nextObj);
303
304 // store l3groupkey with the ofdpaNextGroup for the nextObjective that depends on it
Yi Tsengef19de12017-04-24 11:33:05 -0700305 updatePendingNextObjective(groupInfo.nextGroupDesc().appCookie(), ofdpaGrp);
Pier Ventre42287df2016-11-09 14:17:26 -0800306
307 // now we are ready to send the l2 groupDescription (inner), as all the stores
308 // that will get async replies have been updated. By waiting to update
309 // the stores, we prevent nasty race conditions.
Yi Tsengef19de12017-04-24 11:33:05 -0700310 groupService.addGroup(groupInfo.innerMostGroupDesc());
Pier Ventre42287df2016-11-09 14:17:26 -0800311 } else {
312 // We handle the pseudo wire with a different a procedure.
313 // This procedure is meant to handle both initiation and
314 // termination of the pseudo wire.
315 processPwNextObjective(nextObj);
Charles Chan188ebf52015-12-23 00:15:11 -0800316 }
Charles Chan188ebf52015-12-23 00:15:11 -0800317 }
318
Charles Chan188ebf52015-12-23 00:15:11 -0800319 /**
320 * Creates a simple L2 Interface Group.
321 *
322 * @param nextObj the next Objective
323 */
324 private void createL2InterfaceGroup(NextObjective nextObj) {
Yi Tsengef19de12017-04-24 11:33:05 -0700325 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700326 if (assignedVlan == null) {
327 log.warn("VLAN ID required by simple next obj is missing. Abort.");
Yi Tsengef19de12017-04-24 11:33:05 -0700328 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800329 return;
330 }
331
Charles Chan5b9df8d2016-03-28 22:21:40 -0700332 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Charles Chan188ebf52015-12-23 00:15:11 -0800333
Charles Chan5b9df8d2016-03-28 22:21:40 -0700334 // There is only one L2 interface group in this case
Yi Tsengef19de12017-04-24 11:33:05 -0700335 GroupDescription l2InterfaceGroupDesc = groupInfos.get(0).innerMostGroupDesc();
Charles Chan188ebf52015-12-23 00:15:11 -0800336
Charles Chan5b9df8d2016-03-28 22:21:40 -0700337 // Put all dependency information into allGroupKeys
338 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
339 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
340 gkeyChain.addFirst(l2InterfaceGroupDesc.appCookie());
341 allGroupKeys.add(gkeyChain);
Charles Chan188ebf52015-12-23 00:15:11 -0800342
Charles Chan5b9df8d2016-03-28 22:21:40 -0700343 // Point the next objective to this group
344 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
345 updatePendingNextObjective(l2InterfaceGroupDesc.appCookie(), ofdpaGrp);
346
347 // Start installing the inner-most group
348 groupService.addGroup(l2InterfaceGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800349 }
350
351 /**
352 * Creates one of two possible group-chains from the treatment
353 * passed in. Depending on the MPLS boolean, this method either creates
Charles Chan425854b2016-04-11 15:32:12 -0700354 * an L3Unicast Group --&gt; L2Interface Group, if mpls is false;
355 * or MPLSInterface Group --&gt; L2Interface Group, if mpls is true;
Charles Chan188ebf52015-12-23 00:15:11 -0800356 * The returned 'inner' group description is always the L2 Interface group.
357 *
358 * @param treatment that needs to be broken up to create the group chain
359 * @param nextId of the next objective that needs this group chain
360 * @param appId of the application that sent this next objective
361 * @param mpls determines if L3Unicast or MPLSInterface group is created
362 * @param meta metadata passed in by the application as part of the nextObjective
363 * @return GroupInfo containing the GroupDescription of the
364 * L2Interface group(inner) and the GroupDescription of the (outer)
365 * L3Unicast/MPLSInterface group. May return null if there is an
366 * error in processing the chain
367 */
Charles Chan425854b2016-04-11 15:32:12 -0700368 protected GroupInfo createL2L3Chain(TrafficTreatment treatment, int nextId,
Charles Chanf9e98652016-09-07 16:54:23 -0700369 ApplicationId appId, boolean mpls,
370 TrafficSelector meta) {
371 return createL2L3ChainInternal(treatment, nextId, appId, mpls, meta, true);
372 }
373
374 /**
375 * Internal implementation of createL2L3Chain.
376 * <p>
377 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
378 * Since it is non-OF spec, we need an extension treatment for that.
379 * The useSetVlanExtension must be set to false for OFDPA i12.
380 * </p>
381 *
382 * @param treatment that needs to be broken up to create the group chain
383 * @param nextId of the next objective that needs this group chain
384 * @param appId of the application that sent this next objective
385 * @param mpls determines if L3Unicast or MPLSInterface group is created
386 * @param meta metadata passed in by the application as part of the nextObjective
387 * @param useSetVlanExtension use the setVlanVid extension that has is_present bit set to 0.
388 * @return GroupInfo containing the GroupDescription of the
389 * L2Interface group(inner) and the GroupDescription of the (outer)
390 * L3Unicast/MPLSInterface group. May return null if there is an
391 * error in processing the chain
392 */
393 protected GroupInfo createL2L3ChainInternal(TrafficTreatment treatment, int nextId,
Pier Ventre42287df2016-11-09 14:17:26 -0800394 ApplicationId appId, boolean mpls,
395 TrafficSelector meta, boolean useSetVlanExtension) {
Charles Chan188ebf52015-12-23 00:15:11 -0800396 // for the l2interface group, get vlan and port info
397 // for the outer group, get the src/dst mac, and vlan info
398 TrafficTreatment.Builder outerTtb = DefaultTrafficTreatment.builder();
399 TrafficTreatment.Builder innerTtb = DefaultTrafficTreatment.builder();
400 VlanId vlanid = null;
401 long portNum = 0;
402 boolean setVlan = false, popVlan = false;
Yi Tseng78f51f42017-02-02 13:54:58 -0800403 MacAddress srcMac;
404 MacAddress dstMac;
Charles Chan188ebf52015-12-23 00:15:11 -0800405 for (Instruction ins : treatment.allInstructions()) {
406 if (ins.type() == Instruction.Type.L2MODIFICATION) {
407 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
408 switch (l2ins.subtype()) {
409 case ETH_DST:
Charles Chan5270ed02016-01-30 23:22:37 -0800410 dstMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
411 outerTtb.setEthDst(dstMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800412 break;
413 case ETH_SRC:
Charles Chand0fd5dc2016-02-16 23:14:49 -0800414 srcMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
415 outerTtb.setEthSrc(srcMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800416 break;
417 case VLAN_ID:
418 vlanid = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
Charles Chanf9e98652016-09-07 16:54:23 -0700419 if (useSetVlanExtension) {
420 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
421 outerTtb.extension(ofdpaSetVlanVid, deviceId);
422 } else {
423 outerTtb.setVlanId(vlanid);
424 }
Charles Chan188ebf52015-12-23 00:15:11 -0800425 setVlan = true;
426 break;
427 case VLAN_POP:
428 innerTtb.popVlan();
429 popVlan = true;
430 break;
431 case DEC_MPLS_TTL:
432 case MPLS_LABEL:
433 case MPLS_POP:
434 case MPLS_PUSH:
435 case VLAN_PCP:
436 case VLAN_PUSH:
437 default:
438 break;
439 }
440 } else if (ins.type() == Instruction.Type.OUTPUT) {
441 portNum = ((Instructions.OutputInstruction) ins).port().toLong();
442 innerTtb.add(ins);
443 } else {
444 log.warn("Driver does not handle this type of TrafficTreatment"
445 + " instruction in nextObjectives: {}", ins.type());
446 }
447 }
448
449 if (vlanid == null && meta != null) {
450 // use metadata if available
Pier Ventre140a8942016-11-02 07:26:38 -0700451 Criterion vidCriterion = meta.getCriterion(VLAN_VID);
Charles Chan188ebf52015-12-23 00:15:11 -0800452 if (vidCriterion != null) {
453 vlanid = ((VlanIdCriterion) vidCriterion).vlanId();
454 }
455 // if vlan is not set, use the vlan in metadata for outerTtb
456 if (vlanid != null && !setVlan) {
Charles Chanf9e98652016-09-07 16:54:23 -0700457 if (useSetVlanExtension) {
458 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
459 outerTtb.extension(ofdpaSetVlanVid, deviceId);
460 } else {
461 outerTtb.setVlanId(vlanid);
462 }
Charles Chan188ebf52015-12-23 00:15:11 -0800463 }
464 }
465
466 if (vlanid == null) {
467 log.error("Driver cannot process an L2/L3 group chain without "
468 + "egress vlan information for dev: {} port:{}",
469 deviceId, portNum);
470 return null;
471 }
472
473 if (!setVlan && !popVlan) {
474 // untagged outgoing port
475 TrafficTreatment.Builder temp = DefaultTrafficTreatment.builder();
476 temp.popVlan();
Yi Tsengef19de12017-04-24 11:33:05 -0700477 innerTtb.build().allInstructions().forEach(temp::add);
Charles Chan188ebf52015-12-23 00:15:11 -0800478 innerTtb = temp;
479 }
480
481 // assemble information for ofdpa l2interface group
Yi Tsengef19de12017-04-24 11:33:05 -0700482 int l2groupId = l2GroupId(vlanid, portNum);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800483 // a globally unique groupkey that is different for ports in the same device,
Charles Chan188ebf52015-12-23 00:15:11 -0800484 // but different for the same portnumber on different devices. Also different
485 // for the various group-types created out of the same next objective.
Charles Chane849c192016-01-11 18:28:54 -0800486 int l2gk = l2InterfaceGroupKey(deviceId, vlanid, portNum);
Yi Tsengef19de12017-04-24 11:33:05 -0700487 final GroupKey l2groupkey = new DefaultGroupKey(appKryo.serialize(l2gk));
Charles Chan188ebf52015-12-23 00:15:11 -0800488
489 // assemble information for outer group
Yi Tsengef19de12017-04-24 11:33:05 -0700490 GroupDescription outerGrpDesc;
Charles Chan188ebf52015-12-23 00:15:11 -0800491 if (mpls) {
Yi Tsengef19de12017-04-24 11:33:05 -0700492 // outer group is MPLS Interface
Saurav Das8be4e3a2016-03-11 17:19:07 -0800493 int mplsInterfaceIndex = getNextAvailableIndex();
Yi Tsengef19de12017-04-24 11:33:05 -0700494 int mplsGroupId = MPLS_INTERFACE_TYPE | (SUBTYPE_MASK & mplsInterfaceIndex);
495 final GroupKey mplsGroupKey = new DefaultGroupKey(
496 appKryo.serialize(mplsInterfaceIndex));
Yi Tsengfa394de2017-02-01 11:26:40 -0800497 outerTtb.group(new GroupId(l2groupId));
Charles Chan188ebf52015-12-23 00:15:11 -0800498 // create the mpls-interface group description to wait for the
499 // l2 interface group to be processed
500 GroupBucket mplsinterfaceGroupBucket =
501 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
502 outerGrpDesc = new DefaultGroupDescription(
503 deviceId,
504 GroupDescription.Type.INDIRECT,
505 new GroupBuckets(Collections.singletonList(
506 mplsinterfaceGroupBucket)),
Yi Tsengef19de12017-04-24 11:33:05 -0700507 mplsGroupKey,
508 mplsGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800509 appId);
510 log.debug("Trying MPLS-Interface: device:{} gid:{} gkey:{} nextid:{}",
Yi Tsengef19de12017-04-24 11:33:05 -0700511 deviceId, Integer.toHexString(mplsGroupId),
512 mplsGroupKey, nextId);
Charles Chan188ebf52015-12-23 00:15:11 -0800513 } else {
514 // outer group is L3Unicast
Saurav Das8be4e3a2016-03-11 17:19:07 -0800515 int l3unicastIndex = getNextAvailableIndex();
516 int l3groupId = L3_UNICAST_TYPE | (TYPE_MASK & l3unicastIndex);
517 final GroupKey l3groupkey = new DefaultGroupKey(
Yi Tsengef19de12017-04-24 11:33:05 -0700518 appKryo.serialize(l3unicastIndex));
Yi Tsengfa394de2017-02-01 11:26:40 -0800519 outerTtb.group(new GroupId(l2groupId));
Charles Chan188ebf52015-12-23 00:15:11 -0800520 // create the l3unicast group description to wait for the
521 // l2 interface group to be processed
522 GroupBucket l3unicastGroupBucket =
523 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
524 outerGrpDesc = new DefaultGroupDescription(
525 deviceId,
526 GroupDescription.Type.INDIRECT,
Yi Tsengef19de12017-04-24 11:33:05 -0700527 new GroupBuckets(Collections.singletonList(l3unicastGroupBucket)),
Charles Chan188ebf52015-12-23 00:15:11 -0800528 l3groupkey,
529 l3groupId,
530 appId);
531 log.debug("Trying L3Unicast: device:{} gid:{} gkey:{} nextid:{}",
532 deviceId, Integer.toHexString(l3groupId),
533 l3groupkey, nextId);
534 }
535
536 // store l2groupkey with the groupChainElem for the outer-group that depends on it
Yi Tsengef19de12017-04-24 11:33:05 -0700537 GroupChainElem gce = new GroupChainElem(outerGrpDesc, 1, false, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800538 updatePendingGroups(l2groupkey, gce);
539
Yi Tsengef19de12017-04-24 11:33:05 -0700540 // create group description for the inner l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700541 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800542 DefaultGroupBucket.createIndirectGroupBucket(innerTtb.build());
543 GroupDescription l2groupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -0700544 new DefaultGroupDescription(deviceId,
545 GroupDescription.Type.INDIRECT,
546 new GroupBuckets(Collections.singletonList(l2InterfaceGroupBucket)),
547 l2groupkey,
548 l2groupId,
549 appId);
Charles Chan188ebf52015-12-23 00:15:11 -0800550 log.debug("Trying L2Interface: device:{} gid:{} gkey:{} nextId:{}",
551 deviceId, Integer.toHexString(l2groupId),
552 l2groupkey, nextId);
553 return new GroupInfo(l2groupDescription, outerGrpDesc);
554
555 }
556
557 /**
558 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
559 * a chain of groups. The broadcast Next Objective passed in by the application
560 * has to be broken up into a group chain comprising of an
Saurav Das1a129a02016-11-18 15:21:57 -0800561 * L2 Flood group or L3 Multicast group, whose buckets point to L2 Interface groups.
Charles Chan188ebf52015-12-23 00:15:11 -0800562 *
563 * @param nextObj the nextObjective of type BROADCAST
564 */
565 private void processBroadcastNextObjective(NextObjective nextObj) {
Yi Tsengef19de12017-04-24 11:33:05 -0700566 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700567 if (assignedVlan == null) {
568 log.warn("VLAN ID required by broadcast next obj is missing. Abort.");
Yi Tsengef19de12017-04-24 11:33:05 -0700569 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800570 return;
571 }
Charles Chan188ebf52015-12-23 00:15:11 -0800572
Charles Chan5b9df8d2016-03-28 22:21:40 -0700573 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
574
Yi Tsengef19de12017-04-24 11:33:05 -0700575 IpPrefix ipDst = readIpDstFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700576 if (ipDst != null) {
577 if (ipDst.isMulticast()) {
578 createL3MulticastGroup(nextObj, assignedVlan, groupInfos);
579 } else {
580 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -0700581 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700582 }
583 } else {
584 createL2FloodGroup(nextObj, assignedVlan, groupInfos);
585 }
586 }
587
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700588 private List<GroupInfo> prepareL2InterfaceGroup(NextObjective nextObj,
589 VlanId assignedVlan) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700590 ImmutableList.Builder<GroupInfo> groupInfoBuilder = ImmutableList.builder();
591
592 // break up broadcast next objective to multiple groups
593 Collection<TrafficTreatment> buckets = nextObj.next();
594
Charles Chan188ebf52015-12-23 00:15:11 -0800595 // each treatment is converted to an L2 interface group
Charles Chan188ebf52015-12-23 00:15:11 -0800596 for (TrafficTreatment treatment : buckets) {
597 TrafficTreatment.Builder newTreatment = DefaultTrafficTreatment.builder();
598 PortNumber portNum = null;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700599 VlanId egressVlan = null;
Charles Chan188ebf52015-12-23 00:15:11 -0800600 // ensure that the only allowed treatments are pop-vlan and output
601 for (Instruction ins : treatment.allInstructions()) {
602 if (ins.type() == Instruction.Type.L2MODIFICATION) {
603 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
604 switch (l2ins.subtype()) {
605 case VLAN_POP:
606 newTreatment.add(l2ins);
607 break;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700608 case VLAN_ID:
609 egressVlan = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
610 break;
Charles Chan188ebf52015-12-23 00:15:11 -0800611 default:
612 log.debug("action {} not permitted for broadcast nextObj",
613 l2ins.subtype());
614 break;
615 }
616 } else if (ins.type() == Instruction.Type.OUTPUT) {
617 portNum = ((Instructions.OutputInstruction) ins).port();
618 newTreatment.add(ins);
619 } else {
Charles Chane849c192016-01-11 18:28:54 -0800620 log.debug("TrafficTreatment of type {} not permitted in " +
621 " broadcast nextObjective", ins.type());
Charles Chan188ebf52015-12-23 00:15:11 -0800622 }
623 }
624
Yi Tsengef19de12017-04-24 11:33:05 -0700625 if (portNum == null) {
626 log.warn("Can't find output port for the bucket {}.", treatment);
627 continue;
628 }
629
Charles Chan188ebf52015-12-23 00:15:11 -0800630 // assemble info for l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700631 VlanId l2InterfaceGroupVlan =
632 (egressVlan != null && !assignedVlan.equals(egressVlan)) ?
633 egressVlan : assignedVlan;
634 int l2gk = l2InterfaceGroupKey(deviceId, l2InterfaceGroupVlan, portNum.toLong());
635 final GroupKey l2InterfaceGroupKey =
Yi Tsengef19de12017-04-24 11:33:05 -0700636 new DefaultGroupKey(appKryo.serialize(l2gk));
Charles Chan372b63e2017-02-07 12:10:53 -0800637 int l2InterfaceGroupId = L2_INTERFACE_TYPE |
638 ((l2InterfaceGroupVlan.toShort() & THREE_BIT_MASK) << PORT_LEN) |
639 ((int) portNum.toLong() & FOUR_BIT_MASK);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700640 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800641 DefaultGroupBucket.createIndirectGroupBucket(newTreatment.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700642 GroupDescription l2InterfaceGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -0700643 new DefaultGroupDescription(deviceId,
644 GroupDescription.Type.INDIRECT,
645 new GroupBuckets(Collections.singletonList(
646 l2InterfaceGroupBucket)),
647 l2InterfaceGroupKey,
648 l2InterfaceGroupId,
649 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800650 log.debug("Trying L2-Interface: device:{} gid:{} gkey:{} nextid:{}",
Charles Chan5b9df8d2016-03-28 22:21:40 -0700651 deviceId, Integer.toHexString(l2InterfaceGroupId),
652 l2InterfaceGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -0800653
Charles Chan5b9df8d2016-03-28 22:21:40 -0700654 groupInfoBuilder.add(new GroupInfo(l2InterfaceGroupDescription,
655 l2InterfaceGroupDescription));
Charles Chan188ebf52015-12-23 00:15:11 -0800656 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700657 return groupInfoBuilder.build();
658 }
Charles Chan188ebf52015-12-23 00:15:11 -0800659
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700660 private void createL2FloodGroup(NextObjective nextObj, VlanId vlanId,
661 List<GroupInfo> groupInfos) {
662 // assemble info for l2 flood group. Since there can be only one flood
663 // group for a vlan, its index is always the same - 0
Yi Tsengef19de12017-04-24 11:33:05 -0700664 Integer l2FloodGroupId = L2_FLOOD_TYPE | (vlanId.toShort() << 16);
665 final GroupKey l2FloodGroupKey = l2FloodGroupKey(vlanId, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700666
Charles Chan188ebf52015-12-23 00:15:11 -0800667 // collection of group buckets pointing to all the l2 interface groups
Yi Tsengef19de12017-04-24 11:33:05 -0700668 List<GroupBucket> l2floodBuckets = generateNextGroupBuckets(groupInfos, ALL);
Charles Chan188ebf52015-12-23 00:15:11 -0800669 // create the l2flood group-description to wait for all the
670 // l2interface groups to be processed
671 GroupDescription l2floodGroupDescription =
672 new DefaultGroupDescription(
673 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800674 ALL,
Charles Chan188ebf52015-12-23 00:15:11 -0800675 new GroupBuckets(l2floodBuckets),
Yi Tsengef19de12017-04-24 11:33:05 -0700676 l2FloodGroupKey,
677 l2FloodGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800678 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800679 log.debug("Trying L2-Flood: device:{} gid:{} gkey:{} nextid:{}",
Yi Tsengef19de12017-04-24 11:33:05 -0700680 deviceId, Integer.toHexString(l2FloodGroupId),
681 l2FloodGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -0800682
Charles Chan5b9df8d2016-03-28 22:21:40 -0700683 // Put all dependency information into allGroupKeys
684 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
685 groupInfos.forEach(groupInfo -> {
Yi Tsengef19de12017-04-24 11:33:05 -0700686 Deque<GroupKey> groupKeyChain = new ArrayDeque<>();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700687 // In this case we should have L2 interface group only
Yi Tsengef19de12017-04-24 11:33:05 -0700688 groupKeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
689 groupKeyChain.addFirst(l2FloodGroupKey);
690 allGroupKeys.add(groupKeyChain);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700691 });
Charles Chan188ebf52015-12-23 00:15:11 -0800692
Charles Chan5b9df8d2016-03-28 22:21:40 -0700693 // Point the next objective to this group
694 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -0700695 updatePendingNextObjective(l2FloodGroupKey, ofdpaGrp);
Charles Chan188ebf52015-12-23 00:15:11 -0800696
Charles Chan5b9df8d2016-03-28 22:21:40 -0700697 GroupChainElem gce = new GroupChainElem(l2floodGroupDescription,
Yi Tsengef19de12017-04-24 11:33:05 -0700698 groupInfos.size(), false, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700699 groupInfos.forEach(groupInfo -> {
700 // Point this group to the next group
Yi Tsengef19de12017-04-24 11:33:05 -0700701 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), gce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700702 // Start installing the inner-most group
Yi Tsengef19de12017-04-24 11:33:05 -0700703 groupService.addGroup(groupInfo.innerMostGroupDesc());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700704 });
705 }
706
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700707 private void createL3MulticastGroup(NextObjective nextObj, VlanId vlanId,
708 List<GroupInfo> groupInfos) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700709 List<GroupBucket> l3McastBuckets = new ArrayList<>();
710 groupInfos.forEach(groupInfo -> {
711 // Points to L3 interface group if there is one.
712 // Otherwise points to L2 interface group directly.
Yi Tsengef19de12017-04-24 11:33:05 -0700713 GroupDescription nextGroupDesc = (groupInfo.nextGroupDesc() != null) ?
714 groupInfo.nextGroupDesc() : groupInfo.innerMostGroupDesc();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700715 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Yi Tsengfa394de2017-02-01 11:26:40 -0800716 ttb.group(new GroupId(nextGroupDesc.givenGroupId()));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700717 GroupBucket abucket = DefaultGroupBucket.createAllGroupBucket(ttb.build());
718 l3McastBuckets.add(abucket);
719 });
720
721 int l3MulticastIndex = getNextAvailableIndex();
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700722 int l3MulticastGroupId = L3_MULTICAST_TYPE |
723 vlanId.toShort() << 16 | (TYPE_VLAN_MASK & l3MulticastIndex);
724 final GroupKey l3MulticastGroupKey =
Yi Tsengef19de12017-04-24 11:33:05 -0700725 new DefaultGroupKey(appKryo.serialize(l3MulticastIndex));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700726
727 GroupDescription l3MulticastGroupDesc = new DefaultGroupDescription(deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800728 ALL,
Charles Chan5b9df8d2016-03-28 22:21:40 -0700729 new GroupBuckets(l3McastBuckets),
730 l3MulticastGroupKey,
731 l3MulticastGroupId,
732 nextObj.appId());
733
734 // Put all dependency information into allGroupKeys
735 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
736 groupInfos.forEach(groupInfo -> {
737 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700738 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700739 // Add L3 interface group to the chain if there is one.
Yi Tsengef19de12017-04-24 11:33:05 -0700740 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
741 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700742 }
743 gkeyChain.addFirst(l3MulticastGroupKey);
744 allGroupKeys.add(gkeyChain);
745 });
746
747 // Point the next objective to this group
748 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
749 updatePendingNextObjective(l3MulticastGroupKey, ofdpaGrp);
750
751 GroupChainElem outerGce = new GroupChainElem(l3MulticastGroupDesc,
Yi Tsengef19de12017-04-24 11:33:05 -0700752 groupInfos.size(), false, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700753 groupInfos.forEach(groupInfo -> {
754 // Point this group (L3 multicast) to the next group
Yi Tsengef19de12017-04-24 11:33:05 -0700755 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), outerGce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700756
757 // Point next group to inner-most group, if any
Yi Tsengef19de12017-04-24 11:33:05 -0700758 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
759 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(),
760 1, false, deviceId);
761 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700762 }
763
764 // Start installing the inner-most group
Yi Tsengef19de12017-04-24 11:33:05 -0700765 groupService.addGroup(groupInfo.innerMostGroupDesc());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700766 });
Charles Chan188ebf52015-12-23 00:15:11 -0800767 }
768
Charles Chan188ebf52015-12-23 00:15:11 -0800769 /**
770 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
771 * a chain of groups. The hashed Next Objective passed in by the application
772 * has to be broken up into a group chain comprising of an
773 * L3 ECMP group as the top level group. Buckets of this group can point
774 * to a variety of groups in a group chain, depending on the whether
775 * MPLS labels are being pushed or not.
776 * <p>
777 * NOTE: We do not create MPLS ECMP groups as they are unimplemented in
778 * OF-DPA 2.0 (even though it is in the spec). Therefore we do not
779 * check the nextObjective meta to see what is matching before being
780 * sent to this nextObjective.
781 *
782 * @param nextObj the nextObjective of type HASHED
783 */
Pier Ventre140a8942016-11-02 07:26:38 -0700784 protected void processHashedNextObjective(NextObjective nextObj) {
Charles Chan188ebf52015-12-23 00:15:11 -0800785 // storage for all group keys in the chain of groups created
786 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
787 List<GroupInfo> unsentGroups = new ArrayList<>();
788 createHashBucketChains(nextObj, allGroupKeys, unsentGroups);
789
790 // now we can create the outermost L3 ECMP group
791 List<GroupBucket> l3ecmpGroupBuckets = new ArrayList<>();
792 for (GroupInfo gi : unsentGroups) {
793 // create ECMP bucket to point to the outer group
794 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Yi Tsengef19de12017-04-24 11:33:05 -0700795 ttb.group(new GroupId(gi.nextGroupDesc().givenGroupId()));
Charles Chan188ebf52015-12-23 00:15:11 -0800796 GroupBucket sbucket = DefaultGroupBucket
797 .createSelectGroupBucket(ttb.build());
798 l3ecmpGroupBuckets.add(sbucket);
799 }
Saurav Das8be4e3a2016-03-11 17:19:07 -0800800 int l3ecmpIndex = getNextAvailableIndex();
801 int l3ecmpGroupId = L3_ECMP_TYPE | (TYPE_MASK & l3ecmpIndex);
802 GroupKey l3ecmpGroupKey = new DefaultGroupKey(
Yi Tsengef19de12017-04-24 11:33:05 -0700803 appKryo.serialize(l3ecmpIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800804 GroupDescription l3ecmpGroupDesc =
805 new DefaultGroupDescription(
806 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800807 SELECT,
Charles Chan188ebf52015-12-23 00:15:11 -0800808 new GroupBuckets(l3ecmpGroupBuckets),
809 l3ecmpGroupKey,
810 l3ecmpGroupId,
811 nextObj.appId());
812 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
813 l3ecmpGroupBuckets.size(),
Yi Tsengef19de12017-04-24 11:33:05 -0700814 false, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800815
816 // create objects for local and distributed storage
Yi Tsengef19de12017-04-24 11:33:05 -0700817 allGroupKeys.forEach(gKeyChain -> gKeyChain.addFirst(l3ecmpGroupKey));
Charles Chan188ebf52015-12-23 00:15:11 -0800818 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
819
820 // store l3ecmpGroupKey with the ofdpaGroupChain for the nextObjective
821 // that depends on it
822 updatePendingNextObjective(l3ecmpGroupKey, ofdpaGrp);
823
824 log.debug("Trying L3ECMP: device:{} gid:{} gkey:{} nextId:{}",
825 deviceId, Integer.toHexString(l3ecmpGroupId),
826 l3ecmpGroupKey, nextObj.id());
827 // finally we are ready to send the innermost groups
828 for (GroupInfo gi : unsentGroups) {
829 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -0700830 Integer.toHexString(gi.innerMostGroupDesc().givenGroupId()), deviceId);
831 updatePendingGroups(gi.nextGroupDesc().appCookie(), l3ecmpGce);
832 groupService.addGroup(gi.innerMostGroupDesc());
Charles Chan188ebf52015-12-23 00:15:11 -0800833 }
Charles Chan188ebf52015-12-23 00:15:11 -0800834 }
835
836 /**
837 * Creates group chains for all buckets in a hashed group, and stores the
838 * GroupInfos and GroupKeys for all the groups in the lists passed in, which
839 * should be empty.
840 * <p>
841 * Does not create the top level ECMP group. Does not actually send the
842 * groups to the groupService.
843 *
844 * @param nextObj the Next Objective with buckets that need to be converted
845 * to group chains
846 * @param allGroupKeys a list to store groupKey for each bucket-group-chain
847 * @param unsentGroups a list to store GroupInfo for each bucket-group-chain
848 */
Pier Ventre140a8942016-11-02 07:26:38 -0700849 protected void createHashBucketChains(NextObjective nextObj,
Yi Tsengef19de12017-04-24 11:33:05 -0700850 List<Deque<GroupKey>> allGroupKeys,
851 List<GroupInfo> unsentGroups) {
Charles Chan188ebf52015-12-23 00:15:11 -0800852 // break up hashed next objective to multiple groups
853 Collection<TrafficTreatment> buckets = nextObj.next();
854
855 for (TrafficTreatment bucket : buckets) {
856 //figure out how many labels are pushed in each bucket
857 int labelsPushed = 0;
858 MplsLabel innermostLabel = null;
859 for (Instruction ins : bucket.allInstructions()) {
860 if (ins.type() == Instruction.Type.L2MODIFICATION) {
861 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
862 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_PUSH) {
863 labelsPushed++;
864 }
865 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) {
866 if (innermostLabel == null) {
Yi Tsengef19de12017-04-24 11:33:05 -0700867 innermostLabel =
868 ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label();
Charles Chan188ebf52015-12-23 00:15:11 -0800869 }
870 }
871 }
872 }
873
Yi Tsengef19de12017-04-24 11:33:05 -0700874 Deque<GroupKey> gKeyChain = new ArrayDeque<>();
Charles Chan188ebf52015-12-23 00:15:11 -0800875 // XXX we only deal with 0 and 1 label push right now
876 if (labelsPushed == 0) {
Yi Tsengef19de12017-04-24 11:33:05 -0700877 GroupInfo noLabelGroupInfo;
Pier Ventre140a8942016-11-02 07:26:38 -0700878 TrafficSelector metaSelector = nextObj.meta();
879 if (metaSelector != null) {
880 if (isNotMplsBos(metaSelector)) {
Yi Tsengef19de12017-04-24 11:33:05 -0700881 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
882 nextObj.appId(), true,
883 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700884 } else {
Yi Tsengef19de12017-04-24 11:33:05 -0700885 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
886 nextObj.appId(), false,
887 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700888 }
889 } else {
Yi Tsengef19de12017-04-24 11:33:05 -0700890 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
891 nextObj.appId(), false,
892 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700893 }
Yi Tsengef19de12017-04-24 11:33:05 -0700894 if (noLabelGroupInfo == null) {
Charles Chan188ebf52015-12-23 00:15:11 -0800895 log.error("Could not process nextObj={} in dev:{}",
896 nextObj.id(), deviceId);
897 return;
898 }
Yi Tsengef19de12017-04-24 11:33:05 -0700899 gKeyChain.addFirst(noLabelGroupInfo.innerMostGroupDesc().appCookie());
900 gKeyChain.addFirst(noLabelGroupInfo.nextGroupDesc().appCookie());
Charles Chan188ebf52015-12-23 00:15:11 -0800901
902 // we can't send the inner group description yet, as we have to
903 // create the dependent ECMP group first. So we store..
Yi Tsengef19de12017-04-24 11:33:05 -0700904 unsentGroups.add(noLabelGroupInfo);
Charles Chan188ebf52015-12-23 00:15:11 -0800905
906 } else if (labelsPushed == 1) {
907 GroupInfo onelabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
908 nextObj.appId(), true,
909 nextObj.meta());
910 if (onelabelGroupInfo == null) {
911 log.error("Could not process nextObj={} in dev:{}",
912 nextObj.id(), deviceId);
913 return;
914 }
915 // we need to add another group to this chain - the L3VPN group
916 TrafficTreatment.Builder l3vpnTtb = DefaultTrafficTreatment.builder();
Charles Chan0f43e472017-02-14 14:00:16 -0800917 if (requireVlanPopBeforeMplsPush()) {
918 l3vpnTtb.popVlan();
919 }
Charles Chan188ebf52015-12-23 00:15:11 -0800920 l3vpnTtb.pushMpls()
921 .setMpls(innermostLabel)
Yi Tsengef19de12017-04-24 11:33:05 -0700922 .group(new GroupId(onelabelGroupInfo.nextGroupDesc().givenGroupId()));
Charles Chan40132b32017-01-22 00:19:37 -0800923 if (supportCopyTtl()) {
924 l3vpnTtb.copyTtlOut();
925 }
926 if (supportSetMplsBos()) {
927 l3vpnTtb.setMplsBos(true);
928 }
Charles Chan0f43e472017-02-14 14:00:16 -0800929 if (requireVlanPopBeforeMplsPush()) {
930 l3vpnTtb.pushVlan().setVlanId(VlanId.vlanId(VlanId.RESERVED));
931 }
Charles Chan40132b32017-01-22 00:19:37 -0800932
Charles Chan188ebf52015-12-23 00:15:11 -0800933 GroupBucket l3vpnGrpBkt =
934 DefaultGroupBucket.createIndirectGroupBucket(l3vpnTtb.build());
Saurav Das8be4e3a2016-03-11 17:19:07 -0800935 int l3vpnIndex = getNextAvailableIndex();
Yi Tsengef19de12017-04-24 11:33:05 -0700936 int l3vpnGroupId = MPLS_L3VPN_SUBTYPE | (SUBTYPE_MASK & l3vpnIndex);
937 GroupKey l3vpnGroupKey = new DefaultGroupKey(
938 appKryo.serialize(l3vpnIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800939 GroupDescription l3vpnGroupDesc =
940 new DefaultGroupDescription(
941 deviceId,
942 GroupDescription.Type.INDIRECT,
Yi Tsengef19de12017-04-24 11:33:05 -0700943 new GroupBuckets(Collections.singletonList(l3vpnGrpBkt)),
944 l3vpnGroupKey,
945 l3vpnGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800946 nextObj.appId());
Yi Tsengef19de12017-04-24 11:33:05 -0700947 GroupChainElem l3vpnGce = new GroupChainElem(l3vpnGroupDesc,
948 1,
949 false,
950 deviceId);
951 updatePendingGroups(onelabelGroupInfo.nextGroupDesc().appCookie(), l3vpnGce);
Charles Chan188ebf52015-12-23 00:15:11 -0800952
Yi Tsengef19de12017-04-24 11:33:05 -0700953 gKeyChain.addFirst(onelabelGroupInfo.innerMostGroupDesc().appCookie());
954 gKeyChain.addFirst(onelabelGroupInfo.nextGroupDesc().appCookie());
955 gKeyChain.addFirst(l3vpnGroupKey);
Charles Chan188ebf52015-12-23 00:15:11 -0800956
957 //now we can replace the outerGrpDesc with the one we just created
Yi Tsengef19de12017-04-24 11:33:05 -0700958 onelabelGroupInfo.nextGroupDesc(l3vpnGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800959
960 // we can't send the innermost group yet, as we have to create
961 // the dependent ECMP group first. So we store ...
962 unsentGroups.add(onelabelGroupInfo);
963
Yi Tsengef19de12017-04-24 11:33:05 -0700964 log.debug("Trying L3VPN: device:{} gid:{} group key:{} nextId:{}",
965 deviceId, Integer.toHexString(l3vpnGroupId),
966 l3vpnGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -0800967
968 } else {
969 log.warn("Driver currently does not handle more than 1 MPLS "
970 + "labels. Not processing nextObjective {}", nextObj.id());
971 return;
972 }
973
974 // all groups in this chain
Yi Tsengef19de12017-04-24 11:33:05 -0700975 allGroupKeys.add(gKeyChain);
Charles Chan188ebf52015-12-23 00:15:11 -0800976 }
977 }
978
Pier Ventre42287df2016-11-09 14:17:26 -0800979 /**
980 * Processes the pseudo wire related next objective.
981 * This procedure try to reuse the mpls label groups,
982 * the mpls interface group and the l2 interface group.
983 *
984 * @param nextObjective the objective to process.
985 */
986 protected void processPwNextObjective(NextObjective nextObjective) {
987 log.warn("Pseudo wire extensions are not support for the OFDPA 2.0 {}", nextObjective.id());
Pier Ventre42287df2016-11-09 14:17:26 -0800988 }
989
Saurav Das8be4e3a2016-03-11 17:19:07 -0800990 //////////////////////////////////////
991 // Group Editing
992 //////////////////////////////////////
Charles Chan188ebf52015-12-23 00:15:11 -0800993 /**
994 * Adds a bucket to the top level group of a group-chain, and creates the chain.
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700995 * Ensures that bucket being added is not a duplicate, by checking existing
Yi Tsengef19de12017-04-24 11:33:05 -0700996 * buckets for the same output port.
Charles Chan188ebf52015-12-23 00:15:11 -0800997 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700998 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -0800999 * @param next the representation of the existing group-chain for this next objective
1000 */
1001 protected void addBucketToGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001002 if (nextObjective.type() != NextObjective.Type.HASHED &&
1003 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001004 log.warn("AddBuckets not applied to nextType:{} in dev:{} for next:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001005 nextObjective.type(), deviceId, nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -07001006 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001007 return;
1008 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001009
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001010 // first check to see if bucket being added is not a duplicate of an
Yi Tsengef19de12017-04-24 11:33:05 -07001011 // existing bucket. If it is for an existing output port, then its a
1012 // duplicate.
Yi Tseng78f51f42017-02-02 13:54:58 -08001013 Set<TrafficTreatment> duplicateBuckets = Sets.newHashSet();
Yi Tsengef19de12017-04-24 11:33:05 -07001014 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
1015 Set<PortNumber> existingPorts = getExistingOutputPorts(allActiveKeys,
1016 groupService,
1017 deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -08001018 Set<TrafficTreatment> nonDuplicateBuckets = Sets.newHashSet();
1019 NextObjective objectiveToAdd;
Yi Tseng78f51f42017-02-02 13:54:58 -08001020
1021 nextObjective.next().forEach(trafficTreatment -> {
1022 PortNumber portNumber = readOutPortFromTreatment(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001023 if (portNumber == null) {
1024 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001025 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001026 if (existingPorts.contains(portNumber)) {
1027 duplicateBuckets.add(trafficTreatment);
Yi Tseng47f82dc2017-03-05 22:48:39 -08001028 } else {
1029 nonDuplicateBuckets.add(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001030 }
1031 });
1032
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001033 if (duplicateBuckets.isEmpty()) {
1034 // use the original objective
1035 objectiveToAdd = nextObjective;
1036 } else if (!nonDuplicateBuckets.isEmpty()) {
1037 // only use the non-duplicate buckets if there are any
1038 log.debug("Some buckets {} already exist in next id {}, duplicate "
1039 + "buckets will be ignored.", duplicateBuckets, nextObjective.id());
1040 // new next objective with non duplicate treatments
Yi Tseng47f82dc2017-03-05 22:48:39 -08001041 NextObjective.Builder builder = DefaultNextObjective.builder()
1042 .withType(nextObjective.type())
1043 .withId(nextObjective.id())
1044 .withMeta(nextObjective.meta())
1045 .fromApp(nextObjective.appId());
1046 nonDuplicateBuckets.forEach(builder::addTreatment);
1047
1048 ObjectiveContext context = nextObjective.context().orElse(null);
1049 objectiveToAdd = builder.addToExisting(context);
1050 } else {
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001051 // buckets to add are already there - nothing to do
1052 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001053 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001054
Saurav Das1a129a02016-11-18 15:21:57 -08001055 if (nextObjective.type() == NextObjective.Type.HASHED) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001056 addBucketToHashGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001057 } else if (nextObjective.type() == NextObjective.Type.BROADCAST) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001058 addBucketToBroadcastGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001059 }
1060 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001061
Saurav Das1a129a02016-11-18 15:21:57 -08001062 private void addBucketToHashGroup(NextObjective nextObjective,
Yi Tseng78f51f42017-02-02 13:54:58 -08001063 List<Deque<GroupKey>> allActiveKeys) {
Charles Chan188ebf52015-12-23 00:15:11 -08001064 // storage for all group keys in the chain of groups created
1065 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
1066 List<GroupInfo> unsentGroups = new ArrayList<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001067 List<GroupBucket> newBuckets;
Charles Chan188ebf52015-12-23 00:15:11 -08001068 createHashBucketChains(nextObjective, allGroupKeys, unsentGroups);
1069
Yi Tseng78f51f42017-02-02 13:54:58 -08001070 // now we can create the buckets to add to the outermost L3 ECMP group
1071 newBuckets = generateNextGroupBuckets(unsentGroups, SELECT);
Charles Chan188ebf52015-12-23 00:15:11 -08001072
Saurav Das1a129a02016-11-18 15:21:57 -08001073 // retrieve the original L3 ECMP group
1074 Group l3ecmpGroup = retrieveTopLevelGroup(allActiveKeys, nextObjective.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001075 if (l3ecmpGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001076 fail(nextObjective, ObjectiveError.GROUPMISSING);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001077 return;
1078 }
Saurav Das1a129a02016-11-18 15:21:57 -08001079 GroupKey l3ecmpGroupKey = l3ecmpGroup.appCookie();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001080 int l3ecmpGroupId = l3ecmpGroup.id().id();
Charles Chan188ebf52015-12-23 00:15:11 -08001081
1082 // Although GroupDescriptions are not necessary for adding buckets to
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001083 // existing groups, we still use one in the GroupChainElem. When the latter is
Charles Chan188ebf52015-12-23 00:15:11 -08001084 // processed, the info will be extracted for the bucketAdd call to groupService
1085 GroupDescription l3ecmpGroupDesc =
Yi Tsengef19de12017-04-24 11:33:05 -07001086 new DefaultGroupDescription(deviceId,
1087 SELECT,
1088 new GroupBuckets(newBuckets),
1089 l3ecmpGroupKey,
1090 l3ecmpGroupId,
1091 nextObjective.appId());
Yi Tseng78f51f42017-02-02 13:54:58 -08001092 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
1093 unsentGroups.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001094 true,
1095 deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001096
1097 // update original NextGroup with new bucket-chain
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001098 // If active keys shows only the top-level group without a chain of groups,
1099 // then it represents an empty group. Update by replacing empty chain.
Charles Chan188ebf52015-12-23 00:15:11 -08001100 Deque<GroupKey> newBucketChain = allGroupKeys.get(0);
1101 newBucketChain.addFirst(l3ecmpGroupKey);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001102 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1103 allActiveKeys.clear();
1104 }
1105 allActiveKeys.add(newBucketChain);
Yi Tsengef19de12017-04-24 11:33:05 -07001106 updatePendingNextObjective(l3ecmpGroupKey, new OfdpaNextGroup(allActiveKeys, nextObjective));
Yi Tseng78f51f42017-02-02 13:54:58 -08001107
Yi Tsengef19de12017-04-24 11:33:05 -07001108 log.debug("Adding to L3ECMP: device:{} gid:{} group key:{} nextId:{}",
Charles Chan188ebf52015-12-23 00:15:11 -08001109 deviceId, Integer.toHexString(l3ecmpGroupId),
1110 l3ecmpGroupKey, nextObjective.id());
Yi Tseng78f51f42017-02-02 13:54:58 -08001111
1112 unsentGroups.forEach(groupInfo -> {
1113 // send the innermost group
1114 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001115 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()), deviceId);
1116 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3ecmpGce);
1117 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001118 });
Saurav Das1a129a02016-11-18 15:21:57 -08001119 }
Charles Chan188ebf52015-12-23 00:15:11 -08001120
Saurav Das1a129a02016-11-18 15:21:57 -08001121 private void addBucketToBroadcastGroup(NextObjective nextObj,
Yi Tsengef19de12017-04-24 11:33:05 -07001122 List<Deque<GroupKey>> allActiveKeys) {
1123 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001124 if (assignedVlan == null) {
1125 log.warn("VLAN ID required by broadcast next obj is missing. "
1126 + "Aborting add bucket to broadcast group for next:{} in dev:{}",
1127 nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001128 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001129 return;
1130 }
Saurav Das1a129a02016-11-18 15:21:57 -08001131 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Yi Tsengef19de12017-04-24 11:33:05 -07001132 IpPrefix ipDst = readIpDstFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001133 if (ipDst != null) {
1134 if (ipDst.isMulticast()) {
Yi Tsengef19de12017-04-24 11:33:05 -07001135 addBucketToL3MulticastGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001136 } else {
1137 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001138 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001139 }
1140 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001141 addBucketToL2FloodGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001142 }
1143 }
1144
1145 private void addBucketToL2FloodGroup(NextObjective nextObj,
1146 List<Deque<GroupKey>> allActiveKeys,
1147 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001148 VlanId assignedVlan) {
1149 Group l2FloodGroup = retrieveTopLevelGroup(allActiveKeys, nextObj.id());
1150
1151 if (l2FloodGroup == null) {
1152 log.warn("Can't find L2 flood group while adding bucket to it. NextObj = {}",
1153 nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001154 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001155 return;
1156 }
Saurav Das1a129a02016-11-18 15:21:57 -08001157
Yi Tseng78f51f42017-02-02 13:54:58 -08001158 GroupKey l2floodGroupKey = l2FloodGroup.appCookie();
1159 int l2floodGroupId = l2FloodGroup.id().id();
1160 List<GroupBucket> newBuckets = generateNextGroupBuckets(groupInfos, ALL);
1161
1162 GroupDescription l2FloodGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001163 new DefaultGroupDescription(deviceId,
1164 ALL,
1165 new GroupBuckets(newBuckets),
1166 l2floodGroupKey,
1167 l2floodGroupId,
1168 nextObj.appId());
Saurav Das1a129a02016-11-18 15:21:57 -08001169
Yi Tseng78f51f42017-02-02 13:54:58 -08001170 GroupChainElem l2FloodGroupChainElement =
1171 new GroupChainElem(l2FloodGroupDescription,
1172 groupInfos.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001173 true,
1174 deviceId);
Yi Tseng78f51f42017-02-02 13:54:58 -08001175
Saurav Das1a129a02016-11-18 15:21:57 -08001176 updatePendingNextObjective(l2floodGroupKey,
1177 new OfdpaNextGroup(allActiveKeys, nextObj));
Yi Tseng78f51f42017-02-02 13:54:58 -08001178
1179 //ensure assignedVlan applies to the chosen group
1180 VlanId floodGroupVlan = extractVlanIdFromGroupId(l2floodGroupId);
1181
1182 if (!floodGroupVlan.equals(assignedVlan)) {
1183 log.warn("VLAN ID {} does not match Flood group {} to which bucket is "
1184 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1185 Integer.toHexString(l2floodGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001186 fail(nextObj, ObjectiveError.BADPARAMS);
Yi Tseng78f51f42017-02-02 13:54:58 -08001187 return;
1188 }
1189
1190 groupInfos.forEach(groupInfo -> {
1191 // update original NextGroup with new bucket-chain
1192 // If active keys shows only the top-level group without a chain of groups,
1193 // then it represents an empty group. Update by replacing empty chain.
1194 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001195 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001196 newBucketChain.addFirst(l2floodGroupKey);
1197 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1198 allActiveKeys.clear();
1199 }
1200 allActiveKeys.add(newBucketChain);
1201
Yi Tsengef19de12017-04-24 11:33:05 -07001202 log.debug("Adding to L2FLOOD: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001203 deviceId, Integer.toHexString(l2floodGroupId),
1204 l2floodGroupKey, nextObj.id());
1205 // send the innermost group
1206 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001207 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001208 deviceId);
1209
Yi Tsengef19de12017-04-24 11:33:05 -07001210 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l2FloodGroupChainElement);
Yi Tseng78f51f42017-02-02 13:54:58 -08001211
Yi Tsengef19de12017-04-24 11:33:05 -07001212 DeviceId innerMostGroupDevice = groupInfo.innerMostGroupDesc().deviceId();
1213 GroupKey innerMostGroupKey = groupInfo.innerMostGroupDesc().appCookie();
Yi Tseng78f51f42017-02-02 13:54:58 -08001214 Group existsL2IGroup = groupService.getGroup(innerMostGroupDevice, innerMostGroupKey);
1215
1216 if (existsL2IGroup != null) {
1217 // group already exist
1218 processPendingAddGroupsOrNextObjs(innerMostGroupKey, true);
1219 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001220 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001221 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001222 });
1223 }
1224
Saurav Das1a129a02016-11-18 15:21:57 -08001225 private void addBucketToL3MulticastGroup(NextObjective nextObj,
1226 List<Deque<GroupKey>> allActiveKeys,
1227 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001228 VlanId assignedVlan) {
1229 // create the buckets to add to the outermost L3 Multicast group
1230 List<GroupBucket> newBuckets = Lists.newArrayList();
1231 groupInfos.forEach(groupInfo -> {
1232 // Points to L3 interface group if there is one.
1233 // Otherwise points to L2 interface group directly.
Yi Tsengef19de12017-04-24 11:33:05 -07001234 GroupDescription nextGroupDesc = (groupInfo.nextGroupDesc() != null) ?
1235 groupInfo.nextGroupDesc() : groupInfo.innerMostGroupDesc();
1236 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1237 treatmentBuilder.group(new GroupId(nextGroupDesc.givenGroupId()));
1238 GroupBucket newBucket = DefaultGroupBucket.createAllGroupBucket(treatmentBuilder.build());
Yi Tseng78f51f42017-02-02 13:54:58 -08001239 newBuckets.add(newBucket);
1240 });
Saurav Das1a129a02016-11-18 15:21:57 -08001241
1242 // get the group being edited
1243 Group l3mcastGroup = retrieveTopLevelGroup(allActiveKeys, nextObj.id());
1244 if (l3mcastGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001245 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001246 return;
1247 }
1248 GroupKey l3mcastGroupKey = l3mcastGroup.appCookie();
1249 int l3mcastGroupId = l3mcastGroup.id().id();
1250
1251 //ensure assignedVlan applies to the chosen group
Yi Tseng78f51f42017-02-02 13:54:58 -08001252 VlanId expectedVlan = extractVlanIdFromGroupId(l3mcastGroupId);
Saurav Das1a129a02016-11-18 15:21:57 -08001253 if (!expectedVlan.equals(assignedVlan)) {
1254 log.warn("VLAN ID {} does not match L3 Mcast group {} to which bucket is "
1255 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1256 Integer.toHexString(l3mcastGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001257 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001258 }
1259 GroupDescription l3mcastGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001260 new DefaultGroupDescription(deviceId,
1261 ALL,
1262 new GroupBuckets(newBuckets),
1263 l3mcastGroupKey,
1264 l3mcastGroupId,
1265 nextObj.appId());
Saurav Das1a129a02016-11-18 15:21:57 -08001266 GroupChainElem l3mcastGce = new GroupChainElem(l3mcastGroupDescription,
Yi Tsengef19de12017-04-24 11:33:05 -07001267 groupInfos.size(),
1268 true,
1269 deviceId);
Yi Tseng78f51f42017-02-02 13:54:58 -08001270 groupInfos.forEach(groupInfo -> {
1271 // update original NextGroup with new bucket-chain
1272 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001273 newBucketChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001274 // Add L3 interface group to the chain if there is one.
Yi Tsengef19de12017-04-24 11:33:05 -07001275 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1276 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001277 }
1278 newBucketChain.addFirst(l3mcastGroupKey);
1279 // If active keys shows only the top-level group without a chain of groups,
1280 // then it represents an empty group. Update by replacing empty chain.
1281 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1282 allActiveKeys.clear();
1283 }
1284 allActiveKeys.add(newBucketChain);
1285
Yi Tsengef19de12017-04-24 11:33:05 -07001286 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3mcastGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001287 // Point next group to inner-most group, if any
Yi Tsengef19de12017-04-24 11:33:05 -07001288 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1289 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(),
1290 1,
1291 false,
1292 deviceId);
1293 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001294 }
Yi Tsengef19de12017-04-24 11:33:05 -07001295 log.debug("Adding to L3MCAST: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001296 deviceId, Integer.toHexString(l3mcastGroupId),
1297 l3mcastGroupKey, nextObj.id());
1298 // send the innermost group
1299 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001300 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001301 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001302 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001303
1304 });
1305
Saurav Das1a129a02016-11-18 15:21:57 -08001306 updatePendingNextObjective(l3mcastGroupKey,
1307 new OfdpaNextGroup(allActiveKeys, nextObj));
Charles Chan188ebf52015-12-23 00:15:11 -08001308 }
1309
1310 /**
1311 * Removes the bucket in the top level group of a possible group-chain. Does
Saurav Das1a129a02016-11-18 15:21:57 -08001312 * not remove the groups in the group-chain pointed to by this bucket, as they
Charles Chan188ebf52015-12-23 00:15:11 -08001313 * may be in use (referenced by other groups) elsewhere.
1314 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001315 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -08001316 * @param next the representation of the existing group-chain for this next objective
1317 */
1318 protected void removeBucketFromGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001319 if (nextObjective.type() != NextObjective.Type.HASHED &&
1320 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001321 log.warn("RemoveBuckets not applied to nextType:{} in dev:{} for next:{}",
1322 nextObjective.type(), deviceId, nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -07001323 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001324 return;
1325 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001326 Set<PortNumber> portsToRemove = Sets.newHashSet();
Charles Chan188ebf52015-12-23 00:15:11 -08001327 Collection<TrafficTreatment> treatments = nextObjective.next();
Yi Tseng78f51f42017-02-02 13:54:58 -08001328 for (TrafficTreatment treatment : treatments) {
1329 // find the bucket to remove by noting the outport, and figuring out the
1330 // top-level group in the group-chain that indirectly references the port
1331 PortNumber portToRemove = readOutPortFromTreatment(treatment);
1332 if (portToRemove == null) {
1333 log.warn("treatment {} of next objective {} has no outport.. cannot remove bucket"
1334 + "from group in dev: {}", treatment, nextObjective.id(), deviceId);
1335 } else {
1336 portsToRemove.add(portToRemove);
1337 }
1338 }
1339
1340 if (portsToRemove.isEmpty()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001341 log.warn("next objective {} has no outport.. cannot remove bucket"
Yi Tseng78f51f42017-02-02 13:54:58 -08001342 + "from group in dev: {}", nextObjective.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001343 fail(nextObjective, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -08001344 }
1345
Yi Tsengef19de12017-04-24 11:33:05 -07001346 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Yi Tseng78f51f42017-02-02 13:54:58 -08001347 List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001348 for (Deque<GroupKey> gkeys : allActiveKeys) {
1349 // last group in group chain should have a single bucket pointing to port
Yi Tseng78f51f42017-02-02 13:54:58 -08001350
Charles Chan188ebf52015-12-23 00:15:11 -08001351 GroupKey groupWithPort = gkeys.peekLast();
1352 Group group = groupService.getGroup(deviceId, groupWithPort);
1353 if (group == null) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001354 log.warn("Inconsistent group chain found when removing bucket"
1355 + "for next:{} in dev:{}", nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001356 continue;
1357 }
Yi Tseng91cf2d42017-04-10 17:05:37 -07001358 if (group.buckets().buckets().isEmpty()) {
1359 log.warn("Can't get output port information from group {} " +
1360 "because there is no bucket in the group.",
1361 group.id().toString());
1362 continue;
1363 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001364 PortNumber pout = readOutPortFromTreatment(
1365 group.buckets().buckets().get(0).treatment());
Yi Tseng78f51f42017-02-02 13:54:58 -08001366 if (portsToRemove.contains(pout)) {
1367 chainsToRemove.add(gkeys);
Charles Chan188ebf52015-12-23 00:15:11 -08001368 }
Charles Chan188ebf52015-12-23 00:15:11 -08001369 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001370 if (chainsToRemove.isEmpty()) {
Charles Chan188ebf52015-12-23 00:15:11 -08001371 log.warn("Could not find appropriate group-chain for removing bucket"
1372 + " for next id {} in dev:{}", nextObjective.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001373 fail(nextObjective, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001374 return;
Charles Chan188ebf52015-12-23 00:15:11 -08001375 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001376 List<GroupBucket> bucketsToRemove = Lists.newArrayList();
1377 //first group key is the one we want to modify
1378 GroupKey modGroupKey = chainsToRemove.get(0).peekFirst();
Saurav Das1a129a02016-11-18 15:21:57 -08001379 Group modGroup = groupService.getGroup(deviceId, modGroupKey);
Yi Tseng78f51f42017-02-02 13:54:58 -08001380 for (Deque<GroupKey> foundChain : chainsToRemove) {
1381 //second group key is the one we wish to remove the reference to
1382 if (foundChain.size() < 2) {
1383 // additional check to make sure second group key exist in
1384 // the chain.
1385 log.warn("Can't find second group key from chain {}",
1386 foundChain);
1387 continue;
Saurav Das1a129a02016-11-18 15:21:57 -08001388 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001389 GroupKey pointedGroupKey = foundChain.stream().collect(Collectors.toList()).get(1);
Yi Tseng78f51f42017-02-02 13:54:58 -08001390 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
1391
1392 if (pointedGroup == null) {
1393 continue;
1394 }
1395
1396 GroupBucket bucket;
1397 if (nextObjective.type() == NextObjective.Type.HASHED) {
1398 bucket = DefaultGroupBucket.createSelectGroupBucket(
1399 DefaultTrafficTreatment.builder()
1400 .group(pointedGroup.id())
1401 .build());
1402 } else {
1403 bucket = DefaultGroupBucket.createAllGroupBucket(
1404 DefaultTrafficTreatment.builder()
1405 .group(pointedGroup.id())
1406 .build());
1407 }
1408
1409 bucketsToRemove.add(bucket);
Saurav Das1a129a02016-11-18 15:21:57 -08001410 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001411
1412 GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove);
1413 List<String> pointedGroupIds; // for debug log
1414 pointedGroupIds = bucketsToRemove.stream()
1415 .map(GroupBucket::treatment)
1416 .map(TrafficTreatment::allInstructions)
1417 .flatMap(List::stream)
1418 .filter(inst -> inst instanceof Instructions.GroupInstruction)
1419 .map(inst -> (Instructions.GroupInstruction) inst)
1420 .map(Instructions.GroupInstruction::groupId)
1421 .map(GroupId::id)
1422 .map(Integer::toHexString)
1423 .map(id -> HEX_PREFIX + id)
1424 .collect(Collectors.toList());
1425
Yi Tseng78f51f42017-02-02 13:54:58 -08001426 log.debug("Removing buckets from group id 0x{} pointing to group id(s) {} "
Saurav Das1a129a02016-11-18 15:21:57 -08001427 + "for next id {} in device {}", Integer.toHexString(modGroup.id().id()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001428 pointedGroupIds, nextObjective.id(), deviceId);
1429 addPendingUpdateNextObjective(modGroupKey, nextObjective);
Saurav Das1a129a02016-11-18 15:21:57 -08001430 groupService.removeBucketsFromGroup(deviceId, modGroupKey,
1431 removeBuckets, modGroupKey,
1432 nextObjective.appId());
1433 // update store
Yi Tsengeeb3dc12017-03-15 10:55:50 -07001434 allActiveKeys.removeAll(chainsToRemove);
1435 // If no buckets in the group, then retain an entry for the
1436 // top level group which still exists.
1437 if (allActiveKeys.isEmpty()) {
Saurav Das1a129a02016-11-18 15:21:57 -08001438 ArrayDeque<GroupKey> top = new ArrayDeque<>();
1439 top.add(modGroupKey);
1440 allActiveKeys.add(top);
1441 }
Saurav Das1a129a02016-11-18 15:21:57 -08001442 flowObjectiveStore.putNextGroup(nextObjective.id(),
Yi Tsengef19de12017-04-24 11:33:05 -07001443 new OfdpaNextGroup(allActiveKeys, nextObjective));
Charles Chan188ebf52015-12-23 00:15:11 -08001444 }
1445
1446 /**
Saurav Das961beb22017-03-29 19:09:17 -07001447 * Removes all groups in multiple possible group-chains that represent the next-obj.
Charles Chan188ebf52015-12-23 00:15:11 -08001448 *
1449 * @param nextObjective the next objective to remove
1450 * @param next the NextGroup that represents the existing group-chain for
1451 * this next objective
1452 */
1453 protected void removeGroup(NextObjective nextObjective, NextGroup next) {
Yi Tsengef19de12017-04-24 11:33:05 -07001454 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Charles Chanfc5c7802016-05-17 13:13:55 -07001455
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001456 List<GroupKey> groupKeys = allActiveKeys.stream()
Charles Chanfc5c7802016-05-17 13:13:55 -07001457 .map(Deque::getFirst).collect(Collectors.toList());
Yi Tsengef19de12017-04-24 11:33:05 -07001458 addPendingRemoveNextObjective(nextObjective, groupKeys);
Charles Chanfc5c7802016-05-17 13:13:55 -07001459
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001460 allActiveKeys.forEach(groupChain -> groupChain.forEach(groupKey ->
Charles Chan188ebf52015-12-23 00:15:11 -08001461 groupService.removeGroup(deviceId, groupKey, nextObjective.appId())));
1462 flowObjectiveStore.removeNextGroup(nextObjective.id());
1463 }
1464
Yi Tsengef19de12017-04-24 11:33:05 -07001465 protected void updatePendingNextObjective(GroupKey groupKey, OfdpaNextGroup nextGrp) {
1466 pendingAddNextObjectives.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07001467 if (val == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001468 val = new CopyOnWriteArrayList<>();
Saurav Das961beb22017-03-29 19:09:17 -07001469 }
Yi Tsengef19de12017-04-24 11:33:05 -07001470 val.add(nextGrp);
Saurav Das961beb22017-03-29 19:09:17 -07001471 return val;
1472 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08001473 }
1474
Yi Tsengef19de12017-04-24 11:33:05 -07001475 protected void updatePendingGroups(GroupKey groupKey, GroupChainElem gce) {
1476 pendingGroups.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07001477 if (val == null) {
1478 val = Sets.newConcurrentHashSet();
1479 }
1480 val.add(gce);
1481 return val;
1482 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08001483 }
1484
Yi Tsengef19de12017-04-24 11:33:05 -07001485 protected void addPendingUpdateNextObjective(GroupKey groupKey,
1486 NextObjective nextObjective) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001487 pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> {
1488 if (nextObjs != null) {
1489 nextObjs.add(nextObjective);
1490 } else {
1491 nextObjs = Sets.newHashSet(nextObjective);
1492 }
1493 return nextObjs;
1494 });
1495 }
1496
Yi Tsengef19de12017-04-24 11:33:05 -07001497 private void processPendingUpdateNextObjs(GroupKey groupKey) {
1498 pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> {
1499 if (nextObjs != null) {
1500
1501 nextObjs.forEach(nextObj -> {
1502 log.debug("Group {} updated, update pending next objective {}.",
1503 groupKey, nextObj);
1504
1505 pass(nextObj);
1506 });
1507 }
1508 return Sets.newHashSet();
1509 });
1510 }
1511
1512 private void processPendingRemoveNextObjs(GroupKey key) {
1513 pendingRemoveNextObjectives.asMap().forEach((nextObjective, groupKeys) -> {
1514 if (groupKeys.isEmpty()) {
1515 pendingRemoveNextObjectives.invalidate(nextObjective);
1516 pass(nextObjective);
1517 } else {
1518 groupKeys.remove(key);
1519 }
1520 });
1521 }
1522
1523 protected int getNextAvailableIndex() {
1524 return (int) nextIndex.incrementAndGet();
1525 }
1526
1527 protected Group retrieveTopLevelGroup(List<Deque<GroupKey>> allActiveKeys,
1528 int nextid) {
1529 GroupKey topLevelGroupKey;
1530 if (!allActiveKeys.isEmpty()) {
1531 topLevelGroupKey = allActiveKeys.get(0).peekFirst();
1532 } else {
1533 log.warn("Could not determine top level group while processing"
1534 + "next:{} in dev:{}", nextid, deviceId);
1535 return null;
1536 }
1537 Group topGroup = groupService.getGroup(deviceId, topLevelGroupKey);
1538 if (topGroup == null) {
1539 log.warn("Could not find top level group while processing "
1540 + "next:{} in dev:{}", nextid, deviceId);
1541 }
1542 return topGroup;
1543 }
1544
1545 protected void processPendingAddGroupsOrNextObjs(GroupKey key, boolean added) {
1546 //first check for group chain
1547 Set<OfdpaGroupHandlerUtility.GroupChainElem> gceSet = pendingGroups.asMap().remove(key);
1548 if (gceSet != null) {
1549 for (GroupChainElem gce : gceSet) {
1550 log.debug("Group service {} group key {} in device {}. "
1551 + "Processing next group in group chain with group id 0x{}",
1552 (added) ? "ADDED" : "processed",
1553 key, deviceId,
1554 Integer.toHexString(gce.groupDescription().givenGroupId()));
1555 processGroupChain(gce);
1556 }
1557 } else {
1558 // otherwise chain complete - check for waiting nextObjectives
1559 List<OfdpaGroupHandlerUtility.OfdpaNextGroup> nextGrpList =
1560 pendingAddNextObjectives.getIfPresent(key);
1561 if (nextGrpList != null) {
1562 pendingAddNextObjectives.invalidate(key);
1563 nextGrpList.forEach(nextGrp -> {
1564 log.debug("Group service {} group key {} in device:{}. "
1565 + "Done implementing next objective: {} <<-->> gid:0x{}",
1566 (added) ? "ADDED" : "processed",
1567 key, deviceId, nextGrp.nextObjective().id(),
1568 Integer.toHexString(groupService.getGroup(deviceId, key)
1569 .givenGroupId()));
1570 pass(nextGrp.nextObjective());
1571 flowObjectiveStore.putNextGroup(nextGrp.nextObjective().id(), nextGrp);
1572
1573 // check if addBuckets waiting for this completion
1574 pendingBuckets.compute(nextGrp.nextObjective().id(), (nextId, pendBkts) -> {
1575 if (pendBkts != null) {
1576 pendBkts.forEach(pendBkt -> addBucketToGroup(pendBkt, nextGrp));
1577 }
1578 return null;
1579 });
1580 });
1581 }
1582 }
1583 }
1584
Charles Chan188ebf52015-12-23 00:15:11 -08001585 /**
1586 * Processes next element of a group chain. Assumption is that if this
1587 * group points to another group, the latter has already been created
1588 * and this driver has received notification for it. A second assumption is
1589 * that if there is another group waiting for this group then the appropriate
1590 * stores already have the information to act upon the notification for the
1591 * creation of this group.
1592 * <p>
1593 * The processing of the GroupChainElement depends on the number of groups
1594 * this element is waiting on. For all group types other than SIMPLE, a
1595 * GroupChainElement could be waiting on multiple groups.
1596 *
1597 * @param gce the group chain element to be processed next
1598 */
1599 private void processGroupChain(GroupChainElem gce) {
1600 int waitOnGroups = gce.decrementAndGetGroupsWaitedOn();
1601 if (waitOnGroups != 0) {
1602 log.debug("GCE: {} not ready to be processed", gce);
1603 return;
1604 }
1605 log.debug("GCE: {} ready to be processed", gce);
Yi Tsengef19de12017-04-24 11:33:05 -07001606 if (gce.addBucketToGroup()) {
1607 groupService.addBucketsToGroup(gce.groupDescription().deviceId(),
1608 gce.groupDescription().appCookie(),
1609 gce.groupDescription().buckets(),
1610 gce.groupDescription().appCookie(),
1611 gce.groupDescription().appId());
Charles Chan188ebf52015-12-23 00:15:11 -08001612 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001613 groupService.addGroup(gce.groupDescription());
Charles Chan188ebf52015-12-23 00:15:11 -08001614 }
1615 }
1616
Yi Tsengef19de12017-04-24 11:33:05 -07001617 protected void addPendingRemoveNextObjective(NextObjective nextObjective,
1618 List<GroupKey> groupKeys) {
1619 pendingRemoveNextObjectives.put(nextObjective, groupKeys);
Charles Chan188ebf52015-12-23 00:15:11 -08001620 }
1621
Saurav Das8be4e3a2016-03-11 17:19:07 -08001622 private class InnerGroupListener implements GroupListener {
1623 @Override
1624 public void event(GroupEvent event) {
1625 log.trace("received group event of type {}", event.type());
Charles Chanfc5c7802016-05-17 13:13:55 -07001626 switch (event.type()) {
1627 case GROUP_ADDED:
1628 processPendingAddGroupsOrNextObjs(event.subject().appCookie(), true);
1629 break;
1630 case GROUP_REMOVED:
1631 processPendingRemoveNextObjs(event.subject().appCookie());
1632 break;
Yi Tseng78f51f42017-02-02 13:54:58 -08001633 case GROUP_UPDATED:
1634 processPendingUpdateNextObjs(event.subject().appCookie());
1635 break;
Charles Chanfc5c7802016-05-17 13:13:55 -07001636 default:
1637 break;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001638 }
1639 }
1640 }
Charles Chan188ebf52015-12-23 00:15:11 -08001641}