blob: 0b203b0cc0724d847a034190b73044007dc04f1b [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.*;
pier5c817582019-04-17 17:05:08 +020086import static org.onosproject.driver.pipeline.ofdpa.OfdpaPipelineUtility.*;
Yi Tsengef19de12017-04-24 11:33:05 -070087import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
jayakumarthazhathad3636a2018-10-01 00:51:54 +053088import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.L2_MULTICAST_TYPE;
89import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.l2MulticastGroupKey;
Pier Ventre42287df2016-11-09 14:17:26 -080090import static org.onosproject.net.flow.criteria.Criterion.Type.TUNNEL_ID;
Pier Ventre140a8942016-11-02 07:26:38 -070091import static org.onosproject.net.flow.criteria.Criterion.Type.VLAN_VID;
Yi Tseng78f51f42017-02-02 13:54:58 -080092import static org.onosproject.net.group.GroupDescription.Type.ALL;
Andreas Pantelopoulos671d7462018-01-22 16:00:28 -080093import static org.onosproject.net.group.GroupDescription.Type.INDIRECT;
Yi Tseng78f51f42017-02-02 13:54:58 -080094import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Charles Chan188ebf52015-12-23 00:15:11 -080095import static org.slf4j.LoggerFactory.getLogger;
96
97/**
Saurav Das961beb22017-03-29 19:09:17 -070098 * Group handler that emulates Broadcom OF-DPA TTP.
Charles Chan188ebf52015-12-23 00:15:11 -080099 */
Charles Chan361154b2016-03-24 10:23:39 -0700100public class Ofdpa2GroupHandler {
Yi Tsengef19de12017-04-24 11:33:05 -0700101 protected final Logger log = getLogger(getClass());
Yi Tsengef19de12017-04-24 11:33:05 -0700102 // Services, Stores
Charles Chan188ebf52015-12-23 00:15:11 -0800103 protected GroupService groupService;
Saurav Das8be4e3a2016-03-11 17:19:07 -0800104 protected StorageService storageService;
Yi Tsengef19de12017-04-24 11:33:05 -0700105 protected FlowObjectiveStore flowObjectiveStore;
Charles Chan188ebf52015-12-23 00:15:11 -0800106 // index number for group creation
Saurav Das8be4e3a2016-03-11 17:19:07 -0800107 private AtomicCounter nextIndex;
Yi Tsengef19de12017-04-24 11:33:05 -0700108 protected DeviceId deviceId;
Saurav Das2c57bc82018-04-07 16:51:09 -0700109 Cache<GroupKey, List<OfdpaGroupHandlerUtility.OfdpaNextGroup>> pendingAddNextObjectives;
110 Cache<NextObjective, List<GroupKey>> pendingRemoveNextObjectives;
111 Cache<GroupKey, Set<OfdpaGroupHandlerUtility.GroupChainElem>> pendingGroups;
112 ConcurrentHashMap<GroupKey, Set<NextObjective>> pendingUpdateNextObjectives;
Yi Tseng47f82dc2017-03-05 22:48:39 -0800113 // local store for pending bucketAdds - by design there can be multiple
Charles Chan188ebf52015-12-23 00:15:11 -0800114 // pending bucket for a group
Yi Tseng47f82dc2017-03-05 22:48:39 -0800115 protected ConcurrentHashMap<Integer, Set<NextObjective>> pendingBuckets =
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700116 new ConcurrentHashMap<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700117 private ScheduledExecutorService groupCheckerExecutor =
118 Executors.newScheduledThreadPool(2, groupedThreads("onos/pipeliner", "ofdpa-%d", log));
Charles Chan40132b32017-01-22 00:19:37 -0800119 /**
120 * Determines whether this pipeline support copy ttl instructions or not.
121 *
122 * @return true if copy ttl instructions are supported
123 */
124 protected boolean supportCopyTtl() {
125 return true;
126 }
127
128 /**
129 * Determines whether this pipeline support set mpls bos instruction or not.
130 *
131 * @return true if set mpls bos instruction is supported
132 */
133 protected boolean supportSetMplsBos() {
134 return true;
135 }
136
Charles Chan0f43e472017-02-14 14:00:16 -0800137 /**
138 * Determines whether this pipeline requires popping VLAN before pushing MPLS.
139 * <p>
140 * If required, pop vlan before push mpls and add an arbitrary vlan back afterward.
141 * MPLS interface group will substitute the arbitrary VLAN with expected VLAN later on.
142 *
143 * @return true if this pipeline requires popping VLAN before pushing MPLS
144 */
145 protected boolean requireVlanPopBeforeMplsPush() {
146 return false;
147 }
148
Alex Yashchuka3679532017-12-08 17:40:05 +0200149 public void init(DeviceId deviceId, PipelinerContext context) {
Yi Tsengef19de12017-04-24 11:33:05 -0700150 ServiceDirectory serviceDirectory = context.directory();
Charles Chan188ebf52015-12-23 00:15:11 -0800151 this.deviceId = deviceId;
152 this.flowObjectiveStore = context.store();
Charles Chan188ebf52015-12-23 00:15:11 -0800153 this.groupService = serviceDirectory.get(GroupService.class);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800154 this.storageService = serviceDirectory.get(StorageService.class);
Madan Jampanid5714e02016-04-19 14:15:20 -0700155 this.nextIndex = storageService.getAtomicCounter("group-id-index-counter");
Charles Chan188ebf52015-12-23 00:15:11 -0800156
Charles Chanfc5c7802016-05-17 13:13:55 -0700157 pendingAddNextObjectives = CacheBuilder.newBuilder()
Charles Chan188ebf52015-12-23 00:15:11 -0800158 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700159 .removalListener((RemovalNotification<GroupKey, List<OfdpaNextGroup>> notification) -> {
160 if (notification.getCause() == RemovalCause.EXPIRED &&
161 Objects.nonNull(notification.getValue())) {
162 notification.getValue()
163 .forEach(ofdpaNextGrp ->
164 fail(ofdpaNextGrp.nextObjective(),
165 ObjectiveError.GROUPINSTALLATIONFAILED));
Charles Chanfc5c7802016-05-17 13:13:55 -0700166 }
167 }).build();
Charles Chan188ebf52015-12-23 00:15:11 -0800168
Charles Chanfc5c7802016-05-17 13:13:55 -0700169 pendingRemoveNextObjectives = CacheBuilder.newBuilder()
170 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700171 .removalListener((RemovalNotification<NextObjective, List<GroupKey>> notification) -> {
Charles Chanfc5c7802016-05-17 13:13:55 -0700172 if (notification.getCause() == RemovalCause.EXPIRED) {
Yi Tsengef19de12017-04-24 11:33:05 -0700173 fail(notification.getKey(),
174 ObjectiveError.GROUPREMOVALFAILED);
Charles Chan188ebf52015-12-23 00:15:11 -0800175 }
176 }).build();
Saurav Das961beb22017-03-29 19:09:17 -0700177 pendingGroups = CacheBuilder.newBuilder()
178 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700179 .removalListener((RemovalNotification<GroupKey, Set<GroupChainElem>> notification) -> {
Saurav Das961beb22017-03-29 19:09:17 -0700180 if (notification.getCause() == RemovalCause.EXPIRED) {
181 log.error("Unable to install group with key {} and pending GCEs: {}",
182 notification.getKey(), notification.getValue());
183 }
184 }).build();
Yi Tseng78f51f42017-02-02 13:54:58 -0800185 pendingUpdateNextObjectives = new ConcurrentHashMap<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700186 GroupChecker groupChecker = new GroupChecker(this);
187 groupCheckerExecutor.scheduleAtFixedRate(groupChecker, 0, 500, TimeUnit.MILLISECONDS);
Charles Chan188ebf52015-12-23 00:15:11 -0800188 groupService.addListener(new InnerGroupListener());
189 }
190
Saurav Das8be4e3a2016-03-11 17:19:07 -0800191 //////////////////////////////////////
192 // Group Creation
193 //////////////////////////////////////
194
Yi Tsengef19de12017-04-24 11:33:05 -0700195 /**
196 * Adds a list of group chain by given NextObjective.
197 *
198 * @param nextObjective the NextObjective
199 */
Charles Chan188ebf52015-12-23 00:15:11 -0800200 protected void addGroup(NextObjective nextObjective) {
201 switch (nextObjective.type()) {
202 case SIMPLE:
203 Collection<TrafficTreatment> treatments = nextObjective.next();
204 if (treatments.size() != 1) {
205 log.error("Next Objectives of type Simple should only have a "
206 + "single Traffic Treatment. Next Objective Id:{}",
207 nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -0700208 fail(nextObjective, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800209 return;
210 }
211 processSimpleNextObjective(nextObjective);
212 break;
213 case BROADCAST:
214 processBroadcastNextObjective(nextObjective);
215 break;
216 case HASHED:
Pier Ventre140a8942016-11-02 07:26:38 -0700217 if (!verifyHashedNextObjective(nextObjective)) {
218 log.error("Next Objectives of type hashed not supported. Next Objective Id:{}",
219 nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -0700220 fail(nextObjective, ObjectiveError.BADPARAMS);
Pier Ventre140a8942016-11-02 07:26:38 -0700221 return;
222 }
Charles Chan188ebf52015-12-23 00:15:11 -0800223 processHashedNextObjective(nextObjective);
224 break;
225 case FAILOVER:
Yi Tsengef19de12017-04-24 11:33:05 -0700226 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -0800227 log.warn("Unsupported next objective type {}", nextObjective.type());
228 break;
229 default:
Yi Tsengef19de12017-04-24 11:33:05 -0700230 fail(nextObjective, ObjectiveError.UNKNOWN);
Charles Chan188ebf52015-12-23 00:15:11 -0800231 log.warn("Unknown next objective type {}", nextObjective.type());
232 }
233 }
234
235 /**
jayakumarthazhathad3636a2018-10-01 00:51:54 +0530236 * Similar to processBroadcastNextObjective but handles L2 Multicast Next Objectives.
237 *
238 * @param nextObj NextObjective of L2_MULTICAST with chained NextObjectives for single homed access ports
239 */
240 private void processL2MulticastNextObjective(NextObjective nextObj) {
241
242 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
243 if (assignedVlan == null) {
244 log.warn("VLAN ID required by L2 multicast next objective is missing. Aborting group creation.");
245 fail(nextObj, ObjectiveError.BADPARAMS);
246 return;
247 }
248
249 // Group info should contain only single homed hosts for a given vlanId
250 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
251 createL2MulticastGroup(nextObj, assignedVlan, groupInfos);
252 }
253
254 private void createL2MulticastGroup(NextObjective nextObj, VlanId vlanId, List<GroupInfo> groupInfos) {
255 // Realize & represent L2 multicast group in OFDPA driver layer
256 // TODO : Need to identify significance of OfdpaNextGroup.
257 Integer l2MulticastGroupId = L2_MULTICAST_TYPE | (vlanId.toShort() << 16);
258 final GroupKey l2MulticastGroupKey = l2MulticastGroupKey(vlanId, deviceId);
259 List<Deque<GroupKey>> l2MulticastAllGroup = Lists.newArrayList();
260 groupInfos.forEach(groupInfo -> {
261 Deque<GroupKey> groupKeyChain = new ArrayDeque<>();
262 groupKeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
263 groupKeyChain.addFirst(l2MulticastGroupKey);
264 l2MulticastAllGroup.add(groupKeyChain);
265 });
266 OfdpaNextGroup ofdpaL2MulticastGroup = new OfdpaNextGroup(l2MulticastAllGroup, nextObj);
267 updatePendingNextObjective(l2MulticastGroupKey, ofdpaL2MulticastGroup);
268 // Group Chain Hierarchy creation using group service and thus in device level
269 List<GroupBucket> l2McastBuckets = new ArrayList<>();
270 groupInfos.forEach(groupInfo -> {
271 // Points to L2 interface group directly.
272 TrafficTreatment.Builder trafficTreatment = DefaultTrafficTreatment.builder();
273 trafficTreatment.group(new GroupId(groupInfo.innerMostGroupDesc().givenGroupId()));
274 GroupBucket bucket = DefaultGroupBucket.createAllGroupBucket(trafficTreatment.build());
275 l2McastBuckets.add(bucket);
276 });
277 GroupDescription l2MulticastGroupDescription =
278 new DefaultGroupDescription(
279 deviceId,
280 ALL,
281 new GroupBuckets(l2McastBuckets),
282 l2MulticastGroupKey,
283 l2MulticastGroupId,
284 nextObj.appId());
285 GroupChainElem l2MulticastGce = new GroupChainElem(l2MulticastGroupDescription,
286 groupInfos.size(), false, deviceId);
287 groupInfos.forEach(groupInfo -> {
288 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), l2MulticastGce);
289 groupService.addGroup(groupInfo.innerMostGroupDesc());
290 });
291 }
292
293 /**
Charles Chan188ebf52015-12-23 00:15:11 -0800294 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
Saurav Dasa89b95a2018-02-14 14:14:54 -0800295 * a chain of groups. The simple Next Objective passed in by the application
296 * is broken up into a group chain. The following chains can be created
297 * depending on the parameters in the Next Objective.
298 * 1. L2 Interface group (no chaining)
299 * 2. L3 Unicast group -> L2 Interface group
300 * 3. MPLS Interface group -> L2 Interface group
301 * 4. MPLS Swap group -> MPLS Interface group -> L2 Interface group
302 * 5. PW initiation group chain
Charles Chan188ebf52015-12-23 00:15:11 -0800303 *
304 * @param nextObj the nextObjective of type SIMPLE
305 */
306 private void processSimpleNextObjective(NextObjective nextObj) {
307 TrafficTreatment treatment = nextObj.next().iterator().next();
Andreas Pantelopoulos671d7462018-01-22 16:00:28 -0800308 // determine if plain L2 or L3->L2 or MPLS Swap -> MPLS Interface -> L2
Charles Chan188ebf52015-12-23 00:15:11 -0800309 boolean plainL2 = true;
Andreas Pantelopoulos671d7462018-01-22 16:00:28 -0800310 boolean mplsSwap = false;
311 MplsLabel mplsLabel = null;
Charles Chan188ebf52015-12-23 00:15:11 -0800312 for (Instruction ins : treatment.allInstructions()) {
313 if (ins.type() == Instruction.Type.L2MODIFICATION) {
314 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
315 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_DST ||
316 l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_SRC) {
317 plainL2 = false;
Charles Chan188ebf52015-12-23 00:15:11 -0800318 }
Andreas Pantelopoulos671d7462018-01-22 16:00:28 -0800319 // mpls label in simple next objectives is used only to indicate
320 // a MPLS Swap group before the MPLS Interface Group
321 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) {
322 mplsSwap = true;
323 mplsLabel = ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label();
324 }
Charles Chan188ebf52015-12-23 00:15:11 -0800325 }
326 }
Charles Chan188ebf52015-12-23 00:15:11 -0800327 if (plainL2) {
328 createL2InterfaceGroup(nextObj);
329 return;
330 }
Saurav Dasa89b95a2018-02-14 14:14:54 -0800331 // In order to understand if it is a pseudowire related
Pier Ventre42287df2016-11-09 14:17:26 -0800332 // next objective we look for the tunnel id in the meta.
333 boolean isPw = false;
Pier Ventre140a8942016-11-02 07:26:38 -0700334 if (nextObj.meta() != null) {
Pier Ventre42287df2016-11-09 14:17:26 -0800335 TunnelIdCriterion tunnelIdCriterion = (TunnelIdCriterion) nextObj
336 .meta()
337 .getCriterion(TUNNEL_ID);
338 if (tunnelIdCriterion != null) {
339 isPw = true;
340 }
Pier Ventre140a8942016-11-02 07:26:38 -0700341 }
Andreas Pantelopoulosb524fff2018-02-08 12:42:06 -0800342 if (mplsSwap && !isPw) {
Saurav Dasa89b95a2018-02-14 14:14:54 -0800343 log.debug("Creating a MPLS Swap -> MPLS Interface -> L2 Interface group chain.");
Andreas Pantelopoulos671d7462018-01-22 16:00:28 -0800344 // break up simple next objective to GroupChain objects
345 GroupInfo groupInfo = createL2L3Chain(treatment, nextObj.id(),
346 nextObj.appId(), true,
347 nextObj.meta());
348 if (groupInfo == null) {
349 log.error("Could not process nextObj={} in dev:{}", nextObj.id(), deviceId);
350 fail(nextObj, ObjectiveError.BADPARAMS);
351 return;
352 }
Andreas Pantelopoulos671d7462018-01-22 16:00:28 -0800353 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Saurav Dasa89b95a2018-02-14 14:14:54 -0800354 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie()); // l2 interface
355 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie()); // mpls interface
Andreas Pantelopoulos671d7462018-01-22 16:00:28 -0800356 // creating the mpls swap group and adding it to the chain
Andreas Pantelopoulos671d7462018-01-22 16:00:28 -0800357 int nextGid = groupInfo.nextGroupDesc().givenGroupId();
358 int index = getNextAvailableIndex();
Saurav Dasa89b95a2018-02-14 14:14:54 -0800359 GroupDescription swapGroupDescription = createMplsSwap(
Andreas Pantelopoulos671d7462018-01-22 16:00:28 -0800360 nextGid,
361 OfdpaMplsGroupSubType.MPLS_SWAP_LABEL,
362 index,
363 mplsLabel,
364 nextObj.appId()
365 );
Saurav Dasa89b95a2018-02-14 14:14:54 -0800366 // ensure swap group is added after L2L3 chain
367 GroupKey swapGroupKey = swapGroupDescription.appCookie();
368 GroupChainElem swapChainElem = new GroupChainElem(swapGroupDescription,
369 1, false, deviceId);
370 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), swapChainElem);
371 gkeyChain.addFirst(swapGroupKey);
Saurav Dasa89b95a2018-02-14 14:14:54 -0800372 // ensure nextObjective waits on the outermost groupKey
373 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
374 allGroupKeys.add(gkeyChain);
375 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
376 updatePendingNextObjective(swapGroupKey, ofdpaGrp);
Andreas Pantelopoulos671d7462018-01-22 16:00:28 -0800377 // now we are ready to send the l2 groupDescription (inner), as all the stores
378 // that will get async replies have been updated. By waiting to update
379 // the stores, we prevent nasty race conditions.
380 groupService.addGroup(groupInfo.innerMostGroupDesc());
381 } else if (!isPw) {
Saurav Dasa89b95a2018-02-14 14:14:54 -0800382 boolean isMpls = false;
383 if (nextObj.meta() != null) {
384 isMpls = isNotMplsBos(nextObj.meta());
385 }
386 log.debug("Creating a {} -> L2 Interface group chain.",
387 (isMpls) ? "MPLS Interface" : "L3 Unicast");
Pier Ventre42287df2016-11-09 14:17:26 -0800388 // break up simple next objective to GroupChain objects
389 GroupInfo groupInfo = createL2L3Chain(treatment, nextObj.id(),
390 nextObj.appId(), isMpls,
391 nextObj.meta());
392 if (groupInfo == null) {
393 log.error("Could not process nextObj={} in dev:{}", nextObj.id(), deviceId);
Andreas Pantelopoulos671d7462018-01-22 16:00:28 -0800394 fail(nextObj, ObjectiveError.BADPARAMS);
Pier Ventre42287df2016-11-09 14:17:26 -0800395 return;
396 }
397 // create object for local and distributed storage
398 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700399 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
400 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Saurav Dasa89b95a2018-02-14 14:14:54 -0800401 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
402 allGroupKeys.add(gkeyChain);
403 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Pier Ventre42287df2016-11-09 14:17:26 -0800404 // store l3groupkey with the ofdpaNextGroup for the nextObjective that depends on it
Yi Tsengef19de12017-04-24 11:33:05 -0700405 updatePendingNextObjective(groupInfo.nextGroupDesc().appCookie(), ofdpaGrp);
Pier Ventre42287df2016-11-09 14:17:26 -0800406 // now we are ready to send the l2 groupDescription (inner), as all the stores
407 // that will get async replies have been updated. By waiting to update
408 // the stores, we prevent nasty race conditions.
Yi Tsengef19de12017-04-24 11:33:05 -0700409 groupService.addGroup(groupInfo.innerMostGroupDesc());
Pier Ventre42287df2016-11-09 14:17:26 -0800410 } else {
411 // We handle the pseudo wire with a different a procedure.
412 // This procedure is meant to handle both initiation and
413 // termination of the pseudo wire.
414 processPwNextObjective(nextObj);
Charles Chan188ebf52015-12-23 00:15:11 -0800415 }
Charles Chan188ebf52015-12-23 00:15:11 -0800416 }
417
Charles Chan188ebf52015-12-23 00:15:11 -0800418 /**
419 * Creates a simple L2 Interface Group.
Charles Chan188ebf52015-12-23 00:15:11 -0800420 * @param nextObj the next Objective
421 */
422 private void createL2InterfaceGroup(NextObjective nextObj) {
Yi Tsengef19de12017-04-24 11:33:05 -0700423 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700424 if (assignedVlan == null) {
425 log.warn("VLAN ID required by simple next obj is missing. Abort.");
Yi Tsengef19de12017-04-24 11:33:05 -0700426 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800427 return;
428 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700429 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700430 // There is only one L2 interface group in this case
Yi Tsengef19de12017-04-24 11:33:05 -0700431 GroupDescription l2InterfaceGroupDesc = groupInfos.get(0).innerMostGroupDesc();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700432 // Put all dependency information into allGroupKeys
433 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
434 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
435 gkeyChain.addFirst(l2InterfaceGroupDesc.appCookie());
436 allGroupKeys.add(gkeyChain);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700437 // Point the next objective to this group
438 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
439 updatePendingNextObjective(l2InterfaceGroupDesc.appCookie(), ofdpaGrp);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700440 // Start installing the inner-most group
jayakumarthazhathad3636a2018-10-01 00:51:54 +0530441 groupService.addGroup(l2InterfaceGroupDesc); }
Charles Chan188ebf52015-12-23 00:15:11 -0800442
443 /**
Andreas Pantelopoulos671d7462018-01-22 16:00:28 -0800444 * Creates an Mpls group of type swap.
445 *
446 * @param nextGroupId the next group in the chain
447 * @param subtype the mpls swap label group subtype
448 * @param index the index of the group
449 * @param mplsLabel the mpls label to swap
450 * @param applicationId the application id
451 * @return the group description
452 */
453 protected GroupDescription createMplsSwap(int nextGroupId,
454 OfdpaMplsGroupSubType subtype,
455 int index,
456 MplsLabel mplsLabel,
457 ApplicationId applicationId) {
Andreas Pantelopoulos671d7462018-01-22 16:00:28 -0800458 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Andreas Pantelopoulos671d7462018-01-22 16:00:28 -0800459 treatment.setMpls(mplsLabel);
Andreas Pantelopoulos671d7462018-01-22 16:00:28 -0800460 // We point the group to the next group.
461 treatment.group(new GroupId(nextGroupId));
462 GroupBucket groupBucket = DefaultGroupBucket
463 .createIndirectGroupBucket(treatment.build());
464 // Finally we build the group description.
465 int groupId = makeMplsLabelGroupId(subtype, index);
466 GroupKey groupKey = new DefaultGroupKey(
Saurav Dasa89b95a2018-02-14 14:14:54 -0800467 Ofdpa2Pipeline.appKryo.serialize(index));
Andreas Pantelopoulos671d7462018-01-22 16:00:28 -0800468 return new DefaultGroupDescription(
469 deviceId,
470 INDIRECT,
471 new GroupBuckets(Collections.singletonList(groupBucket)),
472 groupKey,
473 groupId,
Saurav Dasa89b95a2018-02-14 14:14:54 -0800474 applicationId);
Andreas Pantelopoulos671d7462018-01-22 16:00:28 -0800475 }
476
477 /**
Charles Chan188ebf52015-12-23 00:15:11 -0800478 * Creates one of two possible group-chains from the treatment
479 * passed in. Depending on the MPLS boolean, this method either creates
Charles Chan425854b2016-04-11 15:32:12 -0700480 * an L3Unicast Group --&gt; L2Interface Group, if mpls is false;
481 * or MPLSInterface Group --&gt; L2Interface Group, if mpls is true;
Charles Chan188ebf52015-12-23 00:15:11 -0800482 * The returned 'inner' group description is always the L2 Interface group.
483 *
484 * @param treatment that needs to be broken up to create the group chain
485 * @param nextId of the next objective that needs this group chain
486 * @param appId of the application that sent this next objective
487 * @param mpls determines if L3Unicast or MPLSInterface group is created
488 * @param meta metadata passed in by the application as part of the nextObjective
489 * @return GroupInfo containing the GroupDescription of the
490 * L2Interface group(inner) and the GroupDescription of the (outer)
491 * L3Unicast/MPLSInterface group. May return null if there is an
492 * error in processing the chain
493 */
Charles Chan425854b2016-04-11 15:32:12 -0700494 protected GroupInfo createL2L3Chain(TrafficTreatment treatment, int nextId,
Charles Chanf9e98652016-09-07 16:54:23 -0700495 ApplicationId appId, boolean mpls,
496 TrafficSelector meta) {
497 return createL2L3ChainInternal(treatment, nextId, appId, mpls, meta, true);
498 }
499
500 /**
501 * Internal implementation of createL2L3Chain.
502 * <p>
503 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
504 * Since it is non-OF spec, we need an extension treatment for that.
505 * The useSetVlanExtension must be set to false for OFDPA i12.
506 * </p>
507 *
508 * @param treatment that needs to be broken up to create the group chain
509 * @param nextId of the next objective that needs this group chain
510 * @param appId of the application that sent this next objective
511 * @param mpls determines if L3Unicast or MPLSInterface group is created
512 * @param meta metadata passed in by the application as part of the nextObjective
513 * @param useSetVlanExtension use the setVlanVid extension that has is_present bit set to 0.
514 * @return GroupInfo containing the GroupDescription of the
515 * L2Interface group(inner) and the GroupDescription of the (outer)
516 * L3Unicast/MPLSInterface group. May return null if there is an
517 * error in processing the chain
518 */
519 protected GroupInfo createL2L3ChainInternal(TrafficTreatment treatment, int nextId,
Pier Ventre42287df2016-11-09 14:17:26 -0800520 ApplicationId appId, boolean mpls,
521 TrafficSelector meta, boolean useSetVlanExtension) {
Charles Chan188ebf52015-12-23 00:15:11 -0800522 // for the l2interface group, get vlan and port info
523 // for the outer group, get the src/dst mac, and vlan info
524 TrafficTreatment.Builder outerTtb = DefaultTrafficTreatment.builder();
525 TrafficTreatment.Builder innerTtb = DefaultTrafficTreatment.builder();
526 VlanId vlanid = null;
527 long portNum = 0;
528 boolean setVlan = false, popVlan = false;
Yi Tseng78f51f42017-02-02 13:54:58 -0800529 MacAddress srcMac;
530 MacAddress dstMac;
Charles Chan188ebf52015-12-23 00:15:11 -0800531 for (Instruction ins : treatment.allInstructions()) {
532 if (ins.type() == Instruction.Type.L2MODIFICATION) {
533 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
534 switch (l2ins.subtype()) {
535 case ETH_DST:
Charles Chan5270ed02016-01-30 23:22:37 -0800536 dstMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
537 outerTtb.setEthDst(dstMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800538 break;
539 case ETH_SRC:
Charles Chand0fd5dc2016-02-16 23:14:49 -0800540 srcMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
541 outerTtb.setEthSrc(srcMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800542 break;
543 case VLAN_ID:
544 vlanid = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
Charles Chanf9e98652016-09-07 16:54:23 -0700545 if (useSetVlanExtension) {
546 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
547 outerTtb.extension(ofdpaSetVlanVid, deviceId);
548 } else {
549 outerTtb.setVlanId(vlanid);
550 }
Charles Chan188ebf52015-12-23 00:15:11 -0800551 setVlan = true;
552 break;
553 case VLAN_POP:
554 innerTtb.popVlan();
555 popVlan = true;
556 break;
557 case DEC_MPLS_TTL:
558 case MPLS_LABEL:
559 case MPLS_POP:
560 case MPLS_PUSH:
561 case VLAN_PCP:
562 case VLAN_PUSH:
563 default:
564 break;
565 }
566 } else if (ins.type() == Instruction.Type.OUTPUT) {
567 portNum = ((Instructions.OutputInstruction) ins).port().toLong();
568 innerTtb.add(ins);
569 } else {
Saurav Das7bcbe702017-06-13 15:35:54 -0700570 log.debug("Driver does not handle this type of TrafficTreatment"
Saurav Dasa89b95a2018-02-14 14:14:54 -0800571 + " instruction in l2l3chain: {} - {}", ins.type(),
572 ins);
Charles Chan188ebf52015-12-23 00:15:11 -0800573 }
574 }
Charles Chan188ebf52015-12-23 00:15:11 -0800575 if (vlanid == null && meta != null) {
576 // use metadata if available
Pier Ventre140a8942016-11-02 07:26:38 -0700577 Criterion vidCriterion = meta.getCriterion(VLAN_VID);
Charles Chan188ebf52015-12-23 00:15:11 -0800578 if (vidCriterion != null) {
579 vlanid = ((VlanIdCriterion) vidCriterion).vlanId();
580 }
581 // if vlan is not set, use the vlan in metadata for outerTtb
582 if (vlanid != null && !setVlan) {
Charles Chanf9e98652016-09-07 16:54:23 -0700583 if (useSetVlanExtension) {
584 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
585 outerTtb.extension(ofdpaSetVlanVid, deviceId);
586 } else {
587 outerTtb.setVlanId(vlanid);
588 }
Charles Chan188ebf52015-12-23 00:15:11 -0800589 }
590 }
Charles Chan188ebf52015-12-23 00:15:11 -0800591 if (vlanid == null) {
592 log.error("Driver cannot process an L2/L3 group chain without "
593 + "egress vlan information for dev: {} port:{}",
594 deviceId, portNum);
595 return null;
596 }
Charles Chan188ebf52015-12-23 00:15:11 -0800597 if (!setVlan && !popVlan) {
598 // untagged outgoing port
599 TrafficTreatment.Builder temp = DefaultTrafficTreatment.builder();
600 temp.popVlan();
Yi Tsengef19de12017-04-24 11:33:05 -0700601 innerTtb.build().allInstructions().forEach(temp::add);
Charles Chan188ebf52015-12-23 00:15:11 -0800602 innerTtb = temp;
603 }
Charles Chan188ebf52015-12-23 00:15:11 -0800604 // assemble information for ofdpa l2interface group
Yi Tsengef19de12017-04-24 11:33:05 -0700605 int l2groupId = l2GroupId(vlanid, portNum);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800606 // a globally unique groupkey that is different for ports in the same device,
Charles Chan188ebf52015-12-23 00:15:11 -0800607 // but different for the same portnumber on different devices. Also different
608 // for the various group-types created out of the same next objective.
Charles Chane849c192016-01-11 18:28:54 -0800609 int l2gk = l2InterfaceGroupKey(deviceId, vlanid, portNum);
Yi Tsengef19de12017-04-24 11:33:05 -0700610 final GroupKey l2groupkey = new DefaultGroupKey(appKryo.serialize(l2gk));
Charles Chan188ebf52015-12-23 00:15:11 -0800611 // assemble information for outer group
Yi Tsengef19de12017-04-24 11:33:05 -0700612 GroupDescription outerGrpDesc;
Charles Chan188ebf52015-12-23 00:15:11 -0800613 if (mpls) {
Yi Tsengef19de12017-04-24 11:33:05 -0700614 // outer group is MPLS Interface
Saurav Das8be4e3a2016-03-11 17:19:07 -0800615 int mplsInterfaceIndex = getNextAvailableIndex();
Yi Tsengef19de12017-04-24 11:33:05 -0700616 int mplsGroupId = MPLS_INTERFACE_TYPE | (SUBTYPE_MASK & mplsInterfaceIndex);
617 final GroupKey mplsGroupKey = new DefaultGroupKey(
618 appKryo.serialize(mplsInterfaceIndex));
Yi Tsengfa394de2017-02-01 11:26:40 -0800619 outerTtb.group(new GroupId(l2groupId));
Charles Chan188ebf52015-12-23 00:15:11 -0800620 // create the mpls-interface group description to wait for the
621 // l2 interface group to be processed
622 GroupBucket mplsinterfaceGroupBucket =
623 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
624 outerGrpDesc = new DefaultGroupDescription(
625 deviceId,
626 GroupDescription.Type.INDIRECT,
627 new GroupBuckets(Collections.singletonList(
628 mplsinterfaceGroupBucket)),
Yi Tsengef19de12017-04-24 11:33:05 -0700629 mplsGroupKey,
630 mplsGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800631 appId);
632 log.debug("Trying MPLS-Interface: device:{} gid:{} gkey:{} nextid:{}",
Yi Tsengef19de12017-04-24 11:33:05 -0700633 deviceId, Integer.toHexString(mplsGroupId),
634 mplsGroupKey, nextId);
Charles Chan188ebf52015-12-23 00:15:11 -0800635 } else {
636 // outer group is L3Unicast
Saurav Das8be4e3a2016-03-11 17:19:07 -0800637 int l3unicastIndex = getNextAvailableIndex();
638 int l3groupId = L3_UNICAST_TYPE | (TYPE_MASK & l3unicastIndex);
639 final GroupKey l3groupkey = new DefaultGroupKey(
Yi Tsengef19de12017-04-24 11:33:05 -0700640 appKryo.serialize(l3unicastIndex));
Yi Tsengfa394de2017-02-01 11:26:40 -0800641 outerTtb.group(new GroupId(l2groupId));
Charles Chan188ebf52015-12-23 00:15:11 -0800642 // create the l3unicast group description to wait for the
643 // l2 interface group to be processed
644 GroupBucket l3unicastGroupBucket =
645 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
646 outerGrpDesc = new DefaultGroupDescription(
647 deviceId,
648 GroupDescription.Type.INDIRECT,
Yi Tsengef19de12017-04-24 11:33:05 -0700649 new GroupBuckets(Collections.singletonList(l3unicastGroupBucket)),
Charles Chan188ebf52015-12-23 00:15:11 -0800650 l3groupkey,
651 l3groupId,
652 appId);
653 log.debug("Trying L3Unicast: device:{} gid:{} gkey:{} nextid:{}",
654 deviceId, Integer.toHexString(l3groupId),
655 l3groupkey, nextId);
656 }
Charles Chan188ebf52015-12-23 00:15:11 -0800657 // store l2groupkey with the groupChainElem for the outer-group that depends on it
Yi Tsengef19de12017-04-24 11:33:05 -0700658 GroupChainElem gce = new GroupChainElem(outerGrpDesc, 1, false, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800659 updatePendingGroups(l2groupkey, gce);
Yi Tsengef19de12017-04-24 11:33:05 -0700660 // create group description for the inner l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700661 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800662 DefaultGroupBucket.createIndirectGroupBucket(innerTtb.build());
663 GroupDescription l2groupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -0700664 new DefaultGroupDescription(deviceId,
665 GroupDescription.Type.INDIRECT,
666 new GroupBuckets(Collections.singletonList(l2InterfaceGroupBucket)),
667 l2groupkey,
668 l2groupId,
669 appId);
Charles Chan188ebf52015-12-23 00:15:11 -0800670 log.debug("Trying L2Interface: device:{} gid:{} gkey:{} nextId:{}",
671 deviceId, Integer.toHexString(l2groupId),
672 l2groupkey, nextId);
673 return new GroupInfo(l2groupDescription, outerGrpDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800674 }
675
676 /**
677 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
678 * a chain of groups. The broadcast Next Objective passed in by the application
679 * has to be broken up into a group chain comprising of an
Saurav Das1a129a02016-11-18 15:21:57 -0800680 * L2 Flood group or L3 Multicast group, whose buckets point to L2 Interface groups.
Charles Chan188ebf52015-12-23 00:15:11 -0800681 *
682 * @param nextObj the nextObjective of type BROADCAST
683 */
684 private void processBroadcastNextObjective(NextObjective nextObj) {
Yi Tsengef19de12017-04-24 11:33:05 -0700685 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700686 if (assignedVlan == null) {
687 log.warn("VLAN ID required by broadcast next obj is missing. Abort.");
Yi Tsengef19de12017-04-24 11:33:05 -0700688 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800689 return;
690 }
Charles Chan188ebf52015-12-23 00:15:11 -0800691
jayakumarthazhathad3636a2018-10-01 00:51:54 +0530692 // Handling L2 multicast cases.
693 MacAddress dstMac = readEthDstFromSelector(nextObj.meta());
694 if (dstMac != null && dstMac.isMulticast()) {
695 processL2MulticastNextObjective(nextObj);
Jayakumar Thazhathc4857832018-10-24 01:22:04 -0400696 return;
jayakumarthazhathad3636a2018-10-01 00:51:54 +0530697 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700698
jayakumarthazhathad3636a2018-10-01 00:51:54 +0530699 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Yi Tsengef19de12017-04-24 11:33:05 -0700700 IpPrefix ipDst = readIpDstFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700701 if (ipDst != null) {
702 if (ipDst.isMulticast()) {
703 createL3MulticastGroup(nextObj, assignedVlan, groupInfos);
704 } else {
705 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -0700706 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700707 }
708 } else {
709 createL2FloodGroup(nextObj, assignedVlan, groupInfos);
710 }
711 }
712
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700713 private List<GroupInfo> prepareL2InterfaceGroup(NextObjective nextObj,
714 VlanId assignedVlan) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700715 ImmutableList.Builder<GroupInfo> groupInfoBuilder = ImmutableList.builder();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700716 // break up broadcast next objective to multiple groups
717 Collection<TrafficTreatment> buckets = nextObj.next();
Charles Chan188ebf52015-12-23 00:15:11 -0800718 // each treatment is converted to an L2 interface group
Charles Chan188ebf52015-12-23 00:15:11 -0800719 for (TrafficTreatment treatment : buckets) {
720 TrafficTreatment.Builder newTreatment = DefaultTrafficTreatment.builder();
721 PortNumber portNum = null;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700722 VlanId egressVlan = null;
Charles Chan188ebf52015-12-23 00:15:11 -0800723 // ensure that the only allowed treatments are pop-vlan and output
724 for (Instruction ins : treatment.allInstructions()) {
725 if (ins.type() == Instruction.Type.L2MODIFICATION) {
726 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
727 switch (l2ins.subtype()) {
728 case VLAN_POP:
729 newTreatment.add(l2ins);
730 break;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700731 case VLAN_ID:
732 egressVlan = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
733 break;
Charles Chan188ebf52015-12-23 00:15:11 -0800734 default:
735 log.debug("action {} not permitted for broadcast nextObj",
736 l2ins.subtype());
737 break;
738 }
739 } else if (ins.type() == Instruction.Type.OUTPUT) {
740 portNum = ((Instructions.OutputInstruction) ins).port();
741 newTreatment.add(ins);
742 } else {
Charles Chane849c192016-01-11 18:28:54 -0800743 log.debug("TrafficTreatment of type {} not permitted in " +
744 " broadcast nextObjective", ins.type());
Charles Chan188ebf52015-12-23 00:15:11 -0800745 }
746 }
Yi Tsengef19de12017-04-24 11:33:05 -0700747 if (portNum == null) {
Ruchi Sahotaf77dc0f2019-01-28 01:08:18 +0000748 log.debug("Can't find output port for the bucket {}.", treatment);
Yi Tsengef19de12017-04-24 11:33:05 -0700749 continue;
750 }
Charles Chan188ebf52015-12-23 00:15:11 -0800751 // assemble info for l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700752 VlanId l2InterfaceGroupVlan =
753 (egressVlan != null && !assignedVlan.equals(egressVlan)) ?
754 egressVlan : assignedVlan;
755 int l2gk = l2InterfaceGroupKey(deviceId, l2InterfaceGroupVlan, portNum.toLong());
756 final GroupKey l2InterfaceGroupKey =
Yi Tsengef19de12017-04-24 11:33:05 -0700757 new DefaultGroupKey(appKryo.serialize(l2gk));
Charles Chan372b63e2017-02-07 12:10:53 -0800758 int l2InterfaceGroupId = L2_INTERFACE_TYPE |
759 ((l2InterfaceGroupVlan.toShort() & THREE_BIT_MASK) << PORT_LEN) |
760 ((int) portNum.toLong() & FOUR_BIT_MASK);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700761 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800762 DefaultGroupBucket.createIndirectGroupBucket(newTreatment.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700763 GroupDescription l2InterfaceGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -0700764 new DefaultGroupDescription(deviceId,
765 GroupDescription.Type.INDIRECT,
766 new GroupBuckets(Collections.singletonList(
767 l2InterfaceGroupBucket)),
768 l2InterfaceGroupKey,
769 l2InterfaceGroupId,
770 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800771 log.debug("Trying L2-Interface: device:{} gid:{} gkey:{} nextid:{}",
Charles Chan5b9df8d2016-03-28 22:21:40 -0700772 deviceId, Integer.toHexString(l2InterfaceGroupId),
773 l2InterfaceGroupKey, nextObj.id());
Ruchi Sahotaf77dc0f2019-01-28 01:08:18 +0000774
Charles Chan5b9df8d2016-03-28 22:21:40 -0700775 groupInfoBuilder.add(new GroupInfo(l2InterfaceGroupDescription,
776 l2InterfaceGroupDescription));
Charles Chan188ebf52015-12-23 00:15:11 -0800777 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700778 return groupInfoBuilder.build();
779 }
Charles Chan188ebf52015-12-23 00:15:11 -0800780
Ruchi Sahotaf77dc0f2019-01-28 01:08:18 +0000781 private GroupInfo prepareL3UnicastGroup(NextObjective nextObj, NextGroup next) {
782
783 ImmutableList.Builder<GroupInfo> groupInfoBuilder = ImmutableList.builder();
784 TrafficTreatment treatment = nextObj.next().iterator().next();
785
786 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
787 if (assignedVlan == null) {
788 log.warn("VLAN ID required by next obj is missing. Abort.");
789 return null;
790 }
791
792 List<GroupInfo> l2GroupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
793 GroupDescription l2InterfaceGroupDesc = l2GroupInfos.get(0).innerMostGroupDesc();
794 GroupKey l2groupkey = l2InterfaceGroupDesc.appCookie();
795
796 TrafficTreatment.Builder outerTtb = DefaultTrafficTreatment.builder();
797 VlanId vlanid = null;
798 MacAddress srcMac;
799 MacAddress dstMac;
800 for (Instruction ins : treatment.allInstructions()) {
801 if (ins.type() == Instruction.Type.L2MODIFICATION) {
802 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
803 switch (l2ins.subtype()) {
804 case ETH_DST:
805 dstMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
806 outerTtb.setEthDst(dstMac);
807 break;
808 case ETH_SRC:
809 srcMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
810 outerTtb.setEthSrc(srcMac);
811 break;
812 case VLAN_ID:
813 vlanid = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
814 outerTtb.setVlanId(vlanid);
815 break;
816 default:
817 break;
818 }
819 } else {
820 log.debug("Driver does not handle this type of TrafficTreatment"
821 + " instruction in l2l3chain: {} - {}", ins.type(), ins);
822 }
823 }
824
825 GroupId l2groupId = new GroupId(l2InterfaceGroupDesc.givenGroupId());
826 outerTtb.group(l2groupId);
827
828 // we need the top level group's key to point the flow to it
829 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
830 GroupKey l3groupkey = gkeys.get(0).peekFirst();
831 GroupId grpId = groupService.getGroup(deviceId, l3groupkey).id();
832 int l3groupId = grpId.id();
833
834 // create the l3unicast group description to wait for the
835 // l2 interface group to be processed
836 GroupBucket l3UnicastGroupBucket = DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
837
838 GroupDescription l3UnicastGroupDescription = new DefaultGroupDescription(deviceId,
839 GroupDescription.Type.INDIRECT,
840 new GroupBuckets(Collections.singletonList(
841 l3UnicastGroupBucket)), l3groupkey,
842 l3groupId, nextObj.appId());
843
844 // store l2groupkey with the groupChainElem for the outer-group that depends on it
845 GroupChainElem gce = new GroupChainElem(l3UnicastGroupDescription, 1, false, deviceId);
846 updatePendingGroups(l2groupkey, gce);
847
848 log.debug("Trying L3-Interface: device:{} gid:{} gkey:{} nextid:{}",
849 deviceId, Integer.toHexString(l3groupId), l3groupkey, nextObj.id());
850
851 groupInfoBuilder.add(new GroupInfo(l2InterfaceGroupDesc,
852 l3UnicastGroupDescription));
853
854 return groupInfoBuilder.build().iterator().next();
855 }
856
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700857 private void createL2FloodGroup(NextObjective nextObj, VlanId vlanId,
858 List<GroupInfo> groupInfos) {
859 // assemble info for l2 flood group. Since there can be only one flood
860 // group for a vlan, its index is always the same - 0
Yi Tsengef19de12017-04-24 11:33:05 -0700861 Integer l2FloodGroupId = L2_FLOOD_TYPE | (vlanId.toShort() << 16);
862 final GroupKey l2FloodGroupKey = l2FloodGroupKey(vlanId, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800863 // collection of group buckets pointing to all the l2 interface groups
Yi Tsengef19de12017-04-24 11:33:05 -0700864 List<GroupBucket> l2floodBuckets = generateNextGroupBuckets(groupInfos, ALL);
Charles Chan188ebf52015-12-23 00:15:11 -0800865 // create the l2flood group-description to wait for all the
866 // l2interface groups to be processed
867 GroupDescription l2floodGroupDescription =
868 new DefaultGroupDescription(
869 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800870 ALL,
Charles Chan188ebf52015-12-23 00:15:11 -0800871 new GroupBuckets(l2floodBuckets),
Yi Tsengef19de12017-04-24 11:33:05 -0700872 l2FloodGroupKey,
873 l2FloodGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800874 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800875 log.debug("Trying L2-Flood: device:{} gid:{} gkey:{} nextid:{}",
Yi Tsengef19de12017-04-24 11:33:05 -0700876 deviceId, Integer.toHexString(l2FloodGroupId),
877 l2FloodGroupKey, nextObj.id());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700878 // Put all dependency information into allGroupKeys
879 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
880 groupInfos.forEach(groupInfo -> {
Yi Tsengef19de12017-04-24 11:33:05 -0700881 Deque<GroupKey> groupKeyChain = new ArrayDeque<>();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700882 // In this case we should have L2 interface group only
Yi Tsengef19de12017-04-24 11:33:05 -0700883 groupKeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
884 groupKeyChain.addFirst(l2FloodGroupKey);
885 allGroupKeys.add(groupKeyChain);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700886 });
Charles Chan5b9df8d2016-03-28 22:21:40 -0700887 // Point the next objective to this group
888 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -0700889 updatePendingNextObjective(l2FloodGroupKey, ofdpaGrp);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700890 GroupChainElem gce = new GroupChainElem(l2floodGroupDescription,
Yi Tsengef19de12017-04-24 11:33:05 -0700891 groupInfos.size(), false, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700892 groupInfos.forEach(groupInfo -> {
893 // Point this group to the next group
Yi Tsengef19de12017-04-24 11:33:05 -0700894 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), gce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700895 // Start installing the inner-most group
Yi Tsengef19de12017-04-24 11:33:05 -0700896 groupService.addGroup(groupInfo.innerMostGroupDesc());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700897 });
898 }
899
Pier Luigi7b7a29d2018-01-19 10:24:53 +0100900 private void createL3MulticastGroup(NextObjective nextObj, VlanId vlanId,
901 List<GroupInfo> groupInfos) {
902 // Let's create a new list mcast buckets
903 List<GroupBucket> l3McastBuckets = createL3MulticastBucket(groupInfos);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700904
905 int l3MulticastIndex = getNextAvailableIndex();
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700906 int l3MulticastGroupId = L3_MULTICAST_TYPE |
907 vlanId.toShort() << 16 | (TYPE_VLAN_MASK & l3MulticastIndex);
908 final GroupKey l3MulticastGroupKey =
Yi Tsengef19de12017-04-24 11:33:05 -0700909 new DefaultGroupKey(appKryo.serialize(l3MulticastIndex));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700910
911 GroupDescription l3MulticastGroupDesc = new DefaultGroupDescription(deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800912 ALL,
Charles Chan5b9df8d2016-03-28 22:21:40 -0700913 new GroupBuckets(l3McastBuckets),
914 l3MulticastGroupKey,
915 l3MulticastGroupId,
916 nextObj.appId());
917
918 // Put all dependency information into allGroupKeys
919 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
920 groupInfos.forEach(groupInfo -> {
921 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700922 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700923 // Add L3 interface group to the chain if there is one.
Yi Tsengef19de12017-04-24 11:33:05 -0700924 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
925 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700926 }
927 gkeyChain.addFirst(l3MulticastGroupKey);
928 allGroupKeys.add(gkeyChain);
929 });
Charles Chan5b9df8d2016-03-28 22:21:40 -0700930 // Point the next objective to this group
931 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
932 updatePendingNextObjective(l3MulticastGroupKey, ofdpaGrp);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700933 GroupChainElem outerGce = new GroupChainElem(l3MulticastGroupDesc,
Yi Tsengef19de12017-04-24 11:33:05 -0700934 groupInfos.size(), false, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700935 groupInfos.forEach(groupInfo -> {
936 // Point this group (L3 multicast) to the next group
Yi Tsengef19de12017-04-24 11:33:05 -0700937 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), outerGce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700938 // Point next group to inner-most group, if any
Yi Tsengef19de12017-04-24 11:33:05 -0700939 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
940 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(),
941 1, false, deviceId);
942 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700943 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700944 // Start installing the inner-most group
Yi Tsengef19de12017-04-24 11:33:05 -0700945 groupService.addGroup(groupInfo.innerMostGroupDesc());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700946 });
Charles Chan188ebf52015-12-23 00:15:11 -0800947 }
948
Charles Chan188ebf52015-12-23 00:15:11 -0800949 /**
950 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
951 * a chain of groups. The hashed Next Objective passed in by the application
952 * has to be broken up into a group chain comprising of an
953 * L3 ECMP group as the top level group. Buckets of this group can point
954 * to a variety of groups in a group chain, depending on the whether
955 * MPLS labels are being pushed or not.
956 * <p>
957 * NOTE: We do not create MPLS ECMP groups as they are unimplemented in
958 * OF-DPA 2.0 (even though it is in the spec). Therefore we do not
959 * check the nextObjective meta to see what is matching before being
960 * sent to this nextObjective.
961 *
962 * @param nextObj the nextObjective of type HASHED
963 */
Pier Ventre140a8942016-11-02 07:26:38 -0700964 protected void processHashedNextObjective(NextObjective nextObj) {
Charles Chan188ebf52015-12-23 00:15:11 -0800965 // storage for all group keys in the chain of groups created
966 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
967 List<GroupInfo> unsentGroups = new ArrayList<>();
968 createHashBucketChains(nextObj, allGroupKeys, unsentGroups);
Charles Chan188ebf52015-12-23 00:15:11 -0800969 // now we can create the outermost L3 ECMP group
970 List<GroupBucket> l3ecmpGroupBuckets = new ArrayList<>();
971 for (GroupInfo gi : unsentGroups) {
972 // create ECMP bucket to point to the outer group
973 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Yi Tsengef19de12017-04-24 11:33:05 -0700974 ttb.group(new GroupId(gi.nextGroupDesc().givenGroupId()));
Charles Chan188ebf52015-12-23 00:15:11 -0800975 GroupBucket sbucket = DefaultGroupBucket
976 .createSelectGroupBucket(ttb.build());
977 l3ecmpGroupBuckets.add(sbucket);
978 }
Saurav Das8be4e3a2016-03-11 17:19:07 -0800979 int l3ecmpIndex = getNextAvailableIndex();
980 int l3ecmpGroupId = L3_ECMP_TYPE | (TYPE_MASK & l3ecmpIndex);
981 GroupKey l3ecmpGroupKey = new DefaultGroupKey(
Yi Tsengef19de12017-04-24 11:33:05 -0700982 appKryo.serialize(l3ecmpIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800983 GroupDescription l3ecmpGroupDesc =
984 new DefaultGroupDescription(
985 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800986 SELECT,
Charles Chan188ebf52015-12-23 00:15:11 -0800987 new GroupBuckets(l3ecmpGroupBuckets),
988 l3ecmpGroupKey,
989 l3ecmpGroupId,
990 nextObj.appId());
991 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
992 l3ecmpGroupBuckets.size(),
Yi Tsengef19de12017-04-24 11:33:05 -0700993 false, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800994
995 // create objects for local and distributed storage
Yi Tsengef19de12017-04-24 11:33:05 -0700996 allGroupKeys.forEach(gKeyChain -> gKeyChain.addFirst(l3ecmpGroupKey));
Charles Chan188ebf52015-12-23 00:15:11 -0800997 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Charles Chan188ebf52015-12-23 00:15:11 -0800998 // store l3ecmpGroupKey with the ofdpaGroupChain for the nextObjective
999 // that depends on it
1000 updatePendingNextObjective(l3ecmpGroupKey, ofdpaGrp);
Charles Chan188ebf52015-12-23 00:15:11 -08001001 log.debug("Trying L3ECMP: device:{} gid:{} gkey:{} nextId:{}",
1002 deviceId, Integer.toHexString(l3ecmpGroupId),
1003 l3ecmpGroupKey, nextObj.id());
1004 // finally we are ready to send the innermost groups
1005 for (GroupInfo gi : unsentGroups) {
1006 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001007 Integer.toHexString(gi.innerMostGroupDesc().givenGroupId()), deviceId);
1008 updatePendingGroups(gi.nextGroupDesc().appCookie(), l3ecmpGce);
1009 groupService.addGroup(gi.innerMostGroupDesc());
Charles Chan188ebf52015-12-23 00:15:11 -08001010 }
Charles Chan188ebf52015-12-23 00:15:11 -08001011 }
1012
1013 /**
1014 * Creates group chains for all buckets in a hashed group, and stores the
1015 * GroupInfos and GroupKeys for all the groups in the lists passed in, which
1016 * should be empty.
1017 * <p>
1018 * Does not create the top level ECMP group. Does not actually send the
1019 * groups to the groupService.
1020 *
1021 * @param nextObj the Next Objective with buckets that need to be converted
1022 * to group chains
1023 * @param allGroupKeys a list to store groupKey for each bucket-group-chain
1024 * @param unsentGroups a list to store GroupInfo for each bucket-group-chain
1025 */
Pier Ventre140a8942016-11-02 07:26:38 -07001026 protected void createHashBucketChains(NextObjective nextObj,
Yi Tsengef19de12017-04-24 11:33:05 -07001027 List<Deque<GroupKey>> allGroupKeys,
1028 List<GroupInfo> unsentGroups) {
Charles Chan188ebf52015-12-23 00:15:11 -08001029 // break up hashed next objective to multiple groups
1030 Collection<TrafficTreatment> buckets = nextObj.next();
1031
1032 for (TrafficTreatment bucket : buckets) {
1033 //figure out how many labels are pushed in each bucket
1034 int labelsPushed = 0;
1035 MplsLabel innermostLabel = null;
1036 for (Instruction ins : bucket.allInstructions()) {
1037 if (ins.type() == Instruction.Type.L2MODIFICATION) {
1038 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
1039 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_PUSH) {
1040 labelsPushed++;
1041 }
1042 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) {
1043 if (innermostLabel == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001044 innermostLabel =
1045 ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label();
Charles Chan188ebf52015-12-23 00:15:11 -08001046 }
1047 }
1048 }
1049 }
Yi Tsengef19de12017-04-24 11:33:05 -07001050 Deque<GroupKey> gKeyChain = new ArrayDeque<>();
Saurav Dasa89b95a2018-02-14 14:14:54 -08001051 // here we only deal with 0 and 1 label push
Charles Chan188ebf52015-12-23 00:15:11 -08001052 if (labelsPushed == 0) {
Yi Tsengef19de12017-04-24 11:33:05 -07001053 GroupInfo noLabelGroupInfo;
Pier Ventre140a8942016-11-02 07:26:38 -07001054 TrafficSelector metaSelector = nextObj.meta();
1055 if (metaSelector != null) {
1056 if (isNotMplsBos(metaSelector)) {
Yi Tsengef19de12017-04-24 11:33:05 -07001057 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
1058 nextObj.appId(), true,
1059 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -07001060 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001061 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
1062 nextObj.appId(), false,
1063 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -07001064 }
1065 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001066 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
1067 nextObj.appId(), false,
1068 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -07001069 }
Yi Tsengef19de12017-04-24 11:33:05 -07001070 if (noLabelGroupInfo == null) {
Charles Chan188ebf52015-12-23 00:15:11 -08001071 log.error("Could not process nextObj={} in dev:{}",
1072 nextObj.id(), deviceId);
1073 return;
1074 }
Yi Tsengef19de12017-04-24 11:33:05 -07001075 gKeyChain.addFirst(noLabelGroupInfo.innerMostGroupDesc().appCookie());
1076 gKeyChain.addFirst(noLabelGroupInfo.nextGroupDesc().appCookie());
Charles Chan188ebf52015-12-23 00:15:11 -08001077 // we can't send the inner group description yet, as we have to
1078 // create the dependent ECMP group first. So we store..
Yi Tsengef19de12017-04-24 11:33:05 -07001079 unsentGroups.add(noLabelGroupInfo);
Charles Chan188ebf52015-12-23 00:15:11 -08001080 } else if (labelsPushed == 1) {
1081 GroupInfo onelabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
1082 nextObj.appId(), true,
1083 nextObj.meta());
1084 if (onelabelGroupInfo == null) {
1085 log.error("Could not process nextObj={} in dev:{}",
1086 nextObj.id(), deviceId);
1087 return;
1088 }
1089 // we need to add another group to this chain - the L3VPN group
1090 TrafficTreatment.Builder l3vpnTtb = DefaultTrafficTreatment.builder();
Charles Chan0f43e472017-02-14 14:00:16 -08001091 if (requireVlanPopBeforeMplsPush()) {
1092 l3vpnTtb.popVlan();
1093 }
Charles Chan188ebf52015-12-23 00:15:11 -08001094 l3vpnTtb.pushMpls()
1095 .setMpls(innermostLabel)
Yi Tsengef19de12017-04-24 11:33:05 -07001096 .group(new GroupId(onelabelGroupInfo.nextGroupDesc().givenGroupId()));
Charles Chan40132b32017-01-22 00:19:37 -08001097 if (supportCopyTtl()) {
1098 l3vpnTtb.copyTtlOut();
1099 }
1100 if (supportSetMplsBos()) {
1101 l3vpnTtb.setMplsBos(true);
1102 }
Charles Chan0f43e472017-02-14 14:00:16 -08001103 if (requireVlanPopBeforeMplsPush()) {
1104 l3vpnTtb.pushVlan().setVlanId(VlanId.vlanId(VlanId.RESERVED));
1105 }
Charles Chan188ebf52015-12-23 00:15:11 -08001106 GroupBucket l3vpnGrpBkt =
1107 DefaultGroupBucket.createIndirectGroupBucket(l3vpnTtb.build());
Saurav Das8be4e3a2016-03-11 17:19:07 -08001108 int l3vpnIndex = getNextAvailableIndex();
Yi Tsengef19de12017-04-24 11:33:05 -07001109 int l3vpnGroupId = MPLS_L3VPN_SUBTYPE | (SUBTYPE_MASK & l3vpnIndex);
1110 GroupKey l3vpnGroupKey = new DefaultGroupKey(
1111 appKryo.serialize(l3vpnIndex));
Charles Chan188ebf52015-12-23 00:15:11 -08001112 GroupDescription l3vpnGroupDesc =
1113 new DefaultGroupDescription(
1114 deviceId,
1115 GroupDescription.Type.INDIRECT,
Yi Tsengef19de12017-04-24 11:33:05 -07001116 new GroupBuckets(Collections.singletonList(l3vpnGrpBkt)),
1117 l3vpnGroupKey,
1118 l3vpnGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -08001119 nextObj.appId());
Yi Tsengef19de12017-04-24 11:33:05 -07001120 GroupChainElem l3vpnGce = new GroupChainElem(l3vpnGroupDesc,
1121 1,
1122 false,
1123 deviceId);
1124 updatePendingGroups(onelabelGroupInfo.nextGroupDesc().appCookie(), l3vpnGce);
Charles Chan188ebf52015-12-23 00:15:11 -08001125
Yi Tsengef19de12017-04-24 11:33:05 -07001126 gKeyChain.addFirst(onelabelGroupInfo.innerMostGroupDesc().appCookie());
1127 gKeyChain.addFirst(onelabelGroupInfo.nextGroupDesc().appCookie());
1128 gKeyChain.addFirst(l3vpnGroupKey);
Charles Chan188ebf52015-12-23 00:15:11 -08001129 //now we can replace the outerGrpDesc with the one we just created
Yi Tsengef19de12017-04-24 11:33:05 -07001130 onelabelGroupInfo.nextGroupDesc(l3vpnGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -08001131 // we can't send the innermost group yet, as we have to create
1132 // the dependent ECMP group first. So we store ...
1133 unsentGroups.add(onelabelGroupInfo);
Yi Tsengef19de12017-04-24 11:33:05 -07001134 log.debug("Trying L3VPN: device:{} gid:{} group key:{} nextId:{}",
1135 deviceId, Integer.toHexString(l3vpnGroupId),
1136 l3vpnGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -08001137 } else {
1138 log.warn("Driver currently does not handle more than 1 MPLS "
1139 + "labels. Not processing nextObjective {}", nextObj.id());
1140 return;
1141 }
Charles Chan188ebf52015-12-23 00:15:11 -08001142 // all groups in this chain
Yi Tsengef19de12017-04-24 11:33:05 -07001143 allGroupKeys.add(gKeyChain);
Charles Chan188ebf52015-12-23 00:15:11 -08001144 }
1145 }
1146
Pier Ventre42287df2016-11-09 14:17:26 -08001147 /**
1148 * Processes the pseudo wire related next objective.
1149 * This procedure try to reuse the mpls label groups,
1150 * the mpls interface group and the l2 interface group.
1151 *
1152 * @param nextObjective the objective to process.
1153 */
1154 protected void processPwNextObjective(NextObjective nextObjective) {
Saurav Das1547b3f2017-05-05 17:01:08 -07001155 log.warn("Pseudo wire extensions are not supported in OFDPA 2.0 {}",
1156 nextObjective.id());
Pier Ventre42287df2016-11-09 14:17:26 -08001157 }
1158
Saurav Das8be4e3a2016-03-11 17:19:07 -08001159 //////////////////////////////////////
1160 // Group Editing
1161 //////////////////////////////////////
Charles Chan188ebf52015-12-23 00:15:11 -08001162 /**
1163 * Adds a bucket to the top level group of a group-chain, and creates the chain.
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001164 * Ensures that bucket being added is not a duplicate, by checking existing
Yi Tsengef19de12017-04-24 11:33:05 -07001165 * buckets for the same output port.
Charles Chan188ebf52015-12-23 00:15:11 -08001166 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001167 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -08001168 * @param next the representation of the existing group-chain for this next objective
1169 */
1170 protected void addBucketToGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001171 if (nextObjective.type() != NextObjective.Type.HASHED &&
1172 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001173 log.warn("AddBuckets not applied to nextType:{} in dev:{} for next:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001174 nextObjective.type(), deviceId, nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -07001175 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001176 return;
1177 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001178 // first check to see if bucket being added is not a duplicate of an
Yi Tsengef19de12017-04-24 11:33:05 -07001179 // existing bucket. If it is for an existing output port, then its a
1180 // duplicate.
Yi Tseng78f51f42017-02-02 13:54:58 -08001181 Set<TrafficTreatment> duplicateBuckets = Sets.newHashSet();
Yi Tsengef19de12017-04-24 11:33:05 -07001182 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
1183 Set<PortNumber> existingPorts = getExistingOutputPorts(allActiveKeys,
1184 groupService,
1185 deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -08001186 Set<TrafficTreatment> nonDuplicateBuckets = Sets.newHashSet();
1187 NextObjective objectiveToAdd;
Yi Tseng78f51f42017-02-02 13:54:58 -08001188 nextObjective.next().forEach(trafficTreatment -> {
1189 PortNumber portNumber = readOutPortFromTreatment(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001190 if (portNumber == null) {
1191 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001192 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001193 if (existingPorts.contains(portNumber)) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001194 // its possible that portnumbers are same but labels are different
1195 int label = readLabelFromTreatment(trafficTreatment);
1196 if (label == -1) {
1197 duplicateBuckets.add(trafficTreatment);
1198 } else {
Saurav Dasceccf242017-08-03 18:30:35 -07001199 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1200 groupService, deviceId,
1201 portNumber, label);
1202 if (!existing.isEmpty()) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001203 duplicateBuckets.add(trafficTreatment);
1204 } else {
1205 nonDuplicateBuckets.add(trafficTreatment);
1206 }
1207 }
Yi Tseng47f82dc2017-03-05 22:48:39 -08001208 } else {
1209 nonDuplicateBuckets.add(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001210 }
1211 });
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001212 if (duplicateBuckets.isEmpty()) {
1213 // use the original objective
1214 objectiveToAdd = nextObjective;
1215 } else if (!nonDuplicateBuckets.isEmpty()) {
1216 // only use the non-duplicate buckets if there are any
1217 log.debug("Some buckets {} already exist in next id {}, duplicate "
1218 + "buckets will be ignored.", duplicateBuckets, nextObjective.id());
1219 // new next objective with non duplicate treatments
Yi Tseng47f82dc2017-03-05 22:48:39 -08001220 NextObjective.Builder builder = DefaultNextObjective.builder()
1221 .withType(nextObjective.type())
1222 .withId(nextObjective.id())
1223 .withMeta(nextObjective.meta())
1224 .fromApp(nextObjective.appId());
1225 nonDuplicateBuckets.forEach(builder::addTreatment);
Yi Tseng47f82dc2017-03-05 22:48:39 -08001226 ObjectiveContext context = nextObjective.context().orElse(null);
1227 objectiveToAdd = builder.addToExisting(context);
1228 } else {
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001229 // buckets to add are already there - nothing to do
Saurav Das1547b3f2017-05-05 17:01:08 -07001230 log.debug("buckets already exist {} in next: {} ..ignoring bucket add",
1231 duplicateBuckets, nextObjective.id());
1232 pass(nextObjective);
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001233 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001234 }
Saurav Das1a129a02016-11-18 15:21:57 -08001235 if (nextObjective.type() == NextObjective.Type.HASHED) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001236 addBucketToHashGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001237 } else if (nextObjective.type() == NextObjective.Type.BROADCAST) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001238 addBucketToBroadcastGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001239 }
1240 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001241
Saurav Das1a129a02016-11-18 15:21:57 -08001242 private void addBucketToHashGroup(NextObjective nextObjective,
Yi Tseng78f51f42017-02-02 13:54:58 -08001243 List<Deque<GroupKey>> allActiveKeys) {
Charles Chan188ebf52015-12-23 00:15:11 -08001244 // storage for all group keys in the chain of groups created
1245 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
1246 List<GroupInfo> unsentGroups = new ArrayList<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001247 List<GroupBucket> newBuckets;
Charles Chan188ebf52015-12-23 00:15:11 -08001248 createHashBucketChains(nextObjective, allGroupKeys, unsentGroups);
Yi Tseng78f51f42017-02-02 13:54:58 -08001249 // now we can create the buckets to add to the outermost L3 ECMP group
1250 newBuckets = generateNextGroupBuckets(unsentGroups, SELECT);
Saurav Das1a129a02016-11-18 15:21:57 -08001251 // retrieve the original L3 ECMP group
Jonghwan Hyun25199f22018-02-12 16:43:45 +09001252 Group l3ecmpGroup = retrieveTopLevelGroup(allActiveKeys, deviceId,
1253 groupService, nextObjective.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001254 if (l3ecmpGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001255 fail(nextObjective, ObjectiveError.GROUPMISSING);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001256 return;
1257 }
Saurav Das1a129a02016-11-18 15:21:57 -08001258 GroupKey l3ecmpGroupKey = l3ecmpGroup.appCookie();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001259 int l3ecmpGroupId = l3ecmpGroup.id().id();
Charles Chan188ebf52015-12-23 00:15:11 -08001260 // Although GroupDescriptions are not necessary for adding buckets to
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001261 // existing groups, we still use one in the GroupChainElem. When the latter is
Charles Chan188ebf52015-12-23 00:15:11 -08001262 // processed, the info will be extracted for the bucketAdd call to groupService
1263 GroupDescription l3ecmpGroupDesc =
Yi Tsengef19de12017-04-24 11:33:05 -07001264 new DefaultGroupDescription(deviceId,
1265 SELECT,
1266 new GroupBuckets(newBuckets),
1267 l3ecmpGroupKey,
1268 l3ecmpGroupId,
1269 nextObjective.appId());
Yi Tseng78f51f42017-02-02 13:54:58 -08001270 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
1271 unsentGroups.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001272 true,
1273 deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001274
Saurav Dasc88d4662017-05-15 15:34:25 -07001275 // update new bucket-chains
1276 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
1277 for (Deque<GroupKey> newBucketChain : allGroupKeys) {
1278 newBucketChain.addFirst(l3ecmpGroupKey);
1279 addedKeys.add(newBucketChain);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001280 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001281 updatePendingNextObjective(l3ecmpGroupKey,
1282 new OfdpaNextGroup(addedKeys, nextObjective));
Yi Tsengef19de12017-04-24 11:33:05 -07001283 log.debug("Adding to L3ECMP: device:{} gid:{} group key:{} nextId:{}",
Charles Chan188ebf52015-12-23 00:15:11 -08001284 deviceId, Integer.toHexString(l3ecmpGroupId),
1285 l3ecmpGroupKey, nextObjective.id());
Yi Tseng78f51f42017-02-02 13:54:58 -08001286 unsentGroups.forEach(groupInfo -> {
1287 // send the innermost group
1288 log.debug("Sending innermost group {} in group chain on device {} ",
Saurav Dasc88d4662017-05-15 15:34:25 -07001289 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
1290 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001291 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3ecmpGce);
1292 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001293 });
Saurav Das1a129a02016-11-18 15:21:57 -08001294 }
Charles Chan188ebf52015-12-23 00:15:11 -08001295
Saurav Das1a129a02016-11-18 15:21:57 -08001296 private void addBucketToBroadcastGroup(NextObjective nextObj,
Yi Tsengef19de12017-04-24 11:33:05 -07001297 List<Deque<GroupKey>> allActiveKeys) {
1298 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001299 if (assignedVlan == null) {
1300 log.warn("VLAN ID required by broadcast next obj is missing. "
1301 + "Aborting add bucket to broadcast group for next:{} in dev:{}",
1302 nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001303 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001304 return;
1305 }
Saurav Das1a129a02016-11-18 15:21:57 -08001306 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Yi Tsengef19de12017-04-24 11:33:05 -07001307 IpPrefix ipDst = readIpDstFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001308 if (ipDst != null) {
1309 if (ipDst.isMulticast()) {
Yi Tsengef19de12017-04-24 11:33:05 -07001310 addBucketToL3MulticastGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001311 } else {
1312 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001313 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001314 }
1315 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001316 addBucketToL2FloodGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001317 }
1318 }
1319
1320 private void addBucketToL2FloodGroup(NextObjective nextObj,
1321 List<Deque<GroupKey>> allActiveKeys,
1322 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001323 VlanId assignedVlan) {
Jonghwan Hyun25199f22018-02-12 16:43:45 +09001324 Group l2FloodGroup = retrieveTopLevelGroup(allActiveKeys, deviceId,
1325 groupService, nextObj.id());
Yi Tseng78f51f42017-02-02 13:54:58 -08001326
1327 if (l2FloodGroup == null) {
1328 log.warn("Can't find L2 flood group while adding bucket to it. NextObj = {}",
1329 nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001330 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001331 return;
1332 }
Saurav Das1a129a02016-11-18 15:21:57 -08001333
Yi Tseng78f51f42017-02-02 13:54:58 -08001334 GroupKey l2floodGroupKey = l2FloodGroup.appCookie();
1335 int l2floodGroupId = l2FloodGroup.id().id();
1336 List<GroupBucket> newBuckets = generateNextGroupBuckets(groupInfos, ALL);
Yi Tseng78f51f42017-02-02 13:54:58 -08001337 GroupDescription l2FloodGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001338 new DefaultGroupDescription(deviceId,
1339 ALL,
1340 new GroupBuckets(newBuckets),
1341 l2floodGroupKey,
1342 l2floodGroupId,
1343 nextObj.appId());
Yi Tseng78f51f42017-02-02 13:54:58 -08001344 GroupChainElem l2FloodGroupChainElement =
1345 new GroupChainElem(l2FloodGroupDescription,
1346 groupInfos.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001347 true,
1348 deviceId);
Yi Tseng78f51f42017-02-02 13:54:58 -08001349
Yi Tseng78f51f42017-02-02 13:54:58 -08001350
1351 //ensure assignedVlan applies to the chosen group
1352 VlanId floodGroupVlan = extractVlanIdFromGroupId(l2floodGroupId);
Yi Tseng78f51f42017-02-02 13:54:58 -08001353 if (!floodGroupVlan.equals(assignedVlan)) {
1354 log.warn("VLAN ID {} does not match Flood group {} to which bucket is "
1355 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1356 Integer.toHexString(l2floodGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001357 fail(nextObj, ObjectiveError.BADPARAMS);
Yi Tseng78f51f42017-02-02 13:54:58 -08001358 return;
1359 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001360 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
Yi Tseng78f51f42017-02-02 13:54:58 -08001361 groupInfos.forEach(groupInfo -> {
1362 // update original NextGroup with new bucket-chain
Yi Tseng78f51f42017-02-02 13:54:58 -08001363 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001364 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001365 newBucketChain.addFirst(l2floodGroupKey);
Saurav Dasc88d4662017-05-15 15:34:25 -07001366 addedKeys.add(newBucketChain);
Yi Tsengef19de12017-04-24 11:33:05 -07001367 log.debug("Adding to L2FLOOD: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001368 deviceId, Integer.toHexString(l2floodGroupId),
1369 l2floodGroupKey, nextObj.id());
1370 // send the innermost group
1371 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001372 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001373 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001374 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l2FloodGroupChainElement);
Yi Tseng78f51f42017-02-02 13:54:58 -08001375
Yi Tsengef19de12017-04-24 11:33:05 -07001376 DeviceId innerMostGroupDevice = groupInfo.innerMostGroupDesc().deviceId();
1377 GroupKey innerMostGroupKey = groupInfo.innerMostGroupDesc().appCookie();
Yi Tseng78f51f42017-02-02 13:54:58 -08001378 Group existsL2IGroup = groupService.getGroup(innerMostGroupDevice, innerMostGroupKey);
1379
1380 if (existsL2IGroup != null) {
1381 // group already exist
1382 processPendingAddGroupsOrNextObjs(innerMostGroupKey, true);
1383 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001384 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001385 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001386 });
Saurav Dasc88d4662017-05-15 15:34:25 -07001387
1388 updatePendingNextObjective(l2floodGroupKey,
1389 new OfdpaNextGroup(addedKeys, nextObj));
Yi Tseng78f51f42017-02-02 13:54:58 -08001390 }
1391
Saurav Das1a129a02016-11-18 15:21:57 -08001392 private void addBucketToL3MulticastGroup(NextObjective nextObj,
1393 List<Deque<GroupKey>> allActiveKeys,
1394 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001395 VlanId assignedVlan) {
Pier Luigi7b7a29d2018-01-19 10:24:53 +01001396 // Create the buckets to add to the outermost L3 Multicast group
1397 List<GroupBucket> newBuckets = createL3MulticastBucket(groupInfos);
Saurav Das1a129a02016-11-18 15:21:57 -08001398
1399 // get the group being edited
Jonghwan Hyun25199f22018-02-12 16:43:45 +09001400 Group l3mcastGroup = retrieveTopLevelGroup(allActiveKeys, deviceId,
1401 groupService, nextObj.id());
Saurav Das1a129a02016-11-18 15:21:57 -08001402 if (l3mcastGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001403 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001404 return;
1405 }
1406 GroupKey l3mcastGroupKey = l3mcastGroup.appCookie();
1407 int l3mcastGroupId = l3mcastGroup.id().id();
1408
1409 //ensure assignedVlan applies to the chosen group
Yi Tseng78f51f42017-02-02 13:54:58 -08001410 VlanId expectedVlan = extractVlanIdFromGroupId(l3mcastGroupId);
Saurav Das1a129a02016-11-18 15:21:57 -08001411 if (!expectedVlan.equals(assignedVlan)) {
1412 log.warn("VLAN ID {} does not match L3 Mcast group {} to which bucket is "
1413 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1414 Integer.toHexString(l3mcastGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001415 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001416 }
1417 GroupDescription l3mcastGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001418 new DefaultGroupDescription(deviceId,
1419 ALL,
1420 new GroupBuckets(newBuckets),
1421 l3mcastGroupKey,
1422 l3mcastGroupId,
1423 nextObj.appId());
Saurav Das1a129a02016-11-18 15:21:57 -08001424 GroupChainElem l3mcastGce = new GroupChainElem(l3mcastGroupDescription,
Yi Tsengef19de12017-04-24 11:33:05 -07001425 groupInfos.size(),
1426 true,
1427 deviceId);
Saurav Dasc88d4662017-05-15 15:34:25 -07001428
1429 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
Yi Tseng78f51f42017-02-02 13:54:58 -08001430 groupInfos.forEach(groupInfo -> {
1431 // update original NextGroup with new bucket-chain
1432 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001433 newBucketChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001434 // Add L3 interface group to the chain if there is one.
Yi Tsengef19de12017-04-24 11:33:05 -07001435 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1436 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001437 }
1438 newBucketChain.addFirst(l3mcastGroupKey);
Saurav Dasc88d4662017-05-15 15:34:25 -07001439 addedKeys.add(newBucketChain);
Yi Tseng78f51f42017-02-02 13:54:58 -08001440
Yi Tsengef19de12017-04-24 11:33:05 -07001441 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3mcastGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001442 // Point next group to inner-most group, if any
Yi Tsengef19de12017-04-24 11:33:05 -07001443 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1444 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(),
1445 1,
1446 false,
1447 deviceId);
1448 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001449 }
Yi Tsengef19de12017-04-24 11:33:05 -07001450 log.debug("Adding to L3MCAST: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001451 deviceId, Integer.toHexString(l3mcastGroupId),
1452 l3mcastGroupKey, nextObj.id());
1453 // send the innermost group
1454 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001455 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001456 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001457 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001458
1459 });
1460
Saurav Das1a129a02016-11-18 15:21:57 -08001461 updatePendingNextObjective(l3mcastGroupKey,
Saurav Dasc88d4662017-05-15 15:34:25 -07001462 new OfdpaNextGroup(addedKeys, nextObj));
Charles Chan188ebf52015-12-23 00:15:11 -08001463 }
1464
1465 /**
Saurav Dasceccf242017-08-03 18:30:35 -07001466 * Removes buckets in the top level group of a possible group-chain. Does
Saurav Das1a129a02016-11-18 15:21:57 -08001467 * not remove the groups in the group-chain pointed to by this bucket, as they
Charles Chan188ebf52015-12-23 00:15:11 -08001468 * may be in use (referenced by other groups) elsewhere.
1469 *
Saurav Dasceccf242017-08-03 18:30:35 -07001470 * @param nextObjective a next objective that contains information for the
1471 * buckets to be removed from the group
1472 * @param next the representation of the existing group-chains for this next
1473 * objective, from which the top-level buckets to remove are determined
Charles Chan188ebf52015-12-23 00:15:11 -08001474 */
1475 protected void removeBucketFromGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001476 if (nextObjective.type() != NextObjective.Type.HASHED &&
1477 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001478 log.warn("RemoveBuckets not applied to nextType:{} in dev:{} for next:{}",
1479 nextObjective.type(), deviceId, nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -07001480 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001481 return;
1482 }
Yi Tsengef19de12017-04-24 11:33:05 -07001483 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Saurav Dasceccf242017-08-03 18:30:35 -07001484 List<Integer> indicesToRemove = Lists.newArrayList();
1485 for (TrafficTreatment treatment : nextObjective.next()) {
1486 // find the top-level bucket in the group-chain by matching the
1487 // outport and label from different groups in the chain
1488 PortNumber portToRemove = readOutPortFromTreatment(treatment);
1489 int labelToRemove = readLabelFromTreatment(treatment);
1490 if (portToRemove == null) {
1491 log.warn("treatment {} of next objective {} has no outport.. "
1492 + "cannot remove bucket from group in dev: {}", treatment,
1493 nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001494 continue;
1495 }
Saurav Dasceccf242017-08-03 18:30:35 -07001496 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1497 groupService, deviceId,
1498 portToRemove, labelToRemove);
1499 indicesToRemove.addAll(existing);
1500
Charles Chan188ebf52015-12-23 00:15:11 -08001501 }
Saurav Dasceccf242017-08-03 18:30:35 -07001502
1503 List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList();
1504 indicesToRemove.forEach(index -> chainsToRemove
1505 .add(allActiveKeys.get(index)));
Yi Tseng78f51f42017-02-02 13:54:58 -08001506 if (chainsToRemove.isEmpty()) {
Charles Chan188ebf52015-12-23 00:15:11 -08001507 log.warn("Could not find appropriate group-chain for removing bucket"
1508 + " for next id {} in dev:{}", nextObjective.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001509 fail(nextObjective, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001510 return;
Charles Chan188ebf52015-12-23 00:15:11 -08001511 }
Saurav Dasceccf242017-08-03 18:30:35 -07001512 removeBucket(chainsToRemove, nextObjective);
1513 }
1514
1515 /**
1516 * Removes top-level buckets from a group that represents the given next objective.
1517 *
1518 * @param chainsToRemove a list of group bucket chains to remove
1519 * @param nextObjective the next objective that contains information for the
1520 * buckets to be removed from the group
1521 */
1522 protected void removeBucket(List<Deque<GroupKey>> chainsToRemove,
1523 NextObjective nextObjective) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001524 List<GroupBucket> bucketsToRemove = Lists.newArrayList();
1525 //first group key is the one we want to modify
1526 GroupKey modGroupKey = chainsToRemove.get(0).peekFirst();
Saurav Das1a129a02016-11-18 15:21:57 -08001527 Group modGroup = groupService.getGroup(deviceId, modGroupKey);
Ray Milkey8299dc92018-05-08 11:35:45 -07001528 if (modGroup == null) {
1529 log.warn("removeBucket(): Attempt to modify non-existent group {} for device {}", modGroupKey, deviceId);
1530 return;
1531 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001532 for (Deque<GroupKey> foundChain : chainsToRemove) {
1533 //second group key is the one we wish to remove the reference to
1534 if (foundChain.size() < 2) {
Saurav Dasceccf242017-08-03 18:30:35 -07001535 // additional check to make sure second group key exists in
Yi Tseng78f51f42017-02-02 13:54:58 -08001536 // the chain.
1537 log.warn("Can't find second group key from chain {}",
1538 foundChain);
1539 continue;
Saurav Das1a129a02016-11-18 15:21:57 -08001540 }
Saurav Dasceccf242017-08-03 18:30:35 -07001541 GroupKey pointedGroupKey = foundChain.stream()
1542 .collect(Collectors.toList()).get(1);
Yi Tseng78f51f42017-02-02 13:54:58 -08001543 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
Yi Tseng78f51f42017-02-02 13:54:58 -08001544 if (pointedGroup == null) {
1545 continue;
1546 }
1547
1548 GroupBucket bucket;
1549 if (nextObjective.type() == NextObjective.Type.HASHED) {
1550 bucket = DefaultGroupBucket.createSelectGroupBucket(
1551 DefaultTrafficTreatment.builder()
1552 .group(pointedGroup.id())
1553 .build());
1554 } else {
1555 bucket = DefaultGroupBucket.createAllGroupBucket(
1556 DefaultTrafficTreatment.builder()
1557 .group(pointedGroup.id())
1558 .build());
1559 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001560 bucketsToRemove.add(bucket);
Saurav Das1a129a02016-11-18 15:21:57 -08001561 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001562
1563 GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove);
1564 List<String> pointedGroupIds; // for debug log
1565 pointedGroupIds = bucketsToRemove.stream()
1566 .map(GroupBucket::treatment)
1567 .map(TrafficTreatment::allInstructions)
1568 .flatMap(List::stream)
1569 .filter(inst -> inst instanceof Instructions.GroupInstruction)
1570 .map(inst -> (Instructions.GroupInstruction) inst)
1571 .map(Instructions.GroupInstruction::groupId)
1572 .map(GroupId::id)
1573 .map(Integer::toHexString)
1574 .map(id -> HEX_PREFIX + id)
1575 .collect(Collectors.toList());
1576
Yi Tseng78f51f42017-02-02 13:54:58 -08001577 log.debug("Removing buckets from group id 0x{} pointing to group id(s) {} "
Saurav Das1a129a02016-11-18 15:21:57 -08001578 + "for next id {} in device {}", Integer.toHexString(modGroup.id().id()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001579 pointedGroupIds, nextObjective.id(), deviceId);
1580 addPendingUpdateNextObjective(modGroupKey, nextObjective);
Saurav Das1a129a02016-11-18 15:21:57 -08001581 groupService.removeBucketsFromGroup(deviceId, modGroupKey,
1582 removeBuckets, modGroupKey,
1583 nextObjective.appId());
Saurav Dasceccf242017-08-03 18:30:35 -07001584 // update store - synchronize access as there may be multiple threads
1585 // trying to remove buckets from the same group, each with its own
1586 // potentially stale copy of allActiveKeys
Saurav Dasc88d4662017-05-15 15:34:25 -07001587 synchronized (flowObjectiveStore) {
Saurav Dasceccf242017-08-03 18:30:35 -07001588 // get a fresh copy of what the store holds
1589 NextGroup next = flowObjectiveStore.getNextGroup(nextObjective.id());
1590 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Pier Luigiaeb11a72018-01-15 09:48:49 +01001591 allActiveKeys = Lists.newArrayList(allActiveKeys);
Saurav Dasc88d4662017-05-15 15:34:25 -07001592 // Note that since we got a new object, and ArrayDeque does not implement
Saurav Dasceccf242017-08-03 18:30:35 -07001593 // Object.equals(), we have to check the deque elems one by one
1594 allActiveKeys
1595 .removeIf(active ->
1596 chainsToRemove.stream().anyMatch(remove ->
1597 Arrays.equals(remove.toArray(new GroupKey[0]),
1598 active.toArray(new GroupKey[0]))));
Saurav Dasc88d4662017-05-15 15:34:25 -07001599 // If no buckets in the group, then retain an entry for the
1600 // top level group which still exists.
1601 if (allActiveKeys.isEmpty()) {
1602 ArrayDeque<GroupKey> top = new ArrayDeque<>();
1603 top.add(modGroupKey);
1604 allActiveKeys.add(top);
1605 }
1606 flowObjectiveStore.putNextGroup(nextObjective.id(),
Saurav Dasceccf242017-08-03 18:30:35 -07001607 new OfdpaNextGroup(allActiveKeys,
1608 nextObjective));
Saurav Das1a129a02016-11-18 15:21:57 -08001609 }
Charles Chan188ebf52015-12-23 00:15:11 -08001610 }
1611
1612 /**
Saurav Das961beb22017-03-29 19:09:17 -07001613 * Removes all groups in multiple possible group-chains that represent the next-obj.
Charles Chan188ebf52015-12-23 00:15:11 -08001614 *
1615 * @param nextObjective the next objective to remove
1616 * @param next the NextGroup that represents the existing group-chain for
1617 * this next objective
1618 */
1619 protected void removeGroup(NextObjective nextObjective, NextGroup next) {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -07001620
Yi Tsengef19de12017-04-24 11:33:05 -07001621 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Charles Chanfc5c7802016-05-17 13:13:55 -07001622
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001623 List<GroupKey> groupKeys = allActiveKeys.stream()
Charles Chanfc5c7802016-05-17 13:13:55 -07001624 .map(Deque::getFirst).collect(Collectors.toList());
Yi Tsengef19de12017-04-24 11:33:05 -07001625 addPendingRemoveNextObjective(nextObjective, groupKeys);
Charles Chanfc5c7802016-05-17 13:13:55 -07001626
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -07001627 allActiveKeys
1628 .forEach(groupChain -> groupChain.forEach(groupKey -> groupService
1629 .removeGroup(deviceId, groupKey, nextObjective.appId())));
Charles Chan188ebf52015-12-23 00:15:11 -08001630 flowObjectiveStore.removeNextGroup(nextObjective.id());
1631 }
1632
Saurav Dasceccf242017-08-03 18:30:35 -07001633 /**
Ruchi Sahotaf77dc0f2019-01-28 01:08:18 +00001634 * modifies group with next objective.
1635 *
1636 * @param nextObjective the NextObjective
1637 * @param nextGroup the NextGroup
1638 */
1639 protected void modifyBucketFromGroup(NextObjective nextObjective, NextGroup nextGroup) {
1640 switch (nextObjective.type()) {
1641 case SIMPLE:
1642 Collection<TrafficTreatment> treatments = nextObjective.next();
1643 if (treatments.size() != 1) {
1644 log.error("Next Objectives of type Simple should only have a "
1645 + "single Traffic Treatment. Next Objective Id:{}",
1646 nextObjective.id());
1647 fail(nextObjective, ObjectiveError.BADPARAMS);
1648 return;
1649 }
1650 modifySimpleNextObjective(nextObjective, nextGroup);
1651 break;
1652 default:
1653 fail(nextObjective, ObjectiveError.UNKNOWN);
1654 log.warn("Unknown next objective type {}", nextObjective.type());
1655 }
1656 }
1657
1658 /**
1659 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
1660 * a chain of groups. The simple Next Objective passed in by the application
1661 * is broken up into a group chain. The following chains can be modified
1662 * depending on the parameters in the Next Objective.
1663 * 1. L2 Interface group (no chaining)
1664 * 2. L3 Unicast group -> L2 Interface group
1665 *
1666 * @param nextObj the nextObjective of type SIMPLE
1667 */
1668 private void modifySimpleNextObjective(NextObjective nextObj, NextGroup nextGroup) {
1669 TrafficTreatment treatment = nextObj.next().iterator().next();
1670 // determine if plain L2 or L3->L2 chain
1671 boolean plainL2 = true;
1672 for (Instruction ins : treatment.allInstructions()) {
1673 if (ins.type() == Instruction.Type.L2MODIFICATION) {
1674 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
1675 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_DST ||
1676 l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_SRC ||
1677 l2ins.subtype() == L2ModificationInstruction.L2SubType.VLAN_ID) {
1678 plainL2 = false;
1679 }
1680 }
1681 }
1682 if (plainL2) {
1683 modifyBucketInL2Group(nextObj, nextGroup);
1684 } else {
1685 modifyBucketInL3Group(nextObj, nextGroup);
1686 }
1687 return;
1688 }
1689
1690
1691 /**
Jonghwan Hyun25199f22018-02-12 16:43:45 +09001692 * Modify buckets in the L2 interface group.
1693 *
1694 * @param nextObjective a next objective that contains information for the
1695 * buckets to be modified in the group
1696 * @param next the representation of the existing group-chains for this next
1697 * objective, from which the innermost group buckets to remove are determined
1698 */
Ruchi Sahotaf77dc0f2019-01-28 01:08:18 +00001699
1700 protected void modifyBucketInL2Group(NextObjective nextObjective, NextGroup next) {
Jonghwan Hyun25199f22018-02-12 16:43:45 +09001701
1702 VlanId assignedVlan = readVlanFromSelector(nextObjective.meta());
1703 if (assignedVlan == null) {
1704 log.warn("VLAN ID required by simple next obj is missing. Abort.");
1705 fail(nextObjective, ObjectiveError.BADPARAMS);
1706 return;
1707 }
1708
1709 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObjective, assignedVlan);
1710
1711 // There is only one L2 interface group in this case
1712 GroupDescription l2InterfaceGroupDesc = groupInfos.get(0).innerMostGroupDesc();
1713
1714 // Replace group bucket for L2 interface group
1715 groupService.setBucketsForGroup(deviceId,
1716 l2InterfaceGroupDesc.appCookie(),
1717 l2InterfaceGroupDesc.buckets(),
1718 l2InterfaceGroupDesc.appCookie(),
1719 l2InterfaceGroupDesc.appId());
1720
1721 // update store - synchronize access as there may be multiple threads
1722 // trying to remove buckets from the same group, each with its own
1723 // potentially stale copy of allActiveKeys
1724 synchronized (flowObjectiveStore) {
1725 List<Deque<GroupKey>> modifiedGroupKeys = Lists.newArrayList();
1726 ArrayDeque<GroupKey> top = new ArrayDeque<>();
1727 top.add(l2InterfaceGroupDesc.appCookie());
1728 modifiedGroupKeys.add(top);
1729
1730 flowObjectiveStore.putNextGroup(nextObjective.id(),
1731 new OfdpaNextGroup(modifiedGroupKeys,
1732 nextObjective));
1733 }
Ruchi Sahotaf77dc0f2019-01-28 01:08:18 +00001734
Jonghwan Hyun25199f22018-02-12 16:43:45 +09001735 }
1736
Ruchi Sahotaf77dc0f2019-01-28 01:08:18 +00001737 protected void modifyBucketInL3Group(NextObjective nextObjective, NextGroup next) {
1738
1739 //get l3 group
1740 GroupInfo groupInfo = prepareL3UnicastGroup(nextObjective, next);
1741 if (groupInfo == null) {
1742 log.warn("Null groupInfo retrieved for next obj. Abort.");
1743 fail(nextObjective, ObjectiveError.BADPARAMS);
1744 return;
1745 }
1746
1747 GroupDescription l3UnicastGroupDesc = groupInfo.nextGroupDesc();
1748
1749 // Replace group bucket for L3 UC interface group
1750 groupService.setBucketsForGroup(deviceId, l3UnicastGroupDesc.appCookie(),
1751 l3UnicastGroupDesc.buckets(), l3UnicastGroupDesc.appCookie(),
1752 l3UnicastGroupDesc.appId());
1753
1754 // create object for local and distributed storage
1755 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
1756 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
1757 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
1758 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
1759 allGroupKeys.add(gkeyChain);
1760 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObjective);
1761 // store l3groupkey with the ofdpaNextGroup for the nextObjective that depends on it
1762 updatePendingNextObjective(groupInfo.nextGroupDesc().appCookie(), ofdpaGrp);
1763
1764 // update store - synchronize access as there may be multiple threads
1765 // trying to update bucket from the same group, each with its own
1766 // potentially stale copy of allActiveKeys
1767 synchronized (flowObjectiveStore) {
1768
1769 List<Deque<GroupKey>> modifiedGroupKeys = Lists.newArrayList();
1770 ArrayDeque<GroupKey> top = new ArrayDeque<>();
1771 top.add(l3UnicastGroupDesc.appCookie());
1772 top.add(groupInfo.innerMostGroupDesc().appCookie()); //l2 group key
1773 modifiedGroupKeys.add(top);
1774
1775 flowObjectiveStore.putNextGroup(nextObjective.id(),
1776 new OfdpaNextGroup(modifiedGroupKeys,
1777 nextObjective));
1778 }
1779 }
1780
1781
Jonghwan Hyun25199f22018-02-12 16:43:45 +09001782 /**
Saurav Dasceccf242017-08-03 18:30:35 -07001783 * Checks existing buckets in {@link NextGroup} to verify if they match
1784 * the buckets in the given {@link NextObjective}. Adds or removes buckets
1785 * to ensure that the buckets match up.
1786 *
1787 * @param nextObjective the next objective to verify
1788 * @param next the representation of the existing group which has to be
1789 * modified to match the given next objective
1790 */
1791 protected void verifyGroup(NextObjective nextObjective, NextGroup next) {
Pier Luigi3dfd8352018-01-25 16:16:02 +01001792 if (nextObjective.type() == NextObjective.Type.SIMPLE) {
1793 log.warn("verification not supported for indirect group");
Saurav Dasceccf242017-08-03 18:30:35 -07001794 fail(nextObjective, ObjectiveError.UNSUPPORTED);
1795 return;
1796 }
Charles Chan1a446ca2018-05-08 11:49:05 -07001797 log.trace("Call to verify device:{} nextId:{}", deviceId, nextObjective.id());
Saurav Dasceccf242017-08-03 18:30:35 -07001798 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
1799 List<TrafficTreatment> bucketsToCreate = Lists.newArrayList();
1800 List<Integer> indicesToRemove = Lists.newArrayList();
Pier Luigi4a7cdfa2018-01-29 10:30:59 +01001801
1802 // Iterating over the treatments of the next objective allows
1803 // to detect missing buckets and/or duplicate buckets (to be removed)
Saurav Dasceccf242017-08-03 18:30:35 -07001804 for (TrafficTreatment bkt : nextObjective.next()) {
1805 PortNumber portNumber = readOutPortFromTreatment(bkt);
1806 int label = readLabelFromTreatment(bkt);
1807 if (portNumber == null) {
1808 log.warn("treatment {} of next objective {} has no outport.. "
1809 + "cannot remove bucket from group in dev: {}", bkt,
1810 nextObjective.id(), deviceId);
1811 fail(nextObjective, ObjectiveError.BADPARAMS);
1812 return;
1813 }
1814 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1815 groupService, deviceId,
1816 portNumber, label);
1817 if (existing.isEmpty()) {
1818 // if it doesn't exist, mark this bucket for creation
1819 bucketsToCreate.add(bkt);
1820 }
1821 if (existing.size() > 1) {
1822 // if it exists but there are duplicates, mark the others for removal
1823 existing.remove(0);
1824 indicesToRemove.addAll(existing);
1825 }
1826 }
1827
Pier Luigi4a7cdfa2018-01-29 10:30:59 +01001828 // Detect situation where the next data has more buckets
1829 // (not duplicates) respect to the next objective
Saurav Dasa89b95a2018-02-14 14:14:54 -08001830 if (allActiveKeys.size() > nextObjective.next().size() &&
1831 // ignore specific case of empty group
1832 !(nextObjective.next().size() == 0 && allActiveKeys.size() == 1
1833 && allActiveKeys.get(0).size() == 1)) {
Pier Luigi4a7cdfa2018-01-29 10:30:59 +01001834 log.warn("Mismatch detected between next and flowobjstore for device {}: " +
Saurav Dasa89b95a2018-02-14 14:14:54 -08001835 "nextId:{}, nextObjective-size:{} next-size:{} .. correcting",
1836 deviceId, nextObjective.id(), nextObjective.next().size(),
1837 allActiveKeys.size());
1838 List<Integer> otherIndices =
1839 indicesToRemoveFromNextGroup(allActiveKeys, nextObjective,
1840 groupService, deviceId);
Pier Luigi4a7cdfa2018-01-29 10:30:59 +01001841 // Filter out the indices not present
1842 otherIndices = otherIndices.stream()
1843 .filter(index -> !indicesToRemove.contains(index))
1844 .collect(Collectors.toList());
1845 // Add all to the final list
1846 indicesToRemove.addAll(otherIndices);
1847 }
1848
Charles Chan1a446ca2018-05-08 11:49:05 -07001849 log.trace("Buckets to create {}", bucketsToCreate);
1850 log.trace("Indices to remove {}", indicesToRemove);
Pier Luigi3dfd8352018-01-25 16:16:02 +01001851
Saurav Dasceccf242017-08-03 18:30:35 -07001852 if (!bucketsToCreate.isEmpty()) {
1853 log.info("creating {} buckets as part of nextId: {} verification",
1854 bucketsToCreate.size(), nextObjective.id());
1855 //create a nextObjective only with these buckets
1856 NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder()
1857 .withId(nextObjective.id())
Pier Luigi3dfd8352018-01-25 16:16:02 +01001858 .withType(nextObjective.type())
Saurav Dasceccf242017-08-03 18:30:35 -07001859 .withMeta(nextObjective.meta())
1860 .fromApp(nextObjective.appId());
Pier Luigi3dfd8352018-01-25 16:16:02 +01001861 bucketsToCreate.forEach(nextObjBuilder::addTreatment);
1862 // According to the next type we call the proper add function
1863 if (nextObjective.type() == NextObjective.Type.HASHED) {
1864 addBucketToHashGroup(nextObjBuilder.addToExisting(), allActiveKeys);
1865 } else {
1866 addBucketToBroadcastGroup(nextObjBuilder.addToExisting(), allActiveKeys);
1867 }
Saurav Dasceccf242017-08-03 18:30:35 -07001868 }
1869
1870 if (!indicesToRemove.isEmpty()) {
1871 log.info("removing {} buckets as part of nextId: {} verification",
1872 indicesToRemove.size(), nextObjective.id());
1873 List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList();
1874 indicesToRemove.forEach(index -> chainsToRemove
1875 .add(allActiveKeys.get(index)));
1876 removeBucket(chainsToRemove, nextObjective);
1877 }
1878
Saurav Das9df5b7c2017-08-14 16:44:43 -07001879 if (bucketsToCreate.isEmpty() && indicesToRemove.isEmpty()) {
1880 // flowObjective store record is in-sync with nextObjective passed-in
1881 // Nevertheless groupStore may not be in sync due to bug in the store
1882 // - see CORD-1844. XXX When this bug is fixed, the rest of this verify
1883 // method will not be required.
Pier Luigi3dfd8352018-01-25 16:16:02 +01001884 GroupKey topGroupKey = allActiveKeys.get(0).peekFirst();
1885 Group topGroup = groupService.getGroup(deviceId, topGroupKey);
Ravi Dewangan95d08902019-05-28 22:24:18 +00001886 // topGroup should not be null - adding guard
1887 if (topGroup == null) {
1888 log.warn("topGroup {} not found in GroupStore device:{}, nextId:{}",
1889 topGroupKey, deviceId, nextObjective.id());
pierb29eb232019-12-10 18:09:00 +01001890 fail(nextObjective, ObjectiveError.GROUPMISSING);
Ravi Dewangan95d08902019-05-28 22:24:18 +00001891 return;
1892 }
Pier Luigi3dfd8352018-01-25 16:16:02 +01001893 int actualGroupSize = topGroup.buckets().buckets().size();
Saurav Das9df5b7c2017-08-14 16:44:43 -07001894 int objGroupSize = nextObjective.next().size();
1895 if (actualGroupSize != objGroupSize) {
1896 log.warn("Mismatch detected in device:{}, nextId:{}, nextObjective-size"
1897 + ":{} group-size:{} .. correcting", deviceId, nextObjective.id(),
1898 objGroupSize, actualGroupSize);
1899 }
1900 if (actualGroupSize > objGroupSize) {
Pier Luigi3dfd8352018-01-25 16:16:02 +01001901 // Group in the device has more chains
Saurav Das9df5b7c2017-08-14 16:44:43 -07001902 List<GroupBucket> bucketsToRemove = Lists.newArrayList();
1903 //check every bucket in the actual group
Pier Luigi3dfd8352018-01-25 16:16:02 +01001904 for (GroupBucket bucket : topGroup.buckets().buckets()) {
Saurav Das9df5b7c2017-08-14 16:44:43 -07001905 GroupInstruction g = (GroupInstruction) bucket.treatment()
1906 .allInstructions().iterator().next();
1907 GroupId gidToCheck = g.groupId(); // the group pointed to
1908 boolean matches = false;
1909 for (Deque<GroupKey> validChain : allActiveKeys) {
1910 if (validChain.size() < 2) {
1911 continue;
1912 }
1913 GroupKey pointedGroupKey = validChain.stream()
1914 .collect(Collectors.toList()).get(1);
1915 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
1916 if (pointedGroup != null && gidToCheck.equals(pointedGroup.id())) {
1917 matches = true;
1918 break;
1919 }
1920 }
1921 if (!matches) {
1922 log.warn("Removing bucket pointing to groupId:{}", gidToCheck);
1923 bucketsToRemove.add(bucket);
1924 }
1925 }
1926 // remove buckets for which there was no record in the obj store
1927 if (bucketsToRemove.isEmpty()) {
1928 log.warn("Mismatch detected but could not determine which"
1929 + "buckets to remove");
1930 } else {
1931 GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove);
Pier Luigi3dfd8352018-01-25 16:16:02 +01001932 groupService.removeBucketsFromGroup(deviceId, topGroupKey,
1933 removeBuckets, topGroupKey,
Saurav Das9df5b7c2017-08-14 16:44:43 -07001934 nextObjective.appId());
1935 }
1936 } else if (actualGroupSize < objGroupSize) {
Pier Luigi3dfd8352018-01-25 16:16:02 +01001937 // Group in the device has less chains
Saurav Das9df5b7c2017-08-14 16:44:43 -07001938 // should also add buckets not in group-store but in obj-store
1939 List<GroupBucket> bucketsToAdd = Lists.newArrayList();
1940 //check every bucket in the obj
1941 for (Deque<GroupKey> validChain : allActiveKeys) {
1942 if (validChain.size() < 2) {
1943 continue;
1944 }
1945 GroupKey pointedGroupKey = validChain.stream()
1946 .collect(Collectors.toList()).get(1);
1947 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
1948 if (pointedGroup == null) {
1949 // group should exist, otherwise cannot be added as bucket
1950 continue;
1951 }
1952 boolean matches = false;
Pier Luigi3dfd8352018-01-25 16:16:02 +01001953 for (GroupBucket bucket : topGroup.buckets().buckets()) {
Saurav Das9df5b7c2017-08-14 16:44:43 -07001954 GroupInstruction g = (GroupInstruction) bucket.treatment()
1955 .allInstructions().iterator().next();
1956 GroupId gidToCheck = g.groupId(); // the group pointed to
1957 if (pointedGroup.id().equals(gidToCheck)) {
1958 matches = true;
1959 break;
1960 }
1961 }
1962 if (!matches) {
1963 log.warn("Adding bucket pointing to groupId:{}", pointedGroup);
1964 TrafficTreatment t = DefaultTrafficTreatment.builder()
1965 .group(pointedGroup.id())
1966 .build();
Pier Luigi3dfd8352018-01-25 16:16:02 +01001967 // Create the proper bucket according to the next type
1968 if (nextObjective.type() == NextObjective.Type.HASHED) {
1969 bucketsToAdd.add(DefaultGroupBucket.createSelectGroupBucket(t));
1970 } else {
1971 bucketsToAdd.add(DefaultGroupBucket.createAllGroupBucket(t));
1972 }
Saurav Das9df5b7c2017-08-14 16:44:43 -07001973 }
1974 }
1975 if (bucketsToAdd.isEmpty()) {
1976 log.warn("Mismatch detected but could not determine which "
1977 + "buckets to add");
1978 } else {
1979 GroupBuckets addBuckets = new GroupBuckets(bucketsToAdd);
Pier Luigi3dfd8352018-01-25 16:16:02 +01001980 groupService.addBucketsToGroup(deviceId, topGroupKey,
1981 addBuckets, topGroupKey,
Saurav Das9df5b7c2017-08-14 16:44:43 -07001982 nextObjective.appId());
1983 }
1984 }
1985 }
1986
Saurav Dasceccf242017-08-03 18:30:35 -07001987 pass(nextObjective);
1988 }
1989
1990 //////////////////////////////////////
1991 // Helper methods and classes
1992 //////////////////////////////////////
1993
Yi Tsengef19de12017-04-24 11:33:05 -07001994 protected void updatePendingNextObjective(GroupKey groupKey, OfdpaNextGroup nextGrp) {
1995 pendingAddNextObjectives.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07001996 if (val == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001997 val = new CopyOnWriteArrayList<>();
Saurav Das961beb22017-03-29 19:09:17 -07001998 }
Yi Tsengef19de12017-04-24 11:33:05 -07001999 val.add(nextGrp);
Saurav Das961beb22017-03-29 19:09:17 -07002000 return val;
2001 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08002002 }
2003
Yi Tsengef19de12017-04-24 11:33:05 -07002004 protected void updatePendingGroups(GroupKey groupKey, GroupChainElem gce) {
2005 pendingGroups.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07002006 if (val == null) {
2007 val = Sets.newConcurrentHashSet();
2008 }
2009 val.add(gce);
2010 return val;
2011 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08002012 }
2013
Yi Tsengef19de12017-04-24 11:33:05 -07002014 protected void addPendingUpdateNextObjective(GroupKey groupKey,
2015 NextObjective nextObjective) {
Yi Tseng78f51f42017-02-02 13:54:58 -08002016 pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> {
2017 if (nextObjs != null) {
2018 nextObjs.add(nextObjective);
2019 } else {
2020 nextObjs = Sets.newHashSet(nextObjective);
2021 }
2022 return nextObjs;
2023 });
2024 }
2025
Saurav Das2c57bc82018-04-07 16:51:09 -07002026 protected void addPendingRemoveNextObjective(NextObjective nextObjective,
2027 List<GroupKey> groupKeys) {
2028 pendingRemoveNextObjectives.put(nextObjective, groupKeys);
Yi Tsengef19de12017-04-24 11:33:05 -07002029 }
2030
Saurav Das2c57bc82018-04-07 16:51:09 -07002031 protected int getNextAvailableIndex() {
2032 return (int) nextIndex.incrementAndGet();
2033 }
2034
2035 protected void processPendingUpdateNextObjs(GroupKey groupKey) {
2036 Set<NextObjective> nextObjs = pendingUpdateNextObjectives.remove(groupKey);
2037 if (nextObjs != null) {
2038 nextObjs.forEach(nextObj -> {
2039 log.debug("Group {} updated, update pending next objective {}.",
2040 groupKey, nextObj);
2041 pass(nextObj);
2042 });
2043 }
2044 }
2045
2046 protected void processPendingRemoveNextObjs(GroupKey key) {
Yi Tsengef19de12017-04-24 11:33:05 -07002047 pendingRemoveNextObjectives.asMap().forEach((nextObjective, groupKeys) -> {
jaegonkimc92a6f72018-07-09 23:23:10 +09002048 groupKeys.remove(key);
Yi Tsengef19de12017-04-24 11:33:05 -07002049 if (groupKeys.isEmpty()) {
2050 pendingRemoveNextObjectives.invalidate(nextObjective);
2051 pass(nextObjective);
Yi Tsengef19de12017-04-24 11:33:05 -07002052 }
2053 });
2054 }
2055
Yi Tsengef19de12017-04-24 11:33:05 -07002056 protected void processPendingAddGroupsOrNextObjs(GroupKey key, boolean added) {
2057 //first check for group chain
2058 Set<OfdpaGroupHandlerUtility.GroupChainElem> gceSet = pendingGroups.asMap().remove(key);
2059 if (gceSet != null) {
2060 for (GroupChainElem gce : gceSet) {
2061 log.debug("Group service {} group key {} in device {}. "
2062 + "Processing next group in group chain with group id 0x{}",
2063 (added) ? "ADDED" : "processed",
2064 key, deviceId,
2065 Integer.toHexString(gce.groupDescription().givenGroupId()));
2066 processGroupChain(gce);
2067 }
2068 } else {
2069 // otherwise chain complete - check for waiting nextObjectives
2070 List<OfdpaGroupHandlerUtility.OfdpaNextGroup> nextGrpList =
2071 pendingAddNextObjectives.getIfPresent(key);
2072 if (nextGrpList != null) {
2073 pendingAddNextObjectives.invalidate(key);
2074 nextGrpList.forEach(nextGrp -> {
2075 log.debug("Group service {} group key {} in device:{}. "
2076 + "Done implementing next objective: {} <<-->> gid:0x{}",
2077 (added) ? "ADDED" : "processed",
2078 key, deviceId, nextGrp.nextObjective().id(),
2079 Integer.toHexString(groupService.getGroup(deviceId, key)
2080 .givenGroupId()));
2081 pass(nextGrp.nextObjective());
Saurav Dasc88d4662017-05-15 15:34:25 -07002082 updateFlowObjectiveStore(nextGrp.nextObjective().id(), nextGrp);
Yi Tsengef19de12017-04-24 11:33:05 -07002083
2084 // check if addBuckets waiting for this completion
2085 pendingBuckets.compute(nextGrp.nextObjective().id(), (nextId, pendBkts) -> {
2086 if (pendBkts != null) {
2087 pendBkts.forEach(pendBkt -> addBucketToGroup(pendBkt, nextGrp));
2088 }
2089 return null;
2090 });
2091 });
2092 }
2093 }
2094 }
2095
Charles Chan188ebf52015-12-23 00:15:11 -08002096 /**
2097 * Processes next element of a group chain. Assumption is that if this
2098 * group points to another group, the latter has already been created
2099 * and this driver has received notification for it. A second assumption is
2100 * that if there is another group waiting for this group then the appropriate
2101 * stores already have the information to act upon the notification for the
2102 * creation of this group.
2103 * <p>
2104 * The processing of the GroupChainElement depends on the number of groups
2105 * this element is waiting on. For all group types other than SIMPLE, a
2106 * GroupChainElement could be waiting on multiple groups.
2107 *
2108 * @param gce the group chain element to be processed next
2109 */
2110 private void processGroupChain(GroupChainElem gce) {
2111 int waitOnGroups = gce.decrementAndGetGroupsWaitedOn();
2112 if (waitOnGroups != 0) {
2113 log.debug("GCE: {} not ready to be processed", gce);
2114 return;
2115 }
2116 log.debug("GCE: {} ready to be processed", gce);
Yi Tsengef19de12017-04-24 11:33:05 -07002117 if (gce.addBucketToGroup()) {
2118 groupService.addBucketsToGroup(gce.groupDescription().deviceId(),
2119 gce.groupDescription().appCookie(),
2120 gce.groupDescription().buckets(),
2121 gce.groupDescription().appCookie(),
2122 gce.groupDescription().appId());
Charles Chan188ebf52015-12-23 00:15:11 -08002123 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07002124 groupService.addGroup(gce.groupDescription());
Charles Chan188ebf52015-12-23 00:15:11 -08002125 }
2126 }
2127
Saurav Dasc88d4662017-05-15 15:34:25 -07002128 private void updateFlowObjectiveStore(Integer nextId, OfdpaNextGroup nextGrp) {
2129 synchronized (flowObjectiveStore) {
2130 // get fresh copy of what the store holds
2131 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
2132 if (next == null || nextGrp.nextObjective().op() == Operation.ADD) {
2133 flowObjectiveStore.putNextGroup(nextId, nextGrp);
2134 return;
2135 }
2136 if (nextGrp.nextObjective().op() == Operation.ADD_TO_EXISTING) {
2137 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Pier Luigiaeb11a72018-01-15 09:48:49 +01002138 allActiveKeys = Lists.newArrayList(allActiveKeys);
Saurav Dasc88d4662017-05-15 15:34:25 -07002139 // If active keys shows only the top-level group without a chain of groups,
2140 // then it represents an empty group. Update by replacing empty chain.
2141 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
2142 allActiveKeys.clear();
2143 }
2144 allActiveKeys.addAll(nextGrp.allKeys());
2145 flowObjectiveStore.putNextGroup(nextId,
2146 new OfdpaNextGroup(allActiveKeys, nextGrp.nextObjective()));
2147 }
2148 }
2149 }
2150
Saurav Das8be4e3a2016-03-11 17:19:07 -08002151 private class InnerGroupListener implements GroupListener {
2152 @Override
2153 public void event(GroupEvent event) {
Charles Chanfc5c7802016-05-17 13:13:55 -07002154 switch (event.type()) {
2155 case GROUP_ADDED:
2156 processPendingAddGroupsOrNextObjs(event.subject().appCookie(), true);
2157 break;
2158 case GROUP_REMOVED:
2159 processPendingRemoveNextObjs(event.subject().appCookie());
2160 break;
Yi Tseng78f51f42017-02-02 13:54:58 -08002161 case GROUP_UPDATED:
2162 processPendingUpdateNextObjs(event.subject().appCookie());
2163 break;
Charles Chanfc5c7802016-05-17 13:13:55 -07002164 default:
2165 break;
Saurav Das8be4e3a2016-03-11 17:19:07 -08002166 }
2167 }
2168 }
Charles Chan188ebf52015-12-23 00:15:11 -08002169}