blob: 7d85f19642407ef169569c4389c57096a62968fa [file] [log] [blame]
Brian O'Connor7cbbbb72016-04-09 02:13:23 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Charles Chan188ebf52015-12-23 00:15:11 -080016package org.onosproject.driver.pipeline;
17
18import com.google.common.cache.Cache;
19import com.google.common.cache.CacheBuilder;
20import com.google.common.cache.RemovalCause;
21import com.google.common.cache.RemovalNotification;
Charles Chan5b9df8d2016-03-28 22:21:40 -070022import com.google.common.collect.ImmutableList;
23import com.google.common.collect.Lists;
Charles Chan188ebf52015-12-23 00:15:11 -080024import org.onlab.osgi.ServiceDirectory;
Charles Chan5b9df8d2016-03-28 22:21:40 -070025import org.onlab.packet.IpPrefix;
Charles Chan5270ed02016-01-30 23:22:37 -080026import org.onlab.packet.MacAddress;
Charles Chan188ebf52015-12-23 00:15:11 -080027import org.onlab.packet.MplsLabel;
28import org.onlab.packet.VlanId;
29import org.onosproject.core.ApplicationId;
30import org.onosproject.core.DefaultGroupId;
Charles Chan32562522016-04-07 14:37:14 -070031import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Charles Chan188ebf52015-12-23 00:15:11 -080032import org.onosproject.net.DeviceId;
33import org.onosproject.net.PortNumber;
34import org.onosproject.net.behaviour.NextGroup;
35import org.onosproject.net.behaviour.PipelinerContext;
36import org.onosproject.net.flow.DefaultTrafficTreatment;
37import org.onosproject.net.flow.TrafficSelector;
38import org.onosproject.net.flow.TrafficTreatment;
39import org.onosproject.net.flow.criteria.Criterion;
40import org.onosproject.net.flow.criteria.VlanIdCriterion;
41import org.onosproject.net.flow.instructions.Instruction;
42import org.onosproject.net.flow.instructions.Instructions;
43import org.onosproject.net.flow.instructions.L2ModificationInstruction;
44import org.onosproject.net.flowobjective.FlowObjectiveStore;
45import org.onosproject.net.flowobjective.NextObjective;
46import org.onosproject.net.flowobjective.ObjectiveError;
47import org.onosproject.net.group.DefaultGroupBucket;
48import org.onosproject.net.group.DefaultGroupDescription;
49import org.onosproject.net.group.DefaultGroupKey;
50import org.onosproject.net.group.Group;
51import org.onosproject.net.group.GroupBucket;
52import org.onosproject.net.group.GroupBuckets;
53import org.onosproject.net.group.GroupDescription;
54import org.onosproject.net.group.GroupEvent;
55import org.onosproject.net.group.GroupKey;
56import org.onosproject.net.group.GroupListener;
57import org.onosproject.net.group.GroupService;
Saurav Das8be4e3a2016-03-11 17:19:07 -080058import org.onosproject.store.service.AtomicCounter;
59import org.onosproject.store.service.StorageService;
Charles Chan188ebf52015-12-23 00:15:11 -080060import org.slf4j.Logger;
61
62import java.util.ArrayDeque;
63import java.util.ArrayList;
64import java.util.Collection;
65import java.util.Collections;
66import java.util.Deque;
Saurav Das1ce0a7b2016-10-21 14:06:29 -070067import java.util.HashSet;
Charles Chan188ebf52015-12-23 00:15:11 -080068import java.util.List;
Charles Chand0fd5dc2016-02-16 23:14:49 -080069import java.util.Objects;
Charles Chan188ebf52015-12-23 00:15:11 -080070import java.util.Set;
71import java.util.concurrent.ConcurrentHashMap;
72import java.util.concurrent.CopyOnWriteArrayList;
73import java.util.concurrent.Executors;
74import java.util.concurrent.ScheduledExecutorService;
75import java.util.concurrent.TimeUnit;
76import java.util.concurrent.atomic.AtomicInteger;
77import java.util.stream.Collectors;
78
79import static org.onlab.util.Tools.groupedThreads;
80import static org.slf4j.LoggerFactory.getLogger;
81
82/**
83 * Group handler for OFDPA2 pipeline.
84 */
Charles Chan361154b2016-03-24 10:23:39 -070085public class Ofdpa2GroupHandler {
Charles Chan188ebf52015-12-23 00:15:11 -080086 /*
87 * OFDPA requires group-id's to have a certain form.
88 * L2 Interface Groups have <4bits-0><12bits-vlanid><16bits-portid>
89 * L3 Unicast Groups have <4bits-2><28bits-index>
90 * MPLS Interface Groups have <4bits-9><4bits:0><24bits-index>
91 * L3 ECMP Groups have <4bits-7><28bits-index>
92 * L2 Flood Groups have <4bits-4><12bits-vlanid><16bits-index>
93 * L3 VPN Groups have <4bits-9><4bits-2><24bits-index>
94 */
Charles Chan425854b2016-04-11 15:32:12 -070095 protected static final int L2_INTERFACE_TYPE = 0x00000000;
96 protected static final int L3_INTERFACE_TYPE = 0x50000000;
97 protected static final int L3_UNICAST_TYPE = 0x20000000;
98 protected static final int L3_MULTICAST_TYPE = 0x60000000;
99 protected static final int MPLS_INTERFACE_TYPE = 0x90000000;
100 protected static final int MPLS_L3VPN_SUBTYPE = 0x92000000;
101 protected static final int L3_ECMP_TYPE = 0x70000000;
102 protected static final int L2_FLOOD_TYPE = 0x40000000;
Charles Chane849c192016-01-11 18:28:54 -0800103
Charles Chan425854b2016-04-11 15:32:12 -0700104 protected static final int TYPE_MASK = 0x0fffffff;
105 protected static final int SUBTYPE_MASK = 0x00ffffff;
106 protected static final int TYPE_VLAN_MASK = 0x0000ffff;
Charles Chane849c192016-01-11 18:28:54 -0800107
Charles Chan425854b2016-04-11 15:32:12 -0700108 protected static final int PORT_LOWER_BITS_MASK = 0x3f;
109 protected static final long PORT_HIGHER_BITS_MASK = ~PORT_LOWER_BITS_MASK;
Charles Chan188ebf52015-12-23 00:15:11 -0800110
111 private final Logger log = getLogger(getClass());
112 private ServiceDirectory serviceDirectory;
113 protected GroupService groupService;
Saurav Das8be4e3a2016-03-11 17:19:07 -0800114 protected StorageService storageService;
Charles Chan188ebf52015-12-23 00:15:11 -0800115
Charles Chan425854b2016-04-11 15:32:12 -0700116 protected DeviceId deviceId;
Charles Chan188ebf52015-12-23 00:15:11 -0800117 private FlowObjectiveStore flowObjectiveStore;
Charles Chanfc5c7802016-05-17 13:13:55 -0700118 private Cache<GroupKey, List<OfdpaNextGroup>> pendingAddNextObjectives;
119 private Cache<NextObjective, List<GroupKey>> pendingRemoveNextObjectives;
Charles Chan188ebf52015-12-23 00:15:11 -0800120 private ConcurrentHashMap<GroupKey, Set<GroupChainElem>> pendingGroups;
121 private ScheduledExecutorService groupChecker =
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700122 Executors.newScheduledThreadPool(2, groupedThreads("onos/pipeliner", "ofdpa2-%d", log));
Charles Chan188ebf52015-12-23 00:15:11 -0800123
124 // index number for group creation
Saurav Das8be4e3a2016-03-11 17:19:07 -0800125 private AtomicCounter nextIndex;
Charles Chan188ebf52015-12-23 00:15:11 -0800126
Charles Chan188ebf52015-12-23 00:15:11 -0800127 // local store for pending bucketAdds - by design there can only be one
128 // pending bucket for a group
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700129 protected ConcurrentHashMap<Integer, NextObjective> pendingBuckets =
130 new ConcurrentHashMap<>();
Charles Chan188ebf52015-12-23 00:15:11 -0800131
132 protected void init(DeviceId deviceId, PipelinerContext context) {
133 this.deviceId = deviceId;
134 this.flowObjectiveStore = context.store();
135 this.serviceDirectory = context.directory();
136 this.groupService = serviceDirectory.get(GroupService.class);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800137 this.storageService = serviceDirectory.get(StorageService.class);
Madan Jampanid5714e02016-04-19 14:15:20 -0700138 this.nextIndex = storageService.getAtomicCounter("group-id-index-counter");
Charles Chan188ebf52015-12-23 00:15:11 -0800139
Charles Chanfc5c7802016-05-17 13:13:55 -0700140 pendingAddNextObjectives = CacheBuilder.newBuilder()
Charles Chan188ebf52015-12-23 00:15:11 -0800141 .expireAfterWrite(20, TimeUnit.SECONDS)
142 .removalListener((
143 RemovalNotification<GroupKey, List<OfdpaNextGroup>> notification) -> {
144 if (notification.getCause() == RemovalCause.EXPIRED) {
145 notification.getValue().forEach(ofdpaNextGrp ->
Charles Chan361154b2016-03-24 10:23:39 -0700146 Ofdpa2Pipeline.fail(ofdpaNextGrp.nextObj,
Charles Chan188ebf52015-12-23 00:15:11 -0800147 ObjectiveError.GROUPINSTALLATIONFAILED));
Charles Chanfc5c7802016-05-17 13:13:55 -0700148 }
149 }).build();
Charles Chan188ebf52015-12-23 00:15:11 -0800150
Charles Chanfc5c7802016-05-17 13:13:55 -0700151 pendingRemoveNextObjectives = CacheBuilder.newBuilder()
152 .expireAfterWrite(20, TimeUnit.SECONDS)
153 .removalListener((
154 RemovalNotification<NextObjective, List<GroupKey>> notification) -> {
155 if (notification.getCause() == RemovalCause.EXPIRED) {
156 Ofdpa2Pipeline.fail(notification.getKey(),
157 ObjectiveError.GROUPREMOVALFAILED);
Charles Chan188ebf52015-12-23 00:15:11 -0800158 }
159 }).build();
160 pendingGroups = new ConcurrentHashMap<>();
161 groupChecker.scheduleAtFixedRate(new GroupChecker(), 0, 500, TimeUnit.MILLISECONDS);
162
163 groupService.addListener(new InnerGroupListener());
164 }
165
Saurav Das8be4e3a2016-03-11 17:19:07 -0800166 //////////////////////////////////////
167 // Group Creation
168 //////////////////////////////////////
169
Charles Chan188ebf52015-12-23 00:15:11 -0800170 protected void addGroup(NextObjective nextObjective) {
171 switch (nextObjective.type()) {
172 case SIMPLE:
173 Collection<TrafficTreatment> treatments = nextObjective.next();
174 if (treatments.size() != 1) {
175 log.error("Next Objectives of type Simple should only have a "
176 + "single Traffic Treatment. Next Objective Id:{}",
177 nextObjective.id());
Charles Chan361154b2016-03-24 10:23:39 -0700178 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800179 return;
180 }
181 processSimpleNextObjective(nextObjective);
182 break;
183 case BROADCAST:
184 processBroadcastNextObjective(nextObjective);
185 break;
186 case HASHED:
187 processHashedNextObjective(nextObjective);
188 break;
189 case FAILOVER:
Charles Chan361154b2016-03-24 10:23:39 -0700190 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -0800191 log.warn("Unsupported next objective type {}", nextObjective.type());
192 break;
193 default:
Charles Chan361154b2016-03-24 10:23:39 -0700194 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.UNKNOWN);
Charles Chan188ebf52015-12-23 00:15:11 -0800195 log.warn("Unknown next objective type {}", nextObjective.type());
196 }
197 }
198
199 /**
200 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
201 * a chain of groups. The simple Next Objective passed
202 * in by the application has to be broken up into a group chain
203 * comprising of an L3 Unicast Group that points to an L2 Interface
204 * Group which in-turn points to an output port. In some cases, the simple
205 * next Objective can just be an L2 interface without the need for chaining.
206 *
207 * @param nextObj the nextObjective of type SIMPLE
208 */
209 private void processSimpleNextObjective(NextObjective nextObj) {
210 TrafficTreatment treatment = nextObj.next().iterator().next();
211 // determine if plain L2 or L3->L2
212 boolean plainL2 = true;
213 for (Instruction ins : treatment.allInstructions()) {
214 if (ins.type() == Instruction.Type.L2MODIFICATION) {
215 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
216 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_DST ||
217 l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_SRC) {
218 plainL2 = false;
219 break;
220 }
221 }
222 }
223
224 if (plainL2) {
225 createL2InterfaceGroup(nextObj);
226 return;
227 }
228
229 // break up simple next objective to GroupChain objects
230 GroupInfo groupInfo = createL2L3Chain(treatment, nextObj.id(),
Saurav Das8be4e3a2016-03-11 17:19:07 -0800231 nextObj.appId(), false,
232 nextObj.meta());
Charles Chan188ebf52015-12-23 00:15:11 -0800233 if (groupInfo == null) {
234 log.error("Could not process nextObj={} in dev:{}", nextObj.id(), deviceId);
235 return;
236 }
237 // create object for local and distributed storage
238 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700239 gkeyChain.addFirst(groupInfo.innerMostGroupDesc.appCookie());
240 gkeyChain.addFirst(groupInfo.nextGroupDesc.appCookie());
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700241 OfdpaNextGroup ofdpaGrp =
242 new OfdpaNextGroup(Collections.singletonList(gkeyChain), nextObj);
Charles Chan188ebf52015-12-23 00:15:11 -0800243
244 // store l3groupkey with the ofdpaNextGroup for the nextObjective that depends on it
Charles Chan5b9df8d2016-03-28 22:21:40 -0700245 updatePendingNextObjective(groupInfo.nextGroupDesc.appCookie(), ofdpaGrp);
Charles Chan188ebf52015-12-23 00:15:11 -0800246
247 // now we are ready to send the l2 groupDescription (inner), as all the stores
248 // that will get async replies have been updated. By waiting to update
249 // the stores, we prevent nasty race conditions.
Charles Chan5b9df8d2016-03-28 22:21:40 -0700250 groupService.addGroup(groupInfo.innerMostGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800251 }
252
Charles Chan188ebf52015-12-23 00:15:11 -0800253 /**
254 * Creates a simple L2 Interface Group.
255 *
256 * @param nextObj the next Objective
257 */
258 private void createL2InterfaceGroup(NextObjective nextObj) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700259 VlanId assignedVlan = Ofdpa2Pipeline.readVlanFromSelector(nextObj.meta());
260 if (assignedVlan == null) {
261 log.warn("VLAN ID required by simple next obj is missing. Abort.");
262 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800263 return;
264 }
265
Charles Chan5b9df8d2016-03-28 22:21:40 -0700266 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Charles Chan188ebf52015-12-23 00:15:11 -0800267
Charles Chan5b9df8d2016-03-28 22:21:40 -0700268 // There is only one L2 interface group in this case
269 GroupDescription l2InterfaceGroupDesc = groupInfos.get(0).innerMostGroupDesc;
Charles Chan188ebf52015-12-23 00:15:11 -0800270
Charles Chan5b9df8d2016-03-28 22:21:40 -0700271 // Put all dependency information into allGroupKeys
272 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
273 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
274 gkeyChain.addFirst(l2InterfaceGroupDesc.appCookie());
275 allGroupKeys.add(gkeyChain);
Charles Chan188ebf52015-12-23 00:15:11 -0800276
Charles Chan5b9df8d2016-03-28 22:21:40 -0700277 // Point the next objective to this group
278 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
279 updatePendingNextObjective(l2InterfaceGroupDesc.appCookie(), ofdpaGrp);
280
281 // Start installing the inner-most group
282 groupService.addGroup(l2InterfaceGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800283 }
284
285 /**
286 * Creates one of two possible group-chains from the treatment
287 * passed in. Depending on the MPLS boolean, this method either creates
Charles Chan425854b2016-04-11 15:32:12 -0700288 * an L3Unicast Group --&gt; L2Interface Group, if mpls is false;
289 * or MPLSInterface Group --&gt; L2Interface Group, if mpls is true;
Charles Chan188ebf52015-12-23 00:15:11 -0800290 * The returned 'inner' group description is always the L2 Interface group.
291 *
292 * @param treatment that needs to be broken up to create the group chain
293 * @param nextId of the next objective that needs this group chain
294 * @param appId of the application that sent this next objective
295 * @param mpls determines if L3Unicast or MPLSInterface group is created
296 * @param meta metadata passed in by the application as part of the nextObjective
297 * @return GroupInfo containing the GroupDescription of the
298 * L2Interface group(inner) and the GroupDescription of the (outer)
299 * L3Unicast/MPLSInterface group. May return null if there is an
300 * error in processing the chain
301 */
Charles Chan425854b2016-04-11 15:32:12 -0700302 protected GroupInfo createL2L3Chain(TrafficTreatment treatment, int nextId,
Charles Chanf9e98652016-09-07 16:54:23 -0700303 ApplicationId appId, boolean mpls,
304 TrafficSelector meta) {
305 return createL2L3ChainInternal(treatment, nextId, appId, mpls, meta, true);
306 }
307
308 /**
309 * Internal implementation of createL2L3Chain.
310 * <p>
311 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
312 * Since it is non-OF spec, we need an extension treatment for that.
313 * The useSetVlanExtension must be set to false for OFDPA i12.
314 * </p>
315 *
316 * @param treatment that needs to be broken up to create the group chain
317 * @param nextId of the next objective that needs this group chain
318 * @param appId of the application that sent this next objective
319 * @param mpls determines if L3Unicast or MPLSInterface group is created
320 * @param meta metadata passed in by the application as part of the nextObjective
321 * @param useSetVlanExtension use the setVlanVid extension that has is_present bit set to 0.
322 * @return GroupInfo containing the GroupDescription of the
323 * L2Interface group(inner) and the GroupDescription of the (outer)
324 * L3Unicast/MPLSInterface group. May return null if there is an
325 * error in processing the chain
326 */
327 protected GroupInfo createL2L3ChainInternal(TrafficTreatment treatment, int nextId,
Saurav Das8be4e3a2016-03-11 17:19:07 -0800328 ApplicationId appId, boolean mpls,
Charles Chanf9e98652016-09-07 16:54:23 -0700329 TrafficSelector meta, boolean useSetVlanExtension) {
Charles Chan188ebf52015-12-23 00:15:11 -0800330 // for the l2interface group, get vlan and port info
331 // for the outer group, get the src/dst mac, and vlan info
332 TrafficTreatment.Builder outerTtb = DefaultTrafficTreatment.builder();
333 TrafficTreatment.Builder innerTtb = DefaultTrafficTreatment.builder();
334 VlanId vlanid = null;
335 long portNum = 0;
336 boolean setVlan = false, popVlan = false;
Charles Chand0fd5dc2016-02-16 23:14:49 -0800337 MacAddress srcMac = MacAddress.ZERO;
Charles Chan5270ed02016-01-30 23:22:37 -0800338 MacAddress dstMac = MacAddress.ZERO;
Charles Chan188ebf52015-12-23 00:15:11 -0800339 for (Instruction ins : treatment.allInstructions()) {
340 if (ins.type() == Instruction.Type.L2MODIFICATION) {
341 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
342 switch (l2ins.subtype()) {
343 case ETH_DST:
Charles Chan5270ed02016-01-30 23:22:37 -0800344 dstMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
345 outerTtb.setEthDst(dstMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800346 break;
347 case ETH_SRC:
Charles Chand0fd5dc2016-02-16 23:14:49 -0800348 srcMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
349 outerTtb.setEthSrc(srcMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800350 break;
351 case VLAN_ID:
352 vlanid = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
Charles Chanf9e98652016-09-07 16:54:23 -0700353 if (useSetVlanExtension) {
354 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
355 outerTtb.extension(ofdpaSetVlanVid, deviceId);
356 } else {
357 outerTtb.setVlanId(vlanid);
358 }
Charles Chan188ebf52015-12-23 00:15:11 -0800359 setVlan = true;
360 break;
361 case VLAN_POP:
362 innerTtb.popVlan();
363 popVlan = true;
364 break;
365 case DEC_MPLS_TTL:
366 case MPLS_LABEL:
367 case MPLS_POP:
368 case MPLS_PUSH:
369 case VLAN_PCP:
370 case VLAN_PUSH:
371 default:
372 break;
373 }
374 } else if (ins.type() == Instruction.Type.OUTPUT) {
375 portNum = ((Instructions.OutputInstruction) ins).port().toLong();
376 innerTtb.add(ins);
377 } else {
378 log.warn("Driver does not handle this type of TrafficTreatment"
379 + " instruction in nextObjectives: {}", ins.type());
380 }
381 }
382
383 if (vlanid == null && meta != null) {
384 // use metadata if available
385 Criterion vidCriterion = meta.getCriterion(Criterion.Type.VLAN_VID);
386 if (vidCriterion != null) {
387 vlanid = ((VlanIdCriterion) vidCriterion).vlanId();
388 }
389 // if vlan is not set, use the vlan in metadata for outerTtb
390 if (vlanid != null && !setVlan) {
Charles Chanf9e98652016-09-07 16:54:23 -0700391 if (useSetVlanExtension) {
392 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
393 outerTtb.extension(ofdpaSetVlanVid, deviceId);
394 } else {
395 outerTtb.setVlanId(vlanid);
396 }
Charles Chan188ebf52015-12-23 00:15:11 -0800397 }
398 }
399
400 if (vlanid == null) {
401 log.error("Driver cannot process an L2/L3 group chain without "
402 + "egress vlan information for dev: {} port:{}",
403 deviceId, portNum);
404 return null;
405 }
406
407 if (!setVlan && !popVlan) {
408 // untagged outgoing port
409 TrafficTreatment.Builder temp = DefaultTrafficTreatment.builder();
410 temp.popVlan();
411 innerTtb.build().allInstructions().forEach(i -> temp.add(i));
412 innerTtb = temp;
413 }
414
415 // assemble information for ofdpa l2interface group
Charles Chane849c192016-01-11 18:28:54 -0800416 int l2groupId = L2_INTERFACE_TYPE | (vlanid.toShort() << 16) | (int) portNum;
Saurav Das8be4e3a2016-03-11 17:19:07 -0800417 // a globally unique groupkey that is different for ports in the same device,
Charles Chan188ebf52015-12-23 00:15:11 -0800418 // but different for the same portnumber on different devices. Also different
419 // for the various group-types created out of the same next objective.
Charles Chane849c192016-01-11 18:28:54 -0800420 int l2gk = l2InterfaceGroupKey(deviceId, vlanid, portNum);
Charles Chan361154b2016-03-24 10:23:39 -0700421 final GroupKey l2groupkey = new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(l2gk));
Charles Chan188ebf52015-12-23 00:15:11 -0800422
423 // assemble information for outer group
424 GroupDescription outerGrpDesc = null;
425 if (mpls) {
426 // outer group is MPLSInteface
Saurav Das8be4e3a2016-03-11 17:19:07 -0800427 int mplsInterfaceIndex = getNextAvailableIndex();
428 int mplsgroupId = MPLS_INTERFACE_TYPE | (SUBTYPE_MASK & mplsInterfaceIndex);
429 final GroupKey mplsgroupkey = new DefaultGroupKey(
Charles Chan361154b2016-03-24 10:23:39 -0700430 Ofdpa2Pipeline.appKryo.serialize(mplsInterfaceIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800431 outerTtb.group(new DefaultGroupId(l2groupId));
432 // create the mpls-interface group description to wait for the
433 // l2 interface group to be processed
434 GroupBucket mplsinterfaceGroupBucket =
435 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
436 outerGrpDesc = new DefaultGroupDescription(
437 deviceId,
438 GroupDescription.Type.INDIRECT,
439 new GroupBuckets(Collections.singletonList(
440 mplsinterfaceGroupBucket)),
441 mplsgroupkey,
442 mplsgroupId,
443 appId);
444 log.debug("Trying MPLS-Interface: device:{} gid:{} gkey:{} nextid:{}",
445 deviceId, Integer.toHexString(mplsgroupId),
446 mplsgroupkey, nextId);
447 } else {
448 // outer group is L3Unicast
Saurav Das8be4e3a2016-03-11 17:19:07 -0800449 int l3unicastIndex = getNextAvailableIndex();
450 int l3groupId = L3_UNICAST_TYPE | (TYPE_MASK & l3unicastIndex);
451 final GroupKey l3groupkey = new DefaultGroupKey(
Charles Chan361154b2016-03-24 10:23:39 -0700452 Ofdpa2Pipeline.appKryo.serialize(l3unicastIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800453 outerTtb.group(new DefaultGroupId(l2groupId));
454 // create the l3unicast group description to wait for the
455 // l2 interface group to be processed
456 GroupBucket l3unicastGroupBucket =
457 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
458 outerGrpDesc = new DefaultGroupDescription(
459 deviceId,
460 GroupDescription.Type.INDIRECT,
461 new GroupBuckets(Collections.singletonList(
462 l3unicastGroupBucket)),
463 l3groupkey,
464 l3groupId,
465 appId);
466 log.debug("Trying L3Unicast: device:{} gid:{} gkey:{} nextid:{}",
467 deviceId, Integer.toHexString(l3groupId),
468 l3groupkey, nextId);
469 }
470
471 // store l2groupkey with the groupChainElem for the outer-group that depends on it
472 GroupChainElem gce = new GroupChainElem(outerGrpDesc, 1, false);
473 updatePendingGroups(l2groupkey, gce);
474
475 // create group description for the inner l2interfacegroup
Charles Chan5b9df8d2016-03-28 22:21:40 -0700476 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800477 DefaultGroupBucket.createIndirectGroupBucket(innerTtb.build());
478 GroupDescription l2groupDescription =
479 new DefaultGroupDescription(
480 deviceId,
481 GroupDescription.Type.INDIRECT,
482 new GroupBuckets(Collections.singletonList(
Charles Chan5b9df8d2016-03-28 22:21:40 -0700483 l2InterfaceGroupBucket)),
Charles Chan188ebf52015-12-23 00:15:11 -0800484 l2groupkey,
485 l2groupId,
486 appId);
487 log.debug("Trying L2Interface: device:{} gid:{} gkey:{} nextId:{}",
488 deviceId, Integer.toHexString(l2groupId),
489 l2groupkey, nextId);
490 return new GroupInfo(l2groupDescription, outerGrpDesc);
491
492 }
493
494 /**
495 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
496 * a chain of groups. The broadcast Next Objective passed in by the application
497 * has to be broken up into a group chain comprising of an
498 * L2 Flood group whose buckets point to L2 Interface groups.
499 *
500 * @param nextObj the nextObjective of type BROADCAST
501 */
502 private void processBroadcastNextObjective(NextObjective nextObj) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700503 VlanId assignedVlan = Ofdpa2Pipeline.readVlanFromSelector(nextObj.meta());
504 if (assignedVlan == null) {
505 log.warn("VLAN ID required by broadcast next obj is missing. Abort.");
506 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800507 return;
508 }
Charles Chan188ebf52015-12-23 00:15:11 -0800509
Charles Chan5b9df8d2016-03-28 22:21:40 -0700510 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
511
512 IpPrefix ipDst = Ofdpa2Pipeline.readIpDstFromSelector(nextObj.meta());
513 if (ipDst != null) {
514 if (ipDst.isMulticast()) {
515 createL3MulticastGroup(nextObj, assignedVlan, groupInfos);
516 } else {
517 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
518 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
519 return;
520 }
521 } else {
522 createL2FloodGroup(nextObj, assignedVlan, groupInfos);
523 }
524 }
525
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700526 private List<GroupInfo> prepareL2InterfaceGroup(NextObjective nextObj,
527 VlanId assignedVlan) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700528 ImmutableList.Builder<GroupInfo> groupInfoBuilder = ImmutableList.builder();
529
530 // break up broadcast next objective to multiple groups
531 Collection<TrafficTreatment> buckets = nextObj.next();
532
Charles Chan188ebf52015-12-23 00:15:11 -0800533 // each treatment is converted to an L2 interface group
Charles Chan188ebf52015-12-23 00:15:11 -0800534 for (TrafficTreatment treatment : buckets) {
535 TrafficTreatment.Builder newTreatment = DefaultTrafficTreatment.builder();
536 PortNumber portNum = null;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700537 VlanId egressVlan = null;
Charles Chan188ebf52015-12-23 00:15:11 -0800538 // ensure that the only allowed treatments are pop-vlan and output
539 for (Instruction ins : treatment.allInstructions()) {
540 if (ins.type() == Instruction.Type.L2MODIFICATION) {
541 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
542 switch (l2ins.subtype()) {
543 case VLAN_POP:
544 newTreatment.add(l2ins);
545 break;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700546 case VLAN_ID:
547 egressVlan = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
548 break;
Charles Chan188ebf52015-12-23 00:15:11 -0800549 default:
550 log.debug("action {} not permitted for broadcast nextObj",
551 l2ins.subtype());
552 break;
553 }
554 } else if (ins.type() == Instruction.Type.OUTPUT) {
555 portNum = ((Instructions.OutputInstruction) ins).port();
556 newTreatment.add(ins);
557 } else {
Charles Chane849c192016-01-11 18:28:54 -0800558 log.debug("TrafficTreatment of type {} not permitted in " +
559 " broadcast nextObjective", ins.type());
Charles Chan188ebf52015-12-23 00:15:11 -0800560 }
561 }
562
Charles Chan188ebf52015-12-23 00:15:11 -0800563 // assemble info for l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700564 VlanId l2InterfaceGroupVlan =
565 (egressVlan != null && !assignedVlan.equals(egressVlan)) ?
566 egressVlan : assignedVlan;
567 int l2gk = l2InterfaceGroupKey(deviceId, l2InterfaceGroupVlan, portNum.toLong());
568 final GroupKey l2InterfaceGroupKey =
569 new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(l2gk));
570 int l2InterfaceGroupId = L2_INTERFACE_TYPE | (l2InterfaceGroupVlan.toShort() << 16) |
Charles Chan188ebf52015-12-23 00:15:11 -0800571 (int) portNum.toLong();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700572 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800573 DefaultGroupBucket.createIndirectGroupBucket(newTreatment.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700574 GroupDescription l2InterfaceGroupDescription =
Charles Chan188ebf52015-12-23 00:15:11 -0800575 new DefaultGroupDescription(
576 deviceId,
577 GroupDescription.Type.INDIRECT,
578 new GroupBuckets(Collections.singletonList(
Charles Chan5b9df8d2016-03-28 22:21:40 -0700579 l2InterfaceGroupBucket)),
580 l2InterfaceGroupKey,
581 l2InterfaceGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800582 nextObj.appId());
583 log.debug("Trying L2-Interface: device:{} gid:{} gkey:{} nextid:{}",
Charles Chan5b9df8d2016-03-28 22:21:40 -0700584 deviceId, Integer.toHexString(l2InterfaceGroupId),
585 l2InterfaceGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -0800586
Charles Chan5b9df8d2016-03-28 22:21:40 -0700587 groupInfoBuilder.add(new GroupInfo(l2InterfaceGroupDescription,
588 l2InterfaceGroupDescription));
Charles Chan188ebf52015-12-23 00:15:11 -0800589 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700590 return groupInfoBuilder.build();
591 }
Charles Chan188ebf52015-12-23 00:15:11 -0800592
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700593 private void createL2FloodGroup(NextObjective nextObj, VlanId vlanId,
594 List<GroupInfo> groupInfos) {
595 // assemble info for l2 flood group. Since there can be only one flood
596 // group for a vlan, its index is always the same - 0
Saurav Das0fd79d92016-03-07 10:58:36 -0800597 Integer l2floodgroupId = L2_FLOOD_TYPE | (vlanId.toShort() << 16);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800598 int l2floodgk = getNextAvailableIndex();
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700599 final GroupKey l2floodgroupkey =
600 new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(l2floodgk));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700601
Charles Chan188ebf52015-12-23 00:15:11 -0800602 // collection of group buckets pointing to all the l2 interface groups
Charles Chan5b9df8d2016-03-28 22:21:40 -0700603 List<GroupBucket> l2floodBuckets = Lists.newArrayList();
604 groupInfos.forEach(groupInfo -> {
605 GroupDescription l2intGrpDesc = groupInfo.nextGroupDesc;
Charles Chan188ebf52015-12-23 00:15:11 -0800606 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
607 ttb.group(new DefaultGroupId(l2intGrpDesc.givenGroupId()));
608 GroupBucket abucket = DefaultGroupBucket.createAllGroupBucket(ttb.build());
609 l2floodBuckets.add(abucket);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700610 });
Charles Chan188ebf52015-12-23 00:15:11 -0800611 // create the l2flood group-description to wait for all the
612 // l2interface groups to be processed
613 GroupDescription l2floodGroupDescription =
614 new DefaultGroupDescription(
615 deviceId,
616 GroupDescription.Type.ALL,
617 new GroupBuckets(l2floodBuckets),
618 l2floodgroupkey,
619 l2floodgroupId,
620 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800621 log.debug("Trying L2-Flood: device:{} gid:{} gkey:{} nextid:{}",
622 deviceId, Integer.toHexString(l2floodgroupId),
623 l2floodgroupkey, nextObj.id());
624
Charles Chan5b9df8d2016-03-28 22:21:40 -0700625 // Put all dependency information into allGroupKeys
626 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
627 groupInfos.forEach(groupInfo -> {
628 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
629 // In this case we should have L2 interface group only
630 gkeyChain.addFirst(groupInfo.nextGroupDesc.appCookie());
631 gkeyChain.addFirst(l2floodgroupkey);
632 allGroupKeys.add(gkeyChain);
633 });
Charles Chan188ebf52015-12-23 00:15:11 -0800634
Charles Chan5b9df8d2016-03-28 22:21:40 -0700635 // Point the next objective to this group
636 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Charles Chan188ebf52015-12-23 00:15:11 -0800637 updatePendingNextObjective(l2floodgroupkey, ofdpaGrp);
638
Charles Chan5b9df8d2016-03-28 22:21:40 -0700639 GroupChainElem gce = new GroupChainElem(l2floodGroupDescription,
640 groupInfos.size(), false);
641 groupInfos.forEach(groupInfo -> {
642 // Point this group to the next group
643 updatePendingGroups(groupInfo.nextGroupDesc.appCookie(), gce);
644 // Start installing the inner-most group
645 groupService.addGroup(groupInfo.innerMostGroupDesc);
646 });
647 }
648
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700649 private void createL3MulticastGroup(NextObjective nextObj, VlanId vlanId,
650 List<GroupInfo> groupInfos) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700651 List<GroupBucket> l3McastBuckets = new ArrayList<>();
652 groupInfos.forEach(groupInfo -> {
653 // Points to L3 interface group if there is one.
654 // Otherwise points to L2 interface group directly.
655 GroupDescription nextGroupDesc = (groupInfo.nextGroupDesc != null) ?
656 groupInfo.nextGroupDesc : groupInfo.innerMostGroupDesc;
657 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
658 ttb.group(new DefaultGroupId(nextGroupDesc.givenGroupId()));
659 GroupBucket abucket = DefaultGroupBucket.createAllGroupBucket(ttb.build());
660 l3McastBuckets.add(abucket);
661 });
662
663 int l3MulticastIndex = getNextAvailableIndex();
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700664 int l3MulticastGroupId = L3_MULTICAST_TYPE |
665 vlanId.toShort() << 16 | (TYPE_VLAN_MASK & l3MulticastIndex);
666 final GroupKey l3MulticastGroupKey =
667 new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(l3MulticastIndex));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700668
669 GroupDescription l3MulticastGroupDesc = new DefaultGroupDescription(deviceId,
670 GroupDescription.Type.ALL,
671 new GroupBuckets(l3McastBuckets),
672 l3MulticastGroupKey,
673 l3MulticastGroupId,
674 nextObj.appId());
675
676 // Put all dependency information into allGroupKeys
677 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
678 groupInfos.forEach(groupInfo -> {
679 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
680 gkeyChain.addFirst(groupInfo.innerMostGroupDesc.appCookie());
681 // Add L3 interface group to the chain if there is one.
682 if (!groupInfo.nextGroupDesc.equals(groupInfo.innerMostGroupDesc)) {
683 gkeyChain.addFirst(groupInfo.nextGroupDesc.appCookie());
684 }
685 gkeyChain.addFirst(l3MulticastGroupKey);
686 allGroupKeys.add(gkeyChain);
687 });
688
689 // Point the next objective to this group
690 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
691 updatePendingNextObjective(l3MulticastGroupKey, ofdpaGrp);
692
693 GroupChainElem outerGce = new GroupChainElem(l3MulticastGroupDesc,
694 groupInfos.size(), false);
695 groupInfos.forEach(groupInfo -> {
696 // Point this group (L3 multicast) to the next group
697 updatePendingGroups(groupInfo.nextGroupDesc.appCookie(), outerGce);
698
699 // Point next group to inner-most group, if any
700 if (!groupInfo.nextGroupDesc.equals(groupInfo.innerMostGroupDesc)) {
701 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc,
702 1, false);
703 updatePendingGroups(groupInfo.innerMostGroupDesc.appCookie(), innerGce);
704 }
705
706 // Start installing the inner-most group
707 groupService.addGroup(groupInfo.innerMostGroupDesc);
708 });
Charles Chan188ebf52015-12-23 00:15:11 -0800709 }
710
Charles Chan188ebf52015-12-23 00:15:11 -0800711 /**
712 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
713 * a chain of groups. The hashed Next Objective passed in by the application
714 * has to be broken up into a group chain comprising of an
715 * L3 ECMP group as the top level group. Buckets of this group can point
716 * to a variety of groups in a group chain, depending on the whether
717 * MPLS labels are being pushed or not.
718 * <p>
719 * NOTE: We do not create MPLS ECMP groups as they are unimplemented in
720 * OF-DPA 2.0 (even though it is in the spec). Therefore we do not
721 * check the nextObjective meta to see what is matching before being
722 * sent to this nextObjective.
723 *
724 * @param nextObj the nextObjective of type HASHED
725 */
726 private void processHashedNextObjective(NextObjective nextObj) {
727 // storage for all group keys in the chain of groups created
728 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
729 List<GroupInfo> unsentGroups = new ArrayList<>();
730 createHashBucketChains(nextObj, allGroupKeys, unsentGroups);
731
732 // now we can create the outermost L3 ECMP group
733 List<GroupBucket> l3ecmpGroupBuckets = new ArrayList<>();
734 for (GroupInfo gi : unsentGroups) {
735 // create ECMP bucket to point to the outer group
736 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700737 ttb.group(new DefaultGroupId(gi.nextGroupDesc.givenGroupId()));
Charles Chan188ebf52015-12-23 00:15:11 -0800738 GroupBucket sbucket = DefaultGroupBucket
739 .createSelectGroupBucket(ttb.build());
740 l3ecmpGroupBuckets.add(sbucket);
741 }
Saurav Das8be4e3a2016-03-11 17:19:07 -0800742 int l3ecmpIndex = getNextAvailableIndex();
743 int l3ecmpGroupId = L3_ECMP_TYPE | (TYPE_MASK & l3ecmpIndex);
744 GroupKey l3ecmpGroupKey = new DefaultGroupKey(
Charles Chan361154b2016-03-24 10:23:39 -0700745 Ofdpa2Pipeline.appKryo.serialize(l3ecmpIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800746 GroupDescription l3ecmpGroupDesc =
747 new DefaultGroupDescription(
748 deviceId,
749 GroupDescription.Type.SELECT,
750 new GroupBuckets(l3ecmpGroupBuckets),
751 l3ecmpGroupKey,
752 l3ecmpGroupId,
753 nextObj.appId());
754 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
755 l3ecmpGroupBuckets.size(),
756 false);
757
758 // create objects for local and distributed storage
759 allGroupKeys.forEach(gkeyChain -> gkeyChain.addFirst(l3ecmpGroupKey));
760 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
761
762 // store l3ecmpGroupKey with the ofdpaGroupChain for the nextObjective
763 // that depends on it
764 updatePendingNextObjective(l3ecmpGroupKey, ofdpaGrp);
765
766 log.debug("Trying L3ECMP: device:{} gid:{} gkey:{} nextId:{}",
767 deviceId, Integer.toHexString(l3ecmpGroupId),
768 l3ecmpGroupKey, nextObj.id());
769 // finally we are ready to send the innermost groups
770 for (GroupInfo gi : unsentGroups) {
771 log.debug("Sending innermost group {} in group chain on device {} ",
Charles Chan5b9df8d2016-03-28 22:21:40 -0700772 Integer.toHexString(gi.innerMostGroupDesc.givenGroupId()), deviceId);
773 updatePendingGroups(gi.nextGroupDesc.appCookie(), l3ecmpGce);
774 groupService.addGroup(gi.innerMostGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800775 }
776
777 }
778
779 /**
780 * Creates group chains for all buckets in a hashed group, and stores the
781 * GroupInfos and GroupKeys for all the groups in the lists passed in, which
782 * should be empty.
783 * <p>
784 * Does not create the top level ECMP group. Does not actually send the
785 * groups to the groupService.
786 *
787 * @param nextObj the Next Objective with buckets that need to be converted
788 * to group chains
789 * @param allGroupKeys a list to store groupKey for each bucket-group-chain
790 * @param unsentGroups a list to store GroupInfo for each bucket-group-chain
791 */
792 private void createHashBucketChains(NextObjective nextObj,
Saurav Das8be4e3a2016-03-11 17:19:07 -0800793 List<Deque<GroupKey>> allGroupKeys,
794 List<GroupInfo> unsentGroups) {
Charles Chan188ebf52015-12-23 00:15:11 -0800795 // break up hashed next objective to multiple groups
796 Collection<TrafficTreatment> buckets = nextObj.next();
797
798 for (TrafficTreatment bucket : buckets) {
799 //figure out how many labels are pushed in each bucket
800 int labelsPushed = 0;
801 MplsLabel innermostLabel = null;
802 for (Instruction ins : bucket.allInstructions()) {
803 if (ins.type() == Instruction.Type.L2MODIFICATION) {
804 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
805 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_PUSH) {
806 labelsPushed++;
807 }
808 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) {
809 if (innermostLabel == null) {
Ray Milkey125572b2016-02-22 16:48:17 -0800810 innermostLabel = ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label();
Charles Chan188ebf52015-12-23 00:15:11 -0800811 }
812 }
813 }
814 }
815
816 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
817 // XXX we only deal with 0 and 1 label push right now
818 if (labelsPushed == 0) {
819 GroupInfo nolabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
820 nextObj.appId(), false,
821 nextObj.meta());
822 if (nolabelGroupInfo == null) {
823 log.error("Could not process nextObj={} in dev:{}",
824 nextObj.id(), deviceId);
825 return;
826 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700827 gkeyChain.addFirst(nolabelGroupInfo.innerMostGroupDesc.appCookie());
828 gkeyChain.addFirst(nolabelGroupInfo.nextGroupDesc.appCookie());
Charles Chan188ebf52015-12-23 00:15:11 -0800829
830 // we can't send the inner group description yet, as we have to
831 // create the dependent ECMP group first. So we store..
832 unsentGroups.add(nolabelGroupInfo);
833
834 } else if (labelsPushed == 1) {
835 GroupInfo onelabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
836 nextObj.appId(), true,
837 nextObj.meta());
838 if (onelabelGroupInfo == null) {
839 log.error("Could not process nextObj={} in dev:{}",
840 nextObj.id(), deviceId);
841 return;
842 }
843 // we need to add another group to this chain - the L3VPN group
844 TrafficTreatment.Builder l3vpnTtb = DefaultTrafficTreatment.builder();
845 l3vpnTtb.pushMpls()
846 .setMpls(innermostLabel)
847 .setMplsBos(true)
848 .copyTtlOut()
849 .group(new DefaultGroupId(
Charles Chan5b9df8d2016-03-28 22:21:40 -0700850 onelabelGroupInfo.nextGroupDesc.givenGroupId()));
Charles Chan188ebf52015-12-23 00:15:11 -0800851 GroupBucket l3vpnGrpBkt =
852 DefaultGroupBucket.createIndirectGroupBucket(l3vpnTtb.build());
Saurav Das8be4e3a2016-03-11 17:19:07 -0800853 int l3vpnIndex = getNextAvailableIndex();
854 int l3vpngroupId = MPLS_L3VPN_SUBTYPE | (SUBTYPE_MASK & l3vpnIndex);
855 GroupKey l3vpngroupkey = new DefaultGroupKey(
Charles Chan361154b2016-03-24 10:23:39 -0700856 Ofdpa2Pipeline.appKryo.serialize(l3vpnIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800857 GroupDescription l3vpnGroupDesc =
858 new DefaultGroupDescription(
859 deviceId,
860 GroupDescription.Type.INDIRECT,
861 new GroupBuckets(Collections.singletonList(
862 l3vpnGrpBkt)),
863 l3vpngroupkey,
864 l3vpngroupId,
865 nextObj.appId());
866 GroupChainElem l3vpnGce = new GroupChainElem(l3vpnGroupDesc, 1, false);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700867 updatePendingGroups(onelabelGroupInfo.nextGroupDesc.appCookie(), l3vpnGce);
Charles Chan188ebf52015-12-23 00:15:11 -0800868
Charles Chan5b9df8d2016-03-28 22:21:40 -0700869 gkeyChain.addFirst(onelabelGroupInfo.innerMostGroupDesc.appCookie());
870 gkeyChain.addFirst(onelabelGroupInfo.nextGroupDesc.appCookie());
Charles Chan188ebf52015-12-23 00:15:11 -0800871 gkeyChain.addFirst(l3vpngroupkey);
872
873 //now we can replace the outerGrpDesc with the one we just created
Charles Chan5b9df8d2016-03-28 22:21:40 -0700874 onelabelGroupInfo.nextGroupDesc = l3vpnGroupDesc;
Charles Chan188ebf52015-12-23 00:15:11 -0800875
876 // we can't send the innermost group yet, as we have to create
877 // the dependent ECMP group first. So we store ...
878 unsentGroups.add(onelabelGroupInfo);
879
880 log.debug("Trying L3VPN: device:{} gid:{} gkey:{} nextId:{}",
881 deviceId, Integer.toHexString(l3vpngroupId),
882 l3vpngroupkey, nextObj.id());
883
884 } else {
885 log.warn("Driver currently does not handle more than 1 MPLS "
886 + "labels. Not processing nextObjective {}", nextObj.id());
887 return;
888 }
889
890 // all groups in this chain
891 allGroupKeys.add(gkeyChain);
892 }
893 }
894
Saurav Das8be4e3a2016-03-11 17:19:07 -0800895 //////////////////////////////////////
896 // Group Editing
897 //////////////////////////////////////
898
Charles Chan188ebf52015-12-23 00:15:11 -0800899 /**
900 * Adds a bucket to the top level group of a group-chain, and creates the chain.
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700901 * Ensures that bucket being added is not a duplicate, by checking existing
902 * buckets for the same outport.
Charles Chan188ebf52015-12-23 00:15:11 -0800903 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700904 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -0800905 * @param next the representation of the existing group-chain for this next objective
906 */
907 protected void addBucketToGroup(NextObjective nextObjective, NextGroup next) {
908 if (nextObjective.type() != NextObjective.Type.HASHED) {
909 log.warn("AddBuckets not applied to nextType:{} in dev:{} for next:{}",
910 nextObjective.type(), deviceId, nextObjective.id());
911 return;
912 }
913 if (nextObjective.next().size() > 1) {
914 log.warn("Only one bucket can be added at a time");
915 return;
916 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700917 // first check to see if bucket being added is not a duplicate of an
918 // existing bucket. If it is for an existing outport, then its a duplicate.
919 Set<PortNumber> existingOutPorts = new HashSet<>();
920 List<Deque<GroupKey>> allActiveKeys = Ofdpa2Pipeline.appKryo.deserialize(next.data());
921 for (Deque<GroupKey> gkeys : allActiveKeys) {
922 // get the last group for the outport
923 Group glast = groupService.getGroup(deviceId, gkeys.peekLast());
924 if (glast != null && !glast.buckets().buckets().isEmpty()) {
925 PortNumber op = readOutPortFromTreatment(
926 glast.buckets().buckets().get(0).treatment());
927 if (op != null) {
928 existingOutPorts.add(op);
929 }
930 }
931 }
932 // only a single bucket being added
933 TrafficTreatment tt = nextObjective.next().iterator().next();
934 PortNumber newport = readOutPortFromTreatment(tt);
935 if (existingOutPorts.contains(newport)) {
936 log.warn("Attempt to add bucket for existing outport:{} in dev:{} for next:{}",
937 newport, deviceId, nextObjective.id());
938 return;
939 }
940
Charles Chan188ebf52015-12-23 00:15:11 -0800941 // storage for all group keys in the chain of groups created
942 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
943 List<GroupInfo> unsentGroups = new ArrayList<>();
944 createHashBucketChains(nextObjective, allGroupKeys, unsentGroups);
945
946 // now we can create the outermost L3 ECMP group bucket to add
947 GroupInfo gi = unsentGroups.get(0); // only one bucket, so only one group-chain
948 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700949 ttb.group(new DefaultGroupId(gi.nextGroupDesc.givenGroupId()));
Charles Chan188ebf52015-12-23 00:15:11 -0800950 GroupBucket sbucket = DefaultGroupBucket.createSelectGroupBucket(ttb.build());
951
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700952 // retrieve the original L3 ECMP group id and description from the first
953 // element in any deque.
954 GroupKey l3ecmpGroupKey = null;
955 if (!allActiveKeys.isEmpty()) {
956 l3ecmpGroupKey = allActiveKeys.get(0).peekFirst();
957 } else {
958 log.warn("Could not determine top level group while trying to"
959 + "add bucket for port:{} in dev:{} for next:{}",
960 deviceId, nextObjective.id(), newport);
961 return;
962 }
963 Group l3ecmpGroup = groupService.getGroup(deviceId, l3ecmpGroupKey);
964 if (l3ecmpGroup == null) {
965 log.warn("Could not find l3 ecmp group while trying to add bucket"
966 + "for port:{} in dev:{} for next:{}", deviceId,
967 nextObjective.id(), newport);
968 return;
969 }
970 int l3ecmpGroupId = l3ecmpGroup.id().id();
Charles Chan188ebf52015-12-23 00:15:11 -0800971
972 // Although GroupDescriptions are not necessary for adding buckets to
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700973 // existing groups, we still use one in the GroupChainElem. When the latter is
Charles Chan188ebf52015-12-23 00:15:11 -0800974 // processed, the info will be extracted for the bucketAdd call to groupService
975 GroupDescription l3ecmpGroupDesc =
976 new DefaultGroupDescription(
977 deviceId,
978 GroupDescription.Type.SELECT,
979 new GroupBuckets(Collections.singletonList(sbucket)),
980 l3ecmpGroupKey,
981 l3ecmpGroupId,
982 nextObjective.appId());
983 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc, 1, true);
984
985 // update original NextGroup with new bucket-chain
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700986 // If active keys shows only the top-level group without a chain of groups,
987 // then it represents an empty group. Update by replacing empty chain.
Charles Chan188ebf52015-12-23 00:15:11 -0800988 Deque<GroupKey> newBucketChain = allGroupKeys.get(0);
989 newBucketChain.addFirst(l3ecmpGroupKey);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700990 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
991 allActiveKeys.clear();
992 }
993 allActiveKeys.add(newBucketChain);
994 updatePendingNextObjective(l3ecmpGroupKey,
995 new OfdpaNextGroup(allActiveKeys, nextObjective));
Charles Chan188ebf52015-12-23 00:15:11 -0800996 log.debug("Adding to L3ECMP: device:{} gid:{} gkey:{} nextId:{}",
997 deviceId, Integer.toHexString(l3ecmpGroupId),
998 l3ecmpGroupKey, nextObjective.id());
999 // send the innermost group
1000 log.debug("Sending innermost group {} in group chain on device {} ",
Charles Chan5b9df8d2016-03-28 22:21:40 -07001001 Integer.toHexString(gi.innerMostGroupDesc.givenGroupId()), deviceId);
1002 updatePendingGroups(gi.nextGroupDesc.appCookie(), l3ecmpGce);
1003 groupService.addGroup(gi.innerMostGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -08001004
1005 }
1006
1007 /**
1008 * Removes the bucket in the top level group of a possible group-chain. Does
1009 * not remove the groups in a group-chain pointed to by this bucket, as they
1010 * may be in use (referenced by other groups) elsewhere.
1011 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001012 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -08001013 * @param next the representation of the existing group-chain for this next objective
1014 */
1015 protected void removeBucketFromGroup(NextObjective nextObjective, NextGroup next) {
1016 if (nextObjective.type() != NextObjective.Type.HASHED) {
1017 log.warn("RemoveBuckets not applied to nextType:{} in dev:{} for next:{}",
1018 nextObjective.type(), deviceId, nextObjective.id());
1019 return;
1020 }
1021 Collection<TrafficTreatment> treatments = nextObjective.next();
1022 TrafficTreatment treatment = treatments.iterator().next();
1023 // find the bucket to remove by noting the outport, and figuring out the
1024 // top-level group in the group-chain that indirectly references the port
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001025 PortNumber portToRemove = readOutPortFromTreatment(treatment);
1026 if (portToRemove == null) {
1027 log.warn("next objective {} has no outport.. cannot remove bucket"
1028 + "from group in dev: {}", nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001029 return;
1030 }
1031
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001032 List<Deque<GroupKey>> allActiveKeys = Ofdpa2Pipeline.appKryo.deserialize(next.data());
Charles Chan188ebf52015-12-23 00:15:11 -08001033 Deque<GroupKey> foundChain = null;
1034 int index = 0;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001035 for (Deque<GroupKey> gkeys : allActiveKeys) {
1036 // last group in group chain should have a single bucket pointing to port
Charles Chan188ebf52015-12-23 00:15:11 -08001037 GroupKey groupWithPort = gkeys.peekLast();
1038 Group group = groupService.getGroup(deviceId, groupWithPort);
1039 if (group == null) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001040 log.warn("Inconsistent group chain found when removing bucket"
1041 + "for next:{} in dev:{}", nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001042 continue;
1043 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001044 PortNumber pout = readOutPortFromTreatment(
1045 group.buckets().buckets().get(0).treatment());
1046 if (pout.equals(portToRemove)) {
1047 foundChain = gkeys;
Charles Chan188ebf52015-12-23 00:15:11 -08001048 break;
1049 }
1050 index++;
1051 }
1052 if (foundChain != null) {
1053 //first groupkey is the one we want to modify
1054 GroupKey modGroupKey = foundChain.peekFirst();
1055 Group modGroup = groupService.getGroup(deviceId, modGroupKey);
1056 //second groupkey is the one we wish to remove the reference to
1057 GroupKey pointedGroupKey = null;
1058 int i = 0;
1059 for (GroupKey gk : foundChain) {
1060 if (i++ == 1) {
1061 pointedGroupKey = gk;
1062 break;
1063 }
1064 }
1065 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
1066 GroupBucket bucket = DefaultGroupBucket.createSelectGroupBucket(
1067 DefaultTrafficTreatment.builder()
1068 .group(pointedGroup.id())
1069 .build());
1070 GroupBuckets removeBuckets = new GroupBuckets(Collections
1071 .singletonList(bucket));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001072 log.debug("Removing buckets from group id 0x{} pointing to group id 0x{}"
1073 + "for next id {} in device {}", Integer.toHexString(modGroup.id().id()),
1074 Integer.toHexString(pointedGroup.id().id()), nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001075 groupService.removeBucketsFromGroup(deviceId, modGroupKey,
1076 removeBuckets, modGroupKey,
1077 nextObjective.appId());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001078 // update store
1079 // If the bucket removed was the last bucket in the group, then
1080 // retain an entry for the top level group which still exists.
1081 if (allActiveKeys.size() == 1) {
1082 ArrayDeque<GroupKey> top = new ArrayDeque<>();
1083 top.add(modGroupKey);
1084 allActiveKeys.add(top);
1085 }
1086 allActiveKeys.remove(index);
Charles Chan188ebf52015-12-23 00:15:11 -08001087 flowObjectiveStore.putNextGroup(nextObjective.id(),
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001088 new OfdpaNextGroup(allActiveKeys,
1089 nextObjective));
Charles Chan188ebf52015-12-23 00:15:11 -08001090 } else {
1091 log.warn("Could not find appropriate group-chain for removing bucket"
1092 + " for next id {} in dev:{}", nextObjective.id(), deviceId);
1093 }
1094 }
1095
1096 /**
1097 * Removes all groups in multiple possible group-chains that represent the next
1098 * objective.
1099 *
1100 * @param nextObjective the next objective to remove
1101 * @param next the NextGroup that represents the existing group-chain for
1102 * this next objective
1103 */
1104 protected void removeGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001105 List<Deque<GroupKey>> allActiveKeys = Ofdpa2Pipeline.appKryo.deserialize(next.data());
Charles Chanfc5c7802016-05-17 13:13:55 -07001106
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001107 List<GroupKey> groupKeys = allActiveKeys.stream()
Charles Chanfc5c7802016-05-17 13:13:55 -07001108 .map(Deque::getFirst).collect(Collectors.toList());
1109 pendingRemoveNextObjectives.put(nextObjective, groupKeys);
1110
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001111 allActiveKeys.forEach(groupChain -> groupChain.forEach(groupKey ->
Charles Chan188ebf52015-12-23 00:15:11 -08001112 groupService.removeGroup(deviceId, groupKey, nextObjective.appId())));
1113 flowObjectiveStore.removeNextGroup(nextObjective.id());
1114 }
1115
Saurav Das8be4e3a2016-03-11 17:19:07 -08001116 //////////////////////////////////////
1117 // Helper Methods and Classes
1118 //////////////////////////////////////
1119
1120 private void updatePendingNextObjective(GroupKey key, OfdpaNextGroup value) {
1121 List<OfdpaNextGroup> nextList = new CopyOnWriteArrayList<OfdpaNextGroup>();
1122 nextList.add(value);
Charles Chanfc5c7802016-05-17 13:13:55 -07001123 List<OfdpaNextGroup> ret = pendingAddNextObjectives.asMap()
Saurav Das8be4e3a2016-03-11 17:19:07 -08001124 .putIfAbsent(key, nextList);
1125 if (ret != null) {
1126 ret.add(value);
1127 }
1128 }
1129
Charles Chan425854b2016-04-11 15:32:12 -07001130 protected void updatePendingGroups(GroupKey gkey, GroupChainElem gce) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001131 Set<GroupChainElem> gceSet = Collections.newSetFromMap(
1132 new ConcurrentHashMap<GroupChainElem, Boolean>());
1133 gceSet.add(gce);
1134 Set<GroupChainElem> retval = pendingGroups.putIfAbsent(gkey, gceSet);
1135 if (retval != null) {
1136 retval.add(gce);
1137 }
1138 }
1139
Charles Chan188ebf52015-12-23 00:15:11 -08001140 /**
1141 * Processes next element of a group chain. Assumption is that if this
1142 * group points to another group, the latter has already been created
1143 * and this driver has received notification for it. A second assumption is
1144 * that if there is another group waiting for this group then the appropriate
1145 * stores already have the information to act upon the notification for the
1146 * creation of this group.
1147 * <p>
1148 * The processing of the GroupChainElement depends on the number of groups
1149 * this element is waiting on. For all group types other than SIMPLE, a
1150 * GroupChainElement could be waiting on multiple groups.
1151 *
1152 * @param gce the group chain element to be processed next
1153 */
1154 private void processGroupChain(GroupChainElem gce) {
1155 int waitOnGroups = gce.decrementAndGetGroupsWaitedOn();
1156 if (waitOnGroups != 0) {
1157 log.debug("GCE: {} not ready to be processed", gce);
1158 return;
1159 }
1160 log.debug("GCE: {} ready to be processed", gce);
1161 if (gce.addBucketToGroup) {
1162 groupService.addBucketsToGroup(gce.groupDescription.deviceId(),
1163 gce.groupDescription.appCookie(),
1164 gce.groupDescription.buckets(),
1165 gce.groupDescription.appCookie(),
1166 gce.groupDescription.appId());
1167 } else {
1168 groupService.addGroup(gce.groupDescription);
1169 }
1170 }
1171
1172 private class GroupChecker implements Runnable {
1173 @Override
1174 public void run() {
1175 Set<GroupKey> keys = pendingGroups.keySet().stream()
1176 .filter(key -> groupService.getGroup(deviceId, key) != null)
1177 .collect(Collectors.toSet());
Charles Chanfc5c7802016-05-17 13:13:55 -07001178 Set<GroupKey> otherkeys = pendingAddNextObjectives.asMap().keySet().stream()
Charles Chan188ebf52015-12-23 00:15:11 -08001179 .filter(otherkey -> groupService.getGroup(deviceId, otherkey) != null)
1180 .collect(Collectors.toSet());
1181 keys.addAll(otherkeys);
1182
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -07001183 keys.forEach(key ->
Charles Chanfc5c7802016-05-17 13:13:55 -07001184 processPendingAddGroupsOrNextObjs(key, false));
Charles Chan188ebf52015-12-23 00:15:11 -08001185 }
1186 }
1187
Saurav Das8be4e3a2016-03-11 17:19:07 -08001188 private class InnerGroupListener implements GroupListener {
1189 @Override
1190 public void event(GroupEvent event) {
1191 log.trace("received group event of type {}", event.type());
Charles Chanfc5c7802016-05-17 13:13:55 -07001192 switch (event.type()) {
1193 case GROUP_ADDED:
1194 processPendingAddGroupsOrNextObjs(event.subject().appCookie(), true);
1195 break;
1196 case GROUP_REMOVED:
1197 processPendingRemoveNextObjs(event.subject().appCookie());
1198 break;
1199 default:
1200 break;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001201 }
1202 }
1203 }
1204
Charles Chanfc5c7802016-05-17 13:13:55 -07001205 private void processPendingAddGroupsOrNextObjs(GroupKey key, boolean added) {
Charles Chan188ebf52015-12-23 00:15:11 -08001206 //first check for group chain
1207 Set<GroupChainElem> gceSet = pendingGroups.remove(key);
1208 if (gceSet != null) {
1209 for (GroupChainElem gce : gceSet) {
Charles Chan216e3c82016-04-23 14:48:16 -07001210 log.debug("Group service {} group key {} in device {}. "
Saurav Das0fd79d92016-03-07 10:58:36 -08001211 + "Processing next group in group chain with group id 0x{}",
Charles Chan188ebf52015-12-23 00:15:11 -08001212 (added) ? "ADDED" : "processed",
1213 key, deviceId,
1214 Integer.toHexString(gce.groupDescription.givenGroupId()));
1215 processGroupChain(gce);
1216 }
1217 } else {
1218 // otherwise chain complete - check for waiting nextObjectives
Charles Chanfc5c7802016-05-17 13:13:55 -07001219 List<OfdpaNextGroup> nextGrpList = pendingAddNextObjectives.getIfPresent(key);
Charles Chan188ebf52015-12-23 00:15:11 -08001220 if (nextGrpList != null) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001221 pendingAddNextObjectives.invalidate(key);
Charles Chan188ebf52015-12-23 00:15:11 -08001222 nextGrpList.forEach(nextGrp -> {
Charles Chan216e3c82016-04-23 14:48:16 -07001223 log.debug("Group service {} group key {} in device:{}. "
Saurav Das0fd79d92016-03-07 10:58:36 -08001224 + "Done implementing next objective: {} <<-->> gid:0x{}",
Charles Chan188ebf52015-12-23 00:15:11 -08001225 (added) ? "ADDED" : "processed",
1226 key, deviceId, nextGrp.nextObjective().id(),
1227 Integer.toHexString(groupService.getGroup(deviceId, key)
1228 .givenGroupId()));
Charles Chan361154b2016-03-24 10:23:39 -07001229 Ofdpa2Pipeline.pass(nextGrp.nextObjective());
Charles Chan188ebf52015-12-23 00:15:11 -08001230 flowObjectiveStore.putNextGroup(nextGrp.nextObjective().id(), nextGrp);
1231 // check if addBuckets waiting for this completion
1232 NextObjective pendBkt = pendingBuckets
1233 .remove(nextGrp.nextObjective().id());
1234 if (pendBkt != null) {
1235 addBucketToGroup(pendBkt, nextGrp);
1236 }
1237 });
1238 }
1239 }
1240 }
1241
Charles Chanfc5c7802016-05-17 13:13:55 -07001242 private void processPendingRemoveNextObjs(GroupKey key) {
1243 pendingRemoveNextObjectives.asMap().forEach((nextObjective, groupKeys) -> {
1244 if (groupKeys.isEmpty()) {
1245 pendingRemoveNextObjectives.invalidate(nextObjective);
1246 Ofdpa2Pipeline.pass(nextObjective);
1247 } else {
1248 groupKeys.remove(key);
1249 }
1250 });
1251 }
1252
Charles Chan425854b2016-04-11 15:32:12 -07001253 protected int getNextAvailableIndex() {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001254 return (int) nextIndex.incrementAndGet();
1255 }
1256
Charles Chane849c192016-01-11 18:28:54 -08001257 /**
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001258 * Returns the outport in a traffic treatment.
1259 *
1260 * @param tt the treatment
1261 * @return the PortNumber for the outport or null
1262 */
1263 protected static PortNumber readOutPortFromTreatment(TrafficTreatment tt) {
1264 for (Instruction ins : tt.allInstructions()) {
1265 if (ins.type() == Instruction.Type.OUTPUT) {
1266 return ((Instructions.OutputInstruction) ins).port();
1267 }
1268 }
1269 return null;
1270 }
1271
1272 /**
Charles Chane849c192016-01-11 18:28:54 -08001273 * Returns a hash as the L2 Interface Group Key.
1274 *
1275 * Keep the lower 6-bit for port since port number usually smaller than 64.
1276 * Hash other information into remaining 28 bits.
1277 *
1278 * @param deviceId Device ID
1279 * @param vlanId VLAN ID
1280 * @param portNumber Port number
1281 * @return L2 interface group key
1282 */
Charles Chan425854b2016-04-11 15:32:12 -07001283 protected int l2InterfaceGroupKey(
Charles Chane849c192016-01-11 18:28:54 -08001284 DeviceId deviceId, VlanId vlanId, long portNumber) {
1285 int portLowerBits = (int) portNumber & PORT_LOWER_BITS_MASK;
1286 long portHigherBits = portNumber & PORT_HIGHER_BITS_MASK;
Charles Chand0fd5dc2016-02-16 23:14:49 -08001287 int hash = Objects.hash(deviceId, vlanId, portHigherBits);
Charles Chane849c192016-01-11 18:28:54 -08001288 return L2_INTERFACE_TYPE | (TYPE_MASK & hash << 6) | portLowerBits;
1289 }
1290
Charles Chan188ebf52015-12-23 00:15:11 -08001291 /**
1292 * Utility class for moving group information around.
1293 */
Charles Chan425854b2016-04-11 15:32:12 -07001294 protected class GroupInfo {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001295 /**
1296 * Description of the inner-most group of the group chain.
1297 * It is always an L2 interface group.
1298 */
1299 private GroupDescription innerMostGroupDesc;
Charles Chan188ebf52015-12-23 00:15:11 -08001300
Charles Chan5b9df8d2016-03-28 22:21:40 -07001301 /**
1302 * Description of the next group in the group chain.
1303 * It can be L2 interface, L3 interface, L3 unicast, L3 VPN group.
1304 * It is possible that nextGroup is the same as the innerMostGroup.
1305 */
1306 private GroupDescription nextGroupDesc;
1307
1308 GroupInfo(GroupDescription innerMostGroupDesc, GroupDescription nextGroupDesc) {
1309 this.innerMostGroupDesc = innerMostGroupDesc;
1310 this.nextGroupDesc = nextGroupDesc;
Charles Chan188ebf52015-12-23 00:15:11 -08001311 }
1312 }
1313
1314 /**
1315 * Represents an entire group-chain that implements a Next-Objective from
1316 * the application. The objective is represented as a list of deques, where
1317 * each deque is a separate chain of groups.
1318 * <p>
1319 * For example, an ECMP group with 3 buckets, where each bucket points to
1320 * a group chain of L3 Unicast and L2 interface groups will look like this:
1321 * <ul>
1322 * <li>List[0] is a Deque of GroupKeyECMP(first)-GroupKeyL3(middle)-GroupKeyL2(last)
1323 * <li>List[1] is a Deque of GroupKeyECMP(first)-GroupKeyL3(middle)-GroupKeyL2(last)
1324 * <li>List[2] is a Deque of GroupKeyECMP(first)-GroupKeyL3(middle)-GroupKeyL2(last)
1325 * </ul>
1326 * where the first element of each deque is the same, representing the
1327 * top level ECMP group, while every other element represents a unique groupKey.
1328 * <p>
1329 * Also includes information about the next objective that
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001330 * resulted in these group-chains.
Charles Chan188ebf52015-12-23 00:15:11 -08001331 *
1332 */
1333 protected class OfdpaNextGroup implements NextGroup {
1334 private final NextObjective nextObj;
1335 private final List<Deque<GroupKey>> gkeys;
1336
1337 public OfdpaNextGroup(List<Deque<GroupKey>> gkeys, NextObjective nextObj) {
Charles Chan188ebf52015-12-23 00:15:11 -08001338 this.nextObj = nextObj;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001339 this.gkeys = gkeys;
Charles Chan188ebf52015-12-23 00:15:11 -08001340 }
1341
1342 public NextObjective nextObjective() {
1343 return nextObj;
1344 }
1345
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001346 public List<Deque<GroupKey>> groupKeys() {
1347 return gkeys;
1348 }
1349
Charles Chan188ebf52015-12-23 00:15:11 -08001350 @Override
1351 public byte[] data() {
Charles Chan361154b2016-03-24 10:23:39 -07001352 return Ofdpa2Pipeline.appKryo.serialize(gkeys);
Charles Chan188ebf52015-12-23 00:15:11 -08001353 }
1354 }
1355
1356 /**
1357 * Represents a group element that is part of a chain of groups.
1358 * Stores enough information to create a Group Description to add the group
1359 * to the switch by requesting the Group Service. Objects instantiating this
1360 * class are meant to be temporary and live as long as it is needed to wait for
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001361 * referenced groups in the group chain to be created.
Charles Chan188ebf52015-12-23 00:15:11 -08001362 */
Charles Chan425854b2016-04-11 15:32:12 -07001363 protected class GroupChainElem {
Charles Chan188ebf52015-12-23 00:15:11 -08001364 private GroupDescription groupDescription;
1365 private AtomicInteger waitOnGroups;
1366 private boolean addBucketToGroup;
1367
1368 GroupChainElem(GroupDescription groupDescription, int waitOnGroups,
1369 boolean addBucketToGroup) {
1370 this.groupDescription = groupDescription;
1371 this.waitOnGroups = new AtomicInteger(waitOnGroups);
1372 this.addBucketToGroup = addBucketToGroup;
1373 }
1374
1375 /**
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001376 * This method atomically decrements the counter for the number of
Charles Chan188ebf52015-12-23 00:15:11 -08001377 * groups this GroupChainElement is waiting on, for notifications from
1378 * the Group Service. When this method returns a value of 0, this
1379 * GroupChainElement is ready to be processed.
1380 *
1381 * @return integer indication of the number of notifications being waited on
1382 */
1383 int decrementAndGetGroupsWaitedOn() {
1384 return waitOnGroups.decrementAndGet();
1385 }
1386
1387 @Override
1388 public String toString() {
1389 return (Integer.toHexString(groupDescription.givenGroupId()) +
1390 " groupKey: " + groupDescription.appCookie() +
1391 " waiting-on-groups: " + waitOnGroups.get() +
1392 " addBucketToGroup: " + addBucketToGroup +
1393 " device: " + deviceId);
1394 }
1395 }
1396}