blob: 27fc7913f3c8ced1ccac951facfd9c07f5a14af4 [file] [log] [blame]
Brian O'Connor7cbbbb72016-04-09 02:13:23 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
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 */
Charles Chan188ebf52015-12-23 00:15:11 -080016package org.onosproject.driver.pipeline;
17
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;
31import org.onosproject.core.DefaultGroupId;
Yi Tseng78f51f42017-02-02 13:54:58 -080032import org.onosproject.core.GroupId;
Charles Chan32562522016-04-07 14:37:14 -070033import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Charles Chan188ebf52015-12-23 00:15:11 -080034import org.onosproject.net.DeviceId;
35import org.onosproject.net.PortNumber;
36import org.onosproject.net.behaviour.NextGroup;
37import org.onosproject.net.behaviour.PipelinerContext;
38import org.onosproject.net.flow.DefaultTrafficTreatment;
39import org.onosproject.net.flow.TrafficSelector;
40import org.onosproject.net.flow.TrafficTreatment;
41import org.onosproject.net.flow.criteria.Criterion;
Pier Ventre42287df2016-11-09 14:17:26 -080042import org.onosproject.net.flow.criteria.TunnelIdCriterion;
Charles Chan188ebf52015-12-23 00:15:11 -080043import org.onosproject.net.flow.criteria.VlanIdCriterion;
44import org.onosproject.net.flow.instructions.Instruction;
45import org.onosproject.net.flow.instructions.Instructions;
46import org.onosproject.net.flow.instructions.L2ModificationInstruction;
47import org.onosproject.net.flowobjective.FlowObjectiveStore;
48import org.onosproject.net.flowobjective.NextObjective;
49import org.onosproject.net.flowobjective.ObjectiveError;
50import org.onosproject.net.group.DefaultGroupBucket;
51import org.onosproject.net.group.DefaultGroupDescription;
52import org.onosproject.net.group.DefaultGroupKey;
53import org.onosproject.net.group.Group;
54import org.onosproject.net.group.GroupBucket;
55import org.onosproject.net.group.GroupBuckets;
56import org.onosproject.net.group.GroupDescription;
57import org.onosproject.net.group.GroupEvent;
58import org.onosproject.net.group.GroupKey;
59import org.onosproject.net.group.GroupListener;
60import org.onosproject.net.group.GroupService;
Saurav Das8be4e3a2016-03-11 17:19:07 -080061import org.onosproject.store.service.AtomicCounter;
62import org.onosproject.store.service.StorageService;
Charles Chan188ebf52015-12-23 00:15:11 -080063import org.slf4j.Logger;
64
65import java.util.ArrayDeque;
66import java.util.ArrayList;
67import java.util.Collection;
68import java.util.Collections;
69import java.util.Deque;
70import java.util.List;
Charles Chand0fd5dc2016-02-16 23:14:49 -080071import java.util.Objects;
Charles Chan188ebf52015-12-23 00:15:11 -080072import java.util.Set;
73import java.util.concurrent.ConcurrentHashMap;
74import java.util.concurrent.CopyOnWriteArrayList;
75import java.util.concurrent.Executors;
76import java.util.concurrent.ScheduledExecutorService;
77import java.util.concurrent.TimeUnit;
78import java.util.concurrent.atomic.AtomicInteger;
79import java.util.stream.Collectors;
80
81import static org.onlab.util.Tools.groupedThreads;
Pier Ventre140a8942016-11-02 07:26:38 -070082import static org.onosproject.driver.pipeline.Ofdpa2GroupHandler.OfdpaMplsGroupSubType.OFDPA_GROUP_TYPE_SHIFT;
83import static org.onosproject.driver.pipeline.Ofdpa2GroupHandler.OfdpaMplsGroupSubType.OFDPA_MPLS_SUBTYPE_SHIFT;
84import static org.onosproject.driver.pipeline.Ofdpa2Pipeline.isNotMplsBos;
Pier Ventre42287df2016-11-09 14:17:26 -080085import static org.onosproject.net.flow.criteria.Criterion.Type.TUNNEL_ID;
Pier Ventre140a8942016-11-02 07:26:38 -070086import static org.onosproject.net.flow.criteria.Criterion.Type.VLAN_VID;
87import static org.onosproject.net.flowobjective.NextObjective.Type.HASHED;
Yi Tseng78f51f42017-02-02 13:54:58 -080088import static org.onosproject.net.group.GroupDescription.Type.ALL;
89import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Charles Chan188ebf52015-12-23 00:15:11 -080090import static org.slf4j.LoggerFactory.getLogger;
91
92/**
Charles Chan40132b32017-01-22 00:19:37 -080093 * Group handler that emulates Broadcom OF-DPA TTP on CpqD.
Charles Chan188ebf52015-12-23 00:15:11 -080094 */
Charles Chan361154b2016-03-24 10:23:39 -070095public class Ofdpa2GroupHandler {
Charles Chan188ebf52015-12-23 00:15:11 -080096 /*
97 * OFDPA requires group-id's to have a certain form.
98 * L2 Interface Groups have <4bits-0><12bits-vlanid><16bits-portid>
99 * L3 Unicast Groups have <4bits-2><28bits-index>
100 * MPLS Interface Groups have <4bits-9><4bits:0><24bits-index>
101 * L3 ECMP Groups have <4bits-7><28bits-index>
102 * L2 Flood Groups have <4bits-4><12bits-vlanid><16bits-index>
103 * L3 VPN Groups have <4bits-9><4bits-2><24bits-index>
104 */
Charles Chan425854b2016-04-11 15:32:12 -0700105 protected static final int L2_INTERFACE_TYPE = 0x00000000;
106 protected static final int L3_INTERFACE_TYPE = 0x50000000;
107 protected static final int L3_UNICAST_TYPE = 0x20000000;
108 protected static final int L3_MULTICAST_TYPE = 0x60000000;
109 protected static final int MPLS_INTERFACE_TYPE = 0x90000000;
110 protected static final int MPLS_L3VPN_SUBTYPE = 0x92000000;
111 protected static final int L3_ECMP_TYPE = 0x70000000;
112 protected static final int L2_FLOOD_TYPE = 0x40000000;
Charles Chane849c192016-01-11 18:28:54 -0800113
Charles Chan425854b2016-04-11 15:32:12 -0700114 protected static final int TYPE_MASK = 0x0fffffff;
115 protected static final int SUBTYPE_MASK = 0x00ffffff;
116 protected static final int TYPE_VLAN_MASK = 0x0000ffff;
Charles Chane849c192016-01-11 18:28:54 -0800117
Charles Chan372b63e2017-02-07 12:10:53 -0800118 protected static final int THREE_BIT_MASK = 0x0fff;
119 protected static final int FOUR_BIT_MASK = 0xffff;
120 protected static final int PORT_LEN = 16;
121
Charles Chan425854b2016-04-11 15:32:12 -0700122 protected static final int PORT_LOWER_BITS_MASK = 0x3f;
123 protected static final long PORT_HIGHER_BITS_MASK = ~PORT_LOWER_BITS_MASK;
Charles Chan188ebf52015-12-23 00:15:11 -0800124
Yi Tseng78f51f42017-02-02 13:54:58 -0800125 protected static final String HEX_PREFIX = "0x";
126
Charles Chan188ebf52015-12-23 00:15:11 -0800127 private final Logger log = getLogger(getClass());
128 private ServiceDirectory serviceDirectory;
129 protected GroupService groupService;
Saurav Das8be4e3a2016-03-11 17:19:07 -0800130 protected StorageService storageService;
Charles Chan188ebf52015-12-23 00:15:11 -0800131
Charles Chan425854b2016-04-11 15:32:12 -0700132 protected DeviceId deviceId;
Charles Chan188ebf52015-12-23 00:15:11 -0800133 private FlowObjectiveStore flowObjectiveStore;
Charles Chanfc5c7802016-05-17 13:13:55 -0700134 private Cache<GroupKey, List<OfdpaNextGroup>> pendingAddNextObjectives;
135 private Cache<NextObjective, List<GroupKey>> pendingRemoveNextObjectives;
Charles Chan188ebf52015-12-23 00:15:11 -0800136 private ConcurrentHashMap<GroupKey, Set<GroupChainElem>> pendingGroups;
Yi Tseng78f51f42017-02-02 13:54:58 -0800137 private ConcurrentHashMap<GroupKey, Set<NextObjective>> pendingUpdateNextObjectives;
Charles Chan188ebf52015-12-23 00:15:11 -0800138 private ScheduledExecutorService groupChecker =
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700139 Executors.newScheduledThreadPool(2, groupedThreads("onos/pipeliner", "ofdpa2-%d", log));
Charles Chan188ebf52015-12-23 00:15:11 -0800140
141 // index number for group creation
Saurav Das8be4e3a2016-03-11 17:19:07 -0800142 private AtomicCounter nextIndex;
Charles Chan188ebf52015-12-23 00:15:11 -0800143
Charles Chan188ebf52015-12-23 00:15:11 -0800144 // local store for pending bucketAdds - by design there can only be one
145 // pending bucket for a group
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700146 protected ConcurrentHashMap<Integer, NextObjective> pendingBuckets =
147 new ConcurrentHashMap<>();
Charles Chan188ebf52015-12-23 00:15:11 -0800148
Charles Chan40132b32017-01-22 00:19:37 -0800149 /**
150 * Determines whether this pipeline support copy ttl instructions or not.
151 *
152 * @return true if copy ttl instructions are supported
153 */
154 protected boolean supportCopyTtl() {
155 return true;
156 }
157
158 /**
159 * Determines whether this pipeline support set mpls bos instruction or not.
160 *
161 * @return true if set mpls bos instruction is supported
162 */
163 protected boolean supportSetMplsBos() {
164 return true;
165 }
166
Charles Chan188ebf52015-12-23 00:15:11 -0800167 protected void init(DeviceId deviceId, PipelinerContext context) {
168 this.deviceId = deviceId;
169 this.flowObjectiveStore = context.store();
170 this.serviceDirectory = context.directory();
171 this.groupService = serviceDirectory.get(GroupService.class);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800172 this.storageService = serviceDirectory.get(StorageService.class);
Madan Jampanid5714e02016-04-19 14:15:20 -0700173 this.nextIndex = storageService.getAtomicCounter("group-id-index-counter");
Charles Chan188ebf52015-12-23 00:15:11 -0800174
Charles Chanfc5c7802016-05-17 13:13:55 -0700175 pendingAddNextObjectives = CacheBuilder.newBuilder()
Charles Chan188ebf52015-12-23 00:15:11 -0800176 .expireAfterWrite(20, TimeUnit.SECONDS)
177 .removalListener((
178 RemovalNotification<GroupKey, List<OfdpaNextGroup>> notification) -> {
179 if (notification.getCause() == RemovalCause.EXPIRED) {
180 notification.getValue().forEach(ofdpaNextGrp ->
Charles Chan361154b2016-03-24 10:23:39 -0700181 Ofdpa2Pipeline.fail(ofdpaNextGrp.nextObj,
Charles Chan188ebf52015-12-23 00:15:11 -0800182 ObjectiveError.GROUPINSTALLATIONFAILED));
Charles Chanfc5c7802016-05-17 13:13:55 -0700183 }
184 }).build();
Charles Chan188ebf52015-12-23 00:15:11 -0800185
Charles Chanfc5c7802016-05-17 13:13:55 -0700186 pendingRemoveNextObjectives = CacheBuilder.newBuilder()
187 .expireAfterWrite(20, TimeUnit.SECONDS)
188 .removalListener((
189 RemovalNotification<NextObjective, List<GroupKey>> notification) -> {
190 if (notification.getCause() == RemovalCause.EXPIRED) {
191 Ofdpa2Pipeline.fail(notification.getKey(),
192 ObjectiveError.GROUPREMOVALFAILED);
Charles Chan188ebf52015-12-23 00:15:11 -0800193 }
194 }).build();
195 pendingGroups = new ConcurrentHashMap<>();
Yi Tseng78f51f42017-02-02 13:54:58 -0800196 pendingUpdateNextObjectives = new ConcurrentHashMap<>();
Charles Chan188ebf52015-12-23 00:15:11 -0800197 groupChecker.scheduleAtFixedRate(new GroupChecker(), 0, 500, TimeUnit.MILLISECONDS);
198
199 groupService.addListener(new InnerGroupListener());
200 }
201
Pier Ventre140a8942016-11-02 07:26:38 -0700202 /**
203 * The purpose of this function is to verify if the hashed next
204 * objective is supported by the current pipeline.
205 *
206 * @param nextObjective the hashed objective to verify
207 * @return true if the hashed objective is supported. Otherwise false.
208 */
209 public boolean verifyHashedNextObjective(NextObjective nextObjective) {
210 // if it is not hashed, there is something wrong;
211 if (nextObjective.type() != HASHED) {
212 return false;
213 }
214 // The case non supported is the MPLS-ECMP. For now, we try
215 // to create a MPLS-ECMP for the transport of a VPWS. The
216 // necessary info are contained in the meta selector. In particular
217 // we are looking for the case of BoS==False;
218 TrafficSelector metaSelector = nextObjective.meta();
219 if (metaSelector != null && isNotMplsBos(metaSelector)) {
220 return false;
221 }
222
223 return true;
224 }
225
Saurav Das8be4e3a2016-03-11 17:19:07 -0800226 //////////////////////////////////////
227 // Group Creation
228 //////////////////////////////////////
229
Charles Chan188ebf52015-12-23 00:15:11 -0800230 protected void addGroup(NextObjective nextObjective) {
231 switch (nextObjective.type()) {
232 case SIMPLE:
233 Collection<TrafficTreatment> treatments = nextObjective.next();
234 if (treatments.size() != 1) {
235 log.error("Next Objectives of type Simple should only have a "
236 + "single Traffic Treatment. Next Objective Id:{}",
237 nextObjective.id());
Charles Chan361154b2016-03-24 10:23:39 -0700238 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800239 return;
240 }
241 processSimpleNextObjective(nextObjective);
242 break;
243 case BROADCAST:
244 processBroadcastNextObjective(nextObjective);
245 break;
246 case HASHED:
Pier Ventre140a8942016-11-02 07:26:38 -0700247 if (!verifyHashedNextObjective(nextObjective)) {
248 log.error("Next Objectives of type hashed not supported. Next Objective Id:{}",
249 nextObjective.id());
250 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.BADPARAMS);
251 return;
252 }
Charles Chan188ebf52015-12-23 00:15:11 -0800253 processHashedNextObjective(nextObjective);
254 break;
255 case FAILOVER:
Charles Chan361154b2016-03-24 10:23:39 -0700256 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -0800257 log.warn("Unsupported next objective type {}", nextObjective.type());
258 break;
259 default:
Charles Chan361154b2016-03-24 10:23:39 -0700260 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.UNKNOWN);
Charles Chan188ebf52015-12-23 00:15:11 -0800261 log.warn("Unknown next objective type {}", nextObjective.type());
262 }
263 }
264
265 /**
266 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
267 * a chain of groups. The simple Next Objective passed
268 * in by the application has to be broken up into a group chain
269 * comprising of an L3 Unicast Group that points to an L2 Interface
270 * Group which in-turn points to an output port. In some cases, the simple
271 * next Objective can just be an L2 interface without the need for chaining.
272 *
273 * @param nextObj the nextObjective of type SIMPLE
274 */
275 private void processSimpleNextObjective(NextObjective nextObj) {
276 TrafficTreatment treatment = nextObj.next().iterator().next();
277 // determine if plain L2 or L3->L2
278 boolean plainL2 = true;
279 for (Instruction ins : treatment.allInstructions()) {
280 if (ins.type() == Instruction.Type.L2MODIFICATION) {
281 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
282 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_DST ||
283 l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_SRC) {
284 plainL2 = false;
285 break;
286 }
287 }
288 }
289
290 if (plainL2) {
291 createL2InterfaceGroup(nextObj);
292 return;
293 }
294
Pier Ventre140a8942016-11-02 07:26:38 -0700295 boolean isMpls = false;
Pier Ventre42287df2016-11-09 14:17:26 -0800296 // In order to understand if it is a pseudo wire related
297 // next objective we look for the tunnel id in the meta.
298 boolean isPw = false;
Pier Ventre140a8942016-11-02 07:26:38 -0700299 if (nextObj.meta() != null) {
300 isMpls = isNotMplsBos(nextObj.meta());
Pier Ventre42287df2016-11-09 14:17:26 -0800301
302 TunnelIdCriterion tunnelIdCriterion = (TunnelIdCriterion) nextObj
303 .meta()
304 .getCriterion(TUNNEL_ID);
305 if (tunnelIdCriterion != null) {
306 isPw = true;
307 }
308
Pier Ventre140a8942016-11-02 07:26:38 -0700309 }
310
Pier Ventre42287df2016-11-09 14:17:26 -0800311 if (!isPw) {
312 // break up simple next objective to GroupChain objects
313 GroupInfo groupInfo = createL2L3Chain(treatment, nextObj.id(),
314 nextObj.appId(), isMpls,
315 nextObj.meta());
316 if (groupInfo == null) {
317 log.error("Could not process nextObj={} in dev:{}", nextObj.id(), deviceId);
318 return;
319 }
320 // create object for local and distributed storage
321 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
322 gkeyChain.addFirst(groupInfo.innerMostGroupDesc.appCookie());
323 gkeyChain.addFirst(groupInfo.nextGroupDesc.appCookie());
324 OfdpaNextGroup ofdpaGrp =
325 new OfdpaNextGroup(Collections.singletonList(gkeyChain), nextObj);
326
327 // store l3groupkey with the ofdpaNextGroup for the nextObjective that depends on it
328 updatePendingNextObjective(groupInfo.nextGroupDesc.appCookie(), ofdpaGrp);
329
330 // now we are ready to send the l2 groupDescription (inner), as all the stores
331 // that will get async replies have been updated. By waiting to update
332 // the stores, we prevent nasty race conditions.
333 groupService.addGroup(groupInfo.innerMostGroupDesc);
334 } else {
335 // We handle the pseudo wire with a different a procedure.
336 // This procedure is meant to handle both initiation and
337 // termination of the pseudo wire.
338 processPwNextObjective(nextObj);
Charles Chan188ebf52015-12-23 00:15:11 -0800339 }
Charles Chan188ebf52015-12-23 00:15:11 -0800340 }
341
Charles Chan188ebf52015-12-23 00:15:11 -0800342 /**
343 * Creates a simple L2 Interface Group.
344 *
345 * @param nextObj the next Objective
346 */
347 private void createL2InterfaceGroup(NextObjective nextObj) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700348 VlanId assignedVlan = Ofdpa2Pipeline.readVlanFromSelector(nextObj.meta());
349 if (assignedVlan == null) {
350 log.warn("VLAN ID required by simple next obj is missing. Abort.");
351 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800352 return;
353 }
354
Charles Chan5b9df8d2016-03-28 22:21:40 -0700355 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Charles Chan188ebf52015-12-23 00:15:11 -0800356
Charles Chan5b9df8d2016-03-28 22:21:40 -0700357 // There is only one L2 interface group in this case
358 GroupDescription l2InterfaceGroupDesc = groupInfos.get(0).innerMostGroupDesc;
Charles Chan188ebf52015-12-23 00:15:11 -0800359
Charles Chan5b9df8d2016-03-28 22:21:40 -0700360 // Put all dependency information into allGroupKeys
361 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
362 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
363 gkeyChain.addFirst(l2InterfaceGroupDesc.appCookie());
364 allGroupKeys.add(gkeyChain);
Charles Chan188ebf52015-12-23 00:15:11 -0800365
Charles Chan5b9df8d2016-03-28 22:21:40 -0700366 // Point the next objective to this group
367 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
368 updatePendingNextObjective(l2InterfaceGroupDesc.appCookie(), ofdpaGrp);
369
370 // Start installing the inner-most group
371 groupService.addGroup(l2InterfaceGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800372 }
373
374 /**
375 * Creates one of two possible group-chains from the treatment
376 * passed in. Depending on the MPLS boolean, this method either creates
Charles Chan425854b2016-04-11 15:32:12 -0700377 * an L3Unicast Group --&gt; L2Interface Group, if mpls is false;
378 * or MPLSInterface Group --&gt; L2Interface Group, if mpls is true;
Charles Chan188ebf52015-12-23 00:15:11 -0800379 * The returned 'inner' group description is always the L2 Interface group.
380 *
381 * @param treatment that needs to be broken up to create the group chain
382 * @param nextId of the next objective that needs this group chain
383 * @param appId of the application that sent this next objective
384 * @param mpls determines if L3Unicast or MPLSInterface group is created
385 * @param meta metadata passed in by the application as part of the nextObjective
386 * @return GroupInfo containing the GroupDescription of the
387 * L2Interface group(inner) and the GroupDescription of the (outer)
388 * L3Unicast/MPLSInterface group. May return null if there is an
389 * error in processing the chain
390 */
Charles Chan425854b2016-04-11 15:32:12 -0700391 protected GroupInfo createL2L3Chain(TrafficTreatment treatment, int nextId,
Charles Chanf9e98652016-09-07 16:54:23 -0700392 ApplicationId appId, boolean mpls,
393 TrafficSelector meta) {
394 return createL2L3ChainInternal(treatment, nextId, appId, mpls, meta, true);
395 }
396
397 /**
398 * Internal implementation of createL2L3Chain.
399 * <p>
400 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
401 * Since it is non-OF spec, we need an extension treatment for that.
402 * The useSetVlanExtension must be set to false for OFDPA i12.
403 * </p>
404 *
405 * @param treatment that needs to be broken up to create the group chain
406 * @param nextId of the next objective that needs this group chain
407 * @param appId of the application that sent this next objective
408 * @param mpls determines if L3Unicast or MPLSInterface group is created
409 * @param meta metadata passed in by the application as part of the nextObjective
410 * @param useSetVlanExtension use the setVlanVid extension that has is_present bit set to 0.
411 * @return GroupInfo containing the GroupDescription of the
412 * L2Interface group(inner) and the GroupDescription of the (outer)
413 * L3Unicast/MPLSInterface group. May return null if there is an
414 * error in processing the chain
415 */
416 protected GroupInfo createL2L3ChainInternal(TrafficTreatment treatment, int nextId,
Pier Ventre42287df2016-11-09 14:17:26 -0800417 ApplicationId appId, boolean mpls,
418 TrafficSelector meta, boolean useSetVlanExtension) {
Charles Chan188ebf52015-12-23 00:15:11 -0800419 // for the l2interface group, get vlan and port info
420 // for the outer group, get the src/dst mac, and vlan info
421 TrafficTreatment.Builder outerTtb = DefaultTrafficTreatment.builder();
422 TrafficTreatment.Builder innerTtb = DefaultTrafficTreatment.builder();
423 VlanId vlanid = null;
424 long portNum = 0;
425 boolean setVlan = false, popVlan = false;
Yi Tseng78f51f42017-02-02 13:54:58 -0800426 MacAddress srcMac;
427 MacAddress dstMac;
Charles Chan188ebf52015-12-23 00:15:11 -0800428 for (Instruction ins : treatment.allInstructions()) {
429 if (ins.type() == Instruction.Type.L2MODIFICATION) {
430 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
431 switch (l2ins.subtype()) {
432 case ETH_DST:
Charles Chan5270ed02016-01-30 23:22:37 -0800433 dstMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
434 outerTtb.setEthDst(dstMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800435 break;
436 case ETH_SRC:
Charles Chand0fd5dc2016-02-16 23:14:49 -0800437 srcMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
438 outerTtb.setEthSrc(srcMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800439 break;
440 case VLAN_ID:
441 vlanid = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
Charles Chanf9e98652016-09-07 16:54:23 -0700442 if (useSetVlanExtension) {
443 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
444 outerTtb.extension(ofdpaSetVlanVid, deviceId);
445 } else {
446 outerTtb.setVlanId(vlanid);
447 }
Charles Chan188ebf52015-12-23 00:15:11 -0800448 setVlan = true;
449 break;
450 case VLAN_POP:
451 innerTtb.popVlan();
452 popVlan = true;
453 break;
454 case DEC_MPLS_TTL:
455 case MPLS_LABEL:
456 case MPLS_POP:
457 case MPLS_PUSH:
458 case VLAN_PCP:
459 case VLAN_PUSH:
460 default:
461 break;
462 }
463 } else if (ins.type() == Instruction.Type.OUTPUT) {
464 portNum = ((Instructions.OutputInstruction) ins).port().toLong();
465 innerTtb.add(ins);
466 } else {
467 log.warn("Driver does not handle this type of TrafficTreatment"
468 + " instruction in nextObjectives: {}", ins.type());
469 }
470 }
471
472 if (vlanid == null && meta != null) {
473 // use metadata if available
Pier Ventre140a8942016-11-02 07:26:38 -0700474 Criterion vidCriterion = meta.getCriterion(VLAN_VID);
Charles Chan188ebf52015-12-23 00:15:11 -0800475 if (vidCriterion != null) {
476 vlanid = ((VlanIdCriterion) vidCriterion).vlanId();
477 }
478 // if vlan is not set, use the vlan in metadata for outerTtb
479 if (vlanid != null && !setVlan) {
Charles Chanf9e98652016-09-07 16:54:23 -0700480 if (useSetVlanExtension) {
481 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
482 outerTtb.extension(ofdpaSetVlanVid, deviceId);
483 } else {
484 outerTtb.setVlanId(vlanid);
485 }
Charles Chan188ebf52015-12-23 00:15:11 -0800486 }
487 }
488
489 if (vlanid == null) {
490 log.error("Driver cannot process an L2/L3 group chain without "
491 + "egress vlan information for dev: {} port:{}",
492 deviceId, portNum);
493 return null;
494 }
495
496 if (!setVlan && !popVlan) {
497 // untagged outgoing port
498 TrafficTreatment.Builder temp = DefaultTrafficTreatment.builder();
499 temp.popVlan();
500 innerTtb.build().allInstructions().forEach(i -> temp.add(i));
501 innerTtb = temp;
502 }
503
504 // assemble information for ofdpa l2interface group
Charles Chane849c192016-01-11 18:28:54 -0800505 int l2groupId = L2_INTERFACE_TYPE | (vlanid.toShort() << 16) | (int) portNum;
Saurav Das8be4e3a2016-03-11 17:19:07 -0800506 // a globally unique groupkey that is different for ports in the same device,
Charles Chan188ebf52015-12-23 00:15:11 -0800507 // but different for the same portnumber on different devices. Also different
508 // for the various group-types created out of the same next objective.
Charles Chane849c192016-01-11 18:28:54 -0800509 int l2gk = l2InterfaceGroupKey(deviceId, vlanid, portNum);
Charles Chan361154b2016-03-24 10:23:39 -0700510 final GroupKey l2groupkey = new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(l2gk));
Charles Chan188ebf52015-12-23 00:15:11 -0800511
512 // assemble information for outer group
513 GroupDescription outerGrpDesc = null;
514 if (mpls) {
515 // outer group is MPLSInteface
Saurav Das8be4e3a2016-03-11 17:19:07 -0800516 int mplsInterfaceIndex = getNextAvailableIndex();
517 int mplsgroupId = MPLS_INTERFACE_TYPE | (SUBTYPE_MASK & mplsInterfaceIndex);
518 final GroupKey mplsgroupkey = new DefaultGroupKey(
Charles Chan361154b2016-03-24 10:23:39 -0700519 Ofdpa2Pipeline.appKryo.serialize(mplsInterfaceIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800520 outerTtb.group(new DefaultGroupId(l2groupId));
521 // create the mpls-interface group description to wait for the
522 // l2 interface group to be processed
523 GroupBucket mplsinterfaceGroupBucket =
524 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
525 outerGrpDesc = new DefaultGroupDescription(
526 deviceId,
527 GroupDescription.Type.INDIRECT,
528 new GroupBuckets(Collections.singletonList(
529 mplsinterfaceGroupBucket)),
530 mplsgroupkey,
531 mplsgroupId,
532 appId);
533 log.debug("Trying MPLS-Interface: device:{} gid:{} gkey:{} nextid:{}",
534 deviceId, Integer.toHexString(mplsgroupId),
535 mplsgroupkey, nextId);
536 } else {
537 // outer group is L3Unicast
Saurav Das8be4e3a2016-03-11 17:19:07 -0800538 int l3unicastIndex = getNextAvailableIndex();
539 int l3groupId = L3_UNICAST_TYPE | (TYPE_MASK & l3unicastIndex);
540 final GroupKey l3groupkey = new DefaultGroupKey(
Charles Chan361154b2016-03-24 10:23:39 -0700541 Ofdpa2Pipeline.appKryo.serialize(l3unicastIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800542 outerTtb.group(new DefaultGroupId(l2groupId));
543 // create the l3unicast group description to wait for the
544 // l2 interface group to be processed
545 GroupBucket l3unicastGroupBucket =
546 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
547 outerGrpDesc = new DefaultGroupDescription(
548 deviceId,
549 GroupDescription.Type.INDIRECT,
550 new GroupBuckets(Collections.singletonList(
551 l3unicastGroupBucket)),
552 l3groupkey,
553 l3groupId,
554 appId);
555 log.debug("Trying L3Unicast: device:{} gid:{} gkey:{} nextid:{}",
556 deviceId, Integer.toHexString(l3groupId),
557 l3groupkey, nextId);
558 }
559
560 // store l2groupkey with the groupChainElem for the outer-group that depends on it
561 GroupChainElem gce = new GroupChainElem(outerGrpDesc, 1, false);
562 updatePendingGroups(l2groupkey, gce);
563
564 // create group description for the inner l2interfacegroup
Charles Chan5b9df8d2016-03-28 22:21:40 -0700565 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800566 DefaultGroupBucket.createIndirectGroupBucket(innerTtb.build());
567 GroupDescription l2groupDescription =
568 new DefaultGroupDescription(
569 deviceId,
570 GroupDescription.Type.INDIRECT,
571 new GroupBuckets(Collections.singletonList(
Charles Chan5b9df8d2016-03-28 22:21:40 -0700572 l2InterfaceGroupBucket)),
Charles Chan188ebf52015-12-23 00:15:11 -0800573 l2groupkey,
574 l2groupId,
575 appId);
576 log.debug("Trying L2Interface: device:{} gid:{} gkey:{} nextId:{}",
577 deviceId, Integer.toHexString(l2groupId),
578 l2groupkey, nextId);
579 return new GroupInfo(l2groupDescription, outerGrpDesc);
580
581 }
582
583 /**
584 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
585 * a chain of groups. The broadcast Next Objective passed in by the application
586 * has to be broken up into a group chain comprising of an
Saurav Das1a129a02016-11-18 15:21:57 -0800587 * L2 Flood group or L3 Multicast group, whose buckets point to L2 Interface groups.
Charles Chan188ebf52015-12-23 00:15:11 -0800588 *
589 * @param nextObj the nextObjective of type BROADCAST
590 */
591 private void processBroadcastNextObjective(NextObjective nextObj) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700592 VlanId assignedVlan = Ofdpa2Pipeline.readVlanFromSelector(nextObj.meta());
593 if (assignedVlan == null) {
594 log.warn("VLAN ID required by broadcast next obj is missing. Abort.");
595 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800596 return;
597 }
Charles Chan188ebf52015-12-23 00:15:11 -0800598
Charles Chan5b9df8d2016-03-28 22:21:40 -0700599 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
600
601 IpPrefix ipDst = Ofdpa2Pipeline.readIpDstFromSelector(nextObj.meta());
602 if (ipDst != null) {
603 if (ipDst.isMulticast()) {
604 createL3MulticastGroup(nextObj, assignedVlan, groupInfos);
605 } else {
606 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
607 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
608 return;
609 }
610 } else {
611 createL2FloodGroup(nextObj, assignedVlan, groupInfos);
612 }
613 }
614
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700615 private List<GroupInfo> prepareL2InterfaceGroup(NextObjective nextObj,
616 VlanId assignedVlan) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700617 ImmutableList.Builder<GroupInfo> groupInfoBuilder = ImmutableList.builder();
618
619 // break up broadcast next objective to multiple groups
620 Collection<TrafficTreatment> buckets = nextObj.next();
621
Charles Chan188ebf52015-12-23 00:15:11 -0800622 // each treatment is converted to an L2 interface group
Charles Chan188ebf52015-12-23 00:15:11 -0800623 for (TrafficTreatment treatment : buckets) {
624 TrafficTreatment.Builder newTreatment = DefaultTrafficTreatment.builder();
625 PortNumber portNum = null;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700626 VlanId egressVlan = null;
Charles Chan188ebf52015-12-23 00:15:11 -0800627 // ensure that the only allowed treatments are pop-vlan and output
628 for (Instruction ins : treatment.allInstructions()) {
629 if (ins.type() == Instruction.Type.L2MODIFICATION) {
630 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
631 switch (l2ins.subtype()) {
632 case VLAN_POP:
633 newTreatment.add(l2ins);
634 break;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700635 case VLAN_ID:
636 egressVlan = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
637 break;
Charles Chan188ebf52015-12-23 00:15:11 -0800638 default:
639 log.debug("action {} not permitted for broadcast nextObj",
640 l2ins.subtype());
641 break;
642 }
643 } else if (ins.type() == Instruction.Type.OUTPUT) {
644 portNum = ((Instructions.OutputInstruction) ins).port();
645 newTreatment.add(ins);
646 } else {
Charles Chane849c192016-01-11 18:28:54 -0800647 log.debug("TrafficTreatment of type {} not permitted in " +
648 " broadcast nextObjective", ins.type());
Charles Chan188ebf52015-12-23 00:15:11 -0800649 }
650 }
651
Charles Chan188ebf52015-12-23 00:15:11 -0800652 // assemble info for l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700653 VlanId l2InterfaceGroupVlan =
654 (egressVlan != null && !assignedVlan.equals(egressVlan)) ?
655 egressVlan : assignedVlan;
656 int l2gk = l2InterfaceGroupKey(deviceId, l2InterfaceGroupVlan, portNum.toLong());
657 final GroupKey l2InterfaceGroupKey =
658 new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(l2gk));
Charles Chan372b63e2017-02-07 12:10:53 -0800659 int l2InterfaceGroupId = L2_INTERFACE_TYPE |
660 ((l2InterfaceGroupVlan.toShort() & THREE_BIT_MASK) << PORT_LEN) |
661 ((int) portNum.toLong() & FOUR_BIT_MASK);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700662 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800663 DefaultGroupBucket.createIndirectGroupBucket(newTreatment.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700664 GroupDescription l2InterfaceGroupDescription =
Charles Chan188ebf52015-12-23 00:15:11 -0800665 new DefaultGroupDescription(
666 deviceId,
667 GroupDescription.Type.INDIRECT,
668 new GroupBuckets(Collections.singletonList(
Charles Chan5b9df8d2016-03-28 22:21:40 -0700669 l2InterfaceGroupBucket)),
670 l2InterfaceGroupKey,
671 l2InterfaceGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800672 nextObj.appId());
673 log.debug("Trying L2-Interface: device:{} gid:{} gkey:{} nextid:{}",
Charles Chan5b9df8d2016-03-28 22:21:40 -0700674 deviceId, Integer.toHexString(l2InterfaceGroupId),
675 l2InterfaceGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -0800676
Charles Chan5b9df8d2016-03-28 22:21:40 -0700677 groupInfoBuilder.add(new GroupInfo(l2InterfaceGroupDescription,
678 l2InterfaceGroupDescription));
Charles Chan188ebf52015-12-23 00:15:11 -0800679 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700680 return groupInfoBuilder.build();
681 }
Charles Chan188ebf52015-12-23 00:15:11 -0800682
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700683 private void createL2FloodGroup(NextObjective nextObj, VlanId vlanId,
684 List<GroupInfo> groupInfos) {
685 // assemble info for l2 flood group. Since there can be only one flood
686 // group for a vlan, its index is always the same - 0
Saurav Das0fd79d92016-03-07 10:58:36 -0800687 Integer l2floodgroupId = L2_FLOOD_TYPE | (vlanId.toShort() << 16);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800688 int l2floodgk = getNextAvailableIndex();
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700689 final GroupKey l2floodgroupkey =
690 new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(l2floodgk));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700691
Charles Chan188ebf52015-12-23 00:15:11 -0800692 // collection of group buckets pointing to all the l2 interface groups
Yi Tseng78f51f42017-02-02 13:54:58 -0800693 List<GroupBucket> l2floodBuckets =
694 generateNextGroupBuckets(groupInfos, ALL);
Charles Chan188ebf52015-12-23 00:15:11 -0800695 // create the l2flood group-description to wait for all the
696 // l2interface groups to be processed
697 GroupDescription l2floodGroupDescription =
698 new DefaultGroupDescription(
699 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800700 ALL,
Charles Chan188ebf52015-12-23 00:15:11 -0800701 new GroupBuckets(l2floodBuckets),
702 l2floodgroupkey,
703 l2floodgroupId,
704 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800705 log.debug("Trying L2-Flood: device:{} gid:{} gkey:{} nextid:{}",
706 deviceId, Integer.toHexString(l2floodgroupId),
707 l2floodgroupkey, nextObj.id());
708
Charles Chan5b9df8d2016-03-28 22:21:40 -0700709 // Put all dependency information into allGroupKeys
710 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
711 groupInfos.forEach(groupInfo -> {
712 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
713 // In this case we should have L2 interface group only
714 gkeyChain.addFirst(groupInfo.nextGroupDesc.appCookie());
715 gkeyChain.addFirst(l2floodgroupkey);
716 allGroupKeys.add(gkeyChain);
717 });
Charles Chan188ebf52015-12-23 00:15:11 -0800718
Charles Chan5b9df8d2016-03-28 22:21:40 -0700719 // Point the next objective to this group
720 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Charles Chan188ebf52015-12-23 00:15:11 -0800721 updatePendingNextObjective(l2floodgroupkey, ofdpaGrp);
722
Charles Chan5b9df8d2016-03-28 22:21:40 -0700723 GroupChainElem gce = new GroupChainElem(l2floodGroupDescription,
724 groupInfos.size(), false);
725 groupInfos.forEach(groupInfo -> {
726 // Point this group to the next group
727 updatePendingGroups(groupInfo.nextGroupDesc.appCookie(), gce);
728 // Start installing the inner-most group
729 groupService.addGroup(groupInfo.innerMostGroupDesc);
730 });
731 }
732
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700733 private void createL3MulticastGroup(NextObjective nextObj, VlanId vlanId,
734 List<GroupInfo> groupInfos) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700735 List<GroupBucket> l3McastBuckets = new ArrayList<>();
736 groupInfos.forEach(groupInfo -> {
737 // Points to L3 interface group if there is one.
738 // Otherwise points to L2 interface group directly.
739 GroupDescription nextGroupDesc = (groupInfo.nextGroupDesc != null) ?
740 groupInfo.nextGroupDesc : groupInfo.innerMostGroupDesc;
741 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
742 ttb.group(new DefaultGroupId(nextGroupDesc.givenGroupId()));
743 GroupBucket abucket = DefaultGroupBucket.createAllGroupBucket(ttb.build());
744 l3McastBuckets.add(abucket);
745 });
746
747 int l3MulticastIndex = getNextAvailableIndex();
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700748 int l3MulticastGroupId = L3_MULTICAST_TYPE |
749 vlanId.toShort() << 16 | (TYPE_VLAN_MASK & l3MulticastIndex);
750 final GroupKey l3MulticastGroupKey =
751 new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(l3MulticastIndex));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700752
753 GroupDescription l3MulticastGroupDesc = new DefaultGroupDescription(deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800754 ALL,
Charles Chan5b9df8d2016-03-28 22:21:40 -0700755 new GroupBuckets(l3McastBuckets),
756 l3MulticastGroupKey,
757 l3MulticastGroupId,
758 nextObj.appId());
759
760 // Put all dependency information into allGroupKeys
761 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
762 groupInfos.forEach(groupInfo -> {
763 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
764 gkeyChain.addFirst(groupInfo.innerMostGroupDesc.appCookie());
765 // Add L3 interface group to the chain if there is one.
766 if (!groupInfo.nextGroupDesc.equals(groupInfo.innerMostGroupDesc)) {
767 gkeyChain.addFirst(groupInfo.nextGroupDesc.appCookie());
768 }
769 gkeyChain.addFirst(l3MulticastGroupKey);
770 allGroupKeys.add(gkeyChain);
771 });
772
773 // Point the next objective to this group
774 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
775 updatePendingNextObjective(l3MulticastGroupKey, ofdpaGrp);
776
777 GroupChainElem outerGce = new GroupChainElem(l3MulticastGroupDesc,
778 groupInfos.size(), false);
779 groupInfos.forEach(groupInfo -> {
780 // Point this group (L3 multicast) to the next group
781 updatePendingGroups(groupInfo.nextGroupDesc.appCookie(), outerGce);
782
783 // Point next group to inner-most group, if any
784 if (!groupInfo.nextGroupDesc.equals(groupInfo.innerMostGroupDesc)) {
785 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc,
786 1, false);
787 updatePendingGroups(groupInfo.innerMostGroupDesc.appCookie(), innerGce);
788 }
789
790 // Start installing the inner-most group
791 groupService.addGroup(groupInfo.innerMostGroupDesc);
792 });
Charles Chan188ebf52015-12-23 00:15:11 -0800793 }
794
Charles Chan188ebf52015-12-23 00:15:11 -0800795 /**
796 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
797 * a chain of groups. The hashed Next Objective passed in by the application
798 * has to be broken up into a group chain comprising of an
799 * L3 ECMP group as the top level group. Buckets of this group can point
800 * to a variety of groups in a group chain, depending on the whether
801 * MPLS labels are being pushed or not.
802 * <p>
803 * NOTE: We do not create MPLS ECMP groups as they are unimplemented in
804 * OF-DPA 2.0 (even though it is in the spec). Therefore we do not
805 * check the nextObjective meta to see what is matching before being
806 * sent to this nextObjective.
807 *
808 * @param nextObj the nextObjective of type HASHED
809 */
Pier Ventre140a8942016-11-02 07:26:38 -0700810 protected void processHashedNextObjective(NextObjective nextObj) {
Charles Chan188ebf52015-12-23 00:15:11 -0800811 // storage for all group keys in the chain of groups created
812 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
813 List<GroupInfo> unsentGroups = new ArrayList<>();
814 createHashBucketChains(nextObj, allGroupKeys, unsentGroups);
815
816 // now we can create the outermost L3 ECMP group
817 List<GroupBucket> l3ecmpGroupBuckets = new ArrayList<>();
818 for (GroupInfo gi : unsentGroups) {
819 // create ECMP bucket to point to the outer group
820 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700821 ttb.group(new DefaultGroupId(gi.nextGroupDesc.givenGroupId()));
Charles Chan188ebf52015-12-23 00:15:11 -0800822 GroupBucket sbucket = DefaultGroupBucket
823 .createSelectGroupBucket(ttb.build());
824 l3ecmpGroupBuckets.add(sbucket);
825 }
Saurav Das8be4e3a2016-03-11 17:19:07 -0800826 int l3ecmpIndex = getNextAvailableIndex();
827 int l3ecmpGroupId = L3_ECMP_TYPE | (TYPE_MASK & l3ecmpIndex);
828 GroupKey l3ecmpGroupKey = new DefaultGroupKey(
Charles Chan361154b2016-03-24 10:23:39 -0700829 Ofdpa2Pipeline.appKryo.serialize(l3ecmpIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800830 GroupDescription l3ecmpGroupDesc =
831 new DefaultGroupDescription(
832 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800833 SELECT,
Charles Chan188ebf52015-12-23 00:15:11 -0800834 new GroupBuckets(l3ecmpGroupBuckets),
835 l3ecmpGroupKey,
836 l3ecmpGroupId,
837 nextObj.appId());
838 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
839 l3ecmpGroupBuckets.size(),
840 false);
841
842 // create objects for local and distributed storage
843 allGroupKeys.forEach(gkeyChain -> gkeyChain.addFirst(l3ecmpGroupKey));
844 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
845
846 // store l3ecmpGroupKey with the ofdpaGroupChain for the nextObjective
847 // that depends on it
848 updatePendingNextObjective(l3ecmpGroupKey, ofdpaGrp);
849
850 log.debug("Trying L3ECMP: device:{} gid:{} gkey:{} nextId:{}",
851 deviceId, Integer.toHexString(l3ecmpGroupId),
852 l3ecmpGroupKey, nextObj.id());
853 // finally we are ready to send the innermost groups
854 for (GroupInfo gi : unsentGroups) {
855 log.debug("Sending innermost group {} in group chain on device {} ",
Charles Chan5b9df8d2016-03-28 22:21:40 -0700856 Integer.toHexString(gi.innerMostGroupDesc.givenGroupId()), deviceId);
857 updatePendingGroups(gi.nextGroupDesc.appCookie(), l3ecmpGce);
858 groupService.addGroup(gi.innerMostGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800859 }
860
861 }
862
863 /**
864 * Creates group chains for all buckets in a hashed group, and stores the
865 * GroupInfos and GroupKeys for all the groups in the lists passed in, which
866 * should be empty.
867 * <p>
868 * Does not create the top level ECMP group. Does not actually send the
869 * groups to the groupService.
870 *
871 * @param nextObj the Next Objective with buckets that need to be converted
872 * to group chains
873 * @param allGroupKeys a list to store groupKey for each bucket-group-chain
874 * @param unsentGroups a list to store GroupInfo for each bucket-group-chain
875 */
Pier Ventre140a8942016-11-02 07:26:38 -0700876 protected void createHashBucketChains(NextObjective nextObj,
Saurav Das8be4e3a2016-03-11 17:19:07 -0800877 List<Deque<GroupKey>> allGroupKeys,
878 List<GroupInfo> unsentGroups) {
Charles Chan188ebf52015-12-23 00:15:11 -0800879 // break up hashed next objective to multiple groups
880 Collection<TrafficTreatment> buckets = nextObj.next();
881
882 for (TrafficTreatment bucket : buckets) {
883 //figure out how many labels are pushed in each bucket
884 int labelsPushed = 0;
885 MplsLabel innermostLabel = null;
886 for (Instruction ins : bucket.allInstructions()) {
887 if (ins.type() == Instruction.Type.L2MODIFICATION) {
888 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
889 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_PUSH) {
890 labelsPushed++;
891 }
892 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) {
893 if (innermostLabel == null) {
Ray Milkey125572b2016-02-22 16:48:17 -0800894 innermostLabel = ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label();
Charles Chan188ebf52015-12-23 00:15:11 -0800895 }
896 }
897 }
898 }
899
900 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
901 // XXX we only deal with 0 and 1 label push right now
902 if (labelsPushed == 0) {
Pier Ventre140a8942016-11-02 07:26:38 -0700903 GroupInfo nolabelGroupInfo;
904 TrafficSelector metaSelector = nextObj.meta();
905 if (metaSelector != null) {
906 if (isNotMplsBos(metaSelector)) {
907 nolabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
908 nextObj.appId(), true,
909 nextObj.meta());
910 } else {
911 nolabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
912 nextObj.appId(), false,
913 nextObj.meta());
914 }
915 } else {
916 nolabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
917 nextObj.appId(), false,
918 nextObj.meta());
919 }
Charles Chan188ebf52015-12-23 00:15:11 -0800920 if (nolabelGroupInfo == null) {
921 log.error("Could not process nextObj={} in dev:{}",
922 nextObj.id(), deviceId);
923 return;
924 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700925 gkeyChain.addFirst(nolabelGroupInfo.innerMostGroupDesc.appCookie());
926 gkeyChain.addFirst(nolabelGroupInfo.nextGroupDesc.appCookie());
Charles Chan188ebf52015-12-23 00:15:11 -0800927
928 // we can't send the inner group description yet, as we have to
929 // create the dependent ECMP group first. So we store..
930 unsentGroups.add(nolabelGroupInfo);
931
932 } else if (labelsPushed == 1) {
933 GroupInfo onelabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
934 nextObj.appId(), true,
935 nextObj.meta());
936 if (onelabelGroupInfo == null) {
937 log.error("Could not process nextObj={} in dev:{}",
938 nextObj.id(), deviceId);
939 return;
940 }
941 // we need to add another group to this chain - the L3VPN group
942 TrafficTreatment.Builder l3vpnTtb = DefaultTrafficTreatment.builder();
943 l3vpnTtb.pushMpls()
944 .setMpls(innermostLabel)
Charles Chan40132b32017-01-22 00:19:37 -0800945 .group(new DefaultGroupId(onelabelGroupInfo.nextGroupDesc.givenGroupId()));
946 if (supportCopyTtl()) {
947 l3vpnTtb.copyTtlOut();
948 }
949 if (supportSetMplsBos()) {
950 l3vpnTtb.setMplsBos(true);
951 }
952
Charles Chan188ebf52015-12-23 00:15:11 -0800953 GroupBucket l3vpnGrpBkt =
954 DefaultGroupBucket.createIndirectGroupBucket(l3vpnTtb.build());
Saurav Das8be4e3a2016-03-11 17:19:07 -0800955 int l3vpnIndex = getNextAvailableIndex();
956 int l3vpngroupId = MPLS_L3VPN_SUBTYPE | (SUBTYPE_MASK & l3vpnIndex);
957 GroupKey l3vpngroupkey = new DefaultGroupKey(
Charles Chan361154b2016-03-24 10:23:39 -0700958 Ofdpa2Pipeline.appKryo.serialize(l3vpnIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800959 GroupDescription l3vpnGroupDesc =
960 new DefaultGroupDescription(
961 deviceId,
962 GroupDescription.Type.INDIRECT,
963 new GroupBuckets(Collections.singletonList(
964 l3vpnGrpBkt)),
965 l3vpngroupkey,
966 l3vpngroupId,
967 nextObj.appId());
968 GroupChainElem l3vpnGce = new GroupChainElem(l3vpnGroupDesc, 1, false);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700969 updatePendingGroups(onelabelGroupInfo.nextGroupDesc.appCookie(), l3vpnGce);
Charles Chan188ebf52015-12-23 00:15:11 -0800970
Charles Chan5b9df8d2016-03-28 22:21:40 -0700971 gkeyChain.addFirst(onelabelGroupInfo.innerMostGroupDesc.appCookie());
972 gkeyChain.addFirst(onelabelGroupInfo.nextGroupDesc.appCookie());
Charles Chan188ebf52015-12-23 00:15:11 -0800973 gkeyChain.addFirst(l3vpngroupkey);
974
975 //now we can replace the outerGrpDesc with the one we just created
Charles Chan5b9df8d2016-03-28 22:21:40 -0700976 onelabelGroupInfo.nextGroupDesc = l3vpnGroupDesc;
Charles Chan188ebf52015-12-23 00:15:11 -0800977
978 // we can't send the innermost group yet, as we have to create
979 // the dependent ECMP group first. So we store ...
980 unsentGroups.add(onelabelGroupInfo);
981
982 log.debug("Trying L3VPN: device:{} gid:{} gkey:{} nextId:{}",
983 deviceId, Integer.toHexString(l3vpngroupId),
984 l3vpngroupkey, nextObj.id());
985
986 } else {
987 log.warn("Driver currently does not handle more than 1 MPLS "
988 + "labels. Not processing nextObjective {}", nextObj.id());
989 return;
990 }
991
992 // all groups in this chain
993 allGroupKeys.add(gkeyChain);
994 }
995 }
996
Pier Ventre42287df2016-11-09 14:17:26 -0800997 /**
998 * Processes the pseudo wire related next objective.
999 * This procedure try to reuse the mpls label groups,
1000 * the mpls interface group and the l2 interface group.
1001 *
1002 * @param nextObjective the objective to process.
1003 */
1004 protected void processPwNextObjective(NextObjective nextObjective) {
1005 log.warn("Pseudo wire extensions are not support for the OFDPA 2.0 {}", nextObjective.id());
1006 return;
1007 }
1008
Saurav Das8be4e3a2016-03-11 17:19:07 -08001009 //////////////////////////////////////
1010 // Group Editing
1011 //////////////////////////////////////
1012
Charles Chan188ebf52015-12-23 00:15:11 -08001013 /**
1014 * Adds a bucket to the top level group of a group-chain, and creates the chain.
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001015 * Ensures that bucket being added is not a duplicate, by checking existing
1016 * buckets for the same outport.
Charles Chan188ebf52015-12-23 00:15:11 -08001017 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001018 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -08001019 * @param next the representation of the existing group-chain for this next objective
1020 */
1021 protected void addBucketToGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001022 if (nextObjective.type() != NextObjective.Type.HASHED &&
1023 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001024 log.warn("AddBuckets not applied to nextType:{} in dev:{} for next:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001025 nextObjective.type(), deviceId, nextObjective.id());
Saurav Das1a129a02016-11-18 15:21:57 -08001026 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001027 return;
1028 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001029
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001030 // first check to see if bucket being added is not a duplicate of an
1031 // existing bucket. If it is for an existing outport, then its a duplicate.
Yi Tseng78f51f42017-02-02 13:54:58 -08001032 Set<TrafficTreatment> duplicateBuckets = Sets.newHashSet();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001033 List<Deque<GroupKey>> allActiveKeys = Ofdpa2Pipeline.appKryo.deserialize(next.data());
Yi Tseng78f51f42017-02-02 13:54:58 -08001034 Set<PortNumber> existingPorts = getExistingOutputPorts(allActiveKeys);
1035
1036 nextObjective.next().forEach(trafficTreatment -> {
1037 PortNumber portNumber = readOutPortFromTreatment(trafficTreatment);
1038
1039 if (portNumber == null) {
1040 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001041 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001042
1043 if (existingPorts.contains(portNumber)) {
1044 duplicateBuckets.add(trafficTreatment);
1045 }
1046 });
1047
1048 if (!duplicateBuckets.isEmpty()) {
1049 log.warn("Some buckets {} already exists in next id {}, abort.",
1050 duplicateBuckets, nextObjective.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001051 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001052
Saurav Das1a129a02016-11-18 15:21:57 -08001053 if (nextObjective.type() == NextObjective.Type.HASHED) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001054 addBucketToHashGroup(nextObjective, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001055 } else if (nextObjective.type() == NextObjective.Type.BROADCAST) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001056 addBucketToBroadcastGroup(nextObjective, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001057 }
1058 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001059
Yi Tseng78f51f42017-02-02 13:54:58 -08001060 private Set<PortNumber> getExistingOutputPorts(List<Deque<GroupKey>> allActiveKeys) {
1061 Set<PortNumber> existingPorts = Sets.newHashSet();
1062
1063 allActiveKeys.forEach(keyChain -> {
1064 GroupKey ifaceGroupKey = keyChain.peekLast();
1065 Group ifaceGroup = groupService.getGroup(deviceId, ifaceGroupKey);
1066 if (ifaceGroup != null && !ifaceGroup.buckets().buckets().isEmpty()) {
1067 ifaceGroup.buckets().buckets().forEach(bucket -> {
1068 PortNumber portNumber = readOutPortFromTreatment(bucket.treatment());
1069
1070 if (portNumber != null) {
1071 existingPorts.add(portNumber);
1072 }
1073 });
1074 }
1075 });
1076
1077 return existingPorts;
1078 }
1079
Saurav Das1a129a02016-11-18 15:21:57 -08001080 private void addBucketToHashGroup(NextObjective nextObjective,
Yi Tseng78f51f42017-02-02 13:54:58 -08001081 List<Deque<GroupKey>> allActiveKeys) {
Charles Chan188ebf52015-12-23 00:15:11 -08001082 // storage for all group keys in the chain of groups created
1083 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
1084 List<GroupInfo> unsentGroups = new ArrayList<>();
Yi Tseng78f51f42017-02-02 13:54:58 -08001085 List<GroupBucket> newBuckets = Lists.newArrayList();
Charles Chan188ebf52015-12-23 00:15:11 -08001086 createHashBucketChains(nextObjective, allGroupKeys, unsentGroups);
1087
Yi Tseng78f51f42017-02-02 13:54:58 -08001088 // now we can create the buckets to add to the outermost L3 ECMP group
1089 newBuckets = generateNextGroupBuckets(unsentGroups, SELECT);
Charles Chan188ebf52015-12-23 00:15:11 -08001090
Saurav Das1a129a02016-11-18 15:21:57 -08001091 // retrieve the original L3 ECMP group
1092 Group l3ecmpGroup = retrieveTopLevelGroup(allActiveKeys, nextObjective.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001093 if (l3ecmpGroup == null) {
Saurav Das1a129a02016-11-18 15:21:57 -08001094 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.GROUPMISSING);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001095 return;
1096 }
Saurav Das1a129a02016-11-18 15:21:57 -08001097 GroupKey l3ecmpGroupKey = l3ecmpGroup.appCookie();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001098 int l3ecmpGroupId = l3ecmpGroup.id().id();
Charles Chan188ebf52015-12-23 00:15:11 -08001099
1100 // Although GroupDescriptions are not necessary for adding buckets to
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001101 // existing groups, we still use one in the GroupChainElem. When the latter is
Charles Chan188ebf52015-12-23 00:15:11 -08001102 // processed, the info will be extracted for the bucketAdd call to groupService
1103 GroupDescription l3ecmpGroupDesc =
1104 new DefaultGroupDescription(
1105 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -08001106 SELECT,
1107 new GroupBuckets(newBuckets),
Charles Chan188ebf52015-12-23 00:15:11 -08001108 l3ecmpGroupKey,
1109 l3ecmpGroupId,
1110 nextObjective.appId());
Yi Tseng78f51f42017-02-02 13:54:58 -08001111 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
1112 unsentGroups.size(),
1113 true);
Charles Chan188ebf52015-12-23 00:15:11 -08001114
1115 // update original NextGroup with new bucket-chain
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001116 // If active keys shows only the top-level group without a chain of groups,
1117 // then it represents an empty group. Update by replacing empty chain.
Charles Chan188ebf52015-12-23 00:15:11 -08001118 Deque<GroupKey> newBucketChain = allGroupKeys.get(0);
1119 newBucketChain.addFirst(l3ecmpGroupKey);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001120 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1121 allActiveKeys.clear();
1122 }
1123 allActiveKeys.add(newBucketChain);
1124 updatePendingNextObjective(l3ecmpGroupKey,
1125 new OfdpaNextGroup(allActiveKeys, nextObjective));
Yi Tseng78f51f42017-02-02 13:54:58 -08001126
Charles Chan188ebf52015-12-23 00:15:11 -08001127 log.debug("Adding to L3ECMP: device:{} gid:{} gkey:{} nextId:{}",
1128 deviceId, Integer.toHexString(l3ecmpGroupId),
1129 l3ecmpGroupKey, nextObjective.id());
Yi Tseng78f51f42017-02-02 13:54:58 -08001130
1131 unsentGroups.forEach(groupInfo -> {
1132 // send the innermost group
1133 log.debug("Sending innermost group {} in group chain on device {} ",
1134 Integer.toHexString(groupInfo.innerMostGroupDesc.givenGroupId()), deviceId);
1135 updatePendingGroups(groupInfo.nextGroupDesc.appCookie(), l3ecmpGce);
1136 groupService.addGroup(groupInfo.innerMostGroupDesc);
1137 });
1138
Saurav Das1a129a02016-11-18 15:21:57 -08001139 }
Charles Chan188ebf52015-12-23 00:15:11 -08001140
Saurav Das1a129a02016-11-18 15:21:57 -08001141 private void addBucketToBroadcastGroup(NextObjective nextObj,
Yi Tseng78f51f42017-02-02 13:54:58 -08001142 List<Deque<GroupKey>> allActiveKeys) {
Saurav Das1a129a02016-11-18 15:21:57 -08001143 VlanId assignedVlan = Ofdpa2Pipeline.readVlanFromSelector(nextObj.meta());
1144 if (assignedVlan == null) {
1145 log.warn("VLAN ID required by broadcast next obj is missing. "
1146 + "Aborting add bucket to broadcast group for next:{} in dev:{}",
1147 nextObj.id(), deviceId);
1148 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
1149 return;
1150 }
1151
1152 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
1153
1154 IpPrefix ipDst = Ofdpa2Pipeline.readIpDstFromSelector(nextObj.meta());
1155 if (ipDst != null) {
1156 if (ipDst.isMulticast()) {
1157 addBucketToL3MulticastGroup(nextObj, allActiveKeys,
Yi Tseng78f51f42017-02-02 13:54:58 -08001158 groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001159 } else {
1160 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
1161 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
1162 return;
1163 }
1164 } else {
1165 addBucketToL2FloodGroup(nextObj, allActiveKeys,
Yi Tseng78f51f42017-02-02 13:54:58 -08001166 groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001167 }
1168 }
1169
1170 private void addBucketToL2FloodGroup(NextObjective nextObj,
1171 List<Deque<GroupKey>> allActiveKeys,
1172 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001173 VlanId assignedVlan) {
1174 Group l2FloodGroup = retrieveTopLevelGroup(allActiveKeys, nextObj.id());
1175
1176 if (l2FloodGroup == null) {
1177 log.warn("Can't find L2 flood group while adding bucket to it. NextObj = {}",
1178 nextObj);
Saurav Das1a129a02016-11-18 15:21:57 -08001179 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.GROUPMISSING);
1180 return;
1181 }
Saurav Das1a129a02016-11-18 15:21:57 -08001182
Yi Tseng78f51f42017-02-02 13:54:58 -08001183 GroupKey l2floodGroupKey = l2FloodGroup.appCookie();
1184 int l2floodGroupId = l2FloodGroup.id().id();
1185 List<GroupBucket> newBuckets = generateNextGroupBuckets(groupInfos, ALL);
1186
1187 GroupDescription l2FloodGroupDescription =
Saurav Das1a129a02016-11-18 15:21:57 -08001188 new DefaultGroupDescription(
1189 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -08001190 ALL,
1191 new GroupBuckets(newBuckets),
Saurav Das1a129a02016-11-18 15:21:57 -08001192 l2floodGroupKey,
1193 l2floodGroupId,
1194 nextObj.appId());
Saurav Das1a129a02016-11-18 15:21:57 -08001195
Yi Tseng78f51f42017-02-02 13:54:58 -08001196 GroupChainElem l2FloodGroupChainElement =
1197 new GroupChainElem(l2FloodGroupDescription,
1198 groupInfos.size(),
1199 true);
1200
Saurav Das1a129a02016-11-18 15:21:57 -08001201 updatePendingNextObjective(l2floodGroupKey,
1202 new OfdpaNextGroup(allActiveKeys, nextObj));
Yi Tseng78f51f42017-02-02 13:54:58 -08001203
1204 //ensure assignedVlan applies to the chosen group
1205 VlanId floodGroupVlan = extractVlanIdFromGroupId(l2floodGroupId);
1206
1207 if (!floodGroupVlan.equals(assignedVlan)) {
1208 log.warn("VLAN ID {} does not match Flood group {} to which bucket is "
1209 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1210 Integer.toHexString(l2floodGroupId), nextObj.id(), deviceId);
1211 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
1212 return;
1213 }
1214
1215 groupInfos.forEach(groupInfo -> {
1216 // update original NextGroup with new bucket-chain
1217 // If active keys shows only the top-level group without a chain of groups,
1218 // then it represents an empty group. Update by replacing empty chain.
1219 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
1220 newBucketChain.addFirst(groupInfo.nextGroupDesc.appCookie());
1221 newBucketChain.addFirst(l2floodGroupKey);
1222 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1223 allActiveKeys.clear();
1224 }
1225 allActiveKeys.add(newBucketChain);
1226
1227 log.debug("Adding to L2FLOOD: device:{} gid:{} gkey:{} nextId:{}",
1228 deviceId, Integer.toHexString(l2floodGroupId),
1229 l2floodGroupKey, nextObj.id());
1230 // send the innermost group
1231 log.debug("Sending innermost group {} in group chain on device {} ",
1232 Integer.toHexString(groupInfo.innerMostGroupDesc.givenGroupId()),
1233 deviceId);
1234
1235 updatePendingGroups(groupInfo.nextGroupDesc.appCookie(), l2FloodGroupChainElement);
1236
1237 DeviceId innerMostGroupDevice = groupInfo.innerMostGroupDesc.deviceId();
1238 GroupKey innerMostGroupKey = groupInfo.innerMostGroupDesc.appCookie();
1239 Group existsL2IGroup = groupService.getGroup(innerMostGroupDevice, innerMostGroupKey);
1240
1241 if (existsL2IGroup != null) {
1242 // group already exist
1243 processPendingAddGroupsOrNextObjs(innerMostGroupKey, true);
1244 } else {
1245 groupService.addGroup(groupInfo.innerMostGroupDesc);
1246 }
1247
1248 });
1249 }
1250
1251 private VlanId extractVlanIdFromGroupId(int groupId) {
1252 // Extract the 9th to 20th bit from group id as vlan id.
1253 short vlanId = (short) ((groupId & 0x0fff0000) >> 16);
1254 return VlanId.vlanId(vlanId);
1255 }
1256
1257 private List<GroupBucket> generateNextGroupBuckets(List<GroupInfo> groupInfos, GroupDescription.Type bucketType) {
1258 List<GroupBucket> newBuckets = Lists.newArrayList();
1259
1260 groupInfos.forEach(groupInfo -> {
1261 GroupDescription groupDesc = groupInfo.nextGroupDesc;
1262 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1263 treatmentBuilder.group(new DefaultGroupId(groupDesc.givenGroupId()));
1264 GroupBucket newBucket = null;
1265 switch (bucketType) {
1266 case ALL:
1267 newBucket =
1268 DefaultGroupBucket.createAllGroupBucket(treatmentBuilder.build());
1269 break;
1270 case INDIRECT:
1271 newBucket =
1272 DefaultGroupBucket.createIndirectGroupBucket(treatmentBuilder.build());
1273 break;
1274 case SELECT:
1275 newBucket =
1276 DefaultGroupBucket.createSelectGroupBucket(treatmentBuilder.build());
1277 break;
1278 case FAILOVER:
1279 // TODO support failover bucket type
1280 default:
1281 log.warn("Unknown bucket type: {}", bucketType);
1282 break;
1283 }
1284
1285 if (newBucket != null) {
1286 newBuckets.add(newBucket);
1287 }
1288
1289 });
1290
1291 return ImmutableList.copyOf(newBuckets);
Saurav Das1a129a02016-11-18 15:21:57 -08001292 }
1293
1294 private void addBucketToL3MulticastGroup(NextObjective nextObj,
1295 List<Deque<GroupKey>> allActiveKeys,
1296 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001297 VlanId assignedVlan) {
1298 // create the buckets to add to the outermost L3 Multicast group
1299 List<GroupBucket> newBuckets = Lists.newArrayList();
1300 groupInfos.forEach(groupInfo -> {
1301 // Points to L3 interface group if there is one.
1302 // Otherwise points to L2 interface group directly.
1303 GroupDescription nextGroupDesc = (groupInfo.nextGroupDesc != null) ?
1304 groupInfo.nextGroupDesc : groupInfo.innerMostGroupDesc;
1305 TrafficTreatment.Builder treatmentBuidler = DefaultTrafficTreatment.builder();
1306 treatmentBuidler.group(new DefaultGroupId(nextGroupDesc.givenGroupId()));
1307 GroupBucket newBucket = DefaultGroupBucket.createAllGroupBucket(treatmentBuidler.build());
1308 newBuckets.add(newBucket);
1309 });
Saurav Das1a129a02016-11-18 15:21:57 -08001310
1311 // get the group being edited
1312 Group l3mcastGroup = retrieveTopLevelGroup(allActiveKeys, nextObj.id());
1313 if (l3mcastGroup == null) {
1314 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.GROUPMISSING);
1315 return;
1316 }
1317 GroupKey l3mcastGroupKey = l3mcastGroup.appCookie();
1318 int l3mcastGroupId = l3mcastGroup.id().id();
1319
1320 //ensure assignedVlan applies to the chosen group
Yi Tseng78f51f42017-02-02 13:54:58 -08001321 VlanId expectedVlan = extractVlanIdFromGroupId(l3mcastGroupId);
Saurav Das1a129a02016-11-18 15:21:57 -08001322 if (!expectedVlan.equals(assignedVlan)) {
1323 log.warn("VLAN ID {} does not match L3 Mcast group {} to which bucket is "
1324 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1325 Integer.toHexString(l3mcastGroupId), nextObj.id(), deviceId);
1326 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
1327 }
1328 GroupDescription l3mcastGroupDescription =
1329 new DefaultGroupDescription(
1330 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -08001331 ALL,
1332 new GroupBuckets(newBuckets),
Saurav Das1a129a02016-11-18 15:21:57 -08001333 l3mcastGroupKey,
1334 l3mcastGroupId,
1335 nextObj.appId());
1336 GroupChainElem l3mcastGce = new GroupChainElem(l3mcastGroupDescription,
Yi Tseng78f51f42017-02-02 13:54:58 -08001337 groupInfos.size(), true);
Saurav Das1a129a02016-11-18 15:21:57 -08001338
Yi Tseng78f51f42017-02-02 13:54:58 -08001339 groupInfos.forEach(groupInfo -> {
1340 // update original NextGroup with new bucket-chain
1341 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
1342 newBucketChain.addFirst(groupInfo.innerMostGroupDesc.appCookie());
1343 // Add L3 interface group to the chain if there is one.
1344 if (!groupInfo.nextGroupDesc.equals(groupInfo.innerMostGroupDesc)) {
1345 newBucketChain.addFirst(groupInfo.nextGroupDesc.appCookie());
1346 }
1347 newBucketChain.addFirst(l3mcastGroupKey);
1348 // If active keys shows only the top-level group without a chain of groups,
1349 // then it represents an empty group. Update by replacing empty chain.
1350 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1351 allActiveKeys.clear();
1352 }
1353 allActiveKeys.add(newBucketChain);
1354
1355 updatePendingGroups(groupInfo.nextGroupDesc.appCookie(), l3mcastGce);
1356 // Point next group to inner-most group, if any
1357 if (!groupInfo.nextGroupDesc.equals(groupInfo.innerMostGroupDesc)) {
1358 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc,
1359 1, false);
1360 updatePendingGroups(groupInfo.innerMostGroupDesc.appCookie(), innerGce);
1361 }
1362 log.debug("Adding to L3MCAST: device:{} gid:{} gkey:{} nextId:{}",
1363 deviceId, Integer.toHexString(l3mcastGroupId),
1364 l3mcastGroupKey, nextObj.id());
1365 // send the innermost group
1366 log.debug("Sending innermost group {} in group chain on device {} ",
1367 Integer.toHexString(groupInfo.innerMostGroupDesc.givenGroupId()),
1368 deviceId);
1369 groupService.addGroup(groupInfo.innerMostGroupDesc);
1370
1371 });
1372
Saurav Das1a129a02016-11-18 15:21:57 -08001373 updatePendingNextObjective(l3mcastGroupKey,
1374 new OfdpaNextGroup(allActiveKeys, nextObj));
1375
Yi Tseng78f51f42017-02-02 13:54:58 -08001376
1377
Charles Chan188ebf52015-12-23 00:15:11 -08001378 }
1379
1380 /**
1381 * Removes the bucket in the top level group of a possible group-chain. Does
Saurav Das1a129a02016-11-18 15:21:57 -08001382 * not remove the groups in the group-chain pointed to by this bucket, as they
Charles Chan188ebf52015-12-23 00:15:11 -08001383 * may be in use (referenced by other groups) elsewhere.
1384 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001385 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -08001386 * @param next the representation of the existing group-chain for this next objective
1387 */
1388 protected void removeBucketFromGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001389 if (nextObjective.type() != NextObjective.Type.HASHED &&
1390 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001391 log.warn("RemoveBuckets not applied to nextType:{} in dev:{} for next:{}",
1392 nextObjective.type(), deviceId, nextObjective.id());
Saurav Das1a129a02016-11-18 15:21:57 -08001393 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001394 return;
1395 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001396 Set<PortNumber> portsToRemove = Sets.newHashSet();
Charles Chan188ebf52015-12-23 00:15:11 -08001397 Collection<TrafficTreatment> treatments = nextObjective.next();
Yi Tseng78f51f42017-02-02 13:54:58 -08001398 for (TrafficTreatment treatment : treatments) {
1399 // find the bucket to remove by noting the outport, and figuring out the
1400 // top-level group in the group-chain that indirectly references the port
1401 PortNumber portToRemove = readOutPortFromTreatment(treatment);
1402 if (portToRemove == null) {
1403 log.warn("treatment {} of next objective {} has no outport.. cannot remove bucket"
1404 + "from group in dev: {}", treatment, nextObjective.id(), deviceId);
1405 } else {
1406 portsToRemove.add(portToRemove);
1407 }
1408 }
1409
1410 if (portsToRemove.isEmpty()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001411 log.warn("next objective {} has no outport.. cannot remove bucket"
Yi Tseng78f51f42017-02-02 13:54:58 -08001412 + "from group in dev: {}", nextObjective.id(), deviceId);
Saurav Das1a129a02016-11-18 15:21:57 -08001413 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -08001414 }
1415
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001416 List<Deque<GroupKey>> allActiveKeys = Ofdpa2Pipeline.appKryo.deserialize(next.data());
Yi Tseng78f51f42017-02-02 13:54:58 -08001417 List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001418 for (Deque<GroupKey> gkeys : allActiveKeys) {
1419 // last group in group chain should have a single bucket pointing to port
Yi Tseng78f51f42017-02-02 13:54:58 -08001420
Charles Chan188ebf52015-12-23 00:15:11 -08001421 GroupKey groupWithPort = gkeys.peekLast();
1422 Group group = groupService.getGroup(deviceId, groupWithPort);
1423 if (group == null) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001424 log.warn("Inconsistent group chain found when removing bucket"
1425 + "for next:{} in dev:{}", nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001426 continue;
1427 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001428 PortNumber pout = readOutPortFromTreatment(
1429 group.buckets().buckets().get(0).treatment());
Yi Tseng78f51f42017-02-02 13:54:58 -08001430 if (portsToRemove.contains(pout)) {
1431 chainsToRemove.add(gkeys);
Charles Chan188ebf52015-12-23 00:15:11 -08001432 }
Charles Chan188ebf52015-12-23 00:15:11 -08001433 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001434
1435 if (chainsToRemove.isEmpty()) {
Charles Chan188ebf52015-12-23 00:15:11 -08001436 log.warn("Could not find appropriate group-chain for removing bucket"
1437 + " for next id {} in dev:{}", nextObjective.id(), deviceId);
Saurav Das1a129a02016-11-18 15:21:57 -08001438 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.BADPARAMS);
1439 return;
Charles Chan188ebf52015-12-23 00:15:11 -08001440 }
Saurav Das1a129a02016-11-18 15:21:57 -08001441
Yi Tseng78f51f42017-02-02 13:54:58 -08001442
1443 List<GroupBucket> bucketsToRemove = Lists.newArrayList();
1444 //first group key is the one we want to modify
1445 GroupKey modGroupKey = chainsToRemove.get(0).peekFirst();
Saurav Das1a129a02016-11-18 15:21:57 -08001446 Group modGroup = groupService.getGroup(deviceId, modGroupKey);
Yi Tseng78f51f42017-02-02 13:54:58 -08001447
1448 for (Deque<GroupKey> foundChain : chainsToRemove) {
1449 //second group key is the one we wish to remove the reference to
1450 if (foundChain.size() < 2) {
1451 // additional check to make sure second group key exist in
1452 // the chain.
1453 log.warn("Can't find second group key from chain {}",
1454 foundChain);
1455 continue;
Saurav Das1a129a02016-11-18 15:21:57 -08001456 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001457 GroupKey pointedGroupKey = foundChain.stream().collect(Collectors.toList()).get(1);
1458
1459 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
1460
1461 if (pointedGroup == null) {
1462 continue;
1463 }
1464
1465 GroupBucket bucket;
1466 if (nextObjective.type() == NextObjective.Type.HASHED) {
1467 bucket = DefaultGroupBucket.createSelectGroupBucket(
1468 DefaultTrafficTreatment.builder()
1469 .group(pointedGroup.id())
1470 .build());
1471 } else {
1472 bucket = DefaultGroupBucket.createAllGroupBucket(
1473 DefaultTrafficTreatment.builder()
1474 .group(pointedGroup.id())
1475 .build());
1476 }
1477
1478 bucketsToRemove.add(bucket);
Saurav Das1a129a02016-11-18 15:21:57 -08001479 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001480
1481 GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove);
1482 List<String> pointedGroupIds; // for debug log
1483 pointedGroupIds = bucketsToRemove.stream()
1484 .map(GroupBucket::treatment)
1485 .map(TrafficTreatment::allInstructions)
1486 .flatMap(List::stream)
1487 .filter(inst -> inst instanceof Instructions.GroupInstruction)
1488 .map(inst -> (Instructions.GroupInstruction) inst)
1489 .map(Instructions.GroupInstruction::groupId)
1490 .map(GroupId::id)
1491 .map(Integer::toHexString)
1492 .map(id -> HEX_PREFIX + id)
1493 .collect(Collectors.toList());
1494
1495
1496
1497 log.debug("Removing buckets from group id 0x{} pointing to group id(s) {} "
Saurav Das1a129a02016-11-18 15:21:57 -08001498 + "for next id {} in device {}", Integer.toHexString(modGroup.id().id()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001499 pointedGroupIds, nextObjective.id(), deviceId);
1500 addPendingUpdateNextObjective(modGroupKey, nextObjective);
Saurav Das1a129a02016-11-18 15:21:57 -08001501 groupService.removeBucketsFromGroup(deviceId, modGroupKey,
1502 removeBuckets, modGroupKey,
1503 nextObjective.appId());
1504 // update store
1505 // If the bucket removed was the last bucket in the group, then
1506 // retain an entry for the top level group which still exists.
1507 if (allActiveKeys.size() == 1) {
1508 ArrayDeque<GroupKey> top = new ArrayDeque<>();
1509 top.add(modGroupKey);
1510 allActiveKeys.add(top);
1511 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001512
1513 allActiveKeys.removeAll(chainsToRemove);
Saurav Das1a129a02016-11-18 15:21:57 -08001514 flowObjectiveStore.putNextGroup(nextObjective.id(),
1515 new OfdpaNextGroup(allActiveKeys,
1516 nextObjective));
Charles Chan188ebf52015-12-23 00:15:11 -08001517 }
1518
1519 /**
1520 * Removes all groups in multiple possible group-chains that represent the next
1521 * objective.
1522 *
1523 * @param nextObjective the next objective to remove
1524 * @param next the NextGroup that represents the existing group-chain for
1525 * this next objective
1526 */
1527 protected void removeGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001528 List<Deque<GroupKey>> allActiveKeys = Ofdpa2Pipeline.appKryo.deserialize(next.data());
Charles Chanfc5c7802016-05-17 13:13:55 -07001529
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001530 List<GroupKey> groupKeys = allActiveKeys.stream()
Charles Chanfc5c7802016-05-17 13:13:55 -07001531 .map(Deque::getFirst).collect(Collectors.toList());
1532 pendingRemoveNextObjectives.put(nextObjective, groupKeys);
1533
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001534 allActiveKeys.forEach(groupChain -> groupChain.forEach(groupKey ->
Charles Chan188ebf52015-12-23 00:15:11 -08001535 groupService.removeGroup(deviceId, groupKey, nextObjective.appId())));
1536 flowObjectiveStore.removeNextGroup(nextObjective.id());
1537 }
1538
Saurav Das8be4e3a2016-03-11 17:19:07 -08001539 //////////////////////////////////////
1540 // Helper Methods and Classes
1541 //////////////////////////////////////
1542
Pier Ventre140a8942016-11-02 07:26:38 -07001543 protected void updatePendingNextObjective(GroupKey key, OfdpaNextGroup value) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001544 List<OfdpaNextGroup> nextList = new CopyOnWriteArrayList<OfdpaNextGroup>();
1545 nextList.add(value);
Charles Chanfc5c7802016-05-17 13:13:55 -07001546 List<OfdpaNextGroup> ret = pendingAddNextObjectives.asMap()
Saurav Das8be4e3a2016-03-11 17:19:07 -08001547 .putIfAbsent(key, nextList);
1548 if (ret != null) {
1549 ret.add(value);
1550 }
1551 }
1552
Charles Chan425854b2016-04-11 15:32:12 -07001553 protected void updatePendingGroups(GroupKey gkey, GroupChainElem gce) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001554 Set<GroupChainElem> gceSet = Collections.newSetFromMap(
1555 new ConcurrentHashMap<GroupChainElem, Boolean>());
1556 gceSet.add(gce);
1557 Set<GroupChainElem> retval = pendingGroups.putIfAbsent(gkey, gceSet);
1558 if (retval != null) {
1559 retval.add(gce);
1560 }
1561 }
1562
Yi Tseng78f51f42017-02-02 13:54:58 -08001563 private void addPendingUpdateNextObjective(GroupKey groupKey, NextObjective nextObjective) {
1564 pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> {
1565 if (nextObjs != null) {
1566 nextObjs.add(nextObjective);
1567 } else {
1568 nextObjs = Sets.newHashSet(nextObjective);
1569 }
1570 return nextObjs;
1571 });
1572 }
1573
Charles Chan188ebf52015-12-23 00:15:11 -08001574 /**
1575 * Processes next element of a group chain. Assumption is that if this
1576 * group points to another group, the latter has already been created
1577 * and this driver has received notification for it. A second assumption is
1578 * that if there is another group waiting for this group then the appropriate
1579 * stores already have the information to act upon the notification for the
1580 * creation of this group.
1581 * <p>
1582 * The processing of the GroupChainElement depends on the number of groups
1583 * this element is waiting on. For all group types other than SIMPLE, a
1584 * GroupChainElement could be waiting on multiple groups.
1585 *
1586 * @param gce the group chain element to be processed next
1587 */
1588 private void processGroupChain(GroupChainElem gce) {
1589 int waitOnGroups = gce.decrementAndGetGroupsWaitedOn();
1590 if (waitOnGroups != 0) {
1591 log.debug("GCE: {} not ready to be processed", gce);
1592 return;
1593 }
1594 log.debug("GCE: {} ready to be processed", gce);
1595 if (gce.addBucketToGroup) {
1596 groupService.addBucketsToGroup(gce.groupDescription.deviceId(),
1597 gce.groupDescription.appCookie(),
1598 gce.groupDescription.buckets(),
1599 gce.groupDescription.appCookie(),
1600 gce.groupDescription.appId());
1601 } else {
1602 groupService.addGroup(gce.groupDescription);
1603 }
1604 }
1605
1606 private class GroupChecker implements Runnable {
1607 @Override
1608 public void run() {
1609 Set<GroupKey> keys = pendingGroups.keySet().stream()
1610 .filter(key -> groupService.getGroup(deviceId, key) != null)
1611 .collect(Collectors.toSet());
Charles Chanfc5c7802016-05-17 13:13:55 -07001612 Set<GroupKey> otherkeys = pendingAddNextObjectives.asMap().keySet().stream()
Charles Chan188ebf52015-12-23 00:15:11 -08001613 .filter(otherkey -> groupService.getGroup(deviceId, otherkey) != null)
1614 .collect(Collectors.toSet());
1615 keys.addAll(otherkeys);
1616
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -07001617 keys.forEach(key ->
Charles Chanfc5c7802016-05-17 13:13:55 -07001618 processPendingAddGroupsOrNextObjs(key, false));
Charles Chan188ebf52015-12-23 00:15:11 -08001619 }
1620 }
1621
Saurav Das8be4e3a2016-03-11 17:19:07 -08001622 private class InnerGroupListener implements GroupListener {
1623 @Override
1624 public void event(GroupEvent event) {
1625 log.trace("received group event of type {}", event.type());
Charles Chanfc5c7802016-05-17 13:13:55 -07001626 switch (event.type()) {
1627 case GROUP_ADDED:
1628 processPendingAddGroupsOrNextObjs(event.subject().appCookie(), true);
1629 break;
1630 case GROUP_REMOVED:
1631 processPendingRemoveNextObjs(event.subject().appCookie());
1632 break;
Yi Tseng78f51f42017-02-02 13:54:58 -08001633 case GROUP_UPDATED:
1634 processPendingUpdateNextObjs(event.subject().appCookie());
1635 break;
Charles Chanfc5c7802016-05-17 13:13:55 -07001636 default:
1637 break;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001638 }
1639 }
1640 }
1641
Yi Tseng78f51f42017-02-02 13:54:58 -08001642 private void processPendingUpdateNextObjs(GroupKey groupKey) {
1643
1644 pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> {
1645 if (nextObjs != null) {
1646
1647 nextObjs.forEach(nextObj -> {
1648 log.debug("Group {} updated, update pending next objective {}.",
1649 groupKey, nextObj);
1650
1651 Ofdpa2Pipeline.pass(nextObj);
1652 });
1653 }
1654
1655 return Sets.newHashSet();
1656 });
1657 }
1658
Charles Chanfc5c7802016-05-17 13:13:55 -07001659 private void processPendingAddGroupsOrNextObjs(GroupKey key, boolean added) {
Charles Chan188ebf52015-12-23 00:15:11 -08001660 //first check for group chain
1661 Set<GroupChainElem> gceSet = pendingGroups.remove(key);
1662 if (gceSet != null) {
1663 for (GroupChainElem gce : gceSet) {
Charles Chan216e3c82016-04-23 14:48:16 -07001664 log.debug("Group service {} group key {} in device {}. "
Saurav Das0fd79d92016-03-07 10:58:36 -08001665 + "Processing next group in group chain with group id 0x{}",
Charles Chan188ebf52015-12-23 00:15:11 -08001666 (added) ? "ADDED" : "processed",
1667 key, deviceId,
1668 Integer.toHexString(gce.groupDescription.givenGroupId()));
1669 processGroupChain(gce);
1670 }
1671 } else {
1672 // otherwise chain complete - check for waiting nextObjectives
Charles Chanfc5c7802016-05-17 13:13:55 -07001673 List<OfdpaNextGroup> nextGrpList = pendingAddNextObjectives.getIfPresent(key);
Charles Chan188ebf52015-12-23 00:15:11 -08001674 if (nextGrpList != null) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001675 pendingAddNextObjectives.invalidate(key);
Charles Chan188ebf52015-12-23 00:15:11 -08001676 nextGrpList.forEach(nextGrp -> {
Charles Chan216e3c82016-04-23 14:48:16 -07001677 log.debug("Group service {} group key {} in device:{}. "
Saurav Das0fd79d92016-03-07 10:58:36 -08001678 + "Done implementing next objective: {} <<-->> gid:0x{}",
Charles Chan188ebf52015-12-23 00:15:11 -08001679 (added) ? "ADDED" : "processed",
1680 key, deviceId, nextGrp.nextObjective().id(),
1681 Integer.toHexString(groupService.getGroup(deviceId, key)
1682 .givenGroupId()));
Charles Chan361154b2016-03-24 10:23:39 -07001683 Ofdpa2Pipeline.pass(nextGrp.nextObjective());
Charles Chan188ebf52015-12-23 00:15:11 -08001684 flowObjectiveStore.putNextGroup(nextGrp.nextObjective().id(), nextGrp);
1685 // check if addBuckets waiting for this completion
1686 NextObjective pendBkt = pendingBuckets
1687 .remove(nextGrp.nextObjective().id());
1688 if (pendBkt != null) {
1689 addBucketToGroup(pendBkt, nextGrp);
1690 }
1691 });
1692 }
1693 }
1694 }
1695
Charles Chanfc5c7802016-05-17 13:13:55 -07001696 private void processPendingRemoveNextObjs(GroupKey key) {
1697 pendingRemoveNextObjectives.asMap().forEach((nextObjective, groupKeys) -> {
1698 if (groupKeys.isEmpty()) {
1699 pendingRemoveNextObjectives.invalidate(nextObjective);
1700 Ofdpa2Pipeline.pass(nextObjective);
1701 } else {
1702 groupKeys.remove(key);
1703 }
1704 });
1705 }
1706
Charles Chan425854b2016-04-11 15:32:12 -07001707 protected int getNextAvailableIndex() {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001708 return (int) nextIndex.incrementAndGet();
1709 }
1710
Charles Chane849c192016-01-11 18:28:54 -08001711 /**
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001712 * Returns the outport in a traffic treatment.
1713 *
1714 * @param tt the treatment
1715 * @return the PortNumber for the outport or null
1716 */
1717 protected static PortNumber readOutPortFromTreatment(TrafficTreatment tt) {
1718 for (Instruction ins : tt.allInstructions()) {
1719 if (ins.type() == Instruction.Type.OUTPUT) {
1720 return ((Instructions.OutputInstruction) ins).port();
1721 }
1722 }
1723 return null;
1724 }
1725
1726 /**
Charles Chane849c192016-01-11 18:28:54 -08001727 * Returns a hash as the L2 Interface Group Key.
1728 *
1729 * Keep the lower 6-bit for port since port number usually smaller than 64.
1730 * Hash other information into remaining 28 bits.
1731 *
1732 * @param deviceId Device ID
1733 * @param vlanId VLAN ID
1734 * @param portNumber Port number
1735 * @return L2 interface group key
1736 */
Pier Ventre140a8942016-11-02 07:26:38 -07001737 protected int l2InterfaceGroupKey(DeviceId deviceId, VlanId vlanId, long portNumber) {
Charles Chane849c192016-01-11 18:28:54 -08001738 int portLowerBits = (int) portNumber & PORT_LOWER_BITS_MASK;
1739 long portHigherBits = portNumber & PORT_HIGHER_BITS_MASK;
Charles Chand0fd5dc2016-02-16 23:14:49 -08001740 int hash = Objects.hash(deviceId, vlanId, portHigherBits);
Charles Chane849c192016-01-11 18:28:54 -08001741 return L2_INTERFACE_TYPE | (TYPE_MASK & hash << 6) | portLowerBits;
1742 }
1743
Saurav Das1a129a02016-11-18 15:21:57 -08001744 private Group retrieveTopLevelGroup(List<Deque<GroupKey>> allActiveKeys,
1745 int nextid) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001746 GroupKey topLevelGroupKey;
Saurav Das1a129a02016-11-18 15:21:57 -08001747 if (!allActiveKeys.isEmpty()) {
1748 topLevelGroupKey = allActiveKeys.get(0).peekFirst();
1749 } else {
1750 log.warn("Could not determine top level group while processing"
1751 + "next:{} in dev:{}", nextid, deviceId);
1752 return null;
1753 }
1754 Group topGroup = groupService.getGroup(deviceId, topLevelGroupKey);
1755 if (topGroup == null) {
1756 log.warn("Could not find top level group while processing "
1757 + "next:{} in dev:{}", nextid, deviceId);
1758 }
1759 return topGroup;
1760 }
1761
Charles Chan188ebf52015-12-23 00:15:11 -08001762 /**
1763 * Utility class for moving group information around.
Saurav Das1a129a02016-11-18 15:21:57 -08001764 *
1765 * Example: Suppose we are trying to create a group-chain A-B-C-D, where
1766 * A is the top level group, and D is the inner-most group, typically L2 Interface.
1767 * The innerMostGroupDesc is always D. At various stages of the creation
1768 * process the nextGroupDesc may be C or B. The nextGroupDesc exists to
1769 * inform the referencing group about which group it needs to point to,
1770 * and wait for. In some cases the group chain may simply be A-B. In this case,
1771 * both innerMostGroupDesc and nextGroupDesc will be B.
Charles Chan188ebf52015-12-23 00:15:11 -08001772 */
Charles Chan425854b2016-04-11 15:32:12 -07001773 protected class GroupInfo {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001774 /**
1775 * Description of the inner-most group of the group chain.
1776 * It is always an L2 interface group.
1777 */
1778 private GroupDescription innerMostGroupDesc;
Charles Chan188ebf52015-12-23 00:15:11 -08001779
Charles Chan5b9df8d2016-03-28 22:21:40 -07001780 /**
1781 * Description of the next group in the group chain.
1782 * It can be L2 interface, L3 interface, L3 unicast, L3 VPN group.
Saurav Das1a129a02016-11-18 15:21:57 -08001783 * It is possible that nextGroupDesc is the same as the innerMostGroup.
Charles Chan5b9df8d2016-03-28 22:21:40 -07001784 */
1785 private GroupDescription nextGroupDesc;
1786
1787 GroupInfo(GroupDescription innerMostGroupDesc, GroupDescription nextGroupDesc) {
1788 this.innerMostGroupDesc = innerMostGroupDesc;
1789 this.nextGroupDesc = nextGroupDesc;
Charles Chan188ebf52015-12-23 00:15:11 -08001790 }
Pier Ventre140a8942016-11-02 07:26:38 -07001791
1792 /**
1793 * Getter for innerMostGroupDesc.
1794 *
1795 * @return the inner most group description
1796 */
1797 public GroupDescription getInnerMostGroupDesc() {
1798 return innerMostGroupDesc;
1799 }
1800
1801 /**
1802 * Getter for the next group description.
1803 *
1804 * @return the next group description
1805 */
1806 public GroupDescription getNextGroupDesc() {
1807 return nextGroupDesc;
1808 }
Charles Chan188ebf52015-12-23 00:15:11 -08001809 }
1810
1811 /**
1812 * Represents an entire group-chain that implements a Next-Objective from
1813 * the application. The objective is represented as a list of deques, where
1814 * each deque is a separate chain of groups.
1815 * <p>
1816 * For example, an ECMP group with 3 buckets, where each bucket points to
1817 * a group chain of L3 Unicast and L2 interface groups will look like this:
1818 * <ul>
1819 * <li>List[0] is a Deque of GroupKeyECMP(first)-GroupKeyL3(middle)-GroupKeyL2(last)
1820 * <li>List[1] is a Deque of GroupKeyECMP(first)-GroupKeyL3(middle)-GroupKeyL2(last)
1821 * <li>List[2] is a Deque of GroupKeyECMP(first)-GroupKeyL3(middle)-GroupKeyL2(last)
1822 * </ul>
1823 * where the first element of each deque is the same, representing the
1824 * top level ECMP group, while every other element represents a unique groupKey.
1825 * <p>
1826 * Also includes information about the next objective that
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001827 * resulted in these group-chains.
Charles Chan188ebf52015-12-23 00:15:11 -08001828 *
1829 */
1830 protected class OfdpaNextGroup implements NextGroup {
1831 private final NextObjective nextObj;
1832 private final List<Deque<GroupKey>> gkeys;
1833
1834 public OfdpaNextGroup(List<Deque<GroupKey>> gkeys, NextObjective nextObj) {
Charles Chan188ebf52015-12-23 00:15:11 -08001835 this.nextObj = nextObj;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001836 this.gkeys = gkeys;
Charles Chan188ebf52015-12-23 00:15:11 -08001837 }
1838
1839 public NextObjective nextObjective() {
1840 return nextObj;
1841 }
1842
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001843 public List<Deque<GroupKey>> groupKeys() {
1844 return gkeys;
1845 }
1846
Charles Chan188ebf52015-12-23 00:15:11 -08001847 @Override
1848 public byte[] data() {
Charles Chan361154b2016-03-24 10:23:39 -07001849 return Ofdpa2Pipeline.appKryo.serialize(gkeys);
Charles Chan188ebf52015-12-23 00:15:11 -08001850 }
1851 }
1852
1853 /**
1854 * Represents a group element that is part of a chain of groups.
1855 * Stores enough information to create a Group Description to add the group
1856 * to the switch by requesting the Group Service. Objects instantiating this
1857 * class are meant to be temporary and live as long as it is needed to wait for
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001858 * referenced groups in the group chain to be created.
Charles Chan188ebf52015-12-23 00:15:11 -08001859 */
Charles Chan425854b2016-04-11 15:32:12 -07001860 protected class GroupChainElem {
Charles Chan188ebf52015-12-23 00:15:11 -08001861 private GroupDescription groupDescription;
1862 private AtomicInteger waitOnGroups;
1863 private boolean addBucketToGroup;
1864
1865 GroupChainElem(GroupDescription groupDescription, int waitOnGroups,
1866 boolean addBucketToGroup) {
1867 this.groupDescription = groupDescription;
1868 this.waitOnGroups = new AtomicInteger(waitOnGroups);
1869 this.addBucketToGroup = addBucketToGroup;
1870 }
1871
1872 /**
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001873 * This method atomically decrements the counter for the number of
Charles Chan188ebf52015-12-23 00:15:11 -08001874 * groups this GroupChainElement is waiting on, for notifications from
1875 * the Group Service. When this method returns a value of 0, this
1876 * GroupChainElement is ready to be processed.
1877 *
1878 * @return integer indication of the number of notifications being waited on
1879 */
1880 int decrementAndGetGroupsWaitedOn() {
1881 return waitOnGroups.decrementAndGet();
1882 }
1883
1884 @Override
1885 public String toString() {
1886 return (Integer.toHexString(groupDescription.givenGroupId()) +
1887 " groupKey: " + groupDescription.appCookie() +
1888 " waiting-on-groups: " + waitOnGroups.get() +
1889 " addBucketToGroup: " + addBucketToGroup +
1890 " device: " + deviceId);
1891 }
1892 }
Pier Ventre140a8942016-11-02 07:26:38 -07001893
1894 /**
1895 * Helper enum to handle the different MPLS group
1896 * types.
1897 */
1898 protected enum OfdpaMplsGroupSubType {
1899
1900 MPLS_INTF((short) 0),
1901
1902 L2_VPN((short) 1),
1903
1904 L3_VPN((short) 2),
1905
1906 MPLS_TUNNEL_LABEL_1((short) 3),
1907
1908 MPLS_TUNNEL_LABEL_2((short) 4),
1909
1910 MPLS_SWAP_LABEL((short) 5),
1911
1912 MPLS_ECMP((short) 8);
1913
1914 private short value;
1915
1916 public static final int OFDPA_GROUP_TYPE_SHIFT = 28;
1917 public static final int OFDPA_MPLS_SUBTYPE_SHIFT = 24;
1918
1919 OfdpaMplsGroupSubType(short value) {
1920 this.value = value;
1921 }
1922
1923 /**
1924 * Gets the value as an short.
1925 *
1926 * @return the value as an short
1927 */
1928 public short getValue() {
1929 return this.value;
1930 }
1931
1932 }
1933
1934 /**
1935 * Creates MPLS Label group id given a sub type and
1936 * the index.
1937 *
1938 * @param subType the MPLS Label group sub type
1939 * @param index the index of the group
1940 * @return the OFDPA group id
1941 */
1942 public Integer makeMplsLabelGroupId(OfdpaMplsGroupSubType subType, int index) {
1943 index = index & 0x00FFFFFF;
1944 return index | (9 << OFDPA_GROUP_TYPE_SHIFT) | (subType.value << OFDPA_MPLS_SUBTYPE_SHIFT);
1945 }
1946
1947 /**
1948 * Creates MPLS Forwarding group id given a sub type and
1949 * the index.
1950 *
1951 * @param subType the MPLS forwarding group sub type
1952 * @param index the index of the group
1953 * @return the OFDPA group id
1954 */
1955 public Integer makeMplsForwardingGroupId(OfdpaMplsGroupSubType subType, int index) {
1956 index = index & 0x00FFFFFF;
1957 return index | (10 << OFDPA_GROUP_TYPE_SHIFT) | (subType.value << OFDPA_MPLS_SUBTYPE_SHIFT);
1958 }
Charles Chan188ebf52015-12-23 00:15:11 -08001959}