blob: a9436cce6c43af658f69b4f419a7318a54c0fa82 [file] [log] [blame]
Brian O'Connor7cbbbb72016-04-09 02:13:23 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Brian O'Connor7cbbbb72016-04-09 02:13:23 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Yi Tsengef19de12017-04-24 11:33:05 -070016package org.onosproject.driver.pipeline.ofdpa;
Charles Chan188ebf52015-12-23 00:15:11 -080017
18import com.google.common.cache.Cache;
19import com.google.common.cache.CacheBuilder;
20import com.google.common.cache.RemovalCause;
21import com.google.common.cache.RemovalNotification;
Charles Chan5b9df8d2016-03-28 22:21:40 -070022import com.google.common.collect.ImmutableList;
23import com.google.common.collect.Lists;
Yi Tseng78f51f42017-02-02 13:54:58 -080024import com.google.common.collect.Sets;
Charles Chan188ebf52015-12-23 00:15:11 -080025import org.onlab.osgi.ServiceDirectory;
Charles Chan5b9df8d2016-03-28 22:21:40 -070026import org.onlab.packet.IpPrefix;
Charles Chan5270ed02016-01-30 23:22:37 -080027import org.onlab.packet.MacAddress;
Charles Chan188ebf52015-12-23 00:15:11 -080028import org.onlab.packet.MplsLabel;
29import org.onlab.packet.VlanId;
30import org.onosproject.core.ApplicationId;
Yi Tseng78f51f42017-02-02 13:54:58 -080031import org.onosproject.core.GroupId;
Charles Chan32562522016-04-07 14:37:14 -070032import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Charles Chan188ebf52015-12-23 00:15:11 -080033import org.onosproject.net.DeviceId;
34import org.onosproject.net.PortNumber;
35import org.onosproject.net.behaviour.NextGroup;
36import org.onosproject.net.behaviour.PipelinerContext;
37import org.onosproject.net.flow.DefaultTrafficTreatment;
38import org.onosproject.net.flow.TrafficSelector;
39import org.onosproject.net.flow.TrafficTreatment;
40import org.onosproject.net.flow.criteria.Criterion;
Pier Ventre42287df2016-11-09 14:17:26 -080041import org.onosproject.net.flow.criteria.TunnelIdCriterion;
Charles Chan188ebf52015-12-23 00:15:11 -080042import org.onosproject.net.flow.criteria.VlanIdCriterion;
43import org.onosproject.net.flow.instructions.Instruction;
44import org.onosproject.net.flow.instructions.Instructions;
Saurav Das9df5b7c2017-08-14 16:44:43 -070045import org.onosproject.net.flow.instructions.Instructions.GroupInstruction;
Charles Chan188ebf52015-12-23 00:15:11 -080046import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Yi Tseng47f82dc2017-03-05 22:48:39 -080047import org.onosproject.net.flowobjective.DefaultNextObjective;
Charles Chan188ebf52015-12-23 00:15:11 -080048import org.onosproject.net.flowobjective.FlowObjectiveStore;
49import org.onosproject.net.flowobjective.NextObjective;
Saurav Dasc88d4662017-05-15 15:34:25 -070050import org.onosproject.net.flowobjective.Objective.Operation;
Yi Tseng47f82dc2017-03-05 22:48:39 -080051import org.onosproject.net.flowobjective.ObjectiveContext;
Charles Chan188ebf52015-12-23 00:15:11 -080052import org.onosproject.net.flowobjective.ObjectiveError;
53import org.onosproject.net.group.DefaultGroupBucket;
54import org.onosproject.net.group.DefaultGroupDescription;
55import org.onosproject.net.group.DefaultGroupKey;
56import org.onosproject.net.group.Group;
57import org.onosproject.net.group.GroupBucket;
58import org.onosproject.net.group.GroupBuckets;
59import org.onosproject.net.group.GroupDescription;
60import org.onosproject.net.group.GroupEvent;
61import org.onosproject.net.group.GroupKey;
62import org.onosproject.net.group.GroupListener;
63import org.onosproject.net.group.GroupService;
Saurav Das8be4e3a2016-03-11 17:19:07 -080064import org.onosproject.store.service.AtomicCounter;
65import org.onosproject.store.service.StorageService;
Charles Chan188ebf52015-12-23 00:15:11 -080066import org.slf4j.Logger;
67
68import java.util.ArrayDeque;
69import java.util.ArrayList;
Saurav Dasceccf242017-08-03 18:30:35 -070070import java.util.Arrays;
Charles Chan188ebf52015-12-23 00:15:11 -080071import java.util.Collection;
72import java.util.Collections;
73import java.util.Deque;
74import java.util.List;
Charles Chand0fd5dc2016-02-16 23:14:49 -080075import java.util.Objects;
Charles Chan188ebf52015-12-23 00:15:11 -080076import java.util.Set;
77import java.util.concurrent.ConcurrentHashMap;
78import java.util.concurrent.CopyOnWriteArrayList;
79import java.util.concurrent.Executors;
80import java.util.concurrent.ScheduledExecutorService;
81import java.util.concurrent.TimeUnit;
Charles Chan188ebf52015-12-23 00:15:11 -080082import java.util.stream.Collectors;
83
84import static org.onlab.util.Tools.groupedThreads;
Yi Tsengef19de12017-04-24 11:33:05 -070085import static org.onosproject.driver.pipeline.ofdpa.Ofdpa2Pipeline.*;
86import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
jayakumarthazhath46945b62018-10-01 00:51:54 +053087import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.L2_MULTICAST_TYPE;
88import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.l2MulticastGroupKey;
Pier Ventre42287df2016-11-09 14:17:26 -080089import static org.onosproject.net.flow.criteria.Criterion.Type.TUNNEL_ID;
Pier Ventre140a8942016-11-02 07:26:38 -070090import static org.onosproject.net.flow.criteria.Criterion.Type.VLAN_VID;
Yi Tseng78f51f42017-02-02 13:54:58 -080091import static org.onosproject.net.group.GroupDescription.Type.ALL;
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -080092import static org.onosproject.net.group.GroupDescription.Type.INDIRECT;
Yi Tseng78f51f42017-02-02 13:54:58 -080093import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Charles Chan188ebf52015-12-23 00:15:11 -080094import static org.slf4j.LoggerFactory.getLogger;
95
96/**
Saurav Das961beb22017-03-29 19:09:17 -070097 * Group handler that emulates Broadcom OF-DPA TTP.
Charles Chan188ebf52015-12-23 00:15:11 -080098 */
Charles Chan361154b2016-03-24 10:23:39 -070099public class Ofdpa2GroupHandler {
Yi Tsengef19de12017-04-24 11:33:05 -0700100 protected final Logger log = getLogger(getClass());
Yi Tsengef19de12017-04-24 11:33:05 -0700101 // Services, Stores
Charles Chan188ebf52015-12-23 00:15:11 -0800102 protected GroupService groupService;
Saurav Das8be4e3a2016-03-11 17:19:07 -0800103 protected StorageService storageService;
Yi Tsengef19de12017-04-24 11:33:05 -0700104 protected FlowObjectiveStore flowObjectiveStore;
Charles Chan188ebf52015-12-23 00:15:11 -0800105 // index number for group creation
Saurav Das8be4e3a2016-03-11 17:19:07 -0800106 private AtomicCounter nextIndex;
Yi Tsengef19de12017-04-24 11:33:05 -0700107 protected DeviceId deviceId;
Saurav Das2f2c9d02018-04-07 16:51:09 -0700108 Cache<GroupKey, List<OfdpaGroupHandlerUtility.OfdpaNextGroup>> pendingAddNextObjectives;
109 Cache<NextObjective, List<GroupKey>> pendingRemoveNextObjectives;
110 Cache<GroupKey, Set<OfdpaGroupHandlerUtility.GroupChainElem>> pendingGroups;
111 ConcurrentHashMap<GroupKey, Set<NextObjective>> pendingUpdateNextObjectives;
Yi Tseng47f82dc2017-03-05 22:48:39 -0800112 // local store for pending bucketAdds - by design there can be multiple
Charles Chan188ebf52015-12-23 00:15:11 -0800113 // pending bucket for a group
Yi Tseng47f82dc2017-03-05 22:48:39 -0800114 protected ConcurrentHashMap<Integer, Set<NextObjective>> pendingBuckets =
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700115 new ConcurrentHashMap<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700116 private ScheduledExecutorService groupCheckerExecutor =
117 Executors.newScheduledThreadPool(2, groupedThreads("onos/pipeliner", "ofdpa-%d", log));
Charles Chan40132b32017-01-22 00:19:37 -0800118 /**
119 * Determines whether this pipeline support copy ttl instructions or not.
120 *
121 * @return true if copy ttl instructions are supported
122 */
123 protected boolean supportCopyTtl() {
124 return true;
125 }
126
127 /**
128 * Determines whether this pipeline support set mpls bos instruction or not.
129 *
130 * @return true if set mpls bos instruction is supported
131 */
132 protected boolean supportSetMplsBos() {
133 return true;
134 }
135
Charles Chan0f43e472017-02-14 14:00:16 -0800136 /**
137 * Determines whether this pipeline requires popping VLAN before pushing MPLS.
138 * <p>
139 * If required, pop vlan before push mpls and add an arbitrary vlan back afterward.
140 * MPLS interface group will substitute the arbitrary VLAN with expected VLAN later on.
141 *
142 * @return true if this pipeline requires popping VLAN before pushing MPLS
143 */
144 protected boolean requireVlanPopBeforeMplsPush() {
145 return false;
146 }
147
Alex Yashchuk4caa8e82017-12-08 17:40:05 +0200148 public void init(DeviceId deviceId, PipelinerContext context) {
Yi Tsengef19de12017-04-24 11:33:05 -0700149 ServiceDirectory serviceDirectory = context.directory();
Charles Chan188ebf52015-12-23 00:15:11 -0800150 this.deviceId = deviceId;
151 this.flowObjectiveStore = context.store();
Charles Chan188ebf52015-12-23 00:15:11 -0800152 this.groupService = serviceDirectory.get(GroupService.class);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800153 this.storageService = serviceDirectory.get(StorageService.class);
Madan Jampanid5714e02016-04-19 14:15:20 -0700154 this.nextIndex = storageService.getAtomicCounter("group-id-index-counter");
Charles Chan188ebf52015-12-23 00:15:11 -0800155
Charles Chanfc5c7802016-05-17 13:13:55 -0700156 pendingAddNextObjectives = CacheBuilder.newBuilder()
Charles Chan188ebf52015-12-23 00:15:11 -0800157 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700158 .removalListener((RemovalNotification<GroupKey, List<OfdpaNextGroup>> notification) -> {
159 if (notification.getCause() == RemovalCause.EXPIRED &&
160 Objects.nonNull(notification.getValue())) {
161 notification.getValue()
162 .forEach(ofdpaNextGrp ->
163 fail(ofdpaNextGrp.nextObjective(),
164 ObjectiveError.GROUPINSTALLATIONFAILED));
Charles Chanfc5c7802016-05-17 13:13:55 -0700165 }
166 }).build();
Charles Chan188ebf52015-12-23 00:15:11 -0800167
Charles Chanfc5c7802016-05-17 13:13:55 -0700168 pendingRemoveNextObjectives = CacheBuilder.newBuilder()
169 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700170 .removalListener((RemovalNotification<NextObjective, List<GroupKey>> notification) -> {
Charles Chanfc5c7802016-05-17 13:13:55 -0700171 if (notification.getCause() == RemovalCause.EXPIRED) {
Yi Tsengef19de12017-04-24 11:33:05 -0700172 fail(notification.getKey(),
173 ObjectiveError.GROUPREMOVALFAILED);
Charles Chan188ebf52015-12-23 00:15:11 -0800174 }
175 }).build();
Saurav Das961beb22017-03-29 19:09:17 -0700176 pendingGroups = CacheBuilder.newBuilder()
177 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700178 .removalListener((RemovalNotification<GroupKey, Set<GroupChainElem>> notification) -> {
Saurav Das961beb22017-03-29 19:09:17 -0700179 if (notification.getCause() == RemovalCause.EXPIRED) {
180 log.error("Unable to install group with key {} and pending GCEs: {}",
181 notification.getKey(), notification.getValue());
182 }
183 }).build();
Yi Tseng78f51f42017-02-02 13:54:58 -0800184 pendingUpdateNextObjectives = new ConcurrentHashMap<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700185 GroupChecker groupChecker = new GroupChecker(this);
186 groupCheckerExecutor.scheduleAtFixedRate(groupChecker, 0, 500, TimeUnit.MILLISECONDS);
Charles Chan188ebf52015-12-23 00:15:11 -0800187 groupService.addListener(new InnerGroupListener());
188 }
189
Saurav Das8be4e3a2016-03-11 17:19:07 -0800190 //////////////////////////////////////
191 // Group Creation
192 //////////////////////////////////////
193
Yi Tsengef19de12017-04-24 11:33:05 -0700194 /**
195 * Adds a list of group chain by given NextObjective.
196 *
197 * @param nextObjective the NextObjective
198 */
Charles Chan188ebf52015-12-23 00:15:11 -0800199 protected void addGroup(NextObjective nextObjective) {
200 switch (nextObjective.type()) {
201 case SIMPLE:
202 Collection<TrafficTreatment> treatments = nextObjective.next();
203 if (treatments.size() != 1) {
204 log.error("Next Objectives of type Simple should only have a "
205 + "single Traffic Treatment. Next Objective Id:{}",
206 nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -0700207 fail(nextObjective, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800208 return;
209 }
210 processSimpleNextObjective(nextObjective);
211 break;
212 case BROADCAST:
213 processBroadcastNextObjective(nextObjective);
214 break;
215 case HASHED:
Pier Ventre140a8942016-11-02 07:26:38 -0700216 if (!verifyHashedNextObjective(nextObjective)) {
217 log.error("Next Objectives of type hashed not supported. Next Objective Id:{}",
218 nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -0700219 fail(nextObjective, ObjectiveError.BADPARAMS);
Pier Ventre140a8942016-11-02 07:26:38 -0700220 return;
221 }
Charles Chan188ebf52015-12-23 00:15:11 -0800222 processHashedNextObjective(nextObjective);
223 break;
224 case FAILOVER:
Yi Tsengef19de12017-04-24 11:33:05 -0700225 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -0800226 log.warn("Unsupported next objective type {}", nextObjective.type());
227 break;
228 default:
Yi Tsengef19de12017-04-24 11:33:05 -0700229 fail(nextObjective, ObjectiveError.UNKNOWN);
Charles Chan188ebf52015-12-23 00:15:11 -0800230 log.warn("Unknown next objective type {}", nextObjective.type());
231 }
232 }
233
234 /**
jayakumarthazhath46945b62018-10-01 00:51:54 +0530235 * Similar to processBroadcastNextObjective but handles L2 Multicast Next Objectives.
236 *
237 * @param nextObj NextObjective of L2_MULTICAST with chained NextObjectives for single homed access ports
238 */
239 private void processL2MulticastNextObjective(NextObjective nextObj) {
240
241 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
242 if (assignedVlan == null) {
243 log.warn("VLAN ID required by L2 multicast next objective is missing. Aborting group creation.");
244 fail(nextObj, ObjectiveError.BADPARAMS);
245 return;
246 }
247
248 // Group info should contain only single homed hosts for a given vlanId
249 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
250 createL2MulticastGroup(nextObj, assignedVlan, groupInfos);
251 }
252
253 private void createL2MulticastGroup(NextObjective nextObj, VlanId vlanId, List<GroupInfo> groupInfos) {
254 // Realize & represent L2 multicast group in OFDPA driver layer
255 // TODO : Need to identify significance of OfdpaNextGroup.
256 Integer l2MulticastGroupId = L2_MULTICAST_TYPE | (vlanId.toShort() << 16);
257 final GroupKey l2MulticastGroupKey = l2MulticastGroupKey(vlanId, deviceId);
258 List<Deque<GroupKey>> l2MulticastAllGroup = Lists.newArrayList();
259 groupInfos.forEach(groupInfo -> {
260 Deque<GroupKey> groupKeyChain = new ArrayDeque<>();
261 groupKeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
262 groupKeyChain.addFirst(l2MulticastGroupKey);
263 l2MulticastAllGroup.add(groupKeyChain);
264 });
265 OfdpaNextGroup ofdpaL2MulticastGroup = new OfdpaNextGroup(l2MulticastAllGroup, nextObj);
266 updatePendingNextObjective(l2MulticastGroupKey, ofdpaL2MulticastGroup);
267 // Group Chain Hierarchy creation using group service and thus in device level
268 List<GroupBucket> l2McastBuckets = new ArrayList<>();
269 groupInfos.forEach(groupInfo -> {
270 // Points to L2 interface group directly.
271 TrafficTreatment.Builder trafficTreatment = DefaultTrafficTreatment.builder();
272 trafficTreatment.group(new GroupId(groupInfo.innerMostGroupDesc().givenGroupId()));
273 GroupBucket bucket = DefaultGroupBucket.createAllGroupBucket(trafficTreatment.build());
274 l2McastBuckets.add(bucket);
275 });
276 GroupDescription l2MulticastGroupDescription =
277 new DefaultGroupDescription(
278 deviceId,
279 ALL,
280 new GroupBuckets(l2McastBuckets),
281 l2MulticastGroupKey,
282 l2MulticastGroupId,
283 nextObj.appId());
284 GroupChainElem l2MulticastGce = new GroupChainElem(l2MulticastGroupDescription,
285 groupInfos.size(), false, deviceId);
286 groupInfos.forEach(groupInfo -> {
287 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), l2MulticastGce);
288 groupService.addGroup(groupInfo.innerMostGroupDesc());
289 });
290 }
291
292 /**
Charles Chan188ebf52015-12-23 00:15:11 -0800293 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
Saurav Dasa4020382018-02-14 14:14:54 -0800294 * a chain of groups. The simple Next Objective passed in by the application
295 * is broken up into a group chain. The following chains can be created
296 * depending on the parameters in the Next Objective.
297 * 1. L2 Interface group (no chaining)
298 * 2. L3 Unicast group -> L2 Interface group
299 * 3. MPLS Interface group -> L2 Interface group
300 * 4. MPLS Swap group -> MPLS Interface group -> L2 Interface group
301 * 5. PW initiation group chain
Charles Chan188ebf52015-12-23 00:15:11 -0800302 *
303 * @param nextObj the nextObjective of type SIMPLE
304 */
305 private void processSimpleNextObjective(NextObjective nextObj) {
306 TrafficTreatment treatment = nextObj.next().iterator().next();
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800307 // determine if plain L2 or L3->L2 or MPLS Swap -> MPLS Interface -> L2
Charles Chan188ebf52015-12-23 00:15:11 -0800308 boolean plainL2 = true;
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800309 boolean mplsSwap = false;
310 MplsLabel mplsLabel = null;
Charles Chan188ebf52015-12-23 00:15:11 -0800311 for (Instruction ins : treatment.allInstructions()) {
312 if (ins.type() == Instruction.Type.L2MODIFICATION) {
313 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
314 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_DST ||
315 l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_SRC) {
316 plainL2 = false;
Charles Chan188ebf52015-12-23 00:15:11 -0800317 }
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800318 // mpls label in simple next objectives is used only to indicate
319 // a MPLS Swap group before the MPLS Interface Group
320 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) {
321 mplsSwap = true;
322 mplsLabel = ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label();
323 }
Charles Chan188ebf52015-12-23 00:15:11 -0800324 }
325 }
Charles Chan188ebf52015-12-23 00:15:11 -0800326 if (plainL2) {
327 createL2InterfaceGroup(nextObj);
328 return;
329 }
Saurav Dasa4020382018-02-14 14:14:54 -0800330 // In order to understand if it is a pseudowire related
Pier Ventre42287df2016-11-09 14:17:26 -0800331 // next objective we look for the tunnel id in the meta.
332 boolean isPw = false;
Pier Ventre140a8942016-11-02 07:26:38 -0700333 if (nextObj.meta() != null) {
Pier Ventre42287df2016-11-09 14:17:26 -0800334 TunnelIdCriterion tunnelIdCriterion = (TunnelIdCriterion) nextObj
335 .meta()
336 .getCriterion(TUNNEL_ID);
337 if (tunnelIdCriterion != null) {
338 isPw = true;
339 }
Pier Ventre140a8942016-11-02 07:26:38 -0700340 }
Andreas Pantelopoulos25db42f2018-02-08 12:42:06 -0800341 if (mplsSwap && !isPw) {
Saurav Dasa4020382018-02-14 14:14:54 -0800342 log.debug("Creating a MPLS Swap -> MPLS Interface -> L2 Interface group chain.");
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800343 // break up simple next objective to GroupChain objects
344 GroupInfo groupInfo = createL2L3Chain(treatment, nextObj.id(),
345 nextObj.appId(), true,
346 nextObj.meta());
347 if (groupInfo == null) {
348 log.error("Could not process nextObj={} in dev:{}", nextObj.id(), deviceId);
349 fail(nextObj, ObjectiveError.BADPARAMS);
350 return;
351 }
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800352 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Saurav Dasa4020382018-02-14 14:14:54 -0800353 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie()); // l2 interface
354 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie()); // mpls interface
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800355 // creating the mpls swap group and adding it to the chain
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800356 int nextGid = groupInfo.nextGroupDesc().givenGroupId();
357 int index = getNextAvailableIndex();
Saurav Dasa4020382018-02-14 14:14:54 -0800358 GroupDescription swapGroupDescription = createMplsSwap(
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800359 nextGid,
360 OfdpaMplsGroupSubType.MPLS_SWAP_LABEL,
361 index,
362 mplsLabel,
363 nextObj.appId()
364 );
Saurav Dasa4020382018-02-14 14:14:54 -0800365 // ensure swap group is added after L2L3 chain
366 GroupKey swapGroupKey = swapGroupDescription.appCookie();
367 GroupChainElem swapChainElem = new GroupChainElem(swapGroupDescription,
368 1, false, deviceId);
369 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), swapChainElem);
370 gkeyChain.addFirst(swapGroupKey);
Saurav Dasa4020382018-02-14 14:14:54 -0800371 // ensure nextObjective waits on the outermost groupKey
372 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
373 allGroupKeys.add(gkeyChain);
374 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
375 updatePendingNextObjective(swapGroupKey, ofdpaGrp);
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800376 // now we are ready to send the l2 groupDescription (inner), as all the stores
377 // that will get async replies have been updated. By waiting to update
378 // the stores, we prevent nasty race conditions.
379 groupService.addGroup(groupInfo.innerMostGroupDesc());
380 } else if (!isPw) {
Saurav Dasa4020382018-02-14 14:14:54 -0800381 boolean isMpls = false;
382 if (nextObj.meta() != null) {
383 isMpls = isNotMplsBos(nextObj.meta());
384 }
385 log.debug("Creating a {} -> L2 Interface group chain.",
386 (isMpls) ? "MPLS Interface" : "L3 Unicast");
Pier Ventre42287df2016-11-09 14:17:26 -0800387 // break up simple next objective to GroupChain objects
388 GroupInfo groupInfo = createL2L3Chain(treatment, nextObj.id(),
389 nextObj.appId(), isMpls,
390 nextObj.meta());
391 if (groupInfo == null) {
392 log.error("Could not process nextObj={} in dev:{}", nextObj.id(), deviceId);
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800393 fail(nextObj, ObjectiveError.BADPARAMS);
Pier Ventre42287df2016-11-09 14:17:26 -0800394 return;
395 }
396 // create object for local and distributed storage
397 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700398 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
399 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Saurav Dasa4020382018-02-14 14:14:54 -0800400 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
401 allGroupKeys.add(gkeyChain);
402 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Pier Ventre42287df2016-11-09 14:17:26 -0800403 // store l3groupkey with the ofdpaNextGroup for the nextObjective that depends on it
Yi Tsengef19de12017-04-24 11:33:05 -0700404 updatePendingNextObjective(groupInfo.nextGroupDesc().appCookie(), ofdpaGrp);
Pier Ventre42287df2016-11-09 14:17:26 -0800405 // now we are ready to send the l2 groupDescription (inner), as all the stores
406 // that will get async replies have been updated. By waiting to update
407 // the stores, we prevent nasty race conditions.
Yi Tsengef19de12017-04-24 11:33:05 -0700408 groupService.addGroup(groupInfo.innerMostGroupDesc());
Pier Ventre42287df2016-11-09 14:17:26 -0800409 } else {
410 // We handle the pseudo wire with a different a procedure.
411 // This procedure is meant to handle both initiation and
412 // termination of the pseudo wire.
413 processPwNextObjective(nextObj);
Charles Chan188ebf52015-12-23 00:15:11 -0800414 }
Charles Chan188ebf52015-12-23 00:15:11 -0800415 }
416
Charles Chan188ebf52015-12-23 00:15:11 -0800417 /**
418 * Creates a simple L2 Interface Group.
Charles Chan188ebf52015-12-23 00:15:11 -0800419 * @param nextObj the next Objective
420 */
421 private void createL2InterfaceGroup(NextObjective nextObj) {
Yi Tsengef19de12017-04-24 11:33:05 -0700422 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700423 if (assignedVlan == null) {
424 log.warn("VLAN ID required by simple next obj is missing. Abort.");
Yi Tsengef19de12017-04-24 11:33:05 -0700425 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800426 return;
427 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700428 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700429 // There is only one L2 interface group in this case
Yi Tsengef19de12017-04-24 11:33:05 -0700430 GroupDescription l2InterfaceGroupDesc = groupInfos.get(0).innerMostGroupDesc();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700431 // Put all dependency information into allGroupKeys
432 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
433 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
434 gkeyChain.addFirst(l2InterfaceGroupDesc.appCookie());
435 allGroupKeys.add(gkeyChain);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700436 // Point the next objective to this group
437 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
438 updatePendingNextObjective(l2InterfaceGroupDesc.appCookie(), ofdpaGrp);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700439 // Start installing the inner-most group
jayakumarthazhath46945b62018-10-01 00:51:54 +0530440 groupService.addGroup(l2InterfaceGroupDesc); }
Charles Chan188ebf52015-12-23 00:15:11 -0800441
442 /**
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800443 * Creates an Mpls group of type swap.
444 *
445 * @param nextGroupId the next group in the chain
446 * @param subtype the mpls swap label group subtype
447 * @param index the index of the group
448 * @param mplsLabel the mpls label to swap
449 * @param applicationId the application id
450 * @return the group description
451 */
452 protected GroupDescription createMplsSwap(int nextGroupId,
453 OfdpaMplsGroupSubType subtype,
454 int index,
455 MplsLabel mplsLabel,
456 ApplicationId applicationId) {
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800457 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800458 treatment.setMpls(mplsLabel);
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800459 // We point the group to the next group.
460 treatment.group(new GroupId(nextGroupId));
461 GroupBucket groupBucket = DefaultGroupBucket
462 .createIndirectGroupBucket(treatment.build());
463 // Finally we build the group description.
464 int groupId = makeMplsLabelGroupId(subtype, index);
465 GroupKey groupKey = new DefaultGroupKey(
Saurav Dasa4020382018-02-14 14:14:54 -0800466 Ofdpa2Pipeline.appKryo.serialize(index));
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800467 return new DefaultGroupDescription(
468 deviceId,
469 INDIRECT,
470 new GroupBuckets(Collections.singletonList(groupBucket)),
471 groupKey,
472 groupId,
Saurav Dasa4020382018-02-14 14:14:54 -0800473 applicationId);
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800474 }
475
476 /**
Charles Chan188ebf52015-12-23 00:15:11 -0800477 * Creates one of two possible group-chains from the treatment
478 * passed in. Depending on the MPLS boolean, this method either creates
Charles Chan425854b2016-04-11 15:32:12 -0700479 * an L3Unicast Group --&gt; L2Interface Group, if mpls is false;
480 * or MPLSInterface Group --&gt; L2Interface Group, if mpls is true;
Charles Chan188ebf52015-12-23 00:15:11 -0800481 * The returned 'inner' group description is always the L2 Interface group.
482 *
483 * @param treatment that needs to be broken up to create the group chain
484 * @param nextId of the next objective that needs this group chain
485 * @param appId of the application that sent this next objective
486 * @param mpls determines if L3Unicast or MPLSInterface group is created
487 * @param meta metadata passed in by the application as part of the nextObjective
488 * @return GroupInfo containing the GroupDescription of the
489 * L2Interface group(inner) and the GroupDescription of the (outer)
490 * L3Unicast/MPLSInterface group. May return null if there is an
491 * error in processing the chain
492 */
Charles Chan425854b2016-04-11 15:32:12 -0700493 protected GroupInfo createL2L3Chain(TrafficTreatment treatment, int nextId,
Charles Chanf9e98652016-09-07 16:54:23 -0700494 ApplicationId appId, boolean mpls,
495 TrafficSelector meta) {
496 return createL2L3ChainInternal(treatment, nextId, appId, mpls, meta, true);
497 }
498
499 /**
500 * Internal implementation of createL2L3Chain.
501 * <p>
502 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
503 * Since it is non-OF spec, we need an extension treatment for that.
504 * The useSetVlanExtension must be set to false for OFDPA i12.
505 * </p>
506 *
507 * @param treatment that needs to be broken up to create the group chain
508 * @param nextId of the next objective that needs this group chain
509 * @param appId of the application that sent this next objective
510 * @param mpls determines if L3Unicast or MPLSInterface group is created
511 * @param meta metadata passed in by the application as part of the nextObjective
512 * @param useSetVlanExtension use the setVlanVid extension that has is_present bit set to 0.
513 * @return GroupInfo containing the GroupDescription of the
514 * L2Interface group(inner) and the GroupDescription of the (outer)
515 * L3Unicast/MPLSInterface group. May return null if there is an
516 * error in processing the chain
517 */
518 protected GroupInfo createL2L3ChainInternal(TrafficTreatment treatment, int nextId,
Pier Ventre42287df2016-11-09 14:17:26 -0800519 ApplicationId appId, boolean mpls,
520 TrafficSelector meta, boolean useSetVlanExtension) {
Charles Chan188ebf52015-12-23 00:15:11 -0800521 // for the l2interface group, get vlan and port info
522 // for the outer group, get the src/dst mac, and vlan info
523 TrafficTreatment.Builder outerTtb = DefaultTrafficTreatment.builder();
524 TrafficTreatment.Builder innerTtb = DefaultTrafficTreatment.builder();
525 VlanId vlanid = null;
526 long portNum = 0;
527 boolean setVlan = false, popVlan = false;
Yi Tseng78f51f42017-02-02 13:54:58 -0800528 MacAddress srcMac;
529 MacAddress dstMac;
Charles Chan188ebf52015-12-23 00:15:11 -0800530 for (Instruction ins : treatment.allInstructions()) {
531 if (ins.type() == Instruction.Type.L2MODIFICATION) {
532 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
533 switch (l2ins.subtype()) {
534 case ETH_DST:
Charles Chan5270ed02016-01-30 23:22:37 -0800535 dstMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
536 outerTtb.setEthDst(dstMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800537 break;
538 case ETH_SRC:
Charles Chand0fd5dc2016-02-16 23:14:49 -0800539 srcMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
540 outerTtb.setEthSrc(srcMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800541 break;
542 case VLAN_ID:
543 vlanid = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
Charles Chanf9e98652016-09-07 16:54:23 -0700544 if (useSetVlanExtension) {
545 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
546 outerTtb.extension(ofdpaSetVlanVid, deviceId);
547 } else {
548 outerTtb.setVlanId(vlanid);
549 }
Charles Chan188ebf52015-12-23 00:15:11 -0800550 setVlan = true;
551 break;
552 case VLAN_POP:
553 innerTtb.popVlan();
554 popVlan = true;
555 break;
556 case DEC_MPLS_TTL:
557 case MPLS_LABEL:
558 case MPLS_POP:
559 case MPLS_PUSH:
560 case VLAN_PCP:
561 case VLAN_PUSH:
562 default:
563 break;
564 }
565 } else if (ins.type() == Instruction.Type.OUTPUT) {
566 portNum = ((Instructions.OutputInstruction) ins).port().toLong();
567 innerTtb.add(ins);
568 } else {
Saurav Das7bcbe702017-06-13 15:35:54 -0700569 log.debug("Driver does not handle this type of TrafficTreatment"
Saurav Dasa4020382018-02-14 14:14:54 -0800570 + " instruction in l2l3chain: {} - {}", ins.type(),
571 ins);
Charles Chan188ebf52015-12-23 00:15:11 -0800572 }
573 }
Charles Chan188ebf52015-12-23 00:15:11 -0800574 if (vlanid == null && meta != null) {
575 // use metadata if available
Pier Ventre140a8942016-11-02 07:26:38 -0700576 Criterion vidCriterion = meta.getCriterion(VLAN_VID);
Charles Chan188ebf52015-12-23 00:15:11 -0800577 if (vidCriterion != null) {
578 vlanid = ((VlanIdCriterion) vidCriterion).vlanId();
579 }
580 // if vlan is not set, use the vlan in metadata for outerTtb
581 if (vlanid != null && !setVlan) {
Charles Chanf9e98652016-09-07 16:54:23 -0700582 if (useSetVlanExtension) {
583 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
584 outerTtb.extension(ofdpaSetVlanVid, deviceId);
585 } else {
586 outerTtb.setVlanId(vlanid);
587 }
Charles Chan188ebf52015-12-23 00:15:11 -0800588 }
589 }
Charles Chan188ebf52015-12-23 00:15:11 -0800590 if (vlanid == null) {
591 log.error("Driver cannot process an L2/L3 group chain without "
592 + "egress vlan information for dev: {} port:{}",
593 deviceId, portNum);
594 return null;
595 }
Charles Chan188ebf52015-12-23 00:15:11 -0800596 if (!setVlan && !popVlan) {
597 // untagged outgoing port
598 TrafficTreatment.Builder temp = DefaultTrafficTreatment.builder();
599 temp.popVlan();
Yi Tsengef19de12017-04-24 11:33:05 -0700600 innerTtb.build().allInstructions().forEach(temp::add);
Charles Chan188ebf52015-12-23 00:15:11 -0800601 innerTtb = temp;
602 }
Charles Chan188ebf52015-12-23 00:15:11 -0800603 // assemble information for ofdpa l2interface group
Yi Tsengef19de12017-04-24 11:33:05 -0700604 int l2groupId = l2GroupId(vlanid, portNum);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800605 // a globally unique groupkey that is different for ports in the same device,
Charles Chan188ebf52015-12-23 00:15:11 -0800606 // but different for the same portnumber on different devices. Also different
607 // for the various group-types created out of the same next objective.
Charles Chane849c192016-01-11 18:28:54 -0800608 int l2gk = l2InterfaceGroupKey(deviceId, vlanid, portNum);
Yi Tsengef19de12017-04-24 11:33:05 -0700609 final GroupKey l2groupkey = new DefaultGroupKey(appKryo.serialize(l2gk));
Charles Chan188ebf52015-12-23 00:15:11 -0800610 // assemble information for outer group
Yi Tsengef19de12017-04-24 11:33:05 -0700611 GroupDescription outerGrpDesc;
Charles Chan188ebf52015-12-23 00:15:11 -0800612 if (mpls) {
Yi Tsengef19de12017-04-24 11:33:05 -0700613 // outer group is MPLS Interface
Saurav Das8be4e3a2016-03-11 17:19:07 -0800614 int mplsInterfaceIndex = getNextAvailableIndex();
Yi Tsengef19de12017-04-24 11:33:05 -0700615 int mplsGroupId = MPLS_INTERFACE_TYPE | (SUBTYPE_MASK & mplsInterfaceIndex);
616 final GroupKey mplsGroupKey = new DefaultGroupKey(
617 appKryo.serialize(mplsInterfaceIndex));
Yi Tsengfa394de2017-02-01 11:26:40 -0800618 outerTtb.group(new GroupId(l2groupId));
Charles Chan188ebf52015-12-23 00:15:11 -0800619 // create the mpls-interface group description to wait for the
620 // l2 interface group to be processed
621 GroupBucket mplsinterfaceGroupBucket =
622 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
623 outerGrpDesc = new DefaultGroupDescription(
624 deviceId,
625 GroupDescription.Type.INDIRECT,
626 new GroupBuckets(Collections.singletonList(
627 mplsinterfaceGroupBucket)),
Yi Tsengef19de12017-04-24 11:33:05 -0700628 mplsGroupKey,
629 mplsGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800630 appId);
631 log.debug("Trying MPLS-Interface: device:{} gid:{} gkey:{} nextid:{}",
Yi Tsengef19de12017-04-24 11:33:05 -0700632 deviceId, Integer.toHexString(mplsGroupId),
633 mplsGroupKey, nextId);
Charles Chan188ebf52015-12-23 00:15:11 -0800634 } else {
635 // outer group is L3Unicast
Saurav Das8be4e3a2016-03-11 17:19:07 -0800636 int l3unicastIndex = getNextAvailableIndex();
637 int l3groupId = L3_UNICAST_TYPE | (TYPE_MASK & l3unicastIndex);
638 final GroupKey l3groupkey = new DefaultGroupKey(
Yi Tsengef19de12017-04-24 11:33:05 -0700639 appKryo.serialize(l3unicastIndex));
Yi Tsengfa394de2017-02-01 11:26:40 -0800640 outerTtb.group(new GroupId(l2groupId));
Charles Chan188ebf52015-12-23 00:15:11 -0800641 // create the l3unicast group description to wait for the
642 // l2 interface group to be processed
643 GroupBucket l3unicastGroupBucket =
644 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
645 outerGrpDesc = new DefaultGroupDescription(
646 deviceId,
647 GroupDescription.Type.INDIRECT,
Yi Tsengef19de12017-04-24 11:33:05 -0700648 new GroupBuckets(Collections.singletonList(l3unicastGroupBucket)),
Charles Chan188ebf52015-12-23 00:15:11 -0800649 l3groupkey,
650 l3groupId,
651 appId);
652 log.debug("Trying L3Unicast: device:{} gid:{} gkey:{} nextid:{}",
653 deviceId, Integer.toHexString(l3groupId),
654 l3groupkey, nextId);
655 }
Charles Chan188ebf52015-12-23 00:15:11 -0800656 // store l2groupkey with the groupChainElem for the outer-group that depends on it
Yi Tsengef19de12017-04-24 11:33:05 -0700657 GroupChainElem gce = new GroupChainElem(outerGrpDesc, 1, false, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800658 updatePendingGroups(l2groupkey, gce);
Yi Tsengef19de12017-04-24 11:33:05 -0700659 // create group description for the inner l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700660 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800661 DefaultGroupBucket.createIndirectGroupBucket(innerTtb.build());
662 GroupDescription l2groupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -0700663 new DefaultGroupDescription(deviceId,
664 GroupDescription.Type.INDIRECT,
665 new GroupBuckets(Collections.singletonList(l2InterfaceGroupBucket)),
666 l2groupkey,
667 l2groupId,
668 appId);
Charles Chan188ebf52015-12-23 00:15:11 -0800669 log.debug("Trying L2Interface: device:{} gid:{} gkey:{} nextId:{}",
670 deviceId, Integer.toHexString(l2groupId),
671 l2groupkey, nextId);
672 return new GroupInfo(l2groupDescription, outerGrpDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800673 }
674
675 /**
676 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
677 * a chain of groups. The broadcast Next Objective passed in by the application
678 * has to be broken up into a group chain comprising of an
Saurav Das1a129a02016-11-18 15:21:57 -0800679 * L2 Flood group or L3 Multicast group, whose buckets point to L2 Interface groups.
Charles Chan188ebf52015-12-23 00:15:11 -0800680 *
681 * @param nextObj the nextObjective of type BROADCAST
682 */
683 private void processBroadcastNextObjective(NextObjective nextObj) {
Yi Tsengef19de12017-04-24 11:33:05 -0700684 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700685 if (assignedVlan == null) {
686 log.warn("VLAN ID required by broadcast next obj is missing. Abort.");
Yi Tsengef19de12017-04-24 11:33:05 -0700687 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800688 return;
689 }
Charles Chan188ebf52015-12-23 00:15:11 -0800690
jayakumarthazhath46945b62018-10-01 00:51:54 +0530691 // Handling L2 multicast cases.
692 MacAddress dstMac = readEthDstFromSelector(nextObj.meta());
693 if (dstMac != null && dstMac.isMulticast()) {
694 processL2MulticastNextObjective(nextObj);
Jayakumar Thazhatheb0931f2018-10-24 01:22:04 -0400695 return;
jayakumarthazhath46945b62018-10-01 00:51:54 +0530696 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700697
jayakumarthazhath46945b62018-10-01 00:51:54 +0530698 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Yi Tsengef19de12017-04-24 11:33:05 -0700699 IpPrefix ipDst = readIpDstFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700700 if (ipDst != null) {
701 if (ipDst.isMulticast()) {
702 createL3MulticastGroup(nextObj, assignedVlan, groupInfos);
703 } else {
704 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -0700705 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700706 }
707 } else {
708 createL2FloodGroup(nextObj, assignedVlan, groupInfos);
709 }
710 }
711
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700712 private List<GroupInfo> prepareL2InterfaceGroup(NextObjective nextObj,
713 VlanId assignedVlan) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700714 ImmutableList.Builder<GroupInfo> groupInfoBuilder = ImmutableList.builder();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700715 // break up broadcast next objective to multiple groups
716 Collection<TrafficTreatment> buckets = nextObj.next();
Charles Chan188ebf52015-12-23 00:15:11 -0800717 // each treatment is converted to an L2 interface group
Charles Chan188ebf52015-12-23 00:15:11 -0800718 for (TrafficTreatment treatment : buckets) {
719 TrafficTreatment.Builder newTreatment = DefaultTrafficTreatment.builder();
720 PortNumber portNum = null;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700721 VlanId egressVlan = null;
Charles Chan188ebf52015-12-23 00:15:11 -0800722 // ensure that the only allowed treatments are pop-vlan and output
723 for (Instruction ins : treatment.allInstructions()) {
724 if (ins.type() == Instruction.Type.L2MODIFICATION) {
725 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
726 switch (l2ins.subtype()) {
727 case VLAN_POP:
728 newTreatment.add(l2ins);
729 break;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700730 case VLAN_ID:
731 egressVlan = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
732 break;
Charles Chan188ebf52015-12-23 00:15:11 -0800733 default:
734 log.debug("action {} not permitted for broadcast nextObj",
735 l2ins.subtype());
736 break;
737 }
738 } else if (ins.type() == Instruction.Type.OUTPUT) {
739 portNum = ((Instructions.OutputInstruction) ins).port();
740 newTreatment.add(ins);
741 } else {
Charles Chane849c192016-01-11 18:28:54 -0800742 log.debug("TrafficTreatment of type {} not permitted in " +
743 " broadcast nextObjective", ins.type());
Charles Chan188ebf52015-12-23 00:15:11 -0800744 }
745 }
Yi Tsengef19de12017-04-24 11:33:05 -0700746 if (portNum == null) {
Ruchi Sahota5d800282019-01-28 01:08:18 +0000747 log.debug("Can't find output port for the bucket {}.", treatment);
Yi Tsengef19de12017-04-24 11:33:05 -0700748 continue;
749 }
Charles Chan188ebf52015-12-23 00:15:11 -0800750 // assemble info for l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700751 VlanId l2InterfaceGroupVlan =
752 (egressVlan != null && !assignedVlan.equals(egressVlan)) ?
753 egressVlan : assignedVlan;
754 int l2gk = l2InterfaceGroupKey(deviceId, l2InterfaceGroupVlan, portNum.toLong());
755 final GroupKey l2InterfaceGroupKey =
Yi Tsengef19de12017-04-24 11:33:05 -0700756 new DefaultGroupKey(appKryo.serialize(l2gk));
Charles Chan372b63e2017-02-07 12:10:53 -0800757 int l2InterfaceGroupId = L2_INTERFACE_TYPE |
758 ((l2InterfaceGroupVlan.toShort() & THREE_BIT_MASK) << PORT_LEN) |
759 ((int) portNum.toLong() & FOUR_BIT_MASK);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700760 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800761 DefaultGroupBucket.createIndirectGroupBucket(newTreatment.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700762 GroupDescription l2InterfaceGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -0700763 new DefaultGroupDescription(deviceId,
764 GroupDescription.Type.INDIRECT,
765 new GroupBuckets(Collections.singletonList(
766 l2InterfaceGroupBucket)),
767 l2InterfaceGroupKey,
768 l2InterfaceGroupId,
769 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800770 log.debug("Trying L2-Interface: device:{} gid:{} gkey:{} nextid:{}",
Charles Chan5b9df8d2016-03-28 22:21:40 -0700771 deviceId, Integer.toHexString(l2InterfaceGroupId),
772 l2InterfaceGroupKey, nextObj.id());
Ruchi Sahota5d800282019-01-28 01:08:18 +0000773
Charles Chan5b9df8d2016-03-28 22:21:40 -0700774 groupInfoBuilder.add(new GroupInfo(l2InterfaceGroupDescription,
775 l2InterfaceGroupDescription));
Charles Chan188ebf52015-12-23 00:15:11 -0800776 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700777 return groupInfoBuilder.build();
778 }
Charles Chan188ebf52015-12-23 00:15:11 -0800779
Ruchi Sahota5d800282019-01-28 01:08:18 +0000780 private GroupInfo prepareL3UnicastGroup(NextObjective nextObj, NextGroup next) {
781
782 ImmutableList.Builder<GroupInfo> groupInfoBuilder = ImmutableList.builder();
783 TrafficTreatment treatment = nextObj.next().iterator().next();
784
785 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
786 if (assignedVlan == null) {
787 log.warn("VLAN ID required by next obj is missing. Abort.");
788 return null;
789 }
790
791 List<GroupInfo> l2GroupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
792 GroupDescription l2InterfaceGroupDesc = l2GroupInfos.get(0).innerMostGroupDesc();
793 GroupKey l2groupkey = l2InterfaceGroupDesc.appCookie();
794
795 TrafficTreatment.Builder outerTtb = DefaultTrafficTreatment.builder();
796 VlanId vlanid = null;
797 MacAddress srcMac;
798 MacAddress dstMac;
799 for (Instruction ins : treatment.allInstructions()) {
800 if (ins.type() == Instruction.Type.L2MODIFICATION) {
801 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
802 switch (l2ins.subtype()) {
803 case ETH_DST:
804 dstMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
805 outerTtb.setEthDst(dstMac);
806 break;
807 case ETH_SRC:
808 srcMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
809 outerTtb.setEthSrc(srcMac);
810 break;
811 case VLAN_ID:
812 vlanid = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
813 outerTtb.setVlanId(vlanid);
814 break;
815 default:
816 break;
817 }
818 } else {
819 log.debug("Driver does not handle this type of TrafficTreatment"
820 + " instruction in l2l3chain: {} - {}", ins.type(), ins);
821 }
822 }
823
824 GroupId l2groupId = new GroupId(l2InterfaceGroupDesc.givenGroupId());
825 outerTtb.group(l2groupId);
826
827 // we need the top level group's key to point the flow to it
828 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
829 GroupKey l3groupkey = gkeys.get(0).peekFirst();
830 GroupId grpId = groupService.getGroup(deviceId, l3groupkey).id();
831 int l3groupId = grpId.id();
832
833 // create the l3unicast group description to wait for the
834 // l2 interface group to be processed
835 GroupBucket l3UnicastGroupBucket = DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
836
837 GroupDescription l3UnicastGroupDescription = new DefaultGroupDescription(deviceId,
838 GroupDescription.Type.INDIRECT,
839 new GroupBuckets(Collections.singletonList(
840 l3UnicastGroupBucket)), l3groupkey,
841 l3groupId, nextObj.appId());
842
843 // store l2groupkey with the groupChainElem for the outer-group that depends on it
844 GroupChainElem gce = new GroupChainElem(l3UnicastGroupDescription, 1, false, deviceId);
845 updatePendingGroups(l2groupkey, gce);
846
847 log.debug("Trying L3-Interface: device:{} gid:{} gkey:{} nextid:{}",
848 deviceId, Integer.toHexString(l3groupId), l3groupkey, nextObj.id());
849
850 groupInfoBuilder.add(new GroupInfo(l2InterfaceGroupDesc,
851 l3UnicastGroupDescription));
852
853 return groupInfoBuilder.build().iterator().next();
854 }
855
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700856 private void createL2FloodGroup(NextObjective nextObj, VlanId vlanId,
857 List<GroupInfo> groupInfos) {
858 // assemble info for l2 flood group. Since there can be only one flood
859 // group for a vlan, its index is always the same - 0
Yi Tsengef19de12017-04-24 11:33:05 -0700860 Integer l2FloodGroupId = L2_FLOOD_TYPE | (vlanId.toShort() << 16);
861 final GroupKey l2FloodGroupKey = l2FloodGroupKey(vlanId, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800862 // collection of group buckets pointing to all the l2 interface groups
Yi Tsengef19de12017-04-24 11:33:05 -0700863 List<GroupBucket> l2floodBuckets = generateNextGroupBuckets(groupInfos, ALL);
Charles Chan188ebf52015-12-23 00:15:11 -0800864 // create the l2flood group-description to wait for all the
865 // l2interface groups to be processed
866 GroupDescription l2floodGroupDescription =
867 new DefaultGroupDescription(
868 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800869 ALL,
Charles Chan188ebf52015-12-23 00:15:11 -0800870 new GroupBuckets(l2floodBuckets),
Yi Tsengef19de12017-04-24 11:33:05 -0700871 l2FloodGroupKey,
872 l2FloodGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800873 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800874 log.debug("Trying L2-Flood: device:{} gid:{} gkey:{} nextid:{}",
Yi Tsengef19de12017-04-24 11:33:05 -0700875 deviceId, Integer.toHexString(l2FloodGroupId),
876 l2FloodGroupKey, nextObj.id());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700877 // Put all dependency information into allGroupKeys
878 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
879 groupInfos.forEach(groupInfo -> {
Yi Tsengef19de12017-04-24 11:33:05 -0700880 Deque<GroupKey> groupKeyChain = new ArrayDeque<>();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700881 // In this case we should have L2 interface group only
Yi Tsengef19de12017-04-24 11:33:05 -0700882 groupKeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
883 groupKeyChain.addFirst(l2FloodGroupKey);
884 allGroupKeys.add(groupKeyChain);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700885 });
Charles Chan5b9df8d2016-03-28 22:21:40 -0700886 // Point the next objective to this group
887 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -0700888 updatePendingNextObjective(l2FloodGroupKey, ofdpaGrp);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700889 GroupChainElem gce = new GroupChainElem(l2floodGroupDescription,
Yi Tsengef19de12017-04-24 11:33:05 -0700890 groupInfos.size(), false, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700891 groupInfos.forEach(groupInfo -> {
892 // Point this group to the next group
Yi Tsengef19de12017-04-24 11:33:05 -0700893 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), gce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700894 // Start installing the inner-most group
Yi Tsengef19de12017-04-24 11:33:05 -0700895 groupService.addGroup(groupInfo.innerMostGroupDesc());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700896 });
897 }
898
Pier Luigi21fffd22018-01-19 10:24:53 +0100899 private void createL3MulticastGroup(NextObjective nextObj, VlanId vlanId,
900 List<GroupInfo> groupInfos) {
901 // Let's create a new list mcast buckets
902 List<GroupBucket> l3McastBuckets = createL3MulticastBucket(groupInfos);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700903
904 int l3MulticastIndex = getNextAvailableIndex();
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700905 int l3MulticastGroupId = L3_MULTICAST_TYPE |
906 vlanId.toShort() << 16 | (TYPE_VLAN_MASK & l3MulticastIndex);
907 final GroupKey l3MulticastGroupKey =
Yi Tsengef19de12017-04-24 11:33:05 -0700908 new DefaultGroupKey(appKryo.serialize(l3MulticastIndex));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700909
910 GroupDescription l3MulticastGroupDesc = new DefaultGroupDescription(deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800911 ALL,
Charles Chan5b9df8d2016-03-28 22:21:40 -0700912 new GroupBuckets(l3McastBuckets),
913 l3MulticastGroupKey,
914 l3MulticastGroupId,
915 nextObj.appId());
916
917 // Put all dependency information into allGroupKeys
918 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
919 groupInfos.forEach(groupInfo -> {
920 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700921 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700922 // Add L3 interface group to the chain if there is one.
Yi Tsengef19de12017-04-24 11:33:05 -0700923 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
924 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700925 }
926 gkeyChain.addFirst(l3MulticastGroupKey);
927 allGroupKeys.add(gkeyChain);
928 });
Charles Chan5b9df8d2016-03-28 22:21:40 -0700929 // Point the next objective to this group
930 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
931 updatePendingNextObjective(l3MulticastGroupKey, ofdpaGrp);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700932 GroupChainElem outerGce = new GroupChainElem(l3MulticastGroupDesc,
Yi Tsengef19de12017-04-24 11:33:05 -0700933 groupInfos.size(), false, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700934 groupInfos.forEach(groupInfo -> {
935 // Point this group (L3 multicast) to the next group
Yi Tsengef19de12017-04-24 11:33:05 -0700936 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), outerGce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700937 // Point next group to inner-most group, if any
Yi Tsengef19de12017-04-24 11:33:05 -0700938 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
939 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(),
940 1, false, deviceId);
941 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700942 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700943 // Start installing the inner-most group
Yi Tsengef19de12017-04-24 11:33:05 -0700944 groupService.addGroup(groupInfo.innerMostGroupDesc());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700945 });
Charles Chan188ebf52015-12-23 00:15:11 -0800946 }
947
Charles Chan188ebf52015-12-23 00:15:11 -0800948 /**
949 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
950 * a chain of groups. The hashed Next Objective passed in by the application
951 * has to be broken up into a group chain comprising of an
952 * L3 ECMP group as the top level group. Buckets of this group can point
953 * to a variety of groups in a group chain, depending on the whether
954 * MPLS labels are being pushed or not.
955 * <p>
956 * NOTE: We do not create MPLS ECMP groups as they are unimplemented in
957 * OF-DPA 2.0 (even though it is in the spec). Therefore we do not
958 * check the nextObjective meta to see what is matching before being
959 * sent to this nextObjective.
960 *
961 * @param nextObj the nextObjective of type HASHED
962 */
Pier Ventre140a8942016-11-02 07:26:38 -0700963 protected void processHashedNextObjective(NextObjective nextObj) {
Charles Chan188ebf52015-12-23 00:15:11 -0800964 // storage for all group keys in the chain of groups created
965 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
966 List<GroupInfo> unsentGroups = new ArrayList<>();
967 createHashBucketChains(nextObj, allGroupKeys, unsentGroups);
Charles Chan188ebf52015-12-23 00:15:11 -0800968 // now we can create the outermost L3 ECMP group
969 List<GroupBucket> l3ecmpGroupBuckets = new ArrayList<>();
970 for (GroupInfo gi : unsentGroups) {
971 // create ECMP bucket to point to the outer group
972 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Yi Tsengef19de12017-04-24 11:33:05 -0700973 ttb.group(new GroupId(gi.nextGroupDesc().givenGroupId()));
Charles Chan188ebf52015-12-23 00:15:11 -0800974 GroupBucket sbucket = DefaultGroupBucket
975 .createSelectGroupBucket(ttb.build());
976 l3ecmpGroupBuckets.add(sbucket);
977 }
Saurav Das8be4e3a2016-03-11 17:19:07 -0800978 int l3ecmpIndex = getNextAvailableIndex();
979 int l3ecmpGroupId = L3_ECMP_TYPE | (TYPE_MASK & l3ecmpIndex);
980 GroupKey l3ecmpGroupKey = new DefaultGroupKey(
Yi Tsengef19de12017-04-24 11:33:05 -0700981 appKryo.serialize(l3ecmpIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800982 GroupDescription l3ecmpGroupDesc =
983 new DefaultGroupDescription(
984 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800985 SELECT,
Charles Chan188ebf52015-12-23 00:15:11 -0800986 new GroupBuckets(l3ecmpGroupBuckets),
987 l3ecmpGroupKey,
988 l3ecmpGroupId,
989 nextObj.appId());
990 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
991 l3ecmpGroupBuckets.size(),
Yi Tsengef19de12017-04-24 11:33:05 -0700992 false, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800993
994 // create objects for local and distributed storage
Yi Tsengef19de12017-04-24 11:33:05 -0700995 allGroupKeys.forEach(gKeyChain -> gKeyChain.addFirst(l3ecmpGroupKey));
Charles Chan188ebf52015-12-23 00:15:11 -0800996 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Charles Chan188ebf52015-12-23 00:15:11 -0800997 // store l3ecmpGroupKey with the ofdpaGroupChain for the nextObjective
998 // that depends on it
999 updatePendingNextObjective(l3ecmpGroupKey, ofdpaGrp);
Charles Chan188ebf52015-12-23 00:15:11 -08001000 log.debug("Trying L3ECMP: device:{} gid:{} gkey:{} nextId:{}",
1001 deviceId, Integer.toHexString(l3ecmpGroupId),
1002 l3ecmpGroupKey, nextObj.id());
1003 // finally we are ready to send the innermost groups
1004 for (GroupInfo gi : unsentGroups) {
1005 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001006 Integer.toHexString(gi.innerMostGroupDesc().givenGroupId()), deviceId);
1007 updatePendingGroups(gi.nextGroupDesc().appCookie(), l3ecmpGce);
1008 groupService.addGroup(gi.innerMostGroupDesc());
Charles Chan188ebf52015-12-23 00:15:11 -08001009 }
Charles Chan188ebf52015-12-23 00:15:11 -08001010 }
1011
1012 /**
1013 * Creates group chains for all buckets in a hashed group, and stores the
1014 * GroupInfos and GroupKeys for all the groups in the lists passed in, which
1015 * should be empty.
1016 * <p>
1017 * Does not create the top level ECMP group. Does not actually send the
1018 * groups to the groupService.
1019 *
1020 * @param nextObj the Next Objective with buckets that need to be converted
1021 * to group chains
1022 * @param allGroupKeys a list to store groupKey for each bucket-group-chain
1023 * @param unsentGroups a list to store GroupInfo for each bucket-group-chain
1024 */
Pier Ventre140a8942016-11-02 07:26:38 -07001025 protected void createHashBucketChains(NextObjective nextObj,
Yi Tsengef19de12017-04-24 11:33:05 -07001026 List<Deque<GroupKey>> allGroupKeys,
1027 List<GroupInfo> unsentGroups) {
Charles Chan188ebf52015-12-23 00:15:11 -08001028 // break up hashed next objective to multiple groups
1029 Collection<TrafficTreatment> buckets = nextObj.next();
1030
1031 for (TrafficTreatment bucket : buckets) {
1032 //figure out how many labels are pushed in each bucket
1033 int labelsPushed = 0;
1034 MplsLabel innermostLabel = null;
1035 for (Instruction ins : bucket.allInstructions()) {
1036 if (ins.type() == Instruction.Type.L2MODIFICATION) {
1037 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
1038 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_PUSH) {
1039 labelsPushed++;
1040 }
1041 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) {
1042 if (innermostLabel == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001043 innermostLabel =
1044 ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label();
Charles Chan188ebf52015-12-23 00:15:11 -08001045 }
1046 }
1047 }
1048 }
Yi Tsengef19de12017-04-24 11:33:05 -07001049 Deque<GroupKey> gKeyChain = new ArrayDeque<>();
Saurav Dasa4020382018-02-14 14:14:54 -08001050 // here we only deal with 0 and 1 label push
Charles Chan188ebf52015-12-23 00:15:11 -08001051 if (labelsPushed == 0) {
Yi Tsengef19de12017-04-24 11:33:05 -07001052 GroupInfo noLabelGroupInfo;
Pier Ventre140a8942016-11-02 07:26:38 -07001053 TrafficSelector metaSelector = nextObj.meta();
1054 if (metaSelector != null) {
1055 if (isNotMplsBos(metaSelector)) {
Yi Tsengef19de12017-04-24 11:33:05 -07001056 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
1057 nextObj.appId(), true,
1058 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -07001059 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001060 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
1061 nextObj.appId(), false,
1062 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -07001063 }
1064 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001065 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
1066 nextObj.appId(), false,
1067 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -07001068 }
Yi Tsengef19de12017-04-24 11:33:05 -07001069 if (noLabelGroupInfo == null) {
Charles Chan188ebf52015-12-23 00:15:11 -08001070 log.error("Could not process nextObj={} in dev:{}",
1071 nextObj.id(), deviceId);
1072 return;
1073 }
Yi Tsengef19de12017-04-24 11:33:05 -07001074 gKeyChain.addFirst(noLabelGroupInfo.innerMostGroupDesc().appCookie());
1075 gKeyChain.addFirst(noLabelGroupInfo.nextGroupDesc().appCookie());
Charles Chan188ebf52015-12-23 00:15:11 -08001076 // we can't send the inner group description yet, as we have to
1077 // create the dependent ECMP group first. So we store..
Yi Tsengef19de12017-04-24 11:33:05 -07001078 unsentGroups.add(noLabelGroupInfo);
Charles Chan188ebf52015-12-23 00:15:11 -08001079 } else if (labelsPushed == 1) {
1080 GroupInfo onelabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
1081 nextObj.appId(), true,
1082 nextObj.meta());
1083 if (onelabelGroupInfo == null) {
1084 log.error("Could not process nextObj={} in dev:{}",
1085 nextObj.id(), deviceId);
1086 return;
1087 }
1088 // we need to add another group to this chain - the L3VPN group
1089 TrafficTreatment.Builder l3vpnTtb = DefaultTrafficTreatment.builder();
Charles Chan0f43e472017-02-14 14:00:16 -08001090 if (requireVlanPopBeforeMplsPush()) {
1091 l3vpnTtb.popVlan();
1092 }
Charles Chan188ebf52015-12-23 00:15:11 -08001093 l3vpnTtb.pushMpls()
1094 .setMpls(innermostLabel)
Yi Tsengef19de12017-04-24 11:33:05 -07001095 .group(new GroupId(onelabelGroupInfo.nextGroupDesc().givenGroupId()));
Charles Chan40132b32017-01-22 00:19:37 -08001096 if (supportCopyTtl()) {
1097 l3vpnTtb.copyTtlOut();
1098 }
1099 if (supportSetMplsBos()) {
1100 l3vpnTtb.setMplsBos(true);
1101 }
Charles Chan0f43e472017-02-14 14:00:16 -08001102 if (requireVlanPopBeforeMplsPush()) {
1103 l3vpnTtb.pushVlan().setVlanId(VlanId.vlanId(VlanId.RESERVED));
1104 }
Charles Chan188ebf52015-12-23 00:15:11 -08001105 GroupBucket l3vpnGrpBkt =
1106 DefaultGroupBucket.createIndirectGroupBucket(l3vpnTtb.build());
Saurav Das8be4e3a2016-03-11 17:19:07 -08001107 int l3vpnIndex = getNextAvailableIndex();
Yi Tsengef19de12017-04-24 11:33:05 -07001108 int l3vpnGroupId = MPLS_L3VPN_SUBTYPE | (SUBTYPE_MASK & l3vpnIndex);
1109 GroupKey l3vpnGroupKey = new DefaultGroupKey(
1110 appKryo.serialize(l3vpnIndex));
Charles Chan188ebf52015-12-23 00:15:11 -08001111 GroupDescription l3vpnGroupDesc =
1112 new DefaultGroupDescription(
1113 deviceId,
1114 GroupDescription.Type.INDIRECT,
Yi Tsengef19de12017-04-24 11:33:05 -07001115 new GroupBuckets(Collections.singletonList(l3vpnGrpBkt)),
1116 l3vpnGroupKey,
1117 l3vpnGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -08001118 nextObj.appId());
Yi Tsengef19de12017-04-24 11:33:05 -07001119 GroupChainElem l3vpnGce = new GroupChainElem(l3vpnGroupDesc,
1120 1,
1121 false,
1122 deviceId);
1123 updatePendingGroups(onelabelGroupInfo.nextGroupDesc().appCookie(), l3vpnGce);
Charles Chan188ebf52015-12-23 00:15:11 -08001124
Yi Tsengef19de12017-04-24 11:33:05 -07001125 gKeyChain.addFirst(onelabelGroupInfo.innerMostGroupDesc().appCookie());
1126 gKeyChain.addFirst(onelabelGroupInfo.nextGroupDesc().appCookie());
1127 gKeyChain.addFirst(l3vpnGroupKey);
Charles Chan188ebf52015-12-23 00:15:11 -08001128 //now we can replace the outerGrpDesc with the one we just created
Yi Tsengef19de12017-04-24 11:33:05 -07001129 onelabelGroupInfo.nextGroupDesc(l3vpnGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -08001130 // we can't send the innermost group yet, as we have to create
1131 // the dependent ECMP group first. So we store ...
1132 unsentGroups.add(onelabelGroupInfo);
Yi Tsengef19de12017-04-24 11:33:05 -07001133 log.debug("Trying L3VPN: device:{} gid:{} group key:{} nextId:{}",
1134 deviceId, Integer.toHexString(l3vpnGroupId),
1135 l3vpnGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -08001136 } else {
1137 log.warn("Driver currently does not handle more than 1 MPLS "
1138 + "labels. Not processing nextObjective {}", nextObj.id());
1139 return;
1140 }
Charles Chan188ebf52015-12-23 00:15:11 -08001141 // all groups in this chain
Yi Tsengef19de12017-04-24 11:33:05 -07001142 allGroupKeys.add(gKeyChain);
Charles Chan188ebf52015-12-23 00:15:11 -08001143 }
1144 }
1145
Pier Ventre42287df2016-11-09 14:17:26 -08001146 /**
1147 * Processes the pseudo wire related next objective.
1148 * This procedure try to reuse the mpls label groups,
1149 * the mpls interface group and the l2 interface group.
1150 *
1151 * @param nextObjective the objective to process.
1152 */
1153 protected void processPwNextObjective(NextObjective nextObjective) {
Saurav Das1547b3f2017-05-05 17:01:08 -07001154 log.warn("Pseudo wire extensions are not supported in OFDPA 2.0 {}",
1155 nextObjective.id());
Pier Ventre42287df2016-11-09 14:17:26 -08001156 }
1157
Saurav Das8be4e3a2016-03-11 17:19:07 -08001158 //////////////////////////////////////
1159 // Group Editing
1160 //////////////////////////////////////
Charles Chan188ebf52015-12-23 00:15:11 -08001161 /**
1162 * Adds a bucket to the top level group of a group-chain, and creates the chain.
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001163 * Ensures that bucket being added is not a duplicate, by checking existing
Yi Tsengef19de12017-04-24 11:33:05 -07001164 * buckets for the same output port.
Charles Chan188ebf52015-12-23 00:15:11 -08001165 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001166 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -08001167 * @param next the representation of the existing group-chain for this next objective
1168 */
1169 protected void addBucketToGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001170 if (nextObjective.type() != NextObjective.Type.HASHED &&
1171 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001172 log.warn("AddBuckets not applied to nextType:{} in dev:{} for next:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001173 nextObjective.type(), deviceId, nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -07001174 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001175 return;
1176 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001177 // first check to see if bucket being added is not a duplicate of an
Yi Tsengef19de12017-04-24 11:33:05 -07001178 // existing bucket. If it is for an existing output port, then its a
1179 // duplicate.
Yi Tseng78f51f42017-02-02 13:54:58 -08001180 Set<TrafficTreatment> duplicateBuckets = Sets.newHashSet();
Yi Tsengef19de12017-04-24 11:33:05 -07001181 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
1182 Set<PortNumber> existingPorts = getExistingOutputPorts(allActiveKeys,
1183 groupService,
1184 deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -08001185 Set<TrafficTreatment> nonDuplicateBuckets = Sets.newHashSet();
1186 NextObjective objectiveToAdd;
Yi Tseng78f51f42017-02-02 13:54:58 -08001187 nextObjective.next().forEach(trafficTreatment -> {
1188 PortNumber portNumber = readOutPortFromTreatment(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001189 if (portNumber == null) {
1190 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001191 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001192 if (existingPorts.contains(portNumber)) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001193 // its possible that portnumbers are same but labels are different
1194 int label = readLabelFromTreatment(trafficTreatment);
1195 if (label == -1) {
1196 duplicateBuckets.add(trafficTreatment);
1197 } else {
Saurav Dasceccf242017-08-03 18:30:35 -07001198 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1199 groupService, deviceId,
1200 portNumber, label);
1201 if (!existing.isEmpty()) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001202 duplicateBuckets.add(trafficTreatment);
1203 } else {
1204 nonDuplicateBuckets.add(trafficTreatment);
1205 }
1206 }
Yi Tseng47f82dc2017-03-05 22:48:39 -08001207 } else {
1208 nonDuplicateBuckets.add(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001209 }
1210 });
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001211 if (duplicateBuckets.isEmpty()) {
1212 // use the original objective
1213 objectiveToAdd = nextObjective;
1214 } else if (!nonDuplicateBuckets.isEmpty()) {
1215 // only use the non-duplicate buckets if there are any
1216 log.debug("Some buckets {} already exist in next id {}, duplicate "
1217 + "buckets will be ignored.", duplicateBuckets, nextObjective.id());
1218 // new next objective with non duplicate treatments
Yi Tseng47f82dc2017-03-05 22:48:39 -08001219 NextObjective.Builder builder = DefaultNextObjective.builder()
1220 .withType(nextObjective.type())
1221 .withId(nextObjective.id())
1222 .withMeta(nextObjective.meta())
1223 .fromApp(nextObjective.appId());
1224 nonDuplicateBuckets.forEach(builder::addTreatment);
Yi Tseng47f82dc2017-03-05 22:48:39 -08001225 ObjectiveContext context = nextObjective.context().orElse(null);
1226 objectiveToAdd = builder.addToExisting(context);
1227 } else {
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001228 // buckets to add are already there - nothing to do
Saurav Das1547b3f2017-05-05 17:01:08 -07001229 log.debug("buckets already exist {} in next: {} ..ignoring bucket add",
1230 duplicateBuckets, nextObjective.id());
1231 pass(nextObjective);
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001232 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001233 }
Saurav Das1a129a02016-11-18 15:21:57 -08001234 if (nextObjective.type() == NextObjective.Type.HASHED) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001235 addBucketToHashGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001236 } else if (nextObjective.type() == NextObjective.Type.BROADCAST) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001237 addBucketToBroadcastGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001238 }
1239 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001240
Saurav Das1a129a02016-11-18 15:21:57 -08001241 private void addBucketToHashGroup(NextObjective nextObjective,
Yi Tseng78f51f42017-02-02 13:54:58 -08001242 List<Deque<GroupKey>> allActiveKeys) {
Charles Chan188ebf52015-12-23 00:15:11 -08001243 // storage for all group keys in the chain of groups created
1244 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
1245 List<GroupInfo> unsentGroups = new ArrayList<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001246 List<GroupBucket> newBuckets;
Charles Chan188ebf52015-12-23 00:15:11 -08001247 createHashBucketChains(nextObjective, allGroupKeys, unsentGroups);
Yi Tseng78f51f42017-02-02 13:54:58 -08001248 // now we can create the buckets to add to the outermost L3 ECMP group
1249 newBuckets = generateNextGroupBuckets(unsentGroups, SELECT);
Saurav Das1a129a02016-11-18 15:21:57 -08001250 // retrieve the original L3 ECMP group
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001251 Group l3ecmpGroup = retrieveTopLevelGroup(allActiveKeys, deviceId,
1252 groupService, nextObjective.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001253 if (l3ecmpGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001254 fail(nextObjective, ObjectiveError.GROUPMISSING);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001255 return;
1256 }
Saurav Das1a129a02016-11-18 15:21:57 -08001257 GroupKey l3ecmpGroupKey = l3ecmpGroup.appCookie();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001258 int l3ecmpGroupId = l3ecmpGroup.id().id();
Charles Chan188ebf52015-12-23 00:15:11 -08001259 // Although GroupDescriptions are not necessary for adding buckets to
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001260 // existing groups, we still use one in the GroupChainElem. When the latter is
Charles Chan188ebf52015-12-23 00:15:11 -08001261 // processed, the info will be extracted for the bucketAdd call to groupService
1262 GroupDescription l3ecmpGroupDesc =
Yi Tsengef19de12017-04-24 11:33:05 -07001263 new DefaultGroupDescription(deviceId,
1264 SELECT,
1265 new GroupBuckets(newBuckets),
1266 l3ecmpGroupKey,
1267 l3ecmpGroupId,
1268 nextObjective.appId());
Yi Tseng78f51f42017-02-02 13:54:58 -08001269 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
1270 unsentGroups.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001271 true,
1272 deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001273
Saurav Dasc88d4662017-05-15 15:34:25 -07001274 // update new bucket-chains
1275 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
1276 for (Deque<GroupKey> newBucketChain : allGroupKeys) {
1277 newBucketChain.addFirst(l3ecmpGroupKey);
1278 addedKeys.add(newBucketChain);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001279 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001280 updatePendingNextObjective(l3ecmpGroupKey,
1281 new OfdpaNextGroup(addedKeys, nextObjective));
Yi Tsengef19de12017-04-24 11:33:05 -07001282 log.debug("Adding to L3ECMP: device:{} gid:{} group key:{} nextId:{}",
Charles Chan188ebf52015-12-23 00:15:11 -08001283 deviceId, Integer.toHexString(l3ecmpGroupId),
1284 l3ecmpGroupKey, nextObjective.id());
Yi Tseng78f51f42017-02-02 13:54:58 -08001285 unsentGroups.forEach(groupInfo -> {
1286 // send the innermost group
1287 log.debug("Sending innermost group {} in group chain on device {} ",
Saurav Dasc88d4662017-05-15 15:34:25 -07001288 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
1289 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001290 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3ecmpGce);
1291 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001292 });
Saurav Das1a129a02016-11-18 15:21:57 -08001293 }
Charles Chan188ebf52015-12-23 00:15:11 -08001294
Saurav Das1a129a02016-11-18 15:21:57 -08001295 private void addBucketToBroadcastGroup(NextObjective nextObj,
Yi Tsengef19de12017-04-24 11:33:05 -07001296 List<Deque<GroupKey>> allActiveKeys) {
1297 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001298 if (assignedVlan == null) {
1299 log.warn("VLAN ID required by broadcast next obj is missing. "
1300 + "Aborting add bucket to broadcast group for next:{} in dev:{}",
1301 nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001302 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001303 return;
1304 }
Saurav Das1a129a02016-11-18 15:21:57 -08001305 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Yi Tsengef19de12017-04-24 11:33:05 -07001306 IpPrefix ipDst = readIpDstFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001307 if (ipDst != null) {
1308 if (ipDst.isMulticast()) {
Yi Tsengef19de12017-04-24 11:33:05 -07001309 addBucketToL3MulticastGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001310 } else {
1311 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001312 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001313 }
1314 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001315 addBucketToL2FloodGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001316 }
1317 }
1318
1319 private void addBucketToL2FloodGroup(NextObjective nextObj,
1320 List<Deque<GroupKey>> allActiveKeys,
1321 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001322 VlanId assignedVlan) {
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001323 Group l2FloodGroup = retrieveTopLevelGroup(allActiveKeys, deviceId,
1324 groupService, nextObj.id());
Yi Tseng78f51f42017-02-02 13:54:58 -08001325
1326 if (l2FloodGroup == null) {
1327 log.warn("Can't find L2 flood group while adding bucket to it. NextObj = {}",
1328 nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001329 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001330 return;
1331 }
Saurav Das1a129a02016-11-18 15:21:57 -08001332
Yi Tseng78f51f42017-02-02 13:54:58 -08001333 GroupKey l2floodGroupKey = l2FloodGroup.appCookie();
1334 int l2floodGroupId = l2FloodGroup.id().id();
1335 List<GroupBucket> newBuckets = generateNextGroupBuckets(groupInfos, ALL);
Yi Tseng78f51f42017-02-02 13:54:58 -08001336 GroupDescription l2FloodGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001337 new DefaultGroupDescription(deviceId,
1338 ALL,
1339 new GroupBuckets(newBuckets),
1340 l2floodGroupKey,
1341 l2floodGroupId,
1342 nextObj.appId());
Yi Tseng78f51f42017-02-02 13:54:58 -08001343 GroupChainElem l2FloodGroupChainElement =
1344 new GroupChainElem(l2FloodGroupDescription,
1345 groupInfos.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001346 true,
1347 deviceId);
Yi Tseng78f51f42017-02-02 13:54:58 -08001348
Yi Tseng78f51f42017-02-02 13:54:58 -08001349
1350 //ensure assignedVlan applies to the chosen group
1351 VlanId floodGroupVlan = extractVlanIdFromGroupId(l2floodGroupId);
Yi Tseng78f51f42017-02-02 13:54:58 -08001352 if (!floodGroupVlan.equals(assignedVlan)) {
1353 log.warn("VLAN ID {} does not match Flood group {} to which bucket is "
1354 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1355 Integer.toHexString(l2floodGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001356 fail(nextObj, ObjectiveError.BADPARAMS);
Yi Tseng78f51f42017-02-02 13:54:58 -08001357 return;
1358 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001359 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
Yi Tseng78f51f42017-02-02 13:54:58 -08001360 groupInfos.forEach(groupInfo -> {
1361 // update original NextGroup with new bucket-chain
Yi Tseng78f51f42017-02-02 13:54:58 -08001362 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001363 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001364 newBucketChain.addFirst(l2floodGroupKey);
Saurav Dasc88d4662017-05-15 15:34:25 -07001365 addedKeys.add(newBucketChain);
Yi Tsengef19de12017-04-24 11:33:05 -07001366 log.debug("Adding to L2FLOOD: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001367 deviceId, Integer.toHexString(l2floodGroupId),
1368 l2floodGroupKey, nextObj.id());
1369 // send the innermost group
1370 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001371 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001372 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001373 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l2FloodGroupChainElement);
Yi Tseng78f51f42017-02-02 13:54:58 -08001374
Yi Tsengef19de12017-04-24 11:33:05 -07001375 DeviceId innerMostGroupDevice = groupInfo.innerMostGroupDesc().deviceId();
1376 GroupKey innerMostGroupKey = groupInfo.innerMostGroupDesc().appCookie();
Yi Tseng78f51f42017-02-02 13:54:58 -08001377 Group existsL2IGroup = groupService.getGroup(innerMostGroupDevice, innerMostGroupKey);
1378
1379 if (existsL2IGroup != null) {
1380 // group already exist
1381 processPendingAddGroupsOrNextObjs(innerMostGroupKey, true);
1382 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001383 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001384 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001385 });
Saurav Dasc88d4662017-05-15 15:34:25 -07001386
1387 updatePendingNextObjective(l2floodGroupKey,
1388 new OfdpaNextGroup(addedKeys, nextObj));
Yi Tseng78f51f42017-02-02 13:54:58 -08001389 }
1390
Saurav Das1a129a02016-11-18 15:21:57 -08001391 private void addBucketToL3MulticastGroup(NextObjective nextObj,
1392 List<Deque<GroupKey>> allActiveKeys,
1393 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001394 VlanId assignedVlan) {
Pier Luigi21fffd22018-01-19 10:24:53 +01001395 // Create the buckets to add to the outermost L3 Multicast group
1396 List<GroupBucket> newBuckets = createL3MulticastBucket(groupInfos);
Saurav Das1a129a02016-11-18 15:21:57 -08001397
1398 // get the group being edited
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001399 Group l3mcastGroup = retrieveTopLevelGroup(allActiveKeys, deviceId,
1400 groupService, nextObj.id());
Saurav Das1a129a02016-11-18 15:21:57 -08001401 if (l3mcastGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001402 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001403 return;
1404 }
1405 GroupKey l3mcastGroupKey = l3mcastGroup.appCookie();
1406 int l3mcastGroupId = l3mcastGroup.id().id();
1407
1408 //ensure assignedVlan applies to the chosen group
Yi Tseng78f51f42017-02-02 13:54:58 -08001409 VlanId expectedVlan = extractVlanIdFromGroupId(l3mcastGroupId);
Saurav Das1a129a02016-11-18 15:21:57 -08001410 if (!expectedVlan.equals(assignedVlan)) {
1411 log.warn("VLAN ID {} does not match L3 Mcast group {} to which bucket is "
1412 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1413 Integer.toHexString(l3mcastGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001414 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001415 }
1416 GroupDescription l3mcastGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001417 new DefaultGroupDescription(deviceId,
1418 ALL,
1419 new GroupBuckets(newBuckets),
1420 l3mcastGroupKey,
1421 l3mcastGroupId,
1422 nextObj.appId());
Saurav Das1a129a02016-11-18 15:21:57 -08001423 GroupChainElem l3mcastGce = new GroupChainElem(l3mcastGroupDescription,
Yi Tsengef19de12017-04-24 11:33:05 -07001424 groupInfos.size(),
1425 true,
1426 deviceId);
Saurav Dasc88d4662017-05-15 15:34:25 -07001427
1428 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
Yi Tseng78f51f42017-02-02 13:54:58 -08001429 groupInfos.forEach(groupInfo -> {
1430 // update original NextGroup with new bucket-chain
1431 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001432 newBucketChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001433 // Add L3 interface group to the chain if there is one.
Yi Tsengef19de12017-04-24 11:33:05 -07001434 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1435 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001436 }
1437 newBucketChain.addFirst(l3mcastGroupKey);
Saurav Dasc88d4662017-05-15 15:34:25 -07001438 addedKeys.add(newBucketChain);
Yi Tseng78f51f42017-02-02 13:54:58 -08001439
Yi Tsengef19de12017-04-24 11:33:05 -07001440 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3mcastGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001441 // Point next group to inner-most group, if any
Yi Tsengef19de12017-04-24 11:33:05 -07001442 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1443 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(),
1444 1,
1445 false,
1446 deviceId);
1447 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001448 }
Yi Tsengef19de12017-04-24 11:33:05 -07001449 log.debug("Adding to L3MCAST: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001450 deviceId, Integer.toHexString(l3mcastGroupId),
1451 l3mcastGroupKey, nextObj.id());
1452 // send the innermost group
1453 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001454 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001455 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001456 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001457
1458 });
1459
Saurav Das1a129a02016-11-18 15:21:57 -08001460 updatePendingNextObjective(l3mcastGroupKey,
Saurav Dasc88d4662017-05-15 15:34:25 -07001461 new OfdpaNextGroup(addedKeys, nextObj));
Charles Chan188ebf52015-12-23 00:15:11 -08001462 }
1463
1464 /**
Saurav Dasceccf242017-08-03 18:30:35 -07001465 * Removes buckets in the top level group of a possible group-chain. Does
Saurav Das1a129a02016-11-18 15:21:57 -08001466 * not remove the groups in the group-chain pointed to by this bucket, as they
Charles Chan188ebf52015-12-23 00:15:11 -08001467 * may be in use (referenced by other groups) elsewhere.
1468 *
Saurav Dasceccf242017-08-03 18:30:35 -07001469 * @param nextObjective a next objective that contains information for the
1470 * buckets to be removed from the group
1471 * @param next the representation of the existing group-chains for this next
1472 * objective, from which the top-level buckets to remove are determined
Charles Chan188ebf52015-12-23 00:15:11 -08001473 */
1474 protected void removeBucketFromGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001475 if (nextObjective.type() != NextObjective.Type.HASHED &&
1476 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001477 log.warn("RemoveBuckets not applied to nextType:{} in dev:{} for next:{}",
1478 nextObjective.type(), deviceId, nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -07001479 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001480 return;
1481 }
Yi Tsengef19de12017-04-24 11:33:05 -07001482 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Saurav Dasceccf242017-08-03 18:30:35 -07001483 List<Integer> indicesToRemove = Lists.newArrayList();
1484 for (TrafficTreatment treatment : nextObjective.next()) {
1485 // find the top-level bucket in the group-chain by matching the
1486 // outport and label from different groups in the chain
1487 PortNumber portToRemove = readOutPortFromTreatment(treatment);
1488 int labelToRemove = readLabelFromTreatment(treatment);
1489 if (portToRemove == null) {
1490 log.warn("treatment {} of next objective {} has no outport.. "
1491 + "cannot remove bucket from group in dev: {}", treatment,
1492 nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001493 continue;
1494 }
Saurav Dasceccf242017-08-03 18:30:35 -07001495 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1496 groupService, deviceId,
1497 portToRemove, labelToRemove);
1498 indicesToRemove.addAll(existing);
1499
Charles Chan188ebf52015-12-23 00:15:11 -08001500 }
Saurav Dasceccf242017-08-03 18:30:35 -07001501
1502 List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList();
1503 indicesToRemove.forEach(index -> chainsToRemove
1504 .add(allActiveKeys.get(index)));
Yi Tseng78f51f42017-02-02 13:54:58 -08001505 if (chainsToRemove.isEmpty()) {
Charles Chan188ebf52015-12-23 00:15:11 -08001506 log.warn("Could not find appropriate group-chain for removing bucket"
1507 + " for next id {} in dev:{}", nextObjective.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001508 fail(nextObjective, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001509 return;
Charles Chan188ebf52015-12-23 00:15:11 -08001510 }
Saurav Dasceccf242017-08-03 18:30:35 -07001511 removeBucket(chainsToRemove, nextObjective);
1512 }
1513
1514 /**
1515 * Removes top-level buckets from a group that represents the given next objective.
1516 *
1517 * @param chainsToRemove a list of group bucket chains to remove
1518 * @param nextObjective the next objective that contains information for the
1519 * buckets to be removed from the group
1520 */
1521 protected void removeBucket(List<Deque<GroupKey>> chainsToRemove,
1522 NextObjective nextObjective) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001523 List<GroupBucket> bucketsToRemove = Lists.newArrayList();
1524 //first group key is the one we want to modify
1525 GroupKey modGroupKey = chainsToRemove.get(0).peekFirst();
Saurav Das1a129a02016-11-18 15:21:57 -08001526 Group modGroup = groupService.getGroup(deviceId, modGroupKey);
Ray Milkey6b622cd2018-05-04 13:00:39 -07001527 if (modGroup == null) {
1528 log.warn("removeBucket(): Attempt to modify non-existent group {} for device {}", modGroupKey, deviceId);
1529 return;
1530 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001531 for (Deque<GroupKey> foundChain : chainsToRemove) {
1532 //second group key is the one we wish to remove the reference to
1533 if (foundChain.size() < 2) {
Saurav Dasceccf242017-08-03 18:30:35 -07001534 // additional check to make sure second group key exists in
Yi Tseng78f51f42017-02-02 13:54:58 -08001535 // the chain.
1536 log.warn("Can't find second group key from chain {}",
1537 foundChain);
1538 continue;
Saurav Das1a129a02016-11-18 15:21:57 -08001539 }
Saurav Dasceccf242017-08-03 18:30:35 -07001540 GroupKey pointedGroupKey = foundChain.stream()
1541 .collect(Collectors.toList()).get(1);
Yi Tseng78f51f42017-02-02 13:54:58 -08001542 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
Yi Tseng78f51f42017-02-02 13:54:58 -08001543 if (pointedGroup == null) {
1544 continue;
1545 }
1546
1547 GroupBucket bucket;
1548 if (nextObjective.type() == NextObjective.Type.HASHED) {
1549 bucket = DefaultGroupBucket.createSelectGroupBucket(
1550 DefaultTrafficTreatment.builder()
1551 .group(pointedGroup.id())
1552 .build());
1553 } else {
1554 bucket = DefaultGroupBucket.createAllGroupBucket(
1555 DefaultTrafficTreatment.builder()
1556 .group(pointedGroup.id())
1557 .build());
1558 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001559 bucketsToRemove.add(bucket);
Saurav Das1a129a02016-11-18 15:21:57 -08001560 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001561
1562 GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove);
1563 List<String> pointedGroupIds; // for debug log
1564 pointedGroupIds = bucketsToRemove.stream()
1565 .map(GroupBucket::treatment)
1566 .map(TrafficTreatment::allInstructions)
1567 .flatMap(List::stream)
1568 .filter(inst -> inst instanceof Instructions.GroupInstruction)
1569 .map(inst -> (Instructions.GroupInstruction) inst)
1570 .map(Instructions.GroupInstruction::groupId)
1571 .map(GroupId::id)
1572 .map(Integer::toHexString)
1573 .map(id -> HEX_PREFIX + id)
1574 .collect(Collectors.toList());
1575
Yi Tseng78f51f42017-02-02 13:54:58 -08001576 log.debug("Removing buckets from group id 0x{} pointing to group id(s) {} "
Ray Milkey6b622cd2018-05-04 13:00:39 -07001577 + "for next id {} in device {}",
1578 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 Luigid008dbe2018-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 Sahota5d800282019-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 Hyunf810a7a2018-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 Sahota5d800282019-01-28 01:08:18 +00001699
1700 protected void modifyBucketInL2Group(NextObjective nextObjective, NextGroup next) {
Jonghwan Hyunf810a7a2018-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 Sahota5d800282019-01-28 01:08:18 +00001734
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001735 }
1736
Ruchi Sahota5d800282019-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 Hyunf810a7a2018-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 Luigib72201b2018-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 Chan01d45f72018-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 Luigiec6ac422018-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 Luigiec6ac422018-01-29 10:30:59 +01001828 // Detect situation where the next data has more buckets
1829 // (not duplicates) respect to the next objective
Saurav Dasa4020382018-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 Luigiec6ac422018-01-29 10:30:59 +01001834 log.warn("Mismatch detected between next and flowobjstore for device {}: " +
Saurav Dasa4020382018-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 Luigiec6ac422018-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 Chan01d45f72018-05-08 11:49:05 -07001849 log.trace("Buckets to create {}", bucketsToCreate);
1850 log.trace("Indices to remove {}", indicesToRemove);
Pier Luigib72201b2018-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 Luigib72201b2018-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 Luigib72201b2018-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 Luigib72201b2018-01-25 16:16:02 +01001884 GroupKey topGroupKey = allActiveKeys.get(0).peekFirst();
1885 Group topGroup = groupService.getGroup(deviceId, topGroupKey);
1886 int actualGroupSize = topGroup.buckets().buckets().size();
Saurav Das9df5b7c2017-08-14 16:44:43 -07001887 int objGroupSize = nextObjective.next().size();
1888 if (actualGroupSize != objGroupSize) {
1889 log.warn("Mismatch detected in device:{}, nextId:{}, nextObjective-size"
1890 + ":{} group-size:{} .. correcting", deviceId, nextObjective.id(),
1891 objGroupSize, actualGroupSize);
1892 }
1893 if (actualGroupSize > objGroupSize) {
Pier Luigib72201b2018-01-25 16:16:02 +01001894 // Group in the device has more chains
Saurav Das9df5b7c2017-08-14 16:44:43 -07001895 List<GroupBucket> bucketsToRemove = Lists.newArrayList();
1896 //check every bucket in the actual group
Pier Luigib72201b2018-01-25 16:16:02 +01001897 for (GroupBucket bucket : topGroup.buckets().buckets()) {
Saurav Das9df5b7c2017-08-14 16:44:43 -07001898 GroupInstruction g = (GroupInstruction) bucket.treatment()
1899 .allInstructions().iterator().next();
1900 GroupId gidToCheck = g.groupId(); // the group pointed to
1901 boolean matches = false;
1902 for (Deque<GroupKey> validChain : allActiveKeys) {
1903 if (validChain.size() < 2) {
1904 continue;
1905 }
1906 GroupKey pointedGroupKey = validChain.stream()
1907 .collect(Collectors.toList()).get(1);
1908 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
1909 if (pointedGroup != null && gidToCheck.equals(pointedGroup.id())) {
1910 matches = true;
1911 break;
1912 }
1913 }
1914 if (!matches) {
1915 log.warn("Removing bucket pointing to groupId:{}", gidToCheck);
1916 bucketsToRemove.add(bucket);
1917 }
1918 }
1919 // remove buckets for which there was no record in the obj store
1920 if (bucketsToRemove.isEmpty()) {
1921 log.warn("Mismatch detected but could not determine which"
1922 + "buckets to remove");
1923 } else {
1924 GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove);
Pier Luigib72201b2018-01-25 16:16:02 +01001925 groupService.removeBucketsFromGroup(deviceId, topGroupKey,
1926 removeBuckets, topGroupKey,
Saurav Das9df5b7c2017-08-14 16:44:43 -07001927 nextObjective.appId());
1928 }
1929 } else if (actualGroupSize < objGroupSize) {
Pier Luigib72201b2018-01-25 16:16:02 +01001930 // Group in the device has less chains
Saurav Das9df5b7c2017-08-14 16:44:43 -07001931 // should also add buckets not in group-store but in obj-store
1932 List<GroupBucket> bucketsToAdd = Lists.newArrayList();
1933 //check every bucket in the obj
1934 for (Deque<GroupKey> validChain : allActiveKeys) {
1935 if (validChain.size() < 2) {
1936 continue;
1937 }
1938 GroupKey pointedGroupKey = validChain.stream()
1939 .collect(Collectors.toList()).get(1);
1940 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
1941 if (pointedGroup == null) {
1942 // group should exist, otherwise cannot be added as bucket
1943 continue;
1944 }
1945 boolean matches = false;
Pier Luigib72201b2018-01-25 16:16:02 +01001946 for (GroupBucket bucket : topGroup.buckets().buckets()) {
Saurav Das9df5b7c2017-08-14 16:44:43 -07001947 GroupInstruction g = (GroupInstruction) bucket.treatment()
1948 .allInstructions().iterator().next();
1949 GroupId gidToCheck = g.groupId(); // the group pointed to
1950 if (pointedGroup.id().equals(gidToCheck)) {
1951 matches = true;
1952 break;
1953 }
1954 }
1955 if (!matches) {
1956 log.warn("Adding bucket pointing to groupId:{}", pointedGroup);
1957 TrafficTreatment t = DefaultTrafficTreatment.builder()
1958 .group(pointedGroup.id())
1959 .build();
Pier Luigib72201b2018-01-25 16:16:02 +01001960 // Create the proper bucket according to the next type
1961 if (nextObjective.type() == NextObjective.Type.HASHED) {
1962 bucketsToAdd.add(DefaultGroupBucket.createSelectGroupBucket(t));
1963 } else {
1964 bucketsToAdd.add(DefaultGroupBucket.createAllGroupBucket(t));
1965 }
Saurav Das9df5b7c2017-08-14 16:44:43 -07001966 }
1967 }
1968 if (bucketsToAdd.isEmpty()) {
1969 log.warn("Mismatch detected but could not determine which "
1970 + "buckets to add");
1971 } else {
1972 GroupBuckets addBuckets = new GroupBuckets(bucketsToAdd);
Pier Luigib72201b2018-01-25 16:16:02 +01001973 groupService.addBucketsToGroup(deviceId, topGroupKey,
1974 addBuckets, topGroupKey,
Saurav Das9df5b7c2017-08-14 16:44:43 -07001975 nextObjective.appId());
1976 }
1977 }
1978 }
1979
Saurav Dasceccf242017-08-03 18:30:35 -07001980 pass(nextObjective);
1981 }
1982
1983 //////////////////////////////////////
1984 // Helper methods and classes
1985 //////////////////////////////////////
1986
Yi Tsengef19de12017-04-24 11:33:05 -07001987 protected void updatePendingNextObjective(GroupKey groupKey, OfdpaNextGroup nextGrp) {
1988 pendingAddNextObjectives.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07001989 if (val == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001990 val = new CopyOnWriteArrayList<>();
Saurav Das961beb22017-03-29 19:09:17 -07001991 }
Yi Tsengef19de12017-04-24 11:33:05 -07001992 val.add(nextGrp);
Saurav Das961beb22017-03-29 19:09:17 -07001993 return val;
1994 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08001995 }
1996
Yi Tsengef19de12017-04-24 11:33:05 -07001997 protected void updatePendingGroups(GroupKey groupKey, GroupChainElem gce) {
1998 pendingGroups.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07001999 if (val == null) {
2000 val = Sets.newConcurrentHashSet();
2001 }
2002 val.add(gce);
2003 return val;
2004 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08002005 }
2006
Yi Tsengef19de12017-04-24 11:33:05 -07002007 protected void addPendingUpdateNextObjective(GroupKey groupKey,
2008 NextObjective nextObjective) {
Yi Tseng78f51f42017-02-02 13:54:58 -08002009 pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> {
2010 if (nextObjs != null) {
2011 nextObjs.add(nextObjective);
2012 } else {
2013 nextObjs = Sets.newHashSet(nextObjective);
2014 }
2015 return nextObjs;
2016 });
2017 }
2018
Saurav Das2f2c9d02018-04-07 16:51:09 -07002019 protected void addPendingRemoveNextObjective(NextObjective nextObjective,
2020 List<GroupKey> groupKeys) {
2021 pendingRemoveNextObjectives.put(nextObjective, groupKeys);
Yi Tsengef19de12017-04-24 11:33:05 -07002022 }
2023
Saurav Das2f2c9d02018-04-07 16:51:09 -07002024 protected int getNextAvailableIndex() {
2025 return (int) nextIndex.incrementAndGet();
2026 }
2027
2028 protected void processPendingUpdateNextObjs(GroupKey groupKey) {
2029 Set<NextObjective> nextObjs = pendingUpdateNextObjectives.remove(groupKey);
2030 if (nextObjs != null) {
2031 nextObjs.forEach(nextObj -> {
2032 log.debug("Group {} updated, update pending next objective {}.",
2033 groupKey, nextObj);
2034 pass(nextObj);
2035 });
2036 }
2037 }
2038
2039 protected void processPendingRemoveNextObjs(GroupKey key) {
Yi Tsengef19de12017-04-24 11:33:05 -07002040 pendingRemoveNextObjectives.asMap().forEach((nextObjective, groupKeys) -> {
jaegonkimfeb27172018-07-09 23:23:10 +09002041 groupKeys.remove(key);
Yi Tsengef19de12017-04-24 11:33:05 -07002042 if (groupKeys.isEmpty()) {
2043 pendingRemoveNextObjectives.invalidate(nextObjective);
2044 pass(nextObjective);
Yi Tsengef19de12017-04-24 11:33:05 -07002045 }
2046 });
2047 }
2048
Yi Tsengef19de12017-04-24 11:33:05 -07002049 protected void processPendingAddGroupsOrNextObjs(GroupKey key, boolean added) {
2050 //first check for group chain
2051 Set<OfdpaGroupHandlerUtility.GroupChainElem> gceSet = pendingGroups.asMap().remove(key);
2052 if (gceSet != null) {
2053 for (GroupChainElem gce : gceSet) {
2054 log.debug("Group service {} group key {} in device {}. "
2055 + "Processing next group in group chain with group id 0x{}",
2056 (added) ? "ADDED" : "processed",
2057 key, deviceId,
2058 Integer.toHexString(gce.groupDescription().givenGroupId()));
2059 processGroupChain(gce);
2060 }
2061 } else {
2062 // otherwise chain complete - check for waiting nextObjectives
2063 List<OfdpaGroupHandlerUtility.OfdpaNextGroup> nextGrpList =
2064 pendingAddNextObjectives.getIfPresent(key);
2065 if (nextGrpList != null) {
2066 pendingAddNextObjectives.invalidate(key);
2067 nextGrpList.forEach(nextGrp -> {
2068 log.debug("Group service {} group key {} in device:{}. "
2069 + "Done implementing next objective: {} <<-->> gid:0x{}",
2070 (added) ? "ADDED" : "processed",
2071 key, deviceId, nextGrp.nextObjective().id(),
2072 Integer.toHexString(groupService.getGroup(deviceId, key)
2073 .givenGroupId()));
2074 pass(nextGrp.nextObjective());
Saurav Dasc88d4662017-05-15 15:34:25 -07002075 updateFlowObjectiveStore(nextGrp.nextObjective().id(), nextGrp);
Yi Tsengef19de12017-04-24 11:33:05 -07002076
2077 // check if addBuckets waiting for this completion
2078 pendingBuckets.compute(nextGrp.nextObjective().id(), (nextId, pendBkts) -> {
2079 if (pendBkts != null) {
2080 pendBkts.forEach(pendBkt -> addBucketToGroup(pendBkt, nextGrp));
2081 }
2082 return null;
2083 });
2084 });
2085 }
2086 }
2087 }
2088
Charles Chan188ebf52015-12-23 00:15:11 -08002089 /**
2090 * Processes next element of a group chain. Assumption is that if this
2091 * group points to another group, the latter has already been created
2092 * and this driver has received notification for it. A second assumption is
2093 * that if there is another group waiting for this group then the appropriate
2094 * stores already have the information to act upon the notification for the
2095 * creation of this group.
2096 * <p>
2097 * The processing of the GroupChainElement depends on the number of groups
2098 * this element is waiting on. For all group types other than SIMPLE, a
2099 * GroupChainElement could be waiting on multiple groups.
2100 *
2101 * @param gce the group chain element to be processed next
2102 */
2103 private void processGroupChain(GroupChainElem gce) {
2104 int waitOnGroups = gce.decrementAndGetGroupsWaitedOn();
2105 if (waitOnGroups != 0) {
2106 log.debug("GCE: {} not ready to be processed", gce);
2107 return;
2108 }
2109 log.debug("GCE: {} ready to be processed", gce);
Yi Tsengef19de12017-04-24 11:33:05 -07002110 if (gce.addBucketToGroup()) {
2111 groupService.addBucketsToGroup(gce.groupDescription().deviceId(),
2112 gce.groupDescription().appCookie(),
2113 gce.groupDescription().buckets(),
2114 gce.groupDescription().appCookie(),
2115 gce.groupDescription().appId());
Charles Chan188ebf52015-12-23 00:15:11 -08002116 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07002117 groupService.addGroup(gce.groupDescription());
Charles Chan188ebf52015-12-23 00:15:11 -08002118 }
2119 }
2120
Saurav Dasc88d4662017-05-15 15:34:25 -07002121 private void updateFlowObjectiveStore(Integer nextId, OfdpaNextGroup nextGrp) {
2122 synchronized (flowObjectiveStore) {
2123 // get fresh copy of what the store holds
2124 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
2125 if (next == null || nextGrp.nextObjective().op() == Operation.ADD) {
2126 flowObjectiveStore.putNextGroup(nextId, nextGrp);
2127 return;
2128 }
2129 if (nextGrp.nextObjective().op() == Operation.ADD_TO_EXISTING) {
2130 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Pier Luigid008dbe2018-01-15 09:48:49 +01002131 allActiveKeys = Lists.newArrayList(allActiveKeys);
Saurav Dasc88d4662017-05-15 15:34:25 -07002132 // If active keys shows only the top-level group without a chain of groups,
2133 // then it represents an empty group. Update by replacing empty chain.
2134 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
2135 allActiveKeys.clear();
2136 }
2137 allActiveKeys.addAll(nextGrp.allKeys());
2138 flowObjectiveStore.putNextGroup(nextId,
2139 new OfdpaNextGroup(allActiveKeys, nextGrp.nextObjective()));
2140 }
2141 }
2142 }
2143
Saurav Das8be4e3a2016-03-11 17:19:07 -08002144 private class InnerGroupListener implements GroupListener {
2145 @Override
2146 public void event(GroupEvent event) {
Charles Chanfc5c7802016-05-17 13:13:55 -07002147 switch (event.type()) {
2148 case GROUP_ADDED:
2149 processPendingAddGroupsOrNextObjs(event.subject().appCookie(), true);
2150 break;
2151 case GROUP_REMOVED:
2152 processPendingRemoveNextObjs(event.subject().appCookie());
2153 break;
Yi Tseng78f51f42017-02-02 13:54:58 -08002154 case GROUP_UPDATED:
2155 processPendingUpdateNextObjs(event.subject().appCookie());
2156 break;
Charles Chanfc5c7802016-05-17 13:13:55 -07002157 default:
2158 break;
Saurav Das8be4e3a2016-03-11 17:19:07 -08002159 }
2160 }
2161 }
Charles Chan188ebf52015-12-23 00:15:11 -08002162}