blob: 4f16cff0de9b9c3585fbc62e13cc166272e37bf9 [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;
Saurav Dasceccf242017-08-03 18:30:35 -070069import java.util.Arrays;
Charles Chan188ebf52015-12-23 00:15:11 -080070import java.util.Collection;
71import java.util.Collections;
72import java.util.Deque;
73import java.util.List;
Charles Chand0fd5dc2016-02-16 23:14:49 -080074import java.util.Objects;
Charles Chan188ebf52015-12-23 00:15:11 -080075import java.util.Set;
76import java.util.concurrent.ConcurrentHashMap;
77import java.util.concurrent.CopyOnWriteArrayList;
78import java.util.concurrent.Executors;
79import java.util.concurrent.ScheduledExecutorService;
80import java.util.concurrent.TimeUnit;
Charles Chan188ebf52015-12-23 00:15:11 -080081import java.util.stream.Collectors;
82
83import static org.onlab.util.Tools.groupedThreads;
Yi Tsengef19de12017-04-24 11:33:05 -070084import static org.onosproject.driver.pipeline.ofdpa.Ofdpa2Pipeline.*;
85import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
Pier Ventre42287df2016-11-09 14:17:26 -080086import static org.onosproject.net.flow.criteria.Criterion.Type.TUNNEL_ID;
Pier Ventre140a8942016-11-02 07:26:38 -070087import static org.onosproject.net.flow.criteria.Criterion.Type.VLAN_VID;
Yi Tseng78f51f42017-02-02 13:54:58 -080088import static org.onosproject.net.group.GroupDescription.Type.ALL;
89import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Charles Chan188ebf52015-12-23 00:15:11 -080090import static org.slf4j.LoggerFactory.getLogger;
91
92/**
Saurav Das961beb22017-03-29 19:09:17 -070093 * Group handler that emulates Broadcom OF-DPA TTP.
Charles Chan188ebf52015-12-23 00:15:11 -080094 */
Charles Chan361154b2016-03-24 10:23:39 -070095public class Ofdpa2GroupHandler {
Yi Tsengef19de12017-04-24 11:33:05 -070096 protected final Logger log = getLogger(getClass());
Charles Chane849c192016-01-11 18:28:54 -080097
Yi Tsengef19de12017-04-24 11:33:05 -070098 // Services, Stores
Charles Chan188ebf52015-12-23 00:15:11 -080099 protected GroupService groupService;
Saurav Das8be4e3a2016-03-11 17:19:07 -0800100 protected StorageService storageService;
Yi Tsengef19de12017-04-24 11:33:05 -0700101 protected FlowObjectiveStore flowObjectiveStore;
Charles Chan188ebf52015-12-23 00:15:11 -0800102
103 // index number for group creation
Saurav Das8be4e3a2016-03-11 17:19:07 -0800104 private AtomicCounter nextIndex;
Charles Chan188ebf52015-12-23 00:15:11 -0800105
Yi Tsengef19de12017-04-24 11:33:05 -0700106 protected DeviceId deviceId;
107 private Cache<GroupKey, List<OfdpaGroupHandlerUtility.OfdpaNextGroup>> pendingAddNextObjectives;
108 private Cache<NextObjective, List<GroupKey>> pendingRemoveNextObjectives;
109 private Cache<GroupKey, Set<OfdpaGroupHandlerUtility.GroupChainElem>> pendingGroups;
110 private ConcurrentHashMap<GroupKey, Set<NextObjective>> pendingUpdateNextObjectives;
111
Yi Tseng47f82dc2017-03-05 22:48:39 -0800112 // local store for pending bucketAdds - by design there can be multiple
Charles Chan188ebf52015-12-23 00:15:11 -0800113 // pending bucket for a group
Yi Tseng47f82dc2017-03-05 22:48:39 -0800114 protected ConcurrentHashMap<Integer, Set<NextObjective>> pendingBuckets =
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700115 new ConcurrentHashMap<>();
Charles Chan188ebf52015-12-23 00:15:11 -0800116
Yi Tsengef19de12017-04-24 11:33:05 -0700117 private ScheduledExecutorService groupCheckerExecutor =
118 Executors.newScheduledThreadPool(2, groupedThreads("onos/pipeliner", "ofdpa-%d", log));
119
120 public Cache<GroupKey, List<OfdpaNextGroup>> pendingAddNextObjectives() {
121 return pendingAddNextObjectives;
122 }
123
124 public Cache<GroupKey, Set<GroupChainElem>> pendingGroups() {
125 return pendingGroups;
126 }
127
Charles Chan40132b32017-01-22 00:19:37 -0800128 /**
129 * Determines whether this pipeline support copy ttl instructions or not.
130 *
131 * @return true if copy ttl instructions are supported
132 */
133 protected boolean supportCopyTtl() {
134 return true;
135 }
136
137 /**
138 * Determines whether this pipeline support set mpls bos instruction or not.
139 *
140 * @return true if set mpls bos instruction is supported
141 */
142 protected boolean supportSetMplsBos() {
143 return true;
144 }
145
Charles Chan0f43e472017-02-14 14:00:16 -0800146 /**
147 * Determines whether this pipeline requires popping VLAN before pushing MPLS.
148 * <p>
149 * If required, pop vlan before push mpls and add an arbitrary vlan back afterward.
150 * MPLS interface group will substitute the arbitrary VLAN with expected VLAN later on.
151 *
152 * @return true if this pipeline requires popping VLAN before pushing MPLS
153 */
154 protected boolean requireVlanPopBeforeMplsPush() {
155 return false;
156 }
157
Charles Chan188ebf52015-12-23 00:15:11 -0800158 protected void init(DeviceId deviceId, PipelinerContext context) {
Yi Tsengef19de12017-04-24 11:33:05 -0700159 ServiceDirectory serviceDirectory = context.directory();
Charles Chan188ebf52015-12-23 00:15:11 -0800160 this.deviceId = deviceId;
161 this.flowObjectiveStore = context.store();
Charles Chan188ebf52015-12-23 00:15:11 -0800162 this.groupService = serviceDirectory.get(GroupService.class);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800163 this.storageService = serviceDirectory.get(StorageService.class);
Madan Jampanid5714e02016-04-19 14:15:20 -0700164 this.nextIndex = storageService.getAtomicCounter("group-id-index-counter");
Charles Chan188ebf52015-12-23 00:15:11 -0800165
Charles Chanfc5c7802016-05-17 13:13:55 -0700166 pendingAddNextObjectives = CacheBuilder.newBuilder()
Charles Chan188ebf52015-12-23 00:15:11 -0800167 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700168 .removalListener((RemovalNotification<GroupKey, List<OfdpaNextGroup>> notification) -> {
169 if (notification.getCause() == RemovalCause.EXPIRED &&
170 Objects.nonNull(notification.getValue())) {
171 notification.getValue()
172 .forEach(ofdpaNextGrp ->
173 fail(ofdpaNextGrp.nextObjective(),
174 ObjectiveError.GROUPINSTALLATIONFAILED));
Charles Chanfc5c7802016-05-17 13:13:55 -0700175 }
176 }).build();
Charles Chan188ebf52015-12-23 00:15:11 -0800177
Charles Chanfc5c7802016-05-17 13:13:55 -0700178 pendingRemoveNextObjectives = CacheBuilder.newBuilder()
179 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700180 .removalListener((RemovalNotification<NextObjective, List<GroupKey>> notification) -> {
Charles Chanfc5c7802016-05-17 13:13:55 -0700181 if (notification.getCause() == RemovalCause.EXPIRED) {
Yi Tsengef19de12017-04-24 11:33:05 -0700182 fail(notification.getKey(),
183 ObjectiveError.GROUPREMOVALFAILED);
Charles Chan188ebf52015-12-23 00:15:11 -0800184 }
185 }).build();
Saurav Das961beb22017-03-29 19:09:17 -0700186 pendingGroups = CacheBuilder.newBuilder()
187 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700188 .removalListener((RemovalNotification<GroupKey, Set<GroupChainElem>> notification) -> {
Saurav Das961beb22017-03-29 19:09:17 -0700189 if (notification.getCause() == RemovalCause.EXPIRED) {
190 log.error("Unable to install group with key {} and pending GCEs: {}",
191 notification.getKey(), notification.getValue());
192 }
193 }).build();
Yi Tseng78f51f42017-02-02 13:54:58 -0800194 pendingUpdateNextObjectives = new ConcurrentHashMap<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700195 GroupChecker groupChecker = new GroupChecker(this);
196 groupCheckerExecutor.scheduleAtFixedRate(groupChecker, 0, 500, TimeUnit.MILLISECONDS);
Charles Chan188ebf52015-12-23 00:15:11 -0800197 groupService.addListener(new InnerGroupListener());
198 }
199
Saurav Das8be4e3a2016-03-11 17:19:07 -0800200 //////////////////////////////////////
201 // Group Creation
202 //////////////////////////////////////
203
Yi Tsengef19de12017-04-24 11:33:05 -0700204 /**
205 * Adds a list of group chain by given NextObjective.
206 *
207 * @param nextObjective the NextObjective
208 */
Charles Chan188ebf52015-12-23 00:15:11 -0800209 protected void addGroup(NextObjective nextObjective) {
210 switch (nextObjective.type()) {
211 case SIMPLE:
212 Collection<TrafficTreatment> treatments = nextObjective.next();
213 if (treatments.size() != 1) {
214 log.error("Next Objectives of type Simple should only have a "
215 + "single Traffic Treatment. Next Objective Id:{}",
216 nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -0700217 fail(nextObjective, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800218 return;
219 }
220 processSimpleNextObjective(nextObjective);
221 break;
222 case BROADCAST:
223 processBroadcastNextObjective(nextObjective);
224 break;
225 case HASHED:
Pier Ventre140a8942016-11-02 07:26:38 -0700226 if (!verifyHashedNextObjective(nextObjective)) {
227 log.error("Next Objectives of type hashed not supported. Next Objective Id:{}",
228 nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -0700229 fail(nextObjective, ObjectiveError.BADPARAMS);
Pier Ventre140a8942016-11-02 07:26:38 -0700230 return;
231 }
Charles Chan188ebf52015-12-23 00:15:11 -0800232 processHashedNextObjective(nextObjective);
233 break;
234 case FAILOVER:
Yi Tsengef19de12017-04-24 11:33:05 -0700235 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -0800236 log.warn("Unsupported next objective type {}", nextObjective.type());
237 break;
238 default:
Yi Tsengef19de12017-04-24 11:33:05 -0700239 fail(nextObjective, ObjectiveError.UNKNOWN);
Charles Chan188ebf52015-12-23 00:15:11 -0800240 log.warn("Unknown next objective type {}", nextObjective.type());
241 }
242 }
243
244 /**
245 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
246 * a chain of groups. The simple Next Objective passed
247 * in by the application has to be broken up into a group chain
248 * comprising of an L3 Unicast Group that points to an L2 Interface
249 * Group which in-turn points to an output port. In some cases, the simple
250 * next Objective can just be an L2 interface without the need for chaining.
251 *
252 * @param nextObj the nextObjective of type SIMPLE
253 */
254 private void processSimpleNextObjective(NextObjective nextObj) {
255 TrafficTreatment treatment = nextObj.next().iterator().next();
256 // determine if plain L2 or L3->L2
257 boolean plainL2 = true;
258 for (Instruction ins : treatment.allInstructions()) {
259 if (ins.type() == Instruction.Type.L2MODIFICATION) {
260 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
261 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_DST ||
262 l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_SRC) {
263 plainL2 = false;
264 break;
265 }
266 }
267 }
268
269 if (plainL2) {
270 createL2InterfaceGroup(nextObj);
271 return;
272 }
273
Pier Ventre140a8942016-11-02 07:26:38 -0700274 boolean isMpls = false;
Pier Ventre42287df2016-11-09 14:17:26 -0800275 // In order to understand if it is a pseudo wire related
276 // next objective we look for the tunnel id in the meta.
277 boolean isPw = false;
Pier Ventre140a8942016-11-02 07:26:38 -0700278 if (nextObj.meta() != null) {
279 isMpls = isNotMplsBos(nextObj.meta());
Pier Ventre42287df2016-11-09 14:17:26 -0800280
281 TunnelIdCriterion tunnelIdCriterion = (TunnelIdCriterion) nextObj
282 .meta()
283 .getCriterion(TUNNEL_ID);
284 if (tunnelIdCriterion != null) {
285 isPw = true;
286 }
287
Pier Ventre140a8942016-11-02 07:26:38 -0700288 }
289
Pier Ventre42287df2016-11-09 14:17:26 -0800290 if (!isPw) {
291 // break up simple next objective to GroupChain objects
292 GroupInfo groupInfo = createL2L3Chain(treatment, nextObj.id(),
293 nextObj.appId(), isMpls,
294 nextObj.meta());
295 if (groupInfo == null) {
296 log.error("Could not process nextObj={} in dev:{}", nextObj.id(), deviceId);
297 return;
298 }
299 // create object for local and distributed storage
300 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700301 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
302 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Pier Ventre42287df2016-11-09 14:17:26 -0800303 OfdpaNextGroup ofdpaGrp =
304 new OfdpaNextGroup(Collections.singletonList(gkeyChain), nextObj);
305
306 // store l3groupkey with the ofdpaNextGroup for the nextObjective that depends on it
Yi Tsengef19de12017-04-24 11:33:05 -0700307 updatePendingNextObjective(groupInfo.nextGroupDesc().appCookie(), ofdpaGrp);
Pier Ventre42287df2016-11-09 14:17:26 -0800308
309 // now we are ready to send the l2 groupDescription (inner), as all the stores
310 // that will get async replies have been updated. By waiting to update
311 // the stores, we prevent nasty race conditions.
Yi Tsengef19de12017-04-24 11:33:05 -0700312 groupService.addGroup(groupInfo.innerMostGroupDesc());
Pier Ventre42287df2016-11-09 14:17:26 -0800313 } else {
314 // We handle the pseudo wire with a different a procedure.
315 // This procedure is meant to handle both initiation and
316 // termination of the pseudo wire.
317 processPwNextObjective(nextObj);
Charles Chan188ebf52015-12-23 00:15:11 -0800318 }
Charles Chan188ebf52015-12-23 00:15:11 -0800319 }
320
Charles Chan188ebf52015-12-23 00:15:11 -0800321 /**
322 * Creates a simple L2 Interface Group.
323 *
324 * @param nextObj the next Objective
325 */
326 private void createL2InterfaceGroup(NextObjective nextObj) {
Yi Tsengef19de12017-04-24 11:33:05 -0700327 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700328 if (assignedVlan == null) {
329 log.warn("VLAN ID required by simple next obj is missing. Abort.");
Yi Tsengef19de12017-04-24 11:33:05 -0700330 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800331 return;
332 }
333
Charles Chan5b9df8d2016-03-28 22:21:40 -0700334 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Charles Chan188ebf52015-12-23 00:15:11 -0800335
Charles Chan5b9df8d2016-03-28 22:21:40 -0700336 // There is only one L2 interface group in this case
Yi Tsengef19de12017-04-24 11:33:05 -0700337 GroupDescription l2InterfaceGroupDesc = groupInfos.get(0).innerMostGroupDesc();
Charles Chan188ebf52015-12-23 00:15:11 -0800338
Charles Chan5b9df8d2016-03-28 22:21:40 -0700339 // Put all dependency information into allGroupKeys
340 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
341 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
342 gkeyChain.addFirst(l2InterfaceGroupDesc.appCookie());
343 allGroupKeys.add(gkeyChain);
Charles Chan188ebf52015-12-23 00:15:11 -0800344
Charles Chan5b9df8d2016-03-28 22:21:40 -0700345 // Point the next objective to this group
346 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
347 updatePendingNextObjective(l2InterfaceGroupDesc.appCookie(), ofdpaGrp);
348
349 // Start installing the inner-most group
350 groupService.addGroup(l2InterfaceGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800351 }
352
353 /**
354 * Creates one of two possible group-chains from the treatment
355 * passed in. Depending on the MPLS boolean, this method either creates
Charles Chan425854b2016-04-11 15:32:12 -0700356 * an L3Unicast Group --&gt; L2Interface Group, if mpls is false;
357 * or MPLSInterface Group --&gt; L2Interface Group, if mpls is true;
Charles Chan188ebf52015-12-23 00:15:11 -0800358 * The returned 'inner' group description is always the L2 Interface group.
359 *
360 * @param treatment that needs to be broken up to create the group chain
361 * @param nextId of the next objective that needs this group chain
362 * @param appId of the application that sent this next objective
363 * @param mpls determines if L3Unicast or MPLSInterface group is created
364 * @param meta metadata passed in by the application as part of the nextObjective
365 * @return GroupInfo containing the GroupDescription of the
366 * L2Interface group(inner) and the GroupDescription of the (outer)
367 * L3Unicast/MPLSInterface group. May return null if there is an
368 * error in processing the chain
369 */
Charles Chan425854b2016-04-11 15:32:12 -0700370 protected GroupInfo createL2L3Chain(TrafficTreatment treatment, int nextId,
Charles Chanf9e98652016-09-07 16:54:23 -0700371 ApplicationId appId, boolean mpls,
372 TrafficSelector meta) {
373 return createL2L3ChainInternal(treatment, nextId, appId, mpls, meta, true);
374 }
375
376 /**
377 * Internal implementation of createL2L3Chain.
378 * <p>
379 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
380 * Since it is non-OF spec, we need an extension treatment for that.
381 * The useSetVlanExtension must be set to false for OFDPA i12.
382 * </p>
383 *
384 * @param treatment that needs to be broken up to create the group chain
385 * @param nextId of the next objective that needs this group chain
386 * @param appId of the application that sent this next objective
387 * @param mpls determines if L3Unicast or MPLSInterface group is created
388 * @param meta metadata passed in by the application as part of the nextObjective
389 * @param useSetVlanExtension use the setVlanVid extension that has is_present bit set to 0.
390 * @return GroupInfo containing the GroupDescription of the
391 * L2Interface group(inner) and the GroupDescription of the (outer)
392 * L3Unicast/MPLSInterface group. May return null if there is an
393 * error in processing the chain
394 */
395 protected GroupInfo createL2L3ChainInternal(TrafficTreatment treatment, int nextId,
Pier Ventre42287df2016-11-09 14:17:26 -0800396 ApplicationId appId, boolean mpls,
397 TrafficSelector meta, boolean useSetVlanExtension) {
Charles Chan188ebf52015-12-23 00:15:11 -0800398 // for the l2interface group, get vlan and port info
399 // for the outer group, get the src/dst mac, and vlan info
400 TrafficTreatment.Builder outerTtb = DefaultTrafficTreatment.builder();
401 TrafficTreatment.Builder innerTtb = DefaultTrafficTreatment.builder();
402 VlanId vlanid = null;
403 long portNum = 0;
404 boolean setVlan = false, popVlan = false;
Yi Tseng78f51f42017-02-02 13:54:58 -0800405 MacAddress srcMac;
406 MacAddress dstMac;
Charles Chan188ebf52015-12-23 00:15:11 -0800407 for (Instruction ins : treatment.allInstructions()) {
408 if (ins.type() == Instruction.Type.L2MODIFICATION) {
409 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
410 switch (l2ins.subtype()) {
411 case ETH_DST:
Charles Chan5270ed02016-01-30 23:22:37 -0800412 dstMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
413 outerTtb.setEthDst(dstMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800414 break;
415 case ETH_SRC:
Charles Chand0fd5dc2016-02-16 23:14:49 -0800416 srcMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
417 outerTtb.setEthSrc(srcMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800418 break;
419 case VLAN_ID:
420 vlanid = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
Charles Chanf9e98652016-09-07 16:54:23 -0700421 if (useSetVlanExtension) {
422 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
423 outerTtb.extension(ofdpaSetVlanVid, deviceId);
424 } else {
425 outerTtb.setVlanId(vlanid);
426 }
Charles Chan188ebf52015-12-23 00:15:11 -0800427 setVlan = true;
428 break;
429 case VLAN_POP:
430 innerTtb.popVlan();
431 popVlan = true;
432 break;
433 case DEC_MPLS_TTL:
434 case MPLS_LABEL:
435 case MPLS_POP:
436 case MPLS_PUSH:
437 case VLAN_PCP:
438 case VLAN_PUSH:
439 default:
440 break;
441 }
442 } else if (ins.type() == Instruction.Type.OUTPUT) {
443 portNum = ((Instructions.OutputInstruction) ins).port().toLong();
444 innerTtb.add(ins);
445 } else {
Saurav Das7bcbe702017-06-13 15:35:54 -0700446 log.debug("Driver does not handle this type of TrafficTreatment"
Charles Chan188ebf52015-12-23 00:15:11 -0800447 + " instruction in nextObjectives: {}", ins.type());
448 }
449 }
450
451 if (vlanid == null && meta != null) {
452 // use metadata if available
Pier Ventre140a8942016-11-02 07:26:38 -0700453 Criterion vidCriterion = meta.getCriterion(VLAN_VID);
Charles Chan188ebf52015-12-23 00:15:11 -0800454 if (vidCriterion != null) {
455 vlanid = ((VlanIdCriterion) vidCriterion).vlanId();
456 }
457 // if vlan is not set, use the vlan in metadata for outerTtb
458 if (vlanid != null && !setVlan) {
Charles Chanf9e98652016-09-07 16:54:23 -0700459 if (useSetVlanExtension) {
460 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
461 outerTtb.extension(ofdpaSetVlanVid, deviceId);
462 } else {
463 outerTtb.setVlanId(vlanid);
464 }
Charles Chan188ebf52015-12-23 00:15:11 -0800465 }
466 }
467
468 if (vlanid == null) {
469 log.error("Driver cannot process an L2/L3 group chain without "
470 + "egress vlan information for dev: {} port:{}",
471 deviceId, portNum);
472 return null;
473 }
474
475 if (!setVlan && !popVlan) {
476 // untagged outgoing port
477 TrafficTreatment.Builder temp = DefaultTrafficTreatment.builder();
478 temp.popVlan();
Yi Tsengef19de12017-04-24 11:33:05 -0700479 innerTtb.build().allInstructions().forEach(temp::add);
Charles Chan188ebf52015-12-23 00:15:11 -0800480 innerTtb = temp;
481 }
482
483 // assemble information for ofdpa l2interface group
Yi Tsengef19de12017-04-24 11:33:05 -0700484 int l2groupId = l2GroupId(vlanid, portNum);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800485 // a globally unique groupkey that is different for ports in the same device,
Charles Chan188ebf52015-12-23 00:15:11 -0800486 // but different for the same portnumber on different devices. Also different
487 // for the various group-types created out of the same next objective.
Charles Chane849c192016-01-11 18:28:54 -0800488 int l2gk = l2InterfaceGroupKey(deviceId, vlanid, portNum);
Yi Tsengef19de12017-04-24 11:33:05 -0700489 final GroupKey l2groupkey = new DefaultGroupKey(appKryo.serialize(l2gk));
Charles Chan188ebf52015-12-23 00:15:11 -0800490
491 // assemble information for outer group
Yi Tsengef19de12017-04-24 11:33:05 -0700492 GroupDescription outerGrpDesc;
Charles Chan188ebf52015-12-23 00:15:11 -0800493 if (mpls) {
Yi Tsengef19de12017-04-24 11:33:05 -0700494 // outer group is MPLS Interface
Saurav Das8be4e3a2016-03-11 17:19:07 -0800495 int mplsInterfaceIndex = getNextAvailableIndex();
Yi Tsengef19de12017-04-24 11:33:05 -0700496 int mplsGroupId = MPLS_INTERFACE_TYPE | (SUBTYPE_MASK & mplsInterfaceIndex);
497 final GroupKey mplsGroupKey = new DefaultGroupKey(
498 appKryo.serialize(mplsInterfaceIndex));
Yi Tsengfa394de2017-02-01 11:26:40 -0800499 outerTtb.group(new GroupId(l2groupId));
Charles Chan188ebf52015-12-23 00:15:11 -0800500 // create the mpls-interface group description to wait for the
501 // l2 interface group to be processed
502 GroupBucket mplsinterfaceGroupBucket =
503 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
504 outerGrpDesc = new DefaultGroupDescription(
505 deviceId,
506 GroupDescription.Type.INDIRECT,
507 new GroupBuckets(Collections.singletonList(
508 mplsinterfaceGroupBucket)),
Yi Tsengef19de12017-04-24 11:33:05 -0700509 mplsGroupKey,
510 mplsGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800511 appId);
512 log.debug("Trying MPLS-Interface: device:{} gid:{} gkey:{} nextid:{}",
Yi Tsengef19de12017-04-24 11:33:05 -0700513 deviceId, Integer.toHexString(mplsGroupId),
514 mplsGroupKey, nextId);
Charles Chan188ebf52015-12-23 00:15:11 -0800515 } else {
516 // outer group is L3Unicast
Saurav Das8be4e3a2016-03-11 17:19:07 -0800517 int l3unicastIndex = getNextAvailableIndex();
518 int l3groupId = L3_UNICAST_TYPE | (TYPE_MASK & l3unicastIndex);
519 final GroupKey l3groupkey = new DefaultGroupKey(
Yi Tsengef19de12017-04-24 11:33:05 -0700520 appKryo.serialize(l3unicastIndex));
Yi Tsengfa394de2017-02-01 11:26:40 -0800521 outerTtb.group(new GroupId(l2groupId));
Charles Chan188ebf52015-12-23 00:15:11 -0800522 // create the l3unicast group description to wait for the
523 // l2 interface group to be processed
524 GroupBucket l3unicastGroupBucket =
525 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
526 outerGrpDesc = new DefaultGroupDescription(
527 deviceId,
528 GroupDescription.Type.INDIRECT,
Yi Tsengef19de12017-04-24 11:33:05 -0700529 new GroupBuckets(Collections.singletonList(l3unicastGroupBucket)),
Charles Chan188ebf52015-12-23 00:15:11 -0800530 l3groupkey,
531 l3groupId,
532 appId);
533 log.debug("Trying L3Unicast: device:{} gid:{} gkey:{} nextid:{}",
534 deviceId, Integer.toHexString(l3groupId),
535 l3groupkey, nextId);
536 }
537
538 // store l2groupkey with the groupChainElem for the outer-group that depends on it
Yi Tsengef19de12017-04-24 11:33:05 -0700539 GroupChainElem gce = new GroupChainElem(outerGrpDesc, 1, false, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800540 updatePendingGroups(l2groupkey, gce);
541
Yi Tsengef19de12017-04-24 11:33:05 -0700542 // create group description for the inner l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700543 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800544 DefaultGroupBucket.createIndirectGroupBucket(innerTtb.build());
545 GroupDescription l2groupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -0700546 new DefaultGroupDescription(deviceId,
547 GroupDescription.Type.INDIRECT,
548 new GroupBuckets(Collections.singletonList(l2InterfaceGroupBucket)),
549 l2groupkey,
550 l2groupId,
551 appId);
Charles Chan188ebf52015-12-23 00:15:11 -0800552 log.debug("Trying L2Interface: device:{} gid:{} gkey:{} nextId:{}",
553 deviceId, Integer.toHexString(l2groupId),
554 l2groupkey, nextId);
555 return new GroupInfo(l2groupDescription, outerGrpDesc);
556
557 }
558
559 /**
560 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
561 * a chain of groups. The broadcast Next Objective passed in by the application
562 * has to be broken up into a group chain comprising of an
Saurav Das1a129a02016-11-18 15:21:57 -0800563 * L2 Flood group or L3 Multicast group, whose buckets point to L2 Interface groups.
Charles Chan188ebf52015-12-23 00:15:11 -0800564 *
565 * @param nextObj the nextObjective of type BROADCAST
566 */
567 private void processBroadcastNextObjective(NextObjective nextObj) {
Yi Tsengef19de12017-04-24 11:33:05 -0700568 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700569 if (assignedVlan == null) {
570 log.warn("VLAN ID required by broadcast next obj is missing. Abort.");
Yi Tsengef19de12017-04-24 11:33:05 -0700571 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800572 return;
573 }
Charles Chan188ebf52015-12-23 00:15:11 -0800574
Charles Chan5b9df8d2016-03-28 22:21:40 -0700575 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
576
Yi Tsengef19de12017-04-24 11:33:05 -0700577 IpPrefix ipDst = readIpDstFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700578 if (ipDst != null) {
579 if (ipDst.isMulticast()) {
580 createL3MulticastGroup(nextObj, assignedVlan, groupInfos);
581 } else {
582 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -0700583 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700584 }
585 } else {
586 createL2FloodGroup(nextObj, assignedVlan, groupInfos);
587 }
588 }
589
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700590 private List<GroupInfo> prepareL2InterfaceGroup(NextObjective nextObj,
591 VlanId assignedVlan) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700592 ImmutableList.Builder<GroupInfo> groupInfoBuilder = ImmutableList.builder();
593
594 // break up broadcast next objective to multiple groups
595 Collection<TrafficTreatment> buckets = nextObj.next();
596
Charles Chan188ebf52015-12-23 00:15:11 -0800597 // each treatment is converted to an L2 interface group
Charles Chan188ebf52015-12-23 00:15:11 -0800598 for (TrafficTreatment treatment : buckets) {
599 TrafficTreatment.Builder newTreatment = DefaultTrafficTreatment.builder();
600 PortNumber portNum = null;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700601 VlanId egressVlan = null;
Charles Chan188ebf52015-12-23 00:15:11 -0800602 // ensure that the only allowed treatments are pop-vlan and output
603 for (Instruction ins : treatment.allInstructions()) {
604 if (ins.type() == Instruction.Type.L2MODIFICATION) {
605 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
606 switch (l2ins.subtype()) {
607 case VLAN_POP:
608 newTreatment.add(l2ins);
609 break;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700610 case VLAN_ID:
611 egressVlan = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
612 break;
Charles Chan188ebf52015-12-23 00:15:11 -0800613 default:
614 log.debug("action {} not permitted for broadcast nextObj",
615 l2ins.subtype());
616 break;
617 }
618 } else if (ins.type() == Instruction.Type.OUTPUT) {
619 portNum = ((Instructions.OutputInstruction) ins).port();
620 newTreatment.add(ins);
621 } else {
Charles Chane849c192016-01-11 18:28:54 -0800622 log.debug("TrafficTreatment of type {} not permitted in " +
623 " broadcast nextObjective", ins.type());
Charles Chan188ebf52015-12-23 00:15:11 -0800624 }
625 }
626
Yi Tsengef19de12017-04-24 11:33:05 -0700627 if (portNum == null) {
628 log.warn("Can't find output port for the bucket {}.", treatment);
629 continue;
630 }
631
Charles Chan188ebf52015-12-23 00:15:11 -0800632 // assemble info for l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700633 VlanId l2InterfaceGroupVlan =
634 (egressVlan != null && !assignedVlan.equals(egressVlan)) ?
635 egressVlan : assignedVlan;
636 int l2gk = l2InterfaceGroupKey(deviceId, l2InterfaceGroupVlan, portNum.toLong());
637 final GroupKey l2InterfaceGroupKey =
Yi Tsengef19de12017-04-24 11:33:05 -0700638 new DefaultGroupKey(appKryo.serialize(l2gk));
Charles Chan372b63e2017-02-07 12:10:53 -0800639 int l2InterfaceGroupId = L2_INTERFACE_TYPE |
640 ((l2InterfaceGroupVlan.toShort() & THREE_BIT_MASK) << PORT_LEN) |
641 ((int) portNum.toLong() & FOUR_BIT_MASK);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700642 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800643 DefaultGroupBucket.createIndirectGroupBucket(newTreatment.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700644 GroupDescription l2InterfaceGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -0700645 new DefaultGroupDescription(deviceId,
646 GroupDescription.Type.INDIRECT,
647 new GroupBuckets(Collections.singletonList(
648 l2InterfaceGroupBucket)),
649 l2InterfaceGroupKey,
650 l2InterfaceGroupId,
651 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800652 log.debug("Trying L2-Interface: device:{} gid:{} gkey:{} nextid:{}",
Charles Chan5b9df8d2016-03-28 22:21:40 -0700653 deviceId, Integer.toHexString(l2InterfaceGroupId),
654 l2InterfaceGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -0800655
Charles Chan5b9df8d2016-03-28 22:21:40 -0700656 groupInfoBuilder.add(new GroupInfo(l2InterfaceGroupDescription,
657 l2InterfaceGroupDescription));
Charles Chan188ebf52015-12-23 00:15:11 -0800658 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700659 return groupInfoBuilder.build();
660 }
Charles Chan188ebf52015-12-23 00:15:11 -0800661
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700662 private void createL2FloodGroup(NextObjective nextObj, VlanId vlanId,
663 List<GroupInfo> groupInfos) {
664 // assemble info for l2 flood group. Since there can be only one flood
665 // group for a vlan, its index is always the same - 0
Yi Tsengef19de12017-04-24 11:33:05 -0700666 Integer l2FloodGroupId = L2_FLOOD_TYPE | (vlanId.toShort() << 16);
667 final GroupKey l2FloodGroupKey = l2FloodGroupKey(vlanId, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700668
Charles Chan188ebf52015-12-23 00:15:11 -0800669 // collection of group buckets pointing to all the l2 interface groups
Yi Tsengef19de12017-04-24 11:33:05 -0700670 List<GroupBucket> l2floodBuckets = generateNextGroupBuckets(groupInfos, ALL);
Charles Chan188ebf52015-12-23 00:15:11 -0800671 // create the l2flood group-description to wait for all the
672 // l2interface groups to be processed
673 GroupDescription l2floodGroupDescription =
674 new DefaultGroupDescription(
675 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800676 ALL,
Charles Chan188ebf52015-12-23 00:15:11 -0800677 new GroupBuckets(l2floodBuckets),
Yi Tsengef19de12017-04-24 11:33:05 -0700678 l2FloodGroupKey,
679 l2FloodGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800680 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800681 log.debug("Trying L2-Flood: device:{} gid:{} gkey:{} nextid:{}",
Yi Tsengef19de12017-04-24 11:33:05 -0700682 deviceId, Integer.toHexString(l2FloodGroupId),
683 l2FloodGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -0800684
Charles Chan5b9df8d2016-03-28 22:21:40 -0700685 // Put all dependency information into allGroupKeys
686 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
687 groupInfos.forEach(groupInfo -> {
Yi Tsengef19de12017-04-24 11:33:05 -0700688 Deque<GroupKey> groupKeyChain = new ArrayDeque<>();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700689 // In this case we should have L2 interface group only
Yi Tsengef19de12017-04-24 11:33:05 -0700690 groupKeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
691 groupKeyChain.addFirst(l2FloodGroupKey);
692 allGroupKeys.add(groupKeyChain);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700693 });
Charles Chan188ebf52015-12-23 00:15:11 -0800694
Charles Chan5b9df8d2016-03-28 22:21:40 -0700695 // Point the next objective to this group
696 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -0700697 updatePendingNextObjective(l2FloodGroupKey, ofdpaGrp);
Charles Chan188ebf52015-12-23 00:15:11 -0800698
Charles Chan5b9df8d2016-03-28 22:21:40 -0700699 GroupChainElem gce = new GroupChainElem(l2floodGroupDescription,
Yi Tsengef19de12017-04-24 11:33:05 -0700700 groupInfos.size(), false, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700701 groupInfos.forEach(groupInfo -> {
702 // Point this group to the next group
Yi Tsengef19de12017-04-24 11:33:05 -0700703 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), gce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700704 // Start installing the inner-most group
Yi Tsengef19de12017-04-24 11:33:05 -0700705 groupService.addGroup(groupInfo.innerMostGroupDesc());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700706 });
707 }
708
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700709 private void createL3MulticastGroup(NextObjective nextObj, VlanId vlanId,
710 List<GroupInfo> groupInfos) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700711 List<GroupBucket> l3McastBuckets = new ArrayList<>();
712 groupInfos.forEach(groupInfo -> {
713 // Points to L3 interface group if there is one.
714 // Otherwise points to L2 interface group directly.
Yi Tsengef19de12017-04-24 11:33:05 -0700715 GroupDescription nextGroupDesc = (groupInfo.nextGroupDesc() != null) ?
716 groupInfo.nextGroupDesc() : groupInfo.innerMostGroupDesc();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700717 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Yi Tsengfa394de2017-02-01 11:26:40 -0800718 ttb.group(new GroupId(nextGroupDesc.givenGroupId()));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700719 GroupBucket abucket = DefaultGroupBucket.createAllGroupBucket(ttb.build());
720 l3McastBuckets.add(abucket);
721 });
722
723 int l3MulticastIndex = getNextAvailableIndex();
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700724 int l3MulticastGroupId = L3_MULTICAST_TYPE |
725 vlanId.toShort() << 16 | (TYPE_VLAN_MASK & l3MulticastIndex);
726 final GroupKey l3MulticastGroupKey =
Yi Tsengef19de12017-04-24 11:33:05 -0700727 new DefaultGroupKey(appKryo.serialize(l3MulticastIndex));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700728
729 GroupDescription l3MulticastGroupDesc = new DefaultGroupDescription(deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800730 ALL,
Charles Chan5b9df8d2016-03-28 22:21:40 -0700731 new GroupBuckets(l3McastBuckets),
732 l3MulticastGroupKey,
733 l3MulticastGroupId,
734 nextObj.appId());
735
736 // Put all dependency information into allGroupKeys
737 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
738 groupInfos.forEach(groupInfo -> {
739 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700740 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700741 // Add L3 interface group to the chain if there is one.
Yi Tsengef19de12017-04-24 11:33:05 -0700742 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
743 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700744 }
745 gkeyChain.addFirst(l3MulticastGroupKey);
746 allGroupKeys.add(gkeyChain);
747 });
748
749 // Point the next objective to this group
750 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
751 updatePendingNextObjective(l3MulticastGroupKey, ofdpaGrp);
752
753 GroupChainElem outerGce = new GroupChainElem(l3MulticastGroupDesc,
Yi Tsengef19de12017-04-24 11:33:05 -0700754 groupInfos.size(), false, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700755 groupInfos.forEach(groupInfo -> {
756 // Point this group (L3 multicast) to the next group
Yi Tsengef19de12017-04-24 11:33:05 -0700757 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), outerGce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700758
759 // Point next group to inner-most group, if any
Yi Tsengef19de12017-04-24 11:33:05 -0700760 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
761 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(),
762 1, false, deviceId);
763 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700764 }
765
766 // Start installing the inner-most group
Yi Tsengef19de12017-04-24 11:33:05 -0700767 groupService.addGroup(groupInfo.innerMostGroupDesc());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700768 });
Charles Chan188ebf52015-12-23 00:15:11 -0800769 }
770
Charles Chan188ebf52015-12-23 00:15:11 -0800771 /**
772 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
773 * a chain of groups. The hashed Next Objective passed in by the application
774 * has to be broken up into a group chain comprising of an
775 * L3 ECMP group as the top level group. Buckets of this group can point
776 * to a variety of groups in a group chain, depending on the whether
777 * MPLS labels are being pushed or not.
778 * <p>
779 * NOTE: We do not create MPLS ECMP groups as they are unimplemented in
780 * OF-DPA 2.0 (even though it is in the spec). Therefore we do not
781 * check the nextObjective meta to see what is matching before being
782 * sent to this nextObjective.
783 *
784 * @param nextObj the nextObjective of type HASHED
785 */
Pier Ventre140a8942016-11-02 07:26:38 -0700786 protected void processHashedNextObjective(NextObjective nextObj) {
Charles Chan188ebf52015-12-23 00:15:11 -0800787 // storage for all group keys in the chain of groups created
788 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
789 List<GroupInfo> unsentGroups = new ArrayList<>();
790 createHashBucketChains(nextObj, allGroupKeys, unsentGroups);
791
792 // now we can create the outermost L3 ECMP group
793 List<GroupBucket> l3ecmpGroupBuckets = new ArrayList<>();
794 for (GroupInfo gi : unsentGroups) {
795 // create ECMP bucket to point to the outer group
796 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Yi Tsengef19de12017-04-24 11:33:05 -0700797 ttb.group(new GroupId(gi.nextGroupDesc().givenGroupId()));
Charles Chan188ebf52015-12-23 00:15:11 -0800798 GroupBucket sbucket = DefaultGroupBucket
799 .createSelectGroupBucket(ttb.build());
800 l3ecmpGroupBuckets.add(sbucket);
801 }
Saurav Das8be4e3a2016-03-11 17:19:07 -0800802 int l3ecmpIndex = getNextAvailableIndex();
803 int l3ecmpGroupId = L3_ECMP_TYPE | (TYPE_MASK & l3ecmpIndex);
804 GroupKey l3ecmpGroupKey = new DefaultGroupKey(
Yi Tsengef19de12017-04-24 11:33:05 -0700805 appKryo.serialize(l3ecmpIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800806 GroupDescription l3ecmpGroupDesc =
807 new DefaultGroupDescription(
808 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800809 SELECT,
Charles Chan188ebf52015-12-23 00:15:11 -0800810 new GroupBuckets(l3ecmpGroupBuckets),
811 l3ecmpGroupKey,
812 l3ecmpGroupId,
813 nextObj.appId());
814 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
815 l3ecmpGroupBuckets.size(),
Yi Tsengef19de12017-04-24 11:33:05 -0700816 false, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800817
818 // create objects for local and distributed storage
Yi Tsengef19de12017-04-24 11:33:05 -0700819 allGroupKeys.forEach(gKeyChain -> gKeyChain.addFirst(l3ecmpGroupKey));
Charles Chan188ebf52015-12-23 00:15:11 -0800820 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
821
822 // store l3ecmpGroupKey with the ofdpaGroupChain for the nextObjective
823 // that depends on it
824 updatePendingNextObjective(l3ecmpGroupKey, ofdpaGrp);
825
826 log.debug("Trying L3ECMP: device:{} gid:{} gkey:{} nextId:{}",
827 deviceId, Integer.toHexString(l3ecmpGroupId),
828 l3ecmpGroupKey, nextObj.id());
829 // finally we are ready to send the innermost groups
830 for (GroupInfo gi : unsentGroups) {
831 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -0700832 Integer.toHexString(gi.innerMostGroupDesc().givenGroupId()), deviceId);
833 updatePendingGroups(gi.nextGroupDesc().appCookie(), l3ecmpGce);
834 groupService.addGroup(gi.innerMostGroupDesc());
Charles Chan188ebf52015-12-23 00:15:11 -0800835 }
Charles Chan188ebf52015-12-23 00:15:11 -0800836 }
837
838 /**
839 * Creates group chains for all buckets in a hashed group, and stores the
840 * GroupInfos and GroupKeys for all the groups in the lists passed in, which
841 * should be empty.
842 * <p>
843 * Does not create the top level ECMP group. Does not actually send the
844 * groups to the groupService.
845 *
846 * @param nextObj the Next Objective with buckets that need to be converted
847 * to group chains
848 * @param allGroupKeys a list to store groupKey for each bucket-group-chain
849 * @param unsentGroups a list to store GroupInfo for each bucket-group-chain
850 */
Pier Ventre140a8942016-11-02 07:26:38 -0700851 protected void createHashBucketChains(NextObjective nextObj,
Yi Tsengef19de12017-04-24 11:33:05 -0700852 List<Deque<GroupKey>> allGroupKeys,
853 List<GroupInfo> unsentGroups) {
Charles Chan188ebf52015-12-23 00:15:11 -0800854 // break up hashed next objective to multiple groups
855 Collection<TrafficTreatment> buckets = nextObj.next();
856
857 for (TrafficTreatment bucket : buckets) {
858 //figure out how many labels are pushed in each bucket
859 int labelsPushed = 0;
860 MplsLabel innermostLabel = null;
861 for (Instruction ins : bucket.allInstructions()) {
862 if (ins.type() == Instruction.Type.L2MODIFICATION) {
863 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
864 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_PUSH) {
865 labelsPushed++;
866 }
867 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) {
868 if (innermostLabel == null) {
Yi Tsengef19de12017-04-24 11:33:05 -0700869 innermostLabel =
870 ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label();
Charles Chan188ebf52015-12-23 00:15:11 -0800871 }
872 }
873 }
874 }
875
Yi Tsengef19de12017-04-24 11:33:05 -0700876 Deque<GroupKey> gKeyChain = new ArrayDeque<>();
Charles Chan188ebf52015-12-23 00:15:11 -0800877 // XXX we only deal with 0 and 1 label push right now
878 if (labelsPushed == 0) {
Yi Tsengef19de12017-04-24 11:33:05 -0700879 GroupInfo noLabelGroupInfo;
Pier Ventre140a8942016-11-02 07:26:38 -0700880 TrafficSelector metaSelector = nextObj.meta();
881 if (metaSelector != null) {
882 if (isNotMplsBos(metaSelector)) {
Yi Tsengef19de12017-04-24 11:33:05 -0700883 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
884 nextObj.appId(), true,
885 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700886 } else {
Yi Tsengef19de12017-04-24 11:33:05 -0700887 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
888 nextObj.appId(), false,
889 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700890 }
891 } else {
Yi Tsengef19de12017-04-24 11:33:05 -0700892 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
893 nextObj.appId(), false,
894 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700895 }
Yi Tsengef19de12017-04-24 11:33:05 -0700896 if (noLabelGroupInfo == null) {
Charles Chan188ebf52015-12-23 00:15:11 -0800897 log.error("Could not process nextObj={} in dev:{}",
898 nextObj.id(), deviceId);
899 return;
900 }
Yi Tsengef19de12017-04-24 11:33:05 -0700901 gKeyChain.addFirst(noLabelGroupInfo.innerMostGroupDesc().appCookie());
902 gKeyChain.addFirst(noLabelGroupInfo.nextGroupDesc().appCookie());
Charles Chan188ebf52015-12-23 00:15:11 -0800903
904 // we can't send the inner group description yet, as we have to
905 // create the dependent ECMP group first. So we store..
Yi Tsengef19de12017-04-24 11:33:05 -0700906 unsentGroups.add(noLabelGroupInfo);
Charles Chan188ebf52015-12-23 00:15:11 -0800907
908 } else if (labelsPushed == 1) {
909 GroupInfo onelabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
910 nextObj.appId(), true,
911 nextObj.meta());
912 if (onelabelGroupInfo == null) {
913 log.error("Could not process nextObj={} in dev:{}",
914 nextObj.id(), deviceId);
915 return;
916 }
917 // we need to add another group to this chain - the L3VPN group
918 TrafficTreatment.Builder l3vpnTtb = DefaultTrafficTreatment.builder();
Charles Chan0f43e472017-02-14 14:00:16 -0800919 if (requireVlanPopBeforeMplsPush()) {
920 l3vpnTtb.popVlan();
921 }
Charles Chan188ebf52015-12-23 00:15:11 -0800922 l3vpnTtb.pushMpls()
923 .setMpls(innermostLabel)
Yi Tsengef19de12017-04-24 11:33:05 -0700924 .group(new GroupId(onelabelGroupInfo.nextGroupDesc().givenGroupId()));
Charles Chan40132b32017-01-22 00:19:37 -0800925 if (supportCopyTtl()) {
926 l3vpnTtb.copyTtlOut();
927 }
928 if (supportSetMplsBos()) {
929 l3vpnTtb.setMplsBos(true);
930 }
Charles Chan0f43e472017-02-14 14:00:16 -0800931 if (requireVlanPopBeforeMplsPush()) {
932 l3vpnTtb.pushVlan().setVlanId(VlanId.vlanId(VlanId.RESERVED));
933 }
Charles Chan40132b32017-01-22 00:19:37 -0800934
Charles Chan188ebf52015-12-23 00:15:11 -0800935 GroupBucket l3vpnGrpBkt =
936 DefaultGroupBucket.createIndirectGroupBucket(l3vpnTtb.build());
Saurav Das8be4e3a2016-03-11 17:19:07 -0800937 int l3vpnIndex = getNextAvailableIndex();
Yi Tsengef19de12017-04-24 11:33:05 -0700938 int l3vpnGroupId = MPLS_L3VPN_SUBTYPE | (SUBTYPE_MASK & l3vpnIndex);
939 GroupKey l3vpnGroupKey = new DefaultGroupKey(
940 appKryo.serialize(l3vpnIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800941 GroupDescription l3vpnGroupDesc =
942 new DefaultGroupDescription(
943 deviceId,
944 GroupDescription.Type.INDIRECT,
Yi Tsengef19de12017-04-24 11:33:05 -0700945 new GroupBuckets(Collections.singletonList(l3vpnGrpBkt)),
946 l3vpnGroupKey,
947 l3vpnGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800948 nextObj.appId());
Yi Tsengef19de12017-04-24 11:33:05 -0700949 GroupChainElem l3vpnGce = new GroupChainElem(l3vpnGroupDesc,
950 1,
951 false,
952 deviceId);
953 updatePendingGroups(onelabelGroupInfo.nextGroupDesc().appCookie(), l3vpnGce);
Charles Chan188ebf52015-12-23 00:15:11 -0800954
Yi Tsengef19de12017-04-24 11:33:05 -0700955 gKeyChain.addFirst(onelabelGroupInfo.innerMostGroupDesc().appCookie());
956 gKeyChain.addFirst(onelabelGroupInfo.nextGroupDesc().appCookie());
957 gKeyChain.addFirst(l3vpnGroupKey);
Charles Chan188ebf52015-12-23 00:15:11 -0800958
959 //now we can replace the outerGrpDesc with the one we just created
Yi Tsengef19de12017-04-24 11:33:05 -0700960 onelabelGroupInfo.nextGroupDesc(l3vpnGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800961
962 // we can't send the innermost group yet, as we have to create
963 // the dependent ECMP group first. So we store ...
964 unsentGroups.add(onelabelGroupInfo);
965
Yi Tsengef19de12017-04-24 11:33:05 -0700966 log.debug("Trying L3VPN: device:{} gid:{} group key:{} nextId:{}",
967 deviceId, Integer.toHexString(l3vpnGroupId),
968 l3vpnGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -0800969
970 } else {
971 log.warn("Driver currently does not handle more than 1 MPLS "
972 + "labels. Not processing nextObjective {}", nextObj.id());
973 return;
974 }
975
976 // all groups in this chain
Yi Tsengef19de12017-04-24 11:33:05 -0700977 allGroupKeys.add(gKeyChain);
Charles Chan188ebf52015-12-23 00:15:11 -0800978 }
979 }
980
Pier Ventre42287df2016-11-09 14:17:26 -0800981 /**
982 * Processes the pseudo wire related next objective.
983 * This procedure try to reuse the mpls label groups,
984 * the mpls interface group and the l2 interface group.
985 *
986 * @param nextObjective the objective to process.
987 */
988 protected void processPwNextObjective(NextObjective nextObjective) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700989 log.warn("Pseudo wire extensions are not supported in OFDPA 2.0 {}",
990 nextObjective.id());
Pier Ventre42287df2016-11-09 14:17:26 -0800991 }
992
Saurav Das8be4e3a2016-03-11 17:19:07 -0800993 //////////////////////////////////////
994 // Group Editing
995 //////////////////////////////////////
Charles Chan188ebf52015-12-23 00:15:11 -0800996 /**
997 * Adds a bucket to the top level group of a group-chain, and creates the chain.
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700998 * Ensures that bucket being added is not a duplicate, by checking existing
Yi Tsengef19de12017-04-24 11:33:05 -0700999 * buckets for the same output port.
Charles Chan188ebf52015-12-23 00:15:11 -08001000 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001001 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -08001002 * @param next the representation of the existing group-chain for this next objective
1003 */
1004 protected void addBucketToGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001005 if (nextObjective.type() != NextObjective.Type.HASHED &&
1006 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001007 log.warn("AddBuckets not applied to nextType:{} in dev:{} for next:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001008 nextObjective.type(), deviceId, nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -07001009 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001010 return;
1011 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001012
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001013 // first check to see if bucket being added is not a duplicate of an
Yi Tsengef19de12017-04-24 11:33:05 -07001014 // existing bucket. If it is for an existing output port, then its a
1015 // duplicate.
Yi Tseng78f51f42017-02-02 13:54:58 -08001016 Set<TrafficTreatment> duplicateBuckets = Sets.newHashSet();
Yi Tsengef19de12017-04-24 11:33:05 -07001017 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
1018 Set<PortNumber> existingPorts = getExistingOutputPorts(allActiveKeys,
1019 groupService,
1020 deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -08001021 Set<TrafficTreatment> nonDuplicateBuckets = Sets.newHashSet();
1022 NextObjective objectiveToAdd;
Yi Tseng78f51f42017-02-02 13:54:58 -08001023
1024 nextObjective.next().forEach(trafficTreatment -> {
1025 PortNumber portNumber = readOutPortFromTreatment(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001026 if (portNumber == null) {
1027 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001028 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001029 if (existingPorts.contains(portNumber)) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001030 // its possible that portnumbers are same but labels are different
1031 int label = readLabelFromTreatment(trafficTreatment);
1032 if (label == -1) {
1033 duplicateBuckets.add(trafficTreatment);
1034 } else {
Saurav Dasceccf242017-08-03 18:30:35 -07001035 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1036 groupService, deviceId,
1037 portNumber, label);
1038 if (!existing.isEmpty()) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001039 duplicateBuckets.add(trafficTreatment);
1040 } else {
1041 nonDuplicateBuckets.add(trafficTreatment);
1042 }
1043 }
Yi Tseng47f82dc2017-03-05 22:48:39 -08001044 } else {
1045 nonDuplicateBuckets.add(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001046 }
1047 });
1048
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001049 if (duplicateBuckets.isEmpty()) {
1050 // use the original objective
1051 objectiveToAdd = nextObjective;
1052 } else if (!nonDuplicateBuckets.isEmpty()) {
1053 // only use the non-duplicate buckets if there are any
1054 log.debug("Some buckets {} already exist in next id {}, duplicate "
1055 + "buckets will be ignored.", duplicateBuckets, nextObjective.id());
1056 // new next objective with non duplicate treatments
Yi Tseng47f82dc2017-03-05 22:48:39 -08001057 NextObjective.Builder builder = DefaultNextObjective.builder()
1058 .withType(nextObjective.type())
1059 .withId(nextObjective.id())
1060 .withMeta(nextObjective.meta())
1061 .fromApp(nextObjective.appId());
1062 nonDuplicateBuckets.forEach(builder::addTreatment);
1063
1064 ObjectiveContext context = nextObjective.context().orElse(null);
1065 objectiveToAdd = builder.addToExisting(context);
1066 } else {
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001067 // buckets to add are already there - nothing to do
Saurav Das1547b3f2017-05-05 17:01:08 -07001068 log.debug("buckets already exist {} in next: {} ..ignoring bucket add",
1069 duplicateBuckets, nextObjective.id());
1070 pass(nextObjective);
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001071 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001072 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001073
Saurav Das1a129a02016-11-18 15:21:57 -08001074 if (nextObjective.type() == NextObjective.Type.HASHED) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001075 addBucketToHashGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001076 } else if (nextObjective.type() == NextObjective.Type.BROADCAST) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001077 addBucketToBroadcastGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001078 }
1079 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001080
Saurav Das1a129a02016-11-18 15:21:57 -08001081 private void addBucketToHashGroup(NextObjective nextObjective,
Yi Tseng78f51f42017-02-02 13:54:58 -08001082 List<Deque<GroupKey>> allActiveKeys) {
Charles Chan188ebf52015-12-23 00:15:11 -08001083 // storage for all group keys in the chain of groups created
1084 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
1085 List<GroupInfo> unsentGroups = new ArrayList<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001086 List<GroupBucket> newBuckets;
Charles Chan188ebf52015-12-23 00:15:11 -08001087 createHashBucketChains(nextObjective, allGroupKeys, unsentGroups);
1088
Yi Tseng78f51f42017-02-02 13:54:58 -08001089 // now we can create the buckets to add to the outermost L3 ECMP group
1090 newBuckets = generateNextGroupBuckets(unsentGroups, SELECT);
Charles Chan188ebf52015-12-23 00:15:11 -08001091
Saurav Das1a129a02016-11-18 15:21:57 -08001092 // retrieve the original L3 ECMP group
1093 Group l3ecmpGroup = retrieveTopLevelGroup(allActiveKeys, nextObjective.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001094 if (l3ecmpGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001095 fail(nextObjective, ObjectiveError.GROUPMISSING);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001096 return;
1097 }
Saurav Das1a129a02016-11-18 15:21:57 -08001098 GroupKey l3ecmpGroupKey = l3ecmpGroup.appCookie();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001099 int l3ecmpGroupId = l3ecmpGroup.id().id();
Charles Chan188ebf52015-12-23 00:15:11 -08001100
1101 // Although GroupDescriptions are not necessary for adding buckets to
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001102 // existing groups, we still use one in the GroupChainElem. When the latter is
Charles Chan188ebf52015-12-23 00:15:11 -08001103 // processed, the info will be extracted for the bucketAdd call to groupService
1104 GroupDescription l3ecmpGroupDesc =
Yi Tsengef19de12017-04-24 11:33:05 -07001105 new DefaultGroupDescription(deviceId,
1106 SELECT,
1107 new GroupBuckets(newBuckets),
1108 l3ecmpGroupKey,
1109 l3ecmpGroupId,
1110 nextObjective.appId());
Yi Tseng78f51f42017-02-02 13:54:58 -08001111 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
1112 unsentGroups.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001113 true,
1114 deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001115
Saurav Dasc88d4662017-05-15 15:34:25 -07001116 // update new bucket-chains
1117 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
1118 for (Deque<GroupKey> newBucketChain : allGroupKeys) {
1119 newBucketChain.addFirst(l3ecmpGroupKey);
1120 addedKeys.add(newBucketChain);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001121 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001122 updatePendingNextObjective(l3ecmpGroupKey,
1123 new OfdpaNextGroup(addedKeys, nextObjective));
Yi Tsengef19de12017-04-24 11:33:05 -07001124 log.debug("Adding to L3ECMP: device:{} gid:{} group key:{} nextId:{}",
Charles Chan188ebf52015-12-23 00:15:11 -08001125 deviceId, Integer.toHexString(l3ecmpGroupId),
1126 l3ecmpGroupKey, nextObjective.id());
Yi Tseng78f51f42017-02-02 13:54:58 -08001127
1128 unsentGroups.forEach(groupInfo -> {
1129 // send the innermost group
1130 log.debug("Sending innermost group {} in group chain on device {} ",
Saurav Dasc88d4662017-05-15 15:34:25 -07001131 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
1132 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001133 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3ecmpGce);
1134 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001135 });
Saurav Das1a129a02016-11-18 15:21:57 -08001136 }
Charles Chan188ebf52015-12-23 00:15:11 -08001137
Saurav Das1a129a02016-11-18 15:21:57 -08001138 private void addBucketToBroadcastGroup(NextObjective nextObj,
Yi Tsengef19de12017-04-24 11:33:05 -07001139 List<Deque<GroupKey>> allActiveKeys) {
1140 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001141 if (assignedVlan == null) {
1142 log.warn("VLAN ID required by broadcast next obj is missing. "
1143 + "Aborting add bucket to broadcast group for next:{} in dev:{}",
1144 nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001145 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001146 return;
1147 }
Saurav Das1a129a02016-11-18 15:21:57 -08001148 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Yi Tsengef19de12017-04-24 11:33:05 -07001149 IpPrefix ipDst = readIpDstFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001150 if (ipDst != null) {
1151 if (ipDst.isMulticast()) {
Yi Tsengef19de12017-04-24 11:33:05 -07001152 addBucketToL3MulticastGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001153 } else {
1154 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001155 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001156 }
1157 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001158 addBucketToL2FloodGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001159 }
1160 }
1161
1162 private void addBucketToL2FloodGroup(NextObjective nextObj,
1163 List<Deque<GroupKey>> allActiveKeys,
1164 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001165 VlanId assignedVlan) {
1166 Group l2FloodGroup = retrieveTopLevelGroup(allActiveKeys, nextObj.id());
1167
1168 if (l2FloodGroup == null) {
1169 log.warn("Can't find L2 flood group while adding bucket to it. NextObj = {}",
1170 nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001171 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001172 return;
1173 }
Saurav Das1a129a02016-11-18 15:21:57 -08001174
Yi Tseng78f51f42017-02-02 13:54:58 -08001175 GroupKey l2floodGroupKey = l2FloodGroup.appCookie();
1176 int l2floodGroupId = l2FloodGroup.id().id();
1177 List<GroupBucket> newBuckets = generateNextGroupBuckets(groupInfos, ALL);
1178
1179 GroupDescription l2FloodGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001180 new DefaultGroupDescription(deviceId,
1181 ALL,
1182 new GroupBuckets(newBuckets),
1183 l2floodGroupKey,
1184 l2floodGroupId,
1185 nextObj.appId());
Saurav Das1a129a02016-11-18 15:21:57 -08001186
Yi Tseng78f51f42017-02-02 13:54:58 -08001187 GroupChainElem l2FloodGroupChainElement =
1188 new GroupChainElem(l2FloodGroupDescription,
1189 groupInfos.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001190 true,
1191 deviceId);
Yi Tseng78f51f42017-02-02 13:54:58 -08001192
Yi Tseng78f51f42017-02-02 13:54:58 -08001193
1194 //ensure assignedVlan applies to the chosen group
1195 VlanId floodGroupVlan = extractVlanIdFromGroupId(l2floodGroupId);
1196
1197 if (!floodGroupVlan.equals(assignedVlan)) {
1198 log.warn("VLAN ID {} does not match Flood group {} to which bucket is "
1199 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1200 Integer.toHexString(l2floodGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001201 fail(nextObj, ObjectiveError.BADPARAMS);
Yi Tseng78f51f42017-02-02 13:54:58 -08001202 return;
1203 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001204 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
Yi Tseng78f51f42017-02-02 13:54:58 -08001205 groupInfos.forEach(groupInfo -> {
1206 // update original NextGroup with new bucket-chain
Yi Tseng78f51f42017-02-02 13:54:58 -08001207 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001208 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001209 newBucketChain.addFirst(l2floodGroupKey);
Saurav Dasc88d4662017-05-15 15:34:25 -07001210 addedKeys.add(newBucketChain);
Yi Tseng78f51f42017-02-02 13:54:58 -08001211
Yi Tsengef19de12017-04-24 11:33:05 -07001212 log.debug("Adding to L2FLOOD: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001213 deviceId, Integer.toHexString(l2floodGroupId),
1214 l2floodGroupKey, nextObj.id());
1215 // send the innermost group
1216 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001217 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001218 deviceId);
1219
Yi Tsengef19de12017-04-24 11:33:05 -07001220 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l2FloodGroupChainElement);
Yi Tseng78f51f42017-02-02 13:54:58 -08001221
Yi Tsengef19de12017-04-24 11:33:05 -07001222 DeviceId innerMostGroupDevice = groupInfo.innerMostGroupDesc().deviceId();
1223 GroupKey innerMostGroupKey = groupInfo.innerMostGroupDesc().appCookie();
Yi Tseng78f51f42017-02-02 13:54:58 -08001224 Group existsL2IGroup = groupService.getGroup(innerMostGroupDevice, innerMostGroupKey);
1225
1226 if (existsL2IGroup != null) {
1227 // group already exist
1228 processPendingAddGroupsOrNextObjs(innerMostGroupKey, true);
1229 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001230 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001231 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001232 });
Saurav Dasc88d4662017-05-15 15:34:25 -07001233
1234 updatePendingNextObjective(l2floodGroupKey,
1235 new OfdpaNextGroup(addedKeys, nextObj));
Yi Tseng78f51f42017-02-02 13:54:58 -08001236 }
1237
Saurav Das1a129a02016-11-18 15:21:57 -08001238 private void addBucketToL3MulticastGroup(NextObjective nextObj,
1239 List<Deque<GroupKey>> allActiveKeys,
1240 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001241 VlanId assignedVlan) {
1242 // create the buckets to add to the outermost L3 Multicast group
1243 List<GroupBucket> newBuckets = Lists.newArrayList();
1244 groupInfos.forEach(groupInfo -> {
1245 // Points to L3 interface group if there is one.
1246 // Otherwise points to L2 interface group directly.
Yi Tsengef19de12017-04-24 11:33:05 -07001247 GroupDescription nextGroupDesc = (groupInfo.nextGroupDesc() != null) ?
1248 groupInfo.nextGroupDesc() : groupInfo.innerMostGroupDesc();
1249 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1250 treatmentBuilder.group(new GroupId(nextGroupDesc.givenGroupId()));
1251 GroupBucket newBucket = DefaultGroupBucket.createAllGroupBucket(treatmentBuilder.build());
Yi Tseng78f51f42017-02-02 13:54:58 -08001252 newBuckets.add(newBucket);
1253 });
Saurav Das1a129a02016-11-18 15:21:57 -08001254
1255 // get the group being edited
1256 Group l3mcastGroup = retrieveTopLevelGroup(allActiveKeys, nextObj.id());
1257 if (l3mcastGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001258 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001259 return;
1260 }
1261 GroupKey l3mcastGroupKey = l3mcastGroup.appCookie();
1262 int l3mcastGroupId = l3mcastGroup.id().id();
1263
1264 //ensure assignedVlan applies to the chosen group
Yi Tseng78f51f42017-02-02 13:54:58 -08001265 VlanId expectedVlan = extractVlanIdFromGroupId(l3mcastGroupId);
Saurav Das1a129a02016-11-18 15:21:57 -08001266 if (!expectedVlan.equals(assignedVlan)) {
1267 log.warn("VLAN ID {} does not match L3 Mcast group {} to which bucket is "
1268 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1269 Integer.toHexString(l3mcastGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001270 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001271 }
1272 GroupDescription l3mcastGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001273 new DefaultGroupDescription(deviceId,
1274 ALL,
1275 new GroupBuckets(newBuckets),
1276 l3mcastGroupKey,
1277 l3mcastGroupId,
1278 nextObj.appId());
Saurav Das1a129a02016-11-18 15:21:57 -08001279 GroupChainElem l3mcastGce = new GroupChainElem(l3mcastGroupDescription,
Yi Tsengef19de12017-04-24 11:33:05 -07001280 groupInfos.size(),
1281 true,
1282 deviceId);
Saurav Dasc88d4662017-05-15 15:34:25 -07001283
1284 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
Yi Tseng78f51f42017-02-02 13:54:58 -08001285 groupInfos.forEach(groupInfo -> {
1286 // update original NextGroup with new bucket-chain
1287 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001288 newBucketChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001289 // Add L3 interface group to the chain if there is one.
Yi Tsengef19de12017-04-24 11:33:05 -07001290 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1291 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001292 }
1293 newBucketChain.addFirst(l3mcastGroupKey);
Saurav Dasc88d4662017-05-15 15:34:25 -07001294 addedKeys.add(newBucketChain);
Yi Tseng78f51f42017-02-02 13:54:58 -08001295
Yi Tsengef19de12017-04-24 11:33:05 -07001296 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3mcastGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001297 // Point next group to inner-most group, if any
Yi Tsengef19de12017-04-24 11:33:05 -07001298 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1299 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(),
1300 1,
1301 false,
1302 deviceId);
1303 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001304 }
Yi Tsengef19de12017-04-24 11:33:05 -07001305 log.debug("Adding to L3MCAST: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001306 deviceId, Integer.toHexString(l3mcastGroupId),
1307 l3mcastGroupKey, nextObj.id());
1308 // send the innermost group
1309 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001310 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001311 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001312 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001313
1314 });
1315
Saurav Das1a129a02016-11-18 15:21:57 -08001316 updatePendingNextObjective(l3mcastGroupKey,
Saurav Dasc88d4662017-05-15 15:34:25 -07001317 new OfdpaNextGroup(addedKeys, nextObj));
Charles Chan188ebf52015-12-23 00:15:11 -08001318 }
1319
1320 /**
Saurav Dasceccf242017-08-03 18:30:35 -07001321 * Removes buckets in the top level group of a possible group-chain. Does
Saurav Das1a129a02016-11-18 15:21:57 -08001322 * not remove the groups in the group-chain pointed to by this bucket, as they
Charles Chan188ebf52015-12-23 00:15:11 -08001323 * may be in use (referenced by other groups) elsewhere.
1324 *
Saurav Dasceccf242017-08-03 18:30:35 -07001325 * @param nextObjective a next objective that contains information for the
1326 * buckets to be removed from the group
1327 * @param next the representation of the existing group-chains for this next
1328 * objective, from which the top-level buckets to remove are determined
Charles Chan188ebf52015-12-23 00:15:11 -08001329 */
1330 protected void removeBucketFromGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001331 if (nextObjective.type() != NextObjective.Type.HASHED &&
1332 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001333 log.warn("RemoveBuckets not applied to nextType:{} in dev:{} for next:{}",
1334 nextObjective.type(), deviceId, nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -07001335 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001336 return;
1337 }
Yi Tsengef19de12017-04-24 11:33:05 -07001338 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Saurav Dasceccf242017-08-03 18:30:35 -07001339 List<Integer> indicesToRemove = Lists.newArrayList();
1340 for (TrafficTreatment treatment : nextObjective.next()) {
1341 // find the top-level bucket in the group-chain by matching the
1342 // outport and label from different groups in the chain
1343 PortNumber portToRemove = readOutPortFromTreatment(treatment);
1344 int labelToRemove = readLabelFromTreatment(treatment);
1345 if (portToRemove == null) {
1346 log.warn("treatment {} of next objective {} has no outport.. "
1347 + "cannot remove bucket from group in dev: {}", treatment,
1348 nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001349 continue;
1350 }
Saurav Dasceccf242017-08-03 18:30:35 -07001351 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1352 groupService, deviceId,
1353 portToRemove, labelToRemove);
1354 indicesToRemove.addAll(existing);
1355
Charles Chan188ebf52015-12-23 00:15:11 -08001356 }
Saurav Dasceccf242017-08-03 18:30:35 -07001357
1358 List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList();
1359 indicesToRemove.forEach(index -> chainsToRemove
1360 .add(allActiveKeys.get(index)));
Yi Tseng78f51f42017-02-02 13:54:58 -08001361 if (chainsToRemove.isEmpty()) {
Charles Chan188ebf52015-12-23 00:15:11 -08001362 log.warn("Could not find appropriate group-chain for removing bucket"
1363 + " for next id {} in dev:{}", nextObjective.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001364 fail(nextObjective, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001365 return;
Charles Chan188ebf52015-12-23 00:15:11 -08001366 }
Saurav Dasceccf242017-08-03 18:30:35 -07001367 removeBucket(chainsToRemove, nextObjective);
1368 }
1369
1370 /**
1371 * Removes top-level buckets from a group that represents the given next objective.
1372 *
1373 * @param chainsToRemove a list of group bucket chains to remove
1374 * @param nextObjective the next objective that contains information for the
1375 * buckets to be removed from the group
1376 */
1377 protected void removeBucket(List<Deque<GroupKey>> chainsToRemove,
1378 NextObjective nextObjective) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001379 List<GroupBucket> bucketsToRemove = Lists.newArrayList();
1380 //first group key is the one we want to modify
1381 GroupKey modGroupKey = chainsToRemove.get(0).peekFirst();
Saurav Das1a129a02016-11-18 15:21:57 -08001382 Group modGroup = groupService.getGroup(deviceId, modGroupKey);
Yi Tseng78f51f42017-02-02 13:54:58 -08001383 for (Deque<GroupKey> foundChain : chainsToRemove) {
1384 //second group key is the one we wish to remove the reference to
1385 if (foundChain.size() < 2) {
Saurav Dasceccf242017-08-03 18:30:35 -07001386 // additional check to make sure second group key exists in
Yi Tseng78f51f42017-02-02 13:54:58 -08001387 // the chain.
1388 log.warn("Can't find second group key from chain {}",
1389 foundChain);
1390 continue;
Saurav Das1a129a02016-11-18 15:21:57 -08001391 }
Saurav Dasceccf242017-08-03 18:30:35 -07001392 GroupKey pointedGroupKey = foundChain.stream()
1393 .collect(Collectors.toList()).get(1);
Yi Tseng78f51f42017-02-02 13:54:58 -08001394 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
Yi Tseng78f51f42017-02-02 13:54:58 -08001395 if (pointedGroup == null) {
1396 continue;
1397 }
1398
1399 GroupBucket bucket;
1400 if (nextObjective.type() == NextObjective.Type.HASHED) {
1401 bucket = DefaultGroupBucket.createSelectGroupBucket(
1402 DefaultTrafficTreatment.builder()
1403 .group(pointedGroup.id())
1404 .build());
1405 } else {
1406 bucket = DefaultGroupBucket.createAllGroupBucket(
1407 DefaultTrafficTreatment.builder()
1408 .group(pointedGroup.id())
1409 .build());
1410 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001411 bucketsToRemove.add(bucket);
Saurav Das1a129a02016-11-18 15:21:57 -08001412 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001413
1414 GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove);
1415 List<String> pointedGroupIds; // for debug log
1416 pointedGroupIds = bucketsToRemove.stream()
1417 .map(GroupBucket::treatment)
1418 .map(TrafficTreatment::allInstructions)
1419 .flatMap(List::stream)
1420 .filter(inst -> inst instanceof Instructions.GroupInstruction)
1421 .map(inst -> (Instructions.GroupInstruction) inst)
1422 .map(Instructions.GroupInstruction::groupId)
1423 .map(GroupId::id)
1424 .map(Integer::toHexString)
1425 .map(id -> HEX_PREFIX + id)
1426 .collect(Collectors.toList());
1427
Yi Tseng78f51f42017-02-02 13:54:58 -08001428 log.debug("Removing buckets from group id 0x{} pointing to group id(s) {} "
Saurav Das1a129a02016-11-18 15:21:57 -08001429 + "for next id {} in device {}", Integer.toHexString(modGroup.id().id()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001430 pointedGroupIds, nextObjective.id(), deviceId);
1431 addPendingUpdateNextObjective(modGroupKey, nextObjective);
Saurav Das1a129a02016-11-18 15:21:57 -08001432 groupService.removeBucketsFromGroup(deviceId, modGroupKey,
1433 removeBuckets, modGroupKey,
1434 nextObjective.appId());
Saurav Dasceccf242017-08-03 18:30:35 -07001435 // update store - synchronize access as there may be multiple threads
1436 // trying to remove buckets from the same group, each with its own
1437 // potentially stale copy of allActiveKeys
Saurav Dasc88d4662017-05-15 15:34:25 -07001438 synchronized (flowObjectiveStore) {
Saurav Dasceccf242017-08-03 18:30:35 -07001439 // get a fresh copy of what the store holds
1440 NextGroup next = flowObjectiveStore.getNextGroup(nextObjective.id());
1441 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Saurav Dasc88d4662017-05-15 15:34:25 -07001442 // Note that since we got a new object, and ArrayDeque does not implement
Saurav Dasceccf242017-08-03 18:30:35 -07001443 // Object.equals(), we have to check the deque elems one by one
1444 allActiveKeys
1445 .removeIf(active ->
1446 chainsToRemove.stream().anyMatch(remove ->
1447 Arrays.equals(remove.toArray(new GroupKey[0]),
1448 active.toArray(new GroupKey[0]))));
Saurav Dasc88d4662017-05-15 15:34:25 -07001449 // 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(),
Saurav Dasceccf242017-08-03 18:30:35 -07001457 new OfdpaNextGroup(allActiveKeys,
1458 nextObjective));
Saurav Das1a129a02016-11-18 15:21:57 -08001459 }
Charles Chan188ebf52015-12-23 00:15:11 -08001460 }
1461
1462 /**
Saurav Das961beb22017-03-29 19:09:17 -07001463 * Removes all groups in multiple possible group-chains that represent the next-obj.
Charles Chan188ebf52015-12-23 00:15:11 -08001464 *
1465 * @param nextObjective the next objective to remove
1466 * @param next the NextGroup that represents the existing group-chain for
1467 * this next objective
1468 */
1469 protected void removeGroup(NextObjective nextObjective, NextGroup next) {
Yi Tsengef19de12017-04-24 11:33:05 -07001470 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Charles Chanfc5c7802016-05-17 13:13:55 -07001471
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001472 List<GroupKey> groupKeys = allActiveKeys.stream()
Charles Chanfc5c7802016-05-17 13:13:55 -07001473 .map(Deque::getFirst).collect(Collectors.toList());
Yi Tsengef19de12017-04-24 11:33:05 -07001474 addPendingRemoveNextObjective(nextObjective, groupKeys);
Charles Chanfc5c7802016-05-17 13:13:55 -07001475
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001476 allActiveKeys.forEach(groupChain -> groupChain.forEach(groupKey ->
Charles Chan188ebf52015-12-23 00:15:11 -08001477 groupService.removeGroup(deviceId, groupKey, nextObjective.appId())));
1478 flowObjectiveStore.removeNextGroup(nextObjective.id());
1479 }
1480
Saurav Dasceccf242017-08-03 18:30:35 -07001481 /**
1482 * Checks existing buckets in {@link NextGroup} to verify if they match
1483 * the buckets in the given {@link NextObjective}. Adds or removes buckets
1484 * to ensure that the buckets match up.
1485 *
1486 * @param nextObjective the next objective to verify
1487 * @param next the representation of the existing group which has to be
1488 * modified to match the given next objective
1489 */
1490 protected void verifyGroup(NextObjective nextObjective, NextGroup next) {
1491 if (nextObjective.type() != NextObjective.Type.HASHED) {
1492 log.warn("verification not supported for {} group", nextObjective.type());
1493 fail(nextObjective, ObjectiveError.UNSUPPORTED);
1494 return;
1495 }
1496 log.debug("Call to verify device:{} nextId:{}", deviceId, nextObjective.id());
1497 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
1498 List<TrafficTreatment> bucketsToCreate = Lists.newArrayList();
1499 List<Integer> indicesToRemove = Lists.newArrayList();
1500 // XXX verify empty group
1501 for (TrafficTreatment bkt : nextObjective.next()) {
1502 PortNumber portNumber = readOutPortFromTreatment(bkt);
1503 int label = readLabelFromTreatment(bkt);
1504 if (portNumber == null) {
1505 log.warn("treatment {} of next objective {} has no outport.. "
1506 + "cannot remove bucket from group in dev: {}", bkt,
1507 nextObjective.id(), deviceId);
1508 fail(nextObjective, ObjectiveError.BADPARAMS);
1509 return;
1510 }
1511 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1512 groupService, deviceId,
1513 portNumber, label);
1514 if (existing.isEmpty()) {
1515 // if it doesn't exist, mark this bucket for creation
1516 bucketsToCreate.add(bkt);
1517 }
1518 if (existing.size() > 1) {
1519 // if it exists but there are duplicates, mark the others for removal
1520 existing.remove(0);
1521 indicesToRemove.addAll(existing);
1522 }
1523 }
1524
1525 if (!bucketsToCreate.isEmpty()) {
1526 log.info("creating {} buckets as part of nextId: {} verification",
1527 bucketsToCreate.size(), nextObjective.id());
1528 //create a nextObjective only with these buckets
1529 NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder()
1530 .withId(nextObjective.id())
1531 .withType(NextObjective.Type.HASHED)
1532 .withMeta(nextObjective.meta())
1533 .fromApp(nextObjective.appId());
1534 bucketsToCreate.forEach(bucket -> nextObjBuilder.addTreatment(bucket));
1535 addBucketToHashGroup(nextObjBuilder.addToExisting(), allActiveKeys);
1536 }
1537
1538 if (!indicesToRemove.isEmpty()) {
1539 log.info("removing {} buckets as part of nextId: {} verification",
1540 indicesToRemove.size(), nextObjective.id());
1541 List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList();
1542 indicesToRemove.forEach(index -> chainsToRemove
1543 .add(allActiveKeys.get(index)));
1544 removeBucket(chainsToRemove, nextObjective);
1545 }
1546
1547 pass(nextObjective);
1548 }
1549
1550 //////////////////////////////////////
1551 // Helper methods and classes
1552 //////////////////////////////////////
1553
Yi Tsengef19de12017-04-24 11:33:05 -07001554 protected void updatePendingNextObjective(GroupKey groupKey, OfdpaNextGroup nextGrp) {
1555 pendingAddNextObjectives.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07001556 if (val == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001557 val = new CopyOnWriteArrayList<>();
Saurav Das961beb22017-03-29 19:09:17 -07001558 }
Yi Tsengef19de12017-04-24 11:33:05 -07001559 val.add(nextGrp);
Saurav Das961beb22017-03-29 19:09:17 -07001560 return val;
1561 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08001562 }
1563
Yi Tsengef19de12017-04-24 11:33:05 -07001564 protected void updatePendingGroups(GroupKey groupKey, GroupChainElem gce) {
1565 pendingGroups.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07001566 if (val == null) {
1567 val = Sets.newConcurrentHashSet();
1568 }
1569 val.add(gce);
1570 return val;
1571 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08001572 }
1573
Yi Tsengef19de12017-04-24 11:33:05 -07001574 protected void addPendingUpdateNextObjective(GroupKey groupKey,
1575 NextObjective nextObjective) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001576 pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> {
1577 if (nextObjs != null) {
1578 nextObjs.add(nextObjective);
1579 } else {
1580 nextObjs = Sets.newHashSet(nextObjective);
1581 }
1582 return nextObjs;
1583 });
1584 }
1585
Yi Tsengef19de12017-04-24 11:33:05 -07001586 private void processPendingUpdateNextObjs(GroupKey groupKey) {
1587 pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> {
1588 if (nextObjs != null) {
1589
1590 nextObjs.forEach(nextObj -> {
1591 log.debug("Group {} updated, update pending next objective {}.",
1592 groupKey, nextObj);
1593
1594 pass(nextObj);
1595 });
1596 }
1597 return Sets.newHashSet();
1598 });
1599 }
1600
1601 private void processPendingRemoveNextObjs(GroupKey key) {
1602 pendingRemoveNextObjectives.asMap().forEach((nextObjective, groupKeys) -> {
1603 if (groupKeys.isEmpty()) {
1604 pendingRemoveNextObjectives.invalidate(nextObjective);
1605 pass(nextObjective);
1606 } else {
1607 groupKeys.remove(key);
1608 }
1609 });
1610 }
1611
1612 protected int getNextAvailableIndex() {
1613 return (int) nextIndex.incrementAndGet();
1614 }
1615
1616 protected Group retrieveTopLevelGroup(List<Deque<GroupKey>> allActiveKeys,
1617 int nextid) {
1618 GroupKey topLevelGroupKey;
1619 if (!allActiveKeys.isEmpty()) {
1620 topLevelGroupKey = allActiveKeys.get(0).peekFirst();
1621 } else {
1622 log.warn("Could not determine top level group while processing"
1623 + "next:{} in dev:{}", nextid, deviceId);
1624 return null;
1625 }
1626 Group topGroup = groupService.getGroup(deviceId, topLevelGroupKey);
1627 if (topGroup == null) {
1628 log.warn("Could not find top level group while processing "
1629 + "next:{} in dev:{}", nextid, deviceId);
1630 }
1631 return topGroup;
1632 }
1633
1634 protected void processPendingAddGroupsOrNextObjs(GroupKey key, boolean added) {
1635 //first check for group chain
1636 Set<OfdpaGroupHandlerUtility.GroupChainElem> gceSet = pendingGroups.asMap().remove(key);
1637 if (gceSet != null) {
1638 for (GroupChainElem gce : gceSet) {
1639 log.debug("Group service {} group key {} in device {}. "
1640 + "Processing next group in group chain with group id 0x{}",
1641 (added) ? "ADDED" : "processed",
1642 key, deviceId,
1643 Integer.toHexString(gce.groupDescription().givenGroupId()));
1644 processGroupChain(gce);
1645 }
1646 } else {
1647 // otherwise chain complete - check for waiting nextObjectives
1648 List<OfdpaGroupHandlerUtility.OfdpaNextGroup> nextGrpList =
1649 pendingAddNextObjectives.getIfPresent(key);
1650 if (nextGrpList != null) {
1651 pendingAddNextObjectives.invalidate(key);
1652 nextGrpList.forEach(nextGrp -> {
1653 log.debug("Group service {} group key {} in device:{}. "
1654 + "Done implementing next objective: {} <<-->> gid:0x{}",
1655 (added) ? "ADDED" : "processed",
1656 key, deviceId, nextGrp.nextObjective().id(),
1657 Integer.toHexString(groupService.getGroup(deviceId, key)
1658 .givenGroupId()));
1659 pass(nextGrp.nextObjective());
Saurav Dasc88d4662017-05-15 15:34:25 -07001660 updateFlowObjectiveStore(nextGrp.nextObjective().id(), nextGrp);
Yi Tsengef19de12017-04-24 11:33:05 -07001661
1662 // check if addBuckets waiting for this completion
1663 pendingBuckets.compute(nextGrp.nextObjective().id(), (nextId, pendBkts) -> {
1664 if (pendBkts != null) {
1665 pendBkts.forEach(pendBkt -> addBucketToGroup(pendBkt, nextGrp));
1666 }
1667 return null;
1668 });
1669 });
1670 }
1671 }
1672 }
1673
Charles Chan188ebf52015-12-23 00:15:11 -08001674 /**
1675 * Processes next element of a group chain. Assumption is that if this
1676 * group points to another group, the latter has already been created
1677 * and this driver has received notification for it. A second assumption is
1678 * that if there is another group waiting for this group then the appropriate
1679 * stores already have the information to act upon the notification for the
1680 * creation of this group.
1681 * <p>
1682 * The processing of the GroupChainElement depends on the number of groups
1683 * this element is waiting on. For all group types other than SIMPLE, a
1684 * GroupChainElement could be waiting on multiple groups.
1685 *
1686 * @param gce the group chain element to be processed next
1687 */
1688 private void processGroupChain(GroupChainElem gce) {
1689 int waitOnGroups = gce.decrementAndGetGroupsWaitedOn();
1690 if (waitOnGroups != 0) {
1691 log.debug("GCE: {} not ready to be processed", gce);
1692 return;
1693 }
1694 log.debug("GCE: {} ready to be processed", gce);
Yi Tsengef19de12017-04-24 11:33:05 -07001695 if (gce.addBucketToGroup()) {
1696 groupService.addBucketsToGroup(gce.groupDescription().deviceId(),
1697 gce.groupDescription().appCookie(),
1698 gce.groupDescription().buckets(),
1699 gce.groupDescription().appCookie(),
1700 gce.groupDescription().appId());
Charles Chan188ebf52015-12-23 00:15:11 -08001701 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001702 groupService.addGroup(gce.groupDescription());
Charles Chan188ebf52015-12-23 00:15:11 -08001703 }
1704 }
1705
Yi Tsengef19de12017-04-24 11:33:05 -07001706 protected void addPendingRemoveNextObjective(NextObjective nextObjective,
1707 List<GroupKey> groupKeys) {
1708 pendingRemoveNextObjectives.put(nextObjective, groupKeys);
Charles Chan188ebf52015-12-23 00:15:11 -08001709 }
1710
Saurav Dasc88d4662017-05-15 15:34:25 -07001711 private void updateFlowObjectiveStore(Integer nextId, OfdpaNextGroup nextGrp) {
1712 synchronized (flowObjectiveStore) {
1713 // get fresh copy of what the store holds
1714 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1715 if (next == null || nextGrp.nextObjective().op() == Operation.ADD) {
1716 flowObjectiveStore.putNextGroup(nextId, nextGrp);
1717 return;
1718 }
1719 if (nextGrp.nextObjective().op() == Operation.ADD_TO_EXISTING) {
1720 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
1721 // If active keys shows only the top-level group without a chain of groups,
1722 // then it represents an empty group. Update by replacing empty chain.
1723 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1724 allActiveKeys.clear();
1725 }
1726 allActiveKeys.addAll(nextGrp.allKeys());
1727 flowObjectiveStore.putNextGroup(nextId,
1728 new OfdpaNextGroup(allActiveKeys, nextGrp.nextObjective()));
1729 }
1730 }
1731 }
1732
Saurav Das8be4e3a2016-03-11 17:19:07 -08001733 private class InnerGroupListener implements GroupListener {
1734 @Override
1735 public void event(GroupEvent event) {
1736 log.trace("received group event of type {}", event.type());
Charles Chanfc5c7802016-05-17 13:13:55 -07001737 switch (event.type()) {
1738 case GROUP_ADDED:
1739 processPendingAddGroupsOrNextObjs(event.subject().appCookie(), true);
1740 break;
1741 case GROUP_REMOVED:
1742 processPendingRemoveNextObjs(event.subject().appCookie());
1743 break;
Yi Tseng78f51f42017-02-02 13:54:58 -08001744 case GROUP_UPDATED:
1745 processPendingUpdateNextObjs(event.subject().appCookie());
1746 break;
Charles Chanfc5c7802016-05-17 13:13:55 -07001747 default:
1748 break;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001749 }
1750 }
1751 }
Charles Chan188ebf52015-12-23 00:15:11 -08001752}