blob: 5c4e1a881df76293e146c277d2ccccc526cd9af8 [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 {
445 log.warn("Driver does not handle this type of TrafficTreatment"
446 + " 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)) {
1029 duplicateBuckets.add(trafficTreatment);
Yi Tseng47f82dc2017-03-05 22:48:39 -08001030 } else {
1031 nonDuplicateBuckets.add(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001032 }
1033 });
1034
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001035 if (duplicateBuckets.isEmpty()) {
1036 // use the original objective
1037 objectiveToAdd = nextObjective;
1038 } else if (!nonDuplicateBuckets.isEmpty()) {
1039 // only use the non-duplicate buckets if there are any
1040 log.debug("Some buckets {} already exist in next id {}, duplicate "
1041 + "buckets will be ignored.", duplicateBuckets, nextObjective.id());
1042 // new next objective with non duplicate treatments
Yi Tseng47f82dc2017-03-05 22:48:39 -08001043 NextObjective.Builder builder = DefaultNextObjective.builder()
1044 .withType(nextObjective.type())
1045 .withId(nextObjective.id())
1046 .withMeta(nextObjective.meta())
1047 .fromApp(nextObjective.appId());
1048 nonDuplicateBuckets.forEach(builder::addTreatment);
1049
1050 ObjectiveContext context = nextObjective.context().orElse(null);
1051 objectiveToAdd = builder.addToExisting(context);
1052 } else {
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001053 // buckets to add are already there - nothing to do
Saurav Das1547b3f2017-05-05 17:01:08 -07001054 log.debug("buckets already exist {} in next: {} ..ignoring bucket add",
1055 duplicateBuckets, nextObjective.id());
1056 pass(nextObjective);
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001057 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001058 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001059
Saurav Das1a129a02016-11-18 15:21:57 -08001060 if (nextObjective.type() == NextObjective.Type.HASHED) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001061 addBucketToHashGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001062 } else if (nextObjective.type() == NextObjective.Type.BROADCAST) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001063 addBucketToBroadcastGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001064 }
1065 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001066
Saurav Das1a129a02016-11-18 15:21:57 -08001067 private void addBucketToHashGroup(NextObjective nextObjective,
Yi Tseng78f51f42017-02-02 13:54:58 -08001068 List<Deque<GroupKey>> allActiveKeys) {
Charles Chan188ebf52015-12-23 00:15:11 -08001069 // storage for all group keys in the chain of groups created
1070 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
1071 List<GroupInfo> unsentGroups = new ArrayList<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001072 List<GroupBucket> newBuckets;
Charles Chan188ebf52015-12-23 00:15:11 -08001073 createHashBucketChains(nextObjective, allGroupKeys, unsentGroups);
1074
Yi Tseng78f51f42017-02-02 13:54:58 -08001075 // now we can create the buckets to add to the outermost L3 ECMP group
1076 newBuckets = generateNextGroupBuckets(unsentGroups, SELECT);
Charles Chan188ebf52015-12-23 00:15:11 -08001077
Saurav Das1a129a02016-11-18 15:21:57 -08001078 // retrieve the original L3 ECMP group
1079 Group l3ecmpGroup = retrieveTopLevelGroup(allActiveKeys, nextObjective.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001080 if (l3ecmpGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001081 fail(nextObjective, ObjectiveError.GROUPMISSING);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001082 return;
1083 }
Saurav Das1a129a02016-11-18 15:21:57 -08001084 GroupKey l3ecmpGroupKey = l3ecmpGroup.appCookie();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001085 int l3ecmpGroupId = l3ecmpGroup.id().id();
Charles Chan188ebf52015-12-23 00:15:11 -08001086
1087 // Although GroupDescriptions are not necessary for adding buckets to
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001088 // existing groups, we still use one in the GroupChainElem. When the latter is
Charles Chan188ebf52015-12-23 00:15:11 -08001089 // processed, the info will be extracted for the bucketAdd call to groupService
1090 GroupDescription l3ecmpGroupDesc =
Yi Tsengef19de12017-04-24 11:33:05 -07001091 new DefaultGroupDescription(deviceId,
1092 SELECT,
1093 new GroupBuckets(newBuckets),
1094 l3ecmpGroupKey,
1095 l3ecmpGroupId,
1096 nextObjective.appId());
Yi Tseng78f51f42017-02-02 13:54:58 -08001097 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
1098 unsentGroups.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001099 true,
1100 deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001101
Saurav Dasc88d4662017-05-15 15:34:25 -07001102 // update new bucket-chains
1103 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
1104 for (Deque<GroupKey> newBucketChain : allGroupKeys) {
1105 newBucketChain.addFirst(l3ecmpGroupKey);
1106 addedKeys.add(newBucketChain);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001107 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001108 updatePendingNextObjective(l3ecmpGroupKey,
1109 new OfdpaNextGroup(addedKeys, nextObjective));
Yi Tsengef19de12017-04-24 11:33:05 -07001110 log.debug("Adding to L3ECMP: device:{} gid:{} group key:{} nextId:{}",
Charles Chan188ebf52015-12-23 00:15:11 -08001111 deviceId, Integer.toHexString(l3ecmpGroupId),
1112 l3ecmpGroupKey, nextObjective.id());
Yi Tseng78f51f42017-02-02 13:54:58 -08001113
1114 unsentGroups.forEach(groupInfo -> {
1115 // send the innermost group
1116 log.debug("Sending innermost group {} in group chain on device {} ",
Saurav Dasc88d4662017-05-15 15:34:25 -07001117 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
1118 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001119 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3ecmpGce);
1120 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001121 });
Saurav Das1a129a02016-11-18 15:21:57 -08001122 }
Charles Chan188ebf52015-12-23 00:15:11 -08001123
Saurav Das1a129a02016-11-18 15:21:57 -08001124 private void addBucketToBroadcastGroup(NextObjective nextObj,
Yi Tsengef19de12017-04-24 11:33:05 -07001125 List<Deque<GroupKey>> allActiveKeys) {
1126 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001127 if (assignedVlan == null) {
1128 log.warn("VLAN ID required by broadcast next obj is missing. "
1129 + "Aborting add bucket to broadcast group for next:{} in dev:{}",
1130 nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001131 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001132 return;
1133 }
Saurav Das1a129a02016-11-18 15:21:57 -08001134 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Yi Tsengef19de12017-04-24 11:33:05 -07001135 IpPrefix ipDst = readIpDstFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001136 if (ipDst != null) {
1137 if (ipDst.isMulticast()) {
Yi Tsengef19de12017-04-24 11:33:05 -07001138 addBucketToL3MulticastGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001139 } else {
1140 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001141 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001142 }
1143 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001144 addBucketToL2FloodGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001145 }
1146 }
1147
1148 private void addBucketToL2FloodGroup(NextObjective nextObj,
1149 List<Deque<GroupKey>> allActiveKeys,
1150 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001151 VlanId assignedVlan) {
1152 Group l2FloodGroup = retrieveTopLevelGroup(allActiveKeys, nextObj.id());
1153
1154 if (l2FloodGroup == null) {
1155 log.warn("Can't find L2 flood group while adding bucket to it. NextObj = {}",
1156 nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001157 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001158 return;
1159 }
Saurav Das1a129a02016-11-18 15:21:57 -08001160
Yi Tseng78f51f42017-02-02 13:54:58 -08001161 GroupKey l2floodGroupKey = l2FloodGroup.appCookie();
1162 int l2floodGroupId = l2FloodGroup.id().id();
1163 List<GroupBucket> newBuckets = generateNextGroupBuckets(groupInfos, ALL);
1164
1165 GroupDescription l2FloodGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001166 new DefaultGroupDescription(deviceId,
1167 ALL,
1168 new GroupBuckets(newBuckets),
1169 l2floodGroupKey,
1170 l2floodGroupId,
1171 nextObj.appId());
Saurav Das1a129a02016-11-18 15:21:57 -08001172
Yi Tseng78f51f42017-02-02 13:54:58 -08001173 GroupChainElem l2FloodGroupChainElement =
1174 new GroupChainElem(l2FloodGroupDescription,
1175 groupInfos.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001176 true,
1177 deviceId);
Yi Tseng78f51f42017-02-02 13:54:58 -08001178
Yi Tseng78f51f42017-02-02 13:54:58 -08001179
1180 //ensure assignedVlan applies to the chosen group
1181 VlanId floodGroupVlan = extractVlanIdFromGroupId(l2floodGroupId);
1182
1183 if (!floodGroupVlan.equals(assignedVlan)) {
1184 log.warn("VLAN ID {} does not match Flood group {} to which bucket is "
1185 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1186 Integer.toHexString(l2floodGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001187 fail(nextObj, ObjectiveError.BADPARAMS);
Yi Tseng78f51f42017-02-02 13:54:58 -08001188 return;
1189 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001190 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
Yi Tseng78f51f42017-02-02 13:54:58 -08001191 groupInfos.forEach(groupInfo -> {
1192 // update original NextGroup with new bucket-chain
Yi Tseng78f51f42017-02-02 13:54:58 -08001193 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001194 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001195 newBucketChain.addFirst(l2floodGroupKey);
Saurav Dasc88d4662017-05-15 15:34:25 -07001196 addedKeys.add(newBucketChain);
Yi Tseng78f51f42017-02-02 13:54:58 -08001197
Yi Tsengef19de12017-04-24 11:33:05 -07001198 log.debug("Adding to L2FLOOD: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001199 deviceId, Integer.toHexString(l2floodGroupId),
1200 l2floodGroupKey, nextObj.id());
1201 // send the innermost group
1202 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001203 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001204 deviceId);
1205
Yi Tsengef19de12017-04-24 11:33:05 -07001206 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l2FloodGroupChainElement);
Yi Tseng78f51f42017-02-02 13:54:58 -08001207
Yi Tsengef19de12017-04-24 11:33:05 -07001208 DeviceId innerMostGroupDevice = groupInfo.innerMostGroupDesc().deviceId();
1209 GroupKey innerMostGroupKey = groupInfo.innerMostGroupDesc().appCookie();
Yi Tseng78f51f42017-02-02 13:54:58 -08001210 Group existsL2IGroup = groupService.getGroup(innerMostGroupDevice, innerMostGroupKey);
1211
1212 if (existsL2IGroup != null) {
1213 // group already exist
1214 processPendingAddGroupsOrNextObjs(innerMostGroupKey, true);
1215 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001216 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001217 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001218 });
Saurav Dasc88d4662017-05-15 15:34:25 -07001219
1220 updatePendingNextObjective(l2floodGroupKey,
1221 new OfdpaNextGroup(addedKeys, nextObj));
Yi Tseng78f51f42017-02-02 13:54:58 -08001222 }
1223
Saurav Das1a129a02016-11-18 15:21:57 -08001224 private void addBucketToL3MulticastGroup(NextObjective nextObj,
1225 List<Deque<GroupKey>> allActiveKeys,
1226 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001227 VlanId assignedVlan) {
1228 // create the buckets to add to the outermost L3 Multicast group
1229 List<GroupBucket> newBuckets = Lists.newArrayList();
1230 groupInfos.forEach(groupInfo -> {
1231 // Points to L3 interface group if there is one.
1232 // Otherwise points to L2 interface group directly.
Yi Tsengef19de12017-04-24 11:33:05 -07001233 GroupDescription nextGroupDesc = (groupInfo.nextGroupDesc() != null) ?
1234 groupInfo.nextGroupDesc() : groupInfo.innerMostGroupDesc();
1235 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1236 treatmentBuilder.group(new GroupId(nextGroupDesc.givenGroupId()));
1237 GroupBucket newBucket = DefaultGroupBucket.createAllGroupBucket(treatmentBuilder.build());
Yi Tseng78f51f42017-02-02 13:54:58 -08001238 newBuckets.add(newBucket);
1239 });
Saurav Das1a129a02016-11-18 15:21:57 -08001240
1241 // get the group being edited
1242 Group l3mcastGroup = retrieveTopLevelGroup(allActiveKeys, nextObj.id());
1243 if (l3mcastGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001244 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001245 return;
1246 }
1247 GroupKey l3mcastGroupKey = l3mcastGroup.appCookie();
1248 int l3mcastGroupId = l3mcastGroup.id().id();
1249
1250 //ensure assignedVlan applies to the chosen group
Yi Tseng78f51f42017-02-02 13:54:58 -08001251 VlanId expectedVlan = extractVlanIdFromGroupId(l3mcastGroupId);
Saurav Das1a129a02016-11-18 15:21:57 -08001252 if (!expectedVlan.equals(assignedVlan)) {
1253 log.warn("VLAN ID {} does not match L3 Mcast group {} to which bucket is "
1254 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1255 Integer.toHexString(l3mcastGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001256 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001257 }
1258 GroupDescription l3mcastGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001259 new DefaultGroupDescription(deviceId,
1260 ALL,
1261 new GroupBuckets(newBuckets),
1262 l3mcastGroupKey,
1263 l3mcastGroupId,
1264 nextObj.appId());
Saurav Das1a129a02016-11-18 15:21:57 -08001265 GroupChainElem l3mcastGce = new GroupChainElem(l3mcastGroupDescription,
Yi Tsengef19de12017-04-24 11:33:05 -07001266 groupInfos.size(),
1267 true,
1268 deviceId);
Saurav Dasc88d4662017-05-15 15:34:25 -07001269
1270 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
Yi Tseng78f51f42017-02-02 13:54:58 -08001271 groupInfos.forEach(groupInfo -> {
1272 // update original NextGroup with new bucket-chain
1273 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001274 newBucketChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001275 // Add L3 interface group to the chain if there is one.
Yi Tsengef19de12017-04-24 11:33:05 -07001276 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1277 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001278 }
1279 newBucketChain.addFirst(l3mcastGroupKey);
Saurav Dasc88d4662017-05-15 15:34:25 -07001280 addedKeys.add(newBucketChain);
Yi Tseng78f51f42017-02-02 13:54:58 -08001281
Yi Tsengef19de12017-04-24 11:33:05 -07001282 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3mcastGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001283 // Point next group to inner-most group, if any
Yi Tsengef19de12017-04-24 11:33:05 -07001284 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1285 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(),
1286 1,
1287 false,
1288 deviceId);
1289 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001290 }
Yi Tsengef19de12017-04-24 11:33:05 -07001291 log.debug("Adding to L3MCAST: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001292 deviceId, Integer.toHexString(l3mcastGroupId),
1293 l3mcastGroupKey, nextObj.id());
1294 // send the innermost group
1295 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001296 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001297 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001298 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001299
1300 });
1301
Saurav Das1a129a02016-11-18 15:21:57 -08001302 updatePendingNextObjective(l3mcastGroupKey,
Saurav Dasc88d4662017-05-15 15:34:25 -07001303 new OfdpaNextGroup(addedKeys, nextObj));
Charles Chan188ebf52015-12-23 00:15:11 -08001304 }
1305
1306 /**
1307 * Removes the bucket in the top level group of a possible group-chain. Does
Saurav Das1a129a02016-11-18 15:21:57 -08001308 * not remove the groups in the group-chain pointed to by this bucket, as they
Charles Chan188ebf52015-12-23 00:15:11 -08001309 * may be in use (referenced by other groups) elsewhere.
1310 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001311 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -08001312 * @param next the representation of the existing group-chain for this next objective
1313 */
1314 protected void removeBucketFromGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001315 if (nextObjective.type() != NextObjective.Type.HASHED &&
1316 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001317 log.warn("RemoveBuckets not applied to nextType:{} in dev:{} for next:{}",
1318 nextObjective.type(), deviceId, nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -07001319 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001320 return;
1321 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001322 Set<PortNumber> portsToRemove = Sets.newHashSet();
Charles Chan188ebf52015-12-23 00:15:11 -08001323 Collection<TrafficTreatment> treatments = nextObjective.next();
Yi Tseng78f51f42017-02-02 13:54:58 -08001324 for (TrafficTreatment treatment : treatments) {
1325 // find the bucket to remove by noting the outport, and figuring out the
1326 // top-level group in the group-chain that indirectly references the port
1327 PortNumber portToRemove = readOutPortFromTreatment(treatment);
1328 if (portToRemove == null) {
1329 log.warn("treatment {} of next objective {} has no outport.. cannot remove bucket"
1330 + "from group in dev: {}", treatment, nextObjective.id(), deviceId);
1331 } else {
1332 portsToRemove.add(portToRemove);
1333 }
1334 }
1335
1336 if (portsToRemove.isEmpty()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001337 log.warn("next objective {} has no outport.. cannot remove bucket"
Yi Tseng78f51f42017-02-02 13:54:58 -08001338 + "from group in dev: {}", nextObjective.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001339 fail(nextObjective, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -08001340 }
1341
Yi Tsengef19de12017-04-24 11:33:05 -07001342 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Yi Tseng78f51f42017-02-02 13:54:58 -08001343 List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001344 for (Deque<GroupKey> gkeys : allActiveKeys) {
1345 // last group in group chain should have a single bucket pointing to port
Charles Chan188ebf52015-12-23 00:15:11 -08001346 GroupKey groupWithPort = gkeys.peekLast();
1347 Group group = groupService.getGroup(deviceId, groupWithPort);
1348 if (group == null) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001349 log.warn("Inconsistent group chain found when removing bucket"
1350 + "for next:{} in dev:{}", nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001351 continue;
1352 }
Yi Tseng91cf2d42017-04-10 17:05:37 -07001353 if (group.buckets().buckets().isEmpty()) {
1354 log.warn("Can't get output port information from group {} " +
1355 "because there is no bucket in the group.",
1356 group.id().toString());
1357 continue;
1358 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001359 PortNumber pout = readOutPortFromTreatment(
1360 group.buckets().buckets().get(0).treatment());
Yi Tseng78f51f42017-02-02 13:54:58 -08001361 if (portsToRemove.contains(pout)) {
1362 chainsToRemove.add(gkeys);
Charles Chan188ebf52015-12-23 00:15:11 -08001363 }
Charles Chan188ebf52015-12-23 00:15:11 -08001364 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001365 if (chainsToRemove.isEmpty()) {
Charles Chan188ebf52015-12-23 00:15:11 -08001366 log.warn("Could not find appropriate group-chain for removing bucket"
1367 + " for next id {} in dev:{}", nextObjective.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001368 fail(nextObjective, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001369 return;
Charles Chan188ebf52015-12-23 00:15:11 -08001370 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001371 List<GroupBucket> bucketsToRemove = Lists.newArrayList();
1372 //first group key is the one we want to modify
1373 GroupKey modGroupKey = chainsToRemove.get(0).peekFirst();
Saurav Das1a129a02016-11-18 15:21:57 -08001374 Group modGroup = groupService.getGroup(deviceId, modGroupKey);
Yi Tseng78f51f42017-02-02 13:54:58 -08001375 for (Deque<GroupKey> foundChain : chainsToRemove) {
1376 //second group key is the one we wish to remove the reference to
1377 if (foundChain.size() < 2) {
1378 // additional check to make sure second group key exist in
1379 // the chain.
1380 log.warn("Can't find second group key from chain {}",
1381 foundChain);
1382 continue;
Saurav Das1a129a02016-11-18 15:21:57 -08001383 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001384 GroupKey pointedGroupKey = foundChain.stream().collect(Collectors.toList()).get(1);
Yi Tseng78f51f42017-02-02 13:54:58 -08001385 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
1386
1387 if (pointedGroup == null) {
1388 continue;
1389 }
1390
1391 GroupBucket bucket;
1392 if (nextObjective.type() == NextObjective.Type.HASHED) {
1393 bucket = DefaultGroupBucket.createSelectGroupBucket(
1394 DefaultTrafficTreatment.builder()
1395 .group(pointedGroup.id())
1396 .build());
1397 } else {
1398 bucket = DefaultGroupBucket.createAllGroupBucket(
1399 DefaultTrafficTreatment.builder()
1400 .group(pointedGroup.id())
1401 .build());
1402 }
1403
1404 bucketsToRemove.add(bucket);
Saurav Das1a129a02016-11-18 15:21:57 -08001405 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001406
1407 GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove);
1408 List<String> pointedGroupIds; // for debug log
1409 pointedGroupIds = bucketsToRemove.stream()
1410 .map(GroupBucket::treatment)
1411 .map(TrafficTreatment::allInstructions)
1412 .flatMap(List::stream)
1413 .filter(inst -> inst instanceof Instructions.GroupInstruction)
1414 .map(inst -> (Instructions.GroupInstruction) inst)
1415 .map(Instructions.GroupInstruction::groupId)
1416 .map(GroupId::id)
1417 .map(Integer::toHexString)
1418 .map(id -> HEX_PREFIX + id)
1419 .collect(Collectors.toList());
1420
Yi Tseng78f51f42017-02-02 13:54:58 -08001421 log.debug("Removing buckets from group id 0x{} pointing to group id(s) {} "
Saurav Das1a129a02016-11-18 15:21:57 -08001422 + "for next id {} in device {}", Integer.toHexString(modGroup.id().id()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001423 pointedGroupIds, nextObjective.id(), deviceId);
1424 addPendingUpdateNextObjective(modGroupKey, nextObjective);
Saurav Das1a129a02016-11-18 15:21:57 -08001425 groupService.removeBucketsFromGroup(deviceId, modGroupKey,
1426 removeBuckets, modGroupKey,
1427 nextObjective.appId());
Saurav Dasc88d4662017-05-15 15:34:25 -07001428 // update store - synchronize access
1429 synchronized (flowObjectiveStore) {
1430 // get fresh copy of what the store holds
1431 next = flowObjectiveStore.getNextGroup(nextObjective.id());
1432 allActiveKeys = appKryo.deserialize(next.data());
1433 // Note that since we got a new object, and ArrayDeque does not implement
1434 // Object.equals(), we have to check the deque last elems one by one
1435 allActiveKeys.removeIf(active -> chainsToRemove.stream().anyMatch(remove ->
1436 remove.peekLast().equals(active.peekLast())));
1437 // If no buckets in the group, then retain an entry for the
1438 // top level group which still exists.
1439 if (allActiveKeys.isEmpty()) {
1440 ArrayDeque<GroupKey> top = new ArrayDeque<>();
1441 top.add(modGroupKey);
1442 allActiveKeys.add(top);
1443 }
1444 flowObjectiveStore.putNextGroup(nextObjective.id(),
1445 new OfdpaNextGroup(allActiveKeys, nextObjective));
Saurav Das1a129a02016-11-18 15:21:57 -08001446 }
Charles Chan188ebf52015-12-23 00:15:11 -08001447 }
1448
1449 /**
Saurav Das961beb22017-03-29 19:09:17 -07001450 * Removes all groups in multiple possible group-chains that represent the next-obj.
Charles Chan188ebf52015-12-23 00:15:11 -08001451 *
1452 * @param nextObjective the next objective to remove
1453 * @param next the NextGroup that represents the existing group-chain for
1454 * this next objective
1455 */
1456 protected void removeGroup(NextObjective nextObjective, NextGroup next) {
Yi Tsengef19de12017-04-24 11:33:05 -07001457 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Charles Chanfc5c7802016-05-17 13:13:55 -07001458
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001459 List<GroupKey> groupKeys = allActiveKeys.stream()
Charles Chanfc5c7802016-05-17 13:13:55 -07001460 .map(Deque::getFirst).collect(Collectors.toList());
Yi Tsengef19de12017-04-24 11:33:05 -07001461 addPendingRemoveNextObjective(nextObjective, groupKeys);
Charles Chanfc5c7802016-05-17 13:13:55 -07001462
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001463 allActiveKeys.forEach(groupChain -> groupChain.forEach(groupKey ->
Charles Chan188ebf52015-12-23 00:15:11 -08001464 groupService.removeGroup(deviceId, groupKey, nextObjective.appId())));
1465 flowObjectiveStore.removeNextGroup(nextObjective.id());
1466 }
1467
Yi Tsengef19de12017-04-24 11:33:05 -07001468 protected void updatePendingNextObjective(GroupKey groupKey, OfdpaNextGroup nextGrp) {
1469 pendingAddNextObjectives.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07001470 if (val == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001471 val = new CopyOnWriteArrayList<>();
Saurav Das961beb22017-03-29 19:09:17 -07001472 }
Yi Tsengef19de12017-04-24 11:33:05 -07001473 val.add(nextGrp);
Saurav Das961beb22017-03-29 19:09:17 -07001474 return val;
1475 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08001476 }
1477
Yi Tsengef19de12017-04-24 11:33:05 -07001478 protected void updatePendingGroups(GroupKey groupKey, GroupChainElem gce) {
1479 pendingGroups.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07001480 if (val == null) {
1481 val = Sets.newConcurrentHashSet();
1482 }
1483 val.add(gce);
1484 return val;
1485 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08001486 }
1487
Yi Tsengef19de12017-04-24 11:33:05 -07001488 protected void addPendingUpdateNextObjective(GroupKey groupKey,
1489 NextObjective nextObjective) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001490 pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> {
1491 if (nextObjs != null) {
1492 nextObjs.add(nextObjective);
1493 } else {
1494 nextObjs = Sets.newHashSet(nextObjective);
1495 }
1496 return nextObjs;
1497 });
1498 }
1499
Yi Tsengef19de12017-04-24 11:33:05 -07001500 private void processPendingUpdateNextObjs(GroupKey groupKey) {
1501 pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> {
1502 if (nextObjs != null) {
1503
1504 nextObjs.forEach(nextObj -> {
1505 log.debug("Group {} updated, update pending next objective {}.",
1506 groupKey, nextObj);
1507
1508 pass(nextObj);
1509 });
1510 }
1511 return Sets.newHashSet();
1512 });
1513 }
1514
1515 private void processPendingRemoveNextObjs(GroupKey key) {
1516 pendingRemoveNextObjectives.asMap().forEach((nextObjective, groupKeys) -> {
1517 if (groupKeys.isEmpty()) {
1518 pendingRemoveNextObjectives.invalidate(nextObjective);
1519 pass(nextObjective);
1520 } else {
1521 groupKeys.remove(key);
1522 }
1523 });
1524 }
1525
1526 protected int getNextAvailableIndex() {
1527 return (int) nextIndex.incrementAndGet();
1528 }
1529
1530 protected Group retrieveTopLevelGroup(List<Deque<GroupKey>> allActiveKeys,
1531 int nextid) {
1532 GroupKey topLevelGroupKey;
1533 if (!allActiveKeys.isEmpty()) {
1534 topLevelGroupKey = allActiveKeys.get(0).peekFirst();
1535 } else {
1536 log.warn("Could not determine top level group while processing"
1537 + "next:{} in dev:{}", nextid, deviceId);
1538 return null;
1539 }
1540 Group topGroup = groupService.getGroup(deviceId, topLevelGroupKey);
1541 if (topGroup == null) {
1542 log.warn("Could not find top level group while processing "
1543 + "next:{} in dev:{}", nextid, deviceId);
1544 }
1545 return topGroup;
1546 }
1547
1548 protected void processPendingAddGroupsOrNextObjs(GroupKey key, boolean added) {
1549 //first check for group chain
1550 Set<OfdpaGroupHandlerUtility.GroupChainElem> gceSet = pendingGroups.asMap().remove(key);
1551 if (gceSet != null) {
1552 for (GroupChainElem gce : gceSet) {
1553 log.debug("Group service {} group key {} in device {}. "
1554 + "Processing next group in group chain with group id 0x{}",
1555 (added) ? "ADDED" : "processed",
1556 key, deviceId,
1557 Integer.toHexString(gce.groupDescription().givenGroupId()));
1558 processGroupChain(gce);
1559 }
1560 } else {
1561 // otherwise chain complete - check for waiting nextObjectives
1562 List<OfdpaGroupHandlerUtility.OfdpaNextGroup> nextGrpList =
1563 pendingAddNextObjectives.getIfPresent(key);
1564 if (nextGrpList != null) {
1565 pendingAddNextObjectives.invalidate(key);
1566 nextGrpList.forEach(nextGrp -> {
1567 log.debug("Group service {} group key {} in device:{}. "
1568 + "Done implementing next objective: {} <<-->> gid:0x{}",
1569 (added) ? "ADDED" : "processed",
1570 key, deviceId, nextGrp.nextObjective().id(),
1571 Integer.toHexString(groupService.getGroup(deviceId, key)
1572 .givenGroupId()));
1573 pass(nextGrp.nextObjective());
Saurav Dasc88d4662017-05-15 15:34:25 -07001574 updateFlowObjectiveStore(nextGrp.nextObjective().id(), nextGrp);
Yi Tsengef19de12017-04-24 11:33:05 -07001575
1576 // check if addBuckets waiting for this completion
1577 pendingBuckets.compute(nextGrp.nextObjective().id(), (nextId, pendBkts) -> {
1578 if (pendBkts != null) {
1579 pendBkts.forEach(pendBkt -> addBucketToGroup(pendBkt, nextGrp));
1580 }
1581 return null;
1582 });
1583 });
1584 }
1585 }
1586 }
1587
Charles Chan188ebf52015-12-23 00:15:11 -08001588 /**
1589 * Processes next element of a group chain. Assumption is that if this
1590 * group points to another group, the latter has already been created
1591 * and this driver has received notification for it. A second assumption is
1592 * that if there is another group waiting for this group then the appropriate
1593 * stores already have the information to act upon the notification for the
1594 * creation of this group.
1595 * <p>
1596 * The processing of the GroupChainElement depends on the number of groups
1597 * this element is waiting on. For all group types other than SIMPLE, a
1598 * GroupChainElement could be waiting on multiple groups.
1599 *
1600 * @param gce the group chain element to be processed next
1601 */
1602 private void processGroupChain(GroupChainElem gce) {
1603 int waitOnGroups = gce.decrementAndGetGroupsWaitedOn();
1604 if (waitOnGroups != 0) {
1605 log.debug("GCE: {} not ready to be processed", gce);
1606 return;
1607 }
1608 log.debug("GCE: {} ready to be processed", gce);
Yi Tsengef19de12017-04-24 11:33:05 -07001609 if (gce.addBucketToGroup()) {
1610 groupService.addBucketsToGroup(gce.groupDescription().deviceId(),
1611 gce.groupDescription().appCookie(),
1612 gce.groupDescription().buckets(),
1613 gce.groupDescription().appCookie(),
1614 gce.groupDescription().appId());
Charles Chan188ebf52015-12-23 00:15:11 -08001615 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001616 groupService.addGroup(gce.groupDescription());
Charles Chan188ebf52015-12-23 00:15:11 -08001617 }
1618 }
1619
Yi Tsengef19de12017-04-24 11:33:05 -07001620 protected void addPendingRemoveNextObjective(NextObjective nextObjective,
1621 List<GroupKey> groupKeys) {
1622 pendingRemoveNextObjectives.put(nextObjective, groupKeys);
Charles Chan188ebf52015-12-23 00:15:11 -08001623 }
1624
Saurav Dasc88d4662017-05-15 15:34:25 -07001625 private void updateFlowObjectiveStore(Integer nextId, OfdpaNextGroup nextGrp) {
1626 synchronized (flowObjectiveStore) {
1627 // get fresh copy of what the store holds
1628 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1629 if (next == null || nextGrp.nextObjective().op() == Operation.ADD) {
1630 flowObjectiveStore.putNextGroup(nextId, nextGrp);
1631 return;
1632 }
1633 if (nextGrp.nextObjective().op() == Operation.ADD_TO_EXISTING) {
1634 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
1635 // If active keys shows only the top-level group without a chain of groups,
1636 // then it represents an empty group. Update by replacing empty chain.
1637 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1638 allActiveKeys.clear();
1639 }
1640 allActiveKeys.addAll(nextGrp.allKeys());
1641 flowObjectiveStore.putNextGroup(nextId,
1642 new OfdpaNextGroup(allActiveKeys, nextGrp.nextObjective()));
1643 }
1644 }
1645 }
1646
Saurav Das8be4e3a2016-03-11 17:19:07 -08001647 private class InnerGroupListener implements GroupListener {
1648 @Override
1649 public void event(GroupEvent event) {
1650 log.trace("received group event of type {}", event.type());
Charles Chanfc5c7802016-05-17 13:13:55 -07001651 switch (event.type()) {
1652 case GROUP_ADDED:
1653 processPendingAddGroupsOrNextObjs(event.subject().appCookie(), true);
1654 break;
1655 case GROUP_REMOVED:
1656 processPendingRemoveNextObjs(event.subject().appCookie());
1657 break;
Yi Tseng78f51f42017-02-02 13:54:58 -08001658 case GROUP_UPDATED:
1659 processPendingUpdateNextObjs(event.subject().appCookie());
1660 break;
Charles Chanfc5c7802016-05-17 13:13:55 -07001661 default:
1662 break;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001663 }
1664 }
1665 }
Charles Chan188ebf52015-12-23 00:15:11 -08001666}