blob: 0535ca38e327b17a9e0d67ec1a44f75dacc8695e [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;
Yi Tseng78f51f42017-02-02 13:54:58 -080031import org.onosproject.core.GroupId;
Charles Chan32562522016-04-07 14:37:14 -070032import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Charles Chan188ebf52015-12-23 00:15:11 -080033import org.onosproject.net.DeviceId;
34import org.onosproject.net.PortNumber;
35import org.onosproject.net.behaviour.NextGroup;
36import org.onosproject.net.behaviour.PipelinerContext;
37import org.onosproject.net.flow.DefaultTrafficTreatment;
38import org.onosproject.net.flow.TrafficSelector;
39import org.onosproject.net.flow.TrafficTreatment;
40import org.onosproject.net.flow.criteria.Criterion;
Pier Ventre42287df2016-11-09 14:17:26 -080041import org.onosproject.net.flow.criteria.TunnelIdCriterion;
Charles Chan188ebf52015-12-23 00:15:11 -080042import org.onosproject.net.flow.criteria.VlanIdCriterion;
43import org.onosproject.net.flow.instructions.Instruction;
44import org.onosproject.net.flow.instructions.Instructions;
45import org.onosproject.net.flow.instructions.L2ModificationInstruction;
46import org.onosproject.net.flowobjective.FlowObjectiveStore;
47import org.onosproject.net.flowobjective.NextObjective;
48import org.onosproject.net.flowobjective.ObjectiveError;
49import org.onosproject.net.group.DefaultGroupBucket;
50import org.onosproject.net.group.DefaultGroupDescription;
51import org.onosproject.net.group.DefaultGroupKey;
52import org.onosproject.net.group.Group;
53import org.onosproject.net.group.GroupBucket;
54import org.onosproject.net.group.GroupBuckets;
55import org.onosproject.net.group.GroupDescription;
56import org.onosproject.net.group.GroupEvent;
57import org.onosproject.net.group.GroupKey;
58import org.onosproject.net.group.GroupListener;
59import org.onosproject.net.group.GroupService;
Saurav Das8be4e3a2016-03-11 17:19:07 -080060import org.onosproject.store.service.AtomicCounter;
61import org.onosproject.store.service.StorageService;
Charles Chan188ebf52015-12-23 00:15:11 -080062import org.slf4j.Logger;
63
64import java.util.ArrayDeque;
65import java.util.ArrayList;
66import java.util.Collection;
67import java.util.Collections;
68import java.util.Deque;
69import 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;
Yi Tseng78f51f42017-02-02 13:54:58 -080087import static org.onosproject.net.group.GroupDescription.Type.ALL;
88import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Charles Chan188ebf52015-12-23 00:15:11 -080089import static org.slf4j.LoggerFactory.getLogger;
90
91/**
Charles Chan40132b32017-01-22 00:19:37 -080092 * Group handler that emulates Broadcom OF-DPA TTP on CpqD.
Charles Chan188ebf52015-12-23 00:15:11 -080093 */
Charles Chan361154b2016-03-24 10:23:39 -070094public class Ofdpa2GroupHandler {
Charles Chan188ebf52015-12-23 00:15:11 -080095 /*
96 * OFDPA requires group-id's to have a certain form.
97 * L2 Interface Groups have <4bits-0><12bits-vlanid><16bits-portid>
98 * L3 Unicast Groups have <4bits-2><28bits-index>
99 * MPLS Interface Groups have <4bits-9><4bits:0><24bits-index>
100 * L3 ECMP Groups have <4bits-7><28bits-index>
101 * L2 Flood Groups have <4bits-4><12bits-vlanid><16bits-index>
102 * L3 VPN Groups have <4bits-9><4bits-2><24bits-index>
103 */
Charles Chan425854b2016-04-11 15:32:12 -0700104 protected static final int L2_INTERFACE_TYPE = 0x00000000;
105 protected static final int L3_INTERFACE_TYPE = 0x50000000;
106 protected static final int L3_UNICAST_TYPE = 0x20000000;
107 protected static final int L3_MULTICAST_TYPE = 0x60000000;
108 protected static final int MPLS_INTERFACE_TYPE = 0x90000000;
109 protected static final int MPLS_L3VPN_SUBTYPE = 0x92000000;
110 protected static final int L3_ECMP_TYPE = 0x70000000;
111 protected static final int L2_FLOOD_TYPE = 0x40000000;
Charles Chane849c192016-01-11 18:28:54 -0800112
Charles Chan425854b2016-04-11 15:32:12 -0700113 protected static final int TYPE_MASK = 0x0fffffff;
114 protected static final int SUBTYPE_MASK = 0x00ffffff;
115 protected static final int TYPE_VLAN_MASK = 0x0000ffff;
Charles Chane849c192016-01-11 18:28:54 -0800116
Charles Chan372b63e2017-02-07 12:10:53 -0800117 protected static final int THREE_BIT_MASK = 0x0fff;
118 protected static final int FOUR_BIT_MASK = 0xffff;
119 protected static final int PORT_LEN = 16;
120
Charles Chan425854b2016-04-11 15:32:12 -0700121 protected static final int PORT_LOWER_BITS_MASK = 0x3f;
122 protected static final long PORT_HIGHER_BITS_MASK = ~PORT_LOWER_BITS_MASK;
Charles Chan188ebf52015-12-23 00:15:11 -0800123
Yi Tseng78f51f42017-02-02 13:54:58 -0800124 protected static final String HEX_PREFIX = "0x";
125
Charles Chan188ebf52015-12-23 00:15:11 -0800126 private final Logger log = getLogger(getClass());
127 private ServiceDirectory serviceDirectory;
128 protected GroupService groupService;
Saurav Das8be4e3a2016-03-11 17:19:07 -0800129 protected StorageService storageService;
Charles Chan188ebf52015-12-23 00:15:11 -0800130
Charles Chan425854b2016-04-11 15:32:12 -0700131 protected DeviceId deviceId;
Charles Chan188ebf52015-12-23 00:15:11 -0800132 private FlowObjectiveStore flowObjectiveStore;
Charles Chanfc5c7802016-05-17 13:13:55 -0700133 private Cache<GroupKey, List<OfdpaNextGroup>> pendingAddNextObjectives;
134 private Cache<NextObjective, List<GroupKey>> pendingRemoveNextObjectives;
Charles Chan188ebf52015-12-23 00:15:11 -0800135 private ConcurrentHashMap<GroupKey, Set<GroupChainElem>> pendingGroups;
Yi Tseng78f51f42017-02-02 13:54:58 -0800136 private ConcurrentHashMap<GroupKey, Set<NextObjective>> pendingUpdateNextObjectives;
Charles Chan188ebf52015-12-23 00:15:11 -0800137 private ScheduledExecutorService groupChecker =
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700138 Executors.newScheduledThreadPool(2, groupedThreads("onos/pipeliner", "ofdpa2-%d", log));
Charles Chan188ebf52015-12-23 00:15:11 -0800139
140 // index number for group creation
Saurav Das8be4e3a2016-03-11 17:19:07 -0800141 private AtomicCounter nextIndex;
Charles Chan188ebf52015-12-23 00:15:11 -0800142
Charles Chan188ebf52015-12-23 00:15:11 -0800143 // local store for pending bucketAdds - by design there can only be one
144 // pending bucket for a group
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700145 protected ConcurrentHashMap<Integer, NextObjective> pendingBuckets =
146 new ConcurrentHashMap<>();
Charles Chan188ebf52015-12-23 00:15:11 -0800147
Charles Chan40132b32017-01-22 00:19:37 -0800148 /**
149 * Determines whether this pipeline support copy ttl instructions or not.
150 *
151 * @return true if copy ttl instructions are supported
152 */
153 protected boolean supportCopyTtl() {
154 return true;
155 }
156
157 /**
158 * Determines whether this pipeline support set mpls bos instruction or not.
159 *
160 * @return true if set mpls bos instruction is supported
161 */
162 protected boolean supportSetMplsBos() {
163 return true;
164 }
165
Charles Chan0f43e472017-02-14 14:00:16 -0800166 /**
167 * Determines whether this pipeline requires popping VLAN before pushing MPLS.
168 * <p>
169 * If required, pop vlan before push mpls and add an arbitrary vlan back afterward.
170 * MPLS interface group will substitute the arbitrary VLAN with expected VLAN later on.
171 *
172 * @return true if this pipeline requires popping VLAN before pushing MPLS
173 */
174 protected boolean requireVlanPopBeforeMplsPush() {
175 return false;
176 }
177
Charles Chan188ebf52015-12-23 00:15:11 -0800178 protected void init(DeviceId deviceId, PipelinerContext context) {
179 this.deviceId = deviceId;
180 this.flowObjectiveStore = context.store();
181 this.serviceDirectory = context.directory();
182 this.groupService = serviceDirectory.get(GroupService.class);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800183 this.storageService = serviceDirectory.get(StorageService.class);
Madan Jampanid5714e02016-04-19 14:15:20 -0700184 this.nextIndex = storageService.getAtomicCounter("group-id-index-counter");
Charles Chan188ebf52015-12-23 00:15:11 -0800185
Charles Chanfc5c7802016-05-17 13:13:55 -0700186 pendingAddNextObjectives = CacheBuilder.newBuilder()
Charles Chan188ebf52015-12-23 00:15:11 -0800187 .expireAfterWrite(20, TimeUnit.SECONDS)
188 .removalListener((
189 RemovalNotification<GroupKey, List<OfdpaNextGroup>> notification) -> {
190 if (notification.getCause() == RemovalCause.EXPIRED) {
191 notification.getValue().forEach(ofdpaNextGrp ->
Charles Chan361154b2016-03-24 10:23:39 -0700192 Ofdpa2Pipeline.fail(ofdpaNextGrp.nextObj,
Charles Chan188ebf52015-12-23 00:15:11 -0800193 ObjectiveError.GROUPINSTALLATIONFAILED));
Charles Chanfc5c7802016-05-17 13:13:55 -0700194 }
195 }).build();
Charles Chan188ebf52015-12-23 00:15:11 -0800196
Charles Chanfc5c7802016-05-17 13:13:55 -0700197 pendingRemoveNextObjectives = CacheBuilder.newBuilder()
198 .expireAfterWrite(20, TimeUnit.SECONDS)
199 .removalListener((
200 RemovalNotification<NextObjective, List<GroupKey>> notification) -> {
201 if (notification.getCause() == RemovalCause.EXPIRED) {
202 Ofdpa2Pipeline.fail(notification.getKey(),
203 ObjectiveError.GROUPREMOVALFAILED);
Charles Chan188ebf52015-12-23 00:15:11 -0800204 }
205 }).build();
206 pendingGroups = new ConcurrentHashMap<>();
Yi Tseng78f51f42017-02-02 13:54:58 -0800207 pendingUpdateNextObjectives = new ConcurrentHashMap<>();
Charles Chan188ebf52015-12-23 00:15:11 -0800208 groupChecker.scheduleAtFixedRate(new GroupChecker(), 0, 500, TimeUnit.MILLISECONDS);
209
210 groupService.addListener(new InnerGroupListener());
211 }
212
Pier Ventre140a8942016-11-02 07:26:38 -0700213 /**
214 * The purpose of this function is to verify if the hashed next
215 * objective is supported by the current pipeline.
216 *
217 * @param nextObjective the hashed objective to verify
218 * @return true if the hashed objective is supported. Otherwise false.
219 */
220 public boolean verifyHashedNextObjective(NextObjective nextObjective) {
221 // if it is not hashed, there is something wrong;
222 if (nextObjective.type() != HASHED) {
223 return false;
224 }
225 // The case non supported is the MPLS-ECMP. For now, we try
226 // to create a MPLS-ECMP for the transport of a VPWS. The
227 // necessary info are contained in the meta selector. In particular
228 // we are looking for the case of BoS==False;
229 TrafficSelector metaSelector = nextObjective.meta();
230 if (metaSelector != null && isNotMplsBos(metaSelector)) {
231 return false;
232 }
233
234 return true;
235 }
236
Saurav Das8be4e3a2016-03-11 17:19:07 -0800237 //////////////////////////////////////
238 // Group Creation
239 //////////////////////////////////////
240
Charles Chan188ebf52015-12-23 00:15:11 -0800241 protected void addGroup(NextObjective nextObjective) {
242 switch (nextObjective.type()) {
243 case SIMPLE:
244 Collection<TrafficTreatment> treatments = nextObjective.next();
245 if (treatments.size() != 1) {
246 log.error("Next Objectives of type Simple should only have a "
247 + "single Traffic Treatment. Next Objective Id:{}",
248 nextObjective.id());
Charles Chan361154b2016-03-24 10:23:39 -0700249 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800250 return;
251 }
252 processSimpleNextObjective(nextObjective);
253 break;
254 case BROADCAST:
255 processBroadcastNextObjective(nextObjective);
256 break;
257 case HASHED:
Pier Ventre140a8942016-11-02 07:26:38 -0700258 if (!verifyHashedNextObjective(nextObjective)) {
259 log.error("Next Objectives of type hashed not supported. Next Objective Id:{}",
260 nextObjective.id());
261 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.BADPARAMS);
262 return;
263 }
Charles Chan188ebf52015-12-23 00:15:11 -0800264 processHashedNextObjective(nextObjective);
265 break;
266 case FAILOVER:
Charles Chan361154b2016-03-24 10:23:39 -0700267 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -0800268 log.warn("Unsupported next objective type {}", nextObjective.type());
269 break;
270 default:
Charles Chan361154b2016-03-24 10:23:39 -0700271 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.UNKNOWN);
Charles Chan188ebf52015-12-23 00:15:11 -0800272 log.warn("Unknown next objective type {}", nextObjective.type());
273 }
274 }
275
276 /**
277 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
278 * a chain of groups. The simple Next Objective passed
279 * in by the application has to be broken up into a group chain
280 * comprising of an L3 Unicast Group that points to an L2 Interface
281 * Group which in-turn points to an output port. In some cases, the simple
282 * next Objective can just be an L2 interface without the need for chaining.
283 *
284 * @param nextObj the nextObjective of type SIMPLE
285 */
286 private void processSimpleNextObjective(NextObjective nextObj) {
287 TrafficTreatment treatment = nextObj.next().iterator().next();
288 // determine if plain L2 or L3->L2
289 boolean plainL2 = true;
290 for (Instruction ins : treatment.allInstructions()) {
291 if (ins.type() == Instruction.Type.L2MODIFICATION) {
292 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
293 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_DST ||
294 l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_SRC) {
295 plainL2 = false;
296 break;
297 }
298 }
299 }
300
301 if (plainL2) {
302 createL2InterfaceGroup(nextObj);
303 return;
304 }
305
Pier Ventre140a8942016-11-02 07:26:38 -0700306 boolean isMpls = false;
Pier Ventre42287df2016-11-09 14:17:26 -0800307 // In order to understand if it is a pseudo wire related
308 // next objective we look for the tunnel id in the meta.
309 boolean isPw = false;
Pier Ventre140a8942016-11-02 07:26:38 -0700310 if (nextObj.meta() != null) {
311 isMpls = isNotMplsBos(nextObj.meta());
Pier Ventre42287df2016-11-09 14:17:26 -0800312
313 TunnelIdCriterion tunnelIdCriterion = (TunnelIdCriterion) nextObj
314 .meta()
315 .getCriterion(TUNNEL_ID);
316 if (tunnelIdCriterion != null) {
317 isPw = true;
318 }
319
Pier Ventre140a8942016-11-02 07:26:38 -0700320 }
321
Pier Ventre42287df2016-11-09 14:17:26 -0800322 if (!isPw) {
323 // break up simple next objective to GroupChain objects
324 GroupInfo groupInfo = createL2L3Chain(treatment, nextObj.id(),
325 nextObj.appId(), isMpls,
326 nextObj.meta());
327 if (groupInfo == null) {
328 log.error("Could not process nextObj={} in dev:{}", nextObj.id(), deviceId);
329 return;
330 }
331 // create object for local and distributed storage
332 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
333 gkeyChain.addFirst(groupInfo.innerMostGroupDesc.appCookie());
334 gkeyChain.addFirst(groupInfo.nextGroupDesc.appCookie());
335 OfdpaNextGroup ofdpaGrp =
336 new OfdpaNextGroup(Collections.singletonList(gkeyChain), nextObj);
337
338 // store l3groupkey with the ofdpaNextGroup for the nextObjective that depends on it
339 updatePendingNextObjective(groupInfo.nextGroupDesc.appCookie(), ofdpaGrp);
340
341 // now we are ready to send the l2 groupDescription (inner), as all the stores
342 // that will get async replies have been updated. By waiting to update
343 // the stores, we prevent nasty race conditions.
344 groupService.addGroup(groupInfo.innerMostGroupDesc);
345 } else {
346 // We handle the pseudo wire with a different a procedure.
347 // This procedure is meant to handle both initiation and
348 // termination of the pseudo wire.
349 processPwNextObjective(nextObj);
Charles Chan188ebf52015-12-23 00:15:11 -0800350 }
Charles Chan188ebf52015-12-23 00:15:11 -0800351 }
352
Charles Chan188ebf52015-12-23 00:15:11 -0800353 /**
354 * Creates a simple L2 Interface Group.
355 *
356 * @param nextObj the next Objective
357 */
358 private void createL2InterfaceGroup(NextObjective nextObj) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700359 VlanId assignedVlan = Ofdpa2Pipeline.readVlanFromSelector(nextObj.meta());
360 if (assignedVlan == null) {
361 log.warn("VLAN ID required by simple next obj is missing. Abort.");
362 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800363 return;
364 }
365
Charles Chan5b9df8d2016-03-28 22:21:40 -0700366 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Charles Chan188ebf52015-12-23 00:15:11 -0800367
Charles Chan5b9df8d2016-03-28 22:21:40 -0700368 // There is only one L2 interface group in this case
369 GroupDescription l2InterfaceGroupDesc = groupInfos.get(0).innerMostGroupDesc;
Charles Chan188ebf52015-12-23 00:15:11 -0800370
Charles Chan5b9df8d2016-03-28 22:21:40 -0700371 // Put all dependency information into allGroupKeys
372 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
373 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
374 gkeyChain.addFirst(l2InterfaceGroupDesc.appCookie());
375 allGroupKeys.add(gkeyChain);
Charles Chan188ebf52015-12-23 00:15:11 -0800376
Charles Chan5b9df8d2016-03-28 22:21:40 -0700377 // Point the next objective to this group
378 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
379 updatePendingNextObjective(l2InterfaceGroupDesc.appCookie(), ofdpaGrp);
380
381 // Start installing the inner-most group
382 groupService.addGroup(l2InterfaceGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800383 }
384
385 /**
386 * Creates one of two possible group-chains from the treatment
387 * passed in. Depending on the MPLS boolean, this method either creates
Charles Chan425854b2016-04-11 15:32:12 -0700388 * an L3Unicast Group --&gt; L2Interface Group, if mpls is false;
389 * or MPLSInterface Group --&gt; L2Interface Group, if mpls is true;
Charles Chan188ebf52015-12-23 00:15:11 -0800390 * The returned 'inner' group description is always the L2 Interface group.
391 *
392 * @param treatment that needs to be broken up to create the group chain
393 * @param nextId of the next objective that needs this group chain
394 * @param appId of the application that sent this next objective
395 * @param mpls determines if L3Unicast or MPLSInterface group is created
396 * @param meta metadata passed in by the application as part of the nextObjective
397 * @return GroupInfo containing the GroupDescription of the
398 * L2Interface group(inner) and the GroupDescription of the (outer)
399 * L3Unicast/MPLSInterface group. May return null if there is an
400 * error in processing the chain
401 */
Charles Chan425854b2016-04-11 15:32:12 -0700402 protected GroupInfo createL2L3Chain(TrafficTreatment treatment, int nextId,
Charles Chanf9e98652016-09-07 16:54:23 -0700403 ApplicationId appId, boolean mpls,
404 TrafficSelector meta) {
405 return createL2L3ChainInternal(treatment, nextId, appId, mpls, meta, true);
406 }
407
408 /**
409 * Internal implementation of createL2L3Chain.
410 * <p>
411 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
412 * Since it is non-OF spec, we need an extension treatment for that.
413 * The useSetVlanExtension must be set to false for OFDPA i12.
414 * </p>
415 *
416 * @param treatment that needs to be broken up to create the group chain
417 * @param nextId of the next objective that needs this group chain
418 * @param appId of the application that sent this next objective
419 * @param mpls determines if L3Unicast or MPLSInterface group is created
420 * @param meta metadata passed in by the application as part of the nextObjective
421 * @param useSetVlanExtension use the setVlanVid extension that has is_present bit set to 0.
422 * @return GroupInfo containing the GroupDescription of the
423 * L2Interface group(inner) and the GroupDescription of the (outer)
424 * L3Unicast/MPLSInterface group. May return null if there is an
425 * error in processing the chain
426 */
427 protected GroupInfo createL2L3ChainInternal(TrafficTreatment treatment, int nextId,
Pier Ventre42287df2016-11-09 14:17:26 -0800428 ApplicationId appId, boolean mpls,
429 TrafficSelector meta, boolean useSetVlanExtension) {
Charles Chan188ebf52015-12-23 00:15:11 -0800430 // for the l2interface group, get vlan and port info
431 // for the outer group, get the src/dst mac, and vlan info
432 TrafficTreatment.Builder outerTtb = DefaultTrafficTreatment.builder();
433 TrafficTreatment.Builder innerTtb = DefaultTrafficTreatment.builder();
434 VlanId vlanid = null;
435 long portNum = 0;
436 boolean setVlan = false, popVlan = false;
Yi Tseng78f51f42017-02-02 13:54:58 -0800437 MacAddress srcMac;
438 MacAddress dstMac;
Charles Chan188ebf52015-12-23 00:15:11 -0800439 for (Instruction ins : treatment.allInstructions()) {
440 if (ins.type() == Instruction.Type.L2MODIFICATION) {
441 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
442 switch (l2ins.subtype()) {
443 case ETH_DST:
Charles Chan5270ed02016-01-30 23:22:37 -0800444 dstMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
445 outerTtb.setEthDst(dstMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800446 break;
447 case ETH_SRC:
Charles Chand0fd5dc2016-02-16 23:14:49 -0800448 srcMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
449 outerTtb.setEthSrc(srcMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800450 break;
451 case VLAN_ID:
452 vlanid = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
Charles Chanf9e98652016-09-07 16:54:23 -0700453 if (useSetVlanExtension) {
454 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
455 outerTtb.extension(ofdpaSetVlanVid, deviceId);
456 } else {
457 outerTtb.setVlanId(vlanid);
458 }
Charles Chan188ebf52015-12-23 00:15:11 -0800459 setVlan = true;
460 break;
461 case VLAN_POP:
462 innerTtb.popVlan();
463 popVlan = true;
464 break;
465 case DEC_MPLS_TTL:
466 case MPLS_LABEL:
467 case MPLS_POP:
468 case MPLS_PUSH:
469 case VLAN_PCP:
470 case VLAN_PUSH:
471 default:
472 break;
473 }
474 } else if (ins.type() == Instruction.Type.OUTPUT) {
475 portNum = ((Instructions.OutputInstruction) ins).port().toLong();
476 innerTtb.add(ins);
477 } else {
478 log.warn("Driver does not handle this type of TrafficTreatment"
479 + " instruction in nextObjectives: {}", ins.type());
480 }
481 }
482
483 if (vlanid == null && meta != null) {
484 // use metadata if available
Pier Ventre140a8942016-11-02 07:26:38 -0700485 Criterion vidCriterion = meta.getCriterion(VLAN_VID);
Charles Chan188ebf52015-12-23 00:15:11 -0800486 if (vidCriterion != null) {
487 vlanid = ((VlanIdCriterion) vidCriterion).vlanId();
488 }
489 // if vlan is not set, use the vlan in metadata for outerTtb
490 if (vlanid != null && !setVlan) {
Charles Chanf9e98652016-09-07 16:54:23 -0700491 if (useSetVlanExtension) {
492 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
493 outerTtb.extension(ofdpaSetVlanVid, deviceId);
494 } else {
495 outerTtb.setVlanId(vlanid);
496 }
Charles Chan188ebf52015-12-23 00:15:11 -0800497 }
498 }
499
500 if (vlanid == null) {
501 log.error("Driver cannot process an L2/L3 group chain without "
502 + "egress vlan information for dev: {} port:{}",
503 deviceId, portNum);
504 return null;
505 }
506
507 if (!setVlan && !popVlan) {
508 // untagged outgoing port
509 TrafficTreatment.Builder temp = DefaultTrafficTreatment.builder();
510 temp.popVlan();
511 innerTtb.build().allInstructions().forEach(i -> temp.add(i));
512 innerTtb = temp;
513 }
514
515 // assemble information for ofdpa l2interface group
Charles Chane849c192016-01-11 18:28:54 -0800516 int l2groupId = L2_INTERFACE_TYPE | (vlanid.toShort() << 16) | (int) portNum;
Saurav Das8be4e3a2016-03-11 17:19:07 -0800517 // a globally unique groupkey that is different for ports in the same device,
Charles Chan188ebf52015-12-23 00:15:11 -0800518 // but different for the same portnumber on different devices. Also different
519 // for the various group-types created out of the same next objective.
Charles Chane849c192016-01-11 18:28:54 -0800520 int l2gk = l2InterfaceGroupKey(deviceId, vlanid, portNum);
Charles Chan361154b2016-03-24 10:23:39 -0700521 final GroupKey l2groupkey = new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(l2gk));
Charles Chan188ebf52015-12-23 00:15:11 -0800522
523 // assemble information for outer group
524 GroupDescription outerGrpDesc = null;
525 if (mpls) {
526 // outer group is MPLSInteface
Saurav Das8be4e3a2016-03-11 17:19:07 -0800527 int mplsInterfaceIndex = getNextAvailableIndex();
528 int mplsgroupId = MPLS_INTERFACE_TYPE | (SUBTYPE_MASK & mplsInterfaceIndex);
529 final GroupKey mplsgroupkey = new DefaultGroupKey(
Charles Chan361154b2016-03-24 10:23:39 -0700530 Ofdpa2Pipeline.appKryo.serialize(mplsInterfaceIndex));
Yi Tsengfa394de2017-02-01 11:26:40 -0800531 outerTtb.group(new GroupId(l2groupId));
Charles Chan188ebf52015-12-23 00:15:11 -0800532 // create the mpls-interface group description to wait for the
533 // l2 interface group to be processed
534 GroupBucket mplsinterfaceGroupBucket =
535 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
536 outerGrpDesc = new DefaultGroupDescription(
537 deviceId,
538 GroupDescription.Type.INDIRECT,
539 new GroupBuckets(Collections.singletonList(
540 mplsinterfaceGroupBucket)),
541 mplsgroupkey,
542 mplsgroupId,
543 appId);
544 log.debug("Trying MPLS-Interface: device:{} gid:{} gkey:{} nextid:{}",
545 deviceId, Integer.toHexString(mplsgroupId),
546 mplsgroupkey, nextId);
547 } else {
548 // outer group is L3Unicast
Saurav Das8be4e3a2016-03-11 17:19:07 -0800549 int l3unicastIndex = getNextAvailableIndex();
550 int l3groupId = L3_UNICAST_TYPE | (TYPE_MASK & l3unicastIndex);
551 final GroupKey l3groupkey = new DefaultGroupKey(
Charles Chan361154b2016-03-24 10:23:39 -0700552 Ofdpa2Pipeline.appKryo.serialize(l3unicastIndex));
Yi Tsengfa394de2017-02-01 11:26:40 -0800553 outerTtb.group(new GroupId(l2groupId));
Charles Chan188ebf52015-12-23 00:15:11 -0800554 // create the l3unicast group description to wait for the
555 // l2 interface group to be processed
556 GroupBucket l3unicastGroupBucket =
557 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
558 outerGrpDesc = new DefaultGroupDescription(
559 deviceId,
560 GroupDescription.Type.INDIRECT,
561 new GroupBuckets(Collections.singletonList(
562 l3unicastGroupBucket)),
563 l3groupkey,
564 l3groupId,
565 appId);
566 log.debug("Trying L3Unicast: device:{} gid:{} gkey:{} nextid:{}",
567 deviceId, Integer.toHexString(l3groupId),
568 l3groupkey, nextId);
569 }
570
571 // store l2groupkey with the groupChainElem for the outer-group that depends on it
572 GroupChainElem gce = new GroupChainElem(outerGrpDesc, 1, false);
573 updatePendingGroups(l2groupkey, gce);
574
575 // create group description for the inner l2interfacegroup
Charles Chan5b9df8d2016-03-28 22:21:40 -0700576 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800577 DefaultGroupBucket.createIndirectGroupBucket(innerTtb.build());
578 GroupDescription l2groupDescription =
579 new DefaultGroupDescription(
580 deviceId,
581 GroupDescription.Type.INDIRECT,
582 new GroupBuckets(Collections.singletonList(
Charles Chan5b9df8d2016-03-28 22:21:40 -0700583 l2InterfaceGroupBucket)),
Charles Chan188ebf52015-12-23 00:15:11 -0800584 l2groupkey,
585 l2groupId,
586 appId);
587 log.debug("Trying L2Interface: device:{} gid:{} gkey:{} nextId:{}",
588 deviceId, Integer.toHexString(l2groupId),
589 l2groupkey, nextId);
590 return new GroupInfo(l2groupDescription, outerGrpDesc);
591
592 }
593
594 /**
595 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
596 * a chain of groups. The broadcast Next Objective passed in by the application
597 * has to be broken up into a group chain comprising of an
Saurav Das1a129a02016-11-18 15:21:57 -0800598 * L2 Flood group or L3 Multicast group, whose buckets point to L2 Interface groups.
Charles Chan188ebf52015-12-23 00:15:11 -0800599 *
600 * @param nextObj the nextObjective of type BROADCAST
601 */
602 private void processBroadcastNextObjective(NextObjective nextObj) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700603 VlanId assignedVlan = Ofdpa2Pipeline.readVlanFromSelector(nextObj.meta());
604 if (assignedVlan == null) {
605 log.warn("VLAN ID required by broadcast next obj is missing. Abort.");
606 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800607 return;
608 }
Charles Chan188ebf52015-12-23 00:15:11 -0800609
Charles Chan5b9df8d2016-03-28 22:21:40 -0700610 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
611
612 IpPrefix ipDst = Ofdpa2Pipeline.readIpDstFromSelector(nextObj.meta());
613 if (ipDst != null) {
614 if (ipDst.isMulticast()) {
615 createL3MulticastGroup(nextObj, assignedVlan, groupInfos);
616 } else {
617 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
618 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
619 return;
620 }
621 } else {
622 createL2FloodGroup(nextObj, assignedVlan, groupInfos);
623 }
624 }
625
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700626 private List<GroupInfo> prepareL2InterfaceGroup(NextObjective nextObj,
627 VlanId assignedVlan) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700628 ImmutableList.Builder<GroupInfo> groupInfoBuilder = ImmutableList.builder();
629
630 // break up broadcast next objective to multiple groups
631 Collection<TrafficTreatment> buckets = nextObj.next();
632
Charles Chan188ebf52015-12-23 00:15:11 -0800633 // each treatment is converted to an L2 interface group
Charles Chan188ebf52015-12-23 00:15:11 -0800634 for (TrafficTreatment treatment : buckets) {
635 TrafficTreatment.Builder newTreatment = DefaultTrafficTreatment.builder();
636 PortNumber portNum = null;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700637 VlanId egressVlan = null;
Charles Chan188ebf52015-12-23 00:15:11 -0800638 // ensure that the only allowed treatments are pop-vlan and output
639 for (Instruction ins : treatment.allInstructions()) {
640 if (ins.type() == Instruction.Type.L2MODIFICATION) {
641 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
642 switch (l2ins.subtype()) {
643 case VLAN_POP:
644 newTreatment.add(l2ins);
645 break;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700646 case VLAN_ID:
647 egressVlan = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
648 break;
Charles Chan188ebf52015-12-23 00:15:11 -0800649 default:
650 log.debug("action {} not permitted for broadcast nextObj",
651 l2ins.subtype());
652 break;
653 }
654 } else if (ins.type() == Instruction.Type.OUTPUT) {
655 portNum = ((Instructions.OutputInstruction) ins).port();
656 newTreatment.add(ins);
657 } else {
Charles Chane849c192016-01-11 18:28:54 -0800658 log.debug("TrafficTreatment of type {} not permitted in " +
659 " broadcast nextObjective", ins.type());
Charles Chan188ebf52015-12-23 00:15:11 -0800660 }
661 }
662
Charles Chan188ebf52015-12-23 00:15:11 -0800663 // assemble info for l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700664 VlanId l2InterfaceGroupVlan =
665 (egressVlan != null && !assignedVlan.equals(egressVlan)) ?
666 egressVlan : assignedVlan;
667 int l2gk = l2InterfaceGroupKey(deviceId, l2InterfaceGroupVlan, portNum.toLong());
668 final GroupKey l2InterfaceGroupKey =
669 new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(l2gk));
Charles Chan372b63e2017-02-07 12:10:53 -0800670 int l2InterfaceGroupId = L2_INTERFACE_TYPE |
671 ((l2InterfaceGroupVlan.toShort() & THREE_BIT_MASK) << PORT_LEN) |
672 ((int) portNum.toLong() & FOUR_BIT_MASK);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700673 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800674 DefaultGroupBucket.createIndirectGroupBucket(newTreatment.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700675 GroupDescription l2InterfaceGroupDescription =
Charles Chan188ebf52015-12-23 00:15:11 -0800676 new DefaultGroupDescription(
677 deviceId,
678 GroupDescription.Type.INDIRECT,
679 new GroupBuckets(Collections.singletonList(
Charles Chan5b9df8d2016-03-28 22:21:40 -0700680 l2InterfaceGroupBucket)),
681 l2InterfaceGroupKey,
682 l2InterfaceGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800683 nextObj.appId());
684 log.debug("Trying L2-Interface: device:{} gid:{} gkey:{} nextid:{}",
Charles Chan5b9df8d2016-03-28 22:21:40 -0700685 deviceId, Integer.toHexString(l2InterfaceGroupId),
686 l2InterfaceGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -0800687
Charles Chan5b9df8d2016-03-28 22:21:40 -0700688 groupInfoBuilder.add(new GroupInfo(l2InterfaceGroupDescription,
689 l2InterfaceGroupDescription));
Charles Chan188ebf52015-12-23 00:15:11 -0800690 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700691 return groupInfoBuilder.build();
692 }
Charles Chan188ebf52015-12-23 00:15:11 -0800693
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700694 private void createL2FloodGroup(NextObjective nextObj, VlanId vlanId,
695 List<GroupInfo> groupInfos) {
696 // assemble info for l2 flood group. Since there can be only one flood
697 // group for a vlan, its index is always the same - 0
Saurav Das0fd79d92016-03-07 10:58:36 -0800698 Integer l2floodgroupId = L2_FLOOD_TYPE | (vlanId.toShort() << 16);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800699 int l2floodgk = getNextAvailableIndex();
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700700 final GroupKey l2floodgroupkey =
701 new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(l2floodgk));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700702
Charles Chan188ebf52015-12-23 00:15:11 -0800703 // collection of group buckets pointing to all the l2 interface groups
Yi Tseng78f51f42017-02-02 13:54:58 -0800704 List<GroupBucket> l2floodBuckets =
705 generateNextGroupBuckets(groupInfos, ALL);
Charles Chan188ebf52015-12-23 00:15:11 -0800706 // create the l2flood group-description to wait for all the
707 // l2interface groups to be processed
708 GroupDescription l2floodGroupDescription =
709 new DefaultGroupDescription(
710 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800711 ALL,
Charles Chan188ebf52015-12-23 00:15:11 -0800712 new GroupBuckets(l2floodBuckets),
713 l2floodgroupkey,
714 l2floodgroupId,
715 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800716 log.debug("Trying L2-Flood: device:{} gid:{} gkey:{} nextid:{}",
717 deviceId, Integer.toHexString(l2floodgroupId),
718 l2floodgroupkey, nextObj.id());
719
Charles Chan5b9df8d2016-03-28 22:21:40 -0700720 // Put all dependency information into allGroupKeys
721 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
722 groupInfos.forEach(groupInfo -> {
723 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
724 // In this case we should have L2 interface group only
725 gkeyChain.addFirst(groupInfo.nextGroupDesc.appCookie());
726 gkeyChain.addFirst(l2floodgroupkey);
727 allGroupKeys.add(gkeyChain);
728 });
Charles Chan188ebf52015-12-23 00:15:11 -0800729
Charles Chan5b9df8d2016-03-28 22:21:40 -0700730 // Point the next objective to this group
731 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Charles Chan188ebf52015-12-23 00:15:11 -0800732 updatePendingNextObjective(l2floodgroupkey, ofdpaGrp);
733
Charles Chan5b9df8d2016-03-28 22:21:40 -0700734 GroupChainElem gce = new GroupChainElem(l2floodGroupDescription,
735 groupInfos.size(), false);
736 groupInfos.forEach(groupInfo -> {
737 // Point this group to the next group
738 updatePendingGroups(groupInfo.nextGroupDesc.appCookie(), gce);
739 // Start installing the inner-most group
740 groupService.addGroup(groupInfo.innerMostGroupDesc);
741 });
742 }
743
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700744 private void createL3MulticastGroup(NextObjective nextObj, VlanId vlanId,
745 List<GroupInfo> groupInfos) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700746 List<GroupBucket> l3McastBuckets = new ArrayList<>();
747 groupInfos.forEach(groupInfo -> {
748 // Points to L3 interface group if there is one.
749 // Otherwise points to L2 interface group directly.
750 GroupDescription nextGroupDesc = (groupInfo.nextGroupDesc != null) ?
751 groupInfo.nextGroupDesc : groupInfo.innerMostGroupDesc;
752 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Yi Tsengfa394de2017-02-01 11:26:40 -0800753 ttb.group(new GroupId(nextGroupDesc.givenGroupId()));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700754 GroupBucket abucket = DefaultGroupBucket.createAllGroupBucket(ttb.build());
755 l3McastBuckets.add(abucket);
756 });
757
758 int l3MulticastIndex = getNextAvailableIndex();
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700759 int l3MulticastGroupId = L3_MULTICAST_TYPE |
760 vlanId.toShort() << 16 | (TYPE_VLAN_MASK & l3MulticastIndex);
761 final GroupKey l3MulticastGroupKey =
762 new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(l3MulticastIndex));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700763
764 GroupDescription l3MulticastGroupDesc = new DefaultGroupDescription(deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800765 ALL,
Charles Chan5b9df8d2016-03-28 22:21:40 -0700766 new GroupBuckets(l3McastBuckets),
767 l3MulticastGroupKey,
768 l3MulticastGroupId,
769 nextObj.appId());
770
771 // Put all dependency information into allGroupKeys
772 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
773 groupInfos.forEach(groupInfo -> {
774 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
775 gkeyChain.addFirst(groupInfo.innerMostGroupDesc.appCookie());
776 // Add L3 interface group to the chain if there is one.
777 if (!groupInfo.nextGroupDesc.equals(groupInfo.innerMostGroupDesc)) {
778 gkeyChain.addFirst(groupInfo.nextGroupDesc.appCookie());
779 }
780 gkeyChain.addFirst(l3MulticastGroupKey);
781 allGroupKeys.add(gkeyChain);
782 });
783
784 // Point the next objective to this group
785 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
786 updatePendingNextObjective(l3MulticastGroupKey, ofdpaGrp);
787
788 GroupChainElem outerGce = new GroupChainElem(l3MulticastGroupDesc,
789 groupInfos.size(), false);
790 groupInfos.forEach(groupInfo -> {
791 // Point this group (L3 multicast) to the next group
792 updatePendingGroups(groupInfo.nextGroupDesc.appCookie(), outerGce);
793
794 // Point next group to inner-most group, if any
795 if (!groupInfo.nextGroupDesc.equals(groupInfo.innerMostGroupDesc)) {
796 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc,
797 1, false);
798 updatePendingGroups(groupInfo.innerMostGroupDesc.appCookie(), innerGce);
799 }
800
801 // Start installing the inner-most group
802 groupService.addGroup(groupInfo.innerMostGroupDesc);
803 });
Charles Chan188ebf52015-12-23 00:15:11 -0800804 }
805
Charles Chan188ebf52015-12-23 00:15:11 -0800806 /**
807 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
808 * a chain of groups. The hashed Next Objective passed in by the application
809 * has to be broken up into a group chain comprising of an
810 * L3 ECMP group as the top level group. Buckets of this group can point
811 * to a variety of groups in a group chain, depending on the whether
812 * MPLS labels are being pushed or not.
813 * <p>
814 * NOTE: We do not create MPLS ECMP groups as they are unimplemented in
815 * OF-DPA 2.0 (even though it is in the spec). Therefore we do not
816 * check the nextObjective meta to see what is matching before being
817 * sent to this nextObjective.
818 *
819 * @param nextObj the nextObjective of type HASHED
820 */
Pier Ventre140a8942016-11-02 07:26:38 -0700821 protected void processHashedNextObjective(NextObjective nextObj) {
Charles Chan188ebf52015-12-23 00:15:11 -0800822 // storage for all group keys in the chain of groups created
823 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
824 List<GroupInfo> unsentGroups = new ArrayList<>();
825 createHashBucketChains(nextObj, allGroupKeys, unsentGroups);
826
827 // now we can create the outermost L3 ECMP group
828 List<GroupBucket> l3ecmpGroupBuckets = new ArrayList<>();
829 for (GroupInfo gi : unsentGroups) {
830 // create ECMP bucket to point to the outer group
831 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Yi Tsengfa394de2017-02-01 11:26:40 -0800832 ttb.group(new GroupId(gi.nextGroupDesc.givenGroupId()));
Charles Chan188ebf52015-12-23 00:15:11 -0800833 GroupBucket sbucket = DefaultGroupBucket
834 .createSelectGroupBucket(ttb.build());
835 l3ecmpGroupBuckets.add(sbucket);
836 }
Saurav Das8be4e3a2016-03-11 17:19:07 -0800837 int l3ecmpIndex = getNextAvailableIndex();
838 int l3ecmpGroupId = L3_ECMP_TYPE | (TYPE_MASK & l3ecmpIndex);
839 GroupKey l3ecmpGroupKey = new DefaultGroupKey(
Charles Chan361154b2016-03-24 10:23:39 -0700840 Ofdpa2Pipeline.appKryo.serialize(l3ecmpIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800841 GroupDescription l3ecmpGroupDesc =
842 new DefaultGroupDescription(
843 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800844 SELECT,
Charles Chan188ebf52015-12-23 00:15:11 -0800845 new GroupBuckets(l3ecmpGroupBuckets),
846 l3ecmpGroupKey,
847 l3ecmpGroupId,
848 nextObj.appId());
849 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
850 l3ecmpGroupBuckets.size(),
851 false);
852
853 // create objects for local and distributed storage
854 allGroupKeys.forEach(gkeyChain -> gkeyChain.addFirst(l3ecmpGroupKey));
855 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
856
857 // store l3ecmpGroupKey with the ofdpaGroupChain for the nextObjective
858 // that depends on it
859 updatePendingNextObjective(l3ecmpGroupKey, ofdpaGrp);
860
861 log.debug("Trying L3ECMP: device:{} gid:{} gkey:{} nextId:{}",
862 deviceId, Integer.toHexString(l3ecmpGroupId),
863 l3ecmpGroupKey, nextObj.id());
864 // finally we are ready to send the innermost groups
865 for (GroupInfo gi : unsentGroups) {
866 log.debug("Sending innermost group {} in group chain on device {} ",
Charles Chan5b9df8d2016-03-28 22:21:40 -0700867 Integer.toHexString(gi.innerMostGroupDesc.givenGroupId()), deviceId);
868 updatePendingGroups(gi.nextGroupDesc.appCookie(), l3ecmpGce);
869 groupService.addGroup(gi.innerMostGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800870 }
871
872 }
873
874 /**
875 * Creates group chains for all buckets in a hashed group, and stores the
876 * GroupInfos and GroupKeys for all the groups in the lists passed in, which
877 * should be empty.
878 * <p>
879 * Does not create the top level ECMP group. Does not actually send the
880 * groups to the groupService.
881 *
882 * @param nextObj the Next Objective with buckets that need to be converted
883 * to group chains
884 * @param allGroupKeys a list to store groupKey for each bucket-group-chain
885 * @param unsentGroups a list to store GroupInfo for each bucket-group-chain
886 */
Pier Ventre140a8942016-11-02 07:26:38 -0700887 protected void createHashBucketChains(NextObjective nextObj,
Saurav Das8be4e3a2016-03-11 17:19:07 -0800888 List<Deque<GroupKey>> allGroupKeys,
889 List<GroupInfo> unsentGroups) {
Charles Chan188ebf52015-12-23 00:15:11 -0800890 // break up hashed next objective to multiple groups
891 Collection<TrafficTreatment> buckets = nextObj.next();
892
893 for (TrafficTreatment bucket : buckets) {
894 //figure out how many labels are pushed in each bucket
895 int labelsPushed = 0;
896 MplsLabel innermostLabel = null;
897 for (Instruction ins : bucket.allInstructions()) {
898 if (ins.type() == Instruction.Type.L2MODIFICATION) {
899 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
900 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_PUSH) {
901 labelsPushed++;
902 }
903 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) {
904 if (innermostLabel == null) {
Ray Milkey125572b2016-02-22 16:48:17 -0800905 innermostLabel = ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label();
Charles Chan188ebf52015-12-23 00:15:11 -0800906 }
907 }
908 }
909 }
910
911 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
912 // XXX we only deal with 0 and 1 label push right now
913 if (labelsPushed == 0) {
Pier Ventre140a8942016-11-02 07:26:38 -0700914 GroupInfo nolabelGroupInfo;
915 TrafficSelector metaSelector = nextObj.meta();
916 if (metaSelector != null) {
917 if (isNotMplsBos(metaSelector)) {
918 nolabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
919 nextObj.appId(), true,
920 nextObj.meta());
921 } else {
922 nolabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
923 nextObj.appId(), false,
924 nextObj.meta());
925 }
926 } else {
927 nolabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
928 nextObj.appId(), false,
929 nextObj.meta());
930 }
Charles Chan188ebf52015-12-23 00:15:11 -0800931 if (nolabelGroupInfo == null) {
932 log.error("Could not process nextObj={} in dev:{}",
933 nextObj.id(), deviceId);
934 return;
935 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700936 gkeyChain.addFirst(nolabelGroupInfo.innerMostGroupDesc.appCookie());
937 gkeyChain.addFirst(nolabelGroupInfo.nextGroupDesc.appCookie());
Charles Chan188ebf52015-12-23 00:15:11 -0800938
939 // we can't send the inner group description yet, as we have to
940 // create the dependent ECMP group first. So we store..
941 unsentGroups.add(nolabelGroupInfo);
942
943 } else if (labelsPushed == 1) {
944 GroupInfo onelabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
945 nextObj.appId(), true,
946 nextObj.meta());
947 if (onelabelGroupInfo == null) {
948 log.error("Could not process nextObj={} in dev:{}",
949 nextObj.id(), deviceId);
950 return;
951 }
952 // we need to add another group to this chain - the L3VPN group
953 TrafficTreatment.Builder l3vpnTtb = DefaultTrafficTreatment.builder();
Charles Chan0f43e472017-02-14 14:00:16 -0800954 if (requireVlanPopBeforeMplsPush()) {
955 l3vpnTtb.popVlan();
956 }
Charles Chan188ebf52015-12-23 00:15:11 -0800957 l3vpnTtb.pushMpls()
958 .setMpls(innermostLabel)
Yi Tsengfa394de2017-02-01 11:26:40 -0800959 .group(new GroupId(onelabelGroupInfo.nextGroupDesc.givenGroupId()));
Charles Chan40132b32017-01-22 00:19:37 -0800960 if (supportCopyTtl()) {
961 l3vpnTtb.copyTtlOut();
962 }
963 if (supportSetMplsBos()) {
964 l3vpnTtb.setMplsBos(true);
965 }
Charles Chan0f43e472017-02-14 14:00:16 -0800966 if (requireVlanPopBeforeMplsPush()) {
967 l3vpnTtb.pushVlan().setVlanId(VlanId.vlanId(VlanId.RESERVED));
968 }
Charles Chan40132b32017-01-22 00:19:37 -0800969
Charles Chan188ebf52015-12-23 00:15:11 -0800970 GroupBucket l3vpnGrpBkt =
971 DefaultGroupBucket.createIndirectGroupBucket(l3vpnTtb.build());
Saurav Das8be4e3a2016-03-11 17:19:07 -0800972 int l3vpnIndex = getNextAvailableIndex();
973 int l3vpngroupId = MPLS_L3VPN_SUBTYPE | (SUBTYPE_MASK & l3vpnIndex);
974 GroupKey l3vpngroupkey = new DefaultGroupKey(
Charles Chan361154b2016-03-24 10:23:39 -0700975 Ofdpa2Pipeline.appKryo.serialize(l3vpnIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800976 GroupDescription l3vpnGroupDesc =
977 new DefaultGroupDescription(
978 deviceId,
979 GroupDescription.Type.INDIRECT,
980 new GroupBuckets(Collections.singletonList(
981 l3vpnGrpBkt)),
982 l3vpngroupkey,
983 l3vpngroupId,
984 nextObj.appId());
985 GroupChainElem l3vpnGce = new GroupChainElem(l3vpnGroupDesc, 1, false);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700986 updatePendingGroups(onelabelGroupInfo.nextGroupDesc.appCookie(), l3vpnGce);
Charles Chan188ebf52015-12-23 00:15:11 -0800987
Charles Chan5b9df8d2016-03-28 22:21:40 -0700988 gkeyChain.addFirst(onelabelGroupInfo.innerMostGroupDesc.appCookie());
989 gkeyChain.addFirst(onelabelGroupInfo.nextGroupDesc.appCookie());
Charles Chan188ebf52015-12-23 00:15:11 -0800990 gkeyChain.addFirst(l3vpngroupkey);
991
992 //now we can replace the outerGrpDesc with the one we just created
Charles Chan5b9df8d2016-03-28 22:21:40 -0700993 onelabelGroupInfo.nextGroupDesc = l3vpnGroupDesc;
Charles Chan188ebf52015-12-23 00:15:11 -0800994
995 // we can't send the innermost group yet, as we have to create
996 // the dependent ECMP group first. So we store ...
997 unsentGroups.add(onelabelGroupInfo);
998
999 log.debug("Trying L3VPN: device:{} gid:{} gkey:{} nextId:{}",
1000 deviceId, Integer.toHexString(l3vpngroupId),
1001 l3vpngroupkey, nextObj.id());
1002
1003 } else {
1004 log.warn("Driver currently does not handle more than 1 MPLS "
1005 + "labels. Not processing nextObjective {}", nextObj.id());
1006 return;
1007 }
1008
1009 // all groups in this chain
1010 allGroupKeys.add(gkeyChain);
1011 }
1012 }
1013
Pier Ventre42287df2016-11-09 14:17:26 -08001014 /**
1015 * Processes the pseudo wire related next objective.
1016 * This procedure try to reuse the mpls label groups,
1017 * the mpls interface group and the l2 interface group.
1018 *
1019 * @param nextObjective the objective to process.
1020 */
1021 protected void processPwNextObjective(NextObjective nextObjective) {
1022 log.warn("Pseudo wire extensions are not support for the OFDPA 2.0 {}", nextObjective.id());
1023 return;
1024 }
1025
Saurav Das8be4e3a2016-03-11 17:19:07 -08001026 //////////////////////////////////////
1027 // Group Editing
1028 //////////////////////////////////////
1029
Charles Chan188ebf52015-12-23 00:15:11 -08001030 /**
1031 * Adds a bucket to the top level group of a group-chain, and creates the chain.
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001032 * Ensures that bucket being added is not a duplicate, by checking existing
1033 * buckets for the same outport.
Charles Chan188ebf52015-12-23 00:15:11 -08001034 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001035 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -08001036 * @param next the representation of the existing group-chain for this next objective
1037 */
1038 protected void addBucketToGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001039 if (nextObjective.type() != NextObjective.Type.HASHED &&
1040 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001041 log.warn("AddBuckets not applied to nextType:{} in dev:{} for next:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001042 nextObjective.type(), deviceId, nextObjective.id());
Saurav Das1a129a02016-11-18 15:21:57 -08001043 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001044 return;
1045 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001046
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001047 // first check to see if bucket being added is not a duplicate of an
1048 // existing bucket. If it is for an existing outport, then its a duplicate.
Yi Tseng78f51f42017-02-02 13:54:58 -08001049 Set<TrafficTreatment> duplicateBuckets = Sets.newHashSet();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001050 List<Deque<GroupKey>> allActiveKeys = Ofdpa2Pipeline.appKryo.deserialize(next.data());
Yi Tseng78f51f42017-02-02 13:54:58 -08001051 Set<PortNumber> existingPorts = getExistingOutputPorts(allActiveKeys);
1052
1053 nextObjective.next().forEach(trafficTreatment -> {
1054 PortNumber portNumber = readOutPortFromTreatment(trafficTreatment);
1055
1056 if (portNumber == null) {
1057 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001058 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001059
1060 if (existingPorts.contains(portNumber)) {
1061 duplicateBuckets.add(trafficTreatment);
1062 }
1063 });
1064
1065 if (!duplicateBuckets.isEmpty()) {
1066 log.warn("Some buckets {} already exists in next id {}, abort.",
1067 duplicateBuckets, nextObjective.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001068 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001069
Saurav Das1a129a02016-11-18 15:21:57 -08001070 if (nextObjective.type() == NextObjective.Type.HASHED) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001071 addBucketToHashGroup(nextObjective, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001072 } else if (nextObjective.type() == NextObjective.Type.BROADCAST) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001073 addBucketToBroadcastGroup(nextObjective, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001074 }
1075 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001076
Yi Tseng78f51f42017-02-02 13:54:58 -08001077 private Set<PortNumber> getExistingOutputPorts(List<Deque<GroupKey>> allActiveKeys) {
1078 Set<PortNumber> existingPorts = Sets.newHashSet();
1079
1080 allActiveKeys.forEach(keyChain -> {
1081 GroupKey ifaceGroupKey = keyChain.peekLast();
1082 Group ifaceGroup = groupService.getGroup(deviceId, ifaceGroupKey);
1083 if (ifaceGroup != null && !ifaceGroup.buckets().buckets().isEmpty()) {
1084 ifaceGroup.buckets().buckets().forEach(bucket -> {
1085 PortNumber portNumber = readOutPortFromTreatment(bucket.treatment());
1086
1087 if (portNumber != null) {
1088 existingPorts.add(portNumber);
1089 }
1090 });
1091 }
1092 });
1093
1094 return existingPorts;
1095 }
1096
Saurav Das1a129a02016-11-18 15:21:57 -08001097 private void addBucketToHashGroup(NextObjective nextObjective,
Yi Tseng78f51f42017-02-02 13:54:58 -08001098 List<Deque<GroupKey>> allActiveKeys) {
Charles Chan188ebf52015-12-23 00:15:11 -08001099 // storage for all group keys in the chain of groups created
1100 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
1101 List<GroupInfo> unsentGroups = new ArrayList<>();
Yi Tseng78f51f42017-02-02 13:54:58 -08001102 List<GroupBucket> newBuckets = Lists.newArrayList();
Charles Chan188ebf52015-12-23 00:15:11 -08001103 createHashBucketChains(nextObjective, allGroupKeys, unsentGroups);
1104
Yi Tseng78f51f42017-02-02 13:54:58 -08001105 // now we can create the buckets to add to the outermost L3 ECMP group
1106 newBuckets = generateNextGroupBuckets(unsentGroups, SELECT);
Charles Chan188ebf52015-12-23 00:15:11 -08001107
Saurav Das1a129a02016-11-18 15:21:57 -08001108 // retrieve the original L3 ECMP group
1109 Group l3ecmpGroup = retrieveTopLevelGroup(allActiveKeys, nextObjective.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001110 if (l3ecmpGroup == null) {
Saurav Das1a129a02016-11-18 15:21:57 -08001111 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.GROUPMISSING);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001112 return;
1113 }
Saurav Das1a129a02016-11-18 15:21:57 -08001114 GroupKey l3ecmpGroupKey = l3ecmpGroup.appCookie();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001115 int l3ecmpGroupId = l3ecmpGroup.id().id();
Charles Chan188ebf52015-12-23 00:15:11 -08001116
1117 // Although GroupDescriptions are not necessary for adding buckets to
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001118 // existing groups, we still use one in the GroupChainElem. When the latter is
Charles Chan188ebf52015-12-23 00:15:11 -08001119 // processed, the info will be extracted for the bucketAdd call to groupService
1120 GroupDescription l3ecmpGroupDesc =
1121 new DefaultGroupDescription(
1122 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -08001123 SELECT,
1124 new GroupBuckets(newBuckets),
Charles Chan188ebf52015-12-23 00:15:11 -08001125 l3ecmpGroupKey,
1126 l3ecmpGroupId,
1127 nextObjective.appId());
Yi Tseng78f51f42017-02-02 13:54:58 -08001128 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
1129 unsentGroups.size(),
1130 true);
Charles Chan188ebf52015-12-23 00:15:11 -08001131
1132 // update original NextGroup with new bucket-chain
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001133 // If active keys shows only the top-level group without a chain of groups,
1134 // then it represents an empty group. Update by replacing empty chain.
Charles Chan188ebf52015-12-23 00:15:11 -08001135 Deque<GroupKey> newBucketChain = allGroupKeys.get(0);
1136 newBucketChain.addFirst(l3ecmpGroupKey);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001137 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1138 allActiveKeys.clear();
1139 }
1140 allActiveKeys.add(newBucketChain);
1141 updatePendingNextObjective(l3ecmpGroupKey,
1142 new OfdpaNextGroup(allActiveKeys, nextObjective));
Yi Tseng78f51f42017-02-02 13:54:58 -08001143
Charles Chan188ebf52015-12-23 00:15:11 -08001144 log.debug("Adding to L3ECMP: device:{} gid:{} gkey:{} nextId:{}",
1145 deviceId, Integer.toHexString(l3ecmpGroupId),
1146 l3ecmpGroupKey, nextObjective.id());
Yi Tseng78f51f42017-02-02 13:54:58 -08001147
1148 unsentGroups.forEach(groupInfo -> {
1149 // send the innermost group
1150 log.debug("Sending innermost group {} in group chain on device {} ",
1151 Integer.toHexString(groupInfo.innerMostGroupDesc.givenGroupId()), deviceId);
1152 updatePendingGroups(groupInfo.nextGroupDesc.appCookie(), l3ecmpGce);
1153 groupService.addGroup(groupInfo.innerMostGroupDesc);
1154 });
1155
Saurav Das1a129a02016-11-18 15:21:57 -08001156 }
Charles Chan188ebf52015-12-23 00:15:11 -08001157
Saurav Das1a129a02016-11-18 15:21:57 -08001158 private void addBucketToBroadcastGroup(NextObjective nextObj,
Yi Tseng78f51f42017-02-02 13:54:58 -08001159 List<Deque<GroupKey>> allActiveKeys) {
Saurav Das1a129a02016-11-18 15:21:57 -08001160 VlanId assignedVlan = Ofdpa2Pipeline.readVlanFromSelector(nextObj.meta());
1161 if (assignedVlan == null) {
1162 log.warn("VLAN ID required by broadcast next obj is missing. "
1163 + "Aborting add bucket to broadcast group for next:{} in dev:{}",
1164 nextObj.id(), deviceId);
1165 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
1166 return;
1167 }
1168
1169 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
1170
1171 IpPrefix ipDst = Ofdpa2Pipeline.readIpDstFromSelector(nextObj.meta());
1172 if (ipDst != null) {
1173 if (ipDst.isMulticast()) {
1174 addBucketToL3MulticastGroup(nextObj, allActiveKeys,
Yi Tseng78f51f42017-02-02 13:54:58 -08001175 groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001176 } else {
1177 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
1178 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
1179 return;
1180 }
1181 } else {
1182 addBucketToL2FloodGroup(nextObj, allActiveKeys,
Yi Tseng78f51f42017-02-02 13:54:58 -08001183 groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001184 }
1185 }
1186
1187 private void addBucketToL2FloodGroup(NextObjective nextObj,
1188 List<Deque<GroupKey>> allActiveKeys,
1189 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001190 VlanId assignedVlan) {
1191 Group l2FloodGroup = retrieveTopLevelGroup(allActiveKeys, nextObj.id());
1192
1193 if (l2FloodGroup == null) {
1194 log.warn("Can't find L2 flood group while adding bucket to it. NextObj = {}",
1195 nextObj);
Saurav Das1a129a02016-11-18 15:21:57 -08001196 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.GROUPMISSING);
1197 return;
1198 }
Saurav Das1a129a02016-11-18 15:21:57 -08001199
Yi Tseng78f51f42017-02-02 13:54:58 -08001200 GroupKey l2floodGroupKey = l2FloodGroup.appCookie();
1201 int l2floodGroupId = l2FloodGroup.id().id();
1202 List<GroupBucket> newBuckets = generateNextGroupBuckets(groupInfos, ALL);
1203
1204 GroupDescription l2FloodGroupDescription =
Saurav Das1a129a02016-11-18 15:21:57 -08001205 new DefaultGroupDescription(
1206 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -08001207 ALL,
1208 new GroupBuckets(newBuckets),
Saurav Das1a129a02016-11-18 15:21:57 -08001209 l2floodGroupKey,
1210 l2floodGroupId,
1211 nextObj.appId());
Saurav Das1a129a02016-11-18 15:21:57 -08001212
Yi Tseng78f51f42017-02-02 13:54:58 -08001213 GroupChainElem l2FloodGroupChainElement =
1214 new GroupChainElem(l2FloodGroupDescription,
1215 groupInfos.size(),
1216 true);
1217
Saurav Das1a129a02016-11-18 15:21:57 -08001218 updatePendingNextObjective(l2floodGroupKey,
1219 new OfdpaNextGroup(allActiveKeys, nextObj));
Yi Tseng78f51f42017-02-02 13:54:58 -08001220
1221 //ensure assignedVlan applies to the chosen group
1222 VlanId floodGroupVlan = extractVlanIdFromGroupId(l2floodGroupId);
1223
1224 if (!floodGroupVlan.equals(assignedVlan)) {
1225 log.warn("VLAN ID {} does not match Flood group {} to which bucket is "
1226 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1227 Integer.toHexString(l2floodGroupId), nextObj.id(), deviceId);
1228 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
1229 return;
1230 }
1231
1232 groupInfos.forEach(groupInfo -> {
1233 // update original NextGroup with new bucket-chain
1234 // If active keys shows only the top-level group without a chain of groups,
1235 // then it represents an empty group. Update by replacing empty chain.
1236 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
1237 newBucketChain.addFirst(groupInfo.nextGroupDesc.appCookie());
1238 newBucketChain.addFirst(l2floodGroupKey);
1239 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1240 allActiveKeys.clear();
1241 }
1242 allActiveKeys.add(newBucketChain);
1243
1244 log.debug("Adding to L2FLOOD: device:{} gid:{} gkey:{} nextId:{}",
1245 deviceId, Integer.toHexString(l2floodGroupId),
1246 l2floodGroupKey, nextObj.id());
1247 // send the innermost group
1248 log.debug("Sending innermost group {} in group chain on device {} ",
1249 Integer.toHexString(groupInfo.innerMostGroupDesc.givenGroupId()),
1250 deviceId);
1251
1252 updatePendingGroups(groupInfo.nextGroupDesc.appCookie(), l2FloodGroupChainElement);
1253
1254 DeviceId innerMostGroupDevice = groupInfo.innerMostGroupDesc.deviceId();
1255 GroupKey innerMostGroupKey = groupInfo.innerMostGroupDesc.appCookie();
1256 Group existsL2IGroup = groupService.getGroup(innerMostGroupDevice, innerMostGroupKey);
1257
1258 if (existsL2IGroup != null) {
1259 // group already exist
1260 processPendingAddGroupsOrNextObjs(innerMostGroupKey, true);
1261 } else {
1262 groupService.addGroup(groupInfo.innerMostGroupDesc);
1263 }
1264
1265 });
1266 }
1267
1268 private VlanId extractVlanIdFromGroupId(int groupId) {
1269 // Extract the 9th to 20th bit from group id as vlan id.
1270 short vlanId = (short) ((groupId & 0x0fff0000) >> 16);
1271 return VlanId.vlanId(vlanId);
1272 }
1273
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001274 private List<GroupBucket> generateNextGroupBuckets(List<GroupInfo> groupInfos,
1275 GroupDescription.Type bucketType) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001276 List<GroupBucket> newBuckets = Lists.newArrayList();
1277
1278 groupInfos.forEach(groupInfo -> {
1279 GroupDescription groupDesc = groupInfo.nextGroupDesc;
1280 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
Yi Tsengfa394de2017-02-01 11:26:40 -08001281 treatmentBuilder.group(new GroupId(groupDesc.givenGroupId()));
Yi Tseng78f51f42017-02-02 13:54:58 -08001282 GroupBucket newBucket = null;
1283 switch (bucketType) {
1284 case ALL:
1285 newBucket =
1286 DefaultGroupBucket.createAllGroupBucket(treatmentBuilder.build());
1287 break;
1288 case INDIRECT:
1289 newBucket =
1290 DefaultGroupBucket.createIndirectGroupBucket(treatmentBuilder.build());
1291 break;
1292 case SELECT:
1293 newBucket =
1294 DefaultGroupBucket.createSelectGroupBucket(treatmentBuilder.build());
1295 break;
1296 case FAILOVER:
1297 // TODO support failover bucket type
1298 default:
1299 log.warn("Unknown bucket type: {}", bucketType);
1300 break;
1301 }
1302
1303 if (newBucket != null) {
1304 newBuckets.add(newBucket);
1305 }
1306
1307 });
1308
1309 return ImmutableList.copyOf(newBuckets);
Saurav Das1a129a02016-11-18 15:21:57 -08001310 }
1311
1312 private void addBucketToL3MulticastGroup(NextObjective nextObj,
1313 List<Deque<GroupKey>> allActiveKeys,
1314 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001315 VlanId assignedVlan) {
1316 // create the buckets to add to the outermost L3 Multicast group
1317 List<GroupBucket> newBuckets = Lists.newArrayList();
1318 groupInfos.forEach(groupInfo -> {
1319 // Points to L3 interface group if there is one.
1320 // Otherwise points to L2 interface group directly.
1321 GroupDescription nextGroupDesc = (groupInfo.nextGroupDesc != null) ?
1322 groupInfo.nextGroupDesc : groupInfo.innerMostGroupDesc;
1323 TrafficTreatment.Builder treatmentBuidler = DefaultTrafficTreatment.builder();
Yi Tsengfa394de2017-02-01 11:26:40 -08001324 treatmentBuidler.group(new GroupId(nextGroupDesc.givenGroupId()));
Yi Tseng78f51f42017-02-02 13:54:58 -08001325 GroupBucket newBucket = DefaultGroupBucket.createAllGroupBucket(treatmentBuidler.build());
1326 newBuckets.add(newBucket);
1327 });
Saurav Das1a129a02016-11-18 15:21:57 -08001328
1329 // get the group being edited
1330 Group l3mcastGroup = retrieveTopLevelGroup(allActiveKeys, nextObj.id());
1331 if (l3mcastGroup == null) {
1332 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.GROUPMISSING);
1333 return;
1334 }
1335 GroupKey l3mcastGroupKey = l3mcastGroup.appCookie();
1336 int l3mcastGroupId = l3mcastGroup.id().id();
1337
1338 //ensure assignedVlan applies to the chosen group
Yi Tseng78f51f42017-02-02 13:54:58 -08001339 VlanId expectedVlan = extractVlanIdFromGroupId(l3mcastGroupId);
Saurav Das1a129a02016-11-18 15:21:57 -08001340 if (!expectedVlan.equals(assignedVlan)) {
1341 log.warn("VLAN ID {} does not match L3 Mcast group {} to which bucket is "
1342 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1343 Integer.toHexString(l3mcastGroupId), nextObj.id(), deviceId);
1344 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
1345 }
1346 GroupDescription l3mcastGroupDescription =
1347 new DefaultGroupDescription(
1348 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -08001349 ALL,
1350 new GroupBuckets(newBuckets),
Saurav Das1a129a02016-11-18 15:21:57 -08001351 l3mcastGroupKey,
1352 l3mcastGroupId,
1353 nextObj.appId());
1354 GroupChainElem l3mcastGce = new GroupChainElem(l3mcastGroupDescription,
Yi Tseng78f51f42017-02-02 13:54:58 -08001355 groupInfos.size(), true);
Saurav Das1a129a02016-11-18 15:21:57 -08001356
Yi Tseng78f51f42017-02-02 13:54:58 -08001357 groupInfos.forEach(groupInfo -> {
1358 // update original NextGroup with new bucket-chain
1359 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
1360 newBucketChain.addFirst(groupInfo.innerMostGroupDesc.appCookie());
1361 // Add L3 interface group to the chain if there is one.
1362 if (!groupInfo.nextGroupDesc.equals(groupInfo.innerMostGroupDesc)) {
1363 newBucketChain.addFirst(groupInfo.nextGroupDesc.appCookie());
1364 }
1365 newBucketChain.addFirst(l3mcastGroupKey);
1366 // If active keys shows only the top-level group without a chain of groups,
1367 // then it represents an empty group. Update by replacing empty chain.
1368 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1369 allActiveKeys.clear();
1370 }
1371 allActiveKeys.add(newBucketChain);
1372
1373 updatePendingGroups(groupInfo.nextGroupDesc.appCookie(), l3mcastGce);
1374 // Point next group to inner-most group, if any
1375 if (!groupInfo.nextGroupDesc.equals(groupInfo.innerMostGroupDesc)) {
1376 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc,
1377 1, false);
1378 updatePendingGroups(groupInfo.innerMostGroupDesc.appCookie(), innerGce);
1379 }
1380 log.debug("Adding to L3MCAST: device:{} gid:{} gkey:{} nextId:{}",
1381 deviceId, Integer.toHexString(l3mcastGroupId),
1382 l3mcastGroupKey, nextObj.id());
1383 // send the innermost group
1384 log.debug("Sending innermost group {} in group chain on device {} ",
1385 Integer.toHexString(groupInfo.innerMostGroupDesc.givenGroupId()),
1386 deviceId);
1387 groupService.addGroup(groupInfo.innerMostGroupDesc);
1388
1389 });
1390
Saurav Das1a129a02016-11-18 15:21:57 -08001391 updatePendingNextObjective(l3mcastGroupKey,
1392 new OfdpaNextGroup(allActiveKeys, nextObj));
1393
Yi Tseng78f51f42017-02-02 13:54:58 -08001394
1395
Charles Chan188ebf52015-12-23 00:15:11 -08001396 }
1397
1398 /**
1399 * Removes the bucket in the top level group of a possible group-chain. Does
Saurav Das1a129a02016-11-18 15:21:57 -08001400 * not remove the groups in the group-chain pointed to by this bucket, as they
Charles Chan188ebf52015-12-23 00:15:11 -08001401 * may be in use (referenced by other groups) elsewhere.
1402 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001403 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -08001404 * @param next the representation of the existing group-chain for this next objective
1405 */
1406 protected void removeBucketFromGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001407 if (nextObjective.type() != NextObjective.Type.HASHED &&
1408 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001409 log.warn("RemoveBuckets not applied to nextType:{} in dev:{} for next:{}",
1410 nextObjective.type(), deviceId, nextObjective.id());
Saurav Das1a129a02016-11-18 15:21:57 -08001411 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001412 return;
1413 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001414 Set<PortNumber> portsToRemove = Sets.newHashSet();
Charles Chan188ebf52015-12-23 00:15:11 -08001415 Collection<TrafficTreatment> treatments = nextObjective.next();
Yi Tseng78f51f42017-02-02 13:54:58 -08001416 for (TrafficTreatment treatment : treatments) {
1417 // find the bucket to remove by noting the outport, and figuring out the
1418 // top-level group in the group-chain that indirectly references the port
1419 PortNumber portToRemove = readOutPortFromTreatment(treatment);
1420 if (portToRemove == null) {
1421 log.warn("treatment {} of next objective {} has no outport.. cannot remove bucket"
1422 + "from group in dev: {}", treatment, nextObjective.id(), deviceId);
1423 } else {
1424 portsToRemove.add(portToRemove);
1425 }
1426 }
1427
1428 if (portsToRemove.isEmpty()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001429 log.warn("next objective {} has no outport.. cannot remove bucket"
Yi Tseng78f51f42017-02-02 13:54:58 -08001430 + "from group in dev: {}", nextObjective.id(), deviceId);
Saurav Das1a129a02016-11-18 15:21:57 -08001431 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -08001432 }
1433
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001434 List<Deque<GroupKey>> allActiveKeys = Ofdpa2Pipeline.appKryo.deserialize(next.data());
Yi Tseng78f51f42017-02-02 13:54:58 -08001435 List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001436 for (Deque<GroupKey> gkeys : allActiveKeys) {
1437 // last group in group chain should have a single bucket pointing to port
Yi Tseng78f51f42017-02-02 13:54:58 -08001438
Charles Chan188ebf52015-12-23 00:15:11 -08001439 GroupKey groupWithPort = gkeys.peekLast();
1440 Group group = groupService.getGroup(deviceId, groupWithPort);
1441 if (group == null) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001442 log.warn("Inconsistent group chain found when removing bucket"
1443 + "for next:{} in dev:{}", nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001444 continue;
1445 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001446 PortNumber pout = readOutPortFromTreatment(
1447 group.buckets().buckets().get(0).treatment());
Yi Tseng78f51f42017-02-02 13:54:58 -08001448 if (portsToRemove.contains(pout)) {
1449 chainsToRemove.add(gkeys);
Charles Chan188ebf52015-12-23 00:15:11 -08001450 }
Charles Chan188ebf52015-12-23 00:15:11 -08001451 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001452
1453 if (chainsToRemove.isEmpty()) {
Charles Chan188ebf52015-12-23 00:15:11 -08001454 log.warn("Could not find appropriate group-chain for removing bucket"
1455 + " for next id {} in dev:{}", nextObjective.id(), deviceId);
Saurav Das1a129a02016-11-18 15:21:57 -08001456 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.BADPARAMS);
1457 return;
Charles Chan188ebf52015-12-23 00:15:11 -08001458 }
Saurav Das1a129a02016-11-18 15:21:57 -08001459
Yi Tseng78f51f42017-02-02 13:54:58 -08001460 List<GroupBucket> bucketsToRemove = Lists.newArrayList();
1461 //first group key is the one we want to modify
1462 GroupKey modGroupKey = chainsToRemove.get(0).peekFirst();
Saurav Das1a129a02016-11-18 15:21:57 -08001463 Group modGroup = groupService.getGroup(deviceId, modGroupKey);
Yi Tseng78f51f42017-02-02 13:54:58 -08001464
1465 for (Deque<GroupKey> foundChain : chainsToRemove) {
1466 //second group key is the one we wish to remove the reference to
1467 if (foundChain.size() < 2) {
1468 // additional check to make sure second group key exist in
1469 // the chain.
1470 log.warn("Can't find second group key from chain {}",
1471 foundChain);
1472 continue;
Saurav Das1a129a02016-11-18 15:21:57 -08001473 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001474 GroupKey pointedGroupKey = foundChain.stream().collect(Collectors.toList()).get(1);
1475
1476 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
1477
1478 if (pointedGroup == null) {
1479 continue;
1480 }
1481
1482 GroupBucket bucket;
1483 if (nextObjective.type() == NextObjective.Type.HASHED) {
1484 bucket = DefaultGroupBucket.createSelectGroupBucket(
1485 DefaultTrafficTreatment.builder()
1486 .group(pointedGroup.id())
1487 .build());
1488 } else {
1489 bucket = DefaultGroupBucket.createAllGroupBucket(
1490 DefaultTrafficTreatment.builder()
1491 .group(pointedGroup.id())
1492 .build());
1493 }
1494
1495 bucketsToRemove.add(bucket);
Saurav Das1a129a02016-11-18 15:21:57 -08001496 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001497
1498 GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove);
1499 List<String> pointedGroupIds; // for debug log
1500 pointedGroupIds = bucketsToRemove.stream()
1501 .map(GroupBucket::treatment)
1502 .map(TrafficTreatment::allInstructions)
1503 .flatMap(List::stream)
1504 .filter(inst -> inst instanceof Instructions.GroupInstruction)
1505 .map(inst -> (Instructions.GroupInstruction) inst)
1506 .map(Instructions.GroupInstruction::groupId)
1507 .map(GroupId::id)
1508 .map(Integer::toHexString)
1509 .map(id -> HEX_PREFIX + id)
1510 .collect(Collectors.toList());
1511
1512
1513
1514 log.debug("Removing buckets from group id 0x{} pointing to group id(s) {} "
Saurav Das1a129a02016-11-18 15:21:57 -08001515 + "for next id {} in device {}", Integer.toHexString(modGroup.id().id()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001516 pointedGroupIds, nextObjective.id(), deviceId);
1517 addPendingUpdateNextObjective(modGroupKey, nextObjective);
Saurav Das1a129a02016-11-18 15:21:57 -08001518 groupService.removeBucketsFromGroup(deviceId, modGroupKey,
1519 removeBuckets, modGroupKey,
1520 nextObjective.appId());
1521 // update store
Yi Tsengeeb3dc12017-03-15 10:55:50 -07001522 allActiveKeys.removeAll(chainsToRemove);
1523 // If no buckets in the group, then retain an entry for the
1524 // top level group which still exists.
1525 if (allActiveKeys.isEmpty()) {
Saurav Das1a129a02016-11-18 15:21:57 -08001526 ArrayDeque<GroupKey> top = new ArrayDeque<>();
1527 top.add(modGroupKey);
1528 allActiveKeys.add(top);
1529 }
Saurav Das1a129a02016-11-18 15:21:57 -08001530 flowObjectiveStore.putNextGroup(nextObjective.id(),
1531 new OfdpaNextGroup(allActiveKeys,
1532 nextObjective));
Charles Chan188ebf52015-12-23 00:15:11 -08001533 }
1534
1535 /**
1536 * Removes all groups in multiple possible group-chains that represent the next
1537 * objective.
1538 *
1539 * @param nextObjective the next objective to remove
1540 * @param next the NextGroup that represents the existing group-chain for
1541 * this next objective
1542 */
1543 protected void removeGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001544 List<Deque<GroupKey>> allActiveKeys = Ofdpa2Pipeline.appKryo.deserialize(next.data());
Charles Chanfc5c7802016-05-17 13:13:55 -07001545
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001546 List<GroupKey> groupKeys = allActiveKeys.stream()
Charles Chanfc5c7802016-05-17 13:13:55 -07001547 .map(Deque::getFirst).collect(Collectors.toList());
1548 pendingRemoveNextObjectives.put(nextObjective, groupKeys);
1549
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001550 allActiveKeys.forEach(groupChain -> groupChain.forEach(groupKey ->
Charles Chan188ebf52015-12-23 00:15:11 -08001551 groupService.removeGroup(deviceId, groupKey, nextObjective.appId())));
1552 flowObjectiveStore.removeNextGroup(nextObjective.id());
1553 }
1554
Saurav Das8be4e3a2016-03-11 17:19:07 -08001555 //////////////////////////////////////
1556 // Helper Methods and Classes
1557 //////////////////////////////////////
1558
Pier Ventre140a8942016-11-02 07:26:38 -07001559 protected void updatePendingNextObjective(GroupKey key, OfdpaNextGroup value) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001560 List<OfdpaNextGroup> nextList = new CopyOnWriteArrayList<OfdpaNextGroup>();
1561 nextList.add(value);
Charles Chanfc5c7802016-05-17 13:13:55 -07001562 List<OfdpaNextGroup> ret = pendingAddNextObjectives.asMap()
Saurav Das8be4e3a2016-03-11 17:19:07 -08001563 .putIfAbsent(key, nextList);
1564 if (ret != null) {
1565 ret.add(value);
1566 }
1567 }
1568
Charles Chan425854b2016-04-11 15:32:12 -07001569 protected void updatePendingGroups(GroupKey gkey, GroupChainElem gce) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001570 Set<GroupChainElem> gceSet = Collections.newSetFromMap(
1571 new ConcurrentHashMap<GroupChainElem, Boolean>());
1572 gceSet.add(gce);
1573 Set<GroupChainElem> retval = pendingGroups.putIfAbsent(gkey, gceSet);
1574 if (retval != null) {
1575 retval.add(gce);
1576 }
1577 }
1578
Yi Tseng78f51f42017-02-02 13:54:58 -08001579 private void addPendingUpdateNextObjective(GroupKey groupKey, NextObjective nextObjective) {
1580 pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> {
1581 if (nextObjs != null) {
1582 nextObjs.add(nextObjective);
1583 } else {
1584 nextObjs = Sets.newHashSet(nextObjective);
1585 }
1586 return nextObjs;
1587 });
1588 }
1589
Charles Chan188ebf52015-12-23 00:15:11 -08001590 /**
1591 * Processes next element of a group chain. Assumption is that if this
1592 * group points to another group, the latter has already been created
1593 * and this driver has received notification for it. A second assumption is
1594 * that if there is another group waiting for this group then the appropriate
1595 * stores already have the information to act upon the notification for the
1596 * creation of this group.
1597 * <p>
1598 * The processing of the GroupChainElement depends on the number of groups
1599 * this element is waiting on. For all group types other than SIMPLE, a
1600 * GroupChainElement could be waiting on multiple groups.
1601 *
1602 * @param gce the group chain element to be processed next
1603 */
1604 private void processGroupChain(GroupChainElem gce) {
1605 int waitOnGroups = gce.decrementAndGetGroupsWaitedOn();
1606 if (waitOnGroups != 0) {
1607 log.debug("GCE: {} not ready to be processed", gce);
1608 return;
1609 }
1610 log.debug("GCE: {} ready to be processed", gce);
1611 if (gce.addBucketToGroup) {
1612 groupService.addBucketsToGroup(gce.groupDescription.deviceId(),
1613 gce.groupDescription.appCookie(),
1614 gce.groupDescription.buckets(),
1615 gce.groupDescription.appCookie(),
1616 gce.groupDescription.appId());
1617 } else {
1618 groupService.addGroup(gce.groupDescription);
1619 }
1620 }
1621
1622 private class GroupChecker implements Runnable {
1623 @Override
1624 public void run() {
1625 Set<GroupKey> keys = pendingGroups.keySet().stream()
1626 .filter(key -> groupService.getGroup(deviceId, key) != null)
1627 .collect(Collectors.toSet());
Charles Chanfc5c7802016-05-17 13:13:55 -07001628 Set<GroupKey> otherkeys = pendingAddNextObjectives.asMap().keySet().stream()
Charles Chan188ebf52015-12-23 00:15:11 -08001629 .filter(otherkey -> groupService.getGroup(deviceId, otherkey) != null)
1630 .collect(Collectors.toSet());
1631 keys.addAll(otherkeys);
1632
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -07001633 keys.forEach(key ->
Charles Chanfc5c7802016-05-17 13:13:55 -07001634 processPendingAddGroupsOrNextObjs(key, false));
Charles Chan188ebf52015-12-23 00:15:11 -08001635 }
1636 }
1637
Saurav Das8be4e3a2016-03-11 17:19:07 -08001638 private class InnerGroupListener implements GroupListener {
1639 @Override
1640 public void event(GroupEvent event) {
1641 log.trace("received group event of type {}", event.type());
Charles Chanfc5c7802016-05-17 13:13:55 -07001642 switch (event.type()) {
1643 case GROUP_ADDED:
1644 processPendingAddGroupsOrNextObjs(event.subject().appCookie(), true);
1645 break;
1646 case GROUP_REMOVED:
1647 processPendingRemoveNextObjs(event.subject().appCookie());
1648 break;
Yi Tseng78f51f42017-02-02 13:54:58 -08001649 case GROUP_UPDATED:
1650 processPendingUpdateNextObjs(event.subject().appCookie());
1651 break;
Charles Chanfc5c7802016-05-17 13:13:55 -07001652 default:
1653 break;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001654 }
1655 }
1656 }
1657
Yi Tseng78f51f42017-02-02 13:54:58 -08001658 private void processPendingUpdateNextObjs(GroupKey groupKey) {
1659
1660 pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> {
1661 if (nextObjs != null) {
1662
1663 nextObjs.forEach(nextObj -> {
1664 log.debug("Group {} updated, update pending next objective {}.",
1665 groupKey, nextObj);
1666
1667 Ofdpa2Pipeline.pass(nextObj);
1668 });
1669 }
1670
1671 return Sets.newHashSet();
1672 });
1673 }
1674
Charles Chanfc5c7802016-05-17 13:13:55 -07001675 private void processPendingAddGroupsOrNextObjs(GroupKey key, boolean added) {
Charles Chan188ebf52015-12-23 00:15:11 -08001676 //first check for group chain
1677 Set<GroupChainElem> gceSet = pendingGroups.remove(key);
1678 if (gceSet != null) {
1679 for (GroupChainElem gce : gceSet) {
Charles Chan216e3c82016-04-23 14:48:16 -07001680 log.debug("Group service {} group key {} in device {}. "
Saurav Das0fd79d92016-03-07 10:58:36 -08001681 + "Processing next group in group chain with group id 0x{}",
Charles Chan188ebf52015-12-23 00:15:11 -08001682 (added) ? "ADDED" : "processed",
1683 key, deviceId,
1684 Integer.toHexString(gce.groupDescription.givenGroupId()));
1685 processGroupChain(gce);
1686 }
1687 } else {
1688 // otherwise chain complete - check for waiting nextObjectives
Charles Chanfc5c7802016-05-17 13:13:55 -07001689 List<OfdpaNextGroup> nextGrpList = pendingAddNextObjectives.getIfPresent(key);
Charles Chan188ebf52015-12-23 00:15:11 -08001690 if (nextGrpList != null) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001691 pendingAddNextObjectives.invalidate(key);
Charles Chan188ebf52015-12-23 00:15:11 -08001692 nextGrpList.forEach(nextGrp -> {
Charles Chan216e3c82016-04-23 14:48:16 -07001693 log.debug("Group service {} group key {} in device:{}. "
Saurav Das0fd79d92016-03-07 10:58:36 -08001694 + "Done implementing next objective: {} <<-->> gid:0x{}",
Charles Chan188ebf52015-12-23 00:15:11 -08001695 (added) ? "ADDED" : "processed",
1696 key, deviceId, nextGrp.nextObjective().id(),
1697 Integer.toHexString(groupService.getGroup(deviceId, key)
1698 .givenGroupId()));
Charles Chan361154b2016-03-24 10:23:39 -07001699 Ofdpa2Pipeline.pass(nextGrp.nextObjective());
Charles Chan188ebf52015-12-23 00:15:11 -08001700 flowObjectiveStore.putNextGroup(nextGrp.nextObjective().id(), nextGrp);
1701 // check if addBuckets waiting for this completion
1702 NextObjective pendBkt = pendingBuckets
1703 .remove(nextGrp.nextObjective().id());
1704 if (pendBkt != null) {
1705 addBucketToGroup(pendBkt, nextGrp);
1706 }
1707 });
1708 }
1709 }
1710 }
1711
Charles Chanfc5c7802016-05-17 13:13:55 -07001712 private void processPendingRemoveNextObjs(GroupKey key) {
1713 pendingRemoveNextObjectives.asMap().forEach((nextObjective, groupKeys) -> {
1714 if (groupKeys.isEmpty()) {
1715 pendingRemoveNextObjectives.invalidate(nextObjective);
1716 Ofdpa2Pipeline.pass(nextObjective);
1717 } else {
1718 groupKeys.remove(key);
1719 }
1720 });
1721 }
1722
Charles Chan425854b2016-04-11 15:32:12 -07001723 protected int getNextAvailableIndex() {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001724 return (int) nextIndex.incrementAndGet();
1725 }
1726
Charles Chane849c192016-01-11 18:28:54 -08001727 /**
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001728 * Returns the outport in a traffic treatment.
1729 *
1730 * @param tt the treatment
1731 * @return the PortNumber for the outport or null
1732 */
1733 protected static PortNumber readOutPortFromTreatment(TrafficTreatment tt) {
1734 for (Instruction ins : tt.allInstructions()) {
1735 if (ins.type() == Instruction.Type.OUTPUT) {
1736 return ((Instructions.OutputInstruction) ins).port();
1737 }
1738 }
1739 return null;
1740 }
1741
1742 /**
Charles Chane849c192016-01-11 18:28:54 -08001743 * Returns a hash as the L2 Interface Group Key.
1744 *
1745 * Keep the lower 6-bit for port since port number usually smaller than 64.
1746 * Hash other information into remaining 28 bits.
1747 *
1748 * @param deviceId Device ID
1749 * @param vlanId VLAN ID
1750 * @param portNumber Port number
1751 * @return L2 interface group key
1752 */
Pier Ventre140a8942016-11-02 07:26:38 -07001753 protected int l2InterfaceGroupKey(DeviceId deviceId, VlanId vlanId, long portNumber) {
Charles Chane849c192016-01-11 18:28:54 -08001754 int portLowerBits = (int) portNumber & PORT_LOWER_BITS_MASK;
1755 long portHigherBits = portNumber & PORT_HIGHER_BITS_MASK;
Charles Chand0fd5dc2016-02-16 23:14:49 -08001756 int hash = Objects.hash(deviceId, vlanId, portHigherBits);
Charles Chane849c192016-01-11 18:28:54 -08001757 return L2_INTERFACE_TYPE | (TYPE_MASK & hash << 6) | portLowerBits;
1758 }
1759
Saurav Das1a129a02016-11-18 15:21:57 -08001760 private Group retrieveTopLevelGroup(List<Deque<GroupKey>> allActiveKeys,
1761 int nextid) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001762 GroupKey topLevelGroupKey;
Saurav Das1a129a02016-11-18 15:21:57 -08001763 if (!allActiveKeys.isEmpty()) {
1764 topLevelGroupKey = allActiveKeys.get(0).peekFirst();
1765 } else {
1766 log.warn("Could not determine top level group while processing"
1767 + "next:{} in dev:{}", nextid, deviceId);
1768 return null;
1769 }
1770 Group topGroup = groupService.getGroup(deviceId, topLevelGroupKey);
1771 if (topGroup == null) {
1772 log.warn("Could not find top level group while processing "
1773 + "next:{} in dev:{}", nextid, deviceId);
1774 }
1775 return topGroup;
1776 }
1777
Charles Chan188ebf52015-12-23 00:15:11 -08001778 /**
1779 * Utility class for moving group information around.
Saurav Das1a129a02016-11-18 15:21:57 -08001780 *
1781 * Example: Suppose we are trying to create a group-chain A-B-C-D, where
1782 * A is the top level group, and D is the inner-most group, typically L2 Interface.
1783 * The innerMostGroupDesc is always D. At various stages of the creation
1784 * process the nextGroupDesc may be C or B. The nextGroupDesc exists to
1785 * inform the referencing group about which group it needs to point to,
1786 * and wait for. In some cases the group chain may simply be A-B. In this case,
1787 * both innerMostGroupDesc and nextGroupDesc will be B.
Charles Chan188ebf52015-12-23 00:15:11 -08001788 */
Charles Chan425854b2016-04-11 15:32:12 -07001789 protected class GroupInfo {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001790 /**
1791 * Description of the inner-most group of the group chain.
1792 * It is always an L2 interface group.
1793 */
1794 private GroupDescription innerMostGroupDesc;
Charles Chan188ebf52015-12-23 00:15:11 -08001795
Charles Chan5b9df8d2016-03-28 22:21:40 -07001796 /**
1797 * Description of the next group in the group chain.
1798 * It can be L2 interface, L3 interface, L3 unicast, L3 VPN group.
Saurav Das1a129a02016-11-18 15:21:57 -08001799 * It is possible that nextGroupDesc is the same as the innerMostGroup.
Charles Chan5b9df8d2016-03-28 22:21:40 -07001800 */
1801 private GroupDescription nextGroupDesc;
1802
1803 GroupInfo(GroupDescription innerMostGroupDesc, GroupDescription nextGroupDesc) {
1804 this.innerMostGroupDesc = innerMostGroupDesc;
1805 this.nextGroupDesc = nextGroupDesc;
Charles Chan188ebf52015-12-23 00:15:11 -08001806 }
Pier Ventre140a8942016-11-02 07:26:38 -07001807
1808 /**
1809 * Getter for innerMostGroupDesc.
1810 *
1811 * @return the inner most group description
1812 */
1813 public GroupDescription getInnerMostGroupDesc() {
1814 return innerMostGroupDesc;
1815 }
1816
1817 /**
1818 * Getter for the next group description.
1819 *
1820 * @return the next group description
1821 */
1822 public GroupDescription getNextGroupDesc() {
1823 return nextGroupDesc;
1824 }
Charles Chan188ebf52015-12-23 00:15:11 -08001825 }
1826
1827 /**
1828 * Represents an entire group-chain that implements a Next-Objective from
1829 * the application. The objective is represented as a list of deques, where
1830 * each deque is a separate chain of groups.
1831 * <p>
1832 * For example, an ECMP group with 3 buckets, where each bucket points to
1833 * a group chain of L3 Unicast and L2 interface groups will look like this:
1834 * <ul>
1835 * <li>List[0] is a Deque of GroupKeyECMP(first)-GroupKeyL3(middle)-GroupKeyL2(last)
1836 * <li>List[1] is a Deque of GroupKeyECMP(first)-GroupKeyL3(middle)-GroupKeyL2(last)
1837 * <li>List[2] is a Deque of GroupKeyECMP(first)-GroupKeyL3(middle)-GroupKeyL2(last)
1838 * </ul>
1839 * where the first element of each deque is the same, representing the
1840 * top level ECMP group, while every other element represents a unique groupKey.
1841 * <p>
1842 * Also includes information about the next objective that
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001843 * resulted in these group-chains.
Charles Chan188ebf52015-12-23 00:15:11 -08001844 *
1845 */
1846 protected class OfdpaNextGroup implements NextGroup {
1847 private final NextObjective nextObj;
1848 private final List<Deque<GroupKey>> gkeys;
1849
1850 public OfdpaNextGroup(List<Deque<GroupKey>> gkeys, NextObjective nextObj) {
Charles Chan188ebf52015-12-23 00:15:11 -08001851 this.nextObj = nextObj;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001852 this.gkeys = gkeys;
Charles Chan188ebf52015-12-23 00:15:11 -08001853 }
1854
1855 public NextObjective nextObjective() {
1856 return nextObj;
1857 }
1858
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001859 public List<Deque<GroupKey>> groupKeys() {
1860 return gkeys;
1861 }
1862
Charles Chan188ebf52015-12-23 00:15:11 -08001863 @Override
1864 public byte[] data() {
Charles Chan361154b2016-03-24 10:23:39 -07001865 return Ofdpa2Pipeline.appKryo.serialize(gkeys);
Charles Chan188ebf52015-12-23 00:15:11 -08001866 }
1867 }
1868
1869 /**
1870 * Represents a group element that is part of a chain of groups.
1871 * Stores enough information to create a Group Description to add the group
1872 * to the switch by requesting the Group Service. Objects instantiating this
1873 * class are meant to be temporary and live as long as it is needed to wait for
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001874 * referenced groups in the group chain to be created.
Charles Chan188ebf52015-12-23 00:15:11 -08001875 */
Charles Chan425854b2016-04-11 15:32:12 -07001876 protected class GroupChainElem {
Charles Chan188ebf52015-12-23 00:15:11 -08001877 private GroupDescription groupDescription;
1878 private AtomicInteger waitOnGroups;
1879 private boolean addBucketToGroup;
1880
1881 GroupChainElem(GroupDescription groupDescription, int waitOnGroups,
1882 boolean addBucketToGroup) {
1883 this.groupDescription = groupDescription;
1884 this.waitOnGroups = new AtomicInteger(waitOnGroups);
1885 this.addBucketToGroup = addBucketToGroup;
1886 }
1887
1888 /**
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001889 * This method atomically decrements the counter for the number of
Charles Chan188ebf52015-12-23 00:15:11 -08001890 * groups this GroupChainElement is waiting on, for notifications from
1891 * the Group Service. When this method returns a value of 0, this
1892 * GroupChainElement is ready to be processed.
1893 *
1894 * @return integer indication of the number of notifications being waited on
1895 */
1896 int decrementAndGetGroupsWaitedOn() {
1897 return waitOnGroups.decrementAndGet();
1898 }
1899
1900 @Override
1901 public String toString() {
1902 return (Integer.toHexString(groupDescription.givenGroupId()) +
1903 " groupKey: " + groupDescription.appCookie() +
1904 " waiting-on-groups: " + waitOnGroups.get() +
1905 " addBucketToGroup: " + addBucketToGroup +
1906 " device: " + deviceId);
1907 }
1908 }
Pier Ventre140a8942016-11-02 07:26:38 -07001909
1910 /**
1911 * Helper enum to handle the different MPLS group
1912 * types.
1913 */
1914 protected enum OfdpaMplsGroupSubType {
1915
1916 MPLS_INTF((short) 0),
1917
1918 L2_VPN((short) 1),
1919
1920 L3_VPN((short) 2),
1921
1922 MPLS_TUNNEL_LABEL_1((short) 3),
1923
1924 MPLS_TUNNEL_LABEL_2((short) 4),
1925
1926 MPLS_SWAP_LABEL((short) 5),
1927
1928 MPLS_ECMP((short) 8);
1929
1930 private short value;
1931
1932 public static final int OFDPA_GROUP_TYPE_SHIFT = 28;
1933 public static final int OFDPA_MPLS_SUBTYPE_SHIFT = 24;
1934
1935 OfdpaMplsGroupSubType(short value) {
1936 this.value = value;
1937 }
1938
1939 /**
1940 * Gets the value as an short.
1941 *
1942 * @return the value as an short
1943 */
1944 public short getValue() {
1945 return this.value;
1946 }
1947
1948 }
1949
1950 /**
1951 * Creates MPLS Label group id given a sub type and
1952 * the index.
1953 *
1954 * @param subType the MPLS Label group sub type
1955 * @param index the index of the group
1956 * @return the OFDPA group id
1957 */
1958 public Integer makeMplsLabelGroupId(OfdpaMplsGroupSubType subType, int index) {
1959 index = index & 0x00FFFFFF;
1960 return index | (9 << OFDPA_GROUP_TYPE_SHIFT) | (subType.value << OFDPA_MPLS_SUBTYPE_SHIFT);
1961 }
1962
1963 /**
1964 * Creates MPLS Forwarding group id given a sub type and
1965 * the index.
1966 *
1967 * @param subType the MPLS forwarding group sub type
1968 * @param index the index of the group
1969 * @return the OFDPA group id
1970 */
1971 public Integer makeMplsForwardingGroupId(OfdpaMplsGroupSubType subType, int index) {
1972 index = index & 0x00FFFFFF;
1973 return index | (10 << OFDPA_GROUP_TYPE_SHIFT) | (subType.value << OFDPA_MPLS_SUBTYPE_SHIFT);
1974 }
Charles Chan188ebf52015-12-23 00:15:11 -08001975}