blob: e5b284e0fb15ba2a824b7fe11d8653bbf4c02f36 [file] [log] [blame]
Brian O'Connor7cbbbb72016-04-09 02:13:23 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Brian O'Connor7cbbbb72016-04-09 02:13:23 -07003 *
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 */
Yi Tsengef19de12017-04-24 11:33:05 -070016package org.onosproject.driver.pipeline.ofdpa;
Charles Chan188ebf52015-12-23 00:15:11 -080017
18import com.google.common.cache.Cache;
19import com.google.common.cache.CacheBuilder;
20import com.google.common.cache.RemovalCause;
21import com.google.common.cache.RemovalNotification;
Charles Chan5b9df8d2016-03-28 22:21:40 -070022import com.google.common.collect.ImmutableList;
23import com.google.common.collect.Lists;
Yi Tseng78f51f42017-02-02 13:54:58 -080024import com.google.common.collect.Sets;
Charles Chan188ebf52015-12-23 00:15:11 -080025import org.onlab.osgi.ServiceDirectory;
Charles Chan5b9df8d2016-03-28 22:21:40 -070026import org.onlab.packet.IpPrefix;
Charles Chan5270ed02016-01-30 23:22:37 -080027import org.onlab.packet.MacAddress;
Charles Chan188ebf52015-12-23 00:15:11 -080028import org.onlab.packet.MplsLabel;
29import org.onlab.packet.VlanId;
30import org.onosproject.core.ApplicationId;
Yi Tseng78f51f42017-02-02 13:54:58 -080031import org.onosproject.core.GroupId;
Charles Chan32562522016-04-07 14:37:14 -070032import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Charles Chan188ebf52015-12-23 00:15:11 -080033import org.onosproject.net.DeviceId;
34import org.onosproject.net.PortNumber;
35import org.onosproject.net.behaviour.NextGroup;
36import org.onosproject.net.behaviour.PipelinerContext;
37import org.onosproject.net.flow.DefaultTrafficTreatment;
38import org.onosproject.net.flow.TrafficSelector;
39import org.onosproject.net.flow.TrafficTreatment;
40import org.onosproject.net.flow.criteria.Criterion;
Pier Ventre42287df2016-11-09 14:17:26 -080041import org.onosproject.net.flow.criteria.TunnelIdCriterion;
Charles Chan188ebf52015-12-23 00:15:11 -080042import org.onosproject.net.flow.criteria.VlanIdCriterion;
43import org.onosproject.net.flow.instructions.Instruction;
44import org.onosproject.net.flow.instructions.Instructions;
Saurav Das041bb782017-08-14 16:44:43 -070045import org.onosproject.net.flow.instructions.Instructions.GroupInstruction;
Charles Chan188ebf52015-12-23 00:15:11 -080046import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Yi Tseng47f82dc2017-03-05 22:48:39 -080047import org.onosproject.net.flowobjective.DefaultNextObjective;
Charles Chan188ebf52015-12-23 00:15:11 -080048import org.onosproject.net.flowobjective.FlowObjectiveStore;
49import org.onosproject.net.flowobjective.NextObjective;
Saurav Dasc88d4662017-05-15 15:34:25 -070050import org.onosproject.net.flowobjective.Objective.Operation;
Yi Tseng47f82dc2017-03-05 22:48:39 -080051import org.onosproject.net.flowobjective.ObjectiveContext;
Charles Chan188ebf52015-12-23 00:15:11 -080052import org.onosproject.net.flowobjective.ObjectiveError;
53import org.onosproject.net.group.DefaultGroupBucket;
54import org.onosproject.net.group.DefaultGroupDescription;
55import org.onosproject.net.group.DefaultGroupKey;
56import org.onosproject.net.group.Group;
57import org.onosproject.net.group.GroupBucket;
58import org.onosproject.net.group.GroupBuckets;
59import org.onosproject.net.group.GroupDescription;
60import org.onosproject.net.group.GroupEvent;
61import org.onosproject.net.group.GroupKey;
62import org.onosproject.net.group.GroupListener;
63import org.onosproject.net.group.GroupService;
Saurav Das8be4e3a2016-03-11 17:19:07 -080064import org.onosproject.store.service.AtomicCounter;
65import org.onosproject.store.service.StorageService;
Charles Chan188ebf52015-12-23 00:15:11 -080066import org.slf4j.Logger;
67
68import java.util.ArrayDeque;
69import java.util.ArrayList;
Saurav Dasceccf242017-08-03 18:30:35 -070070import java.util.Arrays;
Charles Chan188ebf52015-12-23 00:15:11 -080071import java.util.Collection;
72import java.util.Collections;
73import java.util.Deque;
74import java.util.List;
Charles Chand0fd5dc2016-02-16 23:14:49 -080075import java.util.Objects;
Charles Chan188ebf52015-12-23 00:15:11 -080076import java.util.Set;
77import java.util.concurrent.ConcurrentHashMap;
78import java.util.concurrent.CopyOnWriteArrayList;
79import java.util.concurrent.Executors;
80import java.util.concurrent.ScheduledExecutorService;
81import java.util.concurrent.TimeUnit;
Charles Chan188ebf52015-12-23 00:15:11 -080082import java.util.stream.Collectors;
83
84import static org.onlab.util.Tools.groupedThreads;
Yi Tsengef19de12017-04-24 11:33:05 -070085import static org.onosproject.driver.pipeline.ofdpa.Ofdpa2Pipeline.*;
86import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
Pier Ventre42287df2016-11-09 14:17:26 -080087import static org.onosproject.net.flow.criteria.Criterion.Type.TUNNEL_ID;
Pier Ventre140a8942016-11-02 07:26:38 -070088import static org.onosproject.net.flow.criteria.Criterion.Type.VLAN_VID;
Yi Tseng78f51f42017-02-02 13:54:58 -080089import static org.onosproject.net.group.GroupDescription.Type.ALL;
90import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Charles Chan188ebf52015-12-23 00:15:11 -080091import static org.slf4j.LoggerFactory.getLogger;
92
93/**
Saurav Das961beb22017-03-29 19:09:17 -070094 * Group handler that emulates Broadcom OF-DPA TTP.
Charles Chan188ebf52015-12-23 00:15:11 -080095 */
Charles Chan361154b2016-03-24 10:23:39 -070096public class Ofdpa2GroupHandler {
Yi Tsengef19de12017-04-24 11:33:05 -070097 protected final Logger log = getLogger(getClass());
Charles Chane849c192016-01-11 18:28:54 -080098
Yi Tsengef19de12017-04-24 11:33:05 -070099 // Services, Stores
Charles Chan188ebf52015-12-23 00:15:11 -0800100 protected GroupService groupService;
Saurav Das8be4e3a2016-03-11 17:19:07 -0800101 protected StorageService storageService;
Yi Tsengef19de12017-04-24 11:33:05 -0700102 protected FlowObjectiveStore flowObjectiveStore;
Charles Chan188ebf52015-12-23 00:15:11 -0800103
104 // index number for group creation
Saurav Das8be4e3a2016-03-11 17:19:07 -0800105 private AtomicCounter nextIndex;
Charles Chan188ebf52015-12-23 00:15:11 -0800106
Yi Tsengef19de12017-04-24 11:33:05 -0700107 protected DeviceId deviceId;
108 private Cache<GroupKey, List<OfdpaGroupHandlerUtility.OfdpaNextGroup>> pendingAddNextObjectives;
109 private Cache<NextObjective, List<GroupKey>> pendingRemoveNextObjectives;
110 private Cache<GroupKey, Set<OfdpaGroupHandlerUtility.GroupChainElem>> pendingGroups;
111 private ConcurrentHashMap<GroupKey, Set<NextObjective>> pendingUpdateNextObjectives;
112
Yi Tseng47f82dc2017-03-05 22:48:39 -0800113 // local store for pending bucketAdds - by design there can be multiple
Charles Chan188ebf52015-12-23 00:15:11 -0800114 // pending bucket for a group
Yi Tseng47f82dc2017-03-05 22:48:39 -0800115 protected ConcurrentHashMap<Integer, Set<NextObjective>> pendingBuckets =
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700116 new ConcurrentHashMap<>();
Charles Chan188ebf52015-12-23 00:15:11 -0800117
Yi Tsengef19de12017-04-24 11:33:05 -0700118 private ScheduledExecutorService groupCheckerExecutor =
119 Executors.newScheduledThreadPool(2, groupedThreads("onos/pipeliner", "ofdpa-%d", log));
120
121 public Cache<GroupKey, List<OfdpaNextGroup>> pendingAddNextObjectives() {
122 return pendingAddNextObjectives;
123 }
124
125 public Cache<GroupKey, Set<GroupChainElem>> pendingGroups() {
126 return pendingGroups;
127 }
128
Charles Chan40132b32017-01-22 00:19:37 -0800129 /**
130 * Determines whether this pipeline support copy ttl instructions or not.
131 *
132 * @return true if copy ttl instructions are supported
133 */
134 protected boolean supportCopyTtl() {
135 return true;
136 }
137
138 /**
139 * Determines whether this pipeline support set mpls bos instruction or not.
140 *
141 * @return true if set mpls bos instruction is supported
142 */
143 protected boolean supportSetMplsBos() {
144 return true;
145 }
146
Charles Chan0f43e472017-02-14 14:00:16 -0800147 /**
148 * Determines whether this pipeline requires popping VLAN before pushing MPLS.
149 * <p>
150 * If required, pop vlan before push mpls and add an arbitrary vlan back afterward.
151 * MPLS interface group will substitute the arbitrary VLAN with expected VLAN later on.
152 *
153 * @return true if this pipeline requires popping VLAN before pushing MPLS
154 */
155 protected boolean requireVlanPopBeforeMplsPush() {
156 return false;
157 }
158
Charles Chan188ebf52015-12-23 00:15:11 -0800159 protected void init(DeviceId deviceId, PipelinerContext context) {
Yi Tsengef19de12017-04-24 11:33:05 -0700160 ServiceDirectory serviceDirectory = context.directory();
Charles Chan188ebf52015-12-23 00:15:11 -0800161 this.deviceId = deviceId;
162 this.flowObjectiveStore = context.store();
Charles Chan188ebf52015-12-23 00:15:11 -0800163 this.groupService = serviceDirectory.get(GroupService.class);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800164 this.storageService = serviceDirectory.get(StorageService.class);
Madan Jampanid5714e02016-04-19 14:15:20 -0700165 this.nextIndex = storageService.getAtomicCounter("group-id-index-counter");
Charles Chan188ebf52015-12-23 00:15:11 -0800166
Charles Chanfc5c7802016-05-17 13:13:55 -0700167 pendingAddNextObjectives = CacheBuilder.newBuilder()
Charles Chan188ebf52015-12-23 00:15:11 -0800168 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700169 .removalListener((RemovalNotification<GroupKey, List<OfdpaNextGroup>> notification) -> {
170 if (notification.getCause() == RemovalCause.EXPIRED &&
171 Objects.nonNull(notification.getValue())) {
172 notification.getValue()
173 .forEach(ofdpaNextGrp ->
174 fail(ofdpaNextGrp.nextObjective(),
175 ObjectiveError.GROUPINSTALLATIONFAILED));
Charles Chanfc5c7802016-05-17 13:13:55 -0700176 }
177 }).build();
Charles Chan188ebf52015-12-23 00:15:11 -0800178
Charles Chanfc5c7802016-05-17 13:13:55 -0700179 pendingRemoveNextObjectives = CacheBuilder.newBuilder()
180 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700181 .removalListener((RemovalNotification<NextObjective, List<GroupKey>> notification) -> {
Charles Chanfc5c7802016-05-17 13:13:55 -0700182 if (notification.getCause() == RemovalCause.EXPIRED) {
Yi Tsengef19de12017-04-24 11:33:05 -0700183 fail(notification.getKey(),
184 ObjectiveError.GROUPREMOVALFAILED);
Charles Chan188ebf52015-12-23 00:15:11 -0800185 }
186 }).build();
Saurav Das961beb22017-03-29 19:09:17 -0700187 pendingGroups = CacheBuilder.newBuilder()
188 .expireAfterWrite(20, TimeUnit.SECONDS)
Yi Tsengef19de12017-04-24 11:33:05 -0700189 .removalListener((RemovalNotification<GroupKey, Set<GroupChainElem>> notification) -> {
Saurav Das961beb22017-03-29 19:09:17 -0700190 if (notification.getCause() == RemovalCause.EXPIRED) {
191 log.error("Unable to install group with key {} and pending GCEs: {}",
192 notification.getKey(), notification.getValue());
193 }
194 }).build();
Yi Tseng78f51f42017-02-02 13:54:58 -0800195 pendingUpdateNextObjectives = new ConcurrentHashMap<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700196 GroupChecker groupChecker = new GroupChecker(this);
197 groupCheckerExecutor.scheduleAtFixedRate(groupChecker, 0, 500, TimeUnit.MILLISECONDS);
Charles Chan188ebf52015-12-23 00:15:11 -0800198 groupService.addListener(new InnerGroupListener());
199 }
200
Saurav Das8be4e3a2016-03-11 17:19:07 -0800201 //////////////////////////////////////
202 // Group Creation
203 //////////////////////////////////////
204
Yi Tsengef19de12017-04-24 11:33:05 -0700205 /**
206 * Adds a list of group chain by given NextObjective.
207 *
208 * @param nextObjective the NextObjective
209 */
Charles Chan188ebf52015-12-23 00:15:11 -0800210 protected void addGroup(NextObjective nextObjective) {
211 switch (nextObjective.type()) {
212 case SIMPLE:
213 Collection<TrafficTreatment> treatments = nextObjective.next();
214 if (treatments.size() != 1) {
215 log.error("Next Objectives of type Simple should only have a "
216 + "single Traffic Treatment. Next Objective Id:{}",
217 nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -0700218 fail(nextObjective, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800219 return;
220 }
221 processSimpleNextObjective(nextObjective);
222 break;
223 case BROADCAST:
224 processBroadcastNextObjective(nextObjective);
225 break;
226 case HASHED:
Pier Ventre140a8942016-11-02 07:26:38 -0700227 if (!verifyHashedNextObjective(nextObjective)) {
228 log.error("Next Objectives of type hashed not supported. Next Objective Id:{}",
229 nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -0700230 fail(nextObjective, ObjectiveError.BADPARAMS);
Pier Ventre140a8942016-11-02 07:26:38 -0700231 return;
232 }
Charles Chan188ebf52015-12-23 00:15:11 -0800233 processHashedNextObjective(nextObjective);
234 break;
235 case FAILOVER:
Yi Tsengef19de12017-04-24 11:33:05 -0700236 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -0800237 log.warn("Unsupported next objective type {}", nextObjective.type());
238 break;
239 default:
Yi Tsengef19de12017-04-24 11:33:05 -0700240 fail(nextObjective, ObjectiveError.UNKNOWN);
Charles Chan188ebf52015-12-23 00:15:11 -0800241 log.warn("Unknown next objective type {}", nextObjective.type());
242 }
243 }
244
245 /**
246 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
247 * a chain of groups. The simple Next Objective passed
248 * in by the application has to be broken up into a group chain
249 * comprising of an L3 Unicast Group that points to an L2 Interface
250 * Group which in-turn points to an output port. In some cases, the simple
251 * next Objective can just be an L2 interface without the need for chaining.
252 *
253 * @param nextObj the nextObjective of type SIMPLE
254 */
255 private void processSimpleNextObjective(NextObjective nextObj) {
256 TrafficTreatment treatment = nextObj.next().iterator().next();
257 // determine if plain L2 or L3->L2
258 boolean plainL2 = true;
259 for (Instruction ins : treatment.allInstructions()) {
260 if (ins.type() == Instruction.Type.L2MODIFICATION) {
261 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
262 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_DST ||
263 l2ins.subtype() == L2ModificationInstruction.L2SubType.ETH_SRC) {
264 plainL2 = false;
265 break;
266 }
267 }
268 }
269
270 if (plainL2) {
271 createL2InterfaceGroup(nextObj);
272 return;
273 }
274
Pier Ventre140a8942016-11-02 07:26:38 -0700275 boolean isMpls = false;
Pier Ventre42287df2016-11-09 14:17:26 -0800276 // In order to understand if it is a pseudo wire related
277 // next objective we look for the tunnel id in the meta.
278 boolean isPw = false;
Pier Ventre140a8942016-11-02 07:26:38 -0700279 if (nextObj.meta() != null) {
280 isMpls = isNotMplsBos(nextObj.meta());
Pier Ventre42287df2016-11-09 14:17:26 -0800281
282 TunnelIdCriterion tunnelIdCriterion = (TunnelIdCriterion) nextObj
283 .meta()
284 .getCriterion(TUNNEL_ID);
285 if (tunnelIdCriterion != null) {
286 isPw = true;
287 }
288
Pier Ventre140a8942016-11-02 07:26:38 -0700289 }
290
Pier Ventre42287df2016-11-09 14:17:26 -0800291 if (!isPw) {
292 // break up simple next objective to GroupChain objects
293 GroupInfo groupInfo = createL2L3Chain(treatment, nextObj.id(),
294 nextObj.appId(), isMpls,
295 nextObj.meta());
296 if (groupInfo == null) {
297 log.error("Could not process nextObj={} in dev:{}", nextObj.id(), deviceId);
298 return;
299 }
300 // create object for local and distributed storage
301 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700302 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
303 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Pier Ventre42287df2016-11-09 14:17:26 -0800304 OfdpaNextGroup ofdpaGrp =
305 new OfdpaNextGroup(Collections.singletonList(gkeyChain), nextObj);
306
307 // store l3groupkey with the ofdpaNextGroup for the nextObjective that depends on it
Yi Tsengef19de12017-04-24 11:33:05 -0700308 updatePendingNextObjective(groupInfo.nextGroupDesc().appCookie(), ofdpaGrp);
Pier Ventre42287df2016-11-09 14:17:26 -0800309
310 // now we are ready to send the l2 groupDescription (inner), as all the stores
311 // that will get async replies have been updated. By waiting to update
312 // the stores, we prevent nasty race conditions.
Yi Tsengef19de12017-04-24 11:33:05 -0700313 groupService.addGroup(groupInfo.innerMostGroupDesc());
Pier Ventre42287df2016-11-09 14:17:26 -0800314 } else {
315 // We handle the pseudo wire with a different a procedure.
316 // This procedure is meant to handle both initiation and
317 // termination of the pseudo wire.
318 processPwNextObjective(nextObj);
Charles Chan188ebf52015-12-23 00:15:11 -0800319 }
Charles Chan188ebf52015-12-23 00:15:11 -0800320 }
321
Charles Chan188ebf52015-12-23 00:15:11 -0800322 /**
323 * Creates a simple L2 Interface Group.
324 *
325 * @param nextObj the next Objective
326 */
327 private void createL2InterfaceGroup(NextObjective nextObj) {
Yi Tsengef19de12017-04-24 11:33:05 -0700328 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700329 if (assignedVlan == null) {
330 log.warn("VLAN ID required by simple next obj is missing. Abort.");
Yi Tsengef19de12017-04-24 11:33:05 -0700331 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800332 return;
333 }
334
Charles Chan5b9df8d2016-03-28 22:21:40 -0700335 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Charles Chan188ebf52015-12-23 00:15:11 -0800336
Charles Chan5b9df8d2016-03-28 22:21:40 -0700337 // There is only one L2 interface group in this case
Yi Tsengef19de12017-04-24 11:33:05 -0700338 GroupDescription l2InterfaceGroupDesc = groupInfos.get(0).innerMostGroupDesc();
Charles Chan188ebf52015-12-23 00:15:11 -0800339
Charles Chan5b9df8d2016-03-28 22:21:40 -0700340 // Put all dependency information into allGroupKeys
341 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
342 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
343 gkeyChain.addFirst(l2InterfaceGroupDesc.appCookie());
344 allGroupKeys.add(gkeyChain);
Charles Chan188ebf52015-12-23 00:15:11 -0800345
Charles Chan5b9df8d2016-03-28 22:21:40 -0700346 // Point the next objective to this group
347 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
348 updatePendingNextObjective(l2InterfaceGroupDesc.appCookie(), ofdpaGrp);
349
350 // Start installing the inner-most group
351 groupService.addGroup(l2InterfaceGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800352 }
353
354 /**
355 * Creates one of two possible group-chains from the treatment
356 * passed in. Depending on the MPLS boolean, this method either creates
Charles Chan425854b2016-04-11 15:32:12 -0700357 * an L3Unicast Group --&gt; L2Interface Group, if mpls is false;
358 * or MPLSInterface Group --&gt; L2Interface Group, if mpls is true;
Charles Chan188ebf52015-12-23 00:15:11 -0800359 * The returned 'inner' group description is always the L2 Interface group.
360 *
361 * @param treatment that needs to be broken up to create the group chain
362 * @param nextId of the next objective that needs this group chain
363 * @param appId of the application that sent this next objective
364 * @param mpls determines if L3Unicast or MPLSInterface group is created
365 * @param meta metadata passed in by the application as part of the nextObjective
366 * @return GroupInfo containing the GroupDescription of the
367 * L2Interface group(inner) and the GroupDescription of the (outer)
368 * L3Unicast/MPLSInterface group. May return null if there is an
369 * error in processing the chain
370 */
Charles Chan425854b2016-04-11 15:32:12 -0700371 protected GroupInfo createL2L3Chain(TrafficTreatment treatment, int nextId,
Charles Chanf9e98652016-09-07 16:54:23 -0700372 ApplicationId appId, boolean mpls,
373 TrafficSelector meta) {
374 return createL2L3ChainInternal(treatment, nextId, appId, mpls, meta, true);
375 }
376
377 /**
378 * Internal implementation of createL2L3Chain.
379 * <p>
380 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
381 * Since it is non-OF spec, we need an extension treatment for that.
382 * The useSetVlanExtension must be set to false for OFDPA i12.
383 * </p>
384 *
385 * @param treatment that needs to be broken up to create the group chain
386 * @param nextId of the next objective that needs this group chain
387 * @param appId of the application that sent this next objective
388 * @param mpls determines if L3Unicast or MPLSInterface group is created
389 * @param meta metadata passed in by the application as part of the nextObjective
390 * @param useSetVlanExtension use the setVlanVid extension that has is_present bit set to 0.
391 * @return GroupInfo containing the GroupDescription of the
392 * L2Interface group(inner) and the GroupDescription of the (outer)
393 * L3Unicast/MPLSInterface group. May return null if there is an
394 * error in processing the chain
395 */
396 protected GroupInfo createL2L3ChainInternal(TrafficTreatment treatment, int nextId,
Pier Ventre42287df2016-11-09 14:17:26 -0800397 ApplicationId appId, boolean mpls,
398 TrafficSelector meta, boolean useSetVlanExtension) {
Charles Chan188ebf52015-12-23 00:15:11 -0800399 // for the l2interface group, get vlan and port info
400 // for the outer group, get the src/dst mac, and vlan info
401 TrafficTreatment.Builder outerTtb = DefaultTrafficTreatment.builder();
402 TrafficTreatment.Builder innerTtb = DefaultTrafficTreatment.builder();
403 VlanId vlanid = null;
404 long portNum = 0;
405 boolean setVlan = false, popVlan = false;
Yi Tseng78f51f42017-02-02 13:54:58 -0800406 MacAddress srcMac;
407 MacAddress dstMac;
Charles Chan188ebf52015-12-23 00:15:11 -0800408 for (Instruction ins : treatment.allInstructions()) {
409 if (ins.type() == Instruction.Type.L2MODIFICATION) {
410 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
411 switch (l2ins.subtype()) {
412 case ETH_DST:
Charles Chan5270ed02016-01-30 23:22:37 -0800413 dstMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
414 outerTtb.setEthDst(dstMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800415 break;
416 case ETH_SRC:
Charles Chand0fd5dc2016-02-16 23:14:49 -0800417 srcMac = ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac();
418 outerTtb.setEthSrc(srcMac);
Charles Chan188ebf52015-12-23 00:15:11 -0800419 break;
420 case VLAN_ID:
421 vlanid = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
Charles Chanf9e98652016-09-07 16:54:23 -0700422 if (useSetVlanExtension) {
423 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
424 outerTtb.extension(ofdpaSetVlanVid, deviceId);
425 } else {
426 outerTtb.setVlanId(vlanid);
427 }
Charles Chan188ebf52015-12-23 00:15:11 -0800428 setVlan = true;
429 break;
430 case VLAN_POP:
431 innerTtb.popVlan();
432 popVlan = true;
433 break;
434 case DEC_MPLS_TTL:
435 case MPLS_LABEL:
436 case MPLS_POP:
437 case MPLS_PUSH:
438 case VLAN_PCP:
439 case VLAN_PUSH:
440 default:
441 break;
442 }
443 } else if (ins.type() == Instruction.Type.OUTPUT) {
444 portNum = ((Instructions.OutputInstruction) ins).port().toLong();
445 innerTtb.add(ins);
446 } else {
Saurav Das7bcbe702017-06-13 15:35:54 -0700447 log.debug("Driver does not handle this type of TrafficTreatment"
Charles Chan188ebf52015-12-23 00:15:11 -0800448 + " instruction in nextObjectives: {}", ins.type());
449 }
450 }
451
452 if (vlanid == null && meta != null) {
453 // use metadata if available
Pier Ventre140a8942016-11-02 07:26:38 -0700454 Criterion vidCriterion = meta.getCriterion(VLAN_VID);
Charles Chan188ebf52015-12-23 00:15:11 -0800455 if (vidCriterion != null) {
456 vlanid = ((VlanIdCriterion) vidCriterion).vlanId();
457 }
458 // if vlan is not set, use the vlan in metadata for outerTtb
459 if (vlanid != null && !setVlan) {
Charles Chanf9e98652016-09-07 16:54:23 -0700460 if (useSetVlanExtension) {
461 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(vlanid);
462 outerTtb.extension(ofdpaSetVlanVid, deviceId);
463 } else {
464 outerTtb.setVlanId(vlanid);
465 }
Charles Chan188ebf52015-12-23 00:15:11 -0800466 }
467 }
468
469 if (vlanid == null) {
470 log.error("Driver cannot process an L2/L3 group chain without "
471 + "egress vlan information for dev: {} port:{}",
472 deviceId, portNum);
473 return null;
474 }
475
476 if (!setVlan && !popVlan) {
477 // untagged outgoing port
478 TrafficTreatment.Builder temp = DefaultTrafficTreatment.builder();
479 temp.popVlan();
Yi Tsengef19de12017-04-24 11:33:05 -0700480 innerTtb.build().allInstructions().forEach(temp::add);
Charles Chan188ebf52015-12-23 00:15:11 -0800481 innerTtb = temp;
482 }
483
484 // assemble information for ofdpa l2interface group
Yi Tsengef19de12017-04-24 11:33:05 -0700485 int l2groupId = l2GroupId(vlanid, portNum);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800486 // a globally unique groupkey that is different for ports in the same device,
Charles Chan188ebf52015-12-23 00:15:11 -0800487 // but different for the same portnumber on different devices. Also different
488 // for the various group-types created out of the same next objective.
Charles Chane849c192016-01-11 18:28:54 -0800489 int l2gk = l2InterfaceGroupKey(deviceId, vlanid, portNum);
Yi Tsengef19de12017-04-24 11:33:05 -0700490 final GroupKey l2groupkey = new DefaultGroupKey(appKryo.serialize(l2gk));
Charles Chan188ebf52015-12-23 00:15:11 -0800491
492 // assemble information for outer group
Yi Tsengef19de12017-04-24 11:33:05 -0700493 GroupDescription outerGrpDesc;
Charles Chan188ebf52015-12-23 00:15:11 -0800494 if (mpls) {
Yi Tsengef19de12017-04-24 11:33:05 -0700495 // outer group is MPLS Interface
Saurav Das8be4e3a2016-03-11 17:19:07 -0800496 int mplsInterfaceIndex = getNextAvailableIndex();
Yi Tsengef19de12017-04-24 11:33:05 -0700497 int mplsGroupId = MPLS_INTERFACE_TYPE | (SUBTYPE_MASK & mplsInterfaceIndex);
498 final GroupKey mplsGroupKey = new DefaultGroupKey(
499 appKryo.serialize(mplsInterfaceIndex));
Yi Tsengfa394de2017-02-01 11:26:40 -0800500 outerTtb.group(new GroupId(l2groupId));
Charles Chan188ebf52015-12-23 00:15:11 -0800501 // create the mpls-interface group description to wait for the
502 // l2 interface group to be processed
503 GroupBucket mplsinterfaceGroupBucket =
504 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
505 outerGrpDesc = new DefaultGroupDescription(
506 deviceId,
507 GroupDescription.Type.INDIRECT,
508 new GroupBuckets(Collections.singletonList(
509 mplsinterfaceGroupBucket)),
Yi Tsengef19de12017-04-24 11:33:05 -0700510 mplsGroupKey,
511 mplsGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800512 appId);
513 log.debug("Trying MPLS-Interface: device:{} gid:{} gkey:{} nextid:{}",
Yi Tsengef19de12017-04-24 11:33:05 -0700514 deviceId, Integer.toHexString(mplsGroupId),
515 mplsGroupKey, nextId);
Charles Chan188ebf52015-12-23 00:15:11 -0800516 } else {
517 // outer group is L3Unicast
Saurav Das8be4e3a2016-03-11 17:19:07 -0800518 int l3unicastIndex = getNextAvailableIndex();
519 int l3groupId = L3_UNICAST_TYPE | (TYPE_MASK & l3unicastIndex);
520 final GroupKey l3groupkey = new DefaultGroupKey(
Yi Tsengef19de12017-04-24 11:33:05 -0700521 appKryo.serialize(l3unicastIndex));
Yi Tsengfa394de2017-02-01 11:26:40 -0800522 outerTtb.group(new GroupId(l2groupId));
Charles Chan188ebf52015-12-23 00:15:11 -0800523 // create the l3unicast group description to wait for the
524 // l2 interface group to be processed
525 GroupBucket l3unicastGroupBucket =
526 DefaultGroupBucket.createIndirectGroupBucket(outerTtb.build());
527 outerGrpDesc = new DefaultGroupDescription(
528 deviceId,
529 GroupDescription.Type.INDIRECT,
Yi Tsengef19de12017-04-24 11:33:05 -0700530 new GroupBuckets(Collections.singletonList(l3unicastGroupBucket)),
Charles Chan188ebf52015-12-23 00:15:11 -0800531 l3groupkey,
532 l3groupId,
533 appId);
534 log.debug("Trying L3Unicast: device:{} gid:{} gkey:{} nextid:{}",
535 deviceId, Integer.toHexString(l3groupId),
536 l3groupkey, nextId);
537 }
538
539 // store l2groupkey with the groupChainElem for the outer-group that depends on it
Yi Tsengef19de12017-04-24 11:33:05 -0700540 GroupChainElem gce = new GroupChainElem(outerGrpDesc, 1, false, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800541 updatePendingGroups(l2groupkey, gce);
542
Yi Tsengef19de12017-04-24 11:33:05 -0700543 // create group description for the inner l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700544 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800545 DefaultGroupBucket.createIndirectGroupBucket(innerTtb.build());
546 GroupDescription l2groupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -0700547 new DefaultGroupDescription(deviceId,
548 GroupDescription.Type.INDIRECT,
549 new GroupBuckets(Collections.singletonList(l2InterfaceGroupBucket)),
550 l2groupkey,
551 l2groupId,
552 appId);
Charles Chan188ebf52015-12-23 00:15:11 -0800553 log.debug("Trying L2Interface: device:{} gid:{} gkey:{} nextId:{}",
554 deviceId, Integer.toHexString(l2groupId),
555 l2groupkey, nextId);
556 return new GroupInfo(l2groupDescription, outerGrpDesc);
557
558 }
559
560 /**
561 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
562 * a chain of groups. The broadcast Next Objective passed in by the application
563 * has to be broken up into a group chain comprising of an
Saurav Das1a129a02016-11-18 15:21:57 -0800564 * L2 Flood group or L3 Multicast group, whose buckets point to L2 Interface groups.
Charles Chan188ebf52015-12-23 00:15:11 -0800565 *
566 * @param nextObj the nextObjective of type BROADCAST
567 */
568 private void processBroadcastNextObjective(NextObjective nextObj) {
Yi Tsengef19de12017-04-24 11:33:05 -0700569 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700570 if (assignedVlan == null) {
571 log.warn("VLAN ID required by broadcast next obj is missing. Abort.");
Yi Tsengef19de12017-04-24 11:33:05 -0700572 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan188ebf52015-12-23 00:15:11 -0800573 return;
574 }
Charles Chan188ebf52015-12-23 00:15:11 -0800575
Charles Chan5b9df8d2016-03-28 22:21:40 -0700576 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
577
Yi Tsengef19de12017-04-24 11:33:05 -0700578 IpPrefix ipDst = readIpDstFromSelector(nextObj.meta());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700579 if (ipDst != null) {
580 if (ipDst.isMulticast()) {
581 createL3MulticastGroup(nextObj, assignedVlan, groupInfos);
582 } else {
583 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -0700584 fail(nextObj, ObjectiveError.BADPARAMS);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700585 }
586 } else {
587 createL2FloodGroup(nextObj, assignedVlan, groupInfos);
588 }
589 }
590
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700591 private List<GroupInfo> prepareL2InterfaceGroup(NextObjective nextObj,
592 VlanId assignedVlan) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700593 ImmutableList.Builder<GroupInfo> groupInfoBuilder = ImmutableList.builder();
594
595 // break up broadcast next objective to multiple groups
596 Collection<TrafficTreatment> buckets = nextObj.next();
597
Charles Chan188ebf52015-12-23 00:15:11 -0800598 // each treatment is converted to an L2 interface group
Charles Chan188ebf52015-12-23 00:15:11 -0800599 for (TrafficTreatment treatment : buckets) {
600 TrafficTreatment.Builder newTreatment = DefaultTrafficTreatment.builder();
601 PortNumber portNum = null;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700602 VlanId egressVlan = null;
Charles Chan188ebf52015-12-23 00:15:11 -0800603 // ensure that the only allowed treatments are pop-vlan and output
604 for (Instruction ins : treatment.allInstructions()) {
605 if (ins.type() == Instruction.Type.L2MODIFICATION) {
606 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
607 switch (l2ins.subtype()) {
608 case VLAN_POP:
609 newTreatment.add(l2ins);
610 break;
Charles Chan5b9df8d2016-03-28 22:21:40 -0700611 case VLAN_ID:
612 egressVlan = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
613 break;
Charles Chan188ebf52015-12-23 00:15:11 -0800614 default:
615 log.debug("action {} not permitted for broadcast nextObj",
616 l2ins.subtype());
617 break;
618 }
619 } else if (ins.type() == Instruction.Type.OUTPUT) {
620 portNum = ((Instructions.OutputInstruction) ins).port();
621 newTreatment.add(ins);
622 } else {
Charles Chane849c192016-01-11 18:28:54 -0800623 log.debug("TrafficTreatment of type {} not permitted in " +
624 " broadcast nextObjective", ins.type());
Charles Chan188ebf52015-12-23 00:15:11 -0800625 }
626 }
627
Yi Tsengef19de12017-04-24 11:33:05 -0700628 if (portNum == null) {
629 log.warn("Can't find output port for the bucket {}.", treatment);
630 continue;
631 }
632
Charles Chan188ebf52015-12-23 00:15:11 -0800633 // assemble info for l2 interface group
Charles Chan5b9df8d2016-03-28 22:21:40 -0700634 VlanId l2InterfaceGroupVlan =
635 (egressVlan != null && !assignedVlan.equals(egressVlan)) ?
636 egressVlan : assignedVlan;
637 int l2gk = l2InterfaceGroupKey(deviceId, l2InterfaceGroupVlan, portNum.toLong());
638 final GroupKey l2InterfaceGroupKey =
Yi Tsengef19de12017-04-24 11:33:05 -0700639 new DefaultGroupKey(appKryo.serialize(l2gk));
Charles Chan372b63e2017-02-07 12:10:53 -0800640 int l2InterfaceGroupId = L2_INTERFACE_TYPE |
641 ((l2InterfaceGroupVlan.toShort() & THREE_BIT_MASK) << PORT_LEN) |
642 ((int) portNum.toLong() & FOUR_BIT_MASK);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700643 GroupBucket l2InterfaceGroupBucket =
Charles Chan188ebf52015-12-23 00:15:11 -0800644 DefaultGroupBucket.createIndirectGroupBucket(newTreatment.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700645 GroupDescription l2InterfaceGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -0700646 new DefaultGroupDescription(deviceId,
647 GroupDescription.Type.INDIRECT,
648 new GroupBuckets(Collections.singletonList(
649 l2InterfaceGroupBucket)),
650 l2InterfaceGroupKey,
651 l2InterfaceGroupId,
652 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800653 log.debug("Trying L2-Interface: device:{} gid:{} gkey:{} nextid:{}",
Charles Chan5b9df8d2016-03-28 22:21:40 -0700654 deviceId, Integer.toHexString(l2InterfaceGroupId),
655 l2InterfaceGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -0800656
Charles Chan5b9df8d2016-03-28 22:21:40 -0700657 groupInfoBuilder.add(new GroupInfo(l2InterfaceGroupDescription,
658 l2InterfaceGroupDescription));
Charles Chan188ebf52015-12-23 00:15:11 -0800659 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700660 return groupInfoBuilder.build();
661 }
Charles Chan188ebf52015-12-23 00:15:11 -0800662
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700663 private void createL2FloodGroup(NextObjective nextObj, VlanId vlanId,
664 List<GroupInfo> groupInfos) {
665 // assemble info for l2 flood group. Since there can be only one flood
666 // group for a vlan, its index is always the same - 0
Yi Tsengef19de12017-04-24 11:33:05 -0700667 Integer l2FloodGroupId = L2_FLOOD_TYPE | (vlanId.toShort() << 16);
668 final GroupKey l2FloodGroupKey = l2FloodGroupKey(vlanId, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700669
Charles Chan188ebf52015-12-23 00:15:11 -0800670 // collection of group buckets pointing to all the l2 interface groups
Yi Tsengef19de12017-04-24 11:33:05 -0700671 List<GroupBucket> l2floodBuckets = generateNextGroupBuckets(groupInfos, ALL);
Charles Chan188ebf52015-12-23 00:15:11 -0800672 // create the l2flood group-description to wait for all the
673 // l2interface groups to be processed
674 GroupDescription l2floodGroupDescription =
675 new DefaultGroupDescription(
676 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800677 ALL,
Charles Chan188ebf52015-12-23 00:15:11 -0800678 new GroupBuckets(l2floodBuckets),
Yi Tsengef19de12017-04-24 11:33:05 -0700679 l2FloodGroupKey,
680 l2FloodGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800681 nextObj.appId());
Charles Chan188ebf52015-12-23 00:15:11 -0800682 log.debug("Trying L2-Flood: device:{} gid:{} gkey:{} nextid:{}",
Yi Tsengef19de12017-04-24 11:33:05 -0700683 deviceId, Integer.toHexString(l2FloodGroupId),
684 l2FloodGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -0800685
Charles Chan5b9df8d2016-03-28 22:21:40 -0700686 // Put all dependency information into allGroupKeys
687 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
688 groupInfos.forEach(groupInfo -> {
Yi Tsengef19de12017-04-24 11:33:05 -0700689 Deque<GroupKey> groupKeyChain = new ArrayDeque<>();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700690 // In this case we should have L2 interface group only
Yi Tsengef19de12017-04-24 11:33:05 -0700691 groupKeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
692 groupKeyChain.addFirst(l2FloodGroupKey);
693 allGroupKeys.add(groupKeyChain);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700694 });
Charles Chan188ebf52015-12-23 00:15:11 -0800695
Charles Chan5b9df8d2016-03-28 22:21:40 -0700696 // Point the next objective to this group
697 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -0700698 updatePendingNextObjective(l2FloodGroupKey, ofdpaGrp);
Charles Chan188ebf52015-12-23 00:15:11 -0800699
Charles Chan5b9df8d2016-03-28 22:21:40 -0700700 GroupChainElem gce = new GroupChainElem(l2floodGroupDescription,
Yi Tsengef19de12017-04-24 11:33:05 -0700701 groupInfos.size(), false, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700702 groupInfos.forEach(groupInfo -> {
703 // Point this group to the next group
Yi Tsengef19de12017-04-24 11:33:05 -0700704 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), gce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700705 // Start installing the inner-most group
Yi Tsengef19de12017-04-24 11:33:05 -0700706 groupService.addGroup(groupInfo.innerMostGroupDesc());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700707 });
708 }
709
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700710 private void createL3MulticastGroup(NextObjective nextObj, VlanId vlanId,
711 List<GroupInfo> groupInfos) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700712 List<GroupBucket> l3McastBuckets = new ArrayList<>();
713 groupInfos.forEach(groupInfo -> {
714 // Points to L3 interface group if there is one.
715 // Otherwise points to L2 interface group directly.
Yi Tsengef19de12017-04-24 11:33:05 -0700716 GroupDescription nextGroupDesc = (groupInfo.nextGroupDesc() != null) ?
717 groupInfo.nextGroupDesc() : groupInfo.innerMostGroupDesc();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700718 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Yi Tsengfa394de2017-02-01 11:26:40 -0800719 ttb.group(new GroupId(nextGroupDesc.givenGroupId()));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700720 GroupBucket abucket = DefaultGroupBucket.createAllGroupBucket(ttb.build());
721 l3McastBuckets.add(abucket);
722 });
723
724 int l3MulticastIndex = getNextAvailableIndex();
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700725 int l3MulticastGroupId = L3_MULTICAST_TYPE |
726 vlanId.toShort() << 16 | (TYPE_VLAN_MASK & l3MulticastIndex);
727 final GroupKey l3MulticastGroupKey =
Yi Tsengef19de12017-04-24 11:33:05 -0700728 new DefaultGroupKey(appKryo.serialize(l3MulticastIndex));
Charles Chan5b9df8d2016-03-28 22:21:40 -0700729
730 GroupDescription l3MulticastGroupDesc = new DefaultGroupDescription(deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800731 ALL,
Charles Chan5b9df8d2016-03-28 22:21:40 -0700732 new GroupBuckets(l3McastBuckets),
733 l3MulticastGroupKey,
734 l3MulticastGroupId,
735 nextObj.appId());
736
737 // Put all dependency information into allGroupKeys
738 List<Deque<GroupKey>> allGroupKeys = Lists.newArrayList();
739 groupInfos.forEach(groupInfo -> {
740 Deque<GroupKey> gkeyChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -0700741 gkeyChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700742 // Add L3 interface group to the chain if there is one.
Yi Tsengef19de12017-04-24 11:33:05 -0700743 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
744 gkeyChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700745 }
746 gkeyChain.addFirst(l3MulticastGroupKey);
747 allGroupKeys.add(gkeyChain);
748 });
749
750 // Point the next objective to this group
751 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
752 updatePendingNextObjective(l3MulticastGroupKey, ofdpaGrp);
753
754 GroupChainElem outerGce = new GroupChainElem(l3MulticastGroupDesc,
Yi Tsengef19de12017-04-24 11:33:05 -0700755 groupInfos.size(), false, deviceId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700756 groupInfos.forEach(groupInfo -> {
757 // Point this group (L3 multicast) to the next group
Yi Tsengef19de12017-04-24 11:33:05 -0700758 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), outerGce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700759
760 // Point next group to inner-most group, if any
Yi Tsengef19de12017-04-24 11:33:05 -0700761 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
762 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(),
763 1, false, deviceId);
764 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700765 }
766
767 // Start installing the inner-most group
Yi Tsengef19de12017-04-24 11:33:05 -0700768 groupService.addGroup(groupInfo.innerMostGroupDesc());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700769 });
Charles Chan188ebf52015-12-23 00:15:11 -0800770 }
771
Charles Chan188ebf52015-12-23 00:15:11 -0800772 /**
773 * As per the OFDPA 2.0 TTP, packets are sent out of ports by using
774 * a chain of groups. The hashed Next Objective passed in by the application
775 * has to be broken up into a group chain comprising of an
776 * L3 ECMP group as the top level group. Buckets of this group can point
777 * to a variety of groups in a group chain, depending on the whether
778 * MPLS labels are being pushed or not.
779 * <p>
780 * NOTE: We do not create MPLS ECMP groups as they are unimplemented in
781 * OF-DPA 2.0 (even though it is in the spec). Therefore we do not
782 * check the nextObjective meta to see what is matching before being
783 * sent to this nextObjective.
784 *
785 * @param nextObj the nextObjective of type HASHED
786 */
Pier Ventre140a8942016-11-02 07:26:38 -0700787 protected void processHashedNextObjective(NextObjective nextObj) {
Charles Chan188ebf52015-12-23 00:15:11 -0800788 // storage for all group keys in the chain of groups created
789 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
790 List<GroupInfo> unsentGroups = new ArrayList<>();
791 createHashBucketChains(nextObj, allGroupKeys, unsentGroups);
792
793 // now we can create the outermost L3 ECMP group
794 List<GroupBucket> l3ecmpGroupBuckets = new ArrayList<>();
795 for (GroupInfo gi : unsentGroups) {
796 // create ECMP bucket to point to the outer group
797 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
Yi Tsengef19de12017-04-24 11:33:05 -0700798 ttb.group(new GroupId(gi.nextGroupDesc().givenGroupId()));
Charles Chan188ebf52015-12-23 00:15:11 -0800799 GroupBucket sbucket = DefaultGroupBucket
800 .createSelectGroupBucket(ttb.build());
801 l3ecmpGroupBuckets.add(sbucket);
802 }
Saurav Das8be4e3a2016-03-11 17:19:07 -0800803 int l3ecmpIndex = getNextAvailableIndex();
804 int l3ecmpGroupId = L3_ECMP_TYPE | (TYPE_MASK & l3ecmpIndex);
805 GroupKey l3ecmpGroupKey = new DefaultGroupKey(
Yi Tsengef19de12017-04-24 11:33:05 -0700806 appKryo.serialize(l3ecmpIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800807 GroupDescription l3ecmpGroupDesc =
808 new DefaultGroupDescription(
809 deviceId,
Yi Tseng78f51f42017-02-02 13:54:58 -0800810 SELECT,
Charles Chan188ebf52015-12-23 00:15:11 -0800811 new GroupBuckets(l3ecmpGroupBuckets),
812 l3ecmpGroupKey,
813 l3ecmpGroupId,
814 nextObj.appId());
815 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
816 l3ecmpGroupBuckets.size(),
Yi Tsengef19de12017-04-24 11:33:05 -0700817 false, deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -0800818
819 // create objects for local and distributed storage
Yi Tsengef19de12017-04-24 11:33:05 -0700820 allGroupKeys.forEach(gKeyChain -> gKeyChain.addFirst(l3ecmpGroupKey));
Charles Chan188ebf52015-12-23 00:15:11 -0800821 OfdpaNextGroup ofdpaGrp = new OfdpaNextGroup(allGroupKeys, nextObj);
822
823 // store l3ecmpGroupKey with the ofdpaGroupChain for the nextObjective
824 // that depends on it
825 updatePendingNextObjective(l3ecmpGroupKey, ofdpaGrp);
826
827 log.debug("Trying L3ECMP: device:{} gid:{} gkey:{} nextId:{}",
828 deviceId, Integer.toHexString(l3ecmpGroupId),
829 l3ecmpGroupKey, nextObj.id());
830 // finally we are ready to send the innermost groups
831 for (GroupInfo gi : unsentGroups) {
832 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -0700833 Integer.toHexString(gi.innerMostGroupDesc().givenGroupId()), deviceId);
834 updatePendingGroups(gi.nextGroupDesc().appCookie(), l3ecmpGce);
835 groupService.addGroup(gi.innerMostGroupDesc());
Charles Chan188ebf52015-12-23 00:15:11 -0800836 }
Charles Chan188ebf52015-12-23 00:15:11 -0800837 }
838
839 /**
840 * Creates group chains for all buckets in a hashed group, and stores the
841 * GroupInfos and GroupKeys for all the groups in the lists passed in, which
842 * should be empty.
843 * <p>
844 * Does not create the top level ECMP group. Does not actually send the
845 * groups to the groupService.
846 *
847 * @param nextObj the Next Objective with buckets that need to be converted
848 * to group chains
849 * @param allGroupKeys a list to store groupKey for each bucket-group-chain
850 * @param unsentGroups a list to store GroupInfo for each bucket-group-chain
851 */
Pier Ventre140a8942016-11-02 07:26:38 -0700852 protected void createHashBucketChains(NextObjective nextObj,
Yi Tsengef19de12017-04-24 11:33:05 -0700853 List<Deque<GroupKey>> allGroupKeys,
854 List<GroupInfo> unsentGroups) {
Charles Chan188ebf52015-12-23 00:15:11 -0800855 // break up hashed next objective to multiple groups
856 Collection<TrafficTreatment> buckets = nextObj.next();
857
858 for (TrafficTreatment bucket : buckets) {
859 //figure out how many labels are pushed in each bucket
860 int labelsPushed = 0;
861 MplsLabel innermostLabel = null;
862 for (Instruction ins : bucket.allInstructions()) {
863 if (ins.type() == Instruction.Type.L2MODIFICATION) {
864 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
865 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_PUSH) {
866 labelsPushed++;
867 }
868 if (l2ins.subtype() == L2ModificationInstruction.L2SubType.MPLS_LABEL) {
869 if (innermostLabel == null) {
Yi Tsengef19de12017-04-24 11:33:05 -0700870 innermostLabel =
871 ((L2ModificationInstruction.ModMplsLabelInstruction) l2ins).label();
Charles Chan188ebf52015-12-23 00:15:11 -0800872 }
873 }
874 }
875 }
876
Yi Tsengef19de12017-04-24 11:33:05 -0700877 Deque<GroupKey> gKeyChain = new ArrayDeque<>();
Charles Chan188ebf52015-12-23 00:15:11 -0800878 // XXX we only deal with 0 and 1 label push right now
879 if (labelsPushed == 0) {
Yi Tsengef19de12017-04-24 11:33:05 -0700880 GroupInfo noLabelGroupInfo;
Pier Ventre140a8942016-11-02 07:26:38 -0700881 TrafficSelector metaSelector = nextObj.meta();
882 if (metaSelector != null) {
883 if (isNotMplsBos(metaSelector)) {
Yi Tsengef19de12017-04-24 11:33:05 -0700884 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
885 nextObj.appId(), true,
886 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700887 } else {
Yi Tsengef19de12017-04-24 11:33:05 -0700888 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
889 nextObj.appId(), false,
890 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700891 }
892 } else {
Yi Tsengef19de12017-04-24 11:33:05 -0700893 noLabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
894 nextObj.appId(), false,
895 nextObj.meta());
Pier Ventre140a8942016-11-02 07:26:38 -0700896 }
Yi Tsengef19de12017-04-24 11:33:05 -0700897 if (noLabelGroupInfo == null) {
Charles Chan188ebf52015-12-23 00:15:11 -0800898 log.error("Could not process nextObj={} in dev:{}",
899 nextObj.id(), deviceId);
900 return;
901 }
Yi Tsengef19de12017-04-24 11:33:05 -0700902 gKeyChain.addFirst(noLabelGroupInfo.innerMostGroupDesc().appCookie());
903 gKeyChain.addFirst(noLabelGroupInfo.nextGroupDesc().appCookie());
Charles Chan188ebf52015-12-23 00:15:11 -0800904
905 // we can't send the inner group description yet, as we have to
906 // create the dependent ECMP group first. So we store..
Yi Tsengef19de12017-04-24 11:33:05 -0700907 unsentGroups.add(noLabelGroupInfo);
Charles Chan188ebf52015-12-23 00:15:11 -0800908
909 } else if (labelsPushed == 1) {
910 GroupInfo onelabelGroupInfo = createL2L3Chain(bucket, nextObj.id(),
911 nextObj.appId(), true,
912 nextObj.meta());
913 if (onelabelGroupInfo == null) {
914 log.error("Could not process nextObj={} in dev:{}",
915 nextObj.id(), deviceId);
916 return;
917 }
918 // we need to add another group to this chain - the L3VPN group
919 TrafficTreatment.Builder l3vpnTtb = DefaultTrafficTreatment.builder();
Charles Chan0f43e472017-02-14 14:00:16 -0800920 if (requireVlanPopBeforeMplsPush()) {
921 l3vpnTtb.popVlan();
922 }
Charles Chan188ebf52015-12-23 00:15:11 -0800923 l3vpnTtb.pushMpls()
924 .setMpls(innermostLabel)
Yi Tsengef19de12017-04-24 11:33:05 -0700925 .group(new GroupId(onelabelGroupInfo.nextGroupDesc().givenGroupId()));
Charles Chan40132b32017-01-22 00:19:37 -0800926 if (supportCopyTtl()) {
927 l3vpnTtb.copyTtlOut();
928 }
929 if (supportSetMplsBos()) {
930 l3vpnTtb.setMplsBos(true);
931 }
Charles Chan0f43e472017-02-14 14:00:16 -0800932 if (requireVlanPopBeforeMplsPush()) {
933 l3vpnTtb.pushVlan().setVlanId(VlanId.vlanId(VlanId.RESERVED));
934 }
Charles Chan40132b32017-01-22 00:19:37 -0800935
Charles Chan188ebf52015-12-23 00:15:11 -0800936 GroupBucket l3vpnGrpBkt =
937 DefaultGroupBucket.createIndirectGroupBucket(l3vpnTtb.build());
Saurav Das8be4e3a2016-03-11 17:19:07 -0800938 int l3vpnIndex = getNextAvailableIndex();
Yi Tsengef19de12017-04-24 11:33:05 -0700939 int l3vpnGroupId = MPLS_L3VPN_SUBTYPE | (SUBTYPE_MASK & l3vpnIndex);
940 GroupKey l3vpnGroupKey = new DefaultGroupKey(
941 appKryo.serialize(l3vpnIndex));
Charles Chan188ebf52015-12-23 00:15:11 -0800942 GroupDescription l3vpnGroupDesc =
943 new DefaultGroupDescription(
944 deviceId,
945 GroupDescription.Type.INDIRECT,
Yi Tsengef19de12017-04-24 11:33:05 -0700946 new GroupBuckets(Collections.singletonList(l3vpnGrpBkt)),
947 l3vpnGroupKey,
948 l3vpnGroupId,
Charles Chan188ebf52015-12-23 00:15:11 -0800949 nextObj.appId());
Yi Tsengef19de12017-04-24 11:33:05 -0700950 GroupChainElem l3vpnGce = new GroupChainElem(l3vpnGroupDesc,
951 1,
952 false,
953 deviceId);
954 updatePendingGroups(onelabelGroupInfo.nextGroupDesc().appCookie(), l3vpnGce);
Charles Chan188ebf52015-12-23 00:15:11 -0800955
Yi Tsengef19de12017-04-24 11:33:05 -0700956 gKeyChain.addFirst(onelabelGroupInfo.innerMostGroupDesc().appCookie());
957 gKeyChain.addFirst(onelabelGroupInfo.nextGroupDesc().appCookie());
958 gKeyChain.addFirst(l3vpnGroupKey);
Charles Chan188ebf52015-12-23 00:15:11 -0800959
960 //now we can replace the outerGrpDesc with the one we just created
Yi Tsengef19de12017-04-24 11:33:05 -0700961 onelabelGroupInfo.nextGroupDesc(l3vpnGroupDesc);
Charles Chan188ebf52015-12-23 00:15:11 -0800962
963 // we can't send the innermost group yet, as we have to create
964 // the dependent ECMP group first. So we store ...
965 unsentGroups.add(onelabelGroupInfo);
966
Yi Tsengef19de12017-04-24 11:33:05 -0700967 log.debug("Trying L3VPN: device:{} gid:{} group key:{} nextId:{}",
968 deviceId, Integer.toHexString(l3vpnGroupId),
969 l3vpnGroupKey, nextObj.id());
Charles Chan188ebf52015-12-23 00:15:11 -0800970
971 } else {
972 log.warn("Driver currently does not handle more than 1 MPLS "
973 + "labels. Not processing nextObjective {}", nextObj.id());
974 return;
975 }
976
977 // all groups in this chain
Yi Tsengef19de12017-04-24 11:33:05 -0700978 allGroupKeys.add(gKeyChain);
Charles Chan188ebf52015-12-23 00:15:11 -0800979 }
980 }
981
Pier Ventre42287df2016-11-09 14:17:26 -0800982 /**
983 * Processes the pseudo wire related next objective.
984 * This procedure try to reuse the mpls label groups,
985 * the mpls interface group and the l2 interface group.
986 *
987 * @param nextObjective the objective to process.
988 */
989 protected void processPwNextObjective(NextObjective nextObjective) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700990 log.warn("Pseudo wire extensions are not supported in OFDPA 2.0 {}",
991 nextObjective.id());
Pier Ventre42287df2016-11-09 14:17:26 -0800992 }
993
Saurav Das8be4e3a2016-03-11 17:19:07 -0800994 //////////////////////////////////////
995 // Group Editing
996 //////////////////////////////////////
Charles Chan188ebf52015-12-23 00:15:11 -0800997 /**
998 * Adds a bucket to the top level group of a group-chain, and creates the chain.
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700999 * Ensures that bucket being added is not a duplicate, by checking existing
Yi Tsengef19de12017-04-24 11:33:05 -07001000 * buckets for the same output port.
Charles Chan188ebf52015-12-23 00:15:11 -08001001 *
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001002 * @param nextObjective the bucket information for a next group
Charles Chan188ebf52015-12-23 00:15:11 -08001003 * @param next the representation of the existing group-chain for this next objective
1004 */
1005 protected void addBucketToGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001006 if (nextObjective.type() != NextObjective.Type.HASHED &&
1007 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001008 log.warn("AddBuckets not applied to nextType:{} in dev:{} for next:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001009 nextObjective.type(), deviceId, nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -07001010 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001011 return;
1012 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001013
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001014 // first check to see if bucket being added is not a duplicate of an
Yi Tsengef19de12017-04-24 11:33:05 -07001015 // existing bucket. If it is for an existing output port, then its a
1016 // duplicate.
Yi Tseng78f51f42017-02-02 13:54:58 -08001017 Set<TrafficTreatment> duplicateBuckets = Sets.newHashSet();
Yi Tsengef19de12017-04-24 11:33:05 -07001018 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
1019 Set<PortNumber> existingPorts = getExistingOutputPorts(allActiveKeys,
1020 groupService,
1021 deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -08001022 Set<TrafficTreatment> nonDuplicateBuckets = Sets.newHashSet();
1023 NextObjective objectiveToAdd;
Yi Tseng78f51f42017-02-02 13:54:58 -08001024
1025 nextObjective.next().forEach(trafficTreatment -> {
1026 PortNumber portNumber = readOutPortFromTreatment(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001027 if (portNumber == null) {
1028 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001029 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001030 if (existingPorts.contains(portNumber)) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001031 // its possible that portnumbers are same but labels are different
1032 int label = readLabelFromTreatment(trafficTreatment);
1033 if (label == -1) {
1034 duplicateBuckets.add(trafficTreatment);
1035 } else {
Saurav Dasceccf242017-08-03 18:30:35 -07001036 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1037 groupService, deviceId,
1038 portNumber, label);
1039 if (!existing.isEmpty()) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001040 duplicateBuckets.add(trafficTreatment);
1041 } else {
1042 nonDuplicateBuckets.add(trafficTreatment);
1043 }
1044 }
Yi Tseng47f82dc2017-03-05 22:48:39 -08001045 } else {
1046 nonDuplicateBuckets.add(trafficTreatment);
Yi Tseng78f51f42017-02-02 13:54:58 -08001047 }
1048 });
1049
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001050 if (duplicateBuckets.isEmpty()) {
1051 // use the original objective
1052 objectiveToAdd = nextObjective;
1053 } else if (!nonDuplicateBuckets.isEmpty()) {
1054 // only use the non-duplicate buckets if there are any
1055 log.debug("Some buckets {} already exist in next id {}, duplicate "
1056 + "buckets will be ignored.", duplicateBuckets, nextObjective.id());
1057 // new next objective with non duplicate treatments
Yi Tseng47f82dc2017-03-05 22:48:39 -08001058 NextObjective.Builder builder = DefaultNextObjective.builder()
1059 .withType(nextObjective.type())
1060 .withId(nextObjective.id())
1061 .withMeta(nextObjective.meta())
1062 .fromApp(nextObjective.appId());
1063 nonDuplicateBuckets.forEach(builder::addTreatment);
1064
1065 ObjectiveContext context = nextObjective.context().orElse(null);
1066 objectiveToAdd = builder.addToExisting(context);
1067 } else {
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001068 // buckets to add are already there - nothing to do
Saurav Das1547b3f2017-05-05 17:01:08 -07001069 log.debug("buckets already exist {} in next: {} ..ignoring bucket add",
1070 duplicateBuckets, nextObjective.id());
1071 pass(nextObjective);
Saurav Dasb28d5dd2017-03-24 19:03:58 -07001072 return;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001073 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001074
Saurav Das1a129a02016-11-18 15:21:57 -08001075 if (nextObjective.type() == NextObjective.Type.HASHED) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001076 addBucketToHashGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001077 } else if (nextObjective.type() == NextObjective.Type.BROADCAST) {
Yi Tseng47f82dc2017-03-05 22:48:39 -08001078 addBucketToBroadcastGroup(objectiveToAdd, allActiveKeys);
Saurav Das1a129a02016-11-18 15:21:57 -08001079 }
1080 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001081
Saurav Das1a129a02016-11-18 15:21:57 -08001082 private void addBucketToHashGroup(NextObjective nextObjective,
Yi Tseng78f51f42017-02-02 13:54:58 -08001083 List<Deque<GroupKey>> allActiveKeys) {
Charles Chan188ebf52015-12-23 00:15:11 -08001084 // storage for all group keys in the chain of groups created
1085 List<Deque<GroupKey>> allGroupKeys = new ArrayList<>();
1086 List<GroupInfo> unsentGroups = new ArrayList<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001087 List<GroupBucket> newBuckets;
Charles Chan188ebf52015-12-23 00:15:11 -08001088 createHashBucketChains(nextObjective, allGroupKeys, unsentGroups);
1089
Yi Tseng78f51f42017-02-02 13:54:58 -08001090 // now we can create the buckets to add to the outermost L3 ECMP group
1091 newBuckets = generateNextGroupBuckets(unsentGroups, SELECT);
Charles Chan188ebf52015-12-23 00:15:11 -08001092
Saurav Das1a129a02016-11-18 15:21:57 -08001093 // retrieve the original L3 ECMP group
1094 Group l3ecmpGroup = retrieveTopLevelGroup(allActiveKeys, nextObjective.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001095 if (l3ecmpGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001096 fail(nextObjective, ObjectiveError.GROUPMISSING);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001097 return;
1098 }
Saurav Das1a129a02016-11-18 15:21:57 -08001099 GroupKey l3ecmpGroupKey = l3ecmpGroup.appCookie();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001100 int l3ecmpGroupId = l3ecmpGroup.id().id();
Charles Chan188ebf52015-12-23 00:15:11 -08001101
1102 // Although GroupDescriptions are not necessary for adding buckets to
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001103 // existing groups, we still use one in the GroupChainElem. When the latter is
Charles Chan188ebf52015-12-23 00:15:11 -08001104 // processed, the info will be extracted for the bucketAdd call to groupService
1105 GroupDescription l3ecmpGroupDesc =
Yi Tsengef19de12017-04-24 11:33:05 -07001106 new DefaultGroupDescription(deviceId,
1107 SELECT,
1108 new GroupBuckets(newBuckets),
1109 l3ecmpGroupKey,
1110 l3ecmpGroupId,
1111 nextObjective.appId());
Yi Tseng78f51f42017-02-02 13:54:58 -08001112 GroupChainElem l3ecmpGce = new GroupChainElem(l3ecmpGroupDesc,
1113 unsentGroups.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001114 true,
1115 deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001116
Saurav Dasc88d4662017-05-15 15:34:25 -07001117 // update new bucket-chains
1118 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
1119 for (Deque<GroupKey> newBucketChain : allGroupKeys) {
1120 newBucketChain.addFirst(l3ecmpGroupKey);
1121 addedKeys.add(newBucketChain);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001122 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001123 updatePendingNextObjective(l3ecmpGroupKey,
1124 new OfdpaNextGroup(addedKeys, nextObjective));
Yi Tsengef19de12017-04-24 11:33:05 -07001125 log.debug("Adding to L3ECMP: device:{} gid:{} group key:{} nextId:{}",
Charles Chan188ebf52015-12-23 00:15:11 -08001126 deviceId, Integer.toHexString(l3ecmpGroupId),
1127 l3ecmpGroupKey, nextObjective.id());
Yi Tseng78f51f42017-02-02 13:54:58 -08001128
1129 unsentGroups.forEach(groupInfo -> {
1130 // send the innermost group
1131 log.debug("Sending innermost group {} in group chain on device {} ",
Saurav Dasc88d4662017-05-15 15:34:25 -07001132 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
1133 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001134 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3ecmpGce);
1135 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001136 });
Saurav Das1a129a02016-11-18 15:21:57 -08001137 }
Charles Chan188ebf52015-12-23 00:15:11 -08001138
Saurav Das1a129a02016-11-18 15:21:57 -08001139 private void addBucketToBroadcastGroup(NextObjective nextObj,
Yi Tsengef19de12017-04-24 11:33:05 -07001140 List<Deque<GroupKey>> allActiveKeys) {
1141 VlanId assignedVlan = readVlanFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001142 if (assignedVlan == null) {
1143 log.warn("VLAN ID required by broadcast next obj is missing. "
1144 + "Aborting add bucket to broadcast group for next:{} in dev:{}",
1145 nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001146 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001147 return;
1148 }
Saurav Das1a129a02016-11-18 15:21:57 -08001149 List<GroupInfo> groupInfos = prepareL2InterfaceGroup(nextObj, assignedVlan);
Yi Tsengef19de12017-04-24 11:33:05 -07001150 IpPrefix ipDst = readIpDstFromSelector(nextObj.meta());
Saurav Das1a129a02016-11-18 15:21:57 -08001151 if (ipDst != null) {
1152 if (ipDst.isMulticast()) {
Yi Tsengef19de12017-04-24 11:33:05 -07001153 addBucketToL3MulticastGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001154 } else {
1155 log.warn("Broadcast NextObj with non-multicast IP address {}", nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001156 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001157 }
1158 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001159 addBucketToL2FloodGroup(nextObj, allActiveKeys, groupInfos, assignedVlan);
Saurav Das1a129a02016-11-18 15:21:57 -08001160 }
1161 }
1162
1163 private void addBucketToL2FloodGroup(NextObjective nextObj,
1164 List<Deque<GroupKey>> allActiveKeys,
1165 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001166 VlanId assignedVlan) {
1167 Group l2FloodGroup = retrieveTopLevelGroup(allActiveKeys, nextObj.id());
1168
1169 if (l2FloodGroup == null) {
1170 log.warn("Can't find L2 flood group while adding bucket to it. NextObj = {}",
1171 nextObj);
Yi Tsengef19de12017-04-24 11:33:05 -07001172 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001173 return;
1174 }
Saurav Das1a129a02016-11-18 15:21:57 -08001175
Yi Tseng78f51f42017-02-02 13:54:58 -08001176 GroupKey l2floodGroupKey = l2FloodGroup.appCookie();
1177 int l2floodGroupId = l2FloodGroup.id().id();
1178 List<GroupBucket> newBuckets = generateNextGroupBuckets(groupInfos, ALL);
1179
1180 GroupDescription l2FloodGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001181 new DefaultGroupDescription(deviceId,
1182 ALL,
1183 new GroupBuckets(newBuckets),
1184 l2floodGroupKey,
1185 l2floodGroupId,
1186 nextObj.appId());
Saurav Das1a129a02016-11-18 15:21:57 -08001187
Yi Tseng78f51f42017-02-02 13:54:58 -08001188 GroupChainElem l2FloodGroupChainElement =
1189 new GroupChainElem(l2FloodGroupDescription,
1190 groupInfos.size(),
Yi Tsengef19de12017-04-24 11:33:05 -07001191 true,
1192 deviceId);
Yi Tseng78f51f42017-02-02 13:54:58 -08001193
Yi Tseng78f51f42017-02-02 13:54:58 -08001194
1195 //ensure assignedVlan applies to the chosen group
1196 VlanId floodGroupVlan = extractVlanIdFromGroupId(l2floodGroupId);
1197
1198 if (!floodGroupVlan.equals(assignedVlan)) {
1199 log.warn("VLAN ID {} does not match Flood group {} to which bucket is "
1200 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1201 Integer.toHexString(l2floodGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001202 fail(nextObj, ObjectiveError.BADPARAMS);
Yi Tseng78f51f42017-02-02 13:54:58 -08001203 return;
1204 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001205 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
Yi Tseng78f51f42017-02-02 13:54:58 -08001206 groupInfos.forEach(groupInfo -> {
1207 // update original NextGroup with new bucket-chain
Yi Tseng78f51f42017-02-02 13:54:58 -08001208 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001209 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001210 newBucketChain.addFirst(l2floodGroupKey);
Saurav Dasc88d4662017-05-15 15:34:25 -07001211 addedKeys.add(newBucketChain);
Yi Tseng78f51f42017-02-02 13:54:58 -08001212
Yi Tsengef19de12017-04-24 11:33:05 -07001213 log.debug("Adding to L2FLOOD: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001214 deviceId, Integer.toHexString(l2floodGroupId),
1215 l2floodGroupKey, nextObj.id());
1216 // send the innermost group
1217 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001218 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001219 deviceId);
1220
Yi Tsengef19de12017-04-24 11:33:05 -07001221 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l2FloodGroupChainElement);
Yi Tseng78f51f42017-02-02 13:54:58 -08001222
Yi Tsengef19de12017-04-24 11:33:05 -07001223 DeviceId innerMostGroupDevice = groupInfo.innerMostGroupDesc().deviceId();
1224 GroupKey innerMostGroupKey = groupInfo.innerMostGroupDesc().appCookie();
Yi Tseng78f51f42017-02-02 13:54:58 -08001225 Group existsL2IGroup = groupService.getGroup(innerMostGroupDevice, innerMostGroupKey);
1226
1227 if (existsL2IGroup != null) {
1228 // group already exist
1229 processPendingAddGroupsOrNextObjs(innerMostGroupKey, true);
1230 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001231 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001232 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001233 });
Saurav Dasc88d4662017-05-15 15:34:25 -07001234
1235 updatePendingNextObjective(l2floodGroupKey,
1236 new OfdpaNextGroup(addedKeys, nextObj));
Yi Tseng78f51f42017-02-02 13:54:58 -08001237 }
1238
Saurav Das1a129a02016-11-18 15:21:57 -08001239 private void addBucketToL3MulticastGroup(NextObjective nextObj,
1240 List<Deque<GroupKey>> allActiveKeys,
1241 List<GroupInfo> groupInfos,
Yi Tseng78f51f42017-02-02 13:54:58 -08001242 VlanId assignedVlan) {
1243 // create the buckets to add to the outermost L3 Multicast group
1244 List<GroupBucket> newBuckets = Lists.newArrayList();
1245 groupInfos.forEach(groupInfo -> {
1246 // Points to L3 interface group if there is one.
1247 // Otherwise points to L2 interface group directly.
Yi Tsengef19de12017-04-24 11:33:05 -07001248 GroupDescription nextGroupDesc = (groupInfo.nextGroupDesc() != null) ?
1249 groupInfo.nextGroupDesc() : groupInfo.innerMostGroupDesc();
1250 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1251 treatmentBuilder.group(new GroupId(nextGroupDesc.givenGroupId()));
1252 GroupBucket newBucket = DefaultGroupBucket.createAllGroupBucket(treatmentBuilder.build());
Yi Tseng78f51f42017-02-02 13:54:58 -08001253 newBuckets.add(newBucket);
1254 });
Saurav Das1a129a02016-11-18 15:21:57 -08001255
1256 // get the group being edited
1257 Group l3mcastGroup = retrieveTopLevelGroup(allActiveKeys, nextObj.id());
1258 if (l3mcastGroup == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001259 fail(nextObj, ObjectiveError.GROUPMISSING);
Saurav Das1a129a02016-11-18 15:21:57 -08001260 return;
1261 }
1262 GroupKey l3mcastGroupKey = l3mcastGroup.appCookie();
1263 int l3mcastGroupId = l3mcastGroup.id().id();
1264
1265 //ensure assignedVlan applies to the chosen group
Yi Tseng78f51f42017-02-02 13:54:58 -08001266 VlanId expectedVlan = extractVlanIdFromGroupId(l3mcastGroupId);
Saurav Das1a129a02016-11-18 15:21:57 -08001267 if (!expectedVlan.equals(assignedVlan)) {
1268 log.warn("VLAN ID {} does not match L3 Mcast group {} to which bucket is "
1269 + "being added, for next:{} in dev:{}. Abort.", assignedVlan,
1270 Integer.toHexString(l3mcastGroupId), nextObj.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001271 fail(nextObj, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001272 }
1273 GroupDescription l3mcastGroupDescription =
Yi Tsengef19de12017-04-24 11:33:05 -07001274 new DefaultGroupDescription(deviceId,
1275 ALL,
1276 new GroupBuckets(newBuckets),
1277 l3mcastGroupKey,
1278 l3mcastGroupId,
1279 nextObj.appId());
Saurav Das1a129a02016-11-18 15:21:57 -08001280 GroupChainElem l3mcastGce = new GroupChainElem(l3mcastGroupDescription,
Yi Tsengef19de12017-04-24 11:33:05 -07001281 groupInfos.size(),
1282 true,
1283 deviceId);
Saurav Dasc88d4662017-05-15 15:34:25 -07001284
1285 List<Deque<GroupKey>> addedKeys = new ArrayList<>();
Yi Tseng78f51f42017-02-02 13:54:58 -08001286 groupInfos.forEach(groupInfo -> {
1287 // update original NextGroup with new bucket-chain
1288 Deque<GroupKey> newBucketChain = new ArrayDeque<>();
Yi Tsengef19de12017-04-24 11:33:05 -07001289 newBucketChain.addFirst(groupInfo.innerMostGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001290 // Add L3 interface group to the chain if there is one.
Yi Tsengef19de12017-04-24 11:33:05 -07001291 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1292 newBucketChain.addFirst(groupInfo.nextGroupDesc().appCookie());
Yi Tseng78f51f42017-02-02 13:54:58 -08001293 }
1294 newBucketChain.addFirst(l3mcastGroupKey);
Saurav Dasc88d4662017-05-15 15:34:25 -07001295 addedKeys.add(newBucketChain);
Yi Tseng78f51f42017-02-02 13:54:58 -08001296
Yi Tsengef19de12017-04-24 11:33:05 -07001297 updatePendingGroups(groupInfo.nextGroupDesc().appCookie(), l3mcastGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001298 // Point next group to inner-most group, if any
Yi Tsengef19de12017-04-24 11:33:05 -07001299 if (!groupInfo.nextGroupDesc().equals(groupInfo.innerMostGroupDesc())) {
1300 GroupChainElem innerGce = new GroupChainElem(groupInfo.nextGroupDesc(),
1301 1,
1302 false,
1303 deviceId);
1304 updatePendingGroups(groupInfo.innerMostGroupDesc().appCookie(), innerGce);
Yi Tseng78f51f42017-02-02 13:54:58 -08001305 }
Yi Tsengef19de12017-04-24 11:33:05 -07001306 log.debug("Adding to L3MCAST: device:{} gid:{} group key:{} nextId:{}",
Yi Tseng78f51f42017-02-02 13:54:58 -08001307 deviceId, Integer.toHexString(l3mcastGroupId),
1308 l3mcastGroupKey, nextObj.id());
1309 // send the innermost group
1310 log.debug("Sending innermost group {} in group chain on device {} ",
Yi Tsengef19de12017-04-24 11:33:05 -07001311 Integer.toHexString(groupInfo.innerMostGroupDesc().givenGroupId()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001312 deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001313 groupService.addGroup(groupInfo.innerMostGroupDesc());
Yi Tseng78f51f42017-02-02 13:54:58 -08001314
1315 });
1316
Saurav Das1a129a02016-11-18 15:21:57 -08001317 updatePendingNextObjective(l3mcastGroupKey,
Saurav Dasc88d4662017-05-15 15:34:25 -07001318 new OfdpaNextGroup(addedKeys, nextObj));
Charles Chan188ebf52015-12-23 00:15:11 -08001319 }
1320
1321 /**
Saurav Dasceccf242017-08-03 18:30:35 -07001322 * Removes buckets in the top level group of a possible group-chain. Does
Saurav Das1a129a02016-11-18 15:21:57 -08001323 * not remove the groups in the group-chain pointed to by this bucket, as they
Charles Chan188ebf52015-12-23 00:15:11 -08001324 * may be in use (referenced by other groups) elsewhere.
1325 *
Saurav Dasceccf242017-08-03 18:30:35 -07001326 * @param nextObjective a next objective that contains information for the
1327 * buckets to be removed from the group
1328 * @param next the representation of the existing group-chains for this next
1329 * objective, from which the top-level buckets to remove are determined
Charles Chan188ebf52015-12-23 00:15:11 -08001330 */
1331 protected void removeBucketFromGroup(NextObjective nextObjective, NextGroup next) {
Saurav Das1a129a02016-11-18 15:21:57 -08001332 if (nextObjective.type() != NextObjective.Type.HASHED &&
1333 nextObjective.type() != NextObjective.Type.BROADCAST) {
Charles Chan188ebf52015-12-23 00:15:11 -08001334 log.warn("RemoveBuckets not applied to nextType:{} in dev:{} for next:{}",
1335 nextObjective.type(), deviceId, nextObjective.id());
Yi Tsengef19de12017-04-24 11:33:05 -07001336 fail(nextObjective, ObjectiveError.UNSUPPORTED);
Charles Chan188ebf52015-12-23 00:15:11 -08001337 return;
1338 }
Yi Tsengef19de12017-04-24 11:33:05 -07001339 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Saurav Dasceccf242017-08-03 18:30:35 -07001340 List<Integer> indicesToRemove = Lists.newArrayList();
1341 for (TrafficTreatment treatment : nextObjective.next()) {
1342 // find the top-level bucket in the group-chain by matching the
1343 // outport and label from different groups in the chain
1344 PortNumber portToRemove = readOutPortFromTreatment(treatment);
1345 int labelToRemove = readLabelFromTreatment(treatment);
1346 if (portToRemove == null) {
1347 log.warn("treatment {} of next objective {} has no outport.. "
1348 + "cannot remove bucket from group in dev: {}", treatment,
1349 nextObjective.id(), deviceId);
Charles Chan188ebf52015-12-23 00:15:11 -08001350 continue;
1351 }
Saurav Dasceccf242017-08-03 18:30:35 -07001352 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1353 groupService, deviceId,
1354 portToRemove, labelToRemove);
1355 indicesToRemove.addAll(existing);
1356
Charles Chan188ebf52015-12-23 00:15:11 -08001357 }
Saurav Dasceccf242017-08-03 18:30:35 -07001358
1359 List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList();
1360 indicesToRemove.forEach(index -> chainsToRemove
1361 .add(allActiveKeys.get(index)));
Yi Tseng78f51f42017-02-02 13:54:58 -08001362 if (chainsToRemove.isEmpty()) {
Charles Chan188ebf52015-12-23 00:15:11 -08001363 log.warn("Could not find appropriate group-chain for removing bucket"
1364 + " for next id {} in dev:{}", nextObjective.id(), deviceId);
Yi Tsengef19de12017-04-24 11:33:05 -07001365 fail(nextObjective, ObjectiveError.BADPARAMS);
Saurav Das1a129a02016-11-18 15:21:57 -08001366 return;
Charles Chan188ebf52015-12-23 00:15:11 -08001367 }
Saurav Dasceccf242017-08-03 18:30:35 -07001368 removeBucket(chainsToRemove, nextObjective);
1369 }
1370
1371 /**
1372 * Removes top-level buckets from a group that represents the given next objective.
1373 *
1374 * @param chainsToRemove a list of group bucket chains to remove
1375 * @param nextObjective the next objective that contains information for the
1376 * buckets to be removed from the group
1377 */
1378 protected void removeBucket(List<Deque<GroupKey>> chainsToRemove,
1379 NextObjective nextObjective) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001380 List<GroupBucket> bucketsToRemove = Lists.newArrayList();
1381 //first group key is the one we want to modify
1382 GroupKey modGroupKey = chainsToRemove.get(0).peekFirst();
Saurav Das1a129a02016-11-18 15:21:57 -08001383 Group modGroup = groupService.getGroup(deviceId, modGroupKey);
Yi Tseng78f51f42017-02-02 13:54:58 -08001384 for (Deque<GroupKey> foundChain : chainsToRemove) {
1385 //second group key is the one we wish to remove the reference to
1386 if (foundChain.size() < 2) {
Saurav Dasceccf242017-08-03 18:30:35 -07001387 // additional check to make sure second group key exists in
Yi Tseng78f51f42017-02-02 13:54:58 -08001388 // the chain.
1389 log.warn("Can't find second group key from chain {}",
1390 foundChain);
1391 continue;
Saurav Das1a129a02016-11-18 15:21:57 -08001392 }
Saurav Dasceccf242017-08-03 18:30:35 -07001393 GroupKey pointedGroupKey = foundChain.stream()
1394 .collect(Collectors.toList()).get(1);
Yi Tseng78f51f42017-02-02 13:54:58 -08001395 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
Yi Tseng78f51f42017-02-02 13:54:58 -08001396 if (pointedGroup == null) {
1397 continue;
1398 }
1399
1400 GroupBucket bucket;
1401 if (nextObjective.type() == NextObjective.Type.HASHED) {
1402 bucket = DefaultGroupBucket.createSelectGroupBucket(
1403 DefaultTrafficTreatment.builder()
1404 .group(pointedGroup.id())
1405 .build());
1406 } else {
1407 bucket = DefaultGroupBucket.createAllGroupBucket(
1408 DefaultTrafficTreatment.builder()
1409 .group(pointedGroup.id())
1410 .build());
1411 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001412 bucketsToRemove.add(bucket);
Saurav Das1a129a02016-11-18 15:21:57 -08001413 }
Yi Tseng78f51f42017-02-02 13:54:58 -08001414
1415 GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove);
1416 List<String> pointedGroupIds; // for debug log
1417 pointedGroupIds = bucketsToRemove.stream()
1418 .map(GroupBucket::treatment)
1419 .map(TrafficTreatment::allInstructions)
1420 .flatMap(List::stream)
1421 .filter(inst -> inst instanceof Instructions.GroupInstruction)
1422 .map(inst -> (Instructions.GroupInstruction) inst)
1423 .map(Instructions.GroupInstruction::groupId)
1424 .map(GroupId::id)
1425 .map(Integer::toHexString)
1426 .map(id -> HEX_PREFIX + id)
1427 .collect(Collectors.toList());
1428
Yi Tseng78f51f42017-02-02 13:54:58 -08001429 log.debug("Removing buckets from group id 0x{} pointing to group id(s) {} "
Saurav Das1a129a02016-11-18 15:21:57 -08001430 + "for next id {} in device {}", Integer.toHexString(modGroup.id().id()),
Yi Tseng78f51f42017-02-02 13:54:58 -08001431 pointedGroupIds, nextObjective.id(), deviceId);
1432 addPendingUpdateNextObjective(modGroupKey, nextObjective);
Saurav Das1a129a02016-11-18 15:21:57 -08001433 groupService.removeBucketsFromGroup(deviceId, modGroupKey,
1434 removeBuckets, modGroupKey,
1435 nextObjective.appId());
Saurav Dasceccf242017-08-03 18:30:35 -07001436 // update store - synchronize access as there may be multiple threads
1437 // trying to remove buckets from the same group, each with its own
1438 // potentially stale copy of allActiveKeys
Saurav Dasc88d4662017-05-15 15:34:25 -07001439 synchronized (flowObjectiveStore) {
Saurav Dasceccf242017-08-03 18:30:35 -07001440 // get a fresh copy of what the store holds
1441 NextGroup next = flowObjectiveStore.getNextGroup(nextObjective.id());
1442 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Pier Luigi2b442bd2018-01-15 09:48:49 +01001443 allActiveKeys = Lists.newArrayList(allActiveKeys);
Saurav Dasc88d4662017-05-15 15:34:25 -07001444 // Note that since we got a new object, and ArrayDeque does not implement
Saurav Dasceccf242017-08-03 18:30:35 -07001445 // Object.equals(), we have to check the deque elems one by one
1446 allActiveKeys
1447 .removeIf(active ->
1448 chainsToRemove.stream().anyMatch(remove ->
1449 Arrays.equals(remove.toArray(new GroupKey[0]),
1450 active.toArray(new GroupKey[0]))));
Saurav Dasc88d4662017-05-15 15:34:25 -07001451 // If no buckets in the group, then retain an entry for the
1452 // top level group which still exists.
1453 if (allActiveKeys.isEmpty()) {
1454 ArrayDeque<GroupKey> top = new ArrayDeque<>();
1455 top.add(modGroupKey);
1456 allActiveKeys.add(top);
1457 }
1458 flowObjectiveStore.putNextGroup(nextObjective.id(),
Saurav Dasceccf242017-08-03 18:30:35 -07001459 new OfdpaNextGroup(allActiveKeys,
1460 nextObjective));
Saurav Das1a129a02016-11-18 15:21:57 -08001461 }
Charles Chan188ebf52015-12-23 00:15:11 -08001462 }
1463
1464 /**
Saurav Das961beb22017-03-29 19:09:17 -07001465 * Removes all groups in multiple possible group-chains that represent the next-obj.
Charles Chan188ebf52015-12-23 00:15:11 -08001466 *
1467 * @param nextObjective the next objective to remove
1468 * @param next the NextGroup that represents the existing group-chain for
1469 * this next objective
1470 */
1471 protected void removeGroup(NextObjective nextObjective, NextGroup next) {
Yi Tsengef19de12017-04-24 11:33:05 -07001472 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Charles Chanfc5c7802016-05-17 13:13:55 -07001473
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001474 List<GroupKey> groupKeys = allActiveKeys.stream()
Charles Chanfc5c7802016-05-17 13:13:55 -07001475 .map(Deque::getFirst).collect(Collectors.toList());
Yi Tsengef19de12017-04-24 11:33:05 -07001476 addPendingRemoveNextObjective(nextObjective, groupKeys);
Charles Chanfc5c7802016-05-17 13:13:55 -07001477
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001478 allActiveKeys.forEach(groupChain -> groupChain.forEach(groupKey ->
Charles Chan188ebf52015-12-23 00:15:11 -08001479 groupService.removeGroup(deviceId, groupKey, nextObjective.appId())));
1480 flowObjectiveStore.removeNextGroup(nextObjective.id());
1481 }
1482
Saurav Dasceccf242017-08-03 18:30:35 -07001483 /**
1484 * Checks existing buckets in {@link NextGroup} to verify if they match
1485 * the buckets in the given {@link NextObjective}. Adds or removes buckets
1486 * to ensure that the buckets match up.
1487 *
1488 * @param nextObjective the next objective to verify
1489 * @param next the representation of the existing group which has to be
1490 * modified to match the given next objective
1491 */
1492 protected void verifyGroup(NextObjective nextObjective, NextGroup next) {
1493 if (nextObjective.type() != NextObjective.Type.HASHED) {
1494 log.warn("verification not supported for {} group", nextObjective.type());
1495 fail(nextObjective, ObjectiveError.UNSUPPORTED);
1496 return;
1497 }
1498 log.debug("Call to verify device:{} nextId:{}", deviceId, nextObjective.id());
1499 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
1500 List<TrafficTreatment> bucketsToCreate = Lists.newArrayList();
1501 List<Integer> indicesToRemove = Lists.newArrayList();
Saurav Dasceccf242017-08-03 18:30:35 -07001502 for (TrafficTreatment bkt : nextObjective.next()) {
1503 PortNumber portNumber = readOutPortFromTreatment(bkt);
1504 int label = readLabelFromTreatment(bkt);
1505 if (portNumber == null) {
1506 log.warn("treatment {} of next objective {} has no outport.. "
1507 + "cannot remove bucket from group in dev: {}", bkt,
1508 nextObjective.id(), deviceId);
1509 fail(nextObjective, ObjectiveError.BADPARAMS);
1510 return;
1511 }
1512 List<Integer> existing = existingPortAndLabel(allActiveKeys,
1513 groupService, deviceId,
1514 portNumber, label);
1515 if (existing.isEmpty()) {
1516 // if it doesn't exist, mark this bucket for creation
1517 bucketsToCreate.add(bkt);
1518 }
1519 if (existing.size() > 1) {
1520 // if it exists but there are duplicates, mark the others for removal
1521 existing.remove(0);
1522 indicesToRemove.addAll(existing);
1523 }
1524 }
1525
1526 if (!bucketsToCreate.isEmpty()) {
1527 log.info("creating {} buckets as part of nextId: {} verification",
1528 bucketsToCreate.size(), nextObjective.id());
1529 //create a nextObjective only with these buckets
1530 NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder()
1531 .withId(nextObjective.id())
1532 .withType(NextObjective.Type.HASHED)
1533 .withMeta(nextObjective.meta())
1534 .fromApp(nextObjective.appId());
1535 bucketsToCreate.forEach(bucket -> nextObjBuilder.addTreatment(bucket));
1536 addBucketToHashGroup(nextObjBuilder.addToExisting(), allActiveKeys);
1537 }
1538
1539 if (!indicesToRemove.isEmpty()) {
1540 log.info("removing {} buckets as part of nextId: {} verification",
1541 indicesToRemove.size(), nextObjective.id());
1542 List<Deque<GroupKey>> chainsToRemove = Lists.newArrayList();
1543 indicesToRemove.forEach(index -> chainsToRemove
1544 .add(allActiveKeys.get(index)));
1545 removeBucket(chainsToRemove, nextObjective);
1546 }
1547
Saurav Das041bb782017-08-14 16:44:43 -07001548 if (bucketsToCreate.isEmpty() && indicesToRemove.isEmpty()) {
1549 // flowObjective store record is in-sync with nextObjective passed-in
1550 // Nevertheless groupStore may not be in sync due to bug in the store
1551 // - see CORD-1844. XXX When this bug is fixed, the rest of this verify
1552 // method will not be required.
1553 GroupKey hashGroupKey = allActiveKeys.get(0).peekFirst();
1554 Group hashGroup = groupService.getGroup(deviceId, hashGroupKey);
1555 int actualGroupSize = hashGroup.buckets().buckets().size();
1556 int objGroupSize = nextObjective.next().size();
1557 if (actualGroupSize != objGroupSize) {
1558 log.warn("Mismatch detected in device:{}, nextId:{}, nextObjective-size"
1559 + ":{} group-size:{} .. correcting", deviceId, nextObjective.id(),
1560 objGroupSize, actualGroupSize);
1561 }
1562 if (actualGroupSize > objGroupSize) {
1563 List<GroupBucket> bucketsToRemove = Lists.newArrayList();
1564 //check every bucket in the actual group
1565 for (GroupBucket bucket : hashGroup.buckets().buckets()) {
1566 GroupInstruction g = (GroupInstruction) bucket.treatment()
1567 .allInstructions().iterator().next();
1568 GroupId gidToCheck = g.groupId(); // the group pointed to
1569 boolean matches = false;
1570 for (Deque<GroupKey> validChain : allActiveKeys) {
1571 if (validChain.size() < 2) {
1572 continue;
1573 }
1574 GroupKey pointedGroupKey = validChain.stream()
1575 .collect(Collectors.toList()).get(1);
1576 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
1577 if (pointedGroup != null && gidToCheck.equals(pointedGroup.id())) {
1578 matches = true;
1579 break;
1580 }
1581 }
1582 if (!matches) {
1583 log.warn("Removing bucket pointing to groupId:{}", gidToCheck);
1584 bucketsToRemove.add(bucket);
1585 }
1586 }
1587 // remove buckets for which there was no record in the obj store
1588 if (bucketsToRemove.isEmpty()) {
1589 log.warn("Mismatch detected but could not determine which"
1590 + "buckets to remove");
1591 } else {
1592 GroupBuckets removeBuckets = new GroupBuckets(bucketsToRemove);
1593 groupService.removeBucketsFromGroup(deviceId, hashGroupKey,
1594 removeBuckets, hashGroupKey,
1595 nextObjective.appId());
1596 }
1597 } else if (actualGroupSize < objGroupSize) {
1598 // should also add buckets not in group-store but in obj-store
1599 List<GroupBucket> bucketsToAdd = Lists.newArrayList();
1600 //check every bucket in the obj
1601 for (Deque<GroupKey> validChain : allActiveKeys) {
1602 if (validChain.size() < 2) {
1603 continue;
1604 }
1605 GroupKey pointedGroupKey = validChain.stream()
1606 .collect(Collectors.toList()).get(1);
1607 Group pointedGroup = groupService.getGroup(deviceId, pointedGroupKey);
1608 if (pointedGroup == null) {
1609 // group should exist, otherwise cannot be added as bucket
1610 continue;
1611 }
1612 boolean matches = false;
1613 for (GroupBucket bucket : hashGroup.buckets().buckets()) {
1614 GroupInstruction g = (GroupInstruction) bucket.treatment()
1615 .allInstructions().iterator().next();
1616 GroupId gidToCheck = g.groupId(); // the group pointed to
1617 if (pointedGroup.id().equals(gidToCheck)) {
1618 matches = true;
1619 break;
1620 }
1621 }
1622 if (!matches) {
1623 log.warn("Adding bucket pointing to groupId:{}", pointedGroup);
1624 TrafficTreatment t = DefaultTrafficTreatment.builder()
1625 .group(pointedGroup.id())
1626 .build();
1627 bucketsToAdd.add(DefaultGroupBucket.createSelectGroupBucket(t));
1628 }
1629 }
1630 if (bucketsToAdd.isEmpty()) {
1631 log.warn("Mismatch detected but could not determine which "
1632 + "buckets to add");
1633 } else {
1634 GroupBuckets addBuckets = new GroupBuckets(bucketsToAdd);
1635 groupService.addBucketsToGroup(deviceId, hashGroupKey,
1636 addBuckets, hashGroupKey,
1637 nextObjective.appId());
1638 }
1639 }
1640 }
1641
Saurav Dasceccf242017-08-03 18:30:35 -07001642 pass(nextObjective);
1643 }
1644
1645 //////////////////////////////////////
1646 // Helper methods and classes
1647 //////////////////////////////////////
1648
Yi Tsengef19de12017-04-24 11:33:05 -07001649 protected void updatePendingNextObjective(GroupKey groupKey, OfdpaNextGroup nextGrp) {
1650 pendingAddNextObjectives.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07001651 if (val == null) {
Yi Tsengef19de12017-04-24 11:33:05 -07001652 val = new CopyOnWriteArrayList<>();
Saurav Das961beb22017-03-29 19:09:17 -07001653 }
Yi Tsengef19de12017-04-24 11:33:05 -07001654 val.add(nextGrp);
Saurav Das961beb22017-03-29 19:09:17 -07001655 return val;
1656 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08001657 }
1658
Yi Tsengef19de12017-04-24 11:33:05 -07001659 protected void updatePendingGroups(GroupKey groupKey, GroupChainElem gce) {
1660 pendingGroups.asMap().compute(groupKey, (k, val) -> {
Saurav Das961beb22017-03-29 19:09:17 -07001661 if (val == null) {
1662 val = Sets.newConcurrentHashSet();
1663 }
1664 val.add(gce);
1665 return val;
1666 });
Saurav Das8be4e3a2016-03-11 17:19:07 -08001667 }
1668
Yi Tsengef19de12017-04-24 11:33:05 -07001669 protected void addPendingUpdateNextObjective(GroupKey groupKey,
1670 NextObjective nextObjective) {
Yi Tseng78f51f42017-02-02 13:54:58 -08001671 pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> {
1672 if (nextObjs != null) {
1673 nextObjs.add(nextObjective);
1674 } else {
1675 nextObjs = Sets.newHashSet(nextObjective);
1676 }
1677 return nextObjs;
1678 });
1679 }
1680
Yi Tsengef19de12017-04-24 11:33:05 -07001681 private void processPendingUpdateNextObjs(GroupKey groupKey) {
1682 pendingUpdateNextObjectives.compute(groupKey, (gKey, nextObjs) -> {
1683 if (nextObjs != null) {
1684
1685 nextObjs.forEach(nextObj -> {
1686 log.debug("Group {} updated, update pending next objective {}.",
1687 groupKey, nextObj);
1688
1689 pass(nextObj);
1690 });
1691 }
1692 return Sets.newHashSet();
1693 });
1694 }
1695
1696 private void processPendingRemoveNextObjs(GroupKey key) {
1697 pendingRemoveNextObjectives.asMap().forEach((nextObjective, groupKeys) -> {
1698 if (groupKeys.isEmpty()) {
1699 pendingRemoveNextObjectives.invalidate(nextObjective);
1700 pass(nextObjective);
1701 } else {
1702 groupKeys.remove(key);
1703 }
1704 });
1705 }
1706
1707 protected int getNextAvailableIndex() {
1708 return (int) nextIndex.incrementAndGet();
1709 }
1710
1711 protected Group retrieveTopLevelGroup(List<Deque<GroupKey>> allActiveKeys,
1712 int nextid) {
1713 GroupKey topLevelGroupKey;
1714 if (!allActiveKeys.isEmpty()) {
1715 topLevelGroupKey = allActiveKeys.get(0).peekFirst();
1716 } else {
1717 log.warn("Could not determine top level group while processing"
1718 + "next:{} in dev:{}", nextid, deviceId);
1719 return null;
1720 }
1721 Group topGroup = groupService.getGroup(deviceId, topLevelGroupKey);
1722 if (topGroup == null) {
1723 log.warn("Could not find top level group while processing "
1724 + "next:{} in dev:{}", nextid, deviceId);
1725 }
1726 return topGroup;
1727 }
1728
1729 protected void processPendingAddGroupsOrNextObjs(GroupKey key, boolean added) {
1730 //first check for group chain
1731 Set<OfdpaGroupHandlerUtility.GroupChainElem> gceSet = pendingGroups.asMap().remove(key);
1732 if (gceSet != null) {
1733 for (GroupChainElem gce : gceSet) {
1734 log.debug("Group service {} group key {} in device {}. "
1735 + "Processing next group in group chain with group id 0x{}",
1736 (added) ? "ADDED" : "processed",
1737 key, deviceId,
1738 Integer.toHexString(gce.groupDescription().givenGroupId()));
1739 processGroupChain(gce);
1740 }
1741 } else {
1742 // otherwise chain complete - check for waiting nextObjectives
1743 List<OfdpaGroupHandlerUtility.OfdpaNextGroup> nextGrpList =
1744 pendingAddNextObjectives.getIfPresent(key);
1745 if (nextGrpList != null) {
1746 pendingAddNextObjectives.invalidate(key);
1747 nextGrpList.forEach(nextGrp -> {
1748 log.debug("Group service {} group key {} in device:{}. "
1749 + "Done implementing next objective: {} <<-->> gid:0x{}",
1750 (added) ? "ADDED" : "processed",
1751 key, deviceId, nextGrp.nextObjective().id(),
1752 Integer.toHexString(groupService.getGroup(deviceId, key)
1753 .givenGroupId()));
1754 pass(nextGrp.nextObjective());
Saurav Dasc88d4662017-05-15 15:34:25 -07001755 updateFlowObjectiveStore(nextGrp.nextObjective().id(), nextGrp);
Yi Tsengef19de12017-04-24 11:33:05 -07001756
1757 // check if addBuckets waiting for this completion
1758 pendingBuckets.compute(nextGrp.nextObjective().id(), (nextId, pendBkts) -> {
1759 if (pendBkts != null) {
1760 pendBkts.forEach(pendBkt -> addBucketToGroup(pendBkt, nextGrp));
1761 }
1762 return null;
1763 });
1764 });
1765 }
1766 }
1767 }
1768
Charles Chan188ebf52015-12-23 00:15:11 -08001769 /**
1770 * Processes next element of a group chain. Assumption is that if this
1771 * group points to another group, the latter has already been created
1772 * and this driver has received notification for it. A second assumption is
1773 * that if there is another group waiting for this group then the appropriate
1774 * stores already have the information to act upon the notification for the
1775 * creation of this group.
1776 * <p>
1777 * The processing of the GroupChainElement depends on the number of groups
1778 * this element is waiting on. For all group types other than SIMPLE, a
1779 * GroupChainElement could be waiting on multiple groups.
1780 *
1781 * @param gce the group chain element to be processed next
1782 */
1783 private void processGroupChain(GroupChainElem gce) {
1784 int waitOnGroups = gce.decrementAndGetGroupsWaitedOn();
1785 if (waitOnGroups != 0) {
1786 log.debug("GCE: {} not ready to be processed", gce);
1787 return;
1788 }
1789 log.debug("GCE: {} ready to be processed", gce);
Yi Tsengef19de12017-04-24 11:33:05 -07001790 if (gce.addBucketToGroup()) {
1791 groupService.addBucketsToGroup(gce.groupDescription().deviceId(),
1792 gce.groupDescription().appCookie(),
1793 gce.groupDescription().buckets(),
1794 gce.groupDescription().appCookie(),
1795 gce.groupDescription().appId());
Charles Chan188ebf52015-12-23 00:15:11 -08001796 } else {
Yi Tsengef19de12017-04-24 11:33:05 -07001797 groupService.addGroup(gce.groupDescription());
Charles Chan188ebf52015-12-23 00:15:11 -08001798 }
1799 }
1800
Yi Tsengef19de12017-04-24 11:33:05 -07001801 protected void addPendingRemoveNextObjective(NextObjective nextObjective,
1802 List<GroupKey> groupKeys) {
1803 pendingRemoveNextObjectives.put(nextObjective, groupKeys);
Charles Chan188ebf52015-12-23 00:15:11 -08001804 }
1805
Saurav Dasc88d4662017-05-15 15:34:25 -07001806 private void updateFlowObjectiveStore(Integer nextId, OfdpaNextGroup nextGrp) {
1807 synchronized (flowObjectiveStore) {
1808 // get fresh copy of what the store holds
1809 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1810 if (next == null || nextGrp.nextObjective().op() == Operation.ADD) {
1811 flowObjectiveStore.putNextGroup(nextId, nextGrp);
1812 return;
1813 }
1814 if (nextGrp.nextObjective().op() == Operation.ADD_TO_EXISTING) {
1815 List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
Pier Luigi2b442bd2018-01-15 09:48:49 +01001816 allActiveKeys = Lists.newArrayList(allActiveKeys);
Saurav Dasc88d4662017-05-15 15:34:25 -07001817 // If active keys shows only the top-level group without a chain of groups,
1818 // then it represents an empty group. Update by replacing empty chain.
1819 if (allActiveKeys.size() == 1 && allActiveKeys.get(0).size() == 1) {
1820 allActiveKeys.clear();
1821 }
1822 allActiveKeys.addAll(nextGrp.allKeys());
1823 flowObjectiveStore.putNextGroup(nextId,
1824 new OfdpaNextGroup(allActiveKeys, nextGrp.nextObjective()));
1825 }
1826 }
1827 }
1828
Saurav Das8be4e3a2016-03-11 17:19:07 -08001829 private class InnerGroupListener implements GroupListener {
1830 @Override
1831 public void event(GroupEvent event) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001832 switch (event.type()) {
1833 case GROUP_ADDED:
1834 processPendingAddGroupsOrNextObjs(event.subject().appCookie(), true);
1835 break;
1836 case GROUP_REMOVED:
1837 processPendingRemoveNextObjs(event.subject().appCookie());
1838 break;
Yi Tseng78f51f42017-02-02 13:54:58 -08001839 case GROUP_UPDATED:
1840 processPendingUpdateNextObjs(event.subject().appCookie());
1841 break;
Charles Chanfc5c7802016-05-17 13:13:55 -07001842 default:
1843 break;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001844 }
1845 }
1846 }
Charles Chan188ebf52015-12-23 00:15:11 -08001847}