blob: b4af52ee62b65aa286f2e516a4ff5cc5a7d13e02 [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;
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;
Saurav Dasc88d4662017-05-15 15:34:25 -070049import org.onosproject.net.flowobjective.Objective.Operation;
Yi Tseng47f82dc2017-03-05 22:48:39 -080050import org.onosproject.net.flowobjective.ObjectiveContext;
Charles Chan188ebf52015-12-23 00:15:11 -080051import org.onosproject.net.flowobjective.ObjectiveError;
52import org.onosproject.net.group.DefaultGroupBucket;
53import org.onosproject.net.group.DefaultGroupDescription;
54import org.onosproject.net.group.DefaultGroupKey;
55import org.onosproject.net.group.Group;
56import org.onosproject.net.group.GroupBucket;
57import org.onosproject.net.group.GroupBuckets;
58import org.onosproject.net.group.GroupDescription;
59import org.onosproject.net.group.GroupEvent;
60import org.onosproject.net.group.GroupKey;
61import org.onosproject.net.group.GroupListener;
62import org.onosproject.net.group.GroupService;
Saurav Das8be4e3a2016-03-11 17:19:07 -080063import org.onosproject.store.service.AtomicCounter;
64import org.onosproject.store.service.StorageService;
Charles Chan188ebf52015-12-23 00:15:11 -080065import org.slf4j.Logger;
66
67import java.util.ArrayDeque;
68import java.util.ArrayList;
69import java.util.Collection;
70import java.util.Collections;
71import java.util.Deque;
72import java.util.List;
Charles Chand0fd5dc2016-02-16 23:14:49 -080073import java.util.Objects;
Charles Chan188ebf52015-12-23 00:15:11 -080074import java.util.Set;
75import java.util.concurrent.ConcurrentHashMap;
76import java.util.concurrent.CopyOnWriteArrayList;
77import java.util.concurrent.Executors;
78import java.util.concurrent.ScheduledExecutorService;
79import java.util.concurrent.TimeUnit;
Charles Chan188ebf52015-12-23 00:15:11 -080080import java.util.stream.Collectors;
81
82import static org.onlab.util.Tools.groupedThreads;
Yi Tsengef19de12017-04-24 11:33:05 -070083import static org.onosproject.driver.pipeline.ofdpa.Ofdpa2Pipeline.*;
84import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
Pier Ventre42287df2016-11-09 14:17:26 -080085import static org.onosproject.net.flow.criteria.Criterion.Type.TUNNEL_ID;
Pier Ventre140a8942016-11-02 07:26:38 -070086import static org.onosproject.net.flow.criteria.Criterion.Type.VLAN_VID;
Yi Tseng78f51f42017-02-02 13:54:58 -080087import static org.onosproject.net.group.GroupDescription.Type.ALL;
88import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Charles Chan188ebf52015-12-23 00:15:11 -080089import static org.slf4j.LoggerFactory.getLogger;
90
91/**
Saurav Das961beb22017-03-29 19:09:17 -070092 * Group handler that emulates Broadcom OF-DPA TTP.
Charles Chan188ebf52015-12-23 00:15:11 -080093 */
Charles Chan361154b2016-03-24 10:23:39 -070094public class Ofdpa2GroupHandler {
Yi Tsengef19de12017-04-24 11:33:05 -070095 protected final Logger log = getLogger(getClass());
Charles Chane849c192016-01-11 18:28:54 -080096
Yi Tsengef19de12017-04-24 11:33:05 -070097 // Services, Stores
Charles Chan188ebf52015-12-23 00:15:11 -080098 protected GroupService groupService;
Saurav Das8be4e3a2016-03-11 17:19:07 -080099 protected StorageService storageService;
Yi Tsengef19de12017-04-24 11:33:05 -0700100 protected FlowObjectiveStore flowObjectiveStore;
Charles Chan188ebf52015-12-23 00:15:11 -0800101
102 // index number for group creation
Saurav Das8be4e3a2016-03-11 17:19:07 -0800103 private AtomicCounter nextIndex;
Charles Chan188ebf52015-12-23 00:15:11 -0800104
Yi Tsengef19de12017-04-24 11:33:05 -0700105 protected DeviceId deviceId;
106 private Cache<GroupKey, List<OfdpaGroupHandlerUtility.OfdpaNextGroup>> pendingAddNextObjectives;
107 private Cache<NextObjective, List<GroupKey>> pendingRemoveNextObjectives;
108 private Cache<GroupKey, Set<OfdpaGroupHandlerUtility.GroupChainElem>> pendingGroups;
109 private ConcurrentHashMap<GroupKey, Set<NextObjective>> pendingUpdateNextObjectives;
110
Yi Tseng47f82dc2017-03-05 22:48:39 -0800111 // local store for pending bucketAdds - by design there can be multiple
Charles Chan188ebf52015-12-23 00:15:11 -0800112 // pending bucket for a group
Yi Tseng47f82dc2017-03-05 22:48:39 -0800113 protected ConcurrentHashMap<Integer, Set<NextObjective>> pendingBuckets =
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700114 new ConcurrentHashMap<>();
Charles Chan188ebf52015-12-23 00:15:11 -0800115
Yi Tsengef19de12017-04-24 11:33:05 -0700116 private ScheduledExecutorService groupCheckerExecutor =
117 Executors.newScheduledThreadPool(2, groupedThreads("onos/pipeliner", "ofdpa-%d", log));
118
119 public Cache<GroupKey, List<OfdpaNextGroup>> pendingAddNextObjectives() {
120 return pendingAddNextObjectives;
121 }
122
123 public Cache<GroupKey, Set<GroupChainElem>> pendingGroups() {
124 return pendingGroups;
125 }
126
Charles Chan40132b32017-01-22 00:19:37 -0800127 /**
128 * Determines whether this pipeline support copy ttl instructions or not.
129 *
130 * @return true if copy ttl instructions are supported
131 */
132 protected boolean supportCopyTtl() {
133 return true;
134 }
135
136 /**
137 * Determines whether this pipeline support set mpls bos instruction or not.
138 *
139 * @return true if set mpls bos instruction is supported
140 */
141 protected boolean supportSetMplsBos() {
142 return true;
143 }
144
Charles Chan0f43e472017-02-14 14:00:16 -0800145 /**
146 * Determines whether this pipeline requires popping VLAN before pushing MPLS.
147 * <p>
148 * If required, pop vlan before push mpls and add an arbitrary vlan back afterward.
149 * MPLS interface group will substitute the arbitrary VLAN with expected VLAN later on.
150 *
151 * @return true if this pipeline requires popping VLAN before pushing MPLS
152 */
153 protected boolean requireVlanPopBeforeMplsPush() {
154 return false;
155 }
156
Charles Chan188ebf52015-12-23 00:15:11 -0800157 protected void init(DeviceId deviceId, PipelinerContext context) {
Yi Tsengef19de12017-04-24 11:33:05 -0700158 ServiceDirectory serviceDirectory = context.directory();
Charles Chan188ebf52015-12-23 00:15:11 -0800159 this.deviceId = deviceId;
160 this.flowObjectiveStore = context.store();
Charles Chan188ebf52015-12-23 00:15:11 -0800161 this.groupService = serviceDirectory.get(GroupService.class);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800162 this.storageService = serviceDirectory.get(StorageService.class);
Madan Jampanid5714e02016-04-19 14:15:20 -0700163 this.nextIndex = storageService.getAtomicCounter("group-id-index-counter");
Charles Chan188ebf52015-12-23 00:15:11 -0800164
Charles Chanfc5c7802016-05-17 13:13:55 -0700165 pendingAddNextObjectives = CacheBuilder.newBuilder()
Charles Chan188ebf52015-12-23 00:15:11 -0800166 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700167 .removalListener((RemovalNotification<GroupKey, List<OfdpaNextGroup>> notification) -> {
168 if (notification.getCause() == RemovalCause.EXPIRED &&
169 Objects.nonNull(notification.getValue())) {
170 notification.getValue()
171 .forEach(ofdpaNextGrp ->
172 fail(ofdpaNextGrp.nextObjective(),
173 ObjectiveError.GROUPINSTALLATIONFAILED));
Charles Chanfc5c7802016-05-17 13:13:55 -0700174 }
175 }).build();
Charles Chan188ebf52015-12-23 00:15:11 -0800176
Charles Chanfc5c7802016-05-17 13:13:55 -0700177 pendingRemoveNextObjectives = CacheBuilder.newBuilder()
178 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700179 .removalListener((RemovalNotification<NextObjective, List<GroupKey>> notification) -> {
Charles Chanfc5c7802016-05-17 13:13:55 -0700180 if (notification.getCause() == RemovalCause.EXPIRED) {
Yi Tsengef19de12017-04-24 11:33:05 -0700181 fail(notification.getKey(),
182 ObjectiveError.GROUPREMOVALFAILED);
Charles Chan188ebf52015-12-23 00:15:11 -0800183 }
184 }).build();
Saurav Das961beb22017-03-29 19:09:17 -0700185 pendingGroups = CacheBuilder.newBuilder()
186 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700187 .removalListener((RemovalNotification<GroupKey, Set<GroupChainElem>> notification) -> {
Saurav Das961beb22017-03-29 19:09:17 -0700188 if (notification.getCause() == RemovalCause.EXPIRED) {
189 log.error("Unable to install group with key {} and pending GCEs: {}",
190 notification.getKey(), notification.getValue());
191 }
192 }).build();
Yi Tseng78f51f42017-02-02 13:54:58 -0800193 pendingUpdateNextObjectives = new ConcurrentHashMap<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700194 GroupChecker groupChecker = new GroupChecker(this);
195 groupCheckerExecutor.scheduleAtFixedRate(groupChecker, 0, 500, TimeUnit.MILLISECONDS);
Charles Chan188ebf52015-12-23 00:15:11 -0800196 groupService.addListener(new InnerGroupListener());
197 }
198
Saurav Das8be4e3a2016-03-11 17:19:07 -0800199 //////////////////////////////////////
200 // Group Creation
201 //////////////////////////////////////
202
Yi Tsengef19de12017-04-24 11:33:05 -0700203 /**
204 * Adds a list of group chain by given NextObjective.
205 *
206 * @param nextObjective the NextObjective
207 */
Charles Chan188ebf52015-12-23 00:15:11 -0800208 protected void addGroup(NextObjective nextObjective) {
209 switch (nextObjective.type()) {
210 case SIMPLE:
211 Collection<TrafficTreatment> treatments = nextObjective.next();
212 if (treatments.size() != 1) {
213 log.error("Next Objectives of type Simple should only have a "
214 + "single Traffic Treatment. Next Objective Id:{}",
215 nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -0700216 fail(nextObjective, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800217 return;
218 }
219 processSimpleNextObjective(nextObjective);
220 break;
221 case BROADCAST:
222 processBroadcastNextObjective(nextObjective);
223 break;
224 case HASHED:
Pier Ventre140a8942016-11-02 07:26:38 -0700225 if (!verifyHashedNextObjective(nextObjective)) {
226 log.error("Next Objectives of type hashed not supported. Next Objective Id:{}",
227 nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -0700228 fail(nextObjective, ObjectiveError.BADPARAMS);
Pier Ventre140a8942016-11-02 07:26:38 -0700229 return;
230 }
Charles Chan188ebf52015-12-23 00:15:11 -0800231 processHashedNextObjective(nextObjective);
232 break;
233 case FAILOVER:
Yi Tsengef19de12017-04-24 11:33:05 -0700234 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -0800235 log.warn("Unsupported next objective type {}", nextObjective.type());
236 break;
237 default:
Yi Tsengef19de12017-04-24 11:33:05 -0700238 fail(nextObjective, ObjectiveError.UNKNOWN);
Charles Chan188ebf52015-12-23 00:15:11 -0800239 log.warn("Unknown next objective type {}", nextObjective.type());
240 }
241 }
242
243 /**
244 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
245 * a chain of groups. The simple Next Objective passed
246 * in by the application has to be broken up into a group chain
247 * comprising of an L3 Unicast Group that points to an L2 Interface
248 * Group which in-turn points to an output port. In some cases, the simple
249 * next Objective can just be an L2 interface without the need for chaining.
250 *
251 * @param nextObj the nextObjective of type SIMPLE
252 */
253 private void processSimpleNextObjective(NextObjective nextObj) {
254 TrafficTreatment treatment = nextObj.next().iterator().next();
255 // determine if plain L2 or L3->L2
256 boolean plainL2 = true;
257 for (Instruction ins : treatment.allInstructions()) {
258 if (ins.type() == Instruction.Type.L2MODIFICATION) {
259 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
260 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_DST ||
261 l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_SRC) {
262 plainL2 = false;
263 break;
264 }
265 }
266 }
267
268 if (plainL2) {
269 createL2InterfaceGroup(nextObj);
270 return;
271 }
272
Pier Ventre140a8942016-11-02 07:26:38 -0700273 boolean isMpls = false;
Pier Ventre42287df2016-11-09 14:17:26 -0800274 // In order to understand if it is a pseudo wire related
275 // next objective we look for the tunnel id in the meta.
276 boolean isPw = false;
Pier Ventre140a8942016-11-02 07:26:38 -0700277 if (nextObj.meta() != null) {
278 isMpls = isNotMplsBos(nextObj.meta());
Pier Ventre42287df2016-11-09 14:17:26 -0800279
280 TunnelIdCriterion tunnelIdCriterion = (TunnelIdCriterion) nextObj
281 .meta()
282 .getCriterion(TUNNEL_ID);
283 if (tunnelIdCriterion != null) {
284 isPw = true;
285 }
286
Pier Ventre140a8942016-11-02 07:26:38 -0700287 }
288
Pier Ventre42287df2016-11-09 14:17:26 -0800289 if (!isPw) {
290 // break up simple next objective to GroupChain objects
291 GroupInfo groupInfo = createL2L3Chain(treatment, nextObj.id(),
292 nextObj.appId(), isMpls,
293 nextObj.meta());
294 if (groupInfo == null) {
295 log.error("Could not process nextObj={} in dev:{}", nextObj.id(), deviceId);
296 return;
297 }
298 // create object for local and distributed storage
299 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700300 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
301 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Pier Ventre42287df2016-11-09 14:17:26 -0800302 OfdpaNextGroup ofdpaGrp =
303 new OfdpaNextGroup(Collections.singletonList(gkeyChain), nextObj);
304
305 // store l3groupkey with the ofdpaNextGroup for the nextObjective that depends on it
Yi Tsengef19de12017-04-24 11:33:05 -0700306 updatePendingNextObjective(groupInfo.nextGroupDesc().appCookie(), ofdpaGrp);
Pier Ventre42287df2016-11-09 14:17:26 -0800307
308 // now we are ready to send the l2 groupDescription (inner), as all the stores
309 // that will get async replies have been updated. By waiting to update
310 // the stores, we prevent nasty race conditions.
Yi Tsengef19de12017-04-24 11:33:05 -0700311 groupService.addGroup(groupInfo.innerMostGroupDesc());
Pier Ventre42287df2016-11-09 14:17:26 -0800312 } else {
313 // We handle the pseudo wire with a different a procedure.
314 // This procedure is meant to handle both initiation and
315 // termination of the pseudo wire.
316 processPwNextObjective(nextObj);
Charles Chan188ebf52015-12-23 00:15:11 -0800317 }
Charles Chan188ebf52015-12-23 00:15:11 -0800318 }
319
Charles Chan188ebf52015-12-23 00:15:11 -0800320 /**
321 * Creates a simple L2 Interface Group.
322 *
323 * @param nextObj the next Objective
324 */
325 private void createL2InterfaceGroup(NextObjective nextObj) {
Yi Tsengef19de12017-04-24 11:33:05 -0700326 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700327 if (assignedVlan == null) {
328 log.warn("VLAN ID required by simple next obj is missing. Abort.");
Yi Tsengef19de12017-04-24 11:33:05 -0700329 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800330 return;
331 }
332
Charles Chan5b9df8d2016-03-28 22:21:40 -0700333 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Charles Chan188ebf52015-12-23 00:15:11 -0800334
Charles Chan5b9df8d2016-03-28 22:21:40 -0700335 // There is only one L2 interface group in this case
Yi Tsengef19de12017-04-24 11:33:05 -0700336 GroupDescription l2InterfaceGroupDesc = groupInfos.get(0).innerMostGroupDesc();
Charles Chan188ebf52015-12-23 00:15:11 -0800337
Charles Chan5b9df8d2016-03-28 22:21:40 -0700338 // Put all dependency information into allGroupKeys
339 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
340 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
341 gkeyChain.addFirst(l2InterfaceGroupDesc.appCookie());
342 allGroupKeys.add(gkeyChain);
Charles Chan188ebf52015-12-23 00:15:11 -0800343
Charles Chan5b9df8d2016-03-28 22:21:40 -0700344 // Point the next objective to this group
345 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
346 updatePendingNextObjective(l2InterfaceGroupDesc.appCookie(), ofdpaGrp);
347
348 // Start installing the inner-most group
349 groupService.addGroup(l2InterfaceGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800350 }
351
352 /**
353 * Creates one of two possible group-chains from the treatment
354 * passed in. Depending on the MPLS boolean, this method either creates
Charles Chan425854b2016-04-11 15:32:12 -0700355 * an L3Unicast Group --&gt; L2Interface Group, if mpls is false;
356 * or MPLSInterface Group --&gt; L2Interface Group, if mpls is true;
Charles Chan188ebf52015-12-23 00:15:11 -0800357 * The returned 'inner' group description is always the L2 Interface group.
358 *
359 * @param treatment that needs to be broken up to create the group chain
360 * @param nextId of the next objective that needs this group chain
361 * @param appId of the application that sent this next objective
362 * @param mpls determines if L3Unicast or MPLSInterface group is created
363 * @param meta metadata passed in by the application as part of the nextObjective
364 * @return GroupInfo containing the GroupDescription of the
365 * L2Interface group(inner) and the GroupDescription of the (outer)
366 * L3Unicast/MPLSInterface group. May return null if there is an
367 * error in processing the chain
368 */
Charles Chan425854b2016-04-11 15:32:12 -0700369 protected GroupInfo createL2L3Chain(TrafficTreatment treatment, int nextId,
Charles Chanf9e98652016-09-07 16:54:23 -0700370 ApplicationId appId, boolean mpls,
371 TrafficSelector meta) {
372 return createL2L3ChainInternal(treatment, nextId, appId, mpls, meta, true);
373 }
374
375 /**
376 * Internal implementation of createL2L3Chain.
377 * <p>
378 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
379 * Since it is non-OF spec, we need an extension treatment for that.
380 * The useSetVlanExtension must be set to false for OFDPA i12.
381 * </p>
382 *
383 * @param treatment that needs to be broken up to create the group chain
384 * @param nextId of the next objective that needs this group chain
385 * @param appId of the application that sent this next objective
386 * @param mpls determines if L3Unicast or MPLSInterface group is created
387 * @param meta metadata passed in by the application as part of the nextObjective
388 * @param useSetVlanExtension use the setVlanVid extension that has is_present bit set to 0.
389 * @return GroupInfo containing the GroupDescription of the
390 * L2Interface group(inner) and the GroupDescription of the (outer)
391 * L3Unicast/MPLSInterface group. May return null if there is an
392 * error in processing the chain
393 */
394 protected GroupInfo createL2L3ChainInternal(TrafficTreatment treatment, int nextId,
Pier Ventre42287df2016-11-09 14:17:26 -0800395 ApplicationId appId, boolean mpls,
396 TrafficSelector meta, boolean useSetVlanExtension) {
Charles Chan188ebf52015-12-23 00:15:11 -0800397 // for the l2interface group, get vlan and port info
398 // for the outer group, get the src/dst mac, and vlan info
399 TrafficTreatment.Builder outerTtb = DefaultTrafficTreatment.builder();
400 TrafficTreatment.Builder innerTtb = DefaultTrafficTreatment.builder();
401 VlanId vlanid = null;
402 long portNum = 0;
403 boolean setVlan = false, popVlan = false;
Yi Tseng78f51f42017-02-02 13:54:58 -0800404 MacAddress srcMac;
405 MacAddress dstMac;
Charles Chan188ebf52015-12-23 00:15:11 -0800406 for (Instruction ins : treatment.allInstructions()) {
407 if (ins.type() == Instruction.Type.L2MODIFICATION) {
408 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
409 switch (l2ins.subtype()) {
410 case ETH_DST:
Charles Chan5270ed02016-01-30 23:22:37 -0800411 dstMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
412 outerTtb.setEthDst(dstMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800413 break;
414 case ETH_SRC:
Charles Chand0fd5dc2016-02-16 23:14:49 -0800415 srcMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
416 outerTtb.setEthSrc(srcMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800417 break;
418 case VLAN_ID:
419 vlanid = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
Charles Chanf9e98652016-09-07 16:54:23 -0700420 if (useSetVlanExtension) {
421 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
422 outerTtb.extension(ofdpaSetVlanVid, deviceId);
423 } else {
424 outerTtb.setVlanId(vlanid);
425 }
Charles Chan188ebf52015-12-23 00:15:11 -0800426 setVlan = true;
427 break;
428 case VLAN_POP:
429 innerTtb.popVlan();
430 popVlan = true;
431 break;
432 case DEC_MPLS_TTL:
433 case MPLS_LABEL:
434 case MPLS_POP:
435 case MPLS_PUSH:
436 case VLAN_PCP:
437 case VLAN_PUSH:
438 default:
439 break;
440 }
441 } else if (ins.type() == Instruction.Type.OUTPUT) {
442 portNum = ((Instructions.OutputInstruction) ins).port().toLong();
443 innerTtb.add(ins);
444 } else {
Saurav Das7bcbe702017-06-13 15:35:54 -0700445 log.debug("Driver does not handle this type of TrafficTreatment"
Charles Chan188ebf52015-12-23 00:15:11 -0800446 + " instruction in nextObjectives: {}", ins.type());
447 }
448 }
449
450 if (vlanid == null && meta != null) {
451 // use metadata if available
Pier Ventre140a8942016-11-02 07:26:38 -0700452 Criterion vidCriterion = meta.getCriterion(VLAN_VID);
Charles Chan188ebf52015-12-23 00:15:11 -0800453 if (vidCriterion != null) {
454 vlanid = ((VlanIdCriterion) vidCriterion).vlanId();
455 }
456 // if vlan is not set, use the vlan in metadata for outerTtb
457 if (vlanid != null && !setVlan) {
Charles Chanf9e98652016-09-07 16:54:23 -0700458 if (useSetVlanExtension) {
459 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
460 outerTtb.extension(ofdpaSetVlanVid, deviceId);
461 } else {
462 outerTtb.setVlanId(vlanid);
463 }
Charles Chan188ebf52015-12-23 00:15:11 -0800464 }
465 }
466
467 if (vlanid == null) {
468 log.error("Driver cannot process an L2/L3 group chain without "
469 + "egress vlan information for dev: {} port:{}",
470 deviceId, portNum);
471 return null;
472 }
473
474 if (!setVlan && !popVlan) {
475 // untagged outgoing port
476 TrafficTreatment.Builder temp = DefaultTrafficTreatment.builder();
477 temp.popVlan();
Yi Tsengef19de12017-04-24 11:33:05 -0700478 innerTtb.build().allInstructions().forEach(temp::add);
Charles Chan188ebf52015-12-23 00:15:11 -0800479 innerTtb = temp;
480 }
481
482 // assemble information for ofdpa l2interface group
Yi Tsengef19de12017-04-24 11:33:05 -0700483 int l2groupId = l2GroupId(vlanid, portNum);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800484 // a globally unique groupkey that is different for ports in the same device,
Charles Chan188ebf52015-12-23 00:15:11 -0800485 // but different for the same portnumber on different devices. Also different
486 // for the various group-types created out of the same next objective.
Charles Chane849c192016-01-11 18:28:54 -0800487 int l2gk = l2InterfaceGroupKey(deviceId, vlanid, portNum);
Yi Tsengef19de12017-04-24 11:33:05 -0700488 final GroupKey l2groupkey = new DefaultGroupKey(appKryo.serialize(l2gk));
Charles Chan188ebf52015-12-23 00:15:11 -0800489
490 // assemble information for outer group
Yi Tsengef19de12017-04-24 11:33:05 -0700491 GroupDescription outerGrpDesc;
Charles Chan188ebf52015-12-23 00:15:11 -0800492 if (mpls) {
Yi Tsengef19de12017-04-24 11:33:05 -0700493 // outer group is MPLS Interface
Saurav Das8be4e3a2016-03-11 17:19:07 -0800494 int mplsInterfaceIndex = getNextAvailableIndex();
Yi Tsengef19de12017-04-24 11:33:05 -0700495 int mplsGroupId = MPLS_INTERFACE_TYPE | (SUBTYPE_MASK & mplsInterfaceIndex);
496 final GroupKey mplsGroupKey = new DefaultGroupKey(
497 appKryo.serialize(mplsInterfaceIndex));
Yi Tsengfa394de2017-02-01 11:26:40 -0800498 outerTtb.group(new GroupId(l2groupId));
Charles Chan188ebf52015-12-23 00:15:11 -0800499 // create the mpls-interface group description to wait for the
500 // l2 interface group to be processed
501 GroupBucket mplsinterfaceGroupBucket =
502 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
503 outerGrpDesc = new DefaultGroupDescription(
504 deviceId,
505 GroupDescription.Type.INDIRECT,
506 new GroupBuckets(Collections.singletonList(
507 mplsinterfaceGroupBucket)),
Yi Tsengef19de12017-04-24 11:33:05 -0700508 mplsGroupKey,
509 mplsGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800510 appId);
511 log.debug("Trying MPLS-Interface: device:{} gid:{} gkey:{} nextid:{}",
Yi Tsengef19de12017-04-24 11:33:05 -0700512 deviceId, Integer.toHexString(mplsGroupId),
513 mplsGroupKey, nextId);
Charles Chan188ebf52015-12-23 00:15:11 -0800514 } else {
515 // outer group is L3Unicast
Saurav Das8be4e3a2016-03-11 17:19:07 -0800516 int l3unicastIndex = getNextAvailableIndex();
517 int l3groupId = L3_UNICAST_TYPE | (TYPE_MASK & l3unicastIndex);
518 final GroupKey l3groupkey = new DefaultGroupKey(
Yi Tsengef19de12017-04-24 11:33:05 -0700519 appKryo.serialize(l3unicastIndex));
Yi Tsengfa394de2017-02-01 11:26:40 -0800520 outerTtb.group(new GroupId(l2groupId));
Charles Chan188ebf52015-12-23 00:15:11 -0800521 // create the l3unicast group description to wait for the
522 // l2 interface group to be processed
523 GroupBucket l3unicastGroupBucket =
524 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
525 outerGrpDesc = new DefaultGroupDescription(
526 deviceId,
527 GroupDescription.Type.INDIRECT,
Yi Tsengef19de12017-04-24 11:33:05 -0700528 new GroupBuckets(Collections.singletonList(l3unicastGroupBucket)),
Charles Chan188ebf52015-12-23 00:15:11 -0800529 l3groupkey,
530 l3groupId,
531 appId);
532 log.debug("Trying L3Unicast: device:{} gid:{} gkey:{} nextid:{}",
533 deviceId, Integer.toHexString(l3groupId),
534 l3groupkey, nextId);
535 }
536
537 // store l2groupkey with the groupChainElem for the outer-group that depends on it
Yi Tsengef19de12017-04-24 11:33:05 -0700538 GroupChainElem gce = new GroupChainElem(outerGrpDesc, 1, false, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800539 updatePendingGroups(l2groupkey, gce);
540
Yi Tsengef19de12017-04-24 11:33:05 -0700541 // create group description for the inner l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700542 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800543 DefaultGroupBucket.createIndirectGroupBucket(innerTtb.build());
544 GroupDescription l2groupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -0700545 new DefaultGroupDescription(deviceId,
546 GroupDescription.Type.INDIRECT,
547 new GroupBuckets(Collections.singletonList(l2InterfaceGroupBucket)),
548 l2groupkey,
549 l2groupId,
550 appId);
Charles Chan188ebf52015-12-23 00:15:11 -0800551 log.debug("Trying L2Interface: device:{} gid:{} gkey:{} nextId:{}",
552 deviceId, Integer.toHexString(l2groupId),
553 l2groupkey, nextId);
554 return new GroupInfo(l2groupDescription, outerGrpDesc);
555
556 }
557
558 /**
559 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
560 * a chain of groups. The broadcast Next Objective passed in by the application
561 * has to be broken up into a group chain comprising of an
Saurav Das1a129a02016-11-18 15:21:57 -0800562 * L2 Flood group or L3 Multicast group, whose buckets point to L2 Interface groups.
Charles Chan188ebf52015-12-23 00:15:11 -0800563 *
564 * @param nextObj the nextObjective of type BROADCAST
565 */
566 private void processBroadcastNextObjective(NextObjective nextObj) {
Yi Tsengef19de12017-04-24 11:33:05 -0700567 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700568 if (assignedVlan == null) {
569 log.warn("VLAN ID required by broadcast next obj is missing. Abort.");
Yi Tsengef19de12017-04-24 11:33:05 -0700570 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800571 return;
572 }
Charles Chan188ebf52015-12-23 00:15:11 -0800573
Charles Chan5b9df8d2016-03-28 22:21:40 -0700574 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
575
Yi Tsengef19de12017-04-24 11:33:05 -0700576 IpPrefix ipDst = readIpDstFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700577 if (ipDst != null) {
578 if (ipDst.isMulticast()) {
579 createL3MulticastGroup(nextObj, assignedVlan, groupInfos);
580 } else {
581 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -0700582 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700583 }
584 } else {
585 createL2FloodGroup(nextObj, assignedVlan, groupInfos);
586 }
587 }
588
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700589 private List<GroupInfo> prepareL2InterfaceGroup(NextObjective nextObj,
590 VlanId assignedVlan) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700591 ImmutableList.Builder<GroupInfo> groupInfoBuilder = ImmutableList.builder();
592
593 // break up broadcast next objective to multiple groups
594 Collection<TrafficTreatment> buckets = nextObj.next();
595
Charles Chan188ebf52015-12-23 00:15:11 -0800596 // each treatment is converted to an L2 interface group
Charles Chan188ebf52015-12-23 00:15:11 -0800597 for (TrafficTreatment treatment : buckets) {
598 TrafficTreatment.Builder newTreatment = DefaultTrafficTreatment.builder();
599 PortNumber portNum = null;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700600 VlanId egressVlan = null;
Charles Chan188ebf52015-12-23 00:15:11 -0800601 // ensure that the only allowed treatments are pop-vlan and output
602 for (Instruction ins : treatment.allInstructions()) {
603 if (ins.type() == Instruction.Type.L2MODIFICATION) {
604 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
605 switch (l2ins.subtype()) {
606 case VLAN_POP:
607 newTreatment.add(l2ins);
608 break;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700609 case VLAN_ID:
610 egressVlan = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
611 break;
Charles Chan188ebf52015-12-23 00:15:11 -0800612 default:
613 log.debug("action {} not permitted for broadcast nextObj",
614 l2ins.subtype());
615 break;
616 }
617 } else if (ins.type() == Instruction.Type.OUTPUT) {
618 portNum = ((Instructions.OutputInstruction) ins).port();
619 newTreatment.add(ins);
620 } else {
Charles Chane849c192016-01-11 18:28:54 -0800621 log.debug("TrafficTreatment of type {} not permitted in " +
622 " broadcast nextObjective", ins.type());
Charles Chan188ebf52015-12-23 00:15:11 -0800623 }
624 }
625
Yi Tsengef19de12017-04-24 11:33:05 -0700626 if (portNum == null) {
627 log.warn("Can't find output port for the bucket {}.", treatment);
628 continue;
629 }
630
Charles Chan188ebf52015-12-23 00:15:11 -0800631 // assemble info for l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700632 VlanId l2InterfaceGroupVlan =
633 (egressVlan != null && !assignedVlan.equals(egressVlan)) ?
634 egressVlan : assignedVlan;
635 int l2gk = l2InterfaceGroupKey(deviceId, l2InterfaceGroupVlan, portNum.toLong());
636 final GroupKey l2InterfaceGroupKey =
Yi Tsengef19de12017-04-24 11:33:05 -0700637 new DefaultGroupKey(appKryo.serialize(l2gk));
Charles Chan372b63e2017-02-07 12:10:53 -0800638 int l2InterfaceGroupId = L2_INTERFACE_TYPE |
639 ((l2InterfaceGroupVlan.toShort() & THREE_BIT_MASK) << PORT_LEN) |
640 ((int) portNum.toLong() & FOUR_BIT_MASK);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700641 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800642 DefaultGroupBucket.createIndirectGroupBucket(newTreatment.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700643 GroupDescription l2InterfaceGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -0700644 new DefaultGroupDescription(deviceId,
645 GroupDescription.Type.INDIRECT,
646 new GroupBuckets(Collections.singletonList(
647 l2InterfaceGroupBucket)),
648 l2InterfaceGroupKey,
649 l2InterfaceGroupId,
650 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800651 log.debug("Trying L2-Interface: device:{} gid:{} gkey:{} nextid:{}",
Charles Chan5b9df8d2016-03-28 22:21:40 -0700652 deviceId, Integer.toHexString(l2InterfaceGroupId),
653 l2InterfaceGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -0800654
Charles Chan5b9df8d2016-03-28 22:21:40 -0700655 groupInfoBuilder.add(new GroupInfo(l2InterfaceGroupDescription,
656 l2InterfaceGroupDescription));
Charles Chan188ebf52015-12-23 00:15:11 -0800657 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700658 return groupInfoBuilder.build();
659 }
Charles Chan188ebf52015-12-23 00:15:11 -0800660
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700661 private void createL2FloodGroup(NextObjective nextObj, VlanId vlanId,
662 List<GroupInfo> groupInfos) {
663 // assemble info for l2 flood group. Since there can be only one flood
664 // group for a vlan, its index is always the same - 0
Yi Tsengef19de12017-04-24 11:33:05 -0700665 Integer l2FloodGroupId = L2_FLOOD_TYPE | (vlanId.toShort() << 16);
666 final GroupKey l2FloodGroupKey = l2FloodGroupKey(vlanId, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700667
Charles Chan188ebf52015-12-23 00:15:11 -0800668 // collection of group buckets pointing to all the l2 interface groups
Yi Tsengef19de12017-04-24 11:33:05 -0700669 List<GroupBucket> l2floodBuckets = generateNextGroupBuckets(groupInfos, ALL);
Charles Chan188ebf52015-12-23 00:15:11 -0800670 // create the l2flood group-description to wait for all the
671 // l2interface groups to be processed
672 GroupDescription l2floodGroupDescription =
673 new DefaultGroupDescription(
674 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800675 ALL,
Charles Chan188ebf52015-12-23 00:15:11 -0800676 new GroupBuckets(l2floodBuckets),
Yi Tsengef19de12017-04-24 11:33:05 -0700677 l2FloodGroupKey,
678 l2FloodGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800679 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800680 log.debug("Trying L2-Flood: device:{} gid:{} gkey:{} nextid:{}",
Yi Tsengef19de12017-04-24 11:33:05 -0700681 deviceId, Integer.toHexString(l2FloodGroupId),
682 l2FloodGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -0800683
Charles Chan5b9df8d2016-03-28 22:21:40 -0700684 // Put all dependency information into allGroupKeys
685 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
686 groupInfos.forEach(groupInfo -> {
Yi Tsengef19de12017-04-24 11:33:05 -0700687 Deque<GroupKey> groupKeyChain = new ArrayDeque<>();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700688 // In this case we should have L2 interface group only
Yi Tsengef19de12017-04-24 11:33:05 -0700689 groupKeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
690 groupKeyChain.addFirst(l2FloodGroupKey);
691 allGroupKeys.add(groupKeyChain);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700692 });
Charles Chan188ebf52015-12-23 00:15:11 -0800693
Charles Chan5b9df8d2016-03-28 22:21:40 -0700694 // Point the next objective to this group
695 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -0700696 updatePendingNextObjective(l2FloodGroupKey, ofdpaGrp);
Charles Chan188ebf52015-12-23 00:15:11 -0800697
Charles Chan5b9df8d2016-03-28 22:21:40 -0700698 GroupChainElem gce = new GroupChainElem(l2floodGroupDescription,
Yi Tsengef19de12017-04-24 11:33:05 -0700699 groupInfos.size(), false, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700700 groupInfos.forEach(groupInfo -> {
701 // Point this group to the next group
Yi Tsengef19de12017-04-24 11:33:05 -0700702 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), gce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700703 // Start installing the inner-most group
Yi Tsengef19de12017-04-24 11:33:05 -0700704 groupService.addGroup(groupInfo.innerMostGroupDesc());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700705 });
706 }
707
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700708 private void createL3MulticastGroup(NextObjective nextObj, VlanId vlanId,
709 List<GroupInfo> groupInfos) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700710 List<GroupBucket> l3McastBuckets = new ArrayList<>();
711 groupInfos.forEach(groupInfo -> {
712 // Points to L3 interface group if there is one.
713 // Otherwise points to L2 interface group directly.
Yi Tsengef19de12017-04-24 11:33:05 -0700714 GroupDescription nextGroupDesc = (groupInfo.nextGroupDesc() != null) ?
715 groupInfo.nextGroupDesc() : groupInfo.innerMostGroupDesc();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700716 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Yi Tsengfa394de2017-02-01 11:26:40 -0800717 ttb.group(new GroupId(nextGroupDesc.givenGroupId()));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700718 GroupBucket abucket = DefaultGroupBucket.createAllGroupBucket(ttb.build());
719 l3McastBuckets.add(abucket);
720 });
721
722 int l3MulticastIndex = getNextAvailableIndex();
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700723 int l3MulticastGroupId = L3_MULTICAST_TYPE |
724 vlanId.toShort() << 16 | (TYPE_VLAN_MASK & l3MulticastIndex);
725 final GroupKey l3MulticastGroupKey =
Yi Tsengef19de12017-04-24 11:33:05 -0700726 new DefaultGroupKey(appKryo.serialize(l3MulticastIndex));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700727
728 GroupDescription l3MulticastGroupDesc = new DefaultGroupDescription(deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800729 ALL,
Charles Chan5b9df8d2016-03-28 22:21:40 -0700730 new GroupBuckets(l3McastBuckets),
731 l3MulticastGroupKey,
732 l3MulticastGroupId,
733 nextObj.appId());
734
735 // Put all dependency information into allGroupKeys
736 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
737 groupInfos.forEach(groupInfo -> {
738 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700739 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700740 // Add L3 interface group to the chain if there is one.
Yi Tsengef19de12017-04-24 11:33:05 -0700741 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
742 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700743 }
744 gkeyChain.addFirst(l3MulticastGroupKey);
745 allGroupKeys.add(gkeyChain);
746 });
747
748 // Point the next objective to this group
749 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
750 updatePendingNextObjective(l3MulticastGroupKey, ofdpaGrp);
751
752 GroupChainElem outerGce = new GroupChainElem(l3MulticastGroupDesc,
Yi Tsengef19de12017-04-24 11:33:05 -0700753 groupInfos.size(), false, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700754 groupInfos.forEach(groupInfo -> {
755 // Point this group (L3 multicast) to the next group
Yi Tsengef19de12017-04-24 11:33:05 -0700756 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), outerGce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700757
758 // Point next group to inner-most group, if any
Yi Tsengef19de12017-04-24 11:33:05 -0700759 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
760 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(),
761 1, false, deviceId);
762 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700763 }
764
765 // Start installing the inner-most group
Yi Tsengef19de12017-04-24 11:33:05 -0700766 groupService.addGroup(groupInfo.innerMostGroupDesc());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700767 });
Charles Chan188ebf52015-12-23 00:15:11 -0800768 }
769
Charles Chan188ebf52015-12-23 00:15:11 -0800770 /**
771 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
772 * a chain of groups. The hashed Next Objective passed in by the application
773 * has to be broken up into a group chain comprising of an
774 * L3 ECMP group as the top level group. Buckets of this group can point
775 * to a variety of groups in a group chain, depending on the whether
776 * MPLS labels are being pushed or not.
777 * <p>
778 * NOTE: We do not create MPLS ECMP groups as they are unimplemented in
779 * OF-DPA 2.0 (even though it is in the spec). Therefore we do not
780 * check the nextObjective meta to see what is matching before being
781 * sent to this nextObjective.
782 *
783 * @param nextObj the nextObjective of type HASHED
784 */
Pier Ventre140a8942016-11-02 07:26:38 -0700785 protected void processHashedNextObjective(NextObjective nextObj) {
Charles Chan188ebf52015-12-23 00:15:11 -0800786 // storage for all group keys in the chain of groups created
787 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
788 List<GroupInfo> unsentGroups = new ArrayList<>();
789 createHashBucketChains(nextObj, allGroupKeys, unsentGroups);
790
791 // now we can create the outermost L3 ECMP group
792 List<GroupBucket> l3ecmpGroupBuckets = new ArrayList<>();
793 for (GroupInfo gi : unsentGroups) {
794 // create ECMP bucket to point to the outer group
795 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Yi Tsengef19de12017-04-24 11:33:05 -0700796 ttb.group(new GroupId(gi.nextGroupDesc().givenGroupId()));
Charles Chan188ebf52015-12-23 00:15:11 -0800797 GroupBucket sbucket = DefaultGroupBucket
798 .createSelectGroupBucket(ttb.build());
799 l3ecmpGroupBuckets.add(sbucket);
800 }
Saurav Das8be4e3a2016-03-11 17:19:07 -0800801 int l3ecmpIndex = getNextAvailableIndex();
802 int l3ecmpGroupId = L3_ECMP_TYPE | (TYPE_MASK & l3ecmpIndex);
803 GroupKey l3ecmpGroupKey = new DefaultGroupKey(
Yi Tsengef19de12017-04-24 11:33:05 -0700804 appKryo.serialize(l3ecmpIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800805 GroupDescription l3ecmpGroupDesc =
806 new DefaultGroupDescription(
807 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800808 SELECT,
Charles Chan188ebf52015-12-23 00:15:11 -0800809 new GroupBuckets(l3ecmpGroupBuckets),
810 l3ecmpGroupKey,
811 l3ecmpGroupId,
812 nextObj.appId());
813 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
814 l3ecmpGroupBuckets.size(),
Yi Tsengef19de12017-04-24 11:33:05 -0700815 false, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800816
817 // create objects for local and distributed storage
Yi Tsengef19de12017-04-24 11:33:05 -0700818 allGroupKeys.forEach(gKeyChain -> gKeyChain.addFirst(l3ecmpGroupKey));
Charles Chan188ebf52015-12-23 00:15:11 -0800819 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
820
821 // store l3ecmpGroupKey with the ofdpaGroupChain for the nextObjective
822 // that depends on it
823 updatePendingNextObjective(l3ecmpGroupKey, ofdpaGrp);
824
825 log.debug("Trying L3ECMP: device:{} gid:{} gkey:{} nextId:{}",
826 deviceId, Integer.toHexString(l3ecmpGroupId),
827 l3ecmpGroupKey, nextObj.id());
828 // finally we are ready to send the innermost groups
829 for (GroupInfo gi : unsentGroups) {
830 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -0700831 Integer.toHexString(gi.innerMostGroupDesc().givenGroupId()), deviceId);
832 updatePendingGroups(gi.nextGroupDesc().appCookie(), l3ecmpGce);
833 groupService.addGroup(gi.innerMostGroupDesc());
Charles Chan188ebf52015-12-23 00:15:11 -0800834 }
Charles Chan188ebf52015-12-23 00:15:11 -0800835 }
836
837 /**
838 * Creates group chains for all buckets in a hashed group, and stores the
839 * GroupInfos and GroupKeys for all the groups in the lists passed in, which
840 * should be empty.
841 * <p>
842 * Does not create the top level ECMP group. Does not actually send the
843 * groups to the groupService.
844 *
845 * @param nextObj the Next Objective with buckets that need to be converted
846 * to group chains
847 * @param allGroupKeys a list to store groupKey for each bucket-group-chain
848 * @param unsentGroups a list to store GroupInfo for each bucket-group-chain
849 */
Pier Ventre140a8942016-11-02 07:26:38 -0700850 protected void createHashBucketChains(NextObjective nextObj,
Yi Tsengef19de12017-04-24 11:33:05 -0700851 List<Deque<GroupKey>> allGroupKeys,
852 List<GroupInfo> unsentGroups) {
Charles Chan188ebf52015-12-23 00:15:11 -0800853 // break up hashed next objective to multiple groups
854 Collection<TrafficTreatment> buckets = nextObj.next();
855
856 for (TrafficTreatment bucket : buckets) {
857 //figure out how many labels are pushed in each bucket
858 int labelsPushed = 0;
859 MplsLabel innermostLabel = null;
860 for (Instruction ins : bucket.allInstructions()) {
861 if (ins.type() == Instruction.Type.L2MODIFICATION) {
862 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
863 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_PUSH) {
864 labelsPushed++;
865 }
866 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) {
867 if (innermostLabel == null) {
Yi Tsengef19de12017-04-24 11:33:05 -0700868 innermostLabel =
869 ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label();
Charles Chan188ebf52015-12-23 00:15:11 -0800870 }
871 }
872 }
873 }
874
Yi Tsengef19de12017-04-24 11:33:05 -0700875 Deque<GroupKey> gKeyChain = new ArrayDeque<>();
Charles Chan188ebf52015-12-23 00:15:11 -0800876 // XXX we only deal with 0 and 1 label push right now
877 if (labelsPushed == 0) {
Yi Tsengef19de12017-04-24 11:33:05 -0700878 GroupInfo noLabelGroupInfo;
Pier Ventre140a8942016-11-02 07:26:38 -0700879 TrafficSelector metaSelector = nextObj.meta();
880 if (metaSelector != null) {
881 if (isNotMplsBos(metaSelector)) {
Yi Tsengef19de12017-04-24 11:33:05 -0700882 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
883 nextObj.appId(), true,
884 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700885 } else {
Yi Tsengef19de12017-04-24 11:33:05 -0700886 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
887 nextObj.appId(), false,
888 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700889 }
890 } else {
Yi Tsengef19de12017-04-24 11:33:05 -0700891 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
892 nextObj.appId(), false,
893 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700894 }
Yi Tsengef19de12017-04-24 11:33:05 -0700895 if (noLabelGroupInfo == null) {
Charles Chan188ebf52015-12-23 00:15:11 -0800896 log.error("Could not process nextObj={} in dev:{}",
897 nextObj.id(), deviceId);
898 return;
899 }
Yi Tsengef19de12017-04-24 11:33:05 -0700900 gKeyChain.addFirst(noLabelGroupInfo.innerMostGroupDesc().appCookie());
901 gKeyChain.addFirst(noLabelGroupInfo.nextGroupDesc().appCookie());
Charles Chan188ebf52015-12-23 00:15:11 -0800902
903 // we can't send the inner group description yet, as we have to
904 // create the dependent ECMP group first. So we store..
Yi Tsengef19de12017-04-24 11:33:05 -0700905 unsentGroups.add(noLabelGroupInfo);
Charles Chan188ebf52015-12-23 00:15:11 -0800906
907 } else if (labelsPushed == 1) {
908 GroupInfo onelabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
909 nextObj.appId(), true,
910 nextObj.meta());
911 if (onelabelGroupInfo == null) {
912 log.error("Could not process nextObj={} in dev:{}",
913 nextObj.id(), deviceId);
914 return;
915 }
916 // we need to add another group to this chain - the L3VPN group
917 TrafficTreatment.Builder l3vpnTtb = DefaultTrafficTreatment.builder();
Charles Chan0f43e472017-02-14 14:00:16 -0800918 if (requireVlanPopBeforeMplsPush()) {
919 l3vpnTtb.popVlan();
920 }
Charles Chan188ebf52015-12-23 00:15:11 -0800921 l3vpnTtb.pushMpls()
922 .setMpls(innermostLabel)
Yi Tsengef19de12017-04-24 11:33:05 -0700923 .group(new GroupId(onelabelGroupInfo.nextGroupDesc().givenGroupId()));
Charles Chan40132b32017-01-22 00:19:37 -0800924 if (supportCopyTtl()) {
925 l3vpnTtb.copyTtlOut();
926 }
927 if (supportSetMplsBos()) {
928 l3vpnTtb.setMplsBos(true);
929 }
Charles Chan0f43e472017-02-14 14:00:16 -0800930 if (requireVlanPopBeforeMplsPush()) {
931 l3vpnTtb.pushVlan().setVlanId(VlanId.vlanId(VlanId.RESERVED));
932 }
Charles Chan40132b32017-01-22 00:19:37 -0800933
Charles Chan188ebf52015-12-23 00:15:11 -0800934 GroupBucket l3vpnGrpBkt =
935 DefaultGroupBucket.createIndirectGroupBucket(l3vpnTtb.build());
Saurav Das8be4e3a2016-03-11 17:19:07 -0800936 int l3vpnIndex = getNextAvailableIndex();
Yi Tsengef19de12017-04-24 11:33:05 -0700937 int l3vpnGroupId = MPLS_L3VPN_SUBTYPE | (SUBTYPE_MASK & l3vpnIndex);
938 GroupKey l3vpnGroupKey = new DefaultGroupKey(
939 appKryo.serialize(l3vpnIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800940 GroupDescription l3vpnGroupDesc =
941 new DefaultGroupDescription(
942 deviceId,
943 GroupDescription.Type.INDIRECT,
Yi Tsengef19de12017-04-24 11:33:05 -0700944 new GroupBuckets(Collections.singletonList(l3vpnGrpBkt)),
945 l3vpnGroupKey,
946 l3vpnGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800947 nextObj.appId());
Yi Tsengef19de12017-04-24 11:33:05 -0700948 GroupChainElem l3vpnGce = new GroupChainElem(l3vpnGroupDesc,
949 1,
950 false,
951 deviceId);
952 updatePendingGroups(onelabelGroupInfo.nextGroupDesc().appCookie(), l3vpnGce);
Charles Chan188ebf52015-12-23 00:15:11 -0800953
Yi Tsengef19de12017-04-24 11:33:05 -0700954 gKeyChain.addFirst(onelabelGroupInfo.innerMostGroupDesc().appCookie());
955 gKeyChain.addFirst(onelabelGroupInfo.nextGroupDesc().appCookie());
956 gKeyChain.addFirst(l3vpnGroupKey);
Charles Chan188ebf52015-12-23 00:15:11 -0800957
958 //now we can replace the outerGrpDesc with the one we just created
Yi Tsengef19de12017-04-24 11:33:05 -0700959 onelabelGroupInfo.nextGroupDesc(l3vpnGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800960
961 // we can't send the innermost group yet, as we have to create
962 // the dependent ECMP group first. So we store ...
963 unsentGroups.add(onelabelGroupInfo);
964
Yi Tsengef19de12017-04-24 11:33:05 -0700965 log.debug("Trying L3VPN: device:{} gid:{} group key:{} nextId:{}",
966 deviceId, Integer.toHexString(l3vpnGroupId),
967 l3vpnGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -0800968
969 } else {
970 log.warn("Driver currently does not handle more than 1 MPLS "
971 + "labels. Not processing nextObjective {}", nextObj.id());
972 return;
973 }
974
975 // all groups in this chain
Yi Tsengef19de12017-04-24 11:33:05 -0700976 allGroupKeys.add(gKeyChain);
Charles Chan188ebf52015-12-23 00:15:11 -0800977 }
978 }
979
Pier Ventre42287df2016-11-09 14:17:26 -0800980 /**
981 * Processes the pseudo wire related next objective.
982 * This procedure try to reuse the mpls label groups,
983 * the mpls interface group and the l2 interface group.
984 *
985 * @param nextObjective the objective to process.
986 */
987 protected void processPwNextObjective(NextObjective nextObjective) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700988 log.warn("Pseudo wire extensions are not supported in OFDPA 2.0 {}",
989 nextObjective.id());
Pier Ventre42287df2016-11-09 14:17:26 -0800990 }
991
Saurav Das8be4e3a2016-03-11 17:19:07 -0800992 //////////////////////////////////////
993 // Group Editing
994 //////////////////////////////////////
Charles Chan188ebf52015-12-23 00:15:11 -0800995 /**
996 * Adds a bucket to the top level group of a group-chain, and creates the chain.
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700997 * Ensures that bucket being added is not a duplicate, by checking existing
Yi Tsengef19de12017-04-24 11:33:05 -0700998 * buckets for the same output port.
Charles Chan188ebf52015-12-23 00:15:11 -0800999 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001000 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -08001001 * @param next the representation of the existing group-chain for this next objective
1002 */
1003 protected void addBucketToGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001004 if (nextObjective.type() != NextObjective.Type.HASHED &&
1005 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001006 log.warn("AddBuckets not applied to nextType:{} in dev:{} for next:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001007 nextObjective.type(), deviceId, nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -07001008 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001009 return;
1010 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001011
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001012 // first check to see if bucket being added is not a duplicate of an
Yi Tsengef19de12017-04-24 11:33:05 -07001013 // existing bucket. If it is for an existing output port, then its a
1014 // duplicate.
Yi Tseng78f51f42017-02-02 13:54:58 -08001015 Set<TrafficTreatment> duplicateBuckets = Sets.newHashSet();
Yi Tsengef19de12017-04-24 11:33:05 -07001016 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
1017 Set<PortNumber> existingPorts = getExistingOutputPorts(allActiveKeys,
1018 groupService,
1019 deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -08001020 Set<TrafficTreatment> nonDuplicateBuckets = Sets.newHashSet();
1021 NextObjective objectiveToAdd;
Yi Tseng78f51f42017-02-02 13:54:58 -08001022
1023 nextObjective.next().forEach(trafficTreatment -> {
1024 PortNumber portNumber = readOutPortFromTreatment(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001025 if (portNumber == null) {
1026 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001027 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001028 if (existingPorts.contains(portNumber)) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001029 // its possible that portnumbers are same but labels are different
1030 int label = readLabelFromTreatment(trafficTreatment);
1031 if (label == -1) {
1032 duplicateBuckets.add(trafficTreatment);
1033 } else {
1034 boolean exists = existingPortAndLabel(allActiveKeys, groupService,
1035 deviceId, portNumber, label);
1036 if (exists) {
1037 duplicateBuckets.add(trafficTreatment);
1038 } else {
1039 nonDuplicateBuckets.add(trafficTreatment);
1040 }
1041 }
Yi Tseng47f82dc2017-03-05 22:48:39 -08001042 } else {
1043 nonDuplicateBuckets.add(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001044 }
1045 });
1046
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001047 if (duplicateBuckets.isEmpty()) {
1048 // use the original objective
1049 objectiveToAdd = nextObjective;
1050 } else if (!nonDuplicateBuckets.isEmpty()) {
1051 // only use the non-duplicate buckets if there are any
1052 log.debug("Some buckets {} already exist in next id {}, duplicate "
1053 + "buckets will be ignored.", duplicateBuckets, nextObjective.id());
1054 // new next objective with non duplicate treatments
Yi Tseng47f82dc2017-03-05 22:48:39 -08001055 NextObjective.Builder builder = DefaultNextObjective.builder()
1056 .withType(nextObjective.type())
1057 .withId(nextObjective.id())
1058 .withMeta(nextObjective.meta())
1059 .fromApp(nextObjective.appId());
1060 nonDuplicateBuckets.forEach(builder::addTreatment);
1061
1062 ObjectiveContext context = nextObjective.context().orElse(null);
1063 objectiveToAdd = builder.addToExisting(context);
1064 } else {
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001065 // buckets to add are already there - nothing to do
Saurav Das1547b3f2017-05-05 17:01:08 -07001066 log.debug("buckets already exist {} in next: {} ..ignoring bucket add",
1067 duplicateBuckets, nextObjective.id());
1068 pass(nextObjective);
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001069 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001070 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001071
Saurav Das1a129a02016-11-18 15:21:57 -08001072 if (nextObjective.type() == NextObjective.Type.HASHED) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001073 addBucketToHashGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001074 } else if (nextObjective.type() == NextObjective.Type.BROADCAST) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001075 addBucketToBroadcastGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001076 }
1077 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001078
Saurav Das1a129a02016-11-18 15:21:57 -08001079 private void addBucketToHashGroup(NextObjective nextObjective,
Yi Tseng78f51f42017-02-02 13:54:58 -08001080 List<Deque<GroupKey>> allActiveKeys) {
Charles Chan188ebf52015-12-23 00:15:11 -08001081 // storage for all group keys in the chain of groups created
1082 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
1083 List<GroupInfo> unsentGroups = new ArrayList<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001084 List<GroupBucket> newBuckets;
Charles Chan188ebf52015-12-23 00:15:11 -08001085 createHashBucketChains(nextObjective, allGroupKeys, unsentGroups);
1086
Yi Tseng78f51f42017-02-02 13:54:58 -08001087 // now we can create the buckets to add to the outermost L3 ECMP group
1088 newBuckets = generateNextGroupBuckets(unsentGroups, SELECT);
Charles Chan188ebf52015-12-23 00:15:11 -08001089
Saurav Das1a129a02016-11-18 15:21:57 -08001090 // retrieve the original L3 ECMP group
1091 Group l3ecmpGroup = retrieveTopLevelGroup(allActiveKeys, nextObjective.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001092 if (l3ecmpGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001093 fail(nextObjective, ObjectiveError.GROUPMISSING);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001094 return;
1095 }
Saurav Das1a129a02016-11-18 15:21:57 -08001096 GroupKey l3ecmpGroupKey = l3ecmpGroup.appCookie();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001097 int l3ecmpGroupId = l3ecmpGroup.id().id();
Charles Chan188ebf52015-12-23 00:15:11 -08001098
1099 // Although GroupDescriptions are not necessary for adding buckets to
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001100 // existing groups, we still use one in the GroupChainElem. When the latter is
Charles Chan188ebf52015-12-23 00:15:11 -08001101 // processed, the info will be extracted for the bucketAdd call to groupService
1102 GroupDescription l3ecmpGroupDesc =
Yi Tsengef19de12017-04-24 11:33:05 -07001103 new DefaultGroupDescription(deviceId,
1104 SELECT,
1105 new GroupBuckets(newBuckets),
1106 l3ecmpGroupKey,
1107 l3ecmpGroupId,
1108 nextObjective.appId());
Yi Tseng78f51f42017-02-02 13:54:58 -08001109 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
1110 unsentGroups.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001111 true,
1112 deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001113
Saurav Dasc88d4662017-05-15 15:34:25 -07001114 // update new bucket-chains
1115 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
1116 for (Deque<GroupKey> newBucketChain : allGroupKeys) {
1117 newBucketChain.addFirst(l3ecmpGroupKey);
1118 addedKeys.add(newBucketChain);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001119 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001120 updatePendingNextObjective(l3ecmpGroupKey,
1121 new OfdpaNextGroup(addedKeys, nextObjective));
Yi Tsengef19de12017-04-24 11:33:05 -07001122 log.debug("Adding to L3ECMP: device:{} gid:{} group key:{} nextId:{}",
Charles Chan188ebf52015-12-23 00:15:11 -08001123 deviceId, Integer.toHexString(l3ecmpGroupId),
1124 l3ecmpGroupKey, nextObjective.id());
Yi Tseng78f51f42017-02-02 13:54:58 -08001125
1126 unsentGroups.forEach(groupInfo -> {
1127 // send the innermost group
1128 log.debug("Sending innermost group {} in group chain on device {} ",
Saurav Dasc88d4662017-05-15 15:34:25 -07001129 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
1130 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001131 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3ecmpGce);
1132 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001133 });
Saurav Das1a129a02016-11-18 15:21:57 -08001134 }
Charles Chan188ebf52015-12-23 00:15:11 -08001135
Saurav Das1a129a02016-11-18 15:21:57 -08001136 private void addBucketToBroadcastGroup(NextObjective nextObj,
Yi Tsengef19de12017-04-24 11:33:05 -07001137 List<Deque<GroupKey>> allActiveKeys) {
1138 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001139 if (assignedVlan == null) {
1140 log.warn("VLAN ID required by broadcast next obj is missing. "
1141 + "Aborting add bucket to broadcast group for next:{} in dev:{}",
1142 nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001143 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001144 return;
1145 }
Saurav Das1a129a02016-11-18 15:21:57 -08001146 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Yi Tsengef19de12017-04-24 11:33:05 -07001147 IpPrefix ipDst = readIpDstFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001148 if (ipDst != null) {
1149 if (ipDst.isMulticast()) {
Yi Tsengef19de12017-04-24 11:33:05 -07001150 addBucketToL3MulticastGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001151 } else {
1152 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001153 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001154 }
1155 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001156 addBucketToL2FloodGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001157 }
1158 }
1159
1160 private void addBucketToL2FloodGroup(NextObjective nextObj,
1161 List<Deque<GroupKey>> allActiveKeys,
1162 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001163 VlanId assignedVlan) {
1164 Group l2FloodGroup = retrieveTopLevelGroup(allActiveKeys, nextObj.id());
1165
1166 if (l2FloodGroup == null) {
1167 log.warn("Can't find L2 flood group while adding bucket to it. NextObj = {}",
1168 nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001169 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001170 return;
1171 }
Saurav Das1a129a02016-11-18 15:21:57 -08001172
Yi Tseng78f51f42017-02-02 13:54:58 -08001173 GroupKey l2floodGroupKey = l2FloodGroup.appCookie();
1174 int l2floodGroupId = l2FloodGroup.id().id();
1175 List<GroupBucket> newBuckets = generateNextGroupBuckets(groupInfos, ALL);
1176
1177 GroupDescription l2FloodGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001178 new DefaultGroupDescription(deviceId,
1179 ALL,
1180 new GroupBuckets(newBuckets),
1181 l2floodGroupKey,
1182 l2floodGroupId,
1183 nextObj.appId());
Saurav Das1a129a02016-11-18 15:21:57 -08001184
Yi Tseng78f51f42017-02-02 13:54:58 -08001185 GroupChainElem l2FloodGroupChainElement =
1186 new GroupChainElem(l2FloodGroupDescription,
1187 groupInfos.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001188 true,
1189 deviceId);
Yi Tseng78f51f42017-02-02 13:54:58 -08001190
Yi Tseng78f51f42017-02-02 13:54:58 -08001191
1192 //ensure assignedVlan applies to the chosen group
1193 VlanId floodGroupVlan = extractVlanIdFromGroupId(l2floodGroupId);
1194
1195 if (!floodGroupVlan.equals(assignedVlan)) {
1196 log.warn("VLAN ID {} does not match Flood group {} to which bucket is "
1197 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1198 Integer.toHexString(l2floodGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001199 fail(nextObj, ObjectiveError.BADPARAMS);
Yi Tseng78f51f42017-02-02 13:54:58 -08001200 return;
1201 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001202 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
Yi Tseng78f51f42017-02-02 13:54:58 -08001203 groupInfos.forEach(groupInfo -> {
1204 // update original NextGroup with new bucket-chain
Yi Tseng78f51f42017-02-02 13:54:58 -08001205 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001206 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001207 newBucketChain.addFirst(l2floodGroupKey);
Saurav Dasc88d4662017-05-15 15:34:25 -07001208 addedKeys.add(newBucketChain);
Yi Tseng78f51f42017-02-02 13:54:58 -08001209
Yi Tsengef19de12017-04-24 11:33:05 -07001210 log.debug("Adding to L2FLOOD: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001211 deviceId, Integer.toHexString(l2floodGroupId),
1212 l2floodGroupKey, nextObj.id());
1213 // send the innermost group
1214 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001215 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001216 deviceId);
1217
Yi Tsengef19de12017-04-24 11:33:05 -07001218 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l2FloodGroupChainElement);
Yi Tseng78f51f42017-02-02 13:54:58 -08001219
Yi Tsengef19de12017-04-24 11:33:05 -07001220 DeviceId innerMostGroupDevice = groupInfo.innerMostGroupDesc().deviceId();
1221 GroupKey innerMostGroupKey = groupInfo.innerMostGroupDesc().appCookie();
Yi Tseng78f51f42017-02-02 13:54:58 -08001222 Group existsL2IGroup = groupService.getGroup(innerMostGroupDevice, innerMostGroupKey);
1223
1224 if (existsL2IGroup != null) {
1225 // group already exist
1226 processPendingAddGroupsOrNextObjs(innerMostGroupKey, true);
1227 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001228 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001229 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001230 });
Saurav Dasc88d4662017-05-15 15:34:25 -07001231
1232 updatePendingNextObjective(l2floodGroupKey,
1233 new OfdpaNextGroup(addedKeys, nextObj));
Yi Tseng78f51f42017-02-02 13:54:58 -08001234 }
1235
Saurav Das1a129a02016-11-18 15:21:57 -08001236 private void addBucketToL3MulticastGroup(NextObjective nextObj,
1237 List<Deque<GroupKey>> allActiveKeys,
1238 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001239 VlanId assignedVlan) {
1240 // create the buckets to add to the outermost L3 Multicast group
1241 List<GroupBucket> newBuckets = Lists.newArrayList();
1242 groupInfos.forEach(groupInfo -> {
1243 // Points to L3 interface group if there is one.
1244 // Otherwise points to L2 interface group directly.
Yi Tsengef19de12017-04-24 11:33:05 -07001245 GroupDescription nextGroupDesc = (groupInfo.nextGroupDesc() != null) ?
1246 groupInfo.nextGroupDesc() : groupInfo.innerMostGroupDesc();
1247 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1248 treatmentBuilder.group(new GroupId(nextGroupDesc.givenGroupId()));
1249 GroupBucket newBucket = DefaultGroupBucket.createAllGroupBucket(treatmentBuilder.build());
Yi Tseng78f51f42017-02-02 13:54:58 -08001250 newBuckets.add(newBucket);
1251 });
Saurav Das1a129a02016-11-18 15:21:57 -08001252
1253 // get the group being edited
1254 Group l3mcastGroup = retrieveTopLevelGroup(allActiveKeys, nextObj.id());
1255 if (l3mcastGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001256 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001257 return;
1258 }
1259 GroupKey l3mcastGroupKey = l3mcastGroup.appCookie();
1260 int l3mcastGroupId = l3mcastGroup.id().id();
1261
1262 //ensure assignedVlan applies to the chosen group
Yi Tseng78f51f42017-02-02 13:54:58 -08001263 VlanId expectedVlan = extractVlanIdFromGroupId(l3mcastGroupId);
Saurav Das1a129a02016-11-18 15:21:57 -08001264 if (!expectedVlan.equals(assignedVlan)) {
1265 log.warn("VLAN ID {} does not match L3 Mcast group {} to which bucket is "
1266 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1267 Integer.toHexString(l3mcastGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001268 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001269 }
1270 GroupDescription l3mcastGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001271 new DefaultGroupDescription(deviceId,
1272 ALL,
1273 new GroupBuckets(newBuckets),
1274 l3mcastGroupKey,
1275 l3mcastGroupId,
1276 nextObj.appId());
Saurav Das1a129a02016-11-18 15:21:57 -08001277 GroupChainElem l3mcastGce = new GroupChainElem(l3mcastGroupDescription,
Yi Tsengef19de12017-04-24 11:33:05 -07001278 groupInfos.size(),
1279 true,
1280 deviceId);
Saurav Dasc88d4662017-05-15 15:34:25 -07001281
1282 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
Yi Tseng78f51f42017-02-02 13:54:58 -08001283 groupInfos.forEach(groupInfo -> {
1284 // update original NextGroup with new bucket-chain
1285 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001286 newBucketChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001287 // Add L3 interface group to the chain if there is one.
Yi Tsengef19de12017-04-24 11:33:05 -07001288 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1289 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001290 }
1291 newBucketChain.addFirst(l3mcastGroupKey);
Saurav Dasc88d4662017-05-15 15:34:25 -07001292 addedKeys.add(newBucketChain);
Yi Tseng78f51f42017-02-02 13:54:58 -08001293
Yi Tsengef19de12017-04-24 11:33:05 -07001294 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3mcastGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001295 // Point next group to inner-most group, if any
Yi Tsengef19de12017-04-24 11:33:05 -07001296 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1297 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(),
1298 1,
1299 false,
1300 deviceId);
1301 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001302 }
Yi Tsengef19de12017-04-24 11:33:05 -07001303 log.debug("Adding to L3MCAST: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001304 deviceId, Integer.toHexString(l3mcastGroupId),
1305 l3mcastGroupKey, nextObj.id());
1306 // send the innermost group
1307 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001308 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001309 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001310 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001311
1312 });
1313
Saurav Das1a129a02016-11-18 15:21:57 -08001314 updatePendingNextObjective(l3mcastGroupKey,
Saurav Dasc88d4662017-05-15 15:34:25 -07001315 new OfdpaNextGroup(addedKeys, nextObj));
Charles Chan188ebf52015-12-23 00:15:11 -08001316 }
1317
1318 /**
1319 * Removes the bucket in the top level group of a possible group-chain. Does
Saurav Das1a129a02016-11-18 15:21:57 -08001320 * not remove the groups in the group-chain pointed to by this bucket, as they
Charles Chan188ebf52015-12-23 00:15:11 -08001321 * may be in use (referenced by other groups) elsewhere.
1322 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001323 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -08001324 * @param next the representation of the existing group-chain for this next objective
1325 */
1326 protected void removeBucketFromGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001327 if (nextObjective.type() != NextObjective.Type.HASHED &&
1328 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001329 log.warn("RemoveBuckets not applied to nextType:{} in dev:{} for next:{}",
1330 nextObjective.type(), deviceId, nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -07001331 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001332 return;
1333 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001334 Set<PortNumber> portsToRemove = Sets.newHashSet();
Charles Chan188ebf52015-12-23 00:15:11 -08001335 Collection<TrafficTreatment> treatments = nextObjective.next();
Yi Tseng78f51f42017-02-02 13:54:58 -08001336 for (TrafficTreatment treatment : treatments) {
1337 // find the bucket to remove by noting the outport, and figuring out the
1338 // top-level group in the group-chain that indirectly references the port
1339 PortNumber portToRemove = readOutPortFromTreatment(treatment);
1340 if (portToRemove == null) {
1341 log.warn("treatment {} of next objective {} has no outport.. cannot remove bucket"
1342 + "from group in dev: {}", treatment, nextObjective.id(), deviceId);
1343 } else {
1344 portsToRemove.add(portToRemove);
1345 }
1346 }
1347
1348 if (portsToRemove.isEmpty()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001349 log.warn("next objective {} has no outport.. cannot remove bucket"
Yi Tseng78f51f42017-02-02 13:54:58 -08001350 + "from group in dev: {}", nextObjective.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001351 fail(nextObjective, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -08001352 }
1353
Yi Tsengef19de12017-04-24 11:33:05 -07001354 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Yi Tseng78f51f42017-02-02 13:54:58 -08001355 List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001356 for (Deque<GroupKey> gkeys : allActiveKeys) {
1357 // last group in group chain should have a single bucket pointing to port
Charles Chan188ebf52015-12-23 00:15:11 -08001358 GroupKey groupWithPort = gkeys.peekLast();
1359 Group group = groupService.getGroup(deviceId, groupWithPort);
1360 if (group == null) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001361 log.warn("Inconsistent group chain found when removing bucket"
1362 + "for next:{} in dev:{}", nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001363 continue;
1364 }
Yi Tseng91cf2d42017-04-10 17:05:37 -07001365 if (group.buckets().buckets().isEmpty()) {
1366 log.warn("Can't get output port information from group {} " +
1367 "because there is no bucket in the group.",
1368 group.id().toString());
1369 continue;
1370 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001371 PortNumber pout = readOutPortFromTreatment(
1372 group.buckets().buckets().get(0).treatment());
Yi Tseng78f51f42017-02-02 13:54:58 -08001373 if (portsToRemove.contains(pout)) {
1374 chainsToRemove.add(gkeys);
Charles Chan188ebf52015-12-23 00:15:11 -08001375 }
Charles Chan188ebf52015-12-23 00:15:11 -08001376 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001377 if (chainsToRemove.isEmpty()) {
Charles Chan188ebf52015-12-23 00:15:11 -08001378 log.warn("Could not find appropriate group-chain for removing bucket"
1379 + " for next id {} in dev:{}", nextObjective.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001380 fail(nextObjective, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001381 return;
Charles Chan188ebf52015-12-23 00:15:11 -08001382 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001383 List<GroupBucket> bucketsToRemove = Lists.newArrayList();
1384 //first group key is the one we want to modify
1385 GroupKey modGroupKey = chainsToRemove.get(0).peekFirst();
Saurav Das1a129a02016-11-18 15:21:57 -08001386 Group modGroup = groupService.getGroup(deviceId, modGroupKey);
Yi Tseng78f51f42017-02-02 13:54:58 -08001387 for (Deque<GroupKey> foundChain : chainsToRemove) {
1388 //second group key is the one we wish to remove the reference to
1389 if (foundChain.size() < 2) {
1390 // additional check to make sure second group key exist in
1391 // the chain.
1392 log.warn("Can't find second group key from chain {}",
1393 foundChain);
1394 continue;
Saurav Das1a129a02016-11-18 15:21:57 -08001395 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001396 GroupKey pointedGroupKey = foundChain.stream().collect(Collectors.toList()).get(1);
Yi Tseng78f51f42017-02-02 13:54:58 -08001397 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
1398
1399 if (pointedGroup == null) {
1400 continue;
1401 }
1402
1403 GroupBucket bucket;
1404 if (nextObjective.type() == NextObjective.Type.HASHED) {
1405 bucket = DefaultGroupBucket.createSelectGroupBucket(
1406 DefaultTrafficTreatment.builder()
1407 .group(pointedGroup.id())
1408 .build());
1409 } else {
1410 bucket = DefaultGroupBucket.createAllGroupBucket(
1411 DefaultTrafficTreatment.builder()
1412 .group(pointedGroup.id())
1413 .build());
1414 }
1415
1416 bucketsToRemove.add(bucket);
Saurav Das1a129a02016-11-18 15:21:57 -08001417 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001418
1419 GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove);
1420 List<String> pointedGroupIds; // for debug log
1421 pointedGroupIds = bucketsToRemove.stream()
1422 .map(GroupBucket::treatment)
1423 .map(TrafficTreatment::allInstructions)
1424 .flatMap(List::stream)
1425 .filter(inst -> inst instanceof Instructions.GroupInstruction)
1426 .map(inst -> (Instructions.GroupInstruction) inst)
1427 .map(Instructions.GroupInstruction::groupId)
1428 .map(GroupId::id)
1429 .map(Integer::toHexString)
1430 .map(id -> HEX_PREFIX + id)
1431 .collect(Collectors.toList());
1432
Yi Tseng78f51f42017-02-02 13:54:58 -08001433 log.debug("Removing buckets from group id 0x{} pointing to group id(s) {} "
Saurav Das1a129a02016-11-18 15:21:57 -08001434 + "for next id {} in device {}", Integer.toHexString(modGroup.id().id()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001435 pointedGroupIds, nextObjective.id(), deviceId);
1436 addPendingUpdateNextObjective(modGroupKey, nextObjective);
Saurav Das1a129a02016-11-18 15:21:57 -08001437 groupService.removeBucketsFromGroup(deviceId, modGroupKey,
1438 removeBuckets, modGroupKey,
1439 nextObjective.appId());
Saurav Dasc88d4662017-05-15 15:34:25 -07001440 // update store - synchronize access
1441 synchronized (flowObjectiveStore) {
1442 // get fresh copy of what the store holds
1443 next = flowObjectiveStore.getNextGroup(nextObjective.id());
1444 allActiveKeys = appKryo.deserialize(next.data());
1445 // Note that since we got a new object, and ArrayDeque does not implement
1446 // Object.equals(), we have to check the deque last elems one by one
1447 allActiveKeys.removeIf(active -> chainsToRemove.stream().anyMatch(remove ->
1448 remove.peekLast().equals(active.peekLast())));
1449 // If no buckets in the group, then retain an entry for the
1450 // top level group which still exists.
1451 if (allActiveKeys.isEmpty()) {
1452 ArrayDeque<GroupKey> top = new ArrayDeque<>();
1453 top.add(modGroupKey);
1454 allActiveKeys.add(top);
1455 }
1456 flowObjectiveStore.putNextGroup(nextObjective.id(),
1457 new OfdpaNextGroup(allActiveKeys, nextObjective));
Saurav Das1a129a02016-11-18 15:21:57 -08001458 }
Charles Chan188ebf52015-12-23 00:15:11 -08001459 }
1460
1461 /**
Saurav Das961beb22017-03-29 19:09:17 -07001462 * Removes all groups in multiple possible group-chains that represent the next-obj.
Charles Chan188ebf52015-12-23 00:15:11 -08001463 *
1464 * @param nextObjective the next objective to remove
1465 * @param next the NextGroup that represents the existing group-chain for
1466 * this next objective
1467 */
1468 protected void removeGroup(NextObjective nextObjective, NextGroup next) {
Yi Tsengef19de12017-04-24 11:33:05 -07001469 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Charles Chanfc5c7802016-05-17 13:13:55 -07001470
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001471 List<GroupKey> groupKeys = allActiveKeys.stream()
Charles Chanfc5c7802016-05-17 13:13:55 -07001472 .map(Deque::getFirst).collect(Collectors.toList());
Yi Tsengef19de12017-04-24 11:33:05 -07001473 addPendingRemoveNextObjective(nextObjective, groupKeys);
Charles Chanfc5c7802016-05-17 13:13:55 -07001474
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001475 allActiveKeys.forEach(groupChain -> groupChain.forEach(groupKey ->
Charles Chan188ebf52015-12-23 00:15:11 -08001476 groupService.removeGroup(deviceId, groupKey, nextObjective.appId())));
1477 flowObjectiveStore.removeNextGroup(nextObjective.id());
1478 }
1479
Yi Tsengef19de12017-04-24 11:33:05 -07001480 protected void updatePendingNextObjective(GroupKey groupKey, OfdpaNextGroup nextGrp) {
1481 pendingAddNextObjectives.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07001482 if (val == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001483 val = new CopyOnWriteArrayList<>();
Saurav Das961beb22017-03-29 19:09:17 -07001484 }
Yi Tsengef19de12017-04-24 11:33:05 -07001485 val.add(nextGrp);
Saurav Das961beb22017-03-29 19:09:17 -07001486 return val;
1487 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08001488 }
1489
Yi Tsengef19de12017-04-24 11:33:05 -07001490 protected void updatePendingGroups(GroupKey groupKey, GroupChainElem gce) {
1491 pendingGroups.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07001492 if (val == null) {
1493 val = Sets.newConcurrentHashSet();
1494 }
1495 val.add(gce);
1496 return val;
1497 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08001498 }
1499
Yi Tsengef19de12017-04-24 11:33:05 -07001500 protected void addPendingUpdateNextObjective(GroupKey groupKey,
1501 NextObjective nextObjective) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001502 pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> {
1503 if (nextObjs != null) {
1504 nextObjs.add(nextObjective);
1505 } else {
1506 nextObjs = Sets.newHashSet(nextObjective);
1507 }
1508 return nextObjs;
1509 });
1510 }
1511
Yi Tsengef19de12017-04-24 11:33:05 -07001512 private void processPendingUpdateNextObjs(GroupKey groupKey) {
1513 pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> {
1514 if (nextObjs != null) {
1515
1516 nextObjs.forEach(nextObj -> {
1517 log.debug("Group {} updated, update pending next objective {}.",
1518 groupKey, nextObj);
1519
1520 pass(nextObj);
1521 });
1522 }
1523 return Sets.newHashSet();
1524 });
1525 }
1526
1527 private void processPendingRemoveNextObjs(GroupKey key) {
1528 pendingRemoveNextObjectives.asMap().forEach((nextObjective, groupKeys) -> {
1529 if (groupKeys.isEmpty()) {
1530 pendingRemoveNextObjectives.invalidate(nextObjective);
1531 pass(nextObjective);
1532 } else {
1533 groupKeys.remove(key);
1534 }
1535 });
1536 }
1537
1538 protected int getNextAvailableIndex() {
1539 return (int) nextIndex.incrementAndGet();
1540 }
1541
1542 protected Group retrieveTopLevelGroup(List<Deque<GroupKey>> allActiveKeys,
1543 int nextid) {
1544 GroupKey topLevelGroupKey;
1545 if (!allActiveKeys.isEmpty()) {
1546 topLevelGroupKey = allActiveKeys.get(0).peekFirst();
1547 } else {
1548 log.warn("Could not determine top level group while processing"
1549 + "next:{} in dev:{}", nextid, deviceId);
1550 return null;
1551 }
1552 Group topGroup = groupService.getGroup(deviceId, topLevelGroupKey);
1553 if (topGroup == null) {
1554 log.warn("Could not find top level group while processing "
1555 + "next:{} in dev:{}", nextid, deviceId);
1556 }
1557 return topGroup;
1558 }
1559
1560 protected void processPendingAddGroupsOrNextObjs(GroupKey key, boolean added) {
1561 //first check for group chain
1562 Set<OfdpaGroupHandlerUtility.GroupChainElem> gceSet = pendingGroups.asMap().remove(key);
1563 if (gceSet != null) {
1564 for (GroupChainElem gce : gceSet) {
1565 log.debug("Group service {} group key {} in device {}. "
1566 + "Processing next group in group chain with group id 0x{}",
1567 (added) ? "ADDED" : "processed",
1568 key, deviceId,
1569 Integer.toHexString(gce.groupDescription().givenGroupId()));
1570 processGroupChain(gce);
1571 }
1572 } else {
1573 // otherwise chain complete - check for waiting nextObjectives
1574 List<OfdpaGroupHandlerUtility.OfdpaNextGroup> nextGrpList =
1575 pendingAddNextObjectives.getIfPresent(key);
1576 if (nextGrpList != null) {
1577 pendingAddNextObjectives.invalidate(key);
1578 nextGrpList.forEach(nextGrp -> {
1579 log.debug("Group service {} group key {} in device:{}. "
1580 + "Done implementing next objective: {} <<-->> gid:0x{}",
1581 (added) ? "ADDED" : "processed",
1582 key, deviceId, nextGrp.nextObjective().id(),
1583 Integer.toHexString(groupService.getGroup(deviceId, key)
1584 .givenGroupId()));
1585 pass(nextGrp.nextObjective());
Saurav Dasc88d4662017-05-15 15:34:25 -07001586 updateFlowObjectiveStore(nextGrp.nextObjective().id(), nextGrp);
Yi Tsengef19de12017-04-24 11:33:05 -07001587
1588 // check if addBuckets waiting for this completion
1589 pendingBuckets.compute(nextGrp.nextObjective().id(), (nextId, pendBkts) -> {
1590 if (pendBkts != null) {
1591 pendBkts.forEach(pendBkt -> addBucketToGroup(pendBkt, nextGrp));
1592 }
1593 return null;
1594 });
1595 });
1596 }
1597 }
1598 }
1599
Charles Chan188ebf52015-12-23 00:15:11 -08001600 /**
1601 * Processes next element of a group chain. Assumption is that if this
1602 * group points to another group, the latter has already been created
1603 * and this driver has received notification for it. A second assumption is
1604 * that if there is another group waiting for this group then the appropriate
1605 * stores already have the information to act upon the notification for the
1606 * creation of this group.
1607 * <p>
1608 * The processing of the GroupChainElement depends on the number of groups
1609 * this element is waiting on. For all group types other than SIMPLE, a
1610 * GroupChainElement could be waiting on multiple groups.
1611 *
1612 * @param gce the group chain element to be processed next
1613 */
1614 private void processGroupChain(GroupChainElem gce) {
1615 int waitOnGroups = gce.decrementAndGetGroupsWaitedOn();
1616 if (waitOnGroups != 0) {
1617 log.debug("GCE: {} not ready to be processed", gce);
1618 return;
1619 }
1620 log.debug("GCE: {} ready to be processed", gce);
Yi Tsengef19de12017-04-24 11:33:05 -07001621 if (gce.addBucketToGroup()) {
1622 groupService.addBucketsToGroup(gce.groupDescription().deviceId(),
1623 gce.groupDescription().appCookie(),
1624 gce.groupDescription().buckets(),
1625 gce.groupDescription().appCookie(),
1626 gce.groupDescription().appId());
Charles Chan188ebf52015-12-23 00:15:11 -08001627 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001628 groupService.addGroup(gce.groupDescription());
Charles Chan188ebf52015-12-23 00:15:11 -08001629 }
1630 }
1631
Yi Tsengef19de12017-04-24 11:33:05 -07001632 protected void addPendingRemoveNextObjective(NextObjective nextObjective,
1633 List<GroupKey> groupKeys) {
1634 pendingRemoveNextObjectives.put(nextObjective, groupKeys);
Charles Chan188ebf52015-12-23 00:15:11 -08001635 }
1636
Saurav Dasc88d4662017-05-15 15:34:25 -07001637 private void updateFlowObjectiveStore(Integer nextId, OfdpaNextGroup nextGrp) {
1638 synchronized (flowObjectiveStore) {
1639 // get fresh copy of what the store holds
1640 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1641 if (next == null || nextGrp.nextObjective().op() == Operation.ADD) {
1642 flowObjectiveStore.putNextGroup(nextId, nextGrp);
1643 return;
1644 }
1645 if (nextGrp.nextObjective().op() == Operation.ADD_TO_EXISTING) {
1646 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
1647 // If active keys shows only the top-level group without a chain of groups,
1648 // then it represents an empty group. Update by replacing empty chain.
1649 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1650 allActiveKeys.clear();
1651 }
1652 allActiveKeys.addAll(nextGrp.allKeys());
1653 flowObjectiveStore.putNextGroup(nextId,
1654 new OfdpaNextGroup(allActiveKeys, nextGrp.nextObjective()));
1655 }
1656 }
1657 }
1658
Saurav Das8be4e3a2016-03-11 17:19:07 -08001659 private class InnerGroupListener implements GroupListener {
1660 @Override
1661 public void event(GroupEvent event) {
1662 log.trace("received group event of type {}", event.type());
Charles Chanfc5c7802016-05-17 13:13:55 -07001663 switch (event.type()) {
1664 case GROUP_ADDED:
1665 processPendingAddGroupsOrNextObjs(event.subject().appCookie(), true);
1666 break;
1667 case GROUP_REMOVED:
1668 processPendingRemoveNextObjs(event.subject().appCookie());
1669 break;
Yi Tseng78f51f42017-02-02 13:54:58 -08001670 case GROUP_UPDATED:
1671 processPendingUpdateNextObjs(event.subject().appCookie());
1672 break;
Charles Chanfc5c7802016-05-17 13:13:55 -07001673 default:
1674 break;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001675 }
1676 }
1677 }
Charles Chan188ebf52015-12-23 00:15:11 -08001678}