blob: 9e3161a1c2aac9752af12afcd85e22737b864de5 [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;
Charles Chan188ebf52015-12-23 00:15:11 -080024import org.onlab.osgi.ServiceDirectory;
Charles Chan5b9df8d2016-03-28 22:21:40 -070025import org.onlab.packet.IpPrefix;
Charles Chan5270ed02016-01-30 23:22:37 -080026import org.onlab.packet.MacAddress;
Charles Chan188ebf52015-12-23 00:15:11 -080027import org.onlab.packet.MplsLabel;
28import org.onlab.packet.VlanId;
29import org.onosproject.core.ApplicationId;
30import org.onosproject.core.DefaultGroupId;
Charles Chan32562522016-04-07 14:37:14 -070031import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Charles Chan188ebf52015-12-23 00:15:11 -080032import org.onosproject.net.DeviceId;
33import org.onosproject.net.PortNumber;
34import org.onosproject.net.behaviour.NextGroup;
35import org.onosproject.net.behaviour.PipelinerContext;
36import org.onosproject.net.flow.DefaultTrafficTreatment;
37import org.onosproject.net.flow.TrafficSelector;
38import org.onosproject.net.flow.TrafficTreatment;
39import org.onosproject.net.flow.criteria.Criterion;
Pier Ventre42287df2016-11-09 14:17:26 -080040import org.onosproject.net.flow.criteria.TunnelIdCriterion;
Charles Chan188ebf52015-12-23 00:15:11 -080041import org.onosproject.net.flow.criteria.VlanIdCriterion;
42import org.onosproject.net.flow.instructions.Instruction;
43import org.onosproject.net.flow.instructions.Instructions;
44import org.onosproject.net.flow.instructions.L2ModificationInstruction;
45import org.onosproject.net.flowobjective.FlowObjectiveStore;
46import org.onosproject.net.flowobjective.NextObjective;
47import org.onosproject.net.flowobjective.ObjectiveError;
48import org.onosproject.net.group.DefaultGroupBucket;
49import org.onosproject.net.group.DefaultGroupDescription;
50import org.onosproject.net.group.DefaultGroupKey;
51import org.onosproject.net.group.Group;
52import org.onosproject.net.group.GroupBucket;
53import org.onosproject.net.group.GroupBuckets;
54import org.onosproject.net.group.GroupDescription;
55import org.onosproject.net.group.GroupEvent;
56import org.onosproject.net.group.GroupKey;
57import org.onosproject.net.group.GroupListener;
58import org.onosproject.net.group.GroupService;
Saurav Das8be4e3a2016-03-11 17:19:07 -080059import org.onosproject.store.service.AtomicCounter;
60import org.onosproject.store.service.StorageService;
Charles Chan188ebf52015-12-23 00:15:11 -080061import org.slf4j.Logger;
62
63import java.util.ArrayDeque;
64import java.util.ArrayList;
65import java.util.Collection;
66import java.util.Collections;
67import java.util.Deque;
Saurav Das1ce0a7b2016-10-21 14:06:29 -070068import java.util.HashSet;
Charles Chan188ebf52015-12-23 00:15:11 -080069import java.util.List;
Charles Chand0fd5dc2016-02-16 23:14:49 -080070import java.util.Objects;
Charles Chan188ebf52015-12-23 00:15:11 -080071import java.util.Set;
72import java.util.concurrent.ConcurrentHashMap;
73import java.util.concurrent.CopyOnWriteArrayList;
74import java.util.concurrent.Executors;
75import java.util.concurrent.ScheduledExecutorService;
76import java.util.concurrent.TimeUnit;
77import java.util.concurrent.atomic.AtomicInteger;
78import java.util.stream.Collectors;
79
80import static org.onlab.util.Tools.groupedThreads;
Pier Ventre140a8942016-11-02 07:26:38 -070081import static org.onosproject.driver.pipeline.Ofdpa2GroupHandler.OfdpaMplsGroupSubType.OFDPA_GROUP_TYPE_SHIFT;
82import static org.onosproject.driver.pipeline.Ofdpa2GroupHandler.OfdpaMplsGroupSubType.OFDPA_MPLS_SUBTYPE_SHIFT;
83import static org.onosproject.driver.pipeline.Ofdpa2Pipeline.isNotMplsBos;
Pier Ventre42287df2016-11-09 14:17:26 -080084import static org.onosproject.net.flow.criteria.Criterion.Type.TUNNEL_ID;
Pier Ventre140a8942016-11-02 07:26:38 -070085import static org.onosproject.net.flow.criteria.Criterion.Type.VLAN_VID;
86import static org.onosproject.net.flowobjective.NextObjective.Type.HASHED;
Charles Chan188ebf52015-12-23 00:15:11 -080087import static org.slf4j.LoggerFactory.getLogger;
88
89/**
Charles Chan40132b32017-01-22 00:19:37 -080090 * Group handler that emulates Broadcom OF-DPA TTP on CpqD.
Charles Chan188ebf52015-12-23 00:15:11 -080091 */
Charles Chan361154b2016-03-24 10:23:39 -070092public class Ofdpa2GroupHandler {
Charles Chan188ebf52015-12-23 00:15:11 -080093 /*
94 * OFDPA requires group-id's to have a certain form.
95 * L2 Interface Groups have <4bits-0><12bits-vlanid><16bits-portid>
96 * L3 Unicast Groups have <4bits-2><28bits-index>
97 * MPLS Interface Groups have <4bits-9><4bits:0><24bits-index>
98 * L3 ECMP Groups have <4bits-7><28bits-index>
99 * L2 Flood Groups have <4bits-4><12bits-vlanid><16bits-index>
100 * L3 VPN Groups have <4bits-9><4bits-2><24bits-index>
101 */
Charles Chan425854b2016-04-11 15:32:12 -0700102 protected static final int L2_INTERFACE_TYPE = 0x00000000;
103 protected static final int L3_INTERFACE_TYPE = 0x50000000;
104 protected static final int L3_UNICAST_TYPE = 0x20000000;
105 protected static final int L3_MULTICAST_TYPE = 0x60000000;
106 protected static final int MPLS_INTERFACE_TYPE = 0x90000000;
107 protected static final int MPLS_L3VPN_SUBTYPE = 0x92000000;
108 protected static final int L3_ECMP_TYPE = 0x70000000;
109 protected static final int L2_FLOOD_TYPE = 0x40000000;
Charles Chane849c192016-01-11 18:28:54 -0800110
Charles Chan425854b2016-04-11 15:32:12 -0700111 protected static final int TYPE_MASK = 0x0fffffff;
112 protected static final int SUBTYPE_MASK = 0x00ffffff;
113 protected static final int TYPE_VLAN_MASK = 0x0000ffff;
Charles Chane849c192016-01-11 18:28:54 -0800114
Charles Chan372b63e2017-02-07 12:10:53 -0800115 protected static final int THREE_BIT_MASK = 0x0fff;
116 protected static final int FOUR_BIT_MASK = 0xffff;
117 protected static final int PORT_LEN = 16;
118
Charles Chan425854b2016-04-11 15:32:12 -0700119 protected static final int PORT_LOWER_BITS_MASK = 0x3f;
120 protected static final long PORT_HIGHER_BITS_MASK = ~PORT_LOWER_BITS_MASK;
Charles Chan188ebf52015-12-23 00:15:11 -0800121
122 private final Logger log = getLogger(getClass());
123 private ServiceDirectory serviceDirectory;
124 protected GroupService groupService;
Saurav Das8be4e3a2016-03-11 17:19:07 -0800125 protected StorageService storageService;
Charles Chan188ebf52015-12-23 00:15:11 -0800126
Charles Chan425854b2016-04-11 15:32:12 -0700127 protected DeviceId deviceId;
Charles Chan188ebf52015-12-23 00:15:11 -0800128 private FlowObjectiveStore flowObjectiveStore;
Charles Chanfc5c7802016-05-17 13:13:55 -0700129 private Cache<GroupKey, List<OfdpaNextGroup>> pendingAddNextObjectives;
130 private Cache<NextObjective, List<GroupKey>> pendingRemoveNextObjectives;
Charles Chan188ebf52015-12-23 00:15:11 -0800131 private ConcurrentHashMap<GroupKey, Set<GroupChainElem>> pendingGroups;
132 private ScheduledExecutorService groupChecker =
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700133 Executors.newScheduledThreadPool(2, groupedThreads("onos/pipeliner", "ofdpa2-%d", log));
Charles Chan188ebf52015-12-23 00:15:11 -0800134
135 // index number for group creation
Saurav Das8be4e3a2016-03-11 17:19:07 -0800136 private AtomicCounter nextIndex;
Charles Chan188ebf52015-12-23 00:15:11 -0800137
Charles Chan188ebf52015-12-23 00:15:11 -0800138 // local store for pending bucketAdds - by design there can only be one
139 // pending bucket for a group
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700140 protected ConcurrentHashMap<Integer, NextObjective> pendingBuckets =
141 new ConcurrentHashMap<>();
Charles Chan188ebf52015-12-23 00:15:11 -0800142
Charles Chan40132b32017-01-22 00:19:37 -0800143 /**
144 * Determines whether this pipeline support copy ttl instructions or not.
145 *
146 * @return true if copy ttl instructions are supported
147 */
148 protected boolean supportCopyTtl() {
149 return true;
150 }
151
152 /**
153 * Determines whether this pipeline support set mpls bos instruction or not.
154 *
155 * @return true if set mpls bos instruction is supported
156 */
157 protected boolean supportSetMplsBos() {
158 return true;
159 }
160
Charles Chan188ebf52015-12-23 00:15:11 -0800161 protected void init(DeviceId deviceId, PipelinerContext context) {
162 this.deviceId = deviceId;
163 this.flowObjectiveStore = context.store();
164 this.serviceDirectory = context.directory();
165 this.groupService = serviceDirectory.get(GroupService.class);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800166 this.storageService = serviceDirectory.get(StorageService.class);
Madan Jampanid5714e02016-04-19 14:15:20 -0700167 this.nextIndex = storageService.getAtomicCounter("group-id-index-counter");
Charles Chan188ebf52015-12-23 00:15:11 -0800168
Charles Chanfc5c7802016-05-17 13:13:55 -0700169 pendingAddNextObjectives = CacheBuilder.newBuilder()
Charles Chan188ebf52015-12-23 00:15:11 -0800170 .expireAfterWrite(20, TimeUnit.SECONDS)
171 .removalListener((
172 RemovalNotification<GroupKey, List<OfdpaNextGroup>> notification) -> {
173 if (notification.getCause() == RemovalCause.EXPIRED) {
174 notification.getValue().forEach(ofdpaNextGrp ->
Charles Chan361154b2016-03-24 10:23:39 -0700175 Ofdpa2Pipeline.fail(ofdpaNextGrp.nextObj,
Charles Chan188ebf52015-12-23 00:15:11 -0800176 ObjectiveError.GROUPINSTALLATIONFAILED));
Charles Chanfc5c7802016-05-17 13:13:55 -0700177 }
178 }).build();
Charles Chan188ebf52015-12-23 00:15:11 -0800179
Charles Chanfc5c7802016-05-17 13:13:55 -0700180 pendingRemoveNextObjectives = CacheBuilder.newBuilder()
181 .expireAfterWrite(20, TimeUnit.SECONDS)
182 .removalListener((
183 RemovalNotification<NextObjective, List<GroupKey>> notification) -> {
184 if (notification.getCause() == RemovalCause.EXPIRED) {
185 Ofdpa2Pipeline.fail(notification.getKey(),
186 ObjectiveError.GROUPREMOVALFAILED);
Charles Chan188ebf52015-12-23 00:15:11 -0800187 }
188 }).build();
189 pendingGroups = new ConcurrentHashMap<>();
190 groupChecker.scheduleAtFixedRate(new GroupChecker(), 0, 500, TimeUnit.MILLISECONDS);
191
192 groupService.addListener(new InnerGroupListener());
193 }
194
Pier Ventre140a8942016-11-02 07:26:38 -0700195 /**
196 * The purpose of this function is to verify if the hashed next
197 * objective is supported by the current pipeline.
198 *
199 * @param nextObjective the hashed objective to verify
200 * @return true if the hashed objective is supported. Otherwise false.
201 */
202 public boolean verifyHashedNextObjective(NextObjective nextObjective) {
203 // if it is not hashed, there is something wrong;
204 if (nextObjective.type() != HASHED) {
205 return false;
206 }
207 // The case non supported is the MPLS-ECMP. For now, we try
208 // to create a MPLS-ECMP for the transport of a VPWS. The
209 // necessary info are contained in the meta selector. In particular
210 // we are looking for the case of BoS==False;
211 TrafficSelector metaSelector = nextObjective.meta();
212 if (metaSelector != null && isNotMplsBos(metaSelector)) {
213 return false;
214 }
215
216 return true;
217 }
218
Saurav Das8be4e3a2016-03-11 17:19:07 -0800219 //////////////////////////////////////
220 // Group Creation
221 //////////////////////////////////////
222
Charles Chan188ebf52015-12-23 00:15:11 -0800223 protected void addGroup(NextObjective nextObjective) {
224 switch (nextObjective.type()) {
225 case SIMPLE:
226 Collection<TrafficTreatment> treatments = nextObjective.next();
227 if (treatments.size() != 1) {
228 log.error("Next Objectives of type Simple should only have a "
229 + "single Traffic Treatment. Next Objective Id:{}",
230 nextObjective.id());
Charles Chan361154b2016-03-24 10:23:39 -0700231 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800232 return;
233 }
234 processSimpleNextObjective(nextObjective);
235 break;
236 case BROADCAST:
237 processBroadcastNextObjective(nextObjective);
238 break;
239 case HASHED:
Pier Ventre140a8942016-11-02 07:26:38 -0700240 if (!verifyHashedNextObjective(nextObjective)) {
241 log.error("Next Objectives of type hashed not supported. Next Objective Id:{}",
242 nextObjective.id());
243 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.BADPARAMS);
244 return;
245 }
Charles Chan188ebf52015-12-23 00:15:11 -0800246 processHashedNextObjective(nextObjective);
247 break;
248 case FAILOVER:
Charles Chan361154b2016-03-24 10:23:39 -0700249 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -0800250 log.warn("Unsupported next objective type {}", nextObjective.type());
251 break;
252 default:
Charles Chan361154b2016-03-24 10:23:39 -0700253 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.UNKNOWN);
Charles Chan188ebf52015-12-23 00:15:11 -0800254 log.warn("Unknown next objective type {}", nextObjective.type());
255 }
256 }
257
258 /**
259 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
260 * a chain of groups. The simple Next Objective passed
261 * in by the application has to be broken up into a group chain
262 * comprising of an L3 Unicast Group that points to an L2 Interface
263 * Group which in-turn points to an output port. In some cases, the simple
264 * next Objective can just be an L2 interface without the need for chaining.
265 *
266 * @param nextObj the nextObjective of type SIMPLE
267 */
268 private void processSimpleNextObjective(NextObjective nextObj) {
269 TrafficTreatment treatment = nextObj.next().iterator().next();
270 // determine if plain L2 or L3->L2
271 boolean plainL2 = true;
272 for (Instruction ins : treatment.allInstructions()) {
273 if (ins.type() == Instruction.Type.L2MODIFICATION) {
274 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
275 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_DST ||
276 l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_SRC) {
277 plainL2 = false;
278 break;
279 }
280 }
281 }
282
283 if (plainL2) {
284 createL2InterfaceGroup(nextObj);
285 return;
286 }
287
Pier Ventre140a8942016-11-02 07:26:38 -0700288 boolean isMpls = false;
Pier Ventre42287df2016-11-09 14:17:26 -0800289 // In order to understand if it is a pseudo wire related
290 // next objective we look for the tunnel id in the meta.
291 boolean isPw = false;
Pier Ventre140a8942016-11-02 07:26:38 -0700292 if (nextObj.meta() != null) {
293 isMpls = isNotMplsBos(nextObj.meta());
Pier Ventre42287df2016-11-09 14:17:26 -0800294
295 TunnelIdCriterion tunnelIdCriterion = (TunnelIdCriterion) nextObj
296 .meta()
297 .getCriterion(TUNNEL_ID);
298 if (tunnelIdCriterion != null) {
299 isPw = true;
300 }
301
Pier Ventre140a8942016-11-02 07:26:38 -0700302 }
303
Pier Ventre42287df2016-11-09 14:17:26 -0800304 if (!isPw) {
305 // break up simple next objective to GroupChain objects
306 GroupInfo groupInfo = createL2L3Chain(treatment, nextObj.id(),
307 nextObj.appId(), isMpls,
308 nextObj.meta());
309 if (groupInfo == null) {
310 log.error("Could not process nextObj={} in dev:{}", nextObj.id(), deviceId);
311 return;
312 }
313 // create object for local and distributed storage
314 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
315 gkeyChain.addFirst(groupInfo.innerMostGroupDesc.appCookie());
316 gkeyChain.addFirst(groupInfo.nextGroupDesc.appCookie());
317 OfdpaNextGroup ofdpaGrp =
318 new OfdpaNextGroup(Collections.singletonList(gkeyChain), nextObj);
319
320 // store l3groupkey with the ofdpaNextGroup for the nextObjective that depends on it
321 updatePendingNextObjective(groupInfo.nextGroupDesc.appCookie(), ofdpaGrp);
322
323 // now we are ready to send the l2 groupDescription (inner), as all the stores
324 // that will get async replies have been updated. By waiting to update
325 // the stores, we prevent nasty race conditions.
326 groupService.addGroup(groupInfo.innerMostGroupDesc);
327 } else {
328 // We handle the pseudo wire with a different a procedure.
329 // This procedure is meant to handle both initiation and
330 // termination of the pseudo wire.
331 processPwNextObjective(nextObj);
Charles Chan188ebf52015-12-23 00:15:11 -0800332 }
Charles Chan188ebf52015-12-23 00:15:11 -0800333 }
334
Charles Chan188ebf52015-12-23 00:15:11 -0800335 /**
336 * Creates a simple L2 Interface Group.
337 *
338 * @param nextObj the next Objective
339 */
340 private void createL2InterfaceGroup(NextObjective nextObj) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700341 VlanId assignedVlan = Ofdpa2Pipeline.readVlanFromSelector(nextObj.meta());
342 if (assignedVlan == null) {
343 log.warn("VLAN ID required by simple next obj is missing. Abort.");
344 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800345 return;
346 }
347
Charles Chan5b9df8d2016-03-28 22:21:40 -0700348 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Charles Chan188ebf52015-12-23 00:15:11 -0800349
Charles Chan5b9df8d2016-03-28 22:21:40 -0700350 // There is only one L2 interface group in this case
351 GroupDescription l2InterfaceGroupDesc = groupInfos.get(0).innerMostGroupDesc;
Charles Chan188ebf52015-12-23 00:15:11 -0800352
Charles Chan5b9df8d2016-03-28 22:21:40 -0700353 // Put all dependency information into allGroupKeys
354 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
355 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
356 gkeyChain.addFirst(l2InterfaceGroupDesc.appCookie());
357 allGroupKeys.add(gkeyChain);
Charles Chan188ebf52015-12-23 00:15:11 -0800358
Charles Chan5b9df8d2016-03-28 22:21:40 -0700359 // Point the next objective to this group
360 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
361 updatePendingNextObjective(l2InterfaceGroupDesc.appCookie(), ofdpaGrp);
362
363 // Start installing the inner-most group
364 groupService.addGroup(l2InterfaceGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800365 }
366
367 /**
368 * Creates one of two possible group-chains from the treatment
369 * passed in. Depending on the MPLS boolean, this method either creates
Charles Chan425854b2016-04-11 15:32:12 -0700370 * an L3Unicast Group --&gt; L2Interface Group, if mpls is false;
371 * or MPLSInterface Group --&gt; L2Interface Group, if mpls is true;
Charles Chan188ebf52015-12-23 00:15:11 -0800372 * The returned 'inner' group description is always the L2 Interface group.
373 *
374 * @param treatment that needs to be broken up to create the group chain
375 * @param nextId of the next objective that needs this group chain
376 * @param appId of the application that sent this next objective
377 * @param mpls determines if L3Unicast or MPLSInterface group is created
378 * @param meta metadata passed in by the application as part of the nextObjective
379 * @return GroupInfo containing the GroupDescription of the
380 * L2Interface group(inner) and the GroupDescription of the (outer)
381 * L3Unicast/MPLSInterface group. May return null if there is an
382 * error in processing the chain
383 */
Charles Chan425854b2016-04-11 15:32:12 -0700384 protected GroupInfo createL2L3Chain(TrafficTreatment treatment, int nextId,
Charles Chanf9e98652016-09-07 16:54:23 -0700385 ApplicationId appId, boolean mpls,
386 TrafficSelector meta) {
387 return createL2L3ChainInternal(treatment, nextId, appId, mpls, meta, true);
388 }
389
390 /**
391 * Internal implementation of createL2L3Chain.
392 * <p>
393 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
394 * Since it is non-OF spec, we need an extension treatment for that.
395 * The useSetVlanExtension must be set to false for OFDPA i12.
396 * </p>
397 *
398 * @param treatment that needs to be broken up to create the group chain
399 * @param nextId of the next objective that needs this group chain
400 * @param appId of the application that sent this next objective
401 * @param mpls determines if L3Unicast or MPLSInterface group is created
402 * @param meta metadata passed in by the application as part of the nextObjective
403 * @param useSetVlanExtension use the setVlanVid extension that has is_present bit set to 0.
404 * @return GroupInfo containing the GroupDescription of the
405 * L2Interface group(inner) and the GroupDescription of the (outer)
406 * L3Unicast/MPLSInterface group. May return null if there is an
407 * error in processing the chain
408 */
409 protected GroupInfo createL2L3ChainInternal(TrafficTreatment treatment, int nextId,
Pier Ventre42287df2016-11-09 14:17:26 -0800410 ApplicationId appId, boolean mpls,
411 TrafficSelector meta, boolean useSetVlanExtension) {
Charles Chan188ebf52015-12-23 00:15:11 -0800412 // for the l2interface group, get vlan and port info
413 // for the outer group, get the src/dst mac, and vlan info
414 TrafficTreatment.Builder outerTtb = DefaultTrafficTreatment.builder();
415 TrafficTreatment.Builder innerTtb = DefaultTrafficTreatment.builder();
416 VlanId vlanid = null;
417 long portNum = 0;
418 boolean setVlan = false, popVlan = false;
Charles Chand0fd5dc2016-02-16 23:14:49 -0800419 MacAddress srcMac = MacAddress.ZERO;
Charles Chan5270ed02016-01-30 23:22:37 -0800420 MacAddress dstMac = MacAddress.ZERO;
Charles Chan188ebf52015-12-23 00:15:11 -0800421 for (Instruction ins : treatment.allInstructions()) {
422 if (ins.type() == Instruction.Type.L2MODIFICATION) {
423 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
424 switch (l2ins.subtype()) {
425 case ETH_DST:
Charles Chan5270ed02016-01-30 23:22:37 -0800426 dstMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
427 outerTtb.setEthDst(dstMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800428 break;
429 case ETH_SRC:
Charles Chand0fd5dc2016-02-16 23:14:49 -0800430 srcMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
431 outerTtb.setEthSrc(srcMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800432 break;
433 case VLAN_ID:
434 vlanid = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
Charles Chanf9e98652016-09-07 16:54:23 -0700435 if (useSetVlanExtension) {
436 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
437 outerTtb.extension(ofdpaSetVlanVid, deviceId);
438 } else {
439 outerTtb.setVlanId(vlanid);
440 }
Charles Chan188ebf52015-12-23 00:15:11 -0800441 setVlan = true;
442 break;
443 case VLAN_POP:
444 innerTtb.popVlan();
445 popVlan = true;
446 break;
447 case DEC_MPLS_TTL:
448 case MPLS_LABEL:
449 case MPLS_POP:
450 case MPLS_PUSH:
451 case VLAN_PCP:
452 case VLAN_PUSH:
453 default:
454 break;
455 }
456 } else if (ins.type() == Instruction.Type.OUTPUT) {
457 portNum = ((Instructions.OutputInstruction) ins).port().toLong();
458 innerTtb.add(ins);
459 } else {
460 log.warn("Driver does not handle this type of TrafficTreatment"
461 + " instruction in nextObjectives: {}", ins.type());
462 }
463 }
464
465 if (vlanid == null && meta != null) {
466 // use metadata if available
Pier Ventre140a8942016-11-02 07:26:38 -0700467 Criterion vidCriterion = meta.getCriterion(VLAN_VID);
Charles Chan188ebf52015-12-23 00:15:11 -0800468 if (vidCriterion != null) {
469 vlanid = ((VlanIdCriterion) vidCriterion).vlanId();
470 }
471 // if vlan is not set, use the vlan in metadata for outerTtb
472 if (vlanid != null && !setVlan) {
Charles Chanf9e98652016-09-07 16:54:23 -0700473 if (useSetVlanExtension) {
474 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
475 outerTtb.extension(ofdpaSetVlanVid, deviceId);
476 } else {
477 outerTtb.setVlanId(vlanid);
478 }
Charles Chan188ebf52015-12-23 00:15:11 -0800479 }
480 }
481
482 if (vlanid == null) {
483 log.error("Driver cannot process an L2/L3 group chain without "
484 + "egress vlan information for dev: {} port:{}",
485 deviceId, portNum);
486 return null;
487 }
488
489 if (!setVlan && !popVlan) {
490 // untagged outgoing port
491 TrafficTreatment.Builder temp = DefaultTrafficTreatment.builder();
492 temp.popVlan();
493 innerTtb.build().allInstructions().forEach(i -> temp.add(i));
494 innerTtb = temp;
495 }
496
497 // assemble information for ofdpa l2interface group
Charles Chane849c192016-01-11 18:28:54 -0800498 int l2groupId = L2_INTERFACE_TYPE | (vlanid.toShort() << 16) | (int) portNum;
Saurav Das8be4e3a2016-03-11 17:19:07 -0800499 // a globally unique groupkey that is different for ports in the same device,
Charles Chan188ebf52015-12-23 00:15:11 -0800500 // but different for the same portnumber on different devices. Also different
501 // for the various group-types created out of the same next objective.
Charles Chane849c192016-01-11 18:28:54 -0800502 int l2gk = l2InterfaceGroupKey(deviceId, vlanid, portNum);
Charles Chan361154b2016-03-24 10:23:39 -0700503 final GroupKey l2groupkey = new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(l2gk));
Charles Chan188ebf52015-12-23 00:15:11 -0800504
505 // assemble information for outer group
506 GroupDescription outerGrpDesc = null;
507 if (mpls) {
508 // outer group is MPLSInteface
Saurav Das8be4e3a2016-03-11 17:19:07 -0800509 int mplsInterfaceIndex = getNextAvailableIndex();
510 int mplsgroupId = MPLS_INTERFACE_TYPE | (SUBTYPE_MASK & mplsInterfaceIndex);
511 final GroupKey mplsgroupkey = new DefaultGroupKey(
Charles Chan361154b2016-03-24 10:23:39 -0700512 Ofdpa2Pipeline.appKryo.serialize(mplsInterfaceIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800513 outerTtb.group(new DefaultGroupId(l2groupId));
514 // create the mpls-interface group description to wait for the
515 // l2 interface group to be processed
516 GroupBucket mplsinterfaceGroupBucket =
517 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
518 outerGrpDesc = new DefaultGroupDescription(
519 deviceId,
520 GroupDescription.Type.INDIRECT,
521 new GroupBuckets(Collections.singletonList(
522 mplsinterfaceGroupBucket)),
523 mplsgroupkey,
524 mplsgroupId,
525 appId);
526 log.debug("Trying MPLS-Interface: device:{} gid:{} gkey:{} nextid:{}",
527 deviceId, Integer.toHexString(mplsgroupId),
528 mplsgroupkey, nextId);
529 } else {
530 // outer group is L3Unicast
Saurav Das8be4e3a2016-03-11 17:19:07 -0800531 int l3unicastIndex = getNextAvailableIndex();
532 int l3groupId = L3_UNICAST_TYPE | (TYPE_MASK & l3unicastIndex);
533 final GroupKey l3groupkey = new DefaultGroupKey(
Charles Chan361154b2016-03-24 10:23:39 -0700534 Ofdpa2Pipeline.appKryo.serialize(l3unicastIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800535 outerTtb.group(new DefaultGroupId(l2groupId));
536 // create the l3unicast group description to wait for the
537 // l2 interface group to be processed
538 GroupBucket l3unicastGroupBucket =
539 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
540 outerGrpDesc = new DefaultGroupDescription(
541 deviceId,
542 GroupDescription.Type.INDIRECT,
543 new GroupBuckets(Collections.singletonList(
544 l3unicastGroupBucket)),
545 l3groupkey,
546 l3groupId,
547 appId);
548 log.debug("Trying L3Unicast: device:{} gid:{} gkey:{} nextid:{}",
549 deviceId, Integer.toHexString(l3groupId),
550 l3groupkey, nextId);
551 }
552
553 // store l2groupkey with the groupChainElem for the outer-group that depends on it
554 GroupChainElem gce = new GroupChainElem(outerGrpDesc, 1, false);
555 updatePendingGroups(l2groupkey, gce);
556
557 // create group description for the inner l2interfacegroup
Charles Chan5b9df8d2016-03-28 22:21:40 -0700558 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800559 DefaultGroupBucket.createIndirectGroupBucket(innerTtb.build());
560 GroupDescription l2groupDescription =
561 new DefaultGroupDescription(
562 deviceId,
563 GroupDescription.Type.INDIRECT,
564 new GroupBuckets(Collections.singletonList(
Charles Chan5b9df8d2016-03-28 22:21:40 -0700565 l2InterfaceGroupBucket)),
Charles Chan188ebf52015-12-23 00:15:11 -0800566 l2groupkey,
567 l2groupId,
568 appId);
569 log.debug("Trying L2Interface: device:{} gid:{} gkey:{} nextId:{}",
570 deviceId, Integer.toHexString(l2groupId),
571 l2groupkey, nextId);
572 return new GroupInfo(l2groupDescription, outerGrpDesc);
573
574 }
575
576 /**
577 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
578 * a chain of groups. The broadcast Next Objective passed in by the application
579 * has to be broken up into a group chain comprising of an
Saurav Das1a129a02016-11-18 15:21:57 -0800580 * L2 Flood group or L3 Multicast group, whose buckets point to L2 Interface groups.
Charles Chan188ebf52015-12-23 00:15:11 -0800581 *
582 * @param nextObj the nextObjective of type BROADCAST
583 */
584 private void processBroadcastNextObjective(NextObjective nextObj) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700585 VlanId assignedVlan = Ofdpa2Pipeline.readVlanFromSelector(nextObj.meta());
586 if (assignedVlan == null) {
587 log.warn("VLAN ID required by broadcast next obj is missing. Abort.");
588 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800589 return;
590 }
Charles Chan188ebf52015-12-23 00:15:11 -0800591
Charles Chan5b9df8d2016-03-28 22:21:40 -0700592 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
593
594 IpPrefix ipDst = Ofdpa2Pipeline.readIpDstFromSelector(nextObj.meta());
595 if (ipDst != null) {
596 if (ipDst.isMulticast()) {
597 createL3MulticastGroup(nextObj, assignedVlan, groupInfos);
598 } else {
599 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
600 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
601 return;
602 }
603 } else {
604 createL2FloodGroup(nextObj, assignedVlan, groupInfos);
605 }
606 }
607
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700608 private List<GroupInfo> prepareL2InterfaceGroup(NextObjective nextObj,
609 VlanId assignedVlan) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700610 ImmutableList.Builder<GroupInfo> groupInfoBuilder = ImmutableList.builder();
611
612 // break up broadcast next objective to multiple groups
613 Collection<TrafficTreatment> buckets = nextObj.next();
614
Charles Chan188ebf52015-12-23 00:15:11 -0800615 // each treatment is converted to an L2 interface group
Charles Chan188ebf52015-12-23 00:15:11 -0800616 for (TrafficTreatment treatment : buckets) {
617 TrafficTreatment.Builder newTreatment = DefaultTrafficTreatment.builder();
618 PortNumber portNum = null;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700619 VlanId egressVlan = null;
Charles Chan188ebf52015-12-23 00:15:11 -0800620 // ensure that the only allowed treatments are pop-vlan and output
621 for (Instruction ins : treatment.allInstructions()) {
622 if (ins.type() == Instruction.Type.L2MODIFICATION) {
623 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
624 switch (l2ins.subtype()) {
625 case VLAN_POP:
626 newTreatment.add(l2ins);
627 break;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700628 case VLAN_ID:
629 egressVlan = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
630 break;
Charles Chan188ebf52015-12-23 00:15:11 -0800631 default:
632 log.debug("action {} not permitted for broadcast nextObj",
633 l2ins.subtype());
634 break;
635 }
636 } else if (ins.type() == Instruction.Type.OUTPUT) {
637 portNum = ((Instructions.OutputInstruction) ins).port();
638 newTreatment.add(ins);
639 } else {
Charles Chane849c192016-01-11 18:28:54 -0800640 log.debug("TrafficTreatment of type {} not permitted in " +
641 " broadcast nextObjective", ins.type());
Charles Chan188ebf52015-12-23 00:15:11 -0800642 }
643 }
644
Charles Chan188ebf52015-12-23 00:15:11 -0800645 // assemble info for l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700646 VlanId l2InterfaceGroupVlan =
647 (egressVlan != null && !assignedVlan.equals(egressVlan)) ?
648 egressVlan : assignedVlan;
649 int l2gk = l2InterfaceGroupKey(deviceId, l2InterfaceGroupVlan, portNum.toLong());
650 final GroupKey l2InterfaceGroupKey =
651 new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(l2gk));
Charles Chan372b63e2017-02-07 12:10:53 -0800652 int l2InterfaceGroupId = L2_INTERFACE_TYPE |
653 ((l2InterfaceGroupVlan.toShort() & THREE_BIT_MASK) << PORT_LEN) |
654 ((int) portNum.toLong() & FOUR_BIT_MASK);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700655 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800656 DefaultGroupBucket.createIndirectGroupBucket(newTreatment.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700657 GroupDescription l2InterfaceGroupDescription =
Charles Chan188ebf52015-12-23 00:15:11 -0800658 new DefaultGroupDescription(
659 deviceId,
660 GroupDescription.Type.INDIRECT,
661 new GroupBuckets(Collections.singletonList(
Charles Chan5b9df8d2016-03-28 22:21:40 -0700662 l2InterfaceGroupBucket)),
663 l2InterfaceGroupKey,
664 l2InterfaceGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800665 nextObj.appId());
666 log.debug("Trying L2-Interface: device:{} gid:{} gkey:{} nextid:{}",
Charles Chan5b9df8d2016-03-28 22:21:40 -0700667 deviceId, Integer.toHexString(l2InterfaceGroupId),
668 l2InterfaceGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -0800669
Charles Chan5b9df8d2016-03-28 22:21:40 -0700670 groupInfoBuilder.add(new GroupInfo(l2InterfaceGroupDescription,
671 l2InterfaceGroupDescription));
Charles Chan188ebf52015-12-23 00:15:11 -0800672 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700673 return groupInfoBuilder.build();
674 }
Charles Chan188ebf52015-12-23 00:15:11 -0800675
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700676 private void createL2FloodGroup(NextObjective nextObj, VlanId vlanId,
677 List<GroupInfo> groupInfos) {
678 // assemble info for l2 flood group. Since there can be only one flood
679 // group for a vlan, its index is always the same - 0
Saurav Das0fd79d92016-03-07 10:58:36 -0800680 Integer l2floodgroupId = L2_FLOOD_TYPE | (vlanId.toShort() << 16);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800681 int l2floodgk = getNextAvailableIndex();
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700682 final GroupKey l2floodgroupkey =
683 new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(l2floodgk));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700684
Charles Chan188ebf52015-12-23 00:15:11 -0800685 // collection of group buckets pointing to all the l2 interface groups
Charles Chan5b9df8d2016-03-28 22:21:40 -0700686 List<GroupBucket> l2floodBuckets = Lists.newArrayList();
687 groupInfos.forEach(groupInfo -> {
688 GroupDescription l2intGrpDesc = groupInfo.nextGroupDesc;
Charles Chan188ebf52015-12-23 00:15:11 -0800689 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
690 ttb.group(new DefaultGroupId(l2intGrpDesc.givenGroupId()));
691 GroupBucket abucket = DefaultGroupBucket.createAllGroupBucket(ttb.build());
692 l2floodBuckets.add(abucket);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700693 });
Charles Chan188ebf52015-12-23 00:15:11 -0800694 // create the l2flood group-description to wait for all the
695 // l2interface groups to be processed
696 GroupDescription l2floodGroupDescription =
697 new DefaultGroupDescription(
698 deviceId,
699 GroupDescription.Type.ALL,
700 new GroupBuckets(l2floodBuckets),
701 l2floodgroupkey,
702 l2floodgroupId,
703 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800704 log.debug("Trying L2-Flood: device:{} gid:{} gkey:{} nextid:{}",
705 deviceId, Integer.toHexString(l2floodgroupId),
706 l2floodgroupkey, nextObj.id());
707
Charles Chan5b9df8d2016-03-28 22:21:40 -0700708 // Put all dependency information into allGroupKeys
709 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
710 groupInfos.forEach(groupInfo -> {
711 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
712 // In this case we should have L2 interface group only
713 gkeyChain.addFirst(groupInfo.nextGroupDesc.appCookie());
714 gkeyChain.addFirst(l2floodgroupkey);
715 allGroupKeys.add(gkeyChain);
716 });
Charles Chan188ebf52015-12-23 00:15:11 -0800717
Charles Chan5b9df8d2016-03-28 22:21:40 -0700718 // Point the next objective to this group
719 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Charles Chan188ebf52015-12-23 00:15:11 -0800720 updatePendingNextObjective(l2floodgroupkey, ofdpaGrp);
721
Charles Chan5b9df8d2016-03-28 22:21:40 -0700722 GroupChainElem gce = new GroupChainElem(l2floodGroupDescription,
723 groupInfos.size(), false);
724 groupInfos.forEach(groupInfo -> {
725 // Point this group to the next group
726 updatePendingGroups(groupInfo.nextGroupDesc.appCookie(), gce);
727 // Start installing the inner-most group
728 groupService.addGroup(groupInfo.innerMostGroupDesc);
729 });
730 }
731
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700732 private void createL3MulticastGroup(NextObjective nextObj, VlanId vlanId,
733 List<GroupInfo> groupInfos) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700734 List<GroupBucket> l3McastBuckets = new ArrayList<>();
735 groupInfos.forEach(groupInfo -> {
736 // Points to L3 interface group if there is one.
737 // Otherwise points to L2 interface group directly.
738 GroupDescription nextGroupDesc = (groupInfo.nextGroupDesc != null) ?
739 groupInfo.nextGroupDesc : groupInfo.innerMostGroupDesc;
740 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
741 ttb.group(new DefaultGroupId(nextGroupDesc.givenGroupId()));
742 GroupBucket abucket = DefaultGroupBucket.createAllGroupBucket(ttb.build());
743 l3McastBuckets.add(abucket);
744 });
745
746 int l3MulticastIndex = getNextAvailableIndex();
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700747 int l3MulticastGroupId = L3_MULTICAST_TYPE |
748 vlanId.toShort() << 16 | (TYPE_VLAN_MASK & l3MulticastIndex);
749 final GroupKey l3MulticastGroupKey =
750 new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(l3MulticastIndex));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700751
752 GroupDescription l3MulticastGroupDesc = new DefaultGroupDescription(deviceId,
753 GroupDescription.Type.ALL,
754 new GroupBuckets(l3McastBuckets),
755 l3MulticastGroupKey,
756 l3MulticastGroupId,
757 nextObj.appId());
758
759 // Put all dependency information into allGroupKeys
760 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
761 groupInfos.forEach(groupInfo -> {
762 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
763 gkeyChain.addFirst(groupInfo.innerMostGroupDesc.appCookie());
764 // Add L3 interface group to the chain if there is one.
765 if (!groupInfo.nextGroupDesc.equals(groupInfo.innerMostGroupDesc)) {
766 gkeyChain.addFirst(groupInfo.nextGroupDesc.appCookie());
767 }
768 gkeyChain.addFirst(l3MulticastGroupKey);
769 allGroupKeys.add(gkeyChain);
770 });
771
772 // Point the next objective to this group
773 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
774 updatePendingNextObjective(l3MulticastGroupKey, ofdpaGrp);
775
776 GroupChainElem outerGce = new GroupChainElem(l3MulticastGroupDesc,
777 groupInfos.size(), false);
778 groupInfos.forEach(groupInfo -> {
779 // Point this group (L3 multicast) to the next group
780 updatePendingGroups(groupInfo.nextGroupDesc.appCookie(), outerGce);
781
782 // Point next group to inner-most group, if any
783 if (!groupInfo.nextGroupDesc.equals(groupInfo.innerMostGroupDesc)) {
784 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc,
785 1, false);
786 updatePendingGroups(groupInfo.innerMostGroupDesc.appCookie(), innerGce);
787 }
788
789 // Start installing the inner-most group
790 groupService.addGroup(groupInfo.innerMostGroupDesc);
791 });
Charles Chan188ebf52015-12-23 00:15:11 -0800792 }
793
Charles Chan188ebf52015-12-23 00:15:11 -0800794 /**
795 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
796 * a chain of groups. The hashed Next Objective passed in by the application
797 * has to be broken up into a group chain comprising of an
798 * L3 ECMP group as the top level group. Buckets of this group can point
799 * to a variety of groups in a group chain, depending on the whether
800 * MPLS labels are being pushed or not.
801 * <p>
802 * NOTE: We do not create MPLS ECMP groups as they are unimplemented in
803 * OF-DPA 2.0 (even though it is in the spec). Therefore we do not
804 * check the nextObjective meta to see what is matching before being
805 * sent to this nextObjective.
806 *
807 * @param nextObj the nextObjective of type HASHED
808 */
Pier Ventre140a8942016-11-02 07:26:38 -0700809 protected void processHashedNextObjective(NextObjective nextObj) {
Charles Chan188ebf52015-12-23 00:15:11 -0800810 // storage for all group keys in the chain of groups created
811 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
812 List<GroupInfo> unsentGroups = new ArrayList<>();
813 createHashBucketChains(nextObj, allGroupKeys, unsentGroups);
814
815 // now we can create the outermost L3 ECMP group
816 List<GroupBucket> l3ecmpGroupBuckets = new ArrayList<>();
817 for (GroupInfo gi : unsentGroups) {
818 // create ECMP bucket to point to the outer group
819 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700820 ttb.group(new DefaultGroupId(gi.nextGroupDesc.givenGroupId()));
Charles Chan188ebf52015-12-23 00:15:11 -0800821 GroupBucket sbucket = DefaultGroupBucket
822 .createSelectGroupBucket(ttb.build());
823 l3ecmpGroupBuckets.add(sbucket);
824 }
Saurav Das8be4e3a2016-03-11 17:19:07 -0800825 int l3ecmpIndex = getNextAvailableIndex();
826 int l3ecmpGroupId = L3_ECMP_TYPE | (TYPE_MASK & l3ecmpIndex);
827 GroupKey l3ecmpGroupKey = new DefaultGroupKey(
Charles Chan361154b2016-03-24 10:23:39 -0700828 Ofdpa2Pipeline.appKryo.serialize(l3ecmpIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800829 GroupDescription l3ecmpGroupDesc =
830 new DefaultGroupDescription(
831 deviceId,
832 GroupDescription.Type.SELECT,
833 new GroupBuckets(l3ecmpGroupBuckets),
834 l3ecmpGroupKey,
835 l3ecmpGroupId,
836 nextObj.appId());
837 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
838 l3ecmpGroupBuckets.size(),
839 false);
840
841 // create objects for local and distributed storage
842 allGroupKeys.forEach(gkeyChain -> gkeyChain.addFirst(l3ecmpGroupKey));
843 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
844
845 // store l3ecmpGroupKey with the ofdpaGroupChain for the nextObjective
846 // that depends on it
847 updatePendingNextObjective(l3ecmpGroupKey, ofdpaGrp);
848
849 log.debug("Trying L3ECMP: device:{} gid:{} gkey:{} nextId:{}",
850 deviceId, Integer.toHexString(l3ecmpGroupId),
851 l3ecmpGroupKey, nextObj.id());
852 // finally we are ready to send the innermost groups
853 for (GroupInfo gi : unsentGroups) {
854 log.debug("Sending innermost group {} in group chain on device {} ",
Charles Chan5b9df8d2016-03-28 22:21:40 -0700855 Integer.toHexString(gi.innerMostGroupDesc.givenGroupId()), deviceId);
856 updatePendingGroups(gi.nextGroupDesc.appCookie(), l3ecmpGce);
857 groupService.addGroup(gi.innerMostGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800858 }
859
860 }
861
862 /**
863 * Creates group chains for all buckets in a hashed group, and stores the
864 * GroupInfos and GroupKeys for all the groups in the lists passed in, which
865 * should be empty.
866 * <p>
867 * Does not create the top level ECMP group. Does not actually send the
868 * groups to the groupService.
869 *
870 * @param nextObj the Next Objective with buckets that need to be converted
871 * to group chains
872 * @param allGroupKeys a list to store groupKey for each bucket-group-chain
873 * @param unsentGroups a list to store GroupInfo for each bucket-group-chain
874 */
Pier Ventre140a8942016-11-02 07:26:38 -0700875 protected void createHashBucketChains(NextObjective nextObj,
Saurav Das8be4e3a2016-03-11 17:19:07 -0800876 List<Deque<GroupKey>> allGroupKeys,
877 List<GroupInfo> unsentGroups) {
Charles Chan188ebf52015-12-23 00:15:11 -0800878 // break up hashed next objective to multiple groups
879 Collection<TrafficTreatment> buckets = nextObj.next();
880
881 for (TrafficTreatment bucket : buckets) {
882 //figure out how many labels are pushed in each bucket
883 int labelsPushed = 0;
884 MplsLabel innermostLabel = null;
885 for (Instruction ins : bucket.allInstructions()) {
886 if (ins.type() == Instruction.Type.L2MODIFICATION) {
887 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
888 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_PUSH) {
889 labelsPushed++;
890 }
891 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) {
892 if (innermostLabel == null) {
Ray Milkey125572b2016-02-22 16:48:17 -0800893 innermostLabel = ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label();
Charles Chan188ebf52015-12-23 00:15:11 -0800894 }
895 }
896 }
897 }
898
899 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
900 // XXX we only deal with 0 and 1 label push right now
901 if (labelsPushed == 0) {
Pier Ventre140a8942016-11-02 07:26:38 -0700902 GroupInfo nolabelGroupInfo;
903 TrafficSelector metaSelector = nextObj.meta();
904 if (metaSelector != null) {
905 if (isNotMplsBos(metaSelector)) {
906 nolabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
907 nextObj.appId(), true,
908 nextObj.meta());
909 } else {
910 nolabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
911 nextObj.appId(), false,
912 nextObj.meta());
913 }
914 } else {
915 nolabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
916 nextObj.appId(), false,
917 nextObj.meta());
918 }
Charles Chan188ebf52015-12-23 00:15:11 -0800919 if (nolabelGroupInfo == null) {
920 log.error("Could not process nextObj={} in dev:{}",
921 nextObj.id(), deviceId);
922 return;
923 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700924 gkeyChain.addFirst(nolabelGroupInfo.innerMostGroupDesc.appCookie());
925 gkeyChain.addFirst(nolabelGroupInfo.nextGroupDesc.appCookie());
Charles Chan188ebf52015-12-23 00:15:11 -0800926
927 // we can't send the inner group description yet, as we have to
928 // create the dependent ECMP group first. So we store..
929 unsentGroups.add(nolabelGroupInfo);
930
931 } else if (labelsPushed == 1) {
932 GroupInfo onelabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
933 nextObj.appId(), true,
934 nextObj.meta());
935 if (onelabelGroupInfo == null) {
936 log.error("Could not process nextObj={} in dev:{}",
937 nextObj.id(), deviceId);
938 return;
939 }
940 // we need to add another group to this chain - the L3VPN group
941 TrafficTreatment.Builder l3vpnTtb = DefaultTrafficTreatment.builder();
942 l3vpnTtb.pushMpls()
943 .setMpls(innermostLabel)
Charles Chan40132b32017-01-22 00:19:37 -0800944 .group(new DefaultGroupId(onelabelGroupInfo.nextGroupDesc.givenGroupId()));
945 if (supportCopyTtl()) {
946 l3vpnTtb.copyTtlOut();
947 }
948 if (supportSetMplsBos()) {
949 l3vpnTtb.setMplsBos(true);
950 }
951
Charles Chan188ebf52015-12-23 00:15:11 -0800952 GroupBucket l3vpnGrpBkt =
953 DefaultGroupBucket.createIndirectGroupBucket(l3vpnTtb.build());
Saurav Das8be4e3a2016-03-11 17:19:07 -0800954 int l3vpnIndex = getNextAvailableIndex();
955 int l3vpngroupId = MPLS_L3VPN_SUBTYPE | (SUBTYPE_MASK & l3vpnIndex);
956 GroupKey l3vpngroupkey = new DefaultGroupKey(
Charles Chan361154b2016-03-24 10:23:39 -0700957 Ofdpa2Pipeline.appKryo.serialize(l3vpnIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800958 GroupDescription l3vpnGroupDesc =
959 new DefaultGroupDescription(
960 deviceId,
961 GroupDescription.Type.INDIRECT,
962 new GroupBuckets(Collections.singletonList(
963 l3vpnGrpBkt)),
964 l3vpngroupkey,
965 l3vpngroupId,
966 nextObj.appId());
967 GroupChainElem l3vpnGce = new GroupChainElem(l3vpnGroupDesc, 1, false);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700968 updatePendingGroups(onelabelGroupInfo.nextGroupDesc.appCookie(), l3vpnGce);
Charles Chan188ebf52015-12-23 00:15:11 -0800969
Charles Chan5b9df8d2016-03-28 22:21:40 -0700970 gkeyChain.addFirst(onelabelGroupInfo.innerMostGroupDesc.appCookie());
971 gkeyChain.addFirst(onelabelGroupInfo.nextGroupDesc.appCookie());
Charles Chan188ebf52015-12-23 00:15:11 -0800972 gkeyChain.addFirst(l3vpngroupkey);
973
974 //now we can replace the outerGrpDesc with the one we just created
Charles Chan5b9df8d2016-03-28 22:21:40 -0700975 onelabelGroupInfo.nextGroupDesc = l3vpnGroupDesc;
Charles Chan188ebf52015-12-23 00:15:11 -0800976
977 // we can't send the innermost group yet, as we have to create
978 // the dependent ECMP group first. So we store ...
979 unsentGroups.add(onelabelGroupInfo);
980
981 log.debug("Trying L3VPN: device:{} gid:{} gkey:{} nextId:{}",
982 deviceId, Integer.toHexString(l3vpngroupId),
983 l3vpngroupkey, nextObj.id());
984
985 } else {
986 log.warn("Driver currently does not handle more than 1 MPLS "
987 + "labels. Not processing nextObjective {}", nextObj.id());
988 return;
989 }
990
991 // all groups in this chain
992 allGroupKeys.add(gkeyChain);
993 }
994 }
995
Pier Ventre42287df2016-11-09 14:17:26 -0800996 /**
997 * Processes the pseudo wire related next objective.
998 * This procedure try to reuse the mpls label groups,
999 * the mpls interface group and the l2 interface group.
1000 *
1001 * @param nextObjective the objective to process.
1002 */
1003 protected void processPwNextObjective(NextObjective nextObjective) {
1004 log.warn("Pseudo wire extensions are not support for the OFDPA 2.0 {}", nextObjective.id());
1005 return;
1006 }
1007
Saurav Das8be4e3a2016-03-11 17:19:07 -08001008 //////////////////////////////////////
1009 // Group Editing
1010 //////////////////////////////////////
1011
Charles Chan188ebf52015-12-23 00:15:11 -08001012 /**
1013 * Adds a bucket to the top level group of a group-chain, and creates the chain.
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001014 * Ensures that bucket being added is not a duplicate, by checking existing
1015 * buckets for the same outport.
Charles Chan188ebf52015-12-23 00:15:11 -08001016 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001017 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -08001018 * @param next the representation of the existing group-chain for this next objective
1019 */
1020 protected void addBucketToGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001021 if (nextObjective.type() != NextObjective.Type.HASHED &&
1022 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001023 log.warn("AddBuckets not applied to nextType:{} in dev:{} for next:{}",
1024 nextObjective.type(), deviceId, nextObjective.id());
Saurav Das1a129a02016-11-18 15:21:57 -08001025 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001026 return;
1027 }
1028 if (nextObjective.next().size() > 1) {
Saurav Das1a129a02016-11-18 15:21:57 -08001029 // FIXME - support editing multiple buckets CORD-555
Charles Chan188ebf52015-12-23 00:15:11 -08001030 log.warn("Only one bucket can be added at a time");
Saurav Das1a129a02016-11-18 15:21:57 -08001031 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001032 return;
1033 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001034 // first check to see if bucket being added is not a duplicate of an
1035 // existing bucket. If it is for an existing outport, then its a duplicate.
1036 Set<PortNumber> existingOutPorts = new HashSet<>();
1037 List<Deque<GroupKey>> allActiveKeys = Ofdpa2Pipeline.appKryo.deserialize(next.data());
1038 for (Deque<GroupKey> gkeys : allActiveKeys) {
1039 // get the last group for the outport
1040 Group glast = groupService.getGroup(deviceId, gkeys.peekLast());
1041 if (glast != null && !glast.buckets().buckets().isEmpty()) {
1042 PortNumber op = readOutPortFromTreatment(
1043 glast.buckets().buckets().get(0).treatment());
1044 if (op != null) {
1045 existingOutPorts.add(op);
1046 }
1047 }
1048 }
1049 // only a single bucket being added
1050 TrafficTreatment tt = nextObjective.next().iterator().next();
1051 PortNumber newport = readOutPortFromTreatment(tt);
1052 if (existingOutPorts.contains(newport)) {
Charles Chane5ccd582016-11-10 14:16:54 -08001053 log.info("Attempt to add bucket for existing outport:{} in dev:{} for next:{}",
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001054 newport, deviceId, nextObjective.id());
1055 return;
1056 }
Saurav Das1a129a02016-11-18 15:21:57 -08001057 if (nextObjective.type() == NextObjective.Type.HASHED) {
1058 addBucketToHashGroup(nextObjective, allActiveKeys, newport);
1059 } else if (nextObjective.type() == NextObjective.Type.BROADCAST) {
1060 addBucketToBroadcastGroup(nextObjective, allActiveKeys, newport);
1061 }
1062 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001063
Saurav Das1a129a02016-11-18 15:21:57 -08001064 private void addBucketToHashGroup(NextObjective nextObjective,
1065 List<Deque<GroupKey>> allActiveKeys,
1066 PortNumber newport) {
Charles Chan188ebf52015-12-23 00:15:11 -08001067 // storage for all group keys in the chain of groups created
1068 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
1069 List<GroupInfo> unsentGroups = new ArrayList<>();
1070 createHashBucketChains(nextObjective, allGroupKeys, unsentGroups);
1071
Saurav Das1a129a02016-11-18 15:21:57 -08001072 // now we can create the bucket to add to the outermost L3 ECMP group
Charles Chan188ebf52015-12-23 00:15:11 -08001073 GroupInfo gi = unsentGroups.get(0); // only one bucket, so only one group-chain
1074 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001075 ttb.group(new DefaultGroupId(gi.nextGroupDesc.givenGroupId()));
Charles Chan188ebf52015-12-23 00:15:11 -08001076 GroupBucket sbucket = DefaultGroupBucket.createSelectGroupBucket(ttb.build());
1077
Saurav Das1a129a02016-11-18 15:21:57 -08001078 // retrieve the original L3 ECMP group
1079 Group l3ecmpGroup = retrieveTopLevelGroup(allActiveKeys, nextObjective.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001080 if (l3ecmpGroup == null) {
Saurav Das1a129a02016-11-18 15:21:57 -08001081 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.GROUPMISSING);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001082 return;
1083 }
Saurav Das1a129a02016-11-18 15:21:57 -08001084 GroupKey l3ecmpGroupKey = l3ecmpGroup.appCookie();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001085 int l3ecmpGroupId = l3ecmpGroup.id().id();
Charles Chan188ebf52015-12-23 00:15:11 -08001086
1087 // Although GroupDescriptions are not necessary for adding buckets to
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001088 // existing groups, we still use one in the GroupChainElem. When the latter is
Charles Chan188ebf52015-12-23 00:15:11 -08001089 // processed, the info will be extracted for the bucketAdd call to groupService
1090 GroupDescription l3ecmpGroupDesc =
1091 new DefaultGroupDescription(
1092 deviceId,
1093 GroupDescription.Type.SELECT,
1094 new GroupBuckets(Collections.singletonList(sbucket)),
1095 l3ecmpGroupKey,
1096 l3ecmpGroupId,
1097 nextObjective.appId());
1098 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc, 1, true);
1099
1100 // update original NextGroup with new bucket-chain
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001101 // If active keys shows only the top-level group without a chain of groups,
1102 // then it represents an empty group. Update by replacing empty chain.
Charles Chan188ebf52015-12-23 00:15:11 -08001103 Deque<GroupKey> newBucketChain = allGroupKeys.get(0);
1104 newBucketChain.addFirst(l3ecmpGroupKey);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001105 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1106 allActiveKeys.clear();
1107 }
1108 allActiveKeys.add(newBucketChain);
1109 updatePendingNextObjective(l3ecmpGroupKey,
1110 new OfdpaNextGroup(allActiveKeys, nextObjective));
Charles Chan188ebf52015-12-23 00:15:11 -08001111 log.debug("Adding to L3ECMP: device:{} gid:{} gkey:{} nextId:{}",
1112 deviceId, Integer.toHexString(l3ecmpGroupId),
1113 l3ecmpGroupKey, nextObjective.id());
1114 // send the innermost group
1115 log.debug("Sending innermost group {} in group chain on device {} ",
Charles Chan5b9df8d2016-03-28 22:21:40 -07001116 Integer.toHexString(gi.innerMostGroupDesc.givenGroupId()), deviceId);
1117 updatePendingGroups(gi.nextGroupDesc.appCookie(), l3ecmpGce);
1118 groupService.addGroup(gi.innerMostGroupDesc);
Saurav Das1a129a02016-11-18 15:21:57 -08001119 }
Charles Chan188ebf52015-12-23 00:15:11 -08001120
Saurav Das1a129a02016-11-18 15:21:57 -08001121 private void addBucketToBroadcastGroup(NextObjective nextObj,
1122 List<Deque<GroupKey>> allActiveKeys,
1123 PortNumber newport) {
1124 VlanId assignedVlan = Ofdpa2Pipeline.readVlanFromSelector(nextObj.meta());
1125 if (assignedVlan == null) {
1126 log.warn("VLAN ID required by broadcast next obj is missing. "
1127 + "Aborting add bucket to broadcast group for next:{} in dev:{}",
1128 nextObj.id(), deviceId);
1129 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
1130 return;
1131 }
1132
1133 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
1134
1135 IpPrefix ipDst = Ofdpa2Pipeline.readIpDstFromSelector(nextObj.meta());
1136 if (ipDst != null) {
1137 if (ipDst.isMulticast()) {
1138 addBucketToL3MulticastGroup(nextObj, allActiveKeys,
1139 groupInfos, assignedVlan, newport);
1140 } else {
1141 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
1142 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
1143 return;
1144 }
1145 } else {
1146 addBucketToL2FloodGroup(nextObj, allActiveKeys,
1147 groupInfos, assignedVlan, newport);
1148 }
1149 }
1150
1151 private void addBucketToL2FloodGroup(NextObjective nextObj,
1152 List<Deque<GroupKey>> allActiveKeys,
1153 List<GroupInfo> groupInfos,
1154 VlanId assignedVlan,
1155 PortNumber newport) {
1156 // create the bucket to add to the outermost L2 Flood group
1157 GroupInfo groupInfo = groupInfos.get(0); // only one bucket to add
1158 GroupDescription l2intGrpDesc = groupInfo.nextGroupDesc;
1159 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
1160 ttb.group(new DefaultGroupId(l2intGrpDesc.givenGroupId()));
1161 GroupBucket abucket = DefaultGroupBucket.createAllGroupBucket(ttb.build());
1162 // get the group being edited
1163 Group l2floodGroup = retrieveTopLevelGroup(allActiveKeys, nextObj.id());
1164 if (l2floodGroup == null) {
1165 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.GROUPMISSING);
1166 return;
1167 }
1168 GroupKey l2floodGroupKey = l2floodGroup.appCookie();
1169 int l2floodGroupId = l2floodGroup.id().id();
1170
1171 //ensure assignedVlan applies to the chosen group
1172 VlanId expectedVlan = VlanId.vlanId((short) ((l2floodGroupId & 0x0fff0000) >> 16));
1173 if (!expectedVlan.equals(assignedVlan)) {
1174 log.warn("VLAN ID {} does not match Flood group {} to which bucket is "
1175 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1176 Integer.toHexString(l2floodGroupId), nextObj.id(), deviceId);
1177 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
1178 }
1179 GroupDescription l2floodGroupDescription =
1180 new DefaultGroupDescription(
1181 deviceId,
1182 GroupDescription.Type.ALL,
1183 new GroupBuckets(Collections.singletonList(abucket)),
1184 l2floodGroupKey,
1185 l2floodGroupId,
1186 nextObj.appId());
1187 GroupChainElem l2floodGce = new GroupChainElem(l2floodGroupDescription, 1, true);
1188
1189 // update original NextGroup with new bucket-chain
1190 // If active keys shows only the top-level group without a chain of groups,
1191 // then it represents an empty group. Update by replacing empty chain.
1192 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
1193 newBucketChain.addFirst(groupInfo.nextGroupDesc.appCookie());
1194 newBucketChain.addFirst(l2floodGroupKey);
1195 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1196 allActiveKeys.clear();
1197 }
1198 allActiveKeys.add(newBucketChain);
1199 updatePendingNextObjective(l2floodGroupKey,
1200 new OfdpaNextGroup(allActiveKeys, nextObj));
1201 log.debug("Adding to L2FLOOD: device:{} gid:{} gkey:{} nextId:{}",
1202 deviceId, Integer.toHexString(l2floodGroupId),
1203 l2floodGroupKey, nextObj.id());
1204 // send the innermost group
1205 log.debug("Sending innermost group {} in group chain on device {} ",
1206 Integer.toHexString(groupInfo.innerMostGroupDesc.givenGroupId()),
1207 deviceId);
1208 updatePendingGroups(groupInfo.nextGroupDesc.appCookie(), l2floodGce);
1209 groupService.addGroup(groupInfo.innerMostGroupDesc);
1210 }
1211
1212 private void addBucketToL3MulticastGroup(NextObjective nextObj,
1213 List<Deque<GroupKey>> allActiveKeys,
1214 List<GroupInfo> groupInfos,
1215 VlanId assignedVlan,
1216 PortNumber newport) {
1217 // create the bucket to add to the outermost L3 Multicast group
1218 GroupInfo groupInfo = groupInfos.get(0); // only one bucket to add
1219 // Points to L3 interface group if there is one.
1220 // Otherwise points to L2 interface group directly.
1221 GroupDescription nextGroupDesc = (groupInfo.nextGroupDesc != null) ?
1222 groupInfo.nextGroupDesc : groupInfo.innerMostGroupDesc;
1223 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
1224 ttb.group(new DefaultGroupId(nextGroupDesc.givenGroupId()));
1225 GroupBucket abucket = DefaultGroupBucket.createAllGroupBucket(ttb.build());
1226
1227 // get the group being edited
1228 Group l3mcastGroup = retrieveTopLevelGroup(allActiveKeys, nextObj.id());
1229 if (l3mcastGroup == null) {
1230 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.GROUPMISSING);
1231 return;
1232 }
1233 GroupKey l3mcastGroupKey = l3mcastGroup.appCookie();
1234 int l3mcastGroupId = l3mcastGroup.id().id();
1235
1236 //ensure assignedVlan applies to the chosen group
1237 VlanId expectedVlan = VlanId.vlanId((short) ((l3mcastGroupId & 0x0fff0000) >> 16));
1238 if (!expectedVlan.equals(assignedVlan)) {
1239 log.warn("VLAN ID {} does not match L3 Mcast group {} to which bucket is "
1240 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1241 Integer.toHexString(l3mcastGroupId), nextObj.id(), deviceId);
1242 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
1243 }
1244 GroupDescription l3mcastGroupDescription =
1245 new DefaultGroupDescription(
1246 deviceId,
1247 GroupDescription.Type.ALL,
1248 new GroupBuckets(Collections.singletonList(abucket)),
1249 l3mcastGroupKey,
1250 l3mcastGroupId,
1251 nextObj.appId());
1252 GroupChainElem l3mcastGce = new GroupChainElem(l3mcastGroupDescription,
1253 1, true);
1254
1255 // update original NextGroup with new bucket-chain
1256 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
1257 newBucketChain.addFirst(groupInfo.innerMostGroupDesc.appCookie());
1258 // Add L3 interface group to the chain if there is one.
1259 if (!groupInfo.nextGroupDesc.equals(groupInfo.innerMostGroupDesc)) {
1260 newBucketChain.addFirst(groupInfo.nextGroupDesc.appCookie());
1261 }
1262 newBucketChain.addFirst(l3mcastGroupKey);
1263 // If active keys shows only the top-level group without a chain of groups,
1264 // then it represents an empty group. Update by replacing empty chain.
1265 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1266 allActiveKeys.clear();
1267 }
1268 allActiveKeys.add(newBucketChain);
1269 updatePendingNextObjective(l3mcastGroupKey,
1270 new OfdpaNextGroup(allActiveKeys, nextObj));
1271
1272 updatePendingGroups(groupInfo.nextGroupDesc.appCookie(), l3mcastGce);
1273 // Point next group to inner-most group, if any
1274 if (!groupInfo.nextGroupDesc.equals(groupInfo.innerMostGroupDesc)) {
1275 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc,
1276 1, false);
1277 updatePendingGroups(groupInfo.innerMostGroupDesc.appCookie(), innerGce);
1278 }
1279 log.debug("Adding to L3MCAST: device:{} gid:{} gkey:{} nextId:{}",
1280 deviceId, Integer.toHexString(l3mcastGroupId),
1281 l3mcastGroupKey, nextObj.id());
1282 // send the innermost group
1283 log.debug("Sending innermost group {} in group chain on device {} ",
1284 Integer.toHexString(groupInfo.innerMostGroupDesc.givenGroupId()),
1285 deviceId);
1286 groupService.addGroup(groupInfo.innerMostGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -08001287 }
1288
1289 /**
1290 * Removes the bucket in the top level group of a possible group-chain. Does
Saurav Das1a129a02016-11-18 15:21:57 -08001291 * not remove the groups in the group-chain pointed to by this bucket, as they
Charles Chan188ebf52015-12-23 00:15:11 -08001292 * may be in use (referenced by other groups) elsewhere.
1293 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001294 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -08001295 * @param next the representation of the existing group-chain for this next objective
1296 */
1297 protected void removeBucketFromGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001298 if (nextObjective.type() != NextObjective.Type.HASHED &&
1299 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001300 log.warn("RemoveBuckets not applied to nextType:{} in dev:{} for next:{}",
1301 nextObjective.type(), deviceId, nextObjective.id());
Saurav Das1a129a02016-11-18 15:21:57 -08001302 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001303 return;
1304 }
1305 Collection<TrafficTreatment> treatments = nextObjective.next();
1306 TrafficTreatment treatment = treatments.iterator().next();
1307 // find the bucket to remove by noting the outport, and figuring out the
1308 // top-level group in the group-chain that indirectly references the port
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001309 PortNumber portToRemove = readOutPortFromTreatment(treatment);
1310 if (portToRemove == null) {
1311 log.warn("next objective {} has no outport.. cannot remove bucket"
1312 + "from group in dev: {}", nextObjective.id(), deviceId);
Saurav Das1a129a02016-11-18 15:21:57 -08001313 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -08001314 return;
1315 }
1316
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001317 List<Deque<GroupKey>> allActiveKeys = Ofdpa2Pipeline.appKryo.deserialize(next.data());
Charles Chan188ebf52015-12-23 00:15:11 -08001318 Deque<GroupKey> foundChain = null;
1319 int index = 0;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001320 for (Deque<GroupKey> gkeys : allActiveKeys) {
1321 // last group in group chain should have a single bucket pointing to port
Charles Chan188ebf52015-12-23 00:15:11 -08001322 GroupKey groupWithPort = gkeys.peekLast();
1323 Group group = groupService.getGroup(deviceId, groupWithPort);
1324 if (group == null) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001325 log.warn("Inconsistent group chain found when removing bucket"
1326 + "for next:{} in dev:{}", nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001327 continue;
1328 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001329 PortNumber pout = readOutPortFromTreatment(
1330 group.buckets().buckets().get(0).treatment());
1331 if (pout.equals(portToRemove)) {
1332 foundChain = gkeys;
Charles Chan188ebf52015-12-23 00:15:11 -08001333 break;
1334 }
1335 index++;
1336 }
Saurav Das1a129a02016-11-18 15:21:57 -08001337 if (foundChain == null) {
Charles Chan188ebf52015-12-23 00:15:11 -08001338 log.warn("Could not find appropriate group-chain for removing bucket"
1339 + " for next id {} in dev:{}", nextObjective.id(), deviceId);
Saurav Das1a129a02016-11-18 15:21:57 -08001340 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.BADPARAMS);
1341 return;
Charles Chan188ebf52015-12-23 00:15:11 -08001342 }
Saurav Das1a129a02016-11-18 15:21:57 -08001343
1344 //first groupkey is the one we want to modify
1345 GroupKey modGroupKey = foundChain.peekFirst();
1346 Group modGroup = groupService.getGroup(deviceId, modGroupKey);
1347 //second groupkey is the one we wish to remove the reference to
1348 GroupKey pointedGroupKey = null;
1349 int i = 0;
1350 for (GroupKey gk : foundChain) {
1351 if (i++ == 1) {
1352 pointedGroupKey = gk;
1353 break;
1354 }
1355 }
1356 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
1357 GroupBucket bucket = null;
1358 if (nextObjective.type() == NextObjective.Type.HASHED) {
1359 bucket = DefaultGroupBucket.createSelectGroupBucket(
1360 DefaultTrafficTreatment.builder()
1361 .group(pointedGroup.id())
1362 .build());
1363 } else {
1364 bucket = DefaultGroupBucket.createAllGroupBucket(
1365 DefaultTrafficTreatment.builder()
1366 .group(pointedGroup.id())
1367 .build());
1368 }
1369 GroupBuckets removeBuckets = new GroupBuckets(Collections
1370 .singletonList(bucket));
1371 log.debug("Removing buckets from group id 0x{} pointing to group id 0x{} "
1372 + "for next id {} in device {}", Integer.toHexString(modGroup.id().id()),
1373 Integer.toHexString(pointedGroup.id().id()), nextObjective.id(), deviceId);
1374 groupService.removeBucketsFromGroup(deviceId, modGroupKey,
1375 removeBuckets, modGroupKey,
1376 nextObjective.appId());
1377 // update store
1378 // If the bucket removed was the last bucket in the group, then
1379 // retain an entry for the top level group which still exists.
1380 if (allActiveKeys.size() == 1) {
1381 ArrayDeque<GroupKey> top = new ArrayDeque<>();
1382 top.add(modGroupKey);
1383 allActiveKeys.add(top);
1384 }
1385 allActiveKeys.remove(index);
1386 flowObjectiveStore.putNextGroup(nextObjective.id(),
1387 new OfdpaNextGroup(allActiveKeys,
1388 nextObjective));
Charles Chan188ebf52015-12-23 00:15:11 -08001389 }
1390
1391 /**
1392 * Removes all groups in multiple possible group-chains that represent the next
1393 * objective.
1394 *
1395 * @param nextObjective the next objective to remove
1396 * @param next the NextGroup that represents the existing group-chain for
1397 * this next objective
1398 */
1399 protected void removeGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001400 List<Deque<GroupKey>> allActiveKeys = Ofdpa2Pipeline.appKryo.deserialize(next.data());
Charles Chanfc5c7802016-05-17 13:13:55 -07001401
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001402 List<GroupKey> groupKeys = allActiveKeys.stream()
Charles Chanfc5c7802016-05-17 13:13:55 -07001403 .map(Deque::getFirst).collect(Collectors.toList());
1404 pendingRemoveNextObjectives.put(nextObjective, groupKeys);
1405
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001406 allActiveKeys.forEach(groupChain -> groupChain.forEach(groupKey ->
Charles Chan188ebf52015-12-23 00:15:11 -08001407 groupService.removeGroup(deviceId, groupKey, nextObjective.appId())));
1408 flowObjectiveStore.removeNextGroup(nextObjective.id());
1409 }
1410
Saurav Das8be4e3a2016-03-11 17:19:07 -08001411 //////////////////////////////////////
1412 // Helper Methods and Classes
1413 //////////////////////////////////////
1414
Pier Ventre140a8942016-11-02 07:26:38 -07001415 protected void updatePendingNextObjective(GroupKey key, OfdpaNextGroup value) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001416 List<OfdpaNextGroup> nextList = new CopyOnWriteArrayList<OfdpaNextGroup>();
1417 nextList.add(value);
Charles Chanfc5c7802016-05-17 13:13:55 -07001418 List<OfdpaNextGroup> ret = pendingAddNextObjectives.asMap()
Saurav Das8be4e3a2016-03-11 17:19:07 -08001419 .putIfAbsent(key, nextList);
1420 if (ret != null) {
1421 ret.add(value);
1422 }
1423 }
1424
Charles Chan425854b2016-04-11 15:32:12 -07001425 protected void updatePendingGroups(GroupKey gkey, GroupChainElem gce) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001426 Set<GroupChainElem> gceSet = Collections.newSetFromMap(
1427 new ConcurrentHashMap<GroupChainElem, Boolean>());
1428 gceSet.add(gce);
1429 Set<GroupChainElem> retval = pendingGroups.putIfAbsent(gkey, gceSet);
1430 if (retval != null) {
1431 retval.add(gce);
1432 }
1433 }
1434
Charles Chan188ebf52015-12-23 00:15:11 -08001435 /**
1436 * Processes next element of a group chain. Assumption is that if this
1437 * group points to another group, the latter has already been created
1438 * and this driver has received notification for it. A second assumption is
1439 * that if there is another group waiting for this group then the appropriate
1440 * stores already have the information to act upon the notification for the
1441 * creation of this group.
1442 * <p>
1443 * The processing of the GroupChainElement depends on the number of groups
1444 * this element is waiting on. For all group types other than SIMPLE, a
1445 * GroupChainElement could be waiting on multiple groups.
1446 *
1447 * @param gce the group chain element to be processed next
1448 */
1449 private void processGroupChain(GroupChainElem gce) {
1450 int waitOnGroups = gce.decrementAndGetGroupsWaitedOn();
1451 if (waitOnGroups != 0) {
1452 log.debug("GCE: {} not ready to be processed", gce);
1453 return;
1454 }
1455 log.debug("GCE: {} ready to be processed", gce);
1456 if (gce.addBucketToGroup) {
1457 groupService.addBucketsToGroup(gce.groupDescription.deviceId(),
1458 gce.groupDescription.appCookie(),
1459 gce.groupDescription.buckets(),
1460 gce.groupDescription.appCookie(),
1461 gce.groupDescription.appId());
1462 } else {
1463 groupService.addGroup(gce.groupDescription);
1464 }
1465 }
1466
1467 private class GroupChecker implements Runnable {
1468 @Override
1469 public void run() {
1470 Set<GroupKey> keys = pendingGroups.keySet().stream()
1471 .filter(key -> groupService.getGroup(deviceId, key) != null)
1472 .collect(Collectors.toSet());
Charles Chanfc5c7802016-05-17 13:13:55 -07001473 Set<GroupKey> otherkeys = pendingAddNextObjectives.asMap().keySet().stream()
Charles Chan188ebf52015-12-23 00:15:11 -08001474 .filter(otherkey -> groupService.getGroup(deviceId, otherkey) != null)
1475 .collect(Collectors.toSet());
1476 keys.addAll(otherkeys);
1477
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -07001478 keys.forEach(key ->
Charles Chanfc5c7802016-05-17 13:13:55 -07001479 processPendingAddGroupsOrNextObjs(key, false));
Charles Chan188ebf52015-12-23 00:15:11 -08001480 }
1481 }
1482
Saurav Das8be4e3a2016-03-11 17:19:07 -08001483 private class InnerGroupListener implements GroupListener {
1484 @Override
1485 public void event(GroupEvent event) {
1486 log.trace("received group event of type {}", event.type());
Charles Chanfc5c7802016-05-17 13:13:55 -07001487 switch (event.type()) {
1488 case GROUP_ADDED:
1489 processPendingAddGroupsOrNextObjs(event.subject().appCookie(), true);
1490 break;
1491 case GROUP_REMOVED:
1492 processPendingRemoveNextObjs(event.subject().appCookie());
1493 break;
1494 default:
1495 break;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001496 }
1497 }
1498 }
1499
Charles Chanfc5c7802016-05-17 13:13:55 -07001500 private void processPendingAddGroupsOrNextObjs(GroupKey key, boolean added) {
Charles Chan188ebf52015-12-23 00:15:11 -08001501 //first check for group chain
1502 Set<GroupChainElem> gceSet = pendingGroups.remove(key);
1503 if (gceSet != null) {
1504 for (GroupChainElem gce : gceSet) {
Charles Chan216e3c82016-04-23 14:48:16 -07001505 log.debug("Group service {} group key {} in device {}. "
Saurav Das0fd79d92016-03-07 10:58:36 -08001506 + "Processing next group in group chain with group id 0x{}",
Charles Chan188ebf52015-12-23 00:15:11 -08001507 (added) ? "ADDED" : "processed",
1508 key, deviceId,
1509 Integer.toHexString(gce.groupDescription.givenGroupId()));
1510 processGroupChain(gce);
1511 }
1512 } else {
1513 // otherwise chain complete - check for waiting nextObjectives
Charles Chanfc5c7802016-05-17 13:13:55 -07001514 List<OfdpaNextGroup> nextGrpList = pendingAddNextObjectives.getIfPresent(key);
Charles Chan188ebf52015-12-23 00:15:11 -08001515 if (nextGrpList != null) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001516 pendingAddNextObjectives.invalidate(key);
Charles Chan188ebf52015-12-23 00:15:11 -08001517 nextGrpList.forEach(nextGrp -> {
Charles Chan216e3c82016-04-23 14:48:16 -07001518 log.debug("Group service {} group key {} in device:{}. "
Saurav Das0fd79d92016-03-07 10:58:36 -08001519 + "Done implementing next objective: {} <<-->> gid:0x{}",
Charles Chan188ebf52015-12-23 00:15:11 -08001520 (added) ? "ADDED" : "processed",
1521 key, deviceId, nextGrp.nextObjective().id(),
1522 Integer.toHexString(groupService.getGroup(deviceId, key)
1523 .givenGroupId()));
Charles Chan361154b2016-03-24 10:23:39 -07001524 Ofdpa2Pipeline.pass(nextGrp.nextObjective());
Charles Chan188ebf52015-12-23 00:15:11 -08001525 flowObjectiveStore.putNextGroup(nextGrp.nextObjective().id(), nextGrp);
1526 // check if addBuckets waiting for this completion
1527 NextObjective pendBkt = pendingBuckets
1528 .remove(nextGrp.nextObjective().id());
1529 if (pendBkt != null) {
1530 addBucketToGroup(pendBkt, nextGrp);
1531 }
1532 });
1533 }
1534 }
1535 }
1536
Charles Chanfc5c7802016-05-17 13:13:55 -07001537 private void processPendingRemoveNextObjs(GroupKey key) {
1538 pendingRemoveNextObjectives.asMap().forEach((nextObjective, groupKeys) -> {
1539 if (groupKeys.isEmpty()) {
1540 pendingRemoveNextObjectives.invalidate(nextObjective);
1541 Ofdpa2Pipeline.pass(nextObjective);
1542 } else {
1543 groupKeys.remove(key);
1544 }
1545 });
1546 }
1547
Charles Chan425854b2016-04-11 15:32:12 -07001548 protected int getNextAvailableIndex() {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001549 return (int) nextIndex.incrementAndGet();
1550 }
1551
Charles Chane849c192016-01-11 18:28:54 -08001552 /**
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001553 * Returns the outport in a traffic treatment.
1554 *
1555 * @param tt the treatment
1556 * @return the PortNumber for the outport or null
1557 */
1558 protected static PortNumber readOutPortFromTreatment(TrafficTreatment tt) {
1559 for (Instruction ins : tt.allInstructions()) {
1560 if (ins.type() == Instruction.Type.OUTPUT) {
1561 return ((Instructions.OutputInstruction) ins).port();
1562 }
1563 }
1564 return null;
1565 }
1566
1567 /**
Charles Chane849c192016-01-11 18:28:54 -08001568 * Returns a hash as the L2 Interface Group Key.
1569 *
1570 * Keep the lower 6-bit for port since port number usually smaller than 64.
1571 * Hash other information into remaining 28 bits.
1572 *
1573 * @param deviceId Device ID
1574 * @param vlanId VLAN ID
1575 * @param portNumber Port number
1576 * @return L2 interface group key
1577 */
Pier Ventre140a8942016-11-02 07:26:38 -07001578 protected int l2InterfaceGroupKey(DeviceId deviceId, VlanId vlanId, long portNumber) {
Charles Chane849c192016-01-11 18:28:54 -08001579 int portLowerBits = (int) portNumber & PORT_LOWER_BITS_MASK;
1580 long portHigherBits = portNumber & PORT_HIGHER_BITS_MASK;
Charles Chand0fd5dc2016-02-16 23:14:49 -08001581 int hash = Objects.hash(deviceId, vlanId, portHigherBits);
Charles Chane849c192016-01-11 18:28:54 -08001582 return L2_INTERFACE_TYPE | (TYPE_MASK & hash << 6) | portLowerBits;
1583 }
1584
Saurav Das1a129a02016-11-18 15:21:57 -08001585 private Group retrieveTopLevelGroup(List<Deque<GroupKey>> allActiveKeys,
1586 int nextid) {
1587 GroupKey topLevelGroupKey = null;
1588 if (!allActiveKeys.isEmpty()) {
1589 topLevelGroupKey = allActiveKeys.get(0).peekFirst();
1590 } else {
1591 log.warn("Could not determine top level group while processing"
1592 + "next:{} in dev:{}", nextid, deviceId);
1593 return null;
1594 }
1595 Group topGroup = groupService.getGroup(deviceId, topLevelGroupKey);
1596 if (topGroup == null) {
1597 log.warn("Could not find top level group while processing "
1598 + "next:{} in dev:{}", nextid, deviceId);
1599 }
1600 return topGroup;
1601 }
1602
Charles Chan188ebf52015-12-23 00:15:11 -08001603 /**
1604 * Utility class for moving group information around.
Saurav Das1a129a02016-11-18 15:21:57 -08001605 *
1606 * Example: Suppose we are trying to create a group-chain A-B-C-D, where
1607 * A is the top level group, and D is the inner-most group, typically L2 Interface.
1608 * The innerMostGroupDesc is always D. At various stages of the creation
1609 * process the nextGroupDesc may be C or B. The nextGroupDesc exists to
1610 * inform the referencing group about which group it needs to point to,
1611 * and wait for. In some cases the group chain may simply be A-B. In this case,
1612 * both innerMostGroupDesc and nextGroupDesc will be B.
Charles Chan188ebf52015-12-23 00:15:11 -08001613 */
Charles Chan425854b2016-04-11 15:32:12 -07001614 protected class GroupInfo {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001615 /**
1616 * Description of the inner-most group of the group chain.
1617 * It is always an L2 interface group.
1618 */
1619 private GroupDescription innerMostGroupDesc;
Charles Chan188ebf52015-12-23 00:15:11 -08001620
Charles Chan5b9df8d2016-03-28 22:21:40 -07001621 /**
1622 * Description of the next group in the group chain.
1623 * It can be L2 interface, L3 interface, L3 unicast, L3 VPN group.
Saurav Das1a129a02016-11-18 15:21:57 -08001624 * It is possible that nextGroupDesc is the same as the innerMostGroup.
Charles Chan5b9df8d2016-03-28 22:21:40 -07001625 */
1626 private GroupDescription nextGroupDesc;
1627
1628 GroupInfo(GroupDescription innerMostGroupDesc, GroupDescription nextGroupDesc) {
1629 this.innerMostGroupDesc = innerMostGroupDesc;
1630 this.nextGroupDesc = nextGroupDesc;
Charles Chan188ebf52015-12-23 00:15:11 -08001631 }
Pier Ventre140a8942016-11-02 07:26:38 -07001632
1633 /**
1634 * Getter for innerMostGroupDesc.
1635 *
1636 * @return the inner most group description
1637 */
1638 public GroupDescription getInnerMostGroupDesc() {
1639 return innerMostGroupDesc;
1640 }
1641
1642 /**
1643 * Getter for the next group description.
1644 *
1645 * @return the next group description
1646 */
1647 public GroupDescription getNextGroupDesc() {
1648 return nextGroupDesc;
1649 }
Charles Chan188ebf52015-12-23 00:15:11 -08001650 }
1651
1652 /**
1653 * Represents an entire group-chain that implements a Next-Objective from
1654 * the application. The objective is represented as a list of deques, where
1655 * each deque is a separate chain of groups.
1656 * <p>
1657 * For example, an ECMP group with 3 buckets, where each bucket points to
1658 * a group chain of L3 Unicast and L2 interface groups will look like this:
1659 * <ul>
1660 * <li>List[0] is a Deque of GroupKeyECMP(first)-GroupKeyL3(middle)-GroupKeyL2(last)
1661 * <li>List[1] is a Deque of GroupKeyECMP(first)-GroupKeyL3(middle)-GroupKeyL2(last)
1662 * <li>List[2] is a Deque of GroupKeyECMP(first)-GroupKeyL3(middle)-GroupKeyL2(last)
1663 * </ul>
1664 * where the first element of each deque is the same, representing the
1665 * top level ECMP group, while every other element represents a unique groupKey.
1666 * <p>
1667 * Also includes information about the next objective that
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001668 * resulted in these group-chains.
Charles Chan188ebf52015-12-23 00:15:11 -08001669 *
1670 */
1671 protected class OfdpaNextGroup implements NextGroup {
1672 private final NextObjective nextObj;
1673 private final List<Deque<GroupKey>> gkeys;
1674
1675 public OfdpaNextGroup(List<Deque<GroupKey>> gkeys, NextObjective nextObj) {
Charles Chan188ebf52015-12-23 00:15:11 -08001676 this.nextObj = nextObj;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001677 this.gkeys = gkeys;
Charles Chan188ebf52015-12-23 00:15:11 -08001678 }
1679
1680 public NextObjective nextObjective() {
1681 return nextObj;
1682 }
1683
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001684 public List<Deque<GroupKey>> groupKeys() {
1685 return gkeys;
1686 }
1687
Charles Chan188ebf52015-12-23 00:15:11 -08001688 @Override
1689 public byte[] data() {
Charles Chan361154b2016-03-24 10:23:39 -07001690 return Ofdpa2Pipeline.appKryo.serialize(gkeys);
Charles Chan188ebf52015-12-23 00:15:11 -08001691 }
1692 }
1693
1694 /**
1695 * Represents a group element that is part of a chain of groups.
1696 * Stores enough information to create a Group Description to add the group
1697 * to the switch by requesting the Group Service. Objects instantiating this
1698 * class are meant to be temporary and live as long as it is needed to wait for
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001699 * referenced groups in the group chain to be created.
Charles Chan188ebf52015-12-23 00:15:11 -08001700 */
Charles Chan425854b2016-04-11 15:32:12 -07001701 protected class GroupChainElem {
Charles Chan188ebf52015-12-23 00:15:11 -08001702 private GroupDescription groupDescription;
1703 private AtomicInteger waitOnGroups;
1704 private boolean addBucketToGroup;
1705
1706 GroupChainElem(GroupDescription groupDescription, int waitOnGroups,
1707 boolean addBucketToGroup) {
1708 this.groupDescription = groupDescription;
1709 this.waitOnGroups = new AtomicInteger(waitOnGroups);
1710 this.addBucketToGroup = addBucketToGroup;
1711 }
1712
1713 /**
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001714 * This method atomically decrements the counter for the number of
Charles Chan188ebf52015-12-23 00:15:11 -08001715 * groups this GroupChainElement is waiting on, for notifications from
1716 * the Group Service. When this method returns a value of 0, this
1717 * GroupChainElement is ready to be processed.
1718 *
1719 * @return integer indication of the number of notifications being waited on
1720 */
1721 int decrementAndGetGroupsWaitedOn() {
1722 return waitOnGroups.decrementAndGet();
1723 }
1724
1725 @Override
1726 public String toString() {
1727 return (Integer.toHexString(groupDescription.givenGroupId()) +
1728 " groupKey: " + groupDescription.appCookie() +
1729 " waiting-on-groups: " + waitOnGroups.get() +
1730 " addBucketToGroup: " + addBucketToGroup +
1731 " device: " + deviceId);
1732 }
1733 }
Pier Ventre140a8942016-11-02 07:26:38 -07001734
1735 /**
1736 * Helper enum to handle the different MPLS group
1737 * types.
1738 */
1739 protected enum OfdpaMplsGroupSubType {
1740
1741 MPLS_INTF((short) 0),
1742
1743 L2_VPN((short) 1),
1744
1745 L3_VPN((short) 2),
1746
1747 MPLS_TUNNEL_LABEL_1((short) 3),
1748
1749 MPLS_TUNNEL_LABEL_2((short) 4),
1750
1751 MPLS_SWAP_LABEL((short) 5),
1752
1753 MPLS_ECMP((short) 8);
1754
1755 private short value;
1756
1757 public static final int OFDPA_GROUP_TYPE_SHIFT = 28;
1758 public static final int OFDPA_MPLS_SUBTYPE_SHIFT = 24;
1759
1760 OfdpaMplsGroupSubType(short value) {
1761 this.value = value;
1762 }
1763
1764 /**
1765 * Gets the value as an short.
1766 *
1767 * @return the value as an short
1768 */
1769 public short getValue() {
1770 return this.value;
1771 }
1772
1773 }
1774
1775 /**
1776 * Creates MPLS Label group id given a sub type and
1777 * the index.
1778 *
1779 * @param subType the MPLS Label group sub type
1780 * @param index the index of the group
1781 * @return the OFDPA group id
1782 */
1783 public Integer makeMplsLabelGroupId(OfdpaMplsGroupSubType subType, int index) {
1784 index = index & 0x00FFFFFF;
1785 return index | (9 << OFDPA_GROUP_TYPE_SHIFT) | (subType.value << OFDPA_MPLS_SUBTYPE_SHIFT);
1786 }
1787
1788 /**
1789 * Creates MPLS Forwarding group id given a sub type and
1790 * the index.
1791 *
1792 * @param subType the MPLS forwarding group sub type
1793 * @param index the index of the group
1794 * @return the OFDPA group id
1795 */
1796 public Integer makeMplsForwardingGroupId(OfdpaMplsGroupSubType subType, int index) {
1797 index = index & 0x00FFFFFF;
1798 return index | (10 << OFDPA_GROUP_TYPE_SHIFT) | (subType.value << OFDPA_MPLS_SUBTYPE_SHIFT);
1799 }
Charles Chan188ebf52015-12-23 00:15:11 -08001800}