blob: c5204ce6d5f404ce705bef0f3af2fc1fbdeffbdd [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);
695 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700696
jayakumarthazhath655b9a82018-10-01 00:51:54 +0530697 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Yi Tsengef19de12017-04-24 11:33:05 -0700698 IpPrefix ipDst = readIpDstFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700699 if (ipDst != null) {
700 if (ipDst.isMulticast()) {
701 createL3MulticastGroup(nextObj, assignedVlan, groupInfos);
702 } else {
703 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -0700704 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700705 }
706 } else {
707 createL2FloodGroup(nextObj, assignedVlan, groupInfos);
708 }
709 }
710
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700711 private List<GroupInfo> prepareL2InterfaceGroup(NextObjective nextObj,
712 VlanId assignedVlan) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700713 ImmutableList.Builder<GroupInfo> groupInfoBuilder = ImmutableList.builder();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700714 // break up broadcast next objective to multiple groups
715 Collection<TrafficTreatment> buckets = nextObj.next();
Charles Chan188ebf52015-12-23 00:15:11 -0800716 // each treatment is converted to an L2 interface group
Charles Chan188ebf52015-12-23 00:15:11 -0800717 for (TrafficTreatment treatment : buckets) {
718 TrafficTreatment.Builder newTreatment = DefaultTrafficTreatment.builder();
719 PortNumber portNum = null;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700720 VlanId egressVlan = null;
Charles Chan188ebf52015-12-23 00:15:11 -0800721 // ensure that the only allowed treatments are pop-vlan and output
722 for (Instruction ins : treatment.allInstructions()) {
723 if (ins.type() == Instruction.Type.L2MODIFICATION) {
724 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
725 switch (l2ins.subtype()) {
726 case VLAN_POP:
727 newTreatment.add(l2ins);
728 break;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700729 case VLAN_ID:
730 egressVlan = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
731 break;
Charles Chan188ebf52015-12-23 00:15:11 -0800732 default:
733 log.debug("action {} not permitted for broadcast nextObj",
734 l2ins.subtype());
735 break;
736 }
737 } else if (ins.type() == Instruction.Type.OUTPUT) {
738 portNum = ((Instructions.OutputInstruction) ins).port();
739 newTreatment.add(ins);
740 } else {
Charles Chane849c192016-01-11 18:28:54 -0800741 log.debug("TrafficTreatment of type {} not permitted in " +
742 " broadcast nextObjective", ins.type());
Charles Chan188ebf52015-12-23 00:15:11 -0800743 }
744 }
Yi Tsengef19de12017-04-24 11:33:05 -0700745 if (portNum == null) {
746 log.warn("Can't find output port for the bucket {}.", treatment);
747 continue;
748 }
Charles Chan188ebf52015-12-23 00:15:11 -0800749 // assemble info for l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700750 VlanId l2InterfaceGroupVlan =
751 (egressVlan != null && !assignedVlan.equals(egressVlan)) ?
752 egressVlan : assignedVlan;
753 int l2gk = l2InterfaceGroupKey(deviceId, l2InterfaceGroupVlan, portNum.toLong());
754 final GroupKey l2InterfaceGroupKey =
Yi Tsengef19de12017-04-24 11:33:05 -0700755 new DefaultGroupKey(appKryo.serialize(l2gk));
Charles Chan372b63e2017-02-07 12:10:53 -0800756 int l2InterfaceGroupId = L2_INTERFACE_TYPE |
757 ((l2InterfaceGroupVlan.toShort() & THREE_BIT_MASK) << PORT_LEN) |
758 ((int) portNum.toLong() & FOUR_BIT_MASK);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700759 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800760 DefaultGroupBucket.createIndirectGroupBucket(newTreatment.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700761 GroupDescription l2InterfaceGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -0700762 new DefaultGroupDescription(deviceId,
763 GroupDescription.Type.INDIRECT,
764 new GroupBuckets(Collections.singletonList(
765 l2InterfaceGroupBucket)),
766 l2InterfaceGroupKey,
767 l2InterfaceGroupId,
768 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800769 log.debug("Trying L2-Interface: device:{} gid:{} gkey:{} nextid:{}",
Charles Chan5b9df8d2016-03-28 22:21:40 -0700770 deviceId, Integer.toHexString(l2InterfaceGroupId),
771 l2InterfaceGroupKey, nextObj.id());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700772 groupInfoBuilder.add(new GroupInfo(l2InterfaceGroupDescription,
773 l2InterfaceGroupDescription));
Charles Chan188ebf52015-12-23 00:15:11 -0800774 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700775 return groupInfoBuilder.build();
776 }
Charles Chan188ebf52015-12-23 00:15:11 -0800777
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700778 private void createL2FloodGroup(NextObjective nextObj, VlanId vlanId,
779 List<GroupInfo> groupInfos) {
780 // assemble info for l2 flood group. Since there can be only one flood
781 // group for a vlan, its index is always the same - 0
Yi Tsengef19de12017-04-24 11:33:05 -0700782 Integer l2FloodGroupId = L2_FLOOD_TYPE | (vlanId.toShort() << 16);
783 final GroupKey l2FloodGroupKey = l2FloodGroupKey(vlanId, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800784 // collection of group buckets pointing to all the l2 interface groups
Yi Tsengef19de12017-04-24 11:33:05 -0700785 List<GroupBucket> l2floodBuckets = generateNextGroupBuckets(groupInfos, ALL);
Charles Chan188ebf52015-12-23 00:15:11 -0800786 // create the l2flood group-description to wait for all the
787 // l2interface groups to be processed
788 GroupDescription l2floodGroupDescription =
789 new DefaultGroupDescription(
790 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800791 ALL,
Charles Chan188ebf52015-12-23 00:15:11 -0800792 new GroupBuckets(l2floodBuckets),
Yi Tsengef19de12017-04-24 11:33:05 -0700793 l2FloodGroupKey,
794 l2FloodGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800795 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800796 log.debug("Trying L2-Flood: device:{} gid:{} gkey:{} nextid:{}",
Yi Tsengef19de12017-04-24 11:33:05 -0700797 deviceId, Integer.toHexString(l2FloodGroupId),
798 l2FloodGroupKey, nextObj.id());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700799 // Put all dependency information into allGroupKeys
800 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
801 groupInfos.forEach(groupInfo -> {
Yi Tsengef19de12017-04-24 11:33:05 -0700802 Deque<GroupKey> groupKeyChain = new ArrayDeque<>();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700803 // In this case we should have L2 interface group only
Yi Tsengef19de12017-04-24 11:33:05 -0700804 groupKeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
805 groupKeyChain.addFirst(l2FloodGroupKey);
806 allGroupKeys.add(groupKeyChain);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700807 });
Charles Chan5b9df8d2016-03-28 22:21:40 -0700808 // Point the next objective to this group
809 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -0700810 updatePendingNextObjective(l2FloodGroupKey, ofdpaGrp);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700811 GroupChainElem gce = new GroupChainElem(l2floodGroupDescription,
Yi Tsengef19de12017-04-24 11:33:05 -0700812 groupInfos.size(), false, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700813 groupInfos.forEach(groupInfo -> {
814 // Point this group to the next group
Yi Tsengef19de12017-04-24 11:33:05 -0700815 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), gce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700816 // Start installing the inner-most group
Yi Tsengef19de12017-04-24 11:33:05 -0700817 groupService.addGroup(groupInfo.innerMostGroupDesc());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700818 });
819 }
820
Pier Luigi21fffd22018-01-19 10:24:53 +0100821 private void createL3MulticastGroup(NextObjective nextObj, VlanId vlanId,
822 List<GroupInfo> groupInfos) {
823 // Let's create a new list mcast buckets
824 List<GroupBucket> l3McastBuckets = createL3MulticastBucket(groupInfos);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700825
826 int l3MulticastIndex = getNextAvailableIndex();
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700827 int l3MulticastGroupId = L3_MULTICAST_TYPE |
828 vlanId.toShort() << 16 | (TYPE_VLAN_MASK & l3MulticastIndex);
829 final GroupKey l3MulticastGroupKey =
Yi Tsengef19de12017-04-24 11:33:05 -0700830 new DefaultGroupKey(appKryo.serialize(l3MulticastIndex));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700831
832 GroupDescription l3MulticastGroupDesc = new DefaultGroupDescription(deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800833 ALL,
Charles Chan5b9df8d2016-03-28 22:21:40 -0700834 new GroupBuckets(l3McastBuckets),
835 l3MulticastGroupKey,
836 l3MulticastGroupId,
837 nextObj.appId());
838
839 // Put all dependency information into allGroupKeys
840 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
841 groupInfos.forEach(groupInfo -> {
842 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700843 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700844 // Add L3 interface group to the chain if there is one.
Yi Tsengef19de12017-04-24 11:33:05 -0700845 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
846 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700847 }
848 gkeyChain.addFirst(l3MulticastGroupKey);
849 allGroupKeys.add(gkeyChain);
850 });
Charles Chan5b9df8d2016-03-28 22:21:40 -0700851 // Point the next objective to this group
852 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
853 updatePendingNextObjective(l3MulticastGroupKey, ofdpaGrp);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700854 GroupChainElem outerGce = new GroupChainElem(l3MulticastGroupDesc,
Yi Tsengef19de12017-04-24 11:33:05 -0700855 groupInfos.size(), false, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700856 groupInfos.forEach(groupInfo -> {
857 // Point this group (L3 multicast) to the next group
Yi Tsengef19de12017-04-24 11:33:05 -0700858 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), outerGce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700859 // Point next group to inner-most group, if any
Yi Tsengef19de12017-04-24 11:33:05 -0700860 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
861 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(),
862 1, false, deviceId);
863 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700864 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700865 // Start installing the inner-most group
Yi Tsengef19de12017-04-24 11:33:05 -0700866 groupService.addGroup(groupInfo.innerMostGroupDesc());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700867 });
Charles Chan188ebf52015-12-23 00:15:11 -0800868 }
869
Charles Chan188ebf52015-12-23 00:15:11 -0800870 /**
871 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
872 * a chain of groups. The hashed Next Objective passed in by the application
873 * has to be broken up into a group chain comprising of an
874 * L3 ECMP group as the top level group. Buckets of this group can point
875 * to a variety of groups in a group chain, depending on the whether
876 * MPLS labels are being pushed or not.
877 * <p>
878 * NOTE: We do not create MPLS ECMP groups as they are unimplemented in
879 * OF-DPA 2.0 (even though it is in the spec). Therefore we do not
880 * check the nextObjective meta to see what is matching before being
881 * sent to this nextObjective.
882 *
883 * @param nextObj the nextObjective of type HASHED
884 */
Pier Ventre140a8942016-11-02 07:26:38 -0700885 protected void processHashedNextObjective(NextObjective nextObj) {
Charles Chan188ebf52015-12-23 00:15:11 -0800886 // storage for all group keys in the chain of groups created
887 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
888 List<GroupInfo> unsentGroups = new ArrayList<>();
889 createHashBucketChains(nextObj, allGroupKeys, unsentGroups);
Charles Chan188ebf52015-12-23 00:15:11 -0800890 // now we can create the outermost L3 ECMP group
891 List<GroupBucket> l3ecmpGroupBuckets = new ArrayList<>();
892 for (GroupInfo gi : unsentGroups) {
893 // create ECMP bucket to point to the outer group
894 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Yi Tsengef19de12017-04-24 11:33:05 -0700895 ttb.group(new GroupId(gi.nextGroupDesc().givenGroupId()));
Charles Chan188ebf52015-12-23 00:15:11 -0800896 GroupBucket sbucket = DefaultGroupBucket
897 .createSelectGroupBucket(ttb.build());
898 l3ecmpGroupBuckets.add(sbucket);
899 }
Saurav Das8be4e3a2016-03-11 17:19:07 -0800900 int l3ecmpIndex = getNextAvailableIndex();
901 int l3ecmpGroupId = L3_ECMP_TYPE | (TYPE_MASK & l3ecmpIndex);
902 GroupKey l3ecmpGroupKey = new DefaultGroupKey(
Yi Tsengef19de12017-04-24 11:33:05 -0700903 appKryo.serialize(l3ecmpIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800904 GroupDescription l3ecmpGroupDesc =
905 new DefaultGroupDescription(
906 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800907 SELECT,
Charles Chan188ebf52015-12-23 00:15:11 -0800908 new GroupBuckets(l3ecmpGroupBuckets),
909 l3ecmpGroupKey,
910 l3ecmpGroupId,
911 nextObj.appId());
912 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
913 l3ecmpGroupBuckets.size(),
Yi Tsengef19de12017-04-24 11:33:05 -0700914 false, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800915
916 // create objects for local and distributed storage
Yi Tsengef19de12017-04-24 11:33:05 -0700917 allGroupKeys.forEach(gKeyChain -> gKeyChain.addFirst(l3ecmpGroupKey));
Charles Chan188ebf52015-12-23 00:15:11 -0800918 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Charles Chan188ebf52015-12-23 00:15:11 -0800919 // store l3ecmpGroupKey with the ofdpaGroupChain for the nextObjective
920 // that depends on it
921 updatePendingNextObjective(l3ecmpGroupKey, ofdpaGrp);
Charles Chan188ebf52015-12-23 00:15:11 -0800922 log.debug("Trying L3ECMP: device:{} gid:{} gkey:{} nextId:{}",
923 deviceId, Integer.toHexString(l3ecmpGroupId),
924 l3ecmpGroupKey, nextObj.id());
925 // finally we are ready to send the innermost groups
926 for (GroupInfo gi : unsentGroups) {
927 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -0700928 Integer.toHexString(gi.innerMostGroupDesc().givenGroupId()), deviceId);
929 updatePendingGroups(gi.nextGroupDesc().appCookie(), l3ecmpGce);
930 groupService.addGroup(gi.innerMostGroupDesc());
Charles Chan188ebf52015-12-23 00:15:11 -0800931 }
Charles Chan188ebf52015-12-23 00:15:11 -0800932 }
933
934 /**
935 * Creates group chains for all buckets in a hashed group, and stores the
936 * GroupInfos and GroupKeys for all the groups in the lists passed in, which
937 * should be empty.
938 * <p>
939 * Does not create the top level ECMP group. Does not actually send the
940 * groups to the groupService.
941 *
942 * @param nextObj the Next Objective with buckets that need to be converted
943 * to group chains
944 * @param allGroupKeys a list to store groupKey for each bucket-group-chain
945 * @param unsentGroups a list to store GroupInfo for each bucket-group-chain
946 */
Pier Ventre140a8942016-11-02 07:26:38 -0700947 protected void createHashBucketChains(NextObjective nextObj,
Yi Tsengef19de12017-04-24 11:33:05 -0700948 List<Deque<GroupKey>> allGroupKeys,
949 List<GroupInfo> unsentGroups) {
Charles Chan188ebf52015-12-23 00:15:11 -0800950 // break up hashed next objective to multiple groups
951 Collection<TrafficTreatment> buckets = nextObj.next();
952
953 for (TrafficTreatment bucket : buckets) {
954 //figure out how many labels are pushed in each bucket
955 int labelsPushed = 0;
956 MplsLabel innermostLabel = null;
957 for (Instruction ins : bucket.allInstructions()) {
958 if (ins.type() == Instruction.Type.L2MODIFICATION) {
959 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
960 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_PUSH) {
961 labelsPushed++;
962 }
963 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) {
964 if (innermostLabel == null) {
Yi Tsengef19de12017-04-24 11:33:05 -0700965 innermostLabel =
966 ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label();
Charles Chan188ebf52015-12-23 00:15:11 -0800967 }
968 }
969 }
970 }
Yi Tsengef19de12017-04-24 11:33:05 -0700971 Deque<GroupKey> gKeyChain = new ArrayDeque<>();
Saurav Dasa4020382018-02-14 14:14:54 -0800972 // here we only deal with 0 and 1 label push
Charles Chan188ebf52015-12-23 00:15:11 -0800973 if (labelsPushed == 0) {
Yi Tsengef19de12017-04-24 11:33:05 -0700974 GroupInfo noLabelGroupInfo;
Pier Ventre140a8942016-11-02 07:26:38 -0700975 TrafficSelector metaSelector = nextObj.meta();
976 if (metaSelector != null) {
977 if (isNotMplsBos(metaSelector)) {
Yi Tsengef19de12017-04-24 11:33:05 -0700978 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
979 nextObj.appId(), true,
980 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700981 } else {
Yi Tsengef19de12017-04-24 11:33:05 -0700982 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
983 nextObj.appId(), false,
984 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700985 }
986 } else {
Yi Tsengef19de12017-04-24 11:33:05 -0700987 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
988 nextObj.appId(), false,
989 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700990 }
Yi Tsengef19de12017-04-24 11:33:05 -0700991 if (noLabelGroupInfo == null) {
Charles Chan188ebf52015-12-23 00:15:11 -0800992 log.error("Could not process nextObj={} in dev:{}",
993 nextObj.id(), deviceId);
994 return;
995 }
Yi Tsengef19de12017-04-24 11:33:05 -0700996 gKeyChain.addFirst(noLabelGroupInfo.innerMostGroupDesc().appCookie());
997 gKeyChain.addFirst(noLabelGroupInfo.nextGroupDesc().appCookie());
Charles Chan188ebf52015-12-23 00:15:11 -0800998 // we can't send the inner group description yet, as we have to
999 // create the dependent ECMP group first. So we store..
Yi Tsengef19de12017-04-24 11:33:05 -07001000 unsentGroups.add(noLabelGroupInfo);
Charles Chan188ebf52015-12-23 00:15:11 -08001001 } else if (labelsPushed == 1) {
1002 GroupInfo onelabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
1003 nextObj.appId(), true,
1004 nextObj.meta());
1005 if (onelabelGroupInfo == null) {
1006 log.error("Could not process nextObj={} in dev:{}",
1007 nextObj.id(), deviceId);
1008 return;
1009 }
1010 // we need to add another group to this chain - the L3VPN group
1011 TrafficTreatment.Builder l3vpnTtb = DefaultTrafficTreatment.builder();
Charles Chan0f43e472017-02-14 14:00:16 -08001012 if (requireVlanPopBeforeMplsPush()) {
1013 l3vpnTtb.popVlan();
1014 }
Charles Chan188ebf52015-12-23 00:15:11 -08001015 l3vpnTtb.pushMpls()
1016 .setMpls(innermostLabel)
Yi Tsengef19de12017-04-24 11:33:05 -07001017 .group(new GroupId(onelabelGroupInfo.nextGroupDesc().givenGroupId()));
Charles Chan40132b32017-01-22 00:19:37 -08001018 if (supportCopyTtl()) {
1019 l3vpnTtb.copyTtlOut();
1020 }
1021 if (supportSetMplsBos()) {
1022 l3vpnTtb.setMplsBos(true);
1023 }
Charles Chan0f43e472017-02-14 14:00:16 -08001024 if (requireVlanPopBeforeMplsPush()) {
1025 l3vpnTtb.pushVlan().setVlanId(VlanId.vlanId(VlanId.RESERVED));
1026 }
Charles Chan188ebf52015-12-23 00:15:11 -08001027 GroupBucket l3vpnGrpBkt =
1028 DefaultGroupBucket.createIndirectGroupBucket(l3vpnTtb.build());
Saurav Das8be4e3a2016-03-11 17:19:07 -08001029 int l3vpnIndex = getNextAvailableIndex();
Yi Tsengef19de12017-04-24 11:33:05 -07001030 int l3vpnGroupId = MPLS_L3VPN_SUBTYPE | (SUBTYPE_MASK & l3vpnIndex);
1031 GroupKey l3vpnGroupKey = new DefaultGroupKey(
1032 appKryo.serialize(l3vpnIndex));
Charles Chan188ebf52015-12-23 00:15:11 -08001033 GroupDescription l3vpnGroupDesc =
1034 new DefaultGroupDescription(
1035 deviceId,
1036 GroupDescription.Type.INDIRECT,
Yi Tsengef19de12017-04-24 11:33:05 -07001037 new GroupBuckets(Collections.singletonList(l3vpnGrpBkt)),
1038 l3vpnGroupKey,
1039 l3vpnGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -08001040 nextObj.appId());
Yi Tsengef19de12017-04-24 11:33:05 -07001041 GroupChainElem l3vpnGce = new GroupChainElem(l3vpnGroupDesc,
1042 1,
1043 false,
1044 deviceId);
1045 updatePendingGroups(onelabelGroupInfo.nextGroupDesc().appCookie(), l3vpnGce);
Charles Chan188ebf52015-12-23 00:15:11 -08001046
Yi Tsengef19de12017-04-24 11:33:05 -07001047 gKeyChain.addFirst(onelabelGroupInfo.innerMostGroupDesc().appCookie());
1048 gKeyChain.addFirst(onelabelGroupInfo.nextGroupDesc().appCookie());
1049 gKeyChain.addFirst(l3vpnGroupKey);
Charles Chan188ebf52015-12-23 00:15:11 -08001050 //now we can replace the outerGrpDesc with the one we just created
Yi Tsengef19de12017-04-24 11:33:05 -07001051 onelabelGroupInfo.nextGroupDesc(l3vpnGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -08001052 // we can't send the innermost group yet, as we have to create
1053 // the dependent ECMP group first. So we store ...
1054 unsentGroups.add(onelabelGroupInfo);
Yi Tsengef19de12017-04-24 11:33:05 -07001055 log.debug("Trying L3VPN: device:{} gid:{} group key:{} nextId:{}",
1056 deviceId, Integer.toHexString(l3vpnGroupId),
1057 l3vpnGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -08001058 } else {
1059 log.warn("Driver currently does not handle more than 1 MPLS "
1060 + "labels. Not processing nextObjective {}", nextObj.id());
1061 return;
1062 }
Charles Chan188ebf52015-12-23 00:15:11 -08001063 // all groups in this chain
Yi Tsengef19de12017-04-24 11:33:05 -07001064 allGroupKeys.add(gKeyChain);
Charles Chan188ebf52015-12-23 00:15:11 -08001065 }
1066 }
1067
Pier Ventre42287df2016-11-09 14:17:26 -08001068 /**
1069 * Processes the pseudo wire related next objective.
1070 * This procedure try to reuse the mpls label groups,
1071 * the mpls interface group and the l2 interface group.
1072 *
1073 * @param nextObjective the objective to process.
1074 */
1075 protected void processPwNextObjective(NextObjective nextObjective) {
Saurav Das1547b3f2017-05-05 17:01:08 -07001076 log.warn("Pseudo wire extensions are not supported in OFDPA 2.0 {}",
1077 nextObjective.id());
Pier Ventre42287df2016-11-09 14:17:26 -08001078 }
1079
Saurav Das8be4e3a2016-03-11 17:19:07 -08001080 //////////////////////////////////////
1081 // Group Editing
1082 //////////////////////////////////////
Charles Chan188ebf52015-12-23 00:15:11 -08001083 /**
1084 * Adds a bucket to the top level group of a group-chain, and creates the chain.
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001085 * Ensures that bucket being added is not a duplicate, by checking existing
Yi Tsengef19de12017-04-24 11:33:05 -07001086 * buckets for the same output port.
Charles Chan188ebf52015-12-23 00:15:11 -08001087 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001088 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -08001089 * @param next the representation of the existing group-chain for this next objective
1090 */
1091 protected void addBucketToGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001092 if (nextObjective.type() != NextObjective.Type.HASHED &&
1093 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001094 log.warn("AddBuckets not applied to nextType:{} in dev:{} for next:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001095 nextObjective.type(), deviceId, nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -07001096 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001097 return;
1098 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001099 // first check to see if bucket being added is not a duplicate of an
Yi Tsengef19de12017-04-24 11:33:05 -07001100 // existing bucket. If it is for an existing output port, then its a
1101 // duplicate.
Yi Tseng78f51f42017-02-02 13:54:58 -08001102 Set<TrafficTreatment> duplicateBuckets = Sets.newHashSet();
Yi Tsengef19de12017-04-24 11:33:05 -07001103 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
1104 Set<PortNumber> existingPorts = getExistingOutputPorts(allActiveKeys,
1105 groupService,
1106 deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -08001107 Set<TrafficTreatment> nonDuplicateBuckets = Sets.newHashSet();
1108 NextObjective objectiveToAdd;
Yi Tseng78f51f42017-02-02 13:54:58 -08001109 nextObjective.next().forEach(trafficTreatment -> {
1110 PortNumber portNumber = readOutPortFromTreatment(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001111 if (portNumber == null) {
1112 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001113 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001114 if (existingPorts.contains(portNumber)) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001115 // its possible that portnumbers are same but labels are different
1116 int label = readLabelFromTreatment(trafficTreatment);
1117 if (label == -1) {
1118 duplicateBuckets.add(trafficTreatment);
1119 } else {
Saurav Dasceccf242017-08-03 18:30:35 -07001120 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1121 groupService, deviceId,
1122 portNumber, label);
1123 if (!existing.isEmpty()) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001124 duplicateBuckets.add(trafficTreatment);
1125 } else {
1126 nonDuplicateBuckets.add(trafficTreatment);
1127 }
1128 }
Yi Tseng47f82dc2017-03-05 22:48:39 -08001129 } else {
1130 nonDuplicateBuckets.add(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001131 }
1132 });
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001133 if (duplicateBuckets.isEmpty()) {
1134 // use the original objective
1135 objectiveToAdd = nextObjective;
1136 } else if (!nonDuplicateBuckets.isEmpty()) {
1137 // only use the non-duplicate buckets if there are any
1138 log.debug("Some buckets {} already exist in next id {}, duplicate "
1139 + "buckets will be ignored.", duplicateBuckets, nextObjective.id());
1140 // new next objective with non duplicate treatments
Yi Tseng47f82dc2017-03-05 22:48:39 -08001141 NextObjective.Builder builder = DefaultNextObjective.builder()
1142 .withType(nextObjective.type())
1143 .withId(nextObjective.id())
1144 .withMeta(nextObjective.meta())
1145 .fromApp(nextObjective.appId());
1146 nonDuplicateBuckets.forEach(builder::addTreatment);
Yi Tseng47f82dc2017-03-05 22:48:39 -08001147 ObjectiveContext context = nextObjective.context().orElse(null);
1148 objectiveToAdd = builder.addToExisting(context);
1149 } else {
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001150 // buckets to add are already there - nothing to do
Saurav Das1547b3f2017-05-05 17:01:08 -07001151 log.debug("buckets already exist {} in next: {} ..ignoring bucket add",
1152 duplicateBuckets, nextObjective.id());
1153 pass(nextObjective);
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001154 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001155 }
Saurav Das1a129a02016-11-18 15:21:57 -08001156 if (nextObjective.type() == NextObjective.Type.HASHED) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001157 addBucketToHashGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001158 } else if (nextObjective.type() == NextObjective.Type.BROADCAST) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001159 addBucketToBroadcastGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001160 }
1161 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001162
Saurav Das1a129a02016-11-18 15:21:57 -08001163 private void addBucketToHashGroup(NextObjective nextObjective,
Yi Tseng78f51f42017-02-02 13:54:58 -08001164 List<Deque<GroupKey>> allActiveKeys) {
Charles Chan188ebf52015-12-23 00:15:11 -08001165 // storage for all group keys in the chain of groups created
1166 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
1167 List<GroupInfo> unsentGroups = new ArrayList<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001168 List<GroupBucket> newBuckets;
Charles Chan188ebf52015-12-23 00:15:11 -08001169 createHashBucketChains(nextObjective, allGroupKeys, unsentGroups);
Yi Tseng78f51f42017-02-02 13:54:58 -08001170 // now we can create the buckets to add to the outermost L3 ECMP group
1171 newBuckets = generateNextGroupBuckets(unsentGroups, SELECT);
Saurav Das1a129a02016-11-18 15:21:57 -08001172 // retrieve the original L3 ECMP group
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001173 Group l3ecmpGroup = retrieveTopLevelGroup(allActiveKeys, deviceId,
1174 groupService, nextObjective.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001175 if (l3ecmpGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001176 fail(nextObjective, ObjectiveError.GROUPMISSING);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001177 return;
1178 }
Saurav Das1a129a02016-11-18 15:21:57 -08001179 GroupKey l3ecmpGroupKey = l3ecmpGroup.appCookie();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001180 int l3ecmpGroupId = l3ecmpGroup.id().id();
Charles Chan188ebf52015-12-23 00:15:11 -08001181 // Although GroupDescriptions are not necessary for adding buckets to
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001182 // existing groups, we still use one in the GroupChainElem. When the latter is
Charles Chan188ebf52015-12-23 00:15:11 -08001183 // processed, the info will be extracted for the bucketAdd call to groupService
1184 GroupDescription l3ecmpGroupDesc =
Yi Tsengef19de12017-04-24 11:33:05 -07001185 new DefaultGroupDescription(deviceId,
1186 SELECT,
1187 new GroupBuckets(newBuckets),
1188 l3ecmpGroupKey,
1189 l3ecmpGroupId,
1190 nextObjective.appId());
Yi Tseng78f51f42017-02-02 13:54:58 -08001191 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
1192 unsentGroups.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001193 true,
1194 deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001195
Saurav Dasc88d4662017-05-15 15:34:25 -07001196 // update new bucket-chains
1197 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
1198 for (Deque<GroupKey> newBucketChain : allGroupKeys) {
1199 newBucketChain.addFirst(l3ecmpGroupKey);
1200 addedKeys.add(newBucketChain);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001201 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001202 updatePendingNextObjective(l3ecmpGroupKey,
1203 new OfdpaNextGroup(addedKeys, nextObjective));
Yi Tsengef19de12017-04-24 11:33:05 -07001204 log.debug("Adding to L3ECMP: device:{} gid:{} group key:{} nextId:{}",
Charles Chan188ebf52015-12-23 00:15:11 -08001205 deviceId, Integer.toHexString(l3ecmpGroupId),
1206 l3ecmpGroupKey, nextObjective.id());
Yi Tseng78f51f42017-02-02 13:54:58 -08001207 unsentGroups.forEach(groupInfo -> {
1208 // send the innermost group
1209 log.debug("Sending innermost group {} in group chain on device {} ",
Saurav Dasc88d4662017-05-15 15:34:25 -07001210 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
1211 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001212 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3ecmpGce);
1213 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001214 });
Saurav Das1a129a02016-11-18 15:21:57 -08001215 }
Charles Chan188ebf52015-12-23 00:15:11 -08001216
Saurav Das1a129a02016-11-18 15:21:57 -08001217 private void addBucketToBroadcastGroup(NextObjective nextObj,
Yi Tsengef19de12017-04-24 11:33:05 -07001218 List<Deque<GroupKey>> allActiveKeys) {
1219 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001220 if (assignedVlan == null) {
1221 log.warn("VLAN ID required by broadcast next obj is missing. "
1222 + "Aborting add bucket to broadcast group for next:{} in dev:{}",
1223 nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001224 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001225 return;
1226 }
Saurav Das1a129a02016-11-18 15:21:57 -08001227 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Yi Tsengef19de12017-04-24 11:33:05 -07001228 IpPrefix ipDst = readIpDstFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001229 if (ipDst != null) {
1230 if (ipDst.isMulticast()) {
Yi Tsengef19de12017-04-24 11:33:05 -07001231 addBucketToL3MulticastGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001232 } else {
1233 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001234 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001235 }
1236 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001237 addBucketToL2FloodGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001238 }
1239 }
1240
1241 private void addBucketToL2FloodGroup(NextObjective nextObj,
1242 List<Deque<GroupKey>> allActiveKeys,
1243 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001244 VlanId assignedVlan) {
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001245 Group l2FloodGroup = retrieveTopLevelGroup(allActiveKeys, deviceId,
1246 groupService, nextObj.id());
Yi Tseng78f51f42017-02-02 13:54:58 -08001247
1248 if (l2FloodGroup == null) {
1249 log.warn("Can't find L2 flood group while adding bucket to it. NextObj = {}",
1250 nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001251 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001252 return;
1253 }
Saurav Das1a129a02016-11-18 15:21:57 -08001254
Yi Tseng78f51f42017-02-02 13:54:58 -08001255 GroupKey l2floodGroupKey = l2FloodGroup.appCookie();
1256 int l2floodGroupId = l2FloodGroup.id().id();
1257 List<GroupBucket> newBuckets = generateNextGroupBuckets(groupInfos, ALL);
Yi Tseng78f51f42017-02-02 13:54:58 -08001258 GroupDescription l2FloodGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001259 new DefaultGroupDescription(deviceId,
1260 ALL,
1261 new GroupBuckets(newBuckets),
1262 l2floodGroupKey,
1263 l2floodGroupId,
1264 nextObj.appId());
Yi Tseng78f51f42017-02-02 13:54:58 -08001265 GroupChainElem l2FloodGroupChainElement =
1266 new GroupChainElem(l2FloodGroupDescription,
1267 groupInfos.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001268 true,
1269 deviceId);
Yi Tseng78f51f42017-02-02 13:54:58 -08001270
Yi Tseng78f51f42017-02-02 13:54:58 -08001271
1272 //ensure assignedVlan applies to the chosen group
1273 VlanId floodGroupVlan = extractVlanIdFromGroupId(l2floodGroupId);
Yi Tseng78f51f42017-02-02 13:54:58 -08001274 if (!floodGroupVlan.equals(assignedVlan)) {
1275 log.warn("VLAN ID {} does not match Flood group {} to which bucket is "
1276 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1277 Integer.toHexString(l2floodGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001278 fail(nextObj, ObjectiveError.BADPARAMS);
Yi Tseng78f51f42017-02-02 13:54:58 -08001279 return;
1280 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001281 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
Yi Tseng78f51f42017-02-02 13:54:58 -08001282 groupInfos.forEach(groupInfo -> {
1283 // update original NextGroup with new bucket-chain
Yi Tseng78f51f42017-02-02 13:54:58 -08001284 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001285 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001286 newBucketChain.addFirst(l2floodGroupKey);
Saurav Dasc88d4662017-05-15 15:34:25 -07001287 addedKeys.add(newBucketChain);
Yi Tsengef19de12017-04-24 11:33:05 -07001288 log.debug("Adding to L2FLOOD: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001289 deviceId, Integer.toHexString(l2floodGroupId),
1290 l2floodGroupKey, nextObj.id());
1291 // send the innermost group
1292 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001293 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001294 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001295 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l2FloodGroupChainElement);
Yi Tseng78f51f42017-02-02 13:54:58 -08001296
Yi Tsengef19de12017-04-24 11:33:05 -07001297 DeviceId innerMostGroupDevice = groupInfo.innerMostGroupDesc().deviceId();
1298 GroupKey innerMostGroupKey = groupInfo.innerMostGroupDesc().appCookie();
Yi Tseng78f51f42017-02-02 13:54:58 -08001299 Group existsL2IGroup = groupService.getGroup(innerMostGroupDevice, innerMostGroupKey);
1300
1301 if (existsL2IGroup != null) {
1302 // group already exist
1303 processPendingAddGroupsOrNextObjs(innerMostGroupKey, true);
1304 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001305 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001306 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001307 });
Saurav Dasc88d4662017-05-15 15:34:25 -07001308
1309 updatePendingNextObjective(l2floodGroupKey,
1310 new OfdpaNextGroup(addedKeys, nextObj));
Yi Tseng78f51f42017-02-02 13:54:58 -08001311 }
1312
Saurav Das1a129a02016-11-18 15:21:57 -08001313 private void addBucketToL3MulticastGroup(NextObjective nextObj,
1314 List<Deque<GroupKey>> allActiveKeys,
1315 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001316 VlanId assignedVlan) {
Pier Luigi21fffd22018-01-19 10:24:53 +01001317 // Create the buckets to add to the outermost L3 Multicast group
1318 List<GroupBucket> newBuckets = createL3MulticastBucket(groupInfos);
Saurav Das1a129a02016-11-18 15:21:57 -08001319
1320 // get the group being edited
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001321 Group l3mcastGroup = retrieveTopLevelGroup(allActiveKeys, deviceId,
1322 groupService, nextObj.id());
Saurav Das1a129a02016-11-18 15:21:57 -08001323 if (l3mcastGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001324 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001325 return;
1326 }
1327 GroupKey l3mcastGroupKey = l3mcastGroup.appCookie();
1328 int l3mcastGroupId = l3mcastGroup.id().id();
1329
1330 //ensure assignedVlan applies to the chosen group
Yi Tseng78f51f42017-02-02 13:54:58 -08001331 VlanId expectedVlan = extractVlanIdFromGroupId(l3mcastGroupId);
Saurav Das1a129a02016-11-18 15:21:57 -08001332 if (!expectedVlan.equals(assignedVlan)) {
1333 log.warn("VLAN ID {} does not match L3 Mcast group {} to which bucket is "
1334 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1335 Integer.toHexString(l3mcastGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001336 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001337 }
1338 GroupDescription l3mcastGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001339 new DefaultGroupDescription(deviceId,
1340 ALL,
1341 new GroupBuckets(newBuckets),
1342 l3mcastGroupKey,
1343 l3mcastGroupId,
1344 nextObj.appId());
Saurav Das1a129a02016-11-18 15:21:57 -08001345 GroupChainElem l3mcastGce = new GroupChainElem(l3mcastGroupDescription,
Yi Tsengef19de12017-04-24 11:33:05 -07001346 groupInfos.size(),
1347 true,
1348 deviceId);
Saurav Dasc88d4662017-05-15 15:34:25 -07001349
1350 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
Yi Tseng78f51f42017-02-02 13:54:58 -08001351 groupInfos.forEach(groupInfo -> {
1352 // update original NextGroup with new bucket-chain
1353 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001354 newBucketChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001355 // Add L3 interface group to the chain if there is one.
Yi Tsengef19de12017-04-24 11:33:05 -07001356 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1357 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001358 }
1359 newBucketChain.addFirst(l3mcastGroupKey);
Saurav Dasc88d4662017-05-15 15:34:25 -07001360 addedKeys.add(newBucketChain);
Yi Tseng78f51f42017-02-02 13:54:58 -08001361
Yi Tsengef19de12017-04-24 11:33:05 -07001362 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3mcastGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001363 // Point next group to inner-most group, if any
Yi Tsengef19de12017-04-24 11:33:05 -07001364 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1365 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(),
1366 1,
1367 false,
1368 deviceId);
1369 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001370 }
Yi Tsengef19de12017-04-24 11:33:05 -07001371 log.debug("Adding to L3MCAST: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001372 deviceId, Integer.toHexString(l3mcastGroupId),
1373 l3mcastGroupKey, nextObj.id());
1374 // send the innermost group
1375 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001376 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001377 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001378 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001379
1380 });
1381
Saurav Das1a129a02016-11-18 15:21:57 -08001382 updatePendingNextObjective(l3mcastGroupKey,
Saurav Dasc88d4662017-05-15 15:34:25 -07001383 new OfdpaNextGroup(addedKeys, nextObj));
Charles Chan188ebf52015-12-23 00:15:11 -08001384 }
1385
1386 /**
Saurav Dasceccf242017-08-03 18:30:35 -07001387 * Removes buckets in the top level group of a possible group-chain. Does
Saurav Das1a129a02016-11-18 15:21:57 -08001388 * not remove the groups in the group-chain pointed to by this bucket, as they
Charles Chan188ebf52015-12-23 00:15:11 -08001389 * may be in use (referenced by other groups) elsewhere.
1390 *
Saurav Dasceccf242017-08-03 18:30:35 -07001391 * @param nextObjective a next objective that contains information for the
1392 * buckets to be removed from the group
1393 * @param next the representation of the existing group-chains for this next
1394 * objective, from which the top-level buckets to remove are determined
Charles Chan188ebf52015-12-23 00:15:11 -08001395 */
1396 protected void removeBucketFromGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001397 if (nextObjective.type() != NextObjective.Type.HASHED &&
1398 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001399 log.warn("RemoveBuckets not applied to nextType:{} in dev:{} for next:{}",
1400 nextObjective.type(), deviceId, nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -07001401 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001402 return;
1403 }
Yi Tsengef19de12017-04-24 11:33:05 -07001404 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Saurav Dasceccf242017-08-03 18:30:35 -07001405 List<Integer> indicesToRemove = Lists.newArrayList();
1406 for (TrafficTreatment treatment : nextObjective.next()) {
1407 // find the top-level bucket in the group-chain by matching the
1408 // outport and label from different groups in the chain
1409 PortNumber portToRemove = readOutPortFromTreatment(treatment);
1410 int labelToRemove = readLabelFromTreatment(treatment);
1411 if (portToRemove == null) {
1412 log.warn("treatment {} of next objective {} has no outport.. "
1413 + "cannot remove bucket from group in dev: {}", treatment,
1414 nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001415 continue;
1416 }
Saurav Dasceccf242017-08-03 18:30:35 -07001417 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1418 groupService, deviceId,
1419 portToRemove, labelToRemove);
1420 indicesToRemove.addAll(existing);
1421
Charles Chan188ebf52015-12-23 00:15:11 -08001422 }
Saurav Dasceccf242017-08-03 18:30:35 -07001423
1424 List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList();
1425 indicesToRemove.forEach(index -> chainsToRemove
1426 .add(allActiveKeys.get(index)));
Yi Tseng78f51f42017-02-02 13:54:58 -08001427 if (chainsToRemove.isEmpty()) {
Charles Chan188ebf52015-12-23 00:15:11 -08001428 log.warn("Could not find appropriate group-chain for removing bucket"
1429 + " for next id {} in dev:{}", nextObjective.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001430 fail(nextObjective, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001431 return;
Charles Chan188ebf52015-12-23 00:15:11 -08001432 }
Saurav Dasceccf242017-08-03 18:30:35 -07001433 removeBucket(chainsToRemove, nextObjective);
1434 }
1435
1436 /**
1437 * Removes top-level buckets from a group that represents the given next objective.
1438 *
1439 * @param chainsToRemove a list of group bucket chains to remove
1440 * @param nextObjective the next objective that contains information for the
1441 * buckets to be removed from the group
1442 */
1443 protected void removeBucket(List<Deque<GroupKey>> chainsToRemove,
1444 NextObjective nextObjective) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001445 List<GroupBucket> bucketsToRemove = Lists.newArrayList();
1446 //first group key is the one we want to modify
1447 GroupKey modGroupKey = chainsToRemove.get(0).peekFirst();
Saurav Das1a129a02016-11-18 15:21:57 -08001448 Group modGroup = groupService.getGroup(deviceId, modGroupKey);
Ray Milkey43869812018-05-04 13:00:39 -07001449 if (modGroup == null) {
1450 log.warn("removeBucket(): Attempt to modify non-existent group {} for device {}", modGroupKey, deviceId);
1451 return;
1452 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001453 for (Deque<GroupKey> foundChain : chainsToRemove) {
1454 //second group key is the one we wish to remove the reference to
1455 if (foundChain.size() < 2) {
Saurav Dasceccf242017-08-03 18:30:35 -07001456 // additional check to make sure second group key exists in
Yi Tseng78f51f42017-02-02 13:54:58 -08001457 // the chain.
1458 log.warn("Can't find second group key from chain {}",
1459 foundChain);
1460 continue;
Saurav Das1a129a02016-11-18 15:21:57 -08001461 }
Saurav Dasceccf242017-08-03 18:30:35 -07001462 GroupKey pointedGroupKey = foundChain.stream()
1463 .collect(Collectors.toList()).get(1);
Yi Tseng78f51f42017-02-02 13:54:58 -08001464 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
Yi Tseng78f51f42017-02-02 13:54:58 -08001465 if (pointedGroup == null) {
1466 continue;
1467 }
1468
1469 GroupBucket bucket;
1470 if (nextObjective.type() == NextObjective.Type.HASHED) {
1471 bucket = DefaultGroupBucket.createSelectGroupBucket(
1472 DefaultTrafficTreatment.builder()
1473 .group(pointedGroup.id())
1474 .build());
1475 } else {
1476 bucket = DefaultGroupBucket.createAllGroupBucket(
1477 DefaultTrafficTreatment.builder()
1478 .group(pointedGroup.id())
1479 .build());
1480 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001481 bucketsToRemove.add(bucket);
Saurav Das1a129a02016-11-18 15:21:57 -08001482 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001483
1484 GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove);
1485 List<String> pointedGroupIds; // for debug log
1486 pointedGroupIds = bucketsToRemove.stream()
1487 .map(GroupBucket::treatment)
1488 .map(TrafficTreatment::allInstructions)
1489 .flatMap(List::stream)
1490 .filter(inst -> inst instanceof Instructions.GroupInstruction)
1491 .map(inst -> (Instructions.GroupInstruction) inst)
1492 .map(Instructions.GroupInstruction::groupId)
1493 .map(GroupId::id)
1494 .map(Integer::toHexString)
1495 .map(id -> HEX_PREFIX + id)
1496 .collect(Collectors.toList());
1497
Yi Tseng78f51f42017-02-02 13:54:58 -08001498 log.debug("Removing buckets from group id 0x{} pointing to group id(s) {} "
Ray Milkey43869812018-05-04 13:00:39 -07001499 + "for next id {} in device {}",
1500 Integer.toHexString(modGroup.id().id()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001501 pointedGroupIds, nextObjective.id(), deviceId);
1502 addPendingUpdateNextObjective(modGroupKey, nextObjective);
Saurav Das1a129a02016-11-18 15:21:57 -08001503 groupService.removeBucketsFromGroup(deviceId, modGroupKey,
1504 removeBuckets, modGroupKey,
1505 nextObjective.appId());
Saurav Dasceccf242017-08-03 18:30:35 -07001506 // update store - synchronize access as there may be multiple threads
1507 // trying to remove buckets from the same group, each with its own
1508 // potentially stale copy of allActiveKeys
Saurav Dasc88d4662017-05-15 15:34:25 -07001509 synchronized (flowObjectiveStore) {
Saurav Dasceccf242017-08-03 18:30:35 -07001510 // get a fresh copy of what the store holds
1511 NextGroup next = flowObjectiveStore.getNextGroup(nextObjective.id());
1512 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Pier Luigid008dbe2018-01-15 09:48:49 +01001513 allActiveKeys = Lists.newArrayList(allActiveKeys);
Saurav Dasc88d4662017-05-15 15:34:25 -07001514 // Note that since we got a new object, and ArrayDeque does not implement
Saurav Dasceccf242017-08-03 18:30:35 -07001515 // Object.equals(), we have to check the deque elems one by one
1516 allActiveKeys
1517 .removeIf(active ->
1518 chainsToRemove.stream().anyMatch(remove ->
1519 Arrays.equals(remove.toArray(new GroupKey[0]),
1520 active.toArray(new GroupKey[0]))));
Saurav Dasc88d4662017-05-15 15:34:25 -07001521 // If no buckets in the group, then retain an entry for the
1522 // top level group which still exists.
1523 if (allActiveKeys.isEmpty()) {
1524 ArrayDeque<GroupKey> top = new ArrayDeque<>();
1525 top.add(modGroupKey);
1526 allActiveKeys.add(top);
1527 }
1528 flowObjectiveStore.putNextGroup(nextObjective.id(),
Saurav Dasceccf242017-08-03 18:30:35 -07001529 new OfdpaNextGroup(allActiveKeys,
1530 nextObjective));
Saurav Das1a129a02016-11-18 15:21:57 -08001531 }
Charles Chan188ebf52015-12-23 00:15:11 -08001532 }
1533
1534 /**
Saurav Das961beb22017-03-29 19:09:17 -07001535 * Removes all groups in multiple possible group-chains that represent the next-obj.
Charles Chan188ebf52015-12-23 00:15:11 -08001536 *
1537 * @param nextObjective the next objective to remove
1538 * @param next the NextGroup that represents the existing group-chain for
1539 * this next objective
1540 */
1541 protected void removeGroup(NextObjective nextObjective, NextGroup next) {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -07001542
Yi Tsengef19de12017-04-24 11:33:05 -07001543 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Charles Chanfc5c7802016-05-17 13:13:55 -07001544
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001545 List<GroupKey> groupKeys = allActiveKeys.stream()
Charles Chanfc5c7802016-05-17 13:13:55 -07001546 .map(Deque::getFirst).collect(Collectors.toList());
Yi Tsengef19de12017-04-24 11:33:05 -07001547 addPendingRemoveNextObjective(nextObjective, groupKeys);
Charles Chanfc5c7802016-05-17 13:13:55 -07001548
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -07001549 allActiveKeys
1550 .forEach(groupChain -> groupChain.forEach(groupKey -> groupService
1551 .removeGroup(deviceId, groupKey, nextObjective.appId())));
Charles Chan188ebf52015-12-23 00:15:11 -08001552 flowObjectiveStore.removeNextGroup(nextObjective.id());
1553 }
1554
Saurav Dasceccf242017-08-03 18:30:35 -07001555 /**
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001556 * Modify buckets in the L2 interface group.
1557 *
1558 * @param nextObjective a next objective that contains information for the
1559 * buckets to be modified in the group
1560 * @param next the representation of the existing group-chains for this next
1561 * objective, from which the innermost group buckets to remove are determined
1562 */
1563 protected void modifyBucketFromGroup(NextObjective nextObjective, NextGroup next) {
1564 if (nextObjective.type() != NextObjective.Type.SIMPLE) {
1565 log.warn("ModifyBucketFromGroup cannot be applied to nextType:{} in dev:{} for next:{}",
1566 nextObjective.type(), deviceId, nextObjective.id());
1567 fail(nextObjective, ObjectiveError.UNSUPPORTED);
1568 return;
1569 }
1570
1571 VlanId assignedVlan = readVlanFromSelector(nextObjective.meta());
1572 if (assignedVlan == null) {
1573 log.warn("VLAN ID required by simple next obj is missing. Abort.");
1574 fail(nextObjective, ObjectiveError.BADPARAMS);
1575 return;
1576 }
1577
1578 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObjective, assignedVlan);
1579
1580 // There is only one L2 interface group in this case
1581 GroupDescription l2InterfaceGroupDesc = groupInfos.get(0).innerMostGroupDesc();
1582
1583 // Replace group bucket for L2 interface group
1584 groupService.setBucketsForGroup(deviceId,
1585 l2InterfaceGroupDesc.appCookie(),
1586 l2InterfaceGroupDesc.buckets(),
1587 l2InterfaceGroupDesc.appCookie(),
1588 l2InterfaceGroupDesc.appId());
1589
1590 // update store - synchronize access as there may be multiple threads
1591 // trying to remove buckets from the same group, each with its own
1592 // potentially stale copy of allActiveKeys
1593 synchronized (flowObjectiveStore) {
1594 List<Deque<GroupKey>> modifiedGroupKeys = Lists.newArrayList();
1595 ArrayDeque<GroupKey> top = new ArrayDeque<>();
1596 top.add(l2InterfaceGroupDesc.appCookie());
1597 modifiedGroupKeys.add(top);
1598
1599 flowObjectiveStore.putNextGroup(nextObjective.id(),
1600 new OfdpaNextGroup(modifiedGroupKeys,
1601 nextObjective));
1602 }
1603 }
1604
1605 /**
Saurav Dasceccf242017-08-03 18:30:35 -07001606 * Checks existing buckets in {@link NextGroup} to verify if they match
1607 * the buckets in the given {@link NextObjective}. Adds or removes buckets
1608 * to ensure that the buckets match up.
1609 *
1610 * @param nextObjective the next objective to verify
1611 * @param next the representation of the existing group which has to be
1612 * modified to match the given next objective
1613 */
1614 protected void verifyGroup(NextObjective nextObjective, NextGroup next) {
Pier Luigib72201b2018-01-25 16:16:02 +01001615 if (nextObjective.type() == NextObjective.Type.SIMPLE) {
1616 log.warn("verification not supported for indirect group");
Saurav Dasceccf242017-08-03 18:30:35 -07001617 fail(nextObjective, ObjectiveError.UNSUPPORTED);
1618 return;
1619 }
Charles Chan1dd75572018-05-08 11:49:05 -07001620 log.trace("Call to verify device:{} nextId:{}", deviceId, nextObjective.id());
Saurav Dasceccf242017-08-03 18:30:35 -07001621 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
1622 List<TrafficTreatment> bucketsToCreate = Lists.newArrayList();
1623 List<Integer> indicesToRemove = Lists.newArrayList();
Pier Luigiec6ac422018-01-29 10:30:59 +01001624
1625 // Iterating over the treatments of the next objective allows
1626 // to detect missing buckets and/or duplicate buckets (to be removed)
Saurav Dasceccf242017-08-03 18:30:35 -07001627 for (TrafficTreatment bkt : nextObjective.next()) {
1628 PortNumber portNumber = readOutPortFromTreatment(bkt);
1629 int label = readLabelFromTreatment(bkt);
1630 if (portNumber == null) {
1631 log.warn("treatment {} of next objective {} has no outport.. "
1632 + "cannot remove bucket from group in dev: {}", bkt,
1633 nextObjective.id(), deviceId);
1634 fail(nextObjective, ObjectiveError.BADPARAMS);
1635 return;
1636 }
1637 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1638 groupService, deviceId,
1639 portNumber, label);
1640 if (existing.isEmpty()) {
1641 // if it doesn't exist, mark this bucket for creation
1642 bucketsToCreate.add(bkt);
1643 }
1644 if (existing.size() > 1) {
1645 // if it exists but there are duplicates, mark the others for removal
1646 existing.remove(0);
1647 indicesToRemove.addAll(existing);
1648 }
1649 }
1650
Pier Luigiec6ac422018-01-29 10:30:59 +01001651 // Detect situation where the next data has more buckets
1652 // (not duplicates) respect to the next objective
Saurav Dasa4020382018-02-14 14:14:54 -08001653 if (allActiveKeys.size() > nextObjective.next().size() &&
1654 // ignore specific case of empty group
1655 !(nextObjective.next().size() == 0 && allActiveKeys.size() == 1
1656 && allActiveKeys.get(0).size() == 1)) {
Pier Luigiec6ac422018-01-29 10:30:59 +01001657 log.warn("Mismatch detected between next and flowobjstore for device {}: " +
Saurav Dasa4020382018-02-14 14:14:54 -08001658 "nextId:{}, nextObjective-size:{} next-size:{} .. correcting",
1659 deviceId, nextObjective.id(), nextObjective.next().size(),
1660 allActiveKeys.size());
1661 List<Integer> otherIndices =
1662 indicesToRemoveFromNextGroup(allActiveKeys, nextObjective,
1663 groupService, deviceId);
Pier Luigiec6ac422018-01-29 10:30:59 +01001664 // Filter out the indices not present
1665 otherIndices = otherIndices.stream()
1666 .filter(index -> !indicesToRemove.contains(index))
1667 .collect(Collectors.toList());
1668 // Add all to the final list
1669 indicesToRemove.addAll(otherIndices);
1670 }
1671
Charles Chan1dd75572018-05-08 11:49:05 -07001672 log.trace("Buckets to create {}", bucketsToCreate);
1673 log.trace("Indices to remove {}", indicesToRemove);
Pier Luigib72201b2018-01-25 16:16:02 +01001674
Saurav Dasceccf242017-08-03 18:30:35 -07001675 if (!bucketsToCreate.isEmpty()) {
1676 log.info("creating {} buckets as part of nextId: {} verification",
1677 bucketsToCreate.size(), nextObjective.id());
1678 //create a nextObjective only with these buckets
1679 NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder()
1680 .withId(nextObjective.id())
Pier Luigib72201b2018-01-25 16:16:02 +01001681 .withType(nextObjective.type())
Saurav Dasceccf242017-08-03 18:30:35 -07001682 .withMeta(nextObjective.meta())
1683 .fromApp(nextObjective.appId());
Pier Luigib72201b2018-01-25 16:16:02 +01001684 bucketsToCreate.forEach(nextObjBuilder::addTreatment);
1685 // According to the next type we call the proper add function
1686 if (nextObjective.type() == NextObjective.Type.HASHED) {
1687 addBucketToHashGroup(nextObjBuilder.addToExisting(), allActiveKeys);
1688 } else {
1689 addBucketToBroadcastGroup(nextObjBuilder.addToExisting(), allActiveKeys);
1690 }
Saurav Dasceccf242017-08-03 18:30:35 -07001691 }
1692
1693 if (!indicesToRemove.isEmpty()) {
1694 log.info("removing {} buckets as part of nextId: {} verification",
1695 indicesToRemove.size(), nextObjective.id());
1696 List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList();
1697 indicesToRemove.forEach(index -> chainsToRemove
1698 .add(allActiveKeys.get(index)));
1699 removeBucket(chainsToRemove, nextObjective);
1700 }
1701
Saurav Das9df5b7c2017-08-14 16:44:43 -07001702 if (bucketsToCreate.isEmpty() && indicesToRemove.isEmpty()) {
1703 // flowObjective store record is in-sync with nextObjective passed-in
1704 // Nevertheless groupStore may not be in sync due to bug in the store
1705 // - see CORD-1844. XXX When this bug is fixed, the rest of this verify
1706 // method will not be required.
Pier Luigib72201b2018-01-25 16:16:02 +01001707 GroupKey topGroupKey = allActiveKeys.get(0).peekFirst();
1708 Group topGroup = groupService.getGroup(deviceId, topGroupKey);
1709 int actualGroupSize = topGroup.buckets().buckets().size();
Saurav Das9df5b7c2017-08-14 16:44:43 -07001710 int objGroupSize = nextObjective.next().size();
1711 if (actualGroupSize != objGroupSize) {
1712 log.warn("Mismatch detected in device:{}, nextId:{}, nextObjective-size"
1713 + ":{} group-size:{} .. correcting", deviceId, nextObjective.id(),
1714 objGroupSize, actualGroupSize);
1715 }
1716 if (actualGroupSize > objGroupSize) {
Pier Luigib72201b2018-01-25 16:16:02 +01001717 // Group in the device has more chains
Saurav Das9df5b7c2017-08-14 16:44:43 -07001718 List<GroupBucket> bucketsToRemove = Lists.newArrayList();
1719 //check every bucket in the actual group
Pier Luigib72201b2018-01-25 16:16:02 +01001720 for (GroupBucket bucket : topGroup.buckets().buckets()) {
Saurav Das9df5b7c2017-08-14 16:44:43 -07001721 GroupInstruction g = (GroupInstruction) bucket.treatment()
1722 .allInstructions().iterator().next();
1723 GroupId gidToCheck = g.groupId(); // the group pointed to
1724 boolean matches = false;
1725 for (Deque<GroupKey> validChain : allActiveKeys) {
1726 if (validChain.size() < 2) {
1727 continue;
1728 }
1729 GroupKey pointedGroupKey = validChain.stream()
1730 .collect(Collectors.toList()).get(1);
1731 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
1732 if (pointedGroup != null && gidToCheck.equals(pointedGroup.id())) {
1733 matches = true;
1734 break;
1735 }
1736 }
1737 if (!matches) {
1738 log.warn("Removing bucket pointing to groupId:{}", gidToCheck);
1739 bucketsToRemove.add(bucket);
1740 }
1741 }
1742 // remove buckets for which there was no record in the obj store
1743 if (bucketsToRemove.isEmpty()) {
1744 log.warn("Mismatch detected but could not determine which"
1745 + "buckets to remove");
1746 } else {
1747 GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove);
Pier Luigib72201b2018-01-25 16:16:02 +01001748 groupService.removeBucketsFromGroup(deviceId, topGroupKey,
1749 removeBuckets, topGroupKey,
Saurav Das9df5b7c2017-08-14 16:44:43 -07001750 nextObjective.appId());
1751 }
1752 } else if (actualGroupSize < objGroupSize) {
Pier Luigib72201b2018-01-25 16:16:02 +01001753 // Group in the device has less chains
Saurav Das9df5b7c2017-08-14 16:44:43 -07001754 // should also add buckets not in group-store but in obj-store
1755 List<GroupBucket> bucketsToAdd = Lists.newArrayList();
1756 //check every bucket in the obj
1757 for (Deque<GroupKey> validChain : allActiveKeys) {
1758 if (validChain.size() < 2) {
1759 continue;
1760 }
1761 GroupKey pointedGroupKey = validChain.stream()
1762 .collect(Collectors.toList()).get(1);
1763 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
1764 if (pointedGroup == null) {
1765 // group should exist, otherwise cannot be added as bucket
1766 continue;
1767 }
1768 boolean matches = false;
Pier Luigib72201b2018-01-25 16:16:02 +01001769 for (GroupBucket bucket : topGroup.buckets().buckets()) {
Saurav Das9df5b7c2017-08-14 16:44:43 -07001770 GroupInstruction g = (GroupInstruction) bucket.treatment()
1771 .allInstructions().iterator().next();
1772 GroupId gidToCheck = g.groupId(); // the group pointed to
1773 if (pointedGroup.id().equals(gidToCheck)) {
1774 matches = true;
1775 break;
1776 }
1777 }
1778 if (!matches) {
1779 log.warn("Adding bucket pointing to groupId:{}", pointedGroup);
1780 TrafficTreatment t = DefaultTrafficTreatment.builder()
1781 .group(pointedGroup.id())
1782 .build();
Pier Luigib72201b2018-01-25 16:16:02 +01001783 // Create the proper bucket according to the next type
1784 if (nextObjective.type() == NextObjective.Type.HASHED) {
1785 bucketsToAdd.add(DefaultGroupBucket.createSelectGroupBucket(t));
1786 } else {
1787 bucketsToAdd.add(DefaultGroupBucket.createAllGroupBucket(t));
1788 }
Saurav Das9df5b7c2017-08-14 16:44:43 -07001789 }
1790 }
1791 if (bucketsToAdd.isEmpty()) {
1792 log.warn("Mismatch detected but could not determine which "
1793 + "buckets to add");
1794 } else {
1795 GroupBuckets addBuckets = new GroupBuckets(bucketsToAdd);
Pier Luigib72201b2018-01-25 16:16:02 +01001796 groupService.addBucketsToGroup(deviceId, topGroupKey,
1797 addBuckets, topGroupKey,
Saurav Das9df5b7c2017-08-14 16:44:43 -07001798 nextObjective.appId());
1799 }
1800 }
1801 }
1802
Saurav Dasceccf242017-08-03 18:30:35 -07001803 pass(nextObjective);
1804 }
1805
1806 //////////////////////////////////////
1807 // Helper methods and classes
1808 //////////////////////////////////////
1809
Yi Tsengef19de12017-04-24 11:33:05 -07001810 protected void updatePendingNextObjective(GroupKey groupKey, OfdpaNextGroup nextGrp) {
1811 pendingAddNextObjectives.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07001812 if (val == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001813 val = new CopyOnWriteArrayList<>();
Saurav Das961beb22017-03-29 19:09:17 -07001814 }
Yi Tsengef19de12017-04-24 11:33:05 -07001815 val.add(nextGrp);
Saurav Das961beb22017-03-29 19:09:17 -07001816 return val;
1817 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08001818 }
1819
Yi Tsengef19de12017-04-24 11:33:05 -07001820 protected void updatePendingGroups(GroupKey groupKey, GroupChainElem gce) {
1821 pendingGroups.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07001822 if (val == null) {
1823 val = Sets.newConcurrentHashSet();
1824 }
1825 val.add(gce);
1826 return val;
1827 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08001828 }
1829
Yi Tsengef19de12017-04-24 11:33:05 -07001830 protected void addPendingUpdateNextObjective(GroupKey groupKey,
1831 NextObjective nextObjective) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001832 pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> {
1833 if (nextObjs != null) {
1834 nextObjs.add(nextObjective);
1835 } else {
1836 nextObjs = Sets.newHashSet(nextObjective);
1837 }
1838 return nextObjs;
1839 });
1840 }
1841
Saurav Das2f2c9d02018-04-07 16:51:09 -07001842 protected void addPendingRemoveNextObjective(NextObjective nextObjective,
1843 List<GroupKey> groupKeys) {
1844 pendingRemoveNextObjectives.put(nextObjective, groupKeys);
Yi Tsengef19de12017-04-24 11:33:05 -07001845 }
1846
Saurav Das2f2c9d02018-04-07 16:51:09 -07001847 protected int getNextAvailableIndex() {
1848 return (int) nextIndex.incrementAndGet();
1849 }
1850
1851 protected void processPendingUpdateNextObjs(GroupKey groupKey) {
1852 Set<NextObjective> nextObjs = pendingUpdateNextObjectives.remove(groupKey);
1853 if (nextObjs != null) {
1854 nextObjs.forEach(nextObj -> {
1855 log.debug("Group {} updated, update pending next objective {}.",
1856 groupKey, nextObj);
1857 pass(nextObj);
1858 });
1859 }
1860 }
1861
1862 protected void processPendingRemoveNextObjs(GroupKey key) {
Yi Tsengef19de12017-04-24 11:33:05 -07001863 pendingRemoveNextObjectives.asMap().forEach((nextObjective, groupKeys) -> {
jaegonkim2e4d99e2018-07-09 23:23:10 +09001864 groupKeys.remove(key);
Yi Tsengef19de12017-04-24 11:33:05 -07001865 if (groupKeys.isEmpty()) {
1866 pendingRemoveNextObjectives.invalidate(nextObjective);
1867 pass(nextObjective);
Yi Tsengef19de12017-04-24 11:33:05 -07001868 }
1869 });
1870 }
1871
Yi Tsengef19de12017-04-24 11:33:05 -07001872 protected void processPendingAddGroupsOrNextObjs(GroupKey key, boolean added) {
1873 //first check for group chain
1874 Set<OfdpaGroupHandlerUtility.GroupChainElem> gceSet = pendingGroups.asMap().remove(key);
1875 if (gceSet != null) {
1876 for (GroupChainElem gce : gceSet) {
1877 log.debug("Group service {} group key {} in device {}. "
1878 + "Processing next group in group chain with group id 0x{}",
1879 (added) ? "ADDED" : "processed",
1880 key, deviceId,
1881 Integer.toHexString(gce.groupDescription().givenGroupId()));
1882 processGroupChain(gce);
1883 }
1884 } else {
1885 // otherwise chain complete - check for waiting nextObjectives
1886 List<OfdpaGroupHandlerUtility.OfdpaNextGroup> nextGrpList =
1887 pendingAddNextObjectives.getIfPresent(key);
1888 if (nextGrpList != null) {
1889 pendingAddNextObjectives.invalidate(key);
1890 nextGrpList.forEach(nextGrp -> {
1891 log.debug("Group service {} group key {} in device:{}. "
1892 + "Done implementing next objective: {} <<-->> gid:0x{}",
1893 (added) ? "ADDED" : "processed",
1894 key, deviceId, nextGrp.nextObjective().id(),
1895 Integer.toHexString(groupService.getGroup(deviceId, key)
1896 .givenGroupId()));
1897 pass(nextGrp.nextObjective());
Saurav Dasc88d4662017-05-15 15:34:25 -07001898 updateFlowObjectiveStore(nextGrp.nextObjective().id(), nextGrp);
Yi Tsengef19de12017-04-24 11:33:05 -07001899
1900 // check if addBuckets waiting for this completion
1901 pendingBuckets.compute(nextGrp.nextObjective().id(), (nextId, pendBkts) -> {
1902 if (pendBkts != null) {
1903 pendBkts.forEach(pendBkt -> addBucketToGroup(pendBkt, nextGrp));
1904 }
1905 return null;
1906 });
1907 });
1908 }
1909 }
1910 }
1911
Charles Chan188ebf52015-12-23 00:15:11 -08001912 /**
1913 * Processes next element of a group chain. Assumption is that if this
1914 * group points to another group, the latter has already been created
1915 * and this driver has received notification for it. A second assumption is
1916 * that if there is another group waiting for this group then the appropriate
1917 * stores already have the information to act upon the notification for the
1918 * creation of this group.
1919 * <p>
1920 * The processing of the GroupChainElement depends on the number of groups
1921 * this element is waiting on. For all group types other than SIMPLE, a
1922 * GroupChainElement could be waiting on multiple groups.
1923 *
1924 * @param gce the group chain element to be processed next
1925 */
1926 private void processGroupChain(GroupChainElem gce) {
1927 int waitOnGroups = gce.decrementAndGetGroupsWaitedOn();
1928 if (waitOnGroups != 0) {
1929 log.debug("GCE: {} not ready to be processed", gce);
1930 return;
1931 }
1932 log.debug("GCE: {} ready to be processed", gce);
Yi Tsengef19de12017-04-24 11:33:05 -07001933 if (gce.addBucketToGroup()) {
1934 groupService.addBucketsToGroup(gce.groupDescription().deviceId(),
1935 gce.groupDescription().appCookie(),
1936 gce.groupDescription().buckets(),
1937 gce.groupDescription().appCookie(),
1938 gce.groupDescription().appId());
Charles Chan188ebf52015-12-23 00:15:11 -08001939 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001940 groupService.addGroup(gce.groupDescription());
Charles Chan188ebf52015-12-23 00:15:11 -08001941 }
1942 }
1943
Saurav Dasc88d4662017-05-15 15:34:25 -07001944 private void updateFlowObjectiveStore(Integer nextId, OfdpaNextGroup nextGrp) {
1945 synchronized (flowObjectiveStore) {
1946 // get fresh copy of what the store holds
1947 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1948 if (next == null || nextGrp.nextObjective().op() == Operation.ADD) {
1949 flowObjectiveStore.putNextGroup(nextId, nextGrp);
1950 return;
1951 }
1952 if (nextGrp.nextObjective().op() == Operation.ADD_TO_EXISTING) {
1953 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Pier Luigid008dbe2018-01-15 09:48:49 +01001954 allActiveKeys = Lists.newArrayList(allActiveKeys);
Saurav Dasc88d4662017-05-15 15:34:25 -07001955 // If active keys shows only the top-level group without a chain of groups,
1956 // then it represents an empty group. Update by replacing empty chain.
1957 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1958 allActiveKeys.clear();
1959 }
1960 allActiveKeys.addAll(nextGrp.allKeys());
1961 flowObjectiveStore.putNextGroup(nextId,
1962 new OfdpaNextGroup(allActiveKeys, nextGrp.nextObjective()));
1963 }
1964 }
1965 }
1966
Saurav Das8be4e3a2016-03-11 17:19:07 -08001967 private class InnerGroupListener implements GroupListener {
1968 @Override
1969 public void event(GroupEvent event) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001970 switch (event.type()) {
1971 case GROUP_ADDED:
1972 processPendingAddGroupsOrNextObjs(event.subject().appCookie(), true);
1973 break;
1974 case GROUP_REMOVED:
1975 processPendingRemoveNextObjs(event.subject().appCookie());
1976 break;
Yi Tseng78f51f42017-02-02 13:54:58 -08001977 case GROUP_UPDATED:
1978 processPendingUpdateNextObjs(event.subject().appCookie());
1979 break;
Charles Chanfc5c7802016-05-17 13:13:55 -07001980 default:
1981 break;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001982 }
1983 }
1984 }
Charles Chan188ebf52015-12-23 00:15:11 -08001985}