blob: 2521daf939edc0a737ef2c7338a77bfe2328dc9a [file] [log] [blame]
Brian O'Connor7cbbbb72016-04-09 02:13:23 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Brian O'Connor7cbbbb72016-04-09 02:13:23 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Yi Tsengef19de12017-04-24 11:33:05 -070016package org.onosproject.driver.pipeline.ofdpa;
Charles Chan188ebf52015-12-23 00:15:11 -080017
18import com.google.common.cache.Cache;
19import com.google.common.cache.CacheBuilder;
20import com.google.common.cache.RemovalCause;
21import com.google.common.cache.RemovalNotification;
Charles Chan5b9df8d2016-03-28 22:21:40 -070022import com.google.common.collect.ImmutableList;
23import com.google.common.collect.Lists;
Yi Tseng78f51f42017-02-02 13:54:58 -080024import com.google.common.collect.Sets;
Charles Chan188ebf52015-12-23 00:15:11 -080025import org.onlab.osgi.ServiceDirectory;
Charles Chan5b9df8d2016-03-28 22:21:40 -070026import org.onlab.packet.IpPrefix;
Charles Chan5270ed02016-01-30 23:22:37 -080027import org.onlab.packet.MacAddress;
Charles Chan188ebf52015-12-23 00:15:11 -080028import org.onlab.packet.MplsLabel;
29import org.onlab.packet.VlanId;
30import org.onosproject.core.ApplicationId;
Yi Tseng78f51f42017-02-02 13:54:58 -080031import org.onosproject.core.GroupId;
Charles Chan32562522016-04-07 14:37:14 -070032import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Charles Chan188ebf52015-12-23 00:15:11 -080033import org.onosproject.net.DeviceId;
34import org.onosproject.net.PortNumber;
35import org.onosproject.net.behaviour.NextGroup;
36import org.onosproject.net.behaviour.PipelinerContext;
37import org.onosproject.net.flow.DefaultTrafficTreatment;
38import org.onosproject.net.flow.TrafficSelector;
39import org.onosproject.net.flow.TrafficTreatment;
40import org.onosproject.net.flow.criteria.Criterion;
Pier Ventre42287df2016-11-09 14:17:26 -080041import org.onosproject.net.flow.criteria.TunnelIdCriterion;
Charles Chan188ebf52015-12-23 00:15:11 -080042import org.onosproject.net.flow.criteria.VlanIdCriterion;
43import org.onosproject.net.flow.instructions.Instruction;
44import org.onosproject.net.flow.instructions.Instructions;
Saurav Das9df5b7c2017-08-14 16:44:43 -070045import org.onosproject.net.flow.instructions.Instructions.GroupInstruction;
Charles Chan188ebf52015-12-23 00:15:11 -080046import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Yi Tseng47f82dc2017-03-05 22:48:39 -080047import org.onosproject.net.flowobjective.DefaultNextObjective;
Charles Chan188ebf52015-12-23 00:15:11 -080048import org.onosproject.net.flowobjective.FlowObjectiveStore;
49import org.onosproject.net.flowobjective.NextObjective;
Saurav Dasc88d4662017-05-15 15:34:25 -070050import org.onosproject.net.flowobjective.Objective.Operation;
Yi Tseng47f82dc2017-03-05 22:48:39 -080051import org.onosproject.net.flowobjective.ObjectiveContext;
Charles Chan188ebf52015-12-23 00:15:11 -080052import org.onosproject.net.flowobjective.ObjectiveError;
53import org.onosproject.net.group.DefaultGroupBucket;
54import org.onosproject.net.group.DefaultGroupDescription;
55import org.onosproject.net.group.DefaultGroupKey;
56import org.onosproject.net.group.Group;
57import org.onosproject.net.group.GroupBucket;
58import org.onosproject.net.group.GroupBuckets;
59import org.onosproject.net.group.GroupDescription;
60import org.onosproject.net.group.GroupEvent;
61import org.onosproject.net.group.GroupKey;
62import org.onosproject.net.group.GroupListener;
63import org.onosproject.net.group.GroupService;
Saurav Das8be4e3a2016-03-11 17:19:07 -080064import org.onosproject.store.service.AtomicCounter;
65import org.onosproject.store.service.StorageService;
Charles Chan188ebf52015-12-23 00:15:11 -080066import org.slf4j.Logger;
67
68import java.util.ArrayDeque;
69import java.util.ArrayList;
Saurav Dasceccf242017-08-03 18:30:35 -070070import java.util.Arrays;
Charles Chan188ebf52015-12-23 00:15:11 -080071import java.util.Collection;
72import java.util.Collections;
73import java.util.Deque;
74import java.util.List;
Charles Chand0fd5dc2016-02-16 23:14:49 -080075import java.util.Objects;
Charles Chan188ebf52015-12-23 00:15:11 -080076import java.util.Set;
77import java.util.concurrent.ConcurrentHashMap;
78import java.util.concurrent.CopyOnWriteArrayList;
79import java.util.concurrent.Executors;
80import java.util.concurrent.ScheduledExecutorService;
81import java.util.concurrent.TimeUnit;
Charles Chan188ebf52015-12-23 00:15:11 -080082import java.util.stream.Collectors;
83
84import static org.onlab.util.Tools.groupedThreads;
Yi Tsengef19de12017-04-24 11:33:05 -070085import static org.onosproject.driver.pipeline.ofdpa.Ofdpa2Pipeline.*;
86import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
jayakumarthazhath655b9a82018-10-01 00:51:54 +053087import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.L2_MULTICAST_TYPE;
88import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.l2MulticastGroupKey;
Pier Ventre42287df2016-11-09 14:17:26 -080089import static org.onosproject.net.flow.criteria.Criterion.Type.TUNNEL_ID;
Pier Ventre140a8942016-11-02 07:26:38 -070090import static org.onosproject.net.flow.criteria.Criterion.Type.VLAN_VID;
Yi Tseng78f51f42017-02-02 13:54:58 -080091import static org.onosproject.net.group.GroupDescription.Type.ALL;
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -080092import static org.onosproject.net.group.GroupDescription.Type.INDIRECT;
Yi Tseng78f51f42017-02-02 13:54:58 -080093import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Charles Chan188ebf52015-12-23 00:15:11 -080094import static org.slf4j.LoggerFactory.getLogger;
95
96/**
Saurav Das961beb22017-03-29 19:09:17 -070097 * Group handler that emulates Broadcom OF-DPA TTP.
Charles Chan188ebf52015-12-23 00:15:11 -080098 */
Charles Chan361154b2016-03-24 10:23:39 -070099public class Ofdpa2GroupHandler {
Yi Tsengef19de12017-04-24 11:33:05 -0700100 protected final Logger log = getLogger(getClass());
Yi Tsengef19de12017-04-24 11:33:05 -0700101 // Services, Stores
Charles Chan188ebf52015-12-23 00:15:11 -0800102 protected GroupService groupService;
Saurav Das8be4e3a2016-03-11 17:19:07 -0800103 protected StorageService storageService;
Yi Tsengef19de12017-04-24 11:33:05 -0700104 protected FlowObjectiveStore flowObjectiveStore;
Charles Chan188ebf52015-12-23 00:15:11 -0800105 // index number for group creation
Saurav Das8be4e3a2016-03-11 17:19:07 -0800106 private AtomicCounter nextIndex;
Yi Tsengef19de12017-04-24 11:33:05 -0700107 protected DeviceId deviceId;
Saurav Das2f2c9d02018-04-07 16:51:09 -0700108 Cache<GroupKey, List<OfdpaGroupHandlerUtility.OfdpaNextGroup>> pendingAddNextObjectives;
109 Cache<NextObjective, List<GroupKey>> pendingRemoveNextObjectives;
110 Cache<GroupKey, Set<OfdpaGroupHandlerUtility.GroupChainElem>> pendingGroups;
111 ConcurrentHashMap<GroupKey, Set<NextObjective>> pendingUpdateNextObjectives;
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<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700116 private ScheduledExecutorService groupCheckerExecutor =
117 Executors.newScheduledThreadPool(2, groupedThreads("onos/pipeliner", "ofdpa-%d", log));
Charles Chan40132b32017-01-22 00:19:37 -0800118 /**
119 * Determines whether this pipeline support copy ttl instructions or not.
120 *
121 * @return true if copy ttl instructions are supported
122 */
123 protected boolean supportCopyTtl() {
124 return true;
125 }
126
127 /**
128 * Determines whether this pipeline support set mpls bos instruction or not.
129 *
130 * @return true if set mpls bos instruction is supported
131 */
132 protected boolean supportSetMplsBos() {
133 return true;
134 }
135
Charles Chan0f43e472017-02-14 14:00:16 -0800136 /**
137 * Determines whether this pipeline requires popping VLAN before pushing MPLS.
138 * <p>
139 * If required, pop vlan before push mpls and add an arbitrary vlan back afterward.
140 * MPLS interface group will substitute the arbitrary VLAN with expected VLAN later on.
141 *
142 * @return true if this pipeline requires popping VLAN before pushing MPLS
143 */
144 protected boolean requireVlanPopBeforeMplsPush() {
145 return false;
146 }
147
Alex Yashchuk4caa8e82017-12-08 17:40:05 +0200148 public void init(DeviceId deviceId, PipelinerContext context) {
Yi Tsengef19de12017-04-24 11:33:05 -0700149 ServiceDirectory serviceDirectory = context.directory();
Charles Chan188ebf52015-12-23 00:15:11 -0800150 this.deviceId = deviceId;
151 this.flowObjectiveStore = context.store();
Charles Chan188ebf52015-12-23 00:15:11 -0800152 this.groupService = serviceDirectory.get(GroupService.class);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800153 this.storageService = serviceDirectory.get(StorageService.class);
Madan Jampanid5714e02016-04-19 14:15:20 -0700154 this.nextIndex = storageService.getAtomicCounter("group-id-index-counter");
Charles Chan188ebf52015-12-23 00:15:11 -0800155
Charles Chanfc5c7802016-05-17 13:13:55 -0700156 pendingAddNextObjectives = CacheBuilder.newBuilder()
Charles Chan188ebf52015-12-23 00:15:11 -0800157 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700158 .removalListener((RemovalNotification<GroupKey, List<OfdpaNextGroup>> notification) -> {
159 if (notification.getCause() == RemovalCause.EXPIRED &&
160 Objects.nonNull(notification.getValue())) {
161 notification.getValue()
162 .forEach(ofdpaNextGrp ->
163 fail(ofdpaNextGrp.nextObjective(),
164 ObjectiveError.GROUPINSTALLATIONFAILED));
Charles Chanfc5c7802016-05-17 13:13:55 -0700165 }
166 }).build();
Charles Chan188ebf52015-12-23 00:15:11 -0800167
Charles Chanfc5c7802016-05-17 13:13:55 -0700168 pendingRemoveNextObjectives = CacheBuilder.newBuilder()
169 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700170 .removalListener((RemovalNotification<NextObjective, List<GroupKey>> notification) -> {
Charles Chanfc5c7802016-05-17 13:13:55 -0700171 if (notification.getCause() == RemovalCause.EXPIRED) {
Yi Tsengef19de12017-04-24 11:33:05 -0700172 fail(notification.getKey(),
173 ObjectiveError.GROUPREMOVALFAILED);
Charles Chan188ebf52015-12-23 00:15:11 -0800174 }
175 }).build();
Saurav Das961beb22017-03-29 19:09:17 -0700176 pendingGroups = CacheBuilder.newBuilder()
177 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700178 .removalListener((RemovalNotification<GroupKey, Set<GroupChainElem>> notification) -> {
Saurav Das961beb22017-03-29 19:09:17 -0700179 if (notification.getCause() == RemovalCause.EXPIRED) {
180 log.error("Unable to install group with key {} and pending GCEs: {}",
181 notification.getKey(), notification.getValue());
182 }
183 }).build();
Yi Tseng78f51f42017-02-02 13:54:58 -0800184 pendingUpdateNextObjectives = new ConcurrentHashMap<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700185 GroupChecker groupChecker = new GroupChecker(this);
186 groupCheckerExecutor.scheduleAtFixedRate(groupChecker, 0, 500, TimeUnit.MILLISECONDS);
Charles Chan188ebf52015-12-23 00:15:11 -0800187 groupService.addListener(new InnerGroupListener());
188 }
189
Saurav Das8be4e3a2016-03-11 17:19:07 -0800190 //////////////////////////////////////
191 // Group Creation
192 //////////////////////////////////////
193
Yi Tsengef19de12017-04-24 11:33:05 -0700194 /**
195 * Adds a list of group chain by given NextObjective.
196 *
197 * @param nextObjective the NextObjective
198 */
Charles Chan188ebf52015-12-23 00:15:11 -0800199 protected void addGroup(NextObjective nextObjective) {
200 switch (nextObjective.type()) {
201 case SIMPLE:
202 Collection<TrafficTreatment> treatments = nextObjective.next();
203 if (treatments.size() != 1) {
204 log.error("Next Objectives of type Simple should only have a "
205 + "single Traffic Treatment. Next Objective Id:{}",
206 nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -0700207 fail(nextObjective, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800208 return;
209 }
210 processSimpleNextObjective(nextObjective);
211 break;
212 case BROADCAST:
213 processBroadcastNextObjective(nextObjective);
214 break;
215 case HASHED:
Pier Ventre140a8942016-11-02 07:26:38 -0700216 if (!verifyHashedNextObjective(nextObjective)) {
217 log.error("Next Objectives of type hashed not supported. Next Objective Id:{}",
218 nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -0700219 fail(nextObjective, ObjectiveError.BADPARAMS);
Pier Ventre140a8942016-11-02 07:26:38 -0700220 return;
221 }
Charles Chan188ebf52015-12-23 00:15:11 -0800222 processHashedNextObjective(nextObjective);
223 break;
224 case FAILOVER:
Yi Tsengef19de12017-04-24 11:33:05 -0700225 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -0800226 log.warn("Unsupported next objective type {}", nextObjective.type());
227 break;
228 default:
Yi Tsengef19de12017-04-24 11:33:05 -0700229 fail(nextObjective, ObjectiveError.UNKNOWN);
Charles Chan188ebf52015-12-23 00:15:11 -0800230 log.warn("Unknown next objective type {}", nextObjective.type());
231 }
232 }
233
234 /**
jayakumarthazhath655b9a82018-10-01 00:51:54 +0530235 * Similar to processBroadcastNextObjective but handles L2 Multicast Next Objectives.
236 *
237 * @param nextObj NextObjective of L2_MULTICAST with chained NextObjectives for single homed access ports
238 */
239 private void processL2MulticastNextObjective(NextObjective nextObj) {
240
241 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
242 if (assignedVlan == null) {
243 log.warn("VLAN ID required by L2 multicast next objective is missing. Aborting group creation.");
244 fail(nextObj, ObjectiveError.BADPARAMS);
245 return;
246 }
247
248 // Group info should contain only single homed hosts for a given vlanId
249 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
250 createL2MulticastGroup(nextObj, assignedVlan, groupInfos);
251 }
252
253 private void createL2MulticastGroup(NextObjective nextObj, VlanId vlanId, List<GroupInfo> groupInfos) {
254 // Realize & represent L2 multicast group in OFDPA driver layer
255 // TODO : Need to identify significance of OfdpaNextGroup.
256 Integer l2MulticastGroupId = L2_MULTICAST_TYPE | (vlanId.toShort() << 16);
257 final GroupKey l2MulticastGroupKey = l2MulticastGroupKey(vlanId, deviceId);
258 List<Deque<GroupKey>> l2MulticastAllGroup = Lists.newArrayList();
259 groupInfos.forEach(groupInfo -> {
260 Deque<GroupKey> groupKeyChain = new ArrayDeque<>();
261 groupKeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
262 groupKeyChain.addFirst(l2MulticastGroupKey);
263 l2MulticastAllGroup.add(groupKeyChain);
264 });
265 OfdpaNextGroup ofdpaL2MulticastGroup = new OfdpaNextGroup(l2MulticastAllGroup, nextObj);
266 updatePendingNextObjective(l2MulticastGroupKey, ofdpaL2MulticastGroup);
267 // Group Chain Hierarchy creation using group service and thus in device level
268 List<GroupBucket> l2McastBuckets = new ArrayList<>();
269 groupInfos.forEach(groupInfo -> {
270 // Points to L2 interface group directly.
271 TrafficTreatment.Builder trafficTreatment = DefaultTrafficTreatment.builder();
272 trafficTreatment.group(new GroupId(groupInfo.innerMostGroupDesc().givenGroupId()));
273 GroupBucket bucket = DefaultGroupBucket.createAllGroupBucket(trafficTreatment.build());
274 l2McastBuckets.add(bucket);
275 });
276 GroupDescription l2MulticastGroupDescription =
277 new DefaultGroupDescription(
278 deviceId,
279 ALL,
280 new GroupBuckets(l2McastBuckets),
281 l2MulticastGroupKey,
282 l2MulticastGroupId,
283 nextObj.appId());
284 GroupChainElem l2MulticastGce = new GroupChainElem(l2MulticastGroupDescription,
285 groupInfos.size(), false, deviceId);
286 groupInfos.forEach(groupInfo -> {
287 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), l2MulticastGce);
288 groupService.addGroup(groupInfo.innerMostGroupDesc());
289 });
290 }
291
292 /**
Charles Chan188ebf52015-12-23 00:15:11 -0800293 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
Saurav Dasa4020382018-02-14 14:14:54 -0800294 * a chain of groups. The simple Next Objective passed in by the application
295 * is broken up into a group chain. The following chains can be created
296 * depending on the parameters in the Next Objective.
297 * 1. L2 Interface group (no chaining)
298 * 2. L3 Unicast group -> L2 Interface group
299 * 3. MPLS Interface group -> L2 Interface group
300 * 4. MPLS Swap group -> MPLS Interface group -> L2 Interface group
301 * 5. PW initiation group chain
Charles Chan188ebf52015-12-23 00:15:11 -0800302 *
303 * @param nextObj the nextObjective of type SIMPLE
304 */
305 private void processSimpleNextObjective(NextObjective nextObj) {
306 TrafficTreatment treatment = nextObj.next().iterator().next();
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800307 // determine if plain L2 or L3->L2 or MPLS Swap -> MPLS Interface -> L2
Charles Chan188ebf52015-12-23 00:15:11 -0800308 boolean plainL2 = true;
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800309 boolean mplsSwap = false;
310 MplsLabel mplsLabel = null;
Charles Chan188ebf52015-12-23 00:15:11 -0800311 for (Instruction ins : treatment.allInstructions()) {
312 if (ins.type() == Instruction.Type.L2MODIFICATION) {
313 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
314 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_DST ||
315 l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_SRC) {
316 plainL2 = false;
Charles Chan188ebf52015-12-23 00:15:11 -0800317 }
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800318 // mpls label in simple next objectives is used only to indicate
319 // a MPLS Swap group before the MPLS Interface Group
320 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) {
321 mplsSwap = true;
322 mplsLabel = ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label();
323 }
Charles Chan188ebf52015-12-23 00:15:11 -0800324 }
325 }
Charles Chan188ebf52015-12-23 00:15:11 -0800326 if (plainL2) {
327 createL2InterfaceGroup(nextObj);
328 return;
329 }
Saurav Dasa4020382018-02-14 14:14:54 -0800330 // In order to understand if it is a pseudowire related
Pier Ventre42287df2016-11-09 14:17:26 -0800331 // next objective we look for the tunnel id in the meta.
332 boolean isPw = false;
Pier Ventre140a8942016-11-02 07:26:38 -0700333 if (nextObj.meta() != null) {
Pier Ventre42287df2016-11-09 14:17:26 -0800334 TunnelIdCriterion tunnelIdCriterion = (TunnelIdCriterion) nextObj
335 .meta()
336 .getCriterion(TUNNEL_ID);
337 if (tunnelIdCriterion != null) {
338 isPw = true;
339 }
Pier Ventre140a8942016-11-02 07:26:38 -0700340 }
Andreas Pantelopoulos25db42f2018-02-08 12:42:06 -0800341 if (mplsSwap && !isPw) {
Saurav Dasa4020382018-02-14 14:14:54 -0800342 log.debug("Creating a MPLS Swap -> MPLS Interface -> L2 Interface group chain.");
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800343 // break up simple next objective to GroupChain objects
344 GroupInfo groupInfo = createL2L3Chain(treatment, nextObj.id(),
345 nextObj.appId(), true,
346 nextObj.meta());
347 if (groupInfo == null) {
348 log.error("Could not process nextObj={} in dev:{}", nextObj.id(), deviceId);
349 fail(nextObj, ObjectiveError.BADPARAMS);
350 return;
351 }
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800352 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Saurav Dasa4020382018-02-14 14:14:54 -0800353 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie()); // l2 interface
354 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie()); // mpls interface
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800355 // creating the mpls swap group and adding it to the chain
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800356 int nextGid = groupInfo.nextGroupDesc().givenGroupId();
357 int index = getNextAvailableIndex();
Saurav Dasa4020382018-02-14 14:14:54 -0800358 GroupDescription swapGroupDescription = createMplsSwap(
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800359 nextGid,
360 OfdpaMplsGroupSubType.MPLS_SWAP_LABEL,
361 index,
362 mplsLabel,
363 nextObj.appId()
364 );
Saurav Dasa4020382018-02-14 14:14:54 -0800365 // ensure swap group is added after L2L3 chain
366 GroupKey swapGroupKey = swapGroupDescription.appCookie();
367 GroupChainElem swapChainElem = new GroupChainElem(swapGroupDescription,
368 1, false, deviceId);
369 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), swapChainElem);
370 gkeyChain.addFirst(swapGroupKey);
Saurav Dasa4020382018-02-14 14:14:54 -0800371 // ensure nextObjective waits on the outermost groupKey
372 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
373 allGroupKeys.add(gkeyChain);
374 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
375 updatePendingNextObjective(swapGroupKey, ofdpaGrp);
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800376 // now we are ready to send the l2 groupDescription (inner), as all the stores
377 // that will get async replies have been updated. By waiting to update
378 // the stores, we prevent nasty race conditions.
379 groupService.addGroup(groupInfo.innerMostGroupDesc());
380 } else if (!isPw) {
Saurav Dasa4020382018-02-14 14:14:54 -0800381 boolean isMpls = false;
382 if (nextObj.meta() != null) {
383 isMpls = isNotMplsBos(nextObj.meta());
384 }
385 log.debug("Creating a {} -> L2 Interface group chain.",
386 (isMpls) ? "MPLS Interface" : "L3 Unicast");
Pier Ventre42287df2016-11-09 14:17:26 -0800387 // break up simple next objective to GroupChain objects
388 GroupInfo groupInfo = createL2L3Chain(treatment, nextObj.id(),
389 nextObj.appId(), isMpls,
390 nextObj.meta());
391 if (groupInfo == null) {
392 log.error("Could not process nextObj={} in dev:{}", nextObj.id(), deviceId);
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800393 fail(nextObj, ObjectiveError.BADPARAMS);
Pier Ventre42287df2016-11-09 14:17:26 -0800394 return;
395 }
396 // create object for local and distributed storage
397 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700398 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
399 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Saurav Dasa4020382018-02-14 14:14:54 -0800400 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
401 allGroupKeys.add(gkeyChain);
402 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Pier Ventre42287df2016-11-09 14:17:26 -0800403 // store l3groupkey with the ofdpaNextGroup for the nextObjective that depends on it
Yi Tsengef19de12017-04-24 11:33:05 -0700404 updatePendingNextObjective(groupInfo.nextGroupDesc().appCookie(), ofdpaGrp);
Pier Ventre42287df2016-11-09 14:17:26 -0800405 // now we are ready to send the l2 groupDescription (inner), as all the stores
406 // that will get async replies have been updated. By waiting to update
407 // the stores, we prevent nasty race conditions.
Yi Tsengef19de12017-04-24 11:33:05 -0700408 groupService.addGroup(groupInfo.innerMostGroupDesc());
Pier Ventre42287df2016-11-09 14:17:26 -0800409 } else {
410 // We handle the pseudo wire with a different a procedure.
411 // This procedure is meant to handle both initiation and
412 // termination of the pseudo wire.
413 processPwNextObjective(nextObj);
Charles Chan188ebf52015-12-23 00:15:11 -0800414 }
Charles Chan188ebf52015-12-23 00:15:11 -0800415 }
416
Charles Chan188ebf52015-12-23 00:15:11 -0800417 /**
418 * Creates a simple L2 Interface Group.
Charles Chan188ebf52015-12-23 00:15:11 -0800419 * @param nextObj the next Objective
420 */
421 private void createL2InterfaceGroup(NextObjective nextObj) {
Yi Tsengef19de12017-04-24 11:33:05 -0700422 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700423 if (assignedVlan == null) {
424 log.warn("VLAN ID required by simple next obj is missing. Abort.");
Yi Tsengef19de12017-04-24 11:33:05 -0700425 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800426 return;
427 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700428 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700429 // There is only one L2 interface group in this case
Yi Tsengef19de12017-04-24 11:33:05 -0700430 GroupDescription l2InterfaceGroupDesc = groupInfos.get(0).innerMostGroupDesc();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700431 // Put all dependency information into allGroupKeys
432 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
433 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
434 gkeyChain.addFirst(l2InterfaceGroupDesc.appCookie());
435 allGroupKeys.add(gkeyChain);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700436 // Point the next objective to this group
437 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
438 updatePendingNextObjective(l2InterfaceGroupDesc.appCookie(), ofdpaGrp);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700439 // Start installing the inner-most group
jayakumarthazhath655b9a82018-10-01 00:51:54 +0530440 groupService.addGroup(l2InterfaceGroupDesc); }
Charles Chan188ebf52015-12-23 00:15:11 -0800441
442 /**
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800443 * Creates an Mpls group of type swap.
444 *
445 * @param nextGroupId the next group in the chain
446 * @param subtype the mpls swap label group subtype
447 * @param index the index of the group
448 * @param mplsLabel the mpls label to swap
449 * @param applicationId the application id
450 * @return the group description
451 */
452 protected GroupDescription createMplsSwap(int nextGroupId,
453 OfdpaMplsGroupSubType subtype,
454 int index,
455 MplsLabel mplsLabel,
456 ApplicationId applicationId) {
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800457 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800458 treatment.setMpls(mplsLabel);
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800459 // We point the group to the next group.
460 treatment.group(new GroupId(nextGroupId));
461 GroupBucket groupBucket = DefaultGroupBucket
462 .createIndirectGroupBucket(treatment.build());
463 // Finally we build the group description.
464 int groupId = makeMplsLabelGroupId(subtype, index);
465 GroupKey groupKey = new DefaultGroupKey(
Saurav Dasa4020382018-02-14 14:14:54 -0800466 Ofdpa2Pipeline.appKryo.serialize(index));
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800467 return new DefaultGroupDescription(
468 deviceId,
469 INDIRECT,
470 new GroupBuckets(Collections.singletonList(groupBucket)),
471 groupKey,
472 groupId,
Saurav Dasa4020382018-02-14 14:14:54 -0800473 applicationId);
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800474 }
475
476 /**
Charles Chan188ebf52015-12-23 00:15:11 -0800477 * Creates one of two possible group-chains from the treatment
478 * passed in. Depending on the MPLS boolean, this method either creates
Charles Chan425854b2016-04-11 15:32:12 -0700479 * an L3Unicast Group --&gt; L2Interface Group, if mpls is false;
480 * or MPLSInterface Group --&gt; L2Interface Group, if mpls is true;
Charles Chan188ebf52015-12-23 00:15:11 -0800481 * The returned 'inner' group description is always the L2 Interface group.
482 *
483 * @param treatment that needs to be broken up to create the group chain
484 * @param nextId of the next objective that needs this group chain
485 * @param appId of the application that sent this next objective
486 * @param mpls determines if L3Unicast or MPLSInterface group is created
487 * @param meta metadata passed in by the application as part of the nextObjective
488 * @return GroupInfo containing the GroupDescription of the
489 * L2Interface group(inner) and the GroupDescription of the (outer)
490 * L3Unicast/MPLSInterface group. May return null if there is an
491 * error in processing the chain
492 */
Charles Chan425854b2016-04-11 15:32:12 -0700493 protected GroupInfo createL2L3Chain(TrafficTreatment treatment, int nextId,
Charles Chanf9e98652016-09-07 16:54:23 -0700494 ApplicationId appId, boolean mpls,
495 TrafficSelector meta) {
496 return createL2L3ChainInternal(treatment, nextId, appId, mpls, meta, true);
497 }
498
499 /**
500 * Internal implementation of createL2L3Chain.
501 * <p>
502 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
503 * Since it is non-OF spec, we need an extension treatment for that.
504 * The useSetVlanExtension must be set to false for OFDPA i12.
505 * </p>
506 *
507 * @param treatment that needs to be broken up to create the group chain
508 * @param nextId of the next objective that needs this group chain
509 * @param appId of the application that sent this next objective
510 * @param mpls determines if L3Unicast or MPLSInterface group is created
511 * @param meta metadata passed in by the application as part of the nextObjective
512 * @param useSetVlanExtension use the setVlanVid extension that has is_present bit set to 0.
513 * @return GroupInfo containing the GroupDescription of the
514 * L2Interface group(inner) and the GroupDescription of the (outer)
515 * L3Unicast/MPLSInterface group. May return null if there is an
516 * error in processing the chain
517 */
518 protected GroupInfo createL2L3ChainInternal(TrafficTreatment treatment, int nextId,
Pier Ventre42287df2016-11-09 14:17:26 -0800519 ApplicationId appId, boolean mpls,
520 TrafficSelector meta, boolean useSetVlanExtension) {
Charles Chan188ebf52015-12-23 00:15:11 -0800521 // for the l2interface group, get vlan and port info
522 // for the outer group, get the src/dst mac, and vlan info
523 TrafficTreatment.Builder outerTtb = DefaultTrafficTreatment.builder();
524 TrafficTreatment.Builder innerTtb = DefaultTrafficTreatment.builder();
525 VlanId vlanid = null;
526 long portNum = 0;
527 boolean setVlan = false, popVlan = false;
Yi Tseng78f51f42017-02-02 13:54:58 -0800528 MacAddress srcMac;
529 MacAddress dstMac;
Charles Chan188ebf52015-12-23 00:15:11 -0800530 for (Instruction ins : treatment.allInstructions()) {
531 if (ins.type() == Instruction.Type.L2MODIFICATION) {
532 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
533 switch (l2ins.subtype()) {
534 case ETH_DST:
Charles Chan5270ed02016-01-30 23:22:37 -0800535 dstMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
536 outerTtb.setEthDst(dstMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800537 break;
538 case ETH_SRC:
Charles Chand0fd5dc2016-02-16 23:14:49 -0800539 srcMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
540 outerTtb.setEthSrc(srcMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800541 break;
542 case VLAN_ID:
543 vlanid = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
Charles Chanf9e98652016-09-07 16:54:23 -0700544 if (useSetVlanExtension) {
545 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
546 outerTtb.extension(ofdpaSetVlanVid, deviceId);
547 } else {
548 outerTtb.setVlanId(vlanid);
549 }
Charles Chan188ebf52015-12-23 00:15:11 -0800550 setVlan = true;
551 break;
552 case VLAN_POP:
553 innerTtb.popVlan();
554 popVlan = true;
555 break;
556 case DEC_MPLS_TTL:
557 case MPLS_LABEL:
558 case MPLS_POP:
559 case MPLS_PUSH:
560 case VLAN_PCP:
561 case VLAN_PUSH:
562 default:
563 break;
564 }
565 } else if (ins.type() == Instruction.Type.OUTPUT) {
566 portNum = ((Instructions.OutputInstruction) ins).port().toLong();
567 innerTtb.add(ins);
568 } else {
Saurav Das7bcbe702017-06-13 15:35:54 -0700569 log.debug("Driver does not handle this type of TrafficTreatment"
Saurav Dasa4020382018-02-14 14:14:54 -0800570 + " instruction in l2l3chain: {} - {}", ins.type(),
571 ins);
Charles Chan188ebf52015-12-23 00:15:11 -0800572 }
573 }
Charles Chan188ebf52015-12-23 00:15:11 -0800574 if (vlanid == null && meta != null) {
575 // use metadata if available
Pier Ventre140a8942016-11-02 07:26:38 -0700576 Criterion vidCriterion = meta.getCriterion(VLAN_VID);
Charles Chan188ebf52015-12-23 00:15:11 -0800577 if (vidCriterion != null) {
578 vlanid = ((VlanIdCriterion) vidCriterion).vlanId();
579 }
580 // if vlan is not set, use the vlan in metadata for outerTtb
581 if (vlanid != null && !setVlan) {
Charles Chanf9e98652016-09-07 16:54:23 -0700582 if (useSetVlanExtension) {
583 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
584 outerTtb.extension(ofdpaSetVlanVid, deviceId);
585 } else {
586 outerTtb.setVlanId(vlanid);
587 }
Charles Chan188ebf52015-12-23 00:15:11 -0800588 }
589 }
Charles Chan188ebf52015-12-23 00:15:11 -0800590 if (vlanid == null) {
591 log.error("Driver cannot process an L2/L3 group chain without "
592 + "egress vlan information for dev: {} port:{}",
593 deviceId, portNum);
594 return null;
595 }
Charles Chan188ebf52015-12-23 00:15:11 -0800596 if (!setVlan && !popVlan) {
597 // untagged outgoing port
598 TrafficTreatment.Builder temp = DefaultTrafficTreatment.builder();
599 temp.popVlan();
Yi Tsengef19de12017-04-24 11:33:05 -0700600 innerTtb.build().allInstructions().forEach(temp::add);
Charles Chan188ebf52015-12-23 00:15:11 -0800601 innerTtb = temp;
602 }
Charles Chan188ebf52015-12-23 00:15:11 -0800603 // assemble information for ofdpa l2interface group
Yi Tsengef19de12017-04-24 11:33:05 -0700604 int l2groupId = l2GroupId(vlanid, portNum);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800605 // a globally unique groupkey that is different for ports in the same device,
Charles Chan188ebf52015-12-23 00:15:11 -0800606 // but different for the same portnumber on different devices. Also different
607 // for the various group-types created out of the same next objective.
Charles Chane849c192016-01-11 18:28:54 -0800608 int l2gk = l2InterfaceGroupKey(deviceId, vlanid, portNum);
Yi Tsengef19de12017-04-24 11:33:05 -0700609 final GroupKey l2groupkey = new DefaultGroupKey(appKryo.serialize(l2gk));
Charles Chan188ebf52015-12-23 00:15:11 -0800610 // assemble information for outer group
Yi Tsengef19de12017-04-24 11:33:05 -0700611 GroupDescription outerGrpDesc;
Charles Chan188ebf52015-12-23 00:15:11 -0800612 if (mpls) {
Yi Tsengef19de12017-04-24 11:33:05 -0700613 // outer group is MPLS Interface
Saurav Das8be4e3a2016-03-11 17:19:07 -0800614 int mplsInterfaceIndex = getNextAvailableIndex();
Yi Tsengef19de12017-04-24 11:33:05 -0700615 int mplsGroupId = MPLS_INTERFACE_TYPE | (SUBTYPE_MASK & mplsInterfaceIndex);
616 final GroupKey mplsGroupKey = new DefaultGroupKey(
617 appKryo.serialize(mplsInterfaceIndex));
Yi Tsengfa394de2017-02-01 11:26:40 -0800618 outerTtb.group(new GroupId(l2groupId));
Charles Chan188ebf52015-12-23 00:15:11 -0800619 // create the mpls-interface group description to wait for the
620 // l2 interface group to be processed
621 GroupBucket mplsinterfaceGroupBucket =
622 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
623 outerGrpDesc = new DefaultGroupDescription(
624 deviceId,
625 GroupDescription.Type.INDIRECT,
626 new GroupBuckets(Collections.singletonList(
627 mplsinterfaceGroupBucket)),
Yi Tsengef19de12017-04-24 11:33:05 -0700628 mplsGroupKey,
629 mplsGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800630 appId);
631 log.debug("Trying MPLS-Interface: device:{} gid:{} gkey:{} nextid:{}",
Yi Tsengef19de12017-04-24 11:33:05 -0700632 deviceId, Integer.toHexString(mplsGroupId),
633 mplsGroupKey, nextId);
Charles Chan188ebf52015-12-23 00:15:11 -0800634 } else {
635 // outer group is L3Unicast
Saurav Das8be4e3a2016-03-11 17:19:07 -0800636 int l3unicastIndex = getNextAvailableIndex();
637 int l3groupId = L3_UNICAST_TYPE | (TYPE_MASK & l3unicastIndex);
638 final GroupKey l3groupkey = new DefaultGroupKey(
Yi Tsengef19de12017-04-24 11:33:05 -0700639 appKryo.serialize(l3unicastIndex));
Yi Tsengfa394de2017-02-01 11:26:40 -0800640 outerTtb.group(new GroupId(l2groupId));
Charles Chan188ebf52015-12-23 00:15:11 -0800641 // create the l3unicast group description to wait for the
642 // l2 interface group to be processed
643 GroupBucket l3unicastGroupBucket =
644 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
645 outerGrpDesc = new DefaultGroupDescription(
646 deviceId,
647 GroupDescription.Type.INDIRECT,
Yi Tsengef19de12017-04-24 11:33:05 -0700648 new GroupBuckets(Collections.singletonList(l3unicastGroupBucket)),
Charles Chan188ebf52015-12-23 00:15:11 -0800649 l3groupkey,
650 l3groupId,
651 appId);
652 log.debug("Trying L3Unicast: device:{} gid:{} gkey:{} nextid:{}",
653 deviceId, Integer.toHexString(l3groupId),
654 l3groupkey, nextId);
655 }
Charles Chan188ebf52015-12-23 00:15:11 -0800656 // store l2groupkey with the groupChainElem for the outer-group that depends on it
Yi Tsengef19de12017-04-24 11:33:05 -0700657 GroupChainElem gce = new GroupChainElem(outerGrpDesc, 1, false, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800658 updatePendingGroups(l2groupkey, gce);
Yi Tsengef19de12017-04-24 11:33:05 -0700659 // create group description for the inner l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700660 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800661 DefaultGroupBucket.createIndirectGroupBucket(innerTtb.build());
662 GroupDescription l2groupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -0700663 new DefaultGroupDescription(deviceId,
664 GroupDescription.Type.INDIRECT,
665 new GroupBuckets(Collections.singletonList(l2InterfaceGroupBucket)),
666 l2groupkey,
667 l2groupId,
668 appId);
Charles Chan188ebf52015-12-23 00:15:11 -0800669 log.debug("Trying L2Interface: device:{} gid:{} gkey:{} nextId:{}",
670 deviceId, Integer.toHexString(l2groupId),
671 l2groupkey, nextId);
672 return new GroupInfo(l2groupDescription, outerGrpDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800673 }
674
675 /**
676 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
677 * a chain of groups. The broadcast Next Objective passed in by the application
678 * has to be broken up into a group chain comprising of an
Saurav Das1a129a02016-11-18 15:21:57 -0800679 * L2 Flood group or L3 Multicast group, whose buckets point to L2 Interface groups.
Charles Chan188ebf52015-12-23 00:15:11 -0800680 *
681 * @param nextObj the nextObjective of type BROADCAST
682 */
683 private void processBroadcastNextObjective(NextObjective nextObj) {
Yi Tsengef19de12017-04-24 11:33:05 -0700684 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700685 if (assignedVlan == null) {
686 log.warn("VLAN ID required by broadcast next obj is missing. Abort.");
Yi Tsengef19de12017-04-24 11:33:05 -0700687 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800688 return;
689 }
Charles Chan188ebf52015-12-23 00:15:11 -0800690
jayakumarthazhath655b9a82018-10-01 00:51:54 +0530691 // Handling L2 multicast cases.
692 MacAddress dstMac = readEthDstFromSelector(nextObj.meta());
693 if (dstMac != null && dstMac.isMulticast()) {
694 processL2MulticastNextObjective(nextObj);
Jayakumar Thazhath812aa762018-10-24 01:22:04 -0400695 return;
jayakumarthazhath655b9a82018-10-01 00:51:54 +0530696 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700697
jayakumarthazhath655b9a82018-10-01 00:51:54 +0530698 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Yi Tsengef19de12017-04-24 11:33:05 -0700699 IpPrefix ipDst = readIpDstFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700700 if (ipDst != null) {
701 if (ipDst.isMulticast()) {
702 createL3MulticastGroup(nextObj, assignedVlan, groupInfos);
703 } else {
704 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -0700705 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700706 }
707 } else {
708 createL2FloodGroup(nextObj, assignedVlan, groupInfos);
709 }
710 }
711
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700712 private List<GroupInfo> prepareL2InterfaceGroup(NextObjective nextObj,
713 VlanId assignedVlan) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700714 ImmutableList.Builder<GroupInfo> groupInfoBuilder = ImmutableList.builder();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700715 // break up broadcast next objective to multiple groups
716 Collection<TrafficTreatment> buckets = nextObj.next();
Charles Chan188ebf52015-12-23 00:15:11 -0800717 // each treatment is converted to an L2 interface group
Charles Chan188ebf52015-12-23 00:15:11 -0800718 for (TrafficTreatment treatment : buckets) {
719 TrafficTreatment.Builder newTreatment = DefaultTrafficTreatment.builder();
720 PortNumber portNum = null;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700721 VlanId egressVlan = null;
Charles Chan188ebf52015-12-23 00:15:11 -0800722 // ensure that the only allowed treatments are pop-vlan and output
723 for (Instruction ins : treatment.allInstructions()) {
724 if (ins.type() == Instruction.Type.L2MODIFICATION) {
725 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
726 switch (l2ins.subtype()) {
727 case VLAN_POP:
728 newTreatment.add(l2ins);
729 break;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700730 case VLAN_ID:
731 egressVlan = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
732 break;
Charles Chan188ebf52015-12-23 00:15:11 -0800733 default:
734 log.debug("action {} not permitted for broadcast nextObj",
735 l2ins.subtype());
736 break;
737 }
738 } else if (ins.type() == Instruction.Type.OUTPUT) {
739 portNum = ((Instructions.OutputInstruction) ins).port();
740 newTreatment.add(ins);
741 } else {
Charles Chane849c192016-01-11 18:28:54 -0800742 log.debug("TrafficTreatment of type {} not permitted in " +
743 " broadcast nextObjective", ins.type());
Charles Chan188ebf52015-12-23 00:15:11 -0800744 }
745 }
Yi Tsengef19de12017-04-24 11:33:05 -0700746 if (portNum == null) {
747 log.warn("Can't find output port for the bucket {}.", treatment);
748 continue;
749 }
Charles Chan188ebf52015-12-23 00:15:11 -0800750 // assemble info for l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700751 VlanId l2InterfaceGroupVlan =
752 (egressVlan != null && !assignedVlan.equals(egressVlan)) ?
753 egressVlan : assignedVlan;
754 int l2gk = l2InterfaceGroupKey(deviceId, l2InterfaceGroupVlan, portNum.toLong());
755 final GroupKey l2InterfaceGroupKey =
Yi Tsengef19de12017-04-24 11:33:05 -0700756 new DefaultGroupKey(appKryo.serialize(l2gk));
Charles Chan372b63e2017-02-07 12:10:53 -0800757 int l2InterfaceGroupId = L2_INTERFACE_TYPE |
758 ((l2InterfaceGroupVlan.toShort() & THREE_BIT_MASK) << PORT_LEN) |
759 ((int) portNum.toLong() & FOUR_BIT_MASK);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700760 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800761 DefaultGroupBucket.createIndirectGroupBucket(newTreatment.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700762 GroupDescription l2InterfaceGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -0700763 new DefaultGroupDescription(deviceId,
764 GroupDescription.Type.INDIRECT,
765 new GroupBuckets(Collections.singletonList(
766 l2InterfaceGroupBucket)),
767 l2InterfaceGroupKey,
768 l2InterfaceGroupId,
769 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800770 log.debug("Trying L2-Interface: device:{} gid:{} gkey:{} nextid:{}",
Charles Chan5b9df8d2016-03-28 22:21:40 -0700771 deviceId, Integer.toHexString(l2InterfaceGroupId),
772 l2InterfaceGroupKey, nextObj.id());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700773 groupInfoBuilder.add(new GroupInfo(l2InterfaceGroupDescription,
774 l2InterfaceGroupDescription));
Charles Chan188ebf52015-12-23 00:15:11 -0800775 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700776 return groupInfoBuilder.build();
777 }
Charles Chan188ebf52015-12-23 00:15:11 -0800778
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700779 private void createL2FloodGroup(NextObjective nextObj, VlanId vlanId,
780 List<GroupInfo> groupInfos) {
781 // assemble info for l2 flood group. Since there can be only one flood
782 // group for a vlan, its index is always the same - 0
Yi Tsengef19de12017-04-24 11:33:05 -0700783 Integer l2FloodGroupId = L2_FLOOD_TYPE | (vlanId.toShort() << 16);
784 final GroupKey l2FloodGroupKey = l2FloodGroupKey(vlanId, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800785 // collection of group buckets pointing to all the l2 interface groups
Yi Tsengef19de12017-04-24 11:33:05 -0700786 List<GroupBucket> l2floodBuckets = generateNextGroupBuckets(groupInfos, ALL);
Charles Chan188ebf52015-12-23 00:15:11 -0800787 // create the l2flood group-description to wait for all the
788 // l2interface groups to be processed
789 GroupDescription l2floodGroupDescription =
790 new DefaultGroupDescription(
791 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800792 ALL,
Charles Chan188ebf52015-12-23 00:15:11 -0800793 new GroupBuckets(l2floodBuckets),
Yi Tsengef19de12017-04-24 11:33:05 -0700794 l2FloodGroupKey,
795 l2FloodGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800796 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800797 log.debug("Trying L2-Flood: device:{} gid:{} gkey:{} nextid:{}",
Yi Tsengef19de12017-04-24 11:33:05 -0700798 deviceId, Integer.toHexString(l2FloodGroupId),
799 l2FloodGroupKey, nextObj.id());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700800 // Put all dependency information into allGroupKeys
801 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
802 groupInfos.forEach(groupInfo -> {
Yi Tsengef19de12017-04-24 11:33:05 -0700803 Deque<GroupKey> groupKeyChain = new ArrayDeque<>();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700804 // In this case we should have L2 interface group only
Yi Tsengef19de12017-04-24 11:33:05 -0700805 groupKeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
806 groupKeyChain.addFirst(l2FloodGroupKey);
807 allGroupKeys.add(groupKeyChain);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700808 });
Charles Chan5b9df8d2016-03-28 22:21:40 -0700809 // Point the next objective to this group
810 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -0700811 updatePendingNextObjective(l2FloodGroupKey, ofdpaGrp);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700812 GroupChainElem gce = new GroupChainElem(l2floodGroupDescription,
Yi Tsengef19de12017-04-24 11:33:05 -0700813 groupInfos.size(), false, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700814 groupInfos.forEach(groupInfo -> {
815 // Point this group to the next group
Yi Tsengef19de12017-04-24 11:33:05 -0700816 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), gce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700817 // Start installing the inner-most group
Yi Tsengef19de12017-04-24 11:33:05 -0700818 groupService.addGroup(groupInfo.innerMostGroupDesc());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700819 });
820 }
821
Pier Luigi21fffd22018-01-19 10:24:53 +0100822 private void createL3MulticastGroup(NextObjective nextObj, VlanId vlanId,
823 List<GroupInfo> groupInfos) {
824 // Let's create a new list mcast buckets
825 List<GroupBucket> l3McastBuckets = createL3MulticastBucket(groupInfos);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700826
827 int l3MulticastIndex = getNextAvailableIndex();
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700828 int l3MulticastGroupId = L3_MULTICAST_TYPE |
829 vlanId.toShort() << 16 | (TYPE_VLAN_MASK & l3MulticastIndex);
830 final GroupKey l3MulticastGroupKey =
Yi Tsengef19de12017-04-24 11:33:05 -0700831 new DefaultGroupKey(appKryo.serialize(l3MulticastIndex));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700832
833 GroupDescription l3MulticastGroupDesc = new DefaultGroupDescription(deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800834 ALL,
Charles Chan5b9df8d2016-03-28 22:21:40 -0700835 new GroupBuckets(l3McastBuckets),
836 l3MulticastGroupKey,
837 l3MulticastGroupId,
838 nextObj.appId());
839
840 // Put all dependency information into allGroupKeys
841 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
842 groupInfos.forEach(groupInfo -> {
843 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700844 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700845 // Add L3 interface group to the chain if there is one.
Yi Tsengef19de12017-04-24 11:33:05 -0700846 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
847 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700848 }
849 gkeyChain.addFirst(l3MulticastGroupKey);
850 allGroupKeys.add(gkeyChain);
851 });
Charles Chan5b9df8d2016-03-28 22:21:40 -0700852 // Point the next objective to this group
853 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
854 updatePendingNextObjective(l3MulticastGroupKey, ofdpaGrp);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700855 GroupChainElem outerGce = new GroupChainElem(l3MulticastGroupDesc,
Yi Tsengef19de12017-04-24 11:33:05 -0700856 groupInfos.size(), false, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700857 groupInfos.forEach(groupInfo -> {
858 // Point this group (L3 multicast) to the next group
Yi Tsengef19de12017-04-24 11:33:05 -0700859 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), outerGce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700860 // Point next group to inner-most group, if any
Yi Tsengef19de12017-04-24 11:33:05 -0700861 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
862 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(),
863 1, false, deviceId);
864 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700865 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700866 // Start installing the inner-most group
Yi Tsengef19de12017-04-24 11:33:05 -0700867 groupService.addGroup(groupInfo.innerMostGroupDesc());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700868 });
Charles Chan188ebf52015-12-23 00:15:11 -0800869 }
870
Charles Chan188ebf52015-12-23 00:15:11 -0800871 /**
872 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
873 * a chain of groups. The hashed Next Objective passed in by the application
874 * has to be broken up into a group chain comprising of an
875 * L3 ECMP group as the top level group. Buckets of this group can point
876 * to a variety of groups in a group chain, depending on the whether
877 * MPLS labels are being pushed or not.
878 * <p>
879 * NOTE: We do not create MPLS ECMP groups as they are unimplemented in
880 * OF-DPA 2.0 (even though it is in the spec). Therefore we do not
881 * check the nextObjective meta to see what is matching before being
882 * sent to this nextObjective.
883 *
884 * @param nextObj the nextObjective of type HASHED
885 */
Pier Ventre140a8942016-11-02 07:26:38 -0700886 protected void processHashedNextObjective(NextObjective nextObj) {
Charles Chan188ebf52015-12-23 00:15:11 -0800887 // storage for all group keys in the chain of groups created
888 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
889 List<GroupInfo> unsentGroups = new ArrayList<>();
890 createHashBucketChains(nextObj, allGroupKeys, unsentGroups);
Charles Chan188ebf52015-12-23 00:15:11 -0800891 // now we can create the outermost L3 ECMP group
892 List<GroupBucket> l3ecmpGroupBuckets = new ArrayList<>();
893 for (GroupInfo gi : unsentGroups) {
894 // create ECMP bucket to point to the outer group
895 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Yi Tsengef19de12017-04-24 11:33:05 -0700896 ttb.group(new GroupId(gi.nextGroupDesc().givenGroupId()));
Charles Chan188ebf52015-12-23 00:15:11 -0800897 GroupBucket sbucket = DefaultGroupBucket
898 .createSelectGroupBucket(ttb.build());
899 l3ecmpGroupBuckets.add(sbucket);
900 }
Saurav Das8be4e3a2016-03-11 17:19:07 -0800901 int l3ecmpIndex = getNextAvailableIndex();
902 int l3ecmpGroupId = L3_ECMP_TYPE | (TYPE_MASK & l3ecmpIndex);
903 GroupKey l3ecmpGroupKey = new DefaultGroupKey(
Yi Tsengef19de12017-04-24 11:33:05 -0700904 appKryo.serialize(l3ecmpIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800905 GroupDescription l3ecmpGroupDesc =
906 new DefaultGroupDescription(
907 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800908 SELECT,
Charles Chan188ebf52015-12-23 00:15:11 -0800909 new GroupBuckets(l3ecmpGroupBuckets),
910 l3ecmpGroupKey,
911 l3ecmpGroupId,
912 nextObj.appId());
913 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
914 l3ecmpGroupBuckets.size(),
Yi Tsengef19de12017-04-24 11:33:05 -0700915 false, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800916
917 // create objects for local and distributed storage
Yi Tsengef19de12017-04-24 11:33:05 -0700918 allGroupKeys.forEach(gKeyChain -> gKeyChain.addFirst(l3ecmpGroupKey));
Charles Chan188ebf52015-12-23 00:15:11 -0800919 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Charles Chan188ebf52015-12-23 00:15:11 -0800920 // store l3ecmpGroupKey with the ofdpaGroupChain for the nextObjective
921 // that depends on it
922 updatePendingNextObjective(l3ecmpGroupKey, ofdpaGrp);
Charles Chan188ebf52015-12-23 00:15:11 -0800923 log.debug("Trying L3ECMP: device:{} gid:{} gkey:{} nextId:{}",
924 deviceId, Integer.toHexString(l3ecmpGroupId),
925 l3ecmpGroupKey, nextObj.id());
926 // finally we are ready to send the innermost groups
927 for (GroupInfo gi : unsentGroups) {
928 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -0700929 Integer.toHexString(gi.innerMostGroupDesc().givenGroupId()), deviceId);
930 updatePendingGroups(gi.nextGroupDesc().appCookie(), l3ecmpGce);
931 groupService.addGroup(gi.innerMostGroupDesc());
Charles Chan188ebf52015-12-23 00:15:11 -0800932 }
Charles Chan188ebf52015-12-23 00:15:11 -0800933 }
934
935 /**
936 * Creates group chains for all buckets in a hashed group, and stores the
937 * GroupInfos and GroupKeys for all the groups in the lists passed in, which
938 * should be empty.
939 * <p>
940 * Does not create the top level ECMP group. Does not actually send the
941 * groups to the groupService.
942 *
943 * @param nextObj the Next Objective with buckets that need to be converted
944 * to group chains
945 * @param allGroupKeys a list to store groupKey for each bucket-group-chain
946 * @param unsentGroups a list to store GroupInfo for each bucket-group-chain
947 */
Pier Ventre140a8942016-11-02 07:26:38 -0700948 protected void createHashBucketChains(NextObjective nextObj,
Yi Tsengef19de12017-04-24 11:33:05 -0700949 List<Deque<GroupKey>> allGroupKeys,
950 List<GroupInfo> unsentGroups) {
Charles Chan188ebf52015-12-23 00:15:11 -0800951 // break up hashed next objective to multiple groups
952 Collection<TrafficTreatment> buckets = nextObj.next();
953
954 for (TrafficTreatment bucket : buckets) {
955 //figure out how many labels are pushed in each bucket
956 int labelsPushed = 0;
957 MplsLabel innermostLabel = null;
958 for (Instruction ins : bucket.allInstructions()) {
959 if (ins.type() == Instruction.Type.L2MODIFICATION) {
960 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
961 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_PUSH) {
962 labelsPushed++;
963 }
964 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) {
965 if (innermostLabel == null) {
Yi Tsengef19de12017-04-24 11:33:05 -0700966 innermostLabel =
967 ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label();
Charles Chan188ebf52015-12-23 00:15:11 -0800968 }
969 }
970 }
971 }
Yi Tsengef19de12017-04-24 11:33:05 -0700972 Deque<GroupKey> gKeyChain = new ArrayDeque<>();
Saurav Dasa4020382018-02-14 14:14:54 -0800973 // here we only deal with 0 and 1 label push
Charles Chan188ebf52015-12-23 00:15:11 -0800974 if (labelsPushed == 0) {
Yi Tsengef19de12017-04-24 11:33:05 -0700975 GroupInfo noLabelGroupInfo;
Pier Ventre140a8942016-11-02 07:26:38 -0700976 TrafficSelector metaSelector = nextObj.meta();
977 if (metaSelector != null) {
978 if (isNotMplsBos(metaSelector)) {
Yi Tsengef19de12017-04-24 11:33:05 -0700979 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
980 nextObj.appId(), true,
981 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700982 } else {
Yi Tsengef19de12017-04-24 11:33:05 -0700983 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
984 nextObj.appId(), false,
985 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700986 }
987 } else {
Yi Tsengef19de12017-04-24 11:33:05 -0700988 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
989 nextObj.appId(), false,
990 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700991 }
Yi Tsengef19de12017-04-24 11:33:05 -0700992 if (noLabelGroupInfo == null) {
Charles Chan188ebf52015-12-23 00:15:11 -0800993 log.error("Could not process nextObj={} in dev:{}",
994 nextObj.id(), deviceId);
995 return;
996 }
Yi Tsengef19de12017-04-24 11:33:05 -0700997 gKeyChain.addFirst(noLabelGroupInfo.innerMostGroupDesc().appCookie());
998 gKeyChain.addFirst(noLabelGroupInfo.nextGroupDesc().appCookie());
Charles Chan188ebf52015-12-23 00:15:11 -0800999 // we can't send the inner group description yet, as we have to
1000 // create the dependent ECMP group first. So we store..
Yi Tsengef19de12017-04-24 11:33:05 -07001001 unsentGroups.add(noLabelGroupInfo);
Charles Chan188ebf52015-12-23 00:15:11 -08001002 } else if (labelsPushed == 1) {
1003 GroupInfo onelabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
1004 nextObj.appId(), true,
1005 nextObj.meta());
1006 if (onelabelGroupInfo == null) {
1007 log.error("Could not process nextObj={} in dev:{}",
1008 nextObj.id(), deviceId);
1009 return;
1010 }
1011 // we need to add another group to this chain - the L3VPN group
1012 TrafficTreatment.Builder l3vpnTtb = DefaultTrafficTreatment.builder();
Charles Chan0f43e472017-02-14 14:00:16 -08001013 if (requireVlanPopBeforeMplsPush()) {
1014 l3vpnTtb.popVlan();
1015 }
Charles Chan188ebf52015-12-23 00:15:11 -08001016 l3vpnTtb.pushMpls()
1017 .setMpls(innermostLabel)
Yi Tsengef19de12017-04-24 11:33:05 -07001018 .group(new GroupId(onelabelGroupInfo.nextGroupDesc().givenGroupId()));
Charles Chan40132b32017-01-22 00:19:37 -08001019 if (supportCopyTtl()) {
1020 l3vpnTtb.copyTtlOut();
1021 }
1022 if (supportSetMplsBos()) {
1023 l3vpnTtb.setMplsBos(true);
1024 }
Charles Chan0f43e472017-02-14 14:00:16 -08001025 if (requireVlanPopBeforeMplsPush()) {
1026 l3vpnTtb.pushVlan().setVlanId(VlanId.vlanId(VlanId.RESERVED));
1027 }
Charles Chan188ebf52015-12-23 00:15:11 -08001028 GroupBucket l3vpnGrpBkt =
1029 DefaultGroupBucket.createIndirectGroupBucket(l3vpnTtb.build());
Saurav Das8be4e3a2016-03-11 17:19:07 -08001030 int l3vpnIndex = getNextAvailableIndex();
Yi Tsengef19de12017-04-24 11:33:05 -07001031 int l3vpnGroupId = MPLS_L3VPN_SUBTYPE | (SUBTYPE_MASK & l3vpnIndex);
1032 GroupKey l3vpnGroupKey = new DefaultGroupKey(
1033 appKryo.serialize(l3vpnIndex));
Charles Chan188ebf52015-12-23 00:15:11 -08001034 GroupDescription l3vpnGroupDesc =
1035 new DefaultGroupDescription(
1036 deviceId,
1037 GroupDescription.Type.INDIRECT,
Yi Tsengef19de12017-04-24 11:33:05 -07001038 new GroupBuckets(Collections.singletonList(l3vpnGrpBkt)),
1039 l3vpnGroupKey,
1040 l3vpnGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -08001041 nextObj.appId());
Yi Tsengef19de12017-04-24 11:33:05 -07001042 GroupChainElem l3vpnGce = new GroupChainElem(l3vpnGroupDesc,
1043 1,
1044 false,
1045 deviceId);
1046 updatePendingGroups(onelabelGroupInfo.nextGroupDesc().appCookie(), l3vpnGce);
Charles Chan188ebf52015-12-23 00:15:11 -08001047
Yi Tsengef19de12017-04-24 11:33:05 -07001048 gKeyChain.addFirst(onelabelGroupInfo.innerMostGroupDesc().appCookie());
1049 gKeyChain.addFirst(onelabelGroupInfo.nextGroupDesc().appCookie());
1050 gKeyChain.addFirst(l3vpnGroupKey);
Charles Chan188ebf52015-12-23 00:15:11 -08001051 //now we can replace the outerGrpDesc with the one we just created
Yi Tsengef19de12017-04-24 11:33:05 -07001052 onelabelGroupInfo.nextGroupDesc(l3vpnGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -08001053 // we can't send the innermost group yet, as we have to create
1054 // the dependent ECMP group first. So we store ...
1055 unsentGroups.add(onelabelGroupInfo);
Yi Tsengef19de12017-04-24 11:33:05 -07001056 log.debug("Trying L3VPN: device:{} gid:{} group key:{} nextId:{}",
1057 deviceId, Integer.toHexString(l3vpnGroupId),
1058 l3vpnGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -08001059 } else {
1060 log.warn("Driver currently does not handle more than 1 MPLS "
1061 + "labels. Not processing nextObjective {}", nextObj.id());
1062 return;
1063 }
Charles Chan188ebf52015-12-23 00:15:11 -08001064 // all groups in this chain
Yi Tsengef19de12017-04-24 11:33:05 -07001065 allGroupKeys.add(gKeyChain);
Charles Chan188ebf52015-12-23 00:15:11 -08001066 }
1067 }
1068
Pier Ventre42287df2016-11-09 14:17:26 -08001069 /**
1070 * Processes the pseudo wire related next objective.
1071 * This procedure try to reuse the mpls label groups,
1072 * the mpls interface group and the l2 interface group.
1073 *
1074 * @param nextObjective the objective to process.
1075 */
1076 protected void processPwNextObjective(NextObjective nextObjective) {
Saurav Das1547b3f2017-05-05 17:01:08 -07001077 log.warn("Pseudo wire extensions are not supported in OFDPA 2.0 {}",
1078 nextObjective.id());
Pier Ventre42287df2016-11-09 14:17:26 -08001079 }
1080
Saurav Das8be4e3a2016-03-11 17:19:07 -08001081 //////////////////////////////////////
1082 // Group Editing
1083 //////////////////////////////////////
Charles Chan188ebf52015-12-23 00:15:11 -08001084 /**
1085 * Adds a bucket to the top level group of a group-chain, and creates the chain.
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001086 * Ensures that bucket being added is not a duplicate, by checking existing
Yi Tsengef19de12017-04-24 11:33:05 -07001087 * buckets for the same output port.
Charles Chan188ebf52015-12-23 00:15:11 -08001088 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001089 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -08001090 * @param next the representation of the existing group-chain for this next objective
1091 */
1092 protected void addBucketToGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001093 if (nextObjective.type() != NextObjective.Type.HASHED &&
1094 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001095 log.warn("AddBuckets not applied to nextType:{} in dev:{} for next:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001096 nextObjective.type(), deviceId, nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -07001097 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001098 return;
1099 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001100 // first check to see if bucket being added is not a duplicate of an
Yi Tsengef19de12017-04-24 11:33:05 -07001101 // existing bucket. If it is for an existing output port, then its a
1102 // duplicate.
Yi Tseng78f51f42017-02-02 13:54:58 -08001103 Set<TrafficTreatment> duplicateBuckets = Sets.newHashSet();
Yi Tsengef19de12017-04-24 11:33:05 -07001104 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
1105 Set<PortNumber> existingPorts = getExistingOutputPorts(allActiveKeys,
1106 groupService,
1107 deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -08001108 Set<TrafficTreatment> nonDuplicateBuckets = Sets.newHashSet();
1109 NextObjective objectiveToAdd;
Yi Tseng78f51f42017-02-02 13:54:58 -08001110 nextObjective.next().forEach(trafficTreatment -> {
1111 PortNumber portNumber = readOutPortFromTreatment(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001112 if (portNumber == null) {
1113 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001114 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001115 if (existingPorts.contains(portNumber)) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001116 // its possible that portnumbers are same but labels are different
1117 int label = readLabelFromTreatment(trafficTreatment);
1118 if (label == -1) {
1119 duplicateBuckets.add(trafficTreatment);
1120 } else {
Saurav Dasceccf242017-08-03 18:30:35 -07001121 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1122 groupService, deviceId,
1123 portNumber, label);
1124 if (!existing.isEmpty()) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001125 duplicateBuckets.add(trafficTreatment);
1126 } else {
1127 nonDuplicateBuckets.add(trafficTreatment);
1128 }
1129 }
Yi Tseng47f82dc2017-03-05 22:48:39 -08001130 } else {
1131 nonDuplicateBuckets.add(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001132 }
1133 });
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001134 if (duplicateBuckets.isEmpty()) {
1135 // use the original objective
1136 objectiveToAdd = nextObjective;
1137 } else if (!nonDuplicateBuckets.isEmpty()) {
1138 // only use the non-duplicate buckets if there are any
1139 log.debug("Some buckets {} already exist in next id {}, duplicate "
1140 + "buckets will be ignored.", duplicateBuckets, nextObjective.id());
1141 // new next objective with non duplicate treatments
Yi Tseng47f82dc2017-03-05 22:48:39 -08001142 NextObjective.Builder builder = DefaultNextObjective.builder()
1143 .withType(nextObjective.type())
1144 .withId(nextObjective.id())
1145 .withMeta(nextObjective.meta())
1146 .fromApp(nextObjective.appId());
1147 nonDuplicateBuckets.forEach(builder::addTreatment);
Yi Tseng47f82dc2017-03-05 22:48:39 -08001148 ObjectiveContext context = nextObjective.context().orElse(null);
1149 objectiveToAdd = builder.addToExisting(context);
1150 } else {
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001151 // buckets to add are already there - nothing to do
Saurav Das1547b3f2017-05-05 17:01:08 -07001152 log.debug("buckets already exist {} in next: {} ..ignoring bucket add",
1153 duplicateBuckets, nextObjective.id());
1154 pass(nextObjective);
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001155 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001156 }
Saurav Das1a129a02016-11-18 15:21:57 -08001157 if (nextObjective.type() == NextObjective.Type.HASHED) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001158 addBucketToHashGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001159 } else if (nextObjective.type() == NextObjective.Type.BROADCAST) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001160 addBucketToBroadcastGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001161 }
1162 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001163
Saurav Das1a129a02016-11-18 15:21:57 -08001164 private void addBucketToHashGroup(NextObjective nextObjective,
Yi Tseng78f51f42017-02-02 13:54:58 -08001165 List<Deque<GroupKey>> allActiveKeys) {
Charles Chan188ebf52015-12-23 00:15:11 -08001166 // storage for all group keys in the chain of groups created
1167 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
1168 List<GroupInfo> unsentGroups = new ArrayList<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001169 List<GroupBucket> newBuckets;
Charles Chan188ebf52015-12-23 00:15:11 -08001170 createHashBucketChains(nextObjective, allGroupKeys, unsentGroups);
Yi Tseng78f51f42017-02-02 13:54:58 -08001171 // now we can create the buckets to add to the outermost L3 ECMP group
1172 newBuckets = generateNextGroupBuckets(unsentGroups, SELECT);
Saurav Das1a129a02016-11-18 15:21:57 -08001173 // retrieve the original L3 ECMP group
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001174 Group l3ecmpGroup = retrieveTopLevelGroup(allActiveKeys, deviceId,
1175 groupService, nextObjective.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001176 if (l3ecmpGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001177 fail(nextObjective, ObjectiveError.GROUPMISSING);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001178 return;
1179 }
Saurav Das1a129a02016-11-18 15:21:57 -08001180 GroupKey l3ecmpGroupKey = l3ecmpGroup.appCookie();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001181 int l3ecmpGroupId = l3ecmpGroup.id().id();
Charles Chan188ebf52015-12-23 00:15:11 -08001182 // Although GroupDescriptions are not necessary for adding buckets to
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001183 // existing groups, we still use one in the GroupChainElem. When the latter is
Charles Chan188ebf52015-12-23 00:15:11 -08001184 // processed, the info will be extracted for the bucketAdd call to groupService
1185 GroupDescription l3ecmpGroupDesc =
Yi Tsengef19de12017-04-24 11:33:05 -07001186 new DefaultGroupDescription(deviceId,
1187 SELECT,
1188 new GroupBuckets(newBuckets),
1189 l3ecmpGroupKey,
1190 l3ecmpGroupId,
1191 nextObjective.appId());
Yi Tseng78f51f42017-02-02 13:54:58 -08001192 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
1193 unsentGroups.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001194 true,
1195 deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001196
Saurav Dasc88d4662017-05-15 15:34:25 -07001197 // update new bucket-chains
1198 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
1199 for (Deque<GroupKey> newBucketChain : allGroupKeys) {
1200 newBucketChain.addFirst(l3ecmpGroupKey);
1201 addedKeys.add(newBucketChain);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001202 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001203 updatePendingNextObjective(l3ecmpGroupKey,
1204 new OfdpaNextGroup(addedKeys, nextObjective));
Yi Tsengef19de12017-04-24 11:33:05 -07001205 log.debug("Adding to L3ECMP: device:{} gid:{} group key:{} nextId:{}",
Charles Chan188ebf52015-12-23 00:15:11 -08001206 deviceId, Integer.toHexString(l3ecmpGroupId),
1207 l3ecmpGroupKey, nextObjective.id());
Yi Tseng78f51f42017-02-02 13:54:58 -08001208 unsentGroups.forEach(groupInfo -> {
1209 // send the innermost group
1210 log.debug("Sending innermost group {} in group chain on device {} ",
Saurav Dasc88d4662017-05-15 15:34:25 -07001211 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
1212 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001213 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3ecmpGce);
1214 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001215 });
Saurav Das1a129a02016-11-18 15:21:57 -08001216 }
Charles Chan188ebf52015-12-23 00:15:11 -08001217
Saurav Das1a129a02016-11-18 15:21:57 -08001218 private void addBucketToBroadcastGroup(NextObjective nextObj,
Yi Tsengef19de12017-04-24 11:33:05 -07001219 List<Deque<GroupKey>> allActiveKeys) {
1220 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001221 if (assignedVlan == null) {
1222 log.warn("VLAN ID required by broadcast next obj is missing. "
1223 + "Aborting add bucket to broadcast group for next:{} in dev:{}",
1224 nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001225 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001226 return;
1227 }
Saurav Das1a129a02016-11-18 15:21:57 -08001228 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Yi Tsengef19de12017-04-24 11:33:05 -07001229 IpPrefix ipDst = readIpDstFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001230 if (ipDst != null) {
1231 if (ipDst.isMulticast()) {
Yi Tsengef19de12017-04-24 11:33:05 -07001232 addBucketToL3MulticastGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001233 } else {
1234 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001235 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001236 }
1237 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001238 addBucketToL2FloodGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001239 }
1240 }
1241
1242 private void addBucketToL2FloodGroup(NextObjective nextObj,
1243 List<Deque<GroupKey>> allActiveKeys,
1244 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001245 VlanId assignedVlan) {
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001246 Group l2FloodGroup = retrieveTopLevelGroup(allActiveKeys, deviceId,
1247 groupService, nextObj.id());
Yi Tseng78f51f42017-02-02 13:54:58 -08001248
1249 if (l2FloodGroup == null) {
1250 log.warn("Can't find L2 flood group while adding bucket to it. NextObj = {}",
1251 nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001252 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001253 return;
1254 }
Saurav Das1a129a02016-11-18 15:21:57 -08001255
Yi Tseng78f51f42017-02-02 13:54:58 -08001256 GroupKey l2floodGroupKey = l2FloodGroup.appCookie();
1257 int l2floodGroupId = l2FloodGroup.id().id();
1258 List<GroupBucket> newBuckets = generateNextGroupBuckets(groupInfos, ALL);
Yi Tseng78f51f42017-02-02 13:54:58 -08001259 GroupDescription l2FloodGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001260 new DefaultGroupDescription(deviceId,
1261 ALL,
1262 new GroupBuckets(newBuckets),
1263 l2floodGroupKey,
1264 l2floodGroupId,
1265 nextObj.appId());
Yi Tseng78f51f42017-02-02 13:54:58 -08001266 GroupChainElem l2FloodGroupChainElement =
1267 new GroupChainElem(l2FloodGroupDescription,
1268 groupInfos.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001269 true,
1270 deviceId);
Yi Tseng78f51f42017-02-02 13:54:58 -08001271
Yi Tseng78f51f42017-02-02 13:54:58 -08001272
1273 //ensure assignedVlan applies to the chosen group
1274 VlanId floodGroupVlan = extractVlanIdFromGroupId(l2floodGroupId);
Yi Tseng78f51f42017-02-02 13:54:58 -08001275 if (!floodGroupVlan.equals(assignedVlan)) {
1276 log.warn("VLAN ID {} does not match Flood group {} to which bucket is "
1277 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1278 Integer.toHexString(l2floodGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001279 fail(nextObj, ObjectiveError.BADPARAMS);
Yi Tseng78f51f42017-02-02 13:54:58 -08001280 return;
1281 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001282 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
Yi Tseng78f51f42017-02-02 13:54:58 -08001283 groupInfos.forEach(groupInfo -> {
1284 // update original NextGroup with new bucket-chain
Yi Tseng78f51f42017-02-02 13:54:58 -08001285 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001286 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001287 newBucketChain.addFirst(l2floodGroupKey);
Saurav Dasc88d4662017-05-15 15:34:25 -07001288 addedKeys.add(newBucketChain);
Yi Tsengef19de12017-04-24 11:33:05 -07001289 log.debug("Adding to L2FLOOD: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001290 deviceId, Integer.toHexString(l2floodGroupId),
1291 l2floodGroupKey, nextObj.id());
1292 // send the innermost group
1293 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001294 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001295 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001296 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l2FloodGroupChainElement);
Yi Tseng78f51f42017-02-02 13:54:58 -08001297
Yi Tsengef19de12017-04-24 11:33:05 -07001298 DeviceId innerMostGroupDevice = groupInfo.innerMostGroupDesc().deviceId();
1299 GroupKey innerMostGroupKey = groupInfo.innerMostGroupDesc().appCookie();
Yi Tseng78f51f42017-02-02 13:54:58 -08001300 Group existsL2IGroup = groupService.getGroup(innerMostGroupDevice, innerMostGroupKey);
1301
1302 if (existsL2IGroup != null) {
1303 // group already exist
1304 processPendingAddGroupsOrNextObjs(innerMostGroupKey, true);
1305 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001306 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001307 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001308 });
Saurav Dasc88d4662017-05-15 15:34:25 -07001309
1310 updatePendingNextObjective(l2floodGroupKey,
1311 new OfdpaNextGroup(addedKeys, nextObj));
Yi Tseng78f51f42017-02-02 13:54:58 -08001312 }
1313
Saurav Das1a129a02016-11-18 15:21:57 -08001314 private void addBucketToL3MulticastGroup(NextObjective nextObj,
1315 List<Deque<GroupKey>> allActiveKeys,
1316 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001317 VlanId assignedVlan) {
Pier Luigi21fffd22018-01-19 10:24:53 +01001318 // Create the buckets to add to the outermost L3 Multicast group
1319 List<GroupBucket> newBuckets = createL3MulticastBucket(groupInfos);
Saurav Das1a129a02016-11-18 15:21:57 -08001320
1321 // get the group being edited
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001322 Group l3mcastGroup = retrieveTopLevelGroup(allActiveKeys, deviceId,
1323 groupService, nextObj.id());
Saurav Das1a129a02016-11-18 15:21:57 -08001324 if (l3mcastGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001325 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001326 return;
1327 }
1328 GroupKey l3mcastGroupKey = l3mcastGroup.appCookie();
1329 int l3mcastGroupId = l3mcastGroup.id().id();
1330
1331 //ensure assignedVlan applies to the chosen group
Yi Tseng78f51f42017-02-02 13:54:58 -08001332 VlanId expectedVlan = extractVlanIdFromGroupId(l3mcastGroupId);
Saurav Das1a129a02016-11-18 15:21:57 -08001333 if (!expectedVlan.equals(assignedVlan)) {
1334 log.warn("VLAN ID {} does not match L3 Mcast group {} to which bucket is "
1335 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1336 Integer.toHexString(l3mcastGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001337 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001338 }
1339 GroupDescription l3mcastGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001340 new DefaultGroupDescription(deviceId,
1341 ALL,
1342 new GroupBuckets(newBuckets),
1343 l3mcastGroupKey,
1344 l3mcastGroupId,
1345 nextObj.appId());
Saurav Das1a129a02016-11-18 15:21:57 -08001346 GroupChainElem l3mcastGce = new GroupChainElem(l3mcastGroupDescription,
Yi Tsengef19de12017-04-24 11:33:05 -07001347 groupInfos.size(),
1348 true,
1349 deviceId);
Saurav Dasc88d4662017-05-15 15:34:25 -07001350
1351 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
Yi Tseng78f51f42017-02-02 13:54:58 -08001352 groupInfos.forEach(groupInfo -> {
1353 // update original NextGroup with new bucket-chain
1354 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001355 newBucketChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001356 // Add L3 interface group to the chain if there is one.
Yi Tsengef19de12017-04-24 11:33:05 -07001357 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1358 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001359 }
1360 newBucketChain.addFirst(l3mcastGroupKey);
Saurav Dasc88d4662017-05-15 15:34:25 -07001361 addedKeys.add(newBucketChain);
Yi Tseng78f51f42017-02-02 13:54:58 -08001362
Yi Tsengef19de12017-04-24 11:33:05 -07001363 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3mcastGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001364 // Point next group to inner-most group, if any
Yi Tsengef19de12017-04-24 11:33:05 -07001365 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1366 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(),
1367 1,
1368 false,
1369 deviceId);
1370 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001371 }
Yi Tsengef19de12017-04-24 11:33:05 -07001372 log.debug("Adding to L3MCAST: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001373 deviceId, Integer.toHexString(l3mcastGroupId),
1374 l3mcastGroupKey, nextObj.id());
1375 // send the innermost group
1376 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001377 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001378 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001379 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001380
1381 });
1382
Saurav Das1a129a02016-11-18 15:21:57 -08001383 updatePendingNextObjective(l3mcastGroupKey,
Saurav Dasc88d4662017-05-15 15:34:25 -07001384 new OfdpaNextGroup(addedKeys, nextObj));
Charles Chan188ebf52015-12-23 00:15:11 -08001385 }
1386
1387 /**
Saurav Dasceccf242017-08-03 18:30:35 -07001388 * Removes buckets in the top level group of a possible group-chain. Does
Saurav Das1a129a02016-11-18 15:21:57 -08001389 * not remove the groups in the group-chain pointed to by this bucket, as they
Charles Chan188ebf52015-12-23 00:15:11 -08001390 * may be in use (referenced by other groups) elsewhere.
1391 *
Saurav Dasceccf242017-08-03 18:30:35 -07001392 * @param nextObjective a next objective that contains information for the
1393 * buckets to be removed from the group
1394 * @param next the representation of the existing group-chains for this next
1395 * objective, from which the top-level buckets to remove are determined
Charles Chan188ebf52015-12-23 00:15:11 -08001396 */
1397 protected void removeBucketFromGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001398 if (nextObjective.type() != NextObjective.Type.HASHED &&
1399 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001400 log.warn("RemoveBuckets not applied to nextType:{} in dev:{} for next:{}",
1401 nextObjective.type(), deviceId, nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -07001402 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001403 return;
1404 }
Yi Tsengef19de12017-04-24 11:33:05 -07001405 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Saurav Dasceccf242017-08-03 18:30:35 -07001406 List<Integer> indicesToRemove = Lists.newArrayList();
1407 for (TrafficTreatment treatment : nextObjective.next()) {
1408 // find the top-level bucket in the group-chain by matching the
1409 // outport and label from different groups in the chain
1410 PortNumber portToRemove = readOutPortFromTreatment(treatment);
1411 int labelToRemove = readLabelFromTreatment(treatment);
1412 if (portToRemove == null) {
1413 log.warn("treatment {} of next objective {} has no outport.. "
1414 + "cannot remove bucket from group in dev: {}", treatment,
1415 nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001416 continue;
1417 }
Saurav Dasceccf242017-08-03 18:30:35 -07001418 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1419 groupService, deviceId,
1420 portToRemove, labelToRemove);
1421 indicesToRemove.addAll(existing);
1422
Charles Chan188ebf52015-12-23 00:15:11 -08001423 }
Saurav Dasceccf242017-08-03 18:30:35 -07001424
1425 List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList();
1426 indicesToRemove.forEach(index -> chainsToRemove
1427 .add(allActiveKeys.get(index)));
Yi Tseng78f51f42017-02-02 13:54:58 -08001428 if (chainsToRemove.isEmpty()) {
Charles Chan188ebf52015-12-23 00:15:11 -08001429 log.warn("Could not find appropriate group-chain for removing bucket"
1430 + " for next id {} in dev:{}", nextObjective.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001431 fail(nextObjective, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001432 return;
Charles Chan188ebf52015-12-23 00:15:11 -08001433 }
Saurav Dasceccf242017-08-03 18:30:35 -07001434 removeBucket(chainsToRemove, nextObjective);
1435 }
1436
1437 /**
1438 * Removes top-level buckets from a group that represents the given next objective.
1439 *
1440 * @param chainsToRemove a list of group bucket chains to remove
1441 * @param nextObjective the next objective that contains information for the
1442 * buckets to be removed from the group
1443 */
1444 protected void removeBucket(List<Deque<GroupKey>> chainsToRemove,
1445 NextObjective nextObjective) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001446 List<GroupBucket> bucketsToRemove = Lists.newArrayList();
1447 //first group key is the one we want to modify
1448 GroupKey modGroupKey = chainsToRemove.get(0).peekFirst();
Saurav Das1a129a02016-11-18 15:21:57 -08001449 Group modGroup = groupService.getGroup(deviceId, modGroupKey);
Ray Milkey43869812018-05-04 13:00:39 -07001450 if (modGroup == null) {
1451 log.warn("removeBucket(): Attempt to modify non-existent group {} for device {}", modGroupKey, deviceId);
1452 return;
1453 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001454 for (Deque<GroupKey> foundChain : chainsToRemove) {
1455 //second group key is the one we wish to remove the reference to
1456 if (foundChain.size() < 2) {
Saurav Dasceccf242017-08-03 18:30:35 -07001457 // additional check to make sure second group key exists in
Yi Tseng78f51f42017-02-02 13:54:58 -08001458 // the chain.
1459 log.warn("Can't find second group key from chain {}",
1460 foundChain);
1461 continue;
Saurav Das1a129a02016-11-18 15:21:57 -08001462 }
Saurav Dasceccf242017-08-03 18:30:35 -07001463 GroupKey pointedGroupKey = foundChain.stream()
1464 .collect(Collectors.toList()).get(1);
Yi Tseng78f51f42017-02-02 13:54:58 -08001465 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
Yi Tseng78f51f42017-02-02 13:54:58 -08001466 if (pointedGroup == null) {
1467 continue;
1468 }
1469
1470 GroupBucket bucket;
1471 if (nextObjective.type() == NextObjective.Type.HASHED) {
1472 bucket = DefaultGroupBucket.createSelectGroupBucket(
1473 DefaultTrafficTreatment.builder()
1474 .group(pointedGroup.id())
1475 .build());
1476 } else {
1477 bucket = DefaultGroupBucket.createAllGroupBucket(
1478 DefaultTrafficTreatment.builder()
1479 .group(pointedGroup.id())
1480 .build());
1481 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001482 bucketsToRemove.add(bucket);
Saurav Das1a129a02016-11-18 15:21:57 -08001483 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001484
1485 GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove);
1486 List<String> pointedGroupIds; // for debug log
1487 pointedGroupIds = bucketsToRemove.stream()
1488 .map(GroupBucket::treatment)
1489 .map(TrafficTreatment::allInstructions)
1490 .flatMap(List::stream)
1491 .filter(inst -> inst instanceof Instructions.GroupInstruction)
1492 .map(inst -> (Instructions.GroupInstruction) inst)
1493 .map(Instructions.GroupInstruction::groupId)
1494 .map(GroupId::id)
1495 .map(Integer::toHexString)
1496 .map(id -> HEX_PREFIX + id)
1497 .collect(Collectors.toList());
1498
Yi Tseng78f51f42017-02-02 13:54:58 -08001499 log.debug("Removing buckets from group id 0x{} pointing to group id(s) {} "
Ray Milkey43869812018-05-04 13:00:39 -07001500 + "for next id {} in device {}",
1501 Integer.toHexString(modGroup.id().id()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001502 pointedGroupIds, nextObjective.id(), deviceId);
1503 addPendingUpdateNextObjective(modGroupKey, nextObjective);
Saurav Das1a129a02016-11-18 15:21:57 -08001504 groupService.removeBucketsFromGroup(deviceId, modGroupKey,
1505 removeBuckets, modGroupKey,
1506 nextObjective.appId());
Saurav Dasceccf242017-08-03 18:30:35 -07001507 // update store - synchronize access as there may be multiple threads
1508 // trying to remove buckets from the same group, each with its own
1509 // potentially stale copy of allActiveKeys
Saurav Dasc88d4662017-05-15 15:34:25 -07001510 synchronized (flowObjectiveStore) {
Saurav Dasceccf242017-08-03 18:30:35 -07001511 // get a fresh copy of what the store holds
1512 NextGroup next = flowObjectiveStore.getNextGroup(nextObjective.id());
1513 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Pier Luigid008dbe2018-01-15 09:48:49 +01001514 allActiveKeys = Lists.newArrayList(allActiveKeys);
Saurav Dasc88d4662017-05-15 15:34:25 -07001515 // Note that since we got a new object, and ArrayDeque does not implement
Saurav Dasceccf242017-08-03 18:30:35 -07001516 // Object.equals(), we have to check the deque elems one by one
1517 allActiveKeys
1518 .removeIf(active ->
1519 chainsToRemove.stream().anyMatch(remove ->
1520 Arrays.equals(remove.toArray(new GroupKey[0]),
1521 active.toArray(new GroupKey[0]))));
Saurav Dasc88d4662017-05-15 15:34:25 -07001522 // If no buckets in the group, then retain an entry for the
1523 // top level group which still exists.
1524 if (allActiveKeys.isEmpty()) {
1525 ArrayDeque<GroupKey> top = new ArrayDeque<>();
1526 top.add(modGroupKey);
1527 allActiveKeys.add(top);
1528 }
1529 flowObjectiveStore.putNextGroup(nextObjective.id(),
Saurav Dasceccf242017-08-03 18:30:35 -07001530 new OfdpaNextGroup(allActiveKeys,
1531 nextObjective));
Saurav Das1a129a02016-11-18 15:21:57 -08001532 }
Charles Chan188ebf52015-12-23 00:15:11 -08001533 }
1534
1535 /**
Saurav Das961beb22017-03-29 19:09:17 -07001536 * Removes all groups in multiple possible group-chains that represent the next-obj.
Charles Chan188ebf52015-12-23 00:15:11 -08001537 *
1538 * @param nextObjective the next objective to remove
1539 * @param next the NextGroup that represents the existing group-chain for
1540 * this next objective
1541 */
1542 protected void removeGroup(NextObjective nextObjective, NextGroup next) {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -07001543
Yi Tsengef19de12017-04-24 11:33:05 -07001544 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Charles Chanfc5c7802016-05-17 13:13:55 -07001545
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001546 List<GroupKey> groupKeys = allActiveKeys.stream()
Charles Chanfc5c7802016-05-17 13:13:55 -07001547 .map(Deque::getFirst).collect(Collectors.toList());
Yi Tsengef19de12017-04-24 11:33:05 -07001548 addPendingRemoveNextObjective(nextObjective, groupKeys);
Charles Chanfc5c7802016-05-17 13:13:55 -07001549
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -07001550 allActiveKeys
1551 .forEach(groupChain -> groupChain.forEach(groupKey -> groupService
1552 .removeGroup(deviceId, groupKey, nextObjective.appId())));
Charles Chan188ebf52015-12-23 00:15:11 -08001553 flowObjectiveStore.removeNextGroup(nextObjective.id());
1554 }
1555
Saurav Dasceccf242017-08-03 18:30:35 -07001556 /**
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001557 * Modify buckets in the L2 interface group.
1558 *
1559 * @param nextObjective a next objective that contains information for the
1560 * buckets to be modified in the group
1561 * @param next the representation of the existing group-chains for this next
1562 * objective, from which the innermost group buckets to remove are determined
1563 */
1564 protected void modifyBucketFromGroup(NextObjective nextObjective, NextGroup next) {
1565 if (nextObjective.type() != NextObjective.Type.SIMPLE) {
1566 log.warn("ModifyBucketFromGroup cannot be applied to nextType:{} in dev:{} for next:{}",
1567 nextObjective.type(), deviceId, nextObjective.id());
1568 fail(nextObjective, ObjectiveError.UNSUPPORTED);
1569 return;
1570 }
1571
1572 VlanId assignedVlan = readVlanFromSelector(nextObjective.meta());
1573 if (assignedVlan == null) {
1574 log.warn("VLAN ID required by simple next obj is missing. Abort.");
1575 fail(nextObjective, ObjectiveError.BADPARAMS);
1576 return;
1577 }
1578
1579 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObjective, assignedVlan);
1580
1581 // There is only one L2 interface group in this case
1582 GroupDescription l2InterfaceGroupDesc = groupInfos.get(0).innerMostGroupDesc();
1583
1584 // Replace group bucket for L2 interface group
1585 groupService.setBucketsForGroup(deviceId,
1586 l2InterfaceGroupDesc.appCookie(),
1587 l2InterfaceGroupDesc.buckets(),
1588 l2InterfaceGroupDesc.appCookie(),
1589 l2InterfaceGroupDesc.appId());
1590
1591 // update store - synchronize access as there may be multiple threads
1592 // trying to remove buckets from the same group, each with its own
1593 // potentially stale copy of allActiveKeys
1594 synchronized (flowObjectiveStore) {
1595 List<Deque<GroupKey>> modifiedGroupKeys = Lists.newArrayList();
1596 ArrayDeque<GroupKey> top = new ArrayDeque<>();
1597 top.add(l2InterfaceGroupDesc.appCookie());
1598 modifiedGroupKeys.add(top);
1599
1600 flowObjectiveStore.putNextGroup(nextObjective.id(),
1601 new OfdpaNextGroup(modifiedGroupKeys,
1602 nextObjective));
1603 }
1604 }
1605
1606 /**
Saurav Dasceccf242017-08-03 18:30:35 -07001607 * Checks existing buckets in {@link NextGroup} to verify if they match
1608 * the buckets in the given {@link NextObjective}. Adds or removes buckets
1609 * to ensure that the buckets match up.
1610 *
1611 * @param nextObjective the next objective to verify
1612 * @param next the representation of the existing group which has to be
1613 * modified to match the given next objective
1614 */
1615 protected void verifyGroup(NextObjective nextObjective, NextGroup next) {
Pier Luigib72201b2018-01-25 16:16:02 +01001616 if (nextObjective.type() == NextObjective.Type.SIMPLE) {
1617 log.warn("verification not supported for indirect group");
Saurav Dasceccf242017-08-03 18:30:35 -07001618 fail(nextObjective, ObjectiveError.UNSUPPORTED);
1619 return;
1620 }
Charles Chan1dd75572018-05-08 11:49:05 -07001621 log.trace("Call to verify device:{} nextId:{}", deviceId, nextObjective.id());
Saurav Dasceccf242017-08-03 18:30:35 -07001622 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
1623 List<TrafficTreatment> bucketsToCreate = Lists.newArrayList();
1624 List<Integer> indicesToRemove = Lists.newArrayList();
Pier Luigiec6ac422018-01-29 10:30:59 +01001625
1626 // Iterating over the treatments of the next objective allows
1627 // to detect missing buckets and/or duplicate buckets (to be removed)
Saurav Dasceccf242017-08-03 18:30:35 -07001628 for (TrafficTreatment bkt : nextObjective.next()) {
1629 PortNumber portNumber = readOutPortFromTreatment(bkt);
1630 int label = readLabelFromTreatment(bkt);
1631 if (portNumber == null) {
1632 log.warn("treatment {} of next objective {} has no outport.. "
1633 + "cannot remove bucket from group in dev: {}", bkt,
1634 nextObjective.id(), deviceId);
1635 fail(nextObjective, ObjectiveError.BADPARAMS);
1636 return;
1637 }
1638 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1639 groupService, deviceId,
1640 portNumber, label);
1641 if (existing.isEmpty()) {
1642 // if it doesn't exist, mark this bucket for creation
1643 bucketsToCreate.add(bkt);
1644 }
1645 if (existing.size() > 1) {
1646 // if it exists but there are duplicates, mark the others for removal
1647 existing.remove(0);
1648 indicesToRemove.addAll(existing);
1649 }
1650 }
1651
Pier Luigiec6ac422018-01-29 10:30:59 +01001652 // Detect situation where the next data has more buckets
1653 // (not duplicates) respect to the next objective
Saurav Dasa4020382018-02-14 14:14:54 -08001654 if (allActiveKeys.size() > nextObjective.next().size() &&
1655 // ignore specific case of empty group
1656 !(nextObjective.next().size() == 0 && allActiveKeys.size() == 1
1657 && allActiveKeys.get(0).size() == 1)) {
Pier Luigiec6ac422018-01-29 10:30:59 +01001658 log.warn("Mismatch detected between next and flowobjstore for device {}: " +
Saurav Dasa4020382018-02-14 14:14:54 -08001659 "nextId:{}, nextObjective-size:{} next-size:{} .. correcting",
1660 deviceId, nextObjective.id(), nextObjective.next().size(),
1661 allActiveKeys.size());
1662 List<Integer> otherIndices =
1663 indicesToRemoveFromNextGroup(allActiveKeys, nextObjective,
1664 groupService, deviceId);
Pier Luigiec6ac422018-01-29 10:30:59 +01001665 // Filter out the indices not present
1666 otherIndices = otherIndices.stream()
1667 .filter(index -> !indicesToRemove.contains(index))
1668 .collect(Collectors.toList());
1669 // Add all to the final list
1670 indicesToRemove.addAll(otherIndices);
1671 }
1672
Charles Chan1dd75572018-05-08 11:49:05 -07001673 log.trace("Buckets to create {}", bucketsToCreate);
1674 log.trace("Indices to remove {}", indicesToRemove);
Pier Luigib72201b2018-01-25 16:16:02 +01001675
Saurav Dasceccf242017-08-03 18:30:35 -07001676 if (!bucketsToCreate.isEmpty()) {
1677 log.info("creating {} buckets as part of nextId: {} verification",
1678 bucketsToCreate.size(), nextObjective.id());
1679 //create a nextObjective only with these buckets
1680 NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder()
1681 .withId(nextObjective.id())
Pier Luigib72201b2018-01-25 16:16:02 +01001682 .withType(nextObjective.type())
Saurav Dasceccf242017-08-03 18:30:35 -07001683 .withMeta(nextObjective.meta())
1684 .fromApp(nextObjective.appId());
Pier Luigib72201b2018-01-25 16:16:02 +01001685 bucketsToCreate.forEach(nextObjBuilder::addTreatment);
1686 // According to the next type we call the proper add function
1687 if (nextObjective.type() == NextObjective.Type.HASHED) {
1688 addBucketToHashGroup(nextObjBuilder.addToExisting(), allActiveKeys);
1689 } else {
1690 addBucketToBroadcastGroup(nextObjBuilder.addToExisting(), allActiveKeys);
1691 }
Saurav Dasceccf242017-08-03 18:30:35 -07001692 }
1693
1694 if (!indicesToRemove.isEmpty()) {
1695 log.info("removing {} buckets as part of nextId: {} verification",
1696 indicesToRemove.size(), nextObjective.id());
1697 List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList();
1698 indicesToRemove.forEach(index -> chainsToRemove
1699 .add(allActiveKeys.get(index)));
1700 removeBucket(chainsToRemove, nextObjective);
1701 }
1702
Saurav Das9df5b7c2017-08-14 16:44:43 -07001703 if (bucketsToCreate.isEmpty() && indicesToRemove.isEmpty()) {
1704 // flowObjective store record is in-sync with nextObjective passed-in
1705 // Nevertheless groupStore may not be in sync due to bug in the store
1706 // - see CORD-1844. XXX When this bug is fixed, the rest of this verify
1707 // method will not be required.
Pier Luigib72201b2018-01-25 16:16:02 +01001708 GroupKey topGroupKey = allActiveKeys.get(0).peekFirst();
1709 Group topGroup = groupService.getGroup(deviceId, topGroupKey);
1710 int actualGroupSize = topGroup.buckets().buckets().size();
Saurav Das9df5b7c2017-08-14 16:44:43 -07001711 int objGroupSize = nextObjective.next().size();
1712 if (actualGroupSize != objGroupSize) {
1713 log.warn("Mismatch detected in device:{}, nextId:{}, nextObjective-size"
1714 + ":{} group-size:{} .. correcting", deviceId, nextObjective.id(),
1715 objGroupSize, actualGroupSize);
1716 }
1717 if (actualGroupSize > objGroupSize) {
Pier Luigib72201b2018-01-25 16:16:02 +01001718 // Group in the device has more chains
Saurav Das9df5b7c2017-08-14 16:44:43 -07001719 List<GroupBucket> bucketsToRemove = Lists.newArrayList();
1720 //check every bucket in the actual group
Pier Luigib72201b2018-01-25 16:16:02 +01001721 for (GroupBucket bucket : topGroup.buckets().buckets()) {
Saurav Das9df5b7c2017-08-14 16:44:43 -07001722 GroupInstruction g = (GroupInstruction) bucket.treatment()
1723 .allInstructions().iterator().next();
1724 GroupId gidToCheck = g.groupId(); // the group pointed to
1725 boolean matches = false;
1726 for (Deque<GroupKey> validChain : allActiveKeys) {
1727 if (validChain.size() < 2) {
1728 continue;
1729 }
1730 GroupKey pointedGroupKey = validChain.stream()
1731 .collect(Collectors.toList()).get(1);
1732 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
1733 if (pointedGroup != null && gidToCheck.equals(pointedGroup.id())) {
1734 matches = true;
1735 break;
1736 }
1737 }
1738 if (!matches) {
1739 log.warn("Removing bucket pointing to groupId:{}", gidToCheck);
1740 bucketsToRemove.add(bucket);
1741 }
1742 }
1743 // remove buckets for which there was no record in the obj store
1744 if (bucketsToRemove.isEmpty()) {
1745 log.warn("Mismatch detected but could not determine which"
1746 + "buckets to remove");
1747 } else {
1748 GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove);
Pier Luigib72201b2018-01-25 16:16:02 +01001749 groupService.removeBucketsFromGroup(deviceId, topGroupKey,
1750 removeBuckets, topGroupKey,
Saurav Das9df5b7c2017-08-14 16:44:43 -07001751 nextObjective.appId());
1752 }
1753 } else if (actualGroupSize < objGroupSize) {
Pier Luigib72201b2018-01-25 16:16:02 +01001754 // Group in the device has less chains
Saurav Das9df5b7c2017-08-14 16:44:43 -07001755 // should also add buckets not in group-store but in obj-store
1756 List<GroupBucket> bucketsToAdd = Lists.newArrayList();
1757 //check every bucket in the obj
1758 for (Deque<GroupKey> validChain : allActiveKeys) {
1759 if (validChain.size() < 2) {
1760 continue;
1761 }
1762 GroupKey pointedGroupKey = validChain.stream()
1763 .collect(Collectors.toList()).get(1);
1764 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
1765 if (pointedGroup == null) {
1766 // group should exist, otherwise cannot be added as bucket
1767 continue;
1768 }
1769 boolean matches = false;
Pier Luigib72201b2018-01-25 16:16:02 +01001770 for (GroupBucket bucket : topGroup.buckets().buckets()) {
Saurav Das9df5b7c2017-08-14 16:44:43 -07001771 GroupInstruction g = (GroupInstruction) bucket.treatment()
1772 .allInstructions().iterator().next();
1773 GroupId gidToCheck = g.groupId(); // the group pointed to
1774 if (pointedGroup.id().equals(gidToCheck)) {
1775 matches = true;
1776 break;
1777 }
1778 }
1779 if (!matches) {
1780 log.warn("Adding bucket pointing to groupId:{}", pointedGroup);
1781 TrafficTreatment t = DefaultTrafficTreatment.builder()
1782 .group(pointedGroup.id())
1783 .build();
Pier Luigib72201b2018-01-25 16:16:02 +01001784 // Create the proper bucket according to the next type
1785 if (nextObjective.type() == NextObjective.Type.HASHED) {
1786 bucketsToAdd.add(DefaultGroupBucket.createSelectGroupBucket(t));
1787 } else {
1788 bucketsToAdd.add(DefaultGroupBucket.createAllGroupBucket(t));
1789 }
Saurav Das9df5b7c2017-08-14 16:44:43 -07001790 }
1791 }
1792 if (bucketsToAdd.isEmpty()) {
1793 log.warn("Mismatch detected but could not determine which "
1794 + "buckets to add");
1795 } else {
1796 GroupBuckets addBuckets = new GroupBuckets(bucketsToAdd);
Pier Luigib72201b2018-01-25 16:16:02 +01001797 groupService.addBucketsToGroup(deviceId, topGroupKey,
1798 addBuckets, topGroupKey,
Saurav Das9df5b7c2017-08-14 16:44:43 -07001799 nextObjective.appId());
1800 }
1801 }
1802 }
1803
Saurav Dasceccf242017-08-03 18:30:35 -07001804 pass(nextObjective);
1805 }
1806
1807 //////////////////////////////////////
1808 // Helper methods and classes
1809 //////////////////////////////////////
1810
Yi Tsengef19de12017-04-24 11:33:05 -07001811 protected void updatePendingNextObjective(GroupKey groupKey, OfdpaNextGroup nextGrp) {
1812 pendingAddNextObjectives.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07001813 if (val == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001814 val = new CopyOnWriteArrayList<>();
Saurav Das961beb22017-03-29 19:09:17 -07001815 }
Yi Tsengef19de12017-04-24 11:33:05 -07001816 val.add(nextGrp);
Saurav Das961beb22017-03-29 19:09:17 -07001817 return val;
1818 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08001819 }
1820
Yi Tsengef19de12017-04-24 11:33:05 -07001821 protected void updatePendingGroups(GroupKey groupKey, GroupChainElem gce) {
1822 pendingGroups.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07001823 if (val == null) {
1824 val = Sets.newConcurrentHashSet();
1825 }
1826 val.add(gce);
1827 return val;
1828 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08001829 }
1830
Yi Tsengef19de12017-04-24 11:33:05 -07001831 protected void addPendingUpdateNextObjective(GroupKey groupKey,
1832 NextObjective nextObjective) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001833 pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> {
1834 if (nextObjs != null) {
1835 nextObjs.add(nextObjective);
1836 } else {
1837 nextObjs = Sets.newHashSet(nextObjective);
1838 }
1839 return nextObjs;
1840 });
1841 }
1842
Saurav Das2f2c9d02018-04-07 16:51:09 -07001843 protected void addPendingRemoveNextObjective(NextObjective nextObjective,
1844 List<GroupKey> groupKeys) {
1845 pendingRemoveNextObjectives.put(nextObjective, groupKeys);
Yi Tsengef19de12017-04-24 11:33:05 -07001846 }
1847
Saurav Das2f2c9d02018-04-07 16:51:09 -07001848 protected int getNextAvailableIndex() {
1849 return (int) nextIndex.incrementAndGet();
1850 }
1851
1852 protected void processPendingUpdateNextObjs(GroupKey groupKey) {
1853 Set<NextObjective> nextObjs = pendingUpdateNextObjectives.remove(groupKey);
1854 if (nextObjs != null) {
1855 nextObjs.forEach(nextObj -> {
1856 log.debug("Group {} updated, update pending next objective {}.",
1857 groupKey, nextObj);
1858 pass(nextObj);
1859 });
1860 }
1861 }
1862
1863 protected void processPendingRemoveNextObjs(GroupKey key) {
Yi Tsengef19de12017-04-24 11:33:05 -07001864 pendingRemoveNextObjectives.asMap().forEach((nextObjective, groupKeys) -> {
jaegonkim2e4d99e2018-07-09 23:23:10 +09001865 groupKeys.remove(key);
Yi Tsengef19de12017-04-24 11:33:05 -07001866 if (groupKeys.isEmpty()) {
1867 pendingRemoveNextObjectives.invalidate(nextObjective);
1868 pass(nextObjective);
Yi Tsengef19de12017-04-24 11:33:05 -07001869 }
1870 });
1871 }
1872
Yi Tsengef19de12017-04-24 11:33:05 -07001873 protected void processPendingAddGroupsOrNextObjs(GroupKey key, boolean added) {
1874 //first check for group chain
1875 Set<OfdpaGroupHandlerUtility.GroupChainElem> gceSet = pendingGroups.asMap().remove(key);
1876 if (gceSet != null) {
1877 for (GroupChainElem gce : gceSet) {
1878 log.debug("Group service {} group key {} in device {}. "
1879 + "Processing next group in group chain with group id 0x{}",
1880 (added) ? "ADDED" : "processed",
1881 key, deviceId,
1882 Integer.toHexString(gce.groupDescription().givenGroupId()));
1883 processGroupChain(gce);
1884 }
1885 } else {
1886 // otherwise chain complete - check for waiting nextObjectives
1887 List<OfdpaGroupHandlerUtility.OfdpaNextGroup> nextGrpList =
1888 pendingAddNextObjectives.getIfPresent(key);
1889 if (nextGrpList != null) {
1890 pendingAddNextObjectives.invalidate(key);
1891 nextGrpList.forEach(nextGrp -> {
1892 log.debug("Group service {} group key {} in device:{}. "
1893 + "Done implementing next objective: {} <<-->> gid:0x{}",
1894 (added) ? "ADDED" : "processed",
1895 key, deviceId, nextGrp.nextObjective().id(),
1896 Integer.toHexString(groupService.getGroup(deviceId, key)
1897 .givenGroupId()));
1898 pass(nextGrp.nextObjective());
Saurav Dasc88d4662017-05-15 15:34:25 -07001899 updateFlowObjectiveStore(nextGrp.nextObjective().id(), nextGrp);
Yi Tsengef19de12017-04-24 11:33:05 -07001900
1901 // check if addBuckets waiting for this completion
1902 pendingBuckets.compute(nextGrp.nextObjective().id(), (nextId, pendBkts) -> {
1903 if (pendBkts != null) {
1904 pendBkts.forEach(pendBkt -> addBucketToGroup(pendBkt, nextGrp));
1905 }
1906 return null;
1907 });
1908 });
1909 }
1910 }
1911 }
1912
Charles Chan188ebf52015-12-23 00:15:11 -08001913 /**
1914 * Processes next element of a group chain. Assumption is that if this
1915 * group points to another group, the latter has already been created
1916 * and this driver has received notification for it. A second assumption is
1917 * that if there is another group waiting for this group then the appropriate
1918 * stores already have the information to act upon the notification for the
1919 * creation of this group.
1920 * <p>
1921 * The processing of the GroupChainElement depends on the number of groups
1922 * this element is waiting on. For all group types other than SIMPLE, a
1923 * GroupChainElement could be waiting on multiple groups.
1924 *
1925 * @param gce the group chain element to be processed next
1926 */
1927 private void processGroupChain(GroupChainElem gce) {
1928 int waitOnGroups = gce.decrementAndGetGroupsWaitedOn();
1929 if (waitOnGroups != 0) {
1930 log.debug("GCE: {} not ready to be processed", gce);
1931 return;
1932 }
1933 log.debug("GCE: {} ready to be processed", gce);
Yi Tsengef19de12017-04-24 11:33:05 -07001934 if (gce.addBucketToGroup()) {
1935 groupService.addBucketsToGroup(gce.groupDescription().deviceId(),
1936 gce.groupDescription().appCookie(),
1937 gce.groupDescription().buckets(),
1938 gce.groupDescription().appCookie(),
1939 gce.groupDescription().appId());
Charles Chan188ebf52015-12-23 00:15:11 -08001940 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001941 groupService.addGroup(gce.groupDescription());
Charles Chan188ebf52015-12-23 00:15:11 -08001942 }
1943 }
1944
Saurav Dasc88d4662017-05-15 15:34:25 -07001945 private void updateFlowObjectiveStore(Integer nextId, OfdpaNextGroup nextGrp) {
1946 synchronized (flowObjectiveStore) {
1947 // get fresh copy of what the store holds
1948 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1949 if (next == null || nextGrp.nextObjective().op() == Operation.ADD) {
1950 flowObjectiveStore.putNextGroup(nextId, nextGrp);
1951 return;
1952 }
1953 if (nextGrp.nextObjective().op() == Operation.ADD_TO_EXISTING) {
1954 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Pier Luigid008dbe2018-01-15 09:48:49 +01001955 allActiveKeys = Lists.newArrayList(allActiveKeys);
Saurav Dasc88d4662017-05-15 15:34:25 -07001956 // If active keys shows only the top-level group without a chain of groups,
1957 // then it represents an empty group. Update by replacing empty chain.
1958 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1959 allActiveKeys.clear();
1960 }
1961 allActiveKeys.addAll(nextGrp.allKeys());
1962 flowObjectiveStore.putNextGroup(nextId,
1963 new OfdpaNextGroup(allActiveKeys, nextGrp.nextObjective()));
1964 }
1965 }
1966 }
1967
Saurav Das8be4e3a2016-03-11 17:19:07 -08001968 private class InnerGroupListener implements GroupListener {
1969 @Override
1970 public void event(GroupEvent event) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001971 switch (event.type()) {
1972 case GROUP_ADDED:
1973 processPendingAddGroupsOrNextObjs(event.subject().appCookie(), true);
1974 break;
1975 case GROUP_REMOVED:
1976 processPendingRemoveNextObjs(event.subject().appCookie());
1977 break;
Yi Tseng78f51f42017-02-02 13:54:58 -08001978 case GROUP_UPDATED:
1979 processPendingUpdateNextObjs(event.subject().appCookie());
1980 break;
Charles Chanfc5c7802016-05-17 13:13:55 -07001981 default:
1982 break;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001983 }
1984 }
1985 }
Charles Chan188ebf52015-12-23 00:15:11 -08001986}