blob: f116c718af0529ab1478c5f3b2e1372c8961f627 [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/**
Charles Chan40132b32017-01-22 00:19:37 -080083 * Group handler that emulates Broadcom OF-DPA TTP on CpqD.
Charles Chan188ebf52015-12-23 00:15:11 -080084 */
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
Charles Chan40132b32017-01-22 00:19:37 -0800132 /**
133 * Determines whether this pipeline support copy ttl instructions or not.
134 *
135 * @return true if copy ttl instructions are supported
136 */
137 protected boolean supportCopyTtl() {
138 return true;
139 }
140
141 /**
142 * Determines whether this pipeline support set mpls bos instruction or not.
143 *
144 * @return true if set mpls bos instruction is supported
145 */
146 protected boolean supportSetMplsBos() {
147 return true;
148 }
149
Charles Chan188ebf52015-12-23 00:15:11 -0800150 protected void init(DeviceId deviceId, PipelinerContext context) {
151 this.deviceId = deviceId;
152 this.flowObjectiveStore = context.store();
153 this.serviceDirectory = context.directory();
154 this.groupService = serviceDirectory.get(GroupService.class);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800155 this.storageService = serviceDirectory.get(StorageService.class);
Madan Jampanid5714e02016-04-19 14:15:20 -0700156 this.nextIndex = storageService.getAtomicCounter("group-id-index-counter");
Charles Chan188ebf52015-12-23 00:15:11 -0800157
Charles Chanfc5c7802016-05-17 13:13:55 -0700158 pendingAddNextObjectives = CacheBuilder.newBuilder()
Charles Chan188ebf52015-12-23 00:15:11 -0800159 .expireAfterWrite(20, TimeUnit.SECONDS)
160 .removalListener((
161 RemovalNotification<GroupKey, List<OfdpaNextGroup>> notification) -> {
162 if (notification.getCause() == RemovalCause.EXPIRED) {
163 notification.getValue().forEach(ofdpaNextGrp ->
Charles Chan361154b2016-03-24 10:23:39 -0700164 Ofdpa2Pipeline.fail(ofdpaNextGrp.nextObj,
Charles Chan188ebf52015-12-23 00:15:11 -0800165 ObjectiveError.GROUPINSTALLATIONFAILED));
Charles Chanfc5c7802016-05-17 13:13:55 -0700166 }
167 }).build();
Charles Chan188ebf52015-12-23 00:15:11 -0800168
Charles Chanfc5c7802016-05-17 13:13:55 -0700169 pendingRemoveNextObjectives = CacheBuilder.newBuilder()
170 .expireAfterWrite(20, TimeUnit.SECONDS)
171 .removalListener((
172 RemovalNotification<NextObjective, List<GroupKey>> notification) -> {
173 if (notification.getCause() == RemovalCause.EXPIRED) {
174 Ofdpa2Pipeline.fail(notification.getKey(),
175 ObjectiveError.GROUPREMOVALFAILED);
Charles Chan188ebf52015-12-23 00:15:11 -0800176 }
177 }).build();
178 pendingGroups = new ConcurrentHashMap<>();
179 groupChecker.scheduleAtFixedRate(new GroupChecker(), 0, 500, TimeUnit.MILLISECONDS);
180
181 groupService.addListener(new InnerGroupListener());
182 }
183
Saurav Das8be4e3a2016-03-11 17:19:07 -0800184 //////////////////////////////////////
185 // Group Creation
186 //////////////////////////////////////
187
Charles Chan188ebf52015-12-23 00:15:11 -0800188 protected void addGroup(NextObjective nextObjective) {
189 switch (nextObjective.type()) {
190 case SIMPLE:
191 Collection<TrafficTreatment> treatments = nextObjective.next();
192 if (treatments.size() != 1) {
193 log.error("Next Objectives of type Simple should only have a "
194 + "single Traffic Treatment. Next Objective Id:{}",
195 nextObjective.id());
Charles Chan361154b2016-03-24 10:23:39 -0700196 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800197 return;
198 }
199 processSimpleNextObjective(nextObjective);
200 break;
201 case BROADCAST:
202 processBroadcastNextObjective(nextObjective);
203 break;
204 case HASHED:
205 processHashedNextObjective(nextObjective);
206 break;
207 case FAILOVER:
Charles Chan361154b2016-03-24 10:23:39 -0700208 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -0800209 log.warn("Unsupported next objective type {}", nextObjective.type());
210 break;
211 default:
Charles Chan361154b2016-03-24 10:23:39 -0700212 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.UNKNOWN);
Charles Chan188ebf52015-12-23 00:15:11 -0800213 log.warn("Unknown next objective type {}", nextObjective.type());
214 }
215 }
216
217 /**
218 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
219 * a chain of groups. The simple Next Objective passed
220 * in by the application has to be broken up into a group chain
221 * comprising of an L3 Unicast Group that points to an L2 Interface
222 * Group which in-turn points to an output port. In some cases, the simple
223 * next Objective can just be an L2 interface without the need for chaining.
224 *
225 * @param nextObj the nextObjective of type SIMPLE
226 */
227 private void processSimpleNextObjective(NextObjective nextObj) {
228 TrafficTreatment treatment = nextObj.next().iterator().next();
229 // determine if plain L2 or L3->L2
230 boolean plainL2 = true;
231 for (Instruction ins : treatment.allInstructions()) {
232 if (ins.type() == Instruction.Type.L2MODIFICATION) {
233 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
234 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_DST ||
235 l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_SRC) {
236 plainL2 = false;
237 break;
238 }
239 }
240 }
241
242 if (plainL2) {
243 createL2InterfaceGroup(nextObj);
244 return;
245 }
246
247 // break up simple next objective to GroupChain objects
248 GroupInfo groupInfo = createL2L3Chain(treatment, nextObj.id(),
Saurav Das8be4e3a2016-03-11 17:19:07 -0800249 nextObj.appId(), false,
250 nextObj.meta());
Charles Chan188ebf52015-12-23 00:15:11 -0800251 if (groupInfo == null) {
252 log.error("Could not process nextObj={} in dev:{}", nextObj.id(), deviceId);
253 return;
254 }
255 // create object for local and distributed storage
256 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700257 gkeyChain.addFirst(groupInfo.innerMostGroupDesc.appCookie());
258 gkeyChain.addFirst(groupInfo.nextGroupDesc.appCookie());
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700259 OfdpaNextGroup ofdpaGrp =
260 new OfdpaNextGroup(Collections.singletonList(gkeyChain), nextObj);
Charles Chan188ebf52015-12-23 00:15:11 -0800261
262 // store l3groupkey with the ofdpaNextGroup for the nextObjective that depends on it
Charles Chan5b9df8d2016-03-28 22:21:40 -0700263 updatePendingNextObjective(groupInfo.nextGroupDesc.appCookie(), ofdpaGrp);
Charles Chan188ebf52015-12-23 00:15:11 -0800264
265 // now we are ready to send the l2 groupDescription (inner), as all the stores
266 // that will get async replies have been updated. By waiting to update
267 // the stores, we prevent nasty race conditions.
Charles Chan5b9df8d2016-03-28 22:21:40 -0700268 groupService.addGroup(groupInfo.innerMostGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800269 }
270
Charles Chan188ebf52015-12-23 00:15:11 -0800271 /**
272 * Creates a simple L2 Interface Group.
273 *
274 * @param nextObj the next Objective
275 */
276 private void createL2InterfaceGroup(NextObjective nextObj) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700277 VlanId assignedVlan = Ofdpa2Pipeline.readVlanFromSelector(nextObj.meta());
278 if (assignedVlan == null) {
279 log.warn("VLAN ID required by simple next obj is missing. Abort.");
280 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800281 return;
282 }
283
Charles Chan5b9df8d2016-03-28 22:21:40 -0700284 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Charles Chan188ebf52015-12-23 00:15:11 -0800285
Charles Chan5b9df8d2016-03-28 22:21:40 -0700286 // There is only one L2 interface group in this case
287 GroupDescription l2InterfaceGroupDesc = groupInfos.get(0).innerMostGroupDesc;
Charles Chan188ebf52015-12-23 00:15:11 -0800288
Charles Chan5b9df8d2016-03-28 22:21:40 -0700289 // Put all dependency information into allGroupKeys
290 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
291 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
292 gkeyChain.addFirst(l2InterfaceGroupDesc.appCookie());
293 allGroupKeys.add(gkeyChain);
Charles Chan188ebf52015-12-23 00:15:11 -0800294
Charles Chan5b9df8d2016-03-28 22:21:40 -0700295 // Point the next objective to this group
296 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
297 updatePendingNextObjective(l2InterfaceGroupDesc.appCookie(), ofdpaGrp);
298
299 // Start installing the inner-most group
300 groupService.addGroup(l2InterfaceGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800301 }
302
303 /**
304 * Creates one of two possible group-chains from the treatment
305 * passed in. Depending on the MPLS boolean, this method either creates
Charles Chan425854b2016-04-11 15:32:12 -0700306 * an L3Unicast Group --&gt; L2Interface Group, if mpls is false;
307 * or MPLSInterface Group --&gt; L2Interface Group, if mpls is true;
Charles Chan188ebf52015-12-23 00:15:11 -0800308 * The returned 'inner' group description is always the L2 Interface group.
309 *
310 * @param treatment that needs to be broken up to create the group chain
311 * @param nextId of the next objective that needs this group chain
312 * @param appId of the application that sent this next objective
313 * @param mpls determines if L3Unicast or MPLSInterface group is created
314 * @param meta metadata passed in by the application as part of the nextObjective
315 * @return GroupInfo containing the GroupDescription of the
316 * L2Interface group(inner) and the GroupDescription of the (outer)
317 * L3Unicast/MPLSInterface group. May return null if there is an
318 * error in processing the chain
319 */
Charles Chan425854b2016-04-11 15:32:12 -0700320 protected GroupInfo createL2L3Chain(TrafficTreatment treatment, int nextId,
Charles Chanf9e98652016-09-07 16:54:23 -0700321 ApplicationId appId, boolean mpls,
322 TrafficSelector meta) {
323 return createL2L3ChainInternal(treatment, nextId, appId, mpls, meta, true);
324 }
325
326 /**
327 * Internal implementation of createL2L3Chain.
328 * <p>
329 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
330 * Since it is non-OF spec, we need an extension treatment for that.
331 * The useSetVlanExtension must be set to false for OFDPA i12.
332 * </p>
333 *
334 * @param treatment that needs to be broken up to create the group chain
335 * @param nextId of the next objective that needs this group chain
336 * @param appId of the application that sent this next objective
337 * @param mpls determines if L3Unicast or MPLSInterface group is created
338 * @param meta metadata passed in by the application as part of the nextObjective
339 * @param useSetVlanExtension use the setVlanVid extension that has is_present bit set to 0.
340 * @return GroupInfo containing the GroupDescription of the
341 * L2Interface group(inner) and the GroupDescription of the (outer)
342 * L3Unicast/MPLSInterface group. May return null if there is an
343 * error in processing the chain
344 */
345 protected GroupInfo createL2L3ChainInternal(TrafficTreatment treatment, int nextId,
Saurav Das8be4e3a2016-03-11 17:19:07 -0800346 ApplicationId appId, boolean mpls,
Charles Chanf9e98652016-09-07 16:54:23 -0700347 TrafficSelector meta, boolean useSetVlanExtension) {
Charles Chan188ebf52015-12-23 00:15:11 -0800348 // for the l2interface group, get vlan and port info
349 // for the outer group, get the src/dst mac, and vlan info
350 TrafficTreatment.Builder outerTtb = DefaultTrafficTreatment.builder();
351 TrafficTreatment.Builder innerTtb = DefaultTrafficTreatment.builder();
352 VlanId vlanid = null;
353 long portNum = 0;
354 boolean setVlan = false, popVlan = false;
Charles Chand0fd5dc2016-02-16 23:14:49 -0800355 MacAddress srcMac = MacAddress.ZERO;
Charles Chan5270ed02016-01-30 23:22:37 -0800356 MacAddress dstMac = MacAddress.ZERO;
Charles Chan188ebf52015-12-23 00:15:11 -0800357 for (Instruction ins : treatment.allInstructions()) {
358 if (ins.type() == Instruction.Type.L2MODIFICATION) {
359 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
360 switch (l2ins.subtype()) {
361 case ETH_DST:
Charles Chan5270ed02016-01-30 23:22:37 -0800362 dstMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
363 outerTtb.setEthDst(dstMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800364 break;
365 case ETH_SRC:
Charles Chand0fd5dc2016-02-16 23:14:49 -0800366 srcMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
367 outerTtb.setEthSrc(srcMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800368 break;
369 case VLAN_ID:
370 vlanid = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
Charles Chanf9e98652016-09-07 16:54:23 -0700371 if (useSetVlanExtension) {
372 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
373 outerTtb.extension(ofdpaSetVlanVid, deviceId);
374 } else {
375 outerTtb.setVlanId(vlanid);
376 }
Charles Chan188ebf52015-12-23 00:15:11 -0800377 setVlan = true;
378 break;
379 case VLAN_POP:
380 innerTtb.popVlan();
381 popVlan = true;
382 break;
383 case DEC_MPLS_TTL:
384 case MPLS_LABEL:
385 case MPLS_POP:
386 case MPLS_PUSH:
387 case VLAN_PCP:
388 case VLAN_PUSH:
389 default:
390 break;
391 }
392 } else if (ins.type() == Instruction.Type.OUTPUT) {
393 portNum = ((Instructions.OutputInstruction) ins).port().toLong();
394 innerTtb.add(ins);
395 } else {
396 log.warn("Driver does not handle this type of TrafficTreatment"
397 + " instruction in nextObjectives: {}", ins.type());
398 }
399 }
400
401 if (vlanid == null && meta != null) {
402 // use metadata if available
403 Criterion vidCriterion = meta.getCriterion(Criterion.Type.VLAN_VID);
404 if (vidCriterion != null) {
405 vlanid = ((VlanIdCriterion) vidCriterion).vlanId();
406 }
407 // if vlan is not set, use the vlan in metadata for outerTtb
408 if (vlanid != null && !setVlan) {
Charles Chanf9e98652016-09-07 16:54:23 -0700409 if (useSetVlanExtension) {
410 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
411 outerTtb.extension(ofdpaSetVlanVid, deviceId);
412 } else {
413 outerTtb.setVlanId(vlanid);
414 }
Charles Chan188ebf52015-12-23 00:15:11 -0800415 }
416 }
417
418 if (vlanid == null) {
419 log.error("Driver cannot process an L2/L3 group chain without "
420 + "egress vlan information for dev: {} port:{}",
421 deviceId, portNum);
422 return null;
423 }
424
425 if (!setVlan && !popVlan) {
426 // untagged outgoing port
427 TrafficTreatment.Builder temp = DefaultTrafficTreatment.builder();
428 temp.popVlan();
429 innerTtb.build().allInstructions().forEach(i -> temp.add(i));
430 innerTtb = temp;
431 }
432
433 // assemble information for ofdpa l2interface group
Charles Chane849c192016-01-11 18:28:54 -0800434 int l2groupId = L2_INTERFACE_TYPE | (vlanid.toShort() << 16) | (int) portNum;
Saurav Das8be4e3a2016-03-11 17:19:07 -0800435 // a globally unique groupkey that is different for ports in the same device,
Charles Chan188ebf52015-12-23 00:15:11 -0800436 // but different for the same portnumber on different devices. Also different
437 // for the various group-types created out of the same next objective.
Charles Chane849c192016-01-11 18:28:54 -0800438 int l2gk = l2InterfaceGroupKey(deviceId, vlanid, portNum);
Charles Chan361154b2016-03-24 10:23:39 -0700439 final GroupKey l2groupkey = new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(l2gk));
Charles Chan188ebf52015-12-23 00:15:11 -0800440
441 // assemble information for outer group
442 GroupDescription outerGrpDesc = null;
443 if (mpls) {
444 // outer group is MPLSInteface
Saurav Das8be4e3a2016-03-11 17:19:07 -0800445 int mplsInterfaceIndex = getNextAvailableIndex();
446 int mplsgroupId = MPLS_INTERFACE_TYPE | (SUBTYPE_MASK & mplsInterfaceIndex);
447 final GroupKey mplsgroupkey = new DefaultGroupKey(
Charles Chan361154b2016-03-24 10:23:39 -0700448 Ofdpa2Pipeline.appKryo.serialize(mplsInterfaceIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800449 outerTtb.group(new DefaultGroupId(l2groupId));
450 // create the mpls-interface group description to wait for the
451 // l2 interface group to be processed
452 GroupBucket mplsinterfaceGroupBucket =
453 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
454 outerGrpDesc = new DefaultGroupDescription(
455 deviceId,
456 GroupDescription.Type.INDIRECT,
457 new GroupBuckets(Collections.singletonList(
458 mplsinterfaceGroupBucket)),
459 mplsgroupkey,
460 mplsgroupId,
461 appId);
462 log.debug("Trying MPLS-Interface: device:{} gid:{} gkey:{} nextid:{}",
463 deviceId, Integer.toHexString(mplsgroupId),
464 mplsgroupkey, nextId);
465 } else {
466 // outer group is L3Unicast
Saurav Das8be4e3a2016-03-11 17:19:07 -0800467 int l3unicastIndex = getNextAvailableIndex();
468 int l3groupId = L3_UNICAST_TYPE | (TYPE_MASK & l3unicastIndex);
469 final GroupKey l3groupkey = new DefaultGroupKey(
Charles Chan361154b2016-03-24 10:23:39 -0700470 Ofdpa2Pipeline.appKryo.serialize(l3unicastIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800471 outerTtb.group(new DefaultGroupId(l2groupId));
472 // create the l3unicast group description to wait for the
473 // l2 interface group to be processed
474 GroupBucket l3unicastGroupBucket =
475 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
476 outerGrpDesc = new DefaultGroupDescription(
477 deviceId,
478 GroupDescription.Type.INDIRECT,
479 new GroupBuckets(Collections.singletonList(
480 l3unicastGroupBucket)),
481 l3groupkey,
482 l3groupId,
483 appId);
484 log.debug("Trying L3Unicast: device:{} gid:{} gkey:{} nextid:{}",
485 deviceId, Integer.toHexString(l3groupId),
486 l3groupkey, nextId);
487 }
488
489 // store l2groupkey with the groupChainElem for the outer-group that depends on it
490 GroupChainElem gce = new GroupChainElem(outerGrpDesc, 1, false);
491 updatePendingGroups(l2groupkey, gce);
492
493 // create group description for the inner l2interfacegroup
Charles Chan5b9df8d2016-03-28 22:21:40 -0700494 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800495 DefaultGroupBucket.createIndirectGroupBucket(innerTtb.build());
496 GroupDescription l2groupDescription =
497 new DefaultGroupDescription(
498 deviceId,
499 GroupDescription.Type.INDIRECT,
500 new GroupBuckets(Collections.singletonList(
Charles Chan5b9df8d2016-03-28 22:21:40 -0700501 l2InterfaceGroupBucket)),
Charles Chan188ebf52015-12-23 00:15:11 -0800502 l2groupkey,
503 l2groupId,
504 appId);
505 log.debug("Trying L2Interface: device:{} gid:{} gkey:{} nextId:{}",
506 deviceId, Integer.toHexString(l2groupId),
507 l2groupkey, nextId);
508 return new GroupInfo(l2groupDescription, outerGrpDesc);
509
510 }
511
512 /**
513 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
514 * a chain of groups. The broadcast Next Objective passed in by the application
515 * has to be broken up into a group chain comprising of an
Saurav Das1a129a02016-11-18 15:21:57 -0800516 * L2 Flood group or L3 Multicast group, whose buckets point to L2 Interface groups.
Charles Chan188ebf52015-12-23 00:15:11 -0800517 *
518 * @param nextObj the nextObjective of type BROADCAST
519 */
520 private void processBroadcastNextObjective(NextObjective nextObj) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700521 VlanId assignedVlan = Ofdpa2Pipeline.readVlanFromSelector(nextObj.meta());
522 if (assignedVlan == null) {
523 log.warn("VLAN ID required by broadcast next obj is missing. Abort.");
524 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800525 return;
526 }
Charles Chan188ebf52015-12-23 00:15:11 -0800527
Charles Chan5b9df8d2016-03-28 22:21:40 -0700528 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
529
530 IpPrefix ipDst = Ofdpa2Pipeline.readIpDstFromSelector(nextObj.meta());
531 if (ipDst != null) {
532 if (ipDst.isMulticast()) {
533 createL3MulticastGroup(nextObj, assignedVlan, groupInfos);
534 } else {
535 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
536 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
537 return;
538 }
539 } else {
540 createL2FloodGroup(nextObj, assignedVlan, groupInfos);
541 }
542 }
543
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700544 private List<GroupInfo> prepareL2InterfaceGroup(NextObjective nextObj,
545 VlanId assignedVlan) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700546 ImmutableList.Builder<GroupInfo> groupInfoBuilder = ImmutableList.builder();
547
548 // break up broadcast next objective to multiple groups
549 Collection<TrafficTreatment> buckets = nextObj.next();
550
Charles Chan188ebf52015-12-23 00:15:11 -0800551 // each treatment is converted to an L2 interface group
Charles Chan188ebf52015-12-23 00:15:11 -0800552 for (TrafficTreatment treatment : buckets) {
553 TrafficTreatment.Builder newTreatment = DefaultTrafficTreatment.builder();
554 PortNumber portNum = null;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700555 VlanId egressVlan = null;
Charles Chan188ebf52015-12-23 00:15:11 -0800556 // ensure that the only allowed treatments are pop-vlan and output
557 for (Instruction ins : treatment.allInstructions()) {
558 if (ins.type() == Instruction.Type.L2MODIFICATION) {
559 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
560 switch (l2ins.subtype()) {
561 case VLAN_POP:
562 newTreatment.add(l2ins);
563 break;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700564 case VLAN_ID:
565 egressVlan = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
566 break;
Charles Chan188ebf52015-12-23 00:15:11 -0800567 default:
568 log.debug("action {} not permitted for broadcast nextObj",
569 l2ins.subtype());
570 break;
571 }
572 } else if (ins.type() == Instruction.Type.OUTPUT) {
573 portNum = ((Instructions.OutputInstruction) ins).port();
574 newTreatment.add(ins);
575 } else {
Charles Chane849c192016-01-11 18:28:54 -0800576 log.debug("TrafficTreatment of type {} not permitted in " +
577 " broadcast nextObjective", ins.type());
Charles Chan188ebf52015-12-23 00:15:11 -0800578 }
579 }
580
Charles Chan188ebf52015-12-23 00:15:11 -0800581 // assemble info for l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700582 VlanId l2InterfaceGroupVlan =
583 (egressVlan != null && !assignedVlan.equals(egressVlan)) ?
584 egressVlan : assignedVlan;
585 int l2gk = l2InterfaceGroupKey(deviceId, l2InterfaceGroupVlan, portNum.toLong());
586 final GroupKey l2InterfaceGroupKey =
587 new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(l2gk));
588 int l2InterfaceGroupId = L2_INTERFACE_TYPE | (l2InterfaceGroupVlan.toShort() << 16) |
Charles Chan188ebf52015-12-23 00:15:11 -0800589 (int) portNum.toLong();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700590 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800591 DefaultGroupBucket.createIndirectGroupBucket(newTreatment.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700592 GroupDescription l2InterfaceGroupDescription =
Charles Chan188ebf52015-12-23 00:15:11 -0800593 new DefaultGroupDescription(
594 deviceId,
595 GroupDescription.Type.INDIRECT,
596 new GroupBuckets(Collections.singletonList(
Charles Chan5b9df8d2016-03-28 22:21:40 -0700597 l2InterfaceGroupBucket)),
598 l2InterfaceGroupKey,
599 l2InterfaceGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800600 nextObj.appId());
601 log.debug("Trying L2-Interface: device:{} gid:{} gkey:{} nextid:{}",
Charles Chan5b9df8d2016-03-28 22:21:40 -0700602 deviceId, Integer.toHexString(l2InterfaceGroupId),
603 l2InterfaceGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -0800604
Charles Chan5b9df8d2016-03-28 22:21:40 -0700605 groupInfoBuilder.add(new GroupInfo(l2InterfaceGroupDescription,
606 l2InterfaceGroupDescription));
Charles Chan188ebf52015-12-23 00:15:11 -0800607 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700608 return groupInfoBuilder.build();
609 }
Charles Chan188ebf52015-12-23 00:15:11 -0800610
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700611 private void createL2FloodGroup(NextObjective nextObj, VlanId vlanId,
612 List<GroupInfo> groupInfos) {
613 // assemble info for l2 flood group. Since there can be only one flood
614 // group for a vlan, its index is always the same - 0
Saurav Das0fd79d92016-03-07 10:58:36 -0800615 Integer l2floodgroupId = L2_FLOOD_TYPE | (vlanId.toShort() << 16);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800616 int l2floodgk = getNextAvailableIndex();
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700617 final GroupKey l2floodgroupkey =
618 new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(l2floodgk));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700619
Charles Chan188ebf52015-12-23 00:15:11 -0800620 // collection of group buckets pointing to all the l2 interface groups
Charles Chan5b9df8d2016-03-28 22:21:40 -0700621 List<GroupBucket> l2floodBuckets = Lists.newArrayList();
622 groupInfos.forEach(groupInfo -> {
623 GroupDescription l2intGrpDesc = groupInfo.nextGroupDesc;
Charles Chan188ebf52015-12-23 00:15:11 -0800624 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
625 ttb.group(new DefaultGroupId(l2intGrpDesc.givenGroupId()));
626 GroupBucket abucket = DefaultGroupBucket.createAllGroupBucket(ttb.build());
627 l2floodBuckets.add(abucket);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700628 });
Charles Chan188ebf52015-12-23 00:15:11 -0800629 // create the l2flood group-description to wait for all the
630 // l2interface groups to be processed
631 GroupDescription l2floodGroupDescription =
632 new DefaultGroupDescription(
633 deviceId,
634 GroupDescription.Type.ALL,
635 new GroupBuckets(l2floodBuckets),
636 l2floodgroupkey,
637 l2floodgroupId,
638 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800639 log.debug("Trying L2-Flood: device:{} gid:{} gkey:{} nextid:{}",
640 deviceId, Integer.toHexString(l2floodgroupId),
641 l2floodgroupkey, nextObj.id());
642
Charles Chan5b9df8d2016-03-28 22:21:40 -0700643 // Put all dependency information into allGroupKeys
644 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
645 groupInfos.forEach(groupInfo -> {
646 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
647 // In this case we should have L2 interface group only
648 gkeyChain.addFirst(groupInfo.nextGroupDesc.appCookie());
649 gkeyChain.addFirst(l2floodgroupkey);
650 allGroupKeys.add(gkeyChain);
651 });
Charles Chan188ebf52015-12-23 00:15:11 -0800652
Charles Chan5b9df8d2016-03-28 22:21:40 -0700653 // Point the next objective to this group
654 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Charles Chan188ebf52015-12-23 00:15:11 -0800655 updatePendingNextObjective(l2floodgroupkey, ofdpaGrp);
656
Charles Chan5b9df8d2016-03-28 22:21:40 -0700657 GroupChainElem gce = new GroupChainElem(l2floodGroupDescription,
658 groupInfos.size(), false);
659 groupInfos.forEach(groupInfo -> {
660 // Point this group to the next group
661 updatePendingGroups(groupInfo.nextGroupDesc.appCookie(), gce);
662 // Start installing the inner-most group
663 groupService.addGroup(groupInfo.innerMostGroupDesc);
664 });
665 }
666
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700667 private void createL3MulticastGroup(NextObjective nextObj, VlanId vlanId,
668 List<GroupInfo> groupInfos) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700669 List<GroupBucket> l3McastBuckets = new ArrayList<>();
670 groupInfos.forEach(groupInfo -> {
671 // Points to L3 interface group if there is one.
672 // Otherwise points to L2 interface group directly.
673 GroupDescription nextGroupDesc = (groupInfo.nextGroupDesc != null) ?
674 groupInfo.nextGroupDesc : groupInfo.innerMostGroupDesc;
675 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
676 ttb.group(new DefaultGroupId(nextGroupDesc.givenGroupId()));
677 GroupBucket abucket = DefaultGroupBucket.createAllGroupBucket(ttb.build());
678 l3McastBuckets.add(abucket);
679 });
680
681 int l3MulticastIndex = getNextAvailableIndex();
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700682 int l3MulticastGroupId = L3_MULTICAST_TYPE |
683 vlanId.toShort() << 16 | (TYPE_VLAN_MASK & l3MulticastIndex);
684 final GroupKey l3MulticastGroupKey =
685 new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(l3MulticastIndex));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700686
687 GroupDescription l3MulticastGroupDesc = new DefaultGroupDescription(deviceId,
688 GroupDescription.Type.ALL,
689 new GroupBuckets(l3McastBuckets),
690 l3MulticastGroupKey,
691 l3MulticastGroupId,
692 nextObj.appId());
693
694 // Put all dependency information into allGroupKeys
695 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
696 groupInfos.forEach(groupInfo -> {
697 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
698 gkeyChain.addFirst(groupInfo.innerMostGroupDesc.appCookie());
699 // Add L3 interface group to the chain if there is one.
700 if (!groupInfo.nextGroupDesc.equals(groupInfo.innerMostGroupDesc)) {
701 gkeyChain.addFirst(groupInfo.nextGroupDesc.appCookie());
702 }
703 gkeyChain.addFirst(l3MulticastGroupKey);
704 allGroupKeys.add(gkeyChain);
705 });
706
707 // Point the next objective to this group
708 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
709 updatePendingNextObjective(l3MulticastGroupKey, ofdpaGrp);
710
711 GroupChainElem outerGce = new GroupChainElem(l3MulticastGroupDesc,
712 groupInfos.size(), false);
713 groupInfos.forEach(groupInfo -> {
714 // Point this group (L3 multicast) to the next group
715 updatePendingGroups(groupInfo.nextGroupDesc.appCookie(), outerGce);
716
717 // Point next group to inner-most group, if any
718 if (!groupInfo.nextGroupDesc.equals(groupInfo.innerMostGroupDesc)) {
719 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc,
720 1, false);
721 updatePendingGroups(groupInfo.innerMostGroupDesc.appCookie(), innerGce);
722 }
723
724 // Start installing the inner-most group
725 groupService.addGroup(groupInfo.innerMostGroupDesc);
726 });
Charles Chan188ebf52015-12-23 00:15:11 -0800727 }
728
Charles Chan188ebf52015-12-23 00:15:11 -0800729 /**
730 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
731 * a chain of groups. The hashed Next Objective passed in by the application
732 * has to be broken up into a group chain comprising of an
733 * L3 ECMP group as the top level group. Buckets of this group can point
734 * to a variety of groups in a group chain, depending on the whether
735 * MPLS labels are being pushed or not.
736 * <p>
737 * NOTE: We do not create MPLS ECMP groups as they are unimplemented in
738 * OF-DPA 2.0 (even though it is in the spec). Therefore we do not
739 * check the nextObjective meta to see what is matching before being
740 * sent to this nextObjective.
741 *
742 * @param nextObj the nextObjective of type HASHED
743 */
744 private void processHashedNextObjective(NextObjective nextObj) {
745 // storage for all group keys in the chain of groups created
746 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
747 List<GroupInfo> unsentGroups = new ArrayList<>();
748 createHashBucketChains(nextObj, allGroupKeys, unsentGroups);
749
750 // now we can create the outermost L3 ECMP group
751 List<GroupBucket> l3ecmpGroupBuckets = new ArrayList<>();
752 for (GroupInfo gi : unsentGroups) {
753 // create ECMP bucket to point to the outer group
754 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700755 ttb.group(new DefaultGroupId(gi.nextGroupDesc.givenGroupId()));
Charles Chan188ebf52015-12-23 00:15:11 -0800756 GroupBucket sbucket = DefaultGroupBucket
757 .createSelectGroupBucket(ttb.build());
758 l3ecmpGroupBuckets.add(sbucket);
759 }
Saurav Das8be4e3a2016-03-11 17:19:07 -0800760 int l3ecmpIndex = getNextAvailableIndex();
761 int l3ecmpGroupId = L3_ECMP_TYPE | (TYPE_MASK & l3ecmpIndex);
762 GroupKey l3ecmpGroupKey = new DefaultGroupKey(
Charles Chan361154b2016-03-24 10:23:39 -0700763 Ofdpa2Pipeline.appKryo.serialize(l3ecmpIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800764 GroupDescription l3ecmpGroupDesc =
765 new DefaultGroupDescription(
766 deviceId,
767 GroupDescription.Type.SELECT,
768 new GroupBuckets(l3ecmpGroupBuckets),
769 l3ecmpGroupKey,
770 l3ecmpGroupId,
771 nextObj.appId());
772 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
773 l3ecmpGroupBuckets.size(),
774 false);
775
776 // create objects for local and distributed storage
777 allGroupKeys.forEach(gkeyChain -> gkeyChain.addFirst(l3ecmpGroupKey));
778 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
779
780 // store l3ecmpGroupKey with the ofdpaGroupChain for the nextObjective
781 // that depends on it
782 updatePendingNextObjective(l3ecmpGroupKey, ofdpaGrp);
783
784 log.debug("Trying L3ECMP: device:{} gid:{} gkey:{} nextId:{}",
785 deviceId, Integer.toHexString(l3ecmpGroupId),
786 l3ecmpGroupKey, nextObj.id());
787 // finally we are ready to send the innermost groups
788 for (GroupInfo gi : unsentGroups) {
789 log.debug("Sending innermost group {} in group chain on device {} ",
Charles Chan5b9df8d2016-03-28 22:21:40 -0700790 Integer.toHexString(gi.innerMostGroupDesc.givenGroupId()), deviceId);
791 updatePendingGroups(gi.nextGroupDesc.appCookie(), l3ecmpGce);
792 groupService.addGroup(gi.innerMostGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800793 }
794
795 }
796
797 /**
798 * Creates group chains for all buckets in a hashed group, and stores the
799 * GroupInfos and GroupKeys for all the groups in the lists passed in, which
800 * should be empty.
801 * <p>
802 * Does not create the top level ECMP group. Does not actually send the
803 * groups to the groupService.
804 *
805 * @param nextObj the Next Objective with buckets that need to be converted
806 * to group chains
807 * @param allGroupKeys a list to store groupKey for each bucket-group-chain
808 * @param unsentGroups a list to store GroupInfo for each bucket-group-chain
809 */
810 private void createHashBucketChains(NextObjective nextObj,
Saurav Das8be4e3a2016-03-11 17:19:07 -0800811 List<Deque<GroupKey>> allGroupKeys,
812 List<GroupInfo> unsentGroups) {
Charles Chan188ebf52015-12-23 00:15:11 -0800813 // break up hashed next objective to multiple groups
814 Collection<TrafficTreatment> buckets = nextObj.next();
815
816 for (TrafficTreatment bucket : buckets) {
817 //figure out how many labels are pushed in each bucket
818 int labelsPushed = 0;
819 MplsLabel innermostLabel = null;
820 for (Instruction ins : bucket.allInstructions()) {
821 if (ins.type() == Instruction.Type.L2MODIFICATION) {
822 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
823 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_PUSH) {
824 labelsPushed++;
825 }
826 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) {
827 if (innermostLabel == null) {
Ray Milkey125572b2016-02-22 16:48:17 -0800828 innermostLabel = ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label();
Charles Chan188ebf52015-12-23 00:15:11 -0800829 }
830 }
831 }
832 }
833
834 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
835 // XXX we only deal with 0 and 1 label push right now
836 if (labelsPushed == 0) {
837 GroupInfo nolabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
838 nextObj.appId(), false,
839 nextObj.meta());
840 if (nolabelGroupInfo == null) {
841 log.error("Could not process nextObj={} in dev:{}",
842 nextObj.id(), deviceId);
843 return;
844 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700845 gkeyChain.addFirst(nolabelGroupInfo.innerMostGroupDesc.appCookie());
846 gkeyChain.addFirst(nolabelGroupInfo.nextGroupDesc.appCookie());
Charles Chan188ebf52015-12-23 00:15:11 -0800847
848 // we can't send the inner group description yet, as we have to
849 // create the dependent ECMP group first. So we store..
850 unsentGroups.add(nolabelGroupInfo);
851
852 } else if (labelsPushed == 1) {
853 GroupInfo onelabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
854 nextObj.appId(), true,
855 nextObj.meta());
856 if (onelabelGroupInfo == null) {
857 log.error("Could not process nextObj={} in dev:{}",
858 nextObj.id(), deviceId);
859 return;
860 }
861 // we need to add another group to this chain - the L3VPN group
862 TrafficTreatment.Builder l3vpnTtb = DefaultTrafficTreatment.builder();
863 l3vpnTtb.pushMpls()
864 .setMpls(innermostLabel)
Charles Chan40132b32017-01-22 00:19:37 -0800865 .group(new DefaultGroupId(onelabelGroupInfo.nextGroupDesc.givenGroupId()));
866 if (supportCopyTtl()) {
867 l3vpnTtb.copyTtlOut();
868 }
869 if (supportSetMplsBos()) {
870 l3vpnTtb.setMplsBos(true);
871 }
872
Charles Chan188ebf52015-12-23 00:15:11 -0800873 GroupBucket l3vpnGrpBkt =
874 DefaultGroupBucket.createIndirectGroupBucket(l3vpnTtb.build());
Saurav Das8be4e3a2016-03-11 17:19:07 -0800875 int l3vpnIndex = getNextAvailableIndex();
876 int l3vpngroupId = MPLS_L3VPN_SUBTYPE | (SUBTYPE_MASK & l3vpnIndex);
877 GroupKey l3vpngroupkey = new DefaultGroupKey(
Charles Chan361154b2016-03-24 10:23:39 -0700878 Ofdpa2Pipeline.appKryo.serialize(l3vpnIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800879 GroupDescription l3vpnGroupDesc =
880 new DefaultGroupDescription(
881 deviceId,
882 GroupDescription.Type.INDIRECT,
883 new GroupBuckets(Collections.singletonList(
884 l3vpnGrpBkt)),
885 l3vpngroupkey,
886 l3vpngroupId,
887 nextObj.appId());
888 GroupChainElem l3vpnGce = new GroupChainElem(l3vpnGroupDesc, 1, false);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700889 updatePendingGroups(onelabelGroupInfo.nextGroupDesc.appCookie(), l3vpnGce);
Charles Chan188ebf52015-12-23 00:15:11 -0800890
Charles Chan5b9df8d2016-03-28 22:21:40 -0700891 gkeyChain.addFirst(onelabelGroupInfo.innerMostGroupDesc.appCookie());
892 gkeyChain.addFirst(onelabelGroupInfo.nextGroupDesc.appCookie());
Charles Chan188ebf52015-12-23 00:15:11 -0800893 gkeyChain.addFirst(l3vpngroupkey);
894
895 //now we can replace the outerGrpDesc with the one we just created
Charles Chan5b9df8d2016-03-28 22:21:40 -0700896 onelabelGroupInfo.nextGroupDesc = l3vpnGroupDesc;
Charles Chan188ebf52015-12-23 00:15:11 -0800897
898 // we can't send the innermost group yet, as we have to create
899 // the dependent ECMP group first. So we store ...
900 unsentGroups.add(onelabelGroupInfo);
901
902 log.debug("Trying L3VPN: device:{} gid:{} gkey:{} nextId:{}",
903 deviceId, Integer.toHexString(l3vpngroupId),
904 l3vpngroupkey, nextObj.id());
905
906 } else {
907 log.warn("Driver currently does not handle more than 1 MPLS "
908 + "labels. Not processing nextObjective {}", nextObj.id());
909 return;
910 }
911
912 // all groups in this chain
913 allGroupKeys.add(gkeyChain);
914 }
915 }
916
Saurav Das8be4e3a2016-03-11 17:19:07 -0800917 //////////////////////////////////////
918 // Group Editing
919 //////////////////////////////////////
920
Charles Chan188ebf52015-12-23 00:15:11 -0800921 /**
922 * Adds a bucket to the top level group of a group-chain, and creates the chain.
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700923 * Ensures that bucket being added is not a duplicate, by checking existing
924 * buckets for the same outport.
Charles Chan188ebf52015-12-23 00:15:11 -0800925 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700926 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -0800927 * @param next the representation of the existing group-chain for this next objective
928 */
929 protected void addBucketToGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -0800930 if (nextObjective.type() != NextObjective.Type.HASHED &&
931 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -0800932 log.warn("AddBuckets not applied to nextType:{} in dev:{} for next:{}",
933 nextObjective.type(), deviceId, nextObjective.id());
Saurav Das1a129a02016-11-18 15:21:57 -0800934 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -0800935 return;
936 }
937 if (nextObjective.next().size() > 1) {
Saurav Das1a129a02016-11-18 15:21:57 -0800938 // FIXME - support editing multiple buckets CORD-555
Charles Chan188ebf52015-12-23 00:15:11 -0800939 log.warn("Only one bucket can be added at a time");
Saurav Das1a129a02016-11-18 15:21:57 -0800940 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -0800941 return;
942 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700943 // first check to see if bucket being added is not a duplicate of an
944 // existing bucket. If it is for an existing outport, then its a duplicate.
945 Set<PortNumber> existingOutPorts = new HashSet<>();
946 List<Deque<GroupKey>> allActiveKeys = Ofdpa2Pipeline.appKryo.deserialize(next.data());
947 for (Deque<GroupKey> gkeys : allActiveKeys) {
948 // get the last group for the outport
949 Group glast = groupService.getGroup(deviceId, gkeys.peekLast());
950 if (glast != null && !glast.buckets().buckets().isEmpty()) {
951 PortNumber op = readOutPortFromTreatment(
952 glast.buckets().buckets().get(0).treatment());
953 if (op != null) {
954 existingOutPorts.add(op);
955 }
956 }
957 }
958 // only a single bucket being added
959 TrafficTreatment tt = nextObjective.next().iterator().next();
960 PortNumber newport = readOutPortFromTreatment(tt);
961 if (existingOutPorts.contains(newport)) {
Charles Chane5ccd582016-11-10 14:16:54 -0800962 log.info("Attempt to add bucket for existing outport:{} in dev:{} for next:{}",
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700963 newport, deviceId, nextObjective.id());
964 return;
965 }
Saurav Das1a129a02016-11-18 15:21:57 -0800966 if (nextObjective.type() == NextObjective.Type.HASHED) {
967 addBucketToHashGroup(nextObjective, allActiveKeys, newport);
968 } else if (nextObjective.type() == NextObjective.Type.BROADCAST) {
969 addBucketToBroadcastGroup(nextObjective, allActiveKeys, newport);
970 }
971 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700972
Saurav Das1a129a02016-11-18 15:21:57 -0800973 private void addBucketToHashGroup(NextObjective nextObjective,
974 List<Deque<GroupKey>> allActiveKeys,
975 PortNumber newport) {
Charles Chan188ebf52015-12-23 00:15:11 -0800976 // storage for all group keys in the chain of groups created
977 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
978 List<GroupInfo> unsentGroups = new ArrayList<>();
979 createHashBucketChains(nextObjective, allGroupKeys, unsentGroups);
980
Saurav Das1a129a02016-11-18 15:21:57 -0800981 // now we can create the bucket to add to the outermost L3 ECMP group
Charles Chan188ebf52015-12-23 00:15:11 -0800982 GroupInfo gi = unsentGroups.get(0); // only one bucket, so only one group-chain
983 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700984 ttb.group(new DefaultGroupId(gi.nextGroupDesc.givenGroupId()));
Charles Chan188ebf52015-12-23 00:15:11 -0800985 GroupBucket sbucket = DefaultGroupBucket.createSelectGroupBucket(ttb.build());
986
Saurav Das1a129a02016-11-18 15:21:57 -0800987 // retrieve the original L3 ECMP group
988 Group l3ecmpGroup = retrieveTopLevelGroup(allActiveKeys, nextObjective.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700989 if (l3ecmpGroup == null) {
Saurav Das1a129a02016-11-18 15:21:57 -0800990 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.GROUPMISSING);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700991 return;
992 }
Saurav Das1a129a02016-11-18 15:21:57 -0800993 GroupKey l3ecmpGroupKey = l3ecmpGroup.appCookie();
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700994 int l3ecmpGroupId = l3ecmpGroup.id().id();
Charles Chan188ebf52015-12-23 00:15:11 -0800995
996 // Although GroupDescriptions are not necessary for adding buckets to
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700997 // existing groups, we still use one in the GroupChainElem. When the latter is
Charles Chan188ebf52015-12-23 00:15:11 -0800998 // processed, the info will be extracted for the bucketAdd call to groupService
999 GroupDescription l3ecmpGroupDesc =
1000 new DefaultGroupDescription(
1001 deviceId,
1002 GroupDescription.Type.SELECT,
1003 new GroupBuckets(Collections.singletonList(sbucket)),
1004 l3ecmpGroupKey,
1005 l3ecmpGroupId,
1006 nextObjective.appId());
1007 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc, 1, true);
1008
1009 // update original NextGroup with new bucket-chain
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001010 // If active keys shows only the top-level group without a chain of groups,
1011 // then it represents an empty group. Update by replacing empty chain.
Charles Chan188ebf52015-12-23 00:15:11 -08001012 Deque<GroupKey> newBucketChain = allGroupKeys.get(0);
1013 newBucketChain.addFirst(l3ecmpGroupKey);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001014 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1015 allActiveKeys.clear();
1016 }
1017 allActiveKeys.add(newBucketChain);
1018 updatePendingNextObjective(l3ecmpGroupKey,
1019 new OfdpaNextGroup(allActiveKeys, nextObjective));
Charles Chan188ebf52015-12-23 00:15:11 -08001020 log.debug("Adding to L3ECMP: device:{} gid:{} gkey:{} nextId:{}",
1021 deviceId, Integer.toHexString(l3ecmpGroupId),
1022 l3ecmpGroupKey, nextObjective.id());
1023 // send the innermost group
1024 log.debug("Sending innermost group {} in group chain on device {} ",
Charles Chan5b9df8d2016-03-28 22:21:40 -07001025 Integer.toHexString(gi.innerMostGroupDesc.givenGroupId()), deviceId);
1026 updatePendingGroups(gi.nextGroupDesc.appCookie(), l3ecmpGce);
1027 groupService.addGroup(gi.innerMostGroupDesc);
Saurav Das1a129a02016-11-18 15:21:57 -08001028 }
Charles Chan188ebf52015-12-23 00:15:11 -08001029
Saurav Das1a129a02016-11-18 15:21:57 -08001030 private void addBucketToBroadcastGroup(NextObjective nextObj,
1031 List<Deque<GroupKey>> allActiveKeys,
1032 PortNumber newport) {
1033 VlanId assignedVlan = Ofdpa2Pipeline.readVlanFromSelector(nextObj.meta());
1034 if (assignedVlan == null) {
1035 log.warn("VLAN ID required by broadcast next obj is missing. "
1036 + "Aborting add bucket to broadcast group for next:{} in dev:{}",
1037 nextObj.id(), deviceId);
1038 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
1039 return;
1040 }
1041
1042 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
1043
1044 IpPrefix ipDst = Ofdpa2Pipeline.readIpDstFromSelector(nextObj.meta());
1045 if (ipDst != null) {
1046 if (ipDst.isMulticast()) {
1047 addBucketToL3MulticastGroup(nextObj, allActiveKeys,
1048 groupInfos, assignedVlan, newport);
1049 } else {
1050 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
1051 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
1052 return;
1053 }
1054 } else {
1055 addBucketToL2FloodGroup(nextObj, allActiveKeys,
1056 groupInfos, assignedVlan, newport);
1057 }
1058 }
1059
1060 private void addBucketToL2FloodGroup(NextObjective nextObj,
1061 List<Deque<GroupKey>> allActiveKeys,
1062 List<GroupInfo> groupInfos,
1063 VlanId assignedVlan,
1064 PortNumber newport) {
1065 // create the bucket to add to the outermost L2 Flood group
1066 GroupInfo groupInfo = groupInfos.get(0); // only one bucket to add
1067 GroupDescription l2intGrpDesc = groupInfo.nextGroupDesc;
1068 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
1069 ttb.group(new DefaultGroupId(l2intGrpDesc.givenGroupId()));
1070 GroupBucket abucket = DefaultGroupBucket.createAllGroupBucket(ttb.build());
1071 // get the group being edited
1072 Group l2floodGroup = retrieveTopLevelGroup(allActiveKeys, nextObj.id());
1073 if (l2floodGroup == null) {
1074 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.GROUPMISSING);
1075 return;
1076 }
1077 GroupKey l2floodGroupKey = l2floodGroup.appCookie();
1078 int l2floodGroupId = l2floodGroup.id().id();
1079
1080 //ensure assignedVlan applies to the chosen group
1081 VlanId expectedVlan = VlanId.vlanId((short) ((l2floodGroupId & 0x0fff0000) >> 16));
1082 if (!expectedVlan.equals(assignedVlan)) {
1083 log.warn("VLAN ID {} does not match Flood group {} to which bucket is "
1084 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1085 Integer.toHexString(l2floodGroupId), nextObj.id(), deviceId);
1086 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
1087 }
1088 GroupDescription l2floodGroupDescription =
1089 new DefaultGroupDescription(
1090 deviceId,
1091 GroupDescription.Type.ALL,
1092 new GroupBuckets(Collections.singletonList(abucket)),
1093 l2floodGroupKey,
1094 l2floodGroupId,
1095 nextObj.appId());
1096 GroupChainElem l2floodGce = new GroupChainElem(l2floodGroupDescription, 1, true);
1097
1098 // update original NextGroup with new bucket-chain
1099 // If active keys shows only the top-level group without a chain of groups,
1100 // then it represents an empty group. Update by replacing empty chain.
1101 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
1102 newBucketChain.addFirst(groupInfo.nextGroupDesc.appCookie());
1103 newBucketChain.addFirst(l2floodGroupKey);
1104 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1105 allActiveKeys.clear();
1106 }
1107 allActiveKeys.add(newBucketChain);
1108 updatePendingNextObjective(l2floodGroupKey,
1109 new OfdpaNextGroup(allActiveKeys, nextObj));
1110 log.debug("Adding to L2FLOOD: device:{} gid:{} gkey:{} nextId:{}",
1111 deviceId, Integer.toHexString(l2floodGroupId),
1112 l2floodGroupKey, nextObj.id());
1113 // send the innermost group
1114 log.debug("Sending innermost group {} in group chain on device {} ",
1115 Integer.toHexString(groupInfo.innerMostGroupDesc.givenGroupId()),
1116 deviceId);
1117 updatePendingGroups(groupInfo.nextGroupDesc.appCookie(), l2floodGce);
1118 groupService.addGroup(groupInfo.innerMostGroupDesc);
1119 }
1120
1121 private void addBucketToL3MulticastGroup(NextObjective nextObj,
1122 List<Deque<GroupKey>> allActiveKeys,
1123 List<GroupInfo> groupInfos,
1124 VlanId assignedVlan,
1125 PortNumber newport) {
1126 // create the bucket to add to the outermost L3 Multicast group
1127 GroupInfo groupInfo = groupInfos.get(0); // only one bucket to add
1128 // Points to L3 interface group if there is one.
1129 // Otherwise points to L2 interface group directly.
1130 GroupDescription nextGroupDesc = (groupInfo.nextGroupDesc != null) ?
1131 groupInfo.nextGroupDesc : groupInfo.innerMostGroupDesc;
1132 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
1133 ttb.group(new DefaultGroupId(nextGroupDesc.givenGroupId()));
1134 GroupBucket abucket = DefaultGroupBucket.createAllGroupBucket(ttb.build());
1135
1136 // get the group being edited
1137 Group l3mcastGroup = retrieveTopLevelGroup(allActiveKeys, nextObj.id());
1138 if (l3mcastGroup == null) {
1139 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.GROUPMISSING);
1140 return;
1141 }
1142 GroupKey l3mcastGroupKey = l3mcastGroup.appCookie();
1143 int l3mcastGroupId = l3mcastGroup.id().id();
1144
1145 //ensure assignedVlan applies to the chosen group
1146 VlanId expectedVlan = VlanId.vlanId((short) ((l3mcastGroupId & 0x0fff0000) >> 16));
1147 if (!expectedVlan.equals(assignedVlan)) {
1148 log.warn("VLAN ID {} does not match L3 Mcast group {} to which bucket is "
1149 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1150 Integer.toHexString(l3mcastGroupId), nextObj.id(), deviceId);
1151 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
1152 }
1153 GroupDescription l3mcastGroupDescription =
1154 new DefaultGroupDescription(
1155 deviceId,
1156 GroupDescription.Type.ALL,
1157 new GroupBuckets(Collections.singletonList(abucket)),
1158 l3mcastGroupKey,
1159 l3mcastGroupId,
1160 nextObj.appId());
1161 GroupChainElem l3mcastGce = new GroupChainElem(l3mcastGroupDescription,
1162 1, true);
1163
1164 // update original NextGroup with new bucket-chain
1165 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
1166 newBucketChain.addFirst(groupInfo.innerMostGroupDesc.appCookie());
1167 // Add L3 interface group to the chain if there is one.
1168 if (!groupInfo.nextGroupDesc.equals(groupInfo.innerMostGroupDesc)) {
1169 newBucketChain.addFirst(groupInfo.nextGroupDesc.appCookie());
1170 }
1171 newBucketChain.addFirst(l3mcastGroupKey);
1172 // If active keys shows only the top-level group without a chain of groups,
1173 // then it represents an empty group. Update by replacing empty chain.
1174 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1175 allActiveKeys.clear();
1176 }
1177 allActiveKeys.add(newBucketChain);
1178 updatePendingNextObjective(l3mcastGroupKey,
1179 new OfdpaNextGroup(allActiveKeys, nextObj));
1180
1181 updatePendingGroups(groupInfo.nextGroupDesc.appCookie(), l3mcastGce);
1182 // Point next group to inner-most group, if any
1183 if (!groupInfo.nextGroupDesc.equals(groupInfo.innerMostGroupDesc)) {
1184 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc,
1185 1, false);
1186 updatePendingGroups(groupInfo.innerMostGroupDesc.appCookie(), innerGce);
1187 }
1188 log.debug("Adding to L3MCAST: device:{} gid:{} gkey:{} nextId:{}",
1189 deviceId, Integer.toHexString(l3mcastGroupId),
1190 l3mcastGroupKey, nextObj.id());
1191 // send the innermost group
1192 log.debug("Sending innermost group {} in group chain on device {} ",
1193 Integer.toHexString(groupInfo.innerMostGroupDesc.givenGroupId()),
1194 deviceId);
1195 groupService.addGroup(groupInfo.innerMostGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -08001196 }
1197
1198 /**
1199 * Removes the bucket in the top level group of a possible group-chain. Does
Saurav Das1a129a02016-11-18 15:21:57 -08001200 * not remove the groups in the group-chain pointed to by this bucket, as they
Charles Chan188ebf52015-12-23 00:15:11 -08001201 * may be in use (referenced by other groups) elsewhere.
1202 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001203 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -08001204 * @param next the representation of the existing group-chain for this next objective
1205 */
1206 protected void removeBucketFromGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001207 if (nextObjective.type() != NextObjective.Type.HASHED &&
1208 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001209 log.warn("RemoveBuckets not applied to nextType:{} in dev:{} for next:{}",
1210 nextObjective.type(), deviceId, nextObjective.id());
Saurav Das1a129a02016-11-18 15:21:57 -08001211 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001212 return;
1213 }
1214 Collection<TrafficTreatment> treatments = nextObjective.next();
1215 TrafficTreatment treatment = treatments.iterator().next();
1216 // find the bucket to remove by noting the outport, and figuring out the
1217 // top-level group in the group-chain that indirectly references the port
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001218 PortNumber portToRemove = readOutPortFromTreatment(treatment);
1219 if (portToRemove == null) {
1220 log.warn("next objective {} has no outport.. cannot remove bucket"
1221 + "from group in dev: {}", nextObjective.id(), deviceId);
Saurav Das1a129a02016-11-18 15:21:57 -08001222 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -08001223 return;
1224 }
1225
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001226 List<Deque<GroupKey>> allActiveKeys = Ofdpa2Pipeline.appKryo.deserialize(next.data());
Charles Chan188ebf52015-12-23 00:15:11 -08001227 Deque<GroupKey> foundChain = null;
1228 int index = 0;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001229 for (Deque<GroupKey> gkeys : allActiveKeys) {
1230 // last group in group chain should have a single bucket pointing to port
Charles Chan188ebf52015-12-23 00:15:11 -08001231 GroupKey groupWithPort = gkeys.peekLast();
1232 Group group = groupService.getGroup(deviceId, groupWithPort);
1233 if (group == null) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001234 log.warn("Inconsistent group chain found when removing bucket"
1235 + "for next:{} in dev:{}", nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001236 continue;
1237 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001238 PortNumber pout = readOutPortFromTreatment(
1239 group.buckets().buckets().get(0).treatment());
1240 if (pout.equals(portToRemove)) {
1241 foundChain = gkeys;
Charles Chan188ebf52015-12-23 00:15:11 -08001242 break;
1243 }
1244 index++;
1245 }
Saurav Das1a129a02016-11-18 15:21:57 -08001246 if (foundChain == null) {
Charles Chan188ebf52015-12-23 00:15:11 -08001247 log.warn("Could not find appropriate group-chain for removing bucket"
1248 + " for next id {} in dev:{}", nextObjective.id(), deviceId);
Saurav Das1a129a02016-11-18 15:21:57 -08001249 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.BADPARAMS);
1250 return;
Charles Chan188ebf52015-12-23 00:15:11 -08001251 }
Saurav Das1a129a02016-11-18 15:21:57 -08001252
1253 //first groupkey is the one we want to modify
1254 GroupKey modGroupKey = foundChain.peekFirst();
1255 Group modGroup = groupService.getGroup(deviceId, modGroupKey);
1256 //second groupkey is the one we wish to remove the reference to
1257 GroupKey pointedGroupKey = null;
1258 int i = 0;
1259 for (GroupKey gk : foundChain) {
1260 if (i++ == 1) {
1261 pointedGroupKey = gk;
1262 break;
1263 }
1264 }
1265 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
1266 GroupBucket bucket = null;
1267 if (nextObjective.type() == NextObjective.Type.HASHED) {
1268 bucket = DefaultGroupBucket.createSelectGroupBucket(
1269 DefaultTrafficTreatment.builder()
1270 .group(pointedGroup.id())
1271 .build());
1272 } else {
1273 bucket = DefaultGroupBucket.createAllGroupBucket(
1274 DefaultTrafficTreatment.builder()
1275 .group(pointedGroup.id())
1276 .build());
1277 }
1278 GroupBuckets removeBuckets = new GroupBuckets(Collections
1279 .singletonList(bucket));
1280 log.debug("Removing buckets from group id 0x{} pointing to group id 0x{} "
1281 + "for next id {} in device {}", Integer.toHexString(modGroup.id().id()),
1282 Integer.toHexString(pointedGroup.id().id()), nextObjective.id(), deviceId);
1283 groupService.removeBucketsFromGroup(deviceId, modGroupKey,
1284 removeBuckets, modGroupKey,
1285 nextObjective.appId());
1286 // update store
1287 // If the bucket removed was the last bucket in the group, then
1288 // retain an entry for the top level group which still exists.
1289 if (allActiveKeys.size() == 1) {
1290 ArrayDeque<GroupKey> top = new ArrayDeque<>();
1291 top.add(modGroupKey);
1292 allActiveKeys.add(top);
1293 }
1294 allActiveKeys.remove(index);
1295 flowObjectiveStore.putNextGroup(nextObjective.id(),
1296 new OfdpaNextGroup(allActiveKeys,
1297 nextObjective));
Charles Chan188ebf52015-12-23 00:15:11 -08001298 }
1299
1300 /**
1301 * Removes all groups in multiple possible group-chains that represent the next
1302 * objective.
1303 *
1304 * @param nextObjective the next objective to remove
1305 * @param next the NextGroup that represents the existing group-chain for
1306 * this next objective
1307 */
1308 protected void removeGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001309 List<Deque<GroupKey>> allActiveKeys = Ofdpa2Pipeline.appKryo.deserialize(next.data());
Charles Chanfc5c7802016-05-17 13:13:55 -07001310
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001311 List<GroupKey> groupKeys = allActiveKeys.stream()
Charles Chanfc5c7802016-05-17 13:13:55 -07001312 .map(Deque::getFirst).collect(Collectors.toList());
1313 pendingRemoveNextObjectives.put(nextObjective, groupKeys);
1314
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001315 allActiveKeys.forEach(groupChain -> groupChain.forEach(groupKey ->
Charles Chan188ebf52015-12-23 00:15:11 -08001316 groupService.removeGroup(deviceId, groupKey, nextObjective.appId())));
1317 flowObjectiveStore.removeNextGroup(nextObjective.id());
1318 }
1319
Saurav Das8be4e3a2016-03-11 17:19:07 -08001320 //////////////////////////////////////
1321 // Helper Methods and Classes
1322 //////////////////////////////////////
1323
1324 private void updatePendingNextObjective(GroupKey key, OfdpaNextGroup value) {
1325 List<OfdpaNextGroup> nextList = new CopyOnWriteArrayList<OfdpaNextGroup>();
1326 nextList.add(value);
Charles Chanfc5c7802016-05-17 13:13:55 -07001327 List<OfdpaNextGroup> ret = pendingAddNextObjectives.asMap()
Saurav Das8be4e3a2016-03-11 17:19:07 -08001328 .putIfAbsent(key, nextList);
1329 if (ret != null) {
1330 ret.add(value);
1331 }
1332 }
1333
Charles Chan425854b2016-04-11 15:32:12 -07001334 protected void updatePendingGroups(GroupKey gkey, GroupChainElem gce) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001335 Set<GroupChainElem> gceSet = Collections.newSetFromMap(
1336 new ConcurrentHashMap<GroupChainElem, Boolean>());
1337 gceSet.add(gce);
1338 Set<GroupChainElem> retval = pendingGroups.putIfAbsent(gkey, gceSet);
1339 if (retval != null) {
1340 retval.add(gce);
1341 }
1342 }
1343
Charles Chan188ebf52015-12-23 00:15:11 -08001344 /**
1345 * Processes next element of a group chain. Assumption is that if this
1346 * group points to another group, the latter has already been created
1347 * and this driver has received notification for it. A second assumption is
1348 * that if there is another group waiting for this group then the appropriate
1349 * stores already have the information to act upon the notification for the
1350 * creation of this group.
1351 * <p>
1352 * The processing of the GroupChainElement depends on the number of groups
1353 * this element is waiting on. For all group types other than SIMPLE, a
1354 * GroupChainElement could be waiting on multiple groups.
1355 *
1356 * @param gce the group chain element to be processed next
1357 */
1358 private void processGroupChain(GroupChainElem gce) {
1359 int waitOnGroups = gce.decrementAndGetGroupsWaitedOn();
1360 if (waitOnGroups != 0) {
1361 log.debug("GCE: {} not ready to be processed", gce);
1362 return;
1363 }
1364 log.debug("GCE: {} ready to be processed", gce);
1365 if (gce.addBucketToGroup) {
1366 groupService.addBucketsToGroup(gce.groupDescription.deviceId(),
1367 gce.groupDescription.appCookie(),
1368 gce.groupDescription.buckets(),
1369 gce.groupDescription.appCookie(),
1370 gce.groupDescription.appId());
1371 } else {
1372 groupService.addGroup(gce.groupDescription);
1373 }
1374 }
1375
1376 private class GroupChecker implements Runnable {
1377 @Override
1378 public void run() {
1379 Set<GroupKey> keys = pendingGroups.keySet().stream()
1380 .filter(key -> groupService.getGroup(deviceId, key) != null)
1381 .collect(Collectors.toSet());
Charles Chanfc5c7802016-05-17 13:13:55 -07001382 Set<GroupKey> otherkeys = pendingAddNextObjectives.asMap().keySet().stream()
Charles Chan188ebf52015-12-23 00:15:11 -08001383 .filter(otherkey -> groupService.getGroup(deviceId, otherkey) != null)
1384 .collect(Collectors.toSet());
1385 keys.addAll(otherkeys);
1386
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -07001387 keys.forEach(key ->
Charles Chanfc5c7802016-05-17 13:13:55 -07001388 processPendingAddGroupsOrNextObjs(key, false));
Charles Chan188ebf52015-12-23 00:15:11 -08001389 }
1390 }
1391
Saurav Das8be4e3a2016-03-11 17:19:07 -08001392 private class InnerGroupListener implements GroupListener {
1393 @Override
1394 public void event(GroupEvent event) {
1395 log.trace("received group event of type {}", event.type());
Charles Chanfc5c7802016-05-17 13:13:55 -07001396 switch (event.type()) {
1397 case GROUP_ADDED:
1398 processPendingAddGroupsOrNextObjs(event.subject().appCookie(), true);
1399 break;
1400 case GROUP_REMOVED:
1401 processPendingRemoveNextObjs(event.subject().appCookie());
1402 break;
1403 default:
1404 break;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001405 }
1406 }
1407 }
1408
Charles Chanfc5c7802016-05-17 13:13:55 -07001409 private void processPendingAddGroupsOrNextObjs(GroupKey key, boolean added) {
Charles Chan188ebf52015-12-23 00:15:11 -08001410 //first check for group chain
1411 Set<GroupChainElem> gceSet = pendingGroups.remove(key);
1412 if (gceSet != null) {
1413 for (GroupChainElem gce : gceSet) {
Charles Chan216e3c82016-04-23 14:48:16 -07001414 log.debug("Group service {} group key {} in device {}. "
Saurav Das0fd79d92016-03-07 10:58:36 -08001415 + "Processing next group in group chain with group id 0x{}",
Charles Chan188ebf52015-12-23 00:15:11 -08001416 (added) ? "ADDED" : "processed",
1417 key, deviceId,
1418 Integer.toHexString(gce.groupDescription.givenGroupId()));
1419 processGroupChain(gce);
1420 }
1421 } else {
1422 // otherwise chain complete - check for waiting nextObjectives
Charles Chanfc5c7802016-05-17 13:13:55 -07001423 List<OfdpaNextGroup> nextGrpList = pendingAddNextObjectives.getIfPresent(key);
Charles Chan188ebf52015-12-23 00:15:11 -08001424 if (nextGrpList != null) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001425 pendingAddNextObjectives.invalidate(key);
Charles Chan188ebf52015-12-23 00:15:11 -08001426 nextGrpList.forEach(nextGrp -> {
Charles Chan216e3c82016-04-23 14:48:16 -07001427 log.debug("Group service {} group key {} in device:{}. "
Saurav Das0fd79d92016-03-07 10:58:36 -08001428 + "Done implementing next objective: {} <<-->> gid:0x{}",
Charles Chan188ebf52015-12-23 00:15:11 -08001429 (added) ? "ADDED" : "processed",
1430 key, deviceId, nextGrp.nextObjective().id(),
1431 Integer.toHexString(groupService.getGroup(deviceId, key)
1432 .givenGroupId()));
Charles Chan361154b2016-03-24 10:23:39 -07001433 Ofdpa2Pipeline.pass(nextGrp.nextObjective());
Charles Chan188ebf52015-12-23 00:15:11 -08001434 flowObjectiveStore.putNextGroup(nextGrp.nextObjective().id(), nextGrp);
1435 // check if addBuckets waiting for this completion
1436 NextObjective pendBkt = pendingBuckets
1437 .remove(nextGrp.nextObjective().id());
1438 if (pendBkt != null) {
1439 addBucketToGroup(pendBkt, nextGrp);
1440 }
1441 });
1442 }
1443 }
1444 }
1445
Charles Chanfc5c7802016-05-17 13:13:55 -07001446 private void processPendingRemoveNextObjs(GroupKey key) {
1447 pendingRemoveNextObjectives.asMap().forEach((nextObjective, groupKeys) -> {
1448 if (groupKeys.isEmpty()) {
1449 pendingRemoveNextObjectives.invalidate(nextObjective);
1450 Ofdpa2Pipeline.pass(nextObjective);
1451 } else {
1452 groupKeys.remove(key);
1453 }
1454 });
1455 }
1456
Charles Chan425854b2016-04-11 15:32:12 -07001457 protected int getNextAvailableIndex() {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001458 return (int) nextIndex.incrementAndGet();
1459 }
1460
Charles Chane849c192016-01-11 18:28:54 -08001461 /**
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001462 * Returns the outport in a traffic treatment.
1463 *
1464 * @param tt the treatment
1465 * @return the PortNumber for the outport or null
1466 */
1467 protected static PortNumber readOutPortFromTreatment(TrafficTreatment tt) {
1468 for (Instruction ins : tt.allInstructions()) {
1469 if (ins.type() == Instruction.Type.OUTPUT) {
1470 return ((Instructions.OutputInstruction) ins).port();
1471 }
1472 }
1473 return null;
1474 }
1475
1476 /**
Charles Chane849c192016-01-11 18:28:54 -08001477 * Returns a hash as the L2 Interface Group Key.
1478 *
1479 * Keep the lower 6-bit for port since port number usually smaller than 64.
1480 * Hash other information into remaining 28 bits.
1481 *
1482 * @param deviceId Device ID
1483 * @param vlanId VLAN ID
1484 * @param portNumber Port number
1485 * @return L2 interface group key
1486 */
Charles Chan425854b2016-04-11 15:32:12 -07001487 protected int l2InterfaceGroupKey(
Charles Chane849c192016-01-11 18:28:54 -08001488 DeviceId deviceId, VlanId vlanId, long portNumber) {
1489 int portLowerBits = (int) portNumber & PORT_LOWER_BITS_MASK;
1490 long portHigherBits = portNumber & PORT_HIGHER_BITS_MASK;
Charles Chand0fd5dc2016-02-16 23:14:49 -08001491 int hash = Objects.hash(deviceId, vlanId, portHigherBits);
Charles Chane849c192016-01-11 18:28:54 -08001492 return L2_INTERFACE_TYPE | (TYPE_MASK & hash << 6) | portLowerBits;
1493 }
1494
Saurav Das1a129a02016-11-18 15:21:57 -08001495 private Group retrieveTopLevelGroup(List<Deque<GroupKey>> allActiveKeys,
1496 int nextid) {
1497 GroupKey topLevelGroupKey = null;
1498 if (!allActiveKeys.isEmpty()) {
1499 topLevelGroupKey = allActiveKeys.get(0).peekFirst();
1500 } else {
1501 log.warn("Could not determine top level group while processing"
1502 + "next:{} in dev:{}", nextid, deviceId);
1503 return null;
1504 }
1505 Group topGroup = groupService.getGroup(deviceId, topLevelGroupKey);
1506 if (topGroup == null) {
1507 log.warn("Could not find top level group while processing "
1508 + "next:{} in dev:{}", nextid, deviceId);
1509 }
1510 return topGroup;
1511 }
1512
Charles Chan188ebf52015-12-23 00:15:11 -08001513 /**
1514 * Utility class for moving group information around.
Saurav Das1a129a02016-11-18 15:21:57 -08001515 *
1516 * Example: Suppose we are trying to create a group-chain A-B-C-D, where
1517 * A is the top level group, and D is the inner-most group, typically L2 Interface.
1518 * The innerMostGroupDesc is always D. At various stages of the creation
1519 * process the nextGroupDesc may be C or B. The nextGroupDesc exists to
1520 * inform the referencing group about which group it needs to point to,
1521 * and wait for. In some cases the group chain may simply be A-B. In this case,
1522 * both innerMostGroupDesc and nextGroupDesc will be B.
Charles Chan188ebf52015-12-23 00:15:11 -08001523 */
Charles Chan425854b2016-04-11 15:32:12 -07001524 protected class GroupInfo {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001525 /**
1526 * Description of the inner-most group of the group chain.
1527 * It is always an L2 interface group.
1528 */
1529 private GroupDescription innerMostGroupDesc;
Charles Chan188ebf52015-12-23 00:15:11 -08001530
Charles Chan5b9df8d2016-03-28 22:21:40 -07001531 /**
1532 * Description of the next group in the group chain.
1533 * It can be L2 interface, L3 interface, L3 unicast, L3 VPN group.
Saurav Das1a129a02016-11-18 15:21:57 -08001534 * It is possible that nextGroupDesc is the same as the innerMostGroup.
Charles Chan5b9df8d2016-03-28 22:21:40 -07001535 */
1536 private GroupDescription nextGroupDesc;
1537
1538 GroupInfo(GroupDescription innerMostGroupDesc, GroupDescription nextGroupDesc) {
1539 this.innerMostGroupDesc = innerMostGroupDesc;
1540 this.nextGroupDesc = nextGroupDesc;
Charles Chan188ebf52015-12-23 00:15:11 -08001541 }
1542 }
1543
1544 /**
1545 * Represents an entire group-chain that implements a Next-Objective from
1546 * the application. The objective is represented as a list of deques, where
1547 * each deque is a separate chain of groups.
1548 * <p>
1549 * For example, an ECMP group with 3 buckets, where each bucket points to
1550 * a group chain of L3 Unicast and L2 interface groups will look like this:
1551 * <ul>
1552 * <li>List[0] is a Deque of GroupKeyECMP(first)-GroupKeyL3(middle)-GroupKeyL2(last)
1553 * <li>List[1] is a Deque of GroupKeyECMP(first)-GroupKeyL3(middle)-GroupKeyL2(last)
1554 * <li>List[2] is a Deque of GroupKeyECMP(first)-GroupKeyL3(middle)-GroupKeyL2(last)
1555 * </ul>
1556 * where the first element of each deque is the same, representing the
1557 * top level ECMP group, while every other element represents a unique groupKey.
1558 * <p>
1559 * Also includes information about the next objective that
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001560 * resulted in these group-chains.
Charles Chan188ebf52015-12-23 00:15:11 -08001561 *
1562 */
1563 protected class OfdpaNextGroup implements NextGroup {
1564 private final NextObjective nextObj;
1565 private final List<Deque<GroupKey>> gkeys;
1566
1567 public OfdpaNextGroup(List<Deque<GroupKey>> gkeys, NextObjective nextObj) {
Charles Chan188ebf52015-12-23 00:15:11 -08001568 this.nextObj = nextObj;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001569 this.gkeys = gkeys;
Charles Chan188ebf52015-12-23 00:15:11 -08001570 }
1571
1572 public NextObjective nextObjective() {
1573 return nextObj;
1574 }
1575
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001576 public List<Deque<GroupKey>> groupKeys() {
1577 return gkeys;
1578 }
1579
Charles Chan188ebf52015-12-23 00:15:11 -08001580 @Override
1581 public byte[] data() {
Charles Chan361154b2016-03-24 10:23:39 -07001582 return Ofdpa2Pipeline.appKryo.serialize(gkeys);
Charles Chan188ebf52015-12-23 00:15:11 -08001583 }
1584 }
1585
1586 /**
1587 * Represents a group element that is part of a chain of groups.
1588 * Stores enough information to create a Group Description to add the group
1589 * to the switch by requesting the Group Service. Objects instantiating this
1590 * class are meant to be temporary and live as long as it is needed to wait for
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001591 * referenced groups in the group chain to be created.
Charles Chan188ebf52015-12-23 00:15:11 -08001592 */
Charles Chan425854b2016-04-11 15:32:12 -07001593 protected class GroupChainElem {
Charles Chan188ebf52015-12-23 00:15:11 -08001594 private GroupDescription groupDescription;
1595 private AtomicInteger waitOnGroups;
1596 private boolean addBucketToGroup;
1597
1598 GroupChainElem(GroupDescription groupDescription, int waitOnGroups,
1599 boolean addBucketToGroup) {
1600 this.groupDescription = groupDescription;
1601 this.waitOnGroups = new AtomicInteger(waitOnGroups);
1602 this.addBucketToGroup = addBucketToGroup;
1603 }
1604
1605 /**
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001606 * This method atomically decrements the counter for the number of
Charles Chan188ebf52015-12-23 00:15:11 -08001607 * groups this GroupChainElement is waiting on, for notifications from
1608 * the Group Service. When this method returns a value of 0, this
1609 * GroupChainElement is ready to be processed.
1610 *
1611 * @return integer indication of the number of notifications being waited on
1612 */
1613 int decrementAndGetGroupsWaitedOn() {
1614 return waitOnGroups.decrementAndGet();
1615 }
1616
1617 @Override
1618 public String toString() {
1619 return (Integer.toHexString(groupDescription.givenGroupId()) +
1620 " groupKey: " + groupDescription.appCookie() +
1621 " waiting-on-groups: " + waitOnGroups.get() +
1622 " addBucketToGroup: " + addBucketToGroup +
1623 " device: " + deviceId);
1624 }
1625 }
1626}