blob: 60fefcf1b78383cddc735017df594097a01c9edb [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 Chan372b63e2017-02-07 12:10:53 -0800108 protected static final int THREE_BIT_MASK = 0x0fff;
109 protected static final int FOUR_BIT_MASK = 0xffff;
110 protected static final int PORT_LEN = 16;
111
Charles Chan425854b2016-04-11 15:32:12 -0700112 protected static final int PORT_LOWER_BITS_MASK = 0x3f;
113 protected static final long PORT_HIGHER_BITS_MASK = ~PORT_LOWER_BITS_MASK;
Charles Chan188ebf52015-12-23 00:15:11 -0800114
115 private final Logger log = getLogger(getClass());
116 private ServiceDirectory serviceDirectory;
117 protected GroupService groupService;
Saurav Das8be4e3a2016-03-11 17:19:07 -0800118 protected StorageService storageService;
Charles Chan188ebf52015-12-23 00:15:11 -0800119
Charles Chan425854b2016-04-11 15:32:12 -0700120 protected DeviceId deviceId;
Charles Chan188ebf52015-12-23 00:15:11 -0800121 private FlowObjectiveStore flowObjectiveStore;
Charles Chanfc5c7802016-05-17 13:13:55 -0700122 private Cache<GroupKey, List<OfdpaNextGroup>> pendingAddNextObjectives;
123 private Cache<NextObjective, List<GroupKey>> pendingRemoveNextObjectives;
Charles Chan188ebf52015-12-23 00:15:11 -0800124 private ConcurrentHashMap<GroupKey, Set<GroupChainElem>> pendingGroups;
125 private ScheduledExecutorService groupChecker =
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700126 Executors.newScheduledThreadPool(2, groupedThreads("onos/pipeliner", "ofdpa2-%d", log));
Charles Chan188ebf52015-12-23 00:15:11 -0800127
128 // index number for group creation
Saurav Das8be4e3a2016-03-11 17:19:07 -0800129 private AtomicCounter nextIndex;
Charles Chan188ebf52015-12-23 00:15:11 -0800130
Charles Chan188ebf52015-12-23 00:15:11 -0800131 // local store for pending bucketAdds - by design there can only be one
132 // pending bucket for a group
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700133 protected ConcurrentHashMap<Integer, NextObjective> pendingBuckets =
134 new ConcurrentHashMap<>();
Charles Chan188ebf52015-12-23 00:15:11 -0800135
Charles Chan40132b32017-01-22 00:19:37 -0800136 /**
137 * Determines whether this pipeline support copy ttl instructions or not.
138 *
139 * @return true if copy ttl instructions are supported
140 */
141 protected boolean supportCopyTtl() {
142 return true;
143 }
144
145 /**
146 * Determines whether this pipeline support set mpls bos instruction or not.
147 *
148 * @return true if set mpls bos instruction is supported
149 */
150 protected boolean supportSetMplsBos() {
151 return true;
152 }
153
Charles Chan188ebf52015-12-23 00:15:11 -0800154 protected void init(DeviceId deviceId, PipelinerContext context) {
155 this.deviceId = deviceId;
156 this.flowObjectiveStore = context.store();
157 this.serviceDirectory = context.directory();
158 this.groupService = serviceDirectory.get(GroupService.class);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800159 this.storageService = serviceDirectory.get(StorageService.class);
Madan Jampanid5714e02016-04-19 14:15:20 -0700160 this.nextIndex = storageService.getAtomicCounter("group-id-index-counter");
Charles Chan188ebf52015-12-23 00:15:11 -0800161
Charles Chanfc5c7802016-05-17 13:13:55 -0700162 pendingAddNextObjectives = CacheBuilder.newBuilder()
Charles Chan188ebf52015-12-23 00:15:11 -0800163 .expireAfterWrite(20, TimeUnit.SECONDS)
164 .removalListener((
165 RemovalNotification<GroupKey, List<OfdpaNextGroup>> notification) -> {
166 if (notification.getCause() == RemovalCause.EXPIRED) {
167 notification.getValue().forEach(ofdpaNextGrp ->
Charles Chan361154b2016-03-24 10:23:39 -0700168 Ofdpa2Pipeline.fail(ofdpaNextGrp.nextObj,
Charles Chan188ebf52015-12-23 00:15:11 -0800169 ObjectiveError.GROUPINSTALLATIONFAILED));
Charles Chanfc5c7802016-05-17 13:13:55 -0700170 }
171 }).build();
Charles Chan188ebf52015-12-23 00:15:11 -0800172
Charles Chanfc5c7802016-05-17 13:13:55 -0700173 pendingRemoveNextObjectives = CacheBuilder.newBuilder()
174 .expireAfterWrite(20, TimeUnit.SECONDS)
175 .removalListener((
176 RemovalNotification<NextObjective, List<GroupKey>> notification) -> {
177 if (notification.getCause() == RemovalCause.EXPIRED) {
178 Ofdpa2Pipeline.fail(notification.getKey(),
179 ObjectiveError.GROUPREMOVALFAILED);
Charles Chan188ebf52015-12-23 00:15:11 -0800180 }
181 }).build();
182 pendingGroups = new ConcurrentHashMap<>();
183 groupChecker.scheduleAtFixedRate(new GroupChecker(), 0, 500, TimeUnit.MILLISECONDS);
184
185 groupService.addListener(new InnerGroupListener());
186 }
187
Saurav Das8be4e3a2016-03-11 17:19:07 -0800188 //////////////////////////////////////
189 // Group Creation
190 //////////////////////////////////////
191
Charles Chan188ebf52015-12-23 00:15:11 -0800192 protected void addGroup(NextObjective nextObjective) {
193 switch (nextObjective.type()) {
194 case SIMPLE:
195 Collection<TrafficTreatment> treatments = nextObjective.next();
196 if (treatments.size() != 1) {
197 log.error("Next Objectives of type Simple should only have a "
198 + "single Traffic Treatment. Next Objective Id:{}",
199 nextObjective.id());
Charles Chan361154b2016-03-24 10:23:39 -0700200 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800201 return;
202 }
203 processSimpleNextObjective(nextObjective);
204 break;
205 case BROADCAST:
206 processBroadcastNextObjective(nextObjective);
207 break;
208 case HASHED:
209 processHashedNextObjective(nextObjective);
210 break;
211 case FAILOVER:
Charles Chan361154b2016-03-24 10:23:39 -0700212 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -0800213 log.warn("Unsupported next objective type {}", nextObjective.type());
214 break;
215 default:
Charles Chan361154b2016-03-24 10:23:39 -0700216 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.UNKNOWN);
Charles Chan188ebf52015-12-23 00:15:11 -0800217 log.warn("Unknown next objective type {}", nextObjective.type());
218 }
219 }
220
221 /**
222 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
223 * a chain of groups. The simple Next Objective passed
224 * in by the application has to be broken up into a group chain
225 * comprising of an L3 Unicast Group that points to an L2 Interface
226 * Group which in-turn points to an output port. In some cases, the simple
227 * next Objective can just be an L2 interface without the need for chaining.
228 *
229 * @param nextObj the nextObjective of type SIMPLE
230 */
231 private void processSimpleNextObjective(NextObjective nextObj) {
232 TrafficTreatment treatment = nextObj.next().iterator().next();
233 // determine if plain L2 or L3->L2
234 boolean plainL2 = true;
235 for (Instruction ins : treatment.allInstructions()) {
236 if (ins.type() == Instruction.Type.L2MODIFICATION) {
237 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
238 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_DST ||
239 l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_SRC) {
240 plainL2 = false;
241 break;
242 }
243 }
244 }
245
246 if (plainL2) {
247 createL2InterfaceGroup(nextObj);
248 return;
249 }
250
251 // break up simple next objective to GroupChain objects
252 GroupInfo groupInfo = createL2L3Chain(treatment, nextObj.id(),
Saurav Das8be4e3a2016-03-11 17:19:07 -0800253 nextObj.appId(), false,
254 nextObj.meta());
Charles Chan188ebf52015-12-23 00:15:11 -0800255 if (groupInfo == null) {
256 log.error("Could not process nextObj={} in dev:{}", nextObj.id(), deviceId);
257 return;
258 }
259 // create object for local and distributed storage
260 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700261 gkeyChain.addFirst(groupInfo.innerMostGroupDesc.appCookie());
262 gkeyChain.addFirst(groupInfo.nextGroupDesc.appCookie());
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700263 OfdpaNextGroup ofdpaGrp =
264 new OfdpaNextGroup(Collections.singletonList(gkeyChain), nextObj);
Charles Chan188ebf52015-12-23 00:15:11 -0800265
266 // store l3groupkey with the ofdpaNextGroup for the nextObjective that depends on it
Charles Chan5b9df8d2016-03-28 22:21:40 -0700267 updatePendingNextObjective(groupInfo.nextGroupDesc.appCookie(), ofdpaGrp);
Charles Chan188ebf52015-12-23 00:15:11 -0800268
269 // now we are ready to send the l2 groupDescription (inner), as all the stores
270 // that will get async replies have been updated. By waiting to update
271 // the stores, we prevent nasty race conditions.
Charles Chan5b9df8d2016-03-28 22:21:40 -0700272 groupService.addGroup(groupInfo.innerMostGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800273 }
274
Charles Chan188ebf52015-12-23 00:15:11 -0800275 /**
276 * Creates a simple L2 Interface Group.
277 *
278 * @param nextObj the next Objective
279 */
280 private void createL2InterfaceGroup(NextObjective nextObj) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700281 VlanId assignedVlan = Ofdpa2Pipeline.readVlanFromSelector(nextObj.meta());
282 if (assignedVlan == null) {
283 log.warn("VLAN ID required by simple next obj is missing. Abort.");
284 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800285 return;
286 }
287
Charles Chan5b9df8d2016-03-28 22:21:40 -0700288 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Charles Chan188ebf52015-12-23 00:15:11 -0800289
Charles Chan5b9df8d2016-03-28 22:21:40 -0700290 // There is only one L2 interface group in this case
291 GroupDescription l2InterfaceGroupDesc = groupInfos.get(0).innerMostGroupDesc;
Charles Chan188ebf52015-12-23 00:15:11 -0800292
Charles Chan5b9df8d2016-03-28 22:21:40 -0700293 // Put all dependency information into allGroupKeys
294 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
295 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
296 gkeyChain.addFirst(l2InterfaceGroupDesc.appCookie());
297 allGroupKeys.add(gkeyChain);
Charles Chan188ebf52015-12-23 00:15:11 -0800298
Charles Chan5b9df8d2016-03-28 22:21:40 -0700299 // Point the next objective to this group
300 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
301 updatePendingNextObjective(l2InterfaceGroupDesc.appCookie(), ofdpaGrp);
302
303 // Start installing the inner-most group
304 groupService.addGroup(l2InterfaceGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800305 }
306
307 /**
308 * Creates one of two possible group-chains from the treatment
309 * passed in. Depending on the MPLS boolean, this method either creates
Charles Chan425854b2016-04-11 15:32:12 -0700310 * an L3Unicast Group --&gt; L2Interface Group, if mpls is false;
311 * or MPLSInterface Group --&gt; L2Interface Group, if mpls is true;
Charles Chan188ebf52015-12-23 00:15:11 -0800312 * The returned 'inner' group description is always the L2 Interface group.
313 *
314 * @param treatment that needs to be broken up to create the group chain
315 * @param nextId of the next objective that needs this group chain
316 * @param appId of the application that sent this next objective
317 * @param mpls determines if L3Unicast or MPLSInterface group is created
318 * @param meta metadata passed in by the application as part of the nextObjective
319 * @return GroupInfo containing the GroupDescription of the
320 * L2Interface group(inner) and the GroupDescription of the (outer)
321 * L3Unicast/MPLSInterface group. May return null if there is an
322 * error in processing the chain
323 */
Charles Chan425854b2016-04-11 15:32:12 -0700324 protected GroupInfo createL2L3Chain(TrafficTreatment treatment, int nextId,
Charles Chanf9e98652016-09-07 16:54:23 -0700325 ApplicationId appId, boolean mpls,
326 TrafficSelector meta) {
327 return createL2L3ChainInternal(treatment, nextId, appId, mpls, meta, true);
328 }
329
330 /**
331 * Internal implementation of createL2L3Chain.
332 * <p>
333 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
334 * Since it is non-OF spec, we need an extension treatment for that.
335 * The useSetVlanExtension must be set to false for OFDPA i12.
336 * </p>
337 *
338 * @param treatment that needs to be broken up to create the group chain
339 * @param nextId of the next objective that needs this group chain
340 * @param appId of the application that sent this next objective
341 * @param mpls determines if L3Unicast or MPLSInterface group is created
342 * @param meta metadata passed in by the application as part of the nextObjective
343 * @param useSetVlanExtension use the setVlanVid extension that has is_present bit set to 0.
344 * @return GroupInfo containing the GroupDescription of the
345 * L2Interface group(inner) and the GroupDescription of the (outer)
346 * L3Unicast/MPLSInterface group. May return null if there is an
347 * error in processing the chain
348 */
349 protected GroupInfo createL2L3ChainInternal(TrafficTreatment treatment, int nextId,
Saurav Das8be4e3a2016-03-11 17:19:07 -0800350 ApplicationId appId, boolean mpls,
Charles Chanf9e98652016-09-07 16:54:23 -0700351 TrafficSelector meta, boolean useSetVlanExtension) {
Charles Chan188ebf52015-12-23 00:15:11 -0800352 // for the l2interface group, get vlan and port info
353 // for the outer group, get the src/dst mac, and vlan info
354 TrafficTreatment.Builder outerTtb = DefaultTrafficTreatment.builder();
355 TrafficTreatment.Builder innerTtb = DefaultTrafficTreatment.builder();
356 VlanId vlanid = null;
357 long portNum = 0;
358 boolean setVlan = false, popVlan = false;
Charles Chand0fd5dc2016-02-16 23:14:49 -0800359 MacAddress srcMac = MacAddress.ZERO;
Charles Chan5270ed02016-01-30 23:22:37 -0800360 MacAddress dstMac = MacAddress.ZERO;
Charles Chan188ebf52015-12-23 00:15:11 -0800361 for (Instruction ins : treatment.allInstructions()) {
362 if (ins.type() == Instruction.Type.L2MODIFICATION) {
363 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
364 switch (l2ins.subtype()) {
365 case ETH_DST:
Charles Chan5270ed02016-01-30 23:22:37 -0800366 dstMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
367 outerTtb.setEthDst(dstMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800368 break;
369 case ETH_SRC:
Charles Chand0fd5dc2016-02-16 23:14:49 -0800370 srcMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
371 outerTtb.setEthSrc(srcMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800372 break;
373 case VLAN_ID:
374 vlanid = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
Charles Chanf9e98652016-09-07 16:54:23 -0700375 if (useSetVlanExtension) {
376 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
377 outerTtb.extension(ofdpaSetVlanVid, deviceId);
378 } else {
379 outerTtb.setVlanId(vlanid);
380 }
Charles Chan188ebf52015-12-23 00:15:11 -0800381 setVlan = true;
382 break;
383 case VLAN_POP:
384 innerTtb.popVlan();
385 popVlan = true;
386 break;
387 case DEC_MPLS_TTL:
388 case MPLS_LABEL:
389 case MPLS_POP:
390 case MPLS_PUSH:
391 case VLAN_PCP:
392 case VLAN_PUSH:
393 default:
394 break;
395 }
396 } else if (ins.type() == Instruction.Type.OUTPUT) {
397 portNum = ((Instructions.OutputInstruction) ins).port().toLong();
398 innerTtb.add(ins);
399 } else {
400 log.warn("Driver does not handle this type of TrafficTreatment"
401 + " instruction in nextObjectives: {}", ins.type());
402 }
403 }
404
405 if (vlanid == null && meta != null) {
406 // use metadata if available
407 Criterion vidCriterion = meta.getCriterion(Criterion.Type.VLAN_VID);
408 if (vidCriterion != null) {
409 vlanid = ((VlanIdCriterion) vidCriterion).vlanId();
410 }
411 // if vlan is not set, use the vlan in metadata for outerTtb
412 if (vlanid != null && !setVlan) {
Charles Chanf9e98652016-09-07 16:54:23 -0700413 if (useSetVlanExtension) {
414 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
415 outerTtb.extension(ofdpaSetVlanVid, deviceId);
416 } else {
417 outerTtb.setVlanId(vlanid);
418 }
Charles Chan188ebf52015-12-23 00:15:11 -0800419 }
420 }
421
422 if (vlanid == null) {
423 log.error("Driver cannot process an L2/L3 group chain without "
424 + "egress vlan information for dev: {} port:{}",
425 deviceId, portNum);
426 return null;
427 }
428
429 if (!setVlan && !popVlan) {
430 // untagged outgoing port
431 TrafficTreatment.Builder temp = DefaultTrafficTreatment.builder();
432 temp.popVlan();
433 innerTtb.build().allInstructions().forEach(i -> temp.add(i));
434 innerTtb = temp;
435 }
436
437 // assemble information for ofdpa l2interface group
Charles Chane849c192016-01-11 18:28:54 -0800438 int l2groupId = L2_INTERFACE_TYPE | (vlanid.toShort() << 16) | (int) portNum;
Saurav Das8be4e3a2016-03-11 17:19:07 -0800439 // a globally unique groupkey that is different for ports in the same device,
Charles Chan188ebf52015-12-23 00:15:11 -0800440 // but different for the same portnumber on different devices. Also different
441 // for the various group-types created out of the same next objective.
Charles Chane849c192016-01-11 18:28:54 -0800442 int l2gk = l2InterfaceGroupKey(deviceId, vlanid, portNum);
Charles Chan361154b2016-03-24 10:23:39 -0700443 final GroupKey l2groupkey = new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(l2gk));
Charles Chan188ebf52015-12-23 00:15:11 -0800444
445 // assemble information for outer group
446 GroupDescription outerGrpDesc = null;
447 if (mpls) {
448 // outer group is MPLSInteface
Saurav Das8be4e3a2016-03-11 17:19:07 -0800449 int mplsInterfaceIndex = getNextAvailableIndex();
450 int mplsgroupId = MPLS_INTERFACE_TYPE | (SUBTYPE_MASK & mplsInterfaceIndex);
451 final GroupKey mplsgroupkey = new DefaultGroupKey(
Charles Chan361154b2016-03-24 10:23:39 -0700452 Ofdpa2Pipeline.appKryo.serialize(mplsInterfaceIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800453 outerTtb.group(new DefaultGroupId(l2groupId));
454 // create the mpls-interface group description to wait for the
455 // l2 interface group to be processed
456 GroupBucket mplsinterfaceGroupBucket =
457 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
458 outerGrpDesc = new DefaultGroupDescription(
459 deviceId,
460 GroupDescription.Type.INDIRECT,
461 new GroupBuckets(Collections.singletonList(
462 mplsinterfaceGroupBucket)),
463 mplsgroupkey,
464 mplsgroupId,
465 appId);
466 log.debug("Trying MPLS-Interface: device:{} gid:{} gkey:{} nextid:{}",
467 deviceId, Integer.toHexString(mplsgroupId),
468 mplsgroupkey, nextId);
469 } else {
470 // outer group is L3Unicast
Saurav Das8be4e3a2016-03-11 17:19:07 -0800471 int l3unicastIndex = getNextAvailableIndex();
472 int l3groupId = L3_UNICAST_TYPE | (TYPE_MASK & l3unicastIndex);
473 final GroupKey l3groupkey = new DefaultGroupKey(
Charles Chan361154b2016-03-24 10:23:39 -0700474 Ofdpa2Pipeline.appKryo.serialize(l3unicastIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800475 outerTtb.group(new DefaultGroupId(l2groupId));
476 // create the l3unicast group description to wait for the
477 // l2 interface group to be processed
478 GroupBucket l3unicastGroupBucket =
479 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
480 outerGrpDesc = new DefaultGroupDescription(
481 deviceId,
482 GroupDescription.Type.INDIRECT,
483 new GroupBuckets(Collections.singletonList(
484 l3unicastGroupBucket)),
485 l3groupkey,
486 l3groupId,
487 appId);
488 log.debug("Trying L3Unicast: device:{} gid:{} gkey:{} nextid:{}",
489 deviceId, Integer.toHexString(l3groupId),
490 l3groupkey, nextId);
491 }
492
493 // store l2groupkey with the groupChainElem for the outer-group that depends on it
494 GroupChainElem gce = new GroupChainElem(outerGrpDesc, 1, false);
495 updatePendingGroups(l2groupkey, gce);
496
497 // create group description for the inner l2interfacegroup
Charles Chan5b9df8d2016-03-28 22:21:40 -0700498 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800499 DefaultGroupBucket.createIndirectGroupBucket(innerTtb.build());
500 GroupDescription l2groupDescription =
501 new DefaultGroupDescription(
502 deviceId,
503 GroupDescription.Type.INDIRECT,
504 new GroupBuckets(Collections.singletonList(
Charles Chan5b9df8d2016-03-28 22:21:40 -0700505 l2InterfaceGroupBucket)),
Charles Chan188ebf52015-12-23 00:15:11 -0800506 l2groupkey,
507 l2groupId,
508 appId);
509 log.debug("Trying L2Interface: device:{} gid:{} gkey:{} nextId:{}",
510 deviceId, Integer.toHexString(l2groupId),
511 l2groupkey, nextId);
512 return new GroupInfo(l2groupDescription, outerGrpDesc);
513
514 }
515
516 /**
517 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
518 * a chain of groups. The broadcast Next Objective passed in by the application
519 * has to be broken up into a group chain comprising of an
Saurav Das1a129a02016-11-18 15:21:57 -0800520 * L2 Flood group or L3 Multicast group, whose buckets point to L2 Interface groups.
Charles Chan188ebf52015-12-23 00:15:11 -0800521 *
522 * @param nextObj the nextObjective of type BROADCAST
523 */
524 private void processBroadcastNextObjective(NextObjective nextObj) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700525 VlanId assignedVlan = Ofdpa2Pipeline.readVlanFromSelector(nextObj.meta());
526 if (assignedVlan == null) {
527 log.warn("VLAN ID required by broadcast next obj is missing. Abort.");
528 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800529 return;
530 }
Charles Chan188ebf52015-12-23 00:15:11 -0800531
Charles Chan5b9df8d2016-03-28 22:21:40 -0700532 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
533
534 IpPrefix ipDst = Ofdpa2Pipeline.readIpDstFromSelector(nextObj.meta());
535 if (ipDst != null) {
536 if (ipDst.isMulticast()) {
537 createL3MulticastGroup(nextObj, assignedVlan, groupInfos);
538 } else {
539 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
540 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
541 return;
542 }
543 } else {
544 createL2FloodGroup(nextObj, assignedVlan, groupInfos);
545 }
546 }
547
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700548 private List<GroupInfo> prepareL2InterfaceGroup(NextObjective nextObj,
549 VlanId assignedVlan) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700550 ImmutableList.Builder<GroupInfo> groupInfoBuilder = ImmutableList.builder();
551
552 // break up broadcast next objective to multiple groups
553 Collection<TrafficTreatment> buckets = nextObj.next();
554
Charles Chan188ebf52015-12-23 00:15:11 -0800555 // each treatment is converted to an L2 interface group
Charles Chan188ebf52015-12-23 00:15:11 -0800556 for (TrafficTreatment treatment : buckets) {
557 TrafficTreatment.Builder newTreatment = DefaultTrafficTreatment.builder();
558 PortNumber portNum = null;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700559 VlanId egressVlan = null;
Charles Chan188ebf52015-12-23 00:15:11 -0800560 // ensure that the only allowed treatments are pop-vlan and output
561 for (Instruction ins : treatment.allInstructions()) {
562 if (ins.type() == Instruction.Type.L2MODIFICATION) {
563 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
564 switch (l2ins.subtype()) {
565 case VLAN_POP:
566 newTreatment.add(l2ins);
567 break;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700568 case VLAN_ID:
569 egressVlan = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
570 break;
Charles Chan188ebf52015-12-23 00:15:11 -0800571 default:
572 log.debug("action {} not permitted for broadcast nextObj",
573 l2ins.subtype());
574 break;
575 }
576 } else if (ins.type() == Instruction.Type.OUTPUT) {
577 portNum = ((Instructions.OutputInstruction) ins).port();
578 newTreatment.add(ins);
579 } else {
Charles Chane849c192016-01-11 18:28:54 -0800580 log.debug("TrafficTreatment of type {} not permitted in " +
581 " broadcast nextObjective", ins.type());
Charles Chan188ebf52015-12-23 00:15:11 -0800582 }
583 }
584
Charles Chan188ebf52015-12-23 00:15:11 -0800585 // assemble info for l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700586 VlanId l2InterfaceGroupVlan =
587 (egressVlan != null && !assignedVlan.equals(egressVlan)) ?
588 egressVlan : assignedVlan;
589 int l2gk = l2InterfaceGroupKey(deviceId, l2InterfaceGroupVlan, portNum.toLong());
590 final GroupKey l2InterfaceGroupKey =
591 new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(l2gk));
Charles Chan372b63e2017-02-07 12:10:53 -0800592 int l2InterfaceGroupId = L2_INTERFACE_TYPE |
593 ((l2InterfaceGroupVlan.toShort() & THREE_BIT_MASK) << PORT_LEN) |
594 ((int) portNum.toLong() & FOUR_BIT_MASK);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700595 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800596 DefaultGroupBucket.createIndirectGroupBucket(newTreatment.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700597 GroupDescription l2InterfaceGroupDescription =
Charles Chan188ebf52015-12-23 00:15:11 -0800598 new DefaultGroupDescription(
599 deviceId,
600 GroupDescription.Type.INDIRECT,
601 new GroupBuckets(Collections.singletonList(
Charles Chan5b9df8d2016-03-28 22:21:40 -0700602 l2InterfaceGroupBucket)),
603 l2InterfaceGroupKey,
604 l2InterfaceGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800605 nextObj.appId());
606 log.debug("Trying L2-Interface: device:{} gid:{} gkey:{} nextid:{}",
Charles Chan5b9df8d2016-03-28 22:21:40 -0700607 deviceId, Integer.toHexString(l2InterfaceGroupId),
608 l2InterfaceGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -0800609
Charles Chan5b9df8d2016-03-28 22:21:40 -0700610 groupInfoBuilder.add(new GroupInfo(l2InterfaceGroupDescription,
611 l2InterfaceGroupDescription));
Charles Chan188ebf52015-12-23 00:15:11 -0800612 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700613 return groupInfoBuilder.build();
614 }
Charles Chan188ebf52015-12-23 00:15:11 -0800615
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700616 private void createL2FloodGroup(NextObjective nextObj, VlanId vlanId,
617 List<GroupInfo> groupInfos) {
618 // assemble info for l2 flood group. Since there can be only one flood
619 // group for a vlan, its index is always the same - 0
Saurav Das0fd79d92016-03-07 10:58:36 -0800620 Integer l2floodgroupId = L2_FLOOD_TYPE | (vlanId.toShort() << 16);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800621 int l2floodgk = getNextAvailableIndex();
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700622 final GroupKey l2floodgroupkey =
623 new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(l2floodgk));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700624
Charles Chan188ebf52015-12-23 00:15:11 -0800625 // collection of group buckets pointing to all the l2 interface groups
Charles Chan5b9df8d2016-03-28 22:21:40 -0700626 List<GroupBucket> l2floodBuckets = Lists.newArrayList();
627 groupInfos.forEach(groupInfo -> {
628 GroupDescription l2intGrpDesc = groupInfo.nextGroupDesc;
Charles Chan188ebf52015-12-23 00:15:11 -0800629 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
630 ttb.group(new DefaultGroupId(l2intGrpDesc.givenGroupId()));
631 GroupBucket abucket = DefaultGroupBucket.createAllGroupBucket(ttb.build());
632 l2floodBuckets.add(abucket);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700633 });
Charles Chan188ebf52015-12-23 00:15:11 -0800634 // create the l2flood group-description to wait for all the
635 // l2interface groups to be processed
636 GroupDescription l2floodGroupDescription =
637 new DefaultGroupDescription(
638 deviceId,
639 GroupDescription.Type.ALL,
640 new GroupBuckets(l2floodBuckets),
641 l2floodgroupkey,
642 l2floodgroupId,
643 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800644 log.debug("Trying L2-Flood: device:{} gid:{} gkey:{} nextid:{}",
645 deviceId, Integer.toHexString(l2floodgroupId),
646 l2floodgroupkey, nextObj.id());
647
Charles Chan5b9df8d2016-03-28 22:21:40 -0700648 // Put all dependency information into allGroupKeys
649 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
650 groupInfos.forEach(groupInfo -> {
651 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
652 // In this case we should have L2 interface group only
653 gkeyChain.addFirst(groupInfo.nextGroupDesc.appCookie());
654 gkeyChain.addFirst(l2floodgroupkey);
655 allGroupKeys.add(gkeyChain);
656 });
Charles Chan188ebf52015-12-23 00:15:11 -0800657
Charles Chan5b9df8d2016-03-28 22:21:40 -0700658 // Point the next objective to this group
659 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Charles Chan188ebf52015-12-23 00:15:11 -0800660 updatePendingNextObjective(l2floodgroupkey, ofdpaGrp);
661
Charles Chan5b9df8d2016-03-28 22:21:40 -0700662 GroupChainElem gce = new GroupChainElem(l2floodGroupDescription,
663 groupInfos.size(), false);
664 groupInfos.forEach(groupInfo -> {
665 // Point this group to the next group
666 updatePendingGroups(groupInfo.nextGroupDesc.appCookie(), gce);
667 // Start installing the inner-most group
668 groupService.addGroup(groupInfo.innerMostGroupDesc);
669 });
670 }
671
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700672 private void createL3MulticastGroup(NextObjective nextObj, VlanId vlanId,
673 List<GroupInfo> groupInfos) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700674 List<GroupBucket> l3McastBuckets = new ArrayList<>();
675 groupInfos.forEach(groupInfo -> {
676 // Points to L3 interface group if there is one.
677 // Otherwise points to L2 interface group directly.
678 GroupDescription nextGroupDesc = (groupInfo.nextGroupDesc != null) ?
679 groupInfo.nextGroupDesc : groupInfo.innerMostGroupDesc;
680 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
681 ttb.group(new DefaultGroupId(nextGroupDesc.givenGroupId()));
682 GroupBucket abucket = DefaultGroupBucket.createAllGroupBucket(ttb.build());
683 l3McastBuckets.add(abucket);
684 });
685
686 int l3MulticastIndex = getNextAvailableIndex();
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700687 int l3MulticastGroupId = L3_MULTICAST_TYPE |
688 vlanId.toShort() << 16 | (TYPE_VLAN_MASK & l3MulticastIndex);
689 final GroupKey l3MulticastGroupKey =
690 new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(l3MulticastIndex));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700691
692 GroupDescription l3MulticastGroupDesc = new DefaultGroupDescription(deviceId,
693 GroupDescription.Type.ALL,
694 new GroupBuckets(l3McastBuckets),
695 l3MulticastGroupKey,
696 l3MulticastGroupId,
697 nextObj.appId());
698
699 // Put all dependency information into allGroupKeys
700 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
701 groupInfos.forEach(groupInfo -> {
702 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
703 gkeyChain.addFirst(groupInfo.innerMostGroupDesc.appCookie());
704 // Add L3 interface group to the chain if there is one.
705 if (!groupInfo.nextGroupDesc.equals(groupInfo.innerMostGroupDesc)) {
706 gkeyChain.addFirst(groupInfo.nextGroupDesc.appCookie());
707 }
708 gkeyChain.addFirst(l3MulticastGroupKey);
709 allGroupKeys.add(gkeyChain);
710 });
711
712 // Point the next objective to this group
713 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
714 updatePendingNextObjective(l3MulticastGroupKey, ofdpaGrp);
715
716 GroupChainElem outerGce = new GroupChainElem(l3MulticastGroupDesc,
717 groupInfos.size(), false);
718 groupInfos.forEach(groupInfo -> {
719 // Point this group (L3 multicast) to the next group
720 updatePendingGroups(groupInfo.nextGroupDesc.appCookie(), outerGce);
721
722 // Point next group to inner-most group, if any
723 if (!groupInfo.nextGroupDesc.equals(groupInfo.innerMostGroupDesc)) {
724 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc,
725 1, false);
726 updatePendingGroups(groupInfo.innerMostGroupDesc.appCookie(), innerGce);
727 }
728
729 // Start installing the inner-most group
730 groupService.addGroup(groupInfo.innerMostGroupDesc);
731 });
Charles Chan188ebf52015-12-23 00:15:11 -0800732 }
733
Charles Chan188ebf52015-12-23 00:15:11 -0800734 /**
735 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
736 * a chain of groups. The hashed Next Objective passed in by the application
737 * has to be broken up into a group chain comprising of an
738 * L3 ECMP group as the top level group. Buckets of this group can point
739 * to a variety of groups in a group chain, depending on the whether
740 * MPLS labels are being pushed or not.
741 * <p>
742 * NOTE: We do not create MPLS ECMP groups as they are unimplemented in
743 * OF-DPA 2.0 (even though it is in the spec). Therefore we do not
744 * check the nextObjective meta to see what is matching before being
745 * sent to this nextObjective.
746 *
747 * @param nextObj the nextObjective of type HASHED
748 */
749 private void processHashedNextObjective(NextObjective nextObj) {
750 // storage for all group keys in the chain of groups created
751 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
752 List<GroupInfo> unsentGroups = new ArrayList<>();
753 createHashBucketChains(nextObj, allGroupKeys, unsentGroups);
754
755 // now we can create the outermost L3 ECMP group
756 List<GroupBucket> l3ecmpGroupBuckets = new ArrayList<>();
757 for (GroupInfo gi : unsentGroups) {
758 // create ECMP bucket to point to the outer group
759 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700760 ttb.group(new DefaultGroupId(gi.nextGroupDesc.givenGroupId()));
Charles Chan188ebf52015-12-23 00:15:11 -0800761 GroupBucket sbucket = DefaultGroupBucket
762 .createSelectGroupBucket(ttb.build());
763 l3ecmpGroupBuckets.add(sbucket);
764 }
Saurav Das8be4e3a2016-03-11 17:19:07 -0800765 int l3ecmpIndex = getNextAvailableIndex();
766 int l3ecmpGroupId = L3_ECMP_TYPE | (TYPE_MASK & l3ecmpIndex);
767 GroupKey l3ecmpGroupKey = new DefaultGroupKey(
Charles Chan361154b2016-03-24 10:23:39 -0700768 Ofdpa2Pipeline.appKryo.serialize(l3ecmpIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800769 GroupDescription l3ecmpGroupDesc =
770 new DefaultGroupDescription(
771 deviceId,
772 GroupDescription.Type.SELECT,
773 new GroupBuckets(l3ecmpGroupBuckets),
774 l3ecmpGroupKey,
775 l3ecmpGroupId,
776 nextObj.appId());
777 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
778 l3ecmpGroupBuckets.size(),
779 false);
780
781 // create objects for local and distributed storage
782 allGroupKeys.forEach(gkeyChain -> gkeyChain.addFirst(l3ecmpGroupKey));
783 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
784
785 // store l3ecmpGroupKey with the ofdpaGroupChain for the nextObjective
786 // that depends on it
787 updatePendingNextObjective(l3ecmpGroupKey, ofdpaGrp);
788
789 log.debug("Trying L3ECMP: device:{} gid:{} gkey:{} nextId:{}",
790 deviceId, Integer.toHexString(l3ecmpGroupId),
791 l3ecmpGroupKey, nextObj.id());
792 // finally we are ready to send the innermost groups
793 for (GroupInfo gi : unsentGroups) {
794 log.debug("Sending innermost group {} in group chain on device {} ",
Charles Chan5b9df8d2016-03-28 22:21:40 -0700795 Integer.toHexString(gi.innerMostGroupDesc.givenGroupId()), deviceId);
796 updatePendingGroups(gi.nextGroupDesc.appCookie(), l3ecmpGce);
797 groupService.addGroup(gi.innerMostGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800798 }
799
800 }
801
802 /**
803 * Creates group chains for all buckets in a hashed group, and stores the
804 * GroupInfos and GroupKeys for all the groups in the lists passed in, which
805 * should be empty.
806 * <p>
807 * Does not create the top level ECMP group. Does not actually send the
808 * groups to the groupService.
809 *
810 * @param nextObj the Next Objective with buckets that need to be converted
811 * to group chains
812 * @param allGroupKeys a list to store groupKey for each bucket-group-chain
813 * @param unsentGroups a list to store GroupInfo for each bucket-group-chain
814 */
815 private void createHashBucketChains(NextObjective nextObj,
Saurav Das8be4e3a2016-03-11 17:19:07 -0800816 List<Deque<GroupKey>> allGroupKeys,
817 List<GroupInfo> unsentGroups) {
Charles Chan188ebf52015-12-23 00:15:11 -0800818 // break up hashed next objective to multiple groups
819 Collection<TrafficTreatment> buckets = nextObj.next();
820
821 for (TrafficTreatment bucket : buckets) {
822 //figure out how many labels are pushed in each bucket
823 int labelsPushed = 0;
824 MplsLabel innermostLabel = null;
825 for (Instruction ins : bucket.allInstructions()) {
826 if (ins.type() == Instruction.Type.L2MODIFICATION) {
827 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
828 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_PUSH) {
829 labelsPushed++;
830 }
831 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) {
832 if (innermostLabel == null) {
Ray Milkey125572b2016-02-22 16:48:17 -0800833 innermostLabel = ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label();
Charles Chan188ebf52015-12-23 00:15:11 -0800834 }
835 }
836 }
837 }
838
839 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
840 // XXX we only deal with 0 and 1 label push right now
841 if (labelsPushed == 0) {
842 GroupInfo nolabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
843 nextObj.appId(), false,
844 nextObj.meta());
845 if (nolabelGroupInfo == null) {
846 log.error("Could not process nextObj={} in dev:{}",
847 nextObj.id(), deviceId);
848 return;
849 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700850 gkeyChain.addFirst(nolabelGroupInfo.innerMostGroupDesc.appCookie());
851 gkeyChain.addFirst(nolabelGroupInfo.nextGroupDesc.appCookie());
Charles Chan188ebf52015-12-23 00:15:11 -0800852
853 // we can't send the inner group description yet, as we have to
854 // create the dependent ECMP group first. So we store..
855 unsentGroups.add(nolabelGroupInfo);
856
857 } else if (labelsPushed == 1) {
858 GroupInfo onelabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
859 nextObj.appId(), true,
860 nextObj.meta());
861 if (onelabelGroupInfo == null) {
862 log.error("Could not process nextObj={} in dev:{}",
863 nextObj.id(), deviceId);
864 return;
865 }
866 // we need to add another group to this chain - the L3VPN group
867 TrafficTreatment.Builder l3vpnTtb = DefaultTrafficTreatment.builder();
868 l3vpnTtb.pushMpls()
869 .setMpls(innermostLabel)
Charles Chan40132b32017-01-22 00:19:37 -0800870 .group(new DefaultGroupId(onelabelGroupInfo.nextGroupDesc.givenGroupId()));
871 if (supportCopyTtl()) {
872 l3vpnTtb.copyTtlOut();
873 }
874 if (supportSetMplsBos()) {
875 l3vpnTtb.setMplsBos(true);
876 }
877
Charles Chan188ebf52015-12-23 00:15:11 -0800878 GroupBucket l3vpnGrpBkt =
879 DefaultGroupBucket.createIndirectGroupBucket(l3vpnTtb.build());
Saurav Das8be4e3a2016-03-11 17:19:07 -0800880 int l3vpnIndex = getNextAvailableIndex();
881 int l3vpngroupId = MPLS_L3VPN_SUBTYPE | (SUBTYPE_MASK & l3vpnIndex);
882 GroupKey l3vpngroupkey = new DefaultGroupKey(
Charles Chan361154b2016-03-24 10:23:39 -0700883 Ofdpa2Pipeline.appKryo.serialize(l3vpnIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800884 GroupDescription l3vpnGroupDesc =
885 new DefaultGroupDescription(
886 deviceId,
887 GroupDescription.Type.INDIRECT,
888 new GroupBuckets(Collections.singletonList(
889 l3vpnGrpBkt)),
890 l3vpngroupkey,
891 l3vpngroupId,
892 nextObj.appId());
893 GroupChainElem l3vpnGce = new GroupChainElem(l3vpnGroupDesc, 1, false);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700894 updatePendingGroups(onelabelGroupInfo.nextGroupDesc.appCookie(), l3vpnGce);
Charles Chan188ebf52015-12-23 00:15:11 -0800895
Charles Chan5b9df8d2016-03-28 22:21:40 -0700896 gkeyChain.addFirst(onelabelGroupInfo.innerMostGroupDesc.appCookie());
897 gkeyChain.addFirst(onelabelGroupInfo.nextGroupDesc.appCookie());
Charles Chan188ebf52015-12-23 00:15:11 -0800898 gkeyChain.addFirst(l3vpngroupkey);
899
900 //now we can replace the outerGrpDesc with the one we just created
Charles Chan5b9df8d2016-03-28 22:21:40 -0700901 onelabelGroupInfo.nextGroupDesc = l3vpnGroupDesc;
Charles Chan188ebf52015-12-23 00:15:11 -0800902
903 // we can't send the innermost group yet, as we have to create
904 // the dependent ECMP group first. So we store ...
905 unsentGroups.add(onelabelGroupInfo);
906
907 log.debug("Trying L3VPN: device:{} gid:{} gkey:{} nextId:{}",
908 deviceId, Integer.toHexString(l3vpngroupId),
909 l3vpngroupkey, nextObj.id());
910
911 } else {
912 log.warn("Driver currently does not handle more than 1 MPLS "
913 + "labels. Not processing nextObjective {}", nextObj.id());
914 return;
915 }
916
917 // all groups in this chain
918 allGroupKeys.add(gkeyChain);
919 }
920 }
921
Saurav Das8be4e3a2016-03-11 17:19:07 -0800922 //////////////////////////////////////
923 // Group Editing
924 //////////////////////////////////////
925
Charles Chan188ebf52015-12-23 00:15:11 -0800926 /**
927 * Adds a bucket to the top level group of a group-chain, and creates the chain.
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700928 * Ensures that bucket being added is not a duplicate, by checking existing
929 * buckets for the same outport.
Charles Chan188ebf52015-12-23 00:15:11 -0800930 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700931 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -0800932 * @param next the representation of the existing group-chain for this next objective
933 */
934 protected void addBucketToGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -0800935 if (nextObjective.type() != NextObjective.Type.HASHED &&
936 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -0800937 log.warn("AddBuckets not applied to nextType:{} in dev:{} for next:{}",
938 nextObjective.type(), deviceId, nextObjective.id());
Saurav Das1a129a02016-11-18 15:21:57 -0800939 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -0800940 return;
941 }
942 if (nextObjective.next().size() > 1) {
Saurav Das1a129a02016-11-18 15:21:57 -0800943 // FIXME - support editing multiple buckets CORD-555
Charles Chan188ebf52015-12-23 00:15:11 -0800944 log.warn("Only one bucket can be added at a time");
Saurav Das1a129a02016-11-18 15:21:57 -0800945 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -0800946 return;
947 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700948 // first check to see if bucket being added is not a duplicate of an
949 // existing bucket. If it is for an existing outport, then its a duplicate.
950 Set<PortNumber> existingOutPorts = new HashSet<>();
951 List<Deque<GroupKey>> allActiveKeys = Ofdpa2Pipeline.appKryo.deserialize(next.data());
952 for (Deque<GroupKey> gkeys : allActiveKeys) {
953 // get the last group for the outport
954 Group glast = groupService.getGroup(deviceId, gkeys.peekLast());
955 if (glast != null && !glast.buckets().buckets().isEmpty()) {
956 PortNumber op = readOutPortFromTreatment(
957 glast.buckets().buckets().get(0).treatment());
958 if (op != null) {
959 existingOutPorts.add(op);
960 }
961 }
962 }
963 // only a single bucket being added
964 TrafficTreatment tt = nextObjective.next().iterator().next();
965 PortNumber newport = readOutPortFromTreatment(tt);
966 if (existingOutPorts.contains(newport)) {
Charles Chane5ccd582016-11-10 14:16:54 -0800967 log.info("Attempt to add bucket for existing outport:{} in dev:{} for next:{}",
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700968 newport, deviceId, nextObjective.id());
969 return;
970 }
Saurav Das1a129a02016-11-18 15:21:57 -0800971 if (nextObjective.type() == NextObjective.Type.HASHED) {
972 addBucketToHashGroup(nextObjective, allActiveKeys, newport);
973 } else if (nextObjective.type() == NextObjective.Type.BROADCAST) {
974 addBucketToBroadcastGroup(nextObjective, allActiveKeys, newport);
975 }
976 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700977
Saurav Das1a129a02016-11-18 15:21:57 -0800978 private void addBucketToHashGroup(NextObjective nextObjective,
979 List<Deque<GroupKey>> allActiveKeys,
980 PortNumber newport) {
Charles Chan188ebf52015-12-23 00:15:11 -0800981 // storage for all group keys in the chain of groups created
982 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
983 List<GroupInfo> unsentGroups = new ArrayList<>();
984 createHashBucketChains(nextObjective, allGroupKeys, unsentGroups);
985
Saurav Das1a129a02016-11-18 15:21:57 -0800986 // now we can create the bucket to add to the outermost L3 ECMP group
Charles Chan188ebf52015-12-23 00:15:11 -0800987 GroupInfo gi = unsentGroups.get(0); // only one bucket, so only one group-chain
988 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700989 ttb.group(new DefaultGroupId(gi.nextGroupDesc.givenGroupId()));
Charles Chan188ebf52015-12-23 00:15:11 -0800990 GroupBucket sbucket = DefaultGroupBucket.createSelectGroupBucket(ttb.build());
991
Saurav Das1a129a02016-11-18 15:21:57 -0800992 // retrieve the original L3 ECMP group
993 Group l3ecmpGroup = retrieveTopLevelGroup(allActiveKeys, nextObjective.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700994 if (l3ecmpGroup == null) {
Saurav Das1a129a02016-11-18 15:21:57 -0800995 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.GROUPMISSING);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700996 return;
997 }
Saurav Das1a129a02016-11-18 15:21:57 -0800998 GroupKey l3ecmpGroupKey = l3ecmpGroup.appCookie();
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700999 int l3ecmpGroupId = l3ecmpGroup.id().id();
Charles Chan188ebf52015-12-23 00:15:11 -08001000
1001 // Although GroupDescriptions are not necessary for adding buckets to
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001002 // existing groups, we still use one in the GroupChainElem. When the latter is
Charles Chan188ebf52015-12-23 00:15:11 -08001003 // processed, the info will be extracted for the bucketAdd call to groupService
1004 GroupDescription l3ecmpGroupDesc =
1005 new DefaultGroupDescription(
1006 deviceId,
1007 GroupDescription.Type.SELECT,
1008 new GroupBuckets(Collections.singletonList(sbucket)),
1009 l3ecmpGroupKey,
1010 l3ecmpGroupId,
1011 nextObjective.appId());
1012 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc, 1, true);
1013
1014 // update original NextGroup with new bucket-chain
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001015 // If active keys shows only the top-level group without a chain of groups,
1016 // then it represents an empty group. Update by replacing empty chain.
Charles Chan188ebf52015-12-23 00:15:11 -08001017 Deque<GroupKey> newBucketChain = allGroupKeys.get(0);
1018 newBucketChain.addFirst(l3ecmpGroupKey);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001019 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1020 allActiveKeys.clear();
1021 }
1022 allActiveKeys.add(newBucketChain);
1023 updatePendingNextObjective(l3ecmpGroupKey,
1024 new OfdpaNextGroup(allActiveKeys, nextObjective));
Charles Chan188ebf52015-12-23 00:15:11 -08001025 log.debug("Adding to L3ECMP: device:{} gid:{} gkey:{} nextId:{}",
1026 deviceId, Integer.toHexString(l3ecmpGroupId),
1027 l3ecmpGroupKey, nextObjective.id());
1028 // send the innermost group
1029 log.debug("Sending innermost group {} in group chain on device {} ",
Charles Chan5b9df8d2016-03-28 22:21:40 -07001030 Integer.toHexString(gi.innerMostGroupDesc.givenGroupId()), deviceId);
1031 updatePendingGroups(gi.nextGroupDesc.appCookie(), l3ecmpGce);
1032 groupService.addGroup(gi.innerMostGroupDesc);
Saurav Das1a129a02016-11-18 15:21:57 -08001033 }
Charles Chan188ebf52015-12-23 00:15:11 -08001034
Saurav Das1a129a02016-11-18 15:21:57 -08001035 private void addBucketToBroadcastGroup(NextObjective nextObj,
1036 List<Deque<GroupKey>> allActiveKeys,
1037 PortNumber newport) {
1038 VlanId assignedVlan = Ofdpa2Pipeline.readVlanFromSelector(nextObj.meta());
1039 if (assignedVlan == null) {
1040 log.warn("VLAN ID required by broadcast next obj is missing. "
1041 + "Aborting add bucket to broadcast group for next:{} in dev:{}",
1042 nextObj.id(), deviceId);
1043 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
1044 return;
1045 }
1046
1047 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
1048
1049 IpPrefix ipDst = Ofdpa2Pipeline.readIpDstFromSelector(nextObj.meta());
1050 if (ipDst != null) {
1051 if (ipDst.isMulticast()) {
1052 addBucketToL3MulticastGroup(nextObj, allActiveKeys,
1053 groupInfos, assignedVlan, newport);
1054 } else {
1055 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
1056 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
1057 return;
1058 }
1059 } else {
1060 addBucketToL2FloodGroup(nextObj, allActiveKeys,
1061 groupInfos, assignedVlan, newport);
1062 }
1063 }
1064
1065 private void addBucketToL2FloodGroup(NextObjective nextObj,
1066 List<Deque<GroupKey>> allActiveKeys,
1067 List<GroupInfo> groupInfos,
1068 VlanId assignedVlan,
1069 PortNumber newport) {
1070 // create the bucket to add to the outermost L2 Flood group
1071 GroupInfo groupInfo = groupInfos.get(0); // only one bucket to add
1072 GroupDescription l2intGrpDesc = groupInfo.nextGroupDesc;
1073 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
1074 ttb.group(new DefaultGroupId(l2intGrpDesc.givenGroupId()));
1075 GroupBucket abucket = DefaultGroupBucket.createAllGroupBucket(ttb.build());
1076 // get the group being edited
1077 Group l2floodGroup = retrieveTopLevelGroup(allActiveKeys, nextObj.id());
1078 if (l2floodGroup == null) {
1079 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.GROUPMISSING);
1080 return;
1081 }
1082 GroupKey l2floodGroupKey = l2floodGroup.appCookie();
1083 int l2floodGroupId = l2floodGroup.id().id();
1084
1085 //ensure assignedVlan applies to the chosen group
1086 VlanId expectedVlan = VlanId.vlanId((short) ((l2floodGroupId & 0x0fff0000) >> 16));
1087 if (!expectedVlan.equals(assignedVlan)) {
1088 log.warn("VLAN ID {} does not match Flood group {} to which bucket is "
1089 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1090 Integer.toHexString(l2floodGroupId), nextObj.id(), deviceId);
1091 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
1092 }
1093 GroupDescription l2floodGroupDescription =
1094 new DefaultGroupDescription(
1095 deviceId,
1096 GroupDescription.Type.ALL,
1097 new GroupBuckets(Collections.singletonList(abucket)),
1098 l2floodGroupKey,
1099 l2floodGroupId,
1100 nextObj.appId());
1101 GroupChainElem l2floodGce = new GroupChainElem(l2floodGroupDescription, 1, true);
1102
1103 // update original NextGroup with new bucket-chain
1104 // If active keys shows only the top-level group without a chain of groups,
1105 // then it represents an empty group. Update by replacing empty chain.
1106 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
1107 newBucketChain.addFirst(groupInfo.nextGroupDesc.appCookie());
1108 newBucketChain.addFirst(l2floodGroupKey);
1109 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1110 allActiveKeys.clear();
1111 }
1112 allActiveKeys.add(newBucketChain);
1113 updatePendingNextObjective(l2floodGroupKey,
1114 new OfdpaNextGroup(allActiveKeys, nextObj));
1115 log.debug("Adding to L2FLOOD: device:{} gid:{} gkey:{} nextId:{}",
1116 deviceId, Integer.toHexString(l2floodGroupId),
1117 l2floodGroupKey, nextObj.id());
1118 // send the innermost group
1119 log.debug("Sending innermost group {} in group chain on device {} ",
1120 Integer.toHexString(groupInfo.innerMostGroupDesc.givenGroupId()),
1121 deviceId);
1122 updatePendingGroups(groupInfo.nextGroupDesc.appCookie(), l2floodGce);
1123 groupService.addGroup(groupInfo.innerMostGroupDesc);
1124 }
1125
1126 private void addBucketToL3MulticastGroup(NextObjective nextObj,
1127 List<Deque<GroupKey>> allActiveKeys,
1128 List<GroupInfo> groupInfos,
1129 VlanId assignedVlan,
1130 PortNumber newport) {
1131 // create the bucket to add to the outermost L3 Multicast group
1132 GroupInfo groupInfo = groupInfos.get(0); // only one bucket to add
1133 // Points to L3 interface group if there is one.
1134 // Otherwise points to L2 interface group directly.
1135 GroupDescription nextGroupDesc = (groupInfo.nextGroupDesc != null) ?
1136 groupInfo.nextGroupDesc : groupInfo.innerMostGroupDesc;
1137 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
1138 ttb.group(new DefaultGroupId(nextGroupDesc.givenGroupId()));
1139 GroupBucket abucket = DefaultGroupBucket.createAllGroupBucket(ttb.build());
1140
1141 // get the group being edited
1142 Group l3mcastGroup = retrieveTopLevelGroup(allActiveKeys, nextObj.id());
1143 if (l3mcastGroup == null) {
1144 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.GROUPMISSING);
1145 return;
1146 }
1147 GroupKey l3mcastGroupKey = l3mcastGroup.appCookie();
1148 int l3mcastGroupId = l3mcastGroup.id().id();
1149
1150 //ensure assignedVlan applies to the chosen group
1151 VlanId expectedVlan = VlanId.vlanId((short) ((l3mcastGroupId & 0x0fff0000) >> 16));
1152 if (!expectedVlan.equals(assignedVlan)) {
1153 log.warn("VLAN ID {} does not match L3 Mcast group {} to which bucket is "
1154 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1155 Integer.toHexString(l3mcastGroupId), nextObj.id(), deviceId);
1156 Ofdpa2Pipeline.fail(nextObj, ObjectiveError.BADPARAMS);
1157 }
1158 GroupDescription l3mcastGroupDescription =
1159 new DefaultGroupDescription(
1160 deviceId,
1161 GroupDescription.Type.ALL,
1162 new GroupBuckets(Collections.singletonList(abucket)),
1163 l3mcastGroupKey,
1164 l3mcastGroupId,
1165 nextObj.appId());
1166 GroupChainElem l3mcastGce = new GroupChainElem(l3mcastGroupDescription,
1167 1, true);
1168
1169 // update original NextGroup with new bucket-chain
1170 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
1171 newBucketChain.addFirst(groupInfo.innerMostGroupDesc.appCookie());
1172 // Add L3 interface group to the chain if there is one.
1173 if (!groupInfo.nextGroupDesc.equals(groupInfo.innerMostGroupDesc)) {
1174 newBucketChain.addFirst(groupInfo.nextGroupDesc.appCookie());
1175 }
1176 newBucketChain.addFirst(l3mcastGroupKey);
1177 // If active keys shows only the top-level group without a chain of groups,
1178 // then it represents an empty group. Update by replacing empty chain.
1179 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1180 allActiveKeys.clear();
1181 }
1182 allActiveKeys.add(newBucketChain);
1183 updatePendingNextObjective(l3mcastGroupKey,
1184 new OfdpaNextGroup(allActiveKeys, nextObj));
1185
1186 updatePendingGroups(groupInfo.nextGroupDesc.appCookie(), l3mcastGce);
1187 // Point next group to inner-most group, if any
1188 if (!groupInfo.nextGroupDesc.equals(groupInfo.innerMostGroupDesc)) {
1189 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc,
1190 1, false);
1191 updatePendingGroups(groupInfo.innerMostGroupDesc.appCookie(), innerGce);
1192 }
1193 log.debug("Adding to L3MCAST: device:{} gid:{} gkey:{} nextId:{}",
1194 deviceId, Integer.toHexString(l3mcastGroupId),
1195 l3mcastGroupKey, nextObj.id());
1196 // send the innermost group
1197 log.debug("Sending innermost group {} in group chain on device {} ",
1198 Integer.toHexString(groupInfo.innerMostGroupDesc.givenGroupId()),
1199 deviceId);
1200 groupService.addGroup(groupInfo.innerMostGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -08001201 }
1202
1203 /**
1204 * Removes the bucket in the top level group of a possible group-chain. Does
Saurav Das1a129a02016-11-18 15:21:57 -08001205 * not remove the groups in the group-chain pointed to by this bucket, as they
Charles Chan188ebf52015-12-23 00:15:11 -08001206 * may be in use (referenced by other groups) elsewhere.
1207 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001208 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -08001209 * @param next the representation of the existing group-chain for this next objective
1210 */
1211 protected void removeBucketFromGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001212 if (nextObjective.type() != NextObjective.Type.HASHED &&
1213 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001214 log.warn("RemoveBuckets not applied to nextType:{} in dev:{} for next:{}",
1215 nextObjective.type(), deviceId, nextObjective.id());
Saurav Das1a129a02016-11-18 15:21:57 -08001216 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001217 return;
1218 }
1219 Collection<TrafficTreatment> treatments = nextObjective.next();
1220 TrafficTreatment treatment = treatments.iterator().next();
1221 // find the bucket to remove by noting the outport, and figuring out the
1222 // top-level group in the group-chain that indirectly references the port
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001223 PortNumber portToRemove = readOutPortFromTreatment(treatment);
1224 if (portToRemove == null) {
1225 log.warn("next objective {} has no outport.. cannot remove bucket"
1226 + "from group in dev: {}", nextObjective.id(), deviceId);
Saurav Das1a129a02016-11-18 15:21:57 -08001227 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -08001228 return;
1229 }
1230
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001231 List<Deque<GroupKey>> allActiveKeys = Ofdpa2Pipeline.appKryo.deserialize(next.data());
Charles Chan188ebf52015-12-23 00:15:11 -08001232 Deque<GroupKey> foundChain = null;
1233 int index = 0;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001234 for (Deque<GroupKey> gkeys : allActiveKeys) {
1235 // last group in group chain should have a single bucket pointing to port
Charles Chan188ebf52015-12-23 00:15:11 -08001236 GroupKey groupWithPort = gkeys.peekLast();
1237 Group group = groupService.getGroup(deviceId, groupWithPort);
1238 if (group == null) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001239 log.warn("Inconsistent group chain found when removing bucket"
1240 + "for next:{} in dev:{}", nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001241 continue;
1242 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001243 PortNumber pout = readOutPortFromTreatment(
1244 group.buckets().buckets().get(0).treatment());
1245 if (pout.equals(portToRemove)) {
1246 foundChain = gkeys;
Charles Chan188ebf52015-12-23 00:15:11 -08001247 break;
1248 }
1249 index++;
1250 }
Saurav Das1a129a02016-11-18 15:21:57 -08001251 if (foundChain == null) {
Charles Chan188ebf52015-12-23 00:15:11 -08001252 log.warn("Could not find appropriate group-chain for removing bucket"
1253 + " for next id {} in dev:{}", nextObjective.id(), deviceId);
Saurav Das1a129a02016-11-18 15:21:57 -08001254 Ofdpa2Pipeline.fail(nextObjective, ObjectiveError.BADPARAMS);
1255 return;
Charles Chan188ebf52015-12-23 00:15:11 -08001256 }
Saurav Das1a129a02016-11-18 15:21:57 -08001257
1258 //first groupkey is the one we want to modify
1259 GroupKey modGroupKey = foundChain.peekFirst();
1260 Group modGroup = groupService.getGroup(deviceId, modGroupKey);
1261 //second groupkey is the one we wish to remove the reference to
1262 GroupKey pointedGroupKey = null;
1263 int i = 0;
1264 for (GroupKey gk : foundChain) {
1265 if (i++ == 1) {
1266 pointedGroupKey = gk;
1267 break;
1268 }
1269 }
1270 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
1271 GroupBucket bucket = null;
1272 if (nextObjective.type() == NextObjective.Type.HASHED) {
1273 bucket = DefaultGroupBucket.createSelectGroupBucket(
1274 DefaultTrafficTreatment.builder()
1275 .group(pointedGroup.id())
1276 .build());
1277 } else {
1278 bucket = DefaultGroupBucket.createAllGroupBucket(
1279 DefaultTrafficTreatment.builder()
1280 .group(pointedGroup.id())
1281 .build());
1282 }
1283 GroupBuckets removeBuckets = new GroupBuckets(Collections
1284 .singletonList(bucket));
1285 log.debug("Removing buckets from group id 0x{} pointing to group id 0x{} "
1286 + "for next id {} in device {}", Integer.toHexString(modGroup.id().id()),
1287 Integer.toHexString(pointedGroup.id().id()), nextObjective.id(), deviceId);
1288 groupService.removeBucketsFromGroup(deviceId, modGroupKey,
1289 removeBuckets, modGroupKey,
1290 nextObjective.appId());
1291 // update store
1292 // If the bucket removed was the last bucket in the group, then
1293 // retain an entry for the top level group which still exists.
1294 if (allActiveKeys.size() == 1) {
1295 ArrayDeque<GroupKey> top = new ArrayDeque<>();
1296 top.add(modGroupKey);
1297 allActiveKeys.add(top);
1298 }
1299 allActiveKeys.remove(index);
1300 flowObjectiveStore.putNextGroup(nextObjective.id(),
1301 new OfdpaNextGroup(allActiveKeys,
1302 nextObjective));
Charles Chan188ebf52015-12-23 00:15:11 -08001303 }
1304
1305 /**
1306 * Removes all groups in multiple possible group-chains that represent the next
1307 * objective.
1308 *
1309 * @param nextObjective the next objective to remove
1310 * @param next the NextGroup that represents the existing group-chain for
1311 * this next objective
1312 */
1313 protected void removeGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001314 List<Deque<GroupKey>> allActiveKeys = Ofdpa2Pipeline.appKryo.deserialize(next.data());
Charles Chanfc5c7802016-05-17 13:13:55 -07001315
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001316 List<GroupKey> groupKeys = allActiveKeys.stream()
Charles Chanfc5c7802016-05-17 13:13:55 -07001317 .map(Deque::getFirst).collect(Collectors.toList());
1318 pendingRemoveNextObjectives.put(nextObjective, groupKeys);
1319
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001320 allActiveKeys.forEach(groupChain -> groupChain.forEach(groupKey ->
Charles Chan188ebf52015-12-23 00:15:11 -08001321 groupService.removeGroup(deviceId, groupKey, nextObjective.appId())));
1322 flowObjectiveStore.removeNextGroup(nextObjective.id());
1323 }
1324
Saurav Das8be4e3a2016-03-11 17:19:07 -08001325 //////////////////////////////////////
1326 // Helper Methods and Classes
1327 //////////////////////////////////////
1328
1329 private void updatePendingNextObjective(GroupKey key, OfdpaNextGroup value) {
1330 List<OfdpaNextGroup> nextList = new CopyOnWriteArrayList<OfdpaNextGroup>();
1331 nextList.add(value);
Charles Chanfc5c7802016-05-17 13:13:55 -07001332 List<OfdpaNextGroup> ret = pendingAddNextObjectives.asMap()
Saurav Das8be4e3a2016-03-11 17:19:07 -08001333 .putIfAbsent(key, nextList);
1334 if (ret != null) {
1335 ret.add(value);
1336 }
1337 }
1338
Charles Chan425854b2016-04-11 15:32:12 -07001339 protected void updatePendingGroups(GroupKey gkey, GroupChainElem gce) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001340 Set<GroupChainElem> gceSet = Collections.newSetFromMap(
1341 new ConcurrentHashMap<GroupChainElem, Boolean>());
1342 gceSet.add(gce);
1343 Set<GroupChainElem> retval = pendingGroups.putIfAbsent(gkey, gceSet);
1344 if (retval != null) {
1345 retval.add(gce);
1346 }
1347 }
1348
Charles Chan188ebf52015-12-23 00:15:11 -08001349 /**
1350 * Processes next element of a group chain. Assumption is that if this
1351 * group points to another group, the latter has already been created
1352 * and this driver has received notification for it. A second assumption is
1353 * that if there is another group waiting for this group then the appropriate
1354 * stores already have the information to act upon the notification for the
1355 * creation of this group.
1356 * <p>
1357 * The processing of the GroupChainElement depends on the number of groups
1358 * this element is waiting on. For all group types other than SIMPLE, a
1359 * GroupChainElement could be waiting on multiple groups.
1360 *
1361 * @param gce the group chain element to be processed next
1362 */
1363 private void processGroupChain(GroupChainElem gce) {
1364 int waitOnGroups = gce.decrementAndGetGroupsWaitedOn();
1365 if (waitOnGroups != 0) {
1366 log.debug("GCE: {} not ready to be processed", gce);
1367 return;
1368 }
1369 log.debug("GCE: {} ready to be processed", gce);
1370 if (gce.addBucketToGroup) {
1371 groupService.addBucketsToGroup(gce.groupDescription.deviceId(),
1372 gce.groupDescription.appCookie(),
1373 gce.groupDescription.buckets(),
1374 gce.groupDescription.appCookie(),
1375 gce.groupDescription.appId());
1376 } else {
1377 groupService.addGroup(gce.groupDescription);
1378 }
1379 }
1380
1381 private class GroupChecker implements Runnable {
1382 @Override
1383 public void run() {
1384 Set<GroupKey> keys = pendingGroups.keySet().stream()
1385 .filter(key -> groupService.getGroup(deviceId, key) != null)
1386 .collect(Collectors.toSet());
Charles Chanfc5c7802016-05-17 13:13:55 -07001387 Set<GroupKey> otherkeys = pendingAddNextObjectives.asMap().keySet().stream()
Charles Chan188ebf52015-12-23 00:15:11 -08001388 .filter(otherkey -> groupService.getGroup(deviceId, otherkey) != null)
1389 .collect(Collectors.toSet());
1390 keys.addAll(otherkeys);
1391
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -07001392 keys.forEach(key ->
Charles Chanfc5c7802016-05-17 13:13:55 -07001393 processPendingAddGroupsOrNextObjs(key, false));
Charles Chan188ebf52015-12-23 00:15:11 -08001394 }
1395 }
1396
Saurav Das8be4e3a2016-03-11 17:19:07 -08001397 private class InnerGroupListener implements GroupListener {
1398 @Override
1399 public void event(GroupEvent event) {
1400 log.trace("received group event of type {}", event.type());
Charles Chanfc5c7802016-05-17 13:13:55 -07001401 switch (event.type()) {
1402 case GROUP_ADDED:
1403 processPendingAddGroupsOrNextObjs(event.subject().appCookie(), true);
1404 break;
1405 case GROUP_REMOVED:
1406 processPendingRemoveNextObjs(event.subject().appCookie());
1407 break;
1408 default:
1409 break;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001410 }
1411 }
1412 }
1413
Charles Chanfc5c7802016-05-17 13:13:55 -07001414 private void processPendingAddGroupsOrNextObjs(GroupKey key, boolean added) {
Charles Chan188ebf52015-12-23 00:15:11 -08001415 //first check for group chain
1416 Set<GroupChainElem> gceSet = pendingGroups.remove(key);
1417 if (gceSet != null) {
1418 for (GroupChainElem gce : gceSet) {
Charles Chan216e3c82016-04-23 14:48:16 -07001419 log.debug("Group service {} group key {} in device {}. "
Saurav Das0fd79d92016-03-07 10:58:36 -08001420 + "Processing next group in group chain with group id 0x{}",
Charles Chan188ebf52015-12-23 00:15:11 -08001421 (added) ? "ADDED" : "processed",
1422 key, deviceId,
1423 Integer.toHexString(gce.groupDescription.givenGroupId()));
1424 processGroupChain(gce);
1425 }
1426 } else {
1427 // otherwise chain complete - check for waiting nextObjectives
Charles Chanfc5c7802016-05-17 13:13:55 -07001428 List<OfdpaNextGroup> nextGrpList = pendingAddNextObjectives.getIfPresent(key);
Charles Chan188ebf52015-12-23 00:15:11 -08001429 if (nextGrpList != null) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001430 pendingAddNextObjectives.invalidate(key);
Charles Chan188ebf52015-12-23 00:15:11 -08001431 nextGrpList.forEach(nextGrp -> {
Charles Chan216e3c82016-04-23 14:48:16 -07001432 log.debug("Group service {} group key {} in device:{}. "
Saurav Das0fd79d92016-03-07 10:58:36 -08001433 + "Done implementing next objective: {} <<-->> gid:0x{}",
Charles Chan188ebf52015-12-23 00:15:11 -08001434 (added) ? "ADDED" : "processed",
1435 key, deviceId, nextGrp.nextObjective().id(),
1436 Integer.toHexString(groupService.getGroup(deviceId, key)
1437 .givenGroupId()));
Charles Chan361154b2016-03-24 10:23:39 -07001438 Ofdpa2Pipeline.pass(nextGrp.nextObjective());
Charles Chan188ebf52015-12-23 00:15:11 -08001439 flowObjectiveStore.putNextGroup(nextGrp.nextObjective().id(), nextGrp);
1440 // check if addBuckets waiting for this completion
1441 NextObjective pendBkt = pendingBuckets
1442 .remove(nextGrp.nextObjective().id());
1443 if (pendBkt != null) {
1444 addBucketToGroup(pendBkt, nextGrp);
1445 }
1446 });
1447 }
1448 }
1449 }
1450
Charles Chanfc5c7802016-05-17 13:13:55 -07001451 private void processPendingRemoveNextObjs(GroupKey key) {
1452 pendingRemoveNextObjectives.asMap().forEach((nextObjective, groupKeys) -> {
1453 if (groupKeys.isEmpty()) {
1454 pendingRemoveNextObjectives.invalidate(nextObjective);
1455 Ofdpa2Pipeline.pass(nextObjective);
1456 } else {
1457 groupKeys.remove(key);
1458 }
1459 });
1460 }
1461
Charles Chan425854b2016-04-11 15:32:12 -07001462 protected int getNextAvailableIndex() {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001463 return (int) nextIndex.incrementAndGet();
1464 }
1465
Charles Chane849c192016-01-11 18:28:54 -08001466 /**
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001467 * Returns the outport in a traffic treatment.
1468 *
1469 * @param tt the treatment
1470 * @return the PortNumber for the outport or null
1471 */
1472 protected static PortNumber readOutPortFromTreatment(TrafficTreatment tt) {
1473 for (Instruction ins : tt.allInstructions()) {
1474 if (ins.type() == Instruction.Type.OUTPUT) {
1475 return ((Instructions.OutputInstruction) ins).port();
1476 }
1477 }
1478 return null;
1479 }
1480
1481 /**
Charles Chane849c192016-01-11 18:28:54 -08001482 * Returns a hash as the L2 Interface Group Key.
1483 *
1484 * Keep the lower 6-bit for port since port number usually smaller than 64.
1485 * Hash other information into remaining 28 bits.
1486 *
1487 * @param deviceId Device ID
1488 * @param vlanId VLAN ID
1489 * @param portNumber Port number
1490 * @return L2 interface group key
1491 */
Charles Chan425854b2016-04-11 15:32:12 -07001492 protected int l2InterfaceGroupKey(
Charles Chane849c192016-01-11 18:28:54 -08001493 DeviceId deviceId, VlanId vlanId, long portNumber) {
1494 int portLowerBits = (int) portNumber & PORT_LOWER_BITS_MASK;
1495 long portHigherBits = portNumber & PORT_HIGHER_BITS_MASK;
Charles Chand0fd5dc2016-02-16 23:14:49 -08001496 int hash = Objects.hash(deviceId, vlanId, portHigherBits);
Charles Chane849c192016-01-11 18:28:54 -08001497 return L2_INTERFACE_TYPE | (TYPE_MASK & hash << 6) | portLowerBits;
1498 }
1499
Saurav Das1a129a02016-11-18 15:21:57 -08001500 private Group retrieveTopLevelGroup(List<Deque<GroupKey>> allActiveKeys,
1501 int nextid) {
1502 GroupKey topLevelGroupKey = null;
1503 if (!allActiveKeys.isEmpty()) {
1504 topLevelGroupKey = allActiveKeys.get(0).peekFirst();
1505 } else {
1506 log.warn("Could not determine top level group while processing"
1507 + "next:{} in dev:{}", nextid, deviceId);
1508 return null;
1509 }
1510 Group topGroup = groupService.getGroup(deviceId, topLevelGroupKey);
1511 if (topGroup == null) {
1512 log.warn("Could not find top level group while processing "
1513 + "next:{} in dev:{}", nextid, deviceId);
1514 }
1515 return topGroup;
1516 }
1517
Charles Chan188ebf52015-12-23 00:15:11 -08001518 /**
1519 * Utility class for moving group information around.
Saurav Das1a129a02016-11-18 15:21:57 -08001520 *
1521 * Example: Suppose we are trying to create a group-chain A-B-C-D, where
1522 * A is the top level group, and D is the inner-most group, typically L2 Interface.
1523 * The innerMostGroupDesc is always D. At various stages of the creation
1524 * process the nextGroupDesc may be C or B. The nextGroupDesc exists to
1525 * inform the referencing group about which group it needs to point to,
1526 * and wait for. In some cases the group chain may simply be A-B. In this case,
1527 * both innerMostGroupDesc and nextGroupDesc will be B.
Charles Chan188ebf52015-12-23 00:15:11 -08001528 */
Charles Chan425854b2016-04-11 15:32:12 -07001529 protected class GroupInfo {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001530 /**
1531 * Description of the inner-most group of the group chain.
1532 * It is always an L2 interface group.
1533 */
1534 private GroupDescription innerMostGroupDesc;
Charles Chan188ebf52015-12-23 00:15:11 -08001535
Charles Chan5b9df8d2016-03-28 22:21:40 -07001536 /**
1537 * Description of the next group in the group chain.
1538 * It can be L2 interface, L3 interface, L3 unicast, L3 VPN group.
Saurav Das1a129a02016-11-18 15:21:57 -08001539 * It is possible that nextGroupDesc is the same as the innerMostGroup.
Charles Chan5b9df8d2016-03-28 22:21:40 -07001540 */
1541 private GroupDescription nextGroupDesc;
1542
1543 GroupInfo(GroupDescription innerMostGroupDesc, GroupDescription nextGroupDesc) {
1544 this.innerMostGroupDesc = innerMostGroupDesc;
1545 this.nextGroupDesc = nextGroupDesc;
Charles Chan188ebf52015-12-23 00:15:11 -08001546 }
1547 }
1548
1549 /**
1550 * Represents an entire group-chain that implements a Next-Objective from
1551 * the application. The objective is represented as a list of deques, where
1552 * each deque is a separate chain of groups.
1553 * <p>
1554 * For example, an ECMP group with 3 buckets, where each bucket points to
1555 * a group chain of L3 Unicast and L2 interface groups will look like this:
1556 * <ul>
1557 * <li>List[0] is a Deque of GroupKeyECMP(first)-GroupKeyL3(middle)-GroupKeyL2(last)
1558 * <li>List[1] is a Deque of GroupKeyECMP(first)-GroupKeyL3(middle)-GroupKeyL2(last)
1559 * <li>List[2] is a Deque of GroupKeyECMP(first)-GroupKeyL3(middle)-GroupKeyL2(last)
1560 * </ul>
1561 * where the first element of each deque is the same, representing the
1562 * top level ECMP group, while every other element represents a unique groupKey.
1563 * <p>
1564 * Also includes information about the next objective that
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001565 * resulted in these group-chains.
Charles Chan188ebf52015-12-23 00:15:11 -08001566 *
1567 */
1568 protected class OfdpaNextGroup implements NextGroup {
1569 private final NextObjective nextObj;
1570 private final List<Deque<GroupKey>> gkeys;
1571
1572 public OfdpaNextGroup(List<Deque<GroupKey>> gkeys, NextObjective nextObj) {
Charles Chan188ebf52015-12-23 00:15:11 -08001573 this.nextObj = nextObj;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001574 this.gkeys = gkeys;
Charles Chan188ebf52015-12-23 00:15:11 -08001575 }
1576
1577 public NextObjective nextObjective() {
1578 return nextObj;
1579 }
1580
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001581 public List<Deque<GroupKey>> groupKeys() {
1582 return gkeys;
1583 }
1584
Charles Chan188ebf52015-12-23 00:15:11 -08001585 @Override
1586 public byte[] data() {
Charles Chan361154b2016-03-24 10:23:39 -07001587 return Ofdpa2Pipeline.appKryo.serialize(gkeys);
Charles Chan188ebf52015-12-23 00:15:11 -08001588 }
1589 }
1590
1591 /**
1592 * Represents a group element that is part of a chain of groups.
1593 * Stores enough information to create a Group Description to add the group
1594 * to the switch by requesting the Group Service. Objects instantiating this
1595 * class are meant to be temporary and live as long as it is needed to wait for
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001596 * referenced groups in the group chain to be created.
Charles Chan188ebf52015-12-23 00:15:11 -08001597 */
Charles Chan425854b2016-04-11 15:32:12 -07001598 protected class GroupChainElem {
Charles Chan188ebf52015-12-23 00:15:11 -08001599 private GroupDescription groupDescription;
1600 private AtomicInteger waitOnGroups;
1601 private boolean addBucketToGroup;
1602
1603 GroupChainElem(GroupDescription groupDescription, int waitOnGroups,
1604 boolean addBucketToGroup) {
1605 this.groupDescription = groupDescription;
1606 this.waitOnGroups = new AtomicInteger(waitOnGroups);
1607 this.addBucketToGroup = addBucketToGroup;
1608 }
1609
1610 /**
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001611 * This method atomically decrements the counter for the number of
Charles Chan188ebf52015-12-23 00:15:11 -08001612 * groups this GroupChainElement is waiting on, for notifications from
1613 * the Group Service. When this method returns a value of 0, this
1614 * GroupChainElement is ready to be processed.
1615 *
1616 * @return integer indication of the number of notifications being waited on
1617 */
1618 int decrementAndGetGroupsWaitedOn() {
1619 return waitOnGroups.decrementAndGet();
1620 }
1621
1622 @Override
1623 public String toString() {
1624 return (Integer.toHexString(groupDescription.givenGroupId()) +
1625 " groupKey: " + groupDescription.appCookie() +
1626 " waiting-on-groups: " + waitOnGroups.get() +
1627 " addBucketToGroup: " + addBucketToGroup +
1628 " device: " + deviceId);
1629 }
1630 }
1631}