blob: 8637e0b9ee34289defddaa0d8423aff51c107226 [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;
Yi Tseng47f82dc2017-03-05 22:48:39 -080046import org.onosproject.net.flowobjective.DefaultNextObjective;
Charles Chan188ebf52015-12-23 00:15:11 -080047import org.onosproject.net.flowobjective.FlowObjectiveStore;
48import org.onosproject.net.flowobjective.NextObjective;
Yi Tseng47f82dc2017-03-05 22:48:39 -080049import org.onosproject.net.flowobjective.ObjectiveContext;
Charles Chan188ebf52015-12-23 00:15:11 -080050import org.onosproject.net.flowobjective.ObjectiveError;
51import org.onosproject.net.group.DefaultGroupBucket;
52import org.onosproject.net.group.DefaultGroupDescription;
53import org.onosproject.net.group.DefaultGroupKey;
54import org.onosproject.net.group.Group;
55import org.onosproject.net.group.GroupBucket;
56import org.onosproject.net.group.GroupBuckets;
57import org.onosproject.net.group.GroupDescription;
58import org.onosproject.net.group.GroupEvent;
59import org.onosproject.net.group.GroupKey;
60import org.onosproject.net.group.GroupListener;
61import org.onosproject.net.group.GroupService;
Saurav Das8be4e3a2016-03-11 17:19:07 -080062import org.onosproject.store.service.AtomicCounter;
63import org.onosproject.store.service.StorageService;
Charles Chan188ebf52015-12-23 00:15:11 -080064import org.slf4j.Logger;
65
66import java.util.ArrayDeque;
67import java.util.ArrayList;
68import java.util.Collection;
69import java.util.Collections;
70import java.util.Deque;
71import java.util.List;
Charles Chand0fd5dc2016-02-16 23:14:49 -080072import java.util.Objects;
Charles Chan188ebf52015-12-23 00:15:11 -080073import java.util.Set;
74import java.util.concurrent.ConcurrentHashMap;
75import java.util.concurrent.CopyOnWriteArrayList;
76import java.util.concurrent.Executors;
77import java.util.concurrent.ScheduledExecutorService;
78import java.util.concurrent.TimeUnit;
79import java.util.concurrent.atomic.AtomicInteger;
80import java.util.stream.Collectors;
81
82import static org.onlab.util.Tools.groupedThreads;
Pier Ventre140a8942016-11-02 07:26:38 -070083import static org.onosproject.driver.pipeline.Ofdpa2GroupHandler.OfdpaMplsGroupSubType.OFDPA_GROUP_TYPE_SHIFT;
84import static org.onosproject.driver.pipeline.Ofdpa2GroupHandler.OfdpaMplsGroupSubType.OFDPA_MPLS_SUBTYPE_SHIFT;
85import static org.onosproject.driver.pipeline.Ofdpa2Pipeline.isNotMplsBos;
Pier Ventre42287df2016-11-09 14:17:26 -080086import static org.onosproject.net.flow.criteria.Criterion.Type.TUNNEL_ID;
Pier Ventre140a8942016-11-02 07:26:38 -070087import static org.onosproject.net.flow.criteria.Criterion.Type.VLAN_VID;
88import static org.onosproject.net.flowobjective.NextObjective.Type.HASHED;
Yi Tseng78f51f42017-02-02 13:54:58 -080089import static org.onosproject.net.group.GroupDescription.Type.ALL;
90import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Charles Chan188ebf52015-12-23 00:15:11 -080091import static org.slf4j.LoggerFactory.getLogger;
92
93/**
Charles Chan40132b32017-01-22 00:19:37 -080094 * Group handler that emulates Broadcom OF-DPA TTP on CpqD.
Charles Chan188ebf52015-12-23 00:15:11 -080095 */
Charles Chan361154b2016-03-24 10:23:39 -070096public class Ofdpa2GroupHandler {
Charles Chan188ebf52015-12-23 00:15:11 -080097 /*
98 * OFDPA requires group-id's to have a certain form.
99 * L2 Interface Groups have <4bits-0><12bits-vlanid><16bits-portid>
100 * L3 Unicast Groups have <4bits-2><28bits-index>
101 * MPLS Interface Groups have <4bits-9><4bits:0><24bits-index>
102 * L3 ECMP Groups have <4bits-7><28bits-index>
103 * L2 Flood Groups have <4bits-4><12bits-vlanid><16bits-index>
104 * L3 VPN Groups have <4bits-9><4bits-2><24bits-index>
105 */
Charles Chan425854b2016-04-11 15:32:12 -0700106 protected static final int L2_INTERFACE_TYPE = 0x00000000;
107 protected static final int L3_INTERFACE_TYPE = 0x50000000;
108 protected static final int L3_UNICAST_TYPE = 0x20000000;
109 protected static final int L3_MULTICAST_TYPE = 0x60000000;
110 protected static final int MPLS_INTERFACE_TYPE = 0x90000000;
111 protected static final int MPLS_L3VPN_SUBTYPE = 0x92000000;
112 protected static final int L3_ECMP_TYPE = 0x70000000;
113 protected static final int L2_FLOOD_TYPE = 0x40000000;
Charles Chane849c192016-01-11 18:28:54 -0800114
Charles Chan425854b2016-04-11 15:32:12 -0700115 protected static final int TYPE_MASK = 0x0fffffff;
116 protected static final int SUBTYPE_MASK = 0x00ffffff;
117 protected static final int TYPE_VLAN_MASK = 0x0000ffff;
Charles Chane849c192016-01-11 18:28:54 -0800118
Charles Chan372b63e2017-02-07 12:10:53 -0800119 protected static final int THREE_BIT_MASK = 0x0fff;
120 protected static final int FOUR_BIT_MASK = 0xffff;
121 protected static final int PORT_LEN = 16;
122
Charles Chan425854b2016-04-11 15:32:12 -0700123 protected static final int PORT_LOWER_BITS_MASK = 0x3f;
124 protected static final long PORT_HIGHER_BITS_MASK = ~PORT_LOWER_BITS_MASK;
Charles Chan188ebf52015-12-23 00:15:11 -0800125
Yi Tseng78f51f42017-02-02 13:54:58 -0800126 protected static final String HEX_PREFIX = "0x";
127
Charles Chan188ebf52015-12-23 00:15:11 -0800128 private final Logger log = getLogger(getClass());
129 private ServiceDirectory serviceDirectory;
130 protected GroupService groupService;
Saurav Das8be4e3a2016-03-11 17:19:07 -0800131 protected StorageService storageService;
Charles Chan188ebf52015-12-23 00:15:11 -0800132
Charles Chan425854b2016-04-11 15:32:12 -0700133 protected DeviceId deviceId;
Charles Chan188ebf52015-12-23 00:15:11 -0800134 private FlowObjectiveStore flowObjectiveStore;
Charles Chanfc5c7802016-05-17 13:13:55 -0700135 private Cache<GroupKey, List<OfdpaNextGroup>> pendingAddNextObjectives;
136 private Cache<NextObjective, List<GroupKey>> pendingRemoveNextObjectives;
Charles Chan188ebf52015-12-23 00:15:11 -0800137 private ConcurrentHashMap<GroupKey, Set<GroupChainElem>> pendingGroups;
Yi Tseng78f51f42017-02-02 13:54:58 -0800138 private ConcurrentHashMap<GroupKey, Set<NextObjective>> pendingUpdateNextObjectives;
Charles Chan188ebf52015-12-23 00:15:11 -0800139 private ScheduledExecutorService groupChecker =
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700140 Executors.newScheduledThreadPool(2, groupedThreads("onos/pipeliner", "ofdpa2-%d", log));
Charles Chan188ebf52015-12-23 00:15:11 -0800141
142 // index number for group creation
Saurav Das8be4e3a2016-03-11 17:19:07 -0800143 private AtomicCounter nextIndex;
Charles Chan188ebf52015-12-23 00:15:11 -0800144
Yi Tseng47f82dc2017-03-05 22:48:39 -0800145 // local store for pending bucketAdds - by design there can be multiple
Charles Chan188ebf52015-12-23 00:15:11 -0800146 // pending bucket for a group
Yi Tseng47f82dc2017-03-05 22:48:39 -0800147 protected ConcurrentHashMap<Integer, Set<NextObjective>> pendingBuckets =
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700148 new ConcurrentHashMap<>();
Charles Chan188ebf52015-12-23 00:15:11 -0800149
Charles Chan40132b32017-01-22 00:19:37 -0800150 /**
151 * Determines whether this pipeline support copy ttl instructions or not.
152 *
153 * @return true if copy ttl instructions are supported
154 */
155 protected boolean supportCopyTtl() {
156 return true;
157 }
158
159 /**
160 * Determines whether this pipeline support set mpls bos instruction or not.
161 *
162 * @return true if set mpls bos instruction is supported
163 */
164 protected boolean supportSetMplsBos() {
165 return true;
166 }
167
Charles Chan0f43e472017-02-14 14:00:16 -0800168 /**
169 * Determines whether this pipeline requires popping VLAN before pushing MPLS.
170 * <p>
171 * If required, pop vlan before push mpls and add an arbitrary vlan back afterward.
172 * MPLS interface group will substitute the arbitrary VLAN with expected VLAN later on.
173 *
174 * @return true if this pipeline requires popping VLAN before pushing MPLS
175 */
176 protected boolean requireVlanPopBeforeMplsPush() {
177 return false;
178 }
179
Charles Chan188ebf52015-12-23 00:15:11 -0800180 protected void init(DeviceId deviceId, PipelinerContext context) {
181 this.deviceId = deviceId;
182 this.flowObjectiveStore = context.store();
183 this.serviceDirectory = context.directory();
184 this.groupService = serviceDirectory.get(GroupService.class);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800185 this.storageService = serviceDirectory.get(StorageService.class);
Madan Jampanid5714e02016-04-19 14:15:20 -0700186 this.nextIndex = storageService.getAtomicCounter("group-id-index-counter");
Charles Chan188ebf52015-12-23 00:15:11 -0800187
Charles Chanfc5c7802016-05-17 13:13:55 -0700188 pendingAddNextObjectives = CacheBuilder.newBuilder()
Charles Chan188ebf52015-12-23 00:15:11 -0800189 .expireAfterWrite(20, TimeUnit.SECONDS)
190 .removalListener((
191 RemovalNotification<GroupKey, List<OfdpaNextGroup>> notification) -> {
192 if (notification.getCause() == RemovalCause.EXPIRED) {
193 notification.getValue().forEach(ofdpaNextGrp ->
Charles Chan361154b2016-03-24 10:23:39 -0700194 Ofdpa2Pipeline.fail(ofdpaNextGrp.nextObj,
Charles Chan188ebf52015-12-23 00:15:11 -0800195 ObjectiveError.GROUPINSTALLATIONFAILED));
Charles Chanfc5c7802016-05-17 13:13:55 -0700196 }
197 }).build();
Charles Chan188ebf52015-12-23 00:15:11 -0800198
Charles Chanfc5c7802016-05-17 13:13:55 -0700199 pendingRemoveNextObjectives = CacheBuilder.newBuilder()
200 .expireAfterWrite(20, TimeUnit.SECONDS)
201 .removalListener((
202 RemovalNotification<NextObjective, List<GroupKey>> notification) -> {
203 if (notification.getCause() == RemovalCause.EXPIRED) {
204 Ofdpa2Pipeline.fail(notification.getKey(),
205 ObjectiveError.GROUPREMOVALFAILED);
Charles Chan188ebf52015-12-23 00:15:11 -0800206 }
207 }).build();
208 pendingGroups = new ConcurrentHashMap<>();
Yi Tseng78f51f42017-02-02 13:54:58 -0800209 pendingUpdateNextObjectives = new ConcurrentHashMap<>();
Charles Chan188ebf52015-12-23 00:15:11 -0800210 groupChecker.scheduleAtFixedRate(new GroupChecker(), 0, 500, TimeUnit.MILLISECONDS);
211
212 groupService.addListener(new InnerGroupListener());
213 }
214
Pier Ventre140a8942016-11-02 07:26:38 -0700215 /**
216 * The purpose of this function is to verify if the hashed next
217 * objective is supported by the current pipeline.
218 *
219 * @param nextObjective the hashed objective to verify
220 * @return true if the hashed objective is supported. Otherwise false.
221 */
222 public boolean verifyHashedNextObjective(NextObjective nextObjective) {
223 // if it is not hashed, there is something wrong;
224 if (nextObjective.type() != HASHED) {
225 return false;
226 }
227 // The case non supported is the MPLS-ECMP. For now, we try
228 // to create a MPLS-ECMP for the transport of a VPWS. The
229 // necessary info are contained in the meta selector. In particular
230 // we are looking for the case of BoS==False;
231 TrafficSelector metaSelector = nextObjective.meta();
232 if (metaSelector != null && isNotMplsBos(metaSelector)) {
233 return false;
234 }
235
236 return true;
237 }
238
Saurav Das8be4e3a2016-03-11 17:19:07 -0800239 //////////////////////////////////////
240 // Group Creation
241 //////////////////////////////////////
242
Charles Chan188ebf52015-12-23 00:15:11 -0800243 protected void addGroup(NextObjective nextObjective) {
244 switch (nextObjective.type()) {
245 case SIMPLE:
246 Collection<TrafficTreatment> treatments = nextObjective.next();
247 if (treatments.size() != 1) {
248 log.error("Next Objectives of type Simple should only have a "
249 + "single Traffic Treatment. Next Objective Id:{}",
250 nextObjective.id());
Charles Chan361154b2016-03-24 10:23:39 -0700251 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800252 return;
253 }
254 processSimpleNextObjective(nextObjective);
255 break;
256 case BROADCAST:
257 processBroadcastNextObjective(nextObjective);
258 break;
259 case HASHED:
Pier Ventre140a8942016-11-02 07:26:38 -0700260 if (!verifyHashedNextObjective(nextObjective)) {
261 log.error("Next Objectives of type hashed not supported. Next Objective Id:{}",
262 nextObjective.id());
263 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.BADPARAMS);
264 return;
265 }
Charles Chan188ebf52015-12-23 00:15:11 -0800266 processHashedNextObjective(nextObjective);
267 break;
268 case FAILOVER:
Charles Chan361154b2016-03-24 10:23:39 -0700269 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -0800270 log.warn("Unsupported next objective type {}", nextObjective.type());
271 break;
272 default:
Charles Chan361154b2016-03-24 10:23:39 -0700273 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.UNKNOWN);
Charles Chan188ebf52015-12-23 00:15:11 -0800274 log.warn("Unknown next objective type {}", nextObjective.type());
275 }
276 }
277
278 /**
279 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
280 * a chain of groups. The simple Next Objective passed
281 * in by the application has to be broken up into a group chain
282 * comprising of an L3 Unicast Group that points to an L2 Interface
283 * Group which in-turn points to an output port. In some cases, the simple
284 * next Objective can just be an L2 interface without the need for chaining.
285 *
286 * @param nextObj the nextObjective of type SIMPLE
287 */
288 private void processSimpleNextObjective(NextObjective nextObj) {
289 TrafficTreatment treatment = nextObj.next().iterator().next();
290 // determine if plain L2 or L3->L2
291 boolean plainL2 = true;
292 for (Instruction ins : treatment.allInstructions()) {
293 if (ins.type() == Instruction.Type.L2MODIFICATION) {
294 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
295 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_DST ||
296 l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_SRC) {
297 plainL2 = false;
298 break;
299 }
300 }
301 }
302
303 if (plainL2) {
304 createL2InterfaceGroup(nextObj);
305 return;
306 }
307
Pier Ventre140a8942016-11-02 07:26:38 -0700308 boolean isMpls = false;
Pier Ventre42287df2016-11-09 14:17:26 -0800309 // In order to understand if it is a pseudo wire related
310 // next objective we look for the tunnel id in the meta.
311 boolean isPw = false;
Pier Ventre140a8942016-11-02 07:26:38 -0700312 if (nextObj.meta() != null) {
313 isMpls = isNotMplsBos(nextObj.meta());
Pier Ventre42287df2016-11-09 14:17:26 -0800314
315 TunnelIdCriterion tunnelIdCriterion = (TunnelIdCriterion) nextObj
316 .meta()
317 .getCriterion(TUNNEL_ID);
318 if (tunnelIdCriterion != null) {
319 isPw = true;
320 }
321
Pier Ventre140a8942016-11-02 07:26:38 -0700322 }
323
Pier Ventre42287df2016-11-09 14:17:26 -0800324 if (!isPw) {
325 // break up simple next objective to GroupChain objects
326 GroupInfo groupInfo = createL2L3Chain(treatment, nextObj.id(),
327 nextObj.appId(), isMpls,
328 nextObj.meta());
329 if (groupInfo == null) {
330 log.error("Could not process nextObj={} in dev:{}", nextObj.id(), deviceId);
331 return;
332 }
333 // create object for local and distributed storage
334 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
335 gkeyChain.addFirst(groupInfo.innerMostGroupDesc.appCookie());
336 gkeyChain.addFirst(groupInfo.nextGroupDesc.appCookie());
337 OfdpaNextGroup ofdpaGrp =
338 new OfdpaNextGroup(Collections.singletonList(gkeyChain), nextObj);
339
340 // store l3groupkey with the ofdpaNextGroup for the nextObjective that depends on it
341 updatePendingNextObjective(groupInfo.nextGroupDesc.appCookie(), ofdpaGrp);
342
343 // now we are ready to send the l2 groupDescription (inner), as all the stores
344 // that will get async replies have been updated. By waiting to update
345 // the stores, we prevent nasty race conditions.
346 groupService.addGroup(groupInfo.innerMostGroupDesc);
347 } else {
348 // We handle the pseudo wire with a different a procedure.
349 // This procedure is meant to handle both initiation and
350 // termination of the pseudo wire.
351 processPwNextObjective(nextObj);
Charles Chan188ebf52015-12-23 00:15:11 -0800352 }
Charles Chan188ebf52015-12-23 00:15:11 -0800353 }
354
Charles Chan188ebf52015-12-23 00:15:11 -0800355 /**
356 * Creates a simple L2 Interface Group.
357 *
358 * @param nextObj the next Objective
359 */
360 private void createL2InterfaceGroup(NextObjective nextObj) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700361 VlanId assignedVlan = Ofdpa2Pipeline.readVlanFromSelector(nextObj.meta());
362 if (assignedVlan == null) {
363 log.warn("VLAN ID required by simple next obj is missing. Abort.");
364 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800365 return;
366 }
367
Charles Chan5b9df8d2016-03-28 22:21:40 -0700368 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Charles Chan188ebf52015-12-23 00:15:11 -0800369
Charles Chan5b9df8d2016-03-28 22:21:40 -0700370 // There is only one L2 interface group in this case
371 GroupDescription l2InterfaceGroupDesc = groupInfos.get(0).innerMostGroupDesc;
Charles Chan188ebf52015-12-23 00:15:11 -0800372
Charles Chan5b9df8d2016-03-28 22:21:40 -0700373 // Put all dependency information into allGroupKeys
374 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
375 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
376 gkeyChain.addFirst(l2InterfaceGroupDesc.appCookie());
377 allGroupKeys.add(gkeyChain);
Charles Chan188ebf52015-12-23 00:15:11 -0800378
Charles Chan5b9df8d2016-03-28 22:21:40 -0700379 // Point the next objective to this group
380 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
381 updatePendingNextObjective(l2InterfaceGroupDesc.appCookie(), ofdpaGrp);
382
383 // Start installing the inner-most group
384 groupService.addGroup(l2InterfaceGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800385 }
386
387 /**
388 * Creates one of two possible group-chains from the treatment
389 * passed in. Depending on the MPLS boolean, this method either creates
Charles Chan425854b2016-04-11 15:32:12 -0700390 * an L3Unicast Group --&gt; L2Interface Group, if mpls is false;
391 * or MPLSInterface Group --&gt; L2Interface Group, if mpls is true;
Charles Chan188ebf52015-12-23 00:15:11 -0800392 * The returned 'inner' group description is always the L2 Interface group.
393 *
394 * @param treatment that needs to be broken up to create the group chain
395 * @param nextId of the next objective that needs this group chain
396 * @param appId of the application that sent this next objective
397 * @param mpls determines if L3Unicast or MPLSInterface group is created
398 * @param meta metadata passed in by the application as part of the nextObjective
399 * @return GroupInfo containing the GroupDescription of the
400 * L2Interface group(inner) and the GroupDescription of the (outer)
401 * L3Unicast/MPLSInterface group. May return null if there is an
402 * error in processing the chain
403 */
Charles Chan425854b2016-04-11 15:32:12 -0700404 protected GroupInfo createL2L3Chain(TrafficTreatment treatment, int nextId,
Charles Chanf9e98652016-09-07 16:54:23 -0700405 ApplicationId appId, boolean mpls,
406 TrafficSelector meta) {
407 return createL2L3ChainInternal(treatment, nextId, appId, mpls, meta, true);
408 }
409
410 /**
411 * Internal implementation of createL2L3Chain.
412 * <p>
413 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
414 * Since it is non-OF spec, we need an extension treatment for that.
415 * The useSetVlanExtension must be set to false for OFDPA i12.
416 * </p>
417 *
418 * @param treatment that needs to be broken up to create the group chain
419 * @param nextId of the next objective that needs this group chain
420 * @param appId of the application that sent this next objective
421 * @param mpls determines if L3Unicast or MPLSInterface group is created
422 * @param meta metadata passed in by the application as part of the nextObjective
423 * @param useSetVlanExtension use the setVlanVid extension that has is_present bit set to 0.
424 * @return GroupInfo containing the GroupDescription of the
425 * L2Interface group(inner) and the GroupDescription of the (outer)
426 * L3Unicast/MPLSInterface group. May return null if there is an
427 * error in processing the chain
428 */
429 protected GroupInfo createL2L3ChainInternal(TrafficTreatment treatment, int nextId,
Pier Ventre42287df2016-11-09 14:17:26 -0800430 ApplicationId appId, boolean mpls,
431 TrafficSelector meta, boolean useSetVlanExtension) {
Charles Chan188ebf52015-12-23 00:15:11 -0800432 // for the l2interface group, get vlan and port info
433 // for the outer group, get the src/dst mac, and vlan info
434 TrafficTreatment.Builder outerTtb = DefaultTrafficTreatment.builder();
435 TrafficTreatment.Builder innerTtb = DefaultTrafficTreatment.builder();
436 VlanId vlanid = null;
437 long portNum = 0;
438 boolean setVlan = false, popVlan = false;
Yi Tseng78f51f42017-02-02 13:54:58 -0800439 MacAddress srcMac;
440 MacAddress dstMac;
Charles Chan188ebf52015-12-23 00:15:11 -0800441 for (Instruction ins : treatment.allInstructions()) {
442 if (ins.type() == Instruction.Type.L2MODIFICATION) {
443 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
444 switch (l2ins.subtype()) {
445 case ETH_DST:
Charles Chan5270ed02016-01-30 23:22:37 -0800446 dstMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
447 outerTtb.setEthDst(dstMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800448 break;
449 case ETH_SRC:
Charles Chand0fd5dc2016-02-16 23:14:49 -0800450 srcMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
451 outerTtb.setEthSrc(srcMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800452 break;
453 case VLAN_ID:
454 vlanid = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
Charles Chanf9e98652016-09-07 16:54:23 -0700455 if (useSetVlanExtension) {
456 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
457 outerTtb.extension(ofdpaSetVlanVid, deviceId);
458 } else {
459 outerTtb.setVlanId(vlanid);
460 }
Charles Chan188ebf52015-12-23 00:15:11 -0800461 setVlan = true;
462 break;
463 case VLAN_POP:
464 innerTtb.popVlan();
465 popVlan = true;
466 break;
467 case DEC_MPLS_TTL:
468 case MPLS_LABEL:
469 case MPLS_POP:
470 case MPLS_PUSH:
471 case VLAN_PCP:
472 case VLAN_PUSH:
473 default:
474 break;
475 }
476 } else if (ins.type() == Instruction.Type.OUTPUT) {
477 portNum = ((Instructions.OutputInstruction) ins).port().toLong();
478 innerTtb.add(ins);
479 } else {
480 log.warn("Driver does not handle this type of TrafficTreatment"
481 + " instruction in nextObjectives: {}", ins.type());
482 }
483 }
484
485 if (vlanid == null && meta != null) {
486 // use metadata if available
Pier Ventre140a8942016-11-02 07:26:38 -0700487 Criterion vidCriterion = meta.getCriterion(VLAN_VID);
Charles Chan188ebf52015-12-23 00:15:11 -0800488 if (vidCriterion != null) {
489 vlanid = ((VlanIdCriterion) vidCriterion).vlanId();
490 }
491 // if vlan is not set, use the vlan in metadata for outerTtb
492 if (vlanid != null && !setVlan) {
Charles Chanf9e98652016-09-07 16:54:23 -0700493 if (useSetVlanExtension) {
494 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
495 outerTtb.extension(ofdpaSetVlanVid, deviceId);
496 } else {
497 outerTtb.setVlanId(vlanid);
498 }
Charles Chan188ebf52015-12-23 00:15:11 -0800499 }
500 }
501
502 if (vlanid == null) {
503 log.error("Driver cannot process an L2/L3 group chain without "
504 + "egress vlan information for dev: {} port:{}",
505 deviceId, portNum);
506 return null;
507 }
508
509 if (!setVlan && !popVlan) {
510 // untagged outgoing port
511 TrafficTreatment.Builder temp = DefaultTrafficTreatment.builder();
512 temp.popVlan();
513 innerTtb.build().allInstructions().forEach(i -> temp.add(i));
514 innerTtb = temp;
515 }
516
517 // assemble information for ofdpa l2interface group
Charles Chane849c192016-01-11 18:28:54 -0800518 int l2groupId = L2_INTERFACE_TYPE | (vlanid.toShort() << 16) | (int) portNum;
Saurav Das8be4e3a2016-03-11 17:19:07 -0800519 // a globally unique groupkey that is different for ports in the same device,
Charles Chan188ebf52015-12-23 00:15:11 -0800520 // but different for the same portnumber on different devices. Also different
521 // for the various group-types created out of the same next objective.
Charles Chane849c192016-01-11 18:28:54 -0800522 int l2gk = l2InterfaceGroupKey(deviceId, vlanid, portNum);
Charles Chan361154b2016-03-24 10:23:39 -0700523 final GroupKey l2groupkey = new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(l2gk));
Charles Chan188ebf52015-12-23 00:15:11 -0800524
525 // assemble information for outer group
526 GroupDescription outerGrpDesc = null;
527 if (mpls) {
528 // outer group is MPLSInteface
Saurav Das8be4e3a2016-03-11 17:19:07 -0800529 int mplsInterfaceIndex = getNextAvailableIndex();
530 int mplsgroupId = MPLS_INTERFACE_TYPE | (SUBTYPE_MASK & mplsInterfaceIndex);
531 final GroupKey mplsgroupkey = new DefaultGroupKey(
Charles Chan361154b2016-03-24 10:23:39 -0700532 Ofdpa2Pipeline.appKryo.serialize(mplsInterfaceIndex));
Yi Tsengfa394de2017-02-01 11:26:40 -0800533 outerTtb.group(new GroupId(l2groupId));
Charles Chan188ebf52015-12-23 00:15:11 -0800534 // create the mpls-interface group description to wait for the
535 // l2 interface group to be processed
536 GroupBucket mplsinterfaceGroupBucket =
537 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
538 outerGrpDesc = new DefaultGroupDescription(
539 deviceId,
540 GroupDescription.Type.INDIRECT,
541 new GroupBuckets(Collections.singletonList(
542 mplsinterfaceGroupBucket)),
543 mplsgroupkey,
544 mplsgroupId,
545 appId);
546 log.debug("Trying MPLS-Interface: device:{} gid:{} gkey:{} nextid:{}",
547 deviceId, Integer.toHexString(mplsgroupId),
548 mplsgroupkey, nextId);
549 } else {
550 // outer group is L3Unicast
Saurav Das8be4e3a2016-03-11 17:19:07 -0800551 int l3unicastIndex = getNextAvailableIndex();
552 int l3groupId = L3_UNICAST_TYPE | (TYPE_MASK & l3unicastIndex);
553 final GroupKey l3groupkey = new DefaultGroupKey(
Charles Chan361154b2016-03-24 10:23:39 -0700554 Ofdpa2Pipeline.appKryo.serialize(l3unicastIndex));
Yi Tsengfa394de2017-02-01 11:26:40 -0800555 outerTtb.group(new GroupId(l2groupId));
Charles Chan188ebf52015-12-23 00:15:11 -0800556 // create the l3unicast group description to wait for the
557 // l2 interface group to be processed
558 GroupBucket l3unicastGroupBucket =
559 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
560 outerGrpDesc = new DefaultGroupDescription(
561 deviceId,
562 GroupDescription.Type.INDIRECT,
563 new GroupBuckets(Collections.singletonList(
564 l3unicastGroupBucket)),
565 l3groupkey,
566 l3groupId,
567 appId);
568 log.debug("Trying L3Unicast: device:{} gid:{} gkey:{} nextid:{}",
569 deviceId, Integer.toHexString(l3groupId),
570 l3groupkey, nextId);
571 }
572
573 // store l2groupkey with the groupChainElem for the outer-group that depends on it
574 GroupChainElem gce = new GroupChainElem(outerGrpDesc, 1, false);
575 updatePendingGroups(l2groupkey, gce);
576
577 // create group description for the inner l2interfacegroup
Charles Chan5b9df8d2016-03-28 22:21:40 -0700578 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800579 DefaultGroupBucket.createIndirectGroupBucket(innerTtb.build());
580 GroupDescription l2groupDescription =
581 new DefaultGroupDescription(
582 deviceId,
583 GroupDescription.Type.INDIRECT,
584 new GroupBuckets(Collections.singletonList(
Charles Chan5b9df8d2016-03-28 22:21:40 -0700585 l2InterfaceGroupBucket)),
Charles Chan188ebf52015-12-23 00:15:11 -0800586 l2groupkey,
587 l2groupId,
588 appId);
589 log.debug("Trying L2Interface: device:{} gid:{} gkey:{} nextId:{}",
590 deviceId, Integer.toHexString(l2groupId),
591 l2groupkey, nextId);
592 return new GroupInfo(l2groupDescription, outerGrpDesc);
593
594 }
595
596 /**
597 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
598 * a chain of groups. The broadcast Next Objective passed in by the application
599 * has to be broken up into a group chain comprising of an
Saurav Das1a129a02016-11-18 15:21:57 -0800600 * L2 Flood group or L3 Multicast group, whose buckets point to L2 Interface groups.
Charles Chan188ebf52015-12-23 00:15:11 -0800601 *
602 * @param nextObj the nextObjective of type BROADCAST
603 */
604 private void processBroadcastNextObjective(NextObjective nextObj) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700605 VlanId assignedVlan = Ofdpa2Pipeline.readVlanFromSelector(nextObj.meta());
606 if (assignedVlan == null) {
607 log.warn("VLAN ID required by broadcast next obj is missing. Abort.");
608 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800609 return;
610 }
Charles Chan188ebf52015-12-23 00:15:11 -0800611
Charles Chan5b9df8d2016-03-28 22:21:40 -0700612 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
613
614 IpPrefix ipDst = Ofdpa2Pipeline.readIpDstFromSelector(nextObj.meta());
615 if (ipDst != null) {
616 if (ipDst.isMulticast()) {
617 createL3MulticastGroup(nextObj, assignedVlan, groupInfos);
618 } else {
619 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
620 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
621 return;
622 }
623 } else {
624 createL2FloodGroup(nextObj, assignedVlan, groupInfos);
625 }
626 }
627
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700628 private List<GroupInfo> prepareL2InterfaceGroup(NextObjective nextObj,
629 VlanId assignedVlan) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700630 ImmutableList.Builder<GroupInfo> groupInfoBuilder = ImmutableList.builder();
631
632 // break up broadcast next objective to multiple groups
633 Collection<TrafficTreatment> buckets = nextObj.next();
634
Charles Chan188ebf52015-12-23 00:15:11 -0800635 // each treatment is converted to an L2 interface group
Charles Chan188ebf52015-12-23 00:15:11 -0800636 for (TrafficTreatment treatment : buckets) {
637 TrafficTreatment.Builder newTreatment = DefaultTrafficTreatment.builder();
638 PortNumber portNum = null;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700639 VlanId egressVlan = null;
Charles Chan188ebf52015-12-23 00:15:11 -0800640 // ensure that the only allowed treatments are pop-vlan and output
641 for (Instruction ins : treatment.allInstructions()) {
642 if (ins.type() == Instruction.Type.L2MODIFICATION) {
643 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
644 switch (l2ins.subtype()) {
645 case VLAN_POP:
646 newTreatment.add(l2ins);
647 break;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700648 case VLAN_ID:
649 egressVlan = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
650 break;
Charles Chan188ebf52015-12-23 00:15:11 -0800651 default:
652 log.debug("action {} not permitted for broadcast nextObj",
653 l2ins.subtype());
654 break;
655 }
656 } else if (ins.type() == Instruction.Type.OUTPUT) {
657 portNum = ((Instructions.OutputInstruction) ins).port();
658 newTreatment.add(ins);
659 } else {
Charles Chane849c192016-01-11 18:28:54 -0800660 log.debug("TrafficTreatment of type {} not permitted in " +
661 " broadcast nextObjective", ins.type());
Charles Chan188ebf52015-12-23 00:15:11 -0800662 }
663 }
664
Charles Chan188ebf52015-12-23 00:15:11 -0800665 // assemble info for l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700666 VlanId l2InterfaceGroupVlan =
667 (egressVlan != null && !assignedVlan.equals(egressVlan)) ?
668 egressVlan : assignedVlan;
669 int l2gk = l2InterfaceGroupKey(deviceId, l2InterfaceGroupVlan, portNum.toLong());
670 final GroupKey l2InterfaceGroupKey =
671 new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(l2gk));
Charles Chan372b63e2017-02-07 12:10:53 -0800672 int l2InterfaceGroupId = L2_INTERFACE_TYPE |
673 ((l2InterfaceGroupVlan.toShort() & THREE_BIT_MASK) << PORT_LEN) |
674 ((int) portNum.toLong() & FOUR_BIT_MASK);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700675 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800676 DefaultGroupBucket.createIndirectGroupBucket(newTreatment.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700677 GroupDescription l2InterfaceGroupDescription =
Charles Chan188ebf52015-12-23 00:15:11 -0800678 new DefaultGroupDescription(
679 deviceId,
680 GroupDescription.Type.INDIRECT,
681 new GroupBuckets(Collections.singletonList(
Charles Chan5b9df8d2016-03-28 22:21:40 -0700682 l2InterfaceGroupBucket)),
683 l2InterfaceGroupKey,
684 l2InterfaceGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800685 nextObj.appId());
686 log.debug("Trying L2-Interface: device:{} gid:{} gkey:{} nextid:{}",
Charles Chan5b9df8d2016-03-28 22:21:40 -0700687 deviceId, Integer.toHexString(l2InterfaceGroupId),
688 l2InterfaceGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -0800689
Charles Chan5b9df8d2016-03-28 22:21:40 -0700690 groupInfoBuilder.add(new GroupInfo(l2InterfaceGroupDescription,
691 l2InterfaceGroupDescription));
Charles Chan188ebf52015-12-23 00:15:11 -0800692 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700693 return groupInfoBuilder.build();
694 }
Charles Chan188ebf52015-12-23 00:15:11 -0800695
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700696 private void createL2FloodGroup(NextObjective nextObj, VlanId vlanId,
697 List<GroupInfo> groupInfos) {
698 // assemble info for l2 flood group. Since there can be only one flood
699 // group for a vlan, its index is always the same - 0
Saurav Das0fd79d92016-03-07 10:58:36 -0800700 Integer l2floodgroupId = L2_FLOOD_TYPE | (vlanId.toShort() << 16);
Yi Tseng117952d2017-02-28 10:58:23 -0800701 int l2floodgk = l2FloodGroupKey(vlanId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700702 final GroupKey l2floodgroupkey =
703 new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(l2floodgk));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700704
Charles Chan188ebf52015-12-23 00:15:11 -0800705 // collection of group buckets pointing to all the l2 interface groups
Yi Tseng78f51f42017-02-02 13:54:58 -0800706 List<GroupBucket> l2floodBuckets =
707 generateNextGroupBuckets(groupInfos, ALL);
Charles Chan188ebf52015-12-23 00:15:11 -0800708 // create the l2flood group-description to wait for all the
709 // l2interface groups to be processed
710 GroupDescription l2floodGroupDescription =
711 new DefaultGroupDescription(
712 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800713 ALL,
Charles Chan188ebf52015-12-23 00:15:11 -0800714 new GroupBuckets(l2floodBuckets),
715 l2floodgroupkey,
716 l2floodgroupId,
717 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800718 log.debug("Trying L2-Flood: device:{} gid:{} gkey:{} nextid:{}",
719 deviceId, Integer.toHexString(l2floodgroupId),
720 l2floodgroupkey, nextObj.id());
721
Charles Chan5b9df8d2016-03-28 22:21:40 -0700722 // Put all dependency information into allGroupKeys
723 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
724 groupInfos.forEach(groupInfo -> {
725 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
726 // In this case we should have L2 interface group only
727 gkeyChain.addFirst(groupInfo.nextGroupDesc.appCookie());
728 gkeyChain.addFirst(l2floodgroupkey);
729 allGroupKeys.add(gkeyChain);
730 });
Charles Chan188ebf52015-12-23 00:15:11 -0800731
Charles Chan5b9df8d2016-03-28 22:21:40 -0700732 // Point the next objective to this group
733 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Charles Chan188ebf52015-12-23 00:15:11 -0800734 updatePendingNextObjective(l2floodgroupkey, ofdpaGrp);
735
Charles Chan5b9df8d2016-03-28 22:21:40 -0700736 GroupChainElem gce = new GroupChainElem(l2floodGroupDescription,
737 groupInfos.size(), false);
738 groupInfos.forEach(groupInfo -> {
739 // Point this group to the next group
740 updatePendingGroups(groupInfo.nextGroupDesc.appCookie(), gce);
741 // Start installing the inner-most group
742 groupService.addGroup(groupInfo.innerMostGroupDesc);
743 });
744 }
745
Yi Tseng117952d2017-02-28 10:58:23 -0800746 private int l2FloodGroupKey(VlanId vlanId) {
747 int hash = Objects.hash(deviceId, vlanId);
748 return L2_FLOOD_TYPE | TYPE_MASK & hash;
749 }
750
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700751 private void createL3MulticastGroup(NextObjective nextObj, VlanId vlanId,
752 List<GroupInfo> groupInfos) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700753 List<GroupBucket> l3McastBuckets = new ArrayList<>();
754 groupInfos.forEach(groupInfo -> {
755 // Points to L3 interface group if there is one.
756 // Otherwise points to L2 interface group directly.
757 GroupDescription nextGroupDesc = (groupInfo.nextGroupDesc != null) ?
758 groupInfo.nextGroupDesc : groupInfo.innerMostGroupDesc;
759 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Yi Tsengfa394de2017-02-01 11:26:40 -0800760 ttb.group(new GroupId(nextGroupDesc.givenGroupId()));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700761 GroupBucket abucket = DefaultGroupBucket.createAllGroupBucket(ttb.build());
762 l3McastBuckets.add(abucket);
763 });
764
765 int l3MulticastIndex = getNextAvailableIndex();
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700766 int l3MulticastGroupId = L3_MULTICAST_TYPE |
767 vlanId.toShort() << 16 | (TYPE_VLAN_MASK & l3MulticastIndex);
768 final GroupKey l3MulticastGroupKey =
769 new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(l3MulticastIndex));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700770
771 GroupDescription l3MulticastGroupDesc = new DefaultGroupDescription(deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800772 ALL,
Charles Chan5b9df8d2016-03-28 22:21:40 -0700773 new GroupBuckets(l3McastBuckets),
774 l3MulticastGroupKey,
775 l3MulticastGroupId,
776 nextObj.appId());
777
778 // Put all dependency information into allGroupKeys
779 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
780 groupInfos.forEach(groupInfo -> {
781 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
782 gkeyChain.addFirst(groupInfo.innerMostGroupDesc.appCookie());
783 // Add L3 interface group to the chain if there is one.
784 if (!groupInfo.nextGroupDesc.equals(groupInfo.innerMostGroupDesc)) {
785 gkeyChain.addFirst(groupInfo.nextGroupDesc.appCookie());
786 }
787 gkeyChain.addFirst(l3MulticastGroupKey);
788 allGroupKeys.add(gkeyChain);
789 });
790
791 // Point the next objective to this group
792 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
793 updatePendingNextObjective(l3MulticastGroupKey, ofdpaGrp);
794
795 GroupChainElem outerGce = new GroupChainElem(l3MulticastGroupDesc,
796 groupInfos.size(), false);
797 groupInfos.forEach(groupInfo -> {
798 // Point this group (L3 multicast) to the next group
799 updatePendingGroups(groupInfo.nextGroupDesc.appCookie(), outerGce);
800
801 // Point next group to inner-most group, if any
802 if (!groupInfo.nextGroupDesc.equals(groupInfo.innerMostGroupDesc)) {
803 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc,
804 1, false);
805 updatePendingGroups(groupInfo.innerMostGroupDesc.appCookie(), innerGce);
806 }
807
808 // Start installing the inner-most group
809 groupService.addGroup(groupInfo.innerMostGroupDesc);
810 });
Charles Chan188ebf52015-12-23 00:15:11 -0800811 }
812
Charles Chan188ebf52015-12-23 00:15:11 -0800813 /**
814 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
815 * a chain of groups. The hashed Next Objective passed in by the application
816 * has to be broken up into a group chain comprising of an
817 * L3 ECMP group as the top level group. Buckets of this group can point
818 * to a variety of groups in a group chain, depending on the whether
819 * MPLS labels are being pushed or not.
820 * <p>
821 * NOTE: We do not create MPLS ECMP groups as they are unimplemented in
822 * OF-DPA 2.0 (even though it is in the spec). Therefore we do not
823 * check the nextObjective meta to see what is matching before being
824 * sent to this nextObjective.
825 *
826 * @param nextObj the nextObjective of type HASHED
827 */
Pier Ventre140a8942016-11-02 07:26:38 -0700828 protected void processHashedNextObjective(NextObjective nextObj) {
Charles Chan188ebf52015-12-23 00:15:11 -0800829 // storage for all group keys in the chain of groups created
830 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
831 List<GroupInfo> unsentGroups = new ArrayList<>();
832 createHashBucketChains(nextObj, allGroupKeys, unsentGroups);
833
834 // now we can create the outermost L3 ECMP group
835 List<GroupBucket> l3ecmpGroupBuckets = new ArrayList<>();
836 for (GroupInfo gi : unsentGroups) {
837 // create ECMP bucket to point to the outer group
838 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Yi Tsengfa394de2017-02-01 11:26:40 -0800839 ttb.group(new GroupId(gi.nextGroupDesc.givenGroupId()));
Charles Chan188ebf52015-12-23 00:15:11 -0800840 GroupBucket sbucket = DefaultGroupBucket
841 .createSelectGroupBucket(ttb.build());
842 l3ecmpGroupBuckets.add(sbucket);
843 }
Saurav Das8be4e3a2016-03-11 17:19:07 -0800844 int l3ecmpIndex = getNextAvailableIndex();
845 int l3ecmpGroupId = L3_ECMP_TYPE | (TYPE_MASK & l3ecmpIndex);
846 GroupKey l3ecmpGroupKey = new DefaultGroupKey(
Charles Chan361154b2016-03-24 10:23:39 -0700847 Ofdpa2Pipeline.appKryo.serialize(l3ecmpIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800848 GroupDescription l3ecmpGroupDesc =
849 new DefaultGroupDescription(
850 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800851 SELECT,
Charles Chan188ebf52015-12-23 00:15:11 -0800852 new GroupBuckets(l3ecmpGroupBuckets),
853 l3ecmpGroupKey,
854 l3ecmpGroupId,
855 nextObj.appId());
856 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
857 l3ecmpGroupBuckets.size(),
858 false);
859
860 // create objects for local and distributed storage
861 allGroupKeys.forEach(gkeyChain -> gkeyChain.addFirst(l3ecmpGroupKey));
862 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
863
864 // store l3ecmpGroupKey with the ofdpaGroupChain for the nextObjective
865 // that depends on it
866 updatePendingNextObjective(l3ecmpGroupKey, ofdpaGrp);
867
868 log.debug("Trying L3ECMP: device:{} gid:{} gkey:{} nextId:{}",
869 deviceId, Integer.toHexString(l3ecmpGroupId),
870 l3ecmpGroupKey, nextObj.id());
871 // finally we are ready to send the innermost groups
872 for (GroupInfo gi : unsentGroups) {
873 log.debug("Sending innermost group {} in group chain on device {} ",
Charles Chan5b9df8d2016-03-28 22:21:40 -0700874 Integer.toHexString(gi.innerMostGroupDesc.givenGroupId()), deviceId);
875 updatePendingGroups(gi.nextGroupDesc.appCookie(), l3ecmpGce);
876 groupService.addGroup(gi.innerMostGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800877 }
878
879 }
880
881 /**
882 * Creates group chains for all buckets in a hashed group, and stores the
883 * GroupInfos and GroupKeys for all the groups in the lists passed in, which
884 * should be empty.
885 * <p>
886 * Does not create the top level ECMP group. Does not actually send the
887 * groups to the groupService.
888 *
889 * @param nextObj the Next Objective with buckets that need to be converted
890 * to group chains
891 * @param allGroupKeys a list to store groupKey for each bucket-group-chain
892 * @param unsentGroups a list to store GroupInfo for each bucket-group-chain
893 */
Pier Ventre140a8942016-11-02 07:26:38 -0700894 protected void createHashBucketChains(NextObjective nextObj,
Saurav Das8be4e3a2016-03-11 17:19:07 -0800895 List<Deque<GroupKey>> allGroupKeys,
896 List<GroupInfo> unsentGroups) {
Charles Chan188ebf52015-12-23 00:15:11 -0800897 // break up hashed next objective to multiple groups
898 Collection<TrafficTreatment> buckets = nextObj.next();
899
900 for (TrafficTreatment bucket : buckets) {
901 //figure out how many labels are pushed in each bucket
902 int labelsPushed = 0;
903 MplsLabel innermostLabel = null;
904 for (Instruction ins : bucket.allInstructions()) {
905 if (ins.type() == Instruction.Type.L2MODIFICATION) {
906 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
907 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_PUSH) {
908 labelsPushed++;
909 }
910 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) {
911 if (innermostLabel == null) {
Ray Milkey125572b2016-02-22 16:48:17 -0800912 innermostLabel = ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label();
Charles Chan188ebf52015-12-23 00:15:11 -0800913 }
914 }
915 }
916 }
917
918 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
919 // XXX we only deal with 0 and 1 label push right now
920 if (labelsPushed == 0) {
Pier Ventre140a8942016-11-02 07:26:38 -0700921 GroupInfo nolabelGroupInfo;
922 TrafficSelector metaSelector = nextObj.meta();
923 if (metaSelector != null) {
924 if (isNotMplsBos(metaSelector)) {
925 nolabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
926 nextObj.appId(), true,
927 nextObj.meta());
928 } else {
929 nolabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
930 nextObj.appId(), false,
931 nextObj.meta());
932 }
933 } else {
934 nolabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
935 nextObj.appId(), false,
936 nextObj.meta());
937 }
Charles Chan188ebf52015-12-23 00:15:11 -0800938 if (nolabelGroupInfo == null) {
939 log.error("Could not process nextObj={} in dev:{}",
940 nextObj.id(), deviceId);
941 return;
942 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700943 gkeyChain.addFirst(nolabelGroupInfo.innerMostGroupDesc.appCookie());
944 gkeyChain.addFirst(nolabelGroupInfo.nextGroupDesc.appCookie());
Charles Chan188ebf52015-12-23 00:15:11 -0800945
946 // we can't send the inner group description yet, as we have to
947 // create the dependent ECMP group first. So we store..
948 unsentGroups.add(nolabelGroupInfo);
949
950 } else if (labelsPushed == 1) {
951 GroupInfo onelabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
952 nextObj.appId(), true,
953 nextObj.meta());
954 if (onelabelGroupInfo == null) {
955 log.error("Could not process nextObj={} in dev:{}",
956 nextObj.id(), deviceId);
957 return;
958 }
959 // we need to add another group to this chain - the L3VPN group
960 TrafficTreatment.Builder l3vpnTtb = DefaultTrafficTreatment.builder();
Charles Chan0f43e472017-02-14 14:00:16 -0800961 if (requireVlanPopBeforeMplsPush()) {
962 l3vpnTtb.popVlan();
963 }
Charles Chan188ebf52015-12-23 00:15:11 -0800964 l3vpnTtb.pushMpls()
965 .setMpls(innermostLabel)
Yi Tsengfa394de2017-02-01 11:26:40 -0800966 .group(new GroupId(onelabelGroupInfo.nextGroupDesc.givenGroupId()));
Charles Chan40132b32017-01-22 00:19:37 -0800967 if (supportCopyTtl()) {
968 l3vpnTtb.copyTtlOut();
969 }
970 if (supportSetMplsBos()) {
971 l3vpnTtb.setMplsBos(true);
972 }
Charles Chan0f43e472017-02-14 14:00:16 -0800973 if (requireVlanPopBeforeMplsPush()) {
974 l3vpnTtb.pushVlan().setVlanId(VlanId.vlanId(VlanId.RESERVED));
975 }
Charles Chan40132b32017-01-22 00:19:37 -0800976
Charles Chan188ebf52015-12-23 00:15:11 -0800977 GroupBucket l3vpnGrpBkt =
978 DefaultGroupBucket.createIndirectGroupBucket(l3vpnTtb.build());
Saurav Das8be4e3a2016-03-11 17:19:07 -0800979 int l3vpnIndex = getNextAvailableIndex();
980 int l3vpngroupId = MPLS_L3VPN_SUBTYPE | (SUBTYPE_MASK & l3vpnIndex);
981 GroupKey l3vpngroupkey = new DefaultGroupKey(
Charles Chan361154b2016-03-24 10:23:39 -0700982 Ofdpa2Pipeline.appKryo.serialize(l3vpnIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800983 GroupDescription l3vpnGroupDesc =
984 new DefaultGroupDescription(
985 deviceId,
986 GroupDescription.Type.INDIRECT,
987 new GroupBuckets(Collections.singletonList(
988 l3vpnGrpBkt)),
989 l3vpngroupkey,
990 l3vpngroupId,
991 nextObj.appId());
992 GroupChainElem l3vpnGce = new GroupChainElem(l3vpnGroupDesc, 1, false);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700993 updatePendingGroups(onelabelGroupInfo.nextGroupDesc.appCookie(), l3vpnGce);
Charles Chan188ebf52015-12-23 00:15:11 -0800994
Charles Chan5b9df8d2016-03-28 22:21:40 -0700995 gkeyChain.addFirst(onelabelGroupInfo.innerMostGroupDesc.appCookie());
996 gkeyChain.addFirst(onelabelGroupInfo.nextGroupDesc.appCookie());
Charles Chan188ebf52015-12-23 00:15:11 -0800997 gkeyChain.addFirst(l3vpngroupkey);
998
999 //now we can replace the outerGrpDesc with the one we just created
Charles Chan5b9df8d2016-03-28 22:21:40 -07001000 onelabelGroupInfo.nextGroupDesc = l3vpnGroupDesc;
Charles Chan188ebf52015-12-23 00:15:11 -08001001
1002 // we can't send the innermost group yet, as we have to create
1003 // the dependent ECMP group first. So we store ...
1004 unsentGroups.add(onelabelGroupInfo);
1005
1006 log.debug("Trying L3VPN: device:{} gid:{} gkey:{} nextId:{}",
1007 deviceId, Integer.toHexString(l3vpngroupId),
1008 l3vpngroupkey, nextObj.id());
1009
1010 } else {
1011 log.warn("Driver currently does not handle more than 1 MPLS "
1012 + "labels. Not processing nextObjective {}", nextObj.id());
1013 return;
1014 }
1015
1016 // all groups in this chain
1017 allGroupKeys.add(gkeyChain);
1018 }
1019 }
1020
Pier Ventre42287df2016-11-09 14:17:26 -08001021 /**
1022 * Processes the pseudo wire related next objective.
1023 * This procedure try to reuse the mpls label groups,
1024 * the mpls interface group and the l2 interface group.
1025 *
1026 * @param nextObjective the objective to process.
1027 */
1028 protected void processPwNextObjective(NextObjective nextObjective) {
1029 log.warn("Pseudo wire extensions are not support for the OFDPA 2.0 {}", nextObjective.id());
1030 return;
1031 }
1032
Saurav Das8be4e3a2016-03-11 17:19:07 -08001033 //////////////////////////////////////
1034 // Group Editing
1035 //////////////////////////////////////
1036
Charles Chan188ebf52015-12-23 00:15:11 -08001037 /**
1038 * Adds a bucket to the top level group of a group-chain, and creates the chain.
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001039 * Ensures that bucket being added is not a duplicate, by checking existing
1040 * buckets for the same outport.
Charles Chan188ebf52015-12-23 00:15:11 -08001041 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001042 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -08001043 * @param next the representation of the existing group-chain for this next objective
1044 */
1045 protected void addBucketToGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001046 if (nextObjective.type() != NextObjective.Type.HASHED &&
1047 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001048 log.warn("AddBuckets not applied to nextType:{} in dev:{} for next:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001049 nextObjective.type(), deviceId, nextObjective.id());
Saurav Das1a129a02016-11-18 15:21:57 -08001050 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001051 return;
1052 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001053
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001054 // first check to see if bucket being added is not a duplicate of an
1055 // existing bucket. If it is for an existing outport, then its a duplicate.
Yi Tseng78f51f42017-02-02 13:54:58 -08001056 Set<TrafficTreatment> duplicateBuckets = Sets.newHashSet();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001057 List<Deque<GroupKey>> allActiveKeys = Ofdpa2Pipeline.appKryo.deserialize(next.data());
Yi Tseng78f51f42017-02-02 13:54:58 -08001058 Set<PortNumber> existingPorts = getExistingOutputPorts(allActiveKeys);
Yi Tseng47f82dc2017-03-05 22:48:39 -08001059 Set<TrafficTreatment> nonDuplicateBuckets = Sets.newHashSet();
1060 NextObjective objectiveToAdd;
Yi Tseng78f51f42017-02-02 13:54:58 -08001061
1062 nextObjective.next().forEach(trafficTreatment -> {
1063 PortNumber portNumber = readOutPortFromTreatment(trafficTreatment);
1064
1065 if (portNumber == null) {
1066 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001067 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001068
1069 if (existingPorts.contains(portNumber)) {
1070 duplicateBuckets.add(trafficTreatment);
Yi Tseng47f82dc2017-03-05 22:48:39 -08001071 } else {
1072 nonDuplicateBuckets.add(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001073 }
1074 });
1075
1076 if (!duplicateBuckets.isEmpty()) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001077 log.debug("Some buckets {} already exists in next id {}, duplicate buckets will be ignored.",
Yi Tseng78f51f42017-02-02 13:54:58 -08001078 duplicateBuckets, nextObjective.id());
Yi Tseng47f82dc2017-03-05 22:48:39 -08001079
1080 // new next objective with new treatments
1081 NextObjective.Builder builder = DefaultNextObjective.builder()
1082 .withType(nextObjective.type())
1083 .withId(nextObjective.id())
1084 .withMeta(nextObjective.meta())
1085 .fromApp(nextObjective.appId());
1086 nonDuplicateBuckets.forEach(builder::addTreatment);
1087
1088 ObjectiveContext context = nextObjective.context().orElse(null);
1089 objectiveToAdd = builder.addToExisting(context);
1090 } else {
1091 objectiveToAdd = nextObjective;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001092 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001093
Saurav Das1a129a02016-11-18 15:21:57 -08001094 if (nextObjective.type() == NextObjective.Type.HASHED) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001095 addBucketToHashGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001096 } else if (nextObjective.type() == NextObjective.Type.BROADCAST) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001097 addBucketToBroadcastGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001098 }
1099 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001100
Yi Tseng78f51f42017-02-02 13:54:58 -08001101 private Set<PortNumber> getExistingOutputPorts(List<Deque<GroupKey>> allActiveKeys) {
1102 Set<PortNumber> existingPorts = Sets.newHashSet();
1103
1104 allActiveKeys.forEach(keyChain -> {
1105 GroupKey ifaceGroupKey = keyChain.peekLast();
1106 Group ifaceGroup = groupService.getGroup(deviceId, ifaceGroupKey);
1107 if (ifaceGroup != null && !ifaceGroup.buckets().buckets().isEmpty()) {
1108 ifaceGroup.buckets().buckets().forEach(bucket -> {
1109 PortNumber portNumber = readOutPortFromTreatment(bucket.treatment());
1110
1111 if (portNumber != null) {
1112 existingPorts.add(portNumber);
1113 }
1114 });
1115 }
1116 });
1117
1118 return existingPorts;
1119 }
1120
Saurav Das1a129a02016-11-18 15:21:57 -08001121 private void addBucketToHashGroup(NextObjective nextObjective,
Yi Tseng78f51f42017-02-02 13:54:58 -08001122 List<Deque<GroupKey>> allActiveKeys) {
Charles Chan188ebf52015-12-23 00:15:11 -08001123 // storage for all group keys in the chain of groups created
1124 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
1125 List<GroupInfo> unsentGroups = new ArrayList<>();
Yi Tseng78f51f42017-02-02 13:54:58 -08001126 List<GroupBucket> newBuckets = Lists.newArrayList();
Charles Chan188ebf52015-12-23 00:15:11 -08001127 createHashBucketChains(nextObjective, allGroupKeys, unsentGroups);
1128
Yi Tseng78f51f42017-02-02 13:54:58 -08001129 // now we can create the buckets to add to the outermost L3 ECMP group
1130 newBuckets = generateNextGroupBuckets(unsentGroups, SELECT);
Charles Chan188ebf52015-12-23 00:15:11 -08001131
Saurav Das1a129a02016-11-18 15:21:57 -08001132 // retrieve the original L3 ECMP group
1133 Group l3ecmpGroup = retrieveTopLevelGroup(allActiveKeys, nextObjective.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001134 if (l3ecmpGroup == null) {
Saurav Das1a129a02016-11-18 15:21:57 -08001135 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.GROUPMISSING);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001136 return;
1137 }
Saurav Das1a129a02016-11-18 15:21:57 -08001138 GroupKey l3ecmpGroupKey = l3ecmpGroup.appCookie();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001139 int l3ecmpGroupId = l3ecmpGroup.id().id();
Charles Chan188ebf52015-12-23 00:15:11 -08001140
1141 // Although GroupDescriptions are not necessary for adding buckets to
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001142 // existing groups, we still use one in the GroupChainElem. When the latter is
Charles Chan188ebf52015-12-23 00:15:11 -08001143 // processed, the info will be extracted for the bucketAdd call to groupService
1144 GroupDescription l3ecmpGroupDesc =
1145 new DefaultGroupDescription(
1146 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -08001147 SELECT,
1148 new GroupBuckets(newBuckets),
Charles Chan188ebf52015-12-23 00:15:11 -08001149 l3ecmpGroupKey,
1150 l3ecmpGroupId,
1151 nextObjective.appId());
Yi Tseng78f51f42017-02-02 13:54:58 -08001152 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
1153 unsentGroups.size(),
1154 true);
Charles Chan188ebf52015-12-23 00:15:11 -08001155
1156 // update original NextGroup with new bucket-chain
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001157 // If active keys shows only the top-level group without a chain of groups,
1158 // then it represents an empty group. Update by replacing empty chain.
Charles Chan188ebf52015-12-23 00:15:11 -08001159 Deque<GroupKey> newBucketChain = allGroupKeys.get(0);
1160 newBucketChain.addFirst(l3ecmpGroupKey);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001161 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1162 allActiveKeys.clear();
1163 }
1164 allActiveKeys.add(newBucketChain);
1165 updatePendingNextObjective(l3ecmpGroupKey,
1166 new OfdpaNextGroup(allActiveKeys, nextObjective));
Yi Tseng78f51f42017-02-02 13:54:58 -08001167
Charles Chan188ebf52015-12-23 00:15:11 -08001168 log.debug("Adding to L3ECMP: device:{} gid:{} gkey:{} nextId:{}",
1169 deviceId, Integer.toHexString(l3ecmpGroupId),
1170 l3ecmpGroupKey, nextObjective.id());
Yi Tseng78f51f42017-02-02 13:54:58 -08001171
1172 unsentGroups.forEach(groupInfo -> {
1173 // send the innermost group
1174 log.debug("Sending innermost group {} in group chain on device {} ",
1175 Integer.toHexString(groupInfo.innerMostGroupDesc.givenGroupId()), deviceId);
1176 updatePendingGroups(groupInfo.nextGroupDesc.appCookie(), l3ecmpGce);
1177 groupService.addGroup(groupInfo.innerMostGroupDesc);
1178 });
1179
Saurav Das1a129a02016-11-18 15:21:57 -08001180 }
Charles Chan188ebf52015-12-23 00:15:11 -08001181
Saurav Das1a129a02016-11-18 15:21:57 -08001182 private void addBucketToBroadcastGroup(NextObjective nextObj,
Yi Tseng78f51f42017-02-02 13:54:58 -08001183 List<Deque<GroupKey>> allActiveKeys) {
Saurav Das1a129a02016-11-18 15:21:57 -08001184 VlanId assignedVlan = Ofdpa2Pipeline.readVlanFromSelector(nextObj.meta());
1185 if (assignedVlan == null) {
1186 log.warn("VLAN ID required by broadcast next obj is missing. "
1187 + "Aborting add bucket to broadcast group for next:{} in dev:{}",
1188 nextObj.id(), deviceId);
1189 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
1190 return;
1191 }
1192
1193 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
1194
1195 IpPrefix ipDst = Ofdpa2Pipeline.readIpDstFromSelector(nextObj.meta());
1196 if (ipDst != null) {
1197 if (ipDst.isMulticast()) {
1198 addBucketToL3MulticastGroup(nextObj, allActiveKeys,
Yi Tseng78f51f42017-02-02 13:54:58 -08001199 groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001200 } else {
1201 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
1202 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
1203 return;
1204 }
1205 } else {
1206 addBucketToL2FloodGroup(nextObj, allActiveKeys,
Yi Tseng78f51f42017-02-02 13:54:58 -08001207 groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001208 }
1209 }
1210
1211 private void addBucketToL2FloodGroup(NextObjective nextObj,
1212 List<Deque<GroupKey>> allActiveKeys,
1213 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001214 VlanId assignedVlan) {
1215 Group l2FloodGroup = retrieveTopLevelGroup(allActiveKeys, nextObj.id());
1216
1217 if (l2FloodGroup == null) {
1218 log.warn("Can't find L2 flood group while adding bucket to it. NextObj = {}",
1219 nextObj);
Saurav Das1a129a02016-11-18 15:21:57 -08001220 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.GROUPMISSING);
1221 return;
1222 }
Saurav Das1a129a02016-11-18 15:21:57 -08001223
Yi Tseng78f51f42017-02-02 13:54:58 -08001224 GroupKey l2floodGroupKey = l2FloodGroup.appCookie();
1225 int l2floodGroupId = l2FloodGroup.id().id();
1226 List<GroupBucket> newBuckets = generateNextGroupBuckets(groupInfos, ALL);
1227
1228 GroupDescription l2FloodGroupDescription =
Saurav Das1a129a02016-11-18 15:21:57 -08001229 new DefaultGroupDescription(
1230 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -08001231 ALL,
1232 new GroupBuckets(newBuckets),
Saurav Das1a129a02016-11-18 15:21:57 -08001233 l2floodGroupKey,
1234 l2floodGroupId,
1235 nextObj.appId());
Saurav Das1a129a02016-11-18 15:21:57 -08001236
Yi Tseng78f51f42017-02-02 13:54:58 -08001237 GroupChainElem l2FloodGroupChainElement =
1238 new GroupChainElem(l2FloodGroupDescription,
1239 groupInfos.size(),
1240 true);
1241
Saurav Das1a129a02016-11-18 15:21:57 -08001242 updatePendingNextObjective(l2floodGroupKey,
1243 new OfdpaNextGroup(allActiveKeys, nextObj));
Yi Tseng78f51f42017-02-02 13:54:58 -08001244
1245 //ensure assignedVlan applies to the chosen group
1246 VlanId floodGroupVlan = extractVlanIdFromGroupId(l2floodGroupId);
1247
1248 if (!floodGroupVlan.equals(assignedVlan)) {
1249 log.warn("VLAN ID {} does not match Flood group {} to which bucket is "
1250 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1251 Integer.toHexString(l2floodGroupId), nextObj.id(), deviceId);
1252 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
1253 return;
1254 }
1255
1256 groupInfos.forEach(groupInfo -> {
1257 // update original NextGroup with new bucket-chain
1258 // If active keys shows only the top-level group without a chain of groups,
1259 // then it represents an empty group. Update by replacing empty chain.
1260 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
1261 newBucketChain.addFirst(groupInfo.nextGroupDesc.appCookie());
1262 newBucketChain.addFirst(l2floodGroupKey);
1263 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1264 allActiveKeys.clear();
1265 }
1266 allActiveKeys.add(newBucketChain);
1267
1268 log.debug("Adding to L2FLOOD: device:{} gid:{} gkey:{} nextId:{}",
1269 deviceId, Integer.toHexString(l2floodGroupId),
1270 l2floodGroupKey, nextObj.id());
1271 // send the innermost group
1272 log.debug("Sending innermost group {} in group chain on device {} ",
1273 Integer.toHexString(groupInfo.innerMostGroupDesc.givenGroupId()),
1274 deviceId);
1275
1276 updatePendingGroups(groupInfo.nextGroupDesc.appCookie(), l2FloodGroupChainElement);
1277
1278 DeviceId innerMostGroupDevice = groupInfo.innerMostGroupDesc.deviceId();
1279 GroupKey innerMostGroupKey = groupInfo.innerMostGroupDesc.appCookie();
1280 Group existsL2IGroup = groupService.getGroup(innerMostGroupDevice, innerMostGroupKey);
1281
1282 if (existsL2IGroup != null) {
1283 // group already exist
1284 processPendingAddGroupsOrNextObjs(innerMostGroupKey, true);
1285 } else {
1286 groupService.addGroup(groupInfo.innerMostGroupDesc);
1287 }
1288
1289 });
1290 }
1291
1292 private VlanId extractVlanIdFromGroupId(int groupId) {
1293 // Extract the 9th to 20th bit from group id as vlan id.
1294 short vlanId = (short) ((groupId & 0x0fff0000) >> 16);
1295 return VlanId.vlanId(vlanId);
1296 }
1297
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001298 private List<GroupBucket> generateNextGroupBuckets(List<GroupInfo> groupInfos,
1299 GroupDescription.Type bucketType) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001300 List<GroupBucket> newBuckets = Lists.newArrayList();
1301
1302 groupInfos.forEach(groupInfo -> {
1303 GroupDescription groupDesc = groupInfo.nextGroupDesc;
1304 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
Yi Tsengfa394de2017-02-01 11:26:40 -08001305 treatmentBuilder.group(new GroupId(groupDesc.givenGroupId()));
Yi Tseng78f51f42017-02-02 13:54:58 -08001306 GroupBucket newBucket = null;
1307 switch (bucketType) {
1308 case ALL:
1309 newBucket =
1310 DefaultGroupBucket.createAllGroupBucket(treatmentBuilder.build());
1311 break;
1312 case INDIRECT:
1313 newBucket =
1314 DefaultGroupBucket.createIndirectGroupBucket(treatmentBuilder.build());
1315 break;
1316 case SELECT:
1317 newBucket =
1318 DefaultGroupBucket.createSelectGroupBucket(treatmentBuilder.build());
1319 break;
1320 case FAILOVER:
1321 // TODO support failover bucket type
1322 default:
1323 log.warn("Unknown bucket type: {}", bucketType);
1324 break;
1325 }
1326
1327 if (newBucket != null) {
1328 newBuckets.add(newBucket);
1329 }
1330
1331 });
1332
1333 return ImmutableList.copyOf(newBuckets);
Saurav Das1a129a02016-11-18 15:21:57 -08001334 }
1335
1336 private void addBucketToL3MulticastGroup(NextObjective nextObj,
1337 List<Deque<GroupKey>> allActiveKeys,
1338 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001339 VlanId assignedVlan) {
1340 // create the buckets to add to the outermost L3 Multicast group
1341 List<GroupBucket> newBuckets = Lists.newArrayList();
1342 groupInfos.forEach(groupInfo -> {
1343 // Points to L3 interface group if there is one.
1344 // Otherwise points to L2 interface group directly.
1345 GroupDescription nextGroupDesc = (groupInfo.nextGroupDesc != null) ?
1346 groupInfo.nextGroupDesc : groupInfo.innerMostGroupDesc;
1347 TrafficTreatment.Builder treatmentBuidler = DefaultTrafficTreatment.builder();
Yi Tsengfa394de2017-02-01 11:26:40 -08001348 treatmentBuidler.group(new GroupId(nextGroupDesc.givenGroupId()));
Yi Tseng78f51f42017-02-02 13:54:58 -08001349 GroupBucket newBucket = DefaultGroupBucket.createAllGroupBucket(treatmentBuidler.build());
1350 newBuckets.add(newBucket);
1351 });
Saurav Das1a129a02016-11-18 15:21:57 -08001352
1353 // get the group being edited
1354 Group l3mcastGroup = retrieveTopLevelGroup(allActiveKeys, nextObj.id());
1355 if (l3mcastGroup == null) {
1356 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.GROUPMISSING);
1357 return;
1358 }
1359 GroupKey l3mcastGroupKey = l3mcastGroup.appCookie();
1360 int l3mcastGroupId = l3mcastGroup.id().id();
1361
1362 //ensure assignedVlan applies to the chosen group
Yi Tseng78f51f42017-02-02 13:54:58 -08001363 VlanId expectedVlan = extractVlanIdFromGroupId(l3mcastGroupId);
Saurav Das1a129a02016-11-18 15:21:57 -08001364 if (!expectedVlan.equals(assignedVlan)) {
1365 log.warn("VLAN ID {} does not match L3 Mcast group {} to which bucket is "
1366 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1367 Integer.toHexString(l3mcastGroupId), nextObj.id(), deviceId);
1368 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
1369 }
1370 GroupDescription l3mcastGroupDescription =
1371 new DefaultGroupDescription(
1372 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -08001373 ALL,
1374 new GroupBuckets(newBuckets),
Saurav Das1a129a02016-11-18 15:21:57 -08001375 l3mcastGroupKey,
1376 l3mcastGroupId,
1377 nextObj.appId());
1378 GroupChainElem l3mcastGce = new GroupChainElem(l3mcastGroupDescription,
Yi Tseng78f51f42017-02-02 13:54:58 -08001379 groupInfos.size(), true);
Saurav Das1a129a02016-11-18 15:21:57 -08001380
Yi Tseng78f51f42017-02-02 13:54:58 -08001381 groupInfos.forEach(groupInfo -> {
1382 // update original NextGroup with new bucket-chain
1383 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
1384 newBucketChain.addFirst(groupInfo.innerMostGroupDesc.appCookie());
1385 // Add L3 interface group to the chain if there is one.
1386 if (!groupInfo.nextGroupDesc.equals(groupInfo.innerMostGroupDesc)) {
1387 newBucketChain.addFirst(groupInfo.nextGroupDesc.appCookie());
1388 }
1389 newBucketChain.addFirst(l3mcastGroupKey);
1390 // If active keys shows only the top-level group without a chain of groups,
1391 // then it represents an empty group. Update by replacing empty chain.
1392 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1393 allActiveKeys.clear();
1394 }
1395 allActiveKeys.add(newBucketChain);
1396
1397 updatePendingGroups(groupInfo.nextGroupDesc.appCookie(), l3mcastGce);
1398 // Point next group to inner-most group, if any
1399 if (!groupInfo.nextGroupDesc.equals(groupInfo.innerMostGroupDesc)) {
1400 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc,
1401 1, false);
1402 updatePendingGroups(groupInfo.innerMostGroupDesc.appCookie(), innerGce);
1403 }
1404 log.debug("Adding to L3MCAST: device:{} gid:{} gkey:{} nextId:{}",
1405 deviceId, Integer.toHexString(l3mcastGroupId),
1406 l3mcastGroupKey, nextObj.id());
1407 // send the innermost group
1408 log.debug("Sending innermost group {} in group chain on device {} ",
1409 Integer.toHexString(groupInfo.innerMostGroupDesc.givenGroupId()),
1410 deviceId);
1411 groupService.addGroup(groupInfo.innerMostGroupDesc);
1412
1413 });
1414
Saurav Das1a129a02016-11-18 15:21:57 -08001415 updatePendingNextObjective(l3mcastGroupKey,
1416 new OfdpaNextGroup(allActiveKeys, nextObj));
1417
Yi Tseng78f51f42017-02-02 13:54:58 -08001418
1419
Charles Chan188ebf52015-12-23 00:15:11 -08001420 }
1421
1422 /**
1423 * Removes the bucket in the top level group of a possible group-chain. Does
Saurav Das1a129a02016-11-18 15:21:57 -08001424 * not remove the groups in the group-chain pointed to by this bucket, as they
Charles Chan188ebf52015-12-23 00:15:11 -08001425 * may be in use (referenced by other groups) elsewhere.
1426 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001427 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -08001428 * @param next the representation of the existing group-chain for this next objective
1429 */
1430 protected void removeBucketFromGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001431 if (nextObjective.type() != NextObjective.Type.HASHED &&
1432 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001433 log.warn("RemoveBuckets not applied to nextType:{} in dev:{} for next:{}",
1434 nextObjective.type(), deviceId, nextObjective.id());
Saurav Das1a129a02016-11-18 15:21:57 -08001435 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001436 return;
1437 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001438 Set<PortNumber> portsToRemove = Sets.newHashSet();
Charles Chan188ebf52015-12-23 00:15:11 -08001439 Collection<TrafficTreatment> treatments = nextObjective.next();
Yi Tseng78f51f42017-02-02 13:54:58 -08001440 for (TrafficTreatment treatment : treatments) {
1441 // find the bucket to remove by noting the outport, and figuring out the
1442 // top-level group in the group-chain that indirectly references the port
1443 PortNumber portToRemove = readOutPortFromTreatment(treatment);
1444 if (portToRemove == null) {
1445 log.warn("treatment {} of next objective {} has no outport.. cannot remove bucket"
1446 + "from group in dev: {}", treatment, nextObjective.id(), deviceId);
1447 } else {
1448 portsToRemove.add(portToRemove);
1449 }
1450 }
1451
1452 if (portsToRemove.isEmpty()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001453 log.warn("next objective {} has no outport.. cannot remove bucket"
Yi Tseng78f51f42017-02-02 13:54:58 -08001454 + "from group in dev: {}", nextObjective.id(), deviceId);
Saurav Das1a129a02016-11-18 15:21:57 -08001455 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -08001456 }
1457
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001458 List<Deque<GroupKey>> allActiveKeys = Ofdpa2Pipeline.appKryo.deserialize(next.data());
Yi Tseng78f51f42017-02-02 13:54:58 -08001459 List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001460 for (Deque<GroupKey> gkeys : allActiveKeys) {
1461 // last group in group chain should have a single bucket pointing to port
Yi Tseng78f51f42017-02-02 13:54:58 -08001462
Charles Chan188ebf52015-12-23 00:15:11 -08001463 GroupKey groupWithPort = gkeys.peekLast();
1464 Group group = groupService.getGroup(deviceId, groupWithPort);
1465 if (group == null) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001466 log.warn("Inconsistent group chain found when removing bucket"
1467 + "for next:{} in dev:{}", nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001468 continue;
1469 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001470 PortNumber pout = readOutPortFromTreatment(
1471 group.buckets().buckets().get(0).treatment());
Yi Tseng78f51f42017-02-02 13:54:58 -08001472 if (portsToRemove.contains(pout)) {
1473 chainsToRemove.add(gkeys);
Charles Chan188ebf52015-12-23 00:15:11 -08001474 }
Charles Chan188ebf52015-12-23 00:15:11 -08001475 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001476
1477 if (chainsToRemove.isEmpty()) {
Charles Chan188ebf52015-12-23 00:15:11 -08001478 log.warn("Could not find appropriate group-chain for removing bucket"
1479 + " for next id {} in dev:{}", nextObjective.id(), deviceId);
Saurav Das1a129a02016-11-18 15:21:57 -08001480 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.BADPARAMS);
1481 return;
Charles Chan188ebf52015-12-23 00:15:11 -08001482 }
Saurav Das1a129a02016-11-18 15:21:57 -08001483
Yi Tseng78f51f42017-02-02 13:54:58 -08001484 List<GroupBucket> bucketsToRemove = Lists.newArrayList();
1485 //first group key is the one we want to modify
1486 GroupKey modGroupKey = chainsToRemove.get(0).peekFirst();
Saurav Das1a129a02016-11-18 15:21:57 -08001487 Group modGroup = groupService.getGroup(deviceId, modGroupKey);
Yi Tseng78f51f42017-02-02 13:54:58 -08001488
1489 for (Deque<GroupKey> foundChain : chainsToRemove) {
1490 //second group key is the one we wish to remove the reference to
1491 if (foundChain.size() < 2) {
1492 // additional check to make sure second group key exist in
1493 // the chain.
1494 log.warn("Can't find second group key from chain {}",
1495 foundChain);
1496 continue;
Saurav Das1a129a02016-11-18 15:21:57 -08001497 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001498 GroupKey pointedGroupKey = foundChain.stream().collect(Collectors.toList()).get(1);
1499
1500 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
1501
1502 if (pointedGroup == null) {
1503 continue;
1504 }
1505
1506 GroupBucket bucket;
1507 if (nextObjective.type() == NextObjective.Type.HASHED) {
1508 bucket = DefaultGroupBucket.createSelectGroupBucket(
1509 DefaultTrafficTreatment.builder()
1510 .group(pointedGroup.id())
1511 .build());
1512 } else {
1513 bucket = DefaultGroupBucket.createAllGroupBucket(
1514 DefaultTrafficTreatment.builder()
1515 .group(pointedGroup.id())
1516 .build());
1517 }
1518
1519 bucketsToRemove.add(bucket);
Saurav Das1a129a02016-11-18 15:21:57 -08001520 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001521
1522 GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove);
1523 List<String> pointedGroupIds; // for debug log
1524 pointedGroupIds = bucketsToRemove.stream()
1525 .map(GroupBucket::treatment)
1526 .map(TrafficTreatment::allInstructions)
1527 .flatMap(List::stream)
1528 .filter(inst -> inst instanceof Instructions.GroupInstruction)
1529 .map(inst -> (Instructions.GroupInstruction) inst)
1530 .map(Instructions.GroupInstruction::groupId)
1531 .map(GroupId::id)
1532 .map(Integer::toHexString)
1533 .map(id -> HEX_PREFIX + id)
1534 .collect(Collectors.toList());
1535
1536
1537
1538 log.debug("Removing buckets from group id 0x{} pointing to group id(s) {} "
Saurav Das1a129a02016-11-18 15:21:57 -08001539 + "for next id {} in device {}", Integer.toHexString(modGroup.id().id()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001540 pointedGroupIds, nextObjective.id(), deviceId);
1541 addPendingUpdateNextObjective(modGroupKey, nextObjective);
Saurav Das1a129a02016-11-18 15:21:57 -08001542 groupService.removeBucketsFromGroup(deviceId, modGroupKey,
1543 removeBuckets, modGroupKey,
1544 nextObjective.appId());
1545 // update store
Yi Tsengeeb3dc12017-03-15 10:55:50 -07001546 allActiveKeys.removeAll(chainsToRemove);
1547 // If no buckets in the group, then retain an entry for the
1548 // top level group which still exists.
1549 if (allActiveKeys.isEmpty()) {
Saurav Das1a129a02016-11-18 15:21:57 -08001550 ArrayDeque<GroupKey> top = new ArrayDeque<>();
1551 top.add(modGroupKey);
1552 allActiveKeys.add(top);
1553 }
Saurav Das1a129a02016-11-18 15:21:57 -08001554 flowObjectiveStore.putNextGroup(nextObjective.id(),
1555 new OfdpaNextGroup(allActiveKeys,
1556 nextObjective));
Charles Chan188ebf52015-12-23 00:15:11 -08001557 }
1558
1559 /**
1560 * Removes all groups in multiple possible group-chains that represent the next
1561 * objective.
1562 *
1563 * @param nextObjective the next objective to remove
1564 * @param next the NextGroup that represents the existing group-chain for
1565 * this next objective
1566 */
1567 protected void removeGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001568 List<Deque<GroupKey>> allActiveKeys = Ofdpa2Pipeline.appKryo.deserialize(next.data());
Charles Chanfc5c7802016-05-17 13:13:55 -07001569
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001570 List<GroupKey> groupKeys = allActiveKeys.stream()
Charles Chanfc5c7802016-05-17 13:13:55 -07001571 .map(Deque::getFirst).collect(Collectors.toList());
1572 pendingRemoveNextObjectives.put(nextObjective, groupKeys);
1573
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001574 allActiveKeys.forEach(groupChain -> groupChain.forEach(groupKey ->
Charles Chan188ebf52015-12-23 00:15:11 -08001575 groupService.removeGroup(deviceId, groupKey, nextObjective.appId())));
1576 flowObjectiveStore.removeNextGroup(nextObjective.id());
1577 }
1578
Saurav Das8be4e3a2016-03-11 17:19:07 -08001579 //////////////////////////////////////
1580 // Helper Methods and Classes
1581 //////////////////////////////////////
1582
Pier Ventre140a8942016-11-02 07:26:38 -07001583 protected void updatePendingNextObjective(GroupKey key, OfdpaNextGroup value) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001584 List<OfdpaNextGroup> nextList = new CopyOnWriteArrayList<OfdpaNextGroup>();
1585 nextList.add(value);
Charles Chanfc5c7802016-05-17 13:13:55 -07001586 List<OfdpaNextGroup> ret = pendingAddNextObjectives.asMap()
Saurav Das8be4e3a2016-03-11 17:19:07 -08001587 .putIfAbsent(key, nextList);
1588 if (ret != null) {
1589 ret.add(value);
1590 }
1591 }
1592
Charles Chan425854b2016-04-11 15:32:12 -07001593 protected void updatePendingGroups(GroupKey gkey, GroupChainElem gce) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001594 Set<GroupChainElem> gceSet = Collections.newSetFromMap(
1595 new ConcurrentHashMap<GroupChainElem, Boolean>());
1596 gceSet.add(gce);
1597 Set<GroupChainElem> retval = pendingGroups.putIfAbsent(gkey, gceSet);
1598 if (retval != null) {
1599 retval.add(gce);
1600 }
1601 }
1602
Yi Tseng78f51f42017-02-02 13:54:58 -08001603 private void addPendingUpdateNextObjective(GroupKey groupKey, NextObjective nextObjective) {
1604 pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> {
1605 if (nextObjs != null) {
1606 nextObjs.add(nextObjective);
1607 } else {
1608 nextObjs = Sets.newHashSet(nextObjective);
1609 }
1610 return nextObjs;
1611 });
1612 }
1613
Charles Chan188ebf52015-12-23 00:15:11 -08001614 /**
1615 * Processes next element of a group chain. Assumption is that if this
1616 * group points to another group, the latter has already been created
1617 * and this driver has received notification for it. A second assumption is
1618 * that if there is another group waiting for this group then the appropriate
1619 * stores already have the information to act upon the notification for the
1620 * creation of this group.
1621 * <p>
1622 * The processing of the GroupChainElement depends on the number of groups
1623 * this element is waiting on. For all group types other than SIMPLE, a
1624 * GroupChainElement could be waiting on multiple groups.
1625 *
1626 * @param gce the group chain element to be processed next
1627 */
1628 private void processGroupChain(GroupChainElem gce) {
1629 int waitOnGroups = gce.decrementAndGetGroupsWaitedOn();
1630 if (waitOnGroups != 0) {
1631 log.debug("GCE: {} not ready to be processed", gce);
1632 return;
1633 }
1634 log.debug("GCE: {} ready to be processed", gce);
1635 if (gce.addBucketToGroup) {
1636 groupService.addBucketsToGroup(gce.groupDescription.deviceId(),
1637 gce.groupDescription.appCookie(),
1638 gce.groupDescription.buckets(),
1639 gce.groupDescription.appCookie(),
1640 gce.groupDescription.appId());
1641 } else {
1642 groupService.addGroup(gce.groupDescription);
1643 }
1644 }
1645
1646 private class GroupChecker implements Runnable {
1647 @Override
1648 public void run() {
1649 Set<GroupKey> keys = pendingGroups.keySet().stream()
1650 .filter(key -> groupService.getGroup(deviceId, key) != null)
1651 .collect(Collectors.toSet());
Charles Chanfc5c7802016-05-17 13:13:55 -07001652 Set<GroupKey> otherkeys = pendingAddNextObjectives.asMap().keySet().stream()
Charles Chan188ebf52015-12-23 00:15:11 -08001653 .filter(otherkey -> groupService.getGroup(deviceId, otherkey) != null)
1654 .collect(Collectors.toSet());
1655 keys.addAll(otherkeys);
1656
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -07001657 keys.forEach(key ->
Charles Chanfc5c7802016-05-17 13:13:55 -07001658 processPendingAddGroupsOrNextObjs(key, false));
Charles Chan188ebf52015-12-23 00:15:11 -08001659 }
1660 }
1661
Saurav Das8be4e3a2016-03-11 17:19:07 -08001662 private class InnerGroupListener implements GroupListener {
1663 @Override
1664 public void event(GroupEvent event) {
1665 log.trace("received group event of type {}", event.type());
Charles Chanfc5c7802016-05-17 13:13:55 -07001666 switch (event.type()) {
1667 case GROUP_ADDED:
1668 processPendingAddGroupsOrNextObjs(event.subject().appCookie(), true);
1669 break;
1670 case GROUP_REMOVED:
1671 processPendingRemoveNextObjs(event.subject().appCookie());
1672 break;
Yi Tseng78f51f42017-02-02 13:54:58 -08001673 case GROUP_UPDATED:
1674 processPendingUpdateNextObjs(event.subject().appCookie());
1675 break;
Charles Chanfc5c7802016-05-17 13:13:55 -07001676 default:
1677 break;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001678 }
1679 }
1680 }
1681
Yi Tseng78f51f42017-02-02 13:54:58 -08001682 private void processPendingUpdateNextObjs(GroupKey groupKey) {
1683
1684 pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> {
1685 if (nextObjs != null) {
1686
1687 nextObjs.forEach(nextObj -> {
1688 log.debug("Group {} updated, update pending next objective {}.",
1689 groupKey, nextObj);
1690
1691 Ofdpa2Pipeline.pass(nextObj);
1692 });
1693 }
1694
1695 return Sets.newHashSet();
1696 });
1697 }
1698
Charles Chanfc5c7802016-05-17 13:13:55 -07001699 private void processPendingAddGroupsOrNextObjs(GroupKey key, boolean added) {
Charles Chan188ebf52015-12-23 00:15:11 -08001700 //first check for group chain
1701 Set<GroupChainElem> gceSet = pendingGroups.remove(key);
1702 if (gceSet != null) {
1703 for (GroupChainElem gce : gceSet) {
Charles Chan216e3c82016-04-23 14:48:16 -07001704 log.debug("Group service {} group key {} in device {}. "
Saurav Das0fd79d92016-03-07 10:58:36 -08001705 + "Processing next group in group chain with group id 0x{}",
Charles Chan188ebf52015-12-23 00:15:11 -08001706 (added) ? "ADDED" : "processed",
1707 key, deviceId,
1708 Integer.toHexString(gce.groupDescription.givenGroupId()));
1709 processGroupChain(gce);
1710 }
1711 } else {
1712 // otherwise chain complete - check for waiting nextObjectives
Charles Chanfc5c7802016-05-17 13:13:55 -07001713 List<OfdpaNextGroup> nextGrpList = pendingAddNextObjectives.getIfPresent(key);
Charles Chan188ebf52015-12-23 00:15:11 -08001714 if (nextGrpList != null) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001715 pendingAddNextObjectives.invalidate(key);
Charles Chan188ebf52015-12-23 00:15:11 -08001716 nextGrpList.forEach(nextGrp -> {
Charles Chan216e3c82016-04-23 14:48:16 -07001717 log.debug("Group service {} group key {} in device:{}. "
Saurav Das0fd79d92016-03-07 10:58:36 -08001718 + "Done implementing next objective: {} <<-->> gid:0x{}",
Charles Chan188ebf52015-12-23 00:15:11 -08001719 (added) ? "ADDED" : "processed",
1720 key, deviceId, nextGrp.nextObjective().id(),
1721 Integer.toHexString(groupService.getGroup(deviceId, key)
1722 .givenGroupId()));
Charles Chan361154b2016-03-24 10:23:39 -07001723 Ofdpa2Pipeline.pass(nextGrp.nextObjective());
Charles Chan188ebf52015-12-23 00:15:11 -08001724 flowObjectiveStore.putNextGroup(nextGrp.nextObjective().id(), nextGrp);
Yi Tseng47f82dc2017-03-05 22:48:39 -08001725
Charles Chan188ebf52015-12-23 00:15:11 -08001726 // check if addBuckets waiting for this completion
Yi Tseng47f82dc2017-03-05 22:48:39 -08001727 pendingBuckets.compute(nextGrp.nextObjective().id(), (nextId, pendBkts) -> {
1728 if (pendBkts != null) {
1729 pendBkts.forEach(pendBkt -> addBucketToGroup(pendBkt, nextGrp));
1730 }
1731 return null;
1732 });
Charles Chan188ebf52015-12-23 00:15:11 -08001733 });
1734 }
1735 }
1736 }
1737
Charles Chanfc5c7802016-05-17 13:13:55 -07001738 private void processPendingRemoveNextObjs(GroupKey key) {
1739 pendingRemoveNextObjectives.asMap().forEach((nextObjective, groupKeys) -> {
1740 if (groupKeys.isEmpty()) {
1741 pendingRemoveNextObjectives.invalidate(nextObjective);
1742 Ofdpa2Pipeline.pass(nextObjective);
1743 } else {
1744 groupKeys.remove(key);
1745 }
1746 });
1747 }
1748
Charles Chan425854b2016-04-11 15:32:12 -07001749 protected int getNextAvailableIndex() {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001750 return (int) nextIndex.incrementAndGet();
1751 }
1752
Charles Chane849c192016-01-11 18:28:54 -08001753 /**
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001754 * Returns the outport in a traffic treatment.
1755 *
1756 * @param tt the treatment
1757 * @return the PortNumber for the outport or null
1758 */
1759 protected static PortNumber readOutPortFromTreatment(TrafficTreatment tt) {
1760 for (Instruction ins : tt.allInstructions()) {
1761 if (ins.type() == Instruction.Type.OUTPUT) {
1762 return ((Instructions.OutputInstruction) ins).port();
1763 }
1764 }
1765 return null;
1766 }
1767
1768 /**
Charles Chane849c192016-01-11 18:28:54 -08001769 * Returns a hash as the L2 Interface Group Key.
1770 *
1771 * Keep the lower 6-bit for port since port number usually smaller than 64.
1772 * Hash other information into remaining 28 bits.
1773 *
1774 * @param deviceId Device ID
1775 * @param vlanId VLAN ID
1776 * @param portNumber Port number
1777 * @return L2 interface group key
1778 */
Pier Ventre140a8942016-11-02 07:26:38 -07001779 protected int l2InterfaceGroupKey(DeviceId deviceId, VlanId vlanId, long portNumber) {
Charles Chane849c192016-01-11 18:28:54 -08001780 int portLowerBits = (int) portNumber & PORT_LOWER_BITS_MASK;
1781 long portHigherBits = portNumber & PORT_HIGHER_BITS_MASK;
Charles Chand0fd5dc2016-02-16 23:14:49 -08001782 int hash = Objects.hash(deviceId, vlanId, portHigherBits);
Charles Chane849c192016-01-11 18:28:54 -08001783 return L2_INTERFACE_TYPE | (TYPE_MASK & hash << 6) | portLowerBits;
1784 }
1785
Saurav Das1a129a02016-11-18 15:21:57 -08001786 private Group retrieveTopLevelGroup(List<Deque<GroupKey>> allActiveKeys,
1787 int nextid) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001788 GroupKey topLevelGroupKey;
Saurav Das1a129a02016-11-18 15:21:57 -08001789 if (!allActiveKeys.isEmpty()) {
1790 topLevelGroupKey = allActiveKeys.get(0).peekFirst();
1791 } else {
1792 log.warn("Could not determine top level group while processing"
1793 + "next:{} in dev:{}", nextid, deviceId);
1794 return null;
1795 }
1796 Group topGroup = groupService.getGroup(deviceId, topLevelGroupKey);
1797 if (topGroup == null) {
1798 log.warn("Could not find top level group while processing "
1799 + "next:{} in dev:{}", nextid, deviceId);
1800 }
1801 return topGroup;
1802 }
1803
Charles Chan188ebf52015-12-23 00:15:11 -08001804 /**
1805 * Utility class for moving group information around.
Saurav Das1a129a02016-11-18 15:21:57 -08001806 *
1807 * Example: Suppose we are trying to create a group-chain A-B-C-D, where
1808 * A is the top level group, and D is the inner-most group, typically L2 Interface.
1809 * The innerMostGroupDesc is always D. At various stages of the creation
1810 * process the nextGroupDesc may be C or B. The nextGroupDesc exists to
1811 * inform the referencing group about which group it needs to point to,
1812 * and wait for. In some cases the group chain may simply be A-B. In this case,
1813 * both innerMostGroupDesc and nextGroupDesc will be B.
Charles Chan188ebf52015-12-23 00:15:11 -08001814 */
Charles Chan425854b2016-04-11 15:32:12 -07001815 protected class GroupInfo {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001816 /**
1817 * Description of the inner-most group of the group chain.
1818 * It is always an L2 interface group.
1819 */
1820 private GroupDescription innerMostGroupDesc;
Charles Chan188ebf52015-12-23 00:15:11 -08001821
Charles Chan5b9df8d2016-03-28 22:21:40 -07001822 /**
1823 * Description of the next group in the group chain.
1824 * It can be L2 interface, L3 interface, L3 unicast, L3 VPN group.
Saurav Das1a129a02016-11-18 15:21:57 -08001825 * It is possible that nextGroupDesc is the same as the innerMostGroup.
Charles Chan5b9df8d2016-03-28 22:21:40 -07001826 */
1827 private GroupDescription nextGroupDesc;
1828
1829 GroupInfo(GroupDescription innerMostGroupDesc, GroupDescription nextGroupDesc) {
1830 this.innerMostGroupDesc = innerMostGroupDesc;
1831 this.nextGroupDesc = nextGroupDesc;
Charles Chan188ebf52015-12-23 00:15:11 -08001832 }
Pier Ventre140a8942016-11-02 07:26:38 -07001833
1834 /**
1835 * Getter for innerMostGroupDesc.
1836 *
1837 * @return the inner most group description
1838 */
1839 public GroupDescription getInnerMostGroupDesc() {
1840 return innerMostGroupDesc;
1841 }
1842
1843 /**
1844 * Getter for the next group description.
1845 *
1846 * @return the next group description
1847 */
1848 public GroupDescription getNextGroupDesc() {
1849 return nextGroupDesc;
1850 }
Charles Chan188ebf52015-12-23 00:15:11 -08001851 }
1852
1853 /**
1854 * Represents an entire group-chain that implements a Next-Objective from
1855 * the application. The objective is represented as a list of deques, where
1856 * each deque is a separate chain of groups.
1857 * <p>
1858 * For example, an ECMP group with 3 buckets, where each bucket points to
1859 * a group chain of L3 Unicast and L2 interface groups will look like this:
1860 * <ul>
1861 * <li>List[0] is a Deque of GroupKeyECMP(first)-GroupKeyL3(middle)-GroupKeyL2(last)
1862 * <li>List[1] is a Deque of GroupKeyECMP(first)-GroupKeyL3(middle)-GroupKeyL2(last)
1863 * <li>List[2] is a Deque of GroupKeyECMP(first)-GroupKeyL3(middle)-GroupKeyL2(last)
1864 * </ul>
1865 * where the first element of each deque is the same, representing the
1866 * top level ECMP group, while every other element represents a unique groupKey.
1867 * <p>
1868 * Also includes information about the next objective that
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001869 * resulted in these group-chains.
Charles Chan188ebf52015-12-23 00:15:11 -08001870 *
1871 */
1872 protected class OfdpaNextGroup implements NextGroup {
1873 private final NextObjective nextObj;
1874 private final List<Deque<GroupKey>> gkeys;
1875
1876 public OfdpaNextGroup(List<Deque<GroupKey>> gkeys, NextObjective nextObj) {
Charles Chan188ebf52015-12-23 00:15:11 -08001877 this.nextObj = nextObj;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001878 this.gkeys = gkeys;
Charles Chan188ebf52015-12-23 00:15:11 -08001879 }
1880
1881 public NextObjective nextObjective() {
1882 return nextObj;
1883 }
1884
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001885 public List<Deque<GroupKey>> groupKeys() {
1886 return gkeys;
1887 }
1888
Charles Chan188ebf52015-12-23 00:15:11 -08001889 @Override
1890 public byte[] data() {
Charles Chan361154b2016-03-24 10:23:39 -07001891 return Ofdpa2Pipeline.appKryo.serialize(gkeys);
Charles Chan188ebf52015-12-23 00:15:11 -08001892 }
1893 }
1894
1895 /**
1896 * Represents a group element that is part of a chain of groups.
1897 * Stores enough information to create a Group Description to add the group
1898 * to the switch by requesting the Group Service. Objects instantiating this
1899 * class are meant to be temporary and live as long as it is needed to wait for
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001900 * referenced groups in the group chain to be created.
Charles Chan188ebf52015-12-23 00:15:11 -08001901 */
Charles Chan425854b2016-04-11 15:32:12 -07001902 protected class GroupChainElem {
Charles Chan188ebf52015-12-23 00:15:11 -08001903 private GroupDescription groupDescription;
1904 private AtomicInteger waitOnGroups;
1905 private boolean addBucketToGroup;
1906
1907 GroupChainElem(GroupDescription groupDescription, int waitOnGroups,
1908 boolean addBucketToGroup) {
1909 this.groupDescription = groupDescription;
1910 this.waitOnGroups = new AtomicInteger(waitOnGroups);
1911 this.addBucketToGroup = addBucketToGroup;
1912 }
1913
1914 /**
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001915 * This method atomically decrements the counter for the number of
Charles Chan188ebf52015-12-23 00:15:11 -08001916 * groups this GroupChainElement is waiting on, for notifications from
1917 * the Group Service. When this method returns a value of 0, this
1918 * GroupChainElement is ready to be processed.
1919 *
1920 * @return integer indication of the number of notifications being waited on
1921 */
1922 int decrementAndGetGroupsWaitedOn() {
1923 return waitOnGroups.decrementAndGet();
1924 }
1925
1926 @Override
1927 public String toString() {
1928 return (Integer.toHexString(groupDescription.givenGroupId()) +
1929 " groupKey: " + groupDescription.appCookie() +
1930 " waiting-on-groups: " + waitOnGroups.get() +
1931 " addBucketToGroup: " + addBucketToGroup +
1932 " device: " + deviceId);
1933 }
1934 }
Pier Ventre140a8942016-11-02 07:26:38 -07001935
1936 /**
1937 * Helper enum to handle the different MPLS group
1938 * types.
1939 */
1940 protected enum OfdpaMplsGroupSubType {
Pier Ventre140a8942016-11-02 07:26:38 -07001941 MPLS_INTF((short) 0),
Pier Ventre140a8942016-11-02 07:26:38 -07001942 L2_VPN((short) 1),
Pier Ventre140a8942016-11-02 07:26:38 -07001943 L3_VPN((short) 2),
Pier Ventre140a8942016-11-02 07:26:38 -07001944 MPLS_TUNNEL_LABEL_1((short) 3),
Pier Ventre140a8942016-11-02 07:26:38 -07001945 MPLS_TUNNEL_LABEL_2((short) 4),
Pier Ventre140a8942016-11-02 07:26:38 -07001946 MPLS_SWAP_LABEL((short) 5),
Pier Ventre140a8942016-11-02 07:26:38 -07001947 MPLS_ECMP((short) 8);
1948
1949 private short value;
Pier Ventre140a8942016-11-02 07:26:38 -07001950 public static final int OFDPA_GROUP_TYPE_SHIFT = 28;
1951 public static final int OFDPA_MPLS_SUBTYPE_SHIFT = 24;
1952
1953 OfdpaMplsGroupSubType(short value) {
1954 this.value = value;
1955 }
1956
1957 /**
1958 * Gets the value as an short.
1959 *
1960 * @return the value as an short
1961 */
1962 public short getValue() {
1963 return this.value;
1964 }
1965
1966 }
1967
1968 /**
1969 * Creates MPLS Label group id given a sub type and
1970 * the index.
1971 *
1972 * @param subType the MPLS Label group sub type
1973 * @param index the index of the group
1974 * @return the OFDPA group id
1975 */
1976 public Integer makeMplsLabelGroupId(OfdpaMplsGroupSubType subType, int index) {
1977 index = index & 0x00FFFFFF;
1978 return index | (9 << OFDPA_GROUP_TYPE_SHIFT) | (subType.value << OFDPA_MPLS_SUBTYPE_SHIFT);
1979 }
1980
1981 /**
1982 * Creates MPLS Forwarding group id given a sub type and
1983 * the index.
1984 *
1985 * @param subType the MPLS forwarding group sub type
1986 * @param index the index of the group
1987 * @return the OFDPA group id
1988 */
1989 public Integer makeMplsForwardingGroupId(OfdpaMplsGroupSubType subType, int index) {
1990 index = index & 0x00FFFFFF;
1991 return index | (10 << OFDPA_GROUP_TYPE_SHIFT) | (subType.value << OFDPA_MPLS_SUBTYPE_SHIFT);
1992 }
Charles Chan188ebf52015-12-23 00:15:11 -08001993}