blob: 260254f8b6ff6081c2a1ca4f6460ab7bef422bf5 [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 Chan367c1c12018-10-19 16:23:28 -070032import org.onosproject.driver.extensions.Ofdpa3AllowVlanTranslationType;
33import org.onosproject.driver.extensions.OfdpaSetAllowVlanTranslation;
Charles Chan32562522016-04-07 14:37:14 -070034import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Charles Chan188ebf52015-12-23 00:15:11 -080035import org.onosproject.net.DeviceId;
36import org.onosproject.net.PortNumber;
37import org.onosproject.net.behaviour.NextGroup;
38import org.onosproject.net.behaviour.PipelinerContext;
39import org.onosproject.net.flow.DefaultTrafficTreatment;
40import org.onosproject.net.flow.TrafficSelector;
41import org.onosproject.net.flow.TrafficTreatment;
42import org.onosproject.net.flow.criteria.Criterion;
Charles Chan367c1c12018-10-19 16:23:28 -070043import org.onosproject.net.flow.criteria.PortCriterion;
Pier Ventre42287df2016-11-09 14:17:26 -080044import org.onosproject.net.flow.criteria.TunnelIdCriterion;
Charles Chan188ebf52015-12-23 00:15:11 -080045import org.onosproject.net.flow.criteria.VlanIdCriterion;
46import org.onosproject.net.flow.instructions.Instruction;
47import org.onosproject.net.flow.instructions.Instructions;
Saurav Das9df5b7c2017-08-14 16:44:43 -070048import org.onosproject.net.flow.instructions.Instructions.GroupInstruction;
Charles Chan188ebf52015-12-23 00:15:11 -080049import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Yi Tseng47f82dc2017-03-05 22:48:39 -080050import org.onosproject.net.flowobjective.DefaultNextObjective;
Charles Chan367c1c12018-10-19 16:23:28 -070051import org.onosproject.net.flowobjective.DefaultNextTreatment;
Charles Chan188ebf52015-12-23 00:15:11 -080052import org.onosproject.net.flowobjective.FlowObjectiveStore;
Charles Chan367c1c12018-10-19 16:23:28 -070053import org.onosproject.net.flowobjective.IdNextTreatment;
Charles Chan188ebf52015-12-23 00:15:11 -080054import org.onosproject.net.flowobjective.NextObjective;
Charles Chan367c1c12018-10-19 16:23:28 -070055import org.onosproject.net.flowobjective.NextTreatment;
Saurav Dasc88d4662017-05-15 15:34:25 -070056import org.onosproject.net.flowobjective.Objective.Operation;
Yi Tseng47f82dc2017-03-05 22:48:39 -080057import org.onosproject.net.flowobjective.ObjectiveContext;
Charles Chan188ebf52015-12-23 00:15:11 -080058import org.onosproject.net.flowobjective.ObjectiveError;
59import org.onosproject.net.group.DefaultGroupBucket;
60import org.onosproject.net.group.DefaultGroupDescription;
61import org.onosproject.net.group.DefaultGroupKey;
62import org.onosproject.net.group.Group;
63import org.onosproject.net.group.GroupBucket;
64import org.onosproject.net.group.GroupBuckets;
65import org.onosproject.net.group.GroupDescription;
66import org.onosproject.net.group.GroupEvent;
67import org.onosproject.net.group.GroupKey;
68import org.onosproject.net.group.GroupListener;
69import org.onosproject.net.group.GroupService;
Saurav Das8be4e3a2016-03-11 17:19:07 -080070import org.onosproject.store.service.AtomicCounter;
71import org.onosproject.store.service.StorageService;
Charles Chan188ebf52015-12-23 00:15:11 -080072import org.slf4j.Logger;
73
74import java.util.ArrayDeque;
75import java.util.ArrayList;
Saurav Dasceccf242017-08-03 18:30:35 -070076import java.util.Arrays;
Charles Chan188ebf52015-12-23 00:15:11 -080077import java.util.Collection;
78import java.util.Collections;
79import java.util.Deque;
80import java.util.List;
Charles Chand0fd5dc2016-02-16 23:14:49 -080081import java.util.Objects;
Charles Chan367c1c12018-10-19 16:23:28 -070082import java.util.Optional;
Charles Chan188ebf52015-12-23 00:15:11 -080083import java.util.Set;
84import java.util.concurrent.ConcurrentHashMap;
85import java.util.concurrent.CopyOnWriteArrayList;
86import java.util.concurrent.Executors;
87import java.util.concurrent.ScheduledExecutorService;
88import java.util.concurrent.TimeUnit;
Charles Chan188ebf52015-12-23 00:15:11 -080089import java.util.stream.Collectors;
90
91import static org.onlab.util.Tools.groupedThreads;
Yi Tsengef19de12017-04-24 11:33:05 -070092import static org.onosproject.driver.pipeline.ofdpa.Ofdpa2Pipeline.*;
93import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
jayakumarthazhath655b9a82018-10-01 00:51:54 +053094import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.L2_MULTICAST_TYPE;
95import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.l2MulticastGroupKey;
Charles Chan367c1c12018-10-19 16:23:28 -070096import static org.onosproject.net.flow.criteria.Criterion.Type.IN_PORT;
Pier Ventre42287df2016-11-09 14:17:26 -080097import static org.onosproject.net.flow.criteria.Criterion.Type.TUNNEL_ID;
Pier Ventre140a8942016-11-02 07:26:38 -070098import static org.onosproject.net.flow.criteria.Criterion.Type.VLAN_VID;
Yi Tseng78f51f42017-02-02 13:54:58 -080099import static org.onosproject.net.group.GroupDescription.Type.ALL;
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800100import static org.onosproject.net.group.GroupDescription.Type.INDIRECT;
Yi Tseng78f51f42017-02-02 13:54:58 -0800101import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Charles Chan188ebf52015-12-23 00:15:11 -0800102import static org.slf4j.LoggerFactory.getLogger;
103
104/**
Saurav Das961beb22017-03-29 19:09:17 -0700105 * Group handler that emulates Broadcom OF-DPA TTP.
Charles Chan188ebf52015-12-23 00:15:11 -0800106 */
Charles Chan361154b2016-03-24 10:23:39 -0700107public class Ofdpa2GroupHandler {
Yi Tsengef19de12017-04-24 11:33:05 -0700108 protected final Logger log = getLogger(getClass());
Yi Tsengef19de12017-04-24 11:33:05 -0700109 // Services, Stores
Charles Chan188ebf52015-12-23 00:15:11 -0800110 protected GroupService groupService;
Saurav Das8be4e3a2016-03-11 17:19:07 -0800111 protected StorageService storageService;
Yi Tsengef19de12017-04-24 11:33:05 -0700112 protected FlowObjectiveStore flowObjectiveStore;
Charles Chan188ebf52015-12-23 00:15:11 -0800113 // index number for group creation
Saurav Das8be4e3a2016-03-11 17:19:07 -0800114 private AtomicCounter nextIndex;
Yi Tsengef19de12017-04-24 11:33:05 -0700115 protected DeviceId deviceId;
Saurav Das2f2c9d02018-04-07 16:51:09 -0700116 Cache<GroupKey, List<OfdpaGroupHandlerUtility.OfdpaNextGroup>> pendingAddNextObjectives;
117 Cache<NextObjective, List<GroupKey>> pendingRemoveNextObjectives;
118 Cache<GroupKey, Set<OfdpaGroupHandlerUtility.GroupChainElem>> pendingGroups;
119 ConcurrentHashMap<GroupKey, Set<NextObjective>> pendingUpdateNextObjectives;
Yi Tseng47f82dc2017-03-05 22:48:39 -0800120 // local store for pending bucketAdds - by design there can be multiple
Charles Chan188ebf52015-12-23 00:15:11 -0800121 // pending bucket for a group
Yi Tseng47f82dc2017-03-05 22:48:39 -0800122 protected ConcurrentHashMap<Integer, Set<NextObjective>> pendingBuckets =
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700123 new ConcurrentHashMap<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700124 private ScheduledExecutorService groupCheckerExecutor =
125 Executors.newScheduledThreadPool(2, groupedThreads("onos/pipeliner", "ofdpa-%d", log));
Charles Chan40132b32017-01-22 00:19:37 -0800126 /**
127 * Determines whether this pipeline support copy ttl instructions or not.
128 *
129 * @return true if copy ttl instructions are supported
130 */
131 protected boolean supportCopyTtl() {
132 return true;
133 }
134
135 /**
136 * Determines whether this pipeline support set mpls bos instruction or not.
137 *
138 * @return true if set mpls bos instruction is supported
139 */
140 protected boolean supportSetMplsBos() {
141 return true;
142 }
143
Charles Chan0f43e472017-02-14 14:00:16 -0800144 /**
145 * Determines whether this pipeline requires popping VLAN before pushing MPLS.
146 * <p>
147 * If required, pop vlan before push mpls and add an arbitrary vlan back afterward.
148 * MPLS interface group will substitute the arbitrary VLAN with expected VLAN later on.
149 *
150 * @return true if this pipeline requires popping VLAN before pushing MPLS
151 */
152 protected boolean requireVlanPopBeforeMplsPush() {
153 return false;
154 }
155
Charles Chan367c1c12018-10-19 16:23:28 -0700156 /**
157 * Determines whether this pipeline requires AllowVlanTransition in L2 unfiltered group.
158 *
159 * @return true if the AllowVlanTransition action is required
160 */
161 protected boolean requireAllowVlanTransition() {
162 return true;
163 }
164
Alex Yashchuk4caa8e82017-12-08 17:40:05 +0200165 public void init(DeviceId deviceId, PipelinerContext context) {
Yi Tsengef19de12017-04-24 11:33:05 -0700166 ServiceDirectory serviceDirectory = context.directory();
Charles Chan188ebf52015-12-23 00:15:11 -0800167 this.deviceId = deviceId;
168 this.flowObjectiveStore = context.store();
Charles Chan188ebf52015-12-23 00:15:11 -0800169 this.groupService = serviceDirectory.get(GroupService.class);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800170 this.storageService = serviceDirectory.get(StorageService.class);
Madan Jampanid5714e02016-04-19 14:15:20 -0700171 this.nextIndex = storageService.getAtomicCounter("group-id-index-counter");
Charles Chan188ebf52015-12-23 00:15:11 -0800172
Charles Chanfc5c7802016-05-17 13:13:55 -0700173 pendingAddNextObjectives = CacheBuilder.newBuilder()
Charles Chan188ebf52015-12-23 00:15:11 -0800174 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700175 .removalListener((RemovalNotification<GroupKey, List<OfdpaNextGroup>> notification) -> {
176 if (notification.getCause() == RemovalCause.EXPIRED &&
177 Objects.nonNull(notification.getValue())) {
178 notification.getValue()
179 .forEach(ofdpaNextGrp ->
180 fail(ofdpaNextGrp.nextObjective(),
181 ObjectiveError.GROUPINSTALLATIONFAILED));
Charles Chanfc5c7802016-05-17 13:13:55 -0700182 }
183 }).build();
Charles Chan188ebf52015-12-23 00:15:11 -0800184
Charles Chanfc5c7802016-05-17 13:13:55 -0700185 pendingRemoveNextObjectives = CacheBuilder.newBuilder()
186 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700187 .removalListener((RemovalNotification<NextObjective, List<GroupKey>> notification) -> {
Charles Chanfc5c7802016-05-17 13:13:55 -0700188 if (notification.getCause() == RemovalCause.EXPIRED) {
Yi Tsengef19de12017-04-24 11:33:05 -0700189 fail(notification.getKey(),
190 ObjectiveError.GROUPREMOVALFAILED);
Charles Chan188ebf52015-12-23 00:15:11 -0800191 }
192 }).build();
Saurav Das961beb22017-03-29 19:09:17 -0700193 pendingGroups = CacheBuilder.newBuilder()
194 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700195 .removalListener((RemovalNotification<GroupKey, Set<GroupChainElem>> notification) -> {
Saurav Das961beb22017-03-29 19:09:17 -0700196 if (notification.getCause() == RemovalCause.EXPIRED) {
197 log.error("Unable to install group with key {} and pending GCEs: {}",
198 notification.getKey(), notification.getValue());
199 }
200 }).build();
Yi Tseng78f51f42017-02-02 13:54:58 -0800201 pendingUpdateNextObjectives = new ConcurrentHashMap<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700202 GroupChecker groupChecker = new GroupChecker(this);
203 groupCheckerExecutor.scheduleAtFixedRate(groupChecker, 0, 500, TimeUnit.MILLISECONDS);
Charles Chan188ebf52015-12-23 00:15:11 -0800204 groupService.addListener(new InnerGroupListener());
205 }
206
Saurav Das8be4e3a2016-03-11 17:19:07 -0800207 //////////////////////////////////////
208 // Group Creation
209 //////////////////////////////////////
210
Yi Tsengef19de12017-04-24 11:33:05 -0700211 /**
212 * Adds a list of group chain by given NextObjective.
213 *
214 * @param nextObjective the NextObjective
215 */
Charles Chan188ebf52015-12-23 00:15:11 -0800216 protected void addGroup(NextObjective nextObjective) {
217 switch (nextObjective.type()) {
218 case SIMPLE:
219 Collection<TrafficTreatment> treatments = nextObjective.next();
220 if (treatments.size() != 1) {
221 log.error("Next Objectives of type Simple should only have a "
222 + "single Traffic Treatment. Next Objective Id:{}",
223 nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -0700224 fail(nextObjective, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800225 return;
226 }
227 processSimpleNextObjective(nextObjective);
228 break;
229 case BROADCAST:
230 processBroadcastNextObjective(nextObjective);
231 break;
232 case HASHED:
Pier Ventre140a8942016-11-02 07:26:38 -0700233 if (!verifyHashedNextObjective(nextObjective)) {
234 log.error("Next Objectives of type hashed not supported. Next Objective Id:{}",
235 nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -0700236 fail(nextObjective, ObjectiveError.BADPARAMS);
Pier Ventre140a8942016-11-02 07:26:38 -0700237 return;
238 }
Charles Chan367c1c12018-10-19 16:23:28 -0700239 if (isL2Hash(nextObjective)) {
240 processL2HashedNextObjective(nextObjective);
241 return;
242 }
243 processEcmpHashedNextObjective(nextObjective);
Charles Chan188ebf52015-12-23 00:15:11 -0800244 break;
245 case FAILOVER:
Yi Tsengef19de12017-04-24 11:33:05 -0700246 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -0800247 log.warn("Unsupported next objective type {}", nextObjective.type());
248 break;
249 default:
Yi Tsengef19de12017-04-24 11:33:05 -0700250 fail(nextObjective, ObjectiveError.UNKNOWN);
Charles Chan188ebf52015-12-23 00:15:11 -0800251 log.warn("Unknown next objective type {}", nextObjective.type());
252 }
253 }
254
255 /**
jayakumarthazhath655b9a82018-10-01 00:51:54 +0530256 * Similar to processBroadcastNextObjective but handles L2 Multicast Next Objectives.
257 *
258 * @param nextObj NextObjective of L2_MULTICAST with chained NextObjectives for single homed access ports
259 */
260 private void processL2MulticastNextObjective(NextObjective nextObj) {
261
262 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
263 if (assignedVlan == null) {
264 log.warn("VLAN ID required by L2 multicast next objective is missing. Aborting group creation.");
265 fail(nextObj, ObjectiveError.BADPARAMS);
266 return;
267 }
268
269 // Group info should contain only single homed hosts for a given vlanId
270 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
271 createL2MulticastGroup(nextObj, assignedVlan, groupInfos);
272 }
273
274 private void createL2MulticastGroup(NextObjective nextObj, VlanId vlanId, List<GroupInfo> groupInfos) {
275 // Realize & represent L2 multicast group in OFDPA driver layer
276 // TODO : Need to identify significance of OfdpaNextGroup.
277 Integer l2MulticastGroupId = L2_MULTICAST_TYPE | (vlanId.toShort() << 16);
278 final GroupKey l2MulticastGroupKey = l2MulticastGroupKey(vlanId, deviceId);
279 List<Deque<GroupKey>> l2MulticastAllGroup = Lists.newArrayList();
280 groupInfos.forEach(groupInfo -> {
281 Deque<GroupKey> groupKeyChain = new ArrayDeque<>();
282 groupKeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
283 groupKeyChain.addFirst(l2MulticastGroupKey);
284 l2MulticastAllGroup.add(groupKeyChain);
285 });
286 OfdpaNextGroup ofdpaL2MulticastGroup = new OfdpaNextGroup(l2MulticastAllGroup, nextObj);
287 updatePendingNextObjective(l2MulticastGroupKey, ofdpaL2MulticastGroup);
288 // Group Chain Hierarchy creation using group service and thus in device level
289 List<GroupBucket> l2McastBuckets = new ArrayList<>();
290 groupInfos.forEach(groupInfo -> {
291 // Points to L2 interface group directly.
292 TrafficTreatment.Builder trafficTreatment = DefaultTrafficTreatment.builder();
293 trafficTreatment.group(new GroupId(groupInfo.innerMostGroupDesc().givenGroupId()));
294 GroupBucket bucket = DefaultGroupBucket.createAllGroupBucket(trafficTreatment.build());
295 l2McastBuckets.add(bucket);
296 });
297 GroupDescription l2MulticastGroupDescription =
298 new DefaultGroupDescription(
299 deviceId,
300 ALL,
301 new GroupBuckets(l2McastBuckets),
302 l2MulticastGroupKey,
303 l2MulticastGroupId,
304 nextObj.appId());
305 GroupChainElem l2MulticastGce = new GroupChainElem(l2MulticastGroupDescription,
306 groupInfos.size(), false, deviceId);
307 groupInfos.forEach(groupInfo -> {
308 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), l2MulticastGce);
309 groupService.addGroup(groupInfo.innerMostGroupDesc());
310 });
311 }
312
313 /**
Charles Chan188ebf52015-12-23 00:15:11 -0800314 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
Saurav Dasa4020382018-02-14 14:14:54 -0800315 * a chain of groups. The simple Next Objective passed in by the application
316 * is broken up into a group chain. The following chains can be created
317 * depending on the parameters in the Next Objective.
318 * 1. L2 Interface group (no chaining)
319 * 2. L3 Unicast group -> L2 Interface group
320 * 3. MPLS Interface group -> L2 Interface group
321 * 4. MPLS Swap group -> MPLS Interface group -> L2 Interface group
322 * 5. PW initiation group chain
Charles Chan188ebf52015-12-23 00:15:11 -0800323 *
324 * @param nextObj the nextObjective of type SIMPLE
325 */
326 private void processSimpleNextObjective(NextObjective nextObj) {
327 TrafficTreatment treatment = nextObj.next().iterator().next();
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800328 // determine if plain L2 or L3->L2 or MPLS Swap -> MPLS Interface -> L2
Charles Chan188ebf52015-12-23 00:15:11 -0800329 boolean plainL2 = true;
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800330 boolean mplsSwap = false;
331 MplsLabel mplsLabel = null;
Charles Chan188ebf52015-12-23 00:15:11 -0800332 for (Instruction ins : treatment.allInstructions()) {
333 if (ins.type() == Instruction.Type.L2MODIFICATION) {
334 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
335 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_DST ||
336 l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_SRC) {
337 plainL2 = false;
Charles Chan188ebf52015-12-23 00:15:11 -0800338 }
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800339 // mpls label in simple next objectives is used only to indicate
340 // a MPLS Swap group before the MPLS Interface Group
341 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) {
342 mplsSwap = true;
343 mplsLabel = ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label();
344 }
Charles Chan188ebf52015-12-23 00:15:11 -0800345 }
346 }
Charles Chan188ebf52015-12-23 00:15:11 -0800347 if (plainL2) {
348 createL2InterfaceGroup(nextObj);
349 return;
350 }
Saurav Dasa4020382018-02-14 14:14:54 -0800351 // In order to understand if it is a pseudowire related
Pier Ventre42287df2016-11-09 14:17:26 -0800352 // next objective we look for the tunnel id in the meta.
353 boolean isPw = false;
Pier Ventre140a8942016-11-02 07:26:38 -0700354 if (nextObj.meta() != null) {
Pier Ventre42287df2016-11-09 14:17:26 -0800355 TunnelIdCriterion tunnelIdCriterion = (TunnelIdCriterion) nextObj
356 .meta()
357 .getCriterion(TUNNEL_ID);
358 if (tunnelIdCriterion != null) {
359 isPw = true;
360 }
Pier Ventre140a8942016-11-02 07:26:38 -0700361 }
Andreas Pantelopoulos25db42f2018-02-08 12:42:06 -0800362 if (mplsSwap && !isPw) {
Saurav Dasa4020382018-02-14 14:14:54 -0800363 log.debug("Creating a MPLS Swap -> MPLS Interface -> L2 Interface group chain.");
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800364 // break up simple next objective to GroupChain objects
365 GroupInfo groupInfo = createL2L3Chain(treatment, nextObj.id(),
366 nextObj.appId(), true,
367 nextObj.meta());
368 if (groupInfo == null) {
369 log.error("Could not process nextObj={} in dev:{}", nextObj.id(), deviceId);
370 fail(nextObj, ObjectiveError.BADPARAMS);
371 return;
372 }
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800373 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Saurav Dasa4020382018-02-14 14:14:54 -0800374 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie()); // l2 interface
375 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie()); // mpls interface
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800376 // creating the mpls swap group and adding it to the chain
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800377 int nextGid = groupInfo.nextGroupDesc().givenGroupId();
378 int index = getNextAvailableIndex();
Saurav Dasa4020382018-02-14 14:14:54 -0800379 GroupDescription swapGroupDescription = createMplsSwap(
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800380 nextGid,
381 OfdpaMplsGroupSubType.MPLS_SWAP_LABEL,
382 index,
383 mplsLabel,
384 nextObj.appId()
385 );
Saurav Dasa4020382018-02-14 14:14:54 -0800386 // ensure swap group is added after L2L3 chain
387 GroupKey swapGroupKey = swapGroupDescription.appCookie();
388 GroupChainElem swapChainElem = new GroupChainElem(swapGroupDescription,
389 1, false, deviceId);
390 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), swapChainElem);
391 gkeyChain.addFirst(swapGroupKey);
Saurav Dasa4020382018-02-14 14:14:54 -0800392 // ensure nextObjective waits on the outermost groupKey
393 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
394 allGroupKeys.add(gkeyChain);
395 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
396 updatePendingNextObjective(swapGroupKey, ofdpaGrp);
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800397 // now we are ready to send the l2 groupDescription (inner), as all the stores
398 // that will get async replies have been updated. By waiting to update
399 // the stores, we prevent nasty race conditions.
400 groupService.addGroup(groupInfo.innerMostGroupDesc());
401 } else if (!isPw) {
Saurav Dasa4020382018-02-14 14:14:54 -0800402 boolean isMpls = false;
403 if (nextObj.meta() != null) {
404 isMpls = isNotMplsBos(nextObj.meta());
405 }
406 log.debug("Creating a {} -> L2 Interface group chain.",
407 (isMpls) ? "MPLS Interface" : "L3 Unicast");
Pier Ventre42287df2016-11-09 14:17:26 -0800408 // break up simple next objective to GroupChain objects
409 GroupInfo groupInfo = createL2L3Chain(treatment, nextObj.id(),
410 nextObj.appId(), isMpls,
411 nextObj.meta());
412 if (groupInfo == null) {
413 log.error("Could not process nextObj={} in dev:{}", nextObj.id(), deviceId);
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800414 fail(nextObj, ObjectiveError.BADPARAMS);
Pier Ventre42287df2016-11-09 14:17:26 -0800415 return;
416 }
417 // create object for local and distributed storage
418 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700419 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
420 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Saurav Dasa4020382018-02-14 14:14:54 -0800421 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
422 allGroupKeys.add(gkeyChain);
423 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Pier Ventre42287df2016-11-09 14:17:26 -0800424 // store l3groupkey with the ofdpaNextGroup for the nextObjective that depends on it
Yi Tsengef19de12017-04-24 11:33:05 -0700425 updatePendingNextObjective(groupInfo.nextGroupDesc().appCookie(), ofdpaGrp);
Pier Ventre42287df2016-11-09 14:17:26 -0800426 // now we are ready to send the l2 groupDescription (inner), as all the stores
427 // that will get async replies have been updated. By waiting to update
428 // the stores, we prevent nasty race conditions.
Yi Tsengef19de12017-04-24 11:33:05 -0700429 groupService.addGroup(groupInfo.innerMostGroupDesc());
Pier Ventre42287df2016-11-09 14:17:26 -0800430 } else {
431 // We handle the pseudo wire with a different a procedure.
432 // This procedure is meant to handle both initiation and
433 // termination of the pseudo wire.
434 processPwNextObjective(nextObj);
Charles Chan188ebf52015-12-23 00:15:11 -0800435 }
Charles Chan188ebf52015-12-23 00:15:11 -0800436 }
437
Charles Chan188ebf52015-12-23 00:15:11 -0800438 /**
439 * Creates a simple L2 Interface Group.
Charles Chan188ebf52015-12-23 00:15:11 -0800440 * @param nextObj the next Objective
441 */
442 private void createL2InterfaceGroup(NextObjective nextObj) {
Yi Tsengef19de12017-04-24 11:33:05 -0700443 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700444 if (assignedVlan == null) {
445 log.warn("VLAN ID required by simple next obj is missing. Abort.");
Yi Tsengef19de12017-04-24 11:33:05 -0700446 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800447 return;
448 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700449 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700450 // There is only one L2 interface group in this case
Yi Tsengef19de12017-04-24 11:33:05 -0700451 GroupDescription l2InterfaceGroupDesc = groupInfos.get(0).innerMostGroupDesc();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700452 // Put all dependency information into allGroupKeys
453 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
454 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
455 gkeyChain.addFirst(l2InterfaceGroupDesc.appCookie());
456 allGroupKeys.add(gkeyChain);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700457 // Point the next objective to this group
458 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
459 updatePendingNextObjective(l2InterfaceGroupDesc.appCookie(), ofdpaGrp);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700460 // Start installing the inner-most group
jayakumarthazhath655b9a82018-10-01 00:51:54 +0530461 groupService.addGroup(l2InterfaceGroupDesc); }
Charles Chan188ebf52015-12-23 00:15:11 -0800462
463 /**
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800464 * Creates an Mpls group of type swap.
465 *
466 * @param nextGroupId the next group in the chain
467 * @param subtype the mpls swap label group subtype
468 * @param index the index of the group
469 * @param mplsLabel the mpls label to swap
470 * @param applicationId the application id
471 * @return the group description
472 */
473 protected GroupDescription createMplsSwap(int nextGroupId,
474 OfdpaMplsGroupSubType subtype,
475 int index,
476 MplsLabel mplsLabel,
477 ApplicationId applicationId) {
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800478 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800479 treatment.setMpls(mplsLabel);
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800480 // We point the group to the next group.
481 treatment.group(new GroupId(nextGroupId));
482 GroupBucket groupBucket = DefaultGroupBucket
483 .createIndirectGroupBucket(treatment.build());
484 // Finally we build the group description.
485 int groupId = makeMplsLabelGroupId(subtype, index);
486 GroupKey groupKey = new DefaultGroupKey(
Saurav Dasa4020382018-02-14 14:14:54 -0800487 Ofdpa2Pipeline.appKryo.serialize(index));
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800488 return new DefaultGroupDescription(
489 deviceId,
490 INDIRECT,
491 new GroupBuckets(Collections.singletonList(groupBucket)),
492 groupKey,
493 groupId,
Saurav Dasa4020382018-02-14 14:14:54 -0800494 applicationId);
Andreas Pantelopoulos7a8488c2018-01-22 16:00:28 -0800495 }
496
497 /**
Charles Chan188ebf52015-12-23 00:15:11 -0800498 * Creates one of two possible group-chains from the treatment
499 * passed in. Depending on the MPLS boolean, this method either creates
Charles Chan425854b2016-04-11 15:32:12 -0700500 * an L3Unicast Group --&gt; L2Interface Group, if mpls is false;
501 * or MPLSInterface Group --&gt; L2Interface Group, if mpls is true;
Charles Chan188ebf52015-12-23 00:15:11 -0800502 * The returned 'inner' group description is always the L2 Interface group.
503 *
504 * @param treatment that needs to be broken up to create the group chain
505 * @param nextId of the next objective that needs this group chain
506 * @param appId of the application that sent this next objective
507 * @param mpls determines if L3Unicast or MPLSInterface group is created
508 * @param meta metadata passed in by the application as part of the nextObjective
509 * @return GroupInfo containing the GroupDescription of the
510 * L2Interface group(inner) and the GroupDescription of the (outer)
511 * L3Unicast/MPLSInterface group. May return null if there is an
512 * error in processing the chain
513 */
Charles Chan425854b2016-04-11 15:32:12 -0700514 protected GroupInfo createL2L3Chain(TrafficTreatment treatment, int nextId,
Charles Chanf9e98652016-09-07 16:54:23 -0700515 ApplicationId appId, boolean mpls,
516 TrafficSelector meta) {
517 return createL2L3ChainInternal(treatment, nextId, appId, mpls, meta, true);
518 }
519
520 /**
521 * Internal implementation of createL2L3Chain.
522 * <p>
523 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
524 * Since it is non-OF spec, we need an extension treatment for that.
525 * The useSetVlanExtension must be set to false for OFDPA i12.
526 * </p>
527 *
528 * @param treatment that needs to be broken up to create the group chain
529 * @param nextId of the next objective that needs this group chain
530 * @param appId of the application that sent this next objective
531 * @param mpls determines if L3Unicast or MPLSInterface group is created
532 * @param meta metadata passed in by the application as part of the nextObjective
533 * @param useSetVlanExtension use the setVlanVid extension that has is_present bit set to 0.
534 * @return GroupInfo containing the GroupDescription of the
535 * L2Interface group(inner) and the GroupDescription of the (outer)
536 * L3Unicast/MPLSInterface group. May return null if there is an
537 * error in processing the chain
538 */
539 protected GroupInfo createL2L3ChainInternal(TrafficTreatment treatment, int nextId,
Pier Ventre42287df2016-11-09 14:17:26 -0800540 ApplicationId appId, boolean mpls,
541 TrafficSelector meta, boolean useSetVlanExtension) {
Charles Chan188ebf52015-12-23 00:15:11 -0800542 // for the l2interface group, get vlan and port info
543 // for the outer group, get the src/dst mac, and vlan info
544 TrafficTreatment.Builder outerTtb = DefaultTrafficTreatment.builder();
545 TrafficTreatment.Builder innerTtb = DefaultTrafficTreatment.builder();
546 VlanId vlanid = null;
547 long portNum = 0;
548 boolean setVlan = false, popVlan = false;
Yi Tseng78f51f42017-02-02 13:54:58 -0800549 MacAddress srcMac;
550 MacAddress dstMac;
Charles Chan188ebf52015-12-23 00:15:11 -0800551 for (Instruction ins : treatment.allInstructions()) {
552 if (ins.type() == Instruction.Type.L2MODIFICATION) {
553 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
554 switch (l2ins.subtype()) {
555 case ETH_DST:
Charles Chan5270ed02016-01-30 23:22:37 -0800556 dstMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
557 outerTtb.setEthDst(dstMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800558 break;
559 case ETH_SRC:
Charles Chand0fd5dc2016-02-16 23:14:49 -0800560 srcMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
561 outerTtb.setEthSrc(srcMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800562 break;
563 case VLAN_ID:
564 vlanid = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
Charles Chanf9e98652016-09-07 16:54:23 -0700565 if (useSetVlanExtension) {
566 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
567 outerTtb.extension(ofdpaSetVlanVid, deviceId);
568 } else {
569 outerTtb.setVlanId(vlanid);
570 }
Charles Chan188ebf52015-12-23 00:15:11 -0800571 setVlan = true;
572 break;
573 case VLAN_POP:
574 innerTtb.popVlan();
575 popVlan = true;
576 break;
577 case DEC_MPLS_TTL:
578 case MPLS_LABEL:
579 case MPLS_POP:
580 case MPLS_PUSH:
581 case VLAN_PCP:
582 case VLAN_PUSH:
583 default:
584 break;
585 }
586 } else if (ins.type() == Instruction.Type.OUTPUT) {
587 portNum = ((Instructions.OutputInstruction) ins).port().toLong();
588 innerTtb.add(ins);
589 } else {
Saurav Das7bcbe702017-06-13 15:35:54 -0700590 log.debug("Driver does not handle this type of TrafficTreatment"
Saurav Dasa4020382018-02-14 14:14:54 -0800591 + " instruction in l2l3chain: {} - {}", ins.type(),
592 ins);
Charles Chan188ebf52015-12-23 00:15:11 -0800593 }
594 }
Charles Chan188ebf52015-12-23 00:15:11 -0800595 if (vlanid == null && meta != null) {
596 // use metadata if available
Pier Ventre140a8942016-11-02 07:26:38 -0700597 Criterion vidCriterion = meta.getCriterion(VLAN_VID);
Charles Chan188ebf52015-12-23 00:15:11 -0800598 if (vidCriterion != null) {
599 vlanid = ((VlanIdCriterion) vidCriterion).vlanId();
600 }
601 // if vlan is not set, use the vlan in metadata for outerTtb
602 if (vlanid != null && !setVlan) {
Charles Chanf9e98652016-09-07 16:54:23 -0700603 if (useSetVlanExtension) {
604 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
605 outerTtb.extension(ofdpaSetVlanVid, deviceId);
606 } else {
607 outerTtb.setVlanId(vlanid);
608 }
Charles Chan188ebf52015-12-23 00:15:11 -0800609 }
610 }
Charles Chan188ebf52015-12-23 00:15:11 -0800611 if (vlanid == null) {
612 log.error("Driver cannot process an L2/L3 group chain without "
613 + "egress vlan information for dev: {} port:{}",
614 deviceId, portNum);
615 return null;
616 }
Charles Chan188ebf52015-12-23 00:15:11 -0800617 if (!setVlan && !popVlan) {
618 // untagged outgoing port
619 TrafficTreatment.Builder temp = DefaultTrafficTreatment.builder();
620 temp.popVlan();
Yi Tsengef19de12017-04-24 11:33:05 -0700621 innerTtb.build().allInstructions().forEach(temp::add);
Charles Chan188ebf52015-12-23 00:15:11 -0800622 innerTtb = temp;
623 }
Charles Chan188ebf52015-12-23 00:15:11 -0800624 // assemble information for ofdpa l2interface group
Yi Tsengef19de12017-04-24 11:33:05 -0700625 int l2groupId = l2GroupId(vlanid, portNum);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800626 // a globally unique groupkey that is different for ports in the same device,
Charles Chan188ebf52015-12-23 00:15:11 -0800627 // but different for the same portnumber on different devices. Also different
628 // for the various group-types created out of the same next objective.
Charles Chane849c192016-01-11 18:28:54 -0800629 int l2gk = l2InterfaceGroupKey(deviceId, vlanid, portNum);
Yi Tsengef19de12017-04-24 11:33:05 -0700630 final GroupKey l2groupkey = new DefaultGroupKey(appKryo.serialize(l2gk));
Charles Chan188ebf52015-12-23 00:15:11 -0800631 // assemble information for outer group
Yi Tsengef19de12017-04-24 11:33:05 -0700632 GroupDescription outerGrpDesc;
Charles Chan188ebf52015-12-23 00:15:11 -0800633 if (mpls) {
Yi Tsengef19de12017-04-24 11:33:05 -0700634 // outer group is MPLS Interface
Saurav Das8be4e3a2016-03-11 17:19:07 -0800635 int mplsInterfaceIndex = getNextAvailableIndex();
Yi Tsengef19de12017-04-24 11:33:05 -0700636 int mplsGroupId = MPLS_INTERFACE_TYPE | (SUBTYPE_MASK & mplsInterfaceIndex);
637 final GroupKey mplsGroupKey = new DefaultGroupKey(
638 appKryo.serialize(mplsInterfaceIndex));
Yi Tsengfa394de2017-02-01 11:26:40 -0800639 outerTtb.group(new GroupId(l2groupId));
Charles Chan188ebf52015-12-23 00:15:11 -0800640 // create the mpls-interface group description to wait for the
641 // l2 interface group to be processed
642 GroupBucket mplsinterfaceGroupBucket =
643 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
644 outerGrpDesc = new DefaultGroupDescription(
645 deviceId,
646 GroupDescription.Type.INDIRECT,
647 new GroupBuckets(Collections.singletonList(
648 mplsinterfaceGroupBucket)),
Yi Tsengef19de12017-04-24 11:33:05 -0700649 mplsGroupKey,
650 mplsGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800651 appId);
652 log.debug("Trying MPLS-Interface: device:{} gid:{} gkey:{} nextid:{}",
Yi Tsengef19de12017-04-24 11:33:05 -0700653 deviceId, Integer.toHexString(mplsGroupId),
654 mplsGroupKey, nextId);
Charles Chan188ebf52015-12-23 00:15:11 -0800655 } else {
656 // outer group is L3Unicast
Saurav Das8be4e3a2016-03-11 17:19:07 -0800657 int l3unicastIndex = getNextAvailableIndex();
658 int l3groupId = L3_UNICAST_TYPE | (TYPE_MASK & l3unicastIndex);
659 final GroupKey l3groupkey = new DefaultGroupKey(
Yi Tsengef19de12017-04-24 11:33:05 -0700660 appKryo.serialize(l3unicastIndex));
Yi Tsengfa394de2017-02-01 11:26:40 -0800661 outerTtb.group(new GroupId(l2groupId));
Charles Chan188ebf52015-12-23 00:15:11 -0800662 // create the l3unicast group description to wait for the
663 // l2 interface group to be processed
664 GroupBucket l3unicastGroupBucket =
665 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
666 outerGrpDesc = new DefaultGroupDescription(
667 deviceId,
668 GroupDescription.Type.INDIRECT,
Yi Tsengef19de12017-04-24 11:33:05 -0700669 new GroupBuckets(Collections.singletonList(l3unicastGroupBucket)),
Charles Chan188ebf52015-12-23 00:15:11 -0800670 l3groupkey,
671 l3groupId,
672 appId);
673 log.debug("Trying L3Unicast: device:{} gid:{} gkey:{} nextid:{}",
674 deviceId, Integer.toHexString(l3groupId),
675 l3groupkey, nextId);
676 }
Charles Chan188ebf52015-12-23 00:15:11 -0800677 // store l2groupkey with the groupChainElem for the outer-group that depends on it
Yi Tsengef19de12017-04-24 11:33:05 -0700678 GroupChainElem gce = new GroupChainElem(outerGrpDesc, 1, false, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800679 updatePendingGroups(l2groupkey, gce);
Yi Tsengef19de12017-04-24 11:33:05 -0700680 // create group description for the inner l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700681 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800682 DefaultGroupBucket.createIndirectGroupBucket(innerTtb.build());
683 GroupDescription l2groupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -0700684 new DefaultGroupDescription(deviceId,
685 GroupDescription.Type.INDIRECT,
686 new GroupBuckets(Collections.singletonList(l2InterfaceGroupBucket)),
687 l2groupkey,
688 l2groupId,
689 appId);
Charles Chan188ebf52015-12-23 00:15:11 -0800690 log.debug("Trying L2Interface: device:{} gid:{} gkey:{} nextId:{}",
691 deviceId, Integer.toHexString(l2groupId),
692 l2groupkey, nextId);
693 return new GroupInfo(l2groupDescription, outerGrpDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800694 }
695
696 /**
697 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
698 * a chain of groups. The broadcast Next Objective passed in by the application
699 * has to be broken up into a group chain comprising of an
Saurav Das1a129a02016-11-18 15:21:57 -0800700 * L2 Flood group or L3 Multicast group, whose buckets point to L2 Interface groups.
Charles Chan188ebf52015-12-23 00:15:11 -0800701 *
702 * @param nextObj the nextObjective of type BROADCAST
703 */
704 private void processBroadcastNextObjective(NextObjective nextObj) {
Yi Tsengef19de12017-04-24 11:33:05 -0700705 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700706 if (assignedVlan == null) {
707 log.warn("VLAN ID required by broadcast next obj is missing. Abort.");
Yi Tsengef19de12017-04-24 11:33:05 -0700708 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800709 return;
710 }
Charles Chan188ebf52015-12-23 00:15:11 -0800711
jayakumarthazhath655b9a82018-10-01 00:51:54 +0530712 // Handling L2 multicast cases.
713 MacAddress dstMac = readEthDstFromSelector(nextObj.meta());
714 if (dstMac != null && dstMac.isMulticast()) {
715 processL2MulticastNextObjective(nextObj);
Jayakumar Thazhath812aa762018-10-24 01:22:04 -0400716 return;
jayakumarthazhath655b9a82018-10-01 00:51:54 +0530717 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700718
Charles Chan367c1c12018-10-19 16:23:28 -0700719 // FIXME Improve the logic
720 // If L2 load balancer is not involved, use L2IG. Otherwise, use L2UG.
721 // The purpose is to make sure existing XConnect logic can still work on a configured port.
722 List<GroupInfo> groupInfos;
723 if (nextObj.nextTreatments().stream().allMatch(n -> n.type() == NextTreatment.Type.TREATMENT)) {
724 groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
725 log.debug("prepareL2InterfaceGroup");
726 } else {
727 groupInfos = prepareL2UnfilteredGroup(nextObj);
728 log.debug("prepareL2UnfilteredGroup");
729 }
730
Yi Tsengef19de12017-04-24 11:33:05 -0700731 IpPrefix ipDst = readIpDstFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700732 if (ipDst != null) {
733 if (ipDst.isMulticast()) {
734 createL3MulticastGroup(nextObj, assignedVlan, groupInfos);
735 } else {
736 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -0700737 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700738 }
739 } else {
740 createL2FloodGroup(nextObj, assignedVlan, groupInfos);
741 }
742 }
743
Charles Chan367c1c12018-10-19 16:23:28 -0700744 private List<GroupInfo> prepareL2UnfilteredGroup(NextObjective nextObj) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700745 ImmutableList.Builder<GroupInfo> groupInfoBuilder = ImmutableList.builder();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700746 // break up broadcast next objective to multiple groups
Charles Chan367c1c12018-10-19 16:23:28 -0700747 Collection<TrafficTreatment> treatments = nextObj.nextTreatments().stream()
748 .filter(nt -> nt.type() == NextTreatment.Type.TREATMENT)
749 .map(nt -> ((DefaultNextTreatment) nt).treatment())
750 .collect(Collectors.toSet());
751 Collection<Integer> nextIds = nextObj.nextTreatments().stream()
752 .filter(nt -> nt.type() == NextTreatment.Type.ID)
753 .map(nt -> ((IdNextTreatment) nt).nextId())
754 .collect(Collectors.toSet());
755
756 // Each treatment is converted to an L2 unfiltered group
757 treatments.forEach(treatment -> {
758 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
759 // Extract port information
760 PortNumber port = treatment.allInstructions().stream()
761 .map(instr -> (Instructions.OutputInstruction) instr)
762 .map(instr -> instr.port())
763 .findFirst().orElse(null);
764 if (port == null) {
765 log.debug("Skip bucket without output instruction");
766 return;
767 }
768 tBuilder.setOutput(port);
769 if (requireAllowVlanTransition()) {
770 tBuilder.extension(new OfdpaSetAllowVlanTranslation(Ofdpa3AllowVlanTranslationType.ALLOW), deviceId);
771 }
772
773 // Build L2UG
774 int l2ugk = l2UnfilteredGroupKey(deviceId, port.toLong());
775 final GroupKey l2UnfilterGroupKey = new DefaultGroupKey(appKryo.serialize(l2ugk));
776 int l2UnfilteredGroupId = L2_UNFILTERED_TYPE | ((int) port.toLong() & FOUR_NIBBLE_MASK);
777 GroupBucket l2UnfilteredGroupBucket = DefaultGroupBucket.createIndirectGroupBucket(tBuilder.build());
778 GroupDescription l2UnfilteredGroupDesc = new DefaultGroupDescription(deviceId,
779 GroupDescription.Type.INDIRECT,
780 new GroupBuckets(Collections.singletonList(l2UnfilteredGroupBucket)),
781 l2UnfilterGroupKey,
782 l2UnfilteredGroupId,
783 nextObj.appId());
784 log.debug("Trying L2-Unfiltered: device:{} gid:{} gkey:{} nextid:{}",
785 deviceId, Integer.toHexString(l2UnfilteredGroupId), l2UnfilterGroupKey, nextObj.id());
786 groupInfoBuilder.add(new GroupInfo(l2UnfilteredGroupDesc, l2UnfilteredGroupDesc));
787 });
788
789 // Lookup each nextId in the store and obtain the group information
790 nextIds.forEach(nextId -> {
791 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(flowObjectiveStore.getNextGroup(nextId).data());
792 GroupKey topGroupKey = allActiveKeys.get(0).getFirst();
793 GroupDescription groupDesc = groupService.getGroup(deviceId, topGroupKey);
794 log.debug("Trying L2-Interface: device:{} gid:{}, gkey:{} nextid:{}",
795 deviceId, Integer.toHexString(((Group) groupDesc).id().id()), topGroupKey, nextId);
796 groupInfoBuilder.add(new GroupInfo(groupDesc, groupDesc));
797 });
798
799 return groupInfoBuilder.build();
800 }
801
802 private List<GroupInfo> prepareL2InterfaceGroup(NextObjective nextObj, VlanId assignedVlan) {
803 ImmutableList.Builder<GroupInfo> groupInfoBuilder = ImmutableList.builder();
804 // break up broadcast next objective to multiple groups
805 Collection<TrafficTreatment> buckets = nextObj.nextTreatments().stream()
806 .filter(nt -> nt.type() == NextTreatment.Type.TREATMENT)
807 .map(nt -> ((DefaultNextTreatment) nt).treatment())
808 .collect(Collectors.toSet());
809
810 // Each treatment is converted to an L2 interface group
Charles Chan188ebf52015-12-23 00:15:11 -0800811 for (TrafficTreatment treatment : buckets) {
812 TrafficTreatment.Builder newTreatment = DefaultTrafficTreatment.builder();
813 PortNumber portNum = null;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700814 VlanId egressVlan = null;
Charles Chan188ebf52015-12-23 00:15:11 -0800815 // ensure that the only allowed treatments are pop-vlan and output
816 for (Instruction ins : treatment.allInstructions()) {
817 if (ins.type() == Instruction.Type.L2MODIFICATION) {
818 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
819 switch (l2ins.subtype()) {
820 case VLAN_POP:
821 newTreatment.add(l2ins);
822 break;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700823 case VLAN_ID:
824 egressVlan = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
825 break;
Charles Chan188ebf52015-12-23 00:15:11 -0800826 default:
827 log.debug("action {} not permitted for broadcast nextObj",
828 l2ins.subtype());
829 break;
830 }
831 } else if (ins.type() == Instruction.Type.OUTPUT) {
832 portNum = ((Instructions.OutputInstruction) ins).port();
833 newTreatment.add(ins);
834 } else {
Charles Chane849c192016-01-11 18:28:54 -0800835 log.debug("TrafficTreatment of type {} not permitted in " +
836 " broadcast nextObjective", ins.type());
Charles Chan188ebf52015-12-23 00:15:11 -0800837 }
838 }
Yi Tsengef19de12017-04-24 11:33:05 -0700839 if (portNum == null) {
840 log.warn("Can't find output port for the bucket {}.", treatment);
841 continue;
842 }
Charles Chan188ebf52015-12-23 00:15:11 -0800843 // assemble info for l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700844 VlanId l2InterfaceGroupVlan =
845 (egressVlan != null && !assignedVlan.equals(egressVlan)) ?
846 egressVlan : assignedVlan;
847 int l2gk = l2InterfaceGroupKey(deviceId, l2InterfaceGroupVlan, portNum.toLong());
848 final GroupKey l2InterfaceGroupKey =
Yi Tsengef19de12017-04-24 11:33:05 -0700849 new DefaultGroupKey(appKryo.serialize(l2gk));
Charles Chan372b63e2017-02-07 12:10:53 -0800850 int l2InterfaceGroupId = L2_INTERFACE_TYPE |
Charles Chan367c1c12018-10-19 16:23:28 -0700851 ((l2InterfaceGroupVlan.toShort() & THREE_NIBBLE_MASK) << PORT_LEN) |
852 ((int) portNum.toLong() & FOUR_NIBBLE_MASK);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700853 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800854 DefaultGroupBucket.createIndirectGroupBucket(newTreatment.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700855 GroupDescription l2InterfaceGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -0700856 new DefaultGroupDescription(deviceId,
857 GroupDescription.Type.INDIRECT,
858 new GroupBuckets(Collections.singletonList(
859 l2InterfaceGroupBucket)),
860 l2InterfaceGroupKey,
861 l2InterfaceGroupId,
862 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800863 log.debug("Trying L2-Interface: device:{} gid:{} gkey:{} nextid:{}",
Charles Chan5b9df8d2016-03-28 22:21:40 -0700864 deviceId, Integer.toHexString(l2InterfaceGroupId),
865 l2InterfaceGroupKey, nextObj.id());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700866 groupInfoBuilder.add(new GroupInfo(l2InterfaceGroupDescription,
867 l2InterfaceGroupDescription));
Charles Chan188ebf52015-12-23 00:15:11 -0800868 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700869 return groupInfoBuilder.build();
870 }
Charles Chan188ebf52015-12-23 00:15:11 -0800871
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700872 private void createL2FloodGroup(NextObjective nextObj, VlanId vlanId,
873 List<GroupInfo> groupInfos) {
874 // assemble info for l2 flood group. Since there can be only one flood
875 // group for a vlan, its index is always the same - 0
Yi Tsengef19de12017-04-24 11:33:05 -0700876 Integer l2FloodGroupId = L2_FLOOD_TYPE | (vlanId.toShort() << 16);
877 final GroupKey l2FloodGroupKey = l2FloodGroupKey(vlanId, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800878 // collection of group buckets pointing to all the l2 interface groups
Yi Tsengef19de12017-04-24 11:33:05 -0700879 List<GroupBucket> l2floodBuckets = generateNextGroupBuckets(groupInfos, ALL);
Charles Chan188ebf52015-12-23 00:15:11 -0800880 // create the l2flood group-description to wait for all the
881 // l2interface groups to be processed
882 GroupDescription l2floodGroupDescription =
883 new DefaultGroupDescription(
884 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800885 ALL,
Charles Chan188ebf52015-12-23 00:15:11 -0800886 new GroupBuckets(l2floodBuckets),
Yi Tsengef19de12017-04-24 11:33:05 -0700887 l2FloodGroupKey,
888 l2FloodGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800889 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800890 log.debug("Trying L2-Flood: device:{} gid:{} gkey:{} nextid:{}",
Yi Tsengef19de12017-04-24 11:33:05 -0700891 deviceId, Integer.toHexString(l2FloodGroupId),
892 l2FloodGroupKey, nextObj.id());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700893 // Put all dependency information into allGroupKeys
894 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
895 groupInfos.forEach(groupInfo -> {
Yi Tsengef19de12017-04-24 11:33:05 -0700896 Deque<GroupKey> groupKeyChain = new ArrayDeque<>();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700897 // In this case we should have L2 interface group only
Yi Tsengef19de12017-04-24 11:33:05 -0700898 groupKeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
899 groupKeyChain.addFirst(l2FloodGroupKey);
900 allGroupKeys.add(groupKeyChain);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700901 });
Charles Chan5b9df8d2016-03-28 22:21:40 -0700902 // Point the next objective to this group
903 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -0700904 updatePendingNextObjective(l2FloodGroupKey, ofdpaGrp);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700905 GroupChainElem gce = new GroupChainElem(l2floodGroupDescription,
Yi Tsengef19de12017-04-24 11:33:05 -0700906 groupInfos.size(), false, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700907 groupInfos.forEach(groupInfo -> {
908 // Point this group to the next group
Yi Tsengef19de12017-04-24 11:33:05 -0700909 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), gce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700910 // Start installing the inner-most group
Yi Tsengef19de12017-04-24 11:33:05 -0700911 groupService.addGroup(groupInfo.innerMostGroupDesc());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700912 });
913 }
914
Pier Luigi21fffd22018-01-19 10:24:53 +0100915 private void createL3MulticastGroup(NextObjective nextObj, VlanId vlanId,
916 List<GroupInfo> groupInfos) {
917 // Let's create a new list mcast buckets
918 List<GroupBucket> l3McastBuckets = createL3MulticastBucket(groupInfos);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700919
920 int l3MulticastIndex = getNextAvailableIndex();
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700921 int l3MulticastGroupId = L3_MULTICAST_TYPE |
922 vlanId.toShort() << 16 | (TYPE_VLAN_MASK & l3MulticastIndex);
923 final GroupKey l3MulticastGroupKey =
Yi Tsengef19de12017-04-24 11:33:05 -0700924 new DefaultGroupKey(appKryo.serialize(l3MulticastIndex));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700925
926 GroupDescription l3MulticastGroupDesc = new DefaultGroupDescription(deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800927 ALL,
Charles Chan5b9df8d2016-03-28 22:21:40 -0700928 new GroupBuckets(l3McastBuckets),
929 l3MulticastGroupKey,
930 l3MulticastGroupId,
931 nextObj.appId());
932
933 // Put all dependency information into allGroupKeys
934 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
935 groupInfos.forEach(groupInfo -> {
936 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700937 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700938 // Add L3 interface group to the chain if there is one.
Yi Tsengef19de12017-04-24 11:33:05 -0700939 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
940 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700941 }
942 gkeyChain.addFirst(l3MulticastGroupKey);
943 allGroupKeys.add(gkeyChain);
944 });
Charles Chan5b9df8d2016-03-28 22:21:40 -0700945 // Point the next objective to this group
946 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
947 updatePendingNextObjective(l3MulticastGroupKey, ofdpaGrp);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700948 GroupChainElem outerGce = new GroupChainElem(l3MulticastGroupDesc,
Yi Tsengef19de12017-04-24 11:33:05 -0700949 groupInfos.size(), false, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700950 groupInfos.forEach(groupInfo -> {
951 // Point this group (L3 multicast) to the next group
Yi Tsengef19de12017-04-24 11:33:05 -0700952 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), outerGce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700953 // Point next group to inner-most group, if any
Yi Tsengef19de12017-04-24 11:33:05 -0700954 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
955 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(),
956 1, false, deviceId);
957 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700958 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700959 // Start installing the inner-most group
Yi Tsengef19de12017-04-24 11:33:05 -0700960 groupService.addGroup(groupInfo.innerMostGroupDesc());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700961 });
Charles Chan188ebf52015-12-23 00:15:11 -0800962 }
963
Charles Chan188ebf52015-12-23 00:15:11 -0800964 /**
965 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
966 * a chain of groups. The hashed Next Objective passed in by the application
967 * has to be broken up into a group chain comprising of an
968 * L3 ECMP group as the top level group. Buckets of this group can point
969 * to a variety of groups in a group chain, depending on the whether
970 * MPLS labels are being pushed or not.
971 * <p>
972 * NOTE: We do not create MPLS ECMP groups as they are unimplemented in
973 * OF-DPA 2.0 (even though it is in the spec). Therefore we do not
974 * check the nextObjective meta to see what is matching before being
975 * sent to this nextObjective.
976 *
977 * @param nextObj the nextObjective of type HASHED
978 */
Charles Chan367c1c12018-10-19 16:23:28 -0700979 protected void processEcmpHashedNextObjective(NextObjective nextObj) {
Charles Chan188ebf52015-12-23 00:15:11 -0800980 // storage for all group keys in the chain of groups created
981 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
982 List<GroupInfo> unsentGroups = new ArrayList<>();
Charles Chan367c1c12018-10-19 16:23:28 -0700983 createEcmpHashBucketChains(nextObj, allGroupKeys, unsentGroups);
Charles Chan188ebf52015-12-23 00:15:11 -0800984 // now we can create the outermost L3 ECMP group
985 List<GroupBucket> l3ecmpGroupBuckets = new ArrayList<>();
986 for (GroupInfo gi : unsentGroups) {
987 // create ECMP bucket to point to the outer group
988 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Yi Tsengef19de12017-04-24 11:33:05 -0700989 ttb.group(new GroupId(gi.nextGroupDesc().givenGroupId()));
Charles Chan188ebf52015-12-23 00:15:11 -0800990 GroupBucket sbucket = DefaultGroupBucket
991 .createSelectGroupBucket(ttb.build());
992 l3ecmpGroupBuckets.add(sbucket);
993 }
Saurav Das8be4e3a2016-03-11 17:19:07 -0800994 int l3ecmpIndex = getNextAvailableIndex();
995 int l3ecmpGroupId = L3_ECMP_TYPE | (TYPE_MASK & l3ecmpIndex);
996 GroupKey l3ecmpGroupKey = new DefaultGroupKey(
Yi Tsengef19de12017-04-24 11:33:05 -0700997 appKryo.serialize(l3ecmpIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800998 GroupDescription l3ecmpGroupDesc =
999 new DefaultGroupDescription(
1000 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -08001001 SELECT,
Charles Chan188ebf52015-12-23 00:15:11 -08001002 new GroupBuckets(l3ecmpGroupBuckets),
1003 l3ecmpGroupKey,
1004 l3ecmpGroupId,
1005 nextObj.appId());
1006 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
1007 l3ecmpGroupBuckets.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001008 false, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001009
1010 // create objects for local and distributed storage
Yi Tsengef19de12017-04-24 11:33:05 -07001011 allGroupKeys.forEach(gKeyChain -> gKeyChain.addFirst(l3ecmpGroupKey));
Charles Chan188ebf52015-12-23 00:15:11 -08001012 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Charles Chan188ebf52015-12-23 00:15:11 -08001013 // store l3ecmpGroupKey with the ofdpaGroupChain for the nextObjective
1014 // that depends on it
1015 updatePendingNextObjective(l3ecmpGroupKey, ofdpaGrp);
Charles Chan188ebf52015-12-23 00:15:11 -08001016 log.debug("Trying L3ECMP: device:{} gid:{} gkey:{} nextId:{}",
1017 deviceId, Integer.toHexString(l3ecmpGroupId),
1018 l3ecmpGroupKey, nextObj.id());
1019 // finally we are ready to send the innermost groups
1020 for (GroupInfo gi : unsentGroups) {
1021 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001022 Integer.toHexString(gi.innerMostGroupDesc().givenGroupId()), deviceId);
1023 updatePendingGroups(gi.nextGroupDesc().appCookie(), l3ecmpGce);
1024 groupService.addGroup(gi.innerMostGroupDesc());
Charles Chan188ebf52015-12-23 00:15:11 -08001025 }
Charles Chan188ebf52015-12-23 00:15:11 -08001026 }
1027
1028 /**
1029 * Creates group chains for all buckets in a hashed group, and stores the
1030 * GroupInfos and GroupKeys for all the groups in the lists passed in, which
1031 * should be empty.
1032 * <p>
1033 * Does not create the top level ECMP group. Does not actually send the
1034 * groups to the groupService.
1035 *
1036 * @param nextObj the Next Objective with buckets that need to be converted
1037 * to group chains
1038 * @param allGroupKeys a list to store groupKey for each bucket-group-chain
1039 * @param unsentGroups a list to store GroupInfo for each bucket-group-chain
1040 */
Charles Chan367c1c12018-10-19 16:23:28 -07001041 protected void createEcmpHashBucketChains(NextObjective nextObj,
1042 List<Deque<GroupKey>> allGroupKeys,
1043 List<GroupInfo> unsentGroups) {
Charles Chan188ebf52015-12-23 00:15:11 -08001044 // break up hashed next objective to multiple groups
1045 Collection<TrafficTreatment> buckets = nextObj.next();
1046
1047 for (TrafficTreatment bucket : buckets) {
1048 //figure out how many labels are pushed in each bucket
1049 int labelsPushed = 0;
1050 MplsLabel innermostLabel = null;
1051 for (Instruction ins : bucket.allInstructions()) {
1052 if (ins.type() == Instruction.Type.L2MODIFICATION) {
1053 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
1054 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_PUSH) {
1055 labelsPushed++;
1056 }
1057 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) {
1058 if (innermostLabel == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001059 innermostLabel =
1060 ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label();
Charles Chan188ebf52015-12-23 00:15:11 -08001061 }
1062 }
1063 }
1064 }
Yi Tsengef19de12017-04-24 11:33:05 -07001065 Deque<GroupKey> gKeyChain = new ArrayDeque<>();
Saurav Dasa4020382018-02-14 14:14:54 -08001066 // here we only deal with 0 and 1 label push
Charles Chan188ebf52015-12-23 00:15:11 -08001067 if (labelsPushed == 0) {
Yi Tsengef19de12017-04-24 11:33:05 -07001068 GroupInfo noLabelGroupInfo;
Pier Ventre140a8942016-11-02 07:26:38 -07001069 TrafficSelector metaSelector = nextObj.meta();
1070 if (metaSelector != null) {
1071 if (isNotMplsBos(metaSelector)) {
Yi Tsengef19de12017-04-24 11:33:05 -07001072 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
1073 nextObj.appId(), true,
1074 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -07001075 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001076 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
1077 nextObj.appId(), false,
1078 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -07001079 }
1080 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001081 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
1082 nextObj.appId(), false,
1083 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -07001084 }
Yi Tsengef19de12017-04-24 11:33:05 -07001085 if (noLabelGroupInfo == null) {
Charles Chan188ebf52015-12-23 00:15:11 -08001086 log.error("Could not process nextObj={} in dev:{}",
1087 nextObj.id(), deviceId);
1088 return;
1089 }
Yi Tsengef19de12017-04-24 11:33:05 -07001090 gKeyChain.addFirst(noLabelGroupInfo.innerMostGroupDesc().appCookie());
1091 gKeyChain.addFirst(noLabelGroupInfo.nextGroupDesc().appCookie());
Charles Chan188ebf52015-12-23 00:15:11 -08001092 // we can't send the inner group description yet, as we have to
1093 // create the dependent ECMP group first. So we store..
Yi Tsengef19de12017-04-24 11:33:05 -07001094 unsentGroups.add(noLabelGroupInfo);
Charles Chan188ebf52015-12-23 00:15:11 -08001095 } else if (labelsPushed == 1) {
1096 GroupInfo onelabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
1097 nextObj.appId(), true,
1098 nextObj.meta());
1099 if (onelabelGroupInfo == null) {
1100 log.error("Could not process nextObj={} in dev:{}",
1101 nextObj.id(), deviceId);
1102 return;
1103 }
1104 // we need to add another group to this chain - the L3VPN group
1105 TrafficTreatment.Builder l3vpnTtb = DefaultTrafficTreatment.builder();
Charles Chan0f43e472017-02-14 14:00:16 -08001106 if (requireVlanPopBeforeMplsPush()) {
1107 l3vpnTtb.popVlan();
1108 }
Charles Chan188ebf52015-12-23 00:15:11 -08001109 l3vpnTtb.pushMpls()
1110 .setMpls(innermostLabel)
Yi Tsengef19de12017-04-24 11:33:05 -07001111 .group(new GroupId(onelabelGroupInfo.nextGroupDesc().givenGroupId()));
Charles Chan40132b32017-01-22 00:19:37 -08001112 if (supportCopyTtl()) {
1113 l3vpnTtb.copyTtlOut();
1114 }
1115 if (supportSetMplsBos()) {
1116 l3vpnTtb.setMplsBos(true);
1117 }
Charles Chan0f43e472017-02-14 14:00:16 -08001118 if (requireVlanPopBeforeMplsPush()) {
1119 l3vpnTtb.pushVlan().setVlanId(VlanId.vlanId(VlanId.RESERVED));
1120 }
Charles Chan188ebf52015-12-23 00:15:11 -08001121 GroupBucket l3vpnGrpBkt =
1122 DefaultGroupBucket.createIndirectGroupBucket(l3vpnTtb.build());
Saurav Das8be4e3a2016-03-11 17:19:07 -08001123 int l3vpnIndex = getNextAvailableIndex();
Yi Tsengef19de12017-04-24 11:33:05 -07001124 int l3vpnGroupId = MPLS_L3VPN_SUBTYPE | (SUBTYPE_MASK & l3vpnIndex);
1125 GroupKey l3vpnGroupKey = new DefaultGroupKey(
1126 appKryo.serialize(l3vpnIndex));
Charles Chan188ebf52015-12-23 00:15:11 -08001127 GroupDescription l3vpnGroupDesc =
1128 new DefaultGroupDescription(
1129 deviceId,
1130 GroupDescription.Type.INDIRECT,
Yi Tsengef19de12017-04-24 11:33:05 -07001131 new GroupBuckets(Collections.singletonList(l3vpnGrpBkt)),
1132 l3vpnGroupKey,
1133 l3vpnGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -08001134 nextObj.appId());
Yi Tsengef19de12017-04-24 11:33:05 -07001135 GroupChainElem l3vpnGce = new GroupChainElem(l3vpnGroupDesc,
1136 1,
1137 false,
1138 deviceId);
1139 updatePendingGroups(onelabelGroupInfo.nextGroupDesc().appCookie(), l3vpnGce);
Charles Chan188ebf52015-12-23 00:15:11 -08001140
Yi Tsengef19de12017-04-24 11:33:05 -07001141 gKeyChain.addFirst(onelabelGroupInfo.innerMostGroupDesc().appCookie());
1142 gKeyChain.addFirst(onelabelGroupInfo.nextGroupDesc().appCookie());
1143 gKeyChain.addFirst(l3vpnGroupKey);
Charles Chan188ebf52015-12-23 00:15:11 -08001144 //now we can replace the outerGrpDesc with the one we just created
Yi Tsengef19de12017-04-24 11:33:05 -07001145 onelabelGroupInfo.nextGroupDesc(l3vpnGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -08001146 // we can't send the innermost group yet, as we have to create
1147 // the dependent ECMP group first. So we store ...
1148 unsentGroups.add(onelabelGroupInfo);
Yi Tsengef19de12017-04-24 11:33:05 -07001149 log.debug("Trying L3VPN: device:{} gid:{} group key:{} nextId:{}",
1150 deviceId, Integer.toHexString(l3vpnGroupId),
1151 l3vpnGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -08001152 } else {
1153 log.warn("Driver currently does not handle more than 1 MPLS "
1154 + "labels. Not processing nextObjective {}", nextObj.id());
1155 return;
1156 }
Charles Chan188ebf52015-12-23 00:15:11 -08001157 // all groups in this chain
Yi Tsengef19de12017-04-24 11:33:05 -07001158 allGroupKeys.add(gKeyChain);
Charles Chan188ebf52015-12-23 00:15:11 -08001159 }
1160 }
1161
Pier Ventre42287df2016-11-09 14:17:26 -08001162 /**
Charles Chan367c1c12018-10-19 16:23:28 -07001163 * Create L2 hash group.
1164 *
1165 * @param nextObj next objective
1166 */
1167 private void processL2HashedNextObjective(NextObjective nextObj) {
1168 int l2LbIndex = Optional.ofNullable(nextObj.meta().getCriterion(IN_PORT))
1169 .map(c -> (PortCriterion) c).map(PortCriterion::port).map(PortNumber::toLong).map(Long::intValue)
1170 .orElse(-1);
1171 if (l2LbIndex == -1) {
1172 log.warn("l2LbIndex is not found in the meta of L2 hash objective. Abort");
1173 return;
1174 }
1175
1176 // Storage for all group keys in the chain of groups created
1177 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
1178 List<GroupInfo> unsentGroups = new ArrayList<>();
1179 createL2HashBuckets(nextObj, allGroupKeys, unsentGroups);
1180
1181 // Create L2 load balancing group
1182 List<GroupBucket> l2LbGroupBuckets = new ArrayList<>();
1183 for (GroupInfo gi : unsentGroups) {
1184 // create load balancing bucket to point to the outer group
1185 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
1186 ttb.group(new GroupId(gi.nextGroupDesc().givenGroupId()));
1187 GroupBucket sbucket = DefaultGroupBucket.createSelectGroupBucket(ttb.build());
1188 l2LbGroupBuckets.add(sbucket);
1189 }
1190
1191 int l2LbGroupId = L2_LB_TYPE | (TYPE_MASK & l2LbIndex);
1192 int l2lbgk = l2HashGroupKey(deviceId, l2LbIndex);
1193 GroupKey l2LbGroupKey = new DefaultGroupKey(appKryo.serialize(l2lbgk));
1194 GroupDescription l2LbGroupDesc =
1195 new DefaultGroupDescription(
1196 deviceId,
1197 SELECT,
1198 new GroupBuckets(l2LbGroupBuckets),
1199 l2LbGroupKey,
1200 l2LbGroupId,
1201 nextObj.appId());
1202 GroupChainElem l2LbGce = new GroupChainElem(l2LbGroupDesc, l2LbGroupBuckets.size(), false, deviceId);
1203
1204 // Create objects for local and distributed storage
1205 allGroupKeys.forEach(gKeyChain -> gKeyChain.addFirst(l2LbGroupKey));
1206 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
1207 // Store l2LbGroupKey with the ofdpaGroupChain for the nextObjective that depends on it
1208 updatePendingNextObjective(l2LbGroupKey, ofdpaGrp);
1209 log.debug("Trying L2-LB: device:{} gid:{} gkey:{} nextId:{}",
1210 deviceId, Integer.toHexString(l2LbGroupId), l2LbGroupKey, nextObj.id());
1211 // finally we are ready to send the innermost groups
1212 for (GroupInfo gi : unsentGroups) {
1213 log.debug("Sending innermost group {} in group chain on device {} ",
1214 Integer.toHexString(gi.innerMostGroupDesc().givenGroupId()), deviceId);
1215 updatePendingGroups(gi.nextGroupDesc().appCookie(), l2LbGce);
1216 groupService.addGroup(gi.innerMostGroupDesc());
1217 }
1218 }
1219
1220 /**
1221 * Create L2 hash group buckets.
1222 *
1223 * @param nextObj next objective
1224 */
1225 private void createL2HashBuckets(NextObjective nextObj,
1226 List<Deque<GroupKey>> allGroupKeys, List<GroupInfo> unsentGroups) {
1227 List<GroupInfo> groupInfos = prepareL2UnfilteredGroup(nextObj);
1228 groupInfos.forEach(groupInfo -> {
1229 // Update allGroupKeys
1230 Deque<GroupKey> gKeyChain = new ArrayDeque<>();
1231 gKeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
1232 allGroupKeys.add(gKeyChain);
1233
1234 // Update unsent group list
1235 unsentGroups.add(groupInfo);
1236 });
1237 }
1238
1239 /**
Pier Ventre42287df2016-11-09 14:17:26 -08001240 * Processes the pseudo wire related next objective.
1241 * This procedure try to reuse the mpls label groups,
1242 * the mpls interface group and the l2 interface group.
1243 *
1244 * @param nextObjective the objective to process.
1245 */
1246 protected void processPwNextObjective(NextObjective nextObjective) {
Saurav Das1547b3f2017-05-05 17:01:08 -07001247 log.warn("Pseudo wire extensions are not supported in OFDPA 2.0 {}",
1248 nextObjective.id());
Pier Ventre42287df2016-11-09 14:17:26 -08001249 }
1250
Saurav Das8be4e3a2016-03-11 17:19:07 -08001251 //////////////////////////////////////
1252 // Group Editing
1253 //////////////////////////////////////
Charles Chan188ebf52015-12-23 00:15:11 -08001254 /**
1255 * Adds a bucket to the top level group of a group-chain, and creates the chain.
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001256 * Ensures that bucket being added is not a duplicate, by checking existing
Yi Tsengef19de12017-04-24 11:33:05 -07001257 * buckets for the same output port.
Charles Chan188ebf52015-12-23 00:15:11 -08001258 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001259 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -08001260 * @param next the representation of the existing group-chain for this next objective
1261 */
1262 protected void addBucketToGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001263 if (nextObjective.type() != NextObjective.Type.HASHED &&
1264 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001265 log.warn("AddBuckets not applied to nextType:{} in dev:{} for next:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001266 nextObjective.type(), deviceId, nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -07001267 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001268 return;
1269 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001270 // first check to see if bucket being added is not a duplicate of an
Yi Tsengef19de12017-04-24 11:33:05 -07001271 // existing bucket. If it is for an existing output port, then its a
1272 // duplicate.
Yi Tseng78f51f42017-02-02 13:54:58 -08001273 Set<TrafficTreatment> duplicateBuckets = Sets.newHashSet();
Yi Tsengef19de12017-04-24 11:33:05 -07001274 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
1275 Set<PortNumber> existingPorts = getExistingOutputPorts(allActiveKeys,
1276 groupService,
1277 deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -08001278 Set<TrafficTreatment> nonDuplicateBuckets = Sets.newHashSet();
1279 NextObjective objectiveToAdd;
Yi Tseng78f51f42017-02-02 13:54:58 -08001280 nextObjective.next().forEach(trafficTreatment -> {
1281 PortNumber portNumber = readOutPortFromTreatment(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001282 if (portNumber == null) {
1283 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001284 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001285 if (existingPorts.contains(portNumber)) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001286 // its possible that portnumbers are same but labels are different
1287 int label = readLabelFromTreatment(trafficTreatment);
1288 if (label == -1) {
1289 duplicateBuckets.add(trafficTreatment);
1290 } else {
Saurav Dasceccf242017-08-03 18:30:35 -07001291 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1292 groupService, deviceId,
1293 portNumber, label);
1294 if (!existing.isEmpty()) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001295 duplicateBuckets.add(trafficTreatment);
1296 } else {
1297 nonDuplicateBuckets.add(trafficTreatment);
1298 }
1299 }
Yi Tseng47f82dc2017-03-05 22:48:39 -08001300 } else {
1301 nonDuplicateBuckets.add(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001302 }
1303 });
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001304 if (duplicateBuckets.isEmpty()) {
1305 // use the original objective
1306 objectiveToAdd = nextObjective;
1307 } else if (!nonDuplicateBuckets.isEmpty()) {
1308 // only use the non-duplicate buckets if there are any
1309 log.debug("Some buckets {} already exist in next id {}, duplicate "
1310 + "buckets will be ignored.", duplicateBuckets, nextObjective.id());
1311 // new next objective with non duplicate treatments
Yi Tseng47f82dc2017-03-05 22:48:39 -08001312 NextObjective.Builder builder = DefaultNextObjective.builder()
1313 .withType(nextObjective.type())
1314 .withId(nextObjective.id())
1315 .withMeta(nextObjective.meta())
1316 .fromApp(nextObjective.appId());
1317 nonDuplicateBuckets.forEach(builder::addTreatment);
Yi Tseng47f82dc2017-03-05 22:48:39 -08001318 ObjectiveContext context = nextObjective.context().orElse(null);
1319 objectiveToAdd = builder.addToExisting(context);
1320 } else {
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001321 // buckets to add are already there - nothing to do
Saurav Das1547b3f2017-05-05 17:01:08 -07001322 log.debug("buckets already exist {} in next: {} ..ignoring bucket add",
1323 duplicateBuckets, nextObjective.id());
1324 pass(nextObjective);
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001325 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001326 }
Saurav Das1a129a02016-11-18 15:21:57 -08001327 if (nextObjective.type() == NextObjective.Type.HASHED) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001328 addBucketToHashGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001329 } else if (nextObjective.type() == NextObjective.Type.BROADCAST) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001330 addBucketToBroadcastGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001331 }
1332 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001333
Saurav Das1a129a02016-11-18 15:21:57 -08001334 private void addBucketToHashGroup(NextObjective nextObjective,
Yi Tseng78f51f42017-02-02 13:54:58 -08001335 List<Deque<GroupKey>> allActiveKeys) {
Charles Chan188ebf52015-12-23 00:15:11 -08001336 // storage for all group keys in the chain of groups created
1337 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
1338 List<GroupInfo> unsentGroups = new ArrayList<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001339 List<GroupBucket> newBuckets;
Charles Chan367c1c12018-10-19 16:23:28 -07001340 createEcmpHashBucketChains(nextObjective, allGroupKeys, unsentGroups);
Yi Tseng78f51f42017-02-02 13:54:58 -08001341 // now we can create the buckets to add to the outermost L3 ECMP group
1342 newBuckets = generateNextGroupBuckets(unsentGroups, SELECT);
Saurav Das1a129a02016-11-18 15:21:57 -08001343 // retrieve the original L3 ECMP group
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001344 Group l3ecmpGroup = retrieveTopLevelGroup(allActiveKeys, deviceId,
1345 groupService, nextObjective.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001346 if (l3ecmpGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001347 fail(nextObjective, ObjectiveError.GROUPMISSING);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001348 return;
1349 }
Saurav Das1a129a02016-11-18 15:21:57 -08001350 GroupKey l3ecmpGroupKey = l3ecmpGroup.appCookie();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001351 int l3ecmpGroupId = l3ecmpGroup.id().id();
Charles Chan188ebf52015-12-23 00:15:11 -08001352 // Although GroupDescriptions are not necessary for adding buckets to
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001353 // existing groups, we still use one in the GroupChainElem. When the latter is
Charles Chan188ebf52015-12-23 00:15:11 -08001354 // processed, the info will be extracted for the bucketAdd call to groupService
1355 GroupDescription l3ecmpGroupDesc =
Yi Tsengef19de12017-04-24 11:33:05 -07001356 new DefaultGroupDescription(deviceId,
1357 SELECT,
1358 new GroupBuckets(newBuckets),
1359 l3ecmpGroupKey,
1360 l3ecmpGroupId,
1361 nextObjective.appId());
Yi Tseng78f51f42017-02-02 13:54:58 -08001362 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
1363 unsentGroups.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001364 true,
1365 deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001366
Saurav Dasc88d4662017-05-15 15:34:25 -07001367 // update new bucket-chains
1368 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
1369 for (Deque<GroupKey> newBucketChain : allGroupKeys) {
1370 newBucketChain.addFirst(l3ecmpGroupKey);
1371 addedKeys.add(newBucketChain);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001372 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001373 updatePendingNextObjective(l3ecmpGroupKey,
1374 new OfdpaNextGroup(addedKeys, nextObjective));
Yi Tsengef19de12017-04-24 11:33:05 -07001375 log.debug("Adding to L3ECMP: device:{} gid:{} group key:{} nextId:{}",
Charles Chan188ebf52015-12-23 00:15:11 -08001376 deviceId, Integer.toHexString(l3ecmpGroupId),
1377 l3ecmpGroupKey, nextObjective.id());
Yi Tseng78f51f42017-02-02 13:54:58 -08001378 unsentGroups.forEach(groupInfo -> {
1379 // send the innermost group
1380 log.debug("Sending innermost group {} in group chain on device {} ",
Saurav Dasc88d4662017-05-15 15:34:25 -07001381 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
1382 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001383 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3ecmpGce);
1384 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001385 });
Saurav Das1a129a02016-11-18 15:21:57 -08001386 }
Charles Chan188ebf52015-12-23 00:15:11 -08001387
Saurav Das1a129a02016-11-18 15:21:57 -08001388 private void addBucketToBroadcastGroup(NextObjective nextObj,
Yi Tsengef19de12017-04-24 11:33:05 -07001389 List<Deque<GroupKey>> allActiveKeys) {
1390 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001391 if (assignedVlan == null) {
1392 log.warn("VLAN ID required by broadcast next obj is missing. "
1393 + "Aborting add bucket to broadcast group for next:{} in dev:{}",
1394 nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001395 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001396 return;
1397 }
Saurav Das1a129a02016-11-18 15:21:57 -08001398 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Yi Tsengef19de12017-04-24 11:33:05 -07001399 IpPrefix ipDst = readIpDstFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001400 if (ipDst != null) {
1401 if (ipDst.isMulticast()) {
Yi Tsengef19de12017-04-24 11:33:05 -07001402 addBucketToL3MulticastGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001403 } else {
1404 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001405 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001406 }
1407 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001408 addBucketToL2FloodGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001409 }
1410 }
1411
1412 private void addBucketToL2FloodGroup(NextObjective nextObj,
1413 List<Deque<GroupKey>> allActiveKeys,
1414 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001415 VlanId assignedVlan) {
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001416 Group l2FloodGroup = retrieveTopLevelGroup(allActiveKeys, deviceId,
1417 groupService, nextObj.id());
Yi Tseng78f51f42017-02-02 13:54:58 -08001418
1419 if (l2FloodGroup == null) {
1420 log.warn("Can't find L2 flood group while adding bucket to it. NextObj = {}",
1421 nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001422 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001423 return;
1424 }
Saurav Das1a129a02016-11-18 15:21:57 -08001425
Yi Tseng78f51f42017-02-02 13:54:58 -08001426 GroupKey l2floodGroupKey = l2FloodGroup.appCookie();
1427 int l2floodGroupId = l2FloodGroup.id().id();
1428 List<GroupBucket> newBuckets = generateNextGroupBuckets(groupInfos, ALL);
Yi Tseng78f51f42017-02-02 13:54:58 -08001429 GroupDescription l2FloodGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001430 new DefaultGroupDescription(deviceId,
1431 ALL,
1432 new GroupBuckets(newBuckets),
1433 l2floodGroupKey,
1434 l2floodGroupId,
1435 nextObj.appId());
Yi Tseng78f51f42017-02-02 13:54:58 -08001436 GroupChainElem l2FloodGroupChainElement =
1437 new GroupChainElem(l2FloodGroupDescription,
1438 groupInfos.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001439 true,
1440 deviceId);
Yi Tseng78f51f42017-02-02 13:54:58 -08001441
Yi Tseng78f51f42017-02-02 13:54:58 -08001442
1443 //ensure assignedVlan applies to the chosen group
1444 VlanId floodGroupVlan = extractVlanIdFromGroupId(l2floodGroupId);
Yi Tseng78f51f42017-02-02 13:54:58 -08001445 if (!floodGroupVlan.equals(assignedVlan)) {
1446 log.warn("VLAN ID {} does not match Flood group {} to which bucket is "
1447 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1448 Integer.toHexString(l2floodGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001449 fail(nextObj, ObjectiveError.BADPARAMS);
Yi Tseng78f51f42017-02-02 13:54:58 -08001450 return;
1451 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001452 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
Yi Tseng78f51f42017-02-02 13:54:58 -08001453 groupInfos.forEach(groupInfo -> {
1454 // update original NextGroup with new bucket-chain
Yi Tseng78f51f42017-02-02 13:54:58 -08001455 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001456 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001457 newBucketChain.addFirst(l2floodGroupKey);
Saurav Dasc88d4662017-05-15 15:34:25 -07001458 addedKeys.add(newBucketChain);
Yi Tsengef19de12017-04-24 11:33:05 -07001459 log.debug("Adding to L2FLOOD: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001460 deviceId, Integer.toHexString(l2floodGroupId),
1461 l2floodGroupKey, nextObj.id());
1462 // send the innermost group
1463 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001464 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001465 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001466 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l2FloodGroupChainElement);
Yi Tseng78f51f42017-02-02 13:54:58 -08001467
Yi Tsengef19de12017-04-24 11:33:05 -07001468 DeviceId innerMostGroupDevice = groupInfo.innerMostGroupDesc().deviceId();
1469 GroupKey innerMostGroupKey = groupInfo.innerMostGroupDesc().appCookie();
Yi Tseng78f51f42017-02-02 13:54:58 -08001470 Group existsL2IGroup = groupService.getGroup(innerMostGroupDevice, innerMostGroupKey);
1471
1472 if (existsL2IGroup != null) {
1473 // group already exist
1474 processPendingAddGroupsOrNextObjs(innerMostGroupKey, true);
1475 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001476 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001477 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001478 });
Saurav Dasc88d4662017-05-15 15:34:25 -07001479
1480 updatePendingNextObjective(l2floodGroupKey,
1481 new OfdpaNextGroup(addedKeys, nextObj));
Yi Tseng78f51f42017-02-02 13:54:58 -08001482 }
1483
Saurav Das1a129a02016-11-18 15:21:57 -08001484 private void addBucketToL3MulticastGroup(NextObjective nextObj,
1485 List<Deque<GroupKey>> allActiveKeys,
1486 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001487 VlanId assignedVlan) {
Pier Luigi21fffd22018-01-19 10:24:53 +01001488 // Create the buckets to add to the outermost L3 Multicast group
1489 List<GroupBucket> newBuckets = createL3MulticastBucket(groupInfos);
Saurav Das1a129a02016-11-18 15:21:57 -08001490
1491 // get the group being edited
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001492 Group l3mcastGroup = retrieveTopLevelGroup(allActiveKeys, deviceId,
1493 groupService, nextObj.id());
Saurav Das1a129a02016-11-18 15:21:57 -08001494 if (l3mcastGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001495 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001496 return;
1497 }
1498 GroupKey l3mcastGroupKey = l3mcastGroup.appCookie();
1499 int l3mcastGroupId = l3mcastGroup.id().id();
1500
1501 //ensure assignedVlan applies to the chosen group
Yi Tseng78f51f42017-02-02 13:54:58 -08001502 VlanId expectedVlan = extractVlanIdFromGroupId(l3mcastGroupId);
Saurav Das1a129a02016-11-18 15:21:57 -08001503 if (!expectedVlan.equals(assignedVlan)) {
1504 log.warn("VLAN ID {} does not match L3 Mcast group {} to which bucket is "
1505 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1506 Integer.toHexString(l3mcastGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001507 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001508 }
1509 GroupDescription l3mcastGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001510 new DefaultGroupDescription(deviceId,
1511 ALL,
1512 new GroupBuckets(newBuckets),
1513 l3mcastGroupKey,
1514 l3mcastGroupId,
1515 nextObj.appId());
Saurav Das1a129a02016-11-18 15:21:57 -08001516 GroupChainElem l3mcastGce = new GroupChainElem(l3mcastGroupDescription,
Yi Tsengef19de12017-04-24 11:33:05 -07001517 groupInfos.size(),
1518 true,
1519 deviceId);
Saurav Dasc88d4662017-05-15 15:34:25 -07001520
1521 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
Yi Tseng78f51f42017-02-02 13:54:58 -08001522 groupInfos.forEach(groupInfo -> {
1523 // update original NextGroup with new bucket-chain
1524 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001525 newBucketChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001526 // Add L3 interface group to the chain if there is one.
Yi Tsengef19de12017-04-24 11:33:05 -07001527 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1528 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001529 }
1530 newBucketChain.addFirst(l3mcastGroupKey);
Saurav Dasc88d4662017-05-15 15:34:25 -07001531 addedKeys.add(newBucketChain);
Yi Tseng78f51f42017-02-02 13:54:58 -08001532
Yi Tsengef19de12017-04-24 11:33:05 -07001533 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3mcastGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001534 // Point next group to inner-most group, if any
Yi Tsengef19de12017-04-24 11:33:05 -07001535 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1536 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(),
1537 1,
1538 false,
1539 deviceId);
1540 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001541 }
Yi Tsengef19de12017-04-24 11:33:05 -07001542 log.debug("Adding to L3MCAST: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001543 deviceId, Integer.toHexString(l3mcastGroupId),
1544 l3mcastGroupKey, nextObj.id());
1545 // send the innermost group
1546 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001547 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001548 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001549 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001550
1551 });
1552
Saurav Das1a129a02016-11-18 15:21:57 -08001553 updatePendingNextObjective(l3mcastGroupKey,
Saurav Dasc88d4662017-05-15 15:34:25 -07001554 new OfdpaNextGroup(addedKeys, nextObj));
Charles Chan188ebf52015-12-23 00:15:11 -08001555 }
1556
1557 /**
Saurav Dasceccf242017-08-03 18:30:35 -07001558 * Removes buckets in the top level group of a possible group-chain. Does
Saurav Das1a129a02016-11-18 15:21:57 -08001559 * not remove the groups in the group-chain pointed to by this bucket, as they
Charles Chan188ebf52015-12-23 00:15:11 -08001560 * may be in use (referenced by other groups) elsewhere.
1561 *
Saurav Dasceccf242017-08-03 18:30:35 -07001562 * @param nextObjective a next objective that contains information for the
1563 * buckets to be removed from the group
1564 * @param next the representation of the existing group-chains for this next
1565 * objective, from which the top-level buckets to remove are determined
Charles Chan188ebf52015-12-23 00:15:11 -08001566 */
1567 protected void removeBucketFromGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001568 if (nextObjective.type() != NextObjective.Type.HASHED &&
1569 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001570 log.warn("RemoveBuckets not applied to nextType:{} in dev:{} for next:{}",
1571 nextObjective.type(), deviceId, nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -07001572 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001573 return;
1574 }
Yi Tsengef19de12017-04-24 11:33:05 -07001575 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Saurav Dasceccf242017-08-03 18:30:35 -07001576 List<Integer> indicesToRemove = Lists.newArrayList();
1577 for (TrafficTreatment treatment : nextObjective.next()) {
1578 // find the top-level bucket in the group-chain by matching the
1579 // outport and label from different groups in the chain
1580 PortNumber portToRemove = readOutPortFromTreatment(treatment);
1581 int labelToRemove = readLabelFromTreatment(treatment);
1582 if (portToRemove == null) {
1583 log.warn("treatment {} of next objective {} has no outport.. "
1584 + "cannot remove bucket from group in dev: {}", treatment,
1585 nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001586 continue;
1587 }
Saurav Dasceccf242017-08-03 18:30:35 -07001588 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1589 groupService, deviceId,
1590 portToRemove, labelToRemove);
1591 indicesToRemove.addAll(existing);
1592
Charles Chan188ebf52015-12-23 00:15:11 -08001593 }
Saurav Dasceccf242017-08-03 18:30:35 -07001594
1595 List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList();
1596 indicesToRemove.forEach(index -> chainsToRemove
1597 .add(allActiveKeys.get(index)));
Yi Tseng78f51f42017-02-02 13:54:58 -08001598 if (chainsToRemove.isEmpty()) {
Charles Chan188ebf52015-12-23 00:15:11 -08001599 log.warn("Could not find appropriate group-chain for removing bucket"
1600 + " for next id {} in dev:{}", nextObjective.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001601 fail(nextObjective, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001602 return;
Charles Chan188ebf52015-12-23 00:15:11 -08001603 }
Saurav Dasceccf242017-08-03 18:30:35 -07001604 removeBucket(chainsToRemove, nextObjective);
1605 }
1606
1607 /**
1608 * Removes top-level buckets from a group that represents the given next objective.
1609 *
1610 * @param chainsToRemove a list of group bucket chains to remove
1611 * @param nextObjective the next objective that contains information for the
1612 * buckets to be removed from the group
1613 */
1614 protected void removeBucket(List<Deque<GroupKey>> chainsToRemove,
1615 NextObjective nextObjective) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001616 List<GroupBucket> bucketsToRemove = Lists.newArrayList();
1617 //first group key is the one we want to modify
1618 GroupKey modGroupKey = chainsToRemove.get(0).peekFirst();
Saurav Das1a129a02016-11-18 15:21:57 -08001619 Group modGroup = groupService.getGroup(deviceId, modGroupKey);
Ray Milkey43869812018-05-04 13:00:39 -07001620 if (modGroup == null) {
1621 log.warn("removeBucket(): Attempt to modify non-existent group {} for device {}", modGroupKey, deviceId);
1622 return;
1623 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001624 for (Deque<GroupKey> foundChain : chainsToRemove) {
1625 //second group key is the one we wish to remove the reference to
1626 if (foundChain.size() < 2) {
Saurav Dasceccf242017-08-03 18:30:35 -07001627 // additional check to make sure second group key exists in
Yi Tseng78f51f42017-02-02 13:54:58 -08001628 // the chain.
1629 log.warn("Can't find second group key from chain {}",
1630 foundChain);
1631 continue;
Saurav Das1a129a02016-11-18 15:21:57 -08001632 }
Saurav Dasceccf242017-08-03 18:30:35 -07001633 GroupKey pointedGroupKey = foundChain.stream()
1634 .collect(Collectors.toList()).get(1);
Yi Tseng78f51f42017-02-02 13:54:58 -08001635 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
Yi Tseng78f51f42017-02-02 13:54:58 -08001636 if (pointedGroup == null) {
1637 continue;
1638 }
1639
1640 GroupBucket bucket;
1641 if (nextObjective.type() == NextObjective.Type.HASHED) {
1642 bucket = DefaultGroupBucket.createSelectGroupBucket(
1643 DefaultTrafficTreatment.builder()
1644 .group(pointedGroup.id())
1645 .build());
1646 } else {
1647 bucket = DefaultGroupBucket.createAllGroupBucket(
1648 DefaultTrafficTreatment.builder()
1649 .group(pointedGroup.id())
1650 .build());
1651 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001652 bucketsToRemove.add(bucket);
Saurav Das1a129a02016-11-18 15:21:57 -08001653 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001654
1655 GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove);
1656 List<String> pointedGroupIds; // for debug log
1657 pointedGroupIds = bucketsToRemove.stream()
1658 .map(GroupBucket::treatment)
1659 .map(TrafficTreatment::allInstructions)
1660 .flatMap(List::stream)
1661 .filter(inst -> inst instanceof Instructions.GroupInstruction)
1662 .map(inst -> (Instructions.GroupInstruction) inst)
1663 .map(Instructions.GroupInstruction::groupId)
1664 .map(GroupId::id)
1665 .map(Integer::toHexString)
1666 .map(id -> HEX_PREFIX + id)
1667 .collect(Collectors.toList());
1668
Yi Tseng78f51f42017-02-02 13:54:58 -08001669 log.debug("Removing buckets from group id 0x{} pointing to group id(s) {} "
Ray Milkey43869812018-05-04 13:00:39 -07001670 + "for next id {} in device {}",
1671 Integer.toHexString(modGroup.id().id()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001672 pointedGroupIds, nextObjective.id(), deviceId);
1673 addPendingUpdateNextObjective(modGroupKey, nextObjective);
Saurav Das1a129a02016-11-18 15:21:57 -08001674 groupService.removeBucketsFromGroup(deviceId, modGroupKey,
1675 removeBuckets, modGroupKey,
1676 nextObjective.appId());
Saurav Dasceccf242017-08-03 18:30:35 -07001677 // update store - synchronize access as there may be multiple threads
1678 // trying to remove buckets from the same group, each with its own
1679 // potentially stale copy of allActiveKeys
Saurav Dasc88d4662017-05-15 15:34:25 -07001680 synchronized (flowObjectiveStore) {
Saurav Dasceccf242017-08-03 18:30:35 -07001681 // get a fresh copy of what the store holds
1682 NextGroup next = flowObjectiveStore.getNextGroup(nextObjective.id());
1683 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Pier Luigid008dbe2018-01-15 09:48:49 +01001684 allActiveKeys = Lists.newArrayList(allActiveKeys);
Saurav Dasc88d4662017-05-15 15:34:25 -07001685 // Note that since we got a new object, and ArrayDeque does not implement
Saurav Dasceccf242017-08-03 18:30:35 -07001686 // Object.equals(), we have to check the deque elems one by one
1687 allActiveKeys
1688 .removeIf(active ->
1689 chainsToRemove.stream().anyMatch(remove ->
1690 Arrays.equals(remove.toArray(new GroupKey[0]),
1691 active.toArray(new GroupKey[0]))));
Saurav Dasc88d4662017-05-15 15:34:25 -07001692 // If no buckets in the group, then retain an entry for the
1693 // top level group which still exists.
1694 if (allActiveKeys.isEmpty()) {
1695 ArrayDeque<GroupKey> top = new ArrayDeque<>();
1696 top.add(modGroupKey);
1697 allActiveKeys.add(top);
1698 }
1699 flowObjectiveStore.putNextGroup(nextObjective.id(),
Saurav Dasceccf242017-08-03 18:30:35 -07001700 new OfdpaNextGroup(allActiveKeys,
1701 nextObjective));
Saurav Das1a129a02016-11-18 15:21:57 -08001702 }
Charles Chan188ebf52015-12-23 00:15:11 -08001703 }
1704
1705 /**
Saurav Das961beb22017-03-29 19:09:17 -07001706 * Removes all groups in multiple possible group-chains that represent the next-obj.
Charles Chan188ebf52015-12-23 00:15:11 -08001707 *
1708 * @param nextObjective the next objective to remove
1709 * @param next the NextGroup that represents the existing group-chain for
1710 * this next objective
1711 */
1712 protected void removeGroup(NextObjective nextObjective, NextGroup next) {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -07001713
Yi Tsengef19de12017-04-24 11:33:05 -07001714 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Charles Chanfc5c7802016-05-17 13:13:55 -07001715
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001716 List<GroupKey> groupKeys = allActiveKeys.stream()
Charles Chanfc5c7802016-05-17 13:13:55 -07001717 .map(Deque::getFirst).collect(Collectors.toList());
Yi Tsengef19de12017-04-24 11:33:05 -07001718 addPendingRemoveNextObjective(nextObjective, groupKeys);
Charles Chanfc5c7802016-05-17 13:13:55 -07001719
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -07001720 allActiveKeys
1721 .forEach(groupChain -> groupChain.forEach(groupKey -> groupService
1722 .removeGroup(deviceId, groupKey, nextObjective.appId())));
Charles Chan188ebf52015-12-23 00:15:11 -08001723 flowObjectiveStore.removeNextGroup(nextObjective.id());
1724 }
1725
Saurav Dasceccf242017-08-03 18:30:35 -07001726 /**
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001727 * Modify buckets in the L2 interface group.
1728 *
1729 * @param nextObjective a next objective that contains information for the
1730 * buckets to be modified in the group
1731 * @param next the representation of the existing group-chains for this next
1732 * objective, from which the innermost group buckets to remove are determined
1733 */
1734 protected void modifyBucketFromGroup(NextObjective nextObjective, NextGroup next) {
1735 if (nextObjective.type() != NextObjective.Type.SIMPLE) {
1736 log.warn("ModifyBucketFromGroup cannot be applied to nextType:{} in dev:{} for next:{}",
1737 nextObjective.type(), deviceId, nextObjective.id());
1738 fail(nextObjective, ObjectiveError.UNSUPPORTED);
1739 return;
1740 }
1741
1742 VlanId assignedVlan = readVlanFromSelector(nextObjective.meta());
1743 if (assignedVlan == null) {
1744 log.warn("VLAN ID required by simple next obj is missing. Abort.");
1745 fail(nextObjective, ObjectiveError.BADPARAMS);
1746 return;
1747 }
1748
1749 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObjective, assignedVlan);
1750
1751 // There is only one L2 interface group in this case
1752 GroupDescription l2InterfaceGroupDesc = groupInfos.get(0).innerMostGroupDesc();
1753
1754 // Replace group bucket for L2 interface group
1755 groupService.setBucketsForGroup(deviceId,
1756 l2InterfaceGroupDesc.appCookie(),
1757 l2InterfaceGroupDesc.buckets(),
1758 l2InterfaceGroupDesc.appCookie(),
1759 l2InterfaceGroupDesc.appId());
1760
1761 // update store - synchronize access as there may be multiple threads
1762 // trying to remove buckets from the same group, each with its own
1763 // potentially stale copy of allActiveKeys
1764 synchronized (flowObjectiveStore) {
1765 List<Deque<GroupKey>> modifiedGroupKeys = Lists.newArrayList();
1766 ArrayDeque<GroupKey> top = new ArrayDeque<>();
1767 top.add(l2InterfaceGroupDesc.appCookie());
1768 modifiedGroupKeys.add(top);
1769
1770 flowObjectiveStore.putNextGroup(nextObjective.id(),
1771 new OfdpaNextGroup(modifiedGroupKeys,
1772 nextObjective));
1773 }
1774 }
1775
1776 /**
Saurav Dasceccf242017-08-03 18:30:35 -07001777 * Checks existing buckets in {@link NextGroup} to verify if they match
1778 * the buckets in the given {@link NextObjective}. Adds or removes buckets
1779 * to ensure that the buckets match up.
1780 *
1781 * @param nextObjective the next objective to verify
1782 * @param next the representation of the existing group which has to be
1783 * modified to match the given next objective
1784 */
1785 protected void verifyGroup(NextObjective nextObjective, NextGroup next) {
Pier Luigib72201b2018-01-25 16:16:02 +01001786 if (nextObjective.type() == NextObjective.Type.SIMPLE) {
1787 log.warn("verification not supported for indirect group");
Saurav Dasceccf242017-08-03 18:30:35 -07001788 fail(nextObjective, ObjectiveError.UNSUPPORTED);
1789 return;
1790 }
Charles Chan1dd75572018-05-08 11:49:05 -07001791 log.trace("Call to verify device:{} nextId:{}", deviceId, nextObjective.id());
Saurav Dasceccf242017-08-03 18:30:35 -07001792 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
1793 List<TrafficTreatment> bucketsToCreate = Lists.newArrayList();
1794 List<Integer> indicesToRemove = Lists.newArrayList();
Pier Luigiec6ac422018-01-29 10:30:59 +01001795
1796 // Iterating over the treatments of the next objective allows
1797 // to detect missing buckets and/or duplicate buckets (to be removed)
Saurav Dasceccf242017-08-03 18:30:35 -07001798 for (TrafficTreatment bkt : nextObjective.next()) {
1799 PortNumber portNumber = readOutPortFromTreatment(bkt);
1800 int label = readLabelFromTreatment(bkt);
1801 if (portNumber == null) {
1802 log.warn("treatment {} of next objective {} has no outport.. "
1803 + "cannot remove bucket from group in dev: {}", bkt,
1804 nextObjective.id(), deviceId);
1805 fail(nextObjective, ObjectiveError.BADPARAMS);
1806 return;
1807 }
1808 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1809 groupService, deviceId,
1810 portNumber, label);
1811 if (existing.isEmpty()) {
1812 // if it doesn't exist, mark this bucket for creation
1813 bucketsToCreate.add(bkt);
1814 }
1815 if (existing.size() > 1) {
1816 // if it exists but there are duplicates, mark the others for removal
1817 existing.remove(0);
1818 indicesToRemove.addAll(existing);
1819 }
1820 }
1821
Pier Luigiec6ac422018-01-29 10:30:59 +01001822 // Detect situation where the next data has more buckets
1823 // (not duplicates) respect to the next objective
Saurav Dasa4020382018-02-14 14:14:54 -08001824 if (allActiveKeys.size() > nextObjective.next().size() &&
1825 // ignore specific case of empty group
1826 !(nextObjective.next().size() == 0 && allActiveKeys.size() == 1
1827 && allActiveKeys.get(0).size() == 1)) {
Pier Luigiec6ac422018-01-29 10:30:59 +01001828 log.warn("Mismatch detected between next and flowobjstore for device {}: " +
Saurav Dasa4020382018-02-14 14:14:54 -08001829 "nextId:{}, nextObjective-size:{} next-size:{} .. correcting",
1830 deviceId, nextObjective.id(), nextObjective.next().size(),
1831 allActiveKeys.size());
1832 List<Integer> otherIndices =
1833 indicesToRemoveFromNextGroup(allActiveKeys, nextObjective,
1834 groupService, deviceId);
Pier Luigiec6ac422018-01-29 10:30:59 +01001835 // Filter out the indices not present
1836 otherIndices = otherIndices.stream()
1837 .filter(index -> !indicesToRemove.contains(index))
1838 .collect(Collectors.toList());
1839 // Add all to the final list
1840 indicesToRemove.addAll(otherIndices);
1841 }
1842
Charles Chan1dd75572018-05-08 11:49:05 -07001843 log.trace("Buckets to create {}", bucketsToCreate);
1844 log.trace("Indices to remove {}", indicesToRemove);
Pier Luigib72201b2018-01-25 16:16:02 +01001845
Saurav Dasceccf242017-08-03 18:30:35 -07001846 if (!bucketsToCreate.isEmpty()) {
1847 log.info("creating {} buckets as part of nextId: {} verification",
1848 bucketsToCreate.size(), nextObjective.id());
1849 //create a nextObjective only with these buckets
1850 NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder()
1851 .withId(nextObjective.id())
Pier Luigib72201b2018-01-25 16:16:02 +01001852 .withType(nextObjective.type())
Saurav Dasceccf242017-08-03 18:30:35 -07001853 .withMeta(nextObjective.meta())
1854 .fromApp(nextObjective.appId());
Pier Luigib72201b2018-01-25 16:16:02 +01001855 bucketsToCreate.forEach(nextObjBuilder::addTreatment);
1856 // According to the next type we call the proper add function
1857 if (nextObjective.type() == NextObjective.Type.HASHED) {
1858 addBucketToHashGroup(nextObjBuilder.addToExisting(), allActiveKeys);
1859 } else {
1860 addBucketToBroadcastGroup(nextObjBuilder.addToExisting(), allActiveKeys);
1861 }
Saurav Dasceccf242017-08-03 18:30:35 -07001862 }
1863
1864 if (!indicesToRemove.isEmpty()) {
1865 log.info("removing {} buckets as part of nextId: {} verification",
1866 indicesToRemove.size(), nextObjective.id());
1867 List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList();
1868 indicesToRemove.forEach(index -> chainsToRemove
1869 .add(allActiveKeys.get(index)));
1870 removeBucket(chainsToRemove, nextObjective);
1871 }
1872
Saurav Das9df5b7c2017-08-14 16:44:43 -07001873 if (bucketsToCreate.isEmpty() && indicesToRemove.isEmpty()) {
1874 // flowObjective store record is in-sync with nextObjective passed-in
1875 // Nevertheless groupStore may not be in sync due to bug in the store
1876 // - see CORD-1844. XXX When this bug is fixed, the rest of this verify
1877 // method will not be required.
Pier Luigib72201b2018-01-25 16:16:02 +01001878 GroupKey topGroupKey = allActiveKeys.get(0).peekFirst();
1879 Group topGroup = groupService.getGroup(deviceId, topGroupKey);
1880 int actualGroupSize = topGroup.buckets().buckets().size();
Saurav Das9df5b7c2017-08-14 16:44:43 -07001881 int objGroupSize = nextObjective.next().size();
1882 if (actualGroupSize != objGroupSize) {
1883 log.warn("Mismatch detected in device:{}, nextId:{}, nextObjective-size"
1884 + ":{} group-size:{} .. correcting", deviceId, nextObjective.id(),
1885 objGroupSize, actualGroupSize);
1886 }
1887 if (actualGroupSize > objGroupSize) {
Pier Luigib72201b2018-01-25 16:16:02 +01001888 // Group in the device has more chains
Saurav Das9df5b7c2017-08-14 16:44:43 -07001889 List<GroupBucket> bucketsToRemove = Lists.newArrayList();
1890 //check every bucket in the actual group
Pier Luigib72201b2018-01-25 16:16:02 +01001891 for (GroupBucket bucket : topGroup.buckets().buckets()) {
Saurav Das9df5b7c2017-08-14 16:44:43 -07001892 GroupInstruction g = (GroupInstruction) bucket.treatment()
1893 .allInstructions().iterator().next();
1894 GroupId gidToCheck = g.groupId(); // the group pointed to
1895 boolean matches = false;
1896 for (Deque<GroupKey> validChain : allActiveKeys) {
1897 if (validChain.size() < 2) {
1898 continue;
1899 }
1900 GroupKey pointedGroupKey = validChain.stream()
1901 .collect(Collectors.toList()).get(1);
1902 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
1903 if (pointedGroup != null && gidToCheck.equals(pointedGroup.id())) {
1904 matches = true;
1905 break;
1906 }
1907 }
1908 if (!matches) {
1909 log.warn("Removing bucket pointing to groupId:{}", gidToCheck);
1910 bucketsToRemove.add(bucket);
1911 }
1912 }
1913 // remove buckets for which there was no record in the obj store
1914 if (bucketsToRemove.isEmpty()) {
1915 log.warn("Mismatch detected but could not determine which"
1916 + "buckets to remove");
1917 } else {
1918 GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove);
Pier Luigib72201b2018-01-25 16:16:02 +01001919 groupService.removeBucketsFromGroup(deviceId, topGroupKey,
1920 removeBuckets, topGroupKey,
Saurav Das9df5b7c2017-08-14 16:44:43 -07001921 nextObjective.appId());
1922 }
1923 } else if (actualGroupSize < objGroupSize) {
Pier Luigib72201b2018-01-25 16:16:02 +01001924 // Group in the device has less chains
Saurav Das9df5b7c2017-08-14 16:44:43 -07001925 // should also add buckets not in group-store but in obj-store
1926 List<GroupBucket> bucketsToAdd = Lists.newArrayList();
1927 //check every bucket in the obj
1928 for (Deque<GroupKey> validChain : allActiveKeys) {
1929 if (validChain.size() < 2) {
1930 continue;
1931 }
1932 GroupKey pointedGroupKey = validChain.stream()
1933 .collect(Collectors.toList()).get(1);
1934 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
1935 if (pointedGroup == null) {
1936 // group should exist, otherwise cannot be added as bucket
1937 continue;
1938 }
1939 boolean matches = false;
Pier Luigib72201b2018-01-25 16:16:02 +01001940 for (GroupBucket bucket : topGroup.buckets().buckets()) {
Saurav Das9df5b7c2017-08-14 16:44:43 -07001941 GroupInstruction g = (GroupInstruction) bucket.treatment()
1942 .allInstructions().iterator().next();
1943 GroupId gidToCheck = g.groupId(); // the group pointed to
1944 if (pointedGroup.id().equals(gidToCheck)) {
1945 matches = true;
1946 break;
1947 }
1948 }
1949 if (!matches) {
1950 log.warn("Adding bucket pointing to groupId:{}", pointedGroup);
1951 TrafficTreatment t = DefaultTrafficTreatment.builder()
1952 .group(pointedGroup.id())
1953 .build();
Pier Luigib72201b2018-01-25 16:16:02 +01001954 // Create the proper bucket according to the next type
1955 if (nextObjective.type() == NextObjective.Type.HASHED) {
1956 bucketsToAdd.add(DefaultGroupBucket.createSelectGroupBucket(t));
1957 } else {
1958 bucketsToAdd.add(DefaultGroupBucket.createAllGroupBucket(t));
1959 }
Saurav Das9df5b7c2017-08-14 16:44:43 -07001960 }
1961 }
1962 if (bucketsToAdd.isEmpty()) {
1963 log.warn("Mismatch detected but could not determine which "
1964 + "buckets to add");
1965 } else {
1966 GroupBuckets addBuckets = new GroupBuckets(bucketsToAdd);
Pier Luigib72201b2018-01-25 16:16:02 +01001967 groupService.addBucketsToGroup(deviceId, topGroupKey,
1968 addBuckets, topGroupKey,
Saurav Das9df5b7c2017-08-14 16:44:43 -07001969 nextObjective.appId());
1970 }
1971 }
1972 }
1973
Saurav Dasceccf242017-08-03 18:30:35 -07001974 pass(nextObjective);
1975 }
1976
1977 //////////////////////////////////////
1978 // Helper methods and classes
1979 //////////////////////////////////////
1980
Yi Tsengef19de12017-04-24 11:33:05 -07001981 protected void updatePendingNextObjective(GroupKey groupKey, OfdpaNextGroup nextGrp) {
1982 pendingAddNextObjectives.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07001983 if (val == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001984 val = new CopyOnWriteArrayList<>();
Saurav Das961beb22017-03-29 19:09:17 -07001985 }
Yi Tsengef19de12017-04-24 11:33:05 -07001986 val.add(nextGrp);
Saurav Das961beb22017-03-29 19:09:17 -07001987 return val;
1988 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08001989 }
1990
Yi Tsengef19de12017-04-24 11:33:05 -07001991 protected void updatePendingGroups(GroupKey groupKey, GroupChainElem gce) {
1992 pendingGroups.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07001993 if (val == null) {
1994 val = Sets.newConcurrentHashSet();
1995 }
1996 val.add(gce);
1997 return val;
1998 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08001999 }
2000
Yi Tsengef19de12017-04-24 11:33:05 -07002001 protected void addPendingUpdateNextObjective(GroupKey groupKey,
2002 NextObjective nextObjective) {
Yi Tseng78f51f42017-02-02 13:54:58 -08002003 pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> {
2004 if (nextObjs != null) {
2005 nextObjs.add(nextObjective);
2006 } else {
2007 nextObjs = Sets.newHashSet(nextObjective);
2008 }
2009 return nextObjs;
2010 });
2011 }
2012
Saurav Das2f2c9d02018-04-07 16:51:09 -07002013 protected void addPendingRemoveNextObjective(NextObjective nextObjective,
2014 List<GroupKey> groupKeys) {
2015 pendingRemoveNextObjectives.put(nextObjective, groupKeys);
Yi Tsengef19de12017-04-24 11:33:05 -07002016 }
2017
Saurav Das2f2c9d02018-04-07 16:51:09 -07002018 protected int getNextAvailableIndex() {
2019 return (int) nextIndex.incrementAndGet();
2020 }
2021
2022 protected void processPendingUpdateNextObjs(GroupKey groupKey) {
2023 Set<NextObjective> nextObjs = pendingUpdateNextObjectives.remove(groupKey);
2024 if (nextObjs != null) {
2025 nextObjs.forEach(nextObj -> {
2026 log.debug("Group {} updated, update pending next objective {}.",
2027 groupKey, nextObj);
2028 pass(nextObj);
2029 });
2030 }
2031 }
2032
2033 protected void processPendingRemoveNextObjs(GroupKey key) {
Yi Tsengef19de12017-04-24 11:33:05 -07002034 pendingRemoveNextObjectives.asMap().forEach((nextObjective, groupKeys) -> {
jaegonkim2e4d99e2018-07-09 23:23:10 +09002035 groupKeys.remove(key);
Yi Tsengef19de12017-04-24 11:33:05 -07002036 if (groupKeys.isEmpty()) {
2037 pendingRemoveNextObjectives.invalidate(nextObjective);
2038 pass(nextObjective);
Yi Tsengef19de12017-04-24 11:33:05 -07002039 }
2040 });
2041 }
2042
Yi Tsengef19de12017-04-24 11:33:05 -07002043 protected void processPendingAddGroupsOrNextObjs(GroupKey key, boolean added) {
2044 //first check for group chain
2045 Set<OfdpaGroupHandlerUtility.GroupChainElem> gceSet = pendingGroups.asMap().remove(key);
2046 if (gceSet != null) {
2047 for (GroupChainElem gce : gceSet) {
2048 log.debug("Group service {} group key {} in device {}. "
2049 + "Processing next group in group chain with group id 0x{}",
2050 (added) ? "ADDED" : "processed",
2051 key, deviceId,
2052 Integer.toHexString(gce.groupDescription().givenGroupId()));
2053 processGroupChain(gce);
2054 }
2055 } else {
2056 // otherwise chain complete - check for waiting nextObjectives
2057 List<OfdpaGroupHandlerUtility.OfdpaNextGroup> nextGrpList =
2058 pendingAddNextObjectives.getIfPresent(key);
2059 if (nextGrpList != null) {
2060 pendingAddNextObjectives.invalidate(key);
2061 nextGrpList.forEach(nextGrp -> {
2062 log.debug("Group service {} group key {} in device:{}. "
2063 + "Done implementing next objective: {} <<-->> gid:0x{}",
2064 (added) ? "ADDED" : "processed",
2065 key, deviceId, nextGrp.nextObjective().id(),
2066 Integer.toHexString(groupService.getGroup(deviceId, key)
2067 .givenGroupId()));
2068 pass(nextGrp.nextObjective());
Saurav Dasc88d4662017-05-15 15:34:25 -07002069 updateFlowObjectiveStore(nextGrp.nextObjective().id(), nextGrp);
Yi Tsengef19de12017-04-24 11:33:05 -07002070
2071 // check if addBuckets waiting for this completion
2072 pendingBuckets.compute(nextGrp.nextObjective().id(), (nextId, pendBkts) -> {
2073 if (pendBkts != null) {
2074 pendBkts.forEach(pendBkt -> addBucketToGroup(pendBkt, nextGrp));
2075 }
2076 return null;
2077 });
2078 });
2079 }
2080 }
2081 }
2082
Charles Chan188ebf52015-12-23 00:15:11 -08002083 /**
2084 * Processes next element of a group chain. Assumption is that if this
2085 * group points to another group, the latter has already been created
2086 * and this driver has received notification for it. A second assumption is
2087 * that if there is another group waiting for this group then the appropriate
2088 * stores already have the information to act upon the notification for the
2089 * creation of this group.
2090 * <p>
2091 * The processing of the GroupChainElement depends on the number of groups
2092 * this element is waiting on. For all group types other than SIMPLE, a
2093 * GroupChainElement could be waiting on multiple groups.
2094 *
2095 * @param gce the group chain element to be processed next
2096 */
2097 private void processGroupChain(GroupChainElem gce) {
2098 int waitOnGroups = gce.decrementAndGetGroupsWaitedOn();
2099 if (waitOnGroups != 0) {
2100 log.debug("GCE: {} not ready to be processed", gce);
2101 return;
2102 }
2103 log.debug("GCE: {} ready to be processed", gce);
Yi Tsengef19de12017-04-24 11:33:05 -07002104 if (gce.addBucketToGroup()) {
2105 groupService.addBucketsToGroup(gce.groupDescription().deviceId(),
2106 gce.groupDescription().appCookie(),
2107 gce.groupDescription().buckets(),
2108 gce.groupDescription().appCookie(),
2109 gce.groupDescription().appId());
Charles Chan188ebf52015-12-23 00:15:11 -08002110 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07002111 groupService.addGroup(gce.groupDescription());
Charles Chan188ebf52015-12-23 00:15:11 -08002112 }
2113 }
2114
Saurav Dasc88d4662017-05-15 15:34:25 -07002115 private void updateFlowObjectiveStore(Integer nextId, OfdpaNextGroup nextGrp) {
2116 synchronized (flowObjectiveStore) {
2117 // get fresh copy of what the store holds
2118 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
2119 if (next == null || nextGrp.nextObjective().op() == Operation.ADD) {
2120 flowObjectiveStore.putNextGroup(nextId, nextGrp);
2121 return;
2122 }
2123 if (nextGrp.nextObjective().op() == Operation.ADD_TO_EXISTING) {
2124 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Pier Luigid008dbe2018-01-15 09:48:49 +01002125 allActiveKeys = Lists.newArrayList(allActiveKeys);
Saurav Dasc88d4662017-05-15 15:34:25 -07002126 // If active keys shows only the top-level group without a chain of groups,
2127 // then it represents an empty group. Update by replacing empty chain.
2128 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
2129 allActiveKeys.clear();
2130 }
2131 allActiveKeys.addAll(nextGrp.allKeys());
2132 flowObjectiveStore.putNextGroup(nextId,
2133 new OfdpaNextGroup(allActiveKeys, nextGrp.nextObjective()));
2134 }
2135 }
2136 }
2137
Saurav Das8be4e3a2016-03-11 17:19:07 -08002138 private class InnerGroupListener implements GroupListener {
2139 @Override
2140 public void event(GroupEvent event) {
Charles Chanfc5c7802016-05-17 13:13:55 -07002141 switch (event.type()) {
2142 case GROUP_ADDED:
2143 processPendingAddGroupsOrNextObjs(event.subject().appCookie(), true);
2144 break;
2145 case GROUP_REMOVED:
2146 processPendingRemoveNextObjs(event.subject().appCookie());
2147 break;
Yi Tseng78f51f42017-02-02 13:54:58 -08002148 case GROUP_UPDATED:
2149 processPendingUpdateNextObjs(event.subject().appCookie());
2150 break;
Charles Chanfc5c7802016-05-17 13:13:55 -07002151 default:
2152 break;
Saurav Das8be4e3a2016-03-11 17:19:07 -08002153 }
2154 }
2155 }
Charles Chan188ebf52015-12-23 00:15:11 -08002156}